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

Handle case where ky responses have no body with a getter for a ReadableStream #1224

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ coverage
examples/sub-module/**/bundle.js
examples/sub-module/**/*-minified.js
examples/sub-module/*-bundle.js

.vscode/
.DS_Store
8 changes: 5 additions & 3 deletions src/add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const ndjson = require('iterable-ndjson')
const CID = require('cids')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const { toFormData } = require('./form-data')
const toCamel = require('../lib/object-to-camel')

Expand Down Expand Up @@ -33,15 +33,17 @@ module.exports = configure(({ ky }) => {
if (options.fileImportConcurrency != null) searchParams.set('file-import-concurrency', options.fileImportConcurrency)
if (options.blockWriteConcurrency != null) searchParams.set('block-write-concurrency', options.blockWriteConcurrency)

const formData = await toFormData(input)

const res = await ky.post('add', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams,
body: await toFormData(input)
body: formData
})

for await (let file of ndjson(toIterable(res.body))) {
for await (let file of ndjson(toAsyncIterable(res))) {
file = toCamel(file)

if (options.progress && file.bytes) {
Expand Down
4 changes: 2 additions & 2 deletions src/block/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const CID = require('cids')
const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')

module.exports = configure(({ ky }) => {
return async function * rm (cid, options) {
Expand All @@ -29,7 +29,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const removed of ndjson(toIterable(res.body))) {
for await (const removed of ndjson(toAsyncIterable(res))) {
yield toCoreInterface(removed)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const CID = require('cids')
const { Buffer } = require('buffer')
const configure = require('./lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('./lib/stream-to-async-iterable')

module.exports = configure(({ ky }) => {
return async function * cat (path, options) {
Expand All @@ -27,7 +27,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const chunk of toIterable(res.body)) {
for await (const chunk of toAsyncIterable(res)) {
yield Buffer.from(chunk)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/dht/find-peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const CID = require('cids')
const multiaddr = require('multiaddr')
const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')

module.exports = configure(({ ky }) => {
return async function findPeer (peerId, options) {
Expand All @@ -22,7 +22,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const message of ndjson(toIterable(res.body))) {
for await (const message of ndjson(toAsyncIterable(res))) {
// 3 = QueryError
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
// https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L388-L389
Expand Down
4 changes: 2 additions & 2 deletions src/dht/find-provs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CID = require('cids')
const multiaddr = require('multiaddr')
const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')

module.exports = configure(({ ky }) => {
return async function * findProvs (cid, options) {
Expand All @@ -22,7 +22,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const message of ndjson(toIterable(res.body))) {
for await (const message of ndjson(toAsyncIterable(res))) {
// 3 = QueryError
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
// https://github.com/libp2p/go-libp2p-kad-dht/blob/master/routing.go#L525-L526
Expand Down
4 changes: 2 additions & 2 deletions src/dht/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Buffer } = require('buffer')
const ndjson = require('iterable-ndjson')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const encodeBufferURIComponent = require('../lib/encode-buffer-uri-component')
const configure = require('../lib/configure')

Expand All @@ -23,7 +23,7 @@ module.exports = configure(({ ky }) => {
headers: options.headers
})

for await (const message of ndjson(toIterable(res.body))) {
for await (const message of ndjson(toAsyncIterable(res))) {
// 3 = QueryError
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
// https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L472-L473
Expand Down
4 changes: 2 additions & 2 deletions src/dht/provide.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CID = require('cids')
const multiaddr = require('multiaddr')
const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const toCamel = require('../lib/object-to-camel')

module.exports = configure(({ ky }) => {
Expand All @@ -24,7 +24,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (let message of ndjson(toIterable(res.body))) {
for await (let message of ndjson(toAsyncIterable(res))) {
// 3 = QueryError
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
// https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L283-L284
Expand Down
4 changes: 2 additions & 2 deletions src/dht/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const CID = require('cids')
const multiaddr = require('multiaddr')
const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const encodeBufferURIComponent = require('../lib/encode-buffer-uri-component')
const toCamel = require('../lib/object-to-camel')

Expand All @@ -26,7 +26,7 @@ module.exports = configure(({ ky }) => {
headers: options.headers
})

for await (let message of ndjson(toIterable(res.body))) {
for await (let message of ndjson(toAsyncIterable(res))) {
// 3 = QueryError
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
// https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L472-L473
Expand Down
4 changes: 2 additions & 2 deletions src/dht/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const CID = require('cids')
const ndjson = require('iterable-ndjson')
const multiaddr = require('multiaddr')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const configure = require('../lib/configure')
const toCamel = require('../lib/object-to-camel')

Expand All @@ -22,7 +22,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (let message of ndjson(toIterable(res.body))) {
for await (let message of ndjson(toAsyncIterable(res))) {
message = toCamel(message)
message.id = new CID(message.id)
message.responses = (message.responses || []).map(({ ID, Addrs }) => ({
Expand Down
4 changes: 2 additions & 2 deletions src/files/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const CID = require('cids')
const ndjson = require('iterable-ndjson')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const configure = require('../lib/configure')
const toCamelWithMetadata = require('../lib/object-to-camel-with-metadata')

Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const result of ndjson(toIterable(res.body))) {
for await (const result of ndjson(toAsyncIterable(res))) {
// go-ipfs does not yet support the "stream" option
if ('Entries' in result) {
for (const entry of result.Entries || []) {
Expand Down
4 changes: 2 additions & 2 deletions src/files/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Buffer } = require('buffer')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')

module.exports = configure(({ ky }) => {
return async function * read (path, options) {
Expand All @@ -20,7 +20,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const chunk of toIterable(res.body)) {
for await (const chunk of toAsyncIterable(res)) {
yield Buffer.from(chunk)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const configure = require('./lib/configure')
const Tar = require('it-tar')
const { Buffer } = require('buffer')
const CID = require('cids')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('./lib/stream-to-async-iterable')

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

const extractor = Tar.extract()

for await (const { header, body } of extractor(toIterable(res.body))) {
for await (const { header, body } of extractor(toAsyncIterable(res))) {
if (header.type === 'directory') {
yield {
path: header.name
Expand Down
27 changes: 27 additions & 0 deletions src/lib/stream-to-async-iterable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const toAsyncIterableOriginal = require('stream-to-it/source')

// Note: Turned this into a helper that wraps `stream-to-it/source`
// to handle the body undefined case without requiring that other libs
// that consume that package such as `js-ipfs` and `js-ipfs-utils` modify
// how they use it

module.exports = function toAsyncIterable (res) {
const { body } = res

// An env where res.body getter for ReadableStream with getReader
// is not supported, for example in React Native
if (!body) {
if (res.arrayBuffer) {
Copy link
Contributor Author

@pcowgill pcowgill Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@achingbrain @hugomrdias Do we want to console.log anything in this case, like "falling back to a hackier approach since the fetch being used here doesn't implement the streams spec"?

return (async function * () {
const arrayBuffer = await res.arrayBuffer()
yield arrayBuffer
})()
} else {
throw new Error('Neither Response.body nor Response.arrayBuffer is defined')
}
pcowgill marked this conversation as resolved.
Show resolved Hide resolved
}

return toAsyncIterableOriginal(body)
}
4 changes: 2 additions & 2 deletions src/log/tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')

module.exports = configure(({ ky }) => {
return async function * tail (options) {
Expand All @@ -15,6 +15,6 @@ module.exports = configure(({ ky }) => {
searchParams: options.searchParams
})

yield * ndjson(toIterable(res.body))
yield * ndjson(toAsyncIterable(res))
}
})
4 changes: 2 additions & 2 deletions src/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Buffer } = require('buffer')
const CID = require('cids')
const ndjson = require('iterable-ndjson')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('./lib/stream-to-async-iterable')
const configure = require('./lib/configure')

module.exports = configure(({ ky }) => {
Expand All @@ -25,7 +25,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (let result of ndjson(toIterable(res.body))) {
for await (let result of ndjson(toAsyncIterable(res))) {
result = result.Objects

if (!result) {
Expand Down
4 changes: 2 additions & 2 deletions src/name/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')

module.exports = configure(({ ky }) => {
return async function * (path, options) {
Expand All @@ -23,7 +23,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const result of ndjson(toIterable(res.body))) {
for await (const result of ndjson(toAsyncIterable(res))) {
yield result.Path
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pin/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const ndjson = require('iterable-ndjson')
const CID = require('cids')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')

module.exports = configure(({ ky }) => {
return async function * ls (path, options) {
Expand All @@ -28,7 +28,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const pin of ndjson(toIterable(res.body))) {
for await (const pin of ndjson(toAsyncIterable(res))) {
if (pin.Keys) { // non-streaming response
for (const cid of Object.keys(pin.Keys)) {
yield { cid: new CID(cid), type: pin.Keys[cid].Type }
Expand Down
4 changes: 2 additions & 2 deletions src/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const ndjson = require('iterable-ndjson')
const configure = require('./lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('./lib/stream-to-async-iterable')
const toCamel = require('./lib/object-to-camel')

module.exports = configure(({ ky }) => {
Expand All @@ -20,7 +20,7 @@ module.exports = configure(({ ky }) => {
searchParams
})

for await (const chunk of ndjson(toIterable(res.body))) {
for await (const chunk of ndjson(toAsyncIterable(res))) {
yield toCamel(chunk)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pubsub/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const bs58 = require('bs58')
const { Buffer } = require('buffer')
const log = require('debug')('ipfs-http-client:pubsub:subscribe')
const configure = require('../lib/configure')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const SubscriptionTracker = require('./subscription-tracker')

module.exports = configure((config) => {
Expand Down Expand Up @@ -49,7 +49,7 @@ module.exports = configure((config) => {

clearTimeout(ffWorkaround)

readMessages(ndjson(toIterable(res.body)), {
readMessages(ndjson(toAsyncIterable(res)), {
onMessage: handler,
onEnd: () => subsTracker.unsubscribe(topic, handler),
onError: options.onError
Expand Down
4 changes: 2 additions & 2 deletions src/refs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const configure = require('../lib/configure')
const { Buffer } = require('buffer')
const CID = require('cids')
const ndjson = require('iterable-ndjson')
const toIterable = require('stream-to-it/source')
const toAsyncIterable = require('../lib/stream-to-async-iterable')
const toCamel = require('../lib/object-to-camel')

module.exports = config => {
Expand Down Expand Up @@ -49,7 +49,7 @@ module.exports = config => {
searchParams
})

for await (const file of ndjson(toIterable(res.body))) {
for await (const file of ndjson(toAsyncIterable(res))) {
yield toCamel(file)
}
}
Expand Down
Loading