| | |
| | | /** |
| | | * The `node:zlib` module provides compression functionality implemented using |
| | | * Gzip, Deflate/Inflate, and Brotli. |
| | | * |
| | | * To access it: |
| | | * |
| | | * ```js |
| | | * import zlib from 'node:zlib'; |
| | | * ``` |
| | | * |
| | | * Compression and decompression are built around the Node.js |
| | | * [Streams API](https://nodejs.org/docs/latest-v25.x/api/stream.html). |
| | | * |
| | | * Compressing or decompressing a stream (such as a file) can be accomplished by |
| | | * piping the source stream through a `zlib` `Transform` stream into a destination |
| | | * stream: |
| | | * |
| | | * ```js |
| | | * import { createGzip } from 'node:zlib'; |
| | | * import { pipeline } from 'node:stream'; |
| | | * import { |
| | | * createReadStream, |
| | | * createWriteStream, |
| | | * } from 'node:fs'; |
| | | * |
| | | * const gzip = createGzip(); |
| | | * const source = createReadStream('input.txt'); |
| | | * const destination = createWriteStream('input.txt.gz'); |
| | | * |
| | | * pipeline(source, gzip, destination, (err) => { |
| | | * if (err) { |
| | | * console.error('An error occurred:', err); |
| | | * process.exitCode = 1; |
| | | * } |
| | | * }); |
| | | * |
| | | * // Or, Promisified |
| | | * |
| | | * import { promisify } from 'node:util'; |
| | | * const pipe = promisify(pipeline); |
| | | * |
| | | * async function do_gzip(input, output) { |
| | | * const gzip = createGzip(); |
| | | * const source = createReadStream(input); |
| | | * const destination = createWriteStream(output); |
| | | * await pipe(source, gzip, destination); |
| | | * } |
| | | * |
| | | * do_gzip('input.txt', 'input.txt.gz') |
| | | * .catch((err) => { |
| | | * console.error('An error occurred:', err); |
| | | * process.exitCode = 1; |
| | | * }); |
| | | * ``` |
| | | * |
| | | * It is also possible to compress or decompress data in a single step: |
| | | * |
| | | * ```js |
| | | * import { deflate, unzip } from 'node:zlib'; |
| | | * |
| | | * const input = '.................................'; |
| | | * deflate(input, (err, buffer) => { |
| | | * if (err) { |
| | | * console.error('An error occurred:', err); |
| | | * process.exitCode = 1; |
| | | * } |
| | | * console.log(buffer.toString('base64')); |
| | | * }); |
| | | * |
| | | * const buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64'); |
| | | * unzip(buffer, (err, buffer) => { |
| | | * if (err) { |
| | | * console.error('An error occurred:', err); |
| | | * process.exitCode = 1; |
| | | * } |
| | | * console.log(buffer.toString()); |
| | | * }); |
| | | * |
| | | * // Or, Promisified |
| | | * |
| | | * import { promisify } from 'node:util'; |
| | | * const do_unzip = promisify(unzip); |
| | | * |
| | | * do_unzip(buffer) |
| | | * .then((buf) => console.log(buf.toString())) |
| | | * .catch((err) => { |
| | | * console.error('An error occurred:', err); |
| | | * process.exitCode = 1; |
| | | * }); |
| | | * ``` |
| | | * @since v0.5.8 |
| | | * @see [source](https://github.com/nodejs/node/blob/v25.x/lib/zlib.js) |
| | | */ |
| | | declare module "node:zlib" { |
| | | import { NonSharedBuffer } from "node:buffer"; |
| | | import * as stream from "node:stream"; |
| | |
| | | */ |
| | | chunkSize?: number | undefined; |
| | | windowBits?: number | undefined; |
| | | level?: number | undefined; // compression only |
| | | memLevel?: number | undefined; // compression only |
| | | strategy?: number | undefined; // compression only |
| | | dictionary?: NodeJS.ArrayBufferView | ArrayBuffer | undefined; // deflate/inflate only, empty dictionary by default |
| | | /** compression only */ |
| | | level?: number | undefined; |
| | | /** compression only */ |
| | | memLevel?: number | undefined; |
| | | /** compression only */ |
| | | strategy?: number | undefined; |
| | | /** deflate/inflate only, empty dictionary by default */ |
| | | dictionary?: NodeJS.ArrayBufferView | ArrayBuffer | undefined; |
| | | /** |
| | | * If `true`, returns an object with `buffer` and `engine`. |
| | | */ |
| | |
| | | interface ZlibReset { |
| | | reset(): void; |
| | | } |
| | | /** |
| | | * @since v10.16.0 |
| | | */ |
| | | class BrotliCompress extends stream.Transform { |
| | | constructor(options?: BrotliOptions); |
| | | } |
| | | interface BrotliCompress extends stream.Transform, Zlib {} |
| | | /** |
| | | * @since v10.16.0 |
| | | */ |
| | | class BrotliDecompress extends stream.Transform { |
| | | constructor(options?: BrotliOptions); |
| | | } |
| | | interface BrotliDecompress extends stream.Transform, Zlib {} |
| | | /** |
| | | * @since v0.5.8 |
| | | */ |
| | | class Gzip extends stream.Transform { |
| | | constructor(options?: ZlibOptions); |
| | | } |
| | | interface Gzip extends stream.Transform, Zlib {} |
| | | /** |
| | | * @since v0.5.8 |
| | | */ |
| | | class Gunzip extends stream.Transform { |
| | | constructor(options?: ZlibOptions); |
| | | } |
| | | interface Gunzip extends stream.Transform, Zlib {} |
| | | /** |
| | | * @since v0.5.8 |
| | | */ |
| | | class Deflate extends stream.Transform { |
| | | constructor(options?: ZlibOptions); |
| | | } |
| | | interface Deflate extends stream.Transform, Zlib, ZlibReset, ZlibParams {} |
| | | /** |
| | | * @since v0.5.8 |
| | | */ |
| | | class Inflate extends stream.Transform { |
| | | constructor(options?: ZlibOptions); |
| | | } |
| | | interface Inflate extends stream.Transform, Zlib, ZlibReset {} |
| | | /** |
| | | * @since v0.5.8 |
| | | */ |
| | | class DeflateRaw extends stream.Transform { |
| | | constructor(options?: ZlibOptions); |
| | | } |
| | | interface DeflateRaw extends stream.Transform, Zlib, ZlibReset, ZlibParams {} |
| | | /** |
| | | * @since v0.5.8 |
| | | */ |
| | | class InflateRaw extends stream.Transform { |
| | | constructor(options?: ZlibOptions); |
| | | } |
| | | interface InflateRaw extends stream.Transform, Zlib, ZlibReset {} |
| | | /** |
| | | * @since v0.5.8 |
| | | */ |
| | | class Unzip extends stream.Transform { |
| | | constructor(options?: ZlibOptions); |
| | | } |
| | | interface Unzip extends stream.Transform, Zlib {} |
| | | /** |
| | | * @since v22.15.0 |
| | | * @experimental |
| | | */ |
| | | class ZstdCompress extends stream.Transform { |
| | | constructor(options?: ZstdOptions); |
| | | } |
| | | interface ZstdCompress extends stream.Transform, Zlib {} |
| | | /** |
| | | * @since v22.15.0 |
| | | * @experimental |
| | | */ |
| | | class ZstdDecompress extends stream.Transform { |
| | | constructor(options?: ZstdOptions); |
| | | } |
| | | interface ZstdDecompress extends stream.Transform, Zlib {} |
| | | /** |
| | | * Computes a 32-bit [Cyclic Redundancy Check](https://en.wikipedia.org/wiki/Cyclic_redundancy_check) checksum of `data`. |