byte_utils.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { nodeJsByteUtils } from './node_byte_utils';
  2. import { webByteUtils } from './web_byte_utils';
  3. /** @internal */
  4. export type ByteUtils = {
  5. /** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */
  6. toLocalBufferType(buffer: Uint8Array | ArrayBufferView | ArrayBuffer): Uint8Array;
  7. /** Create empty space of size */
  8. allocate: (size: number) => Uint8Array;
  9. /** Check if two Uint8Arrays are deep equal */
  10. equals: (a: Uint8Array, b: Uint8Array) => boolean;
  11. /** Check if two Uint8Arrays are deep equal */
  12. fromNumberArray: (array: number[]) => Uint8Array;
  13. /** Create a Uint8Array from a base64 string */
  14. fromBase64: (base64: string) => Uint8Array;
  15. /** Create a base64 string from bytes */
  16. toBase64: (buffer: Uint8Array) => string;
  17. /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
  18. fromISO88591: (codePoints: string) => Uint8Array;
  19. /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
  20. toISO88591: (buffer: Uint8Array) => string;
  21. /** Create a Uint8Array from a hex string */
  22. fromHex: (hex: string) => Uint8Array;
  23. /** Create a lowercase hex string from bytes */
  24. toHex: (buffer: Uint8Array) => string;
  25. /** Create a Uint8Array containing utf8 code units from a string */
  26. fromUTF8: (text: string) => Uint8Array;
  27. /** Create a string from utf8 code units */
  28. toUTF8: (buffer: Uint8Array, start: number, end: number) => string;
  29. /** Get the utf8 code unit count from a string if it were to be transformed to utf8 */
  30. utf8ByteLength: (input: string) => number;
  31. /** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */
  32. encodeUTF8Into(destination: Uint8Array, source: string, byteOffset: number): number;
  33. /** Generate a Uint8Array filled with random bytes with byteLength */
  34. randomBytes(byteLength: number): Uint8Array;
  35. };
  36. declare const Buffer: { new (): unknown; prototype?: { _isBuffer?: boolean } } | undefined;
  37. /**
  38. * Check that a global Buffer exists that is a function and
  39. * does not have a '_isBuffer' property defined on the prototype
  40. * (this is to prevent using the npm buffer)
  41. */
  42. const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
  43. /**
  44. * This is the only ByteUtils that should be used across the rest of the BSON library.
  45. *
  46. * The type annotation is important here, it asserts that each of the platform specific
  47. * utils implementations are compatible with the common one.
  48. *
  49. * @internal
  50. */
  51. export const ByteUtils: ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
  52. export class BSONDataView extends DataView {
  53. static fromUint8Array(input: Uint8Array) {
  54. return new DataView(input.buffer, input.byteOffset, input.byteLength);
  55. }
  56. }