{"name":"bson","description":"A bson parser for node.js and the browser","keywords":["mongodb","bson","parser"],"types":"bson.d.ts","version":"5.0.0-alpha.0","author":{"name":"The MongoDB NodeJS Team","email":"dbx-node@mongodb.com"},"license":"Apache-2.0","contributors":[],"repository":{"type":"git","url":"git+https://github.com/mongodb/js-bson.git"},"bugs":{"url":"https://jira.mongodb.org/projects/NODE/issues/"},"devDependencies":{"@istanbuljs/nyc-config-typescript":"^1.0.2","@microsoft/api-extractor":"^7.33.7","@rollup/plugin-node-resolve":"^15.0.1","@rollup/plugin-typescript":"^10.0.1","@types/chai":"^4.3.4","@types/mocha":"^10.0.1","@types/node":"^18.11.12","@types/sinon":"^10.0.13","@types/sinon-chai":"^3.2.9","@typescript-eslint/eslint-plugin":"^5.46.0","@typescript-eslint/parser":"^5.46.0","benchmark":"^2.1.4","chai":"^4.3.7","chalk":"^5.1.2","eslint":"^8.29.0","eslint-config-prettier":"^8.5.0","eslint-plugin-prettier":"^4.2.1","eslint-plugin-tsdoc":"^0.2.17","magic-string":"^0.27.0","mocha":"10.1.0","node-fetch":"^3.2.10","nyc":"^15.1.0","prettier":"^2.8.1","rimraf":"^3.0.2","rollup":"^3.7.1","sinon":"^15.0.0","sinon-chai":"^3.7.0","source-map-support":"^0.5.21","standard-version":"^9.5.0","ts-node":"^10.9.1","tsd":"^0.25.0","typescript":"^4.9.4","typescript-cached-transpile":"0.0.6","uuid":"^9.0.0","v8-profiler-next":"^1.9.0"},"tsd":{"directory":"test/types","compilerOptions":{"strict":true,"target":"esnext","module":"commonjs","moduleResolution":"node"}},"config":{"native":false},"main":"./lib/bson.cjs","module":"./lib/bson.mjs","browser":"./lib/bson.mjs","exports":{"browser":"./lib/bson.mjs","import":"./lib/bson.mjs","require":"./lib/bson.cjs"},"engines":{"node":">=14.20.1"},"scripts":{"pretest":"npm run build","test":"npm run check:node && npm run check:web","check:node":"WEB=false mocha test/node","check:tsd":"npm run build:dts && tsd","check:web":"WEB=true mocha test/node","build:ts":"node ./node_modules/typescript/bin/tsc","build:dts":"npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && rimraf 'lib/**/*.d.ts*' lib/parser lib/utils","build:bundle":"rollup -c rollup.config.mjs","build":"npm run build:dts && npm run build:bundle","check:lint":"eslint -v && eslint --ext '.js,.ts' --max-warnings=0 src test && npm run build:dts && npm run check:tsd","format":"eslint --ext '.js,.ts' src test --fix","check:coverage":"nyc --check-coverage npm run check:node","prepare":"node etc/prepare.js","release":"standard-version -i HISTORY.md"},"readme":"# BSON parser\n\nBSON is short for \"Binary JSON,\" and is the binary-encoded serialization of JSON-like documents. You can learn more about it in [the specification](http://bsonspec.org).\n\nThis browser version of the BSON parser is compiled using [rollup](https://rollupjs.org/) and the current version is pre-compiled in the `dist` directory.\n\nThis is the default BSON parser, however, there is a C++ Node.js addon version as well that does not support the browser. It can be found at [mongod-js/bson-ext](https://github.com/mongodb-js/bson-ext).\n\n### Table of Contents\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 all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the Core Server (i.e. SERVER) 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 (no bundling)\nA simple example of how to use BSON in `Node.js`:\n\n```js\nconst BSON = require('bson');\nconst Long = BSON.Long;\n\n// Serialize a document\nconst doc = { long: Long.fromNumber(100) };\nconst data = BSON.serialize(doc);\nconsole.log('data:', data);\n\n// Deserialize the resulting Buffer\nconst doc_2 = BSON.deserialize(data);\nconsole.log('doc_2:', doc_2);\n```\n\n### Browser (no bundling)\n\nIf you are not using a bundler like webpack, you can include `dist/bson.bundle.js` using a script tag.  It includes polyfills for built-in node types like `Buffer`.\n\n```html\n<script src=\"./dist/bson.bundle.js\"></script>\n\n<script>\n  function start() {\n    // Get the Long type\n    const Long = BSON.Long;\n\n    // Serialize a document\n    const doc = { long: Long.fromNumber(100) }\n    const data = BSON.serialize(doc);\n    console.log('data:', data);\n\n    // De serialize it again\n    const doc_2 = BSON.deserialize(data);\n    console.log('doc_2:', doc_2);\n  }\n</script>\n```\n\n### Using webpack\n\nIf using webpack, you can use your normal import/require syntax of your project to pull in the `bson` library.\n\nES6 Example:\n\n```js\nimport { Long, serialize, deserialize } from 'bson';\n\n// Serialize a document\nconst doc = { long: Long.fromNumber(100) };\nconst data = serialize(doc);\nconsole.log('data:', data);\n\n// De serialize it again\nconst doc_2 = deserialize(data);\nconsole.log('doc_2:', doc_2);\n```\n\nES5 Example:\n\n```js\nconst BSON = require('bson');\nconst Long = BSON.Long;\n\n// Serialize a document\nconst doc = { long: Long.fromNumber(100) };\nconst data = BSON.serialize(doc);\nconsole.log('data:', data);\n\n// Deserialize the resulting Buffer\nconst doc_2 = BSON.deserialize(data);\nconsole.log('doc_2:', doc_2);\n```\n\nDepending on your settings, webpack will under the hood resolve to one of the following:\n\n- `dist/bson.browser.esm.js` If your project is in the browser and using ES6 modules (Default for `webworker` and `web` targets)\n- `dist/bson.browser.umd.js` If your project is in the browser and not using ES6 modules\n- `dist/bson.esm.js` If your project is in Node.js and using ES6 modules (Default for `node` targets)\n- `lib/bson.js` (the normal include path) If your project is in Node.js and not using ES6 modules\n\nFor more information, see [this page on webpack's `resolve.mainFields`](https://webpack.js.org/configuration/resolve/#resolvemainfields) and [the `package.json` for this project](./package.json#L52)\n\n### Usage with Angular\n\nStarting with Angular 6, Angular CLI removed the shim for `global` and other node built-in variables (original comment [here](https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063)). If you are using BSON with Angular, you may need to add the following shim to your `polyfills.ts` file:\n\n```js\n// In polyfills.ts\n(window as any).global = window;\n```\n\n- [Original Comment by Angular CLI](https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063)\n- [Original Source for Solution](https://stackoverflow.com/a/50488337/4930088)\n\n## Installation\n\n`npm install bson`\n\n## Documentation\n\n### Objects\n\n<dl>\n<dt><a href=\"#EJSON\">EJSON</a> : <code>object</code></dt>\n<dd></dd>\n</dl>\n\n### Functions\n\n<dl>\n<dt><a href=\"#setInternalBufferSize\">setInternalBufferSize(size)</a></dt>\n<dd><p>Sets the size of the internal serialization buffer.</p>\n</dd>\n<dt><a href=\"#serialize\">serialize(object)</a> ⇒ <code>Buffer</code></dt>\n<dd><p>Serialize a Javascript object.</p>\n</dd>\n<dt><a href=\"#serializeWithBufferAndIndex\">serializeWithBufferAndIndex(object, buffer)</a> ⇒ <code>Number</code></dt>\n<dd><p>Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.</p>\n</dd>\n<dt><a href=\"#deserialize\">deserialize(buffer)</a> ⇒ <code>Object</code></dt>\n<dd><p>Deserialize data as BSON.</p>\n</dd>\n<dt><a href=\"#calculateObjectSize\">calculateObjectSize(object)</a> ⇒ <code>Number</code></dt>\n<dd><p>Calculate the bson size for a passed in Javascript object.</p>\n</dd>\n<dt><a href=\"#deserializeStream\">deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options])</a> ⇒ <code>Number</code></dt>\n<dd><p>Deserialize stream data as BSON documents.</p>\n</dd>\n</dl>\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\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```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<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```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<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<a name=\"setInternalBufferSize\"></a>\n\n### setInternalBufferSize(size)\n\n| Param | Type | Description |\n| --- | --- | --- |\n| size | <code>number</code> | The desired size for the internal serialization buffer |\n\nSets the size of the internal serialization buffer.\n\n<a name=\"serialize\"></a>\n\n### serialize(object)\n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| object | <code>Object</code> |  | the Javascript object to serialize. |\n| [options.checkKeys] | <code>Boolean</code> |  | the serializer will check if keys are valid. |\n| [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |\n| [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |\n\nSerialize a Javascript object.\n\n**Returns**: <code>Buffer</code> - returns the Buffer object containing the serialized object.\n<a name=\"serializeWithBufferAndIndex\"></a>\n\n### serializeWithBufferAndIndex(object, buffer)\n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| object | <code>Object</code> |  | the Javascript object to serialize. |\n| buffer | <code>Buffer</code> |  | the Buffer you pre-allocated to store the serialized BSON object. |\n| [options.checkKeys] | <code>Boolean</code> |  | the serializer will check if keys are valid. |\n| [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |\n| [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |\n| [options.index] | <code>Number</code> |  | the index in the buffer where we wish to start serializing into. |\n\nSerialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.\n\n**Returns**: <code>Number</code> - returns the index pointing to the last written byte in the buffer.\n<a name=\"deserialize\"></a>\n\n### deserialize(buffer)\n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| buffer | <code>Buffer</code> |  | the buffer containing the serialized set of BSON documents. |\n| [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |\n| [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |\n| [options.promoteLongs] | <code>Object</code> | <code>true</code> | when deserializing a Long will fit it into a Number if it's smaller than 53 bits |\n| [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |\n| [options.promoteValues] | <code>Object</code> | <code>false</code> | when deserializing will promote BSON values to their Node.js closest equivalent types. |\n| [options.fieldsAsRaw] | <code>Object</code> | <code></code> | allow to specify if there what fields we wish to return as unserialized raw buffer. |\n| [options.bsonRegExp] | <code>Object</code> | <code>false</code> | return BSON regular expressions as BSONRegExp instances. |\n| [options.allowObjectSmallerThanBufferSize] | <code>boolean</code> | <code>false</code> | allows the buffer to be larger than the parsed BSON object |\n\nDeserialize data as BSON.\n\n**Returns**: <code>Object</code> - returns the deserialized Javascript Object.\n<a name=\"calculateObjectSize\"></a>\n\n### calculateObjectSize(object)\n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| object | <code>Object</code> |  | the Javascript object to calculate the BSON byte size for. |\n| [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |\n| [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |\n\nCalculate the bson size for a passed in Javascript object.\n\n**Returns**: <code>Number</code> - returns the number of bytes the BSON object will take up.\n<a name=\"deserializeStream\"></a>\n\n### deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options])\n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| data | <code>Buffer</code> |  | the buffer containing the serialized set of BSON documents. |\n| startIndex | <code>Number</code> |  | the start index in the data Buffer where the deserialization is to start. |\n| numberOfDocuments | <code>Number</code> |  | number of documents to deserialize. |\n| documents | <code>Array</code> |  | an array where to store the deserialized documents. |\n| docStartIndex | <code>Number</code> |  | the index in the documents array from where to start inserting documents. |\n| [options] | <code>Object</code> |  | additional options used for the deserialization. |\n| [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |\n| [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |\n| [options.promoteLongs] | <code>Object</code> | <code>true</code> | when deserializing a Long will fit it into a Number if it's smaller than 53 bits |\n| [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |\n| [options.promoteValues] | <code>Object</code> | <code>false</code> | when deserializing will promote BSON values to their Node.js closest equivalent types. |\n| [options.fieldsAsRaw] | <code>Object</code> | <code></code> | allow to specify if there what fields we wish to return as unserialized raw buffer. |\n| [options.bsonRegExp] | <code>Object</code> | <code>false</code> | return BSON regular expressions as BSONRegExp instances. |\n\nDeserialize stream data as BSON documents.\n\n**Returns**: <code>Number</code> - returns the next index in the buffer after deserialization **x** numbers of documents.\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","readmeFilename":"README.md","gitHead":"c0ce081051a335bec9fa65624b173e8a07fb408f","homepage":"https://github.com/mongodb/js-bson#readme","_id":"bson@5.0.0-alpha.0","_nodeVersion":"16.17.0","_npmVersion":"8.18.0","dist":{"integrity":"sha512-xHsQpp8VfELLLD0V0h8iDM6dFbmNgbRrP724FMm6ocQg//g0vzo2kSkLyUMHXS6eBVOH5l8U3fTnRkVJf3ZwRQ==","shasum":"fac0dbf9882803117a8e54fc3084ec73d0d5e35f","tarball":"http://registry-npm.ywwl.com/bson/download/bson-5.0.0-alpha.0.tgz","fileCount":38,"unpackedSize":1256739,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDweLO8CJusj33OP12gXK6Vw4GZSdl8jJYiSspPNAMgfAIhAKGNjM126/lPmF0wXEmKT1yyn/2UR1tDMo166UV2tjH+"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjoI4HACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqPJhAAk8ZmNnsJT1VCHwS1wGngMTvpMk9C8P3YAJ61QDNf4+jQY46d\r\nofMhCC1P62Aux5h36earyI9+aFI8dCMTWFiFwASAhNPekqQGa3h4cXRDv0Gj\r\nmbTVJPjfGdmWtHQfkO1aODcVJpXTQmFJQB8unLAN8h9gLR8BkxbzFNKsN8Ed\r\nWswXE2+e7tlBP03GOkSmuMEJfVX+BrhyEROTGzUBFF+TfJSBqr91jAOt3fI4\r\nnKDGyBJLM+UR5G8iBwLUdInchPf8HbuuvcOUabk3sXkWylzQAOk3dyYH6TwU\r\nTEXYxp0wbFEzeLTAq7w+0UETh0jNC9LVYmRRVU+u4amm79Gj5l3VnZgtfL2H\r\n1HXVSihkI18xeAD0H1/EsMD+QdO/H79d7zD4zosD23SNLGD6RsXbCKLBrrFh\r\nN7TapHqvFcGs3b9s+OBCayPsnLkNQvpjyAE7Axr+zn9BwTMjQnDqGx9IRO29\r\nmIr3HijNhPl32YMY1hifh9IDGOlJdLYVnFqi/ndDrN3VJMwytWG9pyHvERh6\r\nviNmMm2hwanh3FN4kfNAVxjwUtoFiw2loEmrS1aYP49ABQVbgTNZt9LYIVVB\r\nwfETeDyhC7y/lwG9Qf1k0s3dSNQnuUz2jReHnD1PSrX87nY4UD0/+yPWm8ui\r\nJ0klhHq86K7pva5Altc+mSIdc8+xmZn/krk=\r\n=uBqh\r\n-----END PGP SIGNATURE-----\r\n","size":211683,"noattachment":false,"key":"/bson/-/bson-5.0.0-alpha.0.tgz"},"_npmUser":{"name":"nbbeeken","email":"neal.beeken@mongodb.com"},"directories":{},"maintainers":[{"name":"mbroadst","email":"mbroadst@gmail.com"},{"name":"durran","email":"durran@gmail.com"},{"name":"pearsb1","email":"bailey.pearson@gmail.com"},{"name":"nbbeeken","email":"neal.beeken@mongodb.com"},{"name":"kmahar","email":"kaitlin.mahar@mongodb.com"},{"name":"dariakp","email":"daria.pardue@mongodb.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/bson_5.0.0-alpha.0_1671466503455_0.4549371929662993"},"_hasShrinkwrap":false,"publish_time":1671466503681,"_cnpm_publish_time":1671466503681,"dist-tags":{"5x":"5.5.1","alpha":"7.0.0-alpha.2","latest":"7.3.4","legacy":"6.9.1"}}