tar 0.1.17 → 1.0.0

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.
package/README.md CHANGED
@@ -2,49 +2,47 @@
2
2
 
3
3
  Tar for Node.js.
4
4
 
5
- ## Goals of this project
5
+ [![NPM](https://nodei.co/npm/tar.png)](https://nodei.co/npm/tar/)
6
6
 
7
- 1. Be able to parse and reasonably extract the contents of any tar file
8
- created by any program that creates tar files, period.
7
+ ## API
9
8
 
10
- At least, this includes every version of:
9
+ See `examples/` for usage examples.
11
10
 
12
- * bsdtar
13
- * gnutar
14
- * solaris posix tar
15
- * Joerg Schilling's star ("Schilly tar")
11
+ ### var tar = require('tar')
16
12
 
17
- 2. Create tar files that can be extracted by any of the following tar
18
- programs:
13
+ Returns an object with `.Pack`, `.Extract` and `.Parse` methods.
19
14
 
20
- * bsdtar/libarchive version 2.6.2
21
- * gnutar 1.15 and above
22
- * SunOS Posix tar
23
- * Joerg Schilling's star ("Schilly tar")
15
+ ### tar.Pack([properties])
24
16
 
25
- 3. 100% test coverage. Speed is important. Correctness is slightly
26
- more important.
17
+ Returns a through stream. Use
18
+ [fstream](https://npmjs.org/package/fstream) to write files into the
19
+ pack stream and you will receive tar archive data from the pack
20
+ stream.
27
21
 
28
- 4. Create the kind of tar interface that Node users would want to use.
22
+ This only works with directories, it does not work with individual files.
29
23
 
30
- 5. Satisfy npm's needs for a portable tar implementation with a
31
- JavaScript interface.
24
+ The optional `properties` object are used to set properties in the tar
25
+ 'Global Extended Header'.
32
26
 
33
- 6. No excuses. No complaining. No tolerance for failure.
27
+ ### tar.Extract([options])
34
28
 
35
- ## But isn't there already a tar.js?
29
+ Returns a through stream. Write tar data to the stream and the files
30
+ in the tarball will be extracted onto the filesystem.
36
31
 
37
- Yes, there are a few. This one is going to be better, and it will be
38
- fanatically maintained, because npm will depend on it.
32
+ `options` can be:
39
33
 
40
- That's why I need to write it from scratch. Creating and extracting
41
- tarballs is such a large part of what npm does, I simply can't have it
42
- be a black box any longer.
34
+ ```js
35
+ {
36
+ path: '/path/to/extract/tar/into',
37
+ strip: 0, // how many path segments to strip from the root when extracting
38
+ }
39
+ ```
43
40
 
44
- ## Didn't you have something already? Where'd it go?
41
+ `options` also get passed to the `fstream.Writer` instance that `tar`
42
+ uses internally.
45
43
 
46
- It's in the "old" folder. It's not functional. Don't use it.
44
+ ### tar.Parse()
47
45
 
48
- It was a useful exploration to learn the issues involved, but like most
49
- software of any reasonable complexity, node-tar won't be useful until
50
- it's been written at least 3 times.
46
+ Returns a writable stream. Write tar data to it and it will emit
47
+ `entry` events for each entry parsed from the tarball. This is used by
48
+ `tar.Extract`.
@@ -0,0 +1,10 @@
1
+ var tar = require("../tar.js")
2
+ , fstream = require("fstream")
3
+ , fs = require("fs")
4
+
5
+ var dir_destination = fs.createWriteStream('dir.tar')
6
+
7
+ // This must be a "directory"
8
+ fstream.Reader({ path: __dirname, type: "Directory" })
9
+ .pipe(tar.Pack({ noProprietary: true }))
10
+ .pipe(dir_destination)
@@ -20,11 +20,11 @@ function BufferEntry () {
20
20
  })
21
21
  }
22
22
 
23
+ inherits(BufferEntry, Entry)
24
+
23
25
  // collect the bytes as they come in.
24
26
  BufferEntry.prototype.write = function (c) {
25
27
  c.copy(this._buffer, this._offset)
26
28
  this._offset += c.length
27
29
  Entry.prototype.write.call(this, c)
28
30
  }
29
-
30
- inherits(BufferEntry, Entry)
package/lib/entry.js CHANGED
@@ -45,168 +45,169 @@ function Entry (header, extended, global) {
45
45
  this._setProps()
46
46
  }
47
47
 
48
- inherits(Entry, Stream,
49
- { write: function (c) {
50
- if (this._ending) this.error("write() after end()", null, true)
51
- if (this._remaining === 0) {
52
- this.error("invalid bytes past eof")
53
- }
48
+ inherits(Entry, Stream)
54
49
 
55
- // often we'll get a bunch of \0 at the end of the last write,
56
- // since chunks will always be 512 bytes when reading a tarball.
57
- if (c.length > this._remaining) {
58
- c = c.slice(0, this._remaining)
59
- }
60
- this._remaining -= c.length
50
+ Entry.prototype.write = function (c) {
51
+ if (this._ending) this.error("write() after end()", null, true)
52
+ if (this._remaining === 0) {
53
+ this.error("invalid bytes past eof")
54
+ }
61
55
 
62
- // put it on the stack.
63
- var ql = this._queueLen
64
- this._queue.push(c)
65
- this._queueLen ++
56
+ // often we'll get a bunch of \0 at the end of the last write,
57
+ // since chunks will always be 512 bytes when reading a tarball.
58
+ if (c.length > this._remaining) {
59
+ c = c.slice(0, this._remaining)
60
+ }
61
+ this._remaining -= c.length
66
62
 
67
- this._read()
63
+ // put it on the stack.
64
+ var ql = this._queueLen
65
+ this._queue.push(c)
66
+ this._queueLen ++
68
67
 
69
- // either paused, or buffered
70
- if (this._paused || ql > 0) {
71
- this._needDrain = true
72
- return false
73
- }
68
+ this._read()
74
69
 
75
- return true
70
+ // either paused, or buffered
71
+ if (this._paused || ql > 0) {
72
+ this._needDrain = true
73
+ return false
76
74
  }
77
75
 
78
- , end: function (c) {
79
- if (c) this.write(c)
80
- this._ending = true
81
- this._read()
82
- }
76
+ return true
77
+ }
83
78
 
84
- , pause: function () {
85
- this._paused = true
86
- this.emit("pause")
87
- }
79
+ Entry.prototype.end = function (c) {
80
+ if (c) this.write(c)
81
+ this._ending = true
82
+ this._read()
83
+ }
88
84
 
89
- , resume: function () {
90
- // console.error(" Tar Entry resume", this.path)
91
- this.emit("resume")
92
- this._paused = false
93
- this._read()
94
- return this._queueLen - this._index > 1
95
- }
85
+ Entry.prototype.pause = function () {
86
+ this._paused = true
87
+ this.emit("pause")
88
+ }
89
+
90
+ Entry.prototype.resume = function () {
91
+ // console.error(" Tar Entry resume", this.path)
92
+ this.emit("resume")
93
+ this._paused = false
94
+ this._read()
95
+ return this._queueLen - this._index > 1
96
+ }
96
97
 
97
98
  // This is bound to the instance
98
- , _read: function () {
99
- // console.error(" Tar Entry _read", this.path)
99
+ Entry.prototype._read = function () {
100
+ // console.error(" Tar Entry _read", this.path)
100
101
 
101
- if (this._paused || this._reading || this._ended) return
102
+ if (this._paused || this._reading || this._ended) return
102
103
 
103
- // set this flag so that event handlers don't inadvertently
104
- // get multiple _read() calls running.
105
- this._reading = true
104
+ // set this flag so that event handlers don't inadvertently
105
+ // get multiple _read() calls running.
106
+ this._reading = true
106
107
 
107
- // have any data to emit?
108
- while (this._index < this._queueLen && !this._paused) {
109
- var chunk = this._queue[this._index ++]
110
- this.emit("data", chunk)
111
- }
108
+ // have any data to emit?
109
+ while (this._index < this._queueLen && !this._paused) {
110
+ var chunk = this._queue[this._index ++]
111
+ this.emit("data", chunk)
112
+ }
112
113
 
113
- // check if we're drained
114
- if (this._index >= this._queueLen) {
115
- this._queue.length = this._queueLen = this._index = 0
116
- if (this._needDrain) {
117
- this._needDrain = false
118
- this.emit("drain")
119
- }
120
- if (this._ending) {
121
- this._ended = true
122
- this.emit("end")
123
- }
114
+ // check if we're drained
115
+ if (this._index >= this._queueLen) {
116
+ this._queue.length = this._queueLen = this._index = 0
117
+ if (this._needDrain) {
118
+ this._needDrain = false
119
+ this.emit("drain")
124
120
  }
125
-
126
- // if the queue gets too big, then pluck off whatever we can.
127
- // this should be fairly rare.
128
- var mql = this._maxQueueLen
129
- if (this._queueLen > mql && this._index > 0) {
130
- mql = Math.min(this._index, mql)
131
- this._index -= mql
132
- this._queueLen -= mql
133
- this._queue = this._queue.slice(mql)
121
+ if (this._ending) {
122
+ this._ended = true
123
+ this.emit("end")
134
124
  }
125
+ }
135
126
 
136
- this._reading = false
127
+ // if the queue gets too big, then pluck off whatever we can.
128
+ // this should be fairly rare.
129
+ var mql = this._maxQueueLen
130
+ if (this._queueLen > mql && this._index > 0) {
131
+ mql = Math.min(this._index, mql)
132
+ this._index -= mql
133
+ this._queueLen -= mql
134
+ this._queue = this._queue.slice(mql)
137
135
  }
138
136
 
139
- , _setProps: function () {
140
- // props = extended->global->header->{}
141
- var header = this._header
142
- , extended = this._extended
143
- , global = this._global
144
- , props = this.props
145
-
146
- // first get the values from the normal header.
147
- var fields = tar.fields
148
- for (var f = 0; fields[f] !== null; f ++) {
149
- var field = fields[f]
150
- , val = header[field]
151
- if (typeof val !== "undefined") props[field] = val
152
- }
137
+ this._reading = false
138
+ }
153
139
 
154
- // next, the global header for this file.
155
- // numeric values, etc, will have already been parsed.
156
- ;[global, extended].forEach(function (p) {
157
- Object.keys(p).forEach(function (f) {
158
- if (typeof p[f] !== "undefined") props[f] = p[f]
159
- })
160
- })
140
+ Entry.prototype._setProps = function () {
141
+ // props = extended->global->header->{}
142
+ var header = this._header
143
+ , extended = this._extended
144
+ , global = this._global
145
+ , props = this.props
146
+
147
+ // first get the values from the normal header.
148
+ var fields = tar.fields
149
+ for (var f = 0; fields[f] !== null; f ++) {
150
+ var field = fields[f]
151
+ , val = header[field]
152
+ if (typeof val !== "undefined") props[field] = val
153
+ }
161
154
 
162
- // no nulls allowed in path or linkpath
163
- ;["path", "linkpath"].forEach(function (p) {
164
- if (props.hasOwnProperty(p)) {
165
- props[p] = props[p].split("\0")[0]
166
- }
155
+ // next, the global header for this file.
156
+ // numeric values, etc, will have already been parsed.
157
+ ;[global, extended].forEach(function (p) {
158
+ Object.keys(p).forEach(function (f) {
159
+ if (typeof p[f] !== "undefined") props[f] = p[f]
167
160
  })
161
+ })
168
162
 
163
+ // no nulls allowed in path or linkpath
164
+ ;["path", "linkpath"].forEach(function (p) {
165
+ if (props.hasOwnProperty(p)) {
166
+ props[p] = props[p].split("\0")[0]
167
+ }
168
+ })
169
169
 
170
- // set date fields to be a proper date
171
- ;["mtime", "ctime", "atime"].forEach(function (p) {
172
- if (props.hasOwnProperty(p)) {
173
- props[p] = new Date(props[p] * 1000)
174
- }
175
- })
176
170
 
177
- // set the type so that we know what kind of file to create
178
- var type
179
- switch (tar.types[props.type]) {
180
- case "OldFile":
181
- case "ContiguousFile":
182
- type = "File"
183
- break
184
-
185
- case "GNUDumpDir":
186
- type = "Directory"
187
- break
188
-
189
- case undefined:
190
- type = "Unknown"
191
- break
192
-
193
- case "Link":
194
- case "SymbolicLink":
195
- case "CharacterDevice":
196
- case "BlockDevice":
197
- case "Directory":
198
- case "FIFO":
199
- default:
200
- type = tar.types[props.type]
171
+ // set date fields to be a proper date
172
+ ;["mtime", "ctime", "atime"].forEach(function (p) {
173
+ if (props.hasOwnProperty(p)) {
174
+ props[p] = new Date(props[p] * 1000)
201
175
  }
176
+ })
202
177
 
203
- this.type = type
204
- this.path = props.path
205
- this.size = props.size
206
-
207
- // size is special, since it signals when the file needs to end.
208
- this._remaining = props.size
178
+ // set the type so that we know what kind of file to create
179
+ var type
180
+ switch (tar.types[props.type]) {
181
+ case "OldFile":
182
+ case "ContiguousFile":
183
+ type = "File"
184
+ break
185
+
186
+ case "GNUDumpDir":
187
+ type = "Directory"
188
+ break
189
+
190
+ case undefined:
191
+ type = "Unknown"
192
+ break
193
+
194
+ case "Link":
195
+ case "SymbolicLink":
196
+ case "CharacterDevice":
197
+ case "BlockDevice":
198
+ case "Directory":
199
+ case "FIFO":
200
+ default:
201
+ type = tar.types[props.type]
209
202
  }
210
- , warn: fstream.warn
211
- , error: fstream.error
212
- })
203
+
204
+ this.type = type
205
+ this.path = props.path
206
+ this.size = props.size
207
+
208
+ // size is special, since it signals when the file needs to end.
209
+ this._remaining = props.size
210
+ }
211
+
212
+ Entry.prototype.warn = fstream.warn
213
+ Entry.prototype.error = fstream.error
@@ -8,7 +8,6 @@ inherits(ExtendedHeaderWriter, EntryWriter)
8
8
 
9
9
  var tar = require("../tar.js")
10
10
  , path = require("path")
11
- , inherits = require("inherits")
12
11
  , TarHeader = require("./header.js")
13
12
 
14
13
  // props is the props of the thing we need to write an
@@ -30,7 +30,8 @@ function ExtendedHeader () {
30
30
  this._key = ""
31
31
  }
32
32
 
33
- inherits(ExtendedHeader, Entry, { _parse: parse })
33
+ inherits(ExtendedHeader, Entry)
34
+ ExtendedHeader.prototype._parse = parse
34
35
 
35
36
  var s = 0
36
37
  , states = ExtendedHeader.states = {}
package/lib/parse.js CHANGED
@@ -37,6 +37,7 @@ function Parse () {
37
37
  me.readable = true
38
38
  me._stream = new BlockStream(512)
39
39
  me.position = 0
40
+ me._ended = false
40
41
 
41
42
  me._stream.on("error", function (e) {
42
43
  me.emit("error", e)
@@ -118,13 +119,13 @@ Parse.prototype._process = function (c) {
118
119
  // so appending one tarball to another is technically valid.
119
120
  // ending without the eof null blocks is not allowed, however.
120
121
  if (zero) {
121
- this._ended = this._eofStarted
122
+ if (this._eofStarted)
123
+ this._ended = true
122
124
  this._eofStarted = true
123
125
  } else {
124
- this._ended = this._eofStarted = false
126
+ this._eofStarted = false
125
127
  this._startEntry(c)
126
128
  }
127
-
128
129
  }
129
130
 
130
131
  this.position += 512
package/package.json CHANGED
@@ -2,7 +2,7 @@
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": "0.1.17",
5
+ "version": "1.0.0",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git://github.com/isaacs/node-tar.git"
@@ -12,9 +12,9 @@
12
12
  "test": "tap test/*.js"
13
13
  },
14
14
  "dependencies": {
15
- "inherits": "1.x",
16
15
  "block-stream": "*",
17
- "fstream": "~0.1.8"
16
+ "fstream": "^1.0.0",
17
+ "inherits": "2"
18
18
  },
19
19
  "devDependencies": {
20
20
  "tap": "0.x",
package/test/extract.js CHANGED
@@ -1,3 +1,6 @@
1
+ // Set the umask, so that it works the same everywhere.
2
+ process.umask(parseInt('22', 8))
3
+
1
4
  var tap = require("tap")
2
5
  , tar = require("../tar.js")
3
6
  , fs = require("fs")
@@ -114,6 +117,13 @@ var tap = require("tap")
114
117
  size: 200,
115
118
  linkpath: undefined,
116
119
  nlink: 2 },
120
+ { path: '/200LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL',
121
+ mode: '120755',
122
+ type: 'SymbolicLink',
123
+ depth: 1,
124
+ size: 200,
125
+ linkpath: '200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
126
+ nlink: 1 },
117
127
  { path: '/200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
118
128
  mode: '100644',
119
129
  type: 'Link',
@@ -121,13 +131,6 @@ var tap = require("tap")
121
131
  size: 200,
122
132
  linkpath: path.join(target, '200-hard'),
123
133
  nlink: 2 },
124
- { path: '/200LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL',
125
- mode: '120777',
126
- type: 'SymbolicLink',
127
- depth: 1,
128
- size: 200,
129
- linkpath: '200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
130
- nlink: 1 },
131
134
  { path: '/c.txt',
132
135
  mode: '100644',
133
136
  type: 'File',
@@ -280,6 +283,12 @@ var tap = require("tap")
280
283
  // So, this is as much a test of fstream.Reader and fstream.Writer
281
284
  // as it is of tar.Extract, but it sort of makes sense.
282
285
 
286
+ tap.test("preclean", function (t) {
287
+ require("rimraf").sync(__dirname + "/tmp/extract-test")
288
+ t.pass("cleaned!")
289
+ t.end()
290
+ })
291
+
283
292
  tap.test("extract test", function (t) {
284
293
  var extract = tar.Extract(target)
285
294
  var inp = fs.createReadStream(file)