bson 6.5.1 → 6.6.1

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/lib/bson.cjs CHANGED
@@ -130,8 +130,8 @@ class BSONOffsetError extends BSONError {
130
130
  get name() {
131
131
  return 'BSONOffsetError';
132
132
  }
133
- constructor(message, offset) {
134
- super(`${message}. offset: ${offset}`);
133
+ constructor(message, offset, options) {
134
+ super(`${message}. offset: ${offset}`, options);
135
135
  this.offset = offset;
136
136
  }
137
137
  }
@@ -533,16 +533,16 @@ class Binary extends BSONValue {
533
533
  return this.position;
534
534
  }
535
535
  toJSON() {
536
- return ByteUtils.toBase64(this.buffer);
536
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
537
537
  }
538
538
  toString(encoding) {
539
539
  if (encoding === 'hex')
540
- return ByteUtils.toHex(this.buffer);
540
+ return ByteUtils.toHex(this.buffer.subarray(0, this.position));
541
541
  if (encoding === 'base64')
542
- return ByteUtils.toBase64(this.buffer);
542
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
543
543
  if (encoding === 'utf8' || encoding === 'utf-8')
544
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
545
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
544
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
545
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
546
546
  }
547
547
  toExtendedJSON(options) {
548
548
  options = options || {};
@@ -2138,6 +2138,15 @@ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2138
2138
  FLOAT[0] = -1;
2139
2139
  const isBigEndian = FLOAT_BYTES[7] === 0;
2140
2140
  const NumberUtils = {
2141
+ getNonnegativeInt32LE(source, offset) {
2142
+ if (source[offset + 3] > 127) {
2143
+ throw new RangeError(`Size cannot be negative at offset: ${offset}`);
2144
+ }
2145
+ return (source[offset] |
2146
+ (source[offset + 1] << 8) |
2147
+ (source[offset + 2] << 16) |
2148
+ (source[offset + 3] << 24));
2149
+ },
2141
2150
  getInt32LE(source, offset) {
2142
2151
  return (source[offset] |
2143
2152
  (source[offset + 1] << 8) |
@@ -4133,13 +4142,12 @@ EJSON.deserialize = EJSONdeserialize;
4133
4142
  Object.freeze(EJSON);
4134
4143
 
4135
4144
  function getSize(source, offset) {
4136
- if (source[offset + 3] > 127) {
4137
- throw new BSONOffsetError('BSON size cannot be negative', offset);
4145
+ try {
4146
+ return NumberUtils.getNonnegativeInt32LE(source, offset);
4147
+ }
4148
+ catch (cause) {
4149
+ throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
4138
4150
  }
4139
- return (source[offset] |
4140
- (source[offset + 1] << 8) |
4141
- (source[offset + 2] << 16) |
4142
- (source[offset + 3] << 24));
4143
4151
  }
4144
4152
  function findNull(bytes, offset) {
4145
4153
  let nullTerminatorOffset = offset;
@@ -4151,6 +4159,7 @@ function findNull(bytes, offset) {
4151
4159
  return nullTerminatorOffset;
4152
4160
  }
4153
4161
  function parseToElements(bytes, startOffset = 0) {
4162
+ startOffset ??= 0;
4154
4163
  if (bytes.length < 5) {
4155
4164
  throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4156
4165
  }
@@ -4176,7 +4185,10 @@ function parseToElements(bytes, startOffset = 0) {
4176
4185
  const nameLength = findNull(bytes, offset) - nameOffset;
4177
4186
  offset += nameLength + 1;
4178
4187
  let length;
4179
- if (type === 1 || type === 18 || type === 9 || type === 17) {
4188
+ if (type === 1 ||
4189
+ type === 18 ||
4190
+ type === 9 ||
4191
+ type === 17) {
4180
4192
  length = 8;
4181
4193
  }
4182
4194
  else if (type === 16) {
@@ -4191,13 +4203,18 @@ function parseToElements(bytes, startOffset = 0) {
4191
4203
  else if (type === 8) {
4192
4204
  length = 1;
4193
4205
  }
4194
- else if (type === 10 || type === 6 || type === 127 || type === 255) {
4206
+ else if (type === 10 ||
4207
+ type === 6 ||
4208
+ type === 127 ||
4209
+ type === 255) {
4195
4210
  length = 0;
4196
4211
  }
4197
4212
  else if (type === 11) {
4198
4213
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4199
4214
  }
4200
- else if (type === 3 || type === 4 || type === 15) {
4215
+ else if (type === 3 ||
4216
+ type === 4 ||
4217
+ type === 15) {
4201
4218
  length = getSize(bytes, offset);
4202
4219
  }
4203
4220
  else if (type === 2 ||
@@ -4227,7 +4244,8 @@ function parseToElements(bytes, startOffset = 0) {
4227
4244
 
4228
4245
  const onDemand = Object.create(null);
4229
4246
  onDemand.parseToElements = parseToElements;
4230
- onDemand.BSONOffsetError = BSONOffsetError;
4247
+ onDemand.ByteUtils = ByteUtils;
4248
+ onDemand.NumberUtils = NumberUtils;
4231
4249
  Object.freeze(onDemand);
4232
4250
 
4233
4251
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4284,6 +4302,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4284
4302
  var bson = /*#__PURE__*/Object.freeze({
4285
4303
  __proto__: null,
4286
4304
  BSONError: BSONError,
4305
+ BSONOffsetError: BSONOffsetError,
4287
4306
  BSONRegExp: BSONRegExp,
4288
4307
  BSONRuntimeError: BSONRuntimeError,
4289
4308
  BSONSymbol: BSONSymbol,
@@ -4314,6 +4333,7 @@ var bson = /*#__PURE__*/Object.freeze({
4314
4333
 
4315
4334
  exports.BSON = bson;
4316
4335
  exports.BSONError = BSONError;
4336
+ exports.BSONOffsetError = BSONOffsetError;
4317
4337
  exports.BSONRegExp = BSONRegExp;
4318
4338
  exports.BSONRuntimeError = BSONRuntimeError;
4319
4339
  exports.BSONSymbol = BSONSymbol;