multi-byte.d.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Decode / encode the legacy multi-byte encodings according to the
  3. * [Encoding standard](https://encoding.spec.whatwg.org/)
  4. * ([§10](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(simplified)-encodings),
  5. * [§11](https://encoding.spec.whatwg.org/#legacy-multi-byte-chinese-(traditional)-encodings),
  6. * [§12](https://encoding.spec.whatwg.org/#legacy-multi-byte-japanese-encodings),
  7. * [§13](https://encoding.spec.whatwg.org/#legacy-multi-byte-korean-encodings)).
  8. *
  9. * ```js
  10. * import { createMultibyteDecoder, createMultibyteEncoder } from '@exodus/bytes/multi-byte.js'
  11. * ```
  12. *
  13. * > [!WARNING]
  14. * > This is a lower-level API for legacy multi-byte encodings.
  15. * >
  16. * > For a safe WHATWG Encoding-compatible API, see `@exodus/bytes/encoding.js` import (and variants of it).
  17. * >
  18. * > Be sure to know what you are doing and check documentation when directly using encodings from this file.
  19. *
  20. * Supports all legacy multi-byte encodings listed in the WHATWG Encoding standard:
  21. * `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`, `euc-kr`.
  22. *
  23. * @module @exodus/bytes/multi-byte.js
  24. */
  25. /// <reference types="node" />
  26. import type { Uint8ArrayBuffer } from './array.js';
  27. /**
  28. * Create a decoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
  29. *
  30. * Returns a function `decode(arr, stream = false)` that decodes bytes to a string.
  31. *
  32. * The returned function will maintain internal state while `stream = true` is used, allowing it to
  33. * handle incomplete multi-byte sequences across multiple calls.
  34. * State is reset when `stream = false` or when the function is called without the `stream` parameter.
  35. *
  36. * @param encoding - The encoding name (e.g., 'gbk', 'gb18030', 'big5', 'euc-jp', 'iso-2022-jp', 'shift_jis', 'euc-kr')
  37. * @param loose - If true, replaces unmapped bytes with replacement character instead of throwing (default: false)
  38. * @returns A function that decodes bytes to string, with optional streaming support
  39. */
  40. export function createMultibyteDecoder(
  41. encoding: string,
  42. loose?: boolean
  43. ): (arr: Uint8Array, stream?: boolean) => string;
  44. /**
  45. * Create an encoder for a supported legacy multi-byte `encoding`, given its lowercased name `encoding`.
  46. *
  47. * Returns a function `encode(string)` that encodes a string to bytes.
  48. *
  49. * In `'fatal'` mode (default), will throw on non well-formed strings or any codepoints which could
  50. * not be encoded in the target encoding.
  51. *
  52. * @param encoding - The encoding name (e.g., 'gbk', 'gb18030', 'big5', 'euc-jp', 'iso-2022-jp', 'shift_jis', 'euc-kr')
  53. * @param options - Encoding options
  54. * @param options.mode - Encoding mode (default: 'fatal'). Currently, only 'fatal' mode is supported.
  55. * @returns A function that encodes string to bytes
  56. */
  57. export function createMultibyteEncoder(
  58. encoding: string,
  59. options?: { mode?: 'fatal' }
  60. ): (string: string) => Uint8ArrayBuffer;