punkave/read-async-bson

Name: read-async-bson

Owner: P'unk Avenue

Description: Read a stream of BSON documents, passing each one to a callback and waiting patiently until that callback is finished with its work.

Created: 2015-03-07 16:05:34.0

Updated: 2015-03-07 16:49:57.0

Pushed: 2015-03-07 16:49:57.0

Homepage: null

Size: 140

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status

read-async-bson

What's it do?

Let's read a stream of BSON documents from standard input, passing each one through a callback and waiting patiently until that callback is finished with its work:

reader = require('read-async-bson');

rn reader(
from: process.stdin },
nction(item, callback) {
// One BSON document, converted as a JavaScript object
console.log(item);
return callback(null);

nction(err) {
// Called on error or at end of stream
if (!err) {
  console.log('end of stream');
} else {
  console.error(err);
}


read-async-bson reads incoming BSON documents from any node stream, and invokes a callback for each one. Documents are delivered one at a time. The next document will not be delivered until you invoke a callback to signal that you have handled the previous one.

When an error occurs, or the end of the stream is reached, a final callback is invoked.

What exactly is a “stream of BSON documents”?

Documents in the BSON format, concatenated end to end.

But what about writing BSON streams?

That is already easy without any help from me:

bson = require('bson');
BSON = bson.BSONPure.BSON;

stream = process.stdout;

data = [ ... tons and tons of objects ... ]
.forEach(function(item) {
ream.write(BSON.serialize(item, false, true, false));

For more information, see the BSON npm module docs.

Performance

This implementation is extremely fast because it is designed to avoid allocating memory too often. It is especially fast in raw mode (see below).

Options
Why no stream interface?

When you just want to handle incoming objects in a strict order, streams just make things complicated and introduce bugs. We've had no end of trouble with node object streams that don't respect pause() in the streams1 interface, or stall out without emitting readable again in the streams2 interface, or… wait, why are we using these again?

“What I really want is to stream an entire MongoDB database.”

mongo-dump-stream covers that use case nicely. Check it out.

About P'unk Avenue and Apostrophe

read-async-bson was created at P'unk Avenue for use in many projects built with Apostrophe, an open-source content management system built on node.js. If you like read-async-bson you should definitely check out apostrophenow.org.

Support

Feel free to open issues on github.

Changelog
CHANGES IN 0.1.2

Fixed package description.

CHANGES IN 0.1.1

Do not invoke the final callback on end-of-stream unless we have actually consumed all of the data in our buffer.

CHANGES IN 0.1.0

Initial release. Refactored from mongo-dump-stream. New tests written, covering more cases.

LICENSE

Copyright (c) 2015 P'unk Avenue LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.