customized-fabric 1.0.1 → 1.0.3

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.
@@ -1,107 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BSONType = exports.BSON_BINARY_SUBTYPE_USER_DEFINED = exports.BSON_BINARY_SUBTYPE_COLUMN = exports.BSON_BINARY_SUBTYPE_ENCRYPTED = exports.BSON_BINARY_SUBTYPE_MD5 = exports.BSON_BINARY_SUBTYPE_UUID_NEW = exports.BSON_BINARY_SUBTYPE_UUID = exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = exports.BSON_BINARY_SUBTYPE_FUNCTION = exports.BSON_BINARY_SUBTYPE_DEFAULT = exports.BSON_DATA_MAX_KEY = exports.BSON_DATA_MIN_KEY = exports.BSON_DATA_DECIMAL128 = exports.BSON_DATA_LONG = exports.BSON_DATA_TIMESTAMP = exports.BSON_DATA_INT = exports.BSON_DATA_CODE_W_SCOPE = exports.BSON_DATA_SYMBOL = exports.BSON_DATA_CODE = exports.BSON_DATA_DBPOINTER = exports.BSON_DATA_REGEXP = exports.BSON_DATA_NULL = exports.BSON_DATA_DATE = exports.BSON_DATA_BOOLEAN = exports.BSON_DATA_OID = exports.BSON_DATA_UNDEFINED = exports.BSON_DATA_BINARY = exports.BSON_DATA_ARRAY = exports.BSON_DATA_OBJECT = exports.BSON_DATA_STRING = exports.BSON_DATA_NUMBER = exports.JS_INT_MIN = exports.JS_INT_MAX = exports.BSON_INT64_MIN = exports.BSON_INT64_MAX = exports.BSON_INT32_MIN = exports.BSON_INT32_MAX = exports.BSON_MAJOR_VERSION = void 0;
4
- /** @internal */
5
- exports.BSON_MAJOR_VERSION = 5;
6
- /** @internal */
7
- exports.BSON_INT32_MAX = 0x7fffffff;
8
- /** @internal */
9
- exports.BSON_INT32_MIN = -0x80000000;
10
- /** @internal */
11
- exports.BSON_INT64_MAX = Math.pow(2, 63) - 1;
12
- /** @internal */
13
- exports.BSON_INT64_MIN = -Math.pow(2, 63);
14
- /**
15
- * Any integer up to 2^53 can be precisely represented by a double.
16
- * @internal
17
- */
18
- exports.JS_INT_MAX = Math.pow(2, 53);
19
- /**
20
- * Any integer down to -2^53 can be precisely represented by a double.
21
- * @internal
22
- */
23
- exports.JS_INT_MIN = -Math.pow(2, 53);
24
- /** Number BSON Type @internal */
25
- exports.BSON_DATA_NUMBER = 1;
26
- /** String BSON Type @internal */
27
- exports.BSON_DATA_STRING = 2;
28
- /** Object BSON Type @internal */
29
- exports.BSON_DATA_OBJECT = 3;
30
- /** Array BSON Type @internal */
31
- exports.BSON_DATA_ARRAY = 4;
32
- /** Binary BSON Type @internal */
33
- exports.BSON_DATA_BINARY = 5;
34
- /** Binary BSON Type @internal */
35
- exports.BSON_DATA_UNDEFINED = 6;
36
- /** ObjectId BSON Type @internal */
37
- exports.BSON_DATA_OID = 7;
38
- /** Boolean BSON Type @internal */
39
- exports.BSON_DATA_BOOLEAN = 8;
40
- /** Date BSON Type @internal */
41
- exports.BSON_DATA_DATE = 9;
42
- /** null BSON Type @internal */
43
- exports.BSON_DATA_NULL = 10;
44
- /** RegExp BSON Type @internal */
45
- exports.BSON_DATA_REGEXP = 11;
46
- /** Code BSON Type @internal */
47
- exports.BSON_DATA_DBPOINTER = 12;
48
- /** Code BSON Type @internal */
49
- exports.BSON_DATA_CODE = 13;
50
- /** Symbol BSON Type @internal */
51
- exports.BSON_DATA_SYMBOL = 14;
52
- /** Code with Scope BSON Type @internal */
53
- exports.BSON_DATA_CODE_W_SCOPE = 15;
54
- /** 32 bit Integer BSON Type @internal */
55
- exports.BSON_DATA_INT = 16;
56
- /** Timestamp BSON Type @internal */
57
- exports.BSON_DATA_TIMESTAMP = 17;
58
- /** Long BSON Type @internal */
59
- exports.BSON_DATA_LONG = 18;
60
- /** Decimal128 BSON Type @internal */
61
- exports.BSON_DATA_DECIMAL128 = 19;
62
- /** MinKey BSON Type @internal */
63
- exports.BSON_DATA_MIN_KEY = 0xff;
64
- /** MaxKey BSON Type @internal */
65
- exports.BSON_DATA_MAX_KEY = 0x7f;
66
- /** Binary Default Type @internal */
67
- exports.BSON_BINARY_SUBTYPE_DEFAULT = 0;
68
- /** Binary Function Type @internal */
69
- exports.BSON_BINARY_SUBTYPE_FUNCTION = 1;
70
- /** Binary Byte Array Type @internal */
71
- exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
72
- /** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
73
- exports.BSON_BINARY_SUBTYPE_UUID = 3;
74
- /** Binary UUID Type @internal */
75
- exports.BSON_BINARY_SUBTYPE_UUID_NEW = 4;
76
- /** Binary MD5 Type @internal */
77
- exports.BSON_BINARY_SUBTYPE_MD5 = 5;
78
- /** Encrypted BSON type @internal */
79
- exports.BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
80
- /** Column BSON type @internal */
81
- exports.BSON_BINARY_SUBTYPE_COLUMN = 7;
82
- /** Binary User Defined Type @internal */
83
- exports.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
84
- /** @public */
85
- exports.BSONType = Object.freeze({
86
- double: 1,
87
- string: 2,
88
- object: 3,
89
- array: 4,
90
- binData: 5,
91
- undefined: 6,
92
- objectId: 7,
93
- bool: 8,
94
- date: 9,
95
- null: 10,
96
- regex: 11,
97
- dbPointer: 12,
98
- javascript: 13,
99
- symbol: 14,
100
- javascriptWithScope: 15,
101
- int: 16,
102
- timestamp: 17,
103
- long: 18,
104
- decimal: 19,
105
- minKey: -1,
106
- maxKey: 127,
107
- });
@@ -1,79 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BSONRuntimeError = exports.BSONVersionError = exports.BSONError = void 0;
4
- const constants_1 = require("./constants");
5
- /**
6
- * @public
7
- * @category Error
8
- *
9
- * `BSONError` objects are thrown when BSON ecounters an error.
10
- *
11
- * This is the parent class for all the other errors thrown by this library.
12
- */
13
- class BSONError extends Error {
14
- /**
15
- * @internal
16
- * The underlying algorithm for isBSONError may change to improve how strict it is
17
- * about determining if an input is a BSONError. But it must remain backwards compatible
18
- * with previous minors & patches of the current major version.
19
- */
20
- get bsonError() {
21
- return true;
22
- }
23
- get name() {
24
- return "BSONError";
25
- }
26
- constructor(message) {
27
- super(message);
28
- }
29
- /**
30
- * @public
31
- *
32
- * All errors thrown from the BSON library inherit from `BSONError`.
33
- * This method can assist with determining if an error originates from the BSON library
34
- * even if it does not pass an `instanceof` check against this class' constructor.
35
- *
36
- * @param value - any javascript value that needs type checking
37
- */
38
- static isBSONError(value) {
39
- return (value != null &&
40
- typeof value === "object" &&
41
- "bsonError" in value &&
42
- value.bsonError === true &&
43
- // Do not access the following properties, just check existence
44
- "name" in value &&
45
- "message" in value &&
46
- "stack" in value);
47
- }
48
- }
49
- exports.BSONError = BSONError;
50
- /**
51
- * @public
52
- * @category Error
53
- */
54
- class BSONVersionError extends BSONError {
55
- get name() {
56
- return "BSONVersionError";
57
- }
58
- constructor() {
59
- super(`Unsupported BSON version, bson types must be from bson ${constants_1.BSON_MAJOR_VERSION}.0 or later`);
60
- }
61
- }
62
- exports.BSONVersionError = BSONVersionError;
63
- /**
64
- * @public
65
- * @category Error
66
- *
67
- * An error generated when BSON functions encounter an unexpected input
68
- * or reaches an unexpected/invalid internal state
69
- *
70
- */
71
- class BSONRuntimeError extends BSONError {
72
- get name() {
73
- return "BSONRuntimeError";
74
- }
75
- constructor(message) {
76
- super(message);
77
- }
78
- }
79
- exports.BSONRuntimeError = BSONRuntimeError;
@@ -1,281 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ObjectId = void 0;
4
- const bson_value_1 = require("./bson_value");
5
- const error_1 = require("./error");
6
- const utils_1 = require("./parser/utils");
7
- const byte_utils_1 = require("./utils/byte_utils");
8
- // Regular expression that checks for hex value
9
- const checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
10
- // Unique sequence for the current process (initialized on first use)
11
- let PROCESS_UNIQUE = null;
12
- const kId = Symbol("id");
13
- /**
14
- * A class representation of the BSON ObjectId type.
15
- * @public
16
- * @category BSONType
17
- */
18
- class ObjectId extends bson_value_1.BSONValue {
19
- get _bsontype() {
20
- return "ObjectId";
21
- }
22
- /**
23
- * Create an ObjectId type
24
- *
25
- * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
26
- */
27
- constructor(inputId) {
28
- super();
29
- // workingId is set based on type of input and whether valid id exists for the input
30
- let workingId;
31
- if (typeof inputId === "object" && inputId && "id" in inputId) {
32
- if (typeof inputId.id !== "string" && !ArrayBuffer.isView(inputId.id)) {
33
- throw new error_1.BSONError("Argument passed in must have an id that is of type string or Buffer");
34
- }
35
- if ("toHexString" in inputId &&
36
- typeof inputId.toHexString === "function") {
37
- workingId = byte_utils_1.ByteUtils.fromHex(inputId.toHexString());
38
- }
39
- else {
40
- workingId = inputId.id;
41
- }
42
- }
43
- else {
44
- workingId = inputId;
45
- }
46
- // the following cases use workingId to construct an ObjectId
47
- if (workingId == null || typeof workingId === "number") {
48
- // The most common use case (blank id, new objectId instance)
49
- // Generate a new id
50
- this[kId] = ObjectId.generate(typeof workingId === "number" ? workingId : undefined);
51
- }
52
- else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
53
- // If intstanceof matches we can escape calling ensure buffer in Node.js environments
54
- this[kId] = byte_utils_1.ByteUtils.toLocalBufferType(workingId);
55
- }
56
- else if (typeof workingId === "string") {
57
- if (workingId.length === 12) {
58
- // TODO(NODE-4361): Remove string of length 12 support
59
- const bytes = byte_utils_1.ByteUtils.fromUTF8(workingId);
60
- if (bytes.byteLength === 12) {
61
- this[kId] = bytes;
62
- }
63
- else {
64
- throw new error_1.BSONError("Argument passed in must be a string of 12 bytes");
65
- }
66
- }
67
- else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
68
- this[kId] = byte_utils_1.ByteUtils.fromHex(workingId);
69
- }
70
- else {
71
- throw new error_1.BSONError("Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer");
72
- }
73
- }
74
- else {
75
- throw new error_1.BSONError("Argument passed in does not match the accepted types");
76
- }
77
- // If we are caching the hex string
78
- if (ObjectId.cacheHexString) {
79
- this.__id = byte_utils_1.ByteUtils.toHex(this.id);
80
- }
81
- }
82
- /**
83
- * The ObjectId bytes
84
- * @readonly
85
- */
86
- get id() {
87
- return this[kId];
88
- }
89
- set id(value) {
90
- this[kId] = value;
91
- if (ObjectId.cacheHexString) {
92
- this.__id = byte_utils_1.ByteUtils.toHex(value);
93
- }
94
- }
95
- /** Returns the ObjectId id as a 24 character hex string representation */
96
- toHexString() {
97
- if (ObjectId.cacheHexString && this.__id) {
98
- return this.__id;
99
- }
100
- const hexString = byte_utils_1.ByteUtils.toHex(this.id);
101
- if (ObjectId.cacheHexString && !this.__id) {
102
- this.__id = hexString;
103
- }
104
- return hexString;
105
- }
106
- /**
107
- * Update the ObjectId index
108
- * @internal
109
- */
110
- static getInc() {
111
- return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
112
- }
113
- /**
114
- * Generate a 12 byte id buffer used in ObjectId's
115
- *
116
- * @param time - pass in a second based timestamp.
117
- */
118
- static generate(time) {
119
- if ("number" !== typeof time) {
120
- time = Math.floor(Date.now() / 1000);
121
- }
122
- const inc = ObjectId.getInc();
123
- const buffer = byte_utils_1.ByteUtils.allocate(12);
124
- // 4-byte timestamp
125
- byte_utils_1.BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
126
- // set PROCESS_UNIQUE if yet not initialized
127
- if (PROCESS_UNIQUE === null) {
128
- PROCESS_UNIQUE = byte_utils_1.ByteUtils.randomBytes(5);
129
- }
130
- // 5-byte process unique
131
- buffer[4] = PROCESS_UNIQUE[0];
132
- buffer[5] = PROCESS_UNIQUE[1];
133
- buffer[6] = PROCESS_UNIQUE[2];
134
- buffer[7] = PROCESS_UNIQUE[3];
135
- buffer[8] = PROCESS_UNIQUE[4];
136
- // 3-byte counter
137
- buffer[11] = inc & 0xff;
138
- buffer[10] = (inc >> 8) & 0xff;
139
- buffer[9] = (inc >> 16) & 0xff;
140
- return buffer;
141
- }
142
- /**
143
- * Converts the id into a 24 character hex string for printing, unless encoding is provided.
144
- * @param encoding - hex or base64
145
- */
146
- toString(encoding) {
147
- // Is the id a buffer then use the buffer toString method to return the format
148
- if (encoding === "base64")
149
- return byte_utils_1.ByteUtils.toBase64(this.id);
150
- if (encoding === "hex")
151
- return this.toHexString();
152
- return this.toHexString();
153
- }
154
- /** Converts to its JSON the 24 character hex string representation. */
155
- toJSON() {
156
- return this.toHexString();
157
- }
158
- /**
159
- * Compares the equality of this ObjectId with `otherID`.
160
- *
161
- * @param otherId - ObjectId instance to compare against.
162
- */
163
- equals(otherId) {
164
- if (otherId === undefined || otherId === null) {
165
- return false;
166
- }
167
- if (otherId instanceof ObjectId) {
168
- return (this[kId][11] === otherId[kId][11] &&
169
- byte_utils_1.ByteUtils.equals(this[kId], otherId[kId]));
170
- }
171
- if (typeof otherId === "string" &&
172
- ObjectId.isValid(otherId) &&
173
- otherId.length === 12 &&
174
- (0, utils_1.isUint8Array)(this.id)) {
175
- return byte_utils_1.ByteUtils.equals(this.id, byte_utils_1.ByteUtils.fromISO88591(otherId));
176
- }
177
- if (typeof otherId === "string" &&
178
- ObjectId.isValid(otherId) &&
179
- otherId.length === 24) {
180
- return otherId.toLowerCase() === this.toHexString();
181
- }
182
- if (typeof otherId === "string" &&
183
- ObjectId.isValid(otherId) &&
184
- otherId.length === 12) {
185
- return byte_utils_1.ByteUtils.equals(byte_utils_1.ByteUtils.fromUTF8(otherId), this.id);
186
- }
187
- if (typeof otherId === "object" &&
188
- "toHexString" in otherId &&
189
- typeof otherId.toHexString === "function") {
190
- const otherIdString = otherId.toHexString();
191
- const thisIdString = this.toHexString().toLowerCase();
192
- return (typeof otherIdString === "string" &&
193
- otherIdString.toLowerCase() === thisIdString);
194
- }
195
- return false;
196
- }
197
- /** Returns the generation date (accurate up to the second) that this ID was generated. */
198
- getTimestamp() {
199
- const timestamp = new Date();
200
- const time = byte_utils_1.BSONDataView.fromUint8Array(this.id).getUint32(0, false);
201
- timestamp.setTime(Math.floor(time) * 1000);
202
- return timestamp;
203
- }
204
- /** @internal */
205
- static createPk() {
206
- return new ObjectId();
207
- }
208
- /**
209
- * Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
210
- *
211
- * @param time - an integer number representing a number of seconds.
212
- */
213
- static createFromTime(time) {
214
- const buffer = byte_utils_1.ByteUtils.fromNumberArray([
215
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216
- ]);
217
- // Encode time into first 4 bytes
218
- byte_utils_1.BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
219
- // Return the new objectId
220
- return new ObjectId(buffer);
221
- }
222
- /**
223
- * Creates an ObjectId from a hex string representation of an ObjectId.
224
- *
225
- * @param hexString - create a ObjectId from a passed in 24 character hexstring.
226
- */
227
- static createFromHexString(hexString) {
228
- if (hexString?.length !== 24) {
229
- throw new error_1.BSONError("hex string must be 24 characters");
230
- }
231
- return new ObjectId(byte_utils_1.ByteUtils.fromHex(hexString));
232
- }
233
- /** Creates an ObjectId instance from a base64 string */
234
- static createFromBase64(base64) {
235
- if (base64?.length !== 16) {
236
- throw new error_1.BSONError("base64 string must be 16 characters");
237
- }
238
- return new ObjectId(byte_utils_1.ByteUtils.fromBase64(base64));
239
- }
240
- /**
241
- * Checks if a value is a valid bson ObjectId
242
- *
243
- * @param id - ObjectId instance to validate.
244
- */
245
- static isValid(id) {
246
- if (id == null)
247
- return false;
248
- try {
249
- new ObjectId(id);
250
- return true;
251
- }
252
- catch {
253
- return false;
254
- }
255
- }
256
- /** @internal */
257
- toExtendedJSON() {
258
- if (this.toHexString)
259
- return { $oid: this.toHexString() };
260
- return { $oid: this.toString("hex") };
261
- }
262
- /** @internal */
263
- static fromExtendedJSON(doc) {
264
- return new ObjectId(doc.$oid);
265
- }
266
- /**
267
- * Converts to a string representation of this Id.
268
- *
269
- * @returns return the 24 character hex string representation.
270
- * @internal
271
- */
272
- [Symbol.for("nodejs.util.inspect.custom")]() {
273
- return this.inspect();
274
- }
275
- inspect() {
276
- return `new ObjectId("${this.toHexString()}")`;
277
- }
278
- }
279
- exports.ObjectId = ObjectId;
280
- /** @internal */
281
- ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isDate = exports.isMap = exports.isRegExp = exports.isBigUInt64Array = exports.isBigInt64Array = exports.isUint8Array = exports.isAnyArrayBuffer = void 0;
4
- function isAnyArrayBuffer(value) {
5
- return ["[object ArrayBuffer]", "[object SharedArrayBuffer]"].includes(Object.prototype.toString.call(value));
6
- }
7
- exports.isAnyArrayBuffer = isAnyArrayBuffer;
8
- function isUint8Array(value) {
9
- return Object.prototype.toString.call(value) === "[object Uint8Array]";
10
- }
11
- exports.isUint8Array = isUint8Array;
12
- function isBigInt64Array(value) {
13
- return Object.prototype.toString.call(value) === "[object BigInt64Array]";
14
- }
15
- exports.isBigInt64Array = isBigInt64Array;
16
- function isBigUInt64Array(value) {
17
- return Object.prototype.toString.call(value) === "[object BigUint64Array]";
18
- }
19
- exports.isBigUInt64Array = isBigUInt64Array;
20
- function isRegExp(d) {
21
- return Object.prototype.toString.call(d) === "[object RegExp]";
22
- }
23
- exports.isRegExp = isRegExp;
24
- function isMap(d) {
25
- return Object.prototype.toString.call(d) === "[object Map]";
26
- }
27
- exports.isMap = isMap;
28
- function isDate(d) {
29
- return Object.prototype.toString.call(d) === "[object Date]";
30
- }
31
- exports.isDate = isDate;
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BSONDataView = exports.ByteUtils = void 0;
4
- const node_byte_utils_1 = require("./node_byte_utils");
5
- const web_byte_utils_1 = require("./web_byte_utils");
6
- /**
7
- * Check that a global Buffer exists that is a function and
8
- * does not have a '_isBuffer' property defined on the prototype
9
- * (this is to prevent using the npm buffer)
10
- */
11
- const hasGlobalBuffer = typeof Buffer === "function" && Buffer.prototype?._isBuffer !== true;
12
- /**
13
- * This is the only ByteUtils that should be used across the rest of the BSON library.
14
- *
15
- * The type annotation is important here, it asserts that each of the platform specific
16
- * utils implementations are compatible with the common one.
17
- *
18
- * @internal
19
- */
20
- exports.ByteUtils = hasGlobalBuffer
21
- ? node_byte_utils_1.nodeJsByteUtils
22
- : web_byte_utils_1.webByteUtils;
23
- class BSONDataView extends DataView {
24
- static fromUint8Array(input) {
25
- return new DataView(input.buffer, input.byteOffset, input.byteLength);
26
- }
27
- }
28
- exports.BSONDataView = BSONDataView;
@@ -1,98 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.nodeJsByteUtils = exports.nodejsMathRandomBytes = void 0;
4
- const error_1 = require("../error");
5
- /** @internal */
6
- function nodejsMathRandomBytes(byteLength) {
7
- return exports.nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
8
- }
9
- exports.nodejsMathRandomBytes = nodejsMathRandomBytes;
10
- /**
11
- * @internal
12
- * WARNING: REQUIRE WILL BE REWRITTEN
13
- *
14
- * This code is carefully used by require_rewriter.mjs any modifications must be reflected in the plugin.
15
- *
16
- * @remarks
17
- * "crypto" is the only dependency BSON needs. This presents a problem for creating a bundle of the BSON library
18
- * in an es module format that can be used both on the browser and in Node.js. In Node.js when BSON is imported as
19
- * an es module, there will be no global require function defined, making the code below fallback to the much less desireable math.random bytes.
20
- * In order to make our es module bundle work as expected on Node.js we need to change this `require()` to a dynamic import, and the dynamic
21
- * import must be top-level awaited since es modules are async. So we rely on a custom rollup plugin to seek out the following lines of code
22
- * and replace `require` with `await import` and the IIFE line (`nodejsRandomBytes = (() => { ... })()`) with `nodejsRandomBytes = await (async () => { ... })()`
23
- * when generating an es module bundle.
24
- */
25
- const nodejsRandomBytes = (() => {
26
- try {
27
- return require("crypto").randomBytes;
28
- }
29
- catch {
30
- return nodejsMathRandomBytes;
31
- }
32
- })();
33
- /** @internal */
34
- exports.nodeJsByteUtils = {
35
- toLocalBufferType(potentialBuffer) {
36
- if (Buffer.isBuffer(potentialBuffer)) {
37
- return potentialBuffer;
38
- }
39
- if (ArrayBuffer.isView(potentialBuffer)) {
40
- return Buffer.from(potentialBuffer.buffer, potentialBuffer.byteOffset, potentialBuffer.byteLength);
41
- }
42
- const stringTag = potentialBuffer?.[Symbol.toStringTag] ??
43
- Object.prototype.toString.call(potentialBuffer);
44
- if (stringTag === "ArrayBuffer" ||
45
- stringTag === "SharedArrayBuffer" ||
46
- stringTag === "[object ArrayBuffer]" ||
47
- stringTag === "[object SharedArrayBuffer]") {
48
- return Buffer.from(potentialBuffer);
49
- }
50
- throw new error_1.BSONError(`Cannot create Buffer from ${String(potentialBuffer)}`);
51
- },
52
- allocate(size) {
53
- return Buffer.alloc(size);
54
- },
55
- equals(a, b) {
56
- return exports.nodeJsByteUtils.toLocalBufferType(a).equals(b);
57
- },
58
- fromNumberArray(array) {
59
- return Buffer.from(array);
60
- },
61
- fromBase64(base64) {
62
- return Buffer.from(base64, "base64");
63
- },
64
- toBase64(buffer) {
65
- return exports.nodeJsByteUtils.toLocalBufferType(buffer).toString("base64");
66
- },
67
- /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
68
- fromISO88591(codePoints) {
69
- return Buffer.from(codePoints, "binary");
70
- },
71
- /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
72
- toISO88591(buffer) {
73
- return exports.nodeJsByteUtils.toLocalBufferType(buffer).toString("binary");
74
- },
75
- fromHex(hex) {
76
- return Buffer.from(hex, "hex");
77
- },
78
- toHex(buffer) {
79
- return exports.nodeJsByteUtils.toLocalBufferType(buffer).toString("hex");
80
- },
81
- fromUTF8(text) {
82
- return Buffer.from(text, "utf8");
83
- },
84
- toUTF8(buffer, start, end) {
85
- return exports.nodeJsByteUtils
86
- .toLocalBufferType(buffer)
87
- .toString("utf8", start, end);
88
- },
89
- utf8ByteLength(input) {
90
- return Buffer.byteLength(input, "utf8");
91
- },
92
- encodeUTF8Into(buffer, source, byteOffset) {
93
- return exports.nodeJsByteUtils
94
- .toLocalBufferType(buffer)
95
- .write(source, byteOffset, undefined, "utf8");
96
- },
97
- randomBytes: nodejsRandomBytes,
98
- };