mhtml-stream
Version:
Streaming MHTML parser
49 lines (48 loc) • 1.69 kB
TypeScript
import { type MhtmlHeaders } from "./headers.js";
export type { MhtmlHeaders };
/** a file that was encoded in MHTML format */
export interface MhtmlFile {
/** the files's headers */
headers: MhtmlHeaders;
/** the file's content */
content: Uint8Array;
}
/** The decoder of any Content-Transfer-Encoding
*
* @param lines - each line of raw bytes
* @returns data - decoded lines
*/
export type Decoder = (lines: AsyncIterable<Uint8Array>) => AsyncIterable<Uint8Array>;
/** options for mhtml parsing */
export interface ParseOptions {
/**
* custom decoders keyed by (lowercased) Content-Transfer-Encoding
*
* Use these to handle an encoding the defaults don't, or to override one. A
* {@link Decoder} turns the CRLF-split lines of a part into its decoded
* bytes; the parser strips the CRLFs when splitting, so a passthrough decoder
* re-emits them between lines:
*
* ```ts
* const crlf = new Uint8Array([13, 10]);
* const decoderOverrides = new Map([
* ["binary", async function* (lines) {
* let first = true;
* for await (const line of lines) {
* if (!first) yield crlf;
* first = false;
* yield line;
* }
* }],
* ]);
* ```
*/
decoderOverrides?: Map<string, Decoder>;
}
/**
* parse a readable stream into an async iterator of MHTML files
*
* decoderOverrides can be used to overwrite default encoders or specify your
* own if a Content-Transfer-Encoding isn't handled properly
*/
export declare function parseMhtml(stream: AsyncIterable<Uint8Array>, { decoderOverrides }?: ParseOptions): AsyncIterableIterator<MhtmlFile>;