index.d.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Stream = require("stream")
  11. import type { Encoding } from "../types/encodings"
  12. declare namespace iconv {
  13. export interface DecodeOptions {
  14. /**
  15. * Strip the Byte Order Mark (BOM) from the input,
  16. * when decoding, if the codec is BOM-aware. @default true
  17. */
  18. stripBOM?: boolean;
  19. /** Override the default endianness for `UTF-16` and `UTF-32` decodings. */
  20. defaultEncoding?: "utf16be" | "utf32be";
  21. }
  22. export interface EncodeOptions {
  23. /**
  24. * Add a Byte Order Mark (BOM) to the output, when encoding,
  25. * if the codec is BOM-aware. @default false
  26. */
  27. addBOM?: boolean;
  28. /** Override the default endianness for `UTF-32` encoding. */
  29. defaultEncoding?: "utf32be";
  30. }
  31. export interface EncoderStream {
  32. write(str: string): Buffer;
  33. end(): Buffer | undefined;
  34. }
  35. export interface DecoderStream {
  36. write(buf: Buffer): string;
  37. end(): string | undefined;
  38. }
  39. export interface Codec {
  40. encoder: new (options?: EncodeOptions, codec?: Codec) => EncoderStream;
  41. decoder: new (options?: DecodeOptions, codec?: Codec) => DecoderStream;
  42. bomAware?: boolean;
  43. [key: string]: any;
  44. }
  45. /** Encodes a `string` into a `Buffer`, using the provided `encoding`. */
  46. export function encode (content: string, encoding: Encoding, options?: EncodeOptions): Buffer
  47. /** Decodes a `Buffer` into a `string`, using the provided `encoding`. */
  48. export function decode (buffer: Buffer | Uint8Array, encoding: Encoding, options?: DecodeOptions): string
  49. /** Checks if a given encoding is supported by `iconv-lite`. */
  50. export function encodingExists (encoding: string): encoding is Encoding
  51. /** Legacy alias for {@link iconv.encode}. */
  52. export const toEncoding: typeof iconv.encode
  53. /** Legacy alias for {@link iconv.decode}. */
  54. export const fromEncoding: typeof iconv.decode
  55. /** Creates a stream that decodes binary data from a given `encoding` into strings. */
  56. export function decodeStream (encoding: Encoding, options?: DecodeOptions): NodeJS.ReadWriteStream
  57. /** Creates a stream that encodes strings into binary data in a given `encoding`. */
  58. export function encodeStream (encoding: Encoding, options?: EncodeOptions): NodeJS.ReadWriteStream
  59. /**
  60. * Explicitly enable Streaming API in browser environments by passing in:
  61. * ```js
  62. * require('stream')
  63. * ```
  64. * @example iconv.enableStreamingAPI(require('stream'));
  65. */
  66. export function enableStreamingAPI (stream_module: { Transform: typeof Stream.Transform }): void
  67. /** Creates and returns a low-level encoder stream. */
  68. export function getEncoder (encoding: Encoding, options?: EncodeOptions): EncoderStream
  69. /** Creates and returns a low-level decoder stream. */
  70. export function getDecoder (encoding: Encoding, options?: DecodeOptions): DecoderStream
  71. /**
  72. * Returns a codec object for the given `encoding`.
  73. * @throws If the `encoding` is not recognized.
  74. */
  75. export function getCodec (encoding: Encoding): Codec
  76. /** Strips all non-alphanumeric characters and appended year from `encoding`. */
  77. export function _canonicalizeEncoding (encoding: Encoding): string
  78. /** A cache of all loaded encoding definitions. */
  79. export let 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. export let _codecDataCache: Record<string, Codec>
  89. /** The character used for untranslatable `Unicode` characters. @default "�" */
  90. export let defaultCharUnicode: string
  91. /** The character used for untranslatable `single-byte` characters. @default "?" */
  92. export let defaultCharSingleByte: string
  93. /**
  94. * Skip deprecation warning when strings are used instead of Buffers during decoding.
  95. * Note: {@link iconv.decode} converts the string to Buffer regardless.
  96. */
  97. export let skipDecodeWarning: boolean
  98. /** @readonly Whether or not, Streaming API is enabled. */
  99. export const supportsStreams: boolean
  100. export type { iconv as Iconv, Encoding }
  101. }
  102. export = iconv