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

Commit 25bf1d1

Browse files
fix: log works in Node.js and Chrome
1 parent b502cce commit 25bf1d1

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
"multiaddr": "^2.0.2",
3333
"multipart-stream": "^2.0.1",
3434
"promisify-es6": "^1.0.1",
35+
"pull-split": "^0.2.0",
36+
"pull-stream": "^3.4.3",
37+
"pull-stream-to-stream": "^1.3.0",
3538
"qs": "^6.2.1",
39+
"stream-to-pull-stream": "^1.7.0",
3640
"streamifier": "^0.1.1",
3741
"tar-stream": "^1.5.2",
3842
"to-arraybuffer": "^1.0.1"

src/api/log.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
'use strict'
22

3-
const ndjson = require('ndjson')
43
const promisify = require('promisify-es6')
54

65
module.exports = (send) => {
76
return {
87
tail: promisify((callback) => {
98
send({
10-
path: 'log/tail'
11-
}, (err, response) => {
12-
if (err) {
13-
return callback(err)
14-
}
15-
callback(null, response.pipe(ndjson.parse()))
16-
})
9+
path: 'log/tail',
10+
ndjson: true
11+
}, callback)
1712
})
1813
}
1914
}

src/request-api.js

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,73 @@
22
'use strict'
33

44
const Qs = require('qs')
5-
const toReadStream = require('streamifier').createReadStream
65
const isNode = require('detect-node')
76
const bl = require('bl')
87
const toArrayBuffer = require('to-arraybuffer')
8+
const pull = require('pull-stream')
9+
const toStream = require('pull-stream-to-stream')
10+
const toPull = require('stream-to-pull-stream')
11+
const toBuffer = require('arraybuffer-to-buffer')
12+
const split = require('pull-split')
913

1014
const getFilesStream = require('./get-files-stream')
1115
const bufferReturn = require('./buffer-return')
1216

1317
// -- Internal
1418

15-
function onRes (buffer) {
19+
function streamReturn (res, ndjson) {
20+
let stream
21+
if (res.body && res.body.getReader) {
22+
// Chrome implements ReadbleStream
23+
const reader = res.body.getReader()
24+
let ended = false
25+
stream = (end, cb) => {
26+
if (end) ended = end
27+
if (ended) {
28+
reader.cancel()
29+
return cb(ended)
30+
}
31+
32+
reader.read()
33+
.then((result) => {
34+
console.log('got result', result)
35+
if (result.done) ended = true
36+
if (ended) return cb(ended)
37+
38+
const val = result.value
39+
cb(null, Buffer.isBuffer(val) ? val : toBuffer(val))
40+
})
41+
.catch((err) => {
42+
ended = err
43+
cb(ended)
44+
})
45+
}
46+
}
47+
48+
// node-fetch has PassThrough stream as body
49+
if (res.body && res.body.readable) {
50+
if (!ndjson) {
51+
return res.body
52+
}
53+
54+
stream = toPull.source(res.body)
55+
}
56+
57+
if (stream) {
58+
if (ndjson) {
59+
return toStream.source(pull(
60+
stream,
61+
split('\n', JSON.parse)
62+
))
63+
} else {
64+
return toStream.source(stream)
65+
}
66+
}
67+
68+
throw new Error('Streaming is not supported by our browser')
69+
}
70+
71+
function onRes (buffer, ndjson) {
1672
return (res) => {
1773
const stream = Boolean(res.headers.get('x-stream-output'))
1874
const chunkedObjects = Boolean(res.headers.get('x-chunked-output'))
@@ -34,7 +90,7 @@ function onRes (buffer) {
3490
}
3591

3692
if (stream && !buffer) {
37-
return bufferReturn(res).then(toReadStream)
93+
return streamReturn(res, ndjson)
3894
}
3995

4096
if (chunkedObjects) {
@@ -49,8 +105,6 @@ function onRes (buffer) {
49105
return parts
50106
.map(JSON.parse)
51107
} catch (err) {
52-
console.error(parts)
53-
console.error(err.stack)
54108
throw err
55109
}
56110
})
@@ -140,7 +194,7 @@ function requestAPI (config, options) {
140194
mode: 'cors',
141195
body: body
142196
}))
143-
.then(onRes(options.buffer))
197+
.then(onRes(options.buffer, options.ndjson))
144198
}
145199

146200
//

test/interface-ipfs-core/log.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const isPhantom = !isNode && typeof navigator !== 'undefined' && navigator.userA
1212
if (!isPhantom) {
1313
// How did this work before
1414
// our polyfill didn't have real streaming support
15-
describe.skip('.log', () => {
15+
describe('.log', () => {
1616
let ipfs
1717
let fc
1818

@@ -36,6 +36,7 @@ if (!isPhantom) {
3636
expect(req).to.exist
3737

3838
res.once('data', (obj) => {
39+
res.end()
3940
expect(obj).to.be.an('object')
4041
done()
4142
})
@@ -47,6 +48,7 @@ if (!isPhantom) {
4748
return ipfs.log.tail()
4849
.then((res) => {
4950
res.once('data', (obj) => {
51+
res.end()
5052
expect(obj).to.be.an('object')
5153
})
5254
})

0 commit comments

Comments
 (0)