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

react-native-bson

v0.0.3

Published

A bson parser for node.js and the browser

Downloads

125

Readme

This is bson for react-native

The method in bson dose not work for me. I got the way from the test code.

If you don't yet know what BSON actually is, read the spec.

This package can be used to serialize JSON documents into the BSON format or the other way around. If you want to use it within the browser, give browserify a try (it will help you add this package to your bundle). The current build is located in the browser_build/bson.js file.

A simple example of how to use BSON in react-native:


import {BSON,Long, ObjectID,Binary,Code,DBRef,Symbol,Double,Timestamp,MaxKey,MinKey} from 'bson'
import {Buffer} from 'buffer'

var bson = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey])
var user = {
    username: 'username',
    password: 'anymore'
}

// Serialize a document
var data = bson.serialize(user, false, true, false)
console.log('data:', data)

// Deserialize the resulting Buffer
// response is return from server by library react-native-fetch-blob
// the type of response.text() is base64
// so far forgot other choices, only leave this code
var userData = bson.deserialize(new Buffer(response.text(),'binary'))
console.log('userData:', userData)

API

The API consists of two simple methods to serialize/deserialize objects to/from BSON format:

Installation

npm i react-native-bson

API

BSON serialization and deserialiation

new bson.BSONPure.BSON() - Creates a new BSON seralizer/deserializer you can use to serialize and deserialize BSON.

  • BSON.serialize(object, checkKeys, asBuffer, serializeFunctions)

    • @param {Object} object the Javascript object to serialize.
    • @param {Boolean} checkKeys the serializer will check if keys are valid.
    • @param {Boolean} asBuffer return the serialized object as a Buffer object (ignore).
    • @param {Boolean} serializeFunctions serialize the javascript functions (default:false)
    • @return {TypedArray/Array} returns a TypedArray or Array depending on what your browser supports
  • BSON.deserialize(buffer, options, isArray)

    • Options
      • evalFunctions {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
      • cacheFunctions {Boolean, default:false}, cache evaluated functions for reuse.
      • cacheFunctionsCrc32 {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
      • promoteBuffers {Boolean, default:false}, deserialize Binary data directly into node.js Buffer object.
    • @param {TypedArray/Array} a TypedArray/Array containing the BSON data
    • @param {Object} [options] additional options used for the deserialization.
    • @param {Boolean} [isArray] ignore used for recursive parsing.
    • @return {Object} returns the deserialized Javascript Object.

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).

var objectId = new bson.ObjectId(id) - Creates a new ObjectId.

  • id - Must either be a 24-character hex string or a 12 byte binary string.

objectId.toJSON() objectId.toString() objectId.toHexString() - Returns a hexadecimal string representation of the ObjectId.

objectId.equals(otherObjectId) - Returns true if the ObjectIds are the same, false otherwise.

objectId.getTimestamp() - Returns a Date object containing the time the objectId was created for.

objectId.getTimestamp() - Returns a Date object containing the time the objectId contains.