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

Commit d087b72

Browse files
authored
chore: converts remaining files api to async iterators (#1124)
1 parent 191f414 commit d087b72

16 files changed

+274
-568
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"detect-node": "^2.0.4",
5757
"end-of-stream": "^1.4.1",
5858
"err-code": "^2.0.0",
59+
"event-iterator": "^1.2.0",
5960
"explain-error": "^1.0.4",
6061
"flatmap": "0.0.3",
6162
"form-data": "^2.5.1",

src/files-regular/get-pull-stream.js

-44
This file was deleted.

src/files-regular/get-readable-stream.js

-34
This file was deleted.

src/files-regular/get.js

+33-37
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,48 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
3+
const configure = require('../lib/configure')
4+
const tarStreamToObjects = require('../utils/tar-stream-to-objects')
5+
const IsIpfs = require('is-ipfs')
46
const cleanCID = require('../utils/clean-cid')
5-
const TarStreamToObjects = require('../utils/tar-stream-to-objects')
6-
const concat = require('concat-stream')
7-
const through = require('through2')
8-
const v = require('is-ipfs')
9-
10-
module.exports = (send) => {
11-
return promisify((path, opts, callback) => {
12-
if (typeof opts === 'function' && !callback) {
13-
callback = opts
14-
opts = {}
15-
}
167

17-
// opts is the real callback --
18-
// 'callback' is being injected by promisify
19-
if (typeof opts === 'function' && typeof callback === 'function') {
20-
callback = opts
21-
opts = {}
22-
}
8+
module.exports = configure(({ ky }) => {
9+
return async function * get (path, options) {
10+
options = options || {}
2311

2412
try {
2513
path = cleanCID(path)
2614
} catch (err) {
27-
if (!v.ipfsPath(path)) {
28-
return callback(err)
15+
if (!IsIpfs.ipfsPath(path)) {
16+
throw err
2917
}
3018
}
3119

32-
const request = { path: 'get', args: path, qs: opts }
20+
const searchParams = new URLSearchParams()
21+
searchParams.set('arg', path.toString())
3322

34-
// Convert the response stream to TarStream objects
35-
send.andTransform(request, TarStreamToObjects, (err, stream) => {
36-
if (err) { return callback(err) }
23+
if (options.compress !== undefined) {
24+
searchParams.set('compress', options.compress)
25+
}
3726

38-
const files = []
27+
if (options.compressionLevel !== undefined) {
28+
searchParams.set('compression-level', options.compressionLevel)
29+
}
3930

40-
stream.pipe(through.obj((file, enc, next) => {
41-
if (file.content) {
42-
file.content.pipe(concat((content) => {
43-
files.push({ path: file.path, content: content })
44-
}))
45-
} else {
46-
files.push(file)
47-
}
48-
next()
49-
}, () => callback(null, files)))
31+
if (options.offset) {
32+
searchParams.set('offset', options.offset)
33+
}
34+
35+
if (options.length) {
36+
searchParams.set('length', options.length)
37+
}
38+
39+
const res = await ky.get('get', {
40+
timeout: options.timeout,
41+
signal: options.signal,
42+
headers: options.headers,
43+
searchParams
5044
})
51-
})
52-
}
45+
46+
yield * tarStreamToObjects(res.body)
47+
}
48+
})

src/files-regular/index.js

+59-24
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
'use strict'
22

33
const nodeify = require('promise-nodeify')
4-
const moduleConfig = require('../utils/module-config')
4+
const callbackify = require('callbackify')
5+
const all = require('async-iterator-all')
56
const { concatify, collectify, pullify, streamify } = require('../lib/converters')
7+
const toPullStream = require('async-iterator-to-pull-stream')
8+
const pull = require('pull-stream/pull')
9+
const map = require('pull-stream/throughs/map')
610

7-
module.exports = (arg) => {
8-
const send = moduleConfig(arg)
9-
const add = require('../add')(arg)
10-
const addFromFs = require('../add-from-fs')(arg)
11-
const addFromURL = require('../add-from-url')(arg)
12-
const cat = require('../cat')(arg)
11+
module.exports = (config) => {
12+
const add = require('../add')(config)
13+
const addFromFs = require('../add-from-fs')(config)
14+
const addFromURL = require('../add-from-url')(config)
15+
const cat = require('../cat')(config)
16+
const get = require('./get')(config)
17+
const ls = require('./ls')(config)
18+
const refs = require('./refs')(config)
19+
const refsLocal = require('./refs-local')(config)
1320

14-
return {
21+
const api = {
1522
add: (input, options, callback) => {
1623
if (typeof options === 'function') {
1724
callback = options
@@ -43,23 +50,51 @@ module.exports = (arg) => {
4350
return nodeify(collectify(add)(input, options), callback)
4451
},
4552
_addAsyncIterator: add,
46-
cat: (path, options, callback) => {
47-
if (typeof options === 'function') {
48-
callback = options
49-
options = {}
50-
}
51-
return nodeify(concatify(cat)(path, options), callback)
52-
},
53+
cat: callbackify.variadic((path, options) => concatify(cat)(path, options)),
5354
catReadableStream: streamify.readable(cat),
5455
catPullStream: pullify.source(cat),
55-
get: require('../files-regular/get')(send),
56-
getReadableStream: require('../files-regular/get-readable-stream')(send),
57-
getPullStream: require('../files-regular/get-pull-stream')(send),
58-
ls: require('../files-regular/ls')(send),
59-
lsReadableStream: require('../files-regular/ls-readable-stream')(send),
60-
lsPullStream: require('../files-regular/ls-pull-stream')(send),
61-
refs: require('../files-regular/refs')(send),
62-
refsReadableStream: require('../files-regular/refs-readable-stream')(send),
63-
refsPullStream: require('../files-regular/refs-pull-stream')(send)
56+
_catAsyncIterator: cat,
57+
get: callbackify.variadic(async (path, options) => {
58+
const output = []
59+
60+
for await (const entry of get(path, options)) {
61+
if (entry.content) {
62+
entry.content = Buffer.concat(await all(entry.content))
63+
}
64+
65+
output.push(entry)
66+
}
67+
68+
return output
69+
}),
70+
getReadableStream: streamify.readable(get),
71+
getPullStream: (path, options) => {
72+
return pull(
73+
toPullStream(get(path, options)),
74+
map(file => {
75+
if (file.content) {
76+
file.content = toPullStream(file.content)
77+
}
78+
79+
return file
80+
})
81+
)
82+
},
83+
_getAsyncIterator: get,
84+
ls: callbackify.variadic((path, options) => collectify(ls)(path, options)),
85+
lsReadableStream: streamify.readable(ls),
86+
lsPullStream: pullify.source(ls),
87+
_lsAsyncIterator: ls,
88+
refs: callbackify.variadic((path, options) => collectify(refs)(path, options)),
89+
refsReadableStream: streamify.readable(refs),
90+
refsPullStream: pullify.source(refs),
91+
_refsAsyncIterator: refs
6492
}
93+
94+
api.refs.local = callbackify.variadic((options) => collectify(refsLocal)(options))
95+
api.refs.localReadableStream = streamify.readable(refsLocal)
96+
api.refs.localPullStream = pullify.source(refsLocal)
97+
api.refs._localAsyncIterator = refsLocal
98+
99+
return api
65100
}

src/files-regular/ls-pull-stream.js

-74
This file was deleted.

0 commit comments

Comments
 (0)