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

Commit 37b7d27

Browse files
committed
Fix tests, add test, rename to async iterable, fix addFromURL
1 parent f0d4016 commit 37b7d27

24 files changed

+164
-111
lines changed

src/add-from-url.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
'use strict'
22

33
const kyDefault = require('ky-universal').default
4-
const toIterable = require('./lib/stream-to-iterable')
4+
const toAsyncIterable = require('./lib/stream-to-async-iterable')
55

6-
module.exports = (config) => {
6+
module.exports = config => {
77
const add = require('./add')(config)
88

99
return async function * addFromURL (url, options) {
1010
options = options || {}
1111

12-
const { body } = await kyDefault.get(url)
12+
const res = await kyDefault.get(url)
1313

1414
const input = {
1515
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''),
16-
content: toIterable(body)
16+
content: toAsyncIterable(res)
1717
}
1818

1919
yield * add(input, options)

src/add/index.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const ndjson = require('iterable-ndjson')
44
const configure = require('../lib/configure')
5-
const toIterable = require('../lib/stream-to-iterable')
5+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
66
const { toFormData } = require('./form-data')
77
const toCamel = require('../lib/object-to-camel')
88

@@ -16,21 +16,36 @@ module.exports = configure(({ ky }) => {
1616
if (options.chunker) searchParams.set('chunker', options.chunker)
1717
if (options.cidVersion) searchParams.set('cid-version', options.cidVersion)
1818
if (options.cidBase) searchParams.set('cid-base', options.cidBase)
19-
if (options.enableShardingExperiment != null) searchParams.set('enable-sharding-experiment', options.enableShardingExperiment)
19+
if (options.enableShardingExperiment != null) {
20+
searchParams.set(
21+
'enable-sharding-experiment',
22+
options.enableShardingExperiment
23+
)
24+
}
2025
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
21-
if (options.onlyHash != null) searchParams.set('only-hash', options.onlyHash)
26+
if (options.onlyHash != null) { searchParams.set('only-hash', options.onlyHash) }
2227
if (options.pin != null) searchParams.set('pin', options.pin)
2328
if (options.progress) searchParams.set('progress', true)
2429
if (options.quiet != null) searchParams.set('quiet', options.quiet)
2530
if (options.quieter != null) searchParams.set('quieter', options.quieter)
26-
if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves)
27-
if (options.shardSplitThreshold) searchParams.set('shard-split-threshold', options.shardSplitThreshold)
31+
if (options.rawLeaves != null) { searchParams.set('raw-leaves', options.rawLeaves) }
32+
if (options.shardSplitThreshold) { searchParams.set('shard-split-threshold', options.shardSplitThreshold) }
2833
if (options.silent) searchParams.set('silent', options.silent)
2934
if (options.trickle != null) searchParams.set('trickle', options.trickle)
30-
if (options.wrapWithDirectory != null) searchParams.set('wrap-with-directory', options.wrapWithDirectory)
35+
if (options.wrapWithDirectory != null) { searchParams.set('wrap-with-directory', options.wrapWithDirectory) }
3136
if (options.preload != null) searchParams.set('preload', options.preload)
32-
if (options.fileImportConcurrency != null) searchParams.set('file-import-concurrency', options.fileImportConcurrency)
33-
if (options.blockWriteConcurrency != null) searchParams.set('block-write-concurrency', options.blockWriteConcurrency)
37+
if (options.fileImportConcurrency != null) {
38+
searchParams.set(
39+
'file-import-concurrency',
40+
options.fileImportConcurrency
41+
)
42+
}
43+
if (options.blockWriteConcurrency != null) {
44+
searchParams.set(
45+
'block-write-concurrency',
46+
options.blockWriteConcurrency
47+
)
48+
}
3449

3550
const res = await ky.post('add', {
3651
timeout: options.timeout,
@@ -40,7 +55,7 @@ module.exports = configure(({ ky }) => {
4055
body: await toFormData(input)
4156
})
4257

43-
for await (let file of ndjson(toIterable(res))) {
58+
for await (let file of ndjson(toAsyncIterable(res))) {
4459
file = toCamel(file)
4560
// console.log(file)
4661
if (options.progress && file.bytes) {

src/block/rm-async-iterator.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const CID = require('cids')
44
const ndjson = require('iterable-ndjson')
55
const configure = require('../lib/configure')
6-
const toIterable = require('../lib/stream-to-iterable')
6+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
77
const toCamel = require('../lib/object-to-camel')
88

99
module.exports = configure(({ ky }) => {
@@ -30,7 +30,7 @@ module.exports = configure(({ ky }) => {
3030
searchParams
3131
})
3232

33-
for await (const removed of ndjson(toIterable(res))) {
33+
for await (const removed of ndjson(toAsyncIterable(res))) {
3434
yield toCamel(removed)
3535
}
3636
}

src/cat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const CID = require('cids')
44
const { Buffer } = require('buffer')
55
const configure = require('./lib/configure')
6-
const toIterable = require('./lib/stream-to-iterable')
6+
const toAsyncIterable = require('./lib/stream-to-async-iterable')
77

88
module.exports = configure(({ ky }) => {
99
return async function * cat (path, options) {
@@ -27,7 +27,7 @@ module.exports = configure(({ ky }) => {
2727
searchParams
2828
})
2929

30-
for await (const chunk of toIterable(res)) {
30+
for await (const chunk of toAsyncIterable(res)) {
3131
yield Buffer.from(chunk)
3232
}
3333
}

src/dht/find-peer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PeerInfo = require('peer-info')
55
const multiaddr = require('multiaddr')
66
const ndjson = require('iterable-ndjson')
77
const configure = require('../lib/configure')
8-
const toIterable = require('../lib/stream-to-iterable')
8+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
99

1010
module.exports = configure(({ ky }) => {
1111
return async function * findPeer (peerId, options) {
@@ -22,7 +22,7 @@ module.exports = configure(({ ky }) => {
2222
searchParams
2323
})
2424

25-
for await (const message of ndjson(toIterable(res))) {
25+
for await (const message of ndjson(toAsyncIterable(res))) {
2626
// 2 = FinalPeer
2727
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
2828
if (message.Type === 2 && message.Responses) {

src/dht/find-provs.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ const PeerInfo = require('peer-info')
55
const multiaddr = require('multiaddr')
66
const ndjson = require('iterable-ndjson')
77
const configure = require('../lib/configure')
8-
const toIterable = require('../lib/stream-to-iterable')
8+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
99

1010
module.exports = configure(({ ky }) => {
1111
return async function * findProvs (cid, options) {
1212
options = options || {}
1313

1414
const searchParams = new URLSearchParams(options.searchParams)
1515
searchParams.set('arg', `${cid}`)
16-
if (options.numProviders) searchParams.set('num-providers', options.numProviders)
16+
if (options.numProviders) { searchParams.set('num-providers', options.numProviders) }
1717
if (options.verbose != null) searchParams.set('verbose', options.verbose)
1818

1919
const res = await ky.post('dht/findprovs', {
@@ -23,7 +23,7 @@ module.exports = configure(({ ky }) => {
2323
searchParams
2424
})
2525

26-
for await (const message of ndjson(toIterable(res))) {
26+
for await (const message of ndjson(toAsyncIterable(res))) {
2727
// 4 = Provider
2828
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L20
2929
if (message.Type === 4 && message.Responses) {

src/dht/get.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const ndjson = require('iterable-ndjson')
44
const configure = require('../lib/configure')
5-
const toIterable = require('../lib/stream-to-iterable')
5+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
66

77
module.exports = configure(({ ky }) => {
88
return async function * get (key, options) {
@@ -19,7 +19,7 @@ module.exports = configure(({ ky }) => {
1919
searchParams
2020
})
2121

22-
for await (const message of ndjson(toIterable(res))) {
22+
for await (const message of ndjson(toAsyncIterable(res))) {
2323
// 5 = Value
2424
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L21
2525
if (message.Type === 5) {

src/dht/provide.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PeerInfo = require('peer-info')
55
const multiaddr = require('multiaddr')
66
const ndjson = require('iterable-ndjson')
77
const configure = require('../lib/configure')
8-
const toIterable = require('../lib/stream-to-iterable')
8+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
99
const toCamel = require('../lib/object-to-camel')
1010

1111
module.exports = configure(({ ky }) => {
@@ -15,7 +15,7 @@ module.exports = configure(({ ky }) => {
1515

1616
const searchParams = new URLSearchParams(options.searchParams)
1717
cids.forEach(cid => searchParams.append('arg', `${cid}`))
18-
if (options.recursive != null) searchParams.set('recursive', options.recursive)
18+
if (options.recursive != null) { searchParams.set('recursive', options.recursive) }
1919
if (options.verbose != null) searchParams.set('verbose', options.verbose)
2020

2121
const res = await ky.post('dht/provide', {
@@ -25,7 +25,7 @@ module.exports = configure(({ ky }) => {
2525
searchParams
2626
})
2727

28-
for await (let message of ndjson(toIterable(res))) {
28+
for await (let message of ndjson(toAsyncIterable(res))) {
2929
message = toCamel(message)
3030
if (message.responses) {
3131
message.responses = message.responses.map(({ ID, Addrs }) => {

src/dht/put.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PeerInfo = require('peer-info')
55
const multiaddr = require('multiaddr')
66
const ndjson = require('iterable-ndjson')
77
const configure = require('../lib/configure')
8-
const toIterable = require('../lib/stream-to-iterable')
8+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
99
const encodeBufferURIComponent = require('../lib/encode-buffer-uri-component')
1010
const toCamel = require('../lib/object-to-camel')
1111

@@ -16,8 +16,12 @@ module.exports = configure(({ ky }) => {
1616
const searchParams = new URLSearchParams(options.searchParams)
1717
if (options.verbose != null) searchParams.set('verbose', options.verbose)
1818

19-
key = Buffer.isBuffer(key) ? encodeBufferURIComponent(key) : encodeURIComponent(key)
20-
value = Buffer.isBuffer(value) ? encodeBufferURIComponent(value) : encodeURIComponent(value)
19+
key = Buffer.isBuffer(key)
20+
? encodeBufferURIComponent(key)
21+
: encodeURIComponent(key)
22+
value = Buffer.isBuffer(value)
23+
? encodeBufferURIComponent(value)
24+
: encodeURIComponent(value)
2125

2226
const url = `dht/put?arg=${key}&arg=${value}&${searchParams}`
2327
const res = await ky.post(url, {
@@ -26,7 +30,7 @@ module.exports = configure(({ ky }) => {
2630
headers: options.headers
2731
})
2832

29-
for await (let message of ndjson(toIterable(res))) {
33+
for await (let message of ndjson(toAsyncIterable(res))) {
3034
message = toCamel(message)
3135
if (message.responses) {
3236
message.responses = message.responses.map(({ ID, Addrs }) => {

src/dht/query.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const PeerId = require('peer-id')
44
const PeerInfo = require('peer-info')
55
const ndjson = require('iterable-ndjson')
66
const configure = require('../lib/configure')
7-
const toIterable = require('../lib/stream-to-iterable')
7+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
88

99
module.exports = configure(({ ky }) => {
1010
return async function * query (peerId, options) {
@@ -21,7 +21,7 @@ module.exports = configure(({ ky }) => {
2121
searchParams
2222
})
2323

24-
for await (const message of ndjson(toIterable(res))) {
24+
for await (const message of ndjson(toAsyncIterable(res))) {
2525
yield new PeerInfo(PeerId.createFromB58String(message.ID))
2626
}
2727
}

src/files/ls.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const CID = require('cids')
44
const ndjson = require('iterable-ndjson')
5-
const toIterable = require('../lib/stream-to-iterable')
5+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
66
const configure = require('../lib/configure')
77
const toCamelWithMetadata = require('../lib/object-to-camel-with-metadata')
88

@@ -28,7 +28,7 @@ module.exports = configure(({ ky }) => {
2828
searchParams
2929
})
3030

31-
for await (const result of ndjson(toIterable(res))) {
31+
for await (const result of ndjson(toAsyncIterable(res))) {
3232
// go-ipfs does not yet support the "stream" option
3333
if ('Entries' in result) {
3434
for (const entry of result.Entries || []) {

src/files/read.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { Buffer } = require('buffer')
44
const configure = require('../lib/configure')
5-
const toIterable = require('../lib/stream-to-iterable')
5+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
66

77
module.exports = configure(({ ky }) => {
88
return async function * read (path, options) {
@@ -20,7 +20,7 @@ module.exports = configure(({ ky }) => {
2020
searchParams
2121
})
2222

23-
for await (const chunk of toIterable(res)) {
23+
for await (const chunk of toAsyncIterable(res)) {
2424
yield Buffer.from(chunk)
2525
}
2626
}

src/get.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const configure = require('./lib/configure')
44
const Tar = require('it-tar')
55
const { Buffer } = require('buffer')
66
const CID = require('cids')
7-
const toIterable = require('./lib/stream-to-iterable')
7+
const toAsyncIterable = require('./lib/stream-to-async-iterable')
88

99
module.exports = configure(({ ky }) => {
1010
return async function * get (path, options) {
@@ -38,7 +38,7 @@ module.exports = configure(({ ky }) => {
3838

3939
const extractor = Tar.extract()
4040

41-
for await (const { header, body } of extractor(toIterable(res))) {
41+
for await (const { header, body } of extractor(toAsyncIterable(res))) {
4242
if (header.type === 'directory') {
4343
yield {
4444
path: header.name

src/lib/stream-to-iterable.js renamed to src/lib/stream-to-async-iterable.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
'use strict'
22

3-
module.exports = function toIterable (res) {
3+
module.exports = function toAsyncIterable (res) {
4+
const { body } = res
5+
46
// An env where res.body getter for ReadableStream with getReader
57
// is not supported, for example in React Native
6-
if (!res.body) {
7-
return (async function * () {
8-
const arrayBuffer = await res.arrayBuffer()
9-
yield arrayBuffer
10-
})()
8+
if (!body) {
9+
if (res.arrayBuffer) {
10+
return (async function * () {
11+
const arrayBuffer = await res.arrayBuffer()
12+
yield arrayBuffer
13+
})()
14+
}
1115
}
1216

13-
const { body } = res
14-
1517
// Node.js stream
1618
if (body[Symbol.asyncIterator]) return body
1719

src/log/tail.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const ndjson = require('iterable-ndjson')
44
const configure = require('../lib/configure')
5-
const toIterable = require('../lib/stream-to-iterable')
5+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
66

77
module.exports = configure(({ ky }) => {
88
return async function * tail (options) {
@@ -15,6 +15,6 @@ module.exports = configure(({ ky }) => {
1515
searchParams: options.searchParams
1616
})
1717

18-
yield * ndjson(toIterable(res))
18+
yield * ndjson(toAsyncIterable(res))
1919
}
2020
})

src/object/data.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { Buffer } = require('buffer')
44
const CID = require('cids')
55
const configure = require('../lib/configure')
6-
const toIterable = require('../lib/stream-to-iterable')
6+
const toAsyncIterable = require('../lib/stream-to-async-iterable')
77

88
module.exports = configure(({ ky }) => {
99
return async function * data (cid, options) {
@@ -19,7 +19,7 @@ module.exports = configure(({ ky }) => {
1919
searchParams
2020
})
2121

22-
for await (const chunk of toIterable(res)) {
22+
for await (const chunk of toAsyncIterable(res)) {
2323
yield Buffer.from(chunk)
2424
}
2525
}

src/ping.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const ndjson = require('iterable-ndjson')
44
const configure = require('./lib/configure')
5-
const toIterable = require('./lib/stream-to-iterable')
5+
const toAsyncIterable = require('./lib/stream-to-async-iterable')
66
const toCamel = require('./lib/object-to-camel')
77

88
module.exports = configure(({ ky }) => {
@@ -20,7 +20,7 @@ module.exports = configure(({ ky }) => {
2020
searchParams
2121
})
2222

23-
for await (const chunk of ndjson(toIterable(res))) {
23+
for await (const chunk of ndjson(toAsyncIterable(res))) {
2424
yield toCamel(chunk)
2525
}
2626
}

0 commit comments

Comments
 (0)