bson 5.0.1 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "etc/prepare.js"
14
14
  ],
15
15
  "types": "bson.d.ts",
16
- "version": "5.0.1",
16
+ "version": "5.2.0",
17
17
  "author": {
18
18
  "name": "The MongoDB NodeJS Team",
19
19
  "email": "dbx-node@mongodb.com"
package/src/binary.ts CHANGED
@@ -258,6 +258,16 @@ export class Binary extends BSONValue {
258
258
  );
259
259
  }
260
260
 
261
+ /** Creates an Binary instance from a hex digit string */
262
+ static createFromHexString(hex: string, subType?: number): Binary {
263
+ return new Binary(ByteUtils.fromHex(hex), subType);
264
+ }
265
+
266
+ /** Creates an Binary instance from a base64 string */
267
+ static createFromBase64(base64: string, subType?: number): Binary {
268
+ return new Binary(ByteUtils.fromBase64(base64), subType);
269
+ }
270
+
261
271
  /** @internal */
262
272
  static fromExtendedJSON(
263
273
  doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended,
@@ -292,7 +302,8 @@ export class Binary extends BSONValue {
292
302
  }
293
303
 
294
304
  inspect(): string {
295
- return `new Binary(Buffer.from("${ByteUtils.toHex(this.buffer)}", "hex"), ${this.sub_type})`;
305
+ const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
306
+ return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
296
307
  }
297
308
  }
298
309
 
@@ -464,11 +475,16 @@ export class UUID extends Binary {
464
475
  * Creates an UUID from a hex string representation of an UUID.
465
476
  * @param hexString - 32 or 36 character hex string (dashes excluded/included).
466
477
  */
467
- static createFromHexString(hexString: string): UUID {
478
+ static override createFromHexString(hexString: string): UUID {
468
479
  const buffer = uuidHexStringToBuffer(hexString);
469
480
  return new UUID(buffer);
470
481
  }
471
482
 
483
+ /** Creates an UUID from a base64 string representation of an UUID. */
484
+ static override createFromBase64(base64: string): UUID {
485
+ return new UUID(ByteUtils.fromBase64(base64));
486
+ }
487
+
472
488
  /**
473
489
  * Converts to a string representation of this Id.
474
490
  *
@@ -17,7 +17,7 @@ import { Long } from './long';
17
17
  import { MaxKey } from './max_key';
18
18
  import { MinKey } from './min_key';
19
19
  import { ObjectId } from './objectid';
20
- import { isDate, isRegExp } from './parser/utils';
20
+ import { isDate, isRegExp, isMap } from './parser/utils';
21
21
  import { BSONRegExp } from './regexp';
22
22
  import { BSONSymbol } from './symbol';
23
23
  import { Timestamp } from './timestamp';
@@ -190,6 +190,18 @@ function getISOString(date: Date) {
190
190
 
191
191
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
192
192
  function serializeValue(value: any, options: EJSONSerializeOptions): any {
193
+ if (value instanceof Map || isMap(value)) {
194
+ const obj: Record<string, unknown> = Object.create(null);
195
+ for (const [k, v] of value) {
196
+ if (typeof k !== 'string') {
197
+ throw new BSONError('Can only serialize maps with string keys');
198
+ }
199
+ obj[k] = v;
200
+ }
201
+
202
+ return serializeValue(obj, options);
203
+ }
204
+
193
205
  if ((typeof value === 'object' || typeof value === 'function') && value !== null) {
194
206
  const index = options.seenObjects.findIndex(entry => entry.obj === value);
195
207
  if (index !== -1) {
package/src/objectid.ts CHANGED
@@ -264,16 +264,22 @@ export class ObjectId extends BSONValue {
264
264
  * @param hexString - create a ObjectId from a passed in 24 character hexstring.
265
265
  */
266
266
  static createFromHexString(hexString: string): ObjectId {
267
- // Throw an error if it's not a valid setup
268
- if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
269
- throw new BSONError(
270
- 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
271
- );
267
+ if (hexString?.length !== 24) {
268
+ throw new BSONError('hex string must be 24 characters');
272
269
  }
273
270
 
274
271
  return new ObjectId(ByteUtils.fromHex(hexString));
275
272
  }
276
273
 
274
+ /** Creates an ObjectId instance from a base64 string */
275
+ static createFromBase64(base64: string): ObjectId {
276
+ if (base64?.length !== 16) {
277
+ throw new BSONError('base64 string must be 16 characters');
278
+ }
279
+
280
+ return new ObjectId(ByteUtils.fromBase64(base64));
281
+ }
282
+
277
283
  /**
278
284
  * Checks if a value is a valid bson ObjectId
279
285
  *