Skip to content

Commit 24f173f

Browse files
committedJan 15, 2025
Refactor validateExportStream to accept ReadableStream and improve file validation logic
1 parent d8e9678 commit 24f173f

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed
 

‎src/verify.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
import * as tar from 'tar-stream'
2-
import { Readable } from 'stream'
2+
import { type Readable } from 'stream'
33
import YAML from 'yaml'
44

55
/**
66
* Validates the structure and content of an exported ActivityPub tarball.
7-
* @param tarBuffer - A Buffer containing the .tar archive.
7+
* @param tarStream - A ReadableStream containing the .tar archive.
88
* @returns A promise that resolves to an object with `valid` (boolean) and `errors` (string[]).
99
*/
1010
export async function validateExportStream(
11-
tarBuffer: Buffer
11+
tarStream: Readable
1212
): Promise<{ valid: boolean; errors: string[] }> {
13+
console.log('Validating export stream...')
14+
console.log('length of tarStream: ', tarStream)
1315
const extract = tar.extract()
1416
const errors: string[] = []
1517
const requiredFiles = [
16-
'manifest.yaml',
18+
'manifest.yaml', // or 'manifest.yml'
1719
'activitypub/actor.json',
1820
'activitypub/outbox.json'
19-
]
20-
const foundFiles = new Set()
21+
].map((file) => file.toLowerCase()) // Normalize to lowercase for consistent comparison
22+
const foundFiles = new Set<string>()
2123

2224
return await new Promise((resolve) => {
2325
extract.on('entry', (header, stream, next) => {
24-
const fileName = header.name
26+
const fileName = header.name.toLowerCase() // Normalize file name
27+
console.log(`Processing file: ${fileName}`) // Log the file name
2528
foundFiles.add(fileName)
2629

2730
let content = ''
@@ -37,7 +40,7 @@ export async function validateExportStream(
3740
}
3841

3942
// Validate manifest file
40-
if (fileName === 'manifest.yaml') {
43+
if (fileName === 'manifest.yaml' || fileName === 'manifest.yml') {
4144
const manifest = YAML.parse(content)
4245
if (!manifest['ubc-version']) {
4346
errors.push('Manifest is missing required field: ubc-version')
@@ -61,6 +64,9 @@ export async function validateExportStream(
6164
})
6265

6366
extract.on('finish', () => {
67+
console.log('Found files:', Array.from(foundFiles)) // Debug log
68+
console.log('Required files:', requiredFiles) // Debug log
69+
6470
// Check if all required files are present
6571
for (const file of requiredFiles) {
6672
if (!foundFiles.has(file)) {
@@ -82,8 +88,7 @@ export async function validateExportStream(
8288
})
8389
})
8490

85-
// Convert Buffer to a Readable stream and pipe it to the extractor
86-
const stream = Readable.from(tarBuffer)
87-
stream.pipe(extract)
91+
// Pipe the ReadableStream into the extractor
92+
tarStream.pipe(extract)
8893
})
8994
}

0 commit comments

Comments
 (0)
Please sign in to comment.