{"name":"bson","description":"A bson parser for node.js and the browser","keywords":["mongodb","bson","parser"],"version":"0.4.2","author":{"name":"Christian Amor Kvalheim","email":"christkv@gmail.com"},"contributors":[],"repository":{"type":"git","url":"git://github.com/mongodb/js-bson.git"},"bugs":{"url":"https://github.com/mongodb/js-bson/issues"},"devDependencies":{"nodeunit":"0.9.0","gleak":"0.2.3","one":"2.X.X","benchmark":"1.0.0","colors":"1.1.0"},"config":{"native":false},"main":"./lib/bson/index","directories":{"lib":"./lib/bson"},"engines":{"node":">=0.6.19"},"scripts":{"test":"nodeunit ./test/node"},"browser":"lib/bson/bson.js","licenses":[{"type":"Apache License, Version 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0"}],"gitHead":"0a82f7eeb7359ad6cae405e6d49457fc6d2bbab4","homepage":"https://github.com/mongodb/js-bson","_id":"bson@0.4.2","_shasum":"4fe0e8ba9de74c8b640fd1eb4682d99b85940814","_from":".","_npmVersion":"2.7.4","_nodeVersion":"0.12.2","_npmUser":{"name":"christkv","email":"christkv@gmail.com"},"maintainers":[{"name":"octave","email":"chinsay@gmail.com"},{"name":"christkv","email":"christkv@gmail.com"}],"dist":{"shasum":"4fe0e8ba9de74c8b640fd1eb4682d99b85940814","tarball":"http://registry-npm.ywwl.com/bson/download/bson-0.4.2.tgz","integrity":"sha512-FobkdumlFSF6OtGoQZ+OMwE3nVhh4oDIkm9Ad62PJ3PjWOGt54bLbKhgpObCUVCTp3znm0ktS5UyN1FzN5tKgA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC5dL3eLiXIAZJ0EK593yeMInTzCJs97LpRfJGXJezbBAIhAIpgh/91NL8VCmm1dac3/071r49qaHy5sxD6MQn0CnHF"}],"size":84238,"noattachment":false,"key":"/bson/-/bson-0.4.2.tgz"},"deprecated":"Fixed a critical issue with BSON serialization documented in CVE-2019-2391, see https://bit.ly/2KcpXdo for more details","readme":"# BSON parser\n\nBSON is short for \"Binary JSON,\" and is the binary-encoded serialization of JSON-like documents.\nYou can learn more about it in [the specification](http://bsonspec.org).\n\n### Table of Contents\n\n- [Usage](#usage)\n- [Bugs/Feature Requests](#bugs--feature-requests)\n- [Installation](#installation)\n- [Documentation](#documentation)\n- [FAQ](#faq)\n\n## Bugs / Feature Requests\n\nThink you've found a bug? Want to see a new feature in `bson`? Please open a case in our issue management tool, JIRA:\n\n1. Create an account and login: [jira.mongodb.org](https://jira.mongodb.org)\n2. Navigate to the NODE project: [jira.mongodb.org/browse/NODE](https://jira.mongodb.org/browse/NODE)\n3. Click **Create Issue** - Please provide as much information as possible about the issue and how to reproduce it.\n\nBug reports in JIRA for the NODE driver project are **public**.\n\n## Usage\n\nTo build a new version perform the following operations:\n\n```\nnpm install\nnpm run build\n```\n\n### Node.js or Bundling Usage\n\nWhen using a bundler or Node.js you can import bson using the package name:\n\n```js\nimport { BSON, EJSON, ObjectId } from 'bson';\n// or:\n// const { BSON, EJSON, ObjectId } = require('bson');\n\nconst bytes = BSON.serialize({ _id: new ObjectId() });\nconsole.log(bytes);\nconst doc = BSON.deserialize(bytes);\nconsole.log(EJSON.stringify(doc));\n// {\"_id\":{\"$oid\":\"...\"}}\n```\n\n### Browser Usage\n\nIf you are working directly in the browser without a bundler please use the `.mjs` bundle like so:\n\n```html\n<script type=\"module\">\n  import { BSON, EJSON, ObjectId } from './lib/bson.mjs';\n\n  const bytes = BSON.serialize({ _id: new ObjectId() });\n  console.log(bytes);\n  const doc = BSON.deserialize(bytes);\n  console.log(EJSON.stringify(doc));\n  // {\"_id\":{\"$oid\":\"...\"}}\n</script>\n```\n\n## Installation\n\n```sh\nnpm install bson\n```\n\n### MongoDB Node.js Driver Version Compatibility\n\nOnly the following version combinations with the [MongoDB Node.js Driver](https://github.com/mongodb/node-mongodb-native) are considered stable.\n\n|               | `bson@1.x` | `bson@4.x` | `bson@5.x` | `bson@6.x` |\n| ------------- | ---------- | ---------- | ---------- | ---------- |\n| `mongodb@6.x` | N/A        | N/A        | N/A        | ✓          |\n| `mongodb@5.x` | N/A        | N/A        | ✓          | N/A        |\n| `mongodb@4.x` | N/A        | ✓          | N/A        | N/A        |\n| `mongodb@3.x` | ✓          | N/A        | N/A        | N/A        |\n\n## Documentation\n\n### BSON\n\n[API documentation](https://mongodb.github.io/node-mongodb-native/Next/modules/BSON.html)\n\n<a name=\"EJSON\"></a>\n\n### EJSON\n\n- [EJSON](#EJSON)\n\n  - [.parse(text, [options])](#EJSON.parse)\n\n  - [.stringify(value, [replacer], [space], [options])](#EJSON.stringify)\n\n  - [.serialize(bson, [options])](#EJSON.serialize)\n\n  - [.deserialize(ejson, [options])](#EJSON.deserialize)\n\n<a name=\"EJSON.parse\"></a>\n\n#### _EJSON_.parse(text, [options])\n\n| Param             | Type                 | Default           | Description                                                                        |\n| ----------------- | -------------------- | ----------------- | ---------------------------------------------------------------------------------- |\n| text              | <code>string</code>  |                   |                                                                                    |\n| [options]         | <code>object</code>  |                   | Optional settings                                                                  |\n| [options.relaxed] | <code>boolean</code> | <code>true</code> | Attempt to return native JS types where possible, rather than BSON types (if true) |\n\nParse an Extended JSON string, constructing the JavaScript value or object described by that\nstring.\n\n**Example**\n\n```js\nconst { EJSON } = require('bson');\nconst text = '{ \"int32\": { \"$numberInt\": \"10\" } }';\n\n// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }\nconsole.log(EJSON.parse(text, { relaxed: false }));\n\n// prints { int32: 10 }\nconsole.log(EJSON.parse(text));\n```\n\n<a name=\"EJSON.stringify\"></a>\n\n#### _EJSON_.stringify(value, [replacer], [space], [options])\n\n| Param             | Type                                        | Default           | Description                                                                                                                                                                                                                                                                                                                                        |\n| ----------------- | ------------------------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| value             | <code>object</code>                         |                   | The value to convert to extended JSON                                                                                                                                                                                                                                                                                                              |\n| [replacer]        | <code>function</code> \\| <code>array</code> |                   | A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string |\n| [space]           | <code>string</code> \\| <code>number</code>  |                   | A String or Number object that's used to insert white space into the output JSON string for readability purposes.                                                                                                                                                                                                                                  |\n| [options]         | <code>object</code>                         |                   | Optional settings                                                                                                                                                                                                                                                                                                                                  |\n| [options.relaxed] | <code>boolean</code>                        | <code>true</code> | Enabled Extended JSON's `relaxed` mode                                                                                                                                                                                                                                                                                                             |\n| [options.legacy]  | <code>boolean</code>                        | <code>true</code> | Output in Extended JSON v1                                                                                                                                                                                                                                                                                                                         |\n\nConverts a BSON document to an Extended JSON string, optionally replacing values if a replacer\nfunction is specified or optionally including only the specified properties if a replacer array\nis specified.\n\n**Example**\n\n```js\nconst { EJSON } = require('bson');\nconst Int32 = require('mongodb').Int32;\nconst doc = { int32: new Int32(10) };\n\n// prints '{\"int32\":{\"$numberInt\":\"10\"}}'\nconsole.log(EJSON.stringify(doc, { relaxed: false }));\n\n// prints '{\"int32\":10}'\nconsole.log(EJSON.stringify(doc));\n```\n\n<a name=\"EJSON.serialize\"></a>\n\n#### _EJSON_.serialize(bson, [options])\n\n| Param     | Type                | Description                                          |\n| --------- | ------------------- | ---------------------------------------------------- |\n| bson      | <code>object</code> | The object to serialize                              |\n| [options] | <code>object</code> | Optional settings passed to the `stringify` function |\n\nSerializes an object to an Extended JSON string, and reparse it as a JavaScript object.\n\n<a name=\"EJSON.deserialize\"></a>\n\n#### _EJSON_.deserialize(ejson, [options])\n\n| Param     | Type                | Description                                  |\n| --------- | ------------------- | -------------------------------------------- |\n| ejson     | <code>object</code> | The Extended JSON object to deserialize      |\n| [options] | <code>object</code> | Optional settings passed to the parse method |\n\nDeserializes an Extended JSON object into a plain JavaScript object with native/BSON types\n\n## Error Handling\n\nIt is our recommendation to use `BSONError.isBSONError()` checks on errors and to avoid relying on parsing `error.message` and `error.name` strings in your code. We guarantee `BSONError.isBSONError()` checks will pass according to semver guidelines, but errors may be sub-classed or their messages may change at any time, even patch releases, as we see fit to increase the helpfulness of the errors.\n\nAny new errors we add to the driver will directly extend an existing error class and no existing error will be moved to a different parent class outside of a major release.\nThis means `BSONError.isBSONError()` will always be able to accurately capture the errors that our BSON library throws.\n\nHypothetical example: A collection in our Db has an issue with UTF-8 data:\n\n```ts\nlet documentCount = 0;\nconst cursor = collection.find({}, { utf8Validation: true });\ntry {\n  for await (const doc of cursor) documentCount += 1;\n} catch (error) {\n  if (BSONError.isBSONError(error)) {\n    console.log(`Found the troublemaker UTF-8!: ${documentCount} ${error.message}`);\n    return documentCount;\n  }\n  throw error;\n}\n```\n\n## React Native\n\nBSON vendors the required polyfills for `TextEncoder`, `TextDecoder`, `atob`, `btoa` imported from React Native and therefore doesn't expect users to polyfill these. One additional polyfill, `crypto.getRandomValues` is recommended and can be installed with the following command:\n\n```sh\nnpm install --save react-native-get-random-values\n```\n\nThe following snippet should be placed at the top of the entrypoint (by default this is the root `index.js` file) for React Native projects using the BSON library. These lines must be placed for any code that imports `BSON`.\n\n```typescript\n// Required Polyfills For ReactNative\nimport 'react-native-get-random-values';\n```\n\nFinally, import the `BSON` library like so:\n\n```typescript\nimport { BSON, EJSON } from 'bson';\n```\n\nThis will cause React Native to import the `node_modules/bson/lib/bson.rn.cjs` bundle (see the `\"react-native\"` setting we have in the `\"exports\"` section of our [package.json](./package.json).)\n\n### Technical Note about React Native module import\n\nThe `\"exports\"` definition in our `package.json` will result in BSON's CommonJS bundle being imported in a React Native project instead of the ES module bundle. Importing the CommonJS bundle is necessary because BSON's ES module bundle of BSON uses top-level await, which is not supported syntax in [React Native's runtime hermes](https://hermesengine.dev/).\n\n## FAQ\n\n#### Why does `undefined` get converted to `null`?\n\nThe `undefined` BSON type has been [deprecated for many years](http://bsonspec.org/spec.html), so this library has dropped support for it. Use the `ignoreUndefined` option (for example, from the [driver](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect) ) to instead remove `undefined` keys.\n\n#### How do I add custom serialization logic?\n\nThis library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize.\n\n```javascript\nconst BSON = require('bson');\n\nclass CustomSerialize {\n  toBSON() {\n    return 42;\n  }\n}\n\nconst obj = { answer: new CustomSerialize() };\n// \"{ answer: 42 }\"\nconsole.log(BSON.deserialize(BSON.serialize(obj)));\n```\n","publish_time":1434465833260,"_cnpm_publish_time":1434465833260,"dist-tags":{"5x":"5.5.1","alpha":"7.0.0-alpha.2","latest":"7.3.4","legacy":"6.9.1"}}