Skip to content

tar entry destroyed when piping extract to pack #167

@mstimvol

Description

@mstimvol

Hi, I'm trying to "merge" multiple tar archives to one big archive using the tar-stream library and return the merged archive using fastify. My problem is, while merging the archives I'm getting the following error:

node:events:498
      throw er; // Unhandled 'error' event
      ^

Error: tar entry destroyed
    at Sink._getError (C:\app\node_modules\tar-stream\pack.js:111:36)
    at Sink._destroy (C:\app\node_modules\tar-stream\pack.js:121:53)
    at WritableState.updateNonPrimary (C:\app\node_modules\streamx\index.js:208:16)
    at WritableState.update (C:\app\node_modules\streamx\index.js:190:72)
    at WritableState.updateWriteNT (C:\app\node_modules\streamx\index.js:550:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
Emitted 'error' event on Extract instance at:
    at emitErrorNT (node:internal/streams/destroy:170:8)
    at errorOrDestroy (node:internal/streams/destroy:239:7)
    at Extract.onerror (node:internal/streams/readable:1024:9)
    at Extract.emit (node:events:520:28)
    at WritableState.afterDestroy (C:\app\node_modules\streamx\index.js:500:19)
    at Extract._destroy (C:\app\node_modules\tar-stream\extract.js:301:5)
    at WritableState.updateNonPrimary (C:\app\node_modules\streamx\index.js:208:16)
    [... lines matching original stack trace ...]
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)

My source is the following:

const axios = require('axios')
const tar = require('tar-stream')

const { ReadableStream } = require('node:stream/web')

function addArchive (pack, archive) {
  const extract = tar.extract()
  archive.pipe(extract)

  return new Promise(resolve => {
    extract.on('entry', function (header, stream, callback) {
      if (header.type !== 'file') {
        callback()
      }

      stream.pipe(pack.entry(header, callback))
    })

    extract.on('finish', resolve)
  })
}

async function handleRoute(req, reply) {
  const pack = tar.pack()
  const stream = ReadableStream.from(pack)

  reply.send(stream)

  const a1 = await axios.get('https://someurl/archive1.tar', {
    responseType: 'stream'
  })

  await addArchive(pack, a1.data)

  const a2 = await axios.get('https://someurl/archive1.tar', {
    responseType: 'stream'
  })

  await addArchive(pack, a2.data)

  pack.finalize()
}

I was already checking out #24 because it's very similar, but unfortunately I wasn't able to find a solution for my problem... Any ideas how I can solve the issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions