Skip to content

Commit

Permalink
Refactor validateExportStream to accept ReadableStream and improve fi…
Browse files Browse the repository at this point in the history
…le validation logic
  • Loading branch information
0marSalah committed Jan 15, 2025
1 parent d8e9678 commit 24f173f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/verify.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import * as tar from 'tar-stream'
import { Readable } from 'stream'
import { type Readable } from 'stream'
import YAML from 'yaml'

/**
* Validates the structure and content of an exported ActivityPub tarball.
* @param tarBuffer - A Buffer containing the .tar archive.
* @param tarStream - A ReadableStream containing the .tar archive.
* @returns A promise that resolves to an object with `valid` (boolean) and `errors` (string[]).
*/
export async function validateExportStream(
tarBuffer: Buffer
tarStream: Readable
): Promise<{ valid: boolean; errors: string[] }> {
console.log('Validating export stream...')
console.log('length of tarStream: ', tarStream)
const extract = tar.extract()
const errors: string[] = []
const requiredFiles = [
'manifest.yaml',
'manifest.yaml', // or 'manifest.yml'
'activitypub/actor.json',
'activitypub/outbox.json'
]
const foundFiles = new Set()
].map((file) => file.toLowerCase()) // Normalize to lowercase for consistent comparison
const foundFiles = new Set<string>()

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

let content = ''
Expand All @@ -37,7 +40,7 @@ export async function validateExportStream(
}

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

extract.on('finish', () => {
console.log('Found files:', Array.from(foundFiles)) // Debug log
console.log('Required files:', requiredFiles) // Debug log

// Check if all required files are present
for (const file of requiredFiles) {
if (!foundFiles.has(file)) {
Expand All @@ -82,8 +88,7 @@ export async function validateExportStream(
})
})

// Convert Buffer to a Readable stream and pipe it to the extractor
const stream = Readable.from(tarBuffer)
stream.pipe(extract)
// Pipe the ReadableStream into the extractor
tarStream.pipe(extract)
})
}

0 comments on commit 24f173f

Please sign in to comment.