Skip to content

Commit

Permalink
Merge branch 'mafintosh:master' into typing
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Jul 21, 2023
2 parents edd0a48 + 03da589 commit edbf2e6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
20 changes: 11 additions & 9 deletions constants.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const constants = { // just for envs without fs
S_IFMT: 61440,
S_IFDIR: 16384,
S_IFCHR: 8192,
S_IFBLK: 24576,
S_IFIFO: 4096,
S_IFLNK: 40960
}

try {
module.exports = require('fs').constants
module.exports = require('fs').constants || constants
} catch {
module.exports = { // just for envs without fs
S_IFMT: 61440,
S_IFDIR: 16384,
S_IFCHR: 8192,
S_IFBLK: 24576,
S_IFIFO: 4096,
S_IFLNK: 40960
}
module.exports = constants
}
38 changes: 29 additions & 9 deletions pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const END_OF_TAR = b4a.alloc(1024)

class Sink extends Writable {
constructor (pack, header, callback) {
super({ mapWritable })
super({ mapWritable, eagerOpen: true })

this.written = 0
this.header = header
Expand All @@ -33,6 +33,15 @@ class Sink extends Writable {
if (this._pack._stream === this) this._continueOpen()
}

_continuePack (err) {
if (this._callback === null) return

const callback = this._callback
this._callback = null

callback(err)
}

_continueOpen () {
if (this._pack._stream === null) this._pack._stream = this

Expand All @@ -49,6 +58,11 @@ class Sink extends Writable {
this._pack._encode(this.header)
}

if (this._isVoid) {
this._finish()
this._continuePack(null)
}

cb(null)
}

Expand All @@ -59,29 +73,37 @@ class Sink extends Writable {
}

if (this._isVoid) {
return cb(new Error('No body allowed for this entry'))
if (data.byteLength > 0) {
return cb(new Error('No body allowed for this entry'))
}
return cb()
}

this.written += data.byteLength
if (this._pack.push(data)) return cb()
this._pack._drain = cb
}

_final (cb) {
_finish () {
if (this._finished) return
this._finished = true

if (this._isLinkname) {
this.header.linkname = this._linkname ? b4a.toString(this._linkname, 'utf-8') : ''
this._pack._encode(this.header)
}

overflow(this._pack, this.header.size)

this._pack._done(this)
}

_final (cb) {
if (this.written !== this.header.size) { // corrupting tar
return cb(new Error('Size mismatch'))
}

this._pack._done(this)
this._finished = true

this._finish()
cb(null)
}

Expand All @@ -96,8 +118,7 @@ class Sink extends Writable {
_destroy (cb) {
this._pack._done(this)

if (this._finished) this._callback(null)
else this._callback(this._getError())
this._continuePack(this._finished ? null : this._getError())

cb()
}
Expand Down Expand Up @@ -142,7 +163,6 @@ class Pack extends Readable {
}

if (sink._isVoid) {
sink.end()
return sink
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tar-stream",
"version": "3.1.1",
"version": "3.1.6",
"description": "tar-stream is a streaming tar parser and generator and nothing else. It operates purely using streams which means you can easily extract/parse tarballs without ever hitting the file system.",
"main": "index.js",
"types": "index.d.ts",
Expand All @@ -25,6 +25,7 @@
"homepage": "https://github.com/mafintosh/tar-stream",
"dependencies": {
"b4a": "^1.6.4",
"fast-fifo": "^1.2.0",
"streamx": "^2.15.0"
},
"devDependencies": {
Expand Down
24 changes: 24 additions & 0 deletions test/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,30 @@ test('types', function (t) {
}))
})

test('empty directory body is valid', function (t) {
t.plan(1)

const pack = tar.pack()

pack.entry({
name: 'directory',
mtime: new Date(1387580181000),
type: 'directory',
mode: 0o755,
uname: 'maf',
gname: 'staff',
uid: 501,
gid: 20
}, '')

pack.finalize()

pack.resume()

pack.on('error', () => t.fail('should not throw'))
pack.on('close', () => t.pass('closed'))
})

test('long-name', function (t) {
t.plan(2)

Expand Down

0 comments on commit edbf2e6

Please sign in to comment.