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

refactor: use fetch for ipfs.cat #1101

Merged
merged 2 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"just-kebab-case": "^1.1.0",
"just-map-keys": "^1.1.0",
"kind-of": "^6.0.2",
"ky": "^0.13.0",
"ky": "^0.14.0",
"ky-universal": "^0.3.0",
"lru-cache": "^5.1.1",
"multiaddr": "^6.0.6",
Expand Down
34 changes: 34 additions & 0 deletions src/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const CID = require('cids')
const { Buffer } = require('buffer')
const configure = require('./lib/configure')
const toIterable = require('./lib/stream-to-iterable')

module.exports = configure(({ ky }) => {
return (path, options) => (async function * () {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)

if (typeof path === 'string') {
searchParams.set('arg', path)
} else {
searchParams.set('arg', new CID(path).toString())
}

if (options.offset) searchParams.set('offset', options.offset)
if (options.length) searchParams.set('length', options.length)

const res = await ky.get('cat', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
hugomrdias marked this conversation as resolved.
Show resolved Hide resolved
searchParams
})

for await (const chunk of toIterable(res.body)) {
hugomrdias marked this conversation as resolved.
Show resolved Hide resolved
yield Buffer.from(chunk)
}
})()
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this a IIEF ?

Copy link
Contributor Author

@alanshaw alanshaw Sep 11, 2019

Choose a reason for hiding this comment

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

Its a generator function that needs to be called to obtain the generator. I can split this out into a definition and a return of the called function if you think it’ll be easier to understand?

})
35 changes: 0 additions & 35 deletions src/files-regular/cat-pull-stream.js

This file was deleted.

35 changes: 0 additions & 35 deletions src/files-regular/cat-readable-stream.js

This file was deleted.

38 changes: 0 additions & 38 deletions src/files-regular/cat.js

This file was deleted.

15 changes: 11 additions & 4 deletions src/files-regular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const nodeify = require('promise-nodeify')
const moduleConfig = require('../utils/module-config')
const { collectify, pullify, streamify } = require('../lib/converters')
const { concatify, collectify, pullify, streamify } = require('../lib/converters')

module.exports = (arg) => {
const send = moduleConfig(arg)
const add = require('../add')(arg)
const addFromFs = require('../add-from-fs')(arg)
const addFromURL = require('../add-from-url')(arg)
const cat = require('../cat')(arg)

return {
add: (input, options, callback) => {
Expand Down Expand Up @@ -42,9 +43,15 @@ module.exports = (arg) => {
return nodeify(collectify(add)(input, options), callback)
},
_addAsyncIterator: add,
cat: require('../files-regular/cat')(send),
catReadableStream: require('../files-regular/cat-readable-stream')(send),
catPullStream: require('../files-regular/cat-pull-stream')(send),
cat: (path, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
return nodeify(concatify(cat)(path, options), callback)
},
catReadableStream: streamify.readable(cat),
catPullStream: pullify.source(cat),
get: require('../files-regular/get')(send),
getReadableStream: require('../files-regular/get-readable-stream')(send),
getPullStream: require('../files-regular/get-pull-stream')(send),
Expand Down
4 changes: 4 additions & 0 deletions src/lib/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
const toPull = require('async-iterator-to-pull-stream')
const all = require('async-iterator-all')
const toStream = require('it-to-stream')
const { Buffer } = require('buffer')

exports.collectify = fn => (...args) => all(fn(...args))

exports.concatify = fn => async (...args) => Buffer.concat(await all(fn(...args)))

exports.pullify = {
source: fn => (...args) => toPull(fn(...args)),
transform: fn => (...args) => toPull.transform(source => fn(source, ...args))
}

exports.streamify = {
readable: fn => (...args) => toStream(fn(...args), { objectMode: true }),
transform: fn => (...args) => toStream.transform(source => fn(source, ...args), { objectMode: true })
}
14 changes: 13 additions & 1 deletion src/lib/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

const { HTTPError } = require('ky-universal')
const log = require('debug')('ipfs-http-client:lib:error-handler')
const { isNode, isElectron } = require('ipfs-utils/src/env')

function isJsonResponse (res) {
return (res.headers.get('Content-Type') || '').startsWith('application/json')
}

module.exports = async function errorHandler (input, options, response) {
if (response.ok) return
if (response.ok) {
// FIXME: remove when fixed https://github.com/sindresorhus/ky-universal/issues/8
//
// ky clones the response for each handler. In Node.js the response body is
// piped to 2 PassThroughs, one becomes the real body and the other is used
// in the clone.
//
// If the body in the clone is not consumed or destroyed the highwater mark
// will be reached (for large payloads) and stop the real body from flowing.
if (isNode || isElectron) response.body.destroy()
return
}

let msg

Expand Down