bson 4.7.0 → 5.0.0-alpha.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.
Files changed (98) hide show
  1. package/bson.d.ts +207 -259
  2. package/lib/bson.bundle.js +4034 -0
  3. package/lib/bson.bundle.js.map +1 -0
  4. package/lib/bson.cjs +4029 -0
  5. package/lib/bson.cjs.map +1 -0
  6. package/lib/bson.mjs +4003 -0
  7. package/lib/bson.mjs.map +1 -0
  8. package/package.json +49 -62
  9. package/src/binary.ts +64 -53
  10. package/src/bson.ts +27 -108
  11. package/src/code.ts +25 -14
  12. package/src/constants.ts +33 -0
  13. package/src/db_ref.ts +14 -8
  14. package/src/decimal128.ts +32 -25
  15. package/src/double.ts +8 -5
  16. package/src/error.ts +0 -2
  17. package/src/extended_json.ts +148 -148
  18. package/src/index.ts +19 -0
  19. package/src/int_32.ts +8 -5
  20. package/src/long.ts +17 -16
  21. package/src/max_key.ts +8 -6
  22. package/src/min_key.ts +8 -6
  23. package/src/objectid.ts +42 -74
  24. package/src/parser/calculate_size.ts +39 -63
  25. package/src/parser/deserializer.ts +41 -112
  26. package/src/parser/serializer.ts +234 -341
  27. package/src/parser/utils.ts +1 -99
  28. package/src/regexp.ts +17 -5
  29. package/src/symbol.ts +9 -5
  30. package/src/timestamp.ts +63 -27
  31. package/src/utils/byte_utils.ts +61 -0
  32. package/src/utils/node_byte_utils.ts +141 -0
  33. package/src/utils/web_byte_utils.ts +190 -0
  34. package/src/uuid_utils.ts +15 -15
  35. package/bower.json +0 -26
  36. package/dist/bson.browser.esm.js +0 -7470
  37. package/dist/bson.browser.esm.js.map +0 -1
  38. package/dist/bson.browser.umd.js +0 -7537
  39. package/dist/bson.browser.umd.js.map +0 -1
  40. package/dist/bson.bundle.js +0 -7536
  41. package/dist/bson.bundle.js.map +0 -1
  42. package/dist/bson.esm.js +0 -5436
  43. package/dist/bson.esm.js.map +0 -1
  44. package/lib/binary.js +0 -426
  45. package/lib/binary.js.map +0 -1
  46. package/lib/bson.js +0 -251
  47. package/lib/bson.js.map +0 -1
  48. package/lib/code.js +0 -46
  49. package/lib/code.js.map +0 -1
  50. package/lib/constants.js +0 -82
  51. package/lib/constants.js.map +0 -1
  52. package/lib/db_ref.js +0 -97
  53. package/lib/db_ref.js.map +0 -1
  54. package/lib/decimal128.js +0 -669
  55. package/lib/decimal128.js.map +0 -1
  56. package/lib/double.js +0 -76
  57. package/lib/double.js.map +0 -1
  58. package/lib/ensure_buffer.js +0 -25
  59. package/lib/ensure_buffer.js.map +0 -1
  60. package/lib/error.js +0 -55
  61. package/lib/error.js.map +0 -1
  62. package/lib/extended_json.js +0 -390
  63. package/lib/extended_json.js.map +0 -1
  64. package/lib/int_32.js +0 -58
  65. package/lib/int_32.js.map +0 -1
  66. package/lib/long.js +0 -900
  67. package/lib/long.js.map +0 -1
  68. package/lib/map.js +0 -123
  69. package/lib/map.js.map +0 -1
  70. package/lib/max_key.js +0 -33
  71. package/lib/max_key.js.map +0 -1
  72. package/lib/min_key.js +0 -33
  73. package/lib/min_key.js.map +0 -1
  74. package/lib/objectid.js +0 -299
  75. package/lib/objectid.js.map +0 -1
  76. package/lib/parser/calculate_size.js +0 -194
  77. package/lib/parser/calculate_size.js.map +0 -1
  78. package/lib/parser/deserializer.js +0 -665
  79. package/lib/parser/deserializer.js.map +0 -1
  80. package/lib/parser/serializer.js +0 -867
  81. package/lib/parser/serializer.js.map +0 -1
  82. package/lib/parser/utils.js +0 -115
  83. package/lib/parser/utils.js.map +0 -1
  84. package/lib/regexp.js +0 -74
  85. package/lib/regexp.js.map +0 -1
  86. package/lib/symbol.js +0 -48
  87. package/lib/symbol.js.map +0 -1
  88. package/lib/timestamp.js +0 -102
  89. package/lib/timestamp.js.map +0 -1
  90. package/lib/utils/global.js +0 -18
  91. package/lib/utils/global.js.map +0 -1
  92. package/lib/uuid_utils.js +0 -35
  93. package/lib/uuid_utils.js.map +0 -1
  94. package/lib/validate_utf8.js +0 -47
  95. package/lib/validate_utf8.js.map +0 -1
  96. package/src/ensure_buffer.ts +0 -27
  97. package/src/map.ts +0 -119
  98. package/src/utils/global.ts +0 -22
@@ -0,0 +1,190 @@
1
+ import { BSONError } from '../error';
2
+
3
+ type TextDecoder = {
4
+ readonly encoding: string;
5
+ readonly fatal: boolean;
6
+ readonly ignoreBOM: boolean;
7
+ decode(input?: Uint8Array): string;
8
+ };
9
+ type TextDecoderConstructor = {
10
+ new (label: 'utf8', options: { fatal: boolean; ignoreBOM?: boolean }): TextDecoder;
11
+ };
12
+
13
+ type TextEncoder = {
14
+ readonly encoding: string;
15
+ encode(input?: string): Uint8Array;
16
+ };
17
+ type TextEncoderConstructor = {
18
+ new (): TextEncoder;
19
+ };
20
+
21
+ // Web global
22
+ declare const TextDecoder: TextDecoderConstructor;
23
+ declare const TextEncoder: TextEncoderConstructor;
24
+ declare const atob: (base64: string) => string;
25
+ declare const btoa: (binary: string) => string;
26
+
27
+ type ArrayBufferViewWithTag = ArrayBufferView & {
28
+ [Symbol.toStringTag]?: string;
29
+ };
30
+
31
+ function isReactNative() {
32
+ const { navigator } = globalThis as { navigator?: { product?: string } };
33
+ return typeof navigator === 'object' && navigator.product === 'ReactNative';
34
+ }
35
+
36
+ /** @internal */
37
+ export function webMathRandomBytes(byteLength: number) {
38
+ if (byteLength < 0) {
39
+ throw new RangeError(`The argument 'byteLength' is invalid. Received ${byteLength}`);
40
+ }
41
+ return webByteUtils.fromNumberArray(
42
+ Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256))
43
+ );
44
+ }
45
+
46
+ /** @internal */
47
+ const webRandomBytes: (byteLength: number) => Uint8Array = (() => {
48
+ const { crypto } = globalThis as {
49
+ crypto?: { getRandomValues?: (space: Uint8Array) => Uint8Array };
50
+ };
51
+ if (crypto != null && typeof crypto.getRandomValues === 'function') {
52
+ return (byteLength: number) => {
53
+ // @ts-expect-error: crypto.getRandomValues cannot actually be null here
54
+ // You cannot separate getRandomValues from crypto (need to have this === crypto)
55
+ return crypto.getRandomValues(webByteUtils.allocate(byteLength));
56
+ };
57
+ } else {
58
+ if (isReactNative()) {
59
+ const { console } = globalThis as { console?: { warn?: (message: string) => void } };
60
+ console?.warn?.(
61
+ 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
62
+ );
63
+ }
64
+ return webMathRandomBytes;
65
+ }
66
+ })();
67
+
68
+ const HEX_DIGIT = /(\d|[a-f])/i;
69
+
70
+ /** @internal */
71
+ export const webByteUtils = {
72
+ toLocalBufferType(
73
+ potentialUint8array: Uint8Array | ArrayBufferViewWithTag | ArrayBuffer
74
+ ): Uint8Array {
75
+ const stringTag =
76
+ potentialUint8array?.[Symbol.toStringTag] ??
77
+ Object.prototype.toString.call(potentialUint8array);
78
+
79
+ if (stringTag === 'Uint8Array') {
80
+ return potentialUint8array as Uint8Array;
81
+ }
82
+
83
+ if (ArrayBuffer.isView(potentialUint8array)) {
84
+ return new Uint8Array(
85
+ potentialUint8array.buffer.slice(
86
+ potentialUint8array.byteOffset,
87
+ potentialUint8array.byteOffset + potentialUint8array.byteLength
88
+ )
89
+ );
90
+ }
91
+
92
+ if (
93
+ stringTag === 'ArrayBuffer' ||
94
+ stringTag === 'SharedArrayBuffer' ||
95
+ stringTag === '[object ArrayBuffer]' ||
96
+ stringTag === '[object SharedArrayBuffer]'
97
+ ) {
98
+ return new Uint8Array(potentialUint8array);
99
+ }
100
+
101
+ throw new BSONError(`Cannot make a Uint8Array from ${String(potentialUint8array)}`);
102
+ },
103
+
104
+ allocate(size: number): Uint8Array {
105
+ if (typeof size !== 'number') {
106
+ throw new TypeError(`The "size" argument must be of type number. Received ${String(size)}`);
107
+ }
108
+ return new Uint8Array(size);
109
+ },
110
+
111
+ equals(a: Uint8Array, b: Uint8Array): boolean {
112
+ if (a.byteLength !== b.byteLength) {
113
+ return false;
114
+ }
115
+ for (let i = 0; i < a.byteLength; i++) {
116
+ if (a[i] !== b[i]) {
117
+ return false;
118
+ }
119
+ }
120
+ return true;
121
+ },
122
+
123
+ fromNumberArray(array: number[]): Uint8Array {
124
+ return Uint8Array.from(array);
125
+ },
126
+
127
+ fromBase64(base64: string): Uint8Array {
128
+ return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
129
+ },
130
+
131
+ toBase64(uint8array: Uint8Array): string {
132
+ return btoa(webByteUtils.toISO88591(uint8array));
133
+ },
134
+
135
+ /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
136
+ fromISO88591(codePoints: string): Uint8Array {
137
+ return Uint8Array.from(codePoints, c => c.charCodeAt(0) & 0xff);
138
+ },
139
+
140
+ /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
141
+ toISO88591(uint8array: Uint8Array): string {
142
+ return Array.from(Uint16Array.from(uint8array), b => String.fromCharCode(b)).join('');
143
+ },
144
+
145
+ fromHex(hex: string): Uint8Array {
146
+ const evenLengthHex = hex.length % 2 === 0 ? hex : hex.slice(0, hex.length - 1);
147
+ const buffer = [];
148
+
149
+ for (let i = 0; i < evenLengthHex.length; i += 2) {
150
+ const firstDigit = evenLengthHex[i];
151
+ const secondDigit = evenLengthHex[i + 1];
152
+
153
+ if (!HEX_DIGIT.test(firstDigit)) {
154
+ break;
155
+ }
156
+ if (!HEX_DIGIT.test(secondDigit)) {
157
+ break;
158
+ }
159
+
160
+ const hexDigit = Number.parseInt(`${firstDigit}${secondDigit}`, 16);
161
+ buffer.push(hexDigit);
162
+ }
163
+
164
+ return Uint8Array.from(buffer);
165
+ },
166
+
167
+ toHex(uint8array: Uint8Array): string {
168
+ return Array.from(uint8array, byte => byte.toString(16).padStart(2, '0')).join('');
169
+ },
170
+
171
+ fromUTF8(text: string): Uint8Array {
172
+ return new TextEncoder().encode(text);
173
+ },
174
+
175
+ toUTF8(uint8array: Uint8Array): string {
176
+ return new TextDecoder('utf8', { fatal: false }).decode(uint8array);
177
+ },
178
+
179
+ utf8ByteLength(input: string): number {
180
+ return webByteUtils.fromUTF8(input).byteLength;
181
+ },
182
+
183
+ encodeUTF8Into(buffer: Uint8Array, source: string, byteOffset: number): number {
184
+ const bytes = webByteUtils.fromUTF8(source);
185
+ buffer.set(bytes, byteOffset);
186
+ return bytes.byteLength;
187
+ },
188
+
189
+ randomBytes: webRandomBytes
190
+ };
package/src/uuid_utils.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Buffer } from 'buffer';
2
1
  import { BSONTypeError } from './error';
2
+ import { ByteUtils } from './utils/byte_utils';
3
3
 
4
4
  // Validation regex for v4 uuid (validates with or without dashes)
5
5
  const VALIDATION_REGEX =
@@ -8,7 +8,7 @@ const VALIDATION_REGEX =
8
8
  export const uuidValidateString = (str: string): boolean =>
9
9
  typeof str === 'string' && VALIDATION_REGEX.test(str);
10
10
 
11
- export const uuidHexStringToBuffer = (hexString: string): Buffer => {
11
+ export const uuidHexStringToBuffer = (hexString: string): Uint8Array => {
12
12
  if (!uuidValidateString(hexString)) {
13
13
  throw new BSONTypeError(
14
14
  'UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'
@@ -16,18 +16,18 @@ export const uuidHexStringToBuffer = (hexString: string): Buffer => {
16
16
  }
17
17
 
18
18
  const sanitizedHexString = hexString.replace(/-/g, '');
19
- return Buffer.from(sanitizedHexString, 'hex');
19
+ return ByteUtils.fromHex(sanitizedHexString);
20
20
  };
21
21
 
22
- export const bufferToUuidHexString = (buffer: Buffer, includeDashes = true): string =>
23
- includeDashes
24
- ? buffer.toString('hex', 0, 4) +
25
- '-' +
26
- buffer.toString('hex', 4, 6) +
27
- '-' +
28
- buffer.toString('hex', 6, 8) +
29
- '-' +
30
- buffer.toString('hex', 8, 10) +
31
- '-' +
32
- buffer.toString('hex', 10, 16)
33
- : buffer.toString('hex');
22
+ export function bufferToUuidHexString(buffer: Uint8Array, includeDashes = true): string {
23
+ if (includeDashes) {
24
+ return [
25
+ ByteUtils.toHex(buffer.subarray(0, 4)),
26
+ ByteUtils.toHex(buffer.subarray(4, 6)),
27
+ ByteUtils.toHex(buffer.subarray(6, 8)),
28
+ ByteUtils.toHex(buffer.subarray(8, 10)),
29
+ ByteUtils.toHex(buffer.subarray(10, 16))
30
+ ].join('-');
31
+ }
32
+ return ByteUtils.toHex(buffer);
33
+ }
package/bower.json DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "name": "bson",
3
- "description": "A bson parser for node.js and the browser",
4
- "keywords": [
5
- "mongodb",
6
- "bson",
7
- "parser"
8
- ],
9
- "author": "Christian Amor Kvalheim <christkv@gmail.com>",
10
- "main": "./dist/bson.js",
11
- "license": "Apache-2.0",
12
- "moduleType": [
13
- "globals",
14
- "node"
15
- ],
16
- "ignore": [
17
- "**/.*",
18
- "alternate_parsers",
19
- "benchmarks",
20
- "bower_components",
21
- "node_modules",
22
- "test",
23
- "tools"
24
- ],
25
- "version": "4.7.0"
26
- }