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

Commit 12b7a2e

Browse files
author
Alan Shaw
authored
refactor: switch tar-stream for it-tar (#1161)
This switches out `tar-stream` for [`it-tar`](https://www.npmjs.com/package/it-tar), which is a fork that uses pure async iterables. This removes `readable-stream` from this branch of the tree (which will allow us to drop it entirely in the future) and allows us to drop the `event-iterator` dependency.
1 parent 0e01187 commit 12b7a2e

File tree

4 files changed

+38
-92
lines changed

4 files changed

+38
-92
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"detect-node": "^2.0.4",
5858
"end-of-stream": "^1.4.1",
5959
"err-code": "^2.0.0",
60-
"event-iterator": "^1.2.0",
6160
"explain-error": "^1.0.4",
6261
"flatmap": "0.0.3",
6362
"form-data": "^3.0.0",
@@ -74,6 +73,7 @@
7473
"iso-stream-http": "~0.1.2",
7574
"iso-url": "~0.4.6",
7675
"it-glob": "0.0.6",
76+
"it-tar": "^1.1.0",
7777
"it-to-stream": "^0.1.1",
7878
"iterable-ndjson": "^1.1.0",
7979
"just-kebab-case": "^1.1.0",
@@ -101,7 +101,6 @@
101101
"qs": "^6.5.2",
102102
"readable-stream": "^3.1.1",
103103
"stream-to-pull-stream": "^1.7.2",
104-
"tar-stream": "^2.0.1",
105104
"through2": "^3.0.1"
106105
},
107106
"devDependencies": {

src/get.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

33
const configure = require('./lib/configure')
4-
const tarStreamToObjects = require('./utils/tar-stream-to-objects')
4+
const Tar = require('it-tar')
55
const IsIpfs = require('is-ipfs')
6+
const toIterable = require('./lib/stream-to-iterable')
67
const cleanCID = require('./utils/clean-cid')
78

89
module.exports = configure(({ ky }) => {
@@ -43,6 +44,19 @@ module.exports = configure(({ ky }) => {
4344
searchParams
4445
})
4546

46-
yield * tarStreamToObjects(res.body)
47+
const extractor = Tar.extract()
48+
49+
for await (const { header, body } of extractor(toIterable(res.body))) {
50+
if (header.type === 'directory') {
51+
yield {
52+
path: header.name
53+
}
54+
} else {
55+
yield {
56+
path: header.name,
57+
content: body
58+
}
59+
}
60+
}
4761
}
4862
})

src/utils/load-commands.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const { concatify, collectify, pullify, streamify } = require('../lib/converters
77
const toPullStream = require('async-iterator-to-pull-stream')
88
const pull = require('pull-stream/pull')
99
const map = require('pull-stream/throughs/map')
10+
const toStream = require('it-to-stream')
11+
const BufferList = require('bl/BufferList')
1012

1113
function requireCommands (send, config) {
1214
const add = require('../add')(config)
@@ -58,21 +60,37 @@ function requireCommands (send, config) {
5860

5961
for await (const entry of get(path, options)) {
6062
if (entry.content) {
61-
entry.content = Buffer.concat(await all(entry.content))
63+
entry.content = new BufferList(await all(entry.content)).slice()
6264
}
6365

6466
output.push(entry)
6567
}
6668

6769
return output
6870
}),
69-
getReadableStream: streamify.readable(get),
71+
getReadableStream: streamify.readable((path, options) => (async function * () {
72+
for await (const file of get(path, options)) {
73+
if (file.content) {
74+
const { content } = file
75+
file.content = toStream((async function * () {
76+
for await (const chunk of content) {
77+
yield chunk.slice() // Convert bl to Buffer
78+
}
79+
})())
80+
}
81+
82+
yield file
83+
}
84+
})()),
7085
getPullStream: (path, options) => {
7186
return pull(
7287
toPullStream(get(path, options)),
7388
map(file => {
7489
if (file.content) {
75-
file.content = toPullStream(file.content)
90+
file.content = pull(
91+
toPullStream(file.content),
92+
map(chunk => chunk.slice()) // Convert bl to Buffer
93+
)
7694
}
7795

7896
return file

src/utils/tar-stream-to-objects.js

-85
This file was deleted.

0 commit comments

Comments
 (0)