|
2 | 2 |
|
3 | 3 | const tar = require('tar-stream')
|
4 | 4 | const ReadableStream = require('readable-stream').Readable
|
5 |
| -/* |
6 |
| - Transform tar stream into a stream of objects: |
7 | 5 |
|
8 |
| - Output format: |
9 |
| - { path: 'string', content: Readable } |
10 |
| -*/ |
11 |
| -class TarStreamToObjects extends ReadableStream { |
| 6 | +class ObjectsStreams extends ReadableStream { |
12 | 7 | constructor (options) {
|
13 | 8 | const opts = Object.assign(options || {}, { objectMode: true })
|
14 | 9 | super(opts)
|
15 | 10 | }
|
16 | 11 |
|
17 |
| - static from (inputStream, callback) { |
18 |
| - let outputStream = new TarStreamToObjects() |
19 |
| - |
20 |
| - inputStream |
21 |
| - .pipe(tar.extract()) |
22 |
| - .on('entry', (header, stream, next) => { |
23 |
| - stream.on('end', next) |
24 |
| - |
25 |
| - if (header.type !== 'directory') { |
26 |
| - outputStream.push({ |
27 |
| - path: header.name, |
28 |
| - content: stream |
29 |
| - }) |
30 |
| - } else { |
31 |
| - outputStream.push({ |
32 |
| - path: header.name |
33 |
| - }) |
34 |
| - stream.resume() |
35 |
| - } |
36 |
| - }) |
37 |
| - .on('finish', () => outputStream.push(null)) |
38 |
| - |
39 |
| - callback(null, outputStream) |
40 |
| - } |
| 12 | + _read () {} |
| 13 | +} |
41 | 14 |
|
42 |
| - _read () {} |
| 15 | +/* |
| 16 | + Transform a tar stream into a stream of objects: |
| 17 | +
|
| 18 | + Output format: |
| 19 | + { path: 'string', content: Stream<Readable> } |
| 20 | +*/ |
| 21 | +const TarStreamToObjects = (inputStream, callback) => { |
| 22 | + let outputStream = new ObjectsStreams() |
| 23 | + |
| 24 | + inputStream |
| 25 | + .pipe(tar.extract()) |
| 26 | + .on('entry', (header, stream, next) => { |
| 27 | + stream.on('end', next) |
| 28 | + |
| 29 | + if (header.type !== 'directory') { |
| 30 | + outputStream.push({ |
| 31 | + path: header.name, |
| 32 | + content: stream |
| 33 | + }) |
| 34 | + } else { |
| 35 | + outputStream.push({ |
| 36 | + path: header.name |
| 37 | + }) |
| 38 | + stream.resume() |
| 39 | + } |
| 40 | + }) |
| 41 | + .on('finish', () => outputStream.push(null)) |
| 42 | + |
| 43 | + callback(null, outputStream) |
43 | 44 | }
|
44 | 45 |
|
45 | 46 | module.exports = TarStreamToObjects
|
0 commit comments