123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987 |
- 'use strict';
- const Buffer = require('buffer').Buffer;
- const writeIEEE754 = require('../float_parser').writeIEEE754;
- const Long = require('../long');
- const Map = require('../map');
- const MinKey = require('../min_key');
- const Binary = require('../binary');
- const constants = require('../constants');
- const normalizedFunctionString = require('./utils').normalizedFunctionString;
- const regexp = /\x00/; // eslint-disable-line no-control-regex
- const ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']);
- // To ensure that 0.4 of node works correctly
- const isDate = function isDate(d) {
- return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
- };
- const isRegExp = function isRegExp(d) {
- return Object.prototype.toString.call(d) === '[object RegExp]';
- };
- function serializeString(buffer, key, value, index, isArray) {
- // Encode String type
- buffer[index++] = constants.BSON_DATA_STRING;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes + 1;
- buffer[index - 1] = 0;
- // Write the string
- const size = buffer.write(value, index + 4, 'utf8');
- // Write the size of the string to buffer
- buffer[index + 3] = ((size + 1) >> 24) & 0xff;
- buffer[index + 2] = ((size + 1) >> 16) & 0xff;
- buffer[index + 1] = ((size + 1) >> 8) & 0xff;
- buffer[index] = (size + 1) & 0xff;
- // Update index
- index = index + 4 + size;
- // Write zero
- buffer[index++] = 0;
- return index;
- }
- function serializeNumber(buffer, key, value, index, isArray) {
- // We have an integer value
- if (
- Math.floor(value) === value &&
- value >= constants.JS_INT_MIN &&
- value <= constants.JS_INT_MAX
- ) {
- // If the value fits in 32 bits encode as int, if it fits in a double
- // encode it as a double, otherwise long
- if (value >= constants.BSON_INT32_MIN && value <= constants.BSON_INT32_MAX) {
- // Set int type 32 bits or less
- buffer[index++] = constants.BSON_DATA_INT;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write the int value
- buffer[index++] = value & 0xff;
- buffer[index++] = (value >> 8) & 0xff;
- buffer[index++] = (value >> 16) & 0xff;
- buffer[index++] = (value >> 24) & 0xff;
- } else if (value >= constants.JS_INT_MIN && value <= constants.JS_INT_MAX) {
- // Encode as double
- buffer[index++] = constants.BSON_DATA_NUMBER;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write float
- writeIEEE754(buffer, value, index, 'little', 52, 8);
- // Ajust index
- index = index + 8;
- } else {
- // Set long type
- buffer[index++] = constants.BSON_DATA_LONG;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- const longVal = Long.fromNumber(value);
- const lowBits = longVal.getLowBits();
- const highBits = longVal.getHighBits();
- // Encode low bits
- buffer[index++] = lowBits & 0xff;
- buffer[index++] = (lowBits >> 8) & 0xff;
- buffer[index++] = (lowBits >> 16) & 0xff;
- buffer[index++] = (lowBits >> 24) & 0xff;
- // Encode high bits
- buffer[index++] = highBits & 0xff;
- buffer[index++] = (highBits >> 8) & 0xff;
- buffer[index++] = (highBits >> 16) & 0xff;
- buffer[index++] = (highBits >> 24) & 0xff;
- }
- } else {
- // Encode as double
- buffer[index++] = constants.BSON_DATA_NUMBER;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write float
- writeIEEE754(buffer, value, index, 'little', 52, 8);
- // Ajust index
- index = index + 8;
- }
- return index;
- }
- function serializeNull(buffer, key, value, index, isArray) {
- // Set long type
- buffer[index++] = constants.BSON_DATA_NULL;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- return index;
- }
- function serializeBoolean(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_BOOLEAN;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Encode the boolean value
- buffer[index++] = value ? 1 : 0;
- return index;
- }
- function serializeDate(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_DATE;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write the date
- const dateInMilis = Long.fromNumber(value.getTime());
- const lowBits = dateInMilis.getLowBits();
- const highBits = dateInMilis.getHighBits();
- // Encode low bits
- buffer[index++] = lowBits & 0xff;
- buffer[index++] = (lowBits >> 8) & 0xff;
- buffer[index++] = (lowBits >> 16) & 0xff;
- buffer[index++] = (lowBits >> 24) & 0xff;
- // Encode high bits
- buffer[index++] = highBits & 0xff;
- buffer[index++] = (highBits >> 8) & 0xff;
- buffer[index++] = (highBits >> 16) & 0xff;
- buffer[index++] = (highBits >> 24) & 0xff;
- return index;
- }
- function serializeRegExp(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_REGEXP;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- if (value.source && value.source.match(regexp) != null) {
- throw Error('value ' + value.source + ' must not contain null bytes');
- }
- // Adjust the index
- index = index + buffer.write(value.source, index, 'utf8');
- // Write zero
- buffer[index++] = 0x00;
- // Write the parameters
- if (value.ignoreCase) buffer[index++] = 0x69; // i
- if (value.global) buffer[index++] = 0x73; // s
- if (value.multiline) buffer[index++] = 0x6d; // m
- // Add ending zero
- buffer[index++] = 0x00;
- return index;
- }
- function serializeBSONRegExp(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_REGEXP;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Check the pattern for 0 bytes
- if (value.pattern.match(regexp) != null) {
- // The BSON spec doesn't allow keys with null bytes because keys are
- // null-terminated.
- throw Error('pattern ' + value.pattern + ' must not contain null bytes');
- }
- // Adjust the index
- index = index + buffer.write(value.pattern, index, 'utf8');
- // Write zero
- buffer[index++] = 0x00;
- // Write the options
- index =
- index +
- buffer.write(
- value.options
- .split('')
- .sort()
- .join(''),
- index,
- 'utf8'
- );
- // Add ending zero
- buffer[index++] = 0x00;
- return index;
- }
- function serializeMinMax(buffer, key, value, index, isArray) {
- // Write the type of either min or max key
- if (value === null) {
- buffer[index++] = constants.BSON_DATA_NULL;
- } else if (value instanceof MinKey) {
- buffer[index++] = constants.BSON_DATA_MIN_KEY;
- } else {
- buffer[index++] = constants.BSON_DATA_MAX_KEY;
- }
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- return index;
- }
- function serializeObjectId(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_OID;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write the objectId into the shared buffer
- if (typeof value.id === 'string') {
- buffer.write(value.id, index, 'binary');
- } else if (value.id && value.id.copy) {
- value.id.copy(buffer, index, 0, 12);
- } else {
- throw new TypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
- }
- // Ajust index
- return index + 12;
- }
- function serializeBuffer(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_BINARY;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Get size of the buffer (current write point)
- const size = value.length;
- // Write the size of the string to buffer
- buffer[index++] = size & 0xff;
- buffer[index++] = (size >> 8) & 0xff;
- buffer[index++] = (size >> 16) & 0xff;
- buffer[index++] = (size >> 24) & 0xff;
- // Write the default subtype
- buffer[index++] = constants.BSON_BINARY_SUBTYPE_DEFAULT;
- // Copy the content form the binary field to the buffer
- value.copy(buffer, index, 0, size);
- // Adjust the index
- index = index + size;
- return index;
- }
- function serializeObject(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- ignoreUndefined,
- isArray,
- path
- ) {
- for (let i = 0; i < path.length; i++) {
- if (path[i] === value) throw new Error('cyclic dependency detected');
- }
- // Push value to stack
- path.push(value);
- // Write the type
- buffer[index++] = Array.isArray(value) ? constants.BSON_DATA_ARRAY : constants.BSON_DATA_OBJECT;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- const endIndex = serializeInto(
- buffer,
- value,
- checkKeys,
- index,
- depth + 1,
- serializeFunctions,
- ignoreUndefined,
- path
- );
- // Pop stack
- path.pop();
- return endIndex;
- }
- function serializeDecimal128(buffer, key, value, index, isArray) {
- buffer[index++] = constants.BSON_DATA_DECIMAL128;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write the data from the value
- value.bytes.copy(buffer, index, 0, 16);
- return index + 16;
- }
- function serializeLong(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] =
- value._bsontype === 'Long' ? constants.BSON_DATA_LONG : constants.BSON_DATA_TIMESTAMP;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write the date
- const lowBits = value.getLowBits();
- const highBits = value.getHighBits();
- // Encode low bits
- buffer[index++] = lowBits & 0xff;
- buffer[index++] = (lowBits >> 8) & 0xff;
- buffer[index++] = (lowBits >> 16) & 0xff;
- buffer[index++] = (lowBits >> 24) & 0xff;
- // Encode high bits
- buffer[index++] = highBits & 0xff;
- buffer[index++] = (highBits >> 8) & 0xff;
- buffer[index++] = (highBits >> 16) & 0xff;
- buffer[index++] = (highBits >> 24) & 0xff;
- return index;
- }
- function serializeInt32(buffer, key, value, index, isArray) {
- // Set int type 32 bits or less
- buffer[index++] = constants.BSON_DATA_INT;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write the int value
- buffer[index++] = value & 0xff;
- buffer[index++] = (value >> 8) & 0xff;
- buffer[index++] = (value >> 16) & 0xff;
- buffer[index++] = (value >> 24) & 0xff;
- return index;
- }
- function serializeDouble(buffer, key, value, index, isArray) {
- // Encode as double
- buffer[index++] = constants.BSON_DATA_NUMBER;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write float
- writeIEEE754(buffer, value.value, index, 'little', 52, 8);
- // Adjust index
- index = index + 8;
- return index;
- }
- function serializeFunction(buffer, key, value, index, checkKeys, depth, isArray) {
- buffer[index++] = constants.BSON_DATA_CODE;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Function string
- const functionString = normalizedFunctionString(value);
- // Write the string
- const size = buffer.write(functionString, index + 4, 'utf8') + 1;
- // Write the size of the string to buffer
- buffer[index] = size & 0xff;
- buffer[index + 1] = (size >> 8) & 0xff;
- buffer[index + 2] = (size >> 16) & 0xff;
- buffer[index + 3] = (size >> 24) & 0xff;
- // Update index
- index = index + 4 + size - 1;
- // Write zero
- buffer[index++] = 0;
- return index;
- }
- function serializeCode(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- ignoreUndefined,
- isArray
- ) {
- if (value.scope && typeof value.scope === 'object') {
- // Write the type
- buffer[index++] = constants.BSON_DATA_CODE_W_SCOPE;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Starting index
- let startIndex = index;
- // Serialize the function
- // Get the function string
- const functionString = typeof value.code === 'string' ? value.code : value.code.toString();
- // Index adjustment
- index = index + 4;
- // Write string into buffer
- const codeSize = buffer.write(functionString, index + 4, 'utf8') + 1;
- // Write the size of the string to buffer
- buffer[index] = codeSize & 0xff;
- buffer[index + 1] = (codeSize >> 8) & 0xff;
- buffer[index + 2] = (codeSize >> 16) & 0xff;
- buffer[index + 3] = (codeSize >> 24) & 0xff;
- // Write end 0
- buffer[index + 4 + codeSize - 1] = 0;
- // Write the
- index = index + codeSize + 4;
- //
- // Serialize the scope value
- const endIndex = serializeInto(
- buffer,
- value.scope,
- checkKeys,
- index,
- depth + 1,
- serializeFunctions,
- ignoreUndefined
- );
- index = endIndex - 1;
- // Writ the total
- const totalSize = endIndex - startIndex;
- // Write the total size of the object
- buffer[startIndex++] = totalSize & 0xff;
- buffer[startIndex++] = (totalSize >> 8) & 0xff;
- buffer[startIndex++] = (totalSize >> 16) & 0xff;
- buffer[startIndex++] = (totalSize >> 24) & 0xff;
- // Write trailing zero
- buffer[index++] = 0;
- } else {
- buffer[index++] = constants.BSON_DATA_CODE;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Function string
- const functionString = value.code.toString();
- // Write the string
- const size = buffer.write(functionString, index + 4, 'utf8') + 1;
- // Write the size of the string to buffer
- buffer[index] = size & 0xff;
- buffer[index + 1] = (size >> 8) & 0xff;
- buffer[index + 2] = (size >> 16) & 0xff;
- buffer[index + 3] = (size >> 24) & 0xff;
- // Update index
- index = index + 4 + size - 1;
- // Write zero
- buffer[index++] = 0;
- }
- return index;
- }
- function serializeBinary(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_BINARY;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Extract the buffer
- const data = value.value(true);
- // Calculate size
- let size = value.position;
- // Add the deprecated 02 type 4 bytes of size to total
- if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) size = size + 4;
- // Write the size of the string to buffer
- buffer[index++] = size & 0xff;
- buffer[index++] = (size >> 8) & 0xff;
- buffer[index++] = (size >> 16) & 0xff;
- buffer[index++] = (size >> 24) & 0xff;
- // Write the subtype to the buffer
- buffer[index++] = value.sub_type;
- // If we have binary type 2 the 4 first bytes are the size
- if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
- size = size - 4;
- buffer[index++] = size & 0xff;
- buffer[index++] = (size >> 8) & 0xff;
- buffer[index++] = (size >> 16) & 0xff;
- buffer[index++] = (size >> 24) & 0xff;
- }
- // Write the data to the object
- data.copy(buffer, index, 0, value.position);
- // Adjust the index
- index = index + value.position;
- return index;
- }
- function serializeSymbol(buffer, key, value, index, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_SYMBOL;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- // Write the string
- const size = buffer.write(value.value, index + 4, 'utf8') + 1;
- // Write the size of the string to buffer
- buffer[index] = size & 0xff;
- buffer[index + 1] = (size >> 8) & 0xff;
- buffer[index + 2] = (size >> 16) & 0xff;
- buffer[index + 3] = (size >> 24) & 0xff;
- // Update index
- index = index + 4 + size - 1;
- // Write zero
- buffer[index++] = 0x00;
- return index;
- }
- function serializeDBRef(buffer, key, value, index, depth, serializeFunctions, isArray) {
- // Write the type
- buffer[index++] = constants.BSON_DATA_OBJECT;
- // Number of written bytes
- const numberOfWrittenBytes = !isArray
- ? buffer.write(key, index, 'utf8')
- : buffer.write(key, index, 'ascii');
- // Encode the name
- index = index + numberOfWrittenBytes;
- buffer[index++] = 0;
- let startIndex = index;
- let endIndex;
- let output = {
- $ref: value.collection,
- $id: value.oid
- };
- if (value.db != null) output.$db = value.db;
- output = Object.assign(output, value.fields);
- endIndex = serializeInto(buffer, output, false, index, depth + 1, serializeFunctions);
- // Calculate object size
- const size = endIndex - startIndex;
- // Write the size
- buffer[startIndex++] = size & 0xff;
- buffer[startIndex++] = (size >> 8) & 0xff;
- buffer[startIndex++] = (size >> 16) & 0xff;
- buffer[startIndex++] = (size >> 24) & 0xff;
- // Set index
- return endIndex;
- }
- function serializeInto(
- buffer,
- object,
- checkKeys,
- startingIndex,
- depth,
- serializeFunctions,
- ignoreUndefined,
- path
- ) {
- startingIndex = startingIndex || 0;
- path = path || [];
- // Push the object to the path
- path.push(object);
- // Start place to serialize into
- let index = startingIndex + 4;
- // Special case isArray
- if (Array.isArray(object)) {
- // Get object keys
- for (let i = 0; i < object.length; i++) {
- let key = '' + i;
- let value = object[i];
- // Is there an override value
- if (value && value.toBSON) {
- if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
- value = value.toBSON();
- }
- const type = typeof value;
- if (type === 'string') {
- index = serializeString(buffer, key, value, index, true);
- } else if (type === 'number') {
- index = serializeNumber(buffer, key, value, index, true);
- } else if (type === 'boolean') {
- index = serializeBoolean(buffer, key, value, index, true);
- } else if (value instanceof Date || isDate(value)) {
- index = serializeDate(buffer, key, value, index, true);
- } else if (value === undefined) {
- index = serializeNull(buffer, key, value, index, true);
- } else if (value === null) {
- index = serializeNull(buffer, key, value, index, true);
- } else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
- index = serializeObjectId(buffer, key, value, index, true);
- } else if (Buffer.isBuffer(value)) {
- index = serializeBuffer(buffer, key, value, index, true);
- } else if (value instanceof RegExp || isRegExp(value)) {
- index = serializeRegExp(buffer, key, value, index, true);
- } else if (type === 'object' && value['_bsontype'] == null) {
- index = serializeObject(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- ignoreUndefined,
- true,
- path
- );
- } else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
- index = serializeDecimal128(buffer, key, value, index, true);
- } else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
- index = serializeLong(buffer, key, value, index, true);
- } else if (value['_bsontype'] === 'Double') {
- index = serializeDouble(buffer, key, value, index, true);
- } else if (typeof value === 'function' && serializeFunctions) {
- index = serializeFunction(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- true
- );
- } else if (value['_bsontype'] === 'Code') {
- index = serializeCode(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- ignoreUndefined,
- true
- );
- } else if (value['_bsontype'] === 'Binary') {
- index = serializeBinary(buffer, key, value, index, true);
- } else if (value['_bsontype'] === 'Symbol') {
- index = serializeSymbol(buffer, key, value, index, true);
- } else if (value['_bsontype'] === 'DBRef') {
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, true);
- } else if (value['_bsontype'] === 'BSONRegExp') {
- index = serializeBSONRegExp(buffer, key, value, index, true);
- } else if (value['_bsontype'] === 'Int32') {
- index = serializeInt32(buffer, key, value, index, true);
- } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
- index = serializeMinMax(buffer, key, value, index, true);
- }
- }
- } else if (object instanceof Map) {
- const iterator = object.entries();
- let done = false;
- while (!done) {
- // Unpack the next entry
- const entry = iterator.next();
- done = entry.done;
- // Are we done, then skip and terminate
- if (done) continue;
- // Get the entry values
- const key = entry.value[0];
- const value = entry.value[1];
- // Check the type of the value
- const type = typeof value;
- // Check the key and throw error if it's illegal
- if (typeof key === 'string' && !ignoreKeys.has(key)) {
- if (key.match(regexp) != null) {
- // The BSON spec doesn't allow keys with null bytes because keys are
- // null-terminated.
- throw Error('key ' + key + ' must not contain null bytes');
- }
- if (checkKeys) {
- if ('$' === key[0]) {
- throw Error('key ' + key + " must not start with '$'");
- } else if (~key.indexOf('.')) {
- throw Error('key ' + key + " must not contain '.'");
- }
- }
- }
- if (type === 'string') {
- index = serializeString(buffer, key, value, index);
- } else if (type === 'number') {
- index = serializeNumber(buffer, key, value, index);
- } else if (type === 'boolean') {
- index = serializeBoolean(buffer, key, value, index);
- } else if (value instanceof Date || isDate(value)) {
- index = serializeDate(buffer, key, value, index);
- } else if (value === null || (value === undefined && ignoreUndefined === false)) {
- index = serializeNull(buffer, key, value, index);
- } else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
- index = serializeObjectId(buffer, key, value, index);
- } else if (Buffer.isBuffer(value)) {
- index = serializeBuffer(buffer, key, value, index);
- } else if (value instanceof RegExp || isRegExp(value)) {
- index = serializeRegExp(buffer, key, value, index);
- } else if (type === 'object' && value['_bsontype'] == null) {
- index = serializeObject(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- ignoreUndefined,
- false,
- path
- );
- } else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
- index = serializeDecimal128(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
- index = serializeLong(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Double') {
- index = serializeDouble(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Code') {
- index = serializeCode(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- ignoreUndefined
- );
- } else if (typeof value === 'function' && serializeFunctions) {
- index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
- } else if (value['_bsontype'] === 'Binary') {
- index = serializeBinary(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Symbol') {
- index = serializeSymbol(buffer, key, value, index);
- } else if (value['_bsontype'] === 'DBRef') {
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
- } else if (value['_bsontype'] === 'BSONRegExp') {
- index = serializeBSONRegExp(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Int32') {
- index = serializeInt32(buffer, key, value, index);
- } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
- index = serializeMinMax(buffer, key, value, index);
- }
- }
- } else {
- // Did we provide a custom serialization method
- if (object.toBSON) {
- if (typeof object.toBSON !== 'function') throw new TypeError('toBSON is not a function');
- object = object.toBSON();
- if (object != null && typeof object !== 'object')
- throw new TypeError('toBSON function did not return an object');
- }
- // Iterate over all the keys
- for (let key in object) {
- let value = object[key];
- // Is there an override value
- if (value && value.toBSON) {
- if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
- value = value.toBSON();
- }
- // Check the type of the value
- const type = typeof value;
- // Check the key and throw error if it's illegal
- if (typeof key === 'string' && !ignoreKeys.has(key)) {
- if (key.match(regexp) != null) {
- // The BSON spec doesn't allow keys with null bytes because keys are
- // null-terminated.
- throw Error('key ' + key + ' must not contain null bytes');
- }
- if (checkKeys) {
- if ('$' === key[0]) {
- throw Error('key ' + key + " must not start with '$'");
- } else if (~key.indexOf('.')) {
- throw Error('key ' + key + " must not contain '.'");
- }
- }
- }
- if (type === 'string') {
- index = serializeString(buffer, key, value, index);
- } else if (type === 'number') {
- index = serializeNumber(buffer, key, value, index);
- } else if (type === 'boolean') {
- index = serializeBoolean(buffer, key, value, index);
- } else if (value instanceof Date || isDate(value)) {
- index = serializeDate(buffer, key, value, index);
- } else if (value === undefined) {
- if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
- } else if (value === null) {
- index = serializeNull(buffer, key, value, index);
- } else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
- index = serializeObjectId(buffer, key, value, index);
- } else if (Buffer.isBuffer(value)) {
- index = serializeBuffer(buffer, key, value, index);
- } else if (value instanceof RegExp || isRegExp(value)) {
- index = serializeRegExp(buffer, key, value, index);
- } else if (type === 'object' && value['_bsontype'] == null) {
- index = serializeObject(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- ignoreUndefined,
- false,
- path
- );
- } else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
- index = serializeDecimal128(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
- index = serializeLong(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Double') {
- index = serializeDouble(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Code') {
- index = serializeCode(
- buffer,
- key,
- value,
- index,
- checkKeys,
- depth,
- serializeFunctions,
- ignoreUndefined
- );
- } else if (typeof value === 'function' && serializeFunctions) {
- index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
- } else if (value['_bsontype'] === 'Binary') {
- index = serializeBinary(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Symbol') {
- index = serializeSymbol(buffer, key, value, index);
- } else if (value['_bsontype'] === 'DBRef') {
- index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
- } else if (value['_bsontype'] === 'BSONRegExp') {
- index = serializeBSONRegExp(buffer, key, value, index);
- } else if (value['_bsontype'] === 'Int32') {
- index = serializeInt32(buffer, key, value, index);
- } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
- index = serializeMinMax(buffer, key, value, index);
- }
- }
- }
- // Remove the path
- path.pop();
- // Final padding byte for object
- buffer[index++] = 0x00;
- // Final size
- const size = index - startingIndex;
- // Write the size of the object
- buffer[startingIndex++] = size & 0xff;
- buffer[startingIndex++] = (size >> 8) & 0xff;
- buffer[startingIndex++] = (size >> 16) & 0xff;
- buffer[startingIndex++] = (size >> 24) & 0xff;
- return index;
- }
- module.exports = serializeInto;
|