bson 6.4.1 → 6.5.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
@@ -124,6 +124,15 @@ class BSONRuntimeError extends BSONError {
124
124
  super(message);
125
125
  }
126
126
  }
127
+ class BSONOffsetError extends BSONError {
128
+ get name() {
129
+ return 'BSONOffsetError';
130
+ }
131
+ constructor(message, offset) {
132
+ super(`${message}. offset: ${offset}`);
133
+ this.offset = offset;
134
+ }
135
+ }
127
136
 
128
137
  const FIRST_BIT = 0x80;
129
138
  const FIRST_TWO_BITS = 0xc0;
@@ -605,6 +614,7 @@ Binary.SUBTYPE_UUID = 4;
605
614
  Binary.SUBTYPE_MD5 = 5;
606
615
  Binary.SUBTYPE_ENCRYPTED = 6;
607
616
  Binary.SUBTYPE_COLUMN = 7;
617
+ Binary.SUBTYPE_SENSITIVE = 8;
608
618
  Binary.SUBTYPE_USER_DEFINED = 128;
609
619
  const UUID_BYTE_LENGTH = 16;
610
620
  const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
@@ -4120,6 +4130,104 @@ EJSON.serialize = EJSONserialize;
4120
4130
  EJSON.deserialize = EJSONdeserialize;
4121
4131
  Object.freeze(EJSON);
4122
4132
 
4133
+ function getSize(source, offset) {
4134
+ if (source[offset + 3] > 127) {
4135
+ throw new BSONOffsetError('BSON size cannot be negative', offset);
4136
+ }
4137
+ return (source[offset] |
4138
+ (source[offset + 1] << 8) |
4139
+ (source[offset + 2] << 16) |
4140
+ (source[offset + 3] << 24));
4141
+ }
4142
+ function findNull(bytes, offset) {
4143
+ let nullTerminatorOffset = offset;
4144
+ for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++)
4145
+ ;
4146
+ if (nullTerminatorOffset === bytes.length - 1) {
4147
+ throw new BSONOffsetError('Null terminator not found', offset);
4148
+ }
4149
+ return nullTerminatorOffset;
4150
+ }
4151
+ function parseToElements(bytes, startOffset = 0) {
4152
+ if (bytes.length < 5) {
4153
+ throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4154
+ }
4155
+ const documentSize = getSize(bytes, startOffset);
4156
+ if (documentSize > bytes.length - startOffset) {
4157
+ throw new BSONOffsetError(`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, startOffset);
4158
+ }
4159
+ if (bytes[startOffset + documentSize - 1] !== 0x00) {
4160
+ throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize);
4161
+ }
4162
+ const elements = [];
4163
+ let offset = startOffset + 4;
4164
+ while (offset <= documentSize + startOffset) {
4165
+ const type = bytes[offset];
4166
+ offset += 1;
4167
+ if (type === 0) {
4168
+ if (offset - startOffset !== documentSize) {
4169
+ throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
4170
+ }
4171
+ break;
4172
+ }
4173
+ const nameOffset = offset;
4174
+ const nameLength = findNull(bytes, offset) - nameOffset;
4175
+ offset += nameLength + 1;
4176
+ let length;
4177
+ if (type === 1 || type === 18 || type === 9 || type === 17) {
4178
+ length = 8;
4179
+ }
4180
+ else if (type === 16) {
4181
+ length = 4;
4182
+ }
4183
+ else if (type === 7) {
4184
+ length = 12;
4185
+ }
4186
+ else if (type === 19) {
4187
+ length = 16;
4188
+ }
4189
+ else if (type === 8) {
4190
+ length = 1;
4191
+ }
4192
+ else if (type === 10 || type === 6 || type === 127 || type === 255) {
4193
+ length = 0;
4194
+ }
4195
+ else if (type === 11) {
4196
+ length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4197
+ }
4198
+ else if (type === 3 || type === 4 || type === 15) {
4199
+ length = getSize(bytes, offset);
4200
+ }
4201
+ else if (type === 2 ||
4202
+ type === 5 ||
4203
+ type === 12 ||
4204
+ type === 13 ||
4205
+ type === 14) {
4206
+ length = getSize(bytes, offset) + 4;
4207
+ if (type === 5) {
4208
+ length += 1;
4209
+ }
4210
+ if (type === 12) {
4211
+ length += 12;
4212
+ }
4213
+ }
4214
+ else {
4215
+ throw new BSONOffsetError(`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, offset);
4216
+ }
4217
+ if (length > documentSize) {
4218
+ throw new BSONOffsetError('value reports length larger than document', offset);
4219
+ }
4220
+ elements.push([type, nameOffset, nameLength, offset, length]);
4221
+ offset += length;
4222
+ }
4223
+ return elements;
4224
+ }
4225
+
4226
+ const onDemand = Object.create(null);
4227
+ onDemand.parseToElements = parseToElements;
4228
+ onDemand.BSONOffsetError = BSONOffsetError;
4229
+ Object.freeze(onDemand);
4230
+
4123
4231
  const MAXSIZE = 1024 * 1024 * 17;
4124
4232
  let buffer = ByteUtils.allocate(MAXSIZE);
4125
4233
  function setInternalBufferSize(size) {
@@ -4196,10 +4304,11 @@ var bson = /*#__PURE__*/Object.freeze({
4196
4304
  calculateObjectSize: calculateObjectSize,
4197
4305
  deserialize: deserialize,
4198
4306
  deserializeStream: deserializeStream,
4307
+ onDemand: onDemand,
4199
4308
  serialize: serialize,
4200
4309
  serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4201
4310
  setInternalBufferSize: setInternalBufferSize
4202
4311
  });
4203
4312
 
4204
- 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, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
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 };
4205
4314
  //# sourceMappingURL=bson.mjs.map