@@ -124,8 +124,8 @@ operations be cached to avoid duplication of effort.
124124
125125## Compressing HTTP requests and responses
126126
127- The ` node:zlib ` module can be used to implement support for the ` gzip ` , ` deflate `
128- and ` br ` content-encoding mechanisms defined by
127+ The ` node:zlib ` module can be used to implement support for the ` gzip ` , ` deflate ` ,
128+ ` br ` and ` zstd ` content-encoding mechanisms defined by
129129[ HTTP] ( https://tools.ietf.org/html/rfc7230#section-4.2 ) .
130130
131131The HTTP [ ` Accept-Encoding ` ] [ ] header is used within an HTTP request to identify
@@ -148,7 +148,7 @@ const { pipeline } = require('node:stream');
148148const request = http .get ({ host: ' example.com' ,
149149 path: ' /' ,
150150 port: 80 ,
151- headers: { ' Accept-Encoding' : ' br,gzip,deflate' } });
151+ headers: { ' Accept-Encoding' : ' br,gzip,deflate,zstd ' } });
152152request .on (' response' , (response ) => {
153153 const output = fs .createWriteStream (' example.com_index.html' );
154154
@@ -170,6 +170,9 @@ request.on('response', (response) => {
170170 case ' deflate' :
171171 pipeline (response, zlib .createInflate (), output, onError);
172172 break ;
173+ case ' zstd' :
174+ pipeline (response, zlib .createZstdDecompress (), output, onError);
175+ break ;
173176 default :
174177 pipeline (response, output, onError);
175178 break ;
@@ -218,6 +221,9 @@ http.createServer((request, response) => {
218221 } else if (/ \b br\b / .test (acceptEncoding)) {
219222 response .writeHead (200 , { ' Content-Encoding' : ' br' });
220223 pipeline (raw, zlib .createBrotliCompress (), response, onError);
224+ } else if (/ \b zstd\b / .test (acceptEncoding)) {
225+ response .writeHead (200 , { ' Content-Encoding' : ' zstd' });
226+ pipeline (raw, zlib .createZstdCompress (), response, onError);
221227 } else {
222228 response .writeHead (200 , {});
223229 pipeline (raw, response, onError);
@@ -238,6 +244,7 @@ const buffer = Buffer.from('eJzT0yMA', 'base64');
238244zlib .unzip (
239245 buffer,
240246 // For Brotli, the equivalent is zlib.constants.BROTLI_OPERATION_FLUSH.
247+ // For Zstd, the equivalent is zlib.constants.ZSTD_e_flush.
241248 { finishFlush: zlib .constants .Z_SYNC_FLUSH },
242249 (err , buffer ) => {
243250 if (err) {
@@ -309,6 +316,16 @@ these options have different ranges than the zlib ones:
309316
310317See [ below] [ Brotli parameters ] for more details on Brotli-specific options.
311318
319+ ### For Zstd-based streams
320+
321+ There are equivalents to the zlib options for Zstd-based streams, although
322+ these options have different ranges than the zlib ones:
323+
324+ * zlib's ` level ` option matches Zstd's ` ZSTD_c_compressionLevel ` option.
325+ * zlib's ` windowBits ` option matches Zstd's ` ZSTD_c_windowLog ` option.
326+
327+ See [ below] [ Zstd parameters ] for more details on Zstd-specific options.
328+
312329## Flushing
313330
314331Calling [ ` .flush() ` ] [ ] on a compression stream will make ` zlib ` return as much
@@ -487,6 +504,44 @@ These advanced options are available for controlling decompression:
487504 * Boolean flag enabling “Large Window Brotli” mode (not compatible with the
488505 Brotli format as standardized in [ RFC 7932] [ ] ).
489506
507+ ### Zstd constants
508+
509+ <!-- YAML
510+ added: REPLACEME
511+ -->
512+
513+ There are several options and other constants available for Zstd-based
514+ streams:
515+
516+ #### Flush operations
517+
518+ The following values are valid flush operations for Zstd-based streams:
519+
520+ * ` zlib.constants.ZSTD_e_continue ` (default for all operations)
521+ * ` zlib.constants.ZSTD_e_flush ` (default when calling ` .flush() ` )
522+ * ` zlib.constants.ZSTD_e_end ` (default for the last chunk)
523+
524+ #### Compressor options
525+
526+ There are several options that can be set on Zstd encoders, affecting
527+ compression efficiency and speed. Both the keys and the values can be accessed
528+ as properties of the ` zlib.constants ` object.
529+
530+ The most important options are:
531+
532+ * ` ZSTD_c_compressionLevel `
533+ * Set compression parameters according to pre-defined cLevel table. Default
534+ level is ZSTD\_ CLEVEL\_ DEFAULT==3.
535+
536+ #### Decompressor options
537+
538+ These advanced options are available for controlling decompression:
539+
540+ * ` ZSTD_d_windowLogMax `
541+ * Select a size limit (in power of 2) beyond which the streaming API will
542+ refuse to allocate memory buffer in order to protect the host from
543+ unreasonable memory requirements.
544+
490545## Class: ` Options `
491546
492547<!-- YAML
@@ -684,6 +739,51 @@ base class of the compressor/decompressor classes.
684739This class inherits from [ ` stream.Transform ` ] [ ] , allowing ` node:zlib ` objects to
685740be used in pipes and similar stream operations.
686741
742+ ## Class: ` ZstdOptions `
743+
744+ <!-- YAML
745+ added: REPLACEME
746+ -->
747+
748+ <!-- type=misc-->
749+
750+ Each Zstd-based class takes an ` options ` object. All options are optional.
751+
752+ * ` flush ` {integer} ** Default:** ` zlib.constants.ZSTD_e_continue `
753+ * ` finishFlush ` {integer} ** Default:** ` zlib.constants.ZSTD_e_end `
754+ * ` chunkSize ` {integer} ** Default:** ` 16 * 1024 `
755+ * ` params ` {Object} Key-value object containing indexed [ Zstd parameters] [ ] .
756+ * ` maxOutputLength ` {integer} Limits output size when using
757+ [ convenience methods] [ ] . ** Default:** [ ` buffer.kMaxLength ` ] [ ]
758+
759+ For example:
760+
761+ ``` js
762+ const stream = zlib .createZstdCompress ({
763+ chunkSize: 32 * 1024 ,
764+ params: {
765+ [zlib .constants .ZSTD_c_compressionLevel ]: 10 ,
766+ [zlib .constants .ZSTD_c_checksumFlag ]: 1 ,
767+ },
768+ });
769+ ```
770+
771+ ## Class: ` zlib.ZstdCompress `
772+
773+ <!-- YAML
774+ added: REPLACEME
775+ -->
776+
777+ Compress data using the Zstd algorithm.
778+
779+ ## Class: ` zlib.ZstdDecompress `
780+
781+ <!-- YAML
782+ added: REPLACEME
783+ -->
784+
785+ Decompress data using the Zstd algorithm.
786+
687787### ` zlib.bytesRead `
688788
689789<!-- YAML
@@ -874,6 +974,26 @@ added: v0.5.8
874974
875975Creates and returns a new [ ` Unzip ` ] [ ] object.
876976
977+ ## ` zlib.createZstdCompress([options]) `
978+
979+ <!-- YAML
980+ added: REPLACEME
981+ -->
982+
983+ * ` options ` {zstd options}
984+
985+ Creates and returns a new [ ` ZstdCompress ` ] [ ] object.
986+
987+ ## ` zlib.createZstdDecompress([options]) `
988+
989+ <!-- YAML
990+ added: REPLACEME
991+ -->
992+
993+ * ` options ` {zstd options}
994+
995+ Creates and returns a new [ ` ZstdDecompress ` ] [ ] object.
996+
877997## Convenience methods
878998
879999<!-- type=misc-->
@@ -1220,10 +1340,53 @@ changes:
12201340
12211341Decompress a chunk of data with [ ` Unzip ` ] [ ] .
12221342
1343+ ### ` zlib.zstdCompress(buffer[, options], callback) `
1344+
1345+ <!-- YAML
1346+ added: REPLACEME
1347+ -->
1348+
1349+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1350+ * ` options ` {zstd options}
1351+ * ` callback ` {Function}
1352+
1353+ ### ` zlib.zstdCompressSync(buffer[, options]) `
1354+
1355+ <!-- YAML
1356+ added: REPLACEME
1357+ -->
1358+
1359+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1360+ * ` options ` {zstd options}
1361+
1362+ Compress a chunk of data with [ ` ZstdCompress ` ] [ ] .
1363+
1364+ ### ` zlib.zstdDecompress(buffer[, options], callback) `
1365+
1366+ <!-- YAML
1367+ added: REPLACEME
1368+ -->
1369+
1370+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1371+ * ` options ` {zstd options}
1372+ * ` callback ` {Function}
1373+
1374+ ### ` zlib.zstdDecompressSync(buffer[, options]) `
1375+
1376+ <!-- YAML
1377+ added: REPLACEME
1378+ -->
1379+
1380+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1381+ * ` options ` {zstd options}
1382+
1383+ Decompress a chunk of data with [ ` ZstdDecompress ` ] [ ] .
1384+
12231385[ Brotli parameters ] : #brotli-constants
12241386[ Memory usage tuning ] : #memory-usage-tuning
12251387[ RFC 7932 ] : https://www.rfc-editor.org/rfc/rfc7932.txt
12261388[ Streams API ] : stream.md
1389+ [ Zstd parameters ] : #zstd-constants
12271390[ `.flush()` ] : #zlibflushkind-callback
12281391[ `Accept-Encoding` ] : https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
12291392[ `ArrayBuffer` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
@@ -1240,6 +1403,8 @@ Decompress a chunk of data with [`Unzip`][].
12401403[ `Inflate` ] : #class-zlibinflate
12411404[ `TypedArray` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
12421405[ `Unzip` ] : #class-zlibunzip
1406+ [ `ZstdCompress` ] : #class-zlibzstdcompress
1407+ [ `ZstdDecompress` ] : #class-zlibzstddecompress
12431408[ `buffer.kMaxLength` ] : buffer.md#bufferkmaxlength
12441409[ `deflateInit2` and `inflateInit2` ] : https://zlib.net/manual.html#Advanced
12451410[ `stream.Transform` ] : stream.md#class-streamtransform
0 commit comments