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

Commit f0d4016

Browse files
committed
Handling case where res.body is undefined
1 parent d7eb0e8 commit f0d4016

21 files changed

+32
-21
lines changed

src/add/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = configure(({ ky }) => {
4040
body: await toFormData(input)
4141
})
4242

43-
for await (let file of ndjson(toIterable(res.body))) {
43+
for await (let file of ndjson(toIterable(res))) {
4444
file = toCamel(file)
4545
// console.log(file)
4646
if (options.progress && file.bytes) {

src/block/rm-async-iterator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = configure(({ ky }) => {
3030
searchParams
3131
})
3232

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

src/cat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = configure(({ ky }) => {
2727
searchParams
2828
})
2929

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

src/dht/find-peer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = configure(({ ky }) => {
2222
searchParams
2323
})
2424

25-
for await (const message of ndjson(toIterable(res.body))) {
25+
for await (const message of ndjson(toIterable(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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = configure(({ ky }) => {
2323
searchParams
2424
})
2525

26-
for await (const message of ndjson(toIterable(res.body))) {
26+
for await (const message of ndjson(toIterable(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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = configure(({ ky }) => {
1919
searchParams
2020
})
2121

22-
for await (const message of ndjson(toIterable(res.body))) {
22+
for await (const message of ndjson(toIterable(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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = configure(({ ky }) => {
2525
searchParams
2626
})
2727

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

src/dht/put.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = configure(({ ky }) => {
2626
headers: options.headers
2727
})
2828

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

src/dht/query.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = configure(({ ky }) => {
2121
searchParams
2222
})
2323

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

src/files/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = configure(({ ky }) => {
2828
searchParams
2929
})
3030

31-
for await (const result of ndjson(toIterable(res.body))) {
31+
for await (const result of ndjson(toIterable(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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = configure(({ ky }) => {
2020
searchParams
2121
})
2222

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

src/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -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.body))) {
41+
for await (const { header, body } of extractor(toIterable(res))) {
4242
if (header.type === 'directory') {
4343
yield {
4444
path: header.name

src/lib/stream-to-iterable.js

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

3-
module.exports = function toIterable (body) {
3+
module.exports = function toIterable (res) {
4+
// An env where res.body getter for ReadableStream with getReader
5+
// 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+
})()
11+
}
12+
13+
const { body } = res
14+
415
// Node.js stream
516
if (body[Symbol.asyncIterator]) return body
617

src/log/tail.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ module.exports = configure(({ ky }) => {
1515
searchParams: options.searchParams
1616
})
1717

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

src/object/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = configure(({ ky }) => {
1919
searchParams
2020
})
2121

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

src/ping.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = configure(({ ky }) => {
2020
searchParams
2121
})
2222

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

src/pubsub/subscribe.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = configure((config) => {
5050

5151
clearTimeout(ffWorkaround)
5252

53-
readMessages(ndjson(toIterable(res.body)), {
53+
readMessages(ndjson(toIterable(res)), {
5454
onMessage: handler,
5555
onEnd: () => subsTracker.unsubscribe(topic, handler),
5656
onError: options.onError

src/refs/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = config => {
4949
searchParams
5050
})
5151

52-
for await (const file of ndjson(toIterable(res.body))) {
52+
for await (const file of ndjson(toIterable(res))) {
5353
yield toCamel(file)
5454
}
5555
}

src/refs/local.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = configure(({ ky }) => {
1515
headers: options.headers
1616
})
1717

18-
for await (const file of ndjson(toIterable(res.body))) {
18+
for await (const file of ndjson(toIterable(res))) {
1919
yield toCamel(file)
2020
}
2121
}

src/repo/gc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = configure(({ ky }) => {
1919
searchParams
2020
})
2121

22-
for await (const gcResult of ndjson(toIterable(res.body))) {
22+
for await (const gcResult of ndjson(toIterable(res))) {
2323
yield {
2424
err: gcResult.Error ? new Error(gcResult.Error) : null,
2525
cid: (gcResult.Key || {})['/'] ? new CID(gcResult.Key['/']) : null

src/stats/bw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = configure(({ ky }) => {
2222
searchParams
2323
})
2424

25-
for await (const stats of ndjson(toIterable(res.body))) {
25+
for await (const stats of ndjson(toIterable(res))) {
2626
yield {
2727
totalIn: new Big(stats.TotalIn),
2828
totalOut: new Big(stats.TotalOut),

0 commit comments

Comments
 (0)