charset.d.ts

3.2 KB v1.0
import { Buffer } from 'node:buffer';
import { Transform } from 'node:stream';
import type { TransformCallback } from 'node:stream';
/**
 * Encodes an unicode string into an Buffer object as UTF-8
 *
 * We force UTF-8 here, no strange encodings allowed.
 *
 * @param str String to be encoded
 * @return UTF-8 encoded typed array
 */
export declare function encode(str: string): Buffer;
/**
 * Decodes a string from Buffer to an unicode string using specified encoding.
 * An unknown charset falls back to UTF-8
 *
 * @param buf Binary data to be decoded
 * @param [fromCharset='UTF-8'] Binary data is decoded into string using this charset
 * @return Decoded string
 */
export declare function decode(buf: Buffer | string, fromCharset?: string): string;
/**
 * Convert a string from specific encoding to UTF-8 Buffer
 *
 * @param data String or Buffer to be encoded
 * @param [fromCharset='UTF-8'] Source encoding for the string
 * @return UTF-8 encoded typed array
 */
export declare function convert(data: string | Buffer, fromCharset?: string): Buffer;
/**
 * Converts well known invalid character set names to proper names.
 * eg. win-1257 will be converted to windows-1257. Missing or non-string
 * input defaults to UTF-8.
 *
 * @param charset Charset name to convert
 * @return Canonicalized charset name
 */
export declare function normalizeCharset(charset?: unknown): string;
/**
 * Transform stream that decodes bytes in a charset into UTF-8 Buffers, see createDecodeStream()
 */
export declare class DecodeStream extends Transform {
    /** Canonical name of the charset the input was declared in, what normalizeCharset() gives */
    readonly charset: string;
    /**
     * false when the declared charset is unknown, or one that is refused (UTF-7, base64, hex). The
     * input is then decoded as UTF-8, the same fallback decode() applies, so a caller that must
     * not relabel such bytes as UTF-8 checks this before piping anything through
     */
    readonly supported: boolean;
    private decoder;
    private chunks;
    private highSurrogate;
    constructor(charset?: unknown);
    _transform(chunk: Buffer, _encoding: BufferEncoding, done: TransformCallback): void;
    _flush(done: TransformCallback): void;
    private pushText;
}
/**
 * Creates a Transform stream that decodes bytes in the given charset into UTF-8 Buffers. The
 * charset name is resolved with normalizeCharset(), and the output is what decode() gives for
 * the whole input, however it is split into chunks: the Japanese charsets iconv-lite does not
 * support are decoded with encoding-japanese (collected until the input ends, as that decoder
 * is not incremental), and an unknown or refused charset (UTF-7, base64, hex) is decoded as
 * UTF-8 with `supported` set to false on the stream.
 *
 * @param [charset='UTF-8'] Charset the input is encoded in
 * @return Transform stream with `charset` and `supported` properties
 */
export declare function createDecodeStream(charset?: unknown): DecodeStream;
declare const _default: {
    encode: typeof encode;
    decode: typeof decode;
    convert: typeof convert;
    normalizeCharset: typeof normalizeCharset;
    createDecodeStream: typeof createDecodeStream;
    DecodeStream: typeof DecodeStream;
};
export default _default;