bson 6.4.0 → 6.5.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/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;
@@ -2123,6 +2133,8 @@ class MinKey extends BSONValue {
2123
2133
 
2124
2134
  const FLOAT = new Float64Array(1);
2125
2135
  const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2136
+ FLOAT[0] = -1;
2137
+ const isBigEndian = FLOAT_BYTES[7] === 0;
2126
2138
  const NumberUtils = {
2127
2139
  getInt32LE(source, offset) {
2128
2140
  return (source[offset] |
@@ -2147,17 +2159,29 @@ const NumberUtils = {
2147
2159
  const hi = NumberUtils.getUint32LE(source, offset + 4);
2148
2160
  return (BigInt(hi) << BigInt(32)) + BigInt(lo);
2149
2161
  },
2150
- getFloat64LE(source, offset) {
2151
- FLOAT_BYTES[0] = source[offset];
2152
- FLOAT_BYTES[1] = source[offset + 1];
2153
- FLOAT_BYTES[2] = source[offset + 2];
2154
- FLOAT_BYTES[3] = source[offset + 3];
2155
- FLOAT_BYTES[4] = source[offset + 4];
2156
- FLOAT_BYTES[5] = source[offset + 5];
2157
- FLOAT_BYTES[6] = source[offset + 6];
2158
- FLOAT_BYTES[7] = source[offset + 7];
2159
- return FLOAT[0];
2160
- },
2162
+ getFloat64LE: isBigEndian
2163
+ ? (source, offset) => {
2164
+ FLOAT_BYTES[7] = source[offset];
2165
+ FLOAT_BYTES[6] = source[offset + 1];
2166
+ FLOAT_BYTES[5] = source[offset + 2];
2167
+ FLOAT_BYTES[4] = source[offset + 3];
2168
+ FLOAT_BYTES[3] = source[offset + 4];
2169
+ FLOAT_BYTES[2] = source[offset + 5];
2170
+ FLOAT_BYTES[1] = source[offset + 6];
2171
+ FLOAT_BYTES[0] = source[offset + 7];
2172
+ return FLOAT[0];
2173
+ }
2174
+ : (source, offset) => {
2175
+ FLOAT_BYTES[0] = source[offset];
2176
+ FLOAT_BYTES[1] = source[offset + 1];
2177
+ FLOAT_BYTES[2] = source[offset + 2];
2178
+ FLOAT_BYTES[3] = source[offset + 3];
2179
+ FLOAT_BYTES[4] = source[offset + 4];
2180
+ FLOAT_BYTES[5] = source[offset + 5];
2181
+ FLOAT_BYTES[6] = source[offset + 6];
2182
+ FLOAT_BYTES[7] = source[offset + 7];
2183
+ return FLOAT[0];
2184
+ },
2161
2185
  setInt32BE(destination, offset, value) {
2162
2186
  destination[offset + 3] = value;
2163
2187
  value >>>= 8;
@@ -2198,18 +2222,31 @@ const NumberUtils = {
2198
2222
  destination[offset + 7] = hi;
2199
2223
  return 8;
2200
2224
  },
2201
- setFloat64LE(destination, offset, value) {
2202
- FLOAT[0] = value;
2203
- destination[offset] = FLOAT_BYTES[0];
2204
- destination[offset + 1] = FLOAT_BYTES[1];
2205
- destination[offset + 2] = FLOAT_BYTES[2];
2206
- destination[offset + 3] = FLOAT_BYTES[3];
2207
- destination[offset + 4] = FLOAT_BYTES[4];
2208
- destination[offset + 5] = FLOAT_BYTES[5];
2209
- destination[offset + 6] = FLOAT_BYTES[6];
2210
- destination[offset + 7] = FLOAT_BYTES[7];
2211
- return 8;
2212
- }
2225
+ setFloat64LE: isBigEndian
2226
+ ? (destination, offset, value) => {
2227
+ FLOAT[0] = value;
2228
+ destination[offset] = FLOAT_BYTES[7];
2229
+ destination[offset + 1] = FLOAT_BYTES[6];
2230
+ destination[offset + 2] = FLOAT_BYTES[5];
2231
+ destination[offset + 3] = FLOAT_BYTES[4];
2232
+ destination[offset + 4] = FLOAT_BYTES[3];
2233
+ destination[offset + 5] = FLOAT_BYTES[2];
2234
+ destination[offset + 6] = FLOAT_BYTES[1];
2235
+ destination[offset + 7] = FLOAT_BYTES[0];
2236
+ return 8;
2237
+ }
2238
+ : (destination, offset, value) => {
2239
+ FLOAT[0] = value;
2240
+ destination[offset] = FLOAT_BYTES[0];
2241
+ destination[offset + 1] = FLOAT_BYTES[1];
2242
+ destination[offset + 2] = FLOAT_BYTES[2];
2243
+ destination[offset + 3] = FLOAT_BYTES[3];
2244
+ destination[offset + 4] = FLOAT_BYTES[4];
2245
+ destination[offset + 5] = FLOAT_BYTES[5];
2246
+ destination[offset + 6] = FLOAT_BYTES[6];
2247
+ destination[offset + 7] = FLOAT_BYTES[7];
2248
+ return 8;
2249
+ }
2213
2250
  };
2214
2251
 
2215
2252
  const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
@@ -4087,6 +4124,104 @@ EJSON.serialize = EJSONserialize;
4087
4124
  EJSON.deserialize = EJSONdeserialize;
4088
4125
  Object.freeze(EJSON);
4089
4126
 
4127
+ function getSize(source, offset) {
4128
+ if (source[offset + 3] > 127) {
4129
+ throw new BSONOffsetError('BSON size cannot be negative', offset);
4130
+ }
4131
+ return (source[offset] |
4132
+ (source[offset + 1] << 8) |
4133
+ (source[offset + 2] << 16) |
4134
+ (source[offset + 3] << 24));
4135
+ }
4136
+ function findNull(bytes, offset) {
4137
+ let nullTerminatorOffset = offset;
4138
+ for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++)
4139
+ ;
4140
+ if (nullTerminatorOffset === bytes.length - 1) {
4141
+ throw new BSONOffsetError('Null terminator not found', offset);
4142
+ }
4143
+ return nullTerminatorOffset;
4144
+ }
4145
+ function parseToElements(bytes, startOffset = 0) {
4146
+ if (bytes.length < 5) {
4147
+ throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4148
+ }
4149
+ const documentSize = getSize(bytes, startOffset);
4150
+ if (documentSize > bytes.length - startOffset) {
4151
+ throw new BSONOffsetError(`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, startOffset);
4152
+ }
4153
+ if (bytes[startOffset + documentSize - 1] !== 0x00) {
4154
+ throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize);
4155
+ }
4156
+ const elements = [];
4157
+ let offset = startOffset + 4;
4158
+ while (offset <= documentSize + startOffset) {
4159
+ const type = bytes[offset];
4160
+ offset += 1;
4161
+ if (type === 0) {
4162
+ if (offset - startOffset !== documentSize) {
4163
+ throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
4164
+ }
4165
+ break;
4166
+ }
4167
+ const nameOffset = offset;
4168
+ const nameLength = findNull(bytes, offset) - nameOffset;
4169
+ offset += nameLength + 1;
4170
+ let length;
4171
+ if (type === 1 || type === 18 || type === 9 || type === 17) {
4172
+ length = 8;
4173
+ }
4174
+ else if (type === 16) {
4175
+ length = 4;
4176
+ }
4177
+ else if (type === 7) {
4178
+ length = 12;
4179
+ }
4180
+ else if (type === 19) {
4181
+ length = 16;
4182
+ }
4183
+ else if (type === 8) {
4184
+ length = 1;
4185
+ }
4186
+ else if (type === 10 || type === 6 || type === 127 || type === 255) {
4187
+ length = 0;
4188
+ }
4189
+ else if (type === 11) {
4190
+ length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4191
+ }
4192
+ else if (type === 3 || type === 4 || type === 15) {
4193
+ length = getSize(bytes, offset);
4194
+ }
4195
+ else if (type === 2 ||
4196
+ type === 5 ||
4197
+ type === 12 ||
4198
+ type === 13 ||
4199
+ type === 14) {
4200
+ length = getSize(bytes, offset) + 4;
4201
+ if (type === 5) {
4202
+ length += 1;
4203
+ }
4204
+ if (type === 12) {
4205
+ length += 12;
4206
+ }
4207
+ }
4208
+ else {
4209
+ throw new BSONOffsetError(`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, offset);
4210
+ }
4211
+ if (length > documentSize) {
4212
+ throw new BSONOffsetError('value reports length larger than document', offset);
4213
+ }
4214
+ elements.push([type, nameOffset, nameLength, offset, length]);
4215
+ offset += length;
4216
+ }
4217
+ return elements;
4218
+ }
4219
+
4220
+ const onDemand = Object.create(null);
4221
+ onDemand.parseToElements = parseToElements;
4222
+ onDemand.BSONOffsetError = BSONOffsetError;
4223
+ Object.freeze(onDemand);
4224
+
4090
4225
  const MAXSIZE = 1024 * 1024 * 17;
4091
4226
  let buffer = ByteUtils.allocate(MAXSIZE);
4092
4227
  function setInternalBufferSize(size) {
@@ -4163,10 +4298,11 @@ var bson = /*#__PURE__*/Object.freeze({
4163
4298
  calculateObjectSize: calculateObjectSize,
4164
4299
  deserialize: deserialize,
4165
4300
  deserializeStream: deserializeStream,
4301
+ onDemand: onDemand,
4166
4302
  serialize: serialize,
4167
4303
  serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4168
4304
  setInternalBufferSize: setInternalBufferSize
4169
4305
  });
4170
4306
 
4171
- 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 };
4307
+ 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 };
4172
4308
  //# sourceMappingURL=bson.mjs.map