serializer.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.serializeInto = void 0;
  4. var binary_1 = require("../binary");
  5. var constants = require("../constants");
  6. var ensure_buffer_1 = require("../ensure_buffer");
  7. var extended_json_1 = require("../extended_json");
  8. var float_parser_1 = require("../float_parser");
  9. var long_1 = require("../long");
  10. var map_1 = require("../map");
  11. var utils_1 = require("./utils");
  12. var regexp = /\x00/; // eslint-disable-line no-control-regex
  13. var ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']);
  14. /*
  15. * isArray indicates if we are writing to a BSON array (type 0x04)
  16. * which forces the "key" which really an array index as a string to be written as ascii
  17. * This will catch any errors in index as a string generation
  18. */
  19. function serializeString(buffer, key, value, index, isArray) {
  20. // Encode String type
  21. buffer[index++] = constants.BSON_DATA_STRING;
  22. // Number of written bytes
  23. var numberOfWrittenBytes = !isArray
  24. ? buffer.write(key, index, undefined, 'utf8')
  25. : buffer.write(key, index, undefined, 'ascii');
  26. // Encode the name
  27. index = index + numberOfWrittenBytes + 1;
  28. buffer[index - 1] = 0;
  29. // Write the string
  30. var size = buffer.write(value, index + 4, undefined, 'utf8');
  31. // Write the size of the string to buffer
  32. buffer[index + 3] = ((size + 1) >> 24) & 0xff;
  33. buffer[index + 2] = ((size + 1) >> 16) & 0xff;
  34. buffer[index + 1] = ((size + 1) >> 8) & 0xff;
  35. buffer[index] = (size + 1) & 0xff;
  36. // Update index
  37. index = index + 4 + size;
  38. // Write zero
  39. buffer[index++] = 0;
  40. return index;
  41. }
  42. function serializeNumber(buffer, key, value, index, isArray) {
  43. // We have an integer value
  44. // TODO(NODE-2529): Add support for big int
  45. if (Number.isInteger(value) &&
  46. value >= constants.BSON_INT32_MIN &&
  47. value <= constants.BSON_INT32_MAX) {
  48. // If the value fits in 32 bits encode as int32
  49. // Set int type 32 bits or less
  50. buffer[index++] = constants.BSON_DATA_INT;
  51. // Number of written bytes
  52. var numberOfWrittenBytes = !isArray
  53. ? buffer.write(key, index, undefined, 'utf8')
  54. : buffer.write(key, index, undefined, 'ascii');
  55. // Encode the name
  56. index = index + numberOfWrittenBytes;
  57. buffer[index++] = 0;
  58. // Write the int value
  59. buffer[index++] = value & 0xff;
  60. buffer[index++] = (value >> 8) & 0xff;
  61. buffer[index++] = (value >> 16) & 0xff;
  62. buffer[index++] = (value >> 24) & 0xff;
  63. }
  64. else {
  65. // Encode as double
  66. buffer[index++] = constants.BSON_DATA_NUMBER;
  67. // Number of written bytes
  68. var numberOfWrittenBytes = !isArray
  69. ? buffer.write(key, index, undefined, 'utf8')
  70. : buffer.write(key, index, undefined, 'ascii');
  71. // Encode the name
  72. index = index + numberOfWrittenBytes;
  73. buffer[index++] = 0;
  74. // Write float
  75. float_parser_1.writeIEEE754(buffer, value, index, 'little', 52, 8);
  76. // Adjust index
  77. index = index + 8;
  78. }
  79. return index;
  80. }
  81. function serializeNull(buffer, key, _, index, isArray) {
  82. // Set long type
  83. buffer[index++] = constants.BSON_DATA_NULL;
  84. // Number of written bytes
  85. var numberOfWrittenBytes = !isArray
  86. ? buffer.write(key, index, undefined, 'utf8')
  87. : buffer.write(key, index, undefined, 'ascii');
  88. // Encode the name
  89. index = index + numberOfWrittenBytes;
  90. buffer[index++] = 0;
  91. return index;
  92. }
  93. function serializeBoolean(buffer, key, value, index, isArray) {
  94. // Write the type
  95. buffer[index++] = constants.BSON_DATA_BOOLEAN;
  96. // Number of written bytes
  97. var numberOfWrittenBytes = !isArray
  98. ? buffer.write(key, index, undefined, 'utf8')
  99. : buffer.write(key, index, undefined, 'ascii');
  100. // Encode the name
  101. index = index + numberOfWrittenBytes;
  102. buffer[index++] = 0;
  103. // Encode the boolean value
  104. buffer[index++] = value ? 1 : 0;
  105. return index;
  106. }
  107. function serializeDate(buffer, key, value, index, isArray) {
  108. // Write the type
  109. buffer[index++] = constants.BSON_DATA_DATE;
  110. // Number of written bytes
  111. var numberOfWrittenBytes = !isArray
  112. ? buffer.write(key, index, undefined, 'utf8')
  113. : buffer.write(key, index, undefined, 'ascii');
  114. // Encode the name
  115. index = index + numberOfWrittenBytes;
  116. buffer[index++] = 0;
  117. // Write the date
  118. var dateInMilis = long_1.Long.fromNumber(value.getTime());
  119. var lowBits = dateInMilis.getLowBits();
  120. var highBits = dateInMilis.getHighBits();
  121. // Encode low bits
  122. buffer[index++] = lowBits & 0xff;
  123. buffer[index++] = (lowBits >> 8) & 0xff;
  124. buffer[index++] = (lowBits >> 16) & 0xff;
  125. buffer[index++] = (lowBits >> 24) & 0xff;
  126. // Encode high bits
  127. buffer[index++] = highBits & 0xff;
  128. buffer[index++] = (highBits >> 8) & 0xff;
  129. buffer[index++] = (highBits >> 16) & 0xff;
  130. buffer[index++] = (highBits >> 24) & 0xff;
  131. return index;
  132. }
  133. function serializeRegExp(buffer, key, value, index, isArray) {
  134. // Write the type
  135. buffer[index++] = constants.BSON_DATA_REGEXP;
  136. // Number of written bytes
  137. var numberOfWrittenBytes = !isArray
  138. ? buffer.write(key, index, undefined, 'utf8')
  139. : buffer.write(key, index, undefined, 'ascii');
  140. // Encode the name
  141. index = index + numberOfWrittenBytes;
  142. buffer[index++] = 0;
  143. if (value.source && value.source.match(regexp) != null) {
  144. throw Error('value ' + value.source + ' must not contain null bytes');
  145. }
  146. // Adjust the index
  147. index = index + buffer.write(value.source, index, undefined, 'utf8');
  148. // Write zero
  149. buffer[index++] = 0x00;
  150. // Write the parameters
  151. if (value.ignoreCase)
  152. buffer[index++] = 0x69; // i
  153. if (value.global)
  154. buffer[index++] = 0x73; // s
  155. if (value.multiline)
  156. buffer[index++] = 0x6d; // m
  157. // Add ending zero
  158. buffer[index++] = 0x00;
  159. return index;
  160. }
  161. function serializeBSONRegExp(buffer, key, value, index, isArray) {
  162. // Write the type
  163. buffer[index++] = constants.BSON_DATA_REGEXP;
  164. // Number of written bytes
  165. var numberOfWrittenBytes = !isArray
  166. ? buffer.write(key, index, undefined, 'utf8')
  167. : buffer.write(key, index, undefined, 'ascii');
  168. // Encode the name
  169. index = index + numberOfWrittenBytes;
  170. buffer[index++] = 0;
  171. // Check the pattern for 0 bytes
  172. if (value.pattern.match(regexp) != null) {
  173. // The BSON spec doesn't allow keys with null bytes because keys are
  174. // null-terminated.
  175. throw Error('pattern ' + value.pattern + ' must not contain null bytes');
  176. }
  177. // Adjust the index
  178. index = index + buffer.write(value.pattern, index, undefined, 'utf8');
  179. // Write zero
  180. buffer[index++] = 0x00;
  181. // Write the options
  182. index = index + buffer.write(value.options.split('').sort().join(''), index, undefined, 'utf8');
  183. // Add ending zero
  184. buffer[index++] = 0x00;
  185. return index;
  186. }
  187. function serializeMinMax(buffer, key, value, index, isArray) {
  188. // Write the type of either min or max key
  189. if (value === null) {
  190. buffer[index++] = constants.BSON_DATA_NULL;
  191. }
  192. else if (value._bsontype === 'MinKey') {
  193. buffer[index++] = constants.BSON_DATA_MIN_KEY;
  194. }
  195. else {
  196. buffer[index++] = constants.BSON_DATA_MAX_KEY;
  197. }
  198. // Number of written bytes
  199. var numberOfWrittenBytes = !isArray
  200. ? buffer.write(key, index, undefined, 'utf8')
  201. : buffer.write(key, index, undefined, 'ascii');
  202. // Encode the name
  203. index = index + numberOfWrittenBytes;
  204. buffer[index++] = 0;
  205. return index;
  206. }
  207. function serializeObjectId(buffer, key, value, index, isArray) {
  208. // Write the type
  209. buffer[index++] = constants.BSON_DATA_OID;
  210. // Number of written bytes
  211. var numberOfWrittenBytes = !isArray
  212. ? buffer.write(key, index, undefined, 'utf8')
  213. : buffer.write(key, index, undefined, 'ascii');
  214. // Encode the name
  215. index = index + numberOfWrittenBytes;
  216. buffer[index++] = 0;
  217. // Write the objectId into the shared buffer
  218. if (typeof value.id === 'string') {
  219. buffer.write(value.id, index, undefined, 'binary');
  220. }
  221. else if (utils_1.isUint8Array(value.id)) {
  222. // Use the standard JS methods here because buffer.copy() is buggy with the
  223. // browser polyfill
  224. buffer.set(value.id.subarray(0, 12), index);
  225. }
  226. else {
  227. throw new TypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
  228. }
  229. // Adjust index
  230. return index + 12;
  231. }
  232. function serializeBuffer(buffer, key, value, index, isArray) {
  233. // Write the type
  234. buffer[index++] = constants.BSON_DATA_BINARY;
  235. // Number of written bytes
  236. var numberOfWrittenBytes = !isArray
  237. ? buffer.write(key, index, undefined, 'utf8')
  238. : buffer.write(key, index, undefined, 'ascii');
  239. // Encode the name
  240. index = index + numberOfWrittenBytes;
  241. buffer[index++] = 0;
  242. // Get size of the buffer (current write point)
  243. var size = value.length;
  244. // Write the size of the string to buffer
  245. buffer[index++] = size & 0xff;
  246. buffer[index++] = (size >> 8) & 0xff;
  247. buffer[index++] = (size >> 16) & 0xff;
  248. buffer[index++] = (size >> 24) & 0xff;
  249. // Write the default subtype
  250. buffer[index++] = constants.BSON_BINARY_SUBTYPE_DEFAULT;
  251. // Copy the content form the binary field to the buffer
  252. buffer.set(ensure_buffer_1.ensureBuffer(value), index);
  253. // Adjust the index
  254. index = index + size;
  255. return index;
  256. }
  257. function serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, isArray, path) {
  258. if (checkKeys === void 0) { checkKeys = false; }
  259. if (depth === void 0) { depth = 0; }
  260. if (serializeFunctions === void 0) { serializeFunctions = false; }
  261. if (ignoreUndefined === void 0) { ignoreUndefined = true; }
  262. if (isArray === void 0) { isArray = false; }
  263. if (path === void 0) { path = []; }
  264. for (var i = 0; i < path.length; i++) {
  265. if (path[i] === value)
  266. throw new Error('cyclic dependency detected');
  267. }
  268. // Push value to stack
  269. path.push(value);
  270. // Write the type
  271. buffer[index++] = Array.isArray(value) ? constants.BSON_DATA_ARRAY : constants.BSON_DATA_OBJECT;
  272. // Number of written bytes
  273. var numberOfWrittenBytes = !isArray
  274. ? buffer.write(key, index, undefined, 'utf8')
  275. : buffer.write(key, index, undefined, 'ascii');
  276. // Encode the name
  277. index = index + numberOfWrittenBytes;
  278. buffer[index++] = 0;
  279. var endIndex = serializeInto(buffer, value, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined, path);
  280. // Pop stack
  281. path.pop();
  282. return endIndex;
  283. }
  284. function serializeDecimal128(buffer, key, value, index, isArray) {
  285. buffer[index++] = constants.BSON_DATA_DECIMAL128;
  286. // Number of written bytes
  287. var numberOfWrittenBytes = !isArray
  288. ? buffer.write(key, index, undefined, 'utf8')
  289. : buffer.write(key, index, undefined, 'ascii');
  290. // Encode the name
  291. index = index + numberOfWrittenBytes;
  292. buffer[index++] = 0;
  293. // Write the data from the value
  294. // Prefer the standard JS methods because their typechecking is not buggy,
  295. // unlike the `buffer` polyfill's.
  296. buffer.set(value.bytes.subarray(0, 16), index);
  297. return index + 16;
  298. }
  299. function serializeLong(buffer, key, value, index, isArray) {
  300. // Write the type
  301. buffer[index++] =
  302. value._bsontype === 'Long' ? constants.BSON_DATA_LONG : constants.BSON_DATA_TIMESTAMP;
  303. // Number of written bytes
  304. var numberOfWrittenBytes = !isArray
  305. ? buffer.write(key, index, undefined, 'utf8')
  306. : buffer.write(key, index, undefined, 'ascii');
  307. // Encode the name
  308. index = index + numberOfWrittenBytes;
  309. buffer[index++] = 0;
  310. // Write the date
  311. var lowBits = value.getLowBits();
  312. var highBits = value.getHighBits();
  313. // Encode low bits
  314. buffer[index++] = lowBits & 0xff;
  315. buffer[index++] = (lowBits >> 8) & 0xff;
  316. buffer[index++] = (lowBits >> 16) & 0xff;
  317. buffer[index++] = (lowBits >> 24) & 0xff;
  318. // Encode high bits
  319. buffer[index++] = highBits & 0xff;
  320. buffer[index++] = (highBits >> 8) & 0xff;
  321. buffer[index++] = (highBits >> 16) & 0xff;
  322. buffer[index++] = (highBits >> 24) & 0xff;
  323. return index;
  324. }
  325. function serializeInt32(buffer, key, value, index, isArray) {
  326. value = value.valueOf();
  327. // Set int type 32 bits or less
  328. buffer[index++] = constants.BSON_DATA_INT;
  329. // Number of written bytes
  330. var numberOfWrittenBytes = !isArray
  331. ? buffer.write(key, index, undefined, 'utf8')
  332. : buffer.write(key, index, undefined, 'ascii');
  333. // Encode the name
  334. index = index + numberOfWrittenBytes;
  335. buffer[index++] = 0;
  336. // Write the int value
  337. buffer[index++] = value & 0xff;
  338. buffer[index++] = (value >> 8) & 0xff;
  339. buffer[index++] = (value >> 16) & 0xff;
  340. buffer[index++] = (value >> 24) & 0xff;
  341. return index;
  342. }
  343. function serializeDouble(buffer, key, value, index, isArray) {
  344. // Encode as double
  345. buffer[index++] = constants.BSON_DATA_NUMBER;
  346. // Number of written bytes
  347. var numberOfWrittenBytes = !isArray
  348. ? buffer.write(key, index, undefined, 'utf8')
  349. : buffer.write(key, index, undefined, 'ascii');
  350. // Encode the name
  351. index = index + numberOfWrittenBytes;
  352. buffer[index++] = 0;
  353. // Write float
  354. float_parser_1.writeIEEE754(buffer, value.value, index, 'little', 52, 8);
  355. // Adjust index
  356. index = index + 8;
  357. return index;
  358. }
  359. function serializeFunction(buffer, key, value, index, _checkKeys, _depth, isArray) {
  360. if (_checkKeys === void 0) { _checkKeys = false; }
  361. if (_depth === void 0) { _depth = 0; }
  362. buffer[index++] = constants.BSON_DATA_CODE;
  363. // Number of written bytes
  364. var numberOfWrittenBytes = !isArray
  365. ? buffer.write(key, index, undefined, 'utf8')
  366. : buffer.write(key, index, undefined, 'ascii');
  367. // Encode the name
  368. index = index + numberOfWrittenBytes;
  369. buffer[index++] = 0;
  370. // Function string
  371. var functionString = utils_1.normalizedFunctionString(value);
  372. // Write the string
  373. var size = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
  374. // Write the size of the string to buffer
  375. buffer[index] = size & 0xff;
  376. buffer[index + 1] = (size >> 8) & 0xff;
  377. buffer[index + 2] = (size >> 16) & 0xff;
  378. buffer[index + 3] = (size >> 24) & 0xff;
  379. // Update index
  380. index = index + 4 + size - 1;
  381. // Write zero
  382. buffer[index++] = 0;
  383. return index;
  384. }
  385. function serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, isArray) {
  386. if (checkKeys === void 0) { checkKeys = false; }
  387. if (depth === void 0) { depth = 0; }
  388. if (serializeFunctions === void 0) { serializeFunctions = false; }
  389. if (ignoreUndefined === void 0) { ignoreUndefined = true; }
  390. if (isArray === void 0) { isArray = false; }
  391. if (value.scope && typeof value.scope === 'object') {
  392. // Write the type
  393. buffer[index++] = constants.BSON_DATA_CODE_W_SCOPE;
  394. // Number of written bytes
  395. var numberOfWrittenBytes = !isArray
  396. ? buffer.write(key, index, undefined, 'utf8')
  397. : buffer.write(key, index, undefined, 'ascii');
  398. // Encode the name
  399. index = index + numberOfWrittenBytes;
  400. buffer[index++] = 0;
  401. // Starting index
  402. var startIndex = index;
  403. // Serialize the function
  404. // Get the function string
  405. var functionString = typeof value.code === 'string' ? value.code : value.code.toString();
  406. // Index adjustment
  407. index = index + 4;
  408. // Write string into buffer
  409. var codeSize = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
  410. // Write the size of the string to buffer
  411. buffer[index] = codeSize & 0xff;
  412. buffer[index + 1] = (codeSize >> 8) & 0xff;
  413. buffer[index + 2] = (codeSize >> 16) & 0xff;
  414. buffer[index + 3] = (codeSize >> 24) & 0xff;
  415. // Write end 0
  416. buffer[index + 4 + codeSize - 1] = 0;
  417. // Write the
  418. index = index + codeSize + 4;
  419. //
  420. // Serialize the scope value
  421. var endIndex = serializeInto(buffer, value.scope, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined);
  422. index = endIndex - 1;
  423. // Writ the total
  424. var totalSize = endIndex - startIndex;
  425. // Write the total size of the object
  426. buffer[startIndex++] = totalSize & 0xff;
  427. buffer[startIndex++] = (totalSize >> 8) & 0xff;
  428. buffer[startIndex++] = (totalSize >> 16) & 0xff;
  429. buffer[startIndex++] = (totalSize >> 24) & 0xff;
  430. // Write trailing zero
  431. buffer[index++] = 0;
  432. }
  433. else {
  434. buffer[index++] = constants.BSON_DATA_CODE;
  435. // Number of written bytes
  436. var numberOfWrittenBytes = !isArray
  437. ? buffer.write(key, index, undefined, 'utf8')
  438. : buffer.write(key, index, undefined, 'ascii');
  439. // Encode the name
  440. index = index + numberOfWrittenBytes;
  441. buffer[index++] = 0;
  442. // Function string
  443. var functionString = value.code.toString();
  444. // Write the string
  445. var size = buffer.write(functionString, index + 4, undefined, 'utf8') + 1;
  446. // Write the size of the string to buffer
  447. buffer[index] = size & 0xff;
  448. buffer[index + 1] = (size >> 8) & 0xff;
  449. buffer[index + 2] = (size >> 16) & 0xff;
  450. buffer[index + 3] = (size >> 24) & 0xff;
  451. // Update index
  452. index = index + 4 + size - 1;
  453. // Write zero
  454. buffer[index++] = 0;
  455. }
  456. return index;
  457. }
  458. function serializeBinary(buffer, key, value, index, isArray) {
  459. // Write the type
  460. buffer[index++] = constants.BSON_DATA_BINARY;
  461. // Number of written bytes
  462. var numberOfWrittenBytes = !isArray
  463. ? buffer.write(key, index, undefined, 'utf8')
  464. : buffer.write(key, index, undefined, 'ascii');
  465. // Encode the name
  466. index = index + numberOfWrittenBytes;
  467. buffer[index++] = 0;
  468. // Extract the buffer
  469. var data = value.value(true);
  470. // Calculate size
  471. var size = value.position;
  472. // Add the deprecated 02 type 4 bytes of size to total
  473. if (value.sub_type === binary_1.Binary.SUBTYPE_BYTE_ARRAY)
  474. size = size + 4;
  475. // Write the size of the string to buffer
  476. buffer[index++] = size & 0xff;
  477. buffer[index++] = (size >> 8) & 0xff;
  478. buffer[index++] = (size >> 16) & 0xff;
  479. buffer[index++] = (size >> 24) & 0xff;
  480. // Write the subtype to the buffer
  481. buffer[index++] = value.sub_type;
  482. // If we have binary type 2 the 4 first bytes are the size
  483. if (value.sub_type === binary_1.Binary.SUBTYPE_BYTE_ARRAY) {
  484. size = size - 4;
  485. buffer[index++] = size & 0xff;
  486. buffer[index++] = (size >> 8) & 0xff;
  487. buffer[index++] = (size >> 16) & 0xff;
  488. buffer[index++] = (size >> 24) & 0xff;
  489. }
  490. // Write the data to the object
  491. buffer.set(data, index);
  492. // Adjust the index
  493. index = index + value.position;
  494. return index;
  495. }
  496. function serializeSymbol(buffer, key, value, index, isArray) {
  497. // Write the type
  498. buffer[index++] = constants.BSON_DATA_SYMBOL;
  499. // Number of written bytes
  500. var numberOfWrittenBytes = !isArray
  501. ? buffer.write(key, index, undefined, 'utf8')
  502. : buffer.write(key, index, undefined, 'ascii');
  503. // Encode the name
  504. index = index + numberOfWrittenBytes;
  505. buffer[index++] = 0;
  506. // Write the string
  507. var size = buffer.write(value.value, index + 4, undefined, 'utf8') + 1;
  508. // Write the size of the string to buffer
  509. buffer[index] = size & 0xff;
  510. buffer[index + 1] = (size >> 8) & 0xff;
  511. buffer[index + 2] = (size >> 16) & 0xff;
  512. buffer[index + 3] = (size >> 24) & 0xff;
  513. // Update index
  514. index = index + 4 + size - 1;
  515. // Write zero
  516. buffer[index++] = 0x00;
  517. return index;
  518. }
  519. function serializeDBRef(buffer, key, value, index, depth, serializeFunctions, isArray) {
  520. // Write the type
  521. buffer[index++] = constants.BSON_DATA_OBJECT;
  522. // Number of written bytes
  523. var numberOfWrittenBytes = !isArray
  524. ? buffer.write(key, index, undefined, 'utf8')
  525. : buffer.write(key, index, undefined, 'ascii');
  526. // Encode the name
  527. index = index + numberOfWrittenBytes;
  528. buffer[index++] = 0;
  529. var startIndex = index;
  530. var output = {
  531. $ref: value.collection || value.namespace,
  532. $id: value.oid
  533. };
  534. if (value.db != null) {
  535. output.$db = value.db;
  536. }
  537. output = Object.assign(output, value.fields);
  538. var endIndex = serializeInto(buffer, output, false, index, depth + 1, serializeFunctions);
  539. // Calculate object size
  540. var size = endIndex - startIndex;
  541. // Write the size
  542. buffer[startIndex++] = size & 0xff;
  543. buffer[startIndex++] = (size >> 8) & 0xff;
  544. buffer[startIndex++] = (size >> 16) & 0xff;
  545. buffer[startIndex++] = (size >> 24) & 0xff;
  546. // Set index
  547. return endIndex;
  548. }
  549. function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializeFunctions, ignoreUndefined, path) {
  550. if (checkKeys === void 0) { checkKeys = false; }
  551. if (startingIndex === void 0) { startingIndex = 0; }
  552. if (depth === void 0) { depth = 0; }
  553. if (serializeFunctions === void 0) { serializeFunctions = false; }
  554. if (ignoreUndefined === void 0) { ignoreUndefined = true; }
  555. if (path === void 0) { path = []; }
  556. startingIndex = startingIndex || 0;
  557. path = path || [];
  558. // Push the object to the path
  559. path.push(object);
  560. // Start place to serialize into
  561. var index = startingIndex + 4;
  562. // Special case isArray
  563. if (Array.isArray(object)) {
  564. // Get object keys
  565. for (var i = 0; i < object.length; i++) {
  566. var key = '' + i;
  567. var value = object[i];
  568. // Is there an override value
  569. if (value && value.toBSON) {
  570. if (typeof value.toBSON !== 'function')
  571. throw new TypeError('toBSON is not a function');
  572. value = value.toBSON();
  573. }
  574. if (typeof value === 'string') {
  575. index = serializeString(buffer, key, value, index, true);
  576. }
  577. else if (typeof value === 'number') {
  578. index = serializeNumber(buffer, key, value, index, true);
  579. }
  580. else if (typeof value === 'bigint') {
  581. throw new TypeError('Unsupported type BigInt, please use Decimal128');
  582. }
  583. else if (typeof value === 'boolean') {
  584. index = serializeBoolean(buffer, key, value, index, true);
  585. }
  586. else if (value instanceof Date || utils_1.isDate(value)) {
  587. index = serializeDate(buffer, key, value, index, true);
  588. }
  589. else if (value === undefined) {
  590. index = serializeNull(buffer, key, value, index, true);
  591. }
  592. else if (value === null) {
  593. index = serializeNull(buffer, key, value, index, true);
  594. }
  595. else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
  596. index = serializeObjectId(buffer, key, value, index, true);
  597. }
  598. else if (utils_1.isUint8Array(value)) {
  599. index = serializeBuffer(buffer, key, value, index, true);
  600. }
  601. else if (value instanceof RegExp || utils_1.isRegExp(value)) {
  602. index = serializeRegExp(buffer, key, value, index, true);
  603. }
  604. else if (typeof value === 'object' && value['_bsontype'] == null) {
  605. index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, true, path);
  606. }
  607. else if (typeof value === 'object' &&
  608. extended_json_1.isBSONType(value) &&
  609. value._bsontype === 'Decimal128') {
  610. index = serializeDecimal128(buffer, key, value, index, true);
  611. }
  612. else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
  613. index = serializeLong(buffer, key, value, index, true);
  614. }
  615. else if (value['_bsontype'] === 'Double') {
  616. index = serializeDouble(buffer, key, value, index, true);
  617. }
  618. else if (typeof value === 'function' && serializeFunctions) {
  619. index = serializeFunction(buffer, key, value, index, checkKeys, depth, true);
  620. }
  621. else if (value['_bsontype'] === 'Code') {
  622. index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, true);
  623. }
  624. else if (value['_bsontype'] === 'Binary') {
  625. index = serializeBinary(buffer, key, value, index, true);
  626. }
  627. else if (value['_bsontype'] === 'Symbol') {
  628. index = serializeSymbol(buffer, key, value, index, true);
  629. }
  630. else if (value['_bsontype'] === 'DBRef') {
  631. index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, true);
  632. }
  633. else if (value['_bsontype'] === 'BSONRegExp') {
  634. index = serializeBSONRegExp(buffer, key, value, index, true);
  635. }
  636. else if (value['_bsontype'] === 'Int32') {
  637. index = serializeInt32(buffer, key, value, index, true);
  638. }
  639. else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
  640. index = serializeMinMax(buffer, key, value, index, true);
  641. }
  642. else if (typeof value['_bsontype'] !== 'undefined') {
  643. throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
  644. }
  645. }
  646. }
  647. else if (object instanceof map_1.Map || utils_1.isMap(object)) {
  648. var iterator = object.entries();
  649. var done = false;
  650. while (!done) {
  651. // Unpack the next entry
  652. var entry = iterator.next();
  653. done = !!entry.done;
  654. // Are we done, then skip and terminate
  655. if (done)
  656. continue;
  657. // Get the entry values
  658. var key = entry.value[0];
  659. var value = entry.value[1];
  660. // Check the type of the value
  661. var type = typeof value;
  662. // Check the key and throw error if it's illegal
  663. if (typeof key === 'string' && !ignoreKeys.has(key)) {
  664. if (key.match(regexp) != null) {
  665. // The BSON spec doesn't allow keys with null bytes because keys are
  666. // null-terminated.
  667. throw Error('key ' + key + ' must not contain null bytes');
  668. }
  669. if (checkKeys) {
  670. if ('$' === key[0]) {
  671. throw Error('key ' + key + " must not start with '$'");
  672. }
  673. else if (~key.indexOf('.')) {
  674. throw Error('key ' + key + " must not contain '.'");
  675. }
  676. }
  677. }
  678. if (type === 'string') {
  679. index = serializeString(buffer, key, value, index);
  680. }
  681. else if (type === 'number') {
  682. index = serializeNumber(buffer, key, value, index);
  683. }
  684. else if (type === 'bigint' || utils_1.isBigInt64Array(value) || utils_1.isBigUInt64Array(value)) {
  685. throw new TypeError('Unsupported type BigInt, please use Decimal128');
  686. }
  687. else if (type === 'boolean') {
  688. index = serializeBoolean(buffer, key, value, index);
  689. }
  690. else if (value instanceof Date || utils_1.isDate(value)) {
  691. index = serializeDate(buffer, key, value, index);
  692. }
  693. else if (value === null || (value === undefined && ignoreUndefined === false)) {
  694. index = serializeNull(buffer, key, value, index);
  695. }
  696. else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
  697. index = serializeObjectId(buffer, key, value, index);
  698. }
  699. else if (utils_1.isUint8Array(value)) {
  700. index = serializeBuffer(buffer, key, value, index);
  701. }
  702. else if (value instanceof RegExp || utils_1.isRegExp(value)) {
  703. index = serializeRegExp(buffer, key, value, index);
  704. }
  705. else if (type === 'object' && value['_bsontype'] == null) {
  706. index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, false, path);
  707. }
  708. else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
  709. index = serializeDecimal128(buffer, key, value, index);
  710. }
  711. else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
  712. index = serializeLong(buffer, key, value, index);
  713. }
  714. else if (value['_bsontype'] === 'Double') {
  715. index = serializeDouble(buffer, key, value, index);
  716. }
  717. else if (value['_bsontype'] === 'Code') {
  718. index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
  719. }
  720. else if (typeof value === 'function' && serializeFunctions) {
  721. index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
  722. }
  723. else if (value['_bsontype'] === 'Binary') {
  724. index = serializeBinary(buffer, key, value, index);
  725. }
  726. else if (value['_bsontype'] === 'Symbol') {
  727. index = serializeSymbol(buffer, key, value, index);
  728. }
  729. else if (value['_bsontype'] === 'DBRef') {
  730. index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
  731. }
  732. else if (value['_bsontype'] === 'BSONRegExp') {
  733. index = serializeBSONRegExp(buffer, key, value, index);
  734. }
  735. else if (value['_bsontype'] === 'Int32') {
  736. index = serializeInt32(buffer, key, value, index);
  737. }
  738. else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
  739. index = serializeMinMax(buffer, key, value, index);
  740. }
  741. else if (typeof value['_bsontype'] !== 'undefined') {
  742. throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
  743. }
  744. }
  745. }
  746. else {
  747. // Did we provide a custom serialization method
  748. if (object.toBSON) {
  749. if (typeof object.toBSON !== 'function')
  750. throw new TypeError('toBSON is not a function');
  751. object = object.toBSON();
  752. if (object != null && typeof object !== 'object')
  753. throw new TypeError('toBSON function did not return an object');
  754. }
  755. // Iterate over all the keys
  756. for (var key in object) {
  757. var value = object[key];
  758. // Is there an override value
  759. if (value && value.toBSON) {
  760. if (typeof value.toBSON !== 'function')
  761. throw new TypeError('toBSON is not a function');
  762. value = value.toBSON();
  763. }
  764. // Check the type of the value
  765. var type = typeof value;
  766. // Check the key and throw error if it's illegal
  767. if (typeof key === 'string' && !ignoreKeys.has(key)) {
  768. if (key.match(regexp) != null) {
  769. // The BSON spec doesn't allow keys with null bytes because keys are
  770. // null-terminated.
  771. throw Error('key ' + key + ' must not contain null bytes');
  772. }
  773. if (checkKeys) {
  774. if ('$' === key[0]) {
  775. throw Error('key ' + key + " must not start with '$'");
  776. }
  777. else if (~key.indexOf('.')) {
  778. throw Error('key ' + key + " must not contain '.'");
  779. }
  780. }
  781. }
  782. if (type === 'string') {
  783. index = serializeString(buffer, key, value, index);
  784. }
  785. else if (type === 'number') {
  786. index = serializeNumber(buffer, key, value, index);
  787. }
  788. else if (type === 'bigint') {
  789. throw new TypeError('Unsupported type BigInt, please use Decimal128');
  790. }
  791. else if (type === 'boolean') {
  792. index = serializeBoolean(buffer, key, value, index);
  793. }
  794. else if (value instanceof Date || utils_1.isDate(value)) {
  795. index = serializeDate(buffer, key, value, index);
  796. }
  797. else if (value === undefined) {
  798. if (ignoreUndefined === false)
  799. index = serializeNull(buffer, key, value, index);
  800. }
  801. else if (value === null) {
  802. index = serializeNull(buffer, key, value, index);
  803. }
  804. else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
  805. index = serializeObjectId(buffer, key, value, index);
  806. }
  807. else if (utils_1.isUint8Array(value)) {
  808. index = serializeBuffer(buffer, key, value, index);
  809. }
  810. else if (value instanceof RegExp || utils_1.isRegExp(value)) {
  811. index = serializeRegExp(buffer, key, value, index);
  812. }
  813. else if (type === 'object' && value['_bsontype'] == null) {
  814. index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined, false, path);
  815. }
  816. else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
  817. index = serializeDecimal128(buffer, key, value, index);
  818. }
  819. else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
  820. index = serializeLong(buffer, key, value, index);
  821. }
  822. else if (value['_bsontype'] === 'Double') {
  823. index = serializeDouble(buffer, key, value, index);
  824. }
  825. else if (value['_bsontype'] === 'Code') {
  826. index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
  827. }
  828. else if (typeof value === 'function' && serializeFunctions) {
  829. index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
  830. }
  831. else if (value['_bsontype'] === 'Binary') {
  832. index = serializeBinary(buffer, key, value, index);
  833. }
  834. else if (value['_bsontype'] === 'Symbol') {
  835. index = serializeSymbol(buffer, key, value, index);
  836. }
  837. else if (value['_bsontype'] === 'DBRef') {
  838. index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
  839. }
  840. else if (value['_bsontype'] === 'BSONRegExp') {
  841. index = serializeBSONRegExp(buffer, key, value, index);
  842. }
  843. else if (value['_bsontype'] === 'Int32') {
  844. index = serializeInt32(buffer, key, value, index);
  845. }
  846. else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
  847. index = serializeMinMax(buffer, key, value, index);
  848. }
  849. else if (typeof value['_bsontype'] !== 'undefined') {
  850. throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
  851. }
  852. }
  853. }
  854. // Remove the path
  855. path.pop();
  856. // Final padding byte for object
  857. buffer[index++] = 0x00;
  858. // Final size
  859. var size = index - startingIndex;
  860. // Write the size of the object
  861. buffer[startingIndex++] = size & 0xff;
  862. buffer[startingIndex++] = (size >> 8) & 0xff;
  863. buffer[startingIndex++] = (size >> 16) & 0xff;
  864. buffer[startingIndex++] = (size >> 24) & 0xff;
  865. return index;
  866. }
  867. exports.serializeInto = serializeInto;
  868. //# sourceMappingURL=serializer.js.map