Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: ensuring close event is emited after stream has ended
For node 18 combaitibility fixes #321
  • Loading branch information
webark committed Oct 13, 2022
commit 096dc4d51d32d9bc2663579f97d93589253bd03f
10 changes: 9 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const maxMetaEntrySize = 1024 * 1024
const Entry = require('./read-entry.js')
const Pax = require('./pax.js')
const zlib = require('minizlib')
const { nextTick } = require('process')

const gzipHeader = Buffer.from([0x1f, 0x8b])
const STATE = Symbol('state')
Expand Down Expand Up @@ -59,6 +60,7 @@ const DONE = Symbol('onDone')
const SAW_VALID_ENTRY = Symbol('sawValidEntry')
const SAW_NULL_BLOCK = Symbol('sawNullBlock')
const SAW_EOF = Symbol('sawEOF')
const CLOSESTREAM = Symbol('closeStream')

const noop = _ => true

Expand Down Expand Up @@ -89,7 +91,6 @@ module.exports = warner(class Parser extends EE {
this.emit('prefinish')
this.emit('finish')
this.emit('end')
this.emit('close')
})
}

Expand All @@ -114,6 +115,9 @@ module.exports = warner(class Parser extends EE {
this[ABORTED] = false
this[SAW_NULL_BLOCK] = false
this[SAW_EOF] = false

this.on('end', () => this[CLOSESTREAM]())

if (typeof opt.onwarn === 'function') {
this.on('warn', opt.onwarn)
}
Expand Down Expand Up @@ -217,6 +221,10 @@ module.exports = warner(class Parser extends EE {
}
}

[CLOSESTREAM] () {
nextTick(() => this.emit('close'))
}

[PROCESSENTRY] (entry) {
let go = true

Expand Down
1 change: 0 additions & 1 deletion lib/unpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ class Unpack extends Parser {
this.emit('prefinish')
this.emit('finish')
this.emit('end')
this.emit('close')
}
}

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
"events-to-array": "^1.1.2",
"mutate-fs": "^2.1.1",
"rimraf": "^3.0.2",
"tap": "^16.0.1",
"tar-fs": "^2.1.1",
"tar-stream": "^2.2.0"
"tap": "^16.0.1"
},
"license": "ISC",
"engines": {
Expand Down
28 changes: 28 additions & 0 deletions test/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const mkdirp = require('mkdirp')
const { promisify } = require('util')
const rimraf = promisify(require('rimraf'))
const mutateFS = require('mutate-fs')
const pipeline = promisify(require('stream').pipeline)
const https = require('https')

t.teardown(_ => rimraf(extractdir))

Expand Down Expand Up @@ -54,6 +56,32 @@ t.test('basic extracting', t => {
t.end()
})

t.test('ensure an open stream is not prematuraly closed', t => {
const dir = path.resolve(extractdir, 'basic-with-stream')

t.beforeEach(async () => {
await rimraf(dir)
await mkdirp(dir)
})

const check = async t => {
fs.lstatSync(dir + '/node-tar-main/LICENSE')
await rimraf(dir)
t.end()
}

t.test('async promisey', t => {
https.get('https://codeload.github.com/npm/node-tar/tar.gz/main', (stream) => {
return pipeline(
stream,
x({ cwd: dir }, ['node-tar-main/LICENSE'])
).then(_ => check(t))
})
})

t.end()
})

t.test('file list and filter', t => {
const file = path.resolve(tars, 'utf8.tar')
const dir = path.resolve(extractdir, 'filter')
Expand Down