Skip to content

Commit

Permalink
Refactor: improve error handling in importActorProfile, convert follo…
Browse files Browse the repository at this point in the history
…wing csv to json format and change ActorProfileOptions from type to interface
  • Loading branch information
0marSalah committed Nov 15, 2024
1 parent 3e973f9 commit 7bee13a
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type Pack } from 'tar-stream'
import YAML from 'yaml'
import { Readable } from 'stream'

export type ActorProfileOptions = {
export interface ActorProfileOptions {
actorProfile?: any
outbox?: any
followers?: any
Expand Down Expand Up @@ -113,14 +113,12 @@ export function exportActorProfile({
}

if (followingAccounts) {
// CSV headers:
// Account address, Show boosts, Notify on new posts, Languages
manifest.contents.activitypub.contents['following_accounts.csv'] = {
manifest.contents.activitypub.contents['following.json'] = {
url: 'https://docs.joinmastodon.org/user/moving/#export'
}
pack.entry(
{ name: 'activitypub/following_accounts.csv' },
followingAccounts
{ name: 'activitypub/following.json' },
JSON.stringify(followers, null, 2)
)
}

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

return new Promise((resolve, reject) => {
return await new Promise((resolve, reject) => {
extract.on('entry', (header, stream, next) => {
let content = ''
console.log(`Extracting file: ${header.name}`)

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

stream.on('end', () => {
// Handle JSON files
if (header.name.endsWith('.json')) {
try {
try {
if (header.name.endsWith('.json')) {
result[header.name] = JSON.parse(content)
} catch (error) {
console.error(`Error parsing JSON from ${header.name}:`, error)
}
}
// Handle YAML files
else if (
header.name.endsWith('.yaml') ||
header.name.endsWith('.yml')
) {
try {
} else if (
header.name.endsWith('.yaml') ||
header.name.endsWith('.yml')
) {
result[header.name] = YAML.parse(content)
} catch (error) {
console.error(`Error parsing YAML from ${header.name}:`, error)
} else if (header.name.endsWith('.csv')) {
result[header.name] = content
}
console.log(`Successfully parsed: ${header.name}`)
} catch (error) {
console.error(`Error processing file ${header.name}:`, error)
reject(error)
}
// Handle CSV files
else if (header.name.endsWith('.csv')) {
result[header.name] = content // Return raw CSV as a string or implement CSV parsing here if needed
}
next()
})

next() // Process the next file in the tar archive
stream.on('error', (error) => {
console.error(`Stream error on file ${header.name}:`, error)
reject(error)
})
})

stream.on('error', (error) => { reject(error); })
extract.on('finish', () => {
console.log('Extraction complete', result)
resolve(result)
})

extract.on('finish', () => { resolve(result); })
extract.on('error', (error) => {
console.error('Error during extraction:', error)
reject(error)
})

// Stream the buffer data into the tar extractor
const stream = Readable.from(tarBuffer)
stream.pipe(extract)
})
Expand Down

0 comments on commit 7bee13a

Please sign in to comment.