Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7bee13a

Browse files
committedNov 15, 2024·
Refactor: improve error handling in importActorProfile, convert following csv to json format and change ActorProfileOptions from type to interface
1 parent 3e973f9 commit 7bee13a

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed
 

‎src/index.ts

+31-30
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { type Pack } from 'tar-stream'
66
import YAML from 'yaml'
77
import { Readable } from 'stream'
88

9-
export type ActorProfileOptions = {
9+
export interface ActorProfileOptions {
1010
actorProfile?: any
1111
outbox?: any
1212
followers?: any
@@ -113,14 +113,12 @@ export function exportActorProfile({
113113
}
114114

115115
if (followingAccounts) {
116-
// CSV headers:
117-
// Account address, Show boosts, Notify on new posts, Languages
118-
manifest.contents.activitypub.contents['following_accounts.csv'] = {
116+
manifest.contents.activitypub.contents['following.json'] = {
119117
url: 'https://docs.joinmastodon.org/user/moving/#export'
120118
}
121119
pack.entry(
122-
{ name: 'activitypub/following_accounts.csv' },
123-
followingAccounts
120+
{ name: 'activitypub/following.json' },
121+
JSON.stringify(followers, null, 2)
124122
)
125123
}
126124

@@ -161,48 +159,51 @@ export async function importActorProfile(tarBuffer: Buffer): Promise<any> {
161159
const extract = tar.extract()
162160
const result: Record<string, any> = {}
163161

164-
return new Promise((resolve, reject) => {
162+
return await new Promise((resolve, reject) => {
165163
extract.on('entry', (header, stream, next) => {
166164
let content = ''
165+
console.log(`Extracting file: ${header.name}`)
167166

168167
stream.on('data', (chunk) => {
169168
content += chunk.toString()
170169
})
171170

172171
stream.on('end', () => {
173-
// Handle JSON files
174-
if (header.name.endsWith('.json')) {
175-
try {
172+
try {
173+
if (header.name.endsWith('.json')) {
176174
result[header.name] = JSON.parse(content)
177-
} catch (error) {
178-
console.error(`Error parsing JSON from ${header.name}:`, error)
179-
}
180-
}
181-
// Handle YAML files
182-
else if (
183-
header.name.endsWith('.yaml') ||
184-
header.name.endsWith('.yml')
185-
) {
186-
try {
175+
} else if (
176+
header.name.endsWith('.yaml') ||
177+
header.name.endsWith('.yml')
178+
) {
187179
result[header.name] = YAML.parse(content)
188-
} catch (error) {
189-
console.error(`Error parsing YAML from ${header.name}:`, error)
180+
} else if (header.name.endsWith('.csv')) {
181+
result[header.name] = content
190182
}
183+
console.log(`Successfully parsed: ${header.name}`)
184+
} catch (error) {
185+
console.error(`Error processing file ${header.name}:`, error)
186+
reject(error)
191187
}
192-
// Handle CSV files
193-
else if (header.name.endsWith('.csv')) {
194-
result[header.name] = content // Return raw CSV as a string or implement CSV parsing here if needed
195-
}
188+
next()
189+
})
196190

197-
next() // Process the next file in the tar archive
191+
stream.on('error', (error) => {
192+
console.error(`Stream error on file ${header.name}:`, error)
193+
reject(error)
198194
})
195+
})
199196

200-
stream.on('error', (error) => { reject(error); })
197+
extract.on('finish', () => {
198+
console.log('Extraction complete', result)
199+
resolve(result)
201200
})
202201

203-
extract.on('finish', () => { resolve(result); })
202+
extract.on('error', (error) => {
203+
console.error('Error during extraction:', error)
204+
reject(error)
205+
})
204206

205-
// Stream the buffer data into the tar extractor
206207
const stream = Readable.from(tarBuffer)
207208
stream.pipe(extract)
208209
})

0 commit comments

Comments
 (0)
Please sign in to comment.