npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

read-async-bson

v0.1.2

Published

Read BSON documents from a stream, invoking a callback for each one and waiting patiently until it is processed.

Downloads

15

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:

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

return reader(
  { from: process.stdin },
  function(item, callback) {
    // One BSON document, converted as a JavaScript object
    console.log(item);
    return callback(null);
  },
  function(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:

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

var stream = process.stdout;

var data = [ ... tons and tons of objects ... ]
data.forEach(function(item) {
  stream.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

  • from specifies the input stream. If you do not specify from, then process.stdin is assumed.

  • raw: normally BSON documents are parsed into JavaScript objects for you. If raw is true, then raw buffers containing BSON documents are delivered to your callback, which is much, much faster if you plan to just hand them to the mongodb driver. (Note that you must specify raw mode in your insert call.)

  • maxDocumentSize: by default BSON documents may be up to 16,777,216 bytes in length. This is MongoDB's limit. If you wish you may specify a different limit. Mostly useful for testing and low-memory environments.

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.