Gathering detailed insights and metrics for bfj-node4
Gathering detailed insights and metrics for bfj-node4
npm install bfj-node4
Typescript
Module System
Min. Node Version
Node Version
NPM Version
95.7
Supply Chain
100
Quality
75.1
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
72,302,641
Last Day
12,984
Last Week
19,608
Last Month
190,534
Last Year
3,295,556
164 Stars
422 Commits
4 Forks
4 Watching
4 Branches
5 Contributors
Minified
Minified + Gzipped
Latest Version
5.3.1
Package Id
bfj-node4@5.3.1
Size
35.95 kB
NPM Version
5.6.0
Node Version
8.9.4
Publised On
18 Mar 2018
Cumulative downloads
Total Downloads
Last day
52%
12,984
Compared to previous day
Last week
-49.2%
19,608
Compared to previous week
Last month
-2.3%
190,534
Compared to previous month
Last year
-26.1%
3,295,556
Compared to previous year
3
7
Big-Friendly JSON. Asynchronous streaming functions for large JSON data sets.
If you need to parse huge JSON strings or stringify huge JavaScript data sets, it monopolises the event loop and can lead to out-of-memory exceptions. BFJ implements asynchronous functions and uses pre-allocated fixed-length arrays to try and alleviate those issues.
No.
BFJ yields frequently
to avoid monopolising the event loop,
interrupting its own execution
to let other event handlers run.
The frequency of those yields
can be controlled with the yieldRate
option,
but fundamentally it is not designed for speed.
Furthermore,
when serialising data to a stream,
BFJ uses a fixed-length buffer
to avoid exhausting available memory.
Whenever that buffer is full,
serialisation is paused
until the receiving stream processes some more data,
regardless of the value of yieldRate
.
You can control the size of the buffer
using the bufferLength
option
but really,
if you need quick results,
BFJ is not for you.
Eight functions are exported.
Four are concerned with parsing, or turning JSON strings into JavaScript data:
read
asynchronously parses
a JSON file from disk.
parse
and unpipe
are for asynchronously parsing
streams of JSON.
walk
asynchronously walks
a stream,
emitting events
as it encounters
JSON tokens.
Analagous to a
SAX parser.
The other four functions handle the reverse transformations, serialising JavaScript data to JSON:
write
asynchronously serialises data
to a JSON file on disk.
stringify
asynchronously serialises data
to a JSON string.
streamify
asynchronously serialises data
to a stream of JSON.
eventify
asynchronously traverses
a data structure
depth-first,
emitting events
as it encounters items.
By default
it coerces
promises, buffers and iterables
to JSON-friendly values.
If you're using npm:
npm i bfj --save
Or if you just want the git repo:
git clone git@github.com:philbooth/bfj.git
1const bfj = require('bfj'); 2 3bfj.read(path, options) 4 .then(data => { 5 // :) 6 }) 7 .catch(error => { 8 // :( 9 });
read
returns a bluebird promise and
asynchronously parses
a JSON file
from disk.
It takes two arguments; the path to the JSON file and an options object.
If there are no syntax errors, the returned promise is resolved with the parsed data. If syntax errors occur, the promise is rejected with the first error.
1const bfj = require('bfj'); 2 3bfj.write(path, data, options) 4 .then(() => { 5 // :) 6 }) 7 .catch(error => { 8 // :( 9 });
write
returns a bluebird promise
and asynchronously serialises a data structure
to a JSON file on disk.
The promise is resolved
when the file has been written,
or rejected with the error
if writing failed.
It takes three arguments; the path to the JSON file, the data structure to serialise and an options object.
1const bfj = require('bfj'); 2 3// By passing a readable stream to bfj.parse(): 4bfj.parse(fs.createReadStream(path), options) 5 .then(data => { 6 // :) 7 }) 8 .catch(error => { 9 // :( 10 }); 11 12// ...or by passing the result from bfj.unpipe() to stream.pipe(): 13request({ url }).pipe(bfj.unpipe((error, data) => { 14 if (error) { 15 // :( 16 } else { 17 // :) 18 } 19}))
parse
returns a bluebird promise
and asynchronously parses
a stream of JSON data.
It takes two arguments; a readable stream from which the JSON will be parsed and an options object.
If there are no syntax errors, the returned promise is resolved with the parsed data. If syntax errors occur, the promise is rejected with the first error.
unpipe
returns a writable stream
that can be passed to stream.pipe
,
then parses JSON data
read from the stream.
It takes two arguments; a callback function that will be called after parsing is complete and an options object.
If there are no errors, the callback is invoked with the result as the second argument. If errors occur, the first error is passed the callback as the first argument.
1const bfj = require('bfj'); 2 3bfj.stringify(data, options) 4 .then(json => { 5 // :) 6 }) 7 .catch(error => { 8 // :( 9 });
stringify
returns a bluebird promise and
asynchronously serialises a data structure
to a JSON string.
The promise is resolved
to the JSON string
when serialisation is complete.
It takes two arguments; the data structure to serialise and an options object.
1const bfj = require('bfj'); 2 3const stream = bfj.streamify(data, options); 4 5// Get data out of the stream with event handlers 6stream.on('data', chunk => { /* ... */ }); 7stream.on('end', () => { /* ... */); 8stream.on('dataError', () => { /* ... */); 9 10// ...or you can pipe it to another stream 11stream.pipe(someOtherStream);
streamify
returns a readable stream
and asynchronously serialises
a data structure to JSON,
pushing the result
to the returned stream.
It takes two arguments; the data structure to serialise and an options object.
1const bfj = require('bfj'); 2 3const emitter = bfj.walk(fs.createReadStream(path), options); 4 5emitter.on(bfj.events.array, () => { /* ... */ }); 6emitter.on(bfj.events.object, () => { /* ... */ }); 7emitter.on(bfj.events.property, name => { /* ... */ }); 8emitter.on(bfj.events.string, value => { /* ... */ }); 9emitter.on(bfj.events.number, value => { /* ... */ }); 10emitter.on(bfj.events.literal, value => { /* ... */ }); 11emitter.on(bfj.events.endArray, () => { /* ... */ }); 12emitter.on(bfj.events.endObject, () => { /* ... */ }); 13emitter.on(bfj.events.error, error => { /* ... */ }); 14emitter.on(bfj.events.end, () => { /* ... */ });
walk
returns an event emitter
and asynchronously walks
a stream of JSON data,
emitting events
as it encounters
tokens.
It takes two arguments; a readable stream from which the JSON will be read and an options object.
The emitted events
are defined
as public properties
of an object,
bfj.events
:
bfj.events.array
indicates that
an array context
has been entered
by encountering
the [
character.
bfj.events.endArray
indicates that
an array context
has been left
by encountering
the ]
character.
bfj.events.object
indicates that
an object context
has been entered
by encountering
the {
character.
bfj.events.endObject
indicates that
an object context
has been left
by encountering
the }
character.
bfj.events.property
indicates that
a property
has been encountered
in an object.
The listener
will be passed
the name of the property
as its argument
and the next event
to be emitted
will represent
the property's value.
bfj.events.string
indicates that
a string
has been encountered.
The listener
will be passed
the value
as its argument.
bfj.events.number
indicates that
a number
has been encountered.
The listener
will be passed
the value
as its argument.
bfj.events.literal
indicates that
a JSON literal
(either true
, false
or null
)
has been encountered.
The listener
will be passed
the value
as its argument.
bfj.events.error
indicates that
an error has occurred.
The error may be due to
invalid syntax on the incoming stream
or caught from one of the event handlers
in user code.
The listener
will be passed
the Error
instance
as its argument.
bfj.events.end
indicates that
the end of the input
has been reached
and the stream is closed.
bfj.events.endLine
indicates that a root-level newline character
has been encountered in an NDJSON stream.
Only emitted if the ndjson
option is set.
If you are using bfj.walk
to sequentially parse items in an array,
you might also be interested in
the bfj-collections module.
1const bfj = require('bfj'); 2 3const emitter = bfj.eventify(data, options); 4 5emitter.on(bfj.events.array, () => { /* ... */ }); 6emitter.on(bfj.events.object, () => { /* ... */ }); 7emitter.on(bfj.events.property, name => { /* ... */ }); 8emitter.on(bfj.events.string, value => { /* ... */ }); 9emitter.on(bfj.events.number, value => { /* ... */ }); 10emitter.on(bfj.events.literal, value => { /* ... */ }); 11emitter.on(bfj.events.endArray, () => { /* ... */ }); 12emitter.on(bfj.events.endObject, () => { /* ... */ }); 13emitter.on(bfj.events.error, () => { /* ... */ }); 14emitter.on(bfj.events.end, () => { /* ... */ });
eventify
returns an event emitter
and asynchronously traverses
a data structure depth-first,
emitting events as it
encounters items.
By default it coerces
promises, buffers and iterables
to JSON-friendly values.
It takes two arguments; the data structure to traverse and an options object.
The emitted events
are defined
as public properties
of an object,
bfj.events
:
bfj.events.array
indicates that
an array
has been encountered.
bfj.events.endArray
indicates that
the end of an array
has been encountered.
bfj.events.object
indicates that
an object
has been encountered.
bfj.events.endObject
indicates that
the end of an object
has been encountered.
bfj.events.property
indicates that
a property
has been encountered
in an object.
The listener
will be passed
the name of the property
as its argument
and the next event
to be emitted
will represent
the property's value.
bfj.events.string
indicates that
a string
has been encountered.
The listener
will be passed
the value
as its argument.
bfj.events.number
indicates that
a number
has been encountered.
The listener
will be passed
the value
as its argument.
bfj.events.literal
indicates that
a JSON literal
(either true
, false
or null
)
has been encountered.
The listener
will be passed
the value
as its argument.
bfj.events.error
indicates that
an error has occurred.
The error may be due to
a circular reference
encountered in the data
or caught from one of the event handlers
in user code.
The listener
will be passed
the Error
instance
as its argument.
bfj.events.end
indicates that
the end of the data
has been reached and
no further events
will be emitted.
options.reviver
:
Transformation function,
invoked depth-first
against the parsed
data structure.
This option
is analagous to the
reviver parameter for JSON.parse.
options.yieldRate
:
The number of data items to process
before yielding to the event loop.
Smaller values yield to the event loop more frequently,
meaning less time will be consumed by bfj per tick
but the overall parsing time will be slower.
Larger values yield to the event loop less often,
meaning slower tick times but faster overall parsing time.
The default value is 16384
.
options.Promise
:
Promise constructor that will be used
for promises returned by all methods.
If you set this option,
please be aware that some promise implementations
(including native promises)
may cause your process to die
with out-of-memory exceptions.
Defaults to bluebird's implementation,
which does not have that problem.
options.ndjson
:
If set to true
,
newline characters at the root level
will be treated as delimiters between
discrete chunks of JSON.
See NDJSON for more information.
options.space
:
Indentation string
or the number of spaces
to indent
each nested level by.
This option
is analagous to the
space parameter for JSON.stringify.
options.promises
:
By default,
promises are coerced
to their resolved value.
Set this property
to 'ignore'
for improved performance
if you don't need
to coerce promises.
options.buffers
:
By default,
buffers are coerced
using their toString
method.
Set this property
to 'ignore'
for improved performance
if you don't need
to coerce buffers.
options.maps
:
By default,
maps are coerced
to plain objects.
Set this property
to 'ignore'
for improved performance
if you don't need
to coerce maps.
options.iterables
:
By default,
other iterables
(i.e. not arrays, strings or maps)
are coerced
to arrays.
Set this property
to 'ignore'
for improved performance
if you don't need
to coerce iterables.
options.circular
:
By default,
circular references
will cause the write
to fail.
Set this property
to 'ignore'
if you'd prefer
to silently skip past
circular references
in the data.
options.bufferLength
:
The length of the write buffer.
Smaller values use less memory
but may result in a slower serialisation time.
The default value is 1024
.
options.yieldRate
:
The number of data items to process
before yielding to the event loop.
Smaller values yield to the event loop more frequently,
meaning less time will be consumed by bfj per tick
but the overall serialisation time will be slower.
Larger values yield to the event loop less often,
meaning slower tick times but faster overall serialisation time.
The default value is 16384
.
options.Promise
:
Promise constructor that will be used
for promises returned by all methods.
If you set this option,
please be aware that some promise implementations
(including native promises)
may cause your process to die
with out-of-memory exceptions.
Defaults to bluebird's implementation,
which does not have that problem.
Yes it is!
Both walk
and eventify
decorate their returned event emitters
with a pause
method
that will prevent any further events being emitted.
The pause
method itself
returns a resume
function
that you can call to indicate
that processing should continue.
For example:
1const bfj = require('bfj'); 2const emitter = bfj.walk(fs.createReadStream(path), options); 3 4// Later, when you want to pause parsing: 5 6const resume = emitter.pause(); 7 8// Then when you want to resume: 9 10resume();
Yes.
If you pass the ndjson
option
to bfj.walk
or bfj.parse
,
newline characters at the root level
will act as delimiters between
discrete JSON values:
bfj.walk
will emit a bfj.events.endLine
event
each time it encounters a newline character.
bfj.parse
will resolve with the first value
and pause the underlying stream.
If it's called again with the same stream,
it will resume processing
and resolve with the second value.
To parse the entire stream,
calls should be made sequentially one-at-a-time
until the returned promise
resolves to undefined
(undefined
is not a valid JSON token).
bfj.unpipe
and bfj.read
will not parse NDJSON.
Until version 4.2.4
,
native promises were used.
But they were found
to cause out-of-memory errors
when serialising large amounts of data to JSON,
due to well-documented problems
with the native promise implementation.
So in version 5.0.0
,
bluebird promises were used instead.
In version 5.1.0
,
an option was added
that enables callers to specify
the promise constructor to use.
Use it at your own risk.
Yes.
Just pass the Promise
option
to any method.
If you get out-of-memory errors
when using that option,
consider changing your promise implementation.
Yes.
The development environment
relies on Node.js,
ESLint,
Mocha,
Chai,
Proxyquire and
Spooks.
Assuming that
you already have
node and NPM
set up,
you just need
to run
npm install
to install
all of the dependencies
as listed in package.json
.
You can
lint the code
with the command
npm run lint
.
You can
run the tests
with the command
npm test
.
Versions 4 and later.
MIT.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
project is archived
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More