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/bson.d.ts CHANGED
@@ -24,6 +24,8 @@ export declare class Binary extends BSONValue {
24
24
  static readonly SUBTYPE_ENCRYPTED = 6;
25
25
  /** Column BSON type */
26
26
  static readonly SUBTYPE_COLUMN = 7;
27
+ /** Sensitive BSON type */
28
+ static readonly SUBTYPE_SENSITIVE = 8;
27
29
  /** User BSON type */
28
30
  static readonly SUBTYPE_USER_DEFINED = 128;
29
31
  buffer: Uint8Array;
@@ -140,12 +142,25 @@ declare namespace BSON {
140
142
  BSONRuntimeError,
141
143
  BSONType,
142
144
  EJSON,
145
+ onDemand,
143
146
  Document,
144
147
  CalculateObjectSizeOptions
145
148
  }
146
149
  }
147
150
  export { BSON }
148
151
 
152
+ /**
153
+ * @public
154
+ * @experimental
155
+ */
156
+ declare type BSONElement = [
157
+ type: number,
158
+ nameOffset: number,
159
+ nameLength: number,
160
+ offset: number,
161
+ length: number
162
+ ];
163
+
149
164
  /**
150
165
  * @public
151
166
  * @category Error
@@ -172,6 +187,21 @@ export declare class BSONError extends Error {
172
187
  static isBSONError(value: unknown): value is BSONError;
173
188
  }
174
189
 
190
+ /**
191
+ * @public
192
+ * @category Error
193
+ *
194
+ * @experimental
195
+ *
196
+ * An error generated when BSON bytes are invalid.
197
+ * Reports the offset the parser was able to reach before encountering the error.
198
+ */
199
+ declare class BSONOffsetError extends BSONError {
200
+ get name(): 'BSONOffsetError';
201
+ offset: number;
202
+ constructor(message: string, offset: number);
203
+ }
204
+
175
205
  /**
176
206
  * A class representation of the BSON RegExp type.
177
207
  * @public
@@ -1106,6 +1136,26 @@ export declare interface ObjectIdLike {
1106
1136
  toHexString(): string;
1107
1137
  }
1108
1138
 
1139
+ /**
1140
+ * @experimental
1141
+ * @public
1142
+ *
1143
+ * A new set of BSON APIs that are currently experimental and not intended for production use.
1144
+ */
1145
+ declare type OnDemand = {
1146
+ BSONOffsetError: {
1147
+ new (message: string, offset: number): BSONOffsetError;
1148
+ isBSONError(value: unknown): value is BSONError;
1149
+ };
1150
+ parseToElements: (this: void, bytes: Uint8Array, startOffset?: number) => Iterable<BSONElement>;
1151
+ };
1152
+
1153
+ /**
1154
+ * @experimental
1155
+ * @public
1156
+ */
1157
+ export declare const onDemand: OnDemand;
1158
+
1109
1159
  /**
1110
1160
  * Parse an Extended JSON string, constructing the JavaScript value or object described by that
1111
1161
  * string.
@@ -127,6 +127,15 @@ class BSONRuntimeError extends BSONError {
127
127
  super(message);
128
128
  }
129
129
  }
130
+ class BSONOffsetError extends BSONError {
131
+ get name() {
132
+ return 'BSONOffsetError';
133
+ }
134
+ constructor(message, offset) {
135
+ super(`${message}. offset: ${offset}`);
136
+ this.offset = offset;
137
+ }
138
+ }
130
139
 
131
140
  const FIRST_BIT = 0x80;
132
141
  const FIRST_TWO_BITS = 0xc0;
@@ -608,6 +617,7 @@ Binary.SUBTYPE_UUID = 4;
608
617
  Binary.SUBTYPE_MD5 = 5;
609
618
  Binary.SUBTYPE_ENCRYPTED = 6;
610
619
  Binary.SUBTYPE_COLUMN = 7;
620
+ Binary.SUBTYPE_SENSITIVE = 8;
611
621
  Binary.SUBTYPE_USER_DEFINED = 128;
612
622
  const UUID_BYTE_LENGTH = 16;
613
623
  const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
@@ -4123,6 +4133,104 @@ EJSON.serialize = EJSONserialize;
4123
4133
  EJSON.deserialize = EJSONdeserialize;
4124
4134
  Object.freeze(EJSON);
4125
4135
 
4136
+ function getSize(source, offset) {
4137
+ if (source[offset + 3] > 127) {
4138
+ throw new BSONOffsetError('BSON size cannot be negative', offset);
4139
+ }
4140
+ return (source[offset] |
4141
+ (source[offset + 1] << 8) |
4142
+ (source[offset + 2] << 16) |
4143
+ (source[offset + 3] << 24));
4144
+ }
4145
+ function findNull(bytes, offset) {
4146
+ let nullTerminatorOffset = offset;
4147
+ for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++)
4148
+ ;
4149
+ if (nullTerminatorOffset === bytes.length - 1) {
4150
+ throw new BSONOffsetError('Null terminator not found', offset);
4151
+ }
4152
+ return nullTerminatorOffset;
4153
+ }
4154
+ function parseToElements(bytes, startOffset = 0) {
4155
+ if (bytes.length < 5) {
4156
+ throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4157
+ }
4158
+ const documentSize = getSize(bytes, startOffset);
4159
+ if (documentSize > bytes.length - startOffset) {
4160
+ throw new BSONOffsetError(`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, startOffset);
4161
+ }
4162
+ if (bytes[startOffset + documentSize - 1] !== 0x00) {
4163
+ throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize);
4164
+ }
4165
+ const elements = [];
4166
+ let offset = startOffset + 4;
4167
+ while (offset <= documentSize + startOffset) {
4168
+ const type = bytes[offset];
4169
+ offset += 1;
4170
+ if (type === 0) {
4171
+ if (offset - startOffset !== documentSize) {
4172
+ throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
4173
+ }
4174
+ break;
4175
+ }
4176
+ const nameOffset = offset;
4177
+ const nameLength = findNull(bytes, offset) - nameOffset;
4178
+ offset += nameLength + 1;
4179
+ let length;
4180
+ if (type === 1 || type === 18 || type === 9 || type === 17) {
4181
+ length = 8;
4182
+ }
4183
+ else if (type === 16) {
4184
+ length = 4;
4185
+ }
4186
+ else if (type === 7) {
4187
+ length = 12;
4188
+ }
4189
+ else if (type === 19) {
4190
+ length = 16;
4191
+ }
4192
+ else if (type === 8) {
4193
+ length = 1;
4194
+ }
4195
+ else if (type === 10 || type === 6 || type === 127 || type === 255) {
4196
+ length = 0;
4197
+ }
4198
+ else if (type === 11) {
4199
+ length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4200
+ }
4201
+ else if (type === 3 || type === 4 || type === 15) {
4202
+ length = getSize(bytes, offset);
4203
+ }
4204
+ else if (type === 2 ||
4205
+ type === 5 ||
4206
+ type === 12 ||
4207
+ type === 13 ||
4208
+ type === 14) {
4209
+ length = getSize(bytes, offset) + 4;
4210
+ if (type === 5) {
4211
+ length += 1;
4212
+ }
4213
+ if (type === 12) {
4214
+ length += 12;
4215
+ }
4216
+ }
4217
+ else {
4218
+ throw new BSONOffsetError(`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, offset);
4219
+ }
4220
+ if (length > documentSize) {
4221
+ throw new BSONOffsetError('value reports length larger than document', offset);
4222
+ }
4223
+ elements.push([type, nameOffset, nameLength, offset, length]);
4224
+ offset += length;
4225
+ }
4226
+ return elements;
4227
+ }
4228
+
4229
+ const onDemand = Object.create(null);
4230
+ onDemand.parseToElements = parseToElements;
4231
+ onDemand.BSONOffsetError = BSONOffsetError;
4232
+ Object.freeze(onDemand);
4233
+
4126
4234
  const MAXSIZE = 1024 * 1024 * 17;
4127
4235
  let buffer = ByteUtils.allocate(MAXSIZE);
4128
4236
  function setInternalBufferSize(size) {
@@ -4199,6 +4307,7 @@ UUID: UUID,
4199
4307
  calculateObjectSize: calculateObjectSize,
4200
4308
  deserialize: deserialize,
4201
4309
  deserializeStream: deserializeStream,
4310
+ onDemand: onDemand,
4202
4311
  serialize: serialize,
4203
4312
  serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4204
4313
  setInternalBufferSize: setInternalBufferSize
@@ -4228,6 +4337,7 @@ exports.UUID = UUID;
4228
4337
  exports.calculateObjectSize = calculateObjectSize;
4229
4338
  exports.deserialize = deserialize;
4230
4339
  exports.deserializeStream = deserializeStream;
4340
+ exports.onDemand = onDemand;
4231
4341
  exports.serialize = serialize;
4232
4342
  exports.serializeWithBufferAndIndex = serializeWithBufferAndIndex;
4233
4343
  exports.setInternalBufferSize = setInternalBufferSize;