| 1 | # BFJ
|
|---|
| 2 |
|
|---|
| 3 | [](https://gitlab.com/philbooth/bfj/pipelines)
|
|---|
| 4 | [](https://www.npmjs.com/package/bfj)
|
|---|
| 5 | [](https://www.npmjs.com/package/bfj)
|
|---|
| 6 | [](https://opensource.org/licenses/MIT)
|
|---|
| 7 |
|
|---|
| 8 | Big-Friendly JSON. Asynchronous streaming functions for large JSON data sets.
|
|---|
| 9 |
|
|---|
| 10 | * [Why would I want those?](#why-would-i-want-those)
|
|---|
| 11 | * [Is it fast?](#is-it-fast)
|
|---|
| 12 | * [What functions does it implement?](#what-functions-does-it-implement)
|
|---|
| 13 | * [How do I install it?](#how-do-i-install-it)
|
|---|
| 14 | * [How do I read a JSON file?](#how-do-i-read-a-json-file)
|
|---|
| 15 | * [How do I parse a stream of JSON?](#how-do-i-parse-a-stream-of-json)
|
|---|
| 16 | * [How do I selectively parse individual items from a JSON stream?](#how-do-i-selectively-parse-individual-items-from-a-json-stream)
|
|---|
| 17 | * [How do I write a JSON file?](#how-do-i-write-a-json-file)
|
|---|
| 18 | * [How do I create a stream of JSON?](#how-do-i-create-a-stream-of-json)
|
|---|
| 19 | * [How do I create a JSON string?](#how-do-i-create-a-json-string)
|
|---|
| 20 | * [What other methods are there?](#what-other-methods-are-there)
|
|---|
| 21 | * [bfj.walk (stream, options)](#bfjwalk-stream-options)
|
|---|
| 22 | * [bfj.eventify (data, options)](#bfjeventify-data-options)
|
|---|
| 23 | * [What options can I specify?](#what-options-can-i-specify)
|
|---|
| 24 | * [Options for parsing functions](#options-for-parsing-functions)
|
|---|
| 25 | * [Options for serialisation functions](#options-for-serialisation-functions)
|
|---|
| 26 | * [Is it possible to pause parsing or serialisation from calling code?](#is-it-possible-to-pause-parsing-or-serialisation-from-calling-code)
|
|---|
| 27 | * [Can it handle newline-delimited JSON (NDJSON)?](#can-it-handle-newline-delimited-json-ndjson)
|
|---|
| 28 | * [Why does it default to bluebird promises?](#why-does-it-default-to-bluebird-promises)
|
|---|
| 29 | * [Can I specify a different promise implementation?](#can-i-specify-a-different-promise-implementation)
|
|---|
| 30 | * [Is there a change log?](#is-there-a-change-log)
|
|---|
| 31 | * [How do I set up the dev environment?](#how-do-i-set-up-the-dev-environment)
|
|---|
| 32 | * [What versions of Node.js does it support?](#what-versions-of-nodejs-does-it-support)
|
|---|
| 33 | * [What license is it released under?](#what-license-is-it-released-under)
|
|---|
| 34 |
|
|---|
| 35 | ## Why would I want those?
|
|---|
| 36 |
|
|---|
| 37 | If you need
|
|---|
| 38 | to parse huge JSON strings
|
|---|
| 39 | or stringify huge JavaScript data sets,
|
|---|
| 40 | it monopolises the event loop
|
|---|
| 41 | and can lead to out-of-memory exceptions.
|
|---|
| 42 | BFJ implements asynchronous functions
|
|---|
| 43 | and uses pre-allocated fixed-length arrays
|
|---|
| 44 | to try and alleviate those issues.
|
|---|
| 45 |
|
|---|
| 46 | ## Is it fast?
|
|---|
| 47 |
|
|---|
| 48 | No.
|
|---|
| 49 |
|
|---|
| 50 | BFJ yields frequently
|
|---|
| 51 | to avoid monopolising the event loop,
|
|---|
| 52 | interrupting its own execution
|
|---|
| 53 | to let other event handlers run.
|
|---|
| 54 | The frequency of those yields
|
|---|
| 55 | can be controlled with the [`yieldRate` option](#what-options-can-i-specify),
|
|---|
| 56 | but fundamentally it is not designed for speed.
|
|---|
| 57 |
|
|---|
| 58 | Furthermore,
|
|---|
| 59 | when serialising data to a stream,
|
|---|
| 60 | BFJ uses a fixed-length buffer
|
|---|
| 61 | to avoid exhausting available memory.
|
|---|
| 62 | Whenever that buffer is full,
|
|---|
| 63 | serialisation is paused
|
|---|
| 64 | until the receiving stream processes some more data,
|
|---|
| 65 | regardless of the value of `yieldRate`.
|
|---|
| 66 | You can control the size of the buffer
|
|---|
| 67 | using the [`bufferLength` option](#options-for-serialisation-functions)
|
|---|
| 68 | but really,
|
|---|
| 69 | if you need quick results,
|
|---|
| 70 | BFJ is not for you.
|
|---|
| 71 |
|
|---|
| 72 | ## What functions does it implement?
|
|---|
| 73 |
|
|---|
| 74 | Nine functions
|
|---|
| 75 | are exported.
|
|---|
| 76 |
|
|---|
| 77 | Five are
|
|---|
| 78 | concerned with
|
|---|
| 79 | parsing, or
|
|---|
| 80 | turning JSON strings
|
|---|
| 81 | into JavaScript data:
|
|---|
| 82 |
|
|---|
| 83 | * [`read`](#how-do-i-read-a-json-file)
|
|---|
| 84 | asynchronously parses
|
|---|
| 85 | a JSON file from disk.
|
|---|
| 86 |
|
|---|
| 87 | * [`parse` and `unpipe`](#how-do-i-parse-a-stream-of-json)
|
|---|
| 88 | are for asynchronously parsing
|
|---|
| 89 | streams of JSON.
|
|---|
| 90 |
|
|---|
| 91 | * [`match`](#how-do-i-selectively-parse-individual-items-from-a-json-stream)
|
|---|
| 92 | selectively parses individual items
|
|---|
| 93 | from a JSON stream.
|
|---|
| 94 |
|
|---|
| 95 | * [`walk`](#bfjwalk-stream-options)
|
|---|
| 96 | asynchronously walks
|
|---|
| 97 | a stream,
|
|---|
| 98 | emitting events
|
|---|
| 99 | as it encounters
|
|---|
| 100 | JSON tokens.
|
|---|
| 101 | Analagous to a
|
|---|
| 102 | [SAX parser][sax].
|
|---|
| 103 |
|
|---|
| 104 | The other four functions
|
|---|
| 105 | handle the reverse transformations,
|
|---|
| 106 | serialising
|
|---|
| 107 | JavaScript data
|
|---|
| 108 | to JSON:
|
|---|
| 109 |
|
|---|
| 110 | * [`write`](#how-do-i-write-a-json-file)
|
|---|
| 111 | asynchronously serialises data
|
|---|
| 112 | to a JSON file on disk.
|
|---|
| 113 |
|
|---|
| 114 | * [`streamify`](#how-do-i-create-a-stream-of-json)
|
|---|
| 115 | asynchronously serialises data
|
|---|
| 116 | to a stream of JSON.
|
|---|
| 117 |
|
|---|
| 118 | * [`stringify`](#how-do-i-create-a-json-string)
|
|---|
| 119 | asynchronously serialises data
|
|---|
| 120 | to a JSON string.
|
|---|
| 121 |
|
|---|
| 122 | * [`eventify`](#bfjeventify-data-options)
|
|---|
| 123 | asynchronously traverses
|
|---|
| 124 | a data structure
|
|---|
| 125 | depth-first,
|
|---|
| 126 | emitting events
|
|---|
| 127 | as it encounters items.
|
|---|
| 128 | By default
|
|---|
| 129 | it coerces
|
|---|
| 130 | promises, buffers and iterables
|
|---|
| 131 | to JSON-friendly values.
|
|---|
| 132 |
|
|---|
| 133 | ## How do I install it?
|
|---|
| 134 |
|
|---|
| 135 | If you're using npm:
|
|---|
| 136 |
|
|---|
| 137 | ```
|
|---|
| 138 | npm i bfj --save
|
|---|
| 139 | ```
|
|---|
| 140 |
|
|---|
| 141 | Or if you just want
|
|---|
| 142 | the git repo:
|
|---|
| 143 |
|
|---|
| 144 | ```
|
|---|
| 145 | git clone git@gitlab.com:philbooth/bfj.git
|
|---|
| 146 | ```
|
|---|
| 147 |
|
|---|
| 148 | ## How do I read a JSON file?
|
|---|
| 149 |
|
|---|
| 150 | ```js
|
|---|
| 151 | const bfj = require('bfj');
|
|---|
| 152 |
|
|---|
| 153 | bfj.read(path, options)
|
|---|
| 154 | .then(data => {
|
|---|
| 155 | // :)
|
|---|
| 156 | })
|
|---|
| 157 | .catch(error => {
|
|---|
| 158 | // :(
|
|---|
| 159 | });
|
|---|
| 160 | ```
|
|---|
| 161 |
|
|---|
| 162 | `read` returns a [bluebird promise][promise] and
|
|---|
| 163 | asynchronously parses
|
|---|
| 164 | a JSON file
|
|---|
| 165 | from disk.
|
|---|
| 166 |
|
|---|
| 167 | It takes two arguments;
|
|---|
| 168 | the path to the JSON file
|
|---|
| 169 | and an [options](#options-for-parsing-functions) object.
|
|---|
| 170 |
|
|---|
| 171 | If there are
|
|---|
| 172 | no syntax errors,
|
|---|
| 173 | the returned promise is resolved
|
|---|
| 174 | with the parsed data.
|
|---|
| 175 | If syntax errors occur,
|
|---|
| 176 | the promise is rejected
|
|---|
| 177 | with the first error.
|
|---|
| 178 |
|
|---|
| 179 | ## How do I parse a stream of JSON?
|
|---|
| 180 |
|
|---|
| 181 | ```js
|
|---|
| 182 | const bfj = require('bfj');
|
|---|
| 183 |
|
|---|
| 184 | // By passing a readable stream to bfj.parse():
|
|---|
| 185 | bfj.parse(fs.createReadStream(path), options)
|
|---|
| 186 | .then(data => {
|
|---|
| 187 | // :)
|
|---|
| 188 | })
|
|---|
| 189 | .catch(error => {
|
|---|
| 190 | // :(
|
|---|
| 191 | });
|
|---|
| 192 |
|
|---|
| 193 | // ...or by passing the result from bfj.unpipe() to stream.pipe():
|
|---|
| 194 | request({ url }).pipe(bfj.unpipe((error, data) => {
|
|---|
| 195 | if (error) {
|
|---|
| 196 | // :(
|
|---|
| 197 | } else {
|
|---|
| 198 | // :)
|
|---|
| 199 | }
|
|---|
| 200 | }))
|
|---|
| 201 | ```
|
|---|
| 202 |
|
|---|
| 203 | * `parse` returns a [bluebird promise][promise]
|
|---|
| 204 | and asynchronously parses
|
|---|
| 205 | a stream of JSON data.
|
|---|
| 206 |
|
|---|
| 207 | It takes two arguments;
|
|---|
| 208 | a [readable stream][readable]
|
|---|
| 209 | from which
|
|---|
| 210 | the JSON
|
|---|
| 211 | will be parsed
|
|---|
| 212 | and an [options](#options-for-parsing-functions) object.
|
|---|
| 213 |
|
|---|
| 214 | If there are
|
|---|
| 215 | no syntax errors,
|
|---|
| 216 | the returned promise is resolved
|
|---|
| 217 | with the parsed data.
|
|---|
| 218 | If syntax errors occur,
|
|---|
| 219 | the promise is rejected
|
|---|
| 220 | with the first error.
|
|---|
| 221 |
|
|---|
| 222 | * `unpipe` returns a [writable stream][writable]
|
|---|
| 223 | that can be passed to [`stream.pipe`][pipe],
|
|---|
| 224 | then parses JSON data
|
|---|
| 225 | read from the stream.
|
|---|
| 226 |
|
|---|
| 227 | It takes two arguments;
|
|---|
| 228 | a callback function
|
|---|
| 229 | that will be called
|
|---|
| 230 | after parsing is complete
|
|---|
| 231 | and an [options](#options-for-parsing-functions) object.
|
|---|
| 232 |
|
|---|
| 233 | If there are no errors,
|
|---|
| 234 | the callback is invoked
|
|---|
| 235 | with the result as the second argument.
|
|---|
| 236 | If errors occur,
|
|---|
| 237 | the first error is passed
|
|---|
| 238 | the callback
|
|---|
| 239 | as the first argument.
|
|---|
| 240 |
|
|---|
| 241 | ## How do I selectively parse individual items from a JSON stream?
|
|---|
| 242 |
|
|---|
| 243 | ```js
|
|---|
| 244 | const bfj = require('bfj');
|
|---|
| 245 |
|
|---|
| 246 | // Call match with your stream and a selector predicate/regex/JSONPath/string
|
|---|
| 247 | const dataStream = bfj.match(jsonStream, selector, options);
|
|---|
| 248 |
|
|---|
| 249 | // Get data out of the returned stream with event handlers
|
|---|
| 250 | dataStream.on('data', item => { /* ... */ });
|
|---|
| 251 | dataStream.on('end', () => { /* ... */);
|
|---|
| 252 | dataStream.on('error', () => { /* ... */);
|
|---|
| 253 | dataStream.on('dataError', () => { /* ... */);
|
|---|
| 254 |
|
|---|
| 255 | // ...or you can pipe it to another stream
|
|---|
| 256 | dataStream.pipe(someOtherStream);
|
|---|
| 257 | ```
|
|---|
| 258 |
|
|---|
| 259 | `match` returns a readable, object-mode stream
|
|---|
| 260 | and asynchronously parses individual matching items
|
|---|
| 261 | from an input JSON stream.
|
|---|
| 262 |
|
|---|
| 263 | It takes three arguments:
|
|---|
| 264 | a [readable stream][readable]
|
|---|
| 265 | from which the JSON will be parsed;
|
|---|
| 266 | a selector argument for determining matches,
|
|---|
| 267 | which may be a string, a regular expression, a JSONPath expression, or a predicate function;
|
|---|
| 268 | and an [options](#options-for-parsing-functions) object.
|
|---|
| 269 |
|
|---|
| 270 | If the selector is a string,
|
|---|
| 271 | it will be compared to property keys
|
|---|
| 272 | to determine whether
|
|---|
| 273 | each item in the data is a match.
|
|---|
| 274 | If it is a regular expression,
|
|---|
| 275 | the comparison will be made
|
|---|
| 276 | by calling the [RegExp `test` method][regexp-test]
|
|---|
| 277 | with the property key.
|
|---|
| 278 | If it is a JSONPath expression,
|
|---|
| 279 | it must start with `$.` to identify the root node
|
|---|
| 280 | and only use `child` scope expressions for subsequent nodes.
|
|---|
| 281 | Predicate functions will be called with three arguments:
|
|---|
| 282 | `key`, `value` and `depth`.
|
|---|
| 283 | If the result of the predicate is a truthy value
|
|---|
| 284 | then the item will be deemed a match.
|
|---|
| 285 |
|
|---|
| 286 | In addition to the regular options
|
|---|
| 287 | accepted by other parsing functions,
|
|---|
| 288 | you can also specify `minDepth`
|
|---|
| 289 | to only apply the selector
|
|---|
| 290 | to certain depths.
|
|---|
| 291 | This can improve performance
|
|---|
| 292 | and memory usage,
|
|---|
| 293 | if you know that
|
|---|
| 294 | you're not interested in
|
|---|
| 295 | parsing top-level items.
|
|---|
| 296 |
|
|---|
| 297 | If there are any syntax errors in the JSON,
|
|---|
| 298 | a `dataError` event will be emitted.
|
|---|
| 299 | If any other errors occur,
|
|---|
| 300 | an `error` event will be emitted.
|
|---|
| 301 |
|
|---|
| 302 | ## How do I write a JSON file?
|
|---|
| 303 |
|
|---|
| 304 | ```js
|
|---|
| 305 | const bfj = require('bfj');
|
|---|
| 306 |
|
|---|
| 307 | bfj.write(path, data, options)
|
|---|
| 308 | .then(() => {
|
|---|
| 309 | // :)
|
|---|
| 310 | })
|
|---|
| 311 | .catch(error => {
|
|---|
| 312 | // :(
|
|---|
| 313 | });
|
|---|
| 314 | ```
|
|---|
| 315 |
|
|---|
| 316 | `write` returns a [bluebird promise][promise]
|
|---|
| 317 | and asynchronously serialises a data structure
|
|---|
| 318 | to a JSON file on disk.
|
|---|
| 319 | The promise is resolved
|
|---|
| 320 | when the file has been written,
|
|---|
| 321 | or rejected with the error
|
|---|
| 322 | if writing failed.
|
|---|
| 323 |
|
|---|
| 324 | It takes three arguments;
|
|---|
| 325 | the path to the JSON file,
|
|---|
| 326 | the data structure to serialise
|
|---|
| 327 | and an [options](#options-for-serialisation-functions) object.
|
|---|
| 328 |
|
|---|
| 329 | ## How do I create a stream of JSON?
|
|---|
| 330 |
|
|---|
| 331 | ```js
|
|---|
| 332 | const bfj = require('bfj');
|
|---|
| 333 |
|
|---|
| 334 | const stream = bfj.streamify(data, options);
|
|---|
| 335 |
|
|---|
| 336 | // Get data out of the stream with event handlers
|
|---|
| 337 | stream.on('data', chunk => { /* ... */ });
|
|---|
| 338 | stream.on('end', () => { /* ... */);
|
|---|
| 339 | stream.on('error', () => { /* ... */);
|
|---|
| 340 | stream.on('dataError', () => { /* ... */);
|
|---|
| 341 |
|
|---|
| 342 | // ...or you can pipe it to another stream
|
|---|
| 343 | stream.pipe(someOtherStream);
|
|---|
| 344 | ```
|
|---|
| 345 |
|
|---|
| 346 | `streamify` returns a [readable stream][readable]
|
|---|
| 347 | and asynchronously serialises
|
|---|
| 348 | a data structure to JSON,
|
|---|
| 349 | pushing the result
|
|---|
| 350 | to the returned stream.
|
|---|
| 351 |
|
|---|
| 352 | It takes two arguments;
|
|---|
| 353 | the data structure to serialise
|
|---|
| 354 | and an [options](#options-for-serialisation-functions) object.
|
|---|
| 355 |
|
|---|
| 356 | If there a circular reference is encountered in the data
|
|---|
| 357 | and `options.circular` is not set to `'ignore'`,
|
|---|
| 358 | a `dataError` event will be emitted.
|
|---|
| 359 | If any other errors occur,
|
|---|
| 360 | an `error` event will be emitted.
|
|---|
| 361 |
|
|---|
| 362 | ## How do I create a JSON string?
|
|---|
| 363 |
|
|---|
| 364 | ```js
|
|---|
| 365 | const bfj = require('bfj');
|
|---|
| 366 |
|
|---|
| 367 | bfj.stringify(data, options)
|
|---|
| 368 | .then(json => {
|
|---|
| 369 | // :)
|
|---|
| 370 | })
|
|---|
| 371 | .catch(error => {
|
|---|
| 372 | // :(
|
|---|
| 373 | });
|
|---|
| 374 | ```
|
|---|
| 375 |
|
|---|
| 376 | `stringify` returns a [bluebird promise][promise] and
|
|---|
| 377 | asynchronously serialises a data structure
|
|---|
| 378 | to a JSON string.
|
|---|
| 379 | The promise is resolved
|
|---|
| 380 | to the JSON string
|
|---|
| 381 | when serialisation is complete.
|
|---|
| 382 |
|
|---|
| 383 | It takes two arguments;
|
|---|
| 384 | the data structure to serialise
|
|---|
| 385 | and an [options](#options-for-serialisation-functions) object.
|
|---|
| 386 |
|
|---|
| 387 | ## What other methods are there?
|
|---|
| 388 |
|
|---|
| 389 | ### bfj.walk (stream, options)
|
|---|
| 390 |
|
|---|
| 391 | ```js
|
|---|
| 392 | const bfj = require('bfj');
|
|---|
| 393 |
|
|---|
| 394 | const emitter = bfj.walk(fs.createReadStream(path), options);
|
|---|
| 395 |
|
|---|
| 396 | emitter.on(bfj.events.array, () => { /* ... */ });
|
|---|
| 397 | emitter.on(bfj.events.object, () => { /* ... */ });
|
|---|
| 398 | emitter.on(bfj.events.property, name => { /* ... */ });
|
|---|
| 399 | emitter.on(bfj.events.string, value => { /* ... */ });
|
|---|
| 400 | emitter.on(bfj.events.number, value => { /* ... */ });
|
|---|
| 401 | emitter.on(bfj.events.literal, value => { /* ... */ });
|
|---|
| 402 | emitter.on(bfj.events.endArray, () => { /* ... */ });
|
|---|
| 403 | emitter.on(bfj.events.endObject, () => { /* ... */ });
|
|---|
| 404 | emitter.on(bfj.events.error, error => { /* ... */ });
|
|---|
| 405 | emitter.on(bfj.events.dataError, error => { /* ... */ });
|
|---|
| 406 | emitter.on(bfj.events.end, () => { /* ... */ });
|
|---|
| 407 | ```
|
|---|
| 408 |
|
|---|
| 409 | `walk` returns an [event emitter][eventemitter]
|
|---|
| 410 | and asynchronously walks
|
|---|
| 411 | a stream of JSON data,
|
|---|
| 412 | emitting events
|
|---|
| 413 | as it encounters
|
|---|
| 414 | tokens.
|
|---|
| 415 |
|
|---|
| 416 | It takes two arguments;
|
|---|
| 417 | a [readable stream][readable]
|
|---|
| 418 | from which
|
|---|
| 419 | the JSON
|
|---|
| 420 | will be read
|
|---|
| 421 | and an [options](#options-for-parsing-functions) object.
|
|---|
| 422 |
|
|---|
| 423 | The emitted events
|
|---|
| 424 | are defined
|
|---|
| 425 | as public properties
|
|---|
| 426 | of an object,
|
|---|
| 427 | `bfj.events`:
|
|---|
| 428 |
|
|---|
| 429 | * `bfj.events.array`
|
|---|
| 430 | indicates that
|
|---|
| 431 | an array context
|
|---|
| 432 | has been entered
|
|---|
| 433 | by encountering
|
|---|
| 434 | the `[` character.
|
|---|
| 435 |
|
|---|
| 436 | * `bfj.events.endArray`
|
|---|
| 437 | indicates that
|
|---|
| 438 | an array context
|
|---|
| 439 | has been left
|
|---|
| 440 | by encountering
|
|---|
| 441 | the `]` character.
|
|---|
| 442 |
|
|---|
| 443 | * `bfj.events.object`
|
|---|
| 444 | indicates that
|
|---|
| 445 | an object context
|
|---|
| 446 | has been entered
|
|---|
| 447 | by encountering
|
|---|
| 448 | the `{` character.
|
|---|
| 449 |
|
|---|
| 450 | * `bfj.events.endObject`
|
|---|
| 451 | indicates that
|
|---|
| 452 | an object context
|
|---|
| 453 | has been left
|
|---|
| 454 | by encountering
|
|---|
| 455 | the `}` character.
|
|---|
| 456 |
|
|---|
| 457 | * `bfj.events.property`
|
|---|
| 458 | indicates that
|
|---|
| 459 | a property
|
|---|
| 460 | has been encountered
|
|---|
| 461 | in an object.
|
|---|
| 462 | The listener
|
|---|
| 463 | will be passed
|
|---|
| 464 | the name of the property
|
|---|
| 465 | as its argument
|
|---|
| 466 | and the next event
|
|---|
| 467 | to be emitted
|
|---|
| 468 | will represent
|
|---|
| 469 | the property's value.
|
|---|
| 470 |
|
|---|
| 471 | * `bfj.events.string`
|
|---|
| 472 | indicates that
|
|---|
| 473 | a string
|
|---|
| 474 | has been encountered.
|
|---|
| 475 | The listener
|
|---|
| 476 | will be passed
|
|---|
| 477 | the value
|
|---|
| 478 | as its argument.
|
|---|
| 479 |
|
|---|
| 480 | * `bfj.events.number`
|
|---|
| 481 | indicates that
|
|---|
| 482 | a number
|
|---|
| 483 | has been encountered.
|
|---|
| 484 | The listener
|
|---|
| 485 | will be passed
|
|---|
| 486 | the value
|
|---|
| 487 | as its argument.
|
|---|
| 488 |
|
|---|
| 489 | * `bfj.events.literal`
|
|---|
| 490 | indicates that
|
|---|
| 491 | a JSON literal
|
|---|
| 492 | (either `true`, `false` or `null`)
|
|---|
| 493 | has been encountered.
|
|---|
| 494 | The listener
|
|---|
| 495 | will be passed
|
|---|
| 496 | the value
|
|---|
| 497 | as its argument.
|
|---|
| 498 |
|
|---|
| 499 | * `bfj.events.error`
|
|---|
| 500 | indicates that
|
|---|
| 501 | an error was caught
|
|---|
| 502 | from one of the event handlers
|
|---|
| 503 | in user code.
|
|---|
| 504 | The listener
|
|---|
| 505 | will be passed
|
|---|
| 506 | the `Error` instance
|
|---|
| 507 | as its argument.
|
|---|
| 508 |
|
|---|
| 509 | * `bfj.events.dataError`
|
|---|
| 510 | indicates that
|
|---|
| 511 | a syntax error was encountered
|
|---|
| 512 | in the incoming JSON stream.
|
|---|
| 513 | The listener
|
|---|
| 514 | will be passed
|
|---|
| 515 | an `Error` instance
|
|---|
| 516 | decorated with `actual`, `expected`, `lineNumber` and `columnNumber` properties
|
|---|
| 517 | as its argument.
|
|---|
| 518 |
|
|---|
| 519 | * `bfj.events.end`
|
|---|
| 520 | indicates that
|
|---|
| 521 | the end of the input
|
|---|
| 522 | has been reached
|
|---|
| 523 | and the stream is closed.
|
|---|
| 524 |
|
|---|
| 525 | * `bfj.events.endLine`
|
|---|
| 526 | indicates that a root-level newline character
|
|---|
| 527 | has been encountered in an [NDJSON](#can-it-handle-newline-delimited-json-ndjson) stream.
|
|---|
| 528 | Only emitted if the `ndjson` [option](#options-for-parsing-functions) is set.
|
|---|
| 529 |
|
|---|
| 530 | If you are using `bfj.walk`
|
|---|
| 531 | to sequentially parse items in an array,
|
|---|
| 532 | you might also be interested in
|
|---|
| 533 | the [bfj-collections] module.
|
|---|
| 534 |
|
|---|
| 535 | ### bfj.eventify (data, options)
|
|---|
| 536 |
|
|---|
| 537 | ```js
|
|---|
| 538 | const bfj = require('bfj');
|
|---|
| 539 |
|
|---|
| 540 | const emitter = bfj.eventify(data, options);
|
|---|
| 541 |
|
|---|
| 542 | emitter.on(bfj.events.array, () => { /* ... */ });
|
|---|
| 543 | emitter.on(bfj.events.object, () => { /* ... */ });
|
|---|
| 544 | emitter.on(bfj.events.property, name => { /* ... */ });
|
|---|
| 545 | emitter.on(bfj.events.string, value => { /* ... */ });
|
|---|
| 546 | emitter.on(bfj.events.number, value => { /* ... */ });
|
|---|
| 547 | emitter.on(bfj.events.literal, value => { /* ... */ });
|
|---|
| 548 | emitter.on(bfj.events.endArray, () => { /* ... */ });
|
|---|
| 549 | emitter.on(bfj.events.endObject, () => { /* ... */ });
|
|---|
| 550 | emitter.on(bfj.events.error, error => { /* ... */ });
|
|---|
| 551 | emitter.on(bfj.events.dataError, error => { /* ... */ });
|
|---|
| 552 | emitter.on(bfj.events.end, () => { /* ... */ });
|
|---|
| 553 | ```
|
|---|
| 554 |
|
|---|
| 555 | `eventify` returns an [event emitter][eventemitter]
|
|---|
| 556 | and asynchronously traverses
|
|---|
| 557 | a data structure depth-first,
|
|---|
| 558 | emitting events as it
|
|---|
| 559 | encounters items.
|
|---|
| 560 | By default it coerces
|
|---|
| 561 | promises, buffers and iterables
|
|---|
| 562 | to JSON-friendly values.
|
|---|
| 563 |
|
|---|
| 564 | It takes two arguments;
|
|---|
| 565 | the data structure to traverse
|
|---|
| 566 | and an [options](#options-for-serialisation-functions) object.
|
|---|
| 567 |
|
|---|
| 568 | The emitted events
|
|---|
| 569 | are defined
|
|---|
| 570 | as public properties
|
|---|
| 571 | of an object,
|
|---|
| 572 | `bfj.events`:
|
|---|
| 573 |
|
|---|
| 574 | * `bfj.events.array`
|
|---|
| 575 | indicates that
|
|---|
| 576 | an array
|
|---|
| 577 | has been encountered.
|
|---|
| 578 |
|
|---|
| 579 | * `bfj.events.endArray`
|
|---|
| 580 | indicates that
|
|---|
| 581 | the end of an array
|
|---|
| 582 | has been encountered.
|
|---|
| 583 |
|
|---|
| 584 | * `bfj.events.object`
|
|---|
| 585 | indicates that
|
|---|
| 586 | an object
|
|---|
| 587 | has been encountered.
|
|---|
| 588 |
|
|---|
| 589 | * `bfj.events.endObject`
|
|---|
| 590 | indicates that
|
|---|
| 591 | the end of an object
|
|---|
| 592 | has been encountered.
|
|---|
| 593 |
|
|---|
| 594 | * `bfj.events.property`
|
|---|
| 595 | indicates that
|
|---|
| 596 | a property
|
|---|
| 597 | has been encountered
|
|---|
| 598 | in an object.
|
|---|
| 599 | The listener
|
|---|
| 600 | will be passed
|
|---|
| 601 | the name of the property
|
|---|
| 602 | as its argument
|
|---|
| 603 | and the next event
|
|---|
| 604 | to be emitted
|
|---|
| 605 | will represent
|
|---|
| 606 | the property's value.
|
|---|
| 607 |
|
|---|
| 608 | * `bfj.events.string`
|
|---|
| 609 | indicates that
|
|---|
| 610 | a string
|
|---|
| 611 | has been encountered.
|
|---|
| 612 | The listener
|
|---|
| 613 | will be passed
|
|---|
| 614 | the value
|
|---|
| 615 | as its argument.
|
|---|
| 616 |
|
|---|
| 617 | * `bfj.events.number`
|
|---|
| 618 | indicates that
|
|---|
| 619 | a number
|
|---|
| 620 | has been encountered.
|
|---|
| 621 | The listener
|
|---|
| 622 | will be passed
|
|---|
| 623 | the value
|
|---|
| 624 | as its argument.
|
|---|
| 625 |
|
|---|
| 626 | * `bfj.events.literal`
|
|---|
| 627 | indicates that
|
|---|
| 628 | a JSON literal
|
|---|
| 629 | (either `true`, `false` or `null`)
|
|---|
| 630 | has been encountered.
|
|---|
| 631 | The listener
|
|---|
| 632 | will be passed
|
|---|
| 633 | the value
|
|---|
| 634 | as its argument.
|
|---|
| 635 |
|
|---|
| 636 | * `bfj.events.error`
|
|---|
| 637 | indicates that
|
|---|
| 638 | an error was caught
|
|---|
| 639 | from one of the event handlers
|
|---|
| 640 | in user code.
|
|---|
| 641 | The listener
|
|---|
| 642 | will be passed
|
|---|
| 643 | the `Error` instance
|
|---|
| 644 | as its argument.
|
|---|
| 645 |
|
|---|
| 646 | * `bfj.events.dataError`
|
|---|
| 647 | indicates that
|
|---|
| 648 | a circular reference was encountered in the data
|
|---|
| 649 | and the `circular` option was not set to `'ignore'`.
|
|---|
| 650 | The listener
|
|---|
| 651 | will be passed
|
|---|
| 652 | an `Error` instance
|
|---|
| 653 | as its argument.
|
|---|
| 654 |
|
|---|
| 655 | * `bfj.events.end`
|
|---|
| 656 | indicates that
|
|---|
| 657 | the end of the data
|
|---|
| 658 | has been reached and
|
|---|
| 659 | no further events
|
|---|
| 660 | will be emitted.
|
|---|
| 661 |
|
|---|
| 662 | ## What options can I specify?
|
|---|
| 663 |
|
|---|
| 664 | ### Options for parsing functions
|
|---|
| 665 |
|
|---|
| 666 | * `options.reviver`:
|
|---|
| 667 | Transformation function,
|
|---|
| 668 | invoked depth-first
|
|---|
| 669 | against the parsed
|
|---|
| 670 | data structure.
|
|---|
| 671 | This option
|
|---|
| 672 | is analagous to the
|
|---|
| 673 | [reviver parameter for JSON.parse][reviver].
|
|---|
| 674 |
|
|---|
| 675 | * `options.yieldRate`:
|
|---|
| 676 | The number of data items to process
|
|---|
| 677 | before yielding to the event loop.
|
|---|
| 678 | Smaller values yield to the event loop more frequently,
|
|---|
| 679 | meaning less time will be consumed by bfj per tick
|
|---|
| 680 | but the overall parsing time will be slower.
|
|---|
| 681 | Larger values yield to the event loop less often,
|
|---|
| 682 | meaning slower tick times but faster overall parsing time.
|
|---|
| 683 | The default value is `16384`.
|
|---|
| 684 |
|
|---|
| 685 | * `options.Promise`:
|
|---|
| 686 | Promise constructor that will be used
|
|---|
| 687 | for promises returned by all methods.
|
|---|
| 688 | If you set this option,
|
|---|
| 689 | please be aware that some promise implementations
|
|---|
| 690 | (including native promises)
|
|---|
| 691 | may cause your process to die
|
|---|
| 692 | with out-of-memory exceptions.
|
|---|
| 693 | Defaults to [bluebird's implementation][promise],
|
|---|
| 694 | which does not have that problem.
|
|---|
| 695 |
|
|---|
| 696 | * `options.ndjson`:
|
|---|
| 697 | If set to `true`,
|
|---|
| 698 | newline characters at the root level
|
|---|
| 699 | will be treated as delimiters between
|
|---|
| 700 | discrete chunks of JSON.
|
|---|
| 701 | See [NDJSON](#can-it-handle-newline-delimited-json-ndjson) for more information.
|
|---|
| 702 |
|
|---|
| 703 | * `options.numbers`:
|
|---|
| 704 | For `bfj.match` only,
|
|---|
| 705 | set this to `true`
|
|---|
| 706 | if you wish to match against numbers
|
|---|
| 707 | with a string or regular expression
|
|---|
| 708 | `selector` argument.
|
|---|
| 709 |
|
|---|
| 710 | * `options.bufferLength`:
|
|---|
| 711 | For `bfj.match` only,
|
|---|
| 712 | the length of the match buffer.
|
|---|
| 713 | Smaller values use less memory
|
|---|
| 714 | but may result in a slower parse time.
|
|---|
| 715 | The default value is `1024`.
|
|---|
| 716 |
|
|---|
| 717 | * `options.highWaterMark`:
|
|---|
| 718 | For `bfj.match` only,
|
|---|
| 719 | set this if you would like to
|
|---|
| 720 | pass a value for the `highWaterMark` option
|
|---|
| 721 | to the readable stream constructor.
|
|---|
| 722 |
|
|---|
| 723 | ### Options for serialisation functions
|
|---|
| 724 |
|
|---|
| 725 | * `options.space`:
|
|---|
| 726 | Indentation string
|
|---|
| 727 | or the number of spaces
|
|---|
| 728 | to indent
|
|---|
| 729 | each nested level by.
|
|---|
| 730 | This option
|
|---|
| 731 | is analagous to the
|
|---|
| 732 | [space parameter for JSON.stringify][space].
|
|---|
| 733 |
|
|---|
| 734 | * `options.promises`:
|
|---|
| 735 | By default,
|
|---|
| 736 | promises are coerced
|
|---|
| 737 | to their resolved value.
|
|---|
| 738 | Set this property
|
|---|
| 739 | to `'ignore'`
|
|---|
| 740 | for improved performance
|
|---|
| 741 | if you don't need
|
|---|
| 742 | to coerce promises.
|
|---|
| 743 |
|
|---|
| 744 | * `options.buffers`:
|
|---|
| 745 | By default,
|
|---|
| 746 | buffers are coerced
|
|---|
| 747 | using their `toString` method.
|
|---|
| 748 | Set this property
|
|---|
| 749 | to `'ignore'`
|
|---|
| 750 | for improved performance
|
|---|
| 751 | if you don't need
|
|---|
| 752 | to coerce buffers.
|
|---|
| 753 |
|
|---|
| 754 | * `options.maps`:
|
|---|
| 755 | By default,
|
|---|
| 756 | maps are coerced
|
|---|
| 757 | to plain objects.
|
|---|
| 758 | Set this property
|
|---|
| 759 | to `'ignore'`
|
|---|
| 760 | for improved performance
|
|---|
| 761 | if you don't need
|
|---|
| 762 | to coerce maps.
|
|---|
| 763 |
|
|---|
| 764 | * `options.iterables`:
|
|---|
| 765 | By default,
|
|---|
| 766 | other iterables
|
|---|
| 767 | (i.e. not arrays, strings or maps)
|
|---|
| 768 | are coerced
|
|---|
| 769 | to arrays.
|
|---|
| 770 | Set this property
|
|---|
| 771 | to `'ignore'`
|
|---|
| 772 | for improved performance
|
|---|
| 773 | if you don't need
|
|---|
| 774 | to coerce iterables.
|
|---|
| 775 |
|
|---|
| 776 | * `options.circular`:
|
|---|
| 777 | By default,
|
|---|
| 778 | circular references
|
|---|
| 779 | will cause the write
|
|---|
| 780 | to fail.
|
|---|
| 781 | Set this property
|
|---|
| 782 | to `'ignore'`
|
|---|
| 783 | if you'd prefer
|
|---|
| 784 | to silently skip past
|
|---|
| 785 | circular references
|
|---|
| 786 | in the data.
|
|---|
| 787 |
|
|---|
| 788 | * `options.bufferLength`:
|
|---|
| 789 | The length of the write buffer.
|
|---|
| 790 | Smaller values use less memory
|
|---|
| 791 | but may result in a slower serialisation time.
|
|---|
| 792 | The default value is `1024`.
|
|---|
| 793 |
|
|---|
| 794 | * `options.highWaterMark`:
|
|---|
| 795 | Set this if you would like to
|
|---|
| 796 | pass a value for the `highWaterMark` option
|
|---|
| 797 | to the readable stream constructor.
|
|---|
| 798 |
|
|---|
| 799 | * `options.yieldRate`:
|
|---|
| 800 | The number of data items to process
|
|---|
| 801 | before yielding to the event loop.
|
|---|
| 802 | Smaller values yield to the event loop more frequently,
|
|---|
| 803 | meaning less time will be consumed by bfj per tick
|
|---|
| 804 | but the overall serialisation time will be slower.
|
|---|
| 805 | Larger values yield to the event loop less often,
|
|---|
| 806 | meaning slower tick times but faster overall serialisation time.
|
|---|
| 807 | The default value is `16384`.
|
|---|
| 808 |
|
|---|
| 809 | * `options.Promise`:
|
|---|
| 810 | Promise constructor that will be used
|
|---|
| 811 | for promises returned by all methods.
|
|---|
| 812 | If you set this option,
|
|---|
| 813 | please be aware that some promise implementations
|
|---|
| 814 | (including native promises)
|
|---|
| 815 | may cause your process to die
|
|---|
| 816 | with out-of-memory exceptions.
|
|---|
| 817 | Defaults to [bluebird's implementation][promise],
|
|---|
| 818 | which does not have that problem.
|
|---|
| 819 |
|
|---|
| 820 | ## Is it possible to pause parsing or serialisation from calling code?
|
|---|
| 821 |
|
|---|
| 822 | Yes it is!
|
|---|
| 823 | Both [`walk`](#bfjwalk-stream-options)
|
|---|
| 824 | and [`eventify`](#bfjeventify-data-options)
|
|---|
| 825 | decorate their returned event emitters
|
|---|
| 826 | with a `pause` method
|
|---|
| 827 | that will prevent any further events being emitted.
|
|---|
| 828 | The `pause` method itself
|
|---|
| 829 | returns a `resume` function
|
|---|
| 830 | that you can call to indicate
|
|---|
| 831 | that processing should continue.
|
|---|
| 832 |
|
|---|
| 833 | For example:
|
|---|
| 834 |
|
|---|
| 835 | ```js
|
|---|
| 836 | const bfj = require('bfj');
|
|---|
| 837 | const emitter = bfj.walk(fs.createReadStream(path), options);
|
|---|
| 838 |
|
|---|
| 839 | // Later, when you want to pause parsing:
|
|---|
| 840 |
|
|---|
| 841 | const resume = emitter.pause();
|
|---|
| 842 |
|
|---|
| 843 | // Then when you want to resume:
|
|---|
| 844 |
|
|---|
| 845 | resume();
|
|---|
| 846 | ```
|
|---|
| 847 |
|
|---|
| 848 | ## Can it handle [newline-delimited JSON (NDJSON)](http://ndjson.org/)?
|
|---|
| 849 |
|
|---|
| 850 | Yes.
|
|---|
| 851 | If you pass the `ndjson` [option](#options-for-parsing-functions)
|
|---|
| 852 | to `bfj.walk`, `bfj.match` or `bfj.parse`,
|
|---|
| 853 | newline characters at the root level
|
|---|
| 854 | will act as delimiters between
|
|---|
| 855 | discrete JSON values:
|
|---|
| 856 |
|
|---|
| 857 | * `bfj.walk` will emit a `bfj.events.endLine` event
|
|---|
| 858 | each time it encounters a newline character.
|
|---|
| 859 |
|
|---|
| 860 | * `bfj.match` will just ignore the newlines
|
|---|
| 861 | while it continues looking for matching items.
|
|---|
| 862 |
|
|---|
| 863 | * `bfj.parse` will resolve with the first value
|
|---|
| 864 | and pause the underlying stream.
|
|---|
| 865 | If it's called again with the same stream,
|
|---|
| 866 | it will resume processing
|
|---|
| 867 | and resolve with the second value.
|
|---|
| 868 | To parse the entire stream,
|
|---|
| 869 | calls should be made sequentially one-at-a-time
|
|---|
| 870 | until the returned promise
|
|---|
| 871 | resolves to `undefined`
|
|---|
| 872 | (`undefined` is not a valid JSON token).
|
|---|
| 873 |
|
|---|
| 874 | `bfj.unpipe` and `bfj.read` will not parse NDJSON.
|
|---|
| 875 |
|
|---|
| 876 | ## Why does it default to bluebird promises?
|
|---|
| 877 |
|
|---|
| 878 | Until version `4.2.4`,
|
|---|
| 879 | native promises were used.
|
|---|
| 880 | But they were found
|
|---|
| 881 | to cause out-of-memory errors
|
|---|
| 882 | when serialising large amounts of data to JSON,
|
|---|
| 883 | due to [well-documented problems
|
|---|
| 884 | with the native promise implementation](https://alexn.org/blog/2017/10/11/javascript-promise-leaks-memory.html).
|
|---|
| 885 | So in version `5.0.0`,
|
|---|
| 886 | bluebird promises were used instead.
|
|---|
| 887 | In version `5.1.0`,
|
|---|
| 888 | an option was added
|
|---|
| 889 | that enables callers to specify
|
|---|
| 890 | the promise constructor to use.
|
|---|
| 891 | Use it at your own risk.
|
|---|
| 892 |
|
|---|
| 893 | ## Can I specify a different promise implementation?
|
|---|
| 894 |
|
|---|
| 895 | Yes.
|
|---|
| 896 | Just pass the `Promise` option
|
|---|
| 897 | to any method.
|
|---|
| 898 | If you get out-of-memory errors
|
|---|
| 899 | when using that option,
|
|---|
| 900 | consider changing your promise implementation.
|
|---|
| 901 |
|
|---|
| 902 | ## Is there a change log?
|
|---|
| 903 |
|
|---|
| 904 | [Yes][history].
|
|---|
| 905 |
|
|---|
| 906 | ## How do I set up the dev environment?
|
|---|
| 907 |
|
|---|
| 908 | The development environment
|
|---|
| 909 | relies on [Node.js][node],
|
|---|
| 910 | [ESLint],
|
|---|
| 911 | [Mocha],
|
|---|
| 912 | [Chai],
|
|---|
| 913 | [Proxyquire] and
|
|---|
| 914 | [Spooks].
|
|---|
| 915 | Assuming that
|
|---|
| 916 | you already have
|
|---|
| 917 | node and NPM
|
|---|
| 918 | set up,
|
|---|
| 919 | you just need
|
|---|
| 920 | to run
|
|---|
| 921 | `npm install`
|
|---|
| 922 | to install
|
|---|
| 923 | all of the dependencies
|
|---|
| 924 | as listed in `package.json`.
|
|---|
| 925 |
|
|---|
| 926 | You can
|
|---|
| 927 | lint the code
|
|---|
| 928 | with the command
|
|---|
| 929 | `npm run lint`.
|
|---|
| 930 |
|
|---|
| 931 | You can
|
|---|
| 932 | run the tests
|
|---|
| 933 | with the command
|
|---|
| 934 | `npm test`.
|
|---|
| 935 |
|
|---|
| 936 | ## What versions of Node.js does it support?
|
|---|
| 937 |
|
|---|
| 938 | As of [version `7.0.0`](HISTORY.md#700),
|
|---|
| 939 | only Node.js versions 8 or greater
|
|---|
| 940 | are supported.
|
|---|
| 941 |
|
|---|
| 942 | Between versions [`3.0.0`](HISTORY.md#300)
|
|---|
| 943 | and [`6.1.2`](HISTORY.md#612),
|
|---|
| 944 | only Node.js versions 6 or greater
|
|---|
| 945 | were supported.
|
|---|
| 946 |
|
|---|
| 947 | Until [version `2.1.2`](HISTORY.md#212),
|
|---|
| 948 | only Node.js versions 4 or greater
|
|---|
| 949 | were supported.
|
|---|
| 950 |
|
|---|
| 951 | ## What license is it released under?
|
|---|
| 952 |
|
|---|
| 953 | [MIT][license].
|
|---|
| 954 |
|
|---|
| 955 | [ci-image]: https://secure.travis-ci.org/philbooth/bfj.png?branch=master
|
|---|
| 956 | [ci-status]: http://travis-ci.org/#!/philbooth/bfj
|
|---|
| 957 | [sax]: http://en.wikipedia.org/wiki/Simple_API_for_XML
|
|---|
| 958 | [promise]: http://bluebirdjs.com/docs/api-reference.html
|
|---|
| 959 | [bfj-collections]: https://github.com/hash-bang/bfj-collections
|
|---|
| 960 | [eventemitter]: https://nodejs.org/api/events.html#events_class_eventemitter
|
|---|
| 961 | [readable]: https://nodejs.org/api/stream.html#stream_readable_streams
|
|---|
| 962 | [writable]: https://nodejs.org/api/stream.html#stream_writable_streams
|
|---|
| 963 | [pipe]: https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
|
|---|
| 964 | [regexp-test]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
|
|---|
| 965 | [reviver]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter
|
|---|
| 966 | [space]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument
|
|---|
| 967 | [history]: HISTORY.md
|
|---|
| 968 | [node]: https://nodejs.org/en/
|
|---|
| 969 | [eslint]: http://eslint.org/
|
|---|
| 970 | [mocha]: https://mochajs.org/
|
|---|
| 971 | [chai]: http://chaijs.com/
|
|---|
| 972 | [proxyquire]: https://github.com/thlorenz/proxyquire
|
|---|
| 973 | [spooks]: https://gitlab.com/philbooth/spooks.js
|
|---|
| 974 | [license]: COPYING
|
|---|