encoding.d.ts 5.5 KB

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