ldjson-body
Get a stream for a line delimited json response body in the browser, compatible with Fetch
Based on the JSON chunk parser from eBay's jsonpipe
Installation
Install ldjson-body
using npm:
npm install --save ldjson-body
Usage
Module usage
const ldjsonBody = ;
Requirement
You'll need either native Promise
and TextDecoder
implementations or polyfills.
API
ldjsonBody(bodyStream, options)
Name | Type | Description |
---|---|---|
bodyStream | ReadableStream |
The response body stream to parse json from |
options | Object |
See Options |
Returns: a Promise Emitter, i.e. a Promise
enhanced with EventEmitter functions like on
and off
(for available events see Events).
Options
options.delimiter
Type: String
Default: "\n"
Set delimiter when parsing the response. Each delimiter indicates the start of a new json object.
Events
data
Event: Emitted for each found and parsed json object in the response body.
error
Event: Emitted if an error occurs when parsing a chunk of json. error.chunk
is set to the failing string.
end
Event: Emitted when the whole body have been read and parsed.
License
MIT © Joakim Carlstein and eBay Inc