Skip to content

Commit 0fbf1d4

Browse files
0marSalahdmitrizagidulin
authored andcommittedFeb 8, 2025·
Enhance importActorProfile function to normalize file names, skip system-generated files, and improve error handling
1 parent 3350476 commit 0fbf1d4

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed
 

‎src/index.ts

+32-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as tar from 'tar-stream'
55
import { type Pack } from 'tar-stream'
66
import YAML from 'yaml'
77
import { type Readable } from 'stream'
8+
import path from 'path'
89

910
export interface ActorProfileOptions {
1011
actorProfile?: any
@@ -186,12 +187,26 @@ export async function exportActorProfile({
186187
export async function importActorProfile(
187188
tarStream: Readable
188189
): Promise<Record<string, any>> {
190+
console.log('🚀 Starting to process tar stream...')
189191
const extract = tar.extract()
190192
const result: Record<string, any> = {}
191193

192194
return await new Promise((resolve, reject) => {
193195
extract.on('entry', (header, stream, next) => {
194-
const fileName = header.name
196+
// Normalize fileName to include only `activitypub/filename`
197+
const originalFileName = header.name
198+
const fileName = `activitypub/${path.basename(originalFileName)}`
199+
200+
// Skip system-generated files
201+
if (
202+
fileName.startsWith('activitypub/._') ||
203+
fileName.endsWith('.DS_Store')
204+
) {
205+
console.warn(`Skipping system-generated file: ${fileName}`)
206+
next()
207+
}
208+
209+
console.log(`Processing file: ${fileName}`)
195210
let content = ''
196211

197212
stream.on('data', (chunk) => {
@@ -202,31 +217,42 @@ export async function importActorProfile(
202217
try {
203218
if (fileName.endsWith('.json')) {
204219
result[fileName] = JSON.parse(content)
220+
console.log('Parsed JSON file successfully:', fileName)
205221
} else if (fileName.endsWith('.yaml') || fileName.endsWith('.yml')) {
206222
result[fileName] = YAML.parse(content)
207223
} else if (fileName.endsWith('.csv')) {
208224
result[fileName] = content
225+
} else {
226+
console.warn(`Unsupported file type: ${fileName}, skipping...`)
209227
}
210228
} catch (error: any) {
211-
reject(new Error(`Error processing file ${fileName}: ${error}`))
229+
console.error(`Error processing file ${fileName}:`, error.message)
230+
} finally {
231+
next() // Always continue
212232
}
213-
next()
214233
})
215234

216235
stream.on('error', (error: any) => {
217-
reject(new Error(`Stream error on file ${fileName}: ${error}`))
236+
console.error(`Stream error on file ${fileName}:`, error.message)
237+
next() // Continue even on stream error
218238
})
219239
})
220240

221241
extract.on('finish', () => {
242+
console.log('All files processed successfully.')
222243
resolve(result)
223244
})
224245

225246
extract.on('error', (error) => {
226-
reject(new Error(`Error during extraction: ${error}`))
247+
console.error('Error during tar extraction:', error.message)
248+
reject(new Error('Failed to extract tar file.'))
249+
})
250+
251+
tarStream.on('error', (error) => {
252+
console.error('Error in tar stream:', error.message)
253+
reject(new Error('Failed to process tar stream.'))
227254
})
228255

229-
// Pipe the ReadableStream into the extractor
230256
tarStream.pipe(extract)
231257
})
232258
}

0 commit comments

Comments
 (0)
Please sign in to comment.