Skip to content

Commit

Permalink
fix: ensuring close event is emited after stream has ended
Browse files Browse the repository at this point in the history
For node 18 combaitibility fixes #321
  • Loading branch information
webark authored and lukekarrys committed Oct 27, 2022
1 parent 1e3fadf commit 57493ee
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/parse.js
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
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
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
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

0 comments on commit 57493ee

Please sign in to comment.