重构了云函数

This commit is contained in:
walkpan
2025-10-19 20:10:19 +08:00
parent cfb6819bc7
commit 1c40c10210
1942 changed files with 363888 additions and 435 deletions

View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,376 @@
# BSON parser
BSON 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).
This browser version of the BSON parser is compiled using [rollup](https://rollupjs.org/) and the current version is pre-compiled in the `dist` directory.
This 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).
### Table of Contents
- [Usage](#usage)
- [Bugs/Feature Requests](#bugs--feature-requests)
- [Installation](#installation)
- [Documentation](#documentation)
- [FAQ](#faq)
## Bugs / Feature Requests
Think you've found a bug? Want to see a new feature in `bson`? Please open a case in our issue management tool, JIRA:
1. Create an account and login: [jira.mongodb.org](https://jira.mongodb.org)
2. Navigate to the NODE project: [jira.mongodb.org/browse/NODE](https://jira.mongodb.org/browse/NODE)
3. Click **Create Issue** - Please provide as much information as possible about the issue and how to reproduce it.
Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the Core Server (i.e. SERVER) project are **public**.
## Usage
To build a new version perform the following operations:
```
npm install
npm run build
```
### Node (no bundling)
A simple example of how to use BSON in `Node.js`:
```js
const BSON = require('bson');
const Long = BSON.Long;
// Serialize a document
const doc = { long: Long.fromNumber(100) };
const data = BSON.serialize(doc);
console.log('data:', data);
// Deserialize the resulting Buffer
const doc_2 = BSON.deserialize(data);
console.log('doc_2:', doc_2);
```
### Browser (no bundling)
If 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`.
```html
<script src="./dist/bson.bundle.js"></script>
<script>
function start() {
// Get the Long type
const Long = BSON.Long;
// Serialize a document
const doc = { long: Long.fromNumber(100) }
const data = BSON.serialize(doc);
console.log('data:', data);
// De serialize it again
const doc_2 = BSON.deserialize(data);
console.log('doc_2:', doc_2);
}
</script>
```
### Using webpack
If using webpack, you can use your normal import/require syntax of your project to pull in the `bson` library.
ES6 Example:
```js
import { Long, serialize, deserialize } from 'bson';
// Serialize a document
const doc = { long: Long.fromNumber(100) };
const data = serialize(doc);
console.log('data:', data);
// De serialize it again
const doc_2 = deserialize(data);
console.log('doc_2:', doc_2);
```
ES5 Example:
```js
const BSON = require('bson');
const Long = BSON.Long;
// Serialize a document
const doc = { long: Long.fromNumber(100) };
const data = BSON.serialize(doc);
console.log('data:', data);
// Deserialize the resulting Buffer
const doc_2 = BSON.deserialize(data);
console.log('doc_2:', doc_2);
```
Depending on your settings, webpack will under the hood resolve to one of the following:
- `dist/bson.browser.esm.js` If your project is in the browser and using ES6 modules (Default for `webworker` and `web` targets)
- `dist/bson.browser.umd.js` If your project is in the browser and not using ES6 modules
- `dist/bson.esm.js` If your project is in Node.js and using ES6 modules (Default for `node` targets)
- `lib/bson.js` (the normal include path) If your project is in Node.js and not using ES6 modules
For 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)
### Usage with Angular
Starting 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:
```js
// In polyfills.ts
(window as any).global = window;
```
- [Original Comment by Angular CLI](https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063)
- [Original Source for Solution](https://stackoverflow.com/a/50488337/4930088)
## Installation
`npm install bson`
## Documentation
### Objects
<dl>
<dt><a href="#EJSON">EJSON</a> : <code>object</code></dt>
<dd></dd>
</dl>
### Functions
<dl>
<dt><a href="#setInternalBufferSize">setInternalBufferSize(size)</a></dt>
<dd><p>Sets the size of the internal serialization buffer.</p>
</dd>
<dt><a href="#serialize">serialize(object)</a> ⇒ <code>Buffer</code></dt>
<dd><p>Serialize a Javascript object.</p>
</dd>
<dt><a href="#serializeWithBufferAndIndex">serializeWithBufferAndIndex(object, buffer)</a> ⇒ <code>Number</code></dt>
<dd><p>Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.</p>
</dd>
<dt><a href="#deserialize">deserialize(buffer)</a> ⇒ <code>Object</code></dt>
<dd><p>Deserialize data as BSON.</p>
</dd>
<dt><a href="#calculateObjectSize">calculateObjectSize(object)</a> ⇒ <code>Number</code></dt>
<dd><p>Calculate the bson size for a passed in Javascript object.</p>
</dd>
<dt><a href="#deserializeStream">deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options])</a> ⇒ <code>Number</code></dt>
<dd><p>Deserialize stream data as BSON documents.</p>
</dd>
</dl>
<a name="EJSON"></a>
### EJSON
* [EJSON](#EJSON)
* [.parse(text, [options])](#EJSON.parse)
* [.stringify(value, [replacer], [space], [options])](#EJSON.stringify)
* [.serialize(bson, [options])](#EJSON.serialize)
* [.deserialize(ejson, [options])](#EJSON.deserialize)
<a name="EJSON.parse"></a>
#### *EJSON*.parse(text, [options])
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| text | <code>string</code> | | |
| [options] | <code>object</code> | | Optional settings |
| [options.relaxed] | <code>boolean</code> | <code>true</code> | Attempt to return native JS types where possible, rather than BSON types (if true) |
Parse an Extended JSON string, constructing the JavaScript value or object described by that
string.
**Example**
```js
const { EJSON } = require('bson');
const text = '{ "int32": { "$numberInt": "10" } }';
// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
console.log(EJSON.parse(text, { relaxed: false }));
// prints { int32: 10 }
console.log(EJSON.parse(text));
```
<a name="EJSON.stringify"></a>
#### *EJSON*.stringify(value, [replacer], [space], [options])
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| value | <code>object</code> | | The value to convert to extended JSON |
| [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 |
| [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. |
| [options] | <code>object</code> | | Optional settings |
| [options.relaxed] | <code>boolean</code> | <code>true</code> | Enabled Extended JSON's `relaxed` mode |
| [options.legacy] | <code>boolean</code> | <code>true</code> | Output in Extended JSON v1 |
Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
function is specified or optionally including only the specified properties if a replacer array
is specified.
**Example**
```js
const { EJSON } = require('bson');
const Int32 = require('mongodb').Int32;
const doc = { int32: new Int32(10) };
// prints '{"int32":{"$numberInt":"10"}}'
console.log(EJSON.stringify(doc, { relaxed: false }));
// prints '{"int32":10}'
console.log(EJSON.stringify(doc));
```
<a name="EJSON.serialize"></a>
#### *EJSON*.serialize(bson, [options])
| Param | Type | Description |
| --- | --- | --- |
| bson | <code>object</code> | The object to serialize |
| [options] | <code>object</code> | Optional settings passed to the `stringify` function |
Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
<a name="EJSON.deserialize"></a>
#### *EJSON*.deserialize(ejson, [options])
| Param | Type | Description |
| --- | --- | --- |
| ejson | <code>object</code> | The Extended JSON object to deserialize |
| [options] | <code>object</code> | Optional settings passed to the parse method |
Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
<a name="setInternalBufferSize"></a>
### setInternalBufferSize(size)
| Param | Type | Description |
| --- | --- | --- |
| size | <code>number</code> | The desired size for the internal serialization buffer |
Sets the size of the internal serialization buffer.
<a name="serialize"></a>
### serialize(object)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| object | <code>Object</code> | | the Javascript object to serialize. |
| [options.checkKeys] | <code>Boolean</code> | | the serializer will check if keys are valid. |
| [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |
| [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |
Serialize a Javascript object.
**Returns**: <code>Buffer</code> - returns the Buffer object containing the serialized object.
<a name="serializeWithBufferAndIndex"></a>
### serializeWithBufferAndIndex(object, buffer)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| object | <code>Object</code> | | the Javascript object to serialize. |
| buffer | <code>Buffer</code> | | the Buffer you pre-allocated to store the serialized BSON object. |
| [options.checkKeys] | <code>Boolean</code> | | the serializer will check if keys are valid. |
| [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |
| [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |
| [options.index] | <code>Number</code> | | the index in the buffer where we wish to start serializing into. |
Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
**Returns**: <code>Number</code> - returns the index pointing to the last written byte in the buffer.
<a name="deserialize"></a>
### deserialize(buffer)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| buffer | <code>Buffer</code> | | the buffer containing the serialized set of BSON documents. |
| [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |
| [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |
| [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 |
| [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |
| [options.promoteValues] | <code>Object</code> | <code>false</code> | when deserializing will promote BSON values to their Node.js closest equivalent types. |
| [options.fieldsAsRaw] | <code>Object</code> | <code></code> | allow to specify if there what fields we wish to return as unserialized raw buffer. |
| [options.bsonRegExp] | <code>Object</code> | <code>false</code> | return BSON regular expressions as BSONRegExp instances. |
| [options.allowObjectSmallerThanBufferSize] | <code>boolean</code> | <code>false</code> | allows the buffer to be larger than the parsed BSON object |
Deserialize data as BSON.
**Returns**: <code>Object</code> - returns the deserialized Javascript Object.
<a name="calculateObjectSize"></a>
### calculateObjectSize(object)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| object | <code>Object</code> | | the Javascript object to calculate the BSON byte size for. |
| [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |
| [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |
Calculate the bson size for a passed in Javascript object.
**Returns**: <code>Number</code> - returns the number of bytes the BSON object will take up.
<a name="deserializeStream"></a>
### deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options])
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| data | <code>Buffer</code> | | the buffer containing the serialized set of BSON documents. |
| startIndex | <code>Number</code> | | the start index in the data Buffer where the deserialization is to start. |
| numberOfDocuments | <code>Number</code> | | number of documents to deserialize. |
| documents | <code>Array</code> | | an array where to store the deserialized documents. |
| docStartIndex | <code>Number</code> | | the index in the documents array from where to start inserting documents. |
| [options] | <code>Object</code> | | additional options used for the deserialization. |
| [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |
| [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |
| [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 |
| [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |
| [options.promoteValues] | <code>Object</code> | <code>false</code> | when deserializing will promote BSON values to their Node.js closest equivalent types. |
| [options.fieldsAsRaw] | <code>Object</code> | <code></code> | allow to specify if there what fields we wish to return as unserialized raw buffer. |
| [options.bsonRegExp] | <code>Object</code> | <code>false</code> | return BSON regular expressions as BSONRegExp instances. |
Deserialize stream data as BSON documents.
**Returns**: <code>Number</code> - returns the next index in the buffer after deserialization **x** numbers of documents.
## FAQ
#### Why does `undefined` get converted to `null`?
The `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.
#### How do I add custom serialization logic?
This library looks for `toBSON()` functions on every path, and calls the `toBSON()` function to get the value to serialize.
```javascript
const BSON = require('bson');
class CustomSerialize {
toBSON() {
return 42;
}
}
const obj = { answer: new CustomSerialize() };
// "{ answer: 42 }"
console.log(BSON.deserialize(BSON.serialize(obj)));
```

View File

@@ -0,0 +1,26 @@
{
"name": "bson",
"description": "A bson parser for node.js and the browser",
"keywords": [
"mongodb",
"bson",
"parser"
],
"author": "Christian Amor Kvalheim <christkv@gmail.com>",
"main": "./dist/bson.js",
"license": "Apache-2.0",
"moduleType": [
"globals",
"node"
],
"ignore": [
"**/.*",
"alternate_parsers",
"benchmarks",
"bower_components",
"node_modules",
"test",
"tools"
],
"version": "4.7.2"
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,19 @@
#! /usr/bin/env node
var cp = require('child_process');
var fs = require('fs');
var nodeMajorVersion = +process.version.match(/^v(\d+)\.\d+/)[1];
if (fs.existsSync('src') && nodeMajorVersion >= 10) {
cp.spawnSync('npm', ['run', 'build'], { stdio: 'inherit', shell: true });
} else {
if (!fs.existsSync('lib')) {
console.warn('BSON: No compiled javascript present, the library is not installed correctly.');
if (nodeMajorVersion < 10) {
console.warn(
'This library can only be compiled in nodejs version 10 or later, currently running: ' +
nodeMajorVersion
);
}
}
}

View File

@@ -0,0 +1,426 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UUID = exports.Binary = void 0;
var buffer_1 = require("buffer");
var ensure_buffer_1 = require("./ensure_buffer");
var uuid_utils_1 = require("./uuid_utils");
var utils_1 = require("./parser/utils");
var error_1 = require("./error");
var constants_1 = require("./constants");
/**
* A class representation of the BSON Binary type.
* @public
* @category BSONType
*/
var Binary = /** @class */ (function () {
/**
* Create a new Binary instance.
*
* This constructor can accept a string as its first argument. In this case,
* this string will be encoded using ISO-8859-1, **not** using UTF-8.
* This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
* instead to convert the string to a Buffer using UTF-8 first.
*
* @param buffer - a buffer object containing the binary data.
* @param subType - the option binary type.
*/
function Binary(buffer, subType) {
if (!(this instanceof Binary))
return new Binary(buffer, subType);
if (!(buffer == null) &&
!(typeof buffer === 'string') &&
!ArrayBuffer.isView(buffer) &&
!(buffer instanceof ArrayBuffer) &&
!Array.isArray(buffer)) {
throw new error_1.BSONTypeError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
}
this.sub_type = subType !== null && subType !== void 0 ? subType : Binary.BSON_BINARY_SUBTYPE_DEFAULT;
if (buffer == null) {
// create an empty binary buffer
this.buffer = buffer_1.Buffer.alloc(Binary.BUFFER_SIZE);
this.position = 0;
}
else {
if (typeof buffer === 'string') {
// string
this.buffer = buffer_1.Buffer.from(buffer, 'binary');
}
else if (Array.isArray(buffer)) {
// number[]
this.buffer = buffer_1.Buffer.from(buffer);
}
else {
// Buffer | TypedArray | ArrayBuffer
this.buffer = (0, ensure_buffer_1.ensureBuffer)(buffer);
}
this.position = this.buffer.byteLength;
}
}
/**
* Updates this binary with byte_value.
*
* @param byteValue - a single byte we wish to write.
*/
Binary.prototype.put = function (byteValue) {
// If it's a string and a has more than one character throw an error
if (typeof byteValue === 'string' && byteValue.length !== 1) {
throw new error_1.BSONTypeError('only accepts single character String');
}
else if (typeof byteValue !== 'number' && byteValue.length !== 1)
throw new error_1.BSONTypeError('only accepts single character Uint8Array or Array');
// Decode the byte value once
var decodedByte;
if (typeof byteValue === 'string') {
decodedByte = byteValue.charCodeAt(0);
}
else if (typeof byteValue === 'number') {
decodedByte = byteValue;
}
else {
decodedByte = byteValue[0];
}
if (decodedByte < 0 || decodedByte > 255) {
throw new error_1.BSONTypeError('only accepts number in a valid unsigned byte range 0-255');
}
if (this.buffer.length > this.position) {
this.buffer[this.position++] = decodedByte;
}
else {
var buffer = buffer_1.Buffer.alloc(Binary.BUFFER_SIZE + this.buffer.length);
// Combine the two buffers together
this.buffer.copy(buffer, 0, 0, this.buffer.length);
this.buffer = buffer;
this.buffer[this.position++] = decodedByte;
}
};
/**
* Writes a buffer or string to the binary.
*
* @param sequence - a string or buffer to be written to the Binary BSON object.
* @param offset - specify the binary of where to write the content.
*/
Binary.prototype.write = function (sequence, offset) {
offset = typeof offset === 'number' ? offset : this.position;
// If the buffer is to small let's extend the buffer
if (this.buffer.length < offset + sequence.length) {
var buffer = buffer_1.Buffer.alloc(this.buffer.length + sequence.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
// Assign the new buffer
this.buffer = buffer;
}
if (ArrayBuffer.isView(sequence)) {
this.buffer.set((0, ensure_buffer_1.ensureBuffer)(sequence), offset);
this.position =
offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
}
else if (typeof sequence === 'string') {
this.buffer.write(sequence, offset, sequence.length, 'binary');
this.position =
offset + sequence.length > this.position ? offset + sequence.length : this.position;
}
};
/**
* Reads **length** bytes starting at **position**.
*
* @param position - read from the given position in the Binary.
* @param length - the number of bytes to read.
*/
Binary.prototype.read = function (position, length) {
length = length && length > 0 ? length : this.position;
// Let's return the data based on the type we have
return this.buffer.slice(position, position + length);
};
/**
* Returns the value of this binary as a string.
* @param asRaw - Will skip converting to a string
* @remarks
* This is handy when calling this function conditionally for some key value pairs and not others
*/
Binary.prototype.value = function (asRaw) {
asRaw = !!asRaw;
// Optimize to serialize for the situation where the data == size of buffer
if (asRaw && this.buffer.length === this.position) {
return this.buffer;
}
// If it's a node.js buffer object
if (asRaw) {
return this.buffer.slice(0, this.position);
}
return this.buffer.toString('binary', 0, this.position);
};
/** the length of the binary sequence */
Binary.prototype.length = function () {
return this.position;
};
Binary.prototype.toJSON = function () {
return this.buffer.toString('base64');
};
Binary.prototype.toString = function (format) {
return this.buffer.toString(format);
};
/** @internal */
Binary.prototype.toExtendedJSON = function (options) {
options = options || {};
var base64String = this.buffer.toString('base64');
var subType = Number(this.sub_type).toString(16);
if (options.legacy) {
return {
$binary: base64String,
$type: subType.length === 1 ? '0' + subType : subType
};
}
return {
$binary: {
base64: base64String,
subType: subType.length === 1 ? '0' + subType : subType
}
};
};
Binary.prototype.toUUID = function () {
if (this.sub_type === Binary.SUBTYPE_UUID) {
return new UUID(this.buffer.slice(0, this.position));
}
throw new error_1.BSONError("Binary sub_type \"".concat(this.sub_type, "\" is not supported for converting to UUID. Only \"").concat(Binary.SUBTYPE_UUID, "\" is currently supported."));
};
/** @internal */
Binary.fromExtendedJSON = function (doc, options) {
options = options || {};
var data;
var type;
if ('$binary' in doc) {
if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) {
type = doc.$type ? parseInt(doc.$type, 16) : 0;
data = buffer_1.Buffer.from(doc.$binary, 'base64');
}
else {
if (typeof doc.$binary !== 'string') {
type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
data = buffer_1.Buffer.from(doc.$binary.base64, 'base64');
}
}
}
else if ('$uuid' in doc) {
type = 4;
data = (0, uuid_utils_1.uuidHexStringToBuffer)(doc.$uuid);
}
if (!data) {
throw new error_1.BSONTypeError("Unexpected Binary Extended JSON format ".concat(JSON.stringify(doc)));
}
return type === constants_1.BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
};
/** @internal */
Binary.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
Binary.prototype.inspect = function () {
var asBuffer = this.value(true);
return "new Binary(Buffer.from(\"".concat(asBuffer.toString('hex'), "\", \"hex\"), ").concat(this.sub_type, ")");
};
/**
* Binary default subtype
* @internal
*/
Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
/** Initial buffer default size */
Binary.BUFFER_SIZE = 256;
/** Default BSON type */
Binary.SUBTYPE_DEFAULT = 0;
/** Function BSON type */
Binary.SUBTYPE_FUNCTION = 1;
/** Byte Array BSON type */
Binary.SUBTYPE_BYTE_ARRAY = 2;
/** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
Binary.SUBTYPE_UUID_OLD = 3;
/** UUID BSON type */
Binary.SUBTYPE_UUID = 4;
/** MD5 BSON type */
Binary.SUBTYPE_MD5 = 5;
/** Encrypted BSON type */
Binary.SUBTYPE_ENCRYPTED = 6;
/** Column BSON type */
Binary.SUBTYPE_COLUMN = 7;
/** User BSON type */
Binary.SUBTYPE_USER_DEFINED = 128;
return Binary;
}());
exports.Binary = Binary;
Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
var UUID_BYTE_LENGTH = 16;
/**
* A class representation of the BSON UUID type.
* @public
*/
var UUID = /** @class */ (function (_super) {
__extends(UUID, _super);
/**
* Create an UUID type
*
* @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
*/
function UUID(input) {
var _this = this;
var bytes;
var hexStr;
if (input == null) {
bytes = UUID.generate();
}
else if (input instanceof UUID) {
bytes = buffer_1.Buffer.from(input.buffer);
hexStr = input.__id;
}
else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
bytes = (0, ensure_buffer_1.ensureBuffer)(input);
}
else if (typeof input === 'string') {
bytes = (0, uuid_utils_1.uuidHexStringToBuffer)(input);
}
else {
throw new error_1.BSONTypeError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
}
_this = _super.call(this, bytes, constants_1.BSON_BINARY_SUBTYPE_UUID_NEW) || this;
_this.__id = hexStr;
return _this;
}
Object.defineProperty(UUID.prototype, "id", {
/**
* The UUID bytes
* @readonly
*/
get: function () {
return this.buffer;
},
set: function (value) {
this.buffer = value;
if (UUID.cacheHexString) {
this.__id = (0, uuid_utils_1.bufferToUuidHexString)(value);
}
},
enumerable: false,
configurable: true
});
/**
* Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
* @param includeDashes - should the string exclude dash-separators.
* */
UUID.prototype.toHexString = function (includeDashes) {
if (includeDashes === void 0) { includeDashes = true; }
if (UUID.cacheHexString && this.__id) {
return this.__id;
}
var uuidHexString = (0, uuid_utils_1.bufferToUuidHexString)(this.id, includeDashes);
if (UUID.cacheHexString) {
this.__id = uuidHexString;
}
return uuidHexString;
};
/**
* Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
*/
UUID.prototype.toString = function (encoding) {
return encoding ? this.id.toString(encoding) : this.toHexString();
};
/**
* Converts the id into its JSON string representation.
* A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
*/
UUID.prototype.toJSON = function () {
return this.toHexString();
};
/**
* Compares the equality of this UUID with `otherID`.
*
* @param otherId - UUID instance to compare against.
*/
UUID.prototype.equals = function (otherId) {
if (!otherId) {
return false;
}
if (otherId instanceof UUID) {
return otherId.id.equals(this.id);
}
try {
return new UUID(otherId).id.equals(this.id);
}
catch (_a) {
return false;
}
};
/**
* Creates a Binary instance from the current UUID.
*/
UUID.prototype.toBinary = function () {
return new Binary(this.id, Binary.SUBTYPE_UUID);
};
/**
* Generates a populated buffer containing a v4 uuid
*/
UUID.generate = function () {
var bytes = (0, utils_1.randomBytes)(UUID_BYTE_LENGTH);
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
// Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
return buffer_1.Buffer.from(bytes);
};
/**
* Checks if a value is a valid bson UUID
* @param input - UUID, string or Buffer to validate.
*/
UUID.isValid = function (input) {
if (!input) {
return false;
}
if (input instanceof UUID) {
return true;
}
if (typeof input === 'string') {
return (0, uuid_utils_1.uuidValidateString)(input);
}
if ((0, utils_1.isUint8Array)(input)) {
// check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
if (input.length !== UUID_BYTE_LENGTH) {
return false;
}
return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
}
return false;
};
/**
* Creates an UUID from a hex string representation of an UUID.
* @param hexString - 32 or 36 character hex string (dashes excluded/included).
*/
UUID.createFromHexString = function (hexString) {
var buffer = (0, uuid_utils_1.uuidHexStringToBuffer)(hexString);
return new UUID(buffer);
};
/**
* Converts to a string representation of this Id.
*
* @returns return the 36 character hex string representation.
* @internal
*/
UUID.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
UUID.prototype.inspect = function () {
return "new UUID(\"".concat(this.toHexString(), "\")");
};
return UUID;
}(Binary));
exports.UUID = UUID;
//# sourceMappingURL=binary.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,251 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BSONRegExp = exports.MaxKey = exports.MinKey = exports.Int32 = exports.Double = exports.Timestamp = exports.Long = exports.UUID = exports.ObjectId = exports.Binary = exports.DBRef = exports.BSONSymbol = exports.Map = exports.Code = exports.LongWithoutOverridesClass = exports.EJSON = exports.BSON_INT64_MIN = exports.BSON_INT64_MAX = exports.BSON_INT32_MIN = exports.BSON_INT32_MAX = exports.BSON_DATA_UNDEFINED = exports.BSON_DATA_TIMESTAMP = exports.BSON_DATA_SYMBOL = exports.BSON_DATA_STRING = exports.BSON_DATA_REGEXP = exports.BSON_DATA_OID = exports.BSON_DATA_OBJECT = exports.BSON_DATA_NUMBER = exports.BSON_DATA_NULL = exports.BSON_DATA_MIN_KEY = exports.BSON_DATA_MAX_KEY = exports.BSON_DATA_LONG = exports.BSON_DATA_INT = exports.BSON_DATA_DECIMAL128 = exports.BSON_DATA_DBPOINTER = exports.BSON_DATA_DATE = exports.BSON_DATA_CODE_W_SCOPE = exports.BSON_DATA_CODE = exports.BSON_DATA_BOOLEAN = exports.BSON_DATA_BINARY = exports.BSON_DATA_ARRAY = exports.BSON_BINARY_SUBTYPE_COLUMN = exports.BSON_BINARY_SUBTYPE_ENCRYPTED = exports.BSON_BINARY_SUBTYPE_UUID_NEW = exports.BSON_BINARY_SUBTYPE_UUID = exports.BSON_BINARY_SUBTYPE_USER_DEFINED = exports.BSON_BINARY_SUBTYPE_MD5 = exports.BSON_BINARY_SUBTYPE_FUNCTION = exports.BSON_BINARY_SUBTYPE_DEFAULT = exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = void 0;
exports.deserializeStream = exports.calculateObjectSize = exports.deserialize = exports.serializeWithBufferAndIndex = exports.serialize = exports.setInternalBufferSize = exports.BSONTypeError = exports.BSONError = exports.ObjectID = exports.Decimal128 = void 0;
var buffer_1 = require("buffer");
var binary_1 = require("./binary");
Object.defineProperty(exports, "Binary", { enumerable: true, get: function () { return binary_1.Binary; } });
Object.defineProperty(exports, "UUID", { enumerable: true, get: function () { return binary_1.UUID; } });
var code_1 = require("./code");
Object.defineProperty(exports, "Code", { enumerable: true, get: function () { return code_1.Code; } });
var db_ref_1 = require("./db_ref");
Object.defineProperty(exports, "DBRef", { enumerable: true, get: function () { return db_ref_1.DBRef; } });
var decimal128_1 = require("./decimal128");
Object.defineProperty(exports, "Decimal128", { enumerable: true, get: function () { return decimal128_1.Decimal128; } });
var double_1 = require("./double");
Object.defineProperty(exports, "Double", { enumerable: true, get: function () { return double_1.Double; } });
var ensure_buffer_1 = require("./ensure_buffer");
var extended_json_1 = require("./extended_json");
var int_32_1 = require("./int_32");
Object.defineProperty(exports, "Int32", { enumerable: true, get: function () { return int_32_1.Int32; } });
var long_1 = require("./long");
Object.defineProperty(exports, "Long", { enumerable: true, get: function () { return long_1.Long; } });
var map_1 = require("./map");
Object.defineProperty(exports, "Map", { enumerable: true, get: function () { return map_1.Map; } });
var max_key_1 = require("./max_key");
Object.defineProperty(exports, "MaxKey", { enumerable: true, get: function () { return max_key_1.MaxKey; } });
var min_key_1 = require("./min_key");
Object.defineProperty(exports, "MinKey", { enumerable: true, get: function () { return min_key_1.MinKey; } });
var objectid_1 = require("./objectid");
Object.defineProperty(exports, "ObjectId", { enumerable: true, get: function () { return objectid_1.ObjectId; } });
Object.defineProperty(exports, "ObjectID", { enumerable: true, get: function () { return objectid_1.ObjectId; } });
var error_1 = require("./error");
var calculate_size_1 = require("./parser/calculate_size");
// Parts of the parser
var deserializer_1 = require("./parser/deserializer");
var serializer_1 = require("./parser/serializer");
var regexp_1 = require("./regexp");
Object.defineProperty(exports, "BSONRegExp", { enumerable: true, get: function () { return regexp_1.BSONRegExp; } });
var symbol_1 = require("./symbol");
Object.defineProperty(exports, "BSONSymbol", { enumerable: true, get: function () { return symbol_1.BSONSymbol; } });
var timestamp_1 = require("./timestamp");
Object.defineProperty(exports, "Timestamp", { enumerable: true, get: function () { return timestamp_1.Timestamp; } });
var constants_1 = require("./constants");
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_BYTE_ARRAY", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_BYTE_ARRAY; } });
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_DEFAULT", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_DEFAULT; } });
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_FUNCTION", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_FUNCTION; } });
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_MD5", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_MD5; } });
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_USER_DEFINED", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_USER_DEFINED; } });
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_UUID", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_UUID; } });
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_UUID_NEW", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_UUID_NEW; } });
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_ENCRYPTED", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_ENCRYPTED; } });
Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_COLUMN", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_COLUMN; } });
Object.defineProperty(exports, "BSON_DATA_ARRAY", { enumerable: true, get: function () { return constants_1.BSON_DATA_ARRAY; } });
Object.defineProperty(exports, "BSON_DATA_BINARY", { enumerable: true, get: function () { return constants_1.BSON_DATA_BINARY; } });
Object.defineProperty(exports, "BSON_DATA_BOOLEAN", { enumerable: true, get: function () { return constants_1.BSON_DATA_BOOLEAN; } });
Object.defineProperty(exports, "BSON_DATA_CODE", { enumerable: true, get: function () { return constants_1.BSON_DATA_CODE; } });
Object.defineProperty(exports, "BSON_DATA_CODE_W_SCOPE", { enumerable: true, get: function () { return constants_1.BSON_DATA_CODE_W_SCOPE; } });
Object.defineProperty(exports, "BSON_DATA_DATE", { enumerable: true, get: function () { return constants_1.BSON_DATA_DATE; } });
Object.defineProperty(exports, "BSON_DATA_DBPOINTER", { enumerable: true, get: function () { return constants_1.BSON_DATA_DBPOINTER; } });
Object.defineProperty(exports, "BSON_DATA_DECIMAL128", { enumerable: true, get: function () { return constants_1.BSON_DATA_DECIMAL128; } });
Object.defineProperty(exports, "BSON_DATA_INT", { enumerable: true, get: function () { return constants_1.BSON_DATA_INT; } });
Object.defineProperty(exports, "BSON_DATA_LONG", { enumerable: true, get: function () { return constants_1.BSON_DATA_LONG; } });
Object.defineProperty(exports, "BSON_DATA_MAX_KEY", { enumerable: true, get: function () { return constants_1.BSON_DATA_MAX_KEY; } });
Object.defineProperty(exports, "BSON_DATA_MIN_KEY", { enumerable: true, get: function () { return constants_1.BSON_DATA_MIN_KEY; } });
Object.defineProperty(exports, "BSON_DATA_NULL", { enumerable: true, get: function () { return constants_1.BSON_DATA_NULL; } });
Object.defineProperty(exports, "BSON_DATA_NUMBER", { enumerable: true, get: function () { return constants_1.BSON_DATA_NUMBER; } });
Object.defineProperty(exports, "BSON_DATA_OBJECT", { enumerable: true, get: function () { return constants_1.BSON_DATA_OBJECT; } });
Object.defineProperty(exports, "BSON_DATA_OID", { enumerable: true, get: function () { return constants_1.BSON_DATA_OID; } });
Object.defineProperty(exports, "BSON_DATA_REGEXP", { enumerable: true, get: function () { return constants_1.BSON_DATA_REGEXP; } });
Object.defineProperty(exports, "BSON_DATA_STRING", { enumerable: true, get: function () { return constants_1.BSON_DATA_STRING; } });
Object.defineProperty(exports, "BSON_DATA_SYMBOL", { enumerable: true, get: function () { return constants_1.BSON_DATA_SYMBOL; } });
Object.defineProperty(exports, "BSON_DATA_TIMESTAMP", { enumerable: true, get: function () { return constants_1.BSON_DATA_TIMESTAMP; } });
Object.defineProperty(exports, "BSON_DATA_UNDEFINED", { enumerable: true, get: function () { return constants_1.BSON_DATA_UNDEFINED; } });
Object.defineProperty(exports, "BSON_INT32_MAX", { enumerable: true, get: function () { return constants_1.BSON_INT32_MAX; } });
Object.defineProperty(exports, "BSON_INT32_MIN", { enumerable: true, get: function () { return constants_1.BSON_INT32_MIN; } });
Object.defineProperty(exports, "BSON_INT64_MAX", { enumerable: true, get: function () { return constants_1.BSON_INT64_MAX; } });
Object.defineProperty(exports, "BSON_INT64_MIN", { enumerable: true, get: function () { return constants_1.BSON_INT64_MIN; } });
var extended_json_2 = require("./extended_json");
Object.defineProperty(exports, "EJSON", { enumerable: true, get: function () { return extended_json_2.EJSON; } });
var timestamp_2 = require("./timestamp");
Object.defineProperty(exports, "LongWithoutOverridesClass", { enumerable: true, get: function () { return timestamp_2.LongWithoutOverridesClass; } });
var error_2 = require("./error");
Object.defineProperty(exports, "BSONError", { enumerable: true, get: function () { return error_2.BSONError; } });
Object.defineProperty(exports, "BSONTypeError", { enumerable: true, get: function () { return error_2.BSONTypeError; } });
/** @internal */
// Default Max Size
var MAXSIZE = 1024 * 1024 * 17;
// Current Internal Temporary Serialization Buffer
var buffer = buffer_1.Buffer.alloc(MAXSIZE);
/**
* Sets the size of the internal serialization buffer.
*
* @param size - The desired size for the internal serialization buffer
* @public
*/
function setInternalBufferSize(size) {
// Resize the internal serialization buffer if needed
if (buffer.length < size) {
buffer = buffer_1.Buffer.alloc(size);
}
}
exports.setInternalBufferSize = setInternalBufferSize;
/**
* Serialize a Javascript object.
*
* @param object - the Javascript object to serialize.
* @returns Buffer object containing the serialized object.
* @public
*/
function serialize(object, options) {
if (options === void 0) { options = {}; }
// Unpack the options
var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
var serializeFunctions = typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
var ignoreUndefined = typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
var minInternalBufferSize = typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;
// Resize the internal serialization buffer if needed
if (buffer.length < minInternalBufferSize) {
buffer = buffer_1.Buffer.alloc(minInternalBufferSize);
}
// Attempt to serialize
var serializationIndex = (0, serializer_1.serializeInto)(buffer, object, checkKeys, 0, 0, serializeFunctions, ignoreUndefined, []);
// Create the final buffer
var finishedBuffer = buffer_1.Buffer.alloc(serializationIndex);
// Copy into the finished buffer
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
// Return the buffer
return finishedBuffer;
}
exports.serialize = serialize;
/**
* Serialize a Javascript object using a predefined Buffer and index into the buffer,
* useful when pre-allocating the space for serialization.
*
* @param object - the Javascript object to serialize.
* @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.
* @returns the index pointing to the last written byte in the buffer.
* @public
*/
function serializeWithBufferAndIndex(object, finalBuffer, options) {
if (options === void 0) { options = {}; }
// Unpack the options
var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
var serializeFunctions = typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
var ignoreUndefined = typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
var startIndex = typeof options.index === 'number' ? options.index : 0;
// Attempt to serialize
var serializationIndex = (0, serializer_1.serializeInto)(buffer, object, checkKeys, 0, 0, serializeFunctions, ignoreUndefined);
buffer.copy(finalBuffer, startIndex, 0, serializationIndex);
// Return the index
return startIndex + serializationIndex - 1;
}
exports.serializeWithBufferAndIndex = serializeWithBufferAndIndex;
/**
* Deserialize data as BSON.
*
* @param buffer - the buffer containing the serialized set of BSON documents.
* @returns returns the deserialized Javascript Object.
* @public
*/
function deserialize(buffer, options) {
if (options === void 0) { options = {}; }
return (0, deserializer_1.deserialize)(buffer instanceof buffer_1.Buffer ? buffer : (0, ensure_buffer_1.ensureBuffer)(buffer), options);
}
exports.deserialize = deserialize;
/**
* Calculate the bson size for a passed in Javascript object.
*
* @param object - the Javascript object to calculate the BSON byte size for
* @returns size of BSON object in bytes
* @public
*/
function calculateObjectSize(object, options) {
if (options === void 0) { options = {}; }
options = options || {};
var serializeFunctions = typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
var ignoreUndefined = typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
return (0, calculate_size_1.calculateObjectSize)(object, serializeFunctions, ignoreUndefined);
}
exports.calculateObjectSize = calculateObjectSize;
/**
* Deserialize stream data as BSON documents.
*
* @param data - the buffer containing the serialized set of BSON documents.
* @param startIndex - the start index in the data Buffer where the deserialization is to start.
* @param numberOfDocuments - number of documents to deserialize.
* @param documents - an array where to store the deserialized documents.
* @param docStartIndex - the index in the documents array from where to start inserting documents.
* @param options - additional options used for the deserialization.
* @returns next index in the buffer after deserialization **x** numbers of documents.
* @public
*/
function deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, options) {
var internalOptions = Object.assign({ allowObjectSmallerThanBufferSize: true, index: 0 }, options);
var bufferData = (0, ensure_buffer_1.ensureBuffer)(data);
var index = startIndex;
// Loop over all documents
for (var i = 0; i < numberOfDocuments; i++) {
// Find size of the document
var size = bufferData[index] |
(bufferData[index + 1] << 8) |
(bufferData[index + 2] << 16) |
(bufferData[index + 3] << 24);
// Update options with index
internalOptions.index = index;
// Parse the document at this point
documents[docStartIndex + i] = (0, deserializer_1.deserialize)(bufferData, internalOptions);
// Adjust index by the document size
index = index + size;
}
// Return object containing end index of parsing and list of documents
return index;
}
exports.deserializeStream = deserializeStream;
/**
* BSON default export
* @deprecated Please use named exports
* @privateRemarks
* We want to someday deprecate the default export,
* so none of the new TS types are being exported on the default
* @public
*/
var BSON = {
Binary: binary_1.Binary,
Code: code_1.Code,
DBRef: db_ref_1.DBRef,
Decimal128: decimal128_1.Decimal128,
Double: double_1.Double,
Int32: int_32_1.Int32,
Long: long_1.Long,
UUID: binary_1.UUID,
Map: map_1.Map,
MaxKey: max_key_1.MaxKey,
MinKey: min_key_1.MinKey,
ObjectId: objectid_1.ObjectId,
ObjectID: objectid_1.ObjectId,
BSONRegExp: regexp_1.BSONRegExp,
BSONSymbol: symbol_1.BSONSymbol,
Timestamp: timestamp_1.Timestamp,
EJSON: extended_json_1.EJSON,
setInternalBufferSize: setInternalBufferSize,
serialize: serialize,
serializeWithBufferAndIndex: serializeWithBufferAndIndex,
deserialize: deserialize,
calculateObjectSize: calculateObjectSize,
deserializeStream: deserializeStream,
BSONError: error_1.BSONError,
BSONTypeError: error_1.BSONTypeError
};
exports.default = BSON;
//# sourceMappingURL=bson.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,46 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Code = void 0;
/**
* A class representation of the BSON Code type.
* @public
* @category BSONType
*/
var Code = /** @class */ (function () {
/**
* @param code - a string or function.
* @param scope - an optional scope for the function.
*/
function Code(code, scope) {
if (!(this instanceof Code))
return new Code(code, scope);
this.code = code;
this.scope = scope;
}
Code.prototype.toJSON = function () {
return { code: this.code, scope: this.scope };
};
/** @internal */
Code.prototype.toExtendedJSON = function () {
if (this.scope) {
return { $code: this.code, $scope: this.scope };
}
return { $code: this.code };
};
/** @internal */
Code.fromExtendedJSON = function (doc) {
return new Code(doc.$code, doc.$scope);
};
/** @internal */
Code.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
Code.prototype.inspect = function () {
var codeJson = this.toJSON();
return "new Code(\"".concat(String(codeJson.code), "\"").concat(codeJson.scope ? ", ".concat(JSON.stringify(codeJson.scope)) : '', ")");
};
return Code;
}());
exports.Code = Code;
Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });
//# sourceMappingURL=code.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"code.js","sourceRoot":"","sources":["../src/code.ts"],"names":[],"mappings":";;;AAQA;;;;GAIG;AACH;IAKE;;;OAGG;IACH,cAAY,IAAuB,EAAE,KAAgB;QACnD,IAAI,CAAC,CAAC,IAAI,YAAY,IAAI,CAAC;YAAE,OAAO,IAAI,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAE1D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,qBAAM,GAAN;QACE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IAChD,CAAC;IAED,gBAAgB;IAChB,6BAAc,GAAd;QACE,IAAI,IAAI,CAAC,KAAK,EAAE;YACd,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;SACjD;QAED,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IAED,gBAAgB;IACT,qBAAgB,GAAvB,UAAwB,GAAiB;QACvC,OAAO,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACzC,CAAC;IAED,gBAAgB;IAChB,eAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,sBAAO,GAAP;QACE,IAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAC/B,OAAO,qBAAa,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,eACvC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,YAAK,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAE,CAAC,CAAC,CAAC,EAAE,MAC1D,CAAC;IACN,CAAC;IACH,WAAC;AAAD,CAAC,AA7CD,IA6CC;AA7CY,oBAAI;AA+CjB,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC"}

View File

@@ -0,0 +1,82 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BSON_BINARY_SUBTYPE_USER_DEFINED = exports.BSON_BINARY_SUBTYPE_COLUMN = exports.BSON_BINARY_SUBTYPE_ENCRYPTED = exports.BSON_BINARY_SUBTYPE_MD5 = exports.BSON_BINARY_SUBTYPE_UUID_NEW = exports.BSON_BINARY_SUBTYPE_UUID = exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = exports.BSON_BINARY_SUBTYPE_FUNCTION = exports.BSON_BINARY_SUBTYPE_DEFAULT = exports.BSON_DATA_MAX_KEY = exports.BSON_DATA_MIN_KEY = exports.BSON_DATA_DECIMAL128 = exports.BSON_DATA_LONG = exports.BSON_DATA_TIMESTAMP = exports.BSON_DATA_INT = exports.BSON_DATA_CODE_W_SCOPE = exports.BSON_DATA_SYMBOL = exports.BSON_DATA_CODE = exports.BSON_DATA_DBPOINTER = exports.BSON_DATA_REGEXP = exports.BSON_DATA_NULL = exports.BSON_DATA_DATE = exports.BSON_DATA_BOOLEAN = exports.BSON_DATA_OID = exports.BSON_DATA_UNDEFINED = exports.BSON_DATA_BINARY = exports.BSON_DATA_ARRAY = exports.BSON_DATA_OBJECT = exports.BSON_DATA_STRING = exports.BSON_DATA_NUMBER = exports.JS_INT_MIN = exports.JS_INT_MAX = exports.BSON_INT64_MIN = exports.BSON_INT64_MAX = exports.BSON_INT32_MIN = exports.BSON_INT32_MAX = void 0;
/** @internal */
exports.BSON_INT32_MAX = 0x7fffffff;
/** @internal */
exports.BSON_INT32_MIN = -0x80000000;
/** @internal */
exports.BSON_INT64_MAX = Math.pow(2, 63) - 1;
/** @internal */
exports.BSON_INT64_MIN = -Math.pow(2, 63);
/**
* Any integer up to 2^53 can be precisely represented by a double.
* @internal
*/
exports.JS_INT_MAX = Math.pow(2, 53);
/**
* Any integer down to -2^53 can be precisely represented by a double.
* @internal
*/
exports.JS_INT_MIN = -Math.pow(2, 53);
/** Number BSON Type @internal */
exports.BSON_DATA_NUMBER = 1;
/** String BSON Type @internal */
exports.BSON_DATA_STRING = 2;
/** Object BSON Type @internal */
exports.BSON_DATA_OBJECT = 3;
/** Array BSON Type @internal */
exports.BSON_DATA_ARRAY = 4;
/** Binary BSON Type @internal */
exports.BSON_DATA_BINARY = 5;
/** Binary BSON Type @internal */
exports.BSON_DATA_UNDEFINED = 6;
/** ObjectId BSON Type @internal */
exports.BSON_DATA_OID = 7;
/** Boolean BSON Type @internal */
exports.BSON_DATA_BOOLEAN = 8;
/** Date BSON Type @internal */
exports.BSON_DATA_DATE = 9;
/** null BSON Type @internal */
exports.BSON_DATA_NULL = 10;
/** RegExp BSON Type @internal */
exports.BSON_DATA_REGEXP = 11;
/** Code BSON Type @internal */
exports.BSON_DATA_DBPOINTER = 12;
/** Code BSON Type @internal */
exports.BSON_DATA_CODE = 13;
/** Symbol BSON Type @internal */
exports.BSON_DATA_SYMBOL = 14;
/** Code with Scope BSON Type @internal */
exports.BSON_DATA_CODE_W_SCOPE = 15;
/** 32 bit Integer BSON Type @internal */
exports.BSON_DATA_INT = 16;
/** Timestamp BSON Type @internal */
exports.BSON_DATA_TIMESTAMP = 17;
/** Long BSON Type @internal */
exports.BSON_DATA_LONG = 18;
/** Decimal128 BSON Type @internal */
exports.BSON_DATA_DECIMAL128 = 19;
/** MinKey BSON Type @internal */
exports.BSON_DATA_MIN_KEY = 0xff;
/** MaxKey BSON Type @internal */
exports.BSON_DATA_MAX_KEY = 0x7f;
/** Binary Default Type @internal */
exports.BSON_BINARY_SUBTYPE_DEFAULT = 0;
/** Binary Function Type @internal */
exports.BSON_BINARY_SUBTYPE_FUNCTION = 1;
/** Binary Byte Array Type @internal */
exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
/** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
exports.BSON_BINARY_SUBTYPE_UUID = 3;
/** Binary UUID Type @internal */
exports.BSON_BINARY_SUBTYPE_UUID_NEW = 4;
/** Binary MD5 Type @internal */
exports.BSON_BINARY_SUBTYPE_MD5 = 5;
/** Encrypted BSON type @internal */
exports.BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
/** Column BSON type @internal */
exports.BSON_BINARY_SUBTYPE_COLUMN = 7;
/** Binary User Defined Type @internal */
exports.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
//# sourceMappingURL=constants.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA,gBAAgB;AACH,QAAA,cAAc,GAAG,UAAU,CAAC;AACzC,gBAAgB;AACH,QAAA,cAAc,GAAG,CAAC,UAAU,CAAC;AAC1C,gBAAgB;AACH,QAAA,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC;AAClD,gBAAgB;AACH,QAAA,cAAc,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAE/C;;;GAGG;AACU,QAAA,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAE1C;;;GAGG;AACU,QAAA,UAAU,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAE3C,iCAAiC;AACpB,QAAA,gBAAgB,GAAG,CAAC,CAAC;AAElC,iCAAiC;AACpB,QAAA,gBAAgB,GAAG,CAAC,CAAC;AAElC,iCAAiC;AACpB,QAAA,gBAAgB,GAAG,CAAC,CAAC;AAElC,gCAAgC;AACnB,QAAA,eAAe,GAAG,CAAC,CAAC;AAEjC,iCAAiC;AACpB,QAAA,gBAAgB,GAAG,CAAC,CAAC;AAElC,iCAAiC;AACpB,QAAA,mBAAmB,GAAG,CAAC,CAAC;AAErC,mCAAmC;AACtB,QAAA,aAAa,GAAG,CAAC,CAAC;AAE/B,kCAAkC;AACrB,QAAA,iBAAiB,GAAG,CAAC,CAAC;AAEnC,+BAA+B;AAClB,QAAA,cAAc,GAAG,CAAC,CAAC;AAEhC,+BAA+B;AAClB,QAAA,cAAc,GAAG,EAAE,CAAC;AAEjC,iCAAiC;AACpB,QAAA,gBAAgB,GAAG,EAAE,CAAC;AAEnC,+BAA+B;AAClB,QAAA,mBAAmB,GAAG,EAAE,CAAC;AAEtC,+BAA+B;AAClB,QAAA,cAAc,GAAG,EAAE,CAAC;AAEjC,iCAAiC;AACpB,QAAA,gBAAgB,GAAG,EAAE,CAAC;AAEnC,0CAA0C;AAC7B,QAAA,sBAAsB,GAAG,EAAE,CAAC;AAEzC,yCAAyC;AAC5B,QAAA,aAAa,GAAG,EAAE,CAAC;AAEhC,oCAAoC;AACvB,QAAA,mBAAmB,GAAG,EAAE,CAAC;AAEtC,+BAA+B;AAClB,QAAA,cAAc,GAAG,EAAE,CAAC;AAEjC,qCAAqC;AACxB,QAAA,oBAAoB,GAAG,EAAE,CAAC;AAEvC,iCAAiC;AACpB,QAAA,iBAAiB,GAAG,IAAI,CAAC;AAEtC,iCAAiC;AACpB,QAAA,iBAAiB,GAAG,IAAI,CAAC;AAEtC,oCAAoC;AACvB,QAAA,2BAA2B,GAAG,CAAC,CAAC;AAE7C,qCAAqC;AACxB,QAAA,4BAA4B,GAAG,CAAC,CAAC;AAE9C,uCAAuC;AAC1B,QAAA,8BAA8B,GAAG,CAAC,CAAC;AAEhD,gGAAgG;AACnF,QAAA,wBAAwB,GAAG,CAAC,CAAC;AAE1C,iCAAiC;AACpB,QAAA,4BAA4B,GAAG,CAAC,CAAC;AAE9C,gCAAgC;AACnB,QAAA,uBAAuB,GAAG,CAAC,CAAC;AAEzC,oCAAoC;AACvB,QAAA,6BAA6B,GAAG,CAAC,CAAC;AAE/C,iCAAiC;AACpB,QAAA,0BAA0B,GAAG,CAAC,CAAC;AAE5C,yCAAyC;AAC5B,QAAA,gCAAgC,GAAG,GAAG,CAAC"}

View File

@@ -0,0 +1,97 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DBRef = exports.isDBRefLike = void 0;
var utils_1 = require("./parser/utils");
/** @internal */
function isDBRefLike(value) {
return ((0, utils_1.isObjectLike)(value) &&
value.$id != null &&
typeof value.$ref === 'string' &&
(value.$db == null || typeof value.$db === 'string'));
}
exports.isDBRefLike = isDBRefLike;
/**
* A class representation of the BSON DBRef type.
* @public
* @category BSONType
*/
var DBRef = /** @class */ (function () {
/**
* @param collection - the collection name.
* @param oid - the reference ObjectId.
* @param db - optional db name, if omitted the reference is local to the current db.
*/
function DBRef(collection, oid, db, fields) {
if (!(this instanceof DBRef))
return new DBRef(collection, oid, db, fields);
// check if namespace has been provided
var parts = collection.split('.');
if (parts.length === 2) {
db = parts.shift();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
collection = parts.shift();
}
this.collection = collection;
this.oid = oid;
this.db = db;
this.fields = fields || {};
}
Object.defineProperty(DBRef.prototype, "namespace", {
// Property provided for compatibility with the 1.x parser
// the 1.x parser used a "namespace" property, while 4.x uses "collection"
/** @internal */
get: function () {
return this.collection;
},
set: function (value) {
this.collection = value;
},
enumerable: false,
configurable: true
});
DBRef.prototype.toJSON = function () {
var o = Object.assign({
$ref: this.collection,
$id: this.oid
}, this.fields);
if (this.db != null)
o.$db = this.db;
return o;
};
/** @internal */
DBRef.prototype.toExtendedJSON = function (options) {
options = options || {};
var o = {
$ref: this.collection,
$id: this.oid
};
if (options.legacy) {
return o;
}
if (this.db)
o.$db = this.db;
o = Object.assign(o, this.fields);
return o;
};
/** @internal */
DBRef.fromExtendedJSON = function (doc) {
var copy = Object.assign({}, doc);
delete copy.$ref;
delete copy.$id;
delete copy.$db;
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
};
/** @internal */
DBRef.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
DBRef.prototype.inspect = function () {
// NOTE: if OID is an ObjectId class it will just print the oid string.
var oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
return "new DBRef(\"".concat(this.namespace, "\", new ObjectId(\"").concat(String(oid), "\")").concat(this.db ? ", \"".concat(this.db, "\"") : '', ")");
};
return DBRef;
}());
exports.DBRef = DBRef;
Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });
//# sourceMappingURL=db_ref.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"db_ref.js","sourceRoot":"","sources":["../src/db_ref.ts"],"names":[],"mappings":";;;AAGA,wCAA8C;AAS9C,gBAAgB;AAChB,SAAgB,WAAW,CAAC,KAAc;IACxC,OAAO,CACL,IAAA,oBAAY,EAAC,KAAK,CAAC;QACnB,KAAK,CAAC,GAAG,IAAI,IAAI;QACjB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;QAC9B,CAAC,KAAK,CAAC,GAAG,IAAI,IAAI,IAAI,OAAO,KAAK,CAAC,GAAG,KAAK,QAAQ,CAAC,CACrD,CAAC;AACJ,CAAC;AAPD,kCAOC;AAED;;;;GAIG;AACH;IAQE;;;;OAIG;IACH,eAAY,UAAkB,EAAE,GAAa,EAAE,EAAW,EAAE,MAAiB;QAC3E,IAAI,CAAC,CAAC,IAAI,YAAY,KAAK,CAAC;YAAE,OAAO,IAAI,KAAK,CAAC,UAAU,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;QAE5E,uCAAuC;QACvC,IAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACpC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;YACtB,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;YACnB,oEAAoE;YACpE,UAAU,GAAG,KAAK,CAAC,KAAK,EAAG,CAAC;SAC7B;QAED,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;IAC7B,CAAC;IAMD,sBAAI,4BAAS;QAJb,0DAA0D;QAC1D,0EAA0E;QAE1E,gBAAgB;aAChB;YACE,OAAO,IAAI,CAAC,UAAU,CAAC;QACzB,CAAC;aAED,UAAc,KAAa;YACzB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QAC1B,CAAC;;;OAJA;IAMD,sBAAM,GAAN;QACE,IAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CACrB;YACE,IAAI,EAAE,IAAI,CAAC,UAAU;YACrB,GAAG,EAAE,IAAI,CAAC,GAAG;SACd,EACD,IAAI,CAAC,MAAM,CACZ,CAAC;QAEF,IAAI,IAAI,CAAC,EAAE,IAAI,IAAI;YAAE,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,EAAE,CAAC;QACrC,OAAO,CAAC,CAAC;IACX,CAAC;IAED,gBAAgB;IAChB,8BAAc,GAAd,UAAe,OAAsB;QACnC,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;QACxB,IAAI,CAAC,GAAc;YACjB,IAAI,EAAE,IAAI,CAAC,UAAU;YACrB,GAAG,EAAE,IAAI,CAAC,GAAG;SACd,CAAC;QAEF,IAAI,OAAO,CAAC,MAAM,EAAE;YAClB,OAAO,CAAC,CAAC;SACV;QAED,IAAI,IAAI,CAAC,EAAE;YAAE,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,EAAE,CAAC;QAC7B,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QAClC,OAAO,CAAC,CAAC;IACX,CAAC;IAED,gBAAgB;IACT,sBAAgB,GAAvB,UAAwB,GAAc;QACpC,IAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,CAAuB,CAAC;QAC1D,OAAO,IAAI,CAAC,IAAI,CAAC;QACjB,OAAO,IAAI,CAAC,GAAG,CAAC;QAChB,OAAO,IAAI,CAAC,GAAG,CAAC;QAChB,OAAO,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IACrD,CAAC;IAED,gBAAgB;IAChB,gBAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,uBAAO,GAAP;QACE,uEAAuE;QACvE,IAAM,GAAG,GACP,IAAI,CAAC,GAAG,KAAK,SAAS,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;QAC7F,OAAO,sBAAc,IAAI,CAAC,SAAS,gCAAoB,MAAM,CAAC,GAAG,CAAC,gBAChE,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,cAAM,IAAI,CAAC,EAAE,OAAG,CAAC,CAAC,CAAC,EAAE,MAC9B,CAAC;IACN,CAAC;IACH,YAAC;AAAD,CAAC,AA9FD,IA8FC;AA9FY,sBAAK;AAgGlB,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC"}

View File

@@ -0,0 +1,669 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Decimal128 = void 0;
var buffer_1 = require("buffer");
var error_1 = require("./error");
var long_1 = require("./long");
var utils_1 = require("./parser/utils");
var PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
var PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
var PARSE_NAN_REGEXP = /^(\+|-)?NaN$/i;
var EXPONENT_MAX = 6111;
var EXPONENT_MIN = -6176;
var EXPONENT_BIAS = 6176;
var MAX_DIGITS = 34;
// Nan value bits as 32 bit values (due to lack of longs)
var NAN_BUFFER = [
0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
// Infinity value bits 32 bit values (due to lack of longs)
var INF_NEGATIVE_BUFFER = [
0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
var INF_POSITIVE_BUFFER = [
0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
var EXPONENT_REGEX = /^([-+])?(\d+)?$/;
// Extract least significant 5 bits
var COMBINATION_MASK = 0x1f;
// Extract least significant 14 bits
var EXPONENT_MASK = 0x3fff;
// Value of combination field for Inf
var COMBINATION_INFINITY = 30;
// Value of combination field for NaN
var COMBINATION_NAN = 31;
// Detect if the value is a digit
function isDigit(value) {
return !isNaN(parseInt(value, 10));
}
// Divide two uint128 values
function divideu128(value) {
var DIVISOR = long_1.Long.fromNumber(1000 * 1000 * 1000);
var _rem = long_1.Long.fromNumber(0);
if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) {
return { quotient: value, rem: _rem };
}
for (var i = 0; i <= 3; i++) {
// Adjust remainder to match value of next dividend
_rem = _rem.shiftLeft(32);
// Add the divided to _rem
_rem = _rem.add(new long_1.Long(value.parts[i], 0));
value.parts[i] = _rem.div(DIVISOR).low;
_rem = _rem.modulo(DIVISOR);
}
return { quotient: value, rem: _rem };
}
// Multiply two Long values and return the 128 bit value
function multiply64x2(left, right) {
if (!left && !right) {
return { high: long_1.Long.fromNumber(0), low: long_1.Long.fromNumber(0) };
}
var leftHigh = left.shiftRightUnsigned(32);
var leftLow = new long_1.Long(left.getLowBits(), 0);
var rightHigh = right.shiftRightUnsigned(32);
var rightLow = new long_1.Long(right.getLowBits(), 0);
var productHigh = leftHigh.multiply(rightHigh);
var productMid = leftHigh.multiply(rightLow);
var productMid2 = leftLow.multiply(rightHigh);
var productLow = leftLow.multiply(rightLow);
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
productMid = new long_1.Long(productMid.getLowBits(), 0)
.add(productMid2)
.add(productLow.shiftRightUnsigned(32));
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
productLow = productMid.shiftLeft(32).add(new long_1.Long(productLow.getLowBits(), 0));
// Return the 128 bit result
return { high: productHigh, low: productLow };
}
function lessThan(left, right) {
// Make values unsigned
var uhleft = left.high >>> 0;
var uhright = right.high >>> 0;
// Compare high bits first
if (uhleft < uhright) {
return true;
}
else if (uhleft === uhright) {
var ulleft = left.low >>> 0;
var ulright = right.low >>> 0;
if (ulleft < ulright)
return true;
}
return false;
}
function invalidErr(string, message) {
throw new error_1.BSONTypeError("\"".concat(string, "\" is not a valid Decimal128 string - ").concat(message));
}
/**
* A class representation of the BSON Decimal128 type.
* @public
* @category BSONType
*/
var Decimal128 = /** @class */ (function () {
/**
* @param bytes - a buffer containing the raw Decimal128 bytes in little endian order,
* or a string representation as returned by .toString()
*/
function Decimal128(bytes) {
if (!(this instanceof Decimal128))
return new Decimal128(bytes);
if (typeof bytes === 'string') {
this.bytes = Decimal128.fromString(bytes).bytes;
}
else if ((0, utils_1.isUint8Array)(bytes)) {
if (bytes.byteLength !== 16) {
throw new error_1.BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
}
this.bytes = bytes;
}
else {
throw new error_1.BSONTypeError('Decimal128 must take a Buffer or string');
}
}
/**
* Create a Decimal128 instance from a string representation
*
* @param representation - a numeric string representation.
*/
Decimal128.fromString = function (representation) {
// Parse state tracking
var isNegative = false;
var sawRadix = false;
var foundNonZero = false;
// Total number of significant digits (no leading or trailing zero)
var significantDigits = 0;
// Total number of significand digits read
var nDigitsRead = 0;
// Total number of digits (no leading zeros)
var nDigits = 0;
// The number of the digits after radix
var radixPosition = 0;
// The index of the first non-zero in *str*
var firstNonZero = 0;
// Digits Array
var digits = [0];
// The number of digits in digits
var nDigitsStored = 0;
// Insertion pointer for digits
var digitsInsert = 0;
// The index of the first non-zero digit
var firstDigit = 0;
// The index of the last digit
var lastDigit = 0;
// Exponent
var exponent = 0;
// loop index over array
var i = 0;
// The high 17 digits of the significand
var significandHigh = new long_1.Long(0, 0);
// The low 17 digits of the significand
var significandLow = new long_1.Long(0, 0);
// The biased exponent
var biasedExponent = 0;
// Read index
var index = 0;
// Naively prevent against REDOS attacks.
// TODO: implementing a custom parsing for this, or refactoring the regex would yield
// further gains.
if (representation.length >= 7000) {
throw new error_1.BSONTypeError('' + representation + ' not a valid Decimal128 string');
}
// Results
var stringMatch = representation.match(PARSE_STRING_REGEXP);
var infMatch = representation.match(PARSE_INF_REGEXP);
var nanMatch = representation.match(PARSE_NAN_REGEXP);
// Validate the string
if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) {
throw new error_1.BSONTypeError('' + representation + ' not a valid Decimal128 string');
}
if (stringMatch) {
// full_match = stringMatch[0]
// sign = stringMatch[1]
var unsignedNumber = stringMatch[2];
// stringMatch[3] is undefined if a whole number (ex "1", 12")
// but defined if a number w/ decimal in it (ex "1.0, 12.2")
var e = stringMatch[4];
var expSign = stringMatch[5];
var expNumber = stringMatch[6];
// they provided e, but didn't give an exponent number. for ex "1e"
if (e && expNumber === undefined)
invalidErr(representation, 'missing exponent power');
// they provided e, but didn't give a number before it. for ex "e1"
if (e && unsignedNumber === undefined)
invalidErr(representation, 'missing exponent base');
if (e === undefined && (expSign || expNumber)) {
invalidErr(representation, 'missing e before exponent');
}
}
// Get the negative or positive sign
if (representation[index] === '+' || representation[index] === '-') {
isNegative = representation[index++] === '-';
}
// Check if user passed Infinity or NaN
if (!isDigit(representation[index]) && representation[index] !== '.') {
if (representation[index] === 'i' || representation[index] === 'I') {
return new Decimal128(buffer_1.Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
}
else if (representation[index] === 'N') {
return new Decimal128(buffer_1.Buffer.from(NAN_BUFFER));
}
}
// Read all the digits
while (isDigit(representation[index]) || representation[index] === '.') {
if (representation[index] === '.') {
if (sawRadix)
invalidErr(representation, 'contains multiple periods');
sawRadix = true;
index = index + 1;
continue;
}
if (nDigitsStored < 34) {
if (representation[index] !== '0' || foundNonZero) {
if (!foundNonZero) {
firstNonZero = nDigitsRead;
}
foundNonZero = true;
// Only store 34 digits
digits[digitsInsert++] = parseInt(representation[index], 10);
nDigitsStored = nDigitsStored + 1;
}
}
if (foundNonZero)
nDigits = nDigits + 1;
if (sawRadix)
radixPosition = radixPosition + 1;
nDigitsRead = nDigitsRead + 1;
index = index + 1;
}
if (sawRadix && !nDigitsRead)
throw new error_1.BSONTypeError('' + representation + ' not a valid Decimal128 string');
// Read exponent if exists
if (representation[index] === 'e' || representation[index] === 'E') {
// Read exponent digits
var match = representation.substr(++index).match(EXPONENT_REGEX);
// No digits read
if (!match || !match[2])
return new Decimal128(buffer_1.Buffer.from(NAN_BUFFER));
// Get exponent
exponent = parseInt(match[0], 10);
// Adjust the index
index = index + match[0].length;
}
// Return not a number
if (representation[index])
return new Decimal128(buffer_1.Buffer.from(NAN_BUFFER));
// Done reading input
// Find first non-zero digit in digits
firstDigit = 0;
if (!nDigitsStored) {
firstDigit = 0;
lastDigit = 0;
digits[0] = 0;
nDigits = 1;
nDigitsStored = 1;
significantDigits = 0;
}
else {
lastDigit = nDigitsStored - 1;
significantDigits = nDigits;
if (significantDigits !== 1) {
while (digits[firstNonZero + significantDigits - 1] === 0) {
significantDigits = significantDigits - 1;
}
}
}
// Normalization of exponent
// Correct exponent based on radix position, and shift significand as needed
// to represent user input
// Overflow prevention
if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
exponent = EXPONENT_MIN;
}
else {
exponent = exponent - radixPosition;
}
// Attempt to normalize the exponent
while (exponent > EXPONENT_MAX) {
// Shift exponent to significand and decrease
lastDigit = lastDigit + 1;
if (lastDigit - firstDigit > MAX_DIGITS) {
// Check if we have a zero then just hard clamp, otherwise fail
var digitsString = digits.join('');
if (digitsString.match(/^0+$/)) {
exponent = EXPONENT_MAX;
break;
}
invalidErr(representation, 'overflow');
}
exponent = exponent - 1;
}
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
// Shift last digit. can only do this if < significant digits than # stored.
if (lastDigit === 0 && significantDigits < nDigitsStored) {
exponent = EXPONENT_MIN;
significantDigits = 0;
break;
}
if (nDigitsStored < nDigits) {
// adjust to match digits not stored
nDigits = nDigits - 1;
}
else {
// adjust to round
lastDigit = lastDigit - 1;
}
if (exponent < EXPONENT_MAX) {
exponent = exponent + 1;
}
else {
// Check if we have a zero then just hard clamp, otherwise fail
var digitsString = digits.join('');
if (digitsString.match(/^0+$/)) {
exponent = EXPONENT_MAX;
break;
}
invalidErr(representation, 'overflow');
}
}
// Round
// We've normalized the exponent, but might still need to round.
if (lastDigit - firstDigit + 1 < significantDigits) {
var endOfString = nDigitsRead;
// If we have seen a radix point, 'string' is 1 longer than we have
// documented with ndigits_read, so inc the position of the first nonzero
// digit and the position that digits are read to.
if (sawRadix) {
firstNonZero = firstNonZero + 1;
endOfString = endOfString + 1;
}
// if negative, we need to increment again to account for - sign at start.
if (isNegative) {
firstNonZero = firstNonZero + 1;
endOfString = endOfString + 1;
}
var roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
var roundBit = 0;
if (roundDigit >= 5) {
roundBit = 1;
if (roundDigit === 5) {
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
if (parseInt(representation[i], 10)) {
roundBit = 1;
break;
}
}
}
}
if (roundBit) {
var dIdx = lastDigit;
for (; dIdx >= 0; dIdx--) {
if (++digits[dIdx] > 9) {
digits[dIdx] = 0;
// overflowed most significant digit
if (dIdx === 0) {
if (exponent < EXPONENT_MAX) {
exponent = exponent + 1;
digits[dIdx] = 1;
}
else {
return new Decimal128(buffer_1.Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
}
}
}
}
}
}
// Encode significand
// The high 17 digits of the significand
significandHigh = long_1.Long.fromNumber(0);
// The low 17 digits of the significand
significandLow = long_1.Long.fromNumber(0);
// read a zero
if (significantDigits === 0) {
significandHigh = long_1.Long.fromNumber(0);
significandLow = long_1.Long.fromNumber(0);
}
else if (lastDigit - firstDigit < 17) {
var dIdx = firstDigit;
significandLow = long_1.Long.fromNumber(digits[dIdx++]);
significandHigh = new long_1.Long(0, 0);
for (; dIdx <= lastDigit; dIdx++) {
significandLow = significandLow.multiply(long_1.Long.fromNumber(10));
significandLow = significandLow.add(long_1.Long.fromNumber(digits[dIdx]));
}
}
else {
var dIdx = firstDigit;
significandHigh = long_1.Long.fromNumber(digits[dIdx++]);
for (; dIdx <= lastDigit - 17; dIdx++) {
significandHigh = significandHigh.multiply(long_1.Long.fromNumber(10));
significandHigh = significandHigh.add(long_1.Long.fromNumber(digits[dIdx]));
}
significandLow = long_1.Long.fromNumber(digits[dIdx++]);
for (; dIdx <= lastDigit; dIdx++) {
significandLow = significandLow.multiply(long_1.Long.fromNumber(10));
significandLow = significandLow.add(long_1.Long.fromNumber(digits[dIdx]));
}
}
var significand = multiply64x2(significandHigh, long_1.Long.fromString('100000000000000000'));
significand.low = significand.low.add(significandLow);
if (lessThan(significand.low, significandLow)) {
significand.high = significand.high.add(long_1.Long.fromNumber(1));
}
// Biased exponent
biasedExponent = exponent + EXPONENT_BIAS;
var dec = { low: long_1.Long.fromNumber(0), high: long_1.Long.fromNumber(0) };
// Encode combination, exponent, and significand.
if (significand.high.shiftRightUnsigned(49).and(long_1.Long.fromNumber(1)).equals(long_1.Long.fromNumber(1))) {
// Encode '11' into bits 1 to 3
dec.high = dec.high.or(long_1.Long.fromNumber(0x3).shiftLeft(61));
dec.high = dec.high.or(long_1.Long.fromNumber(biasedExponent).and(long_1.Long.fromNumber(0x3fff).shiftLeft(47)));
dec.high = dec.high.or(significand.high.and(long_1.Long.fromNumber(0x7fffffffffff)));
}
else {
dec.high = dec.high.or(long_1.Long.fromNumber(biasedExponent & 0x3fff).shiftLeft(49));
dec.high = dec.high.or(significand.high.and(long_1.Long.fromNumber(0x1ffffffffffff)));
}
dec.low = significand.low;
// Encode sign
if (isNegative) {
dec.high = dec.high.or(long_1.Long.fromString('9223372036854775808'));
}
// Encode into a buffer
var buffer = buffer_1.Buffer.alloc(16);
index = 0;
// Encode the low 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.low.low & 0xff;
buffer[index++] = (dec.low.low >> 8) & 0xff;
buffer[index++] = (dec.low.low >> 16) & 0xff;
buffer[index++] = (dec.low.low >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.low.high & 0xff;
buffer[index++] = (dec.low.high >> 8) & 0xff;
buffer[index++] = (dec.low.high >> 16) & 0xff;
buffer[index++] = (dec.low.high >> 24) & 0xff;
// Encode the high 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.high.low & 0xff;
buffer[index++] = (dec.high.low >> 8) & 0xff;
buffer[index++] = (dec.high.low >> 16) & 0xff;
buffer[index++] = (dec.high.low >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.high.high & 0xff;
buffer[index++] = (dec.high.high >> 8) & 0xff;
buffer[index++] = (dec.high.high >> 16) & 0xff;
buffer[index++] = (dec.high.high >> 24) & 0xff;
// Return the new Decimal128
return new Decimal128(buffer);
};
/** Create a string representation of the raw Decimal128 value */
Decimal128.prototype.toString = function () {
// Note: bits in this routine are referred to starting at 0,
// from the sign bit, towards the coefficient.
// decoded biased exponent (14 bits)
var biased_exponent;
// the number of significand digits
var significand_digits = 0;
// the base-10 digits in the significand
var significand = new Array(36);
for (var i = 0; i < significand.length; i++)
significand[i] = 0;
// read pointer into significand
var index = 0;
// true if the number is zero
var is_zero = false;
// the most significant significand bits (50-46)
var significand_msb;
// temporary storage for significand decoding
var significand128 = { parts: [0, 0, 0, 0] };
// indexing variables
var j, k;
// Output string
var string = [];
// Unpack index
index = 0;
// Buffer reference
var buffer = this.bytes;
// Unpack the low 64bits into a long
// bits 96 - 127
var low = buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// bits 64 - 95
var midl = buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Unpack the high 64bits into a long
// bits 32 - 63
var midh = buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// bits 0 - 31
var high = buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Unpack index
index = 0;
// Create the state of the decimal
var dec = {
low: new long_1.Long(low, midl),
high: new long_1.Long(midh, high)
};
if (dec.high.lessThan(long_1.Long.ZERO)) {
string.push('-');
}
// Decode combination field and exponent
// bits 1 - 5
var combination = (high >> 26) & COMBINATION_MASK;
if (combination >> 3 === 3) {
// Check for 'special' values
if (combination === COMBINATION_INFINITY) {
return string.join('') + 'Infinity';
}
else if (combination === COMBINATION_NAN) {
return 'NaN';
}
else {
biased_exponent = (high >> 15) & EXPONENT_MASK;
significand_msb = 0x08 + ((high >> 14) & 0x01);
}
}
else {
significand_msb = (high >> 14) & 0x07;
biased_exponent = (high >> 17) & EXPONENT_MASK;
}
// unbiased exponent
var exponent = biased_exponent - EXPONENT_BIAS;
// Create string of significand digits
// Convert the 114-bit binary number represented by
// (significand_high, significand_low) to at most 34 decimal
// digits through modulo and division.
significand128.parts[0] = (high & 0x3fff) + ((significand_msb & 0xf) << 14);
significand128.parts[1] = midh;
significand128.parts[2] = midl;
significand128.parts[3] = low;
if (significand128.parts[0] === 0 &&
significand128.parts[1] === 0 &&
significand128.parts[2] === 0 &&
significand128.parts[3] === 0) {
is_zero = true;
}
else {
for (k = 3; k >= 0; k--) {
var least_digits = 0;
// Perform the divide
var result = divideu128(significand128);
significand128 = result.quotient;
least_digits = result.rem.low;
// We now have the 9 least significant digits (in base 2).
// Convert and output to string.
if (!least_digits)
continue;
for (j = 8; j >= 0; j--) {
// significand[k * 9 + j] = Math.round(least_digits % 10);
significand[k * 9 + j] = least_digits % 10;
// least_digits = Math.round(least_digits / 10);
least_digits = Math.floor(least_digits / 10);
}
}
}
// Output format options:
// Scientific - [-]d.dddE(+/-)dd or [-]dE(+/-)dd
// Regular - ddd.ddd
if (is_zero) {
significand_digits = 1;
significand[index] = 0;
}
else {
significand_digits = 36;
while (!significand[index]) {
significand_digits = significand_digits - 1;
index = index + 1;
}
}
// the exponent if scientific notation is used
var scientific_exponent = significand_digits - 1 + exponent;
// The scientific exponent checks are dictated by the string conversion
// specification and are somewhat arbitrary cutoffs.
//
// We must check exponent > 0, because if this is the case, the number
// has trailing zeros. However, we *cannot* output these trailing zeros,
// because doing so would change the precision of the value, and would
// change stored data if the string converted number is round tripped.
if (scientific_exponent >= 34 || scientific_exponent <= -7 || exponent > 0) {
// Scientific format
// if there are too many significant digits, we should just be treating numbers
// as + or - 0 and using the non-scientific exponent (this is for the "invalid
// representation should be treated as 0/-0" spec cases in decimal128-1.json)
if (significand_digits > 34) {
string.push("".concat(0));
if (exponent > 0)
string.push("E+".concat(exponent));
else if (exponent < 0)
string.push("E".concat(exponent));
return string.join('');
}
string.push("".concat(significand[index++]));
significand_digits = significand_digits - 1;
if (significand_digits) {
string.push('.');
}
for (var i = 0; i < significand_digits; i++) {
string.push("".concat(significand[index++]));
}
// Exponent
string.push('E');
if (scientific_exponent > 0) {
string.push("+".concat(scientific_exponent));
}
else {
string.push("".concat(scientific_exponent));
}
}
else {
// Regular format with no decimal place
if (exponent >= 0) {
for (var i = 0; i < significand_digits; i++) {
string.push("".concat(significand[index++]));
}
}
else {
var radix_position = significand_digits + exponent;
// non-zero digits before radix
if (radix_position > 0) {
for (var i = 0; i < radix_position; i++) {
string.push("".concat(significand[index++]));
}
}
else {
string.push('0');
}
string.push('.');
// add leading zeros after radix
while (radix_position++ < 0) {
string.push('0');
}
for (var i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) {
string.push("".concat(significand[index++]));
}
}
}
return string.join('');
};
Decimal128.prototype.toJSON = function () {
return { $numberDecimal: this.toString() };
};
/** @internal */
Decimal128.prototype.toExtendedJSON = function () {
return { $numberDecimal: this.toString() };
};
/** @internal */
Decimal128.fromExtendedJSON = function (doc) {
return Decimal128.fromString(doc.$numberDecimal);
};
/** @internal */
Decimal128.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
Decimal128.prototype.inspect = function () {
return "new Decimal128(\"".concat(this.toString(), "\")");
};
return Decimal128;
}());
exports.Decimal128 = Decimal128;
Object.defineProperty(Decimal128.prototype, '_bsontype', { value: 'Decimal128' });
//# sourceMappingURL=decimal128.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,68 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Double = void 0;
/**
* A class representation of the BSON Double type.
* @public
* @category BSONType
*/
var Double = /** @class */ (function () {
/**
* Create a Double type
*
* @param value - the number we want to represent as a double.
*/
function Double(value) {
if (!(this instanceof Double))
return new Double(value);
if (value instanceof Number) {
value = value.valueOf();
}
this.value = +value;
}
/**
* Access the number value.
*
* @returns returns the wrapped double number.
*/
Double.prototype.valueOf = function () {
return this.value;
};
Double.prototype.toJSON = function () {
return this.value;
};
Double.prototype.toString = function (radix) {
return this.value.toString(radix);
};
/** @internal */
Double.prototype.toExtendedJSON = function (options) {
if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) {
return this.value;
}
if (Object.is(Math.sign(this.value), -0)) {
// NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
// explicitly provided `-0` then we need to ensure the sign makes it into the output
return { $numberDouble: "-".concat(this.value.toFixed(1)) };
}
return {
$numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
};
};
/** @internal */
Double.fromExtendedJSON = function (doc, options) {
var doubleValue = parseFloat(doc.$numberDouble);
return options && options.relaxed ? doubleValue : new Double(doubleValue);
};
/** @internal */
Double.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
Double.prototype.inspect = function () {
var eJSON = this.toExtendedJSON();
return "new Double(".concat(eJSON.$numberDouble, ")");
};
return Double;
}());
exports.Double = Double;
Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });
//# sourceMappingURL=double.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"double.js","sourceRoot":"","sources":["../src/double.ts"],"names":[],"mappings":";;;AAOA;;;;GAIG;AACH;IAIE;;;;OAIG;IACH,gBAAY,KAAa;QACvB,IAAI,CAAC,CAAC,IAAI,YAAY,MAAM,CAAC;YAAE,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;QAExD,IAAK,KAAiB,YAAY,MAAM,EAAE;YACxC,KAAK,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;SACzB;QAED,IAAI,CAAC,KAAK,GAAG,CAAC,KAAK,CAAC;IACtB,CAAC;IAED;;;;OAIG;IACH,wBAAO,GAAP;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,uBAAM,GAAN;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,yBAAQ,GAAR,UAAS,KAAc;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;IAED,gBAAgB;IAChB,+BAAc,GAAd,UAAe,OAAsB;QACnC,IAAI,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;YAC5E,OAAO,IAAI,CAAC,KAAK,CAAC;SACnB;QAED,IAAI,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE;YACxC,oFAAoF;YACpF,oFAAoF;YACpF,OAAO,EAAE,aAAa,EAAE,WAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAE,EAAE,CAAC;SACvD;QAED,OAAO;YACL,aAAa,EAAE,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE;SAC5F,CAAC;IACJ,CAAC;IAED,gBAAgB;IACT,uBAAgB,GAAvB,UAAwB,GAAmB,EAAE,OAAsB;QACjE,IAAM,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAClD,OAAO,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,WAAW,CAAC,CAAC;IAC5E,CAAC;IAED,gBAAgB;IAChB,iBAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,wBAAO,GAAP;QACE,IAAM,KAAK,GAAG,IAAI,CAAC,cAAc,EAAoB,CAAC;QACtD,OAAO,qBAAc,KAAK,CAAC,aAAa,MAAG,CAAC;IAC9C,CAAC;IACH,aAAC;AAAD,CAAC,AApED,IAoEC;AApEY,wBAAM;AAsEnB,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC"}

View File

@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureBuffer = void 0;
var buffer_1 = require("buffer");
var error_1 = require("./error");
var utils_1 = require("./parser/utils");
/**
* Makes sure that, if a Uint8Array is passed in, it is wrapped in a Buffer.
*
* @param potentialBuffer - The potential buffer
* @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
* wraps a passed in Uint8Array
* @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
*/
function ensureBuffer(potentialBuffer) {
if (ArrayBuffer.isView(potentialBuffer)) {
return buffer_1.Buffer.from(potentialBuffer.buffer, potentialBuffer.byteOffset, potentialBuffer.byteLength);
}
if ((0, utils_1.isAnyArrayBuffer)(potentialBuffer)) {
return buffer_1.Buffer.from(potentialBuffer);
}
throw new error_1.BSONTypeError('Must use either Buffer or TypedArray');
}
exports.ensureBuffer = ensureBuffer;
//# sourceMappingURL=ensure_buffer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"ensure_buffer.js","sourceRoot":"","sources":["../src/ensure_buffer.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,iCAAwC;AACxC,wCAAkD;AAElD;;;;;;;GAOG;AACH,SAAgB,YAAY,CAAC,eAAuD;IAClF,IAAI,WAAW,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE;QACvC,OAAO,eAAM,CAAC,IAAI,CAChB,eAAe,CAAC,MAAM,EACtB,eAAe,CAAC,UAAU,EAC1B,eAAe,CAAC,UAAU,CAC3B,CAAC;KACH;IAED,IAAI,IAAA,wBAAgB,EAAC,eAAe,CAAC,EAAE;QACrC,OAAO,eAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;KACrC;IAED,MAAM,IAAI,qBAAa,CAAC,sCAAsC,CAAC,CAAC;AAClE,CAAC;AAdD,oCAcC"}

View File

@@ -0,0 +1,55 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BSONTypeError = exports.BSONError = void 0;
/** @public */
var BSONError = /** @class */ (function (_super) {
__extends(BSONError, _super);
function BSONError(message) {
var _this = _super.call(this, message) || this;
Object.setPrototypeOf(_this, BSONError.prototype);
return _this;
}
Object.defineProperty(BSONError.prototype, "name", {
get: function () {
return 'BSONError';
},
enumerable: false,
configurable: true
});
return BSONError;
}(Error));
exports.BSONError = BSONError;
/** @public */
var BSONTypeError = /** @class */ (function (_super) {
__extends(BSONTypeError, _super);
function BSONTypeError(message) {
var _this = _super.call(this, message) || this;
Object.setPrototypeOf(_this, BSONTypeError.prototype);
return _this;
}
Object.defineProperty(BSONTypeError.prototype, "name", {
get: function () {
return 'BSONTypeError';
},
enumerable: false,
configurable: true
});
return BSONTypeError;
}(TypeError));
exports.BSONTypeError = BSONTypeError;
//# sourceMappingURL=error.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,cAAc;AACd;IAA+B,6BAAK;IAClC,mBAAY,OAAe;QAA3B,YACE,kBAAM,OAAO,CAAC,SAEf;QADC,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;;IACnD,CAAC;IAED,sBAAI,2BAAI;aAAR;YACE,OAAO,WAAW,CAAC;QACrB,CAAC;;;OAAA;IACH,gBAAC;AAAD,CAAC,AATD,CAA+B,KAAK,GASnC;AATY,8BAAS;AAWtB,cAAc;AACd;IAAmC,iCAAS;IAC1C,uBAAY,OAAe;QAA3B,YACE,kBAAM,OAAO,CAAC,SAEf;QADC,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,aAAa,CAAC,SAAS,CAAC,CAAC;;IACvD,CAAC;IAED,sBAAI,+BAAI;aAAR;YACE,OAAO,eAAe,CAAC;QACzB,CAAC;;;OAAA;IACH,oBAAC;AAAD,CAAC,AATD,CAAmC,SAAS,GAS3C;AATY,sCAAa"}

View File

@@ -0,0 +1,390 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EJSON = exports.isBSONType = void 0;
var binary_1 = require("./binary");
var code_1 = require("./code");
var db_ref_1 = require("./db_ref");
var decimal128_1 = require("./decimal128");
var double_1 = require("./double");
var error_1 = require("./error");
var int_32_1 = require("./int_32");
var long_1 = require("./long");
var max_key_1 = require("./max_key");
var min_key_1 = require("./min_key");
var objectid_1 = require("./objectid");
var utils_1 = require("./parser/utils");
var regexp_1 = require("./regexp");
var symbol_1 = require("./symbol");
var timestamp_1 = require("./timestamp");
function isBSONType(value) {
return ((0, utils_1.isObjectLike)(value) && Reflect.has(value, '_bsontype') && typeof value._bsontype === 'string');
}
exports.isBSONType = isBSONType;
// INT32 boundaries
var BSON_INT32_MAX = 0x7fffffff;
var BSON_INT32_MIN = -0x80000000;
// INT64 boundaries
// const BSON_INT64_MAX = 0x7fffffffffffffff; // TODO(NODE-4377): This number cannot be precisely represented in JS
var BSON_INT64_MAX = 0x8000000000000000;
var BSON_INT64_MIN = -0x8000000000000000;
// all the types where we don't need to do any special processing and can just pass the EJSON
//straight to type.fromExtendedJSON
var keysToCodecs = {
$oid: objectid_1.ObjectId,
$binary: binary_1.Binary,
$uuid: binary_1.Binary,
$symbol: symbol_1.BSONSymbol,
$numberInt: int_32_1.Int32,
$numberDecimal: decimal128_1.Decimal128,
$numberDouble: double_1.Double,
$numberLong: long_1.Long,
$minKey: min_key_1.MinKey,
$maxKey: max_key_1.MaxKey,
$regex: regexp_1.BSONRegExp,
$regularExpression: regexp_1.BSONRegExp,
$timestamp: timestamp_1.Timestamp
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function deserializeValue(value, options) {
if (options === void 0) { options = {}; }
if (typeof value === 'number') {
if (options.relaxed || options.legacy) {
return value;
}
// if it's an integer, should interpret as smallest BSON integer
// that can represent it exactly. (if out of range, interpret as double.)
if (Math.floor(value) === value) {
if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX)
return new int_32_1.Int32(value);
if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX)
return long_1.Long.fromNumber(value);
}
// If the number is a non-integer or out of integer range, should interpret as BSON Double.
return new double_1.Double(value);
}
// from here on out we're looking for bson types, so bail if its not an object
if (value == null || typeof value !== 'object')
return value;
// upgrade deprecated undefined to null
if (value.$undefined)
return null;
var keys = Object.keys(value).filter(function (k) { return k.startsWith('$') && value[k] != null; });
for (var i = 0; i < keys.length; i++) {
var c = keysToCodecs[keys[i]];
if (c)
return c.fromExtendedJSON(value, options);
}
if (value.$date != null) {
var d = value.$date;
var date = new Date();
if (options.legacy) {
if (typeof d === 'number')
date.setTime(d);
else if (typeof d === 'string')
date.setTime(Date.parse(d));
}
else {
if (typeof d === 'string')
date.setTime(Date.parse(d));
else if (long_1.Long.isLong(d))
date.setTime(d.toNumber());
else if (typeof d === 'number' && options.relaxed)
date.setTime(d);
}
return date;
}
if (value.$code != null) {
var copy = Object.assign({}, value);
if (value.$scope) {
copy.$scope = deserializeValue(value.$scope);
}
return code_1.Code.fromExtendedJSON(value);
}
if ((0, db_ref_1.isDBRefLike)(value) || value.$dbPointer) {
var v = value.$ref ? value : value.$dbPointer;
// we run into this in a "degenerate EJSON" case (with $id and $ref order flipped)
// because of the order JSON.parse goes through the document
if (v instanceof db_ref_1.DBRef)
return v;
var dollarKeys = Object.keys(v).filter(function (k) { return k.startsWith('$'); });
var valid_1 = true;
dollarKeys.forEach(function (k) {
if (['$ref', '$id', '$db'].indexOf(k) === -1)
valid_1 = false;
});
// only make DBRef if $ keys are all valid
if (valid_1)
return db_ref_1.DBRef.fromExtendedJSON(v);
}
return value;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeArray(array, options) {
return array.map(function (v, index) {
options.seenObjects.push({ propertyName: "index ".concat(index), obj: null });
try {
return serializeValue(v, options);
}
finally {
options.seenObjects.pop();
}
});
}
function getISOString(date) {
var isoStr = date.toISOString();
// we should only show milliseconds in timestamp if they're non-zero
return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z';
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeValue(value, options) {
if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
var index = options.seenObjects.findIndex(function (entry) { return entry.obj === value; });
if (index !== -1) {
var props = options.seenObjects.map(function (entry) { return entry.propertyName; });
var leadingPart = props
.slice(0, index)
.map(function (prop) { return "".concat(prop, " -> "); })
.join('');
var alreadySeen = props[index];
var circularPart = ' -> ' +
props
.slice(index + 1, props.length - 1)
.map(function (prop) { return "".concat(prop, " -> "); })
.join('');
var current = props[props.length - 1];
var leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
var dashes = '-'.repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
throw new error_1.BSONTypeError('Converting circular structure to EJSON:\n' +
" ".concat(leadingPart).concat(alreadySeen).concat(circularPart).concat(current, "\n") +
" ".concat(leadingSpace, "\\").concat(dashes, "/"));
}
options.seenObjects[options.seenObjects.length - 1].obj = value;
}
if (Array.isArray(value))
return serializeArray(value, options);
if (value === undefined)
return null;
if (value instanceof Date || (0, utils_1.isDate)(value)) {
var dateNum = value.getTime(),
// is it in year range 1970-9999?
inRange = dateNum > -1 && dateNum < 253402318800000;
if (options.legacy) {
return options.relaxed && inRange
? { $date: value.getTime() }
: { $date: getISOString(value) };
}
return options.relaxed && inRange
? { $date: getISOString(value) }
: { $date: { $numberLong: value.getTime().toString() } };
}
if (typeof value === 'number' && (!options.relaxed || !isFinite(value))) {
// it's an integer
if (Math.floor(value) === value) {
var int32Range = value >= BSON_INT32_MIN && value <= BSON_INT32_MAX, int64Range = value >= BSON_INT64_MIN && value <= BSON_INT64_MAX;
// interpret as being of the smallest BSON integer type that can represent the number exactly
if (int32Range)
return { $numberInt: value.toString() };
if (int64Range)
return { $numberLong: value.toString() };
}
return { $numberDouble: value.toString() };
}
if (value instanceof RegExp || (0, utils_1.isRegExp)(value)) {
var flags = value.flags;
if (flags === undefined) {
var match = value.toString().match(/[gimuy]*$/);
if (match) {
flags = match[0];
}
}
var rx = new regexp_1.BSONRegExp(value.source, flags);
return rx.toExtendedJSON(options);
}
if (value != null && typeof value === 'object')
return serializeDocument(value, options);
return value;
}
var BSON_TYPE_MAPPINGS = {
Binary: function (o) { return new binary_1.Binary(o.value(), o.sub_type); },
Code: function (o) { return new code_1.Code(o.code, o.scope); },
DBRef: function (o) { return new db_ref_1.DBRef(o.collection || o.namespace, o.oid, o.db, o.fields); },
Decimal128: function (o) { return new decimal128_1.Decimal128(o.bytes); },
Double: function (o) { return new double_1.Double(o.value); },
Int32: function (o) { return new int_32_1.Int32(o.value); },
Long: function (o) {
return long_1.Long.fromBits(
// underscore variants for 1.x backwards compatibility
o.low != null ? o.low : o.low_, o.low != null ? o.high : o.high_, o.low != null ? o.unsigned : o.unsigned_);
},
MaxKey: function () { return new max_key_1.MaxKey(); },
MinKey: function () { return new min_key_1.MinKey(); },
ObjectID: function (o) { return new objectid_1.ObjectId(o); },
ObjectId: function (o) { return new objectid_1.ObjectId(o); },
BSONRegExp: function (o) { return new regexp_1.BSONRegExp(o.pattern, o.options); },
Symbol: function (o) { return new symbol_1.BSONSymbol(o.value); },
Timestamp: function (o) { return timestamp_1.Timestamp.fromBits(o.low, o.high); }
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeDocument(doc, options) {
if (doc == null || typeof doc !== 'object')
throw new error_1.BSONError('not an object instance');
var bsontype = doc._bsontype;
if (typeof bsontype === 'undefined') {
// It's a regular object. Recursively serialize its property values.
var _doc = {};
for (var name in doc) {
options.seenObjects.push({ propertyName: name, obj: null });
try {
var value = serializeValue(doc[name], options);
if (name === '__proto__') {
Object.defineProperty(_doc, name, {
value: value,
writable: true,
enumerable: true,
configurable: true
});
}
else {
_doc[name] = value;
}
}
finally {
options.seenObjects.pop();
}
}
return _doc;
}
else if (isBSONType(doc)) {
// the "document" is really just a BSON type object
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var outDoc = doc;
if (typeof outDoc.toExtendedJSON !== 'function') {
// There's no EJSON serialization function on the object. It's probably an
// object created by a previous version of this library (or another library)
// that's duck-typing objects to look like they were generated by this library).
// Copy the object into this library's version of that type.
var mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
if (!mapper) {
throw new error_1.BSONTypeError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
}
outDoc = mapper(outDoc);
}
// Two BSON types may have nested objects that may need to be serialized too
if (bsontype === 'Code' && outDoc.scope) {
outDoc = new code_1.Code(outDoc.code, serializeValue(outDoc.scope, options));
}
else if (bsontype === 'DBRef' && outDoc.oid) {
outDoc = new db_ref_1.DBRef(serializeValue(outDoc.collection, options), serializeValue(outDoc.oid, options), serializeValue(outDoc.db, options), serializeValue(outDoc.fields, options));
}
return outDoc.toExtendedJSON(options);
}
else {
throw new error_1.BSONError('_bsontype must be a string, but was: ' + typeof bsontype);
}
}
/**
* EJSON parse / stringify API
* @public
*/
// the namespace here is used to emulate `export * as EJSON from '...'`
// which as of now (sept 2020) api-extractor does not support
// eslint-disable-next-line @typescript-eslint/no-namespace
var EJSON;
(function (EJSON) {
/**
* Parse an Extended JSON string, constructing the JavaScript value or object described by that
* string.
*
* @example
* ```js
* const { EJSON } = require('bson');
* const text = '{ "int32": { "$numberInt": "10" } }';
*
* // prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
* console.log(EJSON.parse(text, { relaxed: false }));
*
* // prints { int32: 10 }
* console.log(EJSON.parse(text));
* ```
*/
function parse(text, options) {
var finalOptions = Object.assign({}, { relaxed: true, legacy: false }, options);
// relaxed implies not strict
if (typeof finalOptions.relaxed === 'boolean')
finalOptions.strict = !finalOptions.relaxed;
if (typeof finalOptions.strict === 'boolean')
finalOptions.relaxed = !finalOptions.strict;
return JSON.parse(text, function (key, value) {
if (key.indexOf('\x00') !== -1) {
throw new error_1.BSONError("BSON Document field names cannot contain null bytes, found: ".concat(JSON.stringify(key)));
}
return deserializeValue(value, finalOptions);
});
}
EJSON.parse = parse;
/**
* Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
* function is specified or optionally including only the specified properties if a replacer array
* is specified.
*
* @param value - The value to convert to extended JSON
* @param replacer - 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
* @param space - A String or Number object that's used to insert white space into the output JSON string for readability purposes.
* @param options - Optional settings
*
* @example
* ```js
* const { EJSON } = require('bson');
* const Int32 = require('mongodb').Int32;
* const doc = { int32: new Int32(10) };
*
* // prints '{"int32":{"$numberInt":"10"}}'
* console.log(EJSON.stringify(doc, { relaxed: false }));
*
* // prints '{"int32":10}'
* console.log(EJSON.stringify(doc));
* ```
*/
function stringify(value,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
replacer, space, options) {
if (space != null && typeof space === 'object') {
options = space;
space = 0;
}
if (replacer != null && typeof replacer === 'object' && !Array.isArray(replacer)) {
options = replacer;
replacer = undefined;
space = 0;
}
var serializeOptions = Object.assign({ relaxed: true, legacy: false }, options, {
seenObjects: [{ propertyName: '(root)', obj: null }]
});
var doc = serializeValue(value, serializeOptions);
return JSON.stringify(doc, replacer, space);
}
EJSON.stringify = stringify;
/**
* Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
*
* @param value - The object to serialize
* @param options - Optional settings passed to the `stringify` function
*/
function serialize(value, options) {
options = options || {};
return JSON.parse(stringify(value, options));
}
EJSON.serialize = serialize;
/**
* Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
*
* @param ejson - The Extended JSON object to deserialize
* @param options - Optional settings passed to the parse method
*/
function deserialize(ejson, options) {
options = options || {};
return parse(JSON.stringify(ejson), options);
}
EJSON.deserialize = deserialize;
})(EJSON = exports.EJSON || (exports.EJSON = {}));
//# sourceMappingURL=extended_json.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,58 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Int32 = void 0;
/**
* A class representation of a BSON Int32 type.
* @public
* @category BSONType
*/
var Int32 = /** @class */ (function () {
/**
* Create an Int32 type
*
* @param value - the number we want to represent as an int32.
*/
function Int32(value) {
if (!(this instanceof Int32))
return new Int32(value);
if (value instanceof Number) {
value = value.valueOf();
}
this.value = +value | 0;
}
/**
* Access the number value.
*
* @returns returns the wrapped int32 number.
*/
Int32.prototype.valueOf = function () {
return this.value;
};
Int32.prototype.toString = function (radix) {
return this.value.toString(radix);
};
Int32.prototype.toJSON = function () {
return this.value;
};
/** @internal */
Int32.prototype.toExtendedJSON = function (options) {
if (options && (options.relaxed || options.legacy))
return this.value;
return { $numberInt: this.value.toString() };
};
/** @internal */
Int32.fromExtendedJSON = function (doc, options) {
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
};
/** @internal */
Int32.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
Int32.prototype.inspect = function () {
return "new Int32(".concat(this.valueOf(), ")");
};
return Int32;
}());
exports.Int32 = Int32;
Object.defineProperty(Int32.prototype, '_bsontype', { value: 'Int32' });
//# sourceMappingURL=int_32.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"int_32.js","sourceRoot":"","sources":["../src/int_32.ts"],"names":[],"mappings":";;;AAOA;;;;GAIG;AACH;IAIE;;;;OAIG;IACH,eAAY,KAAsB;QAChC,IAAI,CAAC,CAAC,IAAI,YAAY,KAAK,CAAC;YAAE,OAAO,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;QAEtD,IAAK,KAAiB,YAAY,MAAM,EAAE;YACxC,KAAK,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;SACzB;QAED,IAAI,CAAC,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACH,uBAAO,GAAP;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,wBAAQ,GAAR,UAAS,KAAc;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;IAED,sBAAM,GAAN;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,gBAAgB;IAChB,8BAAc,GAAd,UAAe,OAAsB;QACnC,IAAI,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC;QACtE,OAAO,EAAE,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,CAAC;IAC/C,CAAC;IAED,gBAAgB;IACT,sBAAgB,GAAvB,UAAwB,GAAkB,EAAE,OAAsB;QAChE,OAAO,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC/F,CAAC;IAED,gBAAgB;IAChB,gBAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,uBAAO,GAAP;QACE,OAAO,oBAAa,IAAI,CAAC,OAAO,EAAE,MAAG,CAAC;IACxC,CAAC;IACH,YAAC;AAAD,CAAC,AAvDD,IAuDC;AAvDY,sBAAK;AAyDlB,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC"}

View File

@@ -0,0 +1,900 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Long = void 0;
var utils_1 = require("./parser/utils");
/**
* wasm optimizations, to do native i64 multiplication and divide
*/
var wasm = undefined;
try {
wasm = new WebAssembly.Instance(new WebAssembly.Module(
// prettier-ignore
new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11])), {}).exports;
}
catch (_a) {
// no wasm support
}
var TWO_PWR_16_DBL = 1 << 16;
var TWO_PWR_24_DBL = 1 << 24;
var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
/** A cache of the Long representations of small integer values. */
var INT_CACHE = {};
/** A cache of the Long representations of small unsigned integer values. */
var UINT_CACHE = {};
/**
* A class representing a 64-bit integer
* @public
* @category BSONType
* @remarks
* The internal representation of a long is the two given signed, 32-bit values.
* We use 32-bit pieces because these are the size of integers on which
* Javascript performs bit-operations. For operations like addition and
* multiplication, we split each number into 16 bit pieces, which can easily be
* multiplied within Javascript's floating-point representation without overflow
* or change in sign.
* In the algorithms below, we frequently reduce the negative case to the
* positive case by negating the input(s) and then post-processing the result.
* Note that we must ALWAYS check specially whether those values are MIN_VALUE
* (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
* a positive number, it overflows back into a negative). Not handling this
* case would often result in infinite recursion.
* Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.
*/
var Long = /** @class */ (function () {
/**
* Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
* See the from* functions below for more convenient ways of constructing Longs.
*
* Acceptable signatures are:
* - Long(low, high, unsigned?)
* - Long(bigint, unsigned?)
* - Long(string, unsigned?)
*
* @param low - The low (signed) 32 bits of the long
* @param high - The high (signed) 32 bits of the long
* @param unsigned - Whether unsigned or not, defaults to signed
*/
function Long(low, high, unsigned) {
if (low === void 0) { low = 0; }
if (!(this instanceof Long))
return new Long(low, high, unsigned);
if (typeof low === 'bigint') {
Object.assign(this, Long.fromBigInt(low, !!high));
}
else if (typeof low === 'string') {
Object.assign(this, Long.fromString(low, !!high));
}
else {
this.low = low | 0;
this.high = high | 0;
this.unsigned = !!unsigned;
}
Object.defineProperty(this, '__isLong__', {
value: true,
configurable: false,
writable: false,
enumerable: false
});
}
/**
* Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits.
* Each is assumed to use 32 bits.
* @param lowBits - The low 32 bits
* @param highBits - The high 32 bits
* @param unsigned - Whether unsigned or not, defaults to signed
* @returns The corresponding Long value
*/
Long.fromBits = function (lowBits, highBits, unsigned) {
return new Long(lowBits, highBits, unsigned);
};
/**
* Returns a Long representing the given 32 bit integer value.
* @param value - The 32 bit integer in question
* @param unsigned - Whether unsigned or not, defaults to signed
* @returns The corresponding Long value
*/
Long.fromInt = function (value, unsigned) {
var obj, cachedObj, cache;
if (unsigned) {
value >>>= 0;
if ((cache = 0 <= value && value < 256)) {
cachedObj = UINT_CACHE[value];
if (cachedObj)
return cachedObj;
}
obj = Long.fromBits(value, (value | 0) < 0 ? -1 : 0, true);
if (cache)
UINT_CACHE[value] = obj;
return obj;
}
else {
value |= 0;
if ((cache = -128 <= value && value < 128)) {
cachedObj = INT_CACHE[value];
if (cachedObj)
return cachedObj;
}
obj = Long.fromBits(value, value < 0 ? -1 : 0, false);
if (cache)
INT_CACHE[value] = obj;
return obj;
}
};
/**
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
* @param value - The number in question
* @param unsigned - Whether unsigned or not, defaults to signed
* @returns The corresponding Long value
*/
Long.fromNumber = function (value, unsigned) {
if (isNaN(value))
return unsigned ? Long.UZERO : Long.ZERO;
if (unsigned) {
if (value < 0)
return Long.UZERO;
if (value >= TWO_PWR_64_DBL)
return Long.MAX_UNSIGNED_VALUE;
}
else {
if (value <= -TWO_PWR_63_DBL)
return Long.MIN_VALUE;
if (value + 1 >= TWO_PWR_63_DBL)
return Long.MAX_VALUE;
}
if (value < 0)
return Long.fromNumber(-value, unsigned).neg();
return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
};
/**
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
* @param value - The number in question
* @param unsigned - Whether unsigned or not, defaults to signed
* @returns The corresponding Long value
*/
Long.fromBigInt = function (value, unsigned) {
return Long.fromString(value.toString(), unsigned);
};
/**
* Returns a Long representation of the given string, written using the specified radix.
* @param str - The textual representation of the Long
* @param unsigned - Whether unsigned or not, defaults to signed
* @param radix - The radix in which the text is written (2-36), defaults to 10
* @returns The corresponding Long value
*/
Long.fromString = function (str, unsigned, radix) {
if (str.length === 0)
throw Error('empty string');
if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity')
return Long.ZERO;
if (typeof unsigned === 'number') {
// For goog.math.long compatibility
(radix = unsigned), (unsigned = false);
}
else {
unsigned = !!unsigned;
}
radix = radix || 10;
if (radix < 2 || 36 < radix)
throw RangeError('radix');
var p;
if ((p = str.indexOf('-')) > 0)
throw Error('interior hyphen');
else if (p === 0) {
return Long.fromString(str.substring(1), unsigned, radix).neg();
}
// Do several (8) digits each time through the loop, so as to
// minimize the calls to the very expensive emulated div.
var radixToPower = Long.fromNumber(Math.pow(radix, 8));
var result = Long.ZERO;
for (var i = 0; i < str.length; i += 8) {
var size = Math.min(8, str.length - i), value = parseInt(str.substring(i, i + size), radix);
if (size < 8) {
var power = Long.fromNumber(Math.pow(radix, size));
result = result.mul(power).add(Long.fromNumber(value));
}
else {
result = result.mul(radixToPower);
result = result.add(Long.fromNumber(value));
}
}
result.unsigned = unsigned;
return result;
};
/**
* Creates a Long from its byte representation.
* @param bytes - Byte representation
* @param unsigned - Whether unsigned or not, defaults to signed
* @param le - Whether little or big endian, defaults to big endian
* @returns The corresponding Long value
*/
Long.fromBytes = function (bytes, unsigned, le) {
return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned);
};
/**
* Creates a Long from its little endian byte representation.
* @param bytes - Little endian byte representation
* @param unsigned - Whether unsigned or not, defaults to signed
* @returns The corresponding Long value
*/
Long.fromBytesLE = function (bytes, unsigned) {
return new Long(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24), bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24), unsigned);
};
/**
* Creates a Long from its big endian byte representation.
* @param bytes - Big endian byte representation
* @param unsigned - Whether unsigned or not, defaults to signed
* @returns The corresponding Long value
*/
Long.fromBytesBE = function (bytes, unsigned) {
return new Long((bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | bytes[7], (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3], unsigned);
};
/**
* Tests if the specified object is a Long.
*/
Long.isLong = function (value) {
return (0, utils_1.isObjectLike)(value) && value['__isLong__'] === true;
};
/**
* Converts the specified value to a Long.
* @param unsigned - Whether unsigned or not, defaults to signed
*/
Long.fromValue = function (val, unsigned) {
if (typeof val === 'number')
return Long.fromNumber(val, unsigned);
if (typeof val === 'string')
return Long.fromString(val, unsigned);
// Throws for non-objects, converts non-instanceof Long:
return Long.fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned);
};
/** Returns the sum of this and the specified Long. */
Long.prototype.add = function (addend) {
if (!Long.isLong(addend))
addend = Long.fromValue(addend);
// Divide each number into 4 chunks of 16 bits, and then sum the chunks.
var a48 = this.high >>> 16;
var a32 = this.high & 0xffff;
var a16 = this.low >>> 16;
var a00 = this.low & 0xffff;
var b48 = addend.high >>> 16;
var b32 = addend.high & 0xffff;
var b16 = addend.low >>> 16;
var b00 = addend.low & 0xffff;
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
c00 += a00 + b00;
c16 += c00 >>> 16;
c00 &= 0xffff;
c16 += a16 + b16;
c32 += c16 >>> 16;
c16 &= 0xffff;
c32 += a32 + b32;
c48 += c32 >>> 16;
c32 &= 0xffff;
c48 += a48 + b48;
c48 &= 0xffff;
return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
};
/**
* Returns the sum of this and the specified Long.
* @returns Sum
*/
Long.prototype.and = function (other) {
if (!Long.isLong(other))
other = Long.fromValue(other);
return Long.fromBits(this.low & other.low, this.high & other.high, this.unsigned);
};
/**
* Compares this Long's value with the specified's.
* @returns 0 if they are the same, 1 if the this is greater and -1 if the given one is greater
*/
Long.prototype.compare = function (other) {
if (!Long.isLong(other))
other = Long.fromValue(other);
if (this.eq(other))
return 0;
var thisNeg = this.isNegative(), otherNeg = other.isNegative();
if (thisNeg && !otherNeg)
return -1;
if (!thisNeg && otherNeg)
return 1;
// At this point the sign bits are the same
if (!this.unsigned)
return this.sub(other).isNegative() ? -1 : 1;
// Both are positive if at least one is unsigned
return other.high >>> 0 > this.high >>> 0 ||
(other.high === this.high && other.low >>> 0 > this.low >>> 0)
? -1
: 1;
};
/** This is an alias of {@link Long.compare} */
Long.prototype.comp = function (other) {
return this.compare(other);
};
/**
* Returns this Long divided by the specified. The result is signed if this Long is signed or unsigned if this Long is unsigned.
* @returns Quotient
*/
Long.prototype.divide = function (divisor) {
if (!Long.isLong(divisor))
divisor = Long.fromValue(divisor);
if (divisor.isZero())
throw Error('division by zero');
// use wasm support if present
if (wasm) {
// guard against signed division overflow: the largest
// negative number / -1 would be 1 larger than the largest
// positive number, due to two's complement.
if (!this.unsigned &&
this.high === -0x80000000 &&
divisor.low === -1 &&
divisor.high === -1) {
// be consistent with non-wasm code path
return this;
}
var low = (this.unsigned ? wasm.div_u : wasm.div_s)(this.low, this.high, divisor.low, divisor.high);
return Long.fromBits(low, wasm.get_high(), this.unsigned);
}
if (this.isZero())
return this.unsigned ? Long.UZERO : Long.ZERO;
var approx, rem, res;
if (!this.unsigned) {
// This section is only relevant for signed longs and is derived from the
// closure library as a whole.
if (this.eq(Long.MIN_VALUE)) {
if (divisor.eq(Long.ONE) || divisor.eq(Long.NEG_ONE))
return Long.MIN_VALUE;
// recall that -MIN_VALUE == MIN_VALUE
else if (divisor.eq(Long.MIN_VALUE))
return Long.ONE;
else {
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
var halfThis = this.shr(1);
approx = halfThis.div(divisor).shl(1);
if (approx.eq(Long.ZERO)) {
return divisor.isNegative() ? Long.ONE : Long.NEG_ONE;
}
else {
rem = this.sub(divisor.mul(approx));
res = approx.add(rem.div(divisor));
return res;
}
}
}
else if (divisor.eq(Long.MIN_VALUE))
return this.unsigned ? Long.UZERO : Long.ZERO;
if (this.isNegative()) {
if (divisor.isNegative())
return this.neg().div(divisor.neg());
return this.neg().div(divisor).neg();
}
else if (divisor.isNegative())
return this.div(divisor.neg()).neg();
res = Long.ZERO;
}
else {
// The algorithm below has not been made for unsigned longs. It's therefore
// required to take special care of the MSB prior to running it.
if (!divisor.unsigned)
divisor = divisor.toUnsigned();
if (divisor.gt(this))
return Long.UZERO;
if (divisor.gt(this.shru(1)))
// 15 >>> 1 = 7 ; with divisor = 8 ; true
return Long.UONE;
res = Long.UZERO;
}
// Repeat the following until the remainder is less than other: find a
// floating-point that approximates remainder / other *from below*, add this
// into the result, and subtract it from the remainder. It is critical that
// the approximate value is less than or equal to the real value so that the
// remainder never becomes negative.
// eslint-disable-next-line @typescript-eslint/no-this-alias
rem = this;
while (rem.gte(divisor)) {
// Approximate the result of division. This may be a little greater or
// smaller than the actual value.
approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
// We will tweak the approximate result by changing it in the 48-th digit or
// the smallest non-fractional digit, whichever is larger.
var log2 = Math.ceil(Math.log(approx) / Math.LN2);
var delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48);
// Decrease the approximation until it is smaller than the remainder. Note
// that if it is too large, the product overflows and is negative.
var approxRes = Long.fromNumber(approx);
var approxRem = approxRes.mul(divisor);
while (approxRem.isNegative() || approxRem.gt(rem)) {
approx -= delta;
approxRes = Long.fromNumber(approx, this.unsigned);
approxRem = approxRes.mul(divisor);
}
// We know the answer can't be zero... and actually, zero would cause
// infinite recursion since we would make no progress.
if (approxRes.isZero())
approxRes = Long.ONE;
res = res.add(approxRes);
rem = rem.sub(approxRem);
}
return res;
};
/**This is an alias of {@link Long.divide} */
Long.prototype.div = function (divisor) {
return this.divide(divisor);
};
/**
* Tests if this Long's value equals the specified's.
* @param other - Other value
*/
Long.prototype.equals = function (other) {
if (!Long.isLong(other))
other = Long.fromValue(other);
if (this.unsigned !== other.unsigned && this.high >>> 31 === 1 && other.high >>> 31 === 1)
return false;
return this.high === other.high && this.low === other.low;
};
/** This is an alias of {@link Long.equals} */
Long.prototype.eq = function (other) {
return this.equals(other);
};
/** Gets the high 32 bits as a signed integer. */
Long.prototype.getHighBits = function () {
return this.high;
};
/** Gets the high 32 bits as an unsigned integer. */
Long.prototype.getHighBitsUnsigned = function () {
return this.high >>> 0;
};
/** Gets the low 32 bits as a signed integer. */
Long.prototype.getLowBits = function () {
return this.low;
};
/** Gets the low 32 bits as an unsigned integer. */
Long.prototype.getLowBitsUnsigned = function () {
return this.low >>> 0;
};
/** Gets the number of bits needed to represent the absolute value of this Long. */
Long.prototype.getNumBitsAbs = function () {
if (this.isNegative()) {
// Unsigned Longs are never negative
return this.eq(Long.MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
}
var val = this.high !== 0 ? this.high : this.low;
var bit;
for (bit = 31; bit > 0; bit--)
if ((val & (1 << bit)) !== 0)
break;
return this.high !== 0 ? bit + 33 : bit + 1;
};
/** Tests if this Long's value is greater than the specified's. */
Long.prototype.greaterThan = function (other) {
return this.comp(other) > 0;
};
/** This is an alias of {@link Long.greaterThan} */
Long.prototype.gt = function (other) {
return this.greaterThan(other);
};
/** Tests if this Long's value is greater than or equal the specified's. */
Long.prototype.greaterThanOrEqual = function (other) {
return this.comp(other) >= 0;
};
/** This is an alias of {@link Long.greaterThanOrEqual} */
Long.prototype.gte = function (other) {
return this.greaterThanOrEqual(other);
};
/** This is an alias of {@link Long.greaterThanOrEqual} */
Long.prototype.ge = function (other) {
return this.greaterThanOrEqual(other);
};
/** Tests if this Long's value is even. */
Long.prototype.isEven = function () {
return (this.low & 1) === 0;
};
/** Tests if this Long's value is negative. */
Long.prototype.isNegative = function () {
return !this.unsigned && this.high < 0;
};
/** Tests if this Long's value is odd. */
Long.prototype.isOdd = function () {
return (this.low & 1) === 1;
};
/** Tests if this Long's value is positive. */
Long.prototype.isPositive = function () {
return this.unsigned || this.high >= 0;
};
/** Tests if this Long's value equals zero. */
Long.prototype.isZero = function () {
return this.high === 0 && this.low === 0;
};
/** Tests if this Long's value is less than the specified's. */
Long.prototype.lessThan = function (other) {
return this.comp(other) < 0;
};
/** This is an alias of {@link Long#lessThan}. */
Long.prototype.lt = function (other) {
return this.lessThan(other);
};
/** Tests if this Long's value is less than or equal the specified's. */
Long.prototype.lessThanOrEqual = function (other) {
return this.comp(other) <= 0;
};
/** This is an alias of {@link Long.lessThanOrEqual} */
Long.prototype.lte = function (other) {
return this.lessThanOrEqual(other);
};
/** Returns this Long modulo the specified. */
Long.prototype.modulo = function (divisor) {
if (!Long.isLong(divisor))
divisor = Long.fromValue(divisor);
// use wasm support if present
if (wasm) {
var low = (this.unsigned ? wasm.rem_u : wasm.rem_s)(this.low, this.high, divisor.low, divisor.high);
return Long.fromBits(low, wasm.get_high(), this.unsigned);
}
return this.sub(this.div(divisor).mul(divisor));
};
/** This is an alias of {@link Long.modulo} */
Long.prototype.mod = function (divisor) {
return this.modulo(divisor);
};
/** This is an alias of {@link Long.modulo} */
Long.prototype.rem = function (divisor) {
return this.modulo(divisor);
};
/**
* Returns the product of this and the specified Long.
* @param multiplier - Multiplier
* @returns Product
*/
Long.prototype.multiply = function (multiplier) {
if (this.isZero())
return Long.ZERO;
if (!Long.isLong(multiplier))
multiplier = Long.fromValue(multiplier);
// use wasm support if present
if (wasm) {
var low = wasm.mul(this.low, this.high, multiplier.low, multiplier.high);
return Long.fromBits(low, wasm.get_high(), this.unsigned);
}
if (multiplier.isZero())
return Long.ZERO;
if (this.eq(Long.MIN_VALUE))
return multiplier.isOdd() ? Long.MIN_VALUE : Long.ZERO;
if (multiplier.eq(Long.MIN_VALUE))
return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
if (this.isNegative()) {
if (multiplier.isNegative())
return this.neg().mul(multiplier.neg());
else
return this.neg().mul(multiplier).neg();
}
else if (multiplier.isNegative())
return this.mul(multiplier.neg()).neg();
// If both longs are small, use float multiplication
if (this.lt(Long.TWO_PWR_24) && multiplier.lt(Long.TWO_PWR_24))
return Long.fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
// Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
// We can skip products that would overflow.
var a48 = this.high >>> 16;
var a32 = this.high & 0xffff;
var a16 = this.low >>> 16;
var a00 = this.low & 0xffff;
var b48 = multiplier.high >>> 16;
var b32 = multiplier.high & 0xffff;
var b16 = multiplier.low >>> 16;
var b00 = multiplier.low & 0xffff;
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
c00 += a00 * b00;
c16 += c00 >>> 16;
c00 &= 0xffff;
c16 += a16 * b00;
c32 += c16 >>> 16;
c16 &= 0xffff;
c16 += a00 * b16;
c32 += c16 >>> 16;
c16 &= 0xffff;
c32 += a32 * b00;
c48 += c32 >>> 16;
c32 &= 0xffff;
c32 += a16 * b16;
c48 += c32 >>> 16;
c32 &= 0xffff;
c32 += a00 * b32;
c48 += c32 >>> 16;
c32 &= 0xffff;
c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
c48 &= 0xffff;
return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
};
/** This is an alias of {@link Long.multiply} */
Long.prototype.mul = function (multiplier) {
return this.multiply(multiplier);
};
/** Returns the Negation of this Long's value. */
Long.prototype.negate = function () {
if (!this.unsigned && this.eq(Long.MIN_VALUE))
return Long.MIN_VALUE;
return this.not().add(Long.ONE);
};
/** This is an alias of {@link Long.negate} */
Long.prototype.neg = function () {
return this.negate();
};
/** Returns the bitwise NOT of this Long. */
Long.prototype.not = function () {
return Long.fromBits(~this.low, ~this.high, this.unsigned);
};
/** Tests if this Long's value differs from the specified's. */
Long.prototype.notEquals = function (other) {
return !this.equals(other);
};
/** This is an alias of {@link Long.notEquals} */
Long.prototype.neq = function (other) {
return this.notEquals(other);
};
/** This is an alias of {@link Long.notEquals} */
Long.prototype.ne = function (other) {
return this.notEquals(other);
};
/**
* Returns the bitwise OR of this Long and the specified.
*/
Long.prototype.or = function (other) {
if (!Long.isLong(other))
other = Long.fromValue(other);
return Long.fromBits(this.low | other.low, this.high | other.high, this.unsigned);
};
/**
* Returns this Long with bits shifted to the left by the given amount.
* @param numBits - Number of bits
* @returns Shifted Long
*/
Long.prototype.shiftLeft = function (numBits) {
if (Long.isLong(numBits))
numBits = numBits.toInt();
if ((numBits &= 63) === 0)
return this;
else if (numBits < 32)
return Long.fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
else
return Long.fromBits(0, this.low << (numBits - 32), this.unsigned);
};
/** This is an alias of {@link Long.shiftLeft} */
Long.prototype.shl = function (numBits) {
return this.shiftLeft(numBits);
};
/**
* Returns this Long with bits arithmetically shifted to the right by the given amount.
* @param numBits - Number of bits
* @returns Shifted Long
*/
Long.prototype.shiftRight = function (numBits) {
if (Long.isLong(numBits))
numBits = numBits.toInt();
if ((numBits &= 63) === 0)
return this;
else if (numBits < 32)
return Long.fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
else
return Long.fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
};
/** This is an alias of {@link Long.shiftRight} */
Long.prototype.shr = function (numBits) {
return this.shiftRight(numBits);
};
/**
* Returns this Long with bits logically shifted to the right by the given amount.
* @param numBits - Number of bits
* @returns Shifted Long
*/
Long.prototype.shiftRightUnsigned = function (numBits) {
if (Long.isLong(numBits))
numBits = numBits.toInt();
numBits &= 63;
if (numBits === 0)
return this;
else {
var high = this.high;
if (numBits < 32) {
var low = this.low;
return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned);
}
else if (numBits === 32)
return Long.fromBits(high, 0, this.unsigned);
else
return Long.fromBits(high >>> (numBits - 32), 0, this.unsigned);
}
};
/** This is an alias of {@link Long.shiftRightUnsigned} */
Long.prototype.shr_u = function (numBits) {
return this.shiftRightUnsigned(numBits);
};
/** This is an alias of {@link Long.shiftRightUnsigned} */
Long.prototype.shru = function (numBits) {
return this.shiftRightUnsigned(numBits);
};
/**
* Returns the difference of this and the specified Long.
* @param subtrahend - Subtrahend
* @returns Difference
*/
Long.prototype.subtract = function (subtrahend) {
if (!Long.isLong(subtrahend))
subtrahend = Long.fromValue(subtrahend);
return this.add(subtrahend.neg());
};
/** This is an alias of {@link Long.subtract} */
Long.prototype.sub = function (subtrahend) {
return this.subtract(subtrahend);
};
/** Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. */
Long.prototype.toInt = function () {
return this.unsigned ? this.low >>> 0 : this.low;
};
/** Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). */
Long.prototype.toNumber = function () {
if (this.unsigned)
return (this.high >>> 0) * TWO_PWR_32_DBL + (this.low >>> 0);
return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
};
/** Converts the Long to a BigInt (arbitrary precision). */
Long.prototype.toBigInt = function () {
return BigInt(this.toString());
};
/**
* Converts this Long to its byte representation.
* @param le - Whether little or big endian, defaults to big endian
* @returns Byte representation
*/
Long.prototype.toBytes = function (le) {
return le ? this.toBytesLE() : this.toBytesBE();
};
/**
* Converts this Long to its little endian byte representation.
* @returns Little endian byte representation
*/
Long.prototype.toBytesLE = function () {
var hi = this.high, lo = this.low;
return [
lo & 0xff,
(lo >>> 8) & 0xff,
(lo >>> 16) & 0xff,
lo >>> 24,
hi & 0xff,
(hi >>> 8) & 0xff,
(hi >>> 16) & 0xff,
hi >>> 24
];
};
/**
* Converts this Long to its big endian byte representation.
* @returns Big endian byte representation
*/
Long.prototype.toBytesBE = function () {
var hi = this.high, lo = this.low;
return [
hi >>> 24,
(hi >>> 16) & 0xff,
(hi >>> 8) & 0xff,
hi & 0xff,
lo >>> 24,
(lo >>> 16) & 0xff,
(lo >>> 8) & 0xff,
lo & 0xff
];
};
/**
* Converts this Long to signed.
*/
Long.prototype.toSigned = function () {
if (!this.unsigned)
return this;
return Long.fromBits(this.low, this.high, false);
};
/**
* Converts the Long to a string written in the specified radix.
* @param radix - Radix (2-36), defaults to 10
* @throws RangeError If `radix` is out of range
*/
Long.prototype.toString = function (radix) {
radix = radix || 10;
if (radix < 2 || 36 < radix)
throw RangeError('radix');
if (this.isZero())
return '0';
if (this.isNegative()) {
// Unsigned Longs are never negative
if (this.eq(Long.MIN_VALUE)) {
// We need to change the Long value before it can be negated, so we remove
// the bottom-most digit in this base and then recurse to do the rest.
var radixLong = Long.fromNumber(radix), div = this.div(radixLong), rem1 = div.mul(radixLong).sub(this);
return div.toString(radix) + rem1.toInt().toString(radix);
}
else
return '-' + this.neg().toString(radix);
}
// Do several (6) digits each time through the loop, so as to
// minimize the calls to the very expensive emulated div.
var radixToPower = Long.fromNumber(Math.pow(radix, 6), this.unsigned);
// eslint-disable-next-line @typescript-eslint/no-this-alias
var rem = this;
var result = '';
// eslint-disable-next-line no-constant-condition
while (true) {
var remDiv = rem.div(radixToPower);
var intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0;
var digits = intval.toString(radix);
rem = remDiv;
if (rem.isZero()) {
return digits + result;
}
else {
while (digits.length < 6)
digits = '0' + digits;
result = '' + digits + result;
}
}
};
/** Converts this Long to unsigned. */
Long.prototype.toUnsigned = function () {
if (this.unsigned)
return this;
return Long.fromBits(this.low, this.high, true);
};
/** Returns the bitwise XOR of this Long and the given one. */
Long.prototype.xor = function (other) {
if (!Long.isLong(other))
other = Long.fromValue(other);
return Long.fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
};
/** This is an alias of {@link Long.isZero} */
Long.prototype.eqz = function () {
return this.isZero();
};
/** This is an alias of {@link Long.lessThanOrEqual} */
Long.prototype.le = function (other) {
return this.lessThanOrEqual(other);
};
/*
****************************************************************
* BSON SPECIFIC ADDITIONS *
****************************************************************
*/
Long.prototype.toExtendedJSON = function (options) {
if (options && options.relaxed)
return this.toNumber();
return { $numberLong: this.toString() };
};
Long.fromExtendedJSON = function (doc, options) {
var result = Long.fromString(doc.$numberLong);
return options && options.relaxed ? result.toNumber() : result;
};
/** @internal */
Long.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
Long.prototype.inspect = function () {
return "new Long(\"".concat(this.toString(), "\"").concat(this.unsigned ? ', true' : '', ")");
};
Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
/** Maximum unsigned value. */
Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
/** Signed zero */
Long.ZERO = Long.fromInt(0);
/** Unsigned zero. */
Long.UZERO = Long.fromInt(0, true);
/** Signed one. */
Long.ONE = Long.fromInt(1);
/** Unsigned one. */
Long.UONE = Long.fromInt(1, true);
/** Signed negative one. */
Long.NEG_ONE = Long.fromInt(-1);
/** Maximum signed value. */
Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
/** Minimum signed value. */
Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
return Long;
}());
exports.Long = Long;
Object.defineProperty(Long.prototype, '__isLong__', { value: true });
Object.defineProperty(Long.prototype, '_bsontype', { value: 'Long' });
//# sourceMappingURL=long.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,123 @@
"use strict";
/* eslint-disable @typescript-eslint/no-explicit-any */
// We have an ES6 Map available, return the native instance
Object.defineProperty(exports, "__esModule", { value: true });
exports.Map = void 0;
var global_1 = require("./utils/global");
/** @public */
var bsonMap;
exports.Map = bsonMap;
var bsonGlobal = (0, global_1.getGlobal)();
if (bsonGlobal.Map) {
exports.Map = bsonMap = bsonGlobal.Map;
}
else {
// We will return a polyfill
exports.Map = bsonMap = /** @class */ (function () {
function Map(array) {
if (array === void 0) { array = []; }
this._keys = [];
this._values = {};
for (var i = 0; i < array.length; i++) {
if (array[i] == null)
continue; // skip null and undefined
var entry = array[i];
var key = entry[0];
var value = entry[1];
// Add the key to the list of keys in order
this._keys.push(key);
// Add the key and value to the values dictionary with a point
// to the location in the ordered keys list
this._values[key] = { v: value, i: this._keys.length - 1 };
}
}
Map.prototype.clear = function () {
this._keys = [];
this._values = {};
};
Map.prototype.delete = function (key) {
var value = this._values[key];
if (value == null)
return false;
// Delete entry
delete this._values[key];
// Remove the key from the ordered keys list
this._keys.splice(value.i, 1);
return true;
};
Map.prototype.entries = function () {
var _this = this;
var index = 0;
return {
next: function () {
var key = _this._keys[index++];
return {
value: key !== undefined ? [key, _this._values[key].v] : undefined,
done: key !== undefined ? false : true
};
}
};
};
Map.prototype.forEach = function (callback, self) {
self = self || this;
for (var i = 0; i < this._keys.length; i++) {
var key = this._keys[i];
// Call the forEach callback
callback.call(self, this._values[key].v, key, self);
}
};
Map.prototype.get = function (key) {
return this._values[key] ? this._values[key].v : undefined;
};
Map.prototype.has = function (key) {
return this._values[key] != null;
};
Map.prototype.keys = function () {
var _this = this;
var index = 0;
return {
next: function () {
var key = _this._keys[index++];
return {
value: key !== undefined ? key : undefined,
done: key !== undefined ? false : true
};
}
};
};
Map.prototype.set = function (key, value) {
if (this._values[key]) {
this._values[key].v = value;
return this;
}
// Add the key to the list of keys in order
this._keys.push(key);
// Add the key and value to the values dictionary with a point
// to the location in the ordered keys list
this._values[key] = { v: value, i: this._keys.length - 1 };
return this;
};
Map.prototype.values = function () {
var _this = this;
var index = 0;
return {
next: function () {
var key = _this._keys[index++];
return {
value: key !== undefined ? _this._values[key].v : undefined,
done: key !== undefined ? false : true
};
}
};
};
Object.defineProperty(Map.prototype, "size", {
get: function () {
return this._keys.length;
},
enumerable: false,
configurable: true
});
return Map;
}());
}
//# sourceMappingURL=map.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"map.js","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":";AAAA,uDAAuD;AACvD,2DAA2D;;;AAE3D,yCAA2C;AAE3C,cAAc;AACd,IAAI,OAAuB,CAAC;AAgHR,sBAAG;AA9GvB,IAAM,UAAU,GAAG,IAAA,kBAAS,GAA4B,CAAC;AACzD,IAAI,UAAU,CAAC,GAAG,EAAE;IAClB,cAAA,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC;CAC1B;KAAM;IACL,4BAA4B;IAC5B,cAAA,OAAO,GAAG;QAGR,aAAY,KAA2B;YAA3B,sBAAA,EAAA,UAA2B;YACrC,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;YAChB,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;YAElB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;gBACrC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI;oBAAE,SAAS,CAAC,0BAA0B;gBAC1D,IAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;gBACvB,IAAM,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;gBACrB,IAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;gBACvB,2CAA2C;gBAC3C,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBACrB,8DAA8D;gBAC9D,2CAA2C;gBAC3C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;aAC5D;QACH,CAAC;QACD,mBAAK,GAAL;YACE,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;YAChB,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QACpB,CAAC;QACD,oBAAM,GAAN,UAAO,GAAW;YAChB,IAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAChC,IAAI,KAAK,IAAI,IAAI;gBAAE,OAAO,KAAK,CAAC;YAChC,eAAe;YACf,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACzB,4CAA4C;YAC5C,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YAC9B,OAAO,IAAI,CAAC;QACd,CAAC;QACD,qBAAO,GAAP;YAAA,iBAYC;YAXC,IAAI,KAAK,GAAG,CAAC,CAAC;YAEd,OAAO;gBACL,IAAI,EAAE;oBACJ,IAAM,GAAG,GAAG,KAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;oBAChC,OAAO;wBACL,KAAK,EAAE,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,KAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS;wBACjE,IAAI,EAAE,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI;qBACvC,CAAC;gBACJ,CAAC;aACF,CAAC;QACJ,CAAC;QACD,qBAAO,GAAP,UAAQ,QAAmE,EAAE,IAAW;YACtF,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC;YAEpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;gBAC1C,IAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAC1B,4BAA4B;gBAC5B,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;aACrD;QACH,CAAC;QACD,iBAAG,GAAH,UAAI,GAAW;YACb,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC7D,CAAC;QACD,iBAAG,GAAH,UAAI,GAAW;YACb,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC;QACnC,CAAC;QACD,kBAAI,GAAJ;YAAA,iBAYC;YAXC,IAAI,KAAK,GAAG,CAAC,CAAC;YAEd,OAAO;gBACL,IAAI,EAAE;oBACJ,IAAM,GAAG,GAAG,KAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;oBAChC,OAAO;wBACL,KAAK,EAAE,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS;wBAC1C,IAAI,EAAE,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI;qBACvC,CAAC;gBACJ,CAAC;aACF,CAAC;QACJ,CAAC;QACD,iBAAG,GAAH,UAAI,GAAW,EAAE,KAAU;YACzB,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;gBACrB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;gBAC5B,OAAO,IAAI,CAAC;aACb;YAED,2CAA2C;YAC3C,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACrB,8DAA8D;YAC9D,2CAA2C;YAC3C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC3D,OAAO,IAAI,CAAC;QACd,CAAC;QACD,oBAAM,GAAN;YAAA,iBAYC;YAXC,IAAI,KAAK,GAAG,CAAC,CAAC;YAEd,OAAO;gBACL,IAAI,EAAE;oBACJ,IAAM,GAAG,GAAG,KAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;oBAChC,OAAO;wBACL,KAAK,EAAE,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,KAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS;wBAC1D,IAAI,EAAE,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI;qBACvC,CAAC;gBACJ,CAAC;aACF,CAAC;QACJ,CAAC;QACD,sBAAI,qBAAI;iBAAR;gBACE,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;YAC3B,CAAC;;;WAAA;QACH,UAAC;IAAD,CAAC,AAtGS,GAsGoB,CAAC;CAChC"}

View File

@@ -0,0 +1,33 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MaxKey = void 0;
/**
* A class representation of the BSON MaxKey type.
* @public
* @category BSONType
*/
var MaxKey = /** @class */ (function () {
function MaxKey() {
if (!(this instanceof MaxKey))
return new MaxKey();
}
/** @internal */
MaxKey.prototype.toExtendedJSON = function () {
return { $maxKey: 1 };
};
/** @internal */
MaxKey.fromExtendedJSON = function () {
return new MaxKey();
};
/** @internal */
MaxKey.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
MaxKey.prototype.inspect = function () {
return 'new MaxKey()';
};
return MaxKey;
}());
exports.MaxKey = MaxKey;
Object.defineProperty(MaxKey.prototype, '_bsontype', { value: 'MaxKey' });
//# sourceMappingURL=max_key.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"max_key.js","sourceRoot":"","sources":["../src/max_key.ts"],"names":[],"mappings":";;;AAKA;;;;GAIG;AACH;IAGE;QACE,IAAI,CAAC,CAAC,IAAI,YAAY,MAAM,CAAC;YAAE,OAAO,IAAI,MAAM,EAAE,CAAC;IACrD,CAAC;IAED,gBAAgB;IAChB,+BAAc,GAAd;QACE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;IACxB,CAAC;IAED,gBAAgB;IACT,uBAAgB,GAAvB;QACE,OAAO,IAAI,MAAM,EAAE,CAAC;IACtB,CAAC;IAED,gBAAgB;IAChB,iBAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,wBAAO,GAAP;QACE,OAAO,cAAc,CAAC;IACxB,CAAC;IACH,aAAC;AAAD,CAAC,AAzBD,IAyBC;AAzBY,wBAAM;AA2BnB,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC"}

View File

@@ -0,0 +1,33 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MinKey = void 0;
/**
* A class representation of the BSON MinKey type.
* @public
* @category BSONType
*/
var MinKey = /** @class */ (function () {
function MinKey() {
if (!(this instanceof MinKey))
return new MinKey();
}
/** @internal */
MinKey.prototype.toExtendedJSON = function () {
return { $minKey: 1 };
};
/** @internal */
MinKey.fromExtendedJSON = function () {
return new MinKey();
};
/** @internal */
MinKey.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
MinKey.prototype.inspect = function () {
return 'new MinKey()';
};
return MinKey;
}());
exports.MinKey = MinKey;
Object.defineProperty(MinKey.prototype, '_bsontype', { value: 'MinKey' });
//# sourceMappingURL=min_key.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"min_key.js","sourceRoot":"","sources":["../src/min_key.ts"],"names":[],"mappings":";;;AAKA;;;;GAIG;AACH;IAGE;QACE,IAAI,CAAC,CAAC,IAAI,YAAY,MAAM,CAAC;YAAE,OAAO,IAAI,MAAM,EAAE,CAAC;IACrD,CAAC;IAED,gBAAgB;IAChB,+BAAc,GAAd;QACE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;IACxB,CAAC;IAED,gBAAgB;IACT,uBAAgB,GAAvB;QACE,OAAO,IAAI,MAAM,EAAE,CAAC;IACtB,CAAC;IAED,gBAAgB;IAChB,iBAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,wBAAO,GAAP;QACE,OAAO,cAAc,CAAC;IACxB,CAAC;IACH,aAAC;AAAD,CAAC,AAzBD,IAyBC;AAzBY,wBAAM;AA2BnB,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC"}

View File

@@ -0,0 +1,299 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectId = void 0;
var buffer_1 = require("buffer");
var ensure_buffer_1 = require("./ensure_buffer");
var error_1 = require("./error");
var utils_1 = require("./parser/utils");
// Regular expression that checks for hex value
var checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
// Unique sequence for the current process (initialized on first use)
var PROCESS_UNIQUE = null;
var kId = Symbol('id');
/**
* A class representation of the BSON ObjectId type.
* @public
* @category BSONType
*/
var ObjectId = /** @class */ (function () {
/**
* Create an ObjectId type
*
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
*/
function ObjectId(inputId) {
if (!(this instanceof ObjectId))
return new ObjectId(inputId);
// workingId is set based on type of input and whether valid id exists for the input
var workingId;
if (typeof inputId === 'object' && inputId && 'id' in inputId) {
if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
throw new error_1.BSONTypeError('Argument passed in must have an id that is of type string or Buffer');
}
if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
workingId = buffer_1.Buffer.from(inputId.toHexString(), 'hex');
}
else {
workingId = inputId.id;
}
}
else {
workingId = inputId;
}
// the following cases use workingId to construct an ObjectId
if (workingId == null || typeof workingId === 'number') {
// The most common use case (blank id, new objectId instance)
// Generate a new id
this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
}
else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
this[kId] = workingId instanceof buffer_1.Buffer ? workingId : (0, ensure_buffer_1.ensureBuffer)(workingId);
}
else if (typeof workingId === 'string') {
if (workingId.length === 12) {
var bytes = buffer_1.Buffer.from(workingId);
if (bytes.byteLength === 12) {
this[kId] = bytes;
}
else {
throw new error_1.BSONTypeError('Argument passed in must be a string of 12 bytes');
}
}
else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
this[kId] = buffer_1.Buffer.from(workingId, 'hex');
}
else {
throw new error_1.BSONTypeError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
}
}
else {
throw new error_1.BSONTypeError('Argument passed in does not match the accepted types');
}
// If we are caching the hex string
if (ObjectId.cacheHexString) {
this.__id = this.id.toString('hex');
}
}
Object.defineProperty(ObjectId.prototype, "id", {
/**
* The ObjectId bytes
* @readonly
*/
get: function () {
return this[kId];
},
set: function (value) {
this[kId] = value;
if (ObjectId.cacheHexString) {
this.__id = value.toString('hex');
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(ObjectId.prototype, "generationTime", {
/**
* The generation time of this ObjectId instance
* @deprecated Please use getTimestamp / createFromTime which returns an int32 epoch
*/
get: function () {
return this.id.readInt32BE(0);
},
set: function (value) {
// Encode time into first 4 bytes
this.id.writeUInt32BE(value, 0);
},
enumerable: false,
configurable: true
});
/** Returns the ObjectId id as a 24 character hex string representation */
ObjectId.prototype.toHexString = function () {
if (ObjectId.cacheHexString && this.__id) {
return this.__id;
}
var hexString = this.id.toString('hex');
if (ObjectId.cacheHexString && !this.__id) {
this.__id = hexString;
}
return hexString;
};
/**
* Update the ObjectId index
* @privateRemarks
* Used in generating new ObjectId's on the driver
* @internal
*/
ObjectId.getInc = function () {
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
};
/**
* Generate a 12 byte id buffer used in ObjectId's
*
* @param time - pass in a second based timestamp.
*/
ObjectId.generate = function (time) {
if ('number' !== typeof time) {
time = Math.floor(Date.now() / 1000);
}
var inc = ObjectId.getInc();
var buffer = buffer_1.Buffer.alloc(12);
// 4-byte timestamp
buffer.writeUInt32BE(time, 0);
// set PROCESS_UNIQUE if yet not initialized
if (PROCESS_UNIQUE === null) {
PROCESS_UNIQUE = (0, utils_1.randomBytes)(5);
}
// 5-byte process unique
buffer[4] = PROCESS_UNIQUE[0];
buffer[5] = PROCESS_UNIQUE[1];
buffer[6] = PROCESS_UNIQUE[2];
buffer[7] = PROCESS_UNIQUE[3];
buffer[8] = PROCESS_UNIQUE[4];
// 3-byte counter
buffer[11] = inc & 0xff;
buffer[10] = (inc >> 8) & 0xff;
buffer[9] = (inc >> 16) & 0xff;
return buffer;
};
/**
* Converts the id into a 24 character hex string for printing
*
* @param format - The Buffer toString format parameter.
*/
ObjectId.prototype.toString = function (format) {
// Is the id a buffer then use the buffer toString method to return the format
if (format)
return this.id.toString(format);
return this.toHexString();
};
/** Converts to its JSON the 24 character hex string representation. */
ObjectId.prototype.toJSON = function () {
return this.toHexString();
};
/**
* Compares the equality of this ObjectId with `otherID`.
*
* @param otherId - ObjectId instance to compare against.
*/
ObjectId.prototype.equals = function (otherId) {
if (otherId === undefined || otherId === null) {
return false;
}
if (otherId instanceof ObjectId) {
return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
}
if (typeof otherId === 'string' &&
ObjectId.isValid(otherId) &&
otherId.length === 12 &&
(0, utils_1.isUint8Array)(this.id)) {
return otherId === buffer_1.Buffer.prototype.toString.call(this.id, 'latin1');
}
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
return otherId.toLowerCase() === this.toHexString();
}
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
return buffer_1.Buffer.from(otherId).equals(this.id);
}
if (typeof otherId === 'object' &&
'toHexString' in otherId &&
typeof otherId.toHexString === 'function') {
var otherIdString = otherId.toHexString();
var thisIdString = this.toHexString().toLowerCase();
return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
}
return false;
};
/** Returns the generation date (accurate up to the second) that this ID was generated. */
ObjectId.prototype.getTimestamp = function () {
var timestamp = new Date();
var time = this.id.readUInt32BE(0);
timestamp.setTime(Math.floor(time) * 1000);
return timestamp;
};
/** @internal */
ObjectId.createPk = function () {
return new ObjectId();
};
/**
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
*
* @param time - an integer number representing a number of seconds.
*/
ObjectId.createFromTime = function (time) {
var buffer = buffer_1.Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
// Encode time into first 4 bytes
buffer.writeUInt32BE(time, 0);
// Return the new objectId
return new ObjectId(buffer);
};
/**
* Creates an ObjectId from a hex string representation of an ObjectId.
*
* @param hexString - create a ObjectId from a passed in 24 character hexstring.
*/
ObjectId.createFromHexString = function (hexString) {
// Throw an error if it's not a valid setup
if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
throw new error_1.BSONTypeError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
}
return new ObjectId(buffer_1.Buffer.from(hexString, 'hex'));
};
/**
* Checks if a value is a valid bson ObjectId
*
* @param id - ObjectId instance to validate.
*/
ObjectId.isValid = function (id) {
if (id == null)
return false;
try {
new ObjectId(id);
return true;
}
catch (_a) {
return false;
}
};
/** @internal */
ObjectId.prototype.toExtendedJSON = function () {
if (this.toHexString)
return { $oid: this.toHexString() };
return { $oid: this.toString('hex') };
};
/** @internal */
ObjectId.fromExtendedJSON = function (doc) {
return new ObjectId(doc.$oid);
};
/**
* Converts to a string representation of this Id.
*
* @returns return the 24 character hex string representation.
* @internal
*/
ObjectId.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
ObjectId.prototype.inspect = function () {
return "new ObjectId(\"".concat(this.toHexString(), "\")");
};
/** @internal */
ObjectId.index = Math.floor(Math.random() * 0xffffff);
return ObjectId;
}());
exports.ObjectId = ObjectId;
// Deprecated methods
Object.defineProperty(ObjectId.prototype, 'generate', {
value: (0, utils_1.deprecate)(function (time) { return ObjectId.generate(time); }, 'Please use the static `ObjectId.generate(time)` instead')
});
Object.defineProperty(ObjectId.prototype, 'getInc', {
value: (0, utils_1.deprecate)(function () { return ObjectId.getInc(); }, 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId.prototype, 'get_inc', {
value: (0, utils_1.deprecate)(function () { return ObjectId.getInc(); }, 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId, 'get_inc', {
value: (0, utils_1.deprecate)(function () { return ObjectId.getInc(); }, 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' });
//# sourceMappingURL=objectid.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,194 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateObjectSize = void 0;
var buffer_1 = require("buffer");
var binary_1 = require("../binary");
var constants = require("../constants");
var utils_1 = require("./utils");
function calculateObjectSize(object, serializeFunctions, ignoreUndefined) {
var totalLength = 4 + 1;
if (Array.isArray(object)) {
for (var i = 0; i < object.length; i++) {
totalLength += calculateElement(i.toString(), object[i], serializeFunctions, true, ignoreUndefined);
}
}
else {
// If we have toBSON defined, override the current object
if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
object = object.toBSON();
}
// Calculate size
for (var key in object) {
totalLength += calculateElement(key, object[key], serializeFunctions, false, ignoreUndefined);
}
}
return totalLength;
}
exports.calculateObjectSize = calculateObjectSize;
/** @internal */
function calculateElement(name,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value, serializeFunctions, isArray, ignoreUndefined) {
if (serializeFunctions === void 0) { serializeFunctions = false; }
if (isArray === void 0) { isArray = false; }
if (ignoreUndefined === void 0) { ignoreUndefined = false; }
// If we have toBSON defined, override the current object
if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
value = value.toBSON();
}
switch (typeof value) {
case 'string':
return 1 + buffer_1.Buffer.byteLength(name, 'utf8') + 1 + 4 + buffer_1.Buffer.byteLength(value, 'utf8') + 1;
case 'number':
if (Math.floor(value) === value &&
value >= constants.JS_INT_MIN &&
value <= constants.JS_INT_MAX) {
if (value >= constants.BSON_INT32_MIN && value <= constants.BSON_INT32_MAX) {
// 32 bit
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1);
}
else {
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
}
else {
// 64 bit
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
case 'undefined':
if (isArray || !ignoreUndefined)
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
return 0;
case 'boolean':
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
case 'object':
if (value == null || value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
}
else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
}
else if (value instanceof Date || (0, utils_1.isDate)(value)) {
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
else if (ArrayBuffer.isView(value) ||
value instanceof ArrayBuffer ||
(0, utils_1.isAnyArrayBuffer)(value)) {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + value.byteLength);
}
else if (value['_bsontype'] === 'Long' ||
value['_bsontype'] === 'Double' ||
value['_bsontype'] === 'Timestamp') {
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
else if (value['_bsontype'] === 'Decimal128') {
return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
}
else if (value['_bsontype'] === 'Code') {
// Calculate size depending on the availability of a scope
if (value.scope != null && Object.keys(value.scope).length > 0) {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
4 +
buffer_1.Buffer.byteLength(value.code.toString(), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined));
}
else {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
buffer_1.Buffer.byteLength(value.code.toString(), 'utf8') +
1);
}
}
else if (value['_bsontype'] === 'Binary') {
var binary = value;
// Check what kind of subtype we have
if (binary.sub_type === binary_1.Binary.SUBTYPE_BYTE_ARRAY) {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
(binary.position + 1 + 4 + 1 + 4));
}
else {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (binary.position + 1 + 4 + 1));
}
}
else if (value['_bsontype'] === 'Symbol') {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
buffer_1.Buffer.byteLength(value.value, 'utf8') +
4 +
1 +
1);
}
else if (value['_bsontype'] === 'DBRef') {
// Set up correct object for serialization
var ordered_values = Object.assign({
$ref: value.collection,
$id: value.oid
}, value.fields);
// Add db reference if it exists
if (value.db != null) {
ordered_values['$db'] = value.db;
}
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined));
}
else if (value instanceof RegExp || (0, utils_1.isRegExp)(value)) {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
buffer_1.Buffer.byteLength(value.source, 'utf8') +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1);
}
else if (value['_bsontype'] === 'BSONRegExp') {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
buffer_1.Buffer.byteLength(value.pattern, 'utf8') +
1 +
buffer_1.Buffer.byteLength(value.options, 'utf8') +
1);
}
else {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
calculateObjectSize(value, serializeFunctions, ignoreUndefined) +
1);
}
case 'function':
// WTF for 0.4.X where typeof /someregexp/ === 'function'
if (value instanceof RegExp || (0, utils_1.isRegExp)(value) || String.call(value) === '[object RegExp]') {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
buffer_1.Buffer.byteLength(value.source, 'utf8') +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1);
}
else {
if (serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
4 +
buffer_1.Buffer.byteLength((0, utils_1.normalizedFunctionString)(value), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined));
}
else if (serializeFunctions) {
return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
buffer_1.Buffer.byteLength((0, utils_1.normalizedFunctionString)(value), 'utf8') +
1);
}
}
}
return 0;
}
//# sourceMappingURL=calculate_size.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,665 @@
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserialize = void 0;
var buffer_1 = require("buffer");
var binary_1 = require("../binary");
var code_1 = require("../code");
var constants = require("../constants");
var db_ref_1 = require("../db_ref");
var decimal128_1 = require("../decimal128");
var double_1 = require("../double");
var error_1 = require("../error");
var int_32_1 = require("../int_32");
var long_1 = require("../long");
var max_key_1 = require("../max_key");
var min_key_1 = require("../min_key");
var objectid_1 = require("../objectid");
var regexp_1 = require("../regexp");
var symbol_1 = require("../symbol");
var timestamp_1 = require("../timestamp");
var validate_utf8_1 = require("../validate_utf8");
// Internal long versions
var JS_INT_MAX_LONG = long_1.Long.fromNumber(constants.JS_INT_MAX);
var JS_INT_MIN_LONG = long_1.Long.fromNumber(constants.JS_INT_MIN);
var functionCache = {};
function deserialize(buffer, options, isArray) {
options = options == null ? {} : options;
var index = options && options.index ? options.index : 0;
// Read the document size
var size = buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
if (size < 5) {
throw new error_1.BSONError("bson size must be >= 5, is ".concat(size));
}
if (options.allowObjectSmallerThanBufferSize && buffer.length < size) {
throw new error_1.BSONError("buffer length ".concat(buffer.length, " must be >= bson size ").concat(size));
}
if (!options.allowObjectSmallerThanBufferSize && buffer.length !== size) {
throw new error_1.BSONError("buffer length ".concat(buffer.length, " must === bson size ").concat(size));
}
if (size + index > buffer.byteLength) {
throw new error_1.BSONError("(bson size ".concat(size, " + options.index ").concat(index, " must be <= buffer length ").concat(buffer.byteLength, ")"));
}
// Illegal end value
if (buffer[index + size - 1] !== 0) {
throw new error_1.BSONError("One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00");
}
// Start deserializtion
return deserializeObject(buffer, index, options, isArray);
}
exports.deserialize = deserialize;
var allowedDBRefKeys = /^\$ref$|^\$id$|^\$db$/;
function deserializeObject(buffer, index, options, isArray) {
if (isArray === void 0) { isArray = false; }
var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
var fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
// Return raw bson buffer instead of parsing it
var raw = options['raw'] == null ? false : options['raw'];
// Return BSONRegExp objects instead of native regular expressions
var bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
// Controls the promotion of values vs wrapper classes
var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
var promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
// Ensures default validation option if none given
var validation = options.validation == null ? { utf8: true } : options.validation;
// Shows if global utf-8 validation is enabled or disabled
var globalUTFValidation = true;
// Reflects utf-8 validation setting regardless of global or specific key validation
var validationSetting;
// Set of keys either to enable or disable validation on
var utf8KeysSet = new Set();
// Check for boolean uniformity and empty validation option
var utf8ValidatedKeys = validation.utf8;
if (typeof utf8ValidatedKeys === 'boolean') {
validationSetting = utf8ValidatedKeys;
}
else {
globalUTFValidation = false;
var utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
return utf8ValidatedKeys[key];
});
if (utf8ValidationValues.length === 0) {
throw new error_1.BSONError('UTF-8 validation setting cannot be empty');
}
if (typeof utf8ValidationValues[0] !== 'boolean') {
throw new error_1.BSONError('Invalid UTF-8 validation option, must specify boolean values');
}
validationSetting = utf8ValidationValues[0];
// Ensures boolean uniformity in utf-8 validation (all true or all false)
if (!utf8ValidationValues.every(function (item) { return item === validationSetting; })) {
throw new error_1.BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
}
}
// Add keys to set that will either be validated or not based on validationSetting
if (!globalUTFValidation) {
for (var _i = 0, _a = Object.keys(utf8ValidatedKeys); _i < _a.length; _i++) {
var key = _a[_i];
utf8KeysSet.add(key);
}
}
// Set the start index
var startIndex = index;
// Validate that we have at least 4 bytes of buffer
if (buffer.length < 5)
throw new error_1.BSONError('corrupt bson message < 5 bytes long');
// Read the document size
var size = buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Ensure buffer is valid size
if (size < 5 || size > buffer.length)
throw new error_1.BSONError('corrupt bson message');
// Create holding object
var object = isArray ? [] : {};
// Used for arrays to skip having to perform utf8 decoding
var arrayIndex = 0;
var done = false;
var isPossibleDBRef = isArray ? false : null;
// While we have more left data left keep parsing
var dataview = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
while (!done) {
// Read the type
var elementType = buffer[index++];
// If we get a zero it's the last byte, exit
if (elementType === 0)
break;
// Get the start search index
var i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.byteLength)
throw new error_1.BSONError('Bad BSON Document: illegal CString');
// Represents the key
var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
// shouldValidateKey is true if the key should be validated, false otherwise
var shouldValidateKey = true;
if (globalUTFValidation || utf8KeysSet.has(name)) {
shouldValidateKey = validationSetting;
}
else {
shouldValidateKey = !validationSetting;
}
if (isPossibleDBRef !== false && name[0] === '$') {
isPossibleDBRef = allowedDBRefKeys.test(name);
}
var value = void 0;
index = i + 1;
if (elementType === constants.BSON_DATA_STRING) {
var stringSize = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0) {
throw new error_1.BSONError('bad string length in bson');
}
value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
index = index + stringSize;
}
else if (elementType === constants.BSON_DATA_OID) {
var oid = buffer_1.Buffer.alloc(12);
buffer.copy(oid, 0, index, index + 12);
value = new objectid_1.ObjectId(oid);
index = index + 12;
}
else if (elementType === constants.BSON_DATA_INT && promoteValues === false) {
value = new int_32_1.Int32(buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24));
}
else if (elementType === constants.BSON_DATA_INT) {
value =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
}
else if (elementType === constants.BSON_DATA_NUMBER && promoteValues === false) {
value = new double_1.Double(dataview.getFloat64(index, true));
index = index + 8;
}
else if (elementType === constants.BSON_DATA_NUMBER) {
value = dataview.getFloat64(index, true);
index = index + 8;
}
else if (elementType === constants.BSON_DATA_DATE) {
var lowBits = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
var highBits = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
value = new Date(new long_1.Long(lowBits, highBits).toNumber());
}
else if (elementType === constants.BSON_DATA_BOOLEAN) {
if (buffer[index] !== 0 && buffer[index] !== 1)
throw new error_1.BSONError('illegal boolean type value');
value = buffer[index++] === 1;
}
else if (elementType === constants.BSON_DATA_OBJECT) {
var _index = index;
var objectSize = buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
if (objectSize <= 0 || objectSize > buffer.length - index)
throw new error_1.BSONError('bad embedded document length in bson');
// We have a raw value
if (raw) {
value = buffer.slice(index, index + objectSize);
}
else {
var objectOptions = options;
if (!globalUTFValidation) {
objectOptions = __assign(__assign({}, options), { validation: { utf8: shouldValidateKey } });
}
value = deserializeObject(buffer, _index, objectOptions, false);
}
index = index + objectSize;
}
else if (elementType === constants.BSON_DATA_ARRAY) {
var _index = index;
var objectSize = buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
var arrayOptions = options;
// Stop index
var stopIndex = index + objectSize;
// All elements of array to be returned as raw bson
if (fieldsAsRaw && fieldsAsRaw[name]) {
arrayOptions = {};
for (var n in options) {
arrayOptions[n] = options[n];
}
arrayOptions['raw'] = true;
}
if (!globalUTFValidation) {
arrayOptions = __assign(__assign({}, arrayOptions), { validation: { utf8: shouldValidateKey } });
}
value = deserializeObject(buffer, _index, arrayOptions, true);
index = index + objectSize;
if (buffer[index - 1] !== 0)
throw new error_1.BSONError('invalid array terminator byte');
if (index !== stopIndex)
throw new error_1.BSONError('corrupted array bson');
}
else if (elementType === constants.BSON_DATA_UNDEFINED) {
value = undefined;
}
else if (elementType === constants.BSON_DATA_NULL) {
value = null;
}
else if (elementType === constants.BSON_DATA_LONG) {
// Unpack the low and high bits
var lowBits = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
var highBits = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
var long = new long_1.Long(lowBits, highBits);
// Promote the long if possible
if (promoteLongs && promoteValues === true) {
value =
long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
? long.toNumber()
: long;
}
else {
value = long;
}
}
else if (elementType === constants.BSON_DATA_DECIMAL128) {
// Buffer to contain the decimal bytes
var bytes = buffer_1.Buffer.alloc(16);
// Copy the next 16 bytes into the bytes buffer
buffer.copy(bytes, 0, index, index + 16);
// Update index
index = index + 16;
// Assign the new Decimal128 value
var decimal128 = new decimal128_1.Decimal128(bytes);
// If we have an alternative mapper use that
if ('toObject' in decimal128 && typeof decimal128.toObject === 'function') {
value = decimal128.toObject();
}
else {
value = decimal128;
}
}
else if (elementType === constants.BSON_DATA_BINARY) {
var binarySize = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
var totalBinarySize = binarySize;
var subType = buffer[index++];
// Did we have a negative binary size, throw
if (binarySize < 0)
throw new error_1.BSONError('Negative binary type element size found');
// Is the length longer than the document
if (binarySize > buffer.byteLength)
throw new error_1.BSONError('Binary type size larger than document size');
// Decode as raw Buffer object if options specifies it
if (buffer['slice'] != null) {
// If we have subtype 2 skip the 4 bytes for the size
if (subType === binary_1.Binary.SUBTYPE_BYTE_ARRAY) {
binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (binarySize < 0)
throw new error_1.BSONError('Negative binary type element size found for subtype 0x02');
if (binarySize > totalBinarySize - 4)
throw new error_1.BSONError('Binary type with subtype 0x02 contains too long binary size');
if (binarySize < totalBinarySize - 4)
throw new error_1.BSONError('Binary type with subtype 0x02 contains too short binary size');
}
if (promoteBuffers && promoteValues) {
value = buffer.slice(index, index + binarySize);
}
else {
value = new binary_1.Binary(buffer.slice(index, index + binarySize), subType);
if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = value.toUUID();
}
}
}
else {
var _buffer = buffer_1.Buffer.alloc(binarySize);
// If we have subtype 2 skip the 4 bytes for the size
if (subType === binary_1.Binary.SUBTYPE_BYTE_ARRAY) {
binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (binarySize < 0)
throw new error_1.BSONError('Negative binary type element size found for subtype 0x02');
if (binarySize > totalBinarySize - 4)
throw new error_1.BSONError('Binary type with subtype 0x02 contains too long binary size');
if (binarySize < totalBinarySize - 4)
throw new error_1.BSONError('Binary type with subtype 0x02 contains too short binary size');
}
// Copy the data
for (i = 0; i < binarySize; i++) {
_buffer[i] = buffer[index + i];
}
if (promoteBuffers && promoteValues) {
value = _buffer;
}
else if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = new binary_1.Binary(buffer.slice(index, index + binarySize), subType).toUUID();
}
else {
value = new binary_1.Binary(buffer.slice(index, index + binarySize), subType);
}
}
// Update the index
index = index + binarySize;
}
else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === false) {
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length)
throw new error_1.BSONError('Bad BSON Document: illegal CString');
// Return the C string
var source = buffer.toString('utf8', index, i);
// Create the regexp
index = i + 1;
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length)
throw new error_1.BSONError('Bad BSON Document: illegal CString');
// Return the C string
var regExpOptions = buffer.toString('utf8', index, i);
index = i + 1;
// For each option add the corresponding one for javascript
var optionsArray = new Array(regExpOptions.length);
// Parse options
for (i = 0; i < regExpOptions.length; i++) {
switch (regExpOptions[i]) {
case 'm':
optionsArray[i] = 'm';
break;
case 's':
optionsArray[i] = 'g';
break;
case 'i':
optionsArray[i] = 'i';
break;
}
}
value = new RegExp(source, optionsArray.join(''));
}
else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === true) {
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length)
throw new error_1.BSONError('Bad BSON Document: illegal CString');
// Return the C string
var source = buffer.toString('utf8', index, i);
index = i + 1;
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length)
throw new error_1.BSONError('Bad BSON Document: illegal CString');
// Return the C string
var regExpOptions = buffer.toString('utf8', index, i);
index = i + 1;
// Set the object
value = new regexp_1.BSONRegExp(source, regExpOptions);
}
else if (elementType === constants.BSON_DATA_SYMBOL) {
var stringSize = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0) {
throw new error_1.BSONError('bad string length in bson');
}
var symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
value = promoteValues ? symbol : new symbol_1.BSONSymbol(symbol);
index = index + stringSize;
}
else if (elementType === constants.BSON_DATA_TIMESTAMP) {
var lowBits = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
var highBits = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
value = new timestamp_1.Timestamp(lowBits, highBits);
}
else if (elementType === constants.BSON_DATA_MIN_KEY) {
value = new min_key_1.MinKey();
}
else if (elementType === constants.BSON_DATA_MAX_KEY) {
value = new max_key_1.MaxKey();
}
else if (elementType === constants.BSON_DATA_CODE) {
var stringSize = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0) {
throw new error_1.BSONError('bad string length in bson');
}
var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
// If we are evaluating the functions
if (evalFunctions) {
// If we have cache enabled let's look for the md5 of the function in the cache
if (cacheFunctions) {
// Got to do this to avoid V8 deoptimizing the call due to finding eval
value = isolateEval(functionString, functionCache, object);
}
else {
value = isolateEval(functionString);
}
}
else {
value = new code_1.Code(functionString);
}
// Update parse index position
index = index + stringSize;
}
else if (elementType === constants.BSON_DATA_CODE_W_SCOPE) {
var totalSize = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Element cannot be shorter than totalSize + stringSize + documentSize + terminator
if (totalSize < 4 + 4 + 4 + 1) {
throw new error_1.BSONError('code_w_scope total size shorter minimum expected length');
}
// Get the code string size
var stringSize = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Check if we have a valid string
if (stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0) {
throw new error_1.BSONError('bad string length in bson');
}
// Javascript function
var functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
// Update parse index position
index = index + stringSize;
// Parse the element
var _index = index;
// Decode the size of the object document
var objectSize = buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
// Decode the scope object
var scopeObject = deserializeObject(buffer, _index, options, false);
// Adjust the index
index = index + objectSize;
// Check if field length is too short
if (totalSize < 4 + 4 + objectSize + stringSize) {
throw new error_1.BSONError('code_w_scope total size is too short, truncating scope');
}
// Check if totalSize field is too long
if (totalSize > 4 + 4 + objectSize + stringSize) {
throw new error_1.BSONError('code_w_scope total size is too long, clips outer document');
}
// If we are evaluating the functions
if (evalFunctions) {
// If we have cache enabled let's look for the md5 of the function in the cache
if (cacheFunctions) {
// Got to do this to avoid V8 deoptimizing the call due to finding eval
value = isolateEval(functionString, functionCache, object);
}
else {
value = isolateEval(functionString);
}
value.scope = scopeObject;
}
else {
value = new code_1.Code(functionString, scopeObject);
}
}
else if (elementType === constants.BSON_DATA_DBPOINTER) {
// Get the code string size
var stringSize = buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Check if we have a valid string
if (stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0)
throw new error_1.BSONError('bad string length in bson');
// Namespace
if (validation != null && validation.utf8) {
if (!(0, validate_utf8_1.validateUtf8)(buffer, index, index + stringSize - 1)) {
throw new error_1.BSONError('Invalid UTF-8 string in BSON document');
}
}
var namespace = buffer.toString('utf8', index, index + stringSize - 1);
// Update parse index position
index = index + stringSize;
// Read the oid
var oidBuffer = buffer_1.Buffer.alloc(12);
buffer.copy(oidBuffer, 0, index, index + 12);
var oid = new objectid_1.ObjectId(oidBuffer);
// Update the index
index = index + 12;
// Upgrade to DBRef type
value = new db_ref_1.DBRef(namespace, oid);
}
else {
throw new error_1.BSONError("Detected unknown BSON type ".concat(elementType.toString(16), " for fieldname \"").concat(name, "\""));
}
if (name === '__proto__') {
Object.defineProperty(object, name, {
value: value,
writable: true,
enumerable: true,
configurable: true
});
}
else {
object[name] = value;
}
}
// Check if the deserialization was against a valid array/object
if (size !== index - startIndex) {
if (isArray)
throw new error_1.BSONError('corrupt array bson');
throw new error_1.BSONError('corrupt object bson');
}
// if we did not find "$ref", "$id", "$db", or found an extraneous $key, don't make a DBRef
if (!isPossibleDBRef)
return object;
if ((0, db_ref_1.isDBRefLike)(object)) {
var copy = Object.assign({}, object);
delete copy.$ref;
delete copy.$id;
delete copy.$db;
return new db_ref_1.DBRef(object.$ref, object.$id, object.$db, copy);
}
return object;
}
/**
* Ensure eval is isolated, store the result in functionCache.
*
* @internal
*/
function isolateEval(functionString, functionCache, object) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
if (!functionCache)
return new Function(functionString);
// Check for cache hit, eval if missing and return cached function
if (functionCache[functionString] == null) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
functionCache[functionString] = new Function(functionString);
}
// Set the object
return functionCache[functionString].bind(object);
}
function getValidatedString(buffer, start, end, shouldValidateUtf8) {
var value = buffer.toString('utf8', start, end);
// if utf8 validation is on, do the check
if (shouldValidateUtf8) {
for (var i = 0; i < value.length; i++) {
if (value.charCodeAt(i) === 0xfffd) {
if (!(0, validate_utf8_1.validateUtf8)(buffer, start, end)) {
throw new error_1.BSONError('Invalid UTF-8 string in BSON document');
}
break;
}
}
}
return value;
}
//# sourceMappingURL=deserializer.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,867 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeInto = void 0;
var binary_1 = require("../binary");
var constants = require("../constants");
var ensure_buffer_1 = require("../ensure_buffer");
var error_1 = require("../error");
var extended_json_1 = require("../extended_json");
var long_1 = require("../long");
var map_1 = require("../map");
var utils_1 = require("./utils");
var regexp = /\x00/; // eslint-disable-line no-control-regex
var ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']);
/*
* isArray indicates if we are writing to a BSON array (type 0x04)
* which forces the "key" which really an array index as a string to be written as ascii
* This will catch any errors in index as a string generation
*/
function serializeString(buffer, key, value, index, isArray) {
// Encode String type
buffer[index++] = constants.BSON_DATA_STRING;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes + 1;
buffer[index - 1] = 0;
// Write the string
var size = buffer.write(value, index + 4, undefined, 'utf8');
// Write the size of the string to buffer
buffer[index + 3] = ((size + 1) >> 24) & 0xff;
buffer[index + 2] = ((size + 1) >> 16) & 0xff;
buffer[index + 1] = ((size + 1) >> 8) & 0xff;
buffer[index] = (size + 1) & 0xff;
// Update index
index = index + 4 + size;
// Write zero
buffer[index++] = 0;
return index;
}
var SPACE_FOR_FLOAT64 = new Uint8Array(8);
var DV_FOR_FLOAT64 = new DataView(SPACE_FOR_FLOAT64.buffer, SPACE_FOR_FLOAT64.byteOffset, SPACE_FOR_FLOAT64.byteLength);
function serializeNumber(buffer, key, value, index, isArray) {
// We have an integer value
// TODO(NODE-2529): Add support for big int
if (Number.isInteger(value) &&
value >= constants.BSON_INT32_MIN &&
value <= constants.BSON_INT32_MAX) {
// If the value fits in 32 bits encode as int32
// Set int type 32 bits or less
buffer[index++] = constants.BSON_DATA_INT;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the int value
buffer[index++] = value & 0xff;
buffer[index++] = (value >> 8) & 0xff;
buffer[index++] = (value >> 16) & 0xff;
buffer[index++] = (value >> 24) & 0xff;
}
else {
// Encode as double
buffer[index++] = constants.BSON_DATA_NUMBER;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write float
DV_FOR_FLOAT64.setFloat64(0, value, true);
buffer.set(SPACE_FOR_FLOAT64, index);
// Adjust index
index = index + 8;
}
return index;
}
function serializeNull(buffer, key, _, index, isArray) {
// Set long type
buffer[index++] = constants.BSON_DATA_NULL;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
return index;
}
function serializeBoolean(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_BOOLEAN;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Encode the boolean value
buffer[index++] = value ? 1 : 0;
return index;
}
function serializeDate(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_DATE;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the date
var dateInMilis = long_1.Long.fromNumber(value.getTime());
var lowBits = dateInMilis.getLowBits();
var highBits = dateInMilis.getHighBits();
// Encode low bits
buffer[index++] = lowBits & 0xff;
buffer[index++] = (lowBits >> 8) & 0xff;
buffer[index++] = (lowBits >> 16) & 0xff;
buffer[index++] = (lowBits >> 24) & 0xff;
// Encode high bits
buffer[index++] = highBits & 0xff;
buffer[index++] = (highBits >> 8) & 0xff;
buffer[index++] = (highBits >> 16) & 0xff;
buffer[index++] = (highBits >> 24) & 0xff;
return index;
}
function serializeRegExp(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_REGEXP;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
if (value.source && value.source.match(regexp) != null) {
throw Error('value ' + value.source + ' must not contain null bytes');
}
// Adjust the index
index = index + buffer.write(value.source, index, undefined, 'utf8');
// Write zero
buffer[index++] = 0x00;
// Write the parameters
if (value.ignoreCase)
buffer[index++] = 0x69; // i
if (value.global)
buffer[index++] = 0x73; // s
if (value.multiline)
buffer[index++] = 0x6d; // m
// Add ending zero
buffer[index++] = 0x00;
return index;
}
function serializeBSONRegExp(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_REGEXP;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Check the pattern for 0 bytes
if (value.pattern.match(regexp) != null) {
// The BSON spec doesn't allow keys with null bytes because keys are
// null-terminated.
throw Error('pattern ' + value.pattern + ' must not contain null bytes');
}
// Adjust the index
index = index + buffer.write(value.pattern, index, undefined, 'utf8');
// Write zero
buffer[index++] = 0x00;
// Write the options
index = index + buffer.write(value.options.split('').sort().join(''), index, undefined, 'utf8');
// Add ending zero
buffer[index++] = 0x00;
return index;
}
function serializeMinMax(buffer, key, value, index, isArray) {
// Write the type of either min or max key
if (value === null) {
buffer[index++] = constants.BSON_DATA_NULL;
}
else if (value._bsontype === 'MinKey') {
buffer[index++] = constants.BSON_DATA_MIN_KEY;
}
else {
buffer[index++] = constants.BSON_DATA_MAX_KEY;
}
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
return index;
}
function serializeObjectId(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_OID;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the objectId into the shared buffer
if (typeof value.id === 'string') {
buffer.write(value.id, index, undefined, 'binary');
}
else if ((0, utils_1.isUint8Array)(value.id)) {
// Use the standard JS methods here because buffer.copy() is buggy with the
// browser polyfill
buffer.set(value.id.subarray(0, 12), index);
}
else {
throw new error_1.BSONTypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
}
// Adjust index
return index + 12;
}
function serializeBuffer(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_BINARY;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Get size of the buffer (current write point)
var size = value.length;
// Write the size of the string to buffer
buffer[index++] = size & 0xff;
buffer[index++] = (size >> 8) & 0xff;
buffer[index++] = (size >> 16) & 0xff;
buffer[index++] = (size >> 24) & 0xff;
// Write the default subtype
buffer[index++] = constants.BSON_BINARY_SUBTYPE_DEFAULT;
// Copy the content form the binary field to the buffer
buffer.set((0, ensure_buffer_1.ensureBuffer)(value), index);
// Adjust the index
index = index + size;
return index;
}
function serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, isArray, path) {
if (checkKeys === void 0) { checkKeys = false; }
if (depth === void 0) { depth = 0; }
if (serializeFunctions === void 0) { serializeFunctions = false; }
if (ignoreUndefined === void 0) { ignoreUndefined = true; }
if (isArray === void 0) { isArray = false; }
if (path === void 0) { path = []; }
for (var i = 0; i < path.length; i++) {
if (path[i] === value)
throw new error_1.BSONError('cyclic dependency detected');
}
// Push value to stack
path.push(value);
// Write the type
buffer[index++] = Array.isArray(value) ? constants.BSON_DATA_ARRAY : constants.BSON_DATA_OBJECT;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
var endIndex = serializeInto(buffer, value, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined, path);
// Pop stack
path.pop();
return endIndex;
}
function serializeDecimal128(buffer, key, value, index, isArray) {
buffer[index++] = constants.BSON_DATA_DECIMAL128;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the data from the value
// Prefer the standard JS methods because their typechecking is not buggy,
// unlike the `buffer` polyfill's.
buffer.set(value.bytes.subarray(0, 16), index);
return index + 16;
}
function serializeLong(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] =
value._bsontype === 'Long' ? constants.BSON_DATA_LONG : constants.BSON_DATA_TIMESTAMP;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the date
var lowBits = value.getLowBits();
var highBits = value.getHighBits();
// Encode low bits
buffer[index++] = lowBits & 0xff;
buffer[index++] = (lowBits >> 8) & 0xff;
buffer[index++] = (lowBits >> 16) & 0xff;
buffer[index++] = (lowBits >> 24) & 0xff;
// Encode high bits
buffer[index++] = highBits & 0xff;
buffer[index++] = (highBits >> 8) & 0xff;
buffer[index++] = (highBits >> 16) & 0xff;
buffer[index++] = (highBits >> 24) & 0xff;
return index;
}
function serializeInt32(buffer, key, value, index, isArray) {
value = value.valueOf();
// Set int type 32 bits or less
buffer[index++] = constants.BSON_DATA_INT;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the int value
buffer[index++] = value & 0xff;
buffer[index++] = (value >> 8) & 0xff;
buffer[index++] = (value >> 16) & 0xff;
buffer[index++] = (value >> 24) & 0xff;
return index;
}
function serializeDouble(buffer, key, value, index, isArray) {
// Encode as double
buffer[index++] = constants.BSON_DATA_NUMBER;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write float
DV_FOR_FLOAT64.setFloat64(0, value.value, true);
buffer.set(SPACE_FOR_FLOAT64, index);
// Adjust index
index = index + 8;
return index;
}
function serializeFunction(buffer, key, value, index, _checkKeys, _depth, isArray) {
if (_checkKeys === void 0) { _checkKeys = false; }
if (_depth === void 0) { _depth = 0; }
buffer[index++] = constants.BSON_DATA_CODE;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Function string
var functionString = (0, utils_1.normalizedFunctionString)(value);
// Write the string
var size = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
// Write the size of the string to buffer
buffer[index] = size & 0xff;
buffer[index + 1] = (size >> 8) & 0xff;
buffer[index + 2] = (size >> 16) & 0xff;
buffer[index + 3] = (size >> 24) & 0xff;
// Update index
index = index + 4 + size - 1;
// Write zero
buffer[index++] = 0;
return index;
}
function serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, isArray) {
if (checkKeys === void 0) { checkKeys = false; }
if (depth === void 0) { depth = 0; }
if (serializeFunctions === void 0) { serializeFunctions = false; }
if (ignoreUndefined === void 0) { ignoreUndefined = true; }
if (isArray === void 0) { isArray = false; }
if (value.scope && typeof value.scope === 'object') {
// Write the type
buffer[index++] = constants.BSON_DATA_CODE_W_SCOPE;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Starting index
var startIndex = index;
// Serialize the function
// Get the function string
var functionString = typeof value.code === 'string' ? value.code : value.code.toString();
// Index adjustment
index = index + 4;
// Write string into buffer
var codeSize = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
// Write the size of the string to buffer
buffer[index] = codeSize & 0xff;
buffer[index + 1] = (codeSize >> 8) & 0xff;
buffer[index + 2] = (codeSize >> 16) & 0xff;
buffer[index + 3] = (codeSize >> 24) & 0xff;
// Write end 0
buffer[index + 4 + codeSize - 1] = 0;
// Write the
index = index + codeSize + 4;
//
// Serialize the scope value
var endIndex = serializeInto(buffer, value.scope, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined);
index = endIndex - 1;
// Writ the total
var totalSize = endIndex - startIndex;
// Write the total size of the object
buffer[startIndex++] = totalSize & 0xff;
buffer[startIndex++] = (totalSize >> 8) & 0xff;
buffer[startIndex++] = (totalSize >> 16) & 0xff;
buffer[startIndex++] = (totalSize >> 24) & 0xff;
// Write trailing zero
buffer[index++] = 0;
}
else {
buffer[index++] = constants.BSON_DATA_CODE;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Function string
var functionString = value.code.toString();
// Write the string
var size = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
// Write the size of the string to buffer
buffer[index] = size & 0xff;
buffer[index + 1] = (size >> 8) & 0xff;
buffer[index + 2] = (size >> 16) & 0xff;
buffer[index + 3] = (size >> 24) & 0xff;
// Update index
index = index + 4 + size - 1;
// Write zero
buffer[index++] = 0;
}
return index;
}
function serializeBinary(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_BINARY;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Extract the buffer
var data = value.value(true);
// Calculate size
var size = value.position;
// Add the deprecated 02 type 4 bytes of size to total
if (value.sub_type === binary_1.Binary.SUBTYPE_BYTE_ARRAY)
size = size + 4;
// Write the size of the string to buffer
buffer[index++] = size & 0xff;
buffer[index++] = (size >> 8) & 0xff;
buffer[index++] = (size >> 16) & 0xff;
buffer[index++] = (size >> 24) & 0xff;
// Write the subtype to the buffer
buffer[index++] = value.sub_type;
// If we have binary type 2 the 4 first bytes are the size
if (value.sub_type === binary_1.Binary.SUBTYPE_BYTE_ARRAY) {
size = size - 4;
buffer[index++] = size & 0xff;
buffer[index++] = (size >> 8) & 0xff;
buffer[index++] = (size >> 16) & 0xff;
buffer[index++] = (size >> 24) & 0xff;
}
// Write the data to the object
buffer.set(data, index);
// Adjust the index
index = index + value.position;
return index;
}
function serializeSymbol(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_SYMBOL;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the string
var size = buffer.write(value.value, index + 4, undefined, 'utf8') + 1;
// Write the size of the string to buffer
buffer[index] = size & 0xff;
buffer[index + 1] = (size >> 8) & 0xff;
buffer[index + 2] = (size >> 16) & 0xff;
buffer[index + 3] = (size >> 24) & 0xff;
// Update index
index = index + 4 + size - 1;
// Write zero
buffer[index++] = 0x00;
return index;
}
function serializeDBRef(buffer, key, value, index, depth, serializeFunctions, isArray) {
// Write the type
buffer[index++] = constants.BSON_DATA_OBJECT;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, undefined, 'utf8')
: buffer.write(key, index, undefined, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
var startIndex = index;
var output = {
$ref: value.collection || value.namespace,
$id: value.oid
};
if (value.db != null) {
output.$db = value.db;
}
output = Object.assign(output, value.fields);
var endIndex = serializeInto(buffer, output, false, index, depth + 1, serializeFunctions);
// Calculate object size
var size = endIndex - startIndex;
// Write the size
buffer[startIndex++] = size & 0xff;
buffer[startIndex++] = (size >> 8) & 0xff;
buffer[startIndex++] = (size >> 16) & 0xff;
buffer[startIndex++] = (size >> 24) & 0xff;
// Set index
return endIndex;
}
function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializeFunctions, ignoreUndefined, path) {
if (checkKeys === void 0) { checkKeys = false; }
if (startingIndex === void 0) { startingIndex = 0; }
if (depth === void 0) { depth = 0; }
if (serializeFunctions === void 0) { serializeFunctions = false; }
if (ignoreUndefined === void 0) { ignoreUndefined = true; }
if (path === void 0) { path = []; }
startingIndex = startingIndex || 0;
path = path || [];
// Push the object to the path
path.push(object);
// Start place to serialize into
var index = startingIndex + 4;
// Special case isArray
if (Array.isArray(object)) {
// Get object keys
for (var i = 0; i < object.length; i++) {
var key = "".concat(i);
var value = object[i];
// Is there an override value
if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
value = value.toBSON();
}
if (typeof value === 'string') {
index = serializeString(buffer, key, value, index, true);
}
else if (typeof value === 'number') {
index = serializeNumber(buffer, key, value, index, true);
}
else if (typeof value === 'bigint') {
throw new error_1.BSONTypeError('Unsupported type BigInt, please use Decimal128');
}
else if (typeof value === 'boolean') {
index = serializeBoolean(buffer, key, value, index, true);
}
else if (value instanceof Date || (0, utils_1.isDate)(value)) {
index = serializeDate(buffer, key, value, index, true);
}
else if (value === undefined) {
index = serializeNull(buffer, key, value, index, true);
}
else if (value === null) {
index = serializeNull(buffer, key, value, index, true);
}
else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
index = serializeObjectId(buffer, key, value, index, true);
}
else if ((0, utils_1.isUint8Array)(value)) {
index = serializeBuffer(buffer, key, value, index, true);
}
else if (value instanceof RegExp || (0, utils_1.isRegExp)(value)) {
index = serializeRegExp(buffer, key, value, index, true);
}
else if (typeof value === 'object' && value['_bsontype'] == null) {
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, true, path);
}
else if (typeof value === 'object' &&
(0, extended_json_1.isBSONType)(value) &&
value._bsontype === 'Decimal128') {
index = serializeDecimal128(buffer, key, value, index, true);
}
else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
index = serializeLong(buffer, key, value, index, true);
}
else if (value['_bsontype'] === 'Double') {
index = serializeDouble(buffer, key, value, index, true);
}
else if (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(buffer, key, value, index, checkKeys, depth, true);
}
else if (value['_bsontype'] === 'Code') {
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, true);
}
else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index, true);
}
else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index, true);
}
else if (value['_bsontype'] === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, true);
}
else if (value['_bsontype'] === 'BSONRegExp') {
index = serializeBSONRegExp(buffer, key, value, index, true);
}
else if (value['_bsontype'] === 'Int32') {
index = serializeInt32(buffer, key, value, index, true);
}
else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index, true);
}
else if (typeof value['_bsontype'] !== 'undefined') {
throw new error_1.BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
}
}
}
else if (object instanceof map_1.Map || (0, utils_1.isMap)(object)) {
var iterator = object.entries();
var done = false;
while (!done) {
// Unpack the next entry
var entry = iterator.next();
done = !!entry.done;
// Are we done, then skip and terminate
if (done)
continue;
// Get the entry values
var key = entry.value[0];
var value = entry.value[1];
// Check the type of the value
var type = typeof value;
// Check the key and throw error if it's illegal
if (typeof key === 'string' && !ignoreKeys.has(key)) {
if (key.match(regexp) != null) {
// The BSON spec doesn't allow keys with null bytes because keys are
// null-terminated.
throw Error('key ' + key + ' must not contain null bytes');
}
if (checkKeys) {
if ('$' === key[0]) {
throw Error('key ' + key + " must not start with '$'");
}
else if (~key.indexOf('.')) {
throw Error('key ' + key + " must not contain '.'");
}
}
}
if (type === 'string') {
index = serializeString(buffer, key, value, index);
}
else if (type === 'number') {
index = serializeNumber(buffer, key, value, index);
}
else if (type === 'bigint' || (0, utils_1.isBigInt64Array)(value) || (0, utils_1.isBigUInt64Array)(value)) {
throw new error_1.BSONTypeError('Unsupported type BigInt, please use Decimal128');
}
else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
}
else if (value instanceof Date || (0, utils_1.isDate)(value)) {
index = serializeDate(buffer, key, value, index);
}
else if (value === null || (value === undefined && ignoreUndefined === false)) {
index = serializeNull(buffer, key, value, index);
}
else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
index = serializeObjectId(buffer, key, value, index);
}
else if ((0, utils_1.isUint8Array)(value)) {
index = serializeBuffer(buffer, key, value, index);
}
else if (value instanceof RegExp || (0, utils_1.isRegExp)(value)) {
index = serializeRegExp(buffer, key, value, index);
}
else if (type === 'object' && value['_bsontype'] == null) {
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, false, path);
}
else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
index = serializeDecimal128(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
index = serializeLong(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Double') {
index = serializeDouble(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Code') {
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
}
else if (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
}
else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index);
}
else if (value['_bsontype'] === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
}
else if (value['_bsontype'] === 'BSONRegExp') {
index = serializeBSONRegExp(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Int32') {
index = serializeInt32(buffer, key, value, index);
}
else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index);
}
else if (typeof value['_bsontype'] !== 'undefined') {
throw new error_1.BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
}
}
}
else {
if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
// Provided a custom serialization method
object = object.toBSON();
if (object != null && typeof object !== 'object') {
throw new error_1.BSONTypeError('toBSON function did not return an object');
}
}
// Iterate over all the keys
for (var key in object) {
var value = object[key];
// Is there an override value
if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
value = value.toBSON();
}
// Check the type of the value
var type = typeof value;
// Check the key and throw error if it's illegal
if (typeof key === 'string' && !ignoreKeys.has(key)) {
if (key.match(regexp) != null) {
// The BSON spec doesn't allow keys with null bytes because keys are
// null-terminated.
throw Error('key ' + key + ' must not contain null bytes');
}
if (checkKeys) {
if ('$' === key[0]) {
throw Error('key ' + key + " must not start with '$'");
}
else if (~key.indexOf('.')) {
throw Error('key ' + key + " must not contain '.'");
}
}
}
if (type === 'string') {
index = serializeString(buffer, key, value, index);
}
else if (type === 'number') {
index = serializeNumber(buffer, key, value, index);
}
else if (type === 'bigint') {
throw new error_1.BSONTypeError('Unsupported type BigInt, please use Decimal128');
}
else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
}
else if (value instanceof Date || (0, utils_1.isDate)(value)) {
index = serializeDate(buffer, key, value, index);
}
else if (value === undefined) {
if (ignoreUndefined === false)
index = serializeNull(buffer, key, value, index);
}
else if (value === null) {
index = serializeNull(buffer, key, value, index);
}
else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
index = serializeObjectId(buffer, key, value, index);
}
else if ((0, utils_1.isUint8Array)(value)) {
index = serializeBuffer(buffer, key, value, index);
}
else if (value instanceof RegExp || (0, utils_1.isRegExp)(value)) {
index = serializeRegExp(buffer, key, value, index);
}
else if (type === 'object' && value['_bsontype'] == null) {
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, false, path);
}
else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
index = serializeDecimal128(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
index = serializeLong(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Double') {
index = serializeDouble(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Code') {
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
}
else if (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
}
else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index);
}
else if (value['_bsontype'] === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
}
else if (value['_bsontype'] === 'BSONRegExp') {
index = serializeBSONRegExp(buffer, key, value, index);
}
else if (value['_bsontype'] === 'Int32') {
index = serializeInt32(buffer, key, value, index);
}
else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index);
}
else if (typeof value['_bsontype'] !== 'undefined') {
throw new error_1.BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
}
}
}
// Remove the path
path.pop();
// Final padding byte for object
buffer[index++] = 0x00;
// Final size
var size = index - startingIndex;
// Write the size of the object
buffer[startingIndex++] = size & 0xff;
buffer[startingIndex++] = (size >> 8) & 0xff;
buffer[startingIndex++] = (size >> 16) & 0xff;
buffer[startingIndex++] = (size >> 24) & 0xff;
return index;
}
exports.serializeInto = serializeInto;
//# sourceMappingURL=serializer.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,115 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deprecate = exports.isObjectLike = exports.isDate = exports.haveBuffer = exports.isMap = exports.isRegExp = exports.isBigUInt64Array = exports.isBigInt64Array = exports.isUint8Array = exports.isAnyArrayBuffer = exports.randomBytes = exports.normalizedFunctionString = void 0;
var buffer_1 = require("buffer");
var global_1 = require("../utils/global");
/**
* Normalizes our expected stringified form of a function across versions of node
* @param fn - The function to stringify
*/
function normalizedFunctionString(fn) {
return fn.toString().replace('function(', 'function (');
}
exports.normalizedFunctionString = normalizedFunctionString;
function isReactNative() {
var g = (0, global_1.getGlobal)();
return typeof g.navigator === 'object' && g.navigator.product === 'ReactNative';
}
var insecureRandomBytes = function insecureRandomBytes(size) {
var insecureWarning = isReactNative()
? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
: 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.';
console.warn(insecureWarning);
var result = buffer_1.Buffer.alloc(size);
for (var i = 0; i < size; ++i)
result[i] = Math.floor(Math.random() * 256);
return result;
};
var detectRandomBytes = function () {
if (process.browser) {
if (typeof window !== 'undefined') {
// browser crypto implementation(s)
var target_1 = window.crypto || window.msCrypto; // allow for IE11
if (target_1 && target_1.getRandomValues) {
return function (size) { return target_1.getRandomValues(buffer_1.Buffer.alloc(size)); };
}
}
if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
// allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
return function (size) { return global.crypto.getRandomValues(buffer_1.Buffer.alloc(size)); };
}
return insecureRandomBytes;
}
else {
var requiredRandomBytes = void 0;
try {
requiredRandomBytes = require('crypto').randomBytes;
}
catch (e) {
// keep the fallback
}
// NOTE: in transpiled cases the above require might return null/undefined
return requiredRandomBytes || insecureRandomBytes;
}
};
exports.randomBytes = detectRandomBytes();
function isAnyArrayBuffer(value) {
return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
}
exports.isAnyArrayBuffer = isAnyArrayBuffer;
function isUint8Array(value) {
return Object.prototype.toString.call(value) === '[object Uint8Array]';
}
exports.isUint8Array = isUint8Array;
function isBigInt64Array(value) {
return Object.prototype.toString.call(value) === '[object BigInt64Array]';
}
exports.isBigInt64Array = isBigInt64Array;
function isBigUInt64Array(value) {
return Object.prototype.toString.call(value) === '[object BigUint64Array]';
}
exports.isBigUInt64Array = isBigUInt64Array;
function isRegExp(d) {
return Object.prototype.toString.call(d) === '[object RegExp]';
}
exports.isRegExp = isRegExp;
function isMap(d) {
return Object.prototype.toString.call(d) === '[object Map]';
}
exports.isMap = isMap;
/** Call to check if your environment has `Buffer` */
function haveBuffer() {
return typeof global !== 'undefined' && typeof global.Buffer !== 'undefined';
}
exports.haveBuffer = haveBuffer;
// To ensure that 0.4 of node works correctly
function isDate(d) {
return isObjectLike(d) && Object.prototype.toString.call(d) === '[object Date]';
}
exports.isDate = isDate;
/**
* @internal
* this is to solve the `'someKey' in x` problem where x is unknown.
* https://github.com/typescript-eslint/typescript-eslint/issues/1071#issuecomment-541955753
*/
function isObjectLike(candidate) {
return typeof candidate === 'object' && candidate !== null;
}
exports.isObjectLike = isObjectLike;
function deprecate(fn, message) {
var warned = false;
function deprecated() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (!warned) {
console.warn(message);
warned = true;
}
return fn.apply(this, args);
}
return deprecated;
}
exports.deprecate = deprecate;
//# sourceMappingURL=utils.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/parser/utils.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,0CAA4C;AAI5C;;;GAGG;AACH,SAAgB,wBAAwB,CAAC,EAAY;IACnD,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;AAC1D,CAAC;AAFD,4DAEC;AAED,SAAS,aAAa;IACpB,IAAM,CAAC,GAAG,IAAA,kBAAS,GAAwC,CAAC;IAC5D,OAAO,OAAO,CAAC,CAAC,SAAS,KAAK,QAAQ,IAAI,CAAC,CAAC,SAAS,CAAC,OAAO,KAAK,aAAa,CAAC;AAClF,CAAC;AAED,IAAM,mBAAmB,GAAwB,SAAS,mBAAmB,CAAC,IAAY;IACxF,IAAM,eAAe,GAAG,aAAa,EAAE;QACrC,CAAC,CAAC,0IAA0I;QAC5I,CAAC,CAAC,+GAA+G,CAAC;IACpH,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IAE9B,IAAM,MAAM,GAAG,eAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC;QAAE,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC;IAC3E,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAWF,IAAM,iBAAiB,GAAG;IACxB,IAAI,OAAO,CAAC,OAAO,EAAE;QACnB,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;YACjC,mCAAmC;YACnC,IAAM,QAAM,GAAG,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,iBAAiB;YAClE,IAAI,QAAM,IAAI,QAAM,CAAC,eAAe,EAAE;gBACpC,OAAO,UAAA,IAAI,IAAI,OAAA,QAAM,CAAC,eAAe,CAAC,eAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,EAA1C,CAA0C,CAAC;aAC3D;SACF;QAED,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,eAAe,EAAE;YACnF,gHAAgH;YAChH,OAAO,UAAA,IAAI,IAAI,OAAA,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,eAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,EAAjD,CAAiD,CAAC;SAClE;QAED,OAAO,mBAAmB,CAAC;KAC5B;SAAM;QACL,IAAI,mBAAmB,SAAwC,CAAC;QAChE,IAAI;YACF,mBAAmB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,WAAW,CAAC;SACrD;QAAC,OAAO,CAAC,EAAE;YACV,oBAAoB;SACrB;QAED,0EAA0E;QAE1E,OAAO,mBAAmB,IAAI,mBAAmB,CAAC;KACnD;AACH,CAAC,CAAC;AAEW,QAAA,WAAW,GAAG,iBAAiB,EAAE,CAAC;AAE/C,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,OAAO,CAAC,sBAAsB,EAAE,4BAA4B,CAAC,CAAC,QAAQ,CACpE,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CACtC,CAAC;AACJ,CAAC;AAJD,4CAIC;AAED,SAAgB,YAAY,CAAC,KAAc;IACzC,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,qBAAqB,CAAC;AACzE,CAAC;AAFD,oCAEC;AAED,SAAgB,eAAe,CAAC,KAAc;IAC5C,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,wBAAwB,CAAC;AAC5E,CAAC;AAFD,0CAEC;AAED,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,yBAAyB,CAAC;AAC7E,CAAC;AAFD,4CAEC;AAED,SAAgB,QAAQ,CAAC,CAAU;IACjC,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,iBAAiB,CAAC;AACjE,CAAC;AAFD,4BAEC;AAED,SAAgB,KAAK,CAAC,CAAU;IAC9B,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,cAAc,CAAC;AAC9D,CAAC;AAFD,sBAEC;AAED,qDAAqD;AACrD,SAAgB,UAAU;IACxB,OAAO,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,MAAM,KAAK,WAAW,CAAC;AAC/E,CAAC;AAFD,gCAEC;AAED,6CAA6C;AAC7C,SAAgB,MAAM,CAAC,CAAU;IAC/B,OAAO,YAAY,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,eAAe,CAAC;AAClF,CAAC;AAFD,wBAEC;AAED;;;;GAIG;AACH,SAAgB,YAAY,CAAC,SAAkB;IAC7C,OAAO,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,IAAI,CAAC;AAC7D,CAAC;AAFD,oCAEC;AAGD,SAAgB,SAAS,CAAqB,EAAK,EAAE,OAAe;IAClE,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,SAAS,UAAU;QAAgB,cAAkB;aAAlB,UAAkB,EAAlB,qBAAkB,EAAlB,IAAkB;YAAlB,yBAAkB;;QACnD,IAAI,CAAC,MAAM,EAAE;YACX,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACtB,MAAM,GAAG,IAAI,CAAC;SACf;QACD,OAAO,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC9B,CAAC;IACD,OAAO,UAA0B,CAAC;AACpC,CAAC;AAVD,8BAUC"}

View File

@@ -0,0 +1,74 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BSONRegExp = void 0;
var error_1 = require("./error");
function alphabetize(str) {
return str.split('').sort().join('');
}
/**
* A class representation of the BSON RegExp type.
* @public
* @category BSONType
*/
var BSONRegExp = /** @class */ (function () {
/**
* @param pattern - The regular expression pattern to match
* @param options - The regular expression options
*/
function BSONRegExp(pattern, options) {
if (!(this instanceof BSONRegExp))
return new BSONRegExp(pattern, options);
this.pattern = pattern;
this.options = alphabetize(options !== null && options !== void 0 ? options : '');
if (this.pattern.indexOf('\x00') !== -1) {
throw new error_1.BSONError("BSON Regex patterns cannot contain null bytes, found: ".concat(JSON.stringify(this.pattern)));
}
if (this.options.indexOf('\x00') !== -1) {
throw new error_1.BSONError("BSON Regex options cannot contain null bytes, found: ".concat(JSON.stringify(this.options)));
}
// Validate options
for (var i = 0; i < this.options.length; i++) {
if (!(this.options[i] === 'i' ||
this.options[i] === 'm' ||
this.options[i] === 'x' ||
this.options[i] === 'l' ||
this.options[i] === 's' ||
this.options[i] === 'u')) {
throw new error_1.BSONError("The regular expression option [".concat(this.options[i], "] is not supported"));
}
}
}
BSONRegExp.parseOptions = function (options) {
return options ? options.split('').sort().join('') : '';
};
/** @internal */
BSONRegExp.prototype.toExtendedJSON = function (options) {
options = options || {};
if (options.legacy) {
return { $regex: this.pattern, $options: this.options };
}
return { $regularExpression: { pattern: this.pattern, options: this.options } };
};
/** @internal */
BSONRegExp.fromExtendedJSON = function (doc) {
if ('$regex' in doc) {
if (typeof doc.$regex !== 'string') {
// This is for $regex query operators that have extended json values.
if (doc.$regex._bsontype === 'BSONRegExp') {
return doc;
}
}
else {
return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
}
}
if ('$regularExpression' in doc) {
return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
}
throw new error_1.BSONTypeError("Unexpected BSONRegExp EJSON object form: ".concat(JSON.stringify(doc)));
};
return BSONRegExp;
}());
exports.BSONRegExp = BSONRegExp;
Object.defineProperty(BSONRegExp.prototype, '_bsontype', { value: 'BSONRegExp' });
//# sourceMappingURL=regexp.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"regexp.js","sourceRoot":"","sources":["../src/regexp.ts"],"names":[],"mappings":";;;AAAA,iCAAmD;AAGnD,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACvC,CAAC;AAgBD;;;;GAIG;AACH;IAKE;;;OAGG;IACH,oBAAY,OAAe,EAAE,OAAgB;QAC3C,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC;YAAE,OAAO,IAAI,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAE3E,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC,CAAC;QAE1C,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE;YACvC,MAAM,IAAI,iBAAS,CACjB,gEAAyD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAE,CACxF,CAAC;SACH;QACD,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE;YACvC,MAAM,IAAI,iBAAS,CACjB,+DAAwD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAE,CACvF,CAAC;SACH;QAED,mBAAmB;QACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAC5C,IACE,CAAC,CACC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG;gBACvB,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG;gBACvB,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG;gBACvB,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG;gBACvB,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG;gBACvB,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,CACxB,EACD;gBACA,MAAM,IAAI,iBAAS,CAAC,yCAAkC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,uBAAoB,CAAC,CAAC;aAC5F;SACF;IACH,CAAC;IAEM,uBAAY,GAAnB,UAAoB,OAAgB;QAClC,OAAO,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC1D,CAAC;IAED,gBAAgB;IAChB,mCAAc,GAAd,UAAe,OAAsB;QACnC,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;QACxB,IAAI,OAAO,CAAC,MAAM,EAAE;YAClB,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC;SACzD;QACD,OAAO,EAAE,kBAAkB,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;IAClF,CAAC;IAED,gBAAgB;IACT,2BAAgB,GAAvB,UAAwB,GAAkD;QACxE,IAAI,QAAQ,IAAI,GAAG,EAAE;YACnB,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,QAAQ,EAAE;gBAClC,qEAAqE;gBACrE,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,KAAK,YAAY,EAAE;oBACzC,OAAO,GAA4B,CAAC;iBACrC;aACF;iBAAM;gBACL,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;aAC1E;SACF;QACD,IAAI,oBAAoB,IAAI,GAAG,EAAE;YAC/B,OAAO,IAAI,UAAU,CACnB,GAAG,CAAC,kBAAkB,CAAC,OAAO,EAC9B,UAAU,CAAC,YAAY,CAAC,GAAG,CAAC,kBAAkB,CAAC,OAAO,CAAC,CACxD,CAAC;SACH;QACD,MAAM,IAAI,qBAAa,CAAC,mDAA4C,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAE,CAAC,CAAC;IAC7F,CAAC;IACH,iBAAC;AAAD,CAAC,AA5ED,IA4EC;AA5EY,gCAAU;AA8EvB,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,CAAC"}

View File

@@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BSONSymbol = void 0;
/**
* A class representation of the BSON Symbol type.
* @public
* @category BSONType
*/
var BSONSymbol = /** @class */ (function () {
/**
* @param value - the string representing the symbol.
*/
function BSONSymbol(value) {
if (!(this instanceof BSONSymbol))
return new BSONSymbol(value);
this.value = value;
}
/** Access the wrapped string value. */
BSONSymbol.prototype.valueOf = function () {
return this.value;
};
BSONSymbol.prototype.toString = function () {
return this.value;
};
/** @internal */
BSONSymbol.prototype.inspect = function () {
return "new BSONSymbol(\"".concat(this.value, "\")");
};
BSONSymbol.prototype.toJSON = function () {
return this.value;
};
/** @internal */
BSONSymbol.prototype.toExtendedJSON = function () {
return { $symbol: this.value };
};
/** @internal */
BSONSymbol.fromExtendedJSON = function (doc) {
return new BSONSymbol(doc.$symbol);
};
/** @internal */
BSONSymbol.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
return BSONSymbol;
}());
exports.BSONSymbol = BSONSymbol;
Object.defineProperty(BSONSymbol.prototype, '_bsontype', { value: 'Symbol' });
//# sourceMappingURL=symbol.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"symbol.js","sourceRoot":"","sources":["../src/symbol.ts"],"names":[],"mappings":";;;AAKA;;;;GAIG;AACH;IAIE;;OAEG;IACH,oBAAY,KAAa;QACvB,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC;YAAE,OAAO,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;QAEhE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,uCAAuC;IACvC,4BAAO,GAAP;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,6BAAQ,GAAR;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,gBAAgB;IAChB,4BAAO,GAAP;QACE,OAAO,2BAAmB,IAAI,CAAC,KAAK,QAAI,CAAC;IAC3C,CAAC;IAED,2BAAM,GAAN;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,gBAAgB;IAChB,mCAAc,GAAd;QACE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IACjC,CAAC;IAED,gBAAgB;IACT,2BAAgB,GAAvB,UAAwB,GAAuB;QAC7C,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;IAED,gBAAgB;IAChB,qBAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IACH,iBAAC;AAAD,CAAC,AA7CD,IA6CC;AA7CY,gCAAU;AA+CvB,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC"}

View File

@@ -0,0 +1,102 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timestamp = exports.LongWithoutOverridesClass = void 0;
var long_1 = require("./long");
var utils_1 = require("./parser/utils");
/** @public */
exports.LongWithoutOverridesClass = long_1.Long;
/**
* @public
* @category BSONType
* */
var Timestamp = /** @class */ (function (_super) {
__extends(Timestamp, _super);
function Timestamp(low, high) {
var _this = this;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (!(_this instanceof Timestamp))
return new Timestamp(low, high);
if (long_1.Long.isLong(low)) {
_this = _super.call(this, low.low, low.high, true) || this;
}
else if ((0, utils_1.isObjectLike)(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
_this = _super.call(this, low.i, low.t, true) || this;
}
else {
_this = _super.call(this, low, high, true) || this;
}
Object.defineProperty(_this, '_bsontype', {
value: 'Timestamp',
writable: false,
configurable: false,
enumerable: false
});
return _this;
}
Timestamp.prototype.toJSON = function () {
return {
$timestamp: this.toString()
};
};
/** Returns a Timestamp represented by the given (32-bit) integer value. */
Timestamp.fromInt = function (value) {
return new Timestamp(long_1.Long.fromInt(value, true));
};
/** Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. */
Timestamp.fromNumber = function (value) {
return new Timestamp(long_1.Long.fromNumber(value, true));
};
/**
* Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
*
* @param lowBits - the low 32-bits.
* @param highBits - the high 32-bits.
*/
Timestamp.fromBits = function (lowBits, highBits) {
return new Timestamp(lowBits, highBits);
};
/**
* Returns a Timestamp from the given string, optionally using the given radix.
*
* @param str - the textual representation of the Timestamp.
* @param optRadix - the radix in which the text is written.
*/
Timestamp.fromString = function (str, optRadix) {
return new Timestamp(long_1.Long.fromString(str, true, optRadix));
};
/** @internal */
Timestamp.prototype.toExtendedJSON = function () {
return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
};
/** @internal */
Timestamp.fromExtendedJSON = function (doc) {
return new Timestamp(doc.$timestamp);
};
/** @internal */
Timestamp.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
Timestamp.prototype.inspect = function () {
return "new Timestamp({ t: ".concat(this.getHighBits(), ", i: ").concat(this.getLowBits(), " })");
};
Timestamp.MAX_VALUE = long_1.Long.MAX_UNSIGNED_VALUE;
return Timestamp;
}(exports.LongWithoutOverridesClass));
exports.Timestamp = Timestamp;
//# sourceMappingURL=timestamp.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"timestamp.js","sourceRoot":"","sources":["../src/timestamp.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,+BAA8B;AAC9B,wCAA8C;AAQ9C,cAAc;AACD,QAAA,yBAAyB,GACpC,WAAuC,CAAC;AAU1C;;;KAGK;AACL;IAA+B,6BAAyB;IAmBtD,mBAAY,GAA6C,EAAE,IAAa;QAAxE,iBAkBC;QAjBC,6DAA6D;QAC7D,mBAAmB;QACnB,IAAI,CAAC,CAAC,KAAI,YAAY,SAAS,CAAC;YAAE,OAAO,IAAI,SAAS,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAElE,IAAI,WAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;YACpB,QAAA,kBAAM,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,SAAC;SAChC;aAAM,IAAI,IAAA,oBAAY,EAAC,GAAG,CAAC,IAAI,OAAO,GAAG,CAAC,CAAC,KAAK,WAAW,IAAI,OAAO,GAAG,CAAC,CAAC,KAAK,WAAW,EAAE;YAC5F,QAAA,kBAAM,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,SAAC;SAC3B;aAAM;YACL,QAAA,kBAAM,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,SAAC;SACxB;QACD,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,WAAW,EAAE;YACvC,KAAK,EAAE,WAAW;YAClB,QAAQ,EAAE,KAAK;YACf,YAAY,EAAE,KAAK;YACnB,UAAU,EAAE,KAAK;SAClB,CAAC,CAAC;;IACL,CAAC;IAED,0BAAM,GAAN;QACE,OAAO;YACL,UAAU,EAAE,IAAI,CAAC,QAAQ,EAAE;SAC5B,CAAC;IACJ,CAAC;IAED,2EAA2E;IACpE,iBAAO,GAAd,UAAe,KAAa;QAC1B,OAAO,IAAI,SAAS,CAAC,WAAI,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,iIAAiI;IAC1H,oBAAU,GAAjB,UAAkB,KAAa;QAC7B,OAAO,IAAI,SAAS,CAAC,WAAI,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;IACrD,CAAC;IAED;;;;;OAKG;IACI,kBAAQ,GAAf,UAAgB,OAAe,EAAE,QAAgB;QAC/C,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;OAKG;IACI,oBAAU,GAAjB,UAAkB,GAAW,EAAE,QAAgB;QAC7C,OAAO,IAAI,SAAS,CAAC,WAAI,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC7D,CAAC;IAED,gBAAgB;IAChB,kCAAc,GAAd;QACE,OAAO,EAAE,UAAU,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,GAAG,KAAK,CAAC,EAAE,EAAE,CAAC;IACnE,CAAC;IAED,gBAAgB;IACT,0BAAgB,GAAvB,UAAwB,GAAsB;QAC5C,OAAO,IAAI,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACvC,CAAC;IAED,gBAAgB;IAChB,oBAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,2BAAO,GAAP;QACE,OAAO,6BAAsB,IAAI,CAAC,WAAW,EAAE,kBAAQ,IAAI,CAAC,UAAU,EAAE,QAAK,CAAC;IAChF,CAAC;IAzFe,mBAAS,GAAG,WAAI,CAAC,kBAAkB,CAAC;IA0FtD,gBAAC;CAAA,AA7FD,CAA+B,iCAAyB,GA6FvD;AA7FY,8BAAS"}

View File

@@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGlobal = void 0;
function checkForMath(potentialGlobal) {
// eslint-disable-next-line eqeqeq
return potentialGlobal && potentialGlobal.Math == Math && potentialGlobal;
}
// https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
function getGlobal() {
return (checkForMath(typeof globalThis === 'object' && globalThis) ||
checkForMath(typeof window === 'object' && window) ||
checkForMath(typeof self === 'object' && self) ||
checkForMath(typeof global === 'object' && global) ||
// eslint-disable-next-line @typescript-eslint/no-implied-eval
Function('return this')());
}
exports.getGlobal = getGlobal;
//# sourceMappingURL=global.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"global.js","sourceRoot":"","sources":["../../src/utils/global.ts"],"names":[],"mappings":";;;AAMA,SAAS,YAAY,CAAC,eAAoB;IACxC,kCAAkC;IAClC,OAAO,eAAe,IAAI,eAAe,CAAC,IAAI,IAAI,IAAI,IAAI,eAAe,CAAC;AAC5E,CAAC;AAED,uEAAuE;AACvE,SAAgB,SAAS;IACvB,OAAO,CACL,YAAY,CAAC,OAAO,UAAU,KAAK,QAAQ,IAAI,UAAU,CAAC;QAC1D,YAAY,CAAC,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC;QAClD,YAAY,CAAC,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC;QAC9C,YAAY,CAAC,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC;QAClD,8DAA8D;QAC9D,QAAQ,CAAC,aAAa,CAAC,EAAE,CAC1B,CAAC;AACJ,CAAC;AATD,8BASC"}

View File

@@ -0,0 +1,35 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bufferToUuidHexString = exports.uuidHexStringToBuffer = exports.uuidValidateString = void 0;
var buffer_1 = require("buffer");
var error_1 = require("./error");
// Validation regex for v4 uuid (validates with or without dashes)
var VALIDATION_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15})$/i;
var uuidValidateString = function (str) {
return typeof str === 'string' && VALIDATION_REGEX.test(str);
};
exports.uuidValidateString = uuidValidateString;
var uuidHexStringToBuffer = function (hexString) {
if (!(0, exports.uuidValidateString)(hexString)) {
throw new error_1.BSONTypeError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
}
var sanitizedHexString = hexString.replace(/-/g, '');
return buffer_1.Buffer.from(sanitizedHexString, 'hex');
};
exports.uuidHexStringToBuffer = uuidHexStringToBuffer;
var bufferToUuidHexString = function (buffer, includeDashes) {
if (includeDashes === void 0) { includeDashes = true; }
return includeDashes
? buffer.toString('hex', 0, 4) +
'-' +
buffer.toString('hex', 4, 6) +
'-' +
buffer.toString('hex', 6, 8) +
'-' +
buffer.toString('hex', 8, 10) +
'-' +
buffer.toString('hex', 10, 16)
: buffer.toString('hex');
};
exports.bufferToUuidHexString = bufferToUuidHexString;
//# sourceMappingURL=uuid_utils.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"uuid_utils.js","sourceRoot":"","sources":["../src/uuid_utils.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,iCAAwC;AAExC,kEAAkE;AAClE,IAAM,gBAAgB,GACpB,uHAAuH,CAAC;AAEnH,IAAM,kBAAkB,GAAG,UAAC,GAAW;IAC5C,OAAA,OAAO,GAAG,KAAK,QAAQ,IAAI,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;AAArD,CAAqD,CAAC;AAD3C,QAAA,kBAAkB,sBACyB;AAEjD,IAAM,qBAAqB,GAAG,UAAC,SAAiB;IACrD,IAAI,CAAC,IAAA,0BAAkB,EAAC,SAAS,CAAC,EAAE;QAClC,MAAM,IAAI,qBAAa,CACrB,uLAAuL,CACxL,CAAC;KACH;IAED,IAAM,kBAAkB,GAAG,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACvD,OAAO,eAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;AAChD,CAAC,CAAC;AATW,QAAA,qBAAqB,yBAShC;AAEK,IAAM,qBAAqB,GAAG,UAAC,MAAc,EAAE,aAAoB;IAApB,8BAAA,EAAA,oBAAoB;IACxE,OAAA,aAAa;QACX,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAC5B,GAAG;YACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAC5B,GAAG;YACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAC5B,GAAG;YACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YAC7B,GAAG;YACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC;QAChC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;AAV1B,CAU0B,CAAC;AAXhB,QAAA,qBAAqB,yBAWL"}

View File

@@ -0,0 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateUtf8 = void 0;
var FIRST_BIT = 0x80;
var FIRST_TWO_BITS = 0xc0;
var FIRST_THREE_BITS = 0xe0;
var FIRST_FOUR_BITS = 0xf0;
var FIRST_FIVE_BITS = 0xf8;
var TWO_BIT_CHAR = 0xc0;
var THREE_BIT_CHAR = 0xe0;
var FOUR_BIT_CHAR = 0xf0;
var CONTINUING_CHAR = 0x80;
/**
* Determines if the passed in bytes are valid utf8
* @param bytes - An array of 8-bit bytes. Must be indexable and have length property
* @param start - The index to start validating
* @param end - The index to end validating
*/
function validateUtf8(bytes, start, end) {
var continuation = 0;
for (var i = start; i < end; i += 1) {
var byte = bytes[i];
if (continuation) {
if ((byte & FIRST_TWO_BITS) !== CONTINUING_CHAR) {
return false;
}
continuation -= 1;
}
else if (byte & FIRST_BIT) {
if ((byte & FIRST_THREE_BITS) === TWO_BIT_CHAR) {
continuation = 1;
}
else if ((byte & FIRST_FOUR_BITS) === THREE_BIT_CHAR) {
continuation = 2;
}
else if ((byte & FIRST_FIVE_BITS) === FOUR_BIT_CHAR) {
continuation = 3;
}
else {
return false;
}
}
}
return !continuation;
}
exports.validateUtf8 = validateUtf8;
//# sourceMappingURL=validate_utf8.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"validate_utf8.js","sourceRoot":"","sources":["../src/validate_utf8.ts"],"names":[],"mappings":";;;AAAA,IAAM,SAAS,GAAG,IAAI,CAAC;AACvB,IAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,IAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,IAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,IAAM,eAAe,GAAG,IAAI,CAAC;AAE7B,IAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,IAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,IAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,IAAM,eAAe,GAAG,IAAI,CAAC;AAE7B;;;;;GAKG;AACH,SAAgB,YAAY,CAC1B,KAAkC,EAClC,KAAa,EACb,GAAW;IAEX,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE;QACnC,IAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAEtB,IAAI,YAAY,EAAE;YAChB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC,KAAK,eAAe,EAAE;gBAC/C,OAAO,KAAK,CAAC;aACd;YACD,YAAY,IAAI,CAAC,CAAC;SACnB;aAAM,IAAI,IAAI,GAAG,SAAS,EAAE;YAC3B,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC,KAAK,YAAY,EAAE;gBAC9C,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,cAAc,EAAE;gBACtD,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,aAAa,EAAE;gBACrD,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM;gBACL,OAAO,KAAK,CAAC;aACd;SACF;KACF;IAED,OAAO,CAAC,YAAY,CAAC;AACvB,CAAC;AA7BD,oCA6BC"}

View File

@@ -0,0 +1,116 @@
{
"name": "bson",
"description": "A bson parser for node.js and the browser",
"keywords": [
"mongodb",
"bson",
"parser"
],
"files": [
"lib",
"src",
"dist",
"bson.d.ts",
"etc/prepare.js",
"bower.json"
],
"types": "bson.d.ts",
"version": "4.7.2",
"author": {
"name": "The MongoDB NodeJS Team",
"email": "dbx-node@mongodb.com"
},
"license": "Apache-2.0",
"contributors": [],
"repository": "mongodb/js-bson",
"bugs": {
"url": "https://jira.mongodb.org/projects/NODE/issues/"
},
"devDependencies": {
"@babel/plugin-external-helpers": "^7.10.4",
"@babel/preset-env": "^7.11.0",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@microsoft/api-extractor": "^7.28.0",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-commonjs": "^15.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-replace": "^4.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.30.0",
"@typescript-eslint/parser": "^5.30.0",
"array-includes": "^3.1.3",
"array.prototype.flatmap": "^1.3.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"eslint": "^8.18.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.1.0",
"eslint-plugin-tsdoc": "^0.2.16",
"karma": "^6.3.4",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-rollup-preprocessor": "^7.0.5",
"mocha": "5.2.0",
"node-fetch": "^2.6.1",
"nyc": "^15.1.0",
"object.entries": "^1.1.5",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"rollup": "^2.26.5",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-polyfill-node": "^0.7.0",
"standard-version": "^9.5.0",
"ts-node": "^9.0.0",
"tsd": "^0.21.0",
"typescript": "^4.7.4",
"typescript-cached-transpile": "0.0.6",
"uuid": "^8.3.2"
},
"tsd": {
"directory": "test/types",
"compilerOptions": {
"strict": true,
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node"
}
},
"config": {
"native": false
},
"main": "lib/bson.js",
"module": "dist/bson.esm.js",
"browser": {
"./lib/bson.js": "./dist/bson.browser.umd.js",
"./dist/bson.esm.js": "./dist/bson.browser.esm.js"
},
"engines": {
"node": ">=6.9.0"
},
"scripts": {
"docs": "typedoc",
"test": "npm run build && npm run test-node && npm run test-browser",
"test-node": "mocha test/node test/*_tests.js",
"test-tsd": "npm run build:dts && tsd",
"test-browser": "node --max-old-space-size=4096 ./node_modules/.bin/karma start karma.conf.js",
"build:ts": "tsc",
"build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && rimraf 'lib/**/*.d.ts*'",
"build:bundle": "rollup -c rollup.config.js",
"build": "npm run build:dts && npm run build:bundle",
"lint": "eslint -v && eslint --ext '.js,.ts' --max-warnings=0 src test && tsc -v && tsc --noEmit && npm run test-tsd",
"format": "eslint --ext '.js,.ts' src test --fix",
"coverage": "nyc npm run test-node",
"coverage:html": "npm run coverage && open ./coverage/index.html",
"prepare": "node etc/prepare.js",
"release": "standard-version -i HISTORY.md"
},
"dependencies": {
"buffer": "^5.6.0"
}
}

View File

@@ -0,0 +1,481 @@
import { Buffer } from 'buffer';
import { ensureBuffer } from './ensure_buffer';
import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils';
import { isUint8Array, randomBytes } from './parser/utils';
import type { EJSONOptions } from './extended_json';
import { BSONError, BSONTypeError } from './error';
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
/** @public */
export type BinarySequence = Uint8Array | Buffer | number[];
/** @public */
export interface BinaryExtendedLegacy {
$type: string;
$binary: string;
}
/** @public */
export interface BinaryExtended {
$binary: {
subType: string;
base64: string;
};
}
/**
* A class representation of the BSON Binary type.
* @public
* @category BSONType
*/
export class Binary {
_bsontype!: 'Binary';
/**
* Binary default subtype
* @internal
*/
private static readonly BSON_BINARY_SUBTYPE_DEFAULT = 0;
/** Initial buffer default size */
static readonly BUFFER_SIZE = 256;
/** Default BSON type */
static readonly SUBTYPE_DEFAULT = 0;
/** Function BSON type */
static readonly SUBTYPE_FUNCTION = 1;
/** Byte Array BSON type */
static readonly SUBTYPE_BYTE_ARRAY = 2;
/** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
static readonly SUBTYPE_UUID_OLD = 3;
/** UUID BSON type */
static readonly SUBTYPE_UUID = 4;
/** MD5 BSON type */
static readonly SUBTYPE_MD5 = 5;
/** Encrypted BSON type */
static readonly SUBTYPE_ENCRYPTED = 6;
/** Column BSON type */
static readonly SUBTYPE_COLUMN = 7;
/** User BSON type */
static readonly SUBTYPE_USER_DEFINED = 128;
buffer!: Buffer;
sub_type!: number;
position!: number;
/**
* Create a new Binary instance.
*
* This constructor can accept a string as its first argument. In this case,
* this string will be encoded using ISO-8859-1, **not** using UTF-8.
* This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
* instead to convert the string to a Buffer using UTF-8 first.
*
* @param buffer - a buffer object containing the binary data.
* @param subType - the option binary type.
*/
constructor(buffer?: string | BinarySequence, subType?: number) {
if (!(this instanceof Binary)) return new Binary(buffer, subType);
if (
!(buffer == null) &&
!(typeof buffer === 'string') &&
!ArrayBuffer.isView(buffer) &&
!(buffer instanceof ArrayBuffer) &&
!Array.isArray(buffer)
) {
throw new BSONTypeError(
'Binary can only be constructed from string, Buffer, TypedArray, or Array<number>'
);
}
this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
if (buffer == null) {
// create an empty binary buffer
this.buffer = Buffer.alloc(Binary.BUFFER_SIZE);
this.position = 0;
} else {
if (typeof buffer === 'string') {
// string
this.buffer = Buffer.from(buffer, 'binary');
} else if (Array.isArray(buffer)) {
// number[]
this.buffer = Buffer.from(buffer);
} else {
// Buffer | TypedArray | ArrayBuffer
this.buffer = ensureBuffer(buffer);
}
this.position = this.buffer.byteLength;
}
}
/**
* Updates this binary with byte_value.
*
* @param byteValue - a single byte we wish to write.
*/
put(byteValue: string | number | Uint8Array | Buffer | number[]): void {
// If it's a string and a has more than one character throw an error
if (typeof byteValue === 'string' && byteValue.length !== 1) {
throw new BSONTypeError('only accepts single character String');
} else if (typeof byteValue !== 'number' && byteValue.length !== 1)
throw new BSONTypeError('only accepts single character Uint8Array or Array');
// Decode the byte value once
let decodedByte: number;
if (typeof byteValue === 'string') {
decodedByte = byteValue.charCodeAt(0);
} else if (typeof byteValue === 'number') {
decodedByte = byteValue;
} else {
decodedByte = byteValue[0];
}
if (decodedByte < 0 || decodedByte > 255) {
throw new BSONTypeError('only accepts number in a valid unsigned byte range 0-255');
}
if (this.buffer.length > this.position) {
this.buffer[this.position++] = decodedByte;
} else {
const buffer = Buffer.alloc(Binary.BUFFER_SIZE + this.buffer.length);
// Combine the two buffers together
this.buffer.copy(buffer, 0, 0, this.buffer.length);
this.buffer = buffer;
this.buffer[this.position++] = decodedByte;
}
}
/**
* Writes a buffer or string to the binary.
*
* @param sequence - a string or buffer to be written to the Binary BSON object.
* @param offset - specify the binary of where to write the content.
*/
write(sequence: string | BinarySequence, offset: number): void {
offset = typeof offset === 'number' ? offset : this.position;
// If the buffer is to small let's extend the buffer
if (this.buffer.length < offset + sequence.length) {
const buffer = Buffer.alloc(this.buffer.length + sequence.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
// Assign the new buffer
this.buffer = buffer;
}
if (ArrayBuffer.isView(sequence)) {
this.buffer.set(ensureBuffer(sequence), offset);
this.position =
offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
} else if (typeof sequence === 'string') {
this.buffer.write(sequence, offset, sequence.length, 'binary');
this.position =
offset + sequence.length > this.position ? offset + sequence.length : this.position;
}
}
/**
* Reads **length** bytes starting at **position**.
*
* @param position - read from the given position in the Binary.
* @param length - the number of bytes to read.
*/
read(position: number, length: number): BinarySequence {
length = length && length > 0 ? length : this.position;
// Let's return the data based on the type we have
return this.buffer.slice(position, position + length);
}
/**
* Returns the value of this binary as a string.
* @param asRaw - Will skip converting to a string
* @remarks
* This is handy when calling this function conditionally for some key value pairs and not others
*/
value(asRaw?: boolean): string | BinarySequence {
asRaw = !!asRaw;
// Optimize to serialize for the situation where the data == size of buffer
if (asRaw && this.buffer.length === this.position) {
return this.buffer;
}
// If it's a node.js buffer object
if (asRaw) {
return this.buffer.slice(0, this.position);
}
return this.buffer.toString('binary', 0, this.position);
}
/** the length of the binary sequence */
length(): number {
return this.position;
}
toJSON(): string {
return this.buffer.toString('base64');
}
toString(format?: string): string {
return this.buffer.toString(format);
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended {
options = options || {};
const base64String = this.buffer.toString('base64');
const subType = Number(this.sub_type).toString(16);
if (options.legacy) {
return {
$binary: base64String,
$type: subType.length === 1 ? '0' + subType : subType
};
}
return {
$binary: {
base64: base64String,
subType: subType.length === 1 ? '0' + subType : subType
}
};
}
toUUID(): UUID {
if (this.sub_type === Binary.SUBTYPE_UUID) {
return new UUID(this.buffer.slice(0, this.position));
}
throw new BSONError(
`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`
);
}
/** @internal */
static fromExtendedJSON(
doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended,
options?: EJSONOptions
): Binary {
options = options || {};
let data: Buffer | undefined;
let type;
if ('$binary' in doc) {
if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) {
type = doc.$type ? parseInt(doc.$type, 16) : 0;
data = Buffer.from(doc.$binary, 'base64');
} else {
if (typeof doc.$binary !== 'string') {
type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
data = Buffer.from(doc.$binary.base64, 'base64');
}
}
} else if ('$uuid' in doc) {
type = 4;
data = uuidHexStringToBuffer(doc.$uuid);
}
if (!data) {
throw new BSONTypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
}
return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
const asBuffer = this.value(true);
return `new Binary(Buffer.from("${asBuffer.toString('hex')}", "hex"), ${this.sub_type})`;
}
}
Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
/** @public */
export type UUIDExtended = {
$uuid: string;
};
const UUID_BYTE_LENGTH = 16;
/**
* A class representation of the BSON UUID type.
* @public
*/
export class UUID extends Binary {
static cacheHexString: boolean;
/** UUID hexString cache @internal */
private __id?: string;
/**
* Create an UUID type
*
* @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
*/
constructor(input?: string | Buffer | UUID) {
let bytes;
let hexStr;
if (input == null) {
bytes = UUID.generate();
} else if (input instanceof UUID) {
bytes = Buffer.from(input.buffer);
hexStr = input.__id;
} else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
bytes = ensureBuffer(input);
} else if (typeof input === 'string') {
bytes = uuidHexStringToBuffer(input);
} else {
throw new BSONTypeError(
'Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'
);
}
super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
this.__id = hexStr;
}
/**
* The UUID bytes
* @readonly
*/
get id(): Buffer {
return this.buffer;
}
set id(value: Buffer) {
this.buffer = value;
if (UUID.cacheHexString) {
this.__id = bufferToUuidHexString(value);
}
}
/**
* Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
* @param includeDashes - should the string exclude dash-separators.
* */
toHexString(includeDashes = true): string {
if (UUID.cacheHexString && this.__id) {
return this.__id;
}
const uuidHexString = bufferToUuidHexString(this.id, includeDashes);
if (UUID.cacheHexString) {
this.__id = uuidHexString;
}
return uuidHexString;
}
/**
* Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
*/
toString(encoding?: string): string {
return encoding ? this.id.toString(encoding) : this.toHexString();
}
/**
* Converts the id into its JSON string representation.
* A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
*/
toJSON(): string {
return this.toHexString();
}
/**
* Compares the equality of this UUID with `otherID`.
*
* @param otherId - UUID instance to compare against.
*/
equals(otherId: string | Buffer | UUID): boolean {
if (!otherId) {
return false;
}
if (otherId instanceof UUID) {
return otherId.id.equals(this.id);
}
try {
return new UUID(otherId).id.equals(this.id);
} catch {
return false;
}
}
/**
* Creates a Binary instance from the current UUID.
*/
toBinary(): Binary {
return new Binary(this.id, Binary.SUBTYPE_UUID);
}
/**
* Generates a populated buffer containing a v4 uuid
*/
static generate(): Buffer {
const bytes = randomBytes(UUID_BYTE_LENGTH);
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
// Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
return Buffer.from(bytes);
}
/**
* Checks if a value is a valid bson UUID
* @param input - UUID, string or Buffer to validate.
*/
static isValid(input: string | Buffer | UUID): boolean {
if (!input) {
return false;
}
if (input instanceof UUID) {
return true;
}
if (typeof input === 'string') {
return uuidValidateString(input);
}
if (isUint8Array(input)) {
// check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
if (input.length !== UUID_BYTE_LENGTH) {
return false;
}
return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
}
return false;
}
/**
* Creates an UUID from a hex string representation of an UUID.
* @param hexString - 32 or 36 character hex string (dashes excluded/included).
*/
static createFromHexString(hexString: string): UUID {
const buffer = uuidHexStringToBuffer(hexString);
return new UUID(buffer);
}
/**
* Converts to a string representation of this Id.
*
* @returns return the 36 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new UUID("${this.toHexString()}")`;
}
}

View File

@@ -0,0 +1,330 @@
import { Buffer } from 'buffer';
import { Binary, UUID } from './binary';
import { Code } from './code';
import { DBRef } from './db_ref';
import { Decimal128 } from './decimal128';
import { Double } from './double';
import { ensureBuffer } from './ensure_buffer';
import { EJSON } from './extended_json';
import { Int32 } from './int_32';
import { Long } from './long';
import { Map } from './map';
import { MaxKey } from './max_key';
import { MinKey } from './min_key';
import { ObjectId } from './objectid';
import { BSONError, BSONTypeError } from './error';
import { calculateObjectSize as internalCalculateObjectSize } from './parser/calculate_size';
// Parts of the parser
import { deserialize as internalDeserialize, DeserializeOptions } from './parser/deserializer';
import { serializeInto as internalSerialize, SerializeOptions } from './parser/serializer';
import { BSONRegExp } from './regexp';
import { BSONSymbol } from './symbol';
import { Timestamp } from './timestamp';
export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
export type { CodeExtended } from './code';
export {
BSON_BINARY_SUBTYPE_BYTE_ARRAY,
BSON_BINARY_SUBTYPE_DEFAULT,
BSON_BINARY_SUBTYPE_FUNCTION,
BSON_BINARY_SUBTYPE_MD5,
BSON_BINARY_SUBTYPE_USER_DEFINED,
BSON_BINARY_SUBTYPE_UUID,
BSON_BINARY_SUBTYPE_UUID_NEW,
BSON_BINARY_SUBTYPE_ENCRYPTED,
BSON_BINARY_SUBTYPE_COLUMN,
BSON_DATA_ARRAY,
BSON_DATA_BINARY,
BSON_DATA_BOOLEAN,
BSON_DATA_CODE,
BSON_DATA_CODE_W_SCOPE,
BSON_DATA_DATE,
BSON_DATA_DBPOINTER,
BSON_DATA_DECIMAL128,
BSON_DATA_INT,
BSON_DATA_LONG,
BSON_DATA_MAX_KEY,
BSON_DATA_MIN_KEY,
BSON_DATA_NULL,
BSON_DATA_NUMBER,
BSON_DATA_OBJECT,
BSON_DATA_OID,
BSON_DATA_REGEXP,
BSON_DATA_STRING,
BSON_DATA_SYMBOL,
BSON_DATA_TIMESTAMP,
BSON_DATA_UNDEFINED,
BSON_INT32_MAX,
BSON_INT32_MIN,
BSON_INT64_MAX,
BSON_INT64_MIN
} from './constants';
export type { DBRefLike } from './db_ref';
export type { Decimal128Extended } from './decimal128';
export type { DoubleExtended } from './double';
export type { EJSONOptions } from './extended_json';
export { EJSON } from './extended_json';
export type { Int32Extended } from './int_32';
export type { LongExtended } from './long';
export type { MaxKeyExtended } from './max_key';
export type { MinKeyExtended } from './min_key';
export type { ObjectIdExtended, ObjectIdLike } from './objectid';
export type { BSONRegExpExtended, BSONRegExpExtendedLegacy } from './regexp';
export type { BSONSymbolExtended } from './symbol';
export type { LongWithoutOverrides, TimestampExtended, TimestampOverrides } from './timestamp';
export { LongWithoutOverridesClass } from './timestamp';
export type { SerializeOptions, DeserializeOptions };
export {
Code,
Map,
BSONSymbol,
DBRef,
Binary,
ObjectId,
UUID,
Long,
Timestamp,
Double,
Int32,
MinKey,
MaxKey,
BSONRegExp,
Decimal128,
// In 4.0.0 and 4.0.1, this property name was changed to ObjectId to match the class name.
// This caused interoperability problems with previous versions of the library, so in
// later builds we changed it back to ObjectID (capital D) to match legacy implementations.
ObjectId as ObjectID
};
export { BSONError, BSONTypeError } from './error';
/** @public */
export interface Document {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
/** @internal */
// Default Max Size
const MAXSIZE = 1024 * 1024 * 17;
// Current Internal Temporary Serialization Buffer
let buffer = Buffer.alloc(MAXSIZE);
/**
* Sets the size of the internal serialization buffer.
*
* @param size - The desired size for the internal serialization buffer
* @public
*/
export function setInternalBufferSize(size: number): void {
// Resize the internal serialization buffer if needed
if (buffer.length < size) {
buffer = Buffer.alloc(size);
}
}
/**
* Serialize a Javascript object.
*
* @param object - the Javascript object to serialize.
* @returns Buffer object containing the serialized object.
* @public
*/
export function serialize(object: Document, options: SerializeOptions = {}): Buffer {
// Unpack the options
const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
const serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
const ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
const minInternalBufferSize =
typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;
// Resize the internal serialization buffer if needed
if (buffer.length < minInternalBufferSize) {
buffer = Buffer.alloc(minInternalBufferSize);
}
// Attempt to serialize
const serializationIndex = internalSerialize(
buffer,
object,
checkKeys,
0,
0,
serializeFunctions,
ignoreUndefined,
[]
);
// Create the final buffer
const finishedBuffer = Buffer.alloc(serializationIndex);
// Copy into the finished buffer
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
// Return the buffer
return finishedBuffer;
}
/**
* Serialize a Javascript object using a predefined Buffer and index into the buffer,
* useful when pre-allocating the space for serialization.
*
* @param object - the Javascript object to serialize.
* @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.
* @returns the index pointing to the last written byte in the buffer.
* @public
*/
export function serializeWithBufferAndIndex(
object: Document,
finalBuffer: Buffer,
options: SerializeOptions = {}
): number {
// Unpack the options
const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
const serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
const ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
const startIndex = typeof options.index === 'number' ? options.index : 0;
// Attempt to serialize
const serializationIndex = internalSerialize(
buffer,
object,
checkKeys,
0,
0,
serializeFunctions,
ignoreUndefined
);
buffer.copy(finalBuffer, startIndex, 0, serializationIndex);
// Return the index
return startIndex + serializationIndex - 1;
}
/**
* Deserialize data as BSON.
*
* @param buffer - the buffer containing the serialized set of BSON documents.
* @returns returns the deserialized Javascript Object.
* @public
*/
export function deserialize(
buffer: Buffer | ArrayBufferView | ArrayBuffer,
options: DeserializeOptions = {}
): Document {
return internalDeserialize(buffer instanceof Buffer ? buffer : ensureBuffer(buffer), options);
}
/** @public */
export type CalculateObjectSizeOptions = Pick<
SerializeOptions,
'serializeFunctions' | 'ignoreUndefined'
>;
/**
* Calculate the bson size for a passed in Javascript object.
*
* @param object - the Javascript object to calculate the BSON byte size for
* @returns size of BSON object in bytes
* @public
*/
export function calculateObjectSize(
object: Document,
options: CalculateObjectSizeOptions = {}
): number {
options = options || {};
const serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
const ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
return internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined);
}
/**
* Deserialize stream data as BSON documents.
*
* @param data - the buffer containing the serialized set of BSON documents.
* @param startIndex - the start index in the data Buffer where the deserialization is to start.
* @param numberOfDocuments - number of documents to deserialize.
* @param documents - an array where to store the deserialized documents.
* @param docStartIndex - the index in the documents array from where to start inserting documents.
* @param options - additional options used for the deserialization.
* @returns next index in the buffer after deserialization **x** numbers of documents.
* @public
*/
export function deserializeStream(
data: Buffer | ArrayBufferView | ArrayBuffer,
startIndex: number,
numberOfDocuments: number,
documents: Document[],
docStartIndex: number,
options: DeserializeOptions
): number {
const internalOptions = Object.assign(
{ allowObjectSmallerThanBufferSize: true, index: 0 },
options
);
const bufferData = ensureBuffer(data);
let index = startIndex;
// Loop over all documents
for (let i = 0; i < numberOfDocuments; i++) {
// Find size of the document
const size =
bufferData[index] |
(bufferData[index + 1] << 8) |
(bufferData[index + 2] << 16) |
(bufferData[index + 3] << 24);
// Update options with index
internalOptions.index = index;
// Parse the document at this point
documents[docStartIndex + i] = internalDeserialize(bufferData, internalOptions);
// Adjust index by the document size
index = index + size;
}
// Return object containing end index of parsing and list of documents
return index;
}
/**
* BSON default export
* @deprecated Please use named exports
* @privateRemarks
* We want to someday deprecate the default export,
* so none of the new TS types are being exported on the default
* @public
*/
const BSON = {
Binary,
Code,
DBRef,
Decimal128,
Double,
Int32,
Long,
UUID,
Map,
MaxKey,
MinKey,
ObjectId,
ObjectID: ObjectId,
BSONRegExp,
BSONSymbol,
Timestamp,
EJSON,
setInternalBufferSize,
serialize,
serializeWithBufferAndIndex,
deserialize,
calculateObjectSize,
deserializeStream,
BSONError,
BSONTypeError
};
export default BSON;

View File

@@ -0,0 +1,61 @@
import type { Document } from './bson';
/** @public */
export interface CodeExtended {
$code: string | Function;
$scope?: Document;
}
/**
* A class representation of the BSON Code type.
* @public
* @category BSONType
*/
export class Code {
_bsontype!: 'Code';
code!: string | Function;
scope?: Document;
/**
* @param code - a string or function.
* @param scope - an optional scope for the function.
*/
constructor(code: string | Function, scope?: Document) {
if (!(this instanceof Code)) return new Code(code, scope);
this.code = code;
this.scope = scope;
}
toJSON(): { code: string | Function; scope?: Document } {
return { code: this.code, scope: this.scope };
}
/** @internal */
toExtendedJSON(): CodeExtended {
if (this.scope) {
return { $code: this.code, $scope: this.scope };
}
return { $code: this.code };
}
/** @internal */
static fromExtendedJSON(doc: CodeExtended): Code {
return new Code(doc.$code, doc.$scope);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
const codeJson = this.toJSON();
return `new Code("${String(codeJson.code)}"${
codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''
})`;
}
}
Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });

View File

@@ -0,0 +1,110 @@
/** @internal */
export const BSON_INT32_MAX = 0x7fffffff;
/** @internal */
export const BSON_INT32_MIN = -0x80000000;
/** @internal */
export const BSON_INT64_MAX = Math.pow(2, 63) - 1;
/** @internal */
export const BSON_INT64_MIN = -Math.pow(2, 63);
/**
* Any integer up to 2^53 can be precisely represented by a double.
* @internal
*/
export const JS_INT_MAX = Math.pow(2, 53);
/**
* Any integer down to -2^53 can be precisely represented by a double.
* @internal
*/
export const JS_INT_MIN = -Math.pow(2, 53);
/** Number BSON Type @internal */
export const BSON_DATA_NUMBER = 1;
/** String BSON Type @internal */
export const BSON_DATA_STRING = 2;
/** Object BSON Type @internal */
export const BSON_DATA_OBJECT = 3;
/** Array BSON Type @internal */
export const BSON_DATA_ARRAY = 4;
/** Binary BSON Type @internal */
export const BSON_DATA_BINARY = 5;
/** Binary BSON Type @internal */
export const BSON_DATA_UNDEFINED = 6;
/** ObjectId BSON Type @internal */
export const BSON_DATA_OID = 7;
/** Boolean BSON Type @internal */
export const BSON_DATA_BOOLEAN = 8;
/** Date BSON Type @internal */
export const BSON_DATA_DATE = 9;
/** null BSON Type @internal */
export const BSON_DATA_NULL = 10;
/** RegExp BSON Type @internal */
export const BSON_DATA_REGEXP = 11;
/** Code BSON Type @internal */
export const BSON_DATA_DBPOINTER = 12;
/** Code BSON Type @internal */
export const BSON_DATA_CODE = 13;
/** Symbol BSON Type @internal */
export const BSON_DATA_SYMBOL = 14;
/** Code with Scope BSON Type @internal */
export const BSON_DATA_CODE_W_SCOPE = 15;
/** 32 bit Integer BSON Type @internal */
export const BSON_DATA_INT = 16;
/** Timestamp BSON Type @internal */
export const BSON_DATA_TIMESTAMP = 17;
/** Long BSON Type @internal */
export const BSON_DATA_LONG = 18;
/** Decimal128 BSON Type @internal */
export const BSON_DATA_DECIMAL128 = 19;
/** MinKey BSON Type @internal */
export const BSON_DATA_MIN_KEY = 0xff;
/** MaxKey BSON Type @internal */
export const BSON_DATA_MAX_KEY = 0x7f;
/** Binary Default Type @internal */
export const BSON_BINARY_SUBTYPE_DEFAULT = 0;
/** Binary Function Type @internal */
export const BSON_BINARY_SUBTYPE_FUNCTION = 1;
/** Binary Byte Array Type @internal */
export const BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
/** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
export const BSON_BINARY_SUBTYPE_UUID = 3;
/** Binary UUID Type @internal */
export const BSON_BINARY_SUBTYPE_UUID_NEW = 4;
/** Binary MD5 Type @internal */
export const BSON_BINARY_SUBTYPE_MD5 = 5;
/** Encrypted BSON type @internal */
export const BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
/** Column BSON type @internal */
export const BSON_BINARY_SUBTYPE_COLUMN = 7;
/** Binary User Defined Type @internal */
export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;

View File

@@ -0,0 +1,124 @@
import type { Document } from './bson';
import type { EJSONOptions } from './extended_json';
import type { ObjectId } from './objectid';
import { isObjectLike } from './parser/utils';
/** @public */
export interface DBRefLike {
$ref: string;
$id: ObjectId;
$db?: string;
}
/** @internal */
export function isDBRefLike(value: unknown): value is DBRefLike {
return (
isObjectLike(value) &&
value.$id != null &&
typeof value.$ref === 'string' &&
(value.$db == null || typeof value.$db === 'string')
);
}
/**
* A class representation of the BSON DBRef type.
* @public
* @category BSONType
*/
export class DBRef {
_bsontype!: 'DBRef';
collection!: string;
oid!: ObjectId;
db?: string;
fields!: Document;
/**
* @param collection - the collection name.
* @param oid - the reference ObjectId.
* @param db - optional db name, if omitted the reference is local to the current db.
*/
constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {
if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
// check if namespace has been provided
const parts = collection.split('.');
if (parts.length === 2) {
db = parts.shift();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
collection = parts.shift()!;
}
this.collection = collection;
this.oid = oid;
this.db = db;
this.fields = fields || {};
}
// Property provided for compatibility with the 1.x parser
// the 1.x parser used a "namespace" property, while 4.x uses "collection"
/** @internal */
get namespace(): string {
return this.collection;
}
set namespace(value: string) {
this.collection = value;
}
toJSON(): DBRefLike & Document {
const o = Object.assign(
{
$ref: this.collection,
$id: this.oid
},
this.fields
);
if (this.db != null) o.$db = this.db;
return o;
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): DBRefLike {
options = options || {};
let o: DBRefLike = {
$ref: this.collection,
$id: this.oid
};
if (options.legacy) {
return o;
}
if (this.db) o.$db = this.db;
o = Object.assign(o, this.fields);
return o;
}
/** @internal */
static fromExtendedJSON(doc: DBRefLike): DBRef {
const copy = Object.assign({}, doc) as Partial<DBRefLike>;
delete copy.$ref;
delete copy.$id;
delete copy.$db;
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
// NOTE: if OID is an ObjectId class it will just print the oid string.
const oid =
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${
this.db ? `, "${this.db}"` : ''
})`;
}
}
Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });

View File

@@ -0,0 +1,773 @@
import { Buffer } from 'buffer';
import { BSONTypeError } from './error';
import { Long } from './long';
import { isUint8Array } from './parser/utils';
const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
const PARSE_NAN_REGEXP = /^(\+|-)?NaN$/i;
const EXPONENT_MAX = 6111;
const EXPONENT_MIN = -6176;
const EXPONENT_BIAS = 6176;
const MAX_DIGITS = 34;
// Nan value bits as 32 bit values (due to lack of longs)
const NAN_BUFFER = [
0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
// Infinity value bits 32 bit values (due to lack of longs)
const INF_NEGATIVE_BUFFER = [
0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
const INF_POSITIVE_BUFFER = [
0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
const EXPONENT_REGEX = /^([-+])?(\d+)?$/;
// Extract least significant 5 bits
const COMBINATION_MASK = 0x1f;
// Extract least significant 14 bits
const EXPONENT_MASK = 0x3fff;
// Value of combination field for Inf
const COMBINATION_INFINITY = 30;
// Value of combination field for NaN
const COMBINATION_NAN = 31;
// Detect if the value is a digit
function isDigit(value: string): boolean {
return !isNaN(parseInt(value, 10));
}
// Divide two uint128 values
function divideu128(value: { parts: [number, number, number, number] }) {
const DIVISOR = Long.fromNumber(1000 * 1000 * 1000);
let _rem = Long.fromNumber(0);
if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) {
return { quotient: value, rem: _rem };
}
for (let i = 0; i <= 3; i++) {
// Adjust remainder to match value of next dividend
_rem = _rem.shiftLeft(32);
// Add the divided to _rem
_rem = _rem.add(new Long(value.parts[i], 0));
value.parts[i] = _rem.div(DIVISOR).low;
_rem = _rem.modulo(DIVISOR);
}
return { quotient: value, rem: _rem };
}
// Multiply two Long values and return the 128 bit value
function multiply64x2(left: Long, right: Long): { high: Long; low: Long } {
if (!left && !right) {
return { high: Long.fromNumber(0), low: Long.fromNumber(0) };
}
const leftHigh = left.shiftRightUnsigned(32);
const leftLow = new Long(left.getLowBits(), 0);
const rightHigh = right.shiftRightUnsigned(32);
const rightLow = new Long(right.getLowBits(), 0);
let productHigh = leftHigh.multiply(rightHigh);
let productMid = leftHigh.multiply(rightLow);
const productMid2 = leftLow.multiply(rightHigh);
let productLow = leftLow.multiply(rightLow);
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
productMid = new Long(productMid.getLowBits(), 0)
.add(productMid2)
.add(productLow.shiftRightUnsigned(32));
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
productLow = productMid.shiftLeft(32).add(new Long(productLow.getLowBits(), 0));
// Return the 128 bit result
return { high: productHigh, low: productLow };
}
function lessThan(left: Long, right: Long): boolean {
// Make values unsigned
const uhleft = left.high >>> 0;
const uhright = right.high >>> 0;
// Compare high bits first
if (uhleft < uhright) {
return true;
} else if (uhleft === uhright) {
const ulleft = left.low >>> 0;
const ulright = right.low >>> 0;
if (ulleft < ulright) return true;
}
return false;
}
function invalidErr(string: string, message: string) {
throw new BSONTypeError(`"${string}" is not a valid Decimal128 string - ${message}`);
}
/** @public */
export interface Decimal128Extended {
$numberDecimal: string;
}
/**
* A class representation of the BSON Decimal128 type.
* @public
* @category BSONType
*/
export class Decimal128 {
_bsontype!: 'Decimal128';
readonly bytes!: Buffer;
/**
* @param bytes - a buffer containing the raw Decimal128 bytes in little endian order,
* or a string representation as returned by .toString()
*/
constructor(bytes: Buffer | string) {
if (!(this instanceof Decimal128)) return new Decimal128(bytes);
if (typeof bytes === 'string') {
this.bytes = Decimal128.fromString(bytes).bytes;
} else if (isUint8Array(bytes)) {
if (bytes.byteLength !== 16) {
throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
}
this.bytes = bytes;
} else {
throw new BSONTypeError('Decimal128 must take a Buffer or string');
}
}
/**
* Create a Decimal128 instance from a string representation
*
* @param representation - a numeric string representation.
*/
static fromString(representation: string): Decimal128 {
// Parse state tracking
let isNegative = false;
let sawRadix = false;
let foundNonZero = false;
// Total number of significant digits (no leading or trailing zero)
let significantDigits = 0;
// Total number of significand digits read
let nDigitsRead = 0;
// Total number of digits (no leading zeros)
let nDigits = 0;
// The number of the digits after radix
let radixPosition = 0;
// The index of the first non-zero in *str*
let firstNonZero = 0;
// Digits Array
const digits = [0];
// The number of digits in digits
let nDigitsStored = 0;
// Insertion pointer for digits
let digitsInsert = 0;
// The index of the first non-zero digit
let firstDigit = 0;
// The index of the last digit
let lastDigit = 0;
// Exponent
let exponent = 0;
// loop index over array
let i = 0;
// The high 17 digits of the significand
let significandHigh = new Long(0, 0);
// The low 17 digits of the significand
let significandLow = new Long(0, 0);
// The biased exponent
let biasedExponent = 0;
// Read index
let index = 0;
// Naively prevent against REDOS attacks.
// TODO: implementing a custom parsing for this, or refactoring the regex would yield
// further gains.
if (representation.length >= 7000) {
throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
}
// Results
const stringMatch = representation.match(PARSE_STRING_REGEXP);
const infMatch = representation.match(PARSE_INF_REGEXP);
const nanMatch = representation.match(PARSE_NAN_REGEXP);
// Validate the string
if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) {
throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
}
if (stringMatch) {
// full_match = stringMatch[0]
// sign = stringMatch[1]
const unsignedNumber = stringMatch[2];
// stringMatch[3] is undefined if a whole number (ex "1", 12")
// but defined if a number w/ decimal in it (ex "1.0, 12.2")
const e = stringMatch[4];
const expSign = stringMatch[5];
const expNumber = stringMatch[6];
// they provided e, but didn't give an exponent number. for ex "1e"
if (e && expNumber === undefined) invalidErr(representation, 'missing exponent power');
// they provided e, but didn't give a number before it. for ex "e1"
if (e && unsignedNumber === undefined) invalidErr(representation, 'missing exponent base');
if (e === undefined && (expSign || expNumber)) {
invalidErr(representation, 'missing e before exponent');
}
}
// Get the negative or positive sign
if (representation[index] === '+' || representation[index] === '-') {
isNegative = representation[index++] === '-';
}
// Check if user passed Infinity or NaN
if (!isDigit(representation[index]) && representation[index] !== '.') {
if (representation[index] === 'i' || representation[index] === 'I') {
return new Decimal128(Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
} else if (representation[index] === 'N') {
return new Decimal128(Buffer.from(NAN_BUFFER));
}
}
// Read all the digits
while (isDigit(representation[index]) || representation[index] === '.') {
if (representation[index] === '.') {
if (sawRadix) invalidErr(representation, 'contains multiple periods');
sawRadix = true;
index = index + 1;
continue;
}
if (nDigitsStored < 34) {
if (representation[index] !== '0' || foundNonZero) {
if (!foundNonZero) {
firstNonZero = nDigitsRead;
}
foundNonZero = true;
// Only store 34 digits
digits[digitsInsert++] = parseInt(representation[index], 10);
nDigitsStored = nDigitsStored + 1;
}
}
if (foundNonZero) nDigits = nDigits + 1;
if (sawRadix) radixPosition = radixPosition + 1;
nDigitsRead = nDigitsRead + 1;
index = index + 1;
}
if (sawRadix && !nDigitsRead)
throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
// Read exponent if exists
if (representation[index] === 'e' || representation[index] === 'E') {
// Read exponent digits
const match = representation.substr(++index).match(EXPONENT_REGEX);
// No digits read
if (!match || !match[2]) return new Decimal128(Buffer.from(NAN_BUFFER));
// Get exponent
exponent = parseInt(match[0], 10);
// Adjust the index
index = index + match[0].length;
}
// Return not a number
if (representation[index]) return new Decimal128(Buffer.from(NAN_BUFFER));
// Done reading input
// Find first non-zero digit in digits
firstDigit = 0;
if (!nDigitsStored) {
firstDigit = 0;
lastDigit = 0;
digits[0] = 0;
nDigits = 1;
nDigitsStored = 1;
significantDigits = 0;
} else {
lastDigit = nDigitsStored - 1;
significantDigits = nDigits;
if (significantDigits !== 1) {
while (digits[firstNonZero + significantDigits - 1] === 0) {
significantDigits = significantDigits - 1;
}
}
}
// Normalization of exponent
// Correct exponent based on radix position, and shift significand as needed
// to represent user input
// Overflow prevention
if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
exponent = EXPONENT_MIN;
} else {
exponent = exponent - radixPosition;
}
// Attempt to normalize the exponent
while (exponent > EXPONENT_MAX) {
// Shift exponent to significand and decrease
lastDigit = lastDigit + 1;
if (lastDigit - firstDigit > MAX_DIGITS) {
// Check if we have a zero then just hard clamp, otherwise fail
const digitsString = digits.join('');
if (digitsString.match(/^0+$/)) {
exponent = EXPONENT_MAX;
break;
}
invalidErr(representation, 'overflow');
}
exponent = exponent - 1;
}
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
// Shift last digit. can only do this if < significant digits than # stored.
if (lastDigit === 0 && significantDigits < nDigitsStored) {
exponent = EXPONENT_MIN;
significantDigits = 0;
break;
}
if (nDigitsStored < nDigits) {
// adjust to match digits not stored
nDigits = nDigits - 1;
} else {
// adjust to round
lastDigit = lastDigit - 1;
}
if (exponent < EXPONENT_MAX) {
exponent = exponent + 1;
} else {
// Check if we have a zero then just hard clamp, otherwise fail
const digitsString = digits.join('');
if (digitsString.match(/^0+$/)) {
exponent = EXPONENT_MAX;
break;
}
invalidErr(representation, 'overflow');
}
}
// Round
// We've normalized the exponent, but might still need to round.
if (lastDigit - firstDigit + 1 < significantDigits) {
let endOfString = nDigitsRead;
// If we have seen a radix point, 'string' is 1 longer than we have
// documented with ndigits_read, so inc the position of the first nonzero
// digit and the position that digits are read to.
if (sawRadix) {
firstNonZero = firstNonZero + 1;
endOfString = endOfString + 1;
}
// if negative, we need to increment again to account for - sign at start.
if (isNegative) {
firstNonZero = firstNonZero + 1;
endOfString = endOfString + 1;
}
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
let roundBit = 0;
if (roundDigit >= 5) {
roundBit = 1;
if (roundDigit === 5) {
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
if (parseInt(representation[i], 10)) {
roundBit = 1;
break;
}
}
}
}
if (roundBit) {
let dIdx = lastDigit;
for (; dIdx >= 0; dIdx--) {
if (++digits[dIdx] > 9) {
digits[dIdx] = 0;
// overflowed most significant digit
if (dIdx === 0) {
if (exponent < EXPONENT_MAX) {
exponent = exponent + 1;
digits[dIdx] = 1;
} else {
return new Decimal128(
Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
);
}
}
}
}
}
}
// Encode significand
// The high 17 digits of the significand
significandHigh = Long.fromNumber(0);
// The low 17 digits of the significand
significandLow = Long.fromNumber(0);
// read a zero
if (significantDigits === 0) {
significandHigh = Long.fromNumber(0);
significandLow = Long.fromNumber(0);
} else if (lastDigit - firstDigit < 17) {
let dIdx = firstDigit;
significandLow = Long.fromNumber(digits[dIdx++]);
significandHigh = new Long(0, 0);
for (; dIdx <= lastDigit; dIdx++) {
significandLow = significandLow.multiply(Long.fromNumber(10));
significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
}
} else {
let dIdx = firstDigit;
significandHigh = Long.fromNumber(digits[dIdx++]);
for (; dIdx <= lastDigit - 17; dIdx++) {
significandHigh = significandHigh.multiply(Long.fromNumber(10));
significandHigh = significandHigh.add(Long.fromNumber(digits[dIdx]));
}
significandLow = Long.fromNumber(digits[dIdx++]);
for (; dIdx <= lastDigit; dIdx++) {
significandLow = significandLow.multiply(Long.fromNumber(10));
significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
}
}
const significand = multiply64x2(significandHigh, Long.fromString('100000000000000000'));
significand.low = significand.low.add(significandLow);
if (lessThan(significand.low, significandLow)) {
significand.high = significand.high.add(Long.fromNumber(1));
}
// Biased exponent
biasedExponent = exponent + EXPONENT_BIAS;
const dec = { low: Long.fromNumber(0), high: Long.fromNumber(0) };
// Encode combination, exponent, and significand.
if (
significand.high.shiftRightUnsigned(49).and(Long.fromNumber(1)).equals(Long.fromNumber(1))
) {
// Encode '11' into bits 1 to 3
dec.high = dec.high.or(Long.fromNumber(0x3).shiftLeft(61));
dec.high = dec.high.or(
Long.fromNumber(biasedExponent).and(Long.fromNumber(0x3fff).shiftLeft(47))
);
dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x7fffffffffff)));
} else {
dec.high = dec.high.or(Long.fromNumber(biasedExponent & 0x3fff).shiftLeft(49));
dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x1ffffffffffff)));
}
dec.low = significand.low;
// Encode sign
if (isNegative) {
dec.high = dec.high.or(Long.fromString('9223372036854775808'));
}
// Encode into a buffer
const buffer = Buffer.alloc(16);
index = 0;
// Encode the low 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.low.low & 0xff;
buffer[index++] = (dec.low.low >> 8) & 0xff;
buffer[index++] = (dec.low.low >> 16) & 0xff;
buffer[index++] = (dec.low.low >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.low.high & 0xff;
buffer[index++] = (dec.low.high >> 8) & 0xff;
buffer[index++] = (dec.low.high >> 16) & 0xff;
buffer[index++] = (dec.low.high >> 24) & 0xff;
// Encode the high 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.high.low & 0xff;
buffer[index++] = (dec.high.low >> 8) & 0xff;
buffer[index++] = (dec.high.low >> 16) & 0xff;
buffer[index++] = (dec.high.low >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.high.high & 0xff;
buffer[index++] = (dec.high.high >> 8) & 0xff;
buffer[index++] = (dec.high.high >> 16) & 0xff;
buffer[index++] = (dec.high.high >> 24) & 0xff;
// Return the new Decimal128
return new Decimal128(buffer);
}
/** Create a string representation of the raw Decimal128 value */
toString(): string {
// Note: bits in this routine are referred to starting at 0,
// from the sign bit, towards the coefficient.
// decoded biased exponent (14 bits)
let biased_exponent;
// the number of significand digits
let significand_digits = 0;
// the base-10 digits in the significand
const significand = new Array<number>(36);
for (let i = 0; i < significand.length; i++) significand[i] = 0;
// read pointer into significand
let index = 0;
// true if the number is zero
let is_zero = false;
// the most significant significand bits (50-46)
let significand_msb;
// temporary storage for significand decoding
let significand128: { parts: [number, number, number, number] } = { parts: [0, 0, 0, 0] };
// indexing variables
let j, k;
// Output string
const string: string[] = [];
// Unpack index
index = 0;
// Buffer reference
const buffer = this.bytes;
// Unpack the low 64bits into a long
// bits 96 - 127
const low =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// bits 64 - 95
const midl =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Unpack the high 64bits into a long
// bits 32 - 63
const midh =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// bits 0 - 31
const high =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Unpack index
index = 0;
// Create the state of the decimal
const dec = {
low: new Long(low, midl),
high: new Long(midh, high)
};
if (dec.high.lessThan(Long.ZERO)) {
string.push('-');
}
// Decode combination field and exponent
// bits 1 - 5
const combination = (high >> 26) & COMBINATION_MASK;
if (combination >> 3 === 3) {
// Check for 'special' values
if (combination === COMBINATION_INFINITY) {
return string.join('') + 'Infinity';
} else if (combination === COMBINATION_NAN) {
return 'NaN';
} else {
biased_exponent = (high >> 15) & EXPONENT_MASK;
significand_msb = 0x08 + ((high >> 14) & 0x01);
}
} else {
significand_msb = (high >> 14) & 0x07;
biased_exponent = (high >> 17) & EXPONENT_MASK;
}
// unbiased exponent
const exponent = biased_exponent - EXPONENT_BIAS;
// Create string of significand digits
// Convert the 114-bit binary number represented by
// (significand_high, significand_low) to at most 34 decimal
// digits through modulo and division.
significand128.parts[0] = (high & 0x3fff) + ((significand_msb & 0xf) << 14);
significand128.parts[1] = midh;
significand128.parts[2] = midl;
significand128.parts[3] = low;
if (
significand128.parts[0] === 0 &&
significand128.parts[1] === 0 &&
significand128.parts[2] === 0 &&
significand128.parts[3] === 0
) {
is_zero = true;
} else {
for (k = 3; k >= 0; k--) {
let least_digits = 0;
// Perform the divide
const result = divideu128(significand128);
significand128 = result.quotient;
least_digits = result.rem.low;
// We now have the 9 least significant digits (in base 2).
// Convert and output to string.
if (!least_digits) continue;
for (j = 8; j >= 0; j--) {
// significand[k * 9 + j] = Math.round(least_digits % 10);
significand[k * 9 + j] = least_digits % 10;
// least_digits = Math.round(least_digits / 10);
least_digits = Math.floor(least_digits / 10);
}
}
}
// Output format options:
// Scientific - [-]d.dddE(+/-)dd or [-]dE(+/-)dd
// Regular - ddd.ddd
if (is_zero) {
significand_digits = 1;
significand[index] = 0;
} else {
significand_digits = 36;
while (!significand[index]) {
significand_digits = significand_digits - 1;
index = index + 1;
}
}
// the exponent if scientific notation is used
const scientific_exponent = significand_digits - 1 + exponent;
// The scientific exponent checks are dictated by the string conversion
// specification and are somewhat arbitrary cutoffs.
//
// We must check exponent > 0, because if this is the case, the number
// has trailing zeros. However, we *cannot* output these trailing zeros,
// because doing so would change the precision of the value, and would
// change stored data if the string converted number is round tripped.
if (scientific_exponent >= 34 || scientific_exponent <= -7 || exponent > 0) {
// Scientific format
// if there are too many significant digits, we should just be treating numbers
// as + or - 0 and using the non-scientific exponent (this is for the "invalid
// representation should be treated as 0/-0" spec cases in decimal128-1.json)
if (significand_digits > 34) {
string.push(`${0}`);
if (exponent > 0) string.push(`E+${exponent}`);
else if (exponent < 0) string.push(`E${exponent}`);
return string.join('');
}
string.push(`${significand[index++]}`);
significand_digits = significand_digits - 1;
if (significand_digits) {
string.push('.');
}
for (let i = 0; i < significand_digits; i++) {
string.push(`${significand[index++]}`);
}
// Exponent
string.push('E');
if (scientific_exponent > 0) {
string.push(`+${scientific_exponent}`);
} else {
string.push(`${scientific_exponent}`);
}
} else {
// Regular format with no decimal place
if (exponent >= 0) {
for (let i = 0; i < significand_digits; i++) {
string.push(`${significand[index++]}`);
}
} else {
let radix_position = significand_digits + exponent;
// non-zero digits before radix
if (radix_position > 0) {
for (let i = 0; i < radix_position; i++) {
string.push(`${significand[index++]}`);
}
} else {
string.push('0');
}
string.push('.');
// add leading zeros after radix
while (radix_position++ < 0) {
string.push('0');
}
for (let i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) {
string.push(`${significand[index++]}`);
}
}
}
return string.join('');
}
toJSON(): Decimal128Extended {
return { $numberDecimal: this.toString() };
}
/** @internal */
toExtendedJSON(): Decimal128Extended {
return { $numberDecimal: this.toString() };
}
/** @internal */
static fromExtendedJSON(doc: Decimal128Extended): Decimal128 {
return Decimal128.fromString(doc.$numberDecimal);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new Decimal128("${this.toString()}")`;
}
}
Object.defineProperty(Decimal128.prototype, '_bsontype', { value: 'Decimal128' });

View File

@@ -0,0 +1,83 @@
import type { EJSONOptions } from './extended_json';
/** @public */
export interface DoubleExtended {
$numberDouble: string;
}
/**
* A class representation of the BSON Double type.
* @public
* @category BSONType
*/
export class Double {
_bsontype!: 'Double';
value!: number;
/**
* Create a Double type
*
* @param value - the number we want to represent as a double.
*/
constructor(value: number) {
if (!(this instanceof Double)) return new Double(value);
if ((value as unknown) instanceof Number) {
value = value.valueOf();
}
this.value = +value;
}
/**
* Access the number value.
*
* @returns returns the wrapped double number.
*/
valueOf(): number {
return this.value;
}
toJSON(): number {
return this.value;
}
toString(radix?: number): string {
return this.value.toString(radix);
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): number | DoubleExtended {
if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) {
return this.value;
}
if (Object.is(Math.sign(this.value), -0)) {
// NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
// explicitly provided `-0` then we need to ensure the sign makes it into the output
return { $numberDouble: `-${this.value.toFixed(1)}` };
}
return {
$numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
};
}
/** @internal */
static fromExtendedJSON(doc: DoubleExtended, options?: EJSONOptions): number | Double {
const doubleValue = parseFloat(doc.$numberDouble);
return options && options.relaxed ? doubleValue : new Double(doubleValue);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
const eJSON = this.toExtendedJSON() as DoubleExtended;
return `new Double(${eJSON.$numberDouble})`;
}
}
Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });

View File

@@ -0,0 +1,27 @@
import { Buffer } from 'buffer';
import { BSONTypeError } from './error';
import { isAnyArrayBuffer } from './parser/utils';
/**
* Makes sure that, if a Uint8Array is passed in, it is wrapped in a Buffer.
*
* @param potentialBuffer - The potential buffer
* @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
* wraps a passed in Uint8Array
* @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
*/
export function ensureBuffer(potentialBuffer: Buffer | ArrayBufferView | ArrayBuffer): Buffer {
if (ArrayBuffer.isView(potentialBuffer)) {
return Buffer.from(
potentialBuffer.buffer,
potentialBuffer.byteOffset,
potentialBuffer.byteLength
);
}
if (isAnyArrayBuffer(potentialBuffer)) {
return Buffer.from(potentialBuffer);
}
throw new BSONTypeError('Must use either Buffer or TypedArray');
}

View File

@@ -0,0 +1,23 @@
/** @public */
export class BSONError extends Error {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, BSONError.prototype);
}
get name(): string {
return 'BSONError';
}
}
/** @public */
export class BSONTypeError extends TypeError {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, BSONTypeError.prototype);
}
get name(): string {
return 'BSONTypeError';
}
}

View File

@@ -0,0 +1,462 @@
import { Binary } from './binary';
import type { Document } from './bson';
import { Code } from './code';
import { DBRef, isDBRefLike } from './db_ref';
import { Decimal128 } from './decimal128';
import { Double } from './double';
import { BSONError, BSONTypeError } from './error';
import { Int32 } from './int_32';
import { Long } from './long';
import { MaxKey } from './max_key';
import { MinKey } from './min_key';
import { ObjectId } from './objectid';
import { isDate, isObjectLike, isRegExp } from './parser/utils';
import { BSONRegExp } from './regexp';
import { BSONSymbol } from './symbol';
import { Timestamp } from './timestamp';
/** @public */
export type EJSONOptions = EJSON.Options;
/** @internal */
type BSONType =
| Binary
| Code
| DBRef
| Decimal128
| Double
| Int32
| Long
| MaxKey
| MinKey
| ObjectId
| BSONRegExp
| BSONSymbol
| Timestamp;
export function isBSONType(value: unknown): value is BSONType {
return (
isObjectLike(value) && Reflect.has(value, '_bsontype') && typeof value._bsontype === 'string'
);
}
// INT32 boundaries
const BSON_INT32_MAX = 0x7fffffff;
const BSON_INT32_MIN = -0x80000000;
// INT64 boundaries
// const BSON_INT64_MAX = 0x7fffffffffffffff; // TODO(NODE-4377): This number cannot be precisely represented in JS
const BSON_INT64_MAX = 0x8000000000000000;
const BSON_INT64_MIN = -0x8000000000000000;
// all the types where we don't need to do any special processing and can just pass the EJSON
//straight to type.fromExtendedJSON
const keysToCodecs = {
$oid: ObjectId,
$binary: Binary,
$uuid: Binary,
$symbol: BSONSymbol,
$numberInt: Int32,
$numberDecimal: Decimal128,
$numberDouble: Double,
$numberLong: Long,
$minKey: MinKey,
$maxKey: MaxKey,
$regex: BSONRegExp,
$regularExpression: BSONRegExp,
$timestamp: Timestamp
} as const;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function deserializeValue(value: any, options: EJSON.Options = {}) {
if (typeof value === 'number') {
if (options.relaxed || options.legacy) {
return value;
}
// if it's an integer, should interpret as smallest BSON integer
// that can represent it exactly. (if out of range, interpret as double.)
if (Math.floor(value) === value) {
if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) return new Int32(value);
if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) return Long.fromNumber(value);
}
// If the number is a non-integer or out of integer range, should interpret as BSON Double.
return new Double(value);
}
// from here on out we're looking for bson types, so bail if its not an object
if (value == null || typeof value !== 'object') return value;
// upgrade deprecated undefined to null
if (value.$undefined) return null;
const keys = Object.keys(value).filter(
k => k.startsWith('$') && value[k] != null
) as (keyof typeof keysToCodecs)[];
for (let i = 0; i < keys.length; i++) {
const c = keysToCodecs[keys[i]];
if (c) return c.fromExtendedJSON(value, options);
}
if (value.$date != null) {
const d = value.$date;
const date = new Date();
if (options.legacy) {
if (typeof d === 'number') date.setTime(d);
else if (typeof d === 'string') date.setTime(Date.parse(d));
} else {
if (typeof d === 'string') date.setTime(Date.parse(d));
else if (Long.isLong(d)) date.setTime(d.toNumber());
else if (typeof d === 'number' && options.relaxed) date.setTime(d);
}
return date;
}
if (value.$code != null) {
const copy = Object.assign({}, value);
if (value.$scope) {
copy.$scope = deserializeValue(value.$scope);
}
return Code.fromExtendedJSON(value);
}
if (isDBRefLike(value) || value.$dbPointer) {
const v = value.$ref ? value : value.$dbPointer;
// we run into this in a "degenerate EJSON" case (with $id and $ref order flipped)
// because of the order JSON.parse goes through the document
if (v instanceof DBRef) return v;
const dollarKeys = Object.keys(v).filter(k => k.startsWith('$'));
let valid = true;
dollarKeys.forEach(k => {
if (['$ref', '$id', '$db'].indexOf(k) === -1) valid = false;
});
// only make DBRef if $ keys are all valid
if (valid) return DBRef.fromExtendedJSON(v);
}
return value;
}
type EJSONSerializeOptions = EJSON.Options & {
seenObjects: { obj: unknown; propertyName: string }[];
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeArray(array: any[], options: EJSONSerializeOptions): any[] {
return array.map((v: unknown, index: number) => {
options.seenObjects.push({ propertyName: `index ${index}`, obj: null });
try {
return serializeValue(v, options);
} finally {
options.seenObjects.pop();
}
});
}
function getISOString(date: Date) {
const isoStr = date.toISOString();
// we should only show milliseconds in timestamp if they're non-zero
return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z';
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeValue(value: any, options: EJSONSerializeOptions): any {
if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
const index = options.seenObjects.findIndex(entry => entry.obj === value);
if (index !== -1) {
const props = options.seenObjects.map(entry => entry.propertyName);
const leadingPart = props
.slice(0, index)
.map(prop => `${prop} -> `)
.join('');
const alreadySeen = props[index];
const circularPart =
' -> ' +
props
.slice(index + 1, props.length - 1)
.map(prop => `${prop} -> `)
.join('');
const current = props[props.length - 1];
const leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
const dashes = '-'.repeat(
circularPart.length + (alreadySeen.length + current.length) / 2 - 1
);
throw new BSONTypeError(
'Converting circular structure to EJSON:\n' +
` ${leadingPart}${alreadySeen}${circularPart}${current}\n` +
` ${leadingSpace}\\${dashes}/`
);
}
options.seenObjects[options.seenObjects.length - 1].obj = value;
}
if (Array.isArray(value)) return serializeArray(value, options);
if (value === undefined) return null;
if (value instanceof Date || isDate(value)) {
const dateNum = value.getTime(),
// is it in year range 1970-9999?
inRange = dateNum > -1 && dateNum < 253402318800000;
if (options.legacy) {
return options.relaxed && inRange
? { $date: value.getTime() }
: { $date: getISOString(value) };
}
return options.relaxed && inRange
? { $date: getISOString(value) }
: { $date: { $numberLong: value.getTime().toString() } };
}
if (typeof value === 'number' && (!options.relaxed || !isFinite(value))) {
// it's an integer
if (Math.floor(value) === value) {
const int32Range = value >= BSON_INT32_MIN && value <= BSON_INT32_MAX,
int64Range = value >= BSON_INT64_MIN && value <= BSON_INT64_MAX;
// interpret as being of the smallest BSON integer type that can represent the number exactly
if (int32Range) return { $numberInt: value.toString() };
if (int64Range) return { $numberLong: value.toString() };
}
return { $numberDouble: value.toString() };
}
if (value instanceof RegExp || isRegExp(value)) {
let flags = value.flags;
if (flags === undefined) {
const match = value.toString().match(/[gimuy]*$/);
if (match) {
flags = match[0];
}
}
const rx = new BSONRegExp(value.source, flags);
return rx.toExtendedJSON(options);
}
if (value != null && typeof value === 'object') return serializeDocument(value, options);
return value;
}
const BSON_TYPE_MAPPINGS = {
Binary: (o: Binary) => new Binary(o.value(), o.sub_type),
Code: (o: Code) => new Code(o.code, o.scope),
DBRef: (o: DBRef) => new DBRef(o.collection || o.namespace, o.oid, o.db, o.fields), // "namespace" for 1.x library backwards compat
Decimal128: (o: Decimal128) => new Decimal128(o.bytes),
Double: (o: Double) => new Double(o.value),
Int32: (o: Int32) => new Int32(o.value),
Long: (
o: Long & {
low_: number;
high_: number;
unsigned_: boolean | undefined;
}
) =>
Long.fromBits(
// underscore variants for 1.x backwards compatibility
o.low != null ? o.low : o.low_,
o.low != null ? o.high : o.high_,
o.low != null ? o.unsigned : o.unsigned_
),
MaxKey: () => new MaxKey(),
MinKey: () => new MinKey(),
ObjectID: (o: ObjectId) => new ObjectId(o),
ObjectId: (o: ObjectId) => new ObjectId(o), // support 4.0.0/4.0.1 before _bsontype was reverted back to ObjectID
BSONRegExp: (o: BSONRegExp) => new BSONRegExp(o.pattern, o.options),
Symbol: (o: BSONSymbol) => new BSONSymbol(o.value),
Timestamp: (o: Timestamp) => Timestamp.fromBits(o.low, o.high)
} as const;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeDocument(doc: any, options: EJSONSerializeOptions) {
if (doc == null || typeof doc !== 'object') throw new BSONError('not an object instance');
const bsontype: BSONType['_bsontype'] = doc._bsontype;
if (typeof bsontype === 'undefined') {
// It's a regular object. Recursively serialize its property values.
const _doc: Document = {};
for (const name in doc) {
options.seenObjects.push({ propertyName: name, obj: null });
try {
const value = serializeValue(doc[name], options);
if (name === '__proto__') {
Object.defineProperty(_doc, name, {
value,
writable: true,
enumerable: true,
configurable: true
});
} else {
_doc[name] = value;
}
} finally {
options.seenObjects.pop();
}
}
return _doc;
} else if (isBSONType(doc)) {
// the "document" is really just a BSON type object
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let outDoc: any = doc;
if (typeof outDoc.toExtendedJSON !== 'function') {
// There's no EJSON serialization function on the object. It's probably an
// object created by a previous version of this library (or another library)
// that's duck-typing objects to look like they were generated by this library).
// Copy the object into this library's version of that type.
const mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
if (!mapper) {
throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
}
outDoc = mapper(outDoc);
}
// Two BSON types may have nested objects that may need to be serialized too
if (bsontype === 'Code' && outDoc.scope) {
outDoc = new Code(outDoc.code, serializeValue(outDoc.scope, options));
} else if (bsontype === 'DBRef' && outDoc.oid) {
outDoc = new DBRef(
serializeValue(outDoc.collection, options),
serializeValue(outDoc.oid, options),
serializeValue(outDoc.db, options),
serializeValue(outDoc.fields, options)
);
}
return outDoc.toExtendedJSON(options);
} else {
throw new BSONError('_bsontype must be a string, but was: ' + typeof bsontype);
}
}
/**
* EJSON parse / stringify API
* @public
*/
// the namespace here is used to emulate `export * as EJSON from '...'`
// which as of now (sept 2020) api-extractor does not support
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace EJSON {
export interface Options {
/** Output using the Extended JSON v1 spec */
legacy?: boolean;
/** Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types */
relaxed?: boolean;
/**
* Disable Extended JSON's `relaxed` mode, which attempts to return BSON types where possible, rather than native JS types
* @deprecated Please use the relaxed property instead
*/
strict?: boolean;
}
/**
* Parse an Extended JSON string, constructing the JavaScript value or object described by that
* string.
*
* @example
* ```js
* const { EJSON } = require('bson');
* const text = '{ "int32": { "$numberInt": "10" } }';
*
* // prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
* console.log(EJSON.parse(text, { relaxed: false }));
*
* // prints { int32: 10 }
* console.log(EJSON.parse(text));
* ```
*/
export function parse(text: string, options?: EJSON.Options): SerializableTypes {
const finalOptions = Object.assign({}, { relaxed: true, legacy: false }, options);
// relaxed implies not strict
if (typeof finalOptions.relaxed === 'boolean') finalOptions.strict = !finalOptions.relaxed;
if (typeof finalOptions.strict === 'boolean') finalOptions.relaxed = !finalOptions.strict;
return JSON.parse(text, (key, value) => {
if (key.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Document field names cannot contain null bytes, found: ${JSON.stringify(key)}`
);
}
return deserializeValue(value, finalOptions);
});
}
export type JSONPrimitive = string | number | boolean | null;
export type SerializableTypes = Document | Array<JSONPrimitive | Document> | JSONPrimitive;
/**
* Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
* function is specified or optionally including only the specified properties if a replacer array
* is specified.
*
* @param value - The value to convert to extended JSON
* @param replacer - 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
* @param space - A String or Number object that's used to insert white space into the output JSON string for readability purposes.
* @param options - Optional settings
*
* @example
* ```js
* const { EJSON } = require('bson');
* const Int32 = require('mongodb').Int32;
* const doc = { int32: new Int32(10) };
*
* // prints '{"int32":{"$numberInt":"10"}}'
* console.log(EJSON.stringify(doc, { relaxed: false }));
*
* // prints '{"int32":10}'
* console.log(EJSON.stringify(doc));
* ```
*/
export function stringify(
value: SerializableTypes,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | EJSON.Options,
space?: string | number,
options?: EJSON.Options
): string {
if (space != null && typeof space === 'object') {
options = space;
space = 0;
}
if (replacer != null && typeof replacer === 'object' && !Array.isArray(replacer)) {
options = replacer;
replacer = undefined;
space = 0;
}
const serializeOptions = Object.assign({ relaxed: true, legacy: false }, options, {
seenObjects: [{ propertyName: '(root)', obj: null }]
});
const doc = serializeValue(value, serializeOptions);
return JSON.stringify(doc, replacer as Parameters<JSON['stringify']>[1], space);
}
/**
* Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
*
* @param value - The object to serialize
* @param options - Optional settings passed to the `stringify` function
*/
export function serialize(value: SerializableTypes, options?: EJSON.Options): Document {
options = options || {};
return JSON.parse(stringify(value, options));
}
/**
* Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
*
* @param ejson - The Extended JSON object to deserialize
* @param options - Optional settings passed to the parse method
*/
export function deserialize(ejson: Document, options?: EJSON.Options): SerializableTypes {
options = options || {};
return parse(JSON.stringify(ejson), options);
}
}

View File

@@ -0,0 +1,70 @@
import type { EJSONOptions } from './extended_json';
/** @public */
export interface Int32Extended {
$numberInt: string;
}
/**
* A class representation of a BSON Int32 type.
* @public
* @category BSONType
*/
export class Int32 {
_bsontype!: 'Int32';
value!: number;
/**
* Create an Int32 type
*
* @param value - the number we want to represent as an int32.
*/
constructor(value: number | string) {
if (!(this instanceof Int32)) return new Int32(value);
if ((value as unknown) instanceof Number) {
value = value.valueOf();
}
this.value = +value | 0;
}
/**
* Access the number value.
*
* @returns returns the wrapped int32 number.
*/
valueOf(): number {
return this.value;
}
toString(radix?: number): string {
return this.value.toString(radix);
}
toJSON(): number {
return this.value;
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): number | Int32Extended {
if (options && (options.relaxed || options.legacy)) return this.value;
return { $numberInt: this.value.toString() };
}
/** @internal */
static fromExtendedJSON(doc: Int32Extended, options?: EJSONOptions): number | Int32 {
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new Int32(${this.valueOf()})`;
}
}
Object.defineProperty(Int32.prototype, '_bsontype', { value: 'Int32' });

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,119 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// We have an ES6 Map available, return the native instance
import { getGlobal } from './utils/global';
/** @public */
let bsonMap: MapConstructor;
const bsonGlobal = getGlobal<{ Map?: MapConstructor }>();
if (bsonGlobal.Map) {
bsonMap = bsonGlobal.Map;
} else {
// We will return a polyfill
bsonMap = class Map {
private _keys: string[];
private _values: Record<string, any>;
constructor(array: [string, any][] = []) {
this._keys = [];
this._values = {};
for (let i = 0; i < array.length; i++) {
if (array[i] == null) continue; // skip null and undefined
const entry = array[i];
const key = entry[0];
const value = entry[1];
// Add the key to the list of keys in order
this._keys.push(key);
// Add the key and value to the values dictionary with a point
// to the location in the ordered keys list
this._values[key] = { v: value, i: this._keys.length - 1 };
}
}
clear() {
this._keys = [];
this._values = {};
}
delete(key: string) {
const value = this._values[key];
if (value == null) return false;
// Delete entry
delete this._values[key];
// Remove the key from the ordered keys list
this._keys.splice(value.i, 1);
return true;
}
entries() {
let index = 0;
return {
next: () => {
const key = this._keys[index++];
return {
value: key !== undefined ? [key, this._values[key].v] : undefined,
done: key !== undefined ? false : true
};
}
};
}
forEach(callback: (this: this, value: any, key: string, self: this) => void, self?: this) {
self = self || this;
for (let i = 0; i < this._keys.length; i++) {
const key = this._keys[i];
// Call the forEach callback
callback.call(self, this._values[key].v, key, self);
}
}
get(key: string) {
return this._values[key] ? this._values[key].v : undefined;
}
has(key: string) {
return this._values[key] != null;
}
keys() {
let index = 0;
return {
next: () => {
const key = this._keys[index++];
return {
value: key !== undefined ? key : undefined,
done: key !== undefined ? false : true
};
}
};
}
set(key: string, value: any) {
if (this._values[key]) {
this._values[key].v = value;
return this;
}
// Add the key to the list of keys in order
this._keys.push(key);
// Add the key and value to the values dictionary with a point
// to the location in the ordered keys list
this._values[key] = { v: value, i: this._keys.length - 1 };
return this;
}
values() {
let index = 0;
return {
next: () => {
const key = this._keys[index++];
return {
value: key !== undefined ? this._values[key].v : undefined,
done: key !== undefined ? false : true
};
}
};
}
get size() {
return this._keys.length;
}
} as unknown as MapConstructor;
}
export { bsonMap as Map };

View File

@@ -0,0 +1,38 @@
/** @public */
export interface MaxKeyExtended {
$maxKey: 1;
}
/**
* A class representation of the BSON MaxKey type.
* @public
* @category BSONType
*/
export class MaxKey {
_bsontype!: 'MaxKey';
constructor() {
if (!(this instanceof MaxKey)) return new MaxKey();
}
/** @internal */
toExtendedJSON(): MaxKeyExtended {
return { $maxKey: 1 };
}
/** @internal */
static fromExtendedJSON(): MaxKey {
return new MaxKey();
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return 'new MaxKey()';
}
}
Object.defineProperty(MaxKey.prototype, '_bsontype', { value: 'MaxKey' });

View File

@@ -0,0 +1,38 @@
/** @public */
export interface MinKeyExtended {
$minKey: 1;
}
/**
* A class representation of the BSON MinKey type.
* @public
* @category BSONType
*/
export class MinKey {
_bsontype!: 'MinKey';
constructor() {
if (!(this instanceof MinKey)) return new MinKey();
}
/** @internal */
toExtendedJSON(): MinKeyExtended {
return { $minKey: 1 };
}
/** @internal */
static fromExtendedJSON(): MinKey {
return new MinKey();
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return 'new MinKey()';
}
}
Object.defineProperty(MinKey.prototype, '_bsontype', { value: 'MinKey' });

View File

@@ -0,0 +1,354 @@
import { Buffer } from 'buffer';
import { ensureBuffer } from './ensure_buffer';
import { BSONTypeError } from './error';
import { deprecate, isUint8Array, randomBytes } from './parser/utils';
// Regular expression that checks for hex value
const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
// Unique sequence for the current process (initialized on first use)
let PROCESS_UNIQUE: Uint8Array | null = null;
/** @public */
export interface ObjectIdLike {
id: string | Buffer;
__id?: string;
toHexString(): string;
}
/** @public */
export interface ObjectIdExtended {
$oid: string;
}
const kId = Symbol('id');
/**
* A class representation of the BSON ObjectId type.
* @public
* @category BSONType
*/
export class ObjectId {
_bsontype!: 'ObjectID';
/** @internal */
static index = Math.floor(Math.random() * 0xffffff);
static cacheHexString: boolean;
/** ObjectId Bytes @internal */
private [kId]!: Buffer;
/** ObjectId hexString cache @internal */
private __id?: string;
/**
* Create an ObjectId type
*
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
*/
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array) {
if (!(this instanceof ObjectId)) return new ObjectId(inputId);
// workingId is set based on type of input and whether valid id exists for the input
let workingId;
if (typeof inputId === 'object' && inputId && 'id' in inputId) {
if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
throw new BSONTypeError(
'Argument passed in must have an id that is of type string or Buffer'
);
}
if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
workingId = Buffer.from(inputId.toHexString(), 'hex');
} else {
workingId = inputId.id;
}
} else {
workingId = inputId;
}
// the following cases use workingId to construct an ObjectId
if (workingId == null || typeof workingId === 'number') {
// The most common use case (blank id, new objectId instance)
// Generate a new id
this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
this[kId] = workingId instanceof Buffer ? workingId : ensureBuffer(workingId);
} else if (typeof workingId === 'string') {
if (workingId.length === 12) {
const bytes = Buffer.from(workingId);
if (bytes.byteLength === 12) {
this[kId] = bytes;
} else {
throw new BSONTypeError('Argument passed in must be a string of 12 bytes');
}
} else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
this[kId] = Buffer.from(workingId, 'hex');
} else {
throw new BSONTypeError(
'Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer'
);
}
} else {
throw new BSONTypeError('Argument passed in does not match the accepted types');
}
// If we are caching the hex string
if (ObjectId.cacheHexString) {
this.__id = this.id.toString('hex');
}
}
/**
* The ObjectId bytes
* @readonly
*/
get id(): Buffer {
return this[kId];
}
set id(value: Buffer) {
this[kId] = value;
if (ObjectId.cacheHexString) {
this.__id = value.toString('hex');
}
}
/**
* The generation time of this ObjectId instance
* @deprecated Please use getTimestamp / createFromTime which returns an int32 epoch
*/
get generationTime(): number {
return this.id.readInt32BE(0);
}
set generationTime(value: number) {
// Encode time into first 4 bytes
this.id.writeUInt32BE(value, 0);
}
/** Returns the ObjectId id as a 24 character hex string representation */
toHexString(): string {
if (ObjectId.cacheHexString && this.__id) {
return this.__id;
}
const hexString = this.id.toString('hex');
if (ObjectId.cacheHexString && !this.__id) {
this.__id = hexString;
}
return hexString;
}
/**
* Update the ObjectId index
* @privateRemarks
* Used in generating new ObjectId's on the driver
* @internal
*/
static getInc(): number {
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
}
/**
* Generate a 12 byte id buffer used in ObjectId's
*
* @param time - pass in a second based timestamp.
*/
static generate(time?: number): Buffer {
if ('number' !== typeof time) {
time = Math.floor(Date.now() / 1000);
}
const inc = ObjectId.getInc();
const buffer = Buffer.alloc(12);
// 4-byte timestamp
buffer.writeUInt32BE(time, 0);
// set PROCESS_UNIQUE if yet not initialized
if (PROCESS_UNIQUE === null) {
PROCESS_UNIQUE = randomBytes(5);
}
// 5-byte process unique
buffer[4] = PROCESS_UNIQUE[0];
buffer[5] = PROCESS_UNIQUE[1];
buffer[6] = PROCESS_UNIQUE[2];
buffer[7] = PROCESS_UNIQUE[3];
buffer[8] = PROCESS_UNIQUE[4];
// 3-byte counter
buffer[11] = inc & 0xff;
buffer[10] = (inc >> 8) & 0xff;
buffer[9] = (inc >> 16) & 0xff;
return buffer;
}
/**
* Converts the id into a 24 character hex string for printing
*
* @param format - The Buffer toString format parameter.
*/
toString(format?: string): string {
// Is the id a buffer then use the buffer toString method to return the format
if (format) return this.id.toString(format);
return this.toHexString();
}
/** Converts to its JSON the 24 character hex string representation. */
toJSON(): string {
return this.toHexString();
}
/**
* Compares the equality of this ObjectId with `otherID`.
*
* @param otherId - ObjectId instance to compare against.
*/
equals(otherId: string | ObjectId | ObjectIdLike): boolean {
if (otherId === undefined || otherId === null) {
return false;
}
if (otherId instanceof ObjectId) {
return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
}
if (
typeof otherId === 'string' &&
ObjectId.isValid(otherId) &&
otherId.length === 12 &&
isUint8Array(this.id)
) {
return otherId === Buffer.prototype.toString.call(this.id, 'latin1');
}
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
return otherId.toLowerCase() === this.toHexString();
}
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
return Buffer.from(otherId).equals(this.id);
}
if (
typeof otherId === 'object' &&
'toHexString' in otherId &&
typeof otherId.toHexString === 'function'
) {
const otherIdString = otherId.toHexString();
const thisIdString = this.toHexString().toLowerCase();
return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
}
return false;
}
/** Returns the generation date (accurate up to the second) that this ID was generated. */
getTimestamp(): Date {
const timestamp = new Date();
const time = this.id.readUInt32BE(0);
timestamp.setTime(Math.floor(time) * 1000);
return timestamp;
}
/** @internal */
static createPk(): ObjectId {
return new ObjectId();
}
/**
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
*
* @param time - an integer number representing a number of seconds.
*/
static createFromTime(time: number): ObjectId {
const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
// Encode time into first 4 bytes
buffer.writeUInt32BE(time, 0);
// Return the new objectId
return new ObjectId(buffer);
}
/**
* Creates an ObjectId from a hex string representation of an ObjectId.
*
* @param hexString - create a ObjectId from a passed in 24 character hexstring.
*/
static createFromHexString(hexString: string): ObjectId {
// Throw an error if it's not a valid setup
if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
throw new BSONTypeError(
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
);
}
return new ObjectId(Buffer.from(hexString, 'hex'));
}
/**
* Checks if a value is a valid bson ObjectId
*
* @param id - ObjectId instance to validate.
*/
static isValid(id: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array): boolean {
if (id == null) return false;
try {
new ObjectId(id);
return true;
} catch {
return false;
}
}
/** @internal */
toExtendedJSON(): ObjectIdExtended {
if (this.toHexString) return { $oid: this.toHexString() };
return { $oid: this.toString('hex') };
}
/** @internal */
static fromExtendedJSON(doc: ObjectIdExtended): ObjectId {
return new ObjectId(doc.$oid);
}
/**
* Converts to a string representation of this Id.
*
* @returns return the 24 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new ObjectId("${this.toHexString()}")`;
}
}
// Deprecated methods
Object.defineProperty(ObjectId.prototype, 'generate', {
value: deprecate(
(time: number) => ObjectId.generate(time),
'Please use the static `ObjectId.generate(time)` instead'
)
});
Object.defineProperty(ObjectId.prototype, 'getInc', {
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId.prototype, 'get_inc', {
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId, 'get_inc', {
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' });

View File

@@ -0,0 +1,228 @@
import { Buffer } from 'buffer';
import { Binary } from '../binary';
import type { Document } from '../bson';
import * as constants from '../constants';
import { isAnyArrayBuffer, isDate, isRegExp, normalizedFunctionString } from './utils';
export function calculateObjectSize(
object: Document,
serializeFunctions?: boolean,
ignoreUndefined?: boolean
): number {
let totalLength = 4 + 1;
if (Array.isArray(object)) {
for (let i = 0; i < object.length; i++) {
totalLength += calculateElement(
i.toString(),
object[i],
serializeFunctions,
true,
ignoreUndefined
);
}
} else {
// If we have toBSON defined, override the current object
if (typeof object?.toBSON === 'function') {
object = object.toBSON();
}
// Calculate size
for (const key in object) {
totalLength += calculateElement(key, object[key], serializeFunctions, false, ignoreUndefined);
}
}
return totalLength;
}
/** @internal */
function calculateElement(
name: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
serializeFunctions = false,
isArray = false,
ignoreUndefined = false
) {
// If we have toBSON defined, override the current object
if (typeof value?.toBSON === 'function') {
value = value.toBSON();
}
switch (typeof value) {
case 'string':
return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1;
case 'number':
if (
Math.floor(value) === value &&
value >= constants.JS_INT_MIN &&
value <= constants.JS_INT_MAX
) {
if (value >= constants.BSON_INT32_MIN && value <= constants.BSON_INT32_MAX) {
// 32 bit
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1);
} else {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
} else {
// 64 bit
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
case 'undefined':
if (isArray || !ignoreUndefined)
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
return 0;
case 'boolean':
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
case 'object':
if (value == null || value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
} else if (value instanceof Date || isDate(value)) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
} else if (
ArrayBuffer.isView(value) ||
value instanceof ArrayBuffer ||
isAnyArrayBuffer(value)
) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + value.byteLength
);
} else if (
value['_bsontype'] === 'Long' ||
value['_bsontype'] === 'Double' ||
value['_bsontype'] === 'Timestamp'
) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
} else if (value['_bsontype'] === 'Decimal128') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
} else if (value['_bsontype'] === 'Code') {
// Calculate size depending on the availability of a scope
if (value.scope != null && Object.keys(value.scope).length > 0) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
4 +
Buffer.byteLength(value.code.toString(), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
Buffer.byteLength(value.code.toString(), 'utf8') +
1
);
}
} else if (value['_bsontype'] === 'Binary') {
const binary: Binary = value;
// Check what kind of subtype we have
if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
(binary.position + 1 + 4 + 1 + 4)
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (binary.position + 1 + 4 + 1)
);
}
} else if (value['_bsontype'] === 'Symbol') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
Buffer.byteLength(value.value, 'utf8') +
4 +
1 +
1
);
} else if (value['_bsontype'] === 'DBRef') {
// Set up correct object for serialization
const ordered_values = Object.assign(
{
$ref: value.collection,
$id: value.oid
},
value.fields
);
// Add db reference if it exists
if (value.db != null) {
ordered_values['$db'] = value.db;
}
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined)
);
} else if (value instanceof RegExp || isRegExp(value)) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.source, 'utf8') +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1
);
} else if (value['_bsontype'] === 'BSONRegExp') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.pattern, 'utf8') +
1 +
Buffer.byteLength(value.options, 'utf8') +
1
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
calculateObjectSize(value, serializeFunctions, ignoreUndefined) +
1
);
}
case 'function':
// WTF for 0.4.X where typeof /someregexp/ === 'function'
if (value instanceof RegExp || isRegExp(value) || String.call(value) === '[object RegExp]') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.source, 'utf8') +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1
);
} else {
if (serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
4 +
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
);
} else if (serializeFunctions) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
1
);
}
}
}
return 0;
}

View File

@@ -0,0 +1,782 @@
import { Buffer } from 'buffer';
import { Binary } from '../binary';
import type { Document } from '../bson';
import { Code } from '../code';
import * as constants from '../constants';
import { DBRef, DBRefLike, isDBRefLike } from '../db_ref';
import { Decimal128 } from '../decimal128';
import { Double } from '../double';
import { BSONError } from '../error';
import { Int32 } from '../int_32';
import { Long } from '../long';
import { MaxKey } from '../max_key';
import { MinKey } from '../min_key';
import { ObjectId } from '../objectid';
import { BSONRegExp } from '../regexp';
import { BSONSymbol } from '../symbol';
import { Timestamp } from '../timestamp';
import { validateUtf8 } from '../validate_utf8';
/** @public */
export interface DeserializeOptions {
/** evaluate functions in the BSON document scoped to the object deserialized. */
evalFunctions?: boolean;
/** cache evaluated functions for reuse. */
cacheFunctions?: boolean;
/**
* use a crc32 code for caching, otherwise use the string of the function.
* @deprecated this option to use the crc32 function never worked as intended
* due to the fact that the crc32 function itself was never implemented.
* */
cacheFunctionsCrc32?: boolean;
/** when deserializing a Long will fit it into a Number if it's smaller than 53 bits */
promoteLongs?: boolean;
/** when deserializing a Binary will return it as a node.js Buffer instance. */
promoteBuffers?: boolean;
/** when deserializing will promote BSON values to their Node.js closest equivalent types. */
promoteValues?: boolean;
/** allow to specify if there what fields we wish to return as unserialized raw buffer. */
fieldsAsRaw?: Document;
/** return BSON regular expressions as BSONRegExp instances. */
bsonRegExp?: boolean;
/** allows the buffer to be larger than the parsed BSON object */
allowObjectSmallerThanBufferSize?: boolean;
/** Offset into buffer to begin reading document from */
index?: number;
raw?: boolean;
/** Allows for opt-out utf-8 validation for all keys or
* specified keys. Must be all true or all false.
*
* @example
* ```js
* // disables validation on all keys
* validation: { utf8: false }
*
* // enables validation only on specified keys a, b, and c
* validation: { utf8: { a: true, b: true, c: true } }
*
* // disables validation only on specified keys a, b
* validation: { utf8: { a: false, b: false } }
* ```
*/
validation?: { utf8: boolean | Record<string, true> | Record<string, false> };
}
// Internal long versions
const JS_INT_MAX_LONG = Long.fromNumber(constants.JS_INT_MAX);
const JS_INT_MIN_LONG = Long.fromNumber(constants.JS_INT_MIN);
const functionCache: { [hash: string]: Function } = {};
export function deserialize(
buffer: Buffer,
options: DeserializeOptions,
isArray?: boolean
): Document {
options = options == null ? {} : options;
const index = options && options.index ? options.index : 0;
// Read the document size
const size =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
if (size < 5) {
throw new BSONError(`bson size must be >= 5, is ${size}`);
}
if (options.allowObjectSmallerThanBufferSize && buffer.length < size) {
throw new BSONError(`buffer length ${buffer.length} must be >= bson size ${size}`);
}
if (!options.allowObjectSmallerThanBufferSize && buffer.length !== size) {
throw new BSONError(`buffer length ${buffer.length} must === bson size ${size}`);
}
if (size + index > buffer.byteLength) {
throw new BSONError(
`(bson size ${size} + options.index ${index} must be <= buffer length ${buffer.byteLength})`
);
}
// Illegal end value
if (buffer[index + size - 1] !== 0) {
throw new BSONError(
"One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00"
);
}
// Start deserializtion
return deserializeObject(buffer, index, options, isArray);
}
const allowedDBRefKeys = /^\$ref$|^\$id$|^\$db$/;
function deserializeObject(
buffer: Buffer,
index: number,
options: DeserializeOptions,
isArray = false
) {
const evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
const cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
// Return raw bson buffer instead of parsing it
const raw = options['raw'] == null ? false : options['raw'];
// Return BSONRegExp objects instead of native regular expressions
const bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
// Controls the promotion of values vs wrapper classes
const promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
const promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
const promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
// Ensures default validation option if none given
const validation = options.validation == null ? { utf8: true } : options.validation;
// Shows if global utf-8 validation is enabled or disabled
let globalUTFValidation = true;
// Reflects utf-8 validation setting regardless of global or specific key validation
let validationSetting: boolean;
// Set of keys either to enable or disable validation on
const utf8KeysSet = new Set();
// Check for boolean uniformity and empty validation option
const utf8ValidatedKeys = validation.utf8;
if (typeof utf8ValidatedKeys === 'boolean') {
validationSetting = utf8ValidatedKeys;
} else {
globalUTFValidation = false;
const utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
return utf8ValidatedKeys[key];
});
if (utf8ValidationValues.length === 0) {
throw new BSONError('UTF-8 validation setting cannot be empty');
}
if (typeof utf8ValidationValues[0] !== 'boolean') {
throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
}
validationSetting = utf8ValidationValues[0];
// Ensures boolean uniformity in utf-8 validation (all true or all false)
if (!utf8ValidationValues.every(item => item === validationSetting)) {
throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
}
}
// Add keys to set that will either be validated or not based on validationSetting
if (!globalUTFValidation) {
for (const key of Object.keys(utf8ValidatedKeys)) {
utf8KeysSet.add(key);
}
}
// Set the start index
const startIndex = index;
// Validate that we have at least 4 bytes of buffer
if (buffer.length < 5) throw new BSONError('corrupt bson message < 5 bytes long');
// Read the document size
const size =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Ensure buffer is valid size
if (size < 5 || size > buffer.length) throw new BSONError('corrupt bson message');
// Create holding object
const object: Document = isArray ? [] : {};
// Used for arrays to skip having to perform utf8 decoding
let arrayIndex = 0;
const done = false;
let isPossibleDBRef = isArray ? false : null;
// While we have more left data left keep parsing
const dataview = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
while (!done) {
// Read the type
const elementType = buffer[index++];
// If we get a zero it's the last byte, exit
if (elementType === 0) break;
// Get the start search index
let i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.byteLength) throw new BSONError('Bad BSON Document: illegal CString');
// Represents the key
const name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
// shouldValidateKey is true if the key should be validated, false otherwise
let shouldValidateKey = true;
if (globalUTFValidation || utf8KeysSet.has(name)) {
shouldValidateKey = validationSetting;
} else {
shouldValidateKey = !validationSetting;
}
if (isPossibleDBRef !== false && (name as string)[0] === '$') {
isPossibleDBRef = allowedDBRefKeys.test(name as string);
}
let value;
index = i + 1;
if (elementType === constants.BSON_DATA_STRING) {
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
) {
throw new BSONError('bad string length in bson');
}
value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
index = index + stringSize;
} else if (elementType === constants.BSON_DATA_OID) {
const oid = Buffer.alloc(12);
buffer.copy(oid, 0, index, index + 12);
value = new ObjectId(oid);
index = index + 12;
} else if (elementType === constants.BSON_DATA_INT && promoteValues === false) {
value = new Int32(
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24)
);
} else if (elementType === constants.BSON_DATA_INT) {
value =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
} else if (elementType === constants.BSON_DATA_NUMBER && promoteValues === false) {
value = new Double(dataview.getFloat64(index, true));
index = index + 8;
} else if (elementType === constants.BSON_DATA_NUMBER) {
value = dataview.getFloat64(index, true);
index = index + 8;
} else if (elementType === constants.BSON_DATA_DATE) {
const lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
value = new Date(new Long(lowBits, highBits).toNumber());
} else if (elementType === constants.BSON_DATA_BOOLEAN) {
if (buffer[index] !== 0 && buffer[index] !== 1)
throw new BSONError('illegal boolean type value');
value = buffer[index++] === 1;
} else if (elementType === constants.BSON_DATA_OBJECT) {
const _index = index;
const objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
if (objectSize <= 0 || objectSize > buffer.length - index)
throw new BSONError('bad embedded document length in bson');
// We have a raw value
if (raw) {
value = buffer.slice(index, index + objectSize);
} else {
let objectOptions = options;
if (!globalUTFValidation) {
objectOptions = { ...options, validation: { utf8: shouldValidateKey } };
}
value = deserializeObject(buffer, _index, objectOptions, false);
}
index = index + objectSize;
} else if (elementType === constants.BSON_DATA_ARRAY) {
const _index = index;
const objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
let arrayOptions = options;
// Stop index
const stopIndex = index + objectSize;
// All elements of array to be returned as raw bson
if (fieldsAsRaw && fieldsAsRaw[name]) {
arrayOptions = {};
for (const n in options) {
(
arrayOptions as {
[key: string]: DeserializeOptions[keyof DeserializeOptions];
}
)[n] = options[n as keyof DeserializeOptions];
}
arrayOptions['raw'] = true;
}
if (!globalUTFValidation) {
arrayOptions = { ...arrayOptions, validation: { utf8: shouldValidateKey } };
}
value = deserializeObject(buffer, _index, arrayOptions, true);
index = index + objectSize;
if (buffer[index - 1] !== 0) throw new BSONError('invalid array terminator byte');
if (index !== stopIndex) throw new BSONError('corrupted array bson');
} else if (elementType === constants.BSON_DATA_UNDEFINED) {
value = undefined;
} else if (elementType === constants.BSON_DATA_NULL) {
value = null;
} else if (elementType === constants.BSON_DATA_LONG) {
// Unpack the low and high bits
const lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const long = new Long(lowBits, highBits);
// Promote the long if possible
if (promoteLongs && promoteValues === true) {
value =
long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
? long.toNumber()
: long;
} else {
value = long;
}
} else if (elementType === constants.BSON_DATA_DECIMAL128) {
// Buffer to contain the decimal bytes
const bytes = Buffer.alloc(16);
// Copy the next 16 bytes into the bytes buffer
buffer.copy(bytes, 0, index, index + 16);
// Update index
index = index + 16;
// Assign the new Decimal128 value
const decimal128 = new Decimal128(bytes) as Decimal128 | { toObject(): unknown };
// If we have an alternative mapper use that
if ('toObject' in decimal128 && typeof decimal128.toObject === 'function') {
value = decimal128.toObject();
} else {
value = decimal128;
}
} else if (elementType === constants.BSON_DATA_BINARY) {
let binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const totalBinarySize = binarySize;
const subType = buffer[index++];
// Did we have a negative binary size, throw
if (binarySize < 0) throw new BSONError('Negative binary type element size found');
// Is the length longer than the document
if (binarySize > buffer.byteLength)
throw new BSONError('Binary type size larger than document size');
// Decode as raw Buffer object if options specifies it
if (buffer['slice'] != null) {
// If we have subtype 2 skip the 4 bytes for the size
if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (binarySize < 0)
throw new BSONError('Negative binary type element size found for subtype 0x02');
if (binarySize > totalBinarySize - 4)
throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
if (binarySize < totalBinarySize - 4)
throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
}
if (promoteBuffers && promoteValues) {
value = buffer.slice(index, index + binarySize);
} else {
value = new Binary(buffer.slice(index, index + binarySize), subType);
if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = value.toUUID();
}
}
} else {
const _buffer = Buffer.alloc(binarySize);
// If we have subtype 2 skip the 4 bytes for the size
if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (binarySize < 0)
throw new BSONError('Negative binary type element size found for subtype 0x02');
if (binarySize > totalBinarySize - 4)
throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
if (binarySize < totalBinarySize - 4)
throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
}
// Copy the data
for (i = 0; i < binarySize; i++) {
_buffer[i] = buffer[index + i];
}
if (promoteBuffers && promoteValues) {
value = _buffer;
} else if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
} else {
value = new Binary(buffer.slice(index, index + binarySize), subType);
}
}
// Update the index
index = index + binarySize;
} else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === false) {
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString');
// Return the C string
const source = buffer.toString('utf8', index, i);
// Create the regexp
index = i + 1;
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString');
// Return the C string
const regExpOptions = buffer.toString('utf8', index, i);
index = i + 1;
// For each option add the corresponding one for javascript
const optionsArray = new Array(regExpOptions.length);
// Parse options
for (i = 0; i < regExpOptions.length; i++) {
switch (regExpOptions[i]) {
case 'm':
optionsArray[i] = 'm';
break;
case 's':
optionsArray[i] = 'g';
break;
case 'i':
optionsArray[i] = 'i';
break;
}
}
value = new RegExp(source, optionsArray.join(''));
} else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === true) {
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString');
// Return the C string
const source = buffer.toString('utf8', index, i);
index = i + 1;
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString');
// Return the C string
const regExpOptions = buffer.toString('utf8', index, i);
index = i + 1;
// Set the object
value = new BSONRegExp(source, regExpOptions);
} else if (elementType === constants.BSON_DATA_SYMBOL) {
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
) {
throw new BSONError('bad string length in bson');
}
const symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
value = promoteValues ? symbol : new BSONSymbol(symbol);
index = index + stringSize;
} else if (elementType === constants.BSON_DATA_TIMESTAMP) {
const lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
value = new Timestamp(lowBits, highBits);
} else if (elementType === constants.BSON_DATA_MIN_KEY) {
value = new MinKey();
} else if (elementType === constants.BSON_DATA_MAX_KEY) {
value = new MaxKey();
} else if (elementType === constants.BSON_DATA_CODE) {
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
) {
throw new BSONError('bad string length in bson');
}
const functionString = getValidatedString(
buffer,
index,
index + stringSize - 1,
shouldValidateKey
);
// If we are evaluating the functions
if (evalFunctions) {
// If we have cache enabled let's look for the md5 of the function in the cache
if (cacheFunctions) {
// Got to do this to avoid V8 deoptimizing the call due to finding eval
value = isolateEval(functionString, functionCache, object);
} else {
value = isolateEval(functionString);
}
} else {
value = new Code(functionString);
}
// Update parse index position
index = index + stringSize;
} else if (elementType === constants.BSON_DATA_CODE_W_SCOPE) {
const totalSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Element cannot be shorter than totalSize + stringSize + documentSize + terminator
if (totalSize < 4 + 4 + 4 + 1) {
throw new BSONError('code_w_scope total size shorter minimum expected length');
}
// Get the code string size
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Check if we have a valid string
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
) {
throw new BSONError('bad string length in bson');
}
// Javascript function
const functionString = getValidatedString(
buffer,
index,
index + stringSize - 1,
shouldValidateKey
);
// Update parse index position
index = index + stringSize;
// Parse the element
const _index = index;
// Decode the size of the object document
const objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
// Decode the scope object
const scopeObject = deserializeObject(buffer, _index, options, false);
// Adjust the index
index = index + objectSize;
// Check if field length is too short
if (totalSize < 4 + 4 + objectSize + stringSize) {
throw new BSONError('code_w_scope total size is too short, truncating scope');
}
// Check if totalSize field is too long
if (totalSize > 4 + 4 + objectSize + stringSize) {
throw new BSONError('code_w_scope total size is too long, clips outer document');
}
// If we are evaluating the functions
if (evalFunctions) {
// If we have cache enabled let's look for the md5 of the function in the cache
if (cacheFunctions) {
// Got to do this to avoid V8 deoptimizing the call due to finding eval
value = isolateEval(functionString, functionCache, object);
} else {
value = isolateEval(functionString);
}
value.scope = scopeObject;
} else {
value = new Code(functionString, scopeObject);
}
} else if (elementType === constants.BSON_DATA_DBPOINTER) {
// Get the code string size
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Check if we have a valid string
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
)
throw new BSONError('bad string length in bson');
// Namespace
if (validation != null && validation.utf8) {
if (!validateUtf8(buffer, index, index + stringSize - 1)) {
throw new BSONError('Invalid UTF-8 string in BSON document');
}
}
const namespace = buffer.toString('utf8', index, index + stringSize - 1);
// Update parse index position
index = index + stringSize;
// Read the oid
const oidBuffer = Buffer.alloc(12);
buffer.copy(oidBuffer, 0, index, index + 12);
const oid = new ObjectId(oidBuffer);
// Update the index
index = index + 12;
// Upgrade to DBRef type
value = new DBRef(namespace, oid);
} else {
throw new BSONError(
`Detected unknown BSON type ${elementType.toString(16)} for fieldname "${name}"`
);
}
if (name === '__proto__') {
Object.defineProperty(object, name, {
value,
writable: true,
enumerable: true,
configurable: true
});
} else {
object[name] = value;
}
}
// Check if the deserialization was against a valid array/object
if (size !== index - startIndex) {
if (isArray) throw new BSONError('corrupt array bson');
throw new BSONError('corrupt object bson');
}
// if we did not find "$ref", "$id", "$db", or found an extraneous $key, don't make a DBRef
if (!isPossibleDBRef) return object;
if (isDBRefLike(object)) {
const copy = Object.assign({}, object) as Partial<DBRefLike>;
delete copy.$ref;
delete copy.$id;
delete copy.$db;
return new DBRef(object.$ref, object.$id, object.$db, copy);
}
return object;
}
/**
* Ensure eval is isolated, store the result in functionCache.
*
* @internal
*/
function isolateEval(
functionString: string,
functionCache?: { [hash: string]: Function },
object?: Document
) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
if (!functionCache) return new Function(functionString);
// Check for cache hit, eval if missing and return cached function
if (functionCache[functionString] == null) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
functionCache[functionString] = new Function(functionString);
}
// Set the object
return functionCache[functionString].bind(object);
}
function getValidatedString(
buffer: Buffer,
start: number,
end: number,
shouldValidateUtf8: boolean
) {
const value = buffer.toString('utf8', start, end);
// if utf8 validation is on, do the check
if (shouldValidateUtf8) {
for (let i = 0; i < value.length; i++) {
if (value.charCodeAt(i) === 0xfffd) {
if (!validateUtf8(buffer, start, end)) {
throw new BSONError('Invalid UTF-8 string in BSON document');
}
break;
}
}
}
return value;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,127 @@
import { Buffer } from 'buffer';
import { getGlobal } from '../utils/global';
type RandomBytesFunction = (size: number) => Uint8Array;
/**
* Normalizes our expected stringified form of a function across versions of node
* @param fn - The function to stringify
*/
export function normalizedFunctionString(fn: Function): string {
return fn.toString().replace('function(', 'function (');
}
function isReactNative() {
const g = getGlobal<{ navigator?: { product?: string } }>();
return typeof g.navigator === 'object' && g.navigator.product === 'ReactNative';
}
const insecureRandomBytes: RandomBytesFunction = function insecureRandomBytes(size: number) {
const insecureWarning = isReactNative()
? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
: 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.';
console.warn(insecureWarning);
const result = Buffer.alloc(size);
for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256);
return result;
};
/* We do not want to have to include DOM types just for this check */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let window: any;
declare let require: Function;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let global: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let process: any; // Used by @rollup/plugin-replace
const detectRandomBytes = (): RandomBytesFunction => {
if (process.browser) {
if (typeof window !== 'undefined') {
// browser crypto implementation(s)
const target = window.crypto || window.msCrypto; // allow for IE11
if (target && target.getRandomValues) {
return size => target.getRandomValues(Buffer.alloc(size));
}
}
if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
// allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
return size => global.crypto.getRandomValues(Buffer.alloc(size));
}
return insecureRandomBytes;
} else {
let requiredRandomBytes: RandomBytesFunction | null | undefined;
try {
requiredRandomBytes = require('crypto').randomBytes;
} catch (e) {
// keep the fallback
}
// NOTE: in transpiled cases the above require might return null/undefined
return requiredRandomBytes || insecureRandomBytes;
}
};
export const randomBytes = detectRandomBytes();
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(
Object.prototype.toString.call(value)
);
}
export function isUint8Array(value: unknown): value is Uint8Array {
return Object.prototype.toString.call(value) === '[object Uint8Array]';
}
export function isBigInt64Array(value: unknown): value is BigInt64Array {
return Object.prototype.toString.call(value) === '[object BigInt64Array]';
}
export function isBigUInt64Array(value: unknown): value is BigUint64Array {
return Object.prototype.toString.call(value) === '[object BigUint64Array]';
}
export function isRegExp(d: unknown): d is RegExp {
return Object.prototype.toString.call(d) === '[object RegExp]';
}
export function isMap(d: unknown): d is Map<unknown, unknown> {
return Object.prototype.toString.call(d) === '[object Map]';
}
/** Call to check if your environment has `Buffer` */
export function haveBuffer(): boolean {
return typeof global !== 'undefined' && typeof global.Buffer !== 'undefined';
}
// To ensure that 0.4 of node works correctly
export function isDate(d: unknown): d is Date {
return isObjectLike(d) && Object.prototype.toString.call(d) === '[object Date]';
}
/**
* @internal
* this is to solve the `'someKey' in x` problem where x is unknown.
* https://github.com/typescript-eslint/typescript-eslint/issues/1071#issuecomment-541955753
*/
export function isObjectLike(candidate: unknown): candidate is Record<string, unknown> {
return typeof candidate === 'object' && candidate !== null;
}
declare let console: { warn(...message: unknown[]): void };
export function deprecate<T extends Function>(fn: T, message: string): T {
let warned = false;
function deprecated(this: unknown, ...args: unknown[]) {
if (!warned) {
console.warn(message);
warned = true;
}
return fn.apply(this, args);
}
return deprecated as unknown as T;
}

View File

@@ -0,0 +1,105 @@
import { BSONError, BSONTypeError } from './error';
import type { EJSONOptions } from './extended_json';
function alphabetize(str: string): string {
return str.split('').sort().join('');
}
/** @public */
export interface BSONRegExpExtendedLegacy {
$regex: string | BSONRegExp;
$options: string;
}
/** @public */
export interface BSONRegExpExtended {
$regularExpression: {
pattern: string;
options: string;
};
}
/**
* A class representation of the BSON RegExp type.
* @public
* @category BSONType
*/
export class BSONRegExp {
_bsontype!: 'BSONRegExp';
pattern!: string;
options!: string;
/**
* @param pattern - The regular expression pattern to match
* @param options - The regular expression options
*/
constructor(pattern: string, options?: string) {
if (!(this instanceof BSONRegExp)) return new BSONRegExp(pattern, options);
this.pattern = pattern;
this.options = alphabetize(options ?? '');
if (this.pattern.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Regex patterns cannot contain null bytes, found: ${JSON.stringify(this.pattern)}`
);
}
if (this.options.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Regex options cannot contain null bytes, found: ${JSON.stringify(this.options)}`
);
}
// Validate options
for (let i = 0; i < this.options.length; i++) {
if (
!(
this.options[i] === 'i' ||
this.options[i] === 'm' ||
this.options[i] === 'x' ||
this.options[i] === 'l' ||
this.options[i] === 's' ||
this.options[i] === 'u'
)
) {
throw new BSONError(`The regular expression option [${this.options[i]}] is not supported`);
}
}
}
static parseOptions(options?: string): string {
return options ? options.split('').sort().join('') : '';
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): BSONRegExpExtendedLegacy | BSONRegExpExtended {
options = options || {};
if (options.legacy) {
return { $regex: this.pattern, $options: this.options };
}
return { $regularExpression: { pattern: this.pattern, options: this.options } };
}
/** @internal */
static fromExtendedJSON(doc: BSONRegExpExtendedLegacy | BSONRegExpExtended): BSONRegExp {
if ('$regex' in doc) {
if (typeof doc.$regex !== 'string') {
// This is for $regex query operators that have extended json values.
if (doc.$regex._bsontype === 'BSONRegExp') {
return doc as unknown as BSONRegExp;
}
} else {
return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
}
}
if ('$regularExpression' in doc) {
return new BSONRegExp(
doc.$regularExpression.pattern,
BSONRegExp.parseOptions(doc.$regularExpression.options)
);
}
throw new BSONTypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
}
}
Object.defineProperty(BSONRegExp.prototype, '_bsontype', { value: 'BSONRegExp' });

View File

@@ -0,0 +1,58 @@
/** @public */
export interface BSONSymbolExtended {
$symbol: string;
}
/**
* A class representation of the BSON Symbol type.
* @public
* @category BSONType
*/
export class BSONSymbol {
_bsontype!: 'Symbol';
value!: string;
/**
* @param value - the string representing the symbol.
*/
constructor(value: string) {
if (!(this instanceof BSONSymbol)) return new BSONSymbol(value);
this.value = value;
}
/** Access the wrapped string value. */
valueOf(): string {
return this.value;
}
toString(): string {
return this.value;
}
/** @internal */
inspect(): string {
return `new BSONSymbol("${this.value}")`;
}
toJSON(): string {
return this.value;
}
/** @internal */
toExtendedJSON(): BSONSymbolExtended {
return { $symbol: this.value };
}
/** @internal */
static fromExtendedJSON(doc: BSONSymbolExtended): BSONSymbol {
return new BSONSymbol(doc.$symbol);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
}
Object.defineProperty(BSONSymbol.prototype, '_bsontype', { value: 'Symbol' });

View File

@@ -0,0 +1,119 @@
import { Long } from './long';
import { isObjectLike } from './parser/utils';
/** @public */
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
/** @public */
export type LongWithoutOverrides = new (low: unknown, high?: number, unsigned?: boolean) => {
[P in Exclude<keyof Long, TimestampOverrides>]: Long[P];
};
/** @public */
export const LongWithoutOverridesClass: LongWithoutOverrides =
Long as unknown as LongWithoutOverrides;
/** @public */
export interface TimestampExtended {
$timestamp: {
t: number;
i: number;
};
}
/**
* @public
* @category BSONType
* */
export class Timestamp extends LongWithoutOverridesClass {
_bsontype!: 'Timestamp';
static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
/**
* @param low - A 64-bit Long representing the Timestamp.
*/
constructor(long: Long);
/**
* @param value - A pair of two values indicating timestamp and increment.
*/
constructor(value: { t: number; i: number });
/**
* @param low - the low (signed) 32 bits of the Timestamp.
* @param high - the high (signed) 32 bits of the Timestamp.
* @deprecated Please use `Timestamp({ t: high, i: low })` or `Timestamp(Long(low, high))` instead.
*/
constructor(low: number, high: number);
constructor(low: number | Long | { t: number; i: number }, high?: number) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (!(this instanceof Timestamp)) return new Timestamp(low, high);
if (Long.isLong(low)) {
super(low.low, low.high, true);
} else if (isObjectLike(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
super(low.i, low.t, true);
} else {
super(low, high, true);
}
Object.defineProperty(this, '_bsontype', {
value: 'Timestamp',
writable: false,
configurable: false,
enumerable: false
});
}
toJSON(): { $timestamp: string } {
return {
$timestamp: this.toString()
};
}
/** Returns a Timestamp represented by the given (32-bit) integer value. */
static fromInt(value: number): Timestamp {
return new Timestamp(Long.fromInt(value, true));
}
/** Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. */
static fromNumber(value: number): Timestamp {
return new Timestamp(Long.fromNumber(value, true));
}
/**
* Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
*
* @param lowBits - the low 32-bits.
* @param highBits - the high 32-bits.
*/
static fromBits(lowBits: number, highBits: number): Timestamp {
return new Timestamp(lowBits, highBits);
}
/**
* Returns a Timestamp from the given string, optionally using the given radix.
*
* @param str - the textual representation of the Timestamp.
* @param optRadix - the radix in which the text is written.
*/
static fromString(str: string, optRadix: number): Timestamp {
return new Timestamp(Long.fromString(str, true, optRadix));
}
/** @internal */
toExtendedJSON(): TimestampExtended {
return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
}
/** @internal */
static fromExtendedJSON(doc: TimestampExtended): Timestamp {
return new Timestamp(doc.$timestamp);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
}
}

View File

@@ -0,0 +1,22 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* We do not want to have to include DOM types just for this check */
declare const window: unknown;
declare const self: unknown;
declare const global: unknown;
function checkForMath(potentialGlobal: any) {
// eslint-disable-next-line eqeqeq
return potentialGlobal && potentialGlobal.Math == Math && potentialGlobal;
}
// https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
export function getGlobal<T = Record<string, unknown>>(): T {
return (
checkForMath(typeof globalThis === 'object' && globalThis) ||
checkForMath(typeof window === 'object' && window) ||
checkForMath(typeof self === 'object' && self) ||
checkForMath(typeof global === 'object' && global) ||
// eslint-disable-next-line @typescript-eslint/no-implied-eval
Function('return this')()
);
}

View File

@@ -0,0 +1,33 @@
import { Buffer } from 'buffer';
import { BSONTypeError } from './error';
// Validation regex for v4 uuid (validates with or without dashes)
const VALIDATION_REGEX =
/^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15})$/i;
export const uuidValidateString = (str: string): boolean =>
typeof str === 'string' && VALIDATION_REGEX.test(str);
export const uuidHexStringToBuffer = (hexString: string): Buffer => {
if (!uuidValidateString(hexString)) {
throw new BSONTypeError(
'UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'
);
}
const sanitizedHexString = hexString.replace(/-/g, '');
return Buffer.from(sanitizedHexString, 'hex');
};
export const bufferToUuidHexString = (buffer: Buffer, includeDashes = true): string =>
includeDashes
? buffer.toString('hex', 0, 4) +
'-' +
buffer.toString('hex', 4, 6) +
'-' +
buffer.toString('hex', 6, 8) +
'-' +
buffer.toString('hex', 8, 10) +
'-' +
buffer.toString('hex', 10, 16)
: buffer.toString('hex');

View File

@@ -0,0 +1,47 @@
const FIRST_BIT = 0x80;
const FIRST_TWO_BITS = 0xc0;
const FIRST_THREE_BITS = 0xe0;
const FIRST_FOUR_BITS = 0xf0;
const FIRST_FIVE_BITS = 0xf8;
const TWO_BIT_CHAR = 0xc0;
const THREE_BIT_CHAR = 0xe0;
const FOUR_BIT_CHAR = 0xf0;
const CONTINUING_CHAR = 0x80;
/**
* Determines if the passed in bytes are valid utf8
* @param bytes - An array of 8-bit bytes. Must be indexable and have length property
* @param start - The index to start validating
* @param end - The index to end validating
*/
export function validateUtf8(
bytes: { [index: number]: number },
start: number,
end: number
): boolean {
let continuation = 0;
for (let i = start; i < end; i += 1) {
const byte = bytes[i];
if (continuation) {
if ((byte & FIRST_TWO_BITS) !== CONTINUING_CHAR) {
return false;
}
continuation -= 1;
} else if (byte & FIRST_BIT) {
if ((byte & FIRST_THREE_BITS) === TWO_BIT_CHAR) {
continuation = 1;
} else if ((byte & FIRST_FOUR_BITS) === THREE_BIT_CHAR) {
continuation = 2;
} else if ((byte & FIRST_FIVE_BITS) === FOUR_BIT_CHAR) {
continuation = 3;
} else {
return false;
}
}
}
return !continuation;
}