tar 5.0.5 → 5.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,8 +5,16 @@ const Header = require('./header.js')
5
5
  const ReadEntry = require('./read-entry.js')
6
6
  const fs = require('fs')
7
7
  const path = require('path')
8
+ const normPath = require('./normalize-windows-path.js')
9
+ const stripSlash = require('./strip-trailing-slashes.js')
10
+
11
+ const prefixPath = (path, prefix) => {
12
+ if (!prefix)
13
+ return path
14
+ path = normPath(path).replace(/^\.(\/|$)/, '')
15
+ return stripSlash(prefix) + '/' + path
16
+ }
8
17
 
9
- const types = require('./types.js')
10
18
  const maxReadSize = 16 * 1024 * 1024
11
19
  const PROCESS = Symbol('process')
12
20
  const FILE = Symbol('file')
@@ -23,8 +31,13 @@ const OPENFILE = Symbol('openfile')
23
31
  const ONOPENFILE = Symbol('onopenfile')
24
32
  const CLOSE = Symbol('close')
25
33
  const MODE = Symbol('mode')
34
+ const AWAITDRAIN = Symbol('awaitDrain')
35
+ const ONDRAIN = Symbol('ondrain')
36
+ const PREFIX = Symbol('prefix')
37
+ const HAD_ERROR = Symbol('hadError')
26
38
  const warner = require('./warn-mixin.js')
27
39
  const winchars = require('./winchars.js')
40
+ const stripAbsolutePath = require('./strip-absolute-path.js')
28
41
 
29
42
  const modeFix = require('./mode-fix.js')
30
43
 
@@ -34,41 +47,53 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
34
47
  super(opt)
35
48
  if (typeof p !== 'string')
36
49
  throw new TypeError('path is required')
37
- this.path = p
50
+ this.path = normPath(p)
38
51
  // suppress atime, ctime, uid, gid, uname, gname
39
52
  this.portable = !!opt.portable
40
53
  // until node has builtin pwnam functions, this'll have to do
41
- this.myuid = process.getuid && process.getuid()
54
+ this.myuid = process.getuid && process.getuid() || 0
42
55
  this.myuser = process.env.USER || ''
43
56
  this.maxReadSize = opt.maxReadSize || maxReadSize
44
57
  this.linkCache = opt.linkCache || new Map()
45
58
  this.statCache = opt.statCache || new Map()
46
59
  this.preservePaths = !!opt.preservePaths
47
- this.cwd = opt.cwd || process.cwd()
60
+ this.cwd = normPath(opt.cwd || process.cwd())
48
61
  this.strict = !!opt.strict
49
62
  this.noPax = !!opt.noPax
50
63
  this.noMtime = !!opt.noMtime
51
64
  this.mtime = opt.mtime || null
65
+ this.prefix = opt.prefix ? normPath(opt.prefix) : null
66
+
67
+ this.fd = null
68
+ this.blockLen = null
69
+ this.blockRemain = null
70
+ this.buf = null
71
+ this.offset = null
72
+ this.length = null
73
+ this.pos = null
74
+ this.remain = null
52
75
 
53
76
  if (typeof opt.onwarn === 'function')
54
77
  this.on('warn', opt.onwarn)
55
78
 
56
79
  let pathWarn = false
57
- if (!this.preservePaths && path.win32.isAbsolute(p)) {
58
- // absolutes on posix are also absolutes on win32
59
- // so we only need to test this one to get both
60
- const parsed = path.win32.parse(p)
61
- this.path = p.substr(parsed.root.length)
62
- pathWarn = parsed.root
80
+ if (!this.preservePaths) {
81
+ const [root, stripped] = stripAbsolutePath(this.path)
82
+ if (root) {
83
+ this.path = stripped
84
+ pathWarn = root
85
+ }
63
86
  }
64
87
 
65
88
  this.win32 = !!opt.win32 || process.platform === 'win32'
66
89
  if (this.win32) {
90
+ // force the \ to / normalization, since we might not *actually*
91
+ // be on windows, but want \ to be considered a path separator.
67
92
  this.path = winchars.decode(this.path.replace(/\\/g, '/'))
68
93
  p = p.replace(/\\/g, '/')
69
94
  }
70
95
 
71
- this.absolute = opt.absolute || path.resolve(this.cwd, p)
96
+ this.absolute = normPath(opt.absolute || path.resolve(this.cwd, p))
72
97
 
73
98
  if (this.path === '')
74
99
  this.path = './'
@@ -86,6 +111,12 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
86
111
  this[LSTAT]()
87
112
  }
88
113
 
114
+ emit (ev, ...data) {
115
+ if (ev === 'error')
116
+ this[HAD_ERROR] = true
117
+ return super.emit(ev, ...data)
118
+ }
119
+
89
120
  [LSTAT] () {
90
121
  fs.lstat(this.absolute, (er, stat) => {
91
122
  if (er)
@@ -118,13 +149,19 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
118
149
  return modeFix(mode, this.type === 'Directory', this.portable)
119
150
  }
120
151
 
152
+ [PREFIX] (path) {
153
+ return prefixPath(path, this.prefix)
154
+ }
155
+
121
156
  [HEADER] () {
122
157
  if (this.type === 'Directory' && this.portable)
123
158
  this.noMtime = true
124
159
 
125
160
  this.header = new Header({
126
- path: this.path,
127
- linkpath: this.linkpath,
161
+ path: this[PREFIX](this.path),
162
+ // only apply the prefix to hard links.
163
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
164
+ : this.linkpath,
128
165
  // only the permissions and setuid/setgid/sticky bitflags
129
166
  // not the higher-order bits that specify file type
130
167
  mode: this[MODE](this.stat.mode),
@@ -139,14 +176,15 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
139
176
  ctime: this.portable ? null : this.stat.ctime
140
177
  })
141
178
 
142
- if (this.header.encode() && !this.noPax)
143
- this.write(new Pax({
179
+ if (this.header.encode() && !this.noPax) {
180
+ super.write(new Pax({
144
181
  atime: this.portable ? null : this.header.atime,
145
182
  ctime: this.portable ? null : this.header.ctime,
146
183
  gid: this.portable ? null : this.header.gid,
147
184
  mtime: this.noMtime ? null : this.mtime || this.header.mtime,
148
- path: this.path,
149
- linkpath: this.linkpath,
185
+ path: this[PREFIX](this.path),
186
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
187
+ : this.linkpath,
150
188
  size: this.header.size,
151
189
  uid: this.portable ? null : this.header.uid,
152
190
  uname: this.portable ? null : this.header.uname,
@@ -154,7 +192,8 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
154
192
  ino: this.portable ? null : this.stat.ino,
155
193
  nlink: this.portable ? null : this.stat.nlink
156
194
  }).encode())
157
- this.write(this.header.block)
195
+ }
196
+ super.write(this.header.block)
158
197
  }
159
198
 
160
199
  [DIRECTORY] () {
@@ -174,14 +213,14 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
174
213
  }
175
214
 
176
215
  [ONREADLINK] (linkpath) {
177
- this.linkpath = linkpath
216
+ this.linkpath = normPath(linkpath)
178
217
  this[HEADER]()
179
218
  this.end()
180
219
  }
181
220
 
182
221
  [HARDLINK] (linkpath) {
183
222
  this.type = 'Link'
184
- this.linkpath = path.relative(this.cwd, linkpath)
223
+ this.linkpath = normPath(path.relative(this.cwd, linkpath))
185
224
  this.stat.size = 0
186
225
  this[HEADER]()
187
226
  this.end()
@@ -214,74 +253,110 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
214
253
  }
215
254
 
216
255
  [ONOPENFILE] (fd) {
217
- const blockLen = 512 * Math.ceil(this.stat.size / 512)
218
- const bufLen = Math.min(blockLen, this.maxReadSize)
219
- const buf = Buffer.allocUnsafe(bufLen)
220
- this[READ](fd, buf, 0, buf.length, 0, this.stat.size, blockLen)
256
+ this.fd = fd
257
+ if (this[HAD_ERROR])
258
+ return this[CLOSE]()
259
+
260
+ this.blockLen = 512 * Math.ceil(this.stat.size / 512)
261
+ this.blockRemain = this.blockLen
262
+ const bufLen = Math.min(this.blockLen, this.maxReadSize)
263
+ this.buf = Buffer.allocUnsafe(bufLen)
264
+ this.offset = 0
265
+ this.pos = 0
266
+ this.remain = this.stat.size
267
+ this.length = this.buf.length
268
+ this[READ]()
221
269
  }
222
270
 
223
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
271
+ [READ] () {
272
+ const { fd, buf, offset, length, pos } = this
224
273
  fs.read(fd, buf, offset, length, pos, (er, bytesRead) => {
225
274
  if (er) {
226
275
  // ignoring the error from close(2) is a bad practice, but at
227
276
  // this point we already have an error, don't need another one
228
- return this[CLOSE](fd, () => this.emit('error', er))
277
+ return this[CLOSE](() => this.emit('error', er))
229
278
  }
230
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
279
+ this[ONREAD](bytesRead)
231
280
  })
232
281
  }
233
282
 
234
- [CLOSE] (fd, cb) {
235
- fs.close(fd, cb)
283
+ [CLOSE] (cb) {
284
+ fs.close(this.fd, cb)
236
285
  }
237
286
 
238
- [ONREAD] (fd, buf, offset, length, pos, remain, blockRemain, bytesRead) {
239
- if (bytesRead <= 0 && remain > 0) {
287
+ [ONREAD] (bytesRead) {
288
+ if (bytesRead <= 0 && this.remain > 0) {
240
289
  const er = new Error('encountered unexpected EOF')
241
290
  er.path = this.absolute
242
291
  er.syscall = 'read'
243
292
  er.code = 'EOF'
244
- return this[CLOSE](fd, () => this.emit('error', er))
293
+ return this[CLOSE](() => this.emit('error', er))
245
294
  }
246
295
 
247
- if (bytesRead > remain) {
296
+ if (bytesRead > this.remain) {
248
297
  const er = new Error('did not encounter expected EOF')
249
298
  er.path = this.absolute
250
299
  er.syscall = 'read'
251
300
  er.code = 'EOF'
252
- return this[CLOSE](fd, () => this.emit('error', er))
301
+ return this[CLOSE](() => this.emit('error', er))
253
302
  }
254
303
 
255
304
  // null out the rest of the buffer, if we could fit the block padding
256
- if (bytesRead === remain) {
257
- for (let i = bytesRead; i < length && bytesRead < blockRemain; i++) {
258
- buf[i + offset] = 0
259
- bytesRead ++
260
- remain ++
305
+ // at the end of this loop, we've incremented bytesRead and this.remain
306
+ // to be incremented up to the blockRemain level, as if we had expected
307
+ // to get a null-padded file, and read it until the end. then we will
308
+ // decrement both remain and blockRemain by bytesRead, and know that we
309
+ // reached the expected EOF, without any null buffer to append.
310
+ if (bytesRead === this.remain) {
311
+ for (let i = bytesRead; i < this.length && bytesRead < this.blockRemain; i++) {
312
+ this.buf[i + this.offset] = 0
313
+ bytesRead++
314
+ this.remain++
261
315
  }
262
316
  }
263
317
 
264
- const writeBuf = offset === 0 && bytesRead === buf.length ?
265
- buf : buf.slice(offset, offset + bytesRead)
266
- remain -= bytesRead
267
- blockRemain -= bytesRead
268
- pos += bytesRead
269
- offset += bytesRead
318
+ const writeBuf = this.offset === 0 && bytesRead === this.buf.length ?
319
+ this.buf : this.buf.slice(this.offset, this.offset + bytesRead)
270
320
 
271
- this.write(writeBuf)
321
+ const flushed = this.write(writeBuf)
322
+ if (!flushed)
323
+ this[AWAITDRAIN](() => this[ONDRAIN]())
324
+ else
325
+ this[ONDRAIN]()
326
+ }
272
327
 
273
- if (!remain) {
274
- if (blockRemain)
275
- this.write(Buffer.alloc(blockRemain))
276
- return this[CLOSE](fd, er => er ? this.emit('error', er) : this.end())
328
+ [AWAITDRAIN] (cb) {
329
+ this.once('drain', cb)
330
+ }
331
+
332
+ write (writeBuf) {
333
+ if (this.blockRemain < writeBuf.length) {
334
+ const er = new Error('writing more data than expected')
335
+ er.path = this.absolute
336
+ return this.emit('error', er)
277
337
  }
338
+ this.remain -= writeBuf.length
339
+ this.blockRemain -= writeBuf.length
340
+ this.pos += writeBuf.length
341
+ this.offset += writeBuf.length
342
+ return super.write(writeBuf)
343
+ }
278
344
 
279
- if (offset >= length) {
280
- buf = Buffer.allocUnsafe(length)
281
- offset = 0
345
+ [ONDRAIN] () {
346
+ if (!this.remain) {
347
+ if (this.blockRemain)
348
+ super.write(Buffer.alloc(this.blockRemain))
349
+ return this[CLOSE](er => er ? this.emit('error', er) : this.end())
282
350
  }
283
- length = buf.length - offset
284
- this[READ](fd, buf, offset, length, pos, remain, blockRemain)
351
+
352
+ if (this.offset >= this.length) {
353
+ // if we only have a smaller bit left to read, alloc a smaller buffer
354
+ // otherwise, keep it the same length it was before.
355
+ this.buf = Buffer.allocUnsafe(Math.min(this.blockRemain, this.buf.length))
356
+ this.offset = 0
357
+ }
358
+ this.length = this.buf.length - this.offset
359
+ this[READ]()
285
360
  }
286
361
  })
287
362
 
@@ -302,22 +377,30 @@ class WriteEntrySync extends WriteEntry {
302
377
  this[ONOPENFILE](fs.openSync(this.absolute, 'r'))
303
378
  }
304
379
 
305
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
380
+ [READ] () {
306
381
  let threw = true
307
382
  try {
383
+ const { fd, buf, offset, length, pos } = this
308
384
  const bytesRead = fs.readSync(fd, buf, offset, length, pos)
309
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
385
+ this[ONREAD](bytesRead)
310
386
  threw = false
311
387
  } finally {
312
388
  // ignoring the error from close(2) is a bad practice, but at
313
389
  // this point we already have an error, don't need another one
314
- if (threw)
315
- try { this[CLOSE](fd, () => {}) } catch (er) {}
390
+ if (threw) {
391
+ try {
392
+ this[CLOSE](() => {})
393
+ } catch (er) {}
394
+ }
316
395
  }
317
396
  }
318
397
 
319
- [CLOSE] (fd, cb) {
320
- fs.closeSync(fd)
398
+ [AWAITDRAIN] (cb) {
399
+ cb()
400
+ }
401
+
402
+ [CLOSE] (cb) {
403
+ fs.closeSync(this.fd)
321
404
  cb()
322
405
  }
323
406
  }
@@ -337,7 +420,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
337
420
  if (this.type === 'Directory' && this.portable)
338
421
  this.noMtime = true
339
422
 
340
- this.path = readEntry.path
423
+ this.prefix = opt.prefix || null
424
+
425
+ this.path = normPath(readEntry.path)
341
426
  this.mode = this[MODE](readEntry.mode)
342
427
  this.uid = this.portable ? null : readEntry.uid
343
428
  this.gid = this.portable ? null : readEntry.gid
@@ -347,24 +432,27 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
347
432
  this.mtime = this.noMtime ? null : opt.mtime || readEntry.mtime
348
433
  this.atime = this.portable ? null : readEntry.atime
349
434
  this.ctime = this.portable ? null : readEntry.ctime
350
- this.linkpath = readEntry.linkpath
435
+ this.linkpath = normPath(readEntry.linkpath)
351
436
 
352
437
  if (typeof opt.onwarn === 'function')
353
438
  this.on('warn', opt.onwarn)
354
439
 
355
440
  let pathWarn = false
356
- if (path.isAbsolute(this.path) && !this.preservePaths) {
357
- const parsed = path.parse(this.path)
358
- pathWarn = parsed.root
359
- this.path = this.path.substr(parsed.root.length)
441
+ if (!this.preservePaths) {
442
+ const [root, stripped] = stripAbsolutePath(this.path)
443
+ if (root) {
444
+ this.path = stripped
445
+ pathWarn = root
446
+ }
360
447
  }
361
448
 
362
449
  this.remain = readEntry.size
363
450
  this.blockRemain = readEntry.startBlockSize
364
451
 
365
452
  this.header = new Header({
366
- path: this.path,
367
- linkpath: this.linkpath,
453
+ path: this[PREFIX](this.path),
454
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
455
+ : this.linkpath,
368
456
  // only the permissions and setuid/setgid/sticky bitflags
369
457
  // not the higher-order bits that specify file type
370
458
  mode: this.mode,
@@ -391,8 +479,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
391
479
  ctime: this.portable ? null : this.ctime,
392
480
  gid: this.portable ? null : this.gid,
393
481
  mtime: this.noMtime ? null : this.mtime,
394
- path: this.path,
395
- linkpath: this.linkpath,
482
+ path: this[PREFIX](this.path),
483
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
484
+ : this.linkpath,
396
485
  size: this.size,
397
486
  uid: this.portable ? null : this.uid,
398
487
  uname: this.portable ? null : this.uname,
@@ -405,6 +494,10 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
405
494
  readEntry.pipe(this)
406
495
  }
407
496
 
497
+ [PREFIX] (path) {
498
+ return prefixPath(path, this.prefix)
499
+ }
500
+
408
501
  [MODE] (mode) {
409
502
  return modeFix(mode, this.type === 'Directory', this.portable)
410
503
  }
@@ -419,7 +512,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
419
512
 
420
513
  end () {
421
514
  if (this.blockRemain)
422
- this.write(Buffer.alloc(this.blockRemain))
515
+ super.write(Buffer.alloc(this.blockRemain))
423
516
  return super.end()
424
517
  }
425
518
  })
package/package.json CHANGED
@@ -2,13 +2,18 @@
2
2
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
3
3
  "name": "tar",
4
4
  "description": "tar for node",
5
- "version": "5.0.5",
5
+ "version": "5.0.9",
6
+ "publishConfig": {
7
+ "tag": "v5-legacy"
8
+ },
6
9
  "repository": {
7
10
  "type": "git",
8
11
  "url": "https://github.com/npm/node-tar.git"
9
12
  },
10
13
  "scripts": {
11
- "test": "tap",
14
+ "test:posix": "tap",
15
+ "test:win32": "tap --lines=98 --branches=98 --statements=98 --functions=98",
16
+ "test": "node test/fixtures/test.js",
12
17
  "preversion": "npm test",
13
18
  "postversion": "npm publish",
14
19
  "postpublish": "git push origin --follow-tags",
@@ -16,20 +21,21 @@
16
21
  "bench": "for i in benchmarks/*/*.js; do echo $i; for j in {1..5}; do node $i || break; done; done"
17
22
  },
18
23
  "dependencies": {
19
- "chownr": "^1.1.3",
20
- "fs-minipass": "^2.0.0",
21
- "minipass": "^3.0.0",
22
- "minizlib": "^2.1.0",
23
- "mkdirp": "^0.5.0",
24
+ "chownr": "^1.1.4",
25
+ "fs-minipass": "^2.1.0",
26
+ "minipass": "^3.1.3",
27
+ "minizlib": "^2.1.2",
28
+ "mkdirp": "^0.5.5",
24
29
  "yallist": "^4.0.0"
25
30
  },
26
31
  "devDependencies": {
27
32
  "chmodr": "^1.2.0",
28
- "end-of-stream": "^1.4.3",
33
+ "end-of-stream": "^1.4.4",
29
34
  "events-to-array": "^1.1.2",
30
35
  "mutate-fs": "^2.1.1",
36
+ "require-inject": "^1.4.4",
31
37
  "rimraf": "^2.7.1",
32
- "tap": "^14.6.9",
38
+ "tap": "^14.11.0",
33
39
  "tar-fs": "^1.16.3",
34
40
  "tar-stream": "^1.6.2"
35
41
  },