Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/add-validate-cli-to…
Browse files Browse the repository at this point in the history
…ol' into add-validate-cli-tool
  • Loading branch information
0marSalah committed Feb 17, 2025
2 parents 1666668 + 91e22c1 commit 739f3ff
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export async function exportActorProfile({
* Imports an ActivityPub profile from a .tar archive stream.
* @param tarStream - A ReadableStream containing the .tar archive.
* @param options - Options for the import process.
* @param options - Options for the import process.
* @returns A promise that resolves to the parsed profile data.
*/
export async function importActorProfile(
Expand All @@ -191,7 +192,13 @@ export async function importActorProfile(
console?: Pick<Console, 'log' | 'warn' | 'error'>
onError?: (error: Error, context: { fileName?: string }) => void
} = {}
tarStream: Readable,
options: {
console?: Pick<Console, 'log' | 'warn' | 'error'>
onError?: (error: Error, context: { fileName?: string }) => void
} = {}
): Promise<Record<string, any>> {
const { console = undefined } = options
const { console = undefined } = options
const extract = tar.extract()
const result: Record<string, any> = {}
Expand All @@ -210,6 +217,7 @@ export async function importActorProfile(
return
}

console?.log(`Processing file: ${fileName}`)
console?.log(`Processing file: ${fileName}`)
let content = ''

Expand All @@ -222,12 +230,14 @@ export async function importActorProfile(
if (fileName.endsWith('.json')) {
result[fileName] = JSON.parse(content)
console?.log('Parsed JSON file successfully:', fileName)
console?.log('Parsed JSON file successfully:', fileName)
} else if (fileName.endsWith('.yaml') || fileName.endsWith('.yml')) {
result[fileName] = YAML.parse(content)
} else if (fileName.endsWith('.csv')) {
result[fileName] = content
} else {
console?.warn(`Unsupported file type: ${fileName}, skipping...`)
console?.warn(`Unsupported file type: ${fileName}, skipping...`)
}
} catch (error: any) {
const errorMessage = `Error processing file ${fileName}: ${error.message}`
Expand All @@ -244,20 +254,25 @@ export async function importActorProfile(
stream.on('error', (error: any) => {
console?.error(`Stream error on file ${fileName}:`, error.message)
next(error) // Continue even on stream error
console?.error(`Stream error on file ${fileName}:`, error.message)
next(error) // Continue even on stream error
})
})

extract.on('finish', () => {
console?.log('All files processed successfully.')
console?.log('All files processed successfully.')
resolve(result)
})

extract.on('error', (error) => {
console?.error('Error during tar extraction:', error.message)
console?.error('Error during tar extraction:', error.message)
reject(new Error('Failed to extract tar file.'))
})

tarStream.on('error', (error) => {
console?.error('Error in tar stream:', error.message)
console?.error('Error in tar stream:', error.message)
reject(new Error('Failed to process tar stream.'))
})
Expand Down

0 comments on commit 739f3ff

Please sign in to comment.