Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit f9d3d34

Browse files
committed
Refactor tar-stream-to-objects to better show intent
1 parent be82c11 commit f9d3d34

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

src/api/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ module.exports = (send) => {
3333
}
3434

3535
// Convert the response stream to TarStream objects
36-
send.andTransform(request, TarStreamToObjects.from, callback)
36+
send.andTransform(request, TarStreamToObjects, callback)
3737
})
3838
}

src/tar-stream-to-objects.js

+32-31
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,45 @@
22

33
const tar = require('tar-stream')
44
const ReadableStream = require('readable-stream').Readable
5-
/*
6-
Transform tar stream into a stream of objects:
75

8-
Output format:
9-
{ path: 'string', content: Readable }
10-
*/
11-
class TarStreamToObjects extends ReadableStream {
6+
class ObjectsStreams extends ReadableStream {
127
constructor (options) {
138
const opts = Object.assign(options || {}, { objectMode: true })
149
super(opts)
1510
}
1611

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+
}
4114

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)
4344
}
4445

4546
module.exports = TarStreamToObjects

0 commit comments

Comments
 (0)