import { Code } from "../code.ts";import { BSON_BINARY_SUBTYPE_DEFAULT, BSON_INT32_MAX, BSON_INT32_MIN, BSONData,} from "../constants.ts";import { DBRef, DBRefLike } from "../db_ref.ts";import { Decimal128 } from "../decimal128.ts";import { Double } from "../double.ts";import { BSONError, BSONTypeError } from "../error.ts";import { writeIEEE754 } from "../float_parser.ts";import { Int32 } from "../int_32.ts";import { Long } from "../long.ts";import { MaxKey, MinKey } from "../key.ts";import { ObjectId } from "../objectid.ts";import { Timestamp } from "../timestamp.ts";import { BSONRegExp } from "../regexp.ts";import { Encoding, normalizedFunctionString, writeToBytes } from "./utils.ts";import { Binary, BinarySizes, BSONSymbol, Document } from "../bson.ts";export interface SerializeOptions { checkKeys?: boolean; serializeFunctions?: boolean; ignoreUndefined?: boolean; index?: number;}
const regexp = /\x00/;const ignoreKeys = new Set(["$db", "$ref", "$id", "$clusterTime"]);
function serializeString( buffer: Uint8Array, key: string, value: string, index: number, isArray?: boolean,) { buffer[index++] = BSONData.STRING; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index = index + numberOfWrittenBytes + 1; buffer[index - 1] = 0; const size = writeToBytes(buffer, value, index + 4, Encoding.Utf8); buffer[index + 3] = ((size + 1) >> 24) & 0xff; buffer[index + 2] = ((size + 1) >> 16) & 0xff; buffer[index + 1] = ((size + 1) >> 8) & 0xff; buffer[index] = (size + 1) & 0xff; index = index + 4 + size; buffer[index++] = 0; return index;}
function serializeNumber( buffer: Uint8Array, key: string, value: number, index: number, isArray?: boolean,) { if ( Number.isInteger(value) && value >= BSON_INT32_MIN && value <= BSON_INT32_MAX ) { buffer[index++] = BSONData.INT; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; buffer[index++] = value & 0xff; buffer[index++] = (value >> 8) & 0xff; buffer[index++] = (value >> 16) & 0xff; buffer[index++] = (value >> 24) & 0xff; } else { buffer[index++] = BSONData.NUMBER; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; writeIEEE754(buffer, value, index, "little", 52, 8); index += 8; }
return index;}
function serializeNull( buffer: Uint8Array, key: string, _: unknown, index: number, isArray?: boolean,) { buffer[index++] = BSONData.NULL;
const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii);
index += numberOfWrittenBytes; buffer[index++] = 0; return index;}
function serializeBoolean( buffer: Uint8Array, key: string, value: boolean, index: number, isArray?: boolean,) { buffer[index++] = BSONData.BOOLEAN; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; buffer[index++] = value ? 1 : 0; return index;}
function serializeDate( buffer: Uint8Array, key: string, value: Date, index: number, isArray?: boolean,) { buffer[index++] = BSONData.DATE; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0;
const dateInMilis = Long.fromNumber(value.getTime()); const lowBits = dateInMilis.getLowBits(); const highBits = dateInMilis.getHighBits(); buffer[index++] = lowBits & 0xff; buffer[index++] = (lowBits >> 8) & 0xff; buffer[index++] = (lowBits >> 16) & 0xff; buffer[index++] = (lowBits >> 24) & 0xff; buffer[index++] = highBits & 0xff; buffer[index++] = (highBits >> 8) & 0xff; buffer[index++] = (highBits >> 16) & 0xff; buffer[index++] = (highBits >> 24) & 0xff; return index;}
function serializeRegExp( buffer: Uint8Array, key: string, value: RegExp, index: number, isArray?: boolean,) { buffer[index++] = BSONData.REGEXP; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii);
index += numberOfWrittenBytes; buffer[index++] = 0; if (value.source && value.source.match(regexp) != null) { throw Error(`value ${value.source} must not contain null bytes`); } index += writeToBytes(buffer, value.source, index, Encoding.Utf8); buffer[index++] = 0x00; if (value.ignoreCase) buffer[index++] = 0x69; if (value.global) buffer[index++] = 0x73; if (value.multiline) buffer[index++] = 0x6d;
buffer[index++] = 0x00; return index;}
function serializeBSONRegExp( buffer: Uint8Array, key: string, value: BSONRegExp, index: number, isArray?: boolean,) { buffer[index++] = BSONData.REGEXP; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0;
if (value.pattern.match(regexp) != null) { throw Error(`pattern ${value.pattern} must not contain null bytes`); }
index += writeToBytes(buffer, value.pattern, index, Encoding.Utf8); buffer[index++] = 0x00; index += writeToBytes( buffer, value.options.split("").sort().join(""), index, Encoding.Utf8, ); buffer[index++] = 0x00; return index;}
function serializeMinMax( buffer: Uint8Array, key: string, value: MinKey | MaxKey, index: number, isArray?: boolean,) { if (value === null) { buffer[index++] = BSONData.NULL; } else if (value instanceof MinKey) { buffer[index++] = BSONData.MIN_KEY; } else { buffer[index++] = BSONData.MAX_KEY; }
const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; return index;}
function serializeObjectId( buffer: Uint8Array, key: string, value: ObjectId, index: number, isArray?: boolean,) { buffer[index++] = BSONData.OID; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii);
index += numberOfWrittenBytes; buffer[index++] = 0;
if (typeof value.id === "string") { writeToBytes(buffer, value.id, index, Encoding.Ascii); } else if (value.id instanceof Uint8Array) { buffer.set(value.id.subarray(0, 12), index); } else { throw new BSONTypeError( `object [${JSON.stringify(value)}] is not a valid ObjectId`, ); }
return index + 12;}
function serializeBuffer( buffer: Uint8Array, key: string, value: Uint8Array, index: number, isArray?: boolean,) { buffer[index++] = BSONData.BINARY; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; const size = value.length; buffer[index++] = size & 0xff; buffer[index++] = (size >> 8) & 0xff; buffer[index++] = (size >> 16) & 0xff; buffer[index++] = (size >> 24) & 0xff; buffer[index++] = BSON_BINARY_SUBTYPE_DEFAULT; buffer.set(value, index); index += size; return index;}
function serializeObject( buffer: Uint8Array, key: string, value: Document, index: number, checkKeys = false, depth = 0, serializeFunctions = false, ignoreUndefined = true, isArray = false, path: Document[] = [],) { for (let i = 0; i < path.length; i++) { if (path[i] === value) throw new BSONError("cyclic dependency detected"); }
path.push(value); buffer[index++] = Array.isArray(value) ? BSONData.ARRAY : BSONData.OBJECT; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; const endIndex = serializeInto( buffer, value, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined, path, ); path.pop(); return endIndex;}
function serializeDecimal128( buffer: Uint8Array, key: string, value: Decimal128, index: number, isArray?: boolean,) { buffer[index++] = BSONData.DECIMAL128; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; buffer.set(value.bytes.subarray(0, 16), index); return index + 16;}
function serializeLong( buffer: Uint8Array, key: string, value: Long, index: number, isArray?: boolean,) { buffer[index++] = value instanceof Timestamp ? BSONData.TIMESTAMP : BSONData.LONG; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; const lowBits = value.getLowBits(); const highBits = value.getHighBits(); buffer[index++] = lowBits & 0xff; buffer[index++] = (lowBits >> 8) & 0xff; buffer[index++] = (lowBits >> 16) & 0xff; buffer[index++] = (lowBits >> 24) & 0xff; buffer[index++] = highBits & 0xff; buffer[index++] = (highBits >> 8) & 0xff; buffer[index++] = (highBits >> 16) & 0xff; buffer[index++] = (highBits >> 24) & 0xff; return index;}
function serializeInt32( buffer: Uint8Array, key: string, value: Int32 | number, index: number, isArray?: boolean,) { value = value.valueOf(); buffer[index++] = BSONData.INT; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; buffer[index++] = value & 0xff; buffer[index++] = (value >> 8) & 0xff; buffer[index++] = (value >> 16) & 0xff; buffer[index++] = (value >> 24) & 0xff; return index;}
function serializeDouble( buffer: Uint8Array, key: string, value: Double, index: number, isArray?: boolean,) { buffer[index++] = BSONData.NUMBER;
const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii);
index += numberOfWrittenBytes; buffer[index++] = 0;
writeIEEE754(buffer, value.value, index, "little", 52, 8);
index += 8; return index;}
function serializeFunction( buffer: Uint8Array, key: string, value: Function, index: number, _checkKeys = false, _depth = 0, isArray?: boolean,) { buffer[index++] = BSONData.CODE; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; const functionString = normalizedFunctionString(value);
const size = writeToBytes(buffer, functionString, index + 4, Encoding.Utf8) + 1; buffer[index] = size & 0xff; buffer[index + 1] = (size >> 8) & 0xff; buffer[index + 2] = (size >> 16) & 0xff; buffer[index + 3] = (size >> 24) & 0xff; index = index + 4 + size - 1; buffer[index++] = 0; return index;}
function serializeCode( buffer: Uint8Array, key: string, value: Code, index: number, checkKeys = false, depth = 0, serializeFunctions = false, ignoreUndefined = true, isArray = false,) { if (value.scope && typeof value.scope === "object") { buffer[index++] = BSONData.CODE_W_SCOPE; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0;
let startIndex = index;
const functionString = typeof value.code === "string" ? value.code : value.code.toString(); index += 4; const codeSize = writeToBytes(buffer, functionString, index + 4, Encoding.Utf8) + 1; buffer[index] = codeSize & 0xff; buffer[index + 1] = (codeSize >> 8) & 0xff; buffer[index + 2] = (codeSize >> 16) & 0xff; buffer[index + 3] = (codeSize >> 24) & 0xff; buffer[index + 4 + codeSize - 1] = 0; index = index + codeSize + 4;
const endIndex = serializeInto( buffer, value.scope, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined, ); index = endIndex - 1;
const totalSize = endIndex - startIndex;
buffer[startIndex++] = totalSize & 0xff; buffer[startIndex++] = (totalSize >> 8) & 0xff; buffer[startIndex++] = (totalSize >> 16) & 0xff; buffer[startIndex++] = (totalSize >> 24) & 0xff; } else { buffer[index++] = BSONData.CODE; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; const functionString = value.code.toString(); const size = writeToBytes(buffer, functionString, index + 4, Encoding.Utf8) + 1; buffer[index] = size & 0xff; buffer[index + 1] = (size >> 8) & 0xff; buffer[index + 2] = (size >> 16) & 0xff; buffer[index + 3] = (size >> 24) & 0xff; index = index + 4 + size - 1; } buffer[index++] = 0;
return index;}
function serializeBinary( buffer: Uint8Array, key: string, value: Binary, index: number, isArray?: boolean,) { buffer[index++] = BSONData.BINARY; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; const data = value.buffer; let size = value.buffer.length; if (value.subType === BinarySizes.SUBTYPE_BYTE_ARRAY) size += 4; buffer[index++] = size & 0xff; buffer[index++] = (size >> 8) & 0xff; buffer[index++] = (size >> 16) & 0xff; buffer[index++] = (size >> 24) & 0xff; buffer[index++] = value.subType;
if (value.subType === BinarySizes.SUBTYPE_BYTE_ARRAY) { size -= 4; buffer[index++] = size & 0xff; buffer[index++] = (size >> 8) & 0xff; buffer[index++] = (size >> 16) & 0xff; buffer[index++] = (size >> 24) & 0xff; }
buffer.set(data, index); index += size; return index;}
function serializeSymbol( buffer: Uint8Array, key: string, value: BSONSymbol, index: number, isArray?: boolean,) { buffer[index++] = BSONData.SYMBOL; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii); index += numberOfWrittenBytes; buffer[index++] = 0; const size = writeToBytes(buffer, value.value, index + 4, Encoding.Utf8) + 1; buffer[index] = size & 0xff; buffer[index + 1] = (size >> 8) & 0xff; buffer[index + 2] = (size >> 16) & 0xff; buffer[index + 3] = (size >> 24) & 0xff; index = index + 4 + size - 1; buffer[index++] = 0x00; return index;}
function serializeDBRef( buffer: Uint8Array, key: string, value: DBRef, index: number, depth: number, serializeFunctions: boolean, isArray?: boolean,) { buffer[index++] = BSONData.OBJECT; const numberOfWrittenBytes = !isArray ? writeToBytes(buffer, key, index, Encoding.Utf8) : writeToBytes(buffer, key, index, Encoding.Ascii);
index += numberOfWrittenBytes; buffer[index++] = 0;
let startIndex = index; let output: DBRefLike = { $ref: value.collection, $id: value.oid, };
if (value.db != null) { output.$db = value.db; }
output = Object.assign(output, value.fields); const endIndex = serializeInto( buffer, output, false, index, depth + 1, serializeFunctions, );
const size = endIndex - startIndex; buffer[startIndex++] = size & 0xff; buffer[startIndex++] = (size >> 8) & 0xff; buffer[startIndex++] = (size >> 16) & 0xff; buffer[startIndex++] = (size >> 24) & 0xff; return endIndex;}
export function serializeInto( buffer: Uint8Array, object: Document, checkKeys = false, startingIndex = 0, depth = 0, serializeFunctions = false, ignoreUndefined = true, path: Document[] = [],): number { startingIndex = startingIndex || 0; path = path || [];
path.push(object);
let index = startingIndex + 4;
if (Array.isArray(object)) { for (let i = 0; i < object.length; i++) { const key = i.toString(); let value = object[i];
if (value?.toBSON) { if (typeof value.toBSON !== "function") { throw new BSONTypeError("toBSON is not a function"); } value = value.toBSON(); }
if (typeof value === "string") { index = serializeString(buffer, key, value, index, true); } else if (typeof value === "number") { index = serializeNumber(buffer, key, value, index, true); } else if (typeof value === "bigint") { throw new BSONTypeError( "Unsupported type BigInt, please use Decimal128", ); } else if (typeof value === "boolean") { index = serializeBoolean(buffer, key, value, index, true); } else if (value instanceof Date) { index = serializeDate(buffer, key, value, index, true); } else if (value === undefined) { index = serializeNull(buffer, key, value, index, true); } else if (value === null) { index = serializeNull(buffer, key, value, index, true); } else if (value instanceof ObjectId) { index = serializeObjectId(buffer, key, value, index, true); } else if (value instanceof Uint8Array) { index = serializeBuffer(buffer, key, value, index, true); } else if (value instanceof RegExp) { index = serializeRegExp(buffer, key, value, index, true); } else if (value instanceof Decimal128) { index = serializeDecimal128(buffer, key, value, index, true); } else if (value instanceof Long || value instanceof Timestamp) { index = serializeLong(buffer, key, value, index, true); } else if (value instanceof Double) { index = serializeDouble(buffer, key, value, index, true); } else if (typeof value === "function" && serializeFunctions) { index = serializeFunction( buffer, key, value, index, checkKeys, depth, true, ); } else if (value instanceof Code) { index = serializeCode( buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, true, ); } else if (value instanceof Binary) { index = serializeBinary(buffer, key, value, index, true); } else if (value instanceof BSONSymbol) { index = serializeSymbol(buffer, key, value, index, true); } else if (value instanceof DBRef) { index = serializeDBRef( buffer, key, value, index, depth, serializeFunctions, true, ); } else if (value instanceof BSONRegExp) { index = serializeBSONRegExp(buffer, key, value, index, true); } else if (value instanceof Int32) { index = serializeInt32(buffer, key, value, index, true); } else if (value instanceof MinKey || value instanceof MaxKey) { index = serializeMinMax(buffer, key, value, index, true); } else if (value instanceof Object) { index = serializeObject( buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, true, path, ); } else { throw new BSONTypeError(`Unrecognized or invalid BSON Type: ${value}`); } } } else if (object instanceof Map) { const iterator = object.entries(); let done = false;
while (!done) { const entry = iterator.next(); done = !!entry.done; if (done) continue;
const key = entry.value[0]; const value = entry.value[1];
const type = typeof value;
if (typeof key === "string" && !ignoreKeys.has(key)) { if (key.match(regexp) != null) { throw Error(`key ${key} must not contain null bytes`); }
if (checkKeys) { if (key.startsWith("$")) { throw Error(`key ${key} must not start with '$'`); } else if (~key.indexOf(".")) { throw Error(`key ${key} must not contain '.'`); } } }
if (type === "string") { index = serializeString(buffer, key, value, index); } else if (type === "number") { index = serializeNumber(buffer, key, value, index); } else if ( type === "bigint" || value instanceof BigInt64Array || value instanceof BigUint64Array ) { throw new BSONTypeError( "Unsupported type BigInt, please use Decimal128", ); } else if (type === "boolean") { index = serializeBoolean(buffer, key, value, index); } else if (value instanceof Date) { index = serializeDate(buffer, key, value, index); } else if ( value === null || (value === undefined && ignoreUndefined === false) ) { index = serializeNull(buffer, key, value, index); } else if (value instanceof ObjectId) { index = serializeObjectId(buffer, key, value, index); } else if (value instanceof Uint8Array) { index = serializeBuffer(buffer, key, value, index); } else if (value instanceof RegExp) { index = serializeRegExp(buffer, key, value, index); } else if (type === "object" && value instanceof Object) { index = serializeObject( buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, false, path, ); } else if (type === "object" && value instanceof Decimal128) { index = serializeDecimal128(buffer, key, value, index); } else if (value instanceof Long) { index = serializeLong(buffer, key, value, index); } else if (value instanceof Double) { index = serializeDouble(buffer, key, value, index); } else if (value instanceof Code) { index = serializeCode( buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, ); } else if (typeof value === "function" && serializeFunctions) { index = serializeFunction( buffer, key, value, index, checkKeys, depth, serializeFunctions, ); } else if (value instanceof Binary) { index = serializeBinary(buffer, key, value, index); } else if (value instanceof BSONSymbol) { index = serializeSymbol(buffer, key, value, index); } else if (value instanceof DBRef) { index = serializeDBRef( buffer, key, value, index, depth, serializeFunctions, ); } else if (value instanceof BSONRegExp) { index = serializeBSONRegExp(buffer, key, value, index); } else if (value instanceof Int32) { index = serializeInt32(buffer, key, value, index); } else if (value instanceof MinKey || value instanceof MaxKey) { index = serializeMinMax(buffer, key, value, index); } else { throw new BSONTypeError(`Unrecognized or invalid BSON TYPE: ${value}`); } } } else { if (object.toBSON) { if (typeof object.toBSON !== "function") { throw new BSONTypeError("toBSON is not a function"); } object = object.toBSON(); if (object != null && typeof object !== "object") { throw new BSONTypeError("toBSON function did not return an object"); } }
for (const key in object) { let value = object[key]; if (value?.toBSON) { if (typeof value.toBSON !== "function") { throw new BSONTypeError("toBSON is not a function"); } value = value.toBSON(); }
const type = typeof value;
if (typeof key === "string" && !ignoreKeys.has(key)) { if (key.match(regexp) != null) { throw Error(`key ${key} must not contain null bytes`); }
if (checkKeys) { if (key.startsWith("$")) { throw Error(`key ${key} must not start with '$'`); } else if (~key.indexOf(".")) { throw Error(`key ${key} must not contain '.'`); } } }
if (type === "string") { index = serializeString(buffer, key, value, index); } else if (type === "number") { index = serializeNumber(buffer, key, value, index); } else if (type === "bigint") { throw new BSONTypeError( "Unsupported type BigInt, please use Decimal128", ); } else if (type === "boolean") { index = serializeBoolean(buffer, key, value, index); } else if (value instanceof Date) { index = serializeDate(buffer, key, value, index); } else if (value === undefined) { if (ignoreUndefined === false) { index = serializeNull(buffer, key, value, index); } } else if (value === null) { index = serializeNull(buffer, key, value, index); } else if (value instanceof ObjectId) { index = serializeObjectId(buffer, key, value, index); } else if (value instanceof Uint8Array) { index = serializeBuffer(buffer, key, value, index); } else if (value instanceof RegExp) { index = serializeRegExp(buffer, key, value, index); } else if (type === "object" && value instanceof Decimal128) { index = serializeDecimal128(buffer, key, value, index); } else if (value instanceof Long || value instanceof Timestamp) { index = serializeLong(buffer, key, value, index); } else if (value instanceof Double) { index = serializeDouble(buffer, key, value, index); } else if (value instanceof Code) { index = serializeCode( buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, ); } else if (typeof value === "function" && serializeFunctions) { index = serializeFunction( buffer, key, value, index, checkKeys, depth, serializeFunctions, ); } else if (value instanceof Binary) { index = serializeBinary(buffer, key, value, index); } else if (value instanceof BSONSymbol) { index = serializeSymbol(buffer, key, value, index); } else if (value instanceof DBRef) { index = serializeDBRef( buffer, key, value, index, depth, serializeFunctions, ); } else if (value instanceof BSONRegExp) { index = serializeBSONRegExp(buffer, key, value, index); } else if (value instanceof Int32) { index = serializeInt32(buffer, key, value, index); } else if (value instanceof MinKey || value instanceof MaxKey) { index = serializeMinMax(buffer, key, value, index); } else if (value instanceof Object) { index = serializeObject( buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, false, path, ); } else { throw new BSONTypeError(`Unrecognized or invalid BSON Type: ${value}`); } } }
path.pop();
if (buffer.length < index) { throw new Error("Document exceeds max BSON size"); }
buffer[index++] = 0x00;
const size = index - startingIndex; buffer[startingIndex++] = size & 0xff; buffer[startingIndex++] = (size >> 8) & 0xff; buffer[startingIndex++] = (size >> 16) & 0xff; buffer[startingIndex++] = (size >> 24) & 0xff; return index;}