customized-fabric 1.0.0 → 1.0.2
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/{ClipartObject → customizedFabric/ClipartObject}/index.js +1 -1
- package/lib/{ImagePlaceholderObject → customizedFabric/ImagePlaceholderObject}/index.js +1 -1
- package/lib/{TextInputObject → customizedFabric/TextInputObject}/constants.js +1 -0
- package/lib/{TextInputObject → customizedFabric/TextInputObject}/index.js +1 -1
- package/lib/customizedFabric/index.js +54 -0
- package/lib/index.js +1 -36
- package/package.json +4 -1
- package/src/ClipartObject/constants.ts +0 -9
- package/src/ClipartObject/index.ts +0 -156
- package/src/ClipartObject/interfaces.ts +0 -29
- package/src/ImagePlaceholderObject/constants.ts +0 -9
- package/src/ImagePlaceholderObject/index.ts +0 -155
- package/src/ImagePlaceholderObject/interfaces.ts +0 -21
- package/src/TextInputObject/constants.ts +0 -11
- package/src/TextInputObject/index.ts +0 -205
- package/src/TextInputObject/interfaces.ts +0 -40
- package/src/constants.ts +0 -10
- package/src/index.ts +0 -62
- package/src/interfaces.ts +0 -3
- package/src/objectId/bson_value.ts +0 -18
- package/src/objectId/constants.ts +0 -141
- package/src/objectId/error.ts +0 -85
- package/src/objectId/index.ts +0 -354
- package/src/objectId/parser/utils.ts +0 -29
- package/src/objectId/utils/byte_utils.ts +0 -72
- package/src/objectId/utils/node_byte_utils.ts +0 -173
- package/src/objectId/utils/web_byte_utils.ts +0 -212
- package/src/utils.ts +0 -93
- package/tsconfig.json +0 -110
- /package/lib/{ClipartObject → customizedFabric/ClipartObject}/constants.js +0 -0
- /package/lib/{ClipartObject → customizedFabric/ClipartObject}/interfaces.js +0 -0
- /package/lib/{ImagePlaceholderObject → customizedFabric/ImagePlaceholderObject}/constants.js +0 -0
- /package/lib/{ImagePlaceholderObject → customizedFabric/ImagePlaceholderObject}/interfaces.js +0 -0
- /package/lib/{TextInputObject → customizedFabric/TextInputObject}/interfaces.js +0 -0
- /package/lib/{constants.js → customizedFabric/constants.js} +0 -0
- /package/lib/{interfaces.js → customizedFabric/interfaces.js} +0 -0
- /package/lib/{utils.js → customizedFabric/utils.js} +0 -0
- /package/lib/{objectId → utils/objectId}/bson_value.js +0 -0
- /package/lib/{objectId → utils/objectId}/constants.js +0 -0
- /package/lib/{objectId → utils/objectId}/error.js +0 -0
- /package/lib/{objectId → utils/objectId}/index.js +0 -0
- /package/lib/{objectId → utils/objectId}/parser/utils.js +0 -0
- /package/lib/{objectId → utils/objectId}/utils/byte_utils.js +0 -0
- /package/lib/{objectId → utils/objectId}/utils/node_byte_utils.js +0 -0
- /package/lib/{objectId → utils/objectId}/utils/web_byte_utils.js +0 -0
package/src/objectId/index.ts
DELETED
@@ -1,354 +0,0 @@
|
|
1
|
-
import { BSONValue } from "./bson_value";
|
2
|
-
import { BSONError } from "./error";
|
3
|
-
import { isUint8Array } from "./parser/utils";
|
4
|
-
import { BSONDataView, ByteUtils } from "./utils/byte_utils";
|
5
|
-
|
6
|
-
// Regular expression that checks for hex value
|
7
|
-
const checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
|
8
|
-
|
9
|
-
// Unique sequence for the current process (initialized on first use)
|
10
|
-
let PROCESS_UNIQUE: Uint8Array | null = null;
|
11
|
-
|
12
|
-
/** @public */
|
13
|
-
export interface ObjectIdLike {
|
14
|
-
id: string | Uint8Array;
|
15
|
-
__id?: string;
|
16
|
-
toHexString(): string;
|
17
|
-
}
|
18
|
-
|
19
|
-
/** @public */
|
20
|
-
export interface ObjectIdExtended {
|
21
|
-
$oid: string;
|
22
|
-
}
|
23
|
-
|
24
|
-
const kId = Symbol("id");
|
25
|
-
|
26
|
-
/**
|
27
|
-
* A class representation of the BSON ObjectId type.
|
28
|
-
* @public
|
29
|
-
* @category BSONType
|
30
|
-
*/
|
31
|
-
export class ObjectId extends BSONValue {
|
32
|
-
get _bsontype(): "ObjectId" {
|
33
|
-
return "ObjectId";
|
34
|
-
}
|
35
|
-
|
36
|
-
/** @internal */
|
37
|
-
private static index = Math.floor(Math.random() * 0xffffff);
|
38
|
-
|
39
|
-
static cacheHexString: boolean;
|
40
|
-
|
41
|
-
/** ObjectId Bytes @internal */
|
42
|
-
private [kId]!: Uint8Array;
|
43
|
-
/** ObjectId hexString cache @internal */
|
44
|
-
private __id?: string;
|
45
|
-
|
46
|
-
/**
|
47
|
-
* Create an ObjectId type
|
48
|
-
*
|
49
|
-
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
|
50
|
-
*/
|
51
|
-
constructor(
|
52
|
-
inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array
|
53
|
-
) {
|
54
|
-
super();
|
55
|
-
// workingId is set based on type of input and whether valid id exists for the input
|
56
|
-
let workingId;
|
57
|
-
if (typeof inputId === "object" && inputId && "id" in inputId) {
|
58
|
-
if (typeof inputId.id !== "string" && !ArrayBuffer.isView(inputId.id)) {
|
59
|
-
throw new BSONError(
|
60
|
-
"Argument passed in must have an id that is of type string or Buffer"
|
61
|
-
);
|
62
|
-
}
|
63
|
-
if (
|
64
|
-
"toHexString" in inputId &&
|
65
|
-
typeof inputId.toHexString === "function"
|
66
|
-
) {
|
67
|
-
workingId = ByteUtils.fromHex(inputId.toHexString());
|
68
|
-
} else {
|
69
|
-
workingId = inputId.id;
|
70
|
-
}
|
71
|
-
} else {
|
72
|
-
workingId = inputId;
|
73
|
-
}
|
74
|
-
|
75
|
-
// the following cases use workingId to construct an ObjectId
|
76
|
-
if (workingId == null || typeof workingId === "number") {
|
77
|
-
// The most common use case (blank id, new objectId instance)
|
78
|
-
// Generate a new id
|
79
|
-
this[kId] = ObjectId.generate(
|
80
|
-
typeof workingId === "number" ? workingId : undefined
|
81
|
-
);
|
82
|
-
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
|
83
|
-
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
|
84
|
-
this[kId] = ByteUtils.toLocalBufferType(workingId);
|
85
|
-
} else if (typeof workingId === "string") {
|
86
|
-
if (workingId.length === 12) {
|
87
|
-
// TODO(NODE-4361): Remove string of length 12 support
|
88
|
-
const bytes = ByteUtils.fromUTF8(workingId);
|
89
|
-
if (bytes.byteLength === 12) {
|
90
|
-
this[kId] = bytes;
|
91
|
-
} else {
|
92
|
-
throw new BSONError(
|
93
|
-
"Argument passed in must be a string of 12 bytes"
|
94
|
-
);
|
95
|
-
}
|
96
|
-
} else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
|
97
|
-
this[kId] = ByteUtils.fromHex(workingId);
|
98
|
-
} else {
|
99
|
-
throw new BSONError(
|
100
|
-
"Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer"
|
101
|
-
);
|
102
|
-
}
|
103
|
-
} else {
|
104
|
-
throw new BSONError(
|
105
|
-
"Argument passed in does not match the accepted types"
|
106
|
-
);
|
107
|
-
}
|
108
|
-
// If we are caching the hex string
|
109
|
-
if (ObjectId.cacheHexString) {
|
110
|
-
this.__id = ByteUtils.toHex(this.id);
|
111
|
-
}
|
112
|
-
}
|
113
|
-
|
114
|
-
/**
|
115
|
-
* The ObjectId bytes
|
116
|
-
* @readonly
|
117
|
-
*/
|
118
|
-
get id(): Uint8Array {
|
119
|
-
return this[kId];
|
120
|
-
}
|
121
|
-
|
122
|
-
set id(value: Uint8Array) {
|
123
|
-
this[kId] = value;
|
124
|
-
if (ObjectId.cacheHexString) {
|
125
|
-
this.__id = ByteUtils.toHex(value);
|
126
|
-
}
|
127
|
-
}
|
128
|
-
|
129
|
-
/** Returns the ObjectId id as a 24 character hex string representation */
|
130
|
-
toHexString(): string {
|
131
|
-
if (ObjectId.cacheHexString && this.__id) {
|
132
|
-
return this.__id;
|
133
|
-
}
|
134
|
-
|
135
|
-
const hexString = ByteUtils.toHex(this.id);
|
136
|
-
|
137
|
-
if (ObjectId.cacheHexString && !this.__id) {
|
138
|
-
this.__id = hexString;
|
139
|
-
}
|
140
|
-
|
141
|
-
return hexString;
|
142
|
-
}
|
143
|
-
|
144
|
-
/**
|
145
|
-
* Update the ObjectId index
|
146
|
-
* @internal
|
147
|
-
*/
|
148
|
-
private static getInc(): number {
|
149
|
-
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
|
150
|
-
}
|
151
|
-
|
152
|
-
/**
|
153
|
-
* Generate a 12 byte id buffer used in ObjectId's
|
154
|
-
*
|
155
|
-
* @param time - pass in a second based timestamp.
|
156
|
-
*/
|
157
|
-
static generate(time?: number): Uint8Array {
|
158
|
-
if ("number" !== typeof time) {
|
159
|
-
time = Math.floor(Date.now() / 1000);
|
160
|
-
}
|
161
|
-
|
162
|
-
const inc = ObjectId.getInc();
|
163
|
-
const buffer = ByteUtils.allocate(12);
|
164
|
-
|
165
|
-
// 4-byte timestamp
|
166
|
-
BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
|
167
|
-
|
168
|
-
// set PROCESS_UNIQUE if yet not initialized
|
169
|
-
if (PROCESS_UNIQUE === null) {
|
170
|
-
PROCESS_UNIQUE = ByteUtils.randomBytes(5);
|
171
|
-
}
|
172
|
-
|
173
|
-
// 5-byte process unique
|
174
|
-
buffer[4] = PROCESS_UNIQUE[0];
|
175
|
-
buffer[5] = PROCESS_UNIQUE[1];
|
176
|
-
buffer[6] = PROCESS_UNIQUE[2];
|
177
|
-
buffer[7] = PROCESS_UNIQUE[3];
|
178
|
-
buffer[8] = PROCESS_UNIQUE[4];
|
179
|
-
|
180
|
-
// 3-byte counter
|
181
|
-
buffer[11] = inc & 0xff;
|
182
|
-
buffer[10] = (inc >> 8) & 0xff;
|
183
|
-
buffer[9] = (inc >> 16) & 0xff;
|
184
|
-
|
185
|
-
return buffer;
|
186
|
-
}
|
187
|
-
|
188
|
-
/**
|
189
|
-
* Converts the id into a 24 character hex string for printing, unless encoding is provided.
|
190
|
-
* @param encoding - hex or base64
|
191
|
-
*/
|
192
|
-
toString(encoding?: "hex" | "base64"): string {
|
193
|
-
// Is the id a buffer then use the buffer toString method to return the format
|
194
|
-
if (encoding === "base64") return ByteUtils.toBase64(this.id);
|
195
|
-
if (encoding === "hex") return this.toHexString();
|
196
|
-
return this.toHexString();
|
197
|
-
}
|
198
|
-
|
199
|
-
/** Converts to its JSON the 24 character hex string representation. */
|
200
|
-
toJSON(): string {
|
201
|
-
return this.toHexString();
|
202
|
-
}
|
203
|
-
|
204
|
-
/**
|
205
|
-
* Compares the equality of this ObjectId with `otherID`.
|
206
|
-
*
|
207
|
-
* @param otherId - ObjectId instance to compare against.
|
208
|
-
*/
|
209
|
-
equals(otherId: string | ObjectId | ObjectIdLike): boolean {
|
210
|
-
if (otherId === undefined || otherId === null) {
|
211
|
-
return false;
|
212
|
-
}
|
213
|
-
|
214
|
-
if (otherId instanceof ObjectId) {
|
215
|
-
return (
|
216
|
-
this[kId][11] === otherId[kId][11] &&
|
217
|
-
ByteUtils.equals(this[kId], otherId[kId])
|
218
|
-
);
|
219
|
-
}
|
220
|
-
|
221
|
-
if (
|
222
|
-
typeof otherId === "string" &&
|
223
|
-
ObjectId.isValid(otherId) &&
|
224
|
-
otherId.length === 12 &&
|
225
|
-
isUint8Array(this.id)
|
226
|
-
) {
|
227
|
-
return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
|
228
|
-
}
|
229
|
-
|
230
|
-
if (
|
231
|
-
typeof otherId === "string" &&
|
232
|
-
ObjectId.isValid(otherId) &&
|
233
|
-
otherId.length === 24
|
234
|
-
) {
|
235
|
-
return otherId.toLowerCase() === this.toHexString();
|
236
|
-
}
|
237
|
-
|
238
|
-
if (
|
239
|
-
typeof otherId === "string" &&
|
240
|
-
ObjectId.isValid(otherId) &&
|
241
|
-
otherId.length === 12
|
242
|
-
) {
|
243
|
-
return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
|
244
|
-
}
|
245
|
-
|
246
|
-
if (
|
247
|
-
typeof otherId === "object" &&
|
248
|
-
"toHexString" in otherId &&
|
249
|
-
typeof otherId.toHexString === "function"
|
250
|
-
) {
|
251
|
-
const otherIdString = otherId.toHexString();
|
252
|
-
const thisIdString = this.toHexString().toLowerCase();
|
253
|
-
return (
|
254
|
-
typeof otherIdString === "string" &&
|
255
|
-
otherIdString.toLowerCase() === thisIdString
|
256
|
-
);
|
257
|
-
}
|
258
|
-
|
259
|
-
return false;
|
260
|
-
}
|
261
|
-
|
262
|
-
/** Returns the generation date (accurate up to the second) that this ID was generated. */
|
263
|
-
getTimestamp(): Date {
|
264
|
-
const timestamp = new Date();
|
265
|
-
const time = BSONDataView.fromUint8Array(this.id).getUint32(0, false);
|
266
|
-
timestamp.setTime(Math.floor(time) * 1000);
|
267
|
-
return timestamp;
|
268
|
-
}
|
269
|
-
|
270
|
-
/** @internal */
|
271
|
-
static createPk(): ObjectId {
|
272
|
-
return new ObjectId();
|
273
|
-
}
|
274
|
-
|
275
|
-
/**
|
276
|
-
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
|
277
|
-
*
|
278
|
-
* @param time - an integer number representing a number of seconds.
|
279
|
-
*/
|
280
|
-
static createFromTime(time: number): ObjectId {
|
281
|
-
const buffer = ByteUtils.fromNumberArray([
|
282
|
-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
283
|
-
]);
|
284
|
-
// Encode time into first 4 bytes
|
285
|
-
BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
|
286
|
-
// Return the new objectId
|
287
|
-
return new ObjectId(buffer);
|
288
|
-
}
|
289
|
-
|
290
|
-
/**
|
291
|
-
* Creates an ObjectId from a hex string representation of an ObjectId.
|
292
|
-
*
|
293
|
-
* @param hexString - create a ObjectId from a passed in 24 character hexstring.
|
294
|
-
*/
|
295
|
-
static createFromHexString(hexString: string): ObjectId {
|
296
|
-
if (hexString?.length !== 24) {
|
297
|
-
throw new BSONError("hex string must be 24 characters");
|
298
|
-
}
|
299
|
-
|
300
|
-
return new ObjectId(ByteUtils.fromHex(hexString));
|
301
|
-
}
|
302
|
-
|
303
|
-
/** Creates an ObjectId instance from a base64 string */
|
304
|
-
static createFromBase64(base64: string): ObjectId {
|
305
|
-
if (base64?.length !== 16) {
|
306
|
-
throw new BSONError("base64 string must be 16 characters");
|
307
|
-
}
|
308
|
-
|
309
|
-
return new ObjectId(ByteUtils.fromBase64(base64));
|
310
|
-
}
|
311
|
-
|
312
|
-
/**
|
313
|
-
* Checks if a value is a valid bson ObjectId
|
314
|
-
*
|
315
|
-
* @param id - ObjectId instance to validate.
|
316
|
-
*/
|
317
|
-
static isValid(
|
318
|
-
id: string | number | ObjectId | ObjectIdLike | Uint8Array
|
319
|
-
): boolean {
|
320
|
-
if (id == null) return false;
|
321
|
-
|
322
|
-
try {
|
323
|
-
new ObjectId(id);
|
324
|
-
return true;
|
325
|
-
} catch {
|
326
|
-
return false;
|
327
|
-
}
|
328
|
-
}
|
329
|
-
|
330
|
-
/** @internal */
|
331
|
-
toExtendedJSON(): ObjectIdExtended {
|
332
|
-
if (this.toHexString) return { $oid: this.toHexString() };
|
333
|
-
return { $oid: this.toString("hex") };
|
334
|
-
}
|
335
|
-
|
336
|
-
/** @internal */
|
337
|
-
static fromExtendedJSON(doc: ObjectIdExtended): ObjectId {
|
338
|
-
return new ObjectId(doc.$oid);
|
339
|
-
}
|
340
|
-
|
341
|
-
/**
|
342
|
-
* Converts to a string representation of this Id.
|
343
|
-
*
|
344
|
-
* @returns return the 24 character hex string representation.
|
345
|
-
* @internal
|
346
|
-
*/
|
347
|
-
[Symbol.for("nodejs.util.inspect.custom")](): string {
|
348
|
-
return this.inspect();
|
349
|
-
}
|
350
|
-
|
351
|
-
inspect(): string {
|
352
|
-
return `new ObjectId("${this.toHexString()}")`;
|
353
|
-
}
|
354
|
-
}
|
@@ -1,29 +0,0 @@
|
|
1
|
-
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
|
2
|
-
return ["[object ArrayBuffer]", "[object SharedArrayBuffer]"].includes(
|
3
|
-
Object.prototype.toString.call(value)
|
4
|
-
);
|
5
|
-
}
|
6
|
-
|
7
|
-
export function isUint8Array(value: unknown): value is Uint8Array {
|
8
|
-
return Object.prototype.toString.call(value) === "[object Uint8Array]";
|
9
|
-
}
|
10
|
-
|
11
|
-
export function isBigInt64Array(value: unknown): value is BigInt64Array {
|
12
|
-
return Object.prototype.toString.call(value) === "[object BigInt64Array]";
|
13
|
-
}
|
14
|
-
|
15
|
-
export function isBigUInt64Array(value: unknown): value is BigUint64Array {
|
16
|
-
return Object.prototype.toString.call(value) === "[object BigUint64Array]";
|
17
|
-
}
|
18
|
-
|
19
|
-
export function isRegExp(d: unknown): d is RegExp {
|
20
|
-
return Object.prototype.toString.call(d) === "[object RegExp]";
|
21
|
-
}
|
22
|
-
|
23
|
-
export function isMap(d: unknown): d is Map<unknown, unknown> {
|
24
|
-
return Object.prototype.toString.call(d) === "[object Map]";
|
25
|
-
}
|
26
|
-
|
27
|
-
export function isDate(d: unknown): d is Date {
|
28
|
-
return Object.prototype.toString.call(d) === "[object Date]";
|
29
|
-
}
|
@@ -1,72 +0,0 @@
|
|
1
|
-
import { nodeJsByteUtils } from "./node_byte_utils";
|
2
|
-
import { webByteUtils } from "./web_byte_utils";
|
3
|
-
|
4
|
-
/** @internal */
|
5
|
-
export type ByteUtils = {
|
6
|
-
/** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */
|
7
|
-
toLocalBufferType(
|
8
|
-
buffer: Uint8Array | ArrayBufferView | ArrayBuffer
|
9
|
-
): Uint8Array;
|
10
|
-
/** Create empty space of size */
|
11
|
-
allocate: (size: number) => Uint8Array;
|
12
|
-
/** Check if two Uint8Arrays are deep equal */
|
13
|
-
equals: (a: Uint8Array, b: Uint8Array) => boolean;
|
14
|
-
/** Check if two Uint8Arrays are deep equal */
|
15
|
-
fromNumberArray: (array: number[]) => Uint8Array;
|
16
|
-
/** Create a Uint8Array from a base64 string */
|
17
|
-
fromBase64: (base64: string) => Uint8Array;
|
18
|
-
/** Create a base64 string from bytes */
|
19
|
-
toBase64: (buffer: Uint8Array) => string;
|
20
|
-
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
|
21
|
-
fromISO88591: (codePoints: string) => Uint8Array;
|
22
|
-
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
|
23
|
-
toISO88591: (buffer: Uint8Array) => string;
|
24
|
-
/** Create a Uint8Array from a hex string */
|
25
|
-
fromHex: (hex: string) => Uint8Array;
|
26
|
-
/** Create a hex string from bytes */
|
27
|
-
toHex: (buffer: Uint8Array) => string;
|
28
|
-
/** Create a Uint8Array containing utf8 code units from a string */
|
29
|
-
fromUTF8: (text: string) => Uint8Array;
|
30
|
-
/** Create a string from utf8 code units */
|
31
|
-
toUTF8: (buffer: Uint8Array, start: number, end: number) => string;
|
32
|
-
/** Get the utf8 code unit count from a string if it were to be transformed to utf8 */
|
33
|
-
utf8ByteLength: (input: string) => number;
|
34
|
-
/** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */
|
35
|
-
encodeUTF8Into(
|
36
|
-
destination: Uint8Array,
|
37
|
-
source: string,
|
38
|
-
byteOffset: number
|
39
|
-
): number;
|
40
|
-
/** Generate a Uint8Array filled with random bytes with byteLength */
|
41
|
-
randomBytes(byteLength: number): Uint8Array;
|
42
|
-
};
|
43
|
-
|
44
|
-
declare const Buffer:
|
45
|
-
| { new (): unknown; prototype?: { _isBuffer?: boolean } }
|
46
|
-
| undefined;
|
47
|
-
|
48
|
-
/**
|
49
|
-
* Check that a global Buffer exists that is a function and
|
50
|
-
* does not have a '_isBuffer' property defined on the prototype
|
51
|
-
* (this is to prevent using the npm buffer)
|
52
|
-
*/
|
53
|
-
const hasGlobalBuffer =
|
54
|
-
typeof Buffer === "function" && Buffer.prototype?._isBuffer !== true;
|
55
|
-
|
56
|
-
/**
|
57
|
-
* This is the only ByteUtils that should be used across the rest of the BSON library.
|
58
|
-
*
|
59
|
-
* The type annotation is important here, it asserts that each of the platform specific
|
60
|
-
* utils implementations are compatible with the common one.
|
61
|
-
*
|
62
|
-
* @internal
|
63
|
-
*/
|
64
|
-
export const ByteUtils: ByteUtils = hasGlobalBuffer
|
65
|
-
? nodeJsByteUtils
|
66
|
-
: webByteUtils;
|
67
|
-
|
68
|
-
export class BSONDataView extends DataView {
|
69
|
-
static fromUint8Array(input: Uint8Array) {
|
70
|
-
return new DataView(input.buffer, input.byteOffset, input.byteLength);
|
71
|
-
}
|
72
|
-
}
|
@@ -1,173 +0,0 @@
|
|
1
|
-
import { BSONError } from "../error";
|
2
|
-
|
3
|
-
type NodeJsEncoding = "base64" | "hex" | "utf8" | "binary";
|
4
|
-
type NodeJsBuffer = ArrayBufferView &
|
5
|
-
Uint8Array & {
|
6
|
-
write(
|
7
|
-
string: string,
|
8
|
-
offset: number,
|
9
|
-
length: undefined,
|
10
|
-
encoding: "utf8"
|
11
|
-
): number;
|
12
|
-
copy(
|
13
|
-
target: Uint8Array,
|
14
|
-
targetStart: number,
|
15
|
-
sourceStart: number,
|
16
|
-
sourceEnd: number
|
17
|
-
): number;
|
18
|
-
toString: (
|
19
|
-
this: Uint8Array,
|
20
|
-
encoding: NodeJsEncoding,
|
21
|
-
start?: number,
|
22
|
-
end?: number
|
23
|
-
) => string;
|
24
|
-
equals: (this: Uint8Array, other: Uint8Array) => boolean;
|
25
|
-
};
|
26
|
-
type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, "from"> & {
|
27
|
-
alloc: (size: number) => NodeJsBuffer;
|
28
|
-
from(array: number[]): NodeJsBuffer;
|
29
|
-
from(array: Uint8Array): NodeJsBuffer;
|
30
|
-
from(array: ArrayBuffer): NodeJsBuffer;
|
31
|
-
from(
|
32
|
-
array: ArrayBuffer,
|
33
|
-
byteOffset: number,
|
34
|
-
byteLength: number
|
35
|
-
): NodeJsBuffer;
|
36
|
-
from(base64: string, encoding: NodeJsEncoding): NodeJsBuffer;
|
37
|
-
byteLength(input: string, encoding: "utf8"): number;
|
38
|
-
isBuffer(value: unknown): value is NodeJsBuffer;
|
39
|
-
};
|
40
|
-
|
41
|
-
// This can be nullish, but we gate the nodejs functions on being exported whether or not this exists
|
42
|
-
// Node.js global
|
43
|
-
declare const Buffer: NodeJsBufferConstructor;
|
44
|
-
declare const require: (mod: "crypto") => {
|
45
|
-
randomBytes: (byteLength: number) => Uint8Array;
|
46
|
-
};
|
47
|
-
|
48
|
-
/** @internal */
|
49
|
-
export function nodejsMathRandomBytes(byteLength: number) {
|
50
|
-
return nodeJsByteUtils.fromNumberArray(
|
51
|
-
Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256))
|
52
|
-
);
|
53
|
-
}
|
54
|
-
|
55
|
-
/**
|
56
|
-
* @internal
|
57
|
-
* WARNING: REQUIRE WILL BE REWRITTEN
|
58
|
-
*
|
59
|
-
* This code is carefully used by require_rewriter.mjs any modifications must be reflected in the plugin.
|
60
|
-
*
|
61
|
-
* @remarks
|
62
|
-
* "crypto" is the only dependency BSON needs. This presents a problem for creating a bundle of the BSON library
|
63
|
-
* 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
|
64
|
-
* an es module, there will be no global require function defined, making the code below fallback to the much less desireable math.random bytes.
|
65
|
-
* 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
|
66
|
-
* 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
|
67
|
-
* and replace `require` with `await import` and the IIFE line (`nodejsRandomBytes = (() => { ... })()`) with `nodejsRandomBytes = await (async () => { ... })()`
|
68
|
-
* when generating an es module bundle.
|
69
|
-
*/
|
70
|
-
const nodejsRandomBytes: (byteLength: number) => Uint8Array = (() => {
|
71
|
-
try {
|
72
|
-
return require("crypto").randomBytes;
|
73
|
-
} catch {
|
74
|
-
return nodejsMathRandomBytes;
|
75
|
-
}
|
76
|
-
})();
|
77
|
-
|
78
|
-
/** @internal */
|
79
|
-
export const nodeJsByteUtils = {
|
80
|
-
toLocalBufferType(
|
81
|
-
potentialBuffer: Uint8Array | NodeJsBuffer | ArrayBuffer
|
82
|
-
): NodeJsBuffer {
|
83
|
-
if (Buffer.isBuffer(potentialBuffer)) {
|
84
|
-
return potentialBuffer;
|
85
|
-
}
|
86
|
-
|
87
|
-
if (ArrayBuffer.isView(potentialBuffer)) {
|
88
|
-
return Buffer.from(
|
89
|
-
potentialBuffer.buffer,
|
90
|
-
potentialBuffer.byteOffset,
|
91
|
-
potentialBuffer.byteLength
|
92
|
-
);
|
93
|
-
}
|
94
|
-
|
95
|
-
const stringTag =
|
96
|
-
potentialBuffer?.[Symbol.toStringTag] ??
|
97
|
-
Object.prototype.toString.call(potentialBuffer);
|
98
|
-
if (
|
99
|
-
stringTag === "ArrayBuffer" ||
|
100
|
-
stringTag === "SharedArrayBuffer" ||
|
101
|
-
stringTag === "[object ArrayBuffer]" ||
|
102
|
-
stringTag === "[object SharedArrayBuffer]"
|
103
|
-
) {
|
104
|
-
return Buffer.from(potentialBuffer);
|
105
|
-
}
|
106
|
-
|
107
|
-
throw new BSONError(`Cannot create Buffer from ${String(potentialBuffer)}`);
|
108
|
-
},
|
109
|
-
|
110
|
-
allocate(size: number): NodeJsBuffer {
|
111
|
-
return Buffer.alloc(size);
|
112
|
-
},
|
113
|
-
|
114
|
-
equals(a: Uint8Array, b: Uint8Array): boolean {
|
115
|
-
return nodeJsByteUtils.toLocalBufferType(a).equals(b);
|
116
|
-
},
|
117
|
-
|
118
|
-
fromNumberArray(array: number[]): NodeJsBuffer {
|
119
|
-
return Buffer.from(array);
|
120
|
-
},
|
121
|
-
|
122
|
-
fromBase64(base64: string): NodeJsBuffer {
|
123
|
-
return Buffer.from(base64, "base64");
|
124
|
-
},
|
125
|
-
|
126
|
-
toBase64(buffer: Uint8Array): string {
|
127
|
-
return nodeJsByteUtils.toLocalBufferType(buffer).toString("base64");
|
128
|
-
},
|
129
|
-
|
130
|
-
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
|
131
|
-
fromISO88591(codePoints: string): NodeJsBuffer {
|
132
|
-
return Buffer.from(codePoints, "binary");
|
133
|
-
},
|
134
|
-
|
135
|
-
/** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
|
136
|
-
toISO88591(buffer: Uint8Array): string {
|
137
|
-
return nodeJsByteUtils.toLocalBufferType(buffer).toString("binary");
|
138
|
-
},
|
139
|
-
|
140
|
-
fromHex(hex: string): NodeJsBuffer {
|
141
|
-
return Buffer.from(hex, "hex");
|
142
|
-
},
|
143
|
-
|
144
|
-
toHex(buffer: Uint8Array): string {
|
145
|
-
return nodeJsByteUtils.toLocalBufferType(buffer).toString("hex");
|
146
|
-
},
|
147
|
-
|
148
|
-
fromUTF8(text: string): NodeJsBuffer {
|
149
|
-
return Buffer.from(text, "utf8");
|
150
|
-
},
|
151
|
-
|
152
|
-
toUTF8(buffer: Uint8Array, start: number, end: number): string {
|
153
|
-
return nodeJsByteUtils
|
154
|
-
.toLocalBufferType(buffer)
|
155
|
-
.toString("utf8", start, end);
|
156
|
-
},
|
157
|
-
|
158
|
-
utf8ByteLength(input: string): number {
|
159
|
-
return Buffer.byteLength(input, "utf8");
|
160
|
-
},
|
161
|
-
|
162
|
-
encodeUTF8Into(
|
163
|
-
buffer: Uint8Array,
|
164
|
-
source: string,
|
165
|
-
byteOffset: number
|
166
|
-
): number {
|
167
|
-
return nodeJsByteUtils
|
168
|
-
.toLocalBufferType(buffer)
|
169
|
-
.write(source, byteOffset, undefined, "utf8");
|
170
|
-
},
|
171
|
-
|
172
|
-
randomBytes: nodejsRandomBytes,
|
173
|
-
};
|