All files / src/utils tar-writer.ts

92.85% Statements 13/14
75% Branches 3/4
100% Functions 1/1
92.85% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2533x       33x 6x 10x   10x 3x   3x 3x   3x 3x 7x 7x 7x            
import { createReadStream } from 'fs'
import { Collection } from '..'
import { TarStream } from './tar'
 
export async function writeTar(collection: Collection, tarStream: TarStream) {
  for (const item of collection) {
    tarStream.beginFile(item.path, item.size)
 
    if (item.fsPath) {
      const stream = createReadStream(item.fsPath)
 
      for await (const chunk of stream) {
        await tarStream.appendFile(chunk)
      }
      await tarStream.endFile()
      stream.close()
    } else if (item.file) {
      await tarStream.appendFile(new Uint8Array(await item.file.arrayBuffer()))
      await tarStream.endFile()
    } else E{
      throw new Error('Invalid collection item')
    }
  }
}