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

Commit 27c0c48

Browse files
authored
Merge pull request #1101 from ipfs/refactor/fetch-cat
refactor: use fetch for ipfs.cat
2 parents b89b487 + 9c9aac8 commit 27c0c48

File tree

8 files changed

+63
-114
lines changed

8 files changed

+63
-114
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"just-kebab-case": "^1.1.0",
7676
"just-map-keys": "^1.1.0",
7777
"kind-of": "^6.0.2",
78-
"ky": "^0.13.0",
78+
"ky": "^0.14.0",
7979
"ky-universal": "^0.3.0",
8080
"lru-cache": "^5.1.1",
8181
"merge-options": "^1.0.1",

src/cat.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
const CID = require('cids')
4+
const { Buffer } = require('buffer')
5+
const configure = require('./lib/configure')
6+
const toIterable = require('./lib/stream-to-iterable')
7+
8+
module.exports = configure(({ ky }) => {
9+
return (path, options) => (async function * () {
10+
options = options || {}
11+
12+
const searchParams = new URLSearchParams(options.searchParams)
13+
14+
if (typeof path === 'string') {
15+
searchParams.set('arg', path)
16+
} else {
17+
searchParams.set('arg', new CID(path).toString())
18+
}
19+
20+
if (options.offset) searchParams.set('offset', options.offset)
21+
if (options.length) searchParams.set('length', options.length)
22+
23+
const res = await ky.get('cat', {
24+
timeout: options.timeout,
25+
signal: options.signal,
26+
headers: options.headers,
27+
searchParams
28+
})
29+
30+
for await (const chunk of toIterable(res.body)) {
31+
yield Buffer.from(chunk)
32+
}
33+
})()
34+
})

src/files-regular/cat-pull-stream.js

-35
This file was deleted.

src/files-regular/cat-readable-stream.js

-35
This file was deleted.

src/files-regular/cat.js

-38
This file was deleted.

src/files-regular/index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

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

77
module.exports = (arg) => {
88
const send = moduleConfig(arg)
99
const add = require('../add')(arg)
1010
const addFromFs = require('../add-from-fs')(arg)
1111
const addFromURL = require('../add-from-url')(arg)
12+
const cat = require('../cat')(arg)
1213

1314
return {
1415
add: (input, options, callback) => {
@@ -42,9 +43,15 @@ module.exports = (arg) => {
4243
return nodeify(collectify(add)(input, options), callback)
4344
},
4445
_addAsyncIterator: add,
45-
cat: require('../files-regular/cat')(send),
46-
catReadableStream: require('../files-regular/cat-readable-stream')(send),
47-
catPullStream: require('../files-regular/cat-pull-stream')(send),
46+
cat: (path, options, callback) => {
47+
if (typeof options === 'function') {
48+
callback = options
49+
options = {}
50+
}
51+
return nodeify(concatify(cat)(path, options), callback)
52+
},
53+
catReadableStream: streamify.readable(cat),
54+
catPullStream: pullify.source(cat),
4855
get: require('../files-regular/get')(send),
4956
getReadableStream: require('../files-regular/get-readable-stream')(send),
5057
getPullStream: require('../files-regular/get-pull-stream')(send),

src/lib/converters.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
const toPull = require('async-iterator-to-pull-stream')
44
const all = require('async-iterator-all')
55
const toStream = require('it-to-stream')
6+
const { Buffer } = require('buffer')
67

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

10+
exports.concatify = fn => async (...args) => Buffer.concat(await all(fn(...args)))
11+
912
exports.pullify = {
1013
source: fn => (...args) => toPull(fn(...args)),
1114
transform: fn => (...args) => toPull.transform(source => fn(source, ...args))
1215
}
1316

1417
exports.streamify = {
18+
readable: fn => (...args) => toStream(fn(...args), { objectMode: true }),
1519
transform: fn => (...args) => toStream.transform(source => fn(source, ...args), { objectMode: true })
1620
}

src/lib/error-handler.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22

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

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

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

1325
let msg
1426

0 commit comments

Comments
 (0)