index.d.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License.
  4. * REQUIREMENT: This definition is dependent on the @types/node definition.
  5. * Install with `npm install @types/node --save-dev`
  6. *-------------------------------------------------------------------------------------------- */
  7. /* ---------------------------------------------------------------------------------------------
  8. * This file provides detailed typings for the public API of iconv-lite
  9. *-------------------------------------------------------------------------------------------- */
  10. import type { Encoding } from "../types/encodings"
  11. // --- Options ---
  12. declare namespace iconv {
  13. export interface DecodeOptions {
  14. /** Strip the byte order mark (BOM) from the input, when decoding. @default true */
  15. stripBOM?: boolean;
  16. /** Override the default endianness for `UTF-16` and `UTF-32` decodings. */
  17. defaultEncoding?: "utf16be" | "utf32be";
  18. }
  19. export interface EncodeOptions {
  20. /** Add a byte order mark (BOM) to the output, when encoding. @default false */
  21. addBOM?: boolean;
  22. /** Override the default endianness for `UTF-32` encoding. */
  23. defaultEncoding?: "utf32be";
  24. }
  25. // --- Return values ---
  26. export interface EncoderStream {
  27. write(str: string): Buffer;
  28. end(): Buffer | undefined;
  29. }
  30. export interface DecoderStream {
  31. write(buf: Buffer): string;
  32. end(): string | undefined;
  33. }
  34. export interface Codec {
  35. encoder: new (options?: EncodeOptions, codec?: any) => EncoderStream;
  36. decoder: new (options?: DecodeOptions, codec?: any) => DecoderStream;
  37. [key: string]: any;
  38. }
  39. const iconv: {
  40. // --- Basic API ---
  41. /** Encodes a `string` into a `Buffer`, using the provided `encoding`. */
  42. encode(content: string, encoding: Encoding, options?: EncodeOptions): Buffer;
  43. /** Decodes a `Buffer` into a `string`, using the provided `encoding`. */
  44. decode(buffer: Buffer | Uint8Array, encoding: Encoding, options?: DecodeOptions): string;
  45. /** Checks if a given encoding is supported by `iconv-lite`. */
  46. encodingExists(encoding: string): encoding is Encoding;
  47. // --- Legacy aliases ---
  48. /** Legacy alias for {@link iconv.encode}. */
  49. toEncoding: typeof iconv.encode;
  50. /** Legacy alias for {@link iconv.decode}. */
  51. fromEncoding: typeof iconv.decode;
  52. // --- Stream API ---
  53. /** Creates a stream that decodes binary data from a given `encoding` into strings. */
  54. decodeStream(encoding: Encoding, options?: DecodeOptions): NodeJS.ReadWriteStream;
  55. /** Creates a stream that encodes strings into binary data in a given `encoding`. */
  56. encodeStream(encoding: Encoding, options?: EncodeOptions): NodeJS.ReadWriteStream;
  57. /**
  58. * Explicitly enable Streaming API in browser environments by passing in:
  59. * ```js
  60. * require('stream')
  61. * ```
  62. * @example iconv.enableStreamingAPI(require('stream'));
  63. */
  64. enableStreamingAPI(stream_module: any): void;
  65. // --- Low-level stream APIs ---
  66. /** Creates and returns a low-level encoder stream. */
  67. getEncoder(encoding: Encoding, options?: EncodeOptions): EncoderStream;
  68. /** Creates and returns a low-level decoder stream. */
  69. getDecoder(encoding: Encoding, options?: DecodeOptions): DecoderStream;
  70. /**
  71. * Returns a codec object for the given `encoding`.
  72. * @throws If the `encoding` is not recognized.
  73. */
  74. getCodec(encoding: Encoding): Codec;
  75. /** Strips all non-alphanumeric characters and appended year from `encoding`. */
  76. _canonicalizeEncoding(encoding: Encoding): string;
  77. // --- Properties ---
  78. /** A cache of all loaded encoding definitions. */
  79. encodings: Record<
  80. Encoding,
  81. | string
  82. | {
  83. type: string;
  84. [key: string]: any;
  85. }
  86. > | null;
  87. /** A cache of initialized codec objects. */
  88. _codecDataCache: Record<string, Codec>;
  89. /** The character used for untranslatable `Unicode` characters. @default "�" */
  90. defaultCharUnicode: string;
  91. /** The character used for untranslatable `single-byte` characters. @default "?" */
  92. defaultCharSingleByte: string;
  93. /** @readonly Whether or not, Streaming API is enabled. */
  94. readonly supportsStreams: boolean;
  95. }
  96. export type { iconv as Iconv, Encoding }
  97. export { iconv as default }
  98. }
  99. export = iconv