tar 5.0.9 → 6.0.1

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