something is working

This commit is contained in:
quoije
2022-09-15 01:55:36 -04:00
parent a0aa251c27
commit f38e69883c
1123 changed files with 251731 additions and 11 deletions
+481
View File
@@ -0,0 +1,481 @@
import { Buffer } from 'buffer';
import { ensureBuffer } from './ensure_buffer';
import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils';
import { isUint8Array, randomBytes } from './parser/utils';
import type { EJSONOptions } from './extended_json';
import { BSONError, BSONTypeError } from './error';
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
/** @public */
export type BinarySequence = Uint8Array | Buffer | number[];
/** @public */
export interface BinaryExtendedLegacy {
$type: string;
$binary: string;
}
/** @public */
export interface BinaryExtended {
$binary: {
subType: string;
base64: string;
};
}
/**
* A class representation of the BSON Binary type.
* @public
* @category BSONType
*/
export class Binary {
_bsontype!: 'Binary';
/**
* Binary default subtype
* @internal
*/
private static readonly BSON_BINARY_SUBTYPE_DEFAULT = 0;
/** Initial buffer default size */
static readonly BUFFER_SIZE = 256;
/** Default BSON type */
static readonly SUBTYPE_DEFAULT = 0;
/** Function BSON type */
static readonly SUBTYPE_FUNCTION = 1;
/** Byte Array BSON type */
static readonly SUBTYPE_BYTE_ARRAY = 2;
/** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
static readonly SUBTYPE_UUID_OLD = 3;
/** UUID BSON type */
static readonly SUBTYPE_UUID = 4;
/** MD5 BSON type */
static readonly SUBTYPE_MD5 = 5;
/** Encrypted BSON type */
static readonly SUBTYPE_ENCRYPTED = 6;
/** Column BSON type */
static readonly SUBTYPE_COLUMN = 7;
/** User BSON type */
static readonly SUBTYPE_USER_DEFINED = 128;
buffer!: Buffer;
sub_type!: number;
position!: number;
/**
* Create a new Binary instance.
*
* This constructor can accept a string as its first argument. In this case,
* this string will be encoded using ISO-8859-1, **not** using UTF-8.
* This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
* instead to convert the string to a Buffer using UTF-8 first.
*
* @param buffer - a buffer object containing the binary data.
* @param subType - the option binary type.
*/
constructor(buffer?: string | BinarySequence, subType?: number) {
if (!(this instanceof Binary)) return new Binary(buffer, subType);
if (
!(buffer == null) &&
!(typeof buffer === 'string') &&
!ArrayBuffer.isView(buffer) &&
!(buffer instanceof ArrayBuffer) &&
!Array.isArray(buffer)
) {
throw new BSONTypeError(
'Binary can only be constructed from string, Buffer, TypedArray, or Array<number>'
);
}
this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
if (buffer == null) {
// create an empty binary buffer
this.buffer = Buffer.alloc(Binary.BUFFER_SIZE);
this.position = 0;
} else {
if (typeof buffer === 'string') {
// string
this.buffer = Buffer.from(buffer, 'binary');
} else if (Array.isArray(buffer)) {
// number[]
this.buffer = Buffer.from(buffer);
} else {
// Buffer | TypedArray | ArrayBuffer
this.buffer = ensureBuffer(buffer);
}
this.position = this.buffer.byteLength;
}
}
/**
* Updates this binary with byte_value.
*
* @param byteValue - a single byte we wish to write.
*/
put(byteValue: string | number | Uint8Array | Buffer | number[]): void {
// If it's a string and a has more than one character throw an error
if (typeof byteValue === 'string' && byteValue.length !== 1) {
throw new BSONTypeError('only accepts single character String');
} else if (typeof byteValue !== 'number' && byteValue.length !== 1)
throw new BSONTypeError('only accepts single character Uint8Array or Array');
// Decode the byte value once
let decodedByte: number;
if (typeof byteValue === 'string') {
decodedByte = byteValue.charCodeAt(0);
} else if (typeof byteValue === 'number') {
decodedByte = byteValue;
} else {
decodedByte = byteValue[0];
}
if (decodedByte < 0 || decodedByte > 255) {
throw new BSONTypeError('only accepts number in a valid unsigned byte range 0-255');
}
if (this.buffer.length > this.position) {
this.buffer[this.position++] = decodedByte;
} else {
const buffer = Buffer.alloc(Binary.BUFFER_SIZE + this.buffer.length);
// Combine the two buffers together
this.buffer.copy(buffer, 0, 0, this.buffer.length);
this.buffer = buffer;
this.buffer[this.position++] = decodedByte;
}
}
/**
* Writes a buffer or string to the binary.
*
* @param sequence - a string or buffer to be written to the Binary BSON object.
* @param offset - specify the binary of where to write the content.
*/
write(sequence: string | BinarySequence, offset: number): void {
offset = typeof offset === 'number' ? offset : this.position;
// If the buffer is to small let's extend the buffer
if (this.buffer.length < offset + sequence.length) {
const buffer = Buffer.alloc(this.buffer.length + sequence.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
// Assign the new buffer
this.buffer = buffer;
}
if (ArrayBuffer.isView(sequence)) {
this.buffer.set(ensureBuffer(sequence), offset);
this.position =
offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
} else if (typeof sequence === 'string') {
this.buffer.write(sequence, offset, sequence.length, 'binary');
this.position =
offset + sequence.length > this.position ? offset + sequence.length : this.position;
}
}
/**
* Reads **length** bytes starting at **position**.
*
* @param position - read from the given position in the Binary.
* @param length - the number of bytes to read.
*/
read(position: number, length: number): BinarySequence {
length = length && length > 0 ? length : this.position;
// Let's return the data based on the type we have
return this.buffer.slice(position, position + length);
}
/**
* Returns the value of this binary as a string.
* @param asRaw - Will skip converting to a string
* @remarks
* This is handy when calling this function conditionally for some key value pairs and not others
*/
value(asRaw?: boolean): string | BinarySequence {
asRaw = !!asRaw;
// Optimize to serialize for the situation where the data == size of buffer
if (asRaw && this.buffer.length === this.position) {
return this.buffer;
}
// If it's a node.js buffer object
if (asRaw) {
return this.buffer.slice(0, this.position);
}
return this.buffer.toString('binary', 0, this.position);
}
/** the length of the binary sequence */
length(): number {
return this.position;
}
toJSON(): string {
return this.buffer.toString('base64');
}
toString(format?: string): string {
return this.buffer.toString(format);
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended {
options = options || {};
const base64String = this.buffer.toString('base64');
const subType = Number(this.sub_type).toString(16);
if (options.legacy) {
return {
$binary: base64String,
$type: subType.length === 1 ? '0' + subType : subType
};
}
return {
$binary: {
base64: base64String,
subType: subType.length === 1 ? '0' + subType : subType
}
};
}
toUUID(): UUID {
if (this.sub_type === Binary.SUBTYPE_UUID) {
return new UUID(this.buffer.slice(0, this.position));
}
throw new BSONError(
`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`
);
}
/** @internal */
static fromExtendedJSON(
doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended,
options?: EJSONOptions
): Binary {
options = options || {};
let data: Buffer | undefined;
let type;
if ('$binary' in doc) {
if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) {
type = doc.$type ? parseInt(doc.$type, 16) : 0;
data = Buffer.from(doc.$binary, 'base64');
} else {
if (typeof doc.$binary !== 'string') {
type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
data = Buffer.from(doc.$binary.base64, 'base64');
}
}
} else if ('$uuid' in doc) {
type = 4;
data = uuidHexStringToBuffer(doc.$uuid);
}
if (!data) {
throw new BSONTypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
}
return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
const asBuffer = this.value(true);
return `new Binary(Buffer.from("${asBuffer.toString('hex')}", "hex"), ${this.sub_type})`;
}
}
Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
/** @public */
export type UUIDExtended = {
$uuid: string;
};
const UUID_BYTE_LENGTH = 16;
/**
* A class representation of the BSON UUID type.
* @public
*/
export class UUID extends Binary {
static cacheHexString: boolean;
/** UUID hexString cache @internal */
private __id?: string;
/**
* Create an UUID type
*
* @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
*/
constructor(input?: string | Buffer | UUID) {
let bytes;
let hexStr;
if (input == null) {
bytes = UUID.generate();
} else if (input instanceof UUID) {
bytes = Buffer.from(input.buffer);
hexStr = input.__id;
} else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
bytes = ensureBuffer(input);
} else if (typeof input === 'string') {
bytes = uuidHexStringToBuffer(input);
} else {
throw new BSONTypeError(
'Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'
);
}
super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
this.__id = hexStr;
}
/**
* The UUID bytes
* @readonly
*/
get id(): Buffer {
return this.buffer;
}
set id(value: Buffer) {
this.buffer = value;
if (UUID.cacheHexString) {
this.__id = bufferToUuidHexString(value);
}
}
/**
* Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
* @param includeDashes - should the string exclude dash-separators.
* */
toHexString(includeDashes = true): string {
if (UUID.cacheHexString && this.__id) {
return this.__id;
}
const uuidHexString = bufferToUuidHexString(this.id, includeDashes);
if (UUID.cacheHexString) {
this.__id = uuidHexString;
}
return uuidHexString;
}
/**
* Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
*/
toString(encoding?: string): string {
return encoding ? this.id.toString(encoding) : this.toHexString();
}
/**
* Converts the id into its JSON string representation.
* A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
*/
toJSON(): string {
return this.toHexString();
}
/**
* Compares the equality of this UUID with `otherID`.
*
* @param otherId - UUID instance to compare against.
*/
equals(otherId: string | Buffer | UUID): boolean {
if (!otherId) {
return false;
}
if (otherId instanceof UUID) {
return otherId.id.equals(this.id);
}
try {
return new UUID(otherId).id.equals(this.id);
} catch {
return false;
}
}
/**
* Creates a Binary instance from the current UUID.
*/
toBinary(): Binary {
return new Binary(this.id, Binary.SUBTYPE_UUID);
}
/**
* Generates a populated buffer containing a v4 uuid
*/
static generate(): Buffer {
const bytes = randomBytes(UUID_BYTE_LENGTH);
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
// Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
return Buffer.from(bytes);
}
/**
* Checks if a value is a valid bson UUID
* @param input - UUID, string or Buffer to validate.
*/
static isValid(input: string | Buffer | UUID): boolean {
if (!input) {
return false;
}
if (input instanceof UUID) {
return true;
}
if (typeof input === 'string') {
return uuidValidateString(input);
}
if (isUint8Array(input)) {
// check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
if (input.length !== UUID_BYTE_LENGTH) {
return false;
}
return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
}
return false;
}
/**
* Creates an UUID from a hex string representation of an UUID.
* @param hexString - 32 or 36 character hex string (dashes excluded/included).
*/
static createFromHexString(hexString: string): UUID {
const buffer = uuidHexStringToBuffer(hexString);
return new UUID(buffer);
}
/**
* Converts to a string representation of this Id.
*
* @returns return the 36 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new UUID("${this.toHexString()}")`;
}
}
+330
View File
@@ -0,0 +1,330 @@
import { Buffer } from 'buffer';
import { Binary, UUID } from './binary';
import { Code } from './code';
import { DBRef } from './db_ref';
import { Decimal128 } from './decimal128';
import { Double } from './double';
import { ensureBuffer } from './ensure_buffer';
import { EJSON } from './extended_json';
import { Int32 } from './int_32';
import { Long } from './long';
import { Map } from './map';
import { MaxKey } from './max_key';
import { MinKey } from './min_key';
import { ObjectId } from './objectid';
import { BSONError, BSONTypeError } from './error';
import { calculateObjectSize as internalCalculateObjectSize } from './parser/calculate_size';
// Parts of the parser
import { deserialize as internalDeserialize, DeserializeOptions } from './parser/deserializer';
import { serializeInto as internalSerialize, SerializeOptions } from './parser/serializer';
import { BSONRegExp } from './regexp';
import { BSONSymbol } from './symbol';
import { Timestamp } from './timestamp';
export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
export type { CodeExtended } from './code';
export {
BSON_BINARY_SUBTYPE_BYTE_ARRAY,
BSON_BINARY_SUBTYPE_DEFAULT,
BSON_BINARY_SUBTYPE_FUNCTION,
BSON_BINARY_SUBTYPE_MD5,
BSON_BINARY_SUBTYPE_USER_DEFINED,
BSON_BINARY_SUBTYPE_UUID,
BSON_BINARY_SUBTYPE_UUID_NEW,
BSON_BINARY_SUBTYPE_ENCRYPTED,
BSON_BINARY_SUBTYPE_COLUMN,
BSON_DATA_ARRAY,
BSON_DATA_BINARY,
BSON_DATA_BOOLEAN,
BSON_DATA_CODE,
BSON_DATA_CODE_W_SCOPE,
BSON_DATA_DATE,
BSON_DATA_DBPOINTER,
BSON_DATA_DECIMAL128,
BSON_DATA_INT,
BSON_DATA_LONG,
BSON_DATA_MAX_KEY,
BSON_DATA_MIN_KEY,
BSON_DATA_NULL,
BSON_DATA_NUMBER,
BSON_DATA_OBJECT,
BSON_DATA_OID,
BSON_DATA_REGEXP,
BSON_DATA_STRING,
BSON_DATA_SYMBOL,
BSON_DATA_TIMESTAMP,
BSON_DATA_UNDEFINED,
BSON_INT32_MAX,
BSON_INT32_MIN,
BSON_INT64_MAX,
BSON_INT64_MIN
} from './constants';
export type { DBRefLike } from './db_ref';
export type { Decimal128Extended } from './decimal128';
export type { DoubleExtended } from './double';
export type { EJSONOptions } from './extended_json';
export { EJSON } from './extended_json';
export type { Int32Extended } from './int_32';
export type { LongExtended } from './long';
export type { MaxKeyExtended } from './max_key';
export type { MinKeyExtended } from './min_key';
export type { ObjectIdExtended, ObjectIdLike } from './objectid';
export type { BSONRegExpExtended, BSONRegExpExtendedLegacy } from './regexp';
export type { BSONSymbolExtended } from './symbol';
export type { LongWithoutOverrides, TimestampExtended, TimestampOverrides } from './timestamp';
export { LongWithoutOverridesClass } from './timestamp';
export type { SerializeOptions, DeserializeOptions };
export {
Code,
Map,
BSONSymbol,
DBRef,
Binary,
ObjectId,
UUID,
Long,
Timestamp,
Double,
Int32,
MinKey,
MaxKey,
BSONRegExp,
Decimal128,
// In 4.0.0 and 4.0.1, this property name was changed to ObjectId to match the class name.
// This caused interoperability problems with previous versions of the library, so in
// later builds we changed it back to ObjectID (capital D) to match legacy implementations.
ObjectId as ObjectID
};
export { BSONError, BSONTypeError } from './error';
/** @public */
export interface Document {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
/** @internal */
// Default Max Size
const MAXSIZE = 1024 * 1024 * 17;
// Current Internal Temporary Serialization Buffer
let buffer = Buffer.alloc(MAXSIZE);
/**
* Sets the size of the internal serialization buffer.
*
* @param size - The desired size for the internal serialization buffer
* @public
*/
export function setInternalBufferSize(size: number): void {
// Resize the internal serialization buffer if needed
if (buffer.length < size) {
buffer = Buffer.alloc(size);
}
}
/**
* Serialize a Javascript object.
*
* @param object - the Javascript object to serialize.
* @returns Buffer object containing the serialized object.
* @public
*/
export function serialize(object: Document, options: SerializeOptions = {}): Buffer {
// Unpack the options
const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
const serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
const ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
const minInternalBufferSize =
typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;
// Resize the internal serialization buffer if needed
if (buffer.length < minInternalBufferSize) {
buffer = Buffer.alloc(minInternalBufferSize);
}
// Attempt to serialize
const serializationIndex = internalSerialize(
buffer,
object,
checkKeys,
0,
0,
serializeFunctions,
ignoreUndefined,
[]
);
// Create the final buffer
const finishedBuffer = Buffer.alloc(serializationIndex);
// Copy into the finished buffer
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
// Return the buffer
return finishedBuffer;
}
/**
* Serialize a Javascript object using a predefined Buffer and index into the buffer,
* useful when pre-allocating the space for serialization.
*
* @param object - the Javascript object to serialize.
* @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.
* @returns the index pointing to the last written byte in the buffer.
* @public
*/
export function serializeWithBufferAndIndex(
object: Document,
finalBuffer: Buffer,
options: SerializeOptions = {}
): number {
// Unpack the options
const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
const serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
const ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
const startIndex = typeof options.index === 'number' ? options.index : 0;
// Attempt to serialize
const serializationIndex = internalSerialize(
buffer,
object,
checkKeys,
0,
0,
serializeFunctions,
ignoreUndefined
);
buffer.copy(finalBuffer, startIndex, 0, serializationIndex);
// Return the index
return startIndex + serializationIndex - 1;
}
/**
* Deserialize data as BSON.
*
* @param buffer - the buffer containing the serialized set of BSON documents.
* @returns returns the deserialized Javascript Object.
* @public
*/
export function deserialize(
buffer: Buffer | ArrayBufferView | ArrayBuffer,
options: DeserializeOptions = {}
): Document {
return internalDeserialize(buffer instanceof Buffer ? buffer : ensureBuffer(buffer), options);
}
/** @public */
export type CalculateObjectSizeOptions = Pick<
SerializeOptions,
'serializeFunctions' | 'ignoreUndefined'
>;
/**
* Calculate the bson size for a passed in Javascript object.
*
* @param object - the Javascript object to calculate the BSON byte size for
* @returns size of BSON object in bytes
* @public
*/
export function calculateObjectSize(
object: Document,
options: CalculateObjectSizeOptions = {}
): number {
options = options || {};
const serializeFunctions =
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
const ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
return internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined);
}
/**
* Deserialize stream data as BSON documents.
*
* @param data - the buffer containing the serialized set of BSON documents.
* @param startIndex - the start index in the data Buffer where the deserialization is to start.
* @param numberOfDocuments - number of documents to deserialize.
* @param documents - an array where to store the deserialized documents.
* @param docStartIndex - the index in the documents array from where to start inserting documents.
* @param options - additional options used for the deserialization.
* @returns next index in the buffer after deserialization **x** numbers of documents.
* @public
*/
export function deserializeStream(
data: Buffer | ArrayBufferView | ArrayBuffer,
startIndex: number,
numberOfDocuments: number,
documents: Document[],
docStartIndex: number,
options: DeserializeOptions
): number {
const internalOptions = Object.assign(
{ allowObjectSmallerThanBufferSize: true, index: 0 },
options
);
const bufferData = ensureBuffer(data);
let index = startIndex;
// Loop over all documents
for (let i = 0; i < numberOfDocuments; i++) {
// Find size of the document
const size =
bufferData[index] |
(bufferData[index + 1] << 8) |
(bufferData[index + 2] << 16) |
(bufferData[index + 3] << 24);
// Update options with index
internalOptions.index = index;
// Parse the document at this point
documents[docStartIndex + i] = internalDeserialize(bufferData, internalOptions);
// Adjust index by the document size
index = index + size;
}
// Return object containing end index of parsing and list of documents
return index;
}
/**
* BSON default export
* @deprecated Please use named exports
* @privateRemarks
* We want to someday deprecate the default export,
* so none of the new TS types are being exported on the default
* @public
*/
const BSON = {
Binary,
Code,
DBRef,
Decimal128,
Double,
Int32,
Long,
UUID,
Map,
MaxKey,
MinKey,
ObjectId,
ObjectID: ObjectId,
BSONRegExp,
BSONSymbol,
Timestamp,
EJSON,
setInternalBufferSize,
serialize,
serializeWithBufferAndIndex,
deserialize,
calculateObjectSize,
deserializeStream,
BSONError,
BSONTypeError
};
export default BSON;
+61
View File
@@ -0,0 +1,61 @@
import type { Document } from './bson';
/** @public */
export interface CodeExtended {
$code: string | Function;
$scope?: Document;
}
/**
* A class representation of the BSON Code type.
* @public
* @category BSONType
*/
export class Code {
_bsontype!: 'Code';
code!: string | Function;
scope?: Document;
/**
* @param code - a string or function.
* @param scope - an optional scope for the function.
*/
constructor(code: string | Function, scope?: Document) {
if (!(this instanceof Code)) return new Code(code, scope);
this.code = code;
this.scope = scope;
}
toJSON(): { code: string | Function; scope?: Document } {
return { code: this.code, scope: this.scope };
}
/** @internal */
toExtendedJSON(): CodeExtended {
if (this.scope) {
return { $code: this.code, $scope: this.scope };
}
return { $code: this.code };
}
/** @internal */
static fromExtendedJSON(doc: CodeExtended): Code {
return new Code(doc.$code, doc.$scope);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
const codeJson = this.toJSON();
return `new Code("${String(codeJson.code)}"${
codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''
})`;
}
}
Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });
+110
View File
@@ -0,0 +1,110 @@
/** @internal */
export const BSON_INT32_MAX = 0x7fffffff;
/** @internal */
export const BSON_INT32_MIN = -0x80000000;
/** @internal */
export const BSON_INT64_MAX = Math.pow(2, 63) - 1;
/** @internal */
export const BSON_INT64_MIN = -Math.pow(2, 63);
/**
* Any integer up to 2^53 can be precisely represented by a double.
* @internal
*/
export const JS_INT_MAX = Math.pow(2, 53);
/**
* Any integer down to -2^53 can be precisely represented by a double.
* @internal
*/
export const JS_INT_MIN = -Math.pow(2, 53);
/** Number BSON Type @internal */
export const BSON_DATA_NUMBER = 1;
/** String BSON Type @internal */
export const BSON_DATA_STRING = 2;
/** Object BSON Type @internal */
export const BSON_DATA_OBJECT = 3;
/** Array BSON Type @internal */
export const BSON_DATA_ARRAY = 4;
/** Binary BSON Type @internal */
export const BSON_DATA_BINARY = 5;
/** Binary BSON Type @internal */
export const BSON_DATA_UNDEFINED = 6;
/** ObjectId BSON Type @internal */
export const BSON_DATA_OID = 7;
/** Boolean BSON Type @internal */
export const BSON_DATA_BOOLEAN = 8;
/** Date BSON Type @internal */
export const BSON_DATA_DATE = 9;
/** null BSON Type @internal */
export const BSON_DATA_NULL = 10;
/** RegExp BSON Type @internal */
export const BSON_DATA_REGEXP = 11;
/** Code BSON Type @internal */
export const BSON_DATA_DBPOINTER = 12;
/** Code BSON Type @internal */
export const BSON_DATA_CODE = 13;
/** Symbol BSON Type @internal */
export const BSON_DATA_SYMBOL = 14;
/** Code with Scope BSON Type @internal */
export const BSON_DATA_CODE_W_SCOPE = 15;
/** 32 bit Integer BSON Type @internal */
export const BSON_DATA_INT = 16;
/** Timestamp BSON Type @internal */
export const BSON_DATA_TIMESTAMP = 17;
/** Long BSON Type @internal */
export const BSON_DATA_LONG = 18;
/** Decimal128 BSON Type @internal */
export const BSON_DATA_DECIMAL128 = 19;
/** MinKey BSON Type @internal */
export const BSON_DATA_MIN_KEY = 0xff;
/** MaxKey BSON Type @internal */
export const BSON_DATA_MAX_KEY = 0x7f;
/** Binary Default Type @internal */
export const BSON_BINARY_SUBTYPE_DEFAULT = 0;
/** Binary Function Type @internal */
export const BSON_BINARY_SUBTYPE_FUNCTION = 1;
/** Binary Byte Array Type @internal */
export const BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
/** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
export const BSON_BINARY_SUBTYPE_UUID = 3;
/** Binary UUID Type @internal */
export const BSON_BINARY_SUBTYPE_UUID_NEW = 4;
/** Binary MD5 Type @internal */
export const BSON_BINARY_SUBTYPE_MD5 = 5;
/** Encrypted BSON type @internal */
export const BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
/** Column BSON type @internal */
export const BSON_BINARY_SUBTYPE_COLUMN = 7;
/** Binary User Defined Type @internal */
export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
+124
View File
@@ -0,0 +1,124 @@
import type { Document } from './bson';
import type { EJSONOptions } from './extended_json';
import type { ObjectId } from './objectid';
import { isObjectLike } from './parser/utils';
/** @public */
export interface DBRefLike {
$ref: string;
$id: ObjectId;
$db?: string;
}
/** @internal */
export function isDBRefLike(value: unknown): value is DBRefLike {
return (
isObjectLike(value) &&
value.$id != null &&
typeof value.$ref === 'string' &&
(value.$db == null || typeof value.$db === 'string')
);
}
/**
* A class representation of the BSON DBRef type.
* @public
* @category BSONType
*/
export class DBRef {
_bsontype!: 'DBRef';
collection!: string;
oid!: ObjectId;
db?: string;
fields!: Document;
/**
* @param collection - the collection name.
* @param oid - the reference ObjectId.
* @param db - optional db name, if omitted the reference is local to the current db.
*/
constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {
if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
// check if namespace has been provided
const parts = collection.split('.');
if (parts.length === 2) {
db = parts.shift();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
collection = parts.shift()!;
}
this.collection = collection;
this.oid = oid;
this.db = db;
this.fields = fields || {};
}
// Property provided for compatibility with the 1.x parser
// the 1.x parser used a "namespace" property, while 4.x uses "collection"
/** @internal */
get namespace(): string {
return this.collection;
}
set namespace(value: string) {
this.collection = value;
}
toJSON(): DBRefLike & Document {
const o = Object.assign(
{
$ref: this.collection,
$id: this.oid
},
this.fields
);
if (this.db != null) o.$db = this.db;
return o;
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): DBRefLike {
options = options || {};
let o: DBRefLike = {
$ref: this.collection,
$id: this.oid
};
if (options.legacy) {
return o;
}
if (this.db) o.$db = this.db;
o = Object.assign(o, this.fields);
return o;
}
/** @internal */
static fromExtendedJSON(doc: DBRefLike): DBRef {
const copy = Object.assign({}, doc) as Partial<DBRefLike>;
delete copy.$ref;
delete copy.$id;
delete copy.$db;
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
// NOTE: if OID is an ObjectId class it will just print the oid string.
const oid =
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${
this.db ? `, "${this.db}"` : ''
})`;
}
}
Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });
+773
View File
@@ -0,0 +1,773 @@
import { Buffer } from 'buffer';
import { BSONTypeError } from './error';
import { Long } from './long';
import { isUint8Array } from './parser/utils';
const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
const PARSE_NAN_REGEXP = /^(\+|-)?NaN$/i;
const EXPONENT_MAX = 6111;
const EXPONENT_MIN = -6176;
const EXPONENT_BIAS = 6176;
const MAX_DIGITS = 34;
// Nan value bits as 32 bit values (due to lack of longs)
const NAN_BUFFER = [
0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
// Infinity value bits 32 bit values (due to lack of longs)
const INF_NEGATIVE_BUFFER = [
0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
const INF_POSITIVE_BUFFER = [
0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
].reverse();
const EXPONENT_REGEX = /^([-+])?(\d+)?$/;
// Extract least significant 5 bits
const COMBINATION_MASK = 0x1f;
// Extract least significant 14 bits
const EXPONENT_MASK = 0x3fff;
// Value of combination field for Inf
const COMBINATION_INFINITY = 30;
// Value of combination field for NaN
const COMBINATION_NAN = 31;
// Detect if the value is a digit
function isDigit(value: string): boolean {
return !isNaN(parseInt(value, 10));
}
// Divide two uint128 values
function divideu128(value: { parts: [number, number, number, number] }) {
const DIVISOR = Long.fromNumber(1000 * 1000 * 1000);
let _rem = Long.fromNumber(0);
if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) {
return { quotient: value, rem: _rem };
}
for (let i = 0; i <= 3; i++) {
// Adjust remainder to match value of next dividend
_rem = _rem.shiftLeft(32);
// Add the divided to _rem
_rem = _rem.add(new Long(value.parts[i], 0));
value.parts[i] = _rem.div(DIVISOR).low;
_rem = _rem.modulo(DIVISOR);
}
return { quotient: value, rem: _rem };
}
// Multiply two Long values and return the 128 bit value
function multiply64x2(left: Long, right: Long): { high: Long; low: Long } {
if (!left && !right) {
return { high: Long.fromNumber(0), low: Long.fromNumber(0) };
}
const leftHigh = left.shiftRightUnsigned(32);
const leftLow = new Long(left.getLowBits(), 0);
const rightHigh = right.shiftRightUnsigned(32);
const rightLow = new Long(right.getLowBits(), 0);
let productHigh = leftHigh.multiply(rightHigh);
let productMid = leftHigh.multiply(rightLow);
const productMid2 = leftLow.multiply(rightHigh);
let productLow = leftLow.multiply(rightLow);
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
productMid = new Long(productMid.getLowBits(), 0)
.add(productMid2)
.add(productLow.shiftRightUnsigned(32));
productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
productLow = productMid.shiftLeft(32).add(new Long(productLow.getLowBits(), 0));
// Return the 128 bit result
return { high: productHigh, low: productLow };
}
function lessThan(left: Long, right: Long): boolean {
// Make values unsigned
const uhleft = left.high >>> 0;
const uhright = right.high >>> 0;
// Compare high bits first
if (uhleft < uhright) {
return true;
} else if (uhleft === uhright) {
const ulleft = left.low >>> 0;
const ulright = right.low >>> 0;
if (ulleft < ulright) return true;
}
return false;
}
function invalidErr(string: string, message: string) {
throw new BSONTypeError(`"${string}" is not a valid Decimal128 string - ${message}`);
}
/** @public */
export interface Decimal128Extended {
$numberDecimal: string;
}
/**
* A class representation of the BSON Decimal128 type.
* @public
* @category BSONType
*/
export class Decimal128 {
_bsontype!: 'Decimal128';
readonly bytes!: Buffer;
/**
* @param bytes - a buffer containing the raw Decimal128 bytes in little endian order,
* or a string representation as returned by .toString()
*/
constructor(bytes: Buffer | string) {
if (!(this instanceof Decimal128)) return new Decimal128(bytes);
if (typeof bytes === 'string') {
this.bytes = Decimal128.fromString(bytes).bytes;
} else if (isUint8Array(bytes)) {
if (bytes.byteLength !== 16) {
throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
}
this.bytes = bytes;
} else {
throw new BSONTypeError('Decimal128 must take a Buffer or string');
}
}
/**
* Create a Decimal128 instance from a string representation
*
* @param representation - a numeric string representation.
*/
static fromString(representation: string): Decimal128 {
// Parse state tracking
let isNegative = false;
let sawRadix = false;
let foundNonZero = false;
// Total number of significant digits (no leading or trailing zero)
let significantDigits = 0;
// Total number of significand digits read
let nDigitsRead = 0;
// Total number of digits (no leading zeros)
let nDigits = 0;
// The number of the digits after radix
let radixPosition = 0;
// The index of the first non-zero in *str*
let firstNonZero = 0;
// Digits Array
const digits = [0];
// The number of digits in digits
let nDigitsStored = 0;
// Insertion pointer for digits
let digitsInsert = 0;
// The index of the first non-zero digit
let firstDigit = 0;
// The index of the last digit
let lastDigit = 0;
// Exponent
let exponent = 0;
// loop index over array
let i = 0;
// The high 17 digits of the significand
let significandHigh = new Long(0, 0);
// The low 17 digits of the significand
let significandLow = new Long(0, 0);
// The biased exponent
let biasedExponent = 0;
// Read index
let index = 0;
// Naively prevent against REDOS attacks.
// TODO: implementing a custom parsing for this, or refactoring the regex would yield
// further gains.
if (representation.length >= 7000) {
throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
}
// Results
const stringMatch = representation.match(PARSE_STRING_REGEXP);
const infMatch = representation.match(PARSE_INF_REGEXP);
const nanMatch = representation.match(PARSE_NAN_REGEXP);
// Validate the string
if ((!stringMatch && !infMatch && !nanMatch) || representation.length === 0) {
throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
}
if (stringMatch) {
// full_match = stringMatch[0]
// sign = stringMatch[1]
const unsignedNumber = stringMatch[2];
// stringMatch[3] is undefined if a whole number (ex "1", 12")
// but defined if a number w/ decimal in it (ex "1.0, 12.2")
const e = stringMatch[4];
const expSign = stringMatch[5];
const expNumber = stringMatch[6];
// they provided e, but didn't give an exponent number. for ex "1e"
if (e && expNumber === undefined) invalidErr(representation, 'missing exponent power');
// they provided e, but didn't give a number before it. for ex "e1"
if (e && unsignedNumber === undefined) invalidErr(representation, 'missing exponent base');
if (e === undefined && (expSign || expNumber)) {
invalidErr(representation, 'missing e before exponent');
}
}
// Get the negative or positive sign
if (representation[index] === '+' || representation[index] === '-') {
isNegative = representation[index++] === '-';
}
// Check if user passed Infinity or NaN
if (!isDigit(representation[index]) && representation[index] !== '.') {
if (representation[index] === 'i' || representation[index] === 'I') {
return new Decimal128(Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
} else if (representation[index] === 'N') {
return new Decimal128(Buffer.from(NAN_BUFFER));
}
}
// Read all the digits
while (isDigit(representation[index]) || representation[index] === '.') {
if (representation[index] === '.') {
if (sawRadix) invalidErr(representation, 'contains multiple periods');
sawRadix = true;
index = index + 1;
continue;
}
if (nDigitsStored < 34) {
if (representation[index] !== '0' || foundNonZero) {
if (!foundNonZero) {
firstNonZero = nDigitsRead;
}
foundNonZero = true;
// Only store 34 digits
digits[digitsInsert++] = parseInt(representation[index], 10);
nDigitsStored = nDigitsStored + 1;
}
}
if (foundNonZero) nDigits = nDigits + 1;
if (sawRadix) radixPosition = radixPosition + 1;
nDigitsRead = nDigitsRead + 1;
index = index + 1;
}
if (sawRadix && !nDigitsRead)
throw new BSONTypeError('' + representation + ' not a valid Decimal128 string');
// Read exponent if exists
if (representation[index] === 'e' || representation[index] === 'E') {
// Read exponent digits
const match = representation.substr(++index).match(EXPONENT_REGEX);
// No digits read
if (!match || !match[2]) return new Decimal128(Buffer.from(NAN_BUFFER));
// Get exponent
exponent = parseInt(match[0], 10);
// Adjust the index
index = index + match[0].length;
}
// Return not a number
if (representation[index]) return new Decimal128(Buffer.from(NAN_BUFFER));
// Done reading input
// Find first non-zero digit in digits
firstDigit = 0;
if (!nDigitsStored) {
firstDigit = 0;
lastDigit = 0;
digits[0] = 0;
nDigits = 1;
nDigitsStored = 1;
significantDigits = 0;
} else {
lastDigit = nDigitsStored - 1;
significantDigits = nDigits;
if (significantDigits !== 1) {
while (digits[firstNonZero + significantDigits - 1] === 0) {
significantDigits = significantDigits - 1;
}
}
}
// Normalization of exponent
// Correct exponent based on radix position, and shift significand as needed
// to represent user input
// Overflow prevention
if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
exponent = EXPONENT_MIN;
} else {
exponent = exponent - radixPosition;
}
// Attempt to normalize the exponent
while (exponent > EXPONENT_MAX) {
// Shift exponent to significand and decrease
lastDigit = lastDigit + 1;
if (lastDigit - firstDigit > MAX_DIGITS) {
// Check if we have a zero then just hard clamp, otherwise fail
const digitsString = digits.join('');
if (digitsString.match(/^0+$/)) {
exponent = EXPONENT_MAX;
break;
}
invalidErr(representation, 'overflow');
}
exponent = exponent - 1;
}
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
// Shift last digit. can only do this if < significant digits than # stored.
if (lastDigit === 0 && significantDigits < nDigitsStored) {
exponent = EXPONENT_MIN;
significantDigits = 0;
break;
}
if (nDigitsStored < nDigits) {
// adjust to match digits not stored
nDigits = nDigits - 1;
} else {
// adjust to round
lastDigit = lastDigit - 1;
}
if (exponent < EXPONENT_MAX) {
exponent = exponent + 1;
} else {
// Check if we have a zero then just hard clamp, otherwise fail
const digitsString = digits.join('');
if (digitsString.match(/^0+$/)) {
exponent = EXPONENT_MAX;
break;
}
invalidErr(representation, 'overflow');
}
}
// Round
// We've normalized the exponent, but might still need to round.
if (lastDigit - firstDigit + 1 < significantDigits) {
let endOfString = nDigitsRead;
// If we have seen a radix point, 'string' is 1 longer than we have
// documented with ndigits_read, so inc the position of the first nonzero
// digit and the position that digits are read to.
if (sawRadix) {
firstNonZero = firstNonZero + 1;
endOfString = endOfString + 1;
}
// if negative, we need to increment again to account for - sign at start.
if (isNegative) {
firstNonZero = firstNonZero + 1;
endOfString = endOfString + 1;
}
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
let roundBit = 0;
if (roundDigit >= 5) {
roundBit = 1;
if (roundDigit === 5) {
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
if (parseInt(representation[i], 10)) {
roundBit = 1;
break;
}
}
}
}
if (roundBit) {
let dIdx = lastDigit;
for (; dIdx >= 0; dIdx--) {
if (++digits[dIdx] > 9) {
digits[dIdx] = 0;
// overflowed most significant digit
if (dIdx === 0) {
if (exponent < EXPONENT_MAX) {
exponent = exponent + 1;
digits[dIdx] = 1;
} else {
return new Decimal128(
Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
);
}
}
}
}
}
}
// Encode significand
// The high 17 digits of the significand
significandHigh = Long.fromNumber(0);
// The low 17 digits of the significand
significandLow = Long.fromNumber(0);
// read a zero
if (significantDigits === 0) {
significandHigh = Long.fromNumber(0);
significandLow = Long.fromNumber(0);
} else if (lastDigit - firstDigit < 17) {
let dIdx = firstDigit;
significandLow = Long.fromNumber(digits[dIdx++]);
significandHigh = new Long(0, 0);
for (; dIdx <= lastDigit; dIdx++) {
significandLow = significandLow.multiply(Long.fromNumber(10));
significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
}
} else {
let dIdx = firstDigit;
significandHigh = Long.fromNumber(digits[dIdx++]);
for (; dIdx <= lastDigit - 17; dIdx++) {
significandHigh = significandHigh.multiply(Long.fromNumber(10));
significandHigh = significandHigh.add(Long.fromNumber(digits[dIdx]));
}
significandLow = Long.fromNumber(digits[dIdx++]);
for (; dIdx <= lastDigit; dIdx++) {
significandLow = significandLow.multiply(Long.fromNumber(10));
significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
}
}
const significand = multiply64x2(significandHigh, Long.fromString('100000000000000000'));
significand.low = significand.low.add(significandLow);
if (lessThan(significand.low, significandLow)) {
significand.high = significand.high.add(Long.fromNumber(1));
}
// Biased exponent
biasedExponent = exponent + EXPONENT_BIAS;
const dec = { low: Long.fromNumber(0), high: Long.fromNumber(0) };
// Encode combination, exponent, and significand.
if (
significand.high.shiftRightUnsigned(49).and(Long.fromNumber(1)).equals(Long.fromNumber(1))
) {
// Encode '11' into bits 1 to 3
dec.high = dec.high.or(Long.fromNumber(0x3).shiftLeft(61));
dec.high = dec.high.or(
Long.fromNumber(biasedExponent).and(Long.fromNumber(0x3fff).shiftLeft(47))
);
dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x7fffffffffff)));
} else {
dec.high = dec.high.or(Long.fromNumber(biasedExponent & 0x3fff).shiftLeft(49));
dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x1ffffffffffff)));
}
dec.low = significand.low;
// Encode sign
if (isNegative) {
dec.high = dec.high.or(Long.fromString('9223372036854775808'));
}
// Encode into a buffer
const buffer = Buffer.alloc(16);
index = 0;
// Encode the low 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.low.low & 0xff;
buffer[index++] = (dec.low.low >> 8) & 0xff;
buffer[index++] = (dec.low.low >> 16) & 0xff;
buffer[index++] = (dec.low.low >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.low.high & 0xff;
buffer[index++] = (dec.low.high >> 8) & 0xff;
buffer[index++] = (dec.low.high >> 16) & 0xff;
buffer[index++] = (dec.low.high >> 24) & 0xff;
// Encode the high 64 bits of the decimal
// Encode low bits
buffer[index++] = dec.high.low & 0xff;
buffer[index++] = (dec.high.low >> 8) & 0xff;
buffer[index++] = (dec.high.low >> 16) & 0xff;
buffer[index++] = (dec.high.low >> 24) & 0xff;
// Encode high bits
buffer[index++] = dec.high.high & 0xff;
buffer[index++] = (dec.high.high >> 8) & 0xff;
buffer[index++] = (dec.high.high >> 16) & 0xff;
buffer[index++] = (dec.high.high >> 24) & 0xff;
// Return the new Decimal128
return new Decimal128(buffer);
}
/** Create a string representation of the raw Decimal128 value */
toString(): string {
// Note: bits in this routine are referred to starting at 0,
// from the sign bit, towards the coefficient.
// decoded biased exponent (14 bits)
let biased_exponent;
// the number of significand digits
let significand_digits = 0;
// the base-10 digits in the significand
const significand = new Array<number>(36);
for (let i = 0; i < significand.length; i++) significand[i] = 0;
// read pointer into significand
let index = 0;
// true if the number is zero
let is_zero = false;
// the most significant significand bits (50-46)
let significand_msb;
// temporary storage for significand decoding
let significand128: { parts: [number, number, number, number] } = { parts: [0, 0, 0, 0] };
// indexing variables
let j, k;
// Output string
const string: string[] = [];
// Unpack index
index = 0;
// Buffer reference
const buffer = this.bytes;
// Unpack the low 64bits into a long
// bits 96 - 127
const low =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// bits 64 - 95
const midl =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Unpack the high 64bits into a long
// bits 32 - 63
const midh =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// bits 0 - 31
const high =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Unpack index
index = 0;
// Create the state of the decimal
const dec = {
low: new Long(low, midl),
high: new Long(midh, high)
};
if (dec.high.lessThan(Long.ZERO)) {
string.push('-');
}
// Decode combination field and exponent
// bits 1 - 5
const combination = (high >> 26) & COMBINATION_MASK;
if (combination >> 3 === 3) {
// Check for 'special' values
if (combination === COMBINATION_INFINITY) {
return string.join('') + 'Infinity';
} else if (combination === COMBINATION_NAN) {
return 'NaN';
} else {
biased_exponent = (high >> 15) & EXPONENT_MASK;
significand_msb = 0x08 + ((high >> 14) & 0x01);
}
} else {
significand_msb = (high >> 14) & 0x07;
biased_exponent = (high >> 17) & EXPONENT_MASK;
}
// unbiased exponent
const exponent = biased_exponent - EXPONENT_BIAS;
// Create string of significand digits
// Convert the 114-bit binary number represented by
// (significand_high, significand_low) to at most 34 decimal
// digits through modulo and division.
significand128.parts[0] = (high & 0x3fff) + ((significand_msb & 0xf) << 14);
significand128.parts[1] = midh;
significand128.parts[2] = midl;
significand128.parts[3] = low;
if (
significand128.parts[0] === 0 &&
significand128.parts[1] === 0 &&
significand128.parts[2] === 0 &&
significand128.parts[3] === 0
) {
is_zero = true;
} else {
for (k = 3; k >= 0; k--) {
let least_digits = 0;
// Perform the divide
const result = divideu128(significand128);
significand128 = result.quotient;
least_digits = result.rem.low;
// We now have the 9 least significant digits (in base 2).
// Convert and output to string.
if (!least_digits) continue;
for (j = 8; j >= 0; j--) {
// significand[k * 9 + j] = Math.round(least_digits % 10);
significand[k * 9 + j] = least_digits % 10;
// least_digits = Math.round(least_digits / 10);
least_digits = Math.floor(least_digits / 10);
}
}
}
// Output format options:
// Scientific - [-]d.dddE(+/-)dd or [-]dE(+/-)dd
// Regular - ddd.ddd
if (is_zero) {
significand_digits = 1;
significand[index] = 0;
} else {
significand_digits = 36;
while (!significand[index]) {
significand_digits = significand_digits - 1;
index = index + 1;
}
}
// the exponent if scientific notation is used
const scientific_exponent = significand_digits - 1 + exponent;
// The scientific exponent checks are dictated by the string conversion
// specification and are somewhat arbitrary cutoffs.
//
// We must check exponent > 0, because if this is the case, the number
// has trailing zeros. However, we *cannot* output these trailing zeros,
// because doing so would change the precision of the value, and would
// change stored data if the string converted number is round tripped.
if (scientific_exponent >= 34 || scientific_exponent <= -7 || exponent > 0) {
// Scientific format
// if there are too many significant digits, we should just be treating numbers
// as + or - 0 and using the non-scientific exponent (this is for the "invalid
// representation should be treated as 0/-0" spec cases in decimal128-1.json)
if (significand_digits > 34) {
string.push(`${0}`);
if (exponent > 0) string.push(`E+${exponent}`);
else if (exponent < 0) string.push(`E${exponent}`);
return string.join('');
}
string.push(`${significand[index++]}`);
significand_digits = significand_digits - 1;
if (significand_digits) {
string.push('.');
}
for (let i = 0; i < significand_digits; i++) {
string.push(`${significand[index++]}`);
}
// Exponent
string.push('E');
if (scientific_exponent > 0) {
string.push(`+${scientific_exponent}`);
} else {
string.push(`${scientific_exponent}`);
}
} else {
// Regular format with no decimal place
if (exponent >= 0) {
for (let i = 0; i < significand_digits; i++) {
string.push(`${significand[index++]}`);
}
} else {
let radix_position = significand_digits + exponent;
// non-zero digits before radix
if (radix_position > 0) {
for (let i = 0; i < radix_position; i++) {
string.push(`${significand[index++]}`);
}
} else {
string.push('0');
}
string.push('.');
// add leading zeros after radix
while (radix_position++ < 0) {
string.push('0');
}
for (let i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) {
string.push(`${significand[index++]}`);
}
}
}
return string.join('');
}
toJSON(): Decimal128Extended {
return { $numberDecimal: this.toString() };
}
/** @internal */
toExtendedJSON(): Decimal128Extended {
return { $numberDecimal: this.toString() };
}
/** @internal */
static fromExtendedJSON(doc: Decimal128Extended): Decimal128 {
return Decimal128.fromString(doc.$numberDecimal);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new Decimal128("${this.toString()}")`;
}
}
Object.defineProperty(Decimal128.prototype, '_bsontype', { value: 'Decimal128' });
+91
View File
@@ -0,0 +1,91 @@
import type { EJSONOptions } from './extended_json';
/** @public */
export interface DoubleExtended {
$numberDouble: string;
}
/**
* A class representation of the BSON Double type.
* @public
* @category BSONType
*/
export class Double {
_bsontype!: 'Double';
value!: number;
/**
* Create a Double type
*
* @param value - the number we want to represent as a double.
*/
constructor(value: number) {
if (!(this instanceof Double)) return new Double(value);
if ((value as unknown) instanceof Number) {
value = value.valueOf();
}
this.value = +value;
}
/**
* Access the number value.
*
* @returns returns the wrapped double number.
*/
valueOf(): number {
return this.value;
}
toJSON(): number {
return this.value;
}
toString(radix?: number): string {
return this.value.toString(radix);
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): number | DoubleExtended {
if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) {
return this.value;
}
// 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
if (Object.is(Math.sign(this.value), -0)) {
return { $numberDouble: `-${this.value.toFixed(1)}` };
}
let $numberDouble: string;
if (Number.isInteger(this.value)) {
$numberDouble = this.value.toFixed(1);
if ($numberDouble.length >= 13) {
$numberDouble = this.value.toExponential(13).toUpperCase();
}
} else {
$numberDouble = this.value.toString();
}
return { $numberDouble };
}
/** @internal */
static fromExtendedJSON(doc: DoubleExtended, options?: EJSONOptions): number | Double {
const doubleValue = parseFloat(doc.$numberDouble);
return options && options.relaxed ? doubleValue : new Double(doubleValue);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
const eJSON = this.toExtendedJSON() as DoubleExtended;
return `new Double(${eJSON.$numberDouble})`;
}
}
Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });
+27
View File
@@ -0,0 +1,27 @@
import { Buffer } from 'buffer';
import { BSONTypeError } from './error';
import { isAnyArrayBuffer } from './parser/utils';
/**
* Makes sure that, if a Uint8Array is passed in, it is wrapped in a Buffer.
*
* @param potentialBuffer - The potential buffer
* @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
* wraps a passed in Uint8Array
* @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
*/
export function ensureBuffer(potentialBuffer: Buffer | ArrayBufferView | ArrayBuffer): Buffer {
if (ArrayBuffer.isView(potentialBuffer)) {
return Buffer.from(
potentialBuffer.buffer,
potentialBuffer.byteOffset,
potentialBuffer.byteLength
);
}
if (isAnyArrayBuffer(potentialBuffer)) {
return Buffer.from(potentialBuffer);
}
throw new BSONTypeError('Must use either Buffer or TypedArray');
}
+23
View File
@@ -0,0 +1,23 @@
/** @public */
export class BSONError extends Error {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, BSONError.prototype);
}
get name(): string {
return 'BSONError';
}
}
/** @public */
export class BSONTypeError extends TypeError {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, BSONTypeError.prototype);
}
get name(): string {
return 'BSONTypeError';
}
}
+462
View File
@@ -0,0 +1,462 @@
import { Binary } from './binary';
import type { Document } from './bson';
import { Code } from './code';
import { DBRef, isDBRefLike } from './db_ref';
import { Decimal128 } from './decimal128';
import { Double } from './double';
import { BSONError, BSONTypeError } from './error';
import { Int32 } from './int_32';
import { Long } from './long';
import { MaxKey } from './max_key';
import { MinKey } from './min_key';
import { ObjectId } from './objectid';
import { isDate, isObjectLike, isRegExp } from './parser/utils';
import { BSONRegExp } from './regexp';
import { BSONSymbol } from './symbol';
import { Timestamp } from './timestamp';
/** @public */
export type EJSONOptions = EJSON.Options;
/** @internal */
type BSONType =
| Binary
| Code
| DBRef
| Decimal128
| Double
| Int32
| Long
| MaxKey
| MinKey
| ObjectId
| BSONRegExp
| BSONSymbol
| Timestamp;
export function isBSONType(value: unknown): value is BSONType {
return (
isObjectLike(value) && Reflect.has(value, '_bsontype') && typeof value._bsontype === 'string'
);
}
// INT32 boundaries
const BSON_INT32_MAX = 0x7fffffff;
const BSON_INT32_MIN = -0x80000000;
// INT64 boundaries
// const BSON_INT64_MAX = 0x7fffffffffffffff; // TODO(NODE-4377): This number cannot be precisely represented in JS
const BSON_INT64_MAX = 0x8000000000000000;
const BSON_INT64_MIN = -0x8000000000000000;
// all the types where we don't need to do any special processing and can just pass the EJSON
//straight to type.fromExtendedJSON
const keysToCodecs = {
$oid: ObjectId,
$binary: Binary,
$uuid: Binary,
$symbol: BSONSymbol,
$numberInt: Int32,
$numberDecimal: Decimal128,
$numberDouble: Double,
$numberLong: Long,
$minKey: MinKey,
$maxKey: MaxKey,
$regex: BSONRegExp,
$regularExpression: BSONRegExp,
$timestamp: Timestamp
} as const;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function deserializeValue(value: any, options: EJSON.Options = {}) {
if (typeof value === 'number') {
if (options.relaxed || options.legacy) {
return value;
}
// if it's an integer, should interpret as smallest BSON integer
// that can represent it exactly. (if out of range, interpret as double.)
if (Math.floor(value) === value) {
if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) return new Int32(value);
if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX) return Long.fromNumber(value);
}
// If the number is a non-integer or out of integer range, should interpret as BSON Double.
return new Double(value);
}
// from here on out we're looking for bson types, so bail if its not an object
if (value == null || typeof value !== 'object') return value;
// upgrade deprecated undefined to null
if (value.$undefined) return null;
const keys = Object.keys(value).filter(
k => k.startsWith('$') && value[k] != null
) as (keyof typeof keysToCodecs)[];
for (let i = 0; i < keys.length; i++) {
const c = keysToCodecs[keys[i]];
if (c) return c.fromExtendedJSON(value, options);
}
if (value.$date != null) {
const d = value.$date;
const date = new Date();
if (options.legacy) {
if (typeof d === 'number') date.setTime(d);
else if (typeof d === 'string') date.setTime(Date.parse(d));
} else {
if (typeof d === 'string') date.setTime(Date.parse(d));
else if (Long.isLong(d)) date.setTime(d.toNumber());
else if (typeof d === 'number' && options.relaxed) date.setTime(d);
}
return date;
}
if (value.$code != null) {
const copy = Object.assign({}, value);
if (value.$scope) {
copy.$scope = deserializeValue(value.$scope);
}
return Code.fromExtendedJSON(value);
}
if (isDBRefLike(value) || value.$dbPointer) {
const v = value.$ref ? value : value.$dbPointer;
// we run into this in a "degenerate EJSON" case (with $id and $ref order flipped)
// because of the order JSON.parse goes through the document
if (v instanceof DBRef) return v;
const dollarKeys = Object.keys(v).filter(k => k.startsWith('$'));
let valid = true;
dollarKeys.forEach(k => {
if (['$ref', '$id', '$db'].indexOf(k) === -1) valid = false;
});
// only make DBRef if $ keys are all valid
if (valid) return DBRef.fromExtendedJSON(v);
}
return value;
}
type EJSONSerializeOptions = EJSON.Options & {
seenObjects: { obj: unknown; propertyName: string }[];
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeArray(array: any[], options: EJSONSerializeOptions): any[] {
return array.map((v: unknown, index: number) => {
options.seenObjects.push({ propertyName: `index ${index}`, obj: null });
try {
return serializeValue(v, options);
} finally {
options.seenObjects.pop();
}
});
}
function getISOString(date: Date) {
const isoStr = date.toISOString();
// we should only show milliseconds in timestamp if they're non-zero
return date.getUTCMilliseconds() !== 0 ? isoStr : isoStr.slice(0, -5) + 'Z';
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeValue(value: any, options: EJSONSerializeOptions): any {
if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
const index = options.seenObjects.findIndex(entry => entry.obj === value);
if (index !== -1) {
const props = options.seenObjects.map(entry => entry.propertyName);
const leadingPart = props
.slice(0, index)
.map(prop => `${prop} -> `)
.join('');
const alreadySeen = props[index];
const circularPart =
' -> ' +
props
.slice(index + 1, props.length - 1)
.map(prop => `${prop} -> `)
.join('');
const current = props[props.length - 1];
const leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
const dashes = '-'.repeat(
circularPart.length + (alreadySeen.length + current.length) / 2 - 1
);
throw new BSONTypeError(
'Converting circular structure to EJSON:\n' +
` ${leadingPart}${alreadySeen}${circularPart}${current}\n` +
` ${leadingSpace}\\${dashes}/`
);
}
options.seenObjects[options.seenObjects.length - 1].obj = value;
}
if (Array.isArray(value)) return serializeArray(value, options);
if (value === undefined) return null;
if (value instanceof Date || isDate(value)) {
const dateNum = value.getTime(),
// is it in year range 1970-9999?
inRange = dateNum > -1 && dateNum < 253402318800000;
if (options.legacy) {
return options.relaxed && inRange
? { $date: value.getTime() }
: { $date: getISOString(value) };
}
return options.relaxed && inRange
? { $date: getISOString(value) }
: { $date: { $numberLong: value.getTime().toString() } };
}
if (typeof value === 'number' && (!options.relaxed || !isFinite(value))) {
// it's an integer
if (Math.floor(value) === value) {
const int32Range = value >= BSON_INT32_MIN && value <= BSON_INT32_MAX,
int64Range = value >= BSON_INT64_MIN && value <= BSON_INT64_MAX;
// interpret as being of the smallest BSON integer type that can represent the number exactly
if (int32Range) return { $numberInt: value.toString() };
if (int64Range) return { $numberLong: value.toString() };
}
return { $numberDouble: value.toString() };
}
if (value instanceof RegExp || isRegExp(value)) {
let flags = value.flags;
if (flags === undefined) {
const match = value.toString().match(/[gimuy]*$/);
if (match) {
flags = match[0];
}
}
const rx = new BSONRegExp(value.source, flags);
return rx.toExtendedJSON(options);
}
if (value != null && typeof value === 'object') return serializeDocument(value, options);
return value;
}
const BSON_TYPE_MAPPINGS = {
Binary: (o: Binary) => new Binary(o.value(), o.sub_type),
Code: (o: Code) => new Code(o.code, o.scope),
DBRef: (o: DBRef) => new DBRef(o.collection || o.namespace, o.oid, o.db, o.fields), // "namespace" for 1.x library backwards compat
Decimal128: (o: Decimal128) => new Decimal128(o.bytes),
Double: (o: Double) => new Double(o.value),
Int32: (o: Int32) => new Int32(o.value),
Long: (
o: Long & {
low_: number;
high_: number;
unsigned_: boolean | undefined;
}
) =>
Long.fromBits(
// underscore variants for 1.x backwards compatibility
o.low != null ? o.low : o.low_,
o.low != null ? o.high : o.high_,
o.low != null ? o.unsigned : o.unsigned_
),
MaxKey: () => new MaxKey(),
MinKey: () => new MinKey(),
ObjectID: (o: ObjectId) => new ObjectId(o),
ObjectId: (o: ObjectId) => new ObjectId(o), // support 4.0.0/4.0.1 before _bsontype was reverted back to ObjectID
BSONRegExp: (o: BSONRegExp) => new BSONRegExp(o.pattern, o.options),
Symbol: (o: BSONSymbol) => new BSONSymbol(o.value),
Timestamp: (o: Timestamp) => Timestamp.fromBits(o.low, o.high)
} as const;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeDocument(doc: any, options: EJSONSerializeOptions) {
if (doc == null || typeof doc !== 'object') throw new BSONError('not an object instance');
const bsontype: BSONType['_bsontype'] = doc._bsontype;
if (typeof bsontype === 'undefined') {
// It's a regular object. Recursively serialize its property values.
const _doc: Document = {};
for (const name in doc) {
options.seenObjects.push({ propertyName: name, obj: null });
try {
const value = serializeValue(doc[name], options);
if (name === '__proto__') {
Object.defineProperty(_doc, name, {
value,
writable: true,
enumerable: true,
configurable: true
});
} else {
_doc[name] = value;
}
} finally {
options.seenObjects.pop();
}
}
return _doc;
} else if (isBSONType(doc)) {
// the "document" is really just a BSON type object
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let outDoc: any = doc;
if (typeof outDoc.toExtendedJSON !== 'function') {
// There's no EJSON serialization function on the object. It's probably an
// object created by a previous version of this library (or another library)
// that's duck-typing objects to look like they were generated by this library).
// Copy the object into this library's version of that type.
const mapper = BSON_TYPE_MAPPINGS[doc._bsontype];
if (!mapper) {
throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + doc._bsontype);
}
outDoc = mapper(outDoc);
}
// Two BSON types may have nested objects that may need to be serialized too
if (bsontype === 'Code' && outDoc.scope) {
outDoc = new Code(outDoc.code, serializeValue(outDoc.scope, options));
} else if (bsontype === 'DBRef' && outDoc.oid) {
outDoc = new DBRef(
serializeValue(outDoc.collection, options),
serializeValue(outDoc.oid, options),
serializeValue(outDoc.db, options),
serializeValue(outDoc.fields, options)
);
}
return outDoc.toExtendedJSON(options);
} else {
throw new BSONError('_bsontype must be a string, but was: ' + typeof bsontype);
}
}
/**
* EJSON parse / stringify API
* @public
*/
// the namespace here is used to emulate `export * as EJSON from '...'`
// which as of now (sept 2020) api-extractor does not support
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace EJSON {
export interface Options {
/** Output using the Extended JSON v1 spec */
legacy?: boolean;
/** Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types */
relaxed?: boolean;
/**
* Disable Extended JSON's `relaxed` mode, which attempts to return BSON types where possible, rather than native JS types
* @deprecated Please use the relaxed property instead
*/
strict?: boolean;
}
/**
* Parse an Extended JSON string, constructing the JavaScript value or object described by that
* string.
*
* @example
* ```js
* const { EJSON } = require('bson');
* const text = '{ "int32": { "$numberInt": "10" } }';
*
* // prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
* console.log(EJSON.parse(text, { relaxed: false }));
*
* // prints { int32: 10 }
* console.log(EJSON.parse(text));
* ```
*/
export function parse(text: string, options?: EJSON.Options): SerializableTypes {
const finalOptions = Object.assign({}, { relaxed: true, legacy: false }, options);
// relaxed implies not strict
if (typeof finalOptions.relaxed === 'boolean') finalOptions.strict = !finalOptions.relaxed;
if (typeof finalOptions.strict === 'boolean') finalOptions.relaxed = !finalOptions.strict;
return JSON.parse(text, (key, value) => {
if (key.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Document field names cannot contain null bytes, found: ${JSON.stringify(key)}`
);
}
return deserializeValue(value, finalOptions);
});
}
export type JSONPrimitive = string | number | boolean | null;
export type SerializableTypes = Document | Array<JSONPrimitive | Document> | JSONPrimitive;
/**
* Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
* function is specified or optionally including only the specified properties if a replacer array
* is specified.
*
* @param value - The value to convert to extended JSON
* @param replacer - A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string
* @param space - A String or Number object that's used to insert white space into the output JSON string for readability purposes.
* @param options - Optional settings
*
* @example
* ```js
* const { EJSON } = require('bson');
* const Int32 = require('mongodb').Int32;
* const doc = { int32: new Int32(10) };
*
* // prints '{"int32":{"$numberInt":"10"}}'
* console.log(EJSON.stringify(doc, { relaxed: false }));
*
* // prints '{"int32":10}'
* console.log(EJSON.stringify(doc));
* ```
*/
export function stringify(
value: SerializableTypes,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | EJSON.Options,
space?: string | number,
options?: EJSON.Options
): string {
if (space != null && typeof space === 'object') {
options = space;
space = 0;
}
if (replacer != null && typeof replacer === 'object' && !Array.isArray(replacer)) {
options = replacer;
replacer = undefined;
space = 0;
}
const serializeOptions = Object.assign({ relaxed: true, legacy: false }, options, {
seenObjects: [{ propertyName: '(root)', obj: null }]
});
const doc = serializeValue(value, serializeOptions);
return JSON.stringify(doc, replacer as Parameters<JSON['stringify']>[1], space);
}
/**
* Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
*
* @param value - The object to serialize
* @param options - Optional settings passed to the `stringify` function
*/
export function serialize(value: SerializableTypes, options?: EJSON.Options): Document {
options = options || {};
return JSON.parse(stringify(value, options));
}
/**
* Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
*
* @param ejson - The Extended JSON object to deserialize
* @param options - Optional settings passed to the parse method
*/
export function deserialize(ejson: Document, options?: EJSON.Options): SerializableTypes {
options = options || {};
return parse(JSON.stringify(ejson), options);
}
}
+70
View File
@@ -0,0 +1,70 @@
import type { EJSONOptions } from './extended_json';
/** @public */
export interface Int32Extended {
$numberInt: string;
}
/**
* A class representation of a BSON Int32 type.
* @public
* @category BSONType
*/
export class Int32 {
_bsontype!: 'Int32';
value!: number;
/**
* Create an Int32 type
*
* @param value - the number we want to represent as an int32.
*/
constructor(value: number | string) {
if (!(this instanceof Int32)) return new Int32(value);
if ((value as unknown) instanceof Number) {
value = value.valueOf();
}
this.value = +value | 0;
}
/**
* Access the number value.
*
* @returns returns the wrapped int32 number.
*/
valueOf(): number {
return this.value;
}
toString(radix?: number): string {
return this.value.toString(radix);
}
toJSON(): number {
return this.value;
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): number | Int32Extended {
if (options && (options.relaxed || options.legacy)) return this.value;
return { $numberInt: this.value.toString() };
}
/** @internal */
static fromExtendedJSON(doc: Int32Extended, options?: EJSONOptions): number | Int32 {
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new Int32(${this.valueOf()})`;
}
}
Object.defineProperty(Int32.prototype, '_bsontype', { value: 'Int32' });
+1040
View File
File diff suppressed because it is too large Load Diff
+119
View File
@@ -0,0 +1,119 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// We have an ES6 Map available, return the native instance
import { getGlobal } from './utils/global';
/** @public */
let bsonMap: MapConstructor;
const bsonGlobal = getGlobal<{ Map?: MapConstructor }>();
if (bsonGlobal.Map) {
bsonMap = bsonGlobal.Map;
} else {
// We will return a polyfill
bsonMap = class Map {
private _keys: string[];
private _values: Record<string, any>;
constructor(array: [string, any][] = []) {
this._keys = [];
this._values = {};
for (let i = 0; i < array.length; i++) {
if (array[i] == null) continue; // skip null and undefined
const entry = array[i];
const key = entry[0];
const value = entry[1];
// Add the key to the list of keys in order
this._keys.push(key);
// Add the key and value to the values dictionary with a point
// to the location in the ordered keys list
this._values[key] = { v: value, i: this._keys.length - 1 };
}
}
clear() {
this._keys = [];
this._values = {};
}
delete(key: string) {
const value = this._values[key];
if (value == null) return false;
// Delete entry
delete this._values[key];
// Remove the key from the ordered keys list
this._keys.splice(value.i, 1);
return true;
}
entries() {
let index = 0;
return {
next: () => {
const key = this._keys[index++];
return {
value: key !== undefined ? [key, this._values[key].v] : undefined,
done: key !== undefined ? false : true
};
}
};
}
forEach(callback: (this: this, value: any, key: string, self: this) => void, self?: this) {
self = self || this;
for (let i = 0; i < this._keys.length; i++) {
const key = this._keys[i];
// Call the forEach callback
callback.call(self, this._values[key].v, key, self);
}
}
get(key: string) {
return this._values[key] ? this._values[key].v : undefined;
}
has(key: string) {
return this._values[key] != null;
}
keys() {
let index = 0;
return {
next: () => {
const key = this._keys[index++];
return {
value: key !== undefined ? key : undefined,
done: key !== undefined ? false : true
};
}
};
}
set(key: string, value: any) {
if (this._values[key]) {
this._values[key].v = value;
return this;
}
// Add the key to the list of keys in order
this._keys.push(key);
// Add the key and value to the values dictionary with a point
// to the location in the ordered keys list
this._values[key] = { v: value, i: this._keys.length - 1 };
return this;
}
values() {
let index = 0;
return {
next: () => {
const key = this._keys[index++];
return {
value: key !== undefined ? this._values[key].v : undefined,
done: key !== undefined ? false : true
};
}
};
}
get size() {
return this._keys.length;
}
} as unknown as MapConstructor;
}
export { bsonMap as Map };
+38
View File
@@ -0,0 +1,38 @@
/** @public */
export interface MaxKeyExtended {
$maxKey: 1;
}
/**
* A class representation of the BSON MaxKey type.
* @public
* @category BSONType
*/
export class MaxKey {
_bsontype!: 'MaxKey';
constructor() {
if (!(this instanceof MaxKey)) return new MaxKey();
}
/** @internal */
toExtendedJSON(): MaxKeyExtended {
return { $maxKey: 1 };
}
/** @internal */
static fromExtendedJSON(): MaxKey {
return new MaxKey();
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return 'new MaxKey()';
}
}
Object.defineProperty(MaxKey.prototype, '_bsontype', { value: 'MaxKey' });
+38
View File
@@ -0,0 +1,38 @@
/** @public */
export interface MinKeyExtended {
$minKey: 1;
}
/**
* A class representation of the BSON MinKey type.
* @public
* @category BSONType
*/
export class MinKey {
_bsontype!: 'MinKey';
constructor() {
if (!(this instanceof MinKey)) return new MinKey();
}
/** @internal */
toExtendedJSON(): MinKeyExtended {
return { $minKey: 1 };
}
/** @internal */
static fromExtendedJSON(): MinKey {
return new MinKey();
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return 'new MinKey()';
}
}
Object.defineProperty(MinKey.prototype, '_bsontype', { value: 'MinKey' });
+354
View File
@@ -0,0 +1,354 @@
import { Buffer } from 'buffer';
import { ensureBuffer } from './ensure_buffer';
import { BSONTypeError } from './error';
import { deprecate, isUint8Array, randomBytes } from './parser/utils';
// Regular expression that checks for hex value
const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
// Unique sequence for the current process (initialized on first use)
let PROCESS_UNIQUE: Uint8Array | null = null;
/** @public */
export interface ObjectIdLike {
id: string | Buffer;
__id?: string;
toHexString(): string;
}
/** @public */
export interface ObjectIdExtended {
$oid: string;
}
const kId = Symbol('id');
/**
* A class representation of the BSON ObjectId type.
* @public
* @category BSONType
*/
export class ObjectId {
_bsontype!: 'ObjectID';
/** @internal */
static index = Math.floor(Math.random() * 0xffffff);
static cacheHexString: boolean;
/** ObjectId Bytes @internal */
private [kId]!: Buffer;
/** ObjectId hexString cache @internal */
private __id?: string;
/**
* Create an ObjectId type
*
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
*/
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array) {
if (!(this instanceof ObjectId)) return new ObjectId(inputId);
// workingId is set based on type of input and whether valid id exists for the input
let workingId;
if (typeof inputId === 'object' && inputId && 'id' in inputId) {
if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
throw new BSONTypeError(
'Argument passed in must have an id that is of type string or Buffer'
);
}
if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
workingId = Buffer.from(inputId.toHexString(), 'hex');
} else {
workingId = inputId.id;
}
} else {
workingId = inputId;
}
// the following cases use workingId to construct an ObjectId
if (workingId == null || typeof workingId === 'number') {
// The most common use case (blank id, new objectId instance)
// Generate a new id
this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
this[kId] = workingId instanceof Buffer ? workingId : ensureBuffer(workingId);
} else if (typeof workingId === 'string') {
if (workingId.length === 12) {
const bytes = Buffer.from(workingId);
if (bytes.byteLength === 12) {
this[kId] = bytes;
} else {
throw new BSONTypeError('Argument passed in must be a string of 12 bytes');
}
} else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
this[kId] = Buffer.from(workingId, 'hex');
} else {
throw new BSONTypeError(
'Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer'
);
}
} else {
throw new BSONTypeError('Argument passed in does not match the accepted types');
}
// If we are caching the hex string
if (ObjectId.cacheHexString) {
this.__id = this.id.toString('hex');
}
}
/**
* The ObjectId bytes
* @readonly
*/
get id(): Buffer {
return this[kId];
}
set id(value: Buffer) {
this[kId] = value;
if (ObjectId.cacheHexString) {
this.__id = value.toString('hex');
}
}
/**
* The generation time of this ObjectId instance
* @deprecated Please use getTimestamp / createFromTime which returns an int32 epoch
*/
get generationTime(): number {
return this.id.readInt32BE(0);
}
set generationTime(value: number) {
// Encode time into first 4 bytes
this.id.writeUInt32BE(value, 0);
}
/** Returns the ObjectId id as a 24 character hex string representation */
toHexString(): string {
if (ObjectId.cacheHexString && this.__id) {
return this.__id;
}
const hexString = this.id.toString('hex');
if (ObjectId.cacheHexString && !this.__id) {
this.__id = hexString;
}
return hexString;
}
/**
* Update the ObjectId index
* @privateRemarks
* Used in generating new ObjectId's on the driver
* @internal
*/
static getInc(): number {
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
}
/**
* Generate a 12 byte id buffer used in ObjectId's
*
* @param time - pass in a second based timestamp.
*/
static generate(time?: number): Buffer {
if ('number' !== typeof time) {
time = Math.floor(Date.now() / 1000);
}
const inc = ObjectId.getInc();
const buffer = Buffer.alloc(12);
// 4-byte timestamp
buffer.writeUInt32BE(time, 0);
// set PROCESS_UNIQUE if yet not initialized
if (PROCESS_UNIQUE === null) {
PROCESS_UNIQUE = randomBytes(5);
}
// 5-byte process unique
buffer[4] = PROCESS_UNIQUE[0];
buffer[5] = PROCESS_UNIQUE[1];
buffer[6] = PROCESS_UNIQUE[2];
buffer[7] = PROCESS_UNIQUE[3];
buffer[8] = PROCESS_UNIQUE[4];
// 3-byte counter
buffer[11] = inc & 0xff;
buffer[10] = (inc >> 8) & 0xff;
buffer[9] = (inc >> 16) & 0xff;
return buffer;
}
/**
* Converts the id into a 24 character hex string for printing
*
* @param format - The Buffer toString format parameter.
*/
toString(format?: string): string {
// Is the id a buffer then use the buffer toString method to return the format
if (format) return this.id.toString(format);
return this.toHexString();
}
/** Converts to its JSON the 24 character hex string representation. */
toJSON(): string {
return this.toHexString();
}
/**
* Compares the equality of this ObjectId with `otherID`.
*
* @param otherId - ObjectId instance to compare against.
*/
equals(otherId: string | ObjectId | ObjectIdLike): boolean {
if (otherId === undefined || otherId === null) {
return false;
}
if (otherId instanceof ObjectId) {
return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
}
if (
typeof otherId === 'string' &&
ObjectId.isValid(otherId) &&
otherId.length === 12 &&
isUint8Array(this.id)
) {
return otherId === Buffer.prototype.toString.call(this.id, 'latin1');
}
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
return otherId.toLowerCase() === this.toHexString();
}
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
return Buffer.from(otherId).equals(this.id);
}
if (
typeof otherId === 'object' &&
'toHexString' in otherId &&
typeof otherId.toHexString === 'function'
) {
const otherIdString = otherId.toHexString();
const thisIdString = this.toHexString().toLowerCase();
return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
}
return false;
}
/** Returns the generation date (accurate up to the second) that this ID was generated. */
getTimestamp(): Date {
const timestamp = new Date();
const time = this.id.readUInt32BE(0);
timestamp.setTime(Math.floor(time) * 1000);
return timestamp;
}
/** @internal */
static createPk(): ObjectId {
return new ObjectId();
}
/**
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
*
* @param time - an integer number representing a number of seconds.
*/
static createFromTime(time: number): ObjectId {
const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
// Encode time into first 4 bytes
buffer.writeUInt32BE(time, 0);
// Return the new objectId
return new ObjectId(buffer);
}
/**
* Creates an ObjectId from a hex string representation of an ObjectId.
*
* @param hexString - create a ObjectId from a passed in 24 character hexstring.
*/
static createFromHexString(hexString: string): ObjectId {
// Throw an error if it's not a valid setup
if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
throw new BSONTypeError(
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
);
}
return new ObjectId(Buffer.from(hexString, 'hex'));
}
/**
* Checks if a value is a valid bson ObjectId
*
* @param id - ObjectId instance to validate.
*/
static isValid(id: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array): boolean {
if (id == null) return false;
try {
new ObjectId(id);
return true;
} catch {
return false;
}
}
/** @internal */
toExtendedJSON(): ObjectIdExtended {
if (this.toHexString) return { $oid: this.toHexString() };
return { $oid: this.toString('hex') };
}
/** @internal */
static fromExtendedJSON(doc: ObjectIdExtended): ObjectId {
return new ObjectId(doc.$oid);
}
/**
* Converts to a string representation of this Id.
*
* @returns return the 24 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new ObjectId("${this.toHexString()}")`;
}
}
// Deprecated methods
Object.defineProperty(ObjectId.prototype, 'generate', {
value: deprecate(
(time: number) => ObjectId.generate(time),
'Please use the static `ObjectId.generate(time)` instead'
)
});
Object.defineProperty(ObjectId.prototype, 'getInc', {
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId.prototype, 'get_inc', {
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId, 'get_inc', {
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
});
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' });
+228
View File
@@ -0,0 +1,228 @@
import { Buffer } from 'buffer';
import { Binary } from '../binary';
import type { Document } from '../bson';
import * as constants from '../constants';
import { isAnyArrayBuffer, isDate, isRegExp, normalizedFunctionString } from './utils';
export function calculateObjectSize(
object: Document,
serializeFunctions?: boolean,
ignoreUndefined?: boolean
): number {
let totalLength = 4 + 1;
if (Array.isArray(object)) {
for (let i = 0; i < object.length; i++) {
totalLength += calculateElement(
i.toString(),
object[i],
serializeFunctions,
true,
ignoreUndefined
);
}
} else {
// If we have toBSON defined, override the current object
if (typeof object?.toBSON === 'function') {
object = object.toBSON();
}
// Calculate size
for (const key in object) {
totalLength += calculateElement(key, object[key], serializeFunctions, false, ignoreUndefined);
}
}
return totalLength;
}
/** @internal */
function calculateElement(
name: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
serializeFunctions = false,
isArray = false,
ignoreUndefined = false
) {
// If we have toBSON defined, override the current object
if (typeof value?.toBSON === 'function') {
value = value.toBSON();
}
switch (typeof value) {
case 'string':
return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1;
case 'number':
if (
Math.floor(value) === value &&
value >= constants.JS_INT_MIN &&
value <= constants.JS_INT_MAX
) {
if (value >= constants.BSON_INT32_MIN && value <= constants.BSON_INT32_MAX) {
// 32 bit
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1);
} else {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
} else {
// 64 bit
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
}
case 'undefined':
if (isArray || !ignoreUndefined)
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
return 0;
case 'boolean':
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
case 'object':
if (value == null || value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
} else if (value instanceof Date || isDate(value)) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
} else if (
ArrayBuffer.isView(value) ||
value instanceof ArrayBuffer ||
isAnyArrayBuffer(value)
) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + value.byteLength
);
} else if (
value['_bsontype'] === 'Long' ||
value['_bsontype'] === 'Double' ||
value['_bsontype'] === 'Timestamp'
) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
} else if (value['_bsontype'] === 'Decimal128') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
} else if (value['_bsontype'] === 'Code') {
// Calculate size depending on the availability of a scope
if (value.scope != null && Object.keys(value.scope).length > 0) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
4 +
Buffer.byteLength(value.code.toString(), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
Buffer.byteLength(value.code.toString(), 'utf8') +
1
);
}
} else if (value['_bsontype'] === 'Binary') {
const binary: Binary = value;
// Check what kind of subtype we have
if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
(binary.position + 1 + 4 + 1 + 4)
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (binary.position + 1 + 4 + 1)
);
}
} else if (value['_bsontype'] === 'Symbol') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
Buffer.byteLength(value.value, 'utf8') +
4 +
1 +
1
);
} else if (value['_bsontype'] === 'DBRef') {
// Set up correct object for serialization
const ordered_values = Object.assign(
{
$ref: value.collection,
$id: value.oid
},
value.fields
);
// Add db reference if it exists
if (value.db != null) {
ordered_values['$db'] = value.db;
}
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined)
);
} else if (value instanceof RegExp || isRegExp(value)) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.source, 'utf8') +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1
);
} else if (value['_bsontype'] === 'BSONRegExp') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.pattern, 'utf8') +
1 +
Buffer.byteLength(value.options, 'utf8') +
1
);
} else {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
calculateObjectSize(value, serializeFunctions, ignoreUndefined) +
1
);
}
case 'function':
// WTF for 0.4.X where typeof /someregexp/ === 'function'
if (value instanceof RegExp || isRegExp(value) || String.call(value) === '[object RegExp]') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Buffer.byteLength(value.source, 'utf8') +
1 +
(value.global ? 1 : 0) +
(value.ignoreCase ? 1 : 0) +
(value.multiline ? 1 : 0) +
1
);
} else {
if (serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
4 +
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
1 +
calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
);
} else if (serializeFunctions) {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
4 +
Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
1
);
}
}
}
return 0;
}
+782
View File
@@ -0,0 +1,782 @@
import { Buffer } from 'buffer';
import { Binary } from '../binary';
import type { Document } from '../bson';
import { Code } from '../code';
import * as constants from '../constants';
import { DBRef, DBRefLike, isDBRefLike } from '../db_ref';
import { Decimal128 } from '../decimal128';
import { Double } from '../double';
import { BSONError } from '../error';
import { Int32 } from '../int_32';
import { Long } from '../long';
import { MaxKey } from '../max_key';
import { MinKey } from '../min_key';
import { ObjectId } from '../objectid';
import { BSONRegExp } from '../regexp';
import { BSONSymbol } from '../symbol';
import { Timestamp } from '../timestamp';
import { validateUtf8 } from '../validate_utf8';
/** @public */
export interface DeserializeOptions {
/** evaluate functions in the BSON document scoped to the object deserialized. */
evalFunctions?: boolean;
/** cache evaluated functions for reuse. */
cacheFunctions?: boolean;
/**
* use a crc32 code for caching, otherwise use the string of the function.
* @deprecated this option to use the crc32 function never worked as intended
* due to the fact that the crc32 function itself was never implemented.
* */
cacheFunctionsCrc32?: boolean;
/** when deserializing a Long will fit it into a Number if it's smaller than 53 bits */
promoteLongs?: boolean;
/** when deserializing a Binary will return it as a node.js Buffer instance. */
promoteBuffers?: boolean;
/** when deserializing will promote BSON values to their Node.js closest equivalent types. */
promoteValues?: boolean;
/** allow to specify if there what fields we wish to return as unserialized raw buffer. */
fieldsAsRaw?: Document;
/** return BSON regular expressions as BSONRegExp instances. */
bsonRegExp?: boolean;
/** allows the buffer to be larger than the parsed BSON object */
allowObjectSmallerThanBufferSize?: boolean;
/** Offset into buffer to begin reading document from */
index?: number;
raw?: boolean;
/** Allows for opt-out utf-8 validation for all keys or
* specified keys. Must be all true or all false.
*
* @example
* ```js
* // disables validation on all keys
* validation: { utf8: false }
*
* // enables validation only on specified keys a, b, and c
* validation: { utf8: { a: true, b: true, c: true } }
*
* // disables validation only on specified keys a, b
* validation: { utf8: { a: false, b: false } }
* ```
*/
validation?: { utf8: boolean | Record<string, true> | Record<string, false> };
}
// Internal long versions
const JS_INT_MAX_LONG = Long.fromNumber(constants.JS_INT_MAX);
const JS_INT_MIN_LONG = Long.fromNumber(constants.JS_INT_MIN);
const functionCache: { [hash: string]: Function } = {};
export function deserialize(
buffer: Buffer,
options: DeserializeOptions,
isArray?: boolean
): Document {
options = options == null ? {} : options;
const index = options && options.index ? options.index : 0;
// Read the document size
const size =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
if (size < 5) {
throw new BSONError(`bson size must be >= 5, is ${size}`);
}
if (options.allowObjectSmallerThanBufferSize && buffer.length < size) {
throw new BSONError(`buffer length ${buffer.length} must be >= bson size ${size}`);
}
if (!options.allowObjectSmallerThanBufferSize && buffer.length !== size) {
throw new BSONError(`buffer length ${buffer.length} must === bson size ${size}`);
}
if (size + index > buffer.byteLength) {
throw new BSONError(
`(bson size ${size} + options.index ${index} must be <= buffer length ${buffer.byteLength})`
);
}
// Illegal end value
if (buffer[index + size - 1] !== 0) {
throw new BSONError(
"One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00"
);
}
// Start deserializtion
return deserializeObject(buffer, index, options, isArray);
}
const allowedDBRefKeys = /^\$ref$|^\$id$|^\$db$/;
function deserializeObject(
buffer: Buffer,
index: number,
options: DeserializeOptions,
isArray = false
) {
const evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
const cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
const fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw'];
// Return raw bson buffer instead of parsing it
const raw = options['raw'] == null ? false : options['raw'];
// Return BSONRegExp objects instead of native regular expressions
const bsonRegExp = typeof options['bsonRegExp'] === 'boolean' ? options['bsonRegExp'] : false;
// Controls the promotion of values vs wrapper classes
const promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
const promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
const promoteValues = options['promoteValues'] == null ? true : options['promoteValues'];
// Ensures default validation option if none given
const validation = options.validation == null ? { utf8: true } : options.validation;
// Shows if global utf-8 validation is enabled or disabled
let globalUTFValidation = true;
// Reflects utf-8 validation setting regardless of global or specific key validation
let validationSetting: boolean;
// Set of keys either to enable or disable validation on
const utf8KeysSet = new Set();
// Check for boolean uniformity and empty validation option
const utf8ValidatedKeys = validation.utf8;
if (typeof utf8ValidatedKeys === 'boolean') {
validationSetting = utf8ValidatedKeys;
} else {
globalUTFValidation = false;
const utf8ValidationValues = Object.keys(utf8ValidatedKeys).map(function (key) {
return utf8ValidatedKeys[key];
});
if (utf8ValidationValues.length === 0) {
throw new BSONError('UTF-8 validation setting cannot be empty');
}
if (typeof utf8ValidationValues[0] !== 'boolean') {
throw new BSONError('Invalid UTF-8 validation option, must specify boolean values');
}
validationSetting = utf8ValidationValues[0];
// Ensures boolean uniformity in utf-8 validation (all true or all false)
if (!utf8ValidationValues.every(item => item === validationSetting)) {
throw new BSONError('Invalid UTF-8 validation option - keys must be all true or all false');
}
}
// Add keys to set that will either be validated or not based on validationSetting
if (!globalUTFValidation) {
for (const key of Object.keys(utf8ValidatedKeys)) {
utf8KeysSet.add(key);
}
}
// Set the start index
const startIndex = index;
// Validate that we have at least 4 bytes of buffer
if (buffer.length < 5) throw new BSONError('corrupt bson message < 5 bytes long');
// Read the document size
const size =
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
// Ensure buffer is valid size
if (size < 5 || size > buffer.length) throw new BSONError('corrupt bson message');
// Create holding object
const object: Document = isArray ? [] : {};
// Used for arrays to skip having to perform utf8 decoding
let arrayIndex = 0;
const done = false;
let isPossibleDBRef = isArray ? false : null;
// While we have more left data left keep parsing
const dataview = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
while (!done) {
// Read the type
const elementType = buffer[index++];
// If we get a zero it's the last byte, exit
if (elementType === 0) break;
// Get the start search index
let i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.byteLength) throw new BSONError('Bad BSON Document: illegal CString');
// Represents the key
const name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
// shouldValidateKey is true if the key should be validated, false otherwise
let shouldValidateKey = true;
if (globalUTFValidation || utf8KeysSet.has(name)) {
shouldValidateKey = validationSetting;
} else {
shouldValidateKey = !validationSetting;
}
if (isPossibleDBRef !== false && (name as string)[0] === '$') {
isPossibleDBRef = allowedDBRefKeys.test(name as string);
}
let value;
index = i + 1;
if (elementType === constants.BSON_DATA_STRING) {
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
) {
throw new BSONError('bad string length in bson');
}
value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
index = index + stringSize;
} else if (elementType === constants.BSON_DATA_OID) {
const oid = Buffer.alloc(12);
buffer.copy(oid, 0, index, index + 12);
value = new ObjectId(oid);
index = index + 12;
} else if (elementType === constants.BSON_DATA_INT && promoteValues === false) {
value = new Int32(
buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24)
);
} else if (elementType === constants.BSON_DATA_INT) {
value =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
} else if (elementType === constants.BSON_DATA_NUMBER && promoteValues === false) {
value = new Double(dataview.getFloat64(index, true));
index = index + 8;
} else if (elementType === constants.BSON_DATA_NUMBER) {
value = dataview.getFloat64(index, true);
index = index + 8;
} else if (elementType === constants.BSON_DATA_DATE) {
const lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
value = new Date(new Long(lowBits, highBits).toNumber());
} else if (elementType === constants.BSON_DATA_BOOLEAN) {
if (buffer[index] !== 0 && buffer[index] !== 1)
throw new BSONError('illegal boolean type value');
value = buffer[index++] === 1;
} else if (elementType === constants.BSON_DATA_OBJECT) {
const _index = index;
const objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
if (objectSize <= 0 || objectSize > buffer.length - index)
throw new BSONError('bad embedded document length in bson');
// We have a raw value
if (raw) {
value = buffer.slice(index, index + objectSize);
} else {
let objectOptions = options;
if (!globalUTFValidation) {
objectOptions = { ...options, validation: { utf8: shouldValidateKey } };
}
value = deserializeObject(buffer, _index, objectOptions, false);
}
index = index + objectSize;
} else if (elementType === constants.BSON_DATA_ARRAY) {
const _index = index;
const objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
let arrayOptions = options;
// Stop index
const stopIndex = index + objectSize;
// All elements of array to be returned as raw bson
if (fieldsAsRaw && fieldsAsRaw[name]) {
arrayOptions = {};
for (const n in options) {
(
arrayOptions as {
[key: string]: DeserializeOptions[keyof DeserializeOptions];
}
)[n] = options[n as keyof DeserializeOptions];
}
arrayOptions['raw'] = true;
}
if (!globalUTFValidation) {
arrayOptions = { ...arrayOptions, validation: { utf8: shouldValidateKey } };
}
value = deserializeObject(buffer, _index, arrayOptions, true);
index = index + objectSize;
if (buffer[index - 1] !== 0) throw new BSONError('invalid array terminator byte');
if (index !== stopIndex) throw new BSONError('corrupted array bson');
} else if (elementType === constants.BSON_DATA_UNDEFINED) {
value = undefined;
} else if (elementType === constants.BSON_DATA_NULL) {
value = null;
} else if (elementType === constants.BSON_DATA_LONG) {
// Unpack the low and high bits
const lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const long = new Long(lowBits, highBits);
// Promote the long if possible
if (promoteLongs && promoteValues === true) {
value =
long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG)
? long.toNumber()
: long;
} else {
value = long;
}
} else if (elementType === constants.BSON_DATA_DECIMAL128) {
// Buffer to contain the decimal bytes
const bytes = Buffer.alloc(16);
// Copy the next 16 bytes into the bytes buffer
buffer.copy(bytes, 0, index, index + 16);
// Update index
index = index + 16;
// Assign the new Decimal128 value
const decimal128 = new Decimal128(bytes) as Decimal128 | { toObject(): unknown };
// If we have an alternative mapper use that
if ('toObject' in decimal128 && typeof decimal128.toObject === 'function') {
value = decimal128.toObject();
} else {
value = decimal128;
}
} else if (elementType === constants.BSON_DATA_BINARY) {
let binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const totalBinarySize = binarySize;
const subType = buffer[index++];
// Did we have a negative binary size, throw
if (binarySize < 0) throw new BSONError('Negative binary type element size found');
// Is the length longer than the document
if (binarySize > buffer.byteLength)
throw new BSONError('Binary type size larger than document size');
// Decode as raw Buffer object if options specifies it
if (buffer['slice'] != null) {
// If we have subtype 2 skip the 4 bytes for the size
if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (binarySize < 0)
throw new BSONError('Negative binary type element size found for subtype 0x02');
if (binarySize > totalBinarySize - 4)
throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
if (binarySize < totalBinarySize - 4)
throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
}
if (promoteBuffers && promoteValues) {
value = buffer.slice(index, index + binarySize);
} else {
value = new Binary(buffer.slice(index, index + binarySize), subType);
if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = value.toUUID();
}
}
} else {
const _buffer = Buffer.alloc(binarySize);
// If we have subtype 2 skip the 4 bytes for the size
if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
binarySize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (binarySize < 0)
throw new BSONError('Negative binary type element size found for subtype 0x02');
if (binarySize > totalBinarySize - 4)
throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
if (binarySize < totalBinarySize - 4)
throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
}
// Copy the data
for (i = 0; i < binarySize; i++) {
_buffer[i] = buffer[index + i];
}
if (promoteBuffers && promoteValues) {
value = _buffer;
} else if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW) {
value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
} else {
value = new Binary(buffer.slice(index, index + binarySize), subType);
}
}
// Update the index
index = index + binarySize;
} else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === false) {
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString');
// Return the C string
const source = buffer.toString('utf8', index, i);
// Create the regexp
index = i + 1;
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString');
// Return the C string
const regExpOptions = buffer.toString('utf8', index, i);
index = i + 1;
// For each option add the corresponding one for javascript
const optionsArray = new Array(regExpOptions.length);
// Parse options
for (i = 0; i < regExpOptions.length; i++) {
switch (regExpOptions[i]) {
case 'm':
optionsArray[i] = 'm';
break;
case 's':
optionsArray[i] = 'g';
break;
case 'i':
optionsArray[i] = 'i';
break;
}
}
value = new RegExp(source, optionsArray.join(''));
} else if (elementType === constants.BSON_DATA_REGEXP && bsonRegExp === true) {
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString');
// Return the C string
const source = buffer.toString('utf8', index, i);
index = i + 1;
// Get the start search index
i = index;
// Locate the end of the c string
while (buffer[i] !== 0x00 && i < buffer.length) {
i++;
}
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.length) throw new BSONError('Bad BSON Document: illegal CString');
// Return the C string
const regExpOptions = buffer.toString('utf8', index, i);
index = i + 1;
// Set the object
value = new BSONRegExp(source, regExpOptions);
} else if (elementType === constants.BSON_DATA_SYMBOL) {
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
) {
throw new BSONError('bad string length in bson');
}
const symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
value = promoteValues ? symbol : new BSONSymbol(symbol);
index = index + stringSize;
} else if (elementType === constants.BSON_DATA_TIMESTAMP) {
const lowBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
const highBits =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
value = new Timestamp(lowBits, highBits);
} else if (elementType === constants.BSON_DATA_MIN_KEY) {
value = new MinKey();
} else if (elementType === constants.BSON_DATA_MAX_KEY) {
value = new MaxKey();
} else if (elementType === constants.BSON_DATA_CODE) {
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
) {
throw new BSONError('bad string length in bson');
}
const functionString = getValidatedString(
buffer,
index,
index + stringSize - 1,
shouldValidateKey
);
// If we are evaluating the functions
if (evalFunctions) {
// If we have cache enabled let's look for the md5 of the function in the cache
if (cacheFunctions) {
// Got to do this to avoid V8 deoptimizing the call due to finding eval
value = isolateEval(functionString, functionCache, object);
} else {
value = isolateEval(functionString);
}
} else {
value = new Code(functionString);
}
// Update parse index position
index = index + stringSize;
} else if (elementType === constants.BSON_DATA_CODE_W_SCOPE) {
const totalSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Element cannot be shorter than totalSize + stringSize + documentSize + terminator
if (totalSize < 4 + 4 + 4 + 1) {
throw new BSONError('code_w_scope total size shorter minimum expected length');
}
// Get the code string size
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Check if we have a valid string
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
) {
throw new BSONError('bad string length in bson');
}
// Javascript function
const functionString = getValidatedString(
buffer,
index,
index + stringSize - 1,
shouldValidateKey
);
// Update parse index position
index = index + stringSize;
// Parse the element
const _index = index;
// Decode the size of the object document
const objectSize =
buffer[index] |
(buffer[index + 1] << 8) |
(buffer[index + 2] << 16) |
(buffer[index + 3] << 24);
// Decode the scope object
const scopeObject = deserializeObject(buffer, _index, options, false);
// Adjust the index
index = index + objectSize;
// Check if field length is too short
if (totalSize < 4 + 4 + objectSize + stringSize) {
throw new BSONError('code_w_scope total size is too short, truncating scope');
}
// Check if totalSize field is too long
if (totalSize > 4 + 4 + objectSize + stringSize) {
throw new BSONError('code_w_scope total size is too long, clips outer document');
}
// If we are evaluating the functions
if (evalFunctions) {
// If we have cache enabled let's look for the md5 of the function in the cache
if (cacheFunctions) {
// Got to do this to avoid V8 deoptimizing the call due to finding eval
value = isolateEval(functionString, functionCache, object);
} else {
value = isolateEval(functionString);
}
value.scope = scopeObject;
} else {
value = new Code(functionString, scopeObject);
}
} else if (elementType === constants.BSON_DATA_DBPOINTER) {
// Get the code string size
const stringSize =
buffer[index++] |
(buffer[index++] << 8) |
(buffer[index++] << 16) |
(buffer[index++] << 24);
// Check if we have a valid string
if (
stringSize <= 0 ||
stringSize > buffer.length - index ||
buffer[index + stringSize - 1] !== 0
)
throw new BSONError('bad string length in bson');
// Namespace
if (validation != null && validation.utf8) {
if (!validateUtf8(buffer, index, index + stringSize - 1)) {
throw new BSONError('Invalid UTF-8 string in BSON document');
}
}
const namespace = buffer.toString('utf8', index, index + stringSize - 1);
// Update parse index position
index = index + stringSize;
// Read the oid
const oidBuffer = Buffer.alloc(12);
buffer.copy(oidBuffer, 0, index, index + 12);
const oid = new ObjectId(oidBuffer);
// Update the index
index = index + 12;
// Upgrade to DBRef type
value = new DBRef(namespace, oid);
} else {
throw new BSONError(
`Detected unknown BSON type ${elementType.toString(16)} for fieldname "${name}"`
);
}
if (name === '__proto__') {
Object.defineProperty(object, name, {
value,
writable: true,
enumerable: true,
configurable: true
});
} else {
object[name] = value;
}
}
// Check if the deserialization was against a valid array/object
if (size !== index - startIndex) {
if (isArray) throw new BSONError('corrupt array bson');
throw new BSONError('corrupt object bson');
}
// if we did not find "$ref", "$id", "$db", or found an extraneous $key, don't make a DBRef
if (!isPossibleDBRef) return object;
if (isDBRefLike(object)) {
const copy = Object.assign({}, object) as Partial<DBRefLike>;
delete copy.$ref;
delete copy.$id;
delete copy.$db;
return new DBRef(object.$ref, object.$id, object.$db, copy);
}
return object;
}
/**
* Ensure eval is isolated, store the result in functionCache.
*
* @internal
*/
function isolateEval(
functionString: string,
functionCache?: { [hash: string]: Function },
object?: Document
) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
if (!functionCache) return new Function(functionString);
// Check for cache hit, eval if missing and return cached function
if (functionCache[functionString] == null) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
functionCache[functionString] = new Function(functionString);
}
// Set the object
return functionCache[functionString].bind(object);
}
function getValidatedString(
buffer: Buffer,
start: number,
end: number,
shouldValidateUtf8: boolean
) {
const value = buffer.toString('utf8', start, end);
// if utf8 validation is on, do the check
if (shouldValidateUtf8) {
for (let i = 0; i < value.length; i++) {
if (value.charCodeAt(i) === 0xfffd) {
if (!validateUtf8(buffer, start, end)) {
throw new BSONError('Invalid UTF-8 string in BSON document');
}
break;
}
}
}
return value;
}
+1076
View File
File diff suppressed because it is too large Load Diff
+127
View File
@@ -0,0 +1,127 @@
import { Buffer } from 'buffer';
import { getGlobal } from '../utils/global';
type RandomBytesFunction = (size: number) => Uint8Array;
/**
* Normalizes our expected stringified form of a function across versions of node
* @param fn - The function to stringify
*/
export function normalizedFunctionString(fn: Function): string {
return fn.toString().replace('function(', 'function (');
}
function isReactNative() {
const g = getGlobal<{ navigator?: { product?: string } }>();
return typeof g.navigator === 'object' && g.navigator.product === 'ReactNative';
}
const insecureRandomBytes: RandomBytesFunction = function insecureRandomBytes(size: number) {
const insecureWarning = isReactNative()
? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
: 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.';
console.warn(insecureWarning);
const result = Buffer.alloc(size);
for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256);
return result;
};
/* We do not want to have to include DOM types just for this check */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let window: any;
declare let require: Function;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let global: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let process: any; // Used by @rollup/plugin-replace
const detectRandomBytes = (): RandomBytesFunction => {
if (process.browser) {
if (typeof window !== 'undefined') {
// browser crypto implementation(s)
const target = window.crypto || window.msCrypto; // allow for IE11
if (target && target.getRandomValues) {
return size => target.getRandomValues(Buffer.alloc(size));
}
}
if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
// allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
return size => global.crypto.getRandomValues(Buffer.alloc(size));
}
return insecureRandomBytes;
} else {
let requiredRandomBytes: RandomBytesFunction | null | undefined;
try {
requiredRandomBytes = require('crypto').randomBytes;
} catch (e) {
// keep the fallback
}
// NOTE: in transpiled cases the above require might return null/undefined
return requiredRandomBytes || insecureRandomBytes;
}
};
export const randomBytes = detectRandomBytes();
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(
Object.prototype.toString.call(value)
);
}
export function isUint8Array(value: unknown): value is Uint8Array {
return Object.prototype.toString.call(value) === '[object Uint8Array]';
}
export function isBigInt64Array(value: unknown): value is BigInt64Array {
return Object.prototype.toString.call(value) === '[object BigInt64Array]';
}
export function isBigUInt64Array(value: unknown): value is BigUint64Array {
return Object.prototype.toString.call(value) === '[object BigUint64Array]';
}
export function isRegExp(d: unknown): d is RegExp {
return Object.prototype.toString.call(d) === '[object RegExp]';
}
export function isMap(d: unknown): d is Map<unknown, unknown> {
return Object.prototype.toString.call(d) === '[object Map]';
}
/** Call to check if your environment has `Buffer` */
export function haveBuffer(): boolean {
return typeof global !== 'undefined' && typeof global.Buffer !== 'undefined';
}
// To ensure that 0.4 of node works correctly
export function isDate(d: unknown): d is Date {
return isObjectLike(d) && Object.prototype.toString.call(d) === '[object Date]';
}
/**
* @internal
* this is to solve the `'someKey' in x` problem where x is unknown.
* https://github.com/typescript-eslint/typescript-eslint/issues/1071#issuecomment-541955753
*/
export function isObjectLike(candidate: unknown): candidate is Record<string, unknown> {
return typeof candidate === 'object' && candidate !== null;
}
declare let console: { warn(...message: unknown[]): void };
export function deprecate<T extends Function>(fn: T, message: string): T {
let warned = false;
function deprecated(this: unknown, ...args: unknown[]) {
if (!warned) {
console.warn(message);
warned = true;
}
return fn.apply(this, args);
}
return deprecated as unknown as T;
}
+105
View File
@@ -0,0 +1,105 @@
import { BSONError, BSONTypeError } from './error';
import type { EJSONOptions } from './extended_json';
function alphabetize(str: string): string {
return str.split('').sort().join('');
}
/** @public */
export interface BSONRegExpExtendedLegacy {
$regex: string | BSONRegExp;
$options: string;
}
/** @public */
export interface BSONRegExpExtended {
$regularExpression: {
pattern: string;
options: string;
};
}
/**
* A class representation of the BSON RegExp type.
* @public
* @category BSONType
*/
export class BSONRegExp {
_bsontype!: 'BSONRegExp';
pattern!: string;
options!: string;
/**
* @param pattern - The regular expression pattern to match
* @param options - The regular expression options
*/
constructor(pattern: string, options?: string) {
if (!(this instanceof BSONRegExp)) return new BSONRegExp(pattern, options);
this.pattern = pattern;
this.options = alphabetize(options ?? '');
if (this.pattern.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Regex patterns cannot contain null bytes, found: ${JSON.stringify(this.pattern)}`
);
}
if (this.options.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Regex options cannot contain null bytes, found: ${JSON.stringify(this.options)}`
);
}
// Validate options
for (let i = 0; i < this.options.length; i++) {
if (
!(
this.options[i] === 'i' ||
this.options[i] === 'm' ||
this.options[i] === 'x' ||
this.options[i] === 'l' ||
this.options[i] === 's' ||
this.options[i] === 'u'
)
) {
throw new BSONError(`The regular expression option [${this.options[i]}] is not supported`);
}
}
}
static parseOptions(options?: string): string {
return options ? options.split('').sort().join('') : '';
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): BSONRegExpExtendedLegacy | BSONRegExpExtended {
options = options || {};
if (options.legacy) {
return { $regex: this.pattern, $options: this.options };
}
return { $regularExpression: { pattern: this.pattern, options: this.options } };
}
/** @internal */
static fromExtendedJSON(doc: BSONRegExpExtendedLegacy | BSONRegExpExtended): BSONRegExp {
if ('$regex' in doc) {
if (typeof doc.$regex !== 'string') {
// This is for $regex query operators that have extended json values.
if (doc.$regex._bsontype === 'BSONRegExp') {
return doc as unknown as BSONRegExp;
}
} else {
return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
}
}
if ('$regularExpression' in doc) {
return new BSONRegExp(
doc.$regularExpression.pattern,
BSONRegExp.parseOptions(doc.$regularExpression.options)
);
}
throw new BSONTypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
}
}
Object.defineProperty(BSONRegExp.prototype, '_bsontype', { value: 'BSONRegExp' });
+58
View File
@@ -0,0 +1,58 @@
/** @public */
export interface BSONSymbolExtended {
$symbol: string;
}
/**
* A class representation of the BSON Symbol type.
* @public
* @category BSONType
*/
export class BSONSymbol {
_bsontype!: 'Symbol';
value!: string;
/**
* @param value - the string representing the symbol.
*/
constructor(value: string) {
if (!(this instanceof BSONSymbol)) return new BSONSymbol(value);
this.value = value;
}
/** Access the wrapped string value. */
valueOf(): string {
return this.value;
}
toString(): string {
return this.value;
}
/** @internal */
inspect(): string {
return `new BSONSymbol("${this.value}")`;
}
toJSON(): string {
return this.value;
}
/** @internal */
toExtendedJSON(): BSONSymbolExtended {
return { $symbol: this.value };
}
/** @internal */
static fromExtendedJSON(doc: BSONSymbolExtended): BSONSymbol {
return new BSONSymbol(doc.$symbol);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
}
Object.defineProperty(BSONSymbol.prototype, '_bsontype', { value: 'Symbol' });
+119
View File
@@ -0,0 +1,119 @@
import { Long } from './long';
import { isObjectLike } from './parser/utils';
/** @public */
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
/** @public */
export type LongWithoutOverrides = new (low: unknown, high?: number, unsigned?: boolean) => {
[P in Exclude<keyof Long, TimestampOverrides>]: Long[P];
};
/** @public */
export const LongWithoutOverridesClass: LongWithoutOverrides =
Long as unknown as LongWithoutOverrides;
/** @public */
export interface TimestampExtended {
$timestamp: {
t: number;
i: number;
};
}
/**
* @public
* @category BSONType
* */
export class Timestamp extends LongWithoutOverridesClass {
_bsontype!: 'Timestamp';
static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
/**
* @param low - A 64-bit Long representing the Timestamp.
*/
constructor(long: Long);
/**
* @param value - A pair of two values indicating timestamp and increment.
*/
constructor(value: { t: number; i: number });
/**
* @param low - the low (signed) 32 bits of the Timestamp.
* @param high - the high (signed) 32 bits of the Timestamp.
* @deprecated Please use `Timestamp({ t: high, i: low })` or `Timestamp(Long(low, high))` instead.
*/
constructor(low: number, high: number);
constructor(low: number | Long | { t: number; i: number }, high?: number) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (!(this instanceof Timestamp)) return new Timestamp(low, high);
if (Long.isLong(low)) {
super(low.low, low.high, true);
} else if (isObjectLike(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
super(low.i, low.t, true);
} else {
super(low, high, true);
}
Object.defineProperty(this, '_bsontype', {
value: 'Timestamp',
writable: false,
configurable: false,
enumerable: false
});
}
toJSON(): { $timestamp: string } {
return {
$timestamp: this.toString()
};
}
/** Returns a Timestamp represented by the given (32-bit) integer value. */
static fromInt(value: number): Timestamp {
return new Timestamp(Long.fromInt(value, true));
}
/** Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. */
static fromNumber(value: number): Timestamp {
return new Timestamp(Long.fromNumber(value, true));
}
/**
* Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
*
* @param lowBits - the low 32-bits.
* @param highBits - the high 32-bits.
*/
static fromBits(lowBits: number, highBits: number): Timestamp {
return new Timestamp(lowBits, highBits);
}
/**
* Returns a Timestamp from the given string, optionally using the given radix.
*
* @param str - the textual representation of the Timestamp.
* @param optRadix - the radix in which the text is written.
*/
static fromString(str: string, optRadix: number): Timestamp {
return new Timestamp(Long.fromString(str, true, optRadix));
}
/** @internal */
toExtendedJSON(): TimestampExtended {
return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
}
/** @internal */
static fromExtendedJSON(doc: TimestampExtended): Timestamp {
return new Timestamp(doc.$timestamp);
}
/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}
inspect(): string {
return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
}
}
+22
View File
@@ -0,0 +1,22 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* We do not want to have to include DOM types just for this check */
declare const window: unknown;
declare const self: unknown;
declare const global: unknown;
function checkForMath(potentialGlobal: any) {
// eslint-disable-next-line eqeqeq
return potentialGlobal && potentialGlobal.Math == Math && potentialGlobal;
}
// https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
export function getGlobal<T = Record<string, unknown>>(): T {
return (
checkForMath(typeof globalThis === 'object' && globalThis) ||
checkForMath(typeof window === 'object' && window) ||
checkForMath(typeof self === 'object' && self) ||
checkForMath(typeof global === 'object' && global) ||
// eslint-disable-next-line @typescript-eslint/no-implied-eval
Function('return this')()
);
}
+33
View File
@@ -0,0 +1,33 @@
import { Buffer } from 'buffer';
import { BSONTypeError } from './error';
// Validation regex for v4 uuid (validates with or without dashes)
const VALIDATION_REGEX =
/^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15})$/i;
export const uuidValidateString = (str: string): boolean =>
typeof str === 'string' && VALIDATION_REGEX.test(str);
export const uuidHexStringToBuffer = (hexString: string): Buffer => {
if (!uuidValidateString(hexString)) {
throw new BSONTypeError(
'UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'
);
}
const sanitizedHexString = hexString.replace(/-/g, '');
return Buffer.from(sanitizedHexString, 'hex');
};
export const bufferToUuidHexString = (buffer: Buffer, includeDashes = true): string =>
includeDashes
? buffer.toString('hex', 0, 4) +
'-' +
buffer.toString('hex', 4, 6) +
'-' +
buffer.toString('hex', 6, 8) +
'-' +
buffer.toString('hex', 8, 10) +
'-' +
buffer.toString('hex', 10, 16)
: buffer.toString('hex');
+47
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;
}