some progress

This commit is contained in:
Jonas_Jones 2023-03-30 20:40:42 +02:00
parent aea93a5527
commit e3c15bd288
1388 changed files with 306946 additions and 68323 deletions

201
node_modules/bson/LICENSE.md generated vendored Normal file
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.

368
node_modules/bson/README.md generated vendored Normal file
View file

@ -0,0 +1,368 @@
# 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).
### 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.js or Bundling Usage
When using a bundler or Node.js you can import bson using the package name:
```js
import { BSON, EJSON, ObjectId } from 'bson';
// or:
// const { BSON, EJSON, ObjectId } = require('bson');
const bytes = BSON.serialize({ _id: new ObjectId() });
console.log(bytes);
const doc = BSON.deserialize(bytes);
console.log(EJSON.stringify(doc));
// {"_id":{"$oid":"..."}}
```
### Browser Usage
If you are working directly in the browser without a bundler please use the `.mjs` bundle like so:
```html
<script type="module">
import { BSON, EJSON, ObjectId } from './lib/bson.mjs';
const bytes = BSON.serialize({ _id: new ObjectId() });
console.log(bytes);
const doc = BSON.deserialize(bytes);
console.log(EJSON.stringify(doc));
// {"_id":{"$oid":"..."}}
</script>
```
## Installation
```sh
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.useBigInt64] | <code>Object</code> | <code>false</code> | when deserializing a Long will return a BigInt. |
| [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.
## Error Handling
It is our recommendation to use `BSONError.isBSONError()` checks on errors and to avoid relying on parsing `error.message` and `error.name` strings in your code. We guarantee `BSONError.isBSONError()` checks will pass according to semver guidelines, but errors may be sub-classed or their messages may change at any time, even patch releases, as we see fit to increase the helpfulness of the errors.
Any new errors we add to the driver will directly extend an existing error class and no existing error will be moved to a different parent class outside of a major release.
This means `BSONError.isBSONError()` will always be able to accurately capture the errors that our BSON library throws.
Hypothetical example: A collection in our Db has an issue with UTF-8 data:
```ts
let documentCount = 0;
const cursor = collection.find({}, { utf8Validation: true });
try {
for await (const doc of cursor) documentCount += 1;
} catch (error) {
if (BSONError.isBSONError(error)) {
console.log(`Found the troublemaker UTF-8!: ${documentCount} ${error.message}`);
return documentCount;
}
throw error;
}
```
## React Native
BSON requires that `TextEncoder`, `TextDecoder`, `atob`, `btoa`, and `crypto.getRandomValues` are available globally. These are present in most Javascript runtimes but require polyfilling in React Native. Polyfills for the missing functionality can be installed with the following command:
```sh
npm install --save react-native-get-random-values text-encoding-polyfill base-64
```
The following snippet should be placed at the top of the entrypoint (by default this is the root `index.js` file) for React Native projects using the BSON library. These lines must be placed for any code that imports `BSON`.
```typescript
// Required Polyfills For ReactNative
import {encode, decode} from 'base-64';
if (global.btoa == null) {
global.btoa = encode;
}
if (global.atob == null) {
global.atob = decode;
}
import 'text-encoding-polyfill';
import 'react-native-get-random-values';
```
Finally, import the `BSON` library like so:
```typescript
import { BSON, EJSON } from 'bson';
```
This will cause React Native to import the `node_modules/bson/lib/bson.cjs` bundle (see the `"react-native"` setting we have in the `"exports"` section of our [package.json](./package.json).)
### Technical Note about React Native module import
The `"exports"` definition in our `package.json` will result in BSON's CommonJS bundle being imported in a React Native project instead of the ES module bundle. Importing the CommonJS bundle is necessary because BSON's ES module bundle of BSON uses top-level await, which is not supported syntax in [React Native's runtime hermes](https://hermesengine.dev/).
## 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)));
```

1225
node_modules/bson/bson.d.ts generated vendored Normal file

File diff suppressed because it is too large Load diff

19
node_modules/bson/etc/prepare.js generated vendored Executable file
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
);
}
}
}

4100
node_modules/bson/lib/bson.bundle.js generated vendored Normal file

File diff suppressed because it is too large Load diff

1
node_modules/bson/lib/bson.bundle.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

4095
node_modules/bson/lib/bson.cjs generated vendored Normal file

File diff suppressed because it is too large Load diff

1
node_modules/bson/lib/bson.cjs.map generated vendored Normal file

File diff suppressed because one or more lines are too long

4067
node_modules/bson/lib/bson.mjs generated vendored Normal file

File diff suppressed because it is too large Load diff

1
node_modules/bson/lib/bson.mjs.map generated vendored Normal file

File diff suppressed because one or more lines are too long

115
node_modules/bson/package.json generated vendored Normal file
View file

@ -0,0 +1,115 @@
{
"name": "bson",
"description": "A bson parser for node.js and the browser",
"keywords": [
"mongodb",
"bson",
"parser"
],
"files": [
"lib",
"src",
"bson.d.ts",
"etc/prepare.js"
],
"types": "bson.d.ts",
"version": "5.1.0",
"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": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@microsoft/api-extractor": "^7.33.7",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^10.0.1",
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.12",
"@types/sinon": "^10.0.13",
"@types/sinon-chai": "^3.2.9",
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
"benchmark": "^2.1.4",
"chai": "^4.3.7",
"chalk": "^5.1.2",
"eslint": "^8.29.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-no-bigint-usage": "file:./etc/eslint/no-bigint-usage",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-tsdoc": "^0.2.17",
"magic-string": "^0.27.0",
"mocha": "10.1.0",
"node-fetch": "^3.2.10",
"nyc": "^15.1.0",
"prettier": "^2.8.1",
"rimraf": "^3.0.2",
"rollup": "^3.7.1",
"sinon": "^15.0.0",
"sinon-chai": "^3.7.0",
"source-map-support": "^0.5.21",
"standard-version": "^9.5.0",
"ts-node": "^10.9.1",
"tsd": "^0.25.0",
"typescript": "^4.9.4",
"typescript-cached-transpile": "0.0.6",
"uuid": "^9.0.0",
"v8-profiler-next": "^1.9.0"
},
"tsd": {
"directory": "test/types",
"compilerOptions": {
"strict": true,
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node"
}
},
"config": {
"native": false
},
"main": "./lib/bson.cjs",
"module": "./lib/bson.mjs",
"exports": {
"import": {
"types": "./bson.d.ts",
"default": "./lib/bson.mjs"
},
"require": {
"types": "./bson.d.ts",
"default": "./lib/bson.cjs"
},
"react-native": "./lib/bson.cjs",
"browser": "./lib/bson.mjs"
},
"compass:exports": {
"import": "./lib/bson.cjs",
"require": "./lib/bson.cjs"
},
"engines": {
"node": ">=14.20.1"
},
"scripts": {
"pretest": "npm run build",
"test": "npm run check:node && npm run check:web && npm run check:web-no-bigint",
"check:node": "WEB=false mocha test/node",
"check:tsd": "npm run build:dts && tsd",
"check:web": "WEB=true mocha test/node",
"check:web-no-bigint": "WEB=true NO_BIGINT=true mocha test/node",
"build:ts": "node ./node_modules/typescript/bin/tsc",
"build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && rimraf 'lib/**/*.d.ts*' lib/parser lib/utils",
"build:bundle": "rollup -c rollup.config.mjs",
"build": "npm run build:dts && npm run build:bundle",
"check:lint": "eslint -v && eslint --ext '.js,.ts' --max-warnings=0 src test && npm run build:dts && npm run check:tsd",
"format": "eslint --ext '.js,.ts' src test --fix",
"check:coverage": "nyc --check-coverage npm run check:node",
"prepare": "node etc/prepare.js",
"release": "standard-version -i HISTORY.md"
}
}

485
node_modules/bson/src/binary.ts generated vendored Normal file
View file

@ -0,0 +1,485 @@
import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils';
import { isUint8Array } from './parser/utils';
import type { EJSONOptions } from './extended_json';
import { BSONError } from './error';
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
import { ByteUtils } from './utils/byte_utils';
import { BSONValue } from './bson_value';
/** @public */
export type BinarySequence = Uint8Array | 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 extends BSONValue {
get _bsontype(): 'Binary' {
return '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!: Uint8Array;
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) {
super();
if (
!(buffer == null) &&
!(typeof buffer === 'string') &&
!ArrayBuffer.isView(buffer) &&
!(buffer instanceof ArrayBuffer) &&
!Array.isArray(buffer)
) {
throw new BSONError(
'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 = ByteUtils.allocate(Binary.BUFFER_SIZE);
this.position = 0;
} else {
if (typeof buffer === 'string') {
// string
this.buffer = ByteUtils.fromISO88591(buffer);
} else if (Array.isArray(buffer)) {
// number[]
this.buffer = ByteUtils.fromNumberArray(buffer);
} else {
// Buffer | TypedArray | ArrayBuffer
this.buffer = ByteUtils.toLocalBufferType(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 | 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 BSONError('only accepts single character String');
} else if (typeof byteValue !== 'number' && byteValue.length !== 1)
throw new BSONError('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 BSONError('only accepts number in a valid unsigned byte range 0-255');
}
if (this.buffer.byteLength > this.position) {
this.buffer[this.position++] = decodedByte;
} else {
const newSpace = ByteUtils.allocate(Binary.BUFFER_SIZE + this.buffer.length);
newSpace.set(this.buffer, 0);
this.buffer = newSpace;
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.byteLength < offset + sequence.length) {
const newSpace = ByteUtils.allocate(this.buffer.byteLength + sequence.length);
newSpace.set(this.buffer, 0);
// Assign the new buffer
this.buffer = newSpace;
}
if (ArrayBuffer.isView(sequence)) {
this.buffer.set(ByteUtils.toLocalBufferType(sequence), offset);
this.position =
offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
} else if (typeof sequence === 'string') {
const bytes = ByteUtils.fromISO88591(sequence);
this.buffer.set(bytes, offset);
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);
}
// TODO(NODE-4361): remove binary string support, value(true) should be the default / only option here.
return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
}
/** the length of the binary sequence */
length(): number {
return this.position;
}
toJSON(): string {
return ByteUtils.toBase64(this.buffer);
}
toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string {
if (encoding === 'hex') return ByteUtils.toHex(this.buffer);
if (encoding === 'base64') return ByteUtils.toBase64(this.buffer);
if (encoding === 'utf8' || encoding === 'utf-8') return ByteUtils.toUTF8(this.buffer);
return ByteUtils.toUTF8(this.buffer);
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended {
options = options || {};
const base64String = ByteUtils.toBase64(this.buffer);
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: Uint8Array | 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 = ByteUtils.fromBase64(doc.$binary);
} else {
if (typeof doc.$binary !== 'string') {
type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
data = ByteUtils.fromBase64(doc.$binary.base64);
}
}
} else if ('$uuid' in doc) {
type = 4;
data = uuidHexStringToBuffer(doc.$uuid);
}
if (!data) {
throw new BSONError(`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 {
return `new Binary(Buffer.from("${ByteUtils.toHex(this.buffer)}", "hex"), ${this.sub_type})`;
}
}
/** @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 | Uint8Array | UUID) {
let bytes: Uint8Array;
let hexStr;
if (input == null) {
bytes = UUID.generate();
} else if (input instanceof UUID) {
bytes = ByteUtils.toLocalBufferType(new Uint8Array(input.buffer));
hexStr = input.__id;
} else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
bytes = ByteUtils.toLocalBufferType(input);
} else if (typeof input === 'string') {
bytes = uuidHexStringToBuffer(input);
} else {
throw new BSONError(
'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(): Uint8Array {
return this.buffer;
}
set id(value: Uint8Array) {
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?: 'hex' | 'base64'): string {
if (encoding === 'hex') return ByteUtils.toHex(this.id);
if (encoding === 'base64') return ByteUtils.toBase64(this.id);
return 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 | Uint8Array | UUID): boolean {
if (!otherId) {
return false;
}
if (otherId instanceof UUID) {
return ByteUtils.equals(otherId.id, this.id);
}
try {
return ByteUtils.equals(new UUID(otherId).id, 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(): Uint8Array {
const bytes = ByteUtils.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 bytes;
}
/**
* Checks if a value is a valid bson UUID
* @param input - UUID, string or Buffer to validate.
*/
static isValid(input: string | Uint8Array | 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.byteLength !== 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()}")`;
}
}

250
node_modules/bson/src/bson.ts generated vendored Normal file
View file

@ -0,0 +1,250 @@
import { Binary, UUID } from './binary';
import { Code } from './code';
import { DBRef } from './db_ref';
import { Decimal128 } from './decimal128';
import { Double } from './double';
import { Int32 } from './int_32';
import { Long } from './long';
import { MaxKey } from './max_key';
import { MinKey } from './min_key';
import { ObjectId } from './objectid';
import { internalCalculateObjectSize } from './parser/calculate_size';
// Parts of the parser
import { internalDeserialize, DeserializeOptions } from './parser/deserializer';
import { serializeInto, SerializeOptions } from './parser/serializer';
import { BSONRegExp } from './regexp';
import { BSONSymbol } from './symbol';
import { Timestamp } from './timestamp';
import { ByteUtils } from './utils/byte_utils';
export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
export type { CodeExtended } from './code';
export type { DBRefLike } from './db_ref';
export type { Decimal128Extended } from './decimal128';
export type { DoubleExtended } from './double';
export type { EJSONOptions } 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 type { LongWithoutOverridesClass } from './timestamp';
export type { SerializeOptions, DeserializeOptions };
export {
Code,
BSONSymbol,
DBRef,
Binary,
ObjectId,
UUID,
Long,
Timestamp,
Double,
Int32,
MinKey,
MaxKey,
BSONRegExp,
Decimal128
};
export { BSONValue } from './bson_value';
export { BSONError, BSONVersionError, BSONRuntimeError } from './error';
export { BSONType } from './constants';
export { EJSON } from './extended_json';
/** @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 = ByteUtils.allocate(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 = ByteUtils.allocate(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 = {}): Uint8Array {
// 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 = ByteUtils.allocate(minInternalBufferSize);
}
// Attempt to serialize
const serializationIndex = serializeInto(
buffer,
object,
checkKeys,
0,
0,
serializeFunctions,
ignoreUndefined,
null
);
// Create the final buffer
const finishedBuffer = ByteUtils.allocate(serializationIndex);
// Copy into the finished buffer
finishedBuffer.set(buffer.subarray(0, serializationIndex), 0);
// 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: Uint8Array,
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 = serializeInto(
buffer,
object,
checkKeys,
0,
0,
serializeFunctions,
ignoreUndefined,
null
);
finalBuffer.set(buffer.subarray(0, serializationIndex), startIndex);
// 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: Uint8Array, options: DeserializeOptions = {}): Document {
return internalDeserialize(ByteUtils.toLocalBufferType(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: Uint8Array | ArrayBuffer,
startIndex: number,
numberOfDocuments: number,
documents: Document[],
docStartIndex: number,
options: DeserializeOptions
): number {
const internalOptions = Object.assign(
{ allowObjectSmallerThanBufferSize: true, index: 0 },
options
);
const bufferData = ByteUtils.toLocalBufferType(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;
}

18
node_modules/bson/src/bson_value.ts generated vendored Normal file
View file

@ -0,0 +1,18 @@
import { BSON_MAJOR_VERSION } from './constants';
/** @public */
export abstract class BSONValue {
/** @public */
public abstract get _bsontype(): string;
/** @internal */
get [Symbol.for('@@mdb.bson.version')](): typeof BSON_MAJOR_VERSION {
return BSON_MAJOR_VERSION;
}
/** @public */
public abstract inspect(): string;
/** @internal */
abstract toExtendedJSON(): unknown;
}

69
node_modules/bson/src/code.ts generated vendored Normal file
View file

@ -0,0 +1,69 @@
import type { Document } from './bson';
import { BSONValue } from './bson_value';
/** @public */
export interface CodeExtended {
$code: string;
$scope?: Document;
}
/**
* A class representation of the BSON Code type.
* @public
* @category BSONType
*/
export class Code extends BSONValue {
get _bsontype(): 'Code' {
return 'Code';
}
code: string;
// a code instance having a null scope is what determines whether
// it is BSONType 0x0D (just code) / 0x0F (code with scope)
scope: Document | null;
/**
* @param code - a string or function.
* @param scope - an optional scope for the function.
*/
constructor(code: string | Function, scope?: Document | null) {
super();
this.code = code.toString();
this.scope = scope ?? null;
}
toJSON(): { code: string; scope?: Document } {
if (this.scope != null) {
return { code: this.code, scope: this.scope };
}
return { code: this.code };
}
/** @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 != null ? `, ${JSON.stringify(codeJson.scope)}` : ''
})`;
}
}

141
node_modules/bson/src/constants.ts generated vendored Normal file
View file

@ -0,0 +1,141 @@
/** @internal */
export const BSON_MAJOR_VERSION = 5 as const;
/** @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;
/** @public */
export const BSONType = Object.freeze({
double: 1,
string: 2,
object: 3,
array: 4,
binData: 5,
undefined: 6,
objectId: 7,
bool: 8,
date: 9,
null: 10,
regex: 11,
dbPointer: 12,
javascript: 13,
symbol: 14,
javascriptWithScope: 15,
int: 16,
timestamp: 17,
long: 18,
decimal: 19,
minKey: -1,
maxKey: 127
} as const);
/** @public */
export type BSONType = typeof BSONType[keyof typeof BSONType];

127
node_modules/bson/src/db_ref.ts generated vendored Normal file
View file

@ -0,0 +1,127 @@
import type { Document } from './bson';
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';
import type { ObjectId } from './objectid';
/** @public */
export interface DBRefLike {
$ref: string;
$id: ObjectId;
$db?: string;
}
/** @internal */
export function isDBRefLike(value: unknown): value is DBRefLike {
return (
value != null &&
typeof value === 'object' &&
'$id' in value &&
value.$id != null &&
'$ref' in value &&
typeof value.$ref === 'string' &&
// If '$db' is defined it MUST be a string, otherwise it should be absent
(!('$db' in value) || ('$db' in value && typeof value.$db === 'string'))
);
}
/**
* A class representation of the BSON DBRef type.
* @public
* @category BSONType
*/
export class DBRef extends BSONValue {
get _bsontype(): 'DBRef' {
return '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) {
super();
// 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}"` : ''
})`;
}
}

777
node_modules/bson/src/decimal128.ts generated vendored Normal file
View file

@ -0,0 +1,777 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import { Long } from './long';
import { isUint8Array } from './parser/utils';
import { ByteUtils } from './utils/byte_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 = ByteUtils.fromNumberArray(
[
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 = ByteUtils.fromNumberArray(
[
0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse()
);
const INF_POSITIVE_BUFFER = ByteUtils.fromNumberArray(
[
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 BSONError(`"${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 extends BSONValue {
get _bsontype(): 'Decimal128' {
return 'Decimal128';
}
readonly bytes!: Uint8Array;
/**
* @param bytes - a buffer containing the raw Decimal128 bytes in little endian order,
* or a string representation as returned by .toString()
*/
constructor(bytes: Uint8Array | string) {
super();
if (typeof bytes === 'string') {
this.bytes = Decimal128.fromString(bytes).bytes;
} else if (isUint8Array(bytes)) {
if (bytes.byteLength !== 16) {
throw new BSONError('Decimal128 must take a Buffer of 16 bytes');
}
this.bytes = bytes;
} else {
throw new BSONError('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 BSONError('' + 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 BSONError('' + 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(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
} else if (representation[index] === 'N') {
return new Decimal128(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 BSONError('' + 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(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(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(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 = ByteUtils.allocate(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()}")`;
}
}

83
node_modules/bson/src/double.ts generated vendored Normal file
View file

@ -0,0 +1,83 @@
import { BSONValue } from './bson_value';
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 extends BSONValue {
get _bsontype(): 'Double' {
return 'Double';
}
value!: number;
/**
* Create a Double type
*
* @param value - the number we want to represent as a double.
*/
constructor(value: number) {
super();
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: '-0.0' };
}
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})`;
}
}

85
node_modules/bson/src/error.ts generated vendored Normal file
View file

@ -0,0 +1,85 @@
import { BSON_MAJOR_VERSION } from './constants';
/**
* @public
* @category Error
*
* `BSONError` objects are thrown when BSON ecounters an error.
*
* This is the parent class for all the other errors thrown by this library.
*/
export class BSONError extends Error {
/**
* @internal
* The underlying algorithm for isBSONError may change to improve how strict it is
* about determining if an input is a BSONError. But it must remain backwards compatible
* with previous minors & patches of the current major version.
*/
protected get bsonError(): true {
return true;
}
override get name(): string {
return 'BSONError';
}
constructor(message: string) {
super(message);
}
/**
* @public
*
* All errors thrown from the BSON library inherit from `BSONError`.
* This method can assist with determining if an error originates from the BSON library
* even if it does not pass an `instanceof` check against this class' constructor.
*
* @param value - any javascript value that needs type checking
*/
public static isBSONError(value: unknown): value is BSONError {
return (
value != null &&
typeof value === 'object' &&
'bsonError' in value &&
value.bsonError === true &&
// Do not access the following properties, just check existence
'name' in value &&
'message' in value &&
'stack' in value
);
}
}
/**
* @public
* @category Error
*/
export class BSONVersionError extends BSONError {
get name(): 'BSONVersionError' {
return 'BSONVersionError';
}
constructor() {
super(
`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`
);
}
}
/**
* @public
* @category Error
*
* An error generated when BSON functions encounter an unexpected input
* or reaches an unexpected/invalid internal state
*
*/
export class BSONRuntimeError extends BSONError {
get name(): 'BSONRuntimeError' {
return 'BSONRuntimeError';
}
constructor(message: string) {
super(message);
}
}

507
node_modules/bson/src/extended_json.ts generated vendored Normal file
View file

@ -0,0 +1,507 @@
import { Binary } from './binary';
import type { Document } from './bson';
import { Code } from './code';
import {
BSON_INT32_MAX,
BSON_INT32_MIN,
BSON_INT64_MAX,
BSON_INT64_MIN,
BSON_MAJOR_VERSION
} from './constants';
import { DBRef, isDBRefLike } from './db_ref';
import { Decimal128 } from './decimal128';
import { Double } from './double';
import { BSONError, BSONRuntimeError, BSONVersionError } 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, isRegExp, isMap } from './parser/utils';
import { BSONRegExp } from './regexp';
import { BSONSymbol } from './symbol';
import { Timestamp } from './timestamp';
/** @public */
export type EJSONOptions = {
/** 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;
/** Enable native bigint support */
useBigInt64?: boolean;
};
/** @internal */
type BSONType =
| Binary
| Code
| DBRef
| Decimal128
| Double
| Int32
| Long
| MaxKey
| MinKey
| ObjectId
| BSONRegExp
| BSONSymbol
| Timestamp;
function isBSONType(value: unknown): value is BSONType {
return (
value != null &&
typeof value === 'object' &&
'_bsontype' in value &&
typeof value._bsontype === 'string'
);
}
// 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: EJSONOptions = {}) {
if (typeof value === 'number') {
// TODO(NODE-4377): EJSON js number handling diverges from BSON
const in32BitRange = value <= BSON_INT32_MAX && value >= BSON_INT32_MIN;
const in64BitRange = value <= BSON_INT64_MAX && value >= BSON_INT64_MIN;
if (options.relaxed || options.legacy) {
return value;
}
if (Number.isInteger(value) && !Object.is(value, -0)) {
// interpret as being of the smallest BSON integer type that can represent the number exactly
if (in32BitRange) {
return new Int32(value);
}
if (in64BitRange) {
if (options.useBigInt64) {
// eslint-disable-next-line no-restricted-globals -- This is allowed here as useBigInt64=true
return BigInt(value);
}
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 === 'bigint') date.setTime(Number(d));
else throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof 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);
else if (typeof d === 'bigint') date.setTime(Number(d));
else throw new BSONRuntimeError(`Unrecognized type for EJSON date: ${typeof 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 = EJSONOptions & {
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 (value instanceof Map || isMap(value)) {
const obj: Record<string, unknown> = Object.create(null);
for (const [k, v] of value) {
if (typeof k !== 'string') {
throw new BSONError('Can only serialize maps with string keys');
}
obj[k] = v;
}
return serializeValue(obj, options);
}
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 BSONError(
'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))) {
if (Number.isInteger(value) && !Object.is(value, -0)) {
// interpret as being of the smallest BSON integer type that can represent the number exactly
if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) {
return { $numberInt: value.toString() };
}
if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) {
// TODO(NODE-4377): EJSON js number handling diverges from BSON
return { $numberLong: value.toString() };
}
}
return { $numberDouble: Object.is(value, -0) ? '-0.0' : value.toString() };
}
if (typeof value === 'bigint') {
/* eslint-disable no-restricted-globals -- This is allowed as we are accepting a bigint as input */
if (!options.relaxed) {
return { $numberLong: BigInt.asIntN(64, value).toString() };
}
return Number(BigInt.asIntN(64, value));
/* eslint-enable */
}
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),
BSONRegExp: (o: BSONRegExp) => new BSONRegExp(o.pattern, o.options),
BSONSymbol: (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 of Object.keys(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 (
doc != null &&
typeof doc === 'object' &&
typeof doc._bsontype === 'string' &&
doc[Symbol.for('@@mdb.bson.version')] !== BSON_MAJOR_VERSION
) {
throw new BSONVersionError();
} 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 BSONError('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);
}
}
/**
* 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));
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parse(text: string, options?: EJSONOptions): any {
const ejsonOptions = {
useBigInt64: options?.useBigInt64 ?? false,
relaxed: options?.relaxed ?? true,
legacy: options?.legacy ?? false
};
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, ejsonOptions);
});
}
/**
* 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(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | EJSONOptions,
space?: string | number,
options?: EJSONOptions
): 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
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function EJSONserialize(value: any, options?: EJSONOptions): 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
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function EJSONdeserialize(ejson: Document, options?: EJSONOptions): any {
options = options || {};
return parse(JSON.stringify(ejson), options);
}
/** @public */
const EJSON: {
parse: typeof parse;
stringify: typeof stringify;
serialize: typeof EJSONserialize;
deserialize: typeof EJSONdeserialize;
} = Object.create(null);
EJSON.parse = parse;
EJSON.stringify = stringify;
EJSON.serialize = EJSONserialize;
EJSON.deserialize = EJSONdeserialize;
Object.freeze(EJSON);
export { EJSON };

19
node_modules/bson/src/index.ts generated vendored Normal file
View file

@ -0,0 +1,19 @@
import * as BSON from './bson';
// Export all named properties from BSON to support
// import { ObjectId, serialize } from 'bson';
// const { ObjectId, serialize } = require('bson');
export * from './bson';
// Export BSON as a namespace to support:
// import { BSON } from 'bson';
// const { BSON } = require('bson');
export { BSON };
// BSON does **NOT** have a default export
// The following will crash in es module environments
// import BSON from 'bson';
// The following will work as expected, BSON as a namespace of all the APIs (BSON.ObjectId, BSON.serialize)
// const BSON = require('bson');

70
node_modules/bson/src/int_32.ts generated vendored Normal file
View file

@ -0,0 +1,70 @@
import { BSONValue } from './bson_value';
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 extends BSONValue {
get _bsontype(): 'Int32' {
return 'Int32';
}
value!: number;
/**
* Create an Int32 type
*
* @param value - the number we want to represent as an int32.
*/
constructor(value: number | string) {
super();
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()})`;
}
}

1067
node_modules/bson/src/long.ts generated vendored Normal file

File diff suppressed because it is too large Load diff

36
node_modules/bson/src/max_key.ts generated vendored Normal file
View file

@ -0,0 +1,36 @@
import { BSONValue } from './bson_value';
/** @public */
export interface MaxKeyExtended {
$maxKey: 1;
}
/**
* A class representation of the BSON MaxKey type.
* @public
* @category BSONType
*/
export class MaxKey extends BSONValue {
get _bsontype(): 'MaxKey' {
return '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()';
}
}

36
node_modules/bson/src/min_key.ts generated vendored Normal file
View file

@ -0,0 +1,36 @@
import { BSONValue } from './bson_value';
/** @public */
export interface MinKeyExtended {
$minKey: 1;
}
/**
* A class representation of the BSON MinKey type.
* @public
* @category BSONType
*/
export class MinKey extends BSONValue {
get _bsontype(): 'MinKey' {
return '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()';
}
}

317
node_modules/bson/src/objectid.ts generated vendored Normal file
View file

@ -0,0 +1,317 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import { isUint8Array } from './parser/utils';
import { BSONDataView, ByteUtils } from './utils/byte_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 | Uint8Array;
__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 extends BSONValue {
get _bsontype(): 'ObjectId' {
return 'ObjectId';
}
/** @internal */
private static index = Math.floor(Math.random() * 0xffffff);
static cacheHexString: boolean;
/** ObjectId Bytes @internal */
private [kId]!: Uint8Array;
/** 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 | Uint8Array) {
super();
// 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 BSONError('Argument passed in must have an id that is of type string or Buffer');
}
if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
workingId = ByteUtils.fromHex(inputId.toHexString());
} 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] = ByteUtils.toLocalBufferType(workingId);
} else if (typeof workingId === 'string') {
if (workingId.length === 12) {
// TODO(NODE-4361): Remove string of length 12 support
const bytes = ByteUtils.fromUTF8(workingId);
if (bytes.byteLength === 12) {
this[kId] = bytes;
} else {
throw new BSONError('Argument passed in must be a string of 12 bytes');
}
} else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
this[kId] = ByteUtils.fromHex(workingId);
} else {
throw new BSONError(
'Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer'
);
}
} else {
throw new BSONError('Argument passed in does not match the accepted types');
}
// If we are caching the hex string
if (ObjectId.cacheHexString) {
this.__id = ByteUtils.toHex(this.id);
}
}
/**
* The ObjectId bytes
* @readonly
*/
get id(): Uint8Array {
return this[kId];
}
set id(value: Uint8Array) {
this[kId] = value;
if (ObjectId.cacheHexString) {
this.__id = ByteUtils.toHex(value);
}
}
/** Returns the ObjectId id as a 24 character hex string representation */
toHexString(): string {
if (ObjectId.cacheHexString && this.__id) {
return this.__id;
}
const hexString = ByteUtils.toHex(this.id);
if (ObjectId.cacheHexString && !this.__id) {
this.__id = hexString;
}
return hexString;
}
/**
* Update the ObjectId index
* @internal
*/
private 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): Uint8Array {
if ('number' !== typeof time) {
time = Math.floor(Date.now() / 1000);
}
const inc = ObjectId.getInc();
const buffer = ByteUtils.allocate(12);
// 4-byte timestamp
BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
// set PROCESS_UNIQUE if yet not initialized
if (PROCESS_UNIQUE === null) {
PROCESS_UNIQUE = ByteUtils.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, unless encoding is provided.
* @param encoding - hex or base64
*/
toString(encoding?: 'hex' | 'base64'): string {
// Is the id a buffer then use the buffer toString method to return the format
if (encoding === 'base64') return ByteUtils.toBase64(this.id);
if (encoding === 'hex') return this.toHexString();
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] && ByteUtils.equals(this[kId], otherId[kId]);
}
if (
typeof otherId === 'string' &&
ObjectId.isValid(otherId) &&
otherId.length === 12 &&
isUint8Array(this.id)
) {
return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
}
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 ByteUtils.equals(ByteUtils.fromUTF8(otherId), 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 = BSONDataView.fromUint8Array(this.id).getUint32(0, false);
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 = ByteUtils.fromNumberArray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
// Encode time into first 4 bytes
BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
// 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 BSONError(
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
);
}
return new ObjectId(ByteUtils.fromHex(hexString));
}
/**
* Checks if a value is a valid bson ObjectId
*
* @param id - ObjectId instance to validate.
*/
static isValid(id: string | number | ObjectId | ObjectIdLike | 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()}")`;
}
}

211
node_modules/bson/src/parser/calculate_size.ts generated vendored Normal file
View file

@ -0,0 +1,211 @@
import { Binary } from '../binary';
import type { Document } from '../bson';
import { BSONVersionError } from '../error';
import * as constants from '../constants';
import { ByteUtils } from '../utils/byte_utils';
import { isAnyArrayBuffer, isDate, isRegExp } from './utils';
export function internalCalculateObjectSize(
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 of Object.keys(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 + ByteUtils.utf8ByteLength(name) + 1 + 4 + ByteUtils.utf8ByteLength(value) + 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 ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (4 + 1);
} else {
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
}
} else {
// 64 bit
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
}
case 'undefined':
if (isArray || !ignoreUndefined)
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
return 0;
case 'boolean':
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 1);
case 'object':
if (
value != null &&
typeof value._bsontype === 'string' &&
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
) {
throw new BSONVersionError();
} else if (value == null || value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
} else if (value._bsontype === 'ObjectId') {
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (12 + 1);
} else if (value instanceof Date || isDate(value)) {
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
} else if (
ArrayBuffer.isView(value) ||
value instanceof ArrayBuffer ||
isAnyArrayBuffer(value)
) {
return (
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 4 + 1) + value.byteLength
);
} else if (
value._bsontype === 'Long' ||
value._bsontype === 'Double' ||
value._bsontype === 'Timestamp'
) {
return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
} else if (value._bsontype === 'Decimal128') {
return (name != null ? ByteUtils.utf8ByteLength(name) + 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 ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
1 +
4 +
4 +
ByteUtils.utf8ByteLength(value.code.toString()) +
1 +
internalCalculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
);
} else {
return (
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
1 +
4 +
ByteUtils.utf8ByteLength(value.code.toString()) +
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 ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
(binary.position + 1 + 4 + 1 + 4)
);
} else {
return (
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (binary.position + 1 + 4 + 1)
);
}
} else if (value._bsontype === 'Symbol') {
return (
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
ByteUtils.utf8ByteLength(value.value) +
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 ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
1 +
internalCalculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined)
);
} else if (value instanceof RegExp || isRegExp(value)) {
return (
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
1 +
ByteUtils.utf8ByteLength(value.source) +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1
);
} else if (value._bsontype === 'BSONRegExp') {
return (
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
1 +
ByteUtils.utf8ByteLength(value.pattern) +
1 +
ByteUtils.utf8ByteLength(value.options) +
1
);
} else {
return (
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
internalCalculateObjectSize(value, serializeFunctions, ignoreUndefined) +
1
);
}
case 'function':
if (serializeFunctions) {
return (
(name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
1 +
4 +
ByteUtils.utf8ByteLength(value.toString()) +
1
);
}
}
return 0;
}

726
node_modules/bson/src/parser/deserializer.ts generated vendored Normal file
View file

@ -0,0 +1,726 @@
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 { BSONDataView, ByteUtils } from '../utils/byte_utils';
import { validateUtf8 } from '../validate_utf8';
/** @public */
export interface DeserializeOptions {
/** when deserializing a Long will return as a BigInt. */
useBigInt64?: 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);
export function internalDeserialize(
buffer: Uint8Array,
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 deserialization
return deserializeObject(buffer, index, options, isArray);
}
const allowedDBRefKeys = /^\$ref$|^\$id$|^\$db$/;
function deserializeObject(
buffer: Uint8Array,
index: number,
options: DeserializeOptions,
isArray = false
) {
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 ?? false;
const promoteLongs = options.promoteLongs ?? true;
const promoteValues = options.promoteValues ?? true;
const useBigInt64 = options.useBigInt64 ?? false;
if (useBigInt64 && !promoteValues) {
throw new BSONError('Must either request bigint or Long for int64 deserialization');
}
if (useBigInt64 && !promoteLongs) {
throw new BSONError('Must either request bigint or Long for int64 deserialization');
}
// 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++ : ByteUtils.toUTF8(buffer.subarray(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 = ByteUtils.allocate(12);
oid.set(buffer.subarray(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: DeserializeOptions = options;
// Stop index
const stopIndex = index + objectSize;
// All elements of array to be returned as raw bson
if (fieldsAsRaw && fieldsAsRaw[name]) {
arrayOptions = { ...options, 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 dataview = BSONDataView.fromUint8Array(buffer.subarray(index, index + 8));
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);
if (useBigInt64) {
value = dataview.getBigInt64(0, true);
} else if (promoteLongs && promoteValues === true) {
// Promote the long if possible
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 = ByteUtils.allocate(16);
// Copy the next 16 bytes into the bytes buffer
bytes.set(buffer.subarray(index, index + 16), 0);
// Update index
index = index + 16;
// Assign the new Decimal128 value
value = new Decimal128(bytes);
} 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 = ByteUtils.toLocalBufferType(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 = ByteUtils.allocate(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 = ByteUtils.toUTF8(buffer.subarray(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 = ByteUtils.toUTF8(buffer.subarray(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 = ByteUtils.toUTF8(buffer.subarray(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 = ByteUtils.toUTF8(buffer.subarray(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) {
// We intentionally **do not** use bit shifting here
// Bit shifting in javascript coerces numbers to **signed** int32s
// We need to keep i, and t unsigned
const i =
buffer[index++] +
buffer[index++] * (1 << 8) +
buffer[index++] * (1 << 16) +
buffer[index++] * (1 << 24);
const t =
buffer[index++] +
buffer[index++] * (1 << 8) +
buffer[index++] * (1 << 16) +
buffer[index++] * (1 << 24);
value = new Timestamp({ i, t });
} 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
);
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');
}
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 = ByteUtils.toUTF8(buffer.subarray(index, index + stringSize - 1));
// Update parse index position
index = index + stringSize;
// Read the oid
const oidBuffer = ByteUtils.allocate(12);
oidBuffer.set(buffer.subarray(index, index + 12), 0);
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;
}
function getValidatedString(
buffer: Uint8Array,
start: number,
end: number,
shouldValidateUtf8: boolean
) {
const value = ByteUtils.toUTF8(buffer.subarray(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;
}

988
node_modules/bson/src/parser/serializer.ts generated vendored Normal file
View file

@ -0,0 +1,988 @@
import { Binary } from '../binary';
import type { BSONSymbol, DBRef, Document, MaxKey } from '../bson';
import type { Code } from '../code';
import * as constants from '../constants';
import type { DBRefLike } from '../db_ref';
import type { Decimal128 } from '../decimal128';
import type { Double } from '../double';
import { BSONError, BSONVersionError } from '../error';
import type { Int32 } from '../int_32';
import { Long } from '../long';
import type { MinKey } from '../min_key';
import type { ObjectId } from '../objectid';
import type { BSONRegExp } from '../regexp';
import { ByteUtils } from '../utils/byte_utils';
import { isAnyArrayBuffer, isDate, isMap, isRegExp, isUint8Array } from './utils';
/** @public */
export interface SerializeOptions {
/** the serializer will check if keys are valid. */
checkKeys?: boolean;
/** serialize the javascript functions **(default:false)**. */
serializeFunctions?: boolean;
/** serialize will not emit undefined fields **(default:true)** */
ignoreUndefined?: boolean;
/** @internal Resize internal buffer */
minInternalBufferSize?: number;
/** the index in the buffer where we wish to start serializing into */
index?: number;
}
const regexp = /\x00/; // eslint-disable-line no-control-regex
const 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: Uint8Array, key: string, value: string, index: number) {
// Encode String type
buffer[index++] = constants.BSON_DATA_STRING;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes + 1;
buffer[index - 1] = 0;
// Write the string
const size = ByteUtils.encodeUTF8Into(buffer, value, index + 4);
// 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;
}
const NUMBER_SPACE = new DataView(new ArrayBuffer(8), 0, 8);
const FOUR_BYTE_VIEW_ON_NUMBER = new Uint8Array(NUMBER_SPACE.buffer, 0, 4);
const EIGHT_BYTE_VIEW_ON_NUMBER = new Uint8Array(NUMBER_SPACE.buffer, 0, 8);
function serializeNumber(buffer: Uint8Array, key: string, value: number, index: number) {
const isNegativeZero = Object.is(value, -0);
const type =
!isNegativeZero &&
Number.isSafeInteger(value) &&
value <= constants.BSON_INT32_MAX &&
value >= constants.BSON_INT32_MIN
? constants.BSON_DATA_INT
: constants.BSON_DATA_NUMBER;
if (type === constants.BSON_DATA_INT) {
NUMBER_SPACE.setInt32(0, value, true);
} else {
NUMBER_SPACE.setFloat64(0, value, true);
}
const bytes =
type === constants.BSON_DATA_INT ? FOUR_BYTE_VIEW_ON_NUMBER : EIGHT_BYTE_VIEW_ON_NUMBER;
buffer[index++] = type;
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
index = index + numberOfWrittenBytes;
buffer[index++] = 0x00;
buffer.set(bytes, index);
index += bytes.byteLength;
return index;
}
function serializeBigInt(buffer: Uint8Array, key: string, value: bigint, index: number) {
buffer[index++] = constants.BSON_DATA_LONG;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index += numberOfWrittenBytes;
buffer[index++] = 0;
NUMBER_SPACE.setBigInt64(0, value, true);
// Write BigInt value
buffer.set(EIGHT_BYTE_VIEW_ON_NUMBER, index);
index += EIGHT_BYTE_VIEW_ON_NUMBER.byteLength;
return index;
}
function serializeNull(buffer: Uint8Array, key: string, _: unknown, index: number) {
// Set long type
buffer[index++] = constants.BSON_DATA_NULL;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
return index;
}
function serializeBoolean(buffer: Uint8Array, key: string, value: boolean, index: number) {
// Write the type
buffer[index++] = constants.BSON_DATA_BOOLEAN;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Encode the boolean value
buffer[index++] = value ? 1 : 0;
return index;
}
function serializeDate(buffer: Uint8Array, key: string, value: Date, index: number) {
// Write the type
buffer[index++] = constants.BSON_DATA_DATE;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the date
const dateInMilis = Long.fromNumber(value.getTime());
const lowBits = dateInMilis.getLowBits();
const 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: Uint8Array, key: string, value: RegExp, index: number) {
// Write the type
buffer[index++] = constants.BSON_DATA_REGEXP;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
if (value.source && value.source.match(regexp) != null) {
throw new BSONError('value ' + value.source + ' must not contain null bytes');
}
// Adjust the index
index = index + ByteUtils.encodeUTF8Into(buffer, value.source, index);
// 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: Uint8Array, key: string, value: BSONRegExp, index: number) {
// Write the type
buffer[index++] = constants.BSON_DATA_REGEXP;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// 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 new BSONError('pattern ' + value.pattern + ' must not contain null bytes');
}
// Adjust the index
index = index + ByteUtils.encodeUTF8Into(buffer, value.pattern, index);
// Write zero
buffer[index++] = 0x00;
// Write the options
const sortedOptions = value.options.split('').sort().join('');
index = index + ByteUtils.encodeUTF8Into(buffer, sortedOptions, index);
// Add ending zero
buffer[index++] = 0x00;
return index;
}
function serializeMinMax(buffer: Uint8Array, key: string, value: MinKey | MaxKey, index: number) {
// 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
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
return index;
}
function serializeObjectId(buffer: Uint8Array, key: string, value: ObjectId, index: number) {
// Write the type
buffer[index++] = constants.BSON_DATA_OID;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the objectId into the shared buffer
if (isUint8Array(value.id)) {
buffer.set(value.id.subarray(0, 12), index);
} else {
throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
}
// Adjust index
return index + 12;
}
function serializeBuffer(buffer: Uint8Array, key: string, value: Uint8Array, index: number) {
// Write the type
buffer[index++] = constants.BSON_DATA_BINARY;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Get size of the buffer (current write point)
const 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(value, index);
// Adjust the index
index = index + size;
return index;
}
function serializeObject(
buffer: Uint8Array,
key: string,
value: Document,
index: number,
checkKeys: boolean,
depth: number,
serializeFunctions: boolean,
ignoreUndefined: boolean,
path: Set<Document>
) {
if (path.has(value)) {
throw new BSONError('Cannot convert circular structure to BSON');
}
path.add(value);
// Write the type
buffer[index++] = Array.isArray(value) ? constants.BSON_DATA_ARRAY : constants.BSON_DATA_OBJECT;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
const endIndex = serializeInto(
buffer,
value,
checkKeys,
index,
depth + 1,
serializeFunctions,
ignoreUndefined,
path
);
path.delete(value);
return endIndex;
}
function serializeDecimal128(buffer: Uint8Array, key: string, value: Decimal128, index: number) {
buffer[index++] = constants.BSON_DATA_DECIMAL128;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the data from the value
buffer.set(value.bytes.subarray(0, 16), index);
return index + 16;
}
function serializeLong(buffer: Uint8Array, key: string, value: Long, index: number) {
// Write the type
buffer[index++] =
value._bsontype === 'Long' ? constants.BSON_DATA_LONG : constants.BSON_DATA_TIMESTAMP;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the date
const lowBits = value.getLowBits();
const 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: Uint8Array, key: string, value: Int32 | number, index: number) {
value = value.valueOf();
// Set int type 32 bits or less
buffer[index++] = constants.BSON_DATA_INT;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// 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: Uint8Array, key: string, value: Double, index: number) {
// Encode as double
buffer[index++] = constants.BSON_DATA_NUMBER;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write float
NUMBER_SPACE.setFloat64(0, value.value, true);
buffer.set(EIGHT_BYTE_VIEW_ON_NUMBER, index);
// Adjust index
index = index + 8;
return index;
}
function serializeFunction(buffer: Uint8Array, key: string, value: Function, index: number) {
buffer[index++] = constants.BSON_DATA_CODE;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Function string
const functionString = value.toString();
// Write the string
const size = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 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: Uint8Array,
key: string,
value: Code,
index: number,
checkKeys = false,
depth = 0,
serializeFunctions = false,
ignoreUndefined = true,
path: Set<Document>
) {
if (value.scope && typeof value.scope === 'object') {
// Write the type
buffer[index++] = constants.BSON_DATA_CODE_W_SCOPE;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Starting index
let startIndex = index;
// Serialize the function
// Get the function string
const functionString = value.code;
// Index adjustment
index = index + 4;
// Write string into buffer
const codeSize = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 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
const endIndex = serializeInto(
buffer,
value.scope,
checkKeys,
index,
depth + 1,
serializeFunctions,
ignoreUndefined,
path
);
index = endIndex - 1;
// Writ the total
const 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
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Function string
const functionString = value.code.toString();
// Write the string
const size = ByteUtils.encodeUTF8Into(buffer, functionString, index + 4) + 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: Uint8Array, key: string, value: Binary, index: number) {
// Write the type
buffer[index++] = constants.BSON_DATA_BINARY;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Extract the buffer
const data = value.buffer;
// Calculate size
let size = value.position;
// Add the deprecated 02 type 4 bytes of size to total
if (value.sub_type === 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.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: Uint8Array, key: string, value: BSONSymbol, index: number) {
// Write the type
buffer[index++] = constants.BSON_DATA_SYMBOL;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the string
const size = ByteUtils.encodeUTF8Into(buffer, value.value, index + 4) + 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: Uint8Array,
key: string,
value: DBRef,
index: number,
depth: number,
serializeFunctions: boolean,
path: Set<Document>
) {
// Write the type
buffer[index++] = constants.BSON_DATA_OBJECT;
// Number of written bytes
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
let startIndex = index;
let output: DBRefLike = {
$ref: value.collection || value.namespace, // "namespace" was what library 1.x called "collection"
$id: value.oid
};
if (value.db != null) {
output.$db = value.db;
}
output = Object.assign(output, value.fields);
const endIndex = serializeInto(
buffer,
output,
false,
index,
depth + 1,
serializeFunctions,
true,
path
);
// Calculate object size
const 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;
}
export function serializeInto(
buffer: Uint8Array,
object: Document,
checkKeys: boolean,
startingIndex: number,
depth: number,
serializeFunctions: boolean,
ignoreUndefined: boolean,
path: Set<Document> | null
): number {
if (path == null) {
// We are at the root input
if (object == null) {
// ONLY the root should turn into an empty document
// BSON Empty document has a size of 5 (LE)
buffer[0] = 0x05;
buffer[1] = 0x00;
buffer[2] = 0x00;
buffer[3] = 0x00;
// All documents end with null terminator
buffer[4] = 0x00;
return 5;
}
if (Array.isArray(object)) {
throw new BSONError('serialize does not support an array as the root input');
}
if (typeof object !== 'object') {
throw new BSONError('serialize does not support non-object as the root input');
} else if ('_bsontype' in object && typeof object._bsontype === 'string') {
throw new BSONError(`BSON types cannot be serialized as a document`);
} else if (
isDate(object) ||
isRegExp(object) ||
isUint8Array(object) ||
isAnyArrayBuffer(object)
) {
throw new BSONError(`date, regexp, typedarray, and arraybuffer cannot be BSON documents`);
}
path = new Set();
}
// Push the object to the path
path.add(object);
// Start place to serialize into
let index = startingIndex + 4;
// Special case isArray
if (Array.isArray(object)) {
// Get object keys
for (let i = 0; i < object.length; i++) {
const key = `${i}`;
let value = object[i];
// Is there an override value
if (typeof value?.toBSON === 'function') {
value = value.toBSON();
}
if (typeof value === 'string') {
index = serializeString(buffer, key, value, index);
} else if (typeof value === 'number') {
index = serializeNumber(buffer, key, value, index);
} else if (typeof value === 'bigint') {
index = serializeBigInt(buffer, key, value, index);
} else if (typeof value === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
} else if (value instanceof Date || isDate(value)) {
index = serializeDate(buffer, key, value, index);
} else if (value === undefined) {
index = serializeNull(buffer, key, value, index);
} else if (value === null) {
index = serializeNull(buffer, key, value, index);
} else if (isUint8Array(value)) {
index = serializeBuffer(buffer, key, value, index);
} else if (value instanceof RegExp || isRegExp(value)) {
index = serializeRegExp(buffer, key, value, index);
} else if (typeof value === 'object' && value._bsontype == null) {
index = serializeObject(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined,
path
);
} else if (
typeof value === 'object' &&
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
) {
throw new BSONVersionError();
} else if (value._bsontype === 'ObjectId') {
index = serializeObjectId(buffer, key, value, index);
} else if (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 (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(buffer, key, value, index);
} else if (value._bsontype === 'Code') {
index = serializeCode(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined,
path
);
} else if (value._bsontype === 'Binary') {
index = serializeBinary(buffer, key, value, index);
} else if (value._bsontype === 'BSONSymbol') {
index = serializeSymbol(buffer, key, value, index);
} else if (value._bsontype === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
} 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 BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
}
}
} else if (object instanceof Map || isMap(object)) {
const iterator = object.entries();
let done = false;
while (!done) {
// Unpack the next entry
const entry = iterator.next();
done = !!entry.done;
// Are we done, then skip and terminate
if (done) continue;
// Get the entry values
const key = entry.value[0];
let value = entry.value[1];
if (typeof value?.toBSON === 'function') {
value = value.toBSON();
}
// Check the type of the value
const 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 new BSONError('key ' + key + ' must not contain null bytes');
}
if (checkKeys) {
if ('$' === key[0]) {
throw new BSONError('key ' + key + " must not start with '$'");
} else if (~key.indexOf('.')) {
throw new BSONError('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') {
index = serializeBigInt(buffer, key, value, index);
} else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
} else if (value instanceof Date || isDate(value)) {
index = serializeDate(buffer, key, value, index);
} else if (value === null || (value === undefined && ignoreUndefined === false)) {
index = serializeNull(buffer, key, value, index);
} else if (isUint8Array(value)) {
index = serializeBuffer(buffer, key, value, index);
} else if (value instanceof RegExp || 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,
path
);
} else if (
typeof value === 'object' &&
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
) {
throw new BSONVersionError();
} else if (value._bsontype === 'ObjectId') {
index = serializeObjectId(buffer, key, value, index);
} 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,
path
);
} else if (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(buffer, key, value, index);
} else if (value._bsontype === 'Binary') {
index = serializeBinary(buffer, key, value, index);
} else if (value._bsontype === 'BSONSymbol') {
index = serializeSymbol(buffer, key, value, index);
} else if (value._bsontype === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
} 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 BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
}
}
} else {
if (typeof object?.toBSON === 'function') {
// Provided a custom serialization method
object = object.toBSON();
if (object != null && typeof object !== 'object') {
throw new BSONError('toBSON function did not return an object');
}
}
// Iterate over all the keys
for (const key of Object.keys(object)) {
let value = object[key];
// Is there an override value
if (typeof value?.toBSON === 'function') {
value = value.toBSON();
}
// Check the type of the value
const 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 new BSONError('key ' + key + ' must not contain null bytes');
}
if (checkKeys) {
if ('$' === key[0]) {
throw new BSONError('key ' + key + " must not start with '$'");
} else if (~key.indexOf('.')) {
throw new BSONError('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') {
index = serializeBigInt(buffer, key, value, index);
} else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
} else if (value instanceof Date || 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 (isUint8Array(value)) {
index = serializeBuffer(buffer, key, value, index);
} else if (value instanceof RegExp || 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,
path
);
} else if (
typeof value === 'object' &&
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
) {
throw new BSONVersionError();
} else if (value._bsontype === 'ObjectId') {
index = serializeObjectId(buffer, key, value, index);
} 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,
path
);
} else if (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(buffer, key, value, index);
} else if (value._bsontype === 'Binary') {
index = serializeBinary(buffer, key, value, index);
} else if (value._bsontype === 'BSONSymbol') {
index = serializeSymbol(buffer, key, value, index);
} else if (value._bsontype === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
} 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 BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
}
}
}
// Remove the path
path.delete(object);
// Final padding byte for object
buffer[index++] = 0x00;
// Final size
const 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;
}

29
node_modules/bson/src/parser/utils.ts generated vendored Normal file
View file

@ -0,0 +1,29 @@
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]';
}
export function isDate(d: unknown): d is Date {
return Object.prototype.toString.call(d) === '[object Date]';
}

114
node_modules/bson/src/regexp.ts generated vendored Normal file
View file

@ -0,0 +1,114 @@
import { BSONValue } from './bson_value';
import { BSONError } 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 extends BSONValue {
get _bsontype(): 'BSONRegExp' {
return 'BSONRegExp';
}
pattern!: string;
options!: string;
/**
* @param pattern - The regular expression pattern to match
* @param options - The regular expression options
*/
constructor(pattern: string, options?: string) {
super();
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 BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
}
}

58
node_modules/bson/src/symbol.ts generated vendored Normal file
View file

@ -0,0 +1,58 @@
import { BSONValue } from './bson_value';
/** @public */
export interface BSONSymbolExtended {
$symbol: string;
}
/**
* A class representation of the BSON Symbol type.
* @public
* @category BSONType
*/
export class BSONSymbol extends BSONValue {
get _bsontype(): 'BSONSymbol' {
return 'BSONSymbol';
}
value!: string;
/**
* @param value - the string representing the symbol.
*/
constructor(value: string) {
super();
this.value = value;
}
/** Access the wrapped string value. */
valueOf(): string {
return this.value;
}
toString(): string {
return this.value;
}
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();
}
}

150
node_modules/bson/src/timestamp.ts generated vendored Normal file
View file

@ -0,0 +1,150 @@
import { BSONError } from './error';
import type { Int32 } from './int_32';
import { Long } from './long';
/** @public */
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
/** @public */
export type LongWithoutOverrides = new (
low: unknown,
high?: number | boolean,
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 {
get _bsontype(): 'Timestamp' {
return 'Timestamp';
}
static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
/**
* @param int - A 64-bit bigint representing the Timestamp.
*/
constructor(int: bigint);
/**
* @param long - 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 });
constructor(low?: bigint | Long | { t: number | Int32; i: number | Int32 }) {
if (low == null) {
super(0, 0, true);
} else if (typeof low === 'bigint') {
super(low, true);
} else if (Long.isLong(low)) {
super(low.low, low.high, true);
} else if (typeof low === 'object' && 't' in low && 'i' in low) {
if (typeof low.t !== 'number' && (typeof low.t !== 'object' || low.t._bsontype !== 'Int32')) {
throw new BSONError('Timestamp constructed from { t, i } must provide t as a number');
}
if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) {
throw new BSONError('Timestamp constructed from { t, i } must provide i as a number');
}
if (low.t < 0) {
throw new BSONError('Timestamp constructed from { t, i } must provide a positive t');
}
if (low.i < 0) {
throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
}
if (low.t > 0xffff_ffff) {
throw new BSONError(
'Timestamp constructed from { t, i } must provide t equal or less than uint32 max'
);
}
if (low.i > 0xffff_ffff) {
throw new BSONError(
'Timestamp constructed from { t, i } must provide i equal or less than uint32 max'
);
}
super(low.i.valueOf(), low.t.valueOf(), true);
} else {
throw new BSONError(
'A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }'
);
}
}
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({ i: lowBits, t: 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 {
// The Long check is necessary because extended JSON has different behavior given the size of the input number
const i = Long.isLong(doc.$timestamp.i)
? doc.$timestamp.i.getLowBitsUnsigned() // Need to fetch the least significant 32 bits
: doc.$timestamp.i;
const t = Long.isLong(doc.$timestamp.t)
? doc.$timestamp.t.getLowBitsUnsigned() // Need to fetch the least significant 32 bits
: doc.$timestamp.t;
return new Timestamp({ t, i });
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
}
}

61
node_modules/bson/src/utils/byte_utils.ts generated vendored Normal file
View file

@ -0,0 +1,61 @@
import { nodeJsByteUtils } from './node_byte_utils';
import { webByteUtils } from './web_byte_utils';
/** @internal */
export type ByteUtils = {
/** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */
toLocalBufferType(buffer: Uint8Array | ArrayBufferView | ArrayBuffer): Uint8Array;
/** Create empty space of size */
allocate: (size: number) => Uint8Array;
/** Check if two Uint8Arrays are deep equal */
equals: (a: Uint8Array, b: Uint8Array) => boolean;
/** Check if two Uint8Arrays are deep equal */
fromNumberArray: (array: number[]) => Uint8Array;
/** Create a Uint8Array from a base64 string */
fromBase64: (base64: string) => Uint8Array;
/** Create a base64 string from bytes */
toBase64: (buffer: Uint8Array) => string;
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
fromISO88591: (codePoints: string) => Uint8Array;
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
toISO88591: (buffer: Uint8Array) => string;
/** Create a Uint8Array from a hex string */
fromHex: (hex: string) => Uint8Array;
/** Create a hex string from bytes */
toHex: (buffer: Uint8Array) => string;
/** Create a Uint8Array containing utf8 code units from a string */
fromUTF8: (text: string) => Uint8Array;
/** Create a string from utf8 code units */
toUTF8: (buffer: Uint8Array) => string;
/** Get the utf8 code unit count from a string if it were to be transformed to utf8 */
utf8ByteLength: (input: string) => number;
/** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */
encodeUTF8Into(destination: Uint8Array, source: string, byteOffset: number): number;
/** Generate a Uint8Array filled with random bytes with byteLength */
randomBytes(byteLength: number): Uint8Array;
};
declare const Buffer: { new (): unknown; prototype?: { _isBuffer?: boolean } } | undefined;
/**
* Check that a global Buffer exists that is a function and
* does not have a '_isBuffer' property defined on the prototype
* (this is to prevent using the npm buffer)
*/
const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
/**
* This is the only ByteUtils that should be used across the rest of the BSON library.
*
* The type annotation is important here, it asserts that each of the platform specific
* utils implementations are compatible with the common one.
*
* @internal
*/
export const ByteUtils: ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
export class BSONDataView extends DataView {
static fromUint8Array(input: Uint8Array) {
return new DataView(input.buffer, input.byteOffset, input.byteLength);
}
}

141
node_modules/bson/src/utils/node_byte_utils.ts generated vendored Normal file
View file

@ -0,0 +1,141 @@
import { BSONError } from '../error';
type NodeJsEncoding = 'base64' | 'hex' | 'utf8' | 'binary';
type NodeJsBuffer = ArrayBufferView &
Uint8Array & {
write(string: string, offset: number, length: undefined, encoding: 'utf8'): number;
copy(target: Uint8Array, targetStart: number, sourceStart: number, sourceEnd: number): number;
toString: (this: Uint8Array, encoding: NodeJsEncoding) => string;
equals: (this: Uint8Array, other: Uint8Array) => boolean;
};
type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
alloc: (size: number) => NodeJsBuffer;
from(array: number[]): NodeJsBuffer;
from(array: Uint8Array): NodeJsBuffer;
from(array: ArrayBuffer): NodeJsBuffer;
from(array: ArrayBuffer, byteOffset: number, byteLength: number): NodeJsBuffer;
from(base64: string, encoding: NodeJsEncoding): NodeJsBuffer;
byteLength(input: string, encoding: 'utf8'): number;
isBuffer(value: unknown): value is NodeJsBuffer;
};
// This can be nullish, but we gate the nodejs functions on being exported whether or not this exists
// Node.js global
declare const Buffer: NodeJsBufferConstructor;
declare const require: (mod: 'crypto') => { randomBytes: (byteLength: number) => Uint8Array };
/** @internal */
export function nodejsMathRandomBytes(byteLength: number) {
return nodeJsByteUtils.fromNumberArray(
Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256))
);
}
/**
* @internal
* WARNING: REQUIRE WILL BE REWRITTEN
*
* This code is carefully used by require_rewriter.mjs any modifications must be reflected in the plugin.
*
* @remarks
* "crypto" is the only dependency BSON needs. This presents a problem for creating a bundle of the BSON library
* in an es module format that can be used both on the browser and in Node.js. In Node.js when BSON is imported as
* an es module, there will be no global require function defined, making the code below fallback to the much less desireable math.random bytes.
* In order to make our es module bundle work as expected on Node.js we need to change this `require()` to a dynamic import, and the dynamic
* import must be top-level awaited since es modules are async. So we rely on a custom rollup plugin to seek out the following lines of code
* and replace `require` with `await import` and the IIFE line (`nodejsRandomBytes = (() => { ... })()`) with `nodejsRandomBytes = await (async () => { ... })()`
* when generating an es module bundle.
*/
const nodejsRandomBytes: (byteLength: number) => Uint8Array = (() => {
try {
return require('crypto').randomBytes;
} catch {
return nodejsMathRandomBytes;
}
})();
/** @internal */
export const nodeJsByteUtils = {
toLocalBufferType(potentialBuffer: Uint8Array | NodeJsBuffer | ArrayBuffer): NodeJsBuffer {
if (Buffer.isBuffer(potentialBuffer)) {
return potentialBuffer;
}
if (ArrayBuffer.isView(potentialBuffer)) {
return Buffer.from(
potentialBuffer.buffer,
potentialBuffer.byteOffset,
potentialBuffer.byteLength
);
}
const stringTag =
potentialBuffer?.[Symbol.toStringTag] ?? Object.prototype.toString.call(potentialBuffer);
if (
stringTag === 'ArrayBuffer' ||
stringTag === 'SharedArrayBuffer' ||
stringTag === '[object ArrayBuffer]' ||
stringTag === '[object SharedArrayBuffer]'
) {
return Buffer.from(potentialBuffer);
}
throw new BSONError(`Cannot create Buffer from ${String(potentialBuffer)}`);
},
allocate(size: number): NodeJsBuffer {
return Buffer.alloc(size);
},
equals(a: Uint8Array, b: Uint8Array): boolean {
return nodeJsByteUtils.toLocalBufferType(a).equals(b);
},
fromNumberArray(array: number[]): NodeJsBuffer {
return Buffer.from(array);
},
fromBase64(base64: string): NodeJsBuffer {
return Buffer.from(base64, 'base64');
},
toBase64(buffer: Uint8Array): string {
return nodeJsByteUtils.toLocalBufferType(buffer).toString('base64');
},
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
fromISO88591(codePoints: string): NodeJsBuffer {
return Buffer.from(codePoints, 'binary');
},
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
toISO88591(buffer: Uint8Array): string {
return nodeJsByteUtils.toLocalBufferType(buffer).toString('binary');
},
fromHex(hex: string): NodeJsBuffer {
return Buffer.from(hex, 'hex');
},
toHex(buffer: Uint8Array): string {
return nodeJsByteUtils.toLocalBufferType(buffer).toString('hex');
},
fromUTF8(text: string): NodeJsBuffer {
return Buffer.from(text, 'utf8');
},
toUTF8(buffer: Uint8Array): string {
return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8');
},
utf8ByteLength(input: string): number {
return Buffer.byteLength(input, 'utf8');
},
encodeUTF8Into(buffer: Uint8Array, source: string, byteOffset: number): number {
return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8');
},
randomBytes: nodejsRandomBytes
};

190
node_modules/bson/src/utils/web_byte_utils.ts generated vendored Normal file
View file

@ -0,0 +1,190 @@
import { BSONError } from '../error';
type TextDecoder = {
readonly encoding: string;
readonly fatal: boolean;
readonly ignoreBOM: boolean;
decode(input?: Uint8Array): string;
};
type TextDecoderConstructor = {
new (label: 'utf8', options: { fatal: boolean; ignoreBOM?: boolean }): TextDecoder;
};
type TextEncoder = {
readonly encoding: string;
encode(input?: string): Uint8Array;
};
type TextEncoderConstructor = {
new (): TextEncoder;
};
// Web global
declare const TextDecoder: TextDecoderConstructor;
declare const TextEncoder: TextEncoderConstructor;
declare const atob: (base64: string) => string;
declare const btoa: (binary: string) => string;
type ArrayBufferViewWithTag = ArrayBufferView & {
[Symbol.toStringTag]?: string;
};
function isReactNative() {
const { navigator } = globalThis as { navigator?: { product?: string } };
return typeof navigator === 'object' && navigator.product === 'ReactNative';
}
/** @internal */
export function webMathRandomBytes(byteLength: number) {
if (byteLength < 0) {
throw new RangeError(`The argument 'byteLength' is invalid. Received ${byteLength}`);
}
return webByteUtils.fromNumberArray(
Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256))
);
}
/** @internal */
const webRandomBytes: (byteLength: number) => Uint8Array = (() => {
const { crypto } = globalThis as {
crypto?: { getRandomValues?: (space: Uint8Array) => Uint8Array };
};
if (crypto != null && typeof crypto.getRandomValues === 'function') {
return (byteLength: number) => {
// @ts-expect-error: crypto.getRandomValues cannot actually be null here
// You cannot separate getRandomValues from crypto (need to have this === crypto)
return crypto.getRandomValues(webByteUtils.allocate(byteLength));
};
} else {
if (isReactNative()) {
const { console } = globalThis as { console?: { warn?: (message: string) => void } };
console?.warn?.(
'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
);
}
return webMathRandomBytes;
}
})();
const HEX_DIGIT = /(\d|[a-f])/i;
/** @internal */
export const webByteUtils = {
toLocalBufferType(
potentialUint8array: Uint8Array | ArrayBufferViewWithTag | ArrayBuffer
): Uint8Array {
const stringTag =
potentialUint8array?.[Symbol.toStringTag] ??
Object.prototype.toString.call(potentialUint8array);
if (stringTag === 'Uint8Array') {
return potentialUint8array as Uint8Array;
}
if (ArrayBuffer.isView(potentialUint8array)) {
return new Uint8Array(
potentialUint8array.buffer.slice(
potentialUint8array.byteOffset,
potentialUint8array.byteOffset + potentialUint8array.byteLength
)
);
}
if (
stringTag === 'ArrayBuffer' ||
stringTag === 'SharedArrayBuffer' ||
stringTag === '[object ArrayBuffer]' ||
stringTag === '[object SharedArrayBuffer]'
) {
return new Uint8Array(potentialUint8array);
}
throw new BSONError(`Cannot make a Uint8Array from ${String(potentialUint8array)}`);
},
allocate(size: number): Uint8Array {
if (typeof size !== 'number') {
throw new TypeError(`The "size" argument must be of type number. Received ${String(size)}`);
}
return new Uint8Array(size);
},
equals(a: Uint8Array, b: Uint8Array): boolean {
if (a.byteLength !== b.byteLength) {
return false;
}
for (let i = 0; i < a.byteLength; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
},
fromNumberArray(array: number[]): Uint8Array {
return Uint8Array.from(array);
},
fromBase64(base64: string): Uint8Array {
return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
},
toBase64(uint8array: Uint8Array): string {
return btoa(webByteUtils.toISO88591(uint8array));
},
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
fromISO88591(codePoints: string): Uint8Array {
return Uint8Array.from(codePoints, c => c.charCodeAt(0) & 0xff);
},
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
toISO88591(uint8array: Uint8Array): string {
return Array.from(Uint16Array.from(uint8array), b => String.fromCharCode(b)).join('');
},
fromHex(hex: string): Uint8Array {
const evenLengthHex = hex.length % 2 === 0 ? hex : hex.slice(0, hex.length - 1);
const buffer = [];
for (let i = 0; i < evenLengthHex.length; i += 2) {
const firstDigit = evenLengthHex[i];
const secondDigit = evenLengthHex[i + 1];
if (!HEX_DIGIT.test(firstDigit)) {
break;
}
if (!HEX_DIGIT.test(secondDigit)) {
break;
}
const hexDigit = Number.parseInt(`${firstDigit}${secondDigit}`, 16);
buffer.push(hexDigit);
}
return Uint8Array.from(buffer);
},
toHex(uint8array: Uint8Array): string {
return Array.from(uint8array, byte => byte.toString(16).padStart(2, '0')).join('');
},
fromUTF8(text: string): Uint8Array {
return new TextEncoder().encode(text);
},
toUTF8(uint8array: Uint8Array): string {
return new TextDecoder('utf8', { fatal: false }).decode(uint8array);
},
utf8ByteLength(input: string): number {
return webByteUtils.fromUTF8(input).byteLength;
},
encodeUTF8Into(buffer: Uint8Array, source: string, byteOffset: number): number {
const bytes = webByteUtils.fromUTF8(source);
buffer.set(bytes, byteOffset);
return bytes.byteLength;
},
randomBytes: webRandomBytes
};

33
node_modules/bson/src/uuid_utils.ts generated vendored Normal file
View file

@ -0,0 +1,33 @@
import { BSONError } from './error';
import { ByteUtils } from './utils/byte_utils';
// 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): Uint8Array => {
if (!uuidValidateString(hexString)) {
throw new BSONError(
'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 ByteUtils.fromHex(sanitizedHexString);
};
export function bufferToUuidHexString(buffer: Uint8Array, includeDashes = true): string {
if (includeDashes) {
return [
ByteUtils.toHex(buffer.subarray(0, 4)),
ByteUtils.toHex(buffer.subarray(4, 6)),
ByteUtils.toHex(buffer.subarray(6, 8)),
ByteUtils.toHex(buffer.subarray(8, 10)),
ByteUtils.toHex(buffer.subarray(10, 16))
].join('-');
}
return ByteUtils.toHex(buffer);
}

47
node_modules/bson/src/validate_utf8.ts generated vendored Normal file
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;
}