-
Notifications
You must be signed in to change notification settings - Fork 1.2k
WIP: Modify response to add body in React Native and logging daemon requests #2874
Changes from all commits
d4826e1
c486583
8f44ff1
5f29e6d
06a20c2
14f3b1b
14ace22
427fa4a
c32b769
dd88b64
6f4e910
24d6a9c
6d6d6f2
3633f92
4147294
8c87db2
7c3496c
b62022b
b5ff4b9
f92f5ed
3d7f3c7
094161b
8352224
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
'use strict' | ||
|
||
const CID = require('cids') | ||
|
||
const merge = require('merge-options') | ||
const { toFormData } = require('./form-data') | ||
const toCamel = require('../lib/object-to-camel') | ||
const configure = require('../lib/configure') | ||
|
||
|
||
module.exports = configure((api) => { | ||
return async function * add (input, options = {}) { | ||
console.log("Add called"); | ||
console.log("Using monorepo version") | ||
const progressFn = options.progress | ||
options = merge( | ||
options, | ||
|
@@ -18,20 +22,28 @@ module.exports = configure((api) => { | |
} | ||
) | ||
|
||
const formData = await toFormData(input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hugomrdias @achingbrain Do you have any stylistic objections to building |
||
|
||
console.log({ formData }) | ||
|
||
const res = await api.ndjson('add', { | ||
method: 'POST', | ||
searchParams: options, | ||
body: await toFormData(input), | ||
body: formData, | ||
timeout: options.timeout, | ||
signal: options.signal | ||
}) | ||
|
||
for await (let file of res) { | ||
console.log({ file }); | ||
file = toCamel(file) | ||
console.log("toCamelifiedFile", file); | ||
|
||
if (progressFn && file.bytes) { | ||
console.log("progressFn && file.bytes"); | ||
progressFn(file.bytes) | ||
} else { | ||
console.log("else"); | ||
yield toCoreInterface(file) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
'use strict' | ||
|
||
const { Buffer } = require('buffer') | ||
const toAsyncIterable = require('../lib/stream-to-async-iterable') | ||
// TODO: Decide if we can remove `toIterable` | ||
const toIterable = require('stream-to-it/source') | ||
const configure = require('../lib/configure') | ||
|
||
|
@@ -13,7 +15,7 @@ module.exports = configure(api => { | |
searchParams: options | ||
}) | ||
|
||
for await (const chunk of toIterable(res.body)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hugomrdias It's interesting that there are still some methods where this |
||
for await (const chunk of toAsyncIterable(res)) { | ||
yield Buffer.from(chunk) | ||
} | ||
} | ||
|
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) { | ||
return (async function * () { | ||
const arrayBuffer = await res.arrayBuffer() | ||
yield arrayBuffer | ||
})() | ||
} else { | ||
throw new Error('Neither Response.body nor Response.arrayBuffer is defined') | ||
} | ||
} | ||
|
||
return toAsyncIterableOriginal(body) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ const bs58 = require('bs58') | |
const { Buffer } = require('buffer') | ||
const log = require('debug')('ipfs-http-client:pubsub:subscribe') | ||
const SubscriptionTracker = require('./subscription-tracker') | ||
|
||
// TODO: Update streamToAsyncIterator with any chances in light of | ||
// this feature branch | ||
const { streamToAsyncIterator, ndjson } = require('../lib/core') | ||
Comment on lines
+8
to
10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hugomrdias Can you give a short description of the new approach with |
||
const configure = require('../lib/configure') | ||
|
||
|
@@ -45,6 +48,9 @@ module.exports = configure((api, options) => { | |
|
||
clearTimeout(ffWorkaround) | ||
|
||
// Note: It's interesting that subscribe | ||
// keeps this ndjson(tranformation(res)) pattern although | ||
// that's now long from other IPFS methods | ||
readMessages(ndjson(streamToAsyncIterator(res)), { | ||
Comment on lines
+51
to
54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hugomrdias For parallelism, would it make sense to get rid of this |
||
onMessage: handler, | ||
onEnd: () => subsTracker.unsubscribe(topic, handler), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const { expect } = require('interface-ipfs-core/src/utils/mocha') | ||
const toAsyncIterable = require('../src/lib/stream-to-async-iterable') | ||
|
||
describe('lib/stream-to-async-iterable', () => { | ||
it('should return input if already async iterable', () => { | ||
const input = { | ||
[Symbol.asyncIterator] () { | ||
return this | ||
} | ||
} | ||
const res = { body: input } | ||
expect(toAsyncIterable(res)).to.equal(input) | ||
}) | ||
|
||
it('should convert reader to async iterable', async () => { | ||
const inputData = [2, 31, 3, 4] | ||
|
||
const input = { | ||
getReader () { | ||
let i = 0 | ||
return { | ||
read () { | ||
return Promise.resolve( | ||
i === inputData.length | ||
? { done: true } | ||
: { value: inputData[i++] } | ||
) | ||
}, | ||
releaseLock () { } | ||
} | ||
} | ||
} | ||
const res = { body: input } | ||
|
||
const chunks = [] | ||
for await (const chunk of toAsyncIterable(res)) { | ||
chunks.push(chunk) | ||
} | ||
|
||
expect(chunks).to.eql(inputData) | ||
}) | ||
|
||
it('should return an async iterable even if res.body is undefined', async () => { | ||
const inputData = [2] | ||
const res = { | ||
arrayBuffer () { | ||
return Promise.resolve(inputData[0]) | ||
} | ||
} | ||
|
||
const chunks = [] | ||
for await (const chunk of toAsyncIterable(res)) { | ||
chunks.push(chunk) | ||
} | ||
|
||
expect(chunks).to.eql(inputData) | ||
}) | ||
|
||
it('should throw if res.body and res.arrayBuffer are undefined', () => { | ||
const res = {} | ||
expect(() => toAsyncIterable(res)).to.throw('Neither Response.body nor Response.arrayBuffer is defined') | ||
}) | ||
|
||
it('should throw on unknown stream', () => { | ||
const res = { body: {} } | ||
expect(() => toAsyncIterable(res)).to.throw('unknown stream') | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any objections to these changes making it into the
.gitignore
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hugomrdias @achingbrain Thoughts? Thanks!