Skip to content

Commit

Permalink
Merge pull request ipfs#1101 from ipfs/refactor/fetch-cat
Browse files Browse the repository at this point in the history
refactor: use fetch for ipfs.cat
  • Loading branch information
achingbrain authored Sep 17, 2019
2 parents b89b487 + 9c9aac8 commit 27c0c48
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 114 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,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",
"merge-options": "^1.0.1",
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,
searchParams
})

for await (const chunk of toIterable(res.body)) {
yield Buffer.from(chunk)
}
})()
})
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

0 comments on commit 27c0c48

Please sign in to comment.