GithubHelp home page GithubHelp logo

mpaland / bsonfy Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 6.0 21 KB

Ultrafast BSON typescript serializer and parser

License: MIT License

TypeScript 99.33% JavaScript 0.67%
bson bson-format typescript parser serializer deserializer json

bsonfy's Introduction

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! โœ…

License

bsonfy is written under the MIT license.

bsonfy's People

Contributors

mpaland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bsonfy's Issues

Support ArrayBuffer / SharedArrayBuffer / Uint8Array

Hi,

I am using bsonify and I have noticed some differences from the https://github.com/mongodb/js-bson which I take as reference implementation.

Code like this works in js-bson

      var image = fs.readFileSync(path.join(root, 'assets/small.png'));
      serialize(image);

But with bsonfy one has to

      var image = fs.readFileSync(path.join(root, 'assets/small.png'));
      serialize(Uint8Array(image.buffer));

What should be the path to support more of these translations just like js-bson does to have 1-1 compatbility ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.