array.d.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * TypedArray utils and conversions.
  3. *
  4. * ```js
  5. * import { typedCopyBytes, typedView } from '@exodus/bytes/array.js'
  6. * ```
  7. *
  8. * @module @exodus/bytes/array.js
  9. */
  10. /// <reference types="node" />
  11. // >= TypeScript 5.9 made Uint8Array templated with <> and defaulted to ArrayBufferLike
  12. // which would incorrectly accept SharedArrayBuffer instances.
  13. // < TypeScript 5.7 doesn't support templates for Uint8Array.
  14. // So this type is defined as a workaround to evaluate to Uint8Array<ArrayBuffer> on all versions of TypeScript.
  15. /**
  16. * This is `Uint8Array<ArrayBuffer>`
  17. * (as opposed to `Uint8Array<SharedArrayBuffer>` and `Uint8Array<ArrayBufferLike>`)
  18. * on TypeScript versions that support that distinction.
  19. *
  20. * On TypeScript < 5.7, this is just `Uint8Array`, as it's not a template there.
  21. */
  22. export type Uint8ArrayBuffer = ReturnType<typeof Uint8Array.from>;
  23. /**
  24. * This is `Uint16Array<ArrayBuffer>`
  25. * (as opposed to `Uint16Array<SharedArrayBuffer>` and `Uint16Array<ArrayBufferLike>`)
  26. * on TypeScript versions that support that distinction.
  27. *
  28. * On TypeScript < 5.7, this is just `Uint16Array`, as it's not a template there.
  29. */
  30. export type Uint16ArrayBuffer = ReturnType<typeof Uint16Array.from>;
  31. /**
  32. * This is `Uint32Array<ArrayBuffer>`
  33. * (as opposed to `Uint32Array<SharedArrayBuffer>` and `Uint32Array<ArrayBufferLike>`)
  34. * on TypeScript versions that support that distinction.
  35. *
  36. * On TypeScript < 5.7, this is just `Uint32Array`, as it's not a template there.
  37. */
  38. export type Uint32ArrayBuffer = ReturnType<typeof Uint32Array.from>;
  39. /**
  40. * Output format for typed array conversions
  41. */
  42. export type OutputFormat = 'uint8' | 'arraybuffer' | 'buffer';
  43. /**
  44. * Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
  45. *
  46. * > [!IMPORTANT]
  47. * > Does not copy data, returns a view on the same underlying buffer
  48. *
  49. * > [!WARNING]
  50. * > Viewing `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`) as bytes
  51. * > is platform endianness-dependent.
  52. *
  53. * @param arr - The input TypedArray
  54. * @param format - The desired output format (`'uint8'` or `'buffer'`)
  55. * @returns A view on the same underlying buffer
  56. */
  57. export function typedView(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
  58. export function typedView(arr: ArrayBufferView, format: 'buffer'): Buffer;
  59. export function typedView(arr: ArrayBufferView, format: 'uint8' | 'buffer'): Uint8Array | Buffer;
  60. /**
  61. * Create a copy of TypedArray underlying bytes in the specified format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
  62. *
  63. * This does not copy _values_, but copies the underlying bytes.
  64. * The result is similar to that of `typedView()`, but this function provides a copy, not a view of the same memory.
  65. *
  66. * > [!WARNING]
  67. * > Copying underlying bytes from `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`)
  68. * > is platform endianness-dependent.
  69. *
  70. * > [!NOTE]
  71. * > Buffer might be pooled.
  72. * > Uint8Array return values are not pooled and match their underlying ArrayBuffer.
  73. *
  74. * @param arr - The input TypedArray
  75. * @param format - The desired output format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
  76. * @returns A copy of the underlying buffer
  77. */
  78. export function typedCopyBytes(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
  79. export function typedCopyBytes(arr: ArrayBufferView, format: 'arraybuffer'): ArrayBuffer;
  80. export function typedCopyBytes(arr: ArrayBufferView, format: 'buffer'): Buffer;
  81. export function typedCopyBytes(arr: ArrayBufferView, format: OutputFormat): Uint8Array | ArrayBuffer | Buffer;