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.mjs CHANGED
@@ -128,8 +128,8 @@ class BSONOffsetError extends BSONError {
128
128
  get name() {
129
129
  return 'BSONOffsetError';
130
130
  }
131
- constructor(message, offset) {
132
- super(`${message}. offset: ${offset}`);
131
+ constructor(message, offset, options) {
132
+ super(`${message}. offset: ${offset}`, options);
133
133
  this.offset = offset;
134
134
  }
135
135
  }
@@ -531,16 +531,16 @@ class Binary extends BSONValue {
531
531
  return this.position;
532
532
  }
533
533
  toJSON() {
534
- return ByteUtils.toBase64(this.buffer);
534
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
535
535
  }
536
536
  toString(encoding) {
537
537
  if (encoding === 'hex')
538
- return ByteUtils.toHex(this.buffer);
538
+ return ByteUtils.toHex(this.buffer.subarray(0, this.position));
539
539
  if (encoding === 'base64')
540
- return ByteUtils.toBase64(this.buffer);
540
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
541
541
  if (encoding === 'utf8' || encoding === 'utf-8')
542
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
543
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
542
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
543
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
544
544
  }
545
545
  toExtendedJSON(options) {
546
546
  options = options || {};
@@ -2136,6 +2136,15 @@ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2136
2136
  FLOAT[0] = -1;
2137
2137
  const isBigEndian = FLOAT_BYTES[7] === 0;
2138
2138
  const NumberUtils = {
2139
+ getNonnegativeInt32LE(source, offset) {
2140
+ if (source[offset + 3] > 127) {
2141
+ throw new RangeError(`Size cannot be negative at offset: ${offset}`);
2142
+ }
2143
+ return (source[offset] |
2144
+ (source[offset + 1] << 8) |
2145
+ (source[offset + 2] << 16) |
2146
+ (source[offset + 3] << 24));
2147
+ },
2139
2148
  getInt32LE(source, offset) {
2140
2149
  return (source[offset] |
2141
2150
  (source[offset + 1] << 8) |
@@ -4131,13 +4140,12 @@ EJSON.deserialize = EJSONdeserialize;
4131
4140
  Object.freeze(EJSON);
4132
4141
 
4133
4142
  function getSize(source, offset) {
4134
- if (source[offset + 3] > 127) {
4135
- throw new BSONOffsetError('BSON size cannot be negative', offset);
4143
+ try {
4144
+ return NumberUtils.getNonnegativeInt32LE(source, offset);
4145
+ }
4146
+ catch (cause) {
4147
+ throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
4136
4148
  }
4137
- return (source[offset] |
4138
- (source[offset + 1] << 8) |
4139
- (source[offset + 2] << 16) |
4140
- (source[offset + 3] << 24));
4141
4149
  }
4142
4150
  function findNull(bytes, offset) {
4143
4151
  let nullTerminatorOffset = offset;
@@ -4149,6 +4157,7 @@ function findNull(bytes, offset) {
4149
4157
  return nullTerminatorOffset;
4150
4158
  }
4151
4159
  function parseToElements(bytes, startOffset = 0) {
4160
+ startOffset ??= 0;
4152
4161
  if (bytes.length < 5) {
4153
4162
  throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4154
4163
  }
@@ -4174,7 +4183,10 @@ function parseToElements(bytes, startOffset = 0) {
4174
4183
  const nameLength = findNull(bytes, offset) - nameOffset;
4175
4184
  offset += nameLength + 1;
4176
4185
  let length;
4177
- if (type === 1 || type === 18 || type === 9 || type === 17) {
4186
+ if (type === 1 ||
4187
+ type === 18 ||
4188
+ type === 9 ||
4189
+ type === 17) {
4178
4190
  length = 8;
4179
4191
  }
4180
4192
  else if (type === 16) {
@@ -4189,13 +4201,18 @@ function parseToElements(bytes, startOffset = 0) {
4189
4201
  else if (type === 8) {
4190
4202
  length = 1;
4191
4203
  }
4192
- else if (type === 10 || type === 6 || type === 127 || type === 255) {
4204
+ else if (type === 10 ||
4205
+ type === 6 ||
4206
+ type === 127 ||
4207
+ type === 255) {
4193
4208
  length = 0;
4194
4209
  }
4195
4210
  else if (type === 11) {
4196
4211
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4197
4212
  }
4198
- else if (type === 3 || type === 4 || type === 15) {
4213
+ else if (type === 3 ||
4214
+ type === 4 ||
4215
+ type === 15) {
4199
4216
  length = getSize(bytes, offset);
4200
4217
  }
4201
4218
  else if (type === 2 ||
@@ -4225,7 +4242,8 @@ function parseToElements(bytes, startOffset = 0) {
4225
4242
 
4226
4243
  const onDemand = Object.create(null);
4227
4244
  onDemand.parseToElements = parseToElements;
4228
- onDemand.BSONOffsetError = BSONOffsetError;
4245
+ onDemand.ByteUtils = ByteUtils;
4246
+ onDemand.NumberUtils = NumberUtils;
4229
4247
  Object.freeze(onDemand);
4230
4248
 
4231
4249
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4282,6 +4300,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4282
4300
  var bson = /*#__PURE__*/Object.freeze({
4283
4301
  __proto__: null,
4284
4302
  BSONError: BSONError,
4303
+ BSONOffsetError: BSONOffsetError,
4285
4304
  BSONRegExp: BSONRegExp,
4286
4305
  BSONRuntimeError: BSONRuntimeError,
4287
4306
  BSONSymbol: BSONSymbol,
@@ -4310,5 +4329,5 @@ var bson = /*#__PURE__*/Object.freeze({
4310
4329
  setInternalBufferSize: setInternalBufferSize
4311
4330
  });
4312
4331
 
4313
- export { bson as BSON, BSONError, BSONRegExp, BSONRuntimeError, BSONSymbol, BSONType, BSONValue, BSONVersionError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, onDemand, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
4332
+ export { bson as BSON, BSONError, BSONOffsetError, BSONRegExp, BSONRuntimeError, BSONSymbol, BSONType, BSONValue, BSONVersionError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, onDemand, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
4314
4333
  //# sourceMappingURL=bson.mjs.map