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

@ferrisk/bsonfy

v1.0.4

Published

Ultrafast BSON serializer/parser

Downloads

10

Readme

bsonfy - ultrafast BSON parser

npm npm Github Issues Github Releases GitHub license

bsonfy is an ultrafast serializer and deserializer for the BSON format.
It is written in clean typescript and has no other lib dependencies. BSON is mainly used as compact transport format for (JSON) objects.

Motivation

I needed a simple, fast and clean (typescript) module to generate and parse BSON for storing JSON objects in files efficiently.
There are some parsers around (2016/06), mainly the primary one of the mongodb project. But I found that it's really not lightweight enough and too slow for mobile usage.
A further requirement was using typed arrays (Uint8Array) instead of nodejs buffers, to get this baby portable and running in browsers, too.

Design goals:

  • Written in typescript
  • Fast and lightweight parser
  • Very easy to use, just one include module, NO dependencies
  • tslint warning free, clean code
  • Unit tested, around 50 passing test cases
  • Rocksolid (I hope so)
  • MIT license

Usage

Using this module is rather simple. Copy or (npm) install bsonfy to your project and use it like:

import { BSON } from 'bsonfy';

// create a test document
let doc = { id: 10, time: new BSON.UTC(), arr: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]) };

// serialize the document
let bson = BSON.serialize(doc);

// and deserialize it, using BSON.UTC objects as time representation
let orig = BSON.deserialize(bson, true);

API

Basically the API consists of just two static methods to serialize/deserialize objects to/from BSON format:

BSON serialization and deserialiation

BSON.serialize(object)

  • @param {Object} object The Javascript object to serialize
  • @return {Uint8Array} returns an Uint8Array in BSON format.
    Unknown objects are ignored in serialization.

BSON.deserialize(buffer, useUTC)

  • @param {Uint8Array} buffer An Uint8Array containing the BSON data
  • @param {Boolean} useUTC Optional, if set a BSON.UTC object is created for 'UTC datetime' instead of a normal JS Date object. Defaults to false
  • @return {Object} returns the deserialized Javascript object or undefined in case of a parsing error (unsupported BSON element etc.)

UTC

bson.ObjectId.isValid(id) - Returns true if id is a valid number or hexadecimal string representing an ObjectId. bson.ObjectId.createFromHexString(hexString) - Returns the ObjectId the hexString represents. bson.ObjectId.createFromTime(time) - Returns an ObjectId containing the passed time.

  • time - A Unix timestamp (number of seconds since the epoch).

UUID

bson.ObjectId.isValid(id) - Returns true if id is a valid number or hexadecimal string representing an ObjectId. bson.ObjectId.createFromHexString(hexString) - Returns the ObjectId the hexString represents. bson.ObjectId.createFromTime(time) - Returns an ObjectId containing the passed time.

  • time - A Unix timestamp (number of seconds since the epoch).

ObjectId

bson.ObjectId.isValid(id) - Returns true if id is a valid number or hexadecimal string representing an ObjectId. bson.ObjectId.createFromHexString(hexString) - Returns the ObjectId the hexString represents. bson.ObjectId.createFromTime(time) - Returns an ObjectId containing the passed time.

  • time - A Unix timestamp (number of seconds since the epoch).

Unsupported elements

The following BSON elements are currently not supported (and lead to a deserialiation error):

  • JavaScript code
  • Min key
  • Max key
  • Regular expression (implemented, but untested yet - so don't rely on it)

Caveats

  • 64-bit integer BSON values are converted to the Javascript Number type.
    However, Javascript supports integer precision up to 2^53 as maximum size. If a parsed 64-bit integer exceeds this size, floating point rounding errors may occur!

Test suite

bsonfy is using the mocha test suite for testing. To do all tests just run npm run test.

Contributing

If you find any bugs, have any comments, improvements or suggestions:

  1. Create an issue and describe your idea
  2. Fork it
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Publish the branch (git push origin my-new-feature)
  6. Create a new pull request
  7. Profit! :white_check_mark:

License

bsonfy is written under the MIT license.