-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implementation bug in normaliseInput #3138
Comments
Also it appears that parenthesis is in the wrong place here
|
Please can you submit a PR with a failing test, and preferably, a fix. |
I'm working on the patch that adds tests and fixes. |
Here is the example that illustrates the issue: class IterableOf {
constructor(...entries) {
this.entries = entries
}
[Symbol.iterator]() {
return new IteratorOf(...this.entries)
}
}
class IteratorOf {
constructor(...entries) {
this.position = 0
this.entries = entries
}
next() {
if (this.position < this.entries.length) {
return { done: false, value: this.entries[this.position++] }
} else {
return { done: true }
}
}
}
function * test () {
const iterable = new IterableOf(1, 2, 3)
const iterator = iterable[Symbol.iterator]()
yield iterator.next().value
yield * iterator
}
[...test()] // Uncaught TypeError: iterator is not iterable I should note that browser built-ins (including generators) tend to return iterators that effectively have |
I don't think you can use
If I change it to a
Which is true, it's not iterable. Something is iterable if it has a |
Another problem is the iterators can have state, you can't just function * test () {
const iterable = new IterableOf(1, 2, 3)
const iterator = iterable[Symbol.iterator]()
yield iterator.next().value
yield * iterable
}
console.info([...test()])
// [ 1, 1, 2, 3 ] If you drop down to the low-level interface you can avoid it, which I guess is the solution here: function * test () {
const iterable = new IterableOf(1, 2, 3)
const iterator = iterable[Symbol.iterator]()
yield iterator.next().value
while (true) {
const { value, done } = iterator.next()
if (done) {
break
}
yield value
}
}
console.info([...test()])
// [ 1, 2, 3 ] |
Yes that was what I was trying to illustrate. You see a different error because I'm guessing you ran it on V8 while I've run it on SpiderMonkey. They produce different error messages but result is the same. |
That is why I introduced Peekables in #3151, so you could peek at first item, without consuming it & also use the whole thing as iterable. js-ipfs/packages/ipfs-core-utils/src/files/normalise-input.js Lines 409 to 452 in dcedb66
|
To support streaming of native types with no buffering, normalise add input to blobs and upload using native FormData when the http client is run in the browser. That is, if the user passes a blob to the http client in the browser leave it alone as enumerating blob contents cause the file data to be read. Browser FormData objects do not allow you to specify headers for each multipart part which means we can't pass UnixFS metadata via the headers so we turn the metadata into a querystring and append it to the field name for each multipart part as a workaround. Fixes #3138 BREAKING CHANGES: - Removes the `mode`, `mtime` and `mtime-nsec` headers from multipart requests - Passes `mode`, `mtime` and `mtime-nsec` as querystring parameters appended to the field name of multipart requests
Yes, it was a good idea - I broke some similar code out into it-peekable |
To support streaming of native types with no buffering, normalise add input to blobs and upload using native FormData when the http client is run in the browser. That is, if the user passes a blob to the http client in the browser leave it alone as enumerating blob contents cause the file data to be read. Browser FormData objects do not allow you to specify headers for each multipart part which means we can't pass UnixFS metadata via the headers so we turn the metadata into a querystring and append it to the field name for each multipart part as a workaround. Fixes #3138 BREAKING CHANGES: - Removes the `mode`, `mtime` and `mtime-nsec` headers from multipart requests - Passes `mode`, `mtime` and `mtime-nsec` as querystring parameters appended to the field name of multipart requests
I am pretty sure following lines would throw
TypeError: iterator is not iterable
becauseyield *
expression expects iterable and not an iterator.js-ipfs/packages/ipfs-core-utils/src/files/normalise-input.js
Lines 73 to 82 in 8cb8c73
js-ipfs/packages/ipfs-core-utils/src/files/normalise-input.js
Lines 114 to 122 in 8cb8c73
js-ipfs/packages/ipfs-core-utils/src/files/normalise-input.js
Lines 191 to 199 in 8cb8c73
It is also worrying that no tests seem to catch that.
The text was updated successfully, but these errors were encountered: