First commit
This commit is contained in:
		
							
								
								
									
										61
									
								
								node_modules/bson/src/utils/byte_utils.ts
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								node_modules/bson/src/utils/byte_utils.ts
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | ||||
| import { nodeJsByteUtils } from './node_byte_utils'; | ||||
| import { webByteUtils } from './web_byte_utils'; | ||||
|  | ||||
| /** @internal */ | ||||
| export type ByteUtils = { | ||||
|   /** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */ | ||||
|   toLocalBufferType(buffer: Uint8Array | ArrayBufferView | ArrayBuffer): Uint8Array; | ||||
|   /** Create empty space of size */ | ||||
|   allocate: (size: number) => Uint8Array; | ||||
|   /** Check if two Uint8Arrays are deep equal */ | ||||
|   equals: (a: Uint8Array, b: Uint8Array) => boolean; | ||||
|   /** Check if two Uint8Arrays are deep equal */ | ||||
|   fromNumberArray: (array: number[]) => Uint8Array; | ||||
|   /** Create a Uint8Array from a base64 string */ | ||||
|   fromBase64: (base64: string) => Uint8Array; | ||||
|   /** Create a base64 string from bytes */ | ||||
|   toBase64: (buffer: Uint8Array) => string; | ||||
|   /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ | ||||
|   fromISO88591: (codePoints: string) => Uint8Array; | ||||
|   /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ | ||||
|   toISO88591: (buffer: Uint8Array) => string; | ||||
|   /** Create a Uint8Array from a hex string */ | ||||
|   fromHex: (hex: string) => Uint8Array; | ||||
|   /** Create a hex string from bytes */ | ||||
|   toHex: (buffer: Uint8Array) => string; | ||||
|   /** Create a Uint8Array containing utf8 code units from a string */ | ||||
|   fromUTF8: (text: string) => Uint8Array; | ||||
|   /** Create a string from utf8 code units */ | ||||
|   toUTF8: (buffer: Uint8Array) => string; | ||||
|   /** Get the utf8 code unit count from a string if it were to be transformed to utf8 */ | ||||
|   utf8ByteLength: (input: string) => number; | ||||
|   /** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */ | ||||
|   encodeUTF8Into(destination: Uint8Array, source: string, byteOffset: number): number; | ||||
|   /** Generate a Uint8Array filled with random bytes with byteLength */ | ||||
|   randomBytes(byteLength: number): Uint8Array; | ||||
| }; | ||||
|  | ||||
| declare const Buffer: { new (): unknown; prototype?: { _isBuffer?: boolean } } | undefined; | ||||
|  | ||||
| /** | ||||
|  * Check that a global Buffer exists that is a function and | ||||
|  * does not have a '_isBuffer' property defined on the prototype | ||||
|  * (this is to prevent using the npm buffer) | ||||
|  */ | ||||
| const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true; | ||||
|  | ||||
| /** | ||||
|  * This is the only ByteUtils that should be used across the rest of the BSON library. | ||||
|  * | ||||
|  * The type annotation is important here, it asserts that each of the platform specific | ||||
|  * utils implementations are compatible with the common one. | ||||
|  * | ||||
|  * @internal | ||||
|  */ | ||||
| export const ByteUtils: ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils; | ||||
|  | ||||
| export class BSONDataView extends DataView { | ||||
|   static fromUint8Array(input: Uint8Array) { | ||||
|     return new DataView(input.buffer, input.byteOffset, input.byteLength); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										141
									
								
								node_modules/bson/src/utils/node_byte_utils.ts
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										141
									
								
								node_modules/bson/src/utils/node_byte_utils.ts
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,141 @@ | ||||
| import { BSONError } from '../error'; | ||||
|  | ||||
| type NodeJsEncoding = 'base64' | 'hex' | 'utf8' | 'binary'; | ||||
| type NodeJsBuffer = ArrayBufferView & | ||||
|   Uint8Array & { | ||||
|     write(string: string, offset: number, length: undefined, encoding: 'utf8'): number; | ||||
|     copy(target: Uint8Array, targetStart: number, sourceStart: number, sourceEnd: number): number; | ||||
|     toString: (this: Uint8Array, encoding: NodeJsEncoding) => string; | ||||
|     equals: (this: Uint8Array, other: Uint8Array) => boolean; | ||||
|   }; | ||||
| type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & { | ||||
|   alloc: (size: number) => NodeJsBuffer; | ||||
|   from(array: number[]): NodeJsBuffer; | ||||
|   from(array: Uint8Array): NodeJsBuffer; | ||||
|   from(array: ArrayBuffer): NodeJsBuffer; | ||||
|   from(array: ArrayBuffer, byteOffset: number, byteLength: number): NodeJsBuffer; | ||||
|   from(base64: string, encoding: NodeJsEncoding): NodeJsBuffer; | ||||
|   byteLength(input: string, encoding: 'utf8'): number; | ||||
|   isBuffer(value: unknown): value is NodeJsBuffer; | ||||
| }; | ||||
|  | ||||
| // This can be nullish, but we gate the nodejs functions on being exported whether or not this exists | ||||
| // Node.js global | ||||
| declare const Buffer: NodeJsBufferConstructor; | ||||
| declare const require: (mod: 'crypto') => { randomBytes: (byteLength: number) => Uint8Array }; | ||||
|  | ||||
| /** @internal */ | ||||
| export function nodejsMathRandomBytes(byteLength: number) { | ||||
|   return nodeJsByteUtils.fromNumberArray( | ||||
|     Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)) | ||||
|   ); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * @internal | ||||
|  * WARNING: REQUIRE WILL BE REWRITTEN | ||||
|  * | ||||
|  * This code is carefully used by require_rewriter.mjs any modifications must be reflected in the plugin. | ||||
|  * | ||||
|  * @remarks | ||||
|  * "crypto" is the only dependency BSON needs. This presents a problem for creating a bundle of the BSON library | ||||
|  * 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 | ||||
|  * an es module, there will be no global require function defined, making the code below fallback to the much less desireable math.random bytes. | ||||
|  * 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 | ||||
|  * 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 | ||||
|  * and replace `require` with `await import` and the IIFE line (`nodejsRandomBytes = (() => { ... })()`) with `nodejsRandomBytes = await (async () => { ... })()` | ||||
|  * when generating an es module bundle. | ||||
|  */ | ||||
| const nodejsRandomBytes: (byteLength: number) => Uint8Array = (() => { | ||||
|   try { | ||||
|     return require('crypto').randomBytes; | ||||
|   } catch { | ||||
|     return nodejsMathRandomBytes; | ||||
|   } | ||||
| })(); | ||||
|  | ||||
| /** @internal */ | ||||
| export const nodeJsByteUtils = { | ||||
|   toLocalBufferType(potentialBuffer: Uint8Array | NodeJsBuffer | ArrayBuffer): NodeJsBuffer { | ||||
|     if (Buffer.isBuffer(potentialBuffer)) { | ||||
|       return potentialBuffer; | ||||
|     } | ||||
|  | ||||
|     if (ArrayBuffer.isView(potentialBuffer)) { | ||||
|       return Buffer.from( | ||||
|         potentialBuffer.buffer, | ||||
|         potentialBuffer.byteOffset, | ||||
|         potentialBuffer.byteLength | ||||
|       ); | ||||
|     } | ||||
|  | ||||
|     const stringTag = | ||||
|       potentialBuffer?.[Symbol.toStringTag] ?? Object.prototype.toString.call(potentialBuffer); | ||||
|     if ( | ||||
|       stringTag === 'ArrayBuffer' || | ||||
|       stringTag === 'SharedArrayBuffer' || | ||||
|       stringTag === '[object ArrayBuffer]' || | ||||
|       stringTag === '[object SharedArrayBuffer]' | ||||
|     ) { | ||||
|       return Buffer.from(potentialBuffer); | ||||
|     } | ||||
|  | ||||
|     throw new BSONError(`Cannot create Buffer from ${String(potentialBuffer)}`); | ||||
|   }, | ||||
|  | ||||
|   allocate(size: number): NodeJsBuffer { | ||||
|     return Buffer.alloc(size); | ||||
|   }, | ||||
|  | ||||
|   equals(a: Uint8Array, b: Uint8Array): boolean { | ||||
|     return nodeJsByteUtils.toLocalBufferType(a).equals(b); | ||||
|   }, | ||||
|  | ||||
|   fromNumberArray(array: number[]): NodeJsBuffer { | ||||
|     return Buffer.from(array); | ||||
|   }, | ||||
|  | ||||
|   fromBase64(base64: string): NodeJsBuffer { | ||||
|     return Buffer.from(base64, 'base64'); | ||||
|   }, | ||||
|  | ||||
|   toBase64(buffer: Uint8Array): string { | ||||
|     return nodeJsByteUtils.toLocalBufferType(buffer).toString('base64'); | ||||
|   }, | ||||
|  | ||||
|   /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ | ||||
|   fromISO88591(codePoints: string): NodeJsBuffer { | ||||
|     return Buffer.from(codePoints, 'binary'); | ||||
|   }, | ||||
|  | ||||
|   /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ | ||||
|   toISO88591(buffer: Uint8Array): string { | ||||
|     return nodeJsByteUtils.toLocalBufferType(buffer).toString('binary'); | ||||
|   }, | ||||
|  | ||||
|   fromHex(hex: string): NodeJsBuffer { | ||||
|     return Buffer.from(hex, 'hex'); | ||||
|   }, | ||||
|  | ||||
|   toHex(buffer: Uint8Array): string { | ||||
|     return nodeJsByteUtils.toLocalBufferType(buffer).toString('hex'); | ||||
|   }, | ||||
|  | ||||
|   fromUTF8(text: string): NodeJsBuffer { | ||||
|     return Buffer.from(text, 'utf8'); | ||||
|   }, | ||||
|  | ||||
|   toUTF8(buffer: Uint8Array): string { | ||||
|     return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8'); | ||||
|   }, | ||||
|  | ||||
|   utf8ByteLength(input: string): number { | ||||
|     return Buffer.byteLength(input, 'utf8'); | ||||
|   }, | ||||
|  | ||||
|   encodeUTF8Into(buffer: Uint8Array, source: string, byteOffset: number): number { | ||||
|     return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8'); | ||||
|   }, | ||||
|  | ||||
|   randomBytes: nodejsRandomBytes | ||||
| }; | ||||
							
								
								
									
										190
									
								
								node_modules/bson/src/utils/web_byte_utils.ts
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										190
									
								
								node_modules/bson/src/utils/web_byte_utils.ts
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,190 @@ | ||||
| import { BSONError } from '../error'; | ||||
|  | ||||
| type TextDecoder = { | ||||
|   readonly encoding: string; | ||||
|   readonly fatal: boolean; | ||||
|   readonly ignoreBOM: boolean; | ||||
|   decode(input?: Uint8Array): string; | ||||
| }; | ||||
| type TextDecoderConstructor = { | ||||
|   new (label: 'utf8', options: { fatal: boolean; ignoreBOM?: boolean }): TextDecoder; | ||||
| }; | ||||
|  | ||||
| type TextEncoder = { | ||||
|   readonly encoding: string; | ||||
|   encode(input?: string): Uint8Array; | ||||
| }; | ||||
| type TextEncoderConstructor = { | ||||
|   new (): TextEncoder; | ||||
| }; | ||||
|  | ||||
| // Web global | ||||
| declare const TextDecoder: TextDecoderConstructor; | ||||
| declare const TextEncoder: TextEncoderConstructor; | ||||
| declare const atob: (base64: string) => string; | ||||
| declare const btoa: (binary: string) => string; | ||||
|  | ||||
| type ArrayBufferViewWithTag = ArrayBufferView & { | ||||
|   [Symbol.toStringTag]?: string; | ||||
| }; | ||||
|  | ||||
| function isReactNative() { | ||||
|   const { navigator } = globalThis as { navigator?: { product?: string } }; | ||||
|   return typeof navigator === 'object' && navigator.product === 'ReactNative'; | ||||
| } | ||||
|  | ||||
| /** @internal */ | ||||
| export function webMathRandomBytes(byteLength: number) { | ||||
|   if (byteLength < 0) { | ||||
|     throw new RangeError(`The argument 'byteLength' is invalid. Received ${byteLength}`); | ||||
|   } | ||||
|   return webByteUtils.fromNumberArray( | ||||
|     Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)) | ||||
|   ); | ||||
| } | ||||
|  | ||||
| /** @internal */ | ||||
| const webRandomBytes: (byteLength: number) => Uint8Array = (() => { | ||||
|   const { crypto } = globalThis as { | ||||
|     crypto?: { getRandomValues?: (space: Uint8Array) => Uint8Array }; | ||||
|   }; | ||||
|   if (crypto != null && typeof crypto.getRandomValues === 'function') { | ||||
|     return (byteLength: number) => { | ||||
|       // @ts-expect-error: crypto.getRandomValues cannot actually be null here | ||||
|       // You cannot separate getRandomValues from crypto (need to have this === crypto) | ||||
|       return crypto.getRandomValues(webByteUtils.allocate(byteLength)); | ||||
|     }; | ||||
|   } else { | ||||
|     if (isReactNative()) { | ||||
|       const { console } = globalThis as { console?: { warn?: (message: string) => void } }; | ||||
|       console?.warn?.( | ||||
|         'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.' | ||||
|       ); | ||||
|     } | ||||
|     return webMathRandomBytes; | ||||
|   } | ||||
| })(); | ||||
|  | ||||
| const HEX_DIGIT = /(\d|[a-f])/i; | ||||
|  | ||||
| /** @internal */ | ||||
| export const webByteUtils = { | ||||
|   toLocalBufferType( | ||||
|     potentialUint8array: Uint8Array | ArrayBufferViewWithTag | ArrayBuffer | ||||
|   ): Uint8Array { | ||||
|     const stringTag = | ||||
|       potentialUint8array?.[Symbol.toStringTag] ?? | ||||
|       Object.prototype.toString.call(potentialUint8array); | ||||
|  | ||||
|     if (stringTag === 'Uint8Array') { | ||||
|       return potentialUint8array as Uint8Array; | ||||
|     } | ||||
|  | ||||
|     if (ArrayBuffer.isView(potentialUint8array)) { | ||||
|       return new Uint8Array( | ||||
|         potentialUint8array.buffer.slice( | ||||
|           potentialUint8array.byteOffset, | ||||
|           potentialUint8array.byteOffset + potentialUint8array.byteLength | ||||
|         ) | ||||
|       ); | ||||
|     } | ||||
|  | ||||
|     if ( | ||||
|       stringTag === 'ArrayBuffer' || | ||||
|       stringTag === 'SharedArrayBuffer' || | ||||
|       stringTag === '[object ArrayBuffer]' || | ||||
|       stringTag === '[object SharedArrayBuffer]' | ||||
|     ) { | ||||
|       return new Uint8Array(potentialUint8array); | ||||
|     } | ||||
|  | ||||
|     throw new BSONError(`Cannot make a Uint8Array from ${String(potentialUint8array)}`); | ||||
|   }, | ||||
|  | ||||
|   allocate(size: number): Uint8Array { | ||||
|     if (typeof size !== 'number') { | ||||
|       throw new TypeError(`The "size" argument must be of type number. Received ${String(size)}`); | ||||
|     } | ||||
|     return new Uint8Array(size); | ||||
|   }, | ||||
|  | ||||
|   equals(a: Uint8Array, b: Uint8Array): boolean { | ||||
|     if (a.byteLength !== b.byteLength) { | ||||
|       return false; | ||||
|     } | ||||
|     for (let i = 0; i < a.byteLength; i++) { | ||||
|       if (a[i] !== b[i]) { | ||||
|         return false; | ||||
|       } | ||||
|     } | ||||
|     return true; | ||||
|   }, | ||||
|  | ||||
|   fromNumberArray(array: number[]): Uint8Array { | ||||
|     return Uint8Array.from(array); | ||||
|   }, | ||||
|  | ||||
|   fromBase64(base64: string): Uint8Array { | ||||
|     return Uint8Array.from(atob(base64), c => c.charCodeAt(0)); | ||||
|   }, | ||||
|  | ||||
|   toBase64(uint8array: Uint8Array): string { | ||||
|     return btoa(webByteUtils.toISO88591(uint8array)); | ||||
|   }, | ||||
|  | ||||
|   /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ | ||||
|   fromISO88591(codePoints: string): Uint8Array { | ||||
|     return Uint8Array.from(codePoints, c => c.charCodeAt(0) & 0xff); | ||||
|   }, | ||||
|  | ||||
|   /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */ | ||||
|   toISO88591(uint8array: Uint8Array): string { | ||||
|     return Array.from(Uint16Array.from(uint8array), b => String.fromCharCode(b)).join(''); | ||||
|   }, | ||||
|  | ||||
|   fromHex(hex: string): Uint8Array { | ||||
|     const evenLengthHex = hex.length % 2 === 0 ? hex : hex.slice(0, hex.length - 1); | ||||
|     const buffer = []; | ||||
|  | ||||
|     for (let i = 0; i < evenLengthHex.length; i += 2) { | ||||
|       const firstDigit = evenLengthHex[i]; | ||||
|       const secondDigit = evenLengthHex[i + 1]; | ||||
|  | ||||
|       if (!HEX_DIGIT.test(firstDigit)) { | ||||
|         break; | ||||
|       } | ||||
|       if (!HEX_DIGIT.test(secondDigit)) { | ||||
|         break; | ||||
|       } | ||||
|  | ||||
|       const hexDigit = Number.parseInt(`${firstDigit}${secondDigit}`, 16); | ||||
|       buffer.push(hexDigit); | ||||
|     } | ||||
|  | ||||
|     return Uint8Array.from(buffer); | ||||
|   }, | ||||
|  | ||||
|   toHex(uint8array: Uint8Array): string { | ||||
|     return Array.from(uint8array, byte => byte.toString(16).padStart(2, '0')).join(''); | ||||
|   }, | ||||
|  | ||||
|   fromUTF8(text: string): Uint8Array { | ||||
|     return new TextEncoder().encode(text); | ||||
|   }, | ||||
|  | ||||
|   toUTF8(uint8array: Uint8Array): string { | ||||
|     return new TextDecoder('utf8', { fatal: false }).decode(uint8array); | ||||
|   }, | ||||
|  | ||||
|   utf8ByteLength(input: string): number { | ||||
|     return webByteUtils.fromUTF8(input).byteLength; | ||||
|   }, | ||||
|  | ||||
|   encodeUTF8Into(buffer: Uint8Array, source: string, byteOffset: number): number { | ||||
|     const bytes = webByteUtils.fromUTF8(source); | ||||
|     buffer.set(bytes, byteOffset); | ||||
|     return bytes.byteLength; | ||||
|   }, | ||||
|  | ||||
|   randomBytes: webRandomBytes | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user