-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for binary data (#12) #15
Conversation
* Request implementations now supply a `Uint8Array` of binary data through to the `chunkParser`. * `defaultChunkParser` converts the `chunkBytes` into a string for processing * Overhauled how parserState is handled; no longer assume that the `chunkParser` will emit a string; instead we use a `state` object whose contract is wholly owned by the parser (we just shuffle it around internally). * Added test-coverage for UTF-8 characters * Added dependency on `utf-8` lib for handling (un)marhsalling of binary data to text. * Updated documentation * Updated npm dependencies. Inspiration provided by @mwitkow and @ariutta -- many thanks! :)
Looks good! Only question is about xhr.js. It doesn't convert the string to an ArrayBuffer. Is it best to make the parsers responsible for handling two types of input types -- string or ArrayBuffer? |
* Update README to convey which polyfills are required for crappy browsers.
Would appear that IE10 has typedarray support :)
@ariutta thanks so much for your input on #17; I've adopted your approach of using I've left the polyfills for Let me know your thoughts; if it looks good to you I'll land this (and add you to the CONTRIBUTORS). Thanks again. |
@jonnyreeves: sure, keeping the polyfills out of the default bundle is usually a best practice, so that's fine. It's easy but maybe less clean to exclude them with this syntax in the browser field:
About the
By not using the Here's how I handled this case: Maybe we should also use the One more thing: the defaultParser seemed to have trouble with this sample I made several changes to |
Makes me wonder if we want more control over the lifecycle of the Perhaps instead of supply a // use `onChunk` to emit parsed data from the stream.
chunkParser(stream, onChunk) {
let state = {};
stream.on('data', bytes => {
// ...
onChunk(jsonObjects);
});
stream.on('end', () => {
onChunk(flushedState);
});
} Now the (default) Let me know your thoughts - and thanks again. |
I like the idea of passing a minimal stream to the parser. It separates concerns nicely. Only question is whether to base it on Node streams (
For the parser, some of the ideas you're raising about handling state and transforms remind me of transducers. I'm interested in using transducers for the parsers because it appears they may be the fastest way to transform data when it is sometimes returned synchronously and other times asynchronously. I also like that transducers "are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transformation in terms of an individual element. Because transducers are decoupled from input or output sources, they can be used in many different processes," such as native or Underscore reduce implementations, immutable-js, Node streams, RxJS, etc (source). I've already created transducers for quite a few different parsers here. What do you think about using WHATWG streams and transducers? |
In my fork, I created a new branch feature/reader to show the use of WHATWG streams, passing a This required creating a minimal |
Browsers using the fallback `xhr` transport should expect an addtional call to their `chunkParser` as we flush out the state of the `TextEncoder` when the XHR connection is closed.
Thanks @ariutta; I've started incorperating the changes from your feature/reader branch into this Pull Request; I'll start pulling across the It's obvious now that we should look to de-dupe Thank you so much for your contributions - no only are you helping shape this library, but you are helping me think about the problem in a new way - I think I owe you a 🍻 :) |
Beoir! 👍 Glad to collaborate -- it's tricky trying to cover the appropriate use cases without over-engineering. Agree on segmenting the changes into multiple PRs. |
@ariutta; will follow-up with integrating your streams work in a subsiquent PR as this one was getting quite chunky and it would get good to get some of the recent improvements out the door first :) |
Good plan. On Aug 26, 2016 10:12 AM, "John Reeves" notifications@github.com wrote:
|
Uint8Array
of binary data through to thechunkParser
.defaultChunkParser
converts thechunkBytes
into a string for processingchunkParser
will emit a string; instead we use astate
object whose contract is wholly owned by the parser (we just shuffle it around internally).utf-8
lib for handling (un)marhsalling of binary data to text.Inspiration provided by @mwitkow and @ariutta -- many thanks! :)