encoding.d.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Implements the [Encoding standard](https://encoding.spec.whatwg.org/):
  3. * [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder),
  4. * [TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder),
  5. * [TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream),
  6. * [TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream),
  7. * some [hooks](https://encoding.spec.whatwg.org/#specification-hooks).
  8. *
  9. * ```js
  10. * import { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'
  11. * import { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams
  12. * import { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding.js'
  13. *
  14. * // Hooks for standards
  15. * import { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'
  16. * ```
  17. *
  18. * @module @exodus/bytes/encoding.js
  19. */
  20. /// <reference types="node" />
  21. /**
  22. * Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
  23. * as an ASCII-lowercased string.
  24. *
  25. * If an encoding with that label does not exist, returns `null`.
  26. *
  27. * This is the same as [`decoder.encoding` getter](https://encoding.spec.whatwg.org/#dom-textdecoder-encoding),
  28. * except that it:
  29. * 1. Supports [`replacement` encoding](https://encoding.spec.whatwg.org/#replacement) and its
  30. * [labels](https://encoding.spec.whatwg.org/#ref-for-replacement%E2%91%A1)
  31. * 2. Does not throw for invalid labels and instead returns `null`
  32. *
  33. * It is identical to:
  34. * ```js
  35. * labelToName(label)?.toLowerCase() ?? null
  36. * ```
  37. *
  38. * All encoding names are also valid labels for corresponding encodings.
  39. *
  40. * @param label - The encoding label to normalize
  41. * @returns The normalized encoding name, or null if invalid
  42. */
  43. export function normalizeEncoding(label: string): string | null;
  44. /**
  45. * Implements [BOM sniff](https://encoding.spec.whatwg.org/#bom-sniff) legacy hook.
  46. *
  47. * Given a `TypedArray` or an `ArrayBuffer` instance `input`, returns either of:
  48. * - `'utf-8'`, if `input` starts with UTF-8 byte order mark.
  49. * - `'utf-16le'`, if `input` starts with UTF-16LE byte order mark.
  50. * - `'utf-16be'`, if `input` starts with UTF-16BE byte order mark.
  51. * - `null` otherwise.
  52. *
  53. * @param input - The bytes to check for BOM
  54. * @returns The encoding ('utf-8', 'utf-16le', 'utf-16be'), or null if no BOM found
  55. */
  56. export function getBOMEncoding(
  57. input: ArrayBufferLike | ArrayBufferView
  58. ): 'utf-8' | 'utf-16le' | 'utf-16be' | null;
  59. /**
  60. * Implements [decode](https://encoding.spec.whatwg.org/#decode) legacy hook.
  61. *
  62. * Given a `TypedArray` or an `ArrayBuffer` instance `input` and an optional `fallbackEncoding`
  63. * encoding [label](https://encoding.spec.whatwg.org/#names-and-labels),
  64. * sniffs encoding from BOM with `fallbackEncoding` fallback and then
  65. * decodes the `input` using that encoding, skipping BOM if it was present.
  66. *
  67. * Notes:
  68. *
  69. * - BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.
  70. * Use with care.
  71. * - Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),
  72. * aka replacement. It can convert different byte sequences to equal strings.
  73. *
  74. * This method is similar to the following code, except that it doesn't support encoding labels and
  75. * only expects lowercased encoding name:
  76. *
  77. * ```js
  78. * new TextDecoder(getBOMEncoding(input) ?? fallbackEncoding).decode(input)
  79. * ```
  80. *
  81. * @param input - The bytes to decode
  82. * @param fallbackEncoding - The encoding to use if no BOM detected (default: 'utf-8')
  83. * @returns The decoded string
  84. */
  85. export function legacyHookDecode(
  86. input: ArrayBufferLike | ArrayBufferView,
  87. fallbackEncoding?: string
  88. ): string;
  89. /**
  90. * Implements [isomorphic decode](https://infra.spec.whatwg.org/#isomorphic-decode).
  91. *
  92. * Given a `TypedArray` or an `ArrayBuffer` instance `input`, creates a string of the same length
  93. * as input byteLength, using bytes from input as codepoints.
  94. *
  95. * E.g. for `Uint8Array` input, this is similar to `String.fromCodePoint(...input)`.
  96. *
  97. * Wider `TypedArray` inputs, e.g. `Uint16Array`, are interpreted as underlying _bytes_.
  98. *
  99. * @param input - The bytes to decode
  100. * @returns The decoded string
  101. */
  102. export function isomorphicDecode(input: ArrayBufferLike | ArrayBufferView): string;
  103. /**
  104. * Implements [isomorphic encode](https://infra.spec.whatwg.org/#isomorphic-encode).
  105. *
  106. * Given a string, creates an `Uint8Array` of the same length with the string codepoints as byte values.
  107. *
  108. * Accepts only [isomorphic string](https://infra.spec.whatwg.org/#isomorphic-string) input
  109. * and asserts that, throwing on any strings containing codepoints higher than `U+00FF`.
  110. *
  111. * @param string - The string to encode
  112. * @returns An Uint8Array containing the input codepoints encoded as bytes
  113. */
  114. export function isomorphicEncode(string: string): Uint8Array;
  115. /**
  116. * Implements [get an encoding from a string `label`](https://encoding.spec.whatwg.org/#concept-encoding-get).
  117. *
  118. * Convert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,
  119. * as a case-sensitive string.
  120. *
  121. * If an encoding with that label does not exist, returns `null`.
  122. *
  123. * All encoding names are also valid labels for corresponding encodings.
  124. *
  125. * @param label - The encoding label
  126. * @returns The proper case encoding name, or null if invalid
  127. */
  128. export function labelToName(label: string): string | null;
  129. /**
  130. * [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) implementation/polyfill.
  131. *
  132. * Decode bytes to strings according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
  133. */
  134. export const TextDecoder: typeof globalThis.TextDecoder;
  135. /**
  136. * [TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) implementation/polyfill.
  137. *
  138. * Encode strings to UTF-8 bytes according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.
  139. */
  140. export const TextEncoder: typeof globalThis.TextEncoder;
  141. /**
  142. * [TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream) implementation/polyfill.
  143. *
  144. * A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextDecoder`.
  145. *
  146. * Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
  147. * [polyfilled](https://npmjs.com/package/web-streams-polyfill).
  148. */
  149. export const TextDecoderStream: typeof globalThis.TextDecoderStream;
  150. /**
  151. * [TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream) implementation/polyfill.
  152. *
  153. * A [Streams](https://streams.spec.whatwg.org/) wrapper for `TextEncoder`.
  154. *
  155. * Requires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or
  156. * [polyfilled](https://npmjs.com/package/web-streams-polyfill).
  157. */
  158. export const TextEncoderStream: typeof globalThis.TextEncoderStream;