7
7
<!-- source_link=lib/zlib.js -->
8
8
9
9
The ` node:zlib ` module provides compression functionality implemented using
10
- Gzip, Deflate/Inflate, and Brotli .
10
+ Gzip, Deflate/Inflate, Brotli, and Zstd .
11
11
12
12
To access it:
13
13
@@ -220,8 +220,8 @@ operations be cached to avoid duplication of effort.
220
220
221
221
## Compressing HTTP requests and responses
222
222
223
- The ` node:zlib ` module can be used to implement support for the ` gzip ` , ` deflate `
224
- and ` br ` content-encoding mechanisms defined by
223
+ The ` node:zlib ` module can be used to implement support for the ` gzip ` , ` deflate ` ,
224
+ ` br ` , and ` zstd ` content-encoding mechanisms defined by
225
225
[ HTTP] ( https://tools.ietf.org/html/rfc7230#section-4.2 ) .
226
226
227
227
The HTTP [ ` Accept-Encoding ` ] [ ] header is used within an HTTP request to identify
@@ -284,7 +284,7 @@ const { pipeline } = require('node:stream');
284
284
const request = http .get ({ host: ' example.com' ,
285
285
path: ' /' ,
286
286
port: 80 ,
287
- headers: { ' Accept-Encoding' : ' br,gzip,deflate' } });
287
+ headers: { ' Accept-Encoding' : ' br,gzip,deflate,zstd ' } });
288
288
request .on (' response' , (response ) => {
289
289
const output = fs .createWriteStream (' example.com_index.html' );
290
290
@@ -306,6 +306,9 @@ request.on('response', (response) => {
306
306
case ' deflate' :
307
307
pipeline (response, zlib .createInflate (), output, onError);
308
308
break ;
309
+ case ' zstd' :
310
+ pipeline (response, zlib .createZstdDecompress (), output, onError);
311
+ break ;
309
312
default :
310
313
pipeline (response, output, onError);
311
314
break ;
@@ -396,6 +399,9 @@ http.createServer((request, response) => {
396
399
} else if (/ \b br\b / .test (acceptEncoding)) {
397
400
response .writeHead (200 , { ' Content-Encoding' : ' br' });
398
401
pipeline (raw, zlib .createBrotliCompress (), response, onError);
402
+ } else if (/ \b zstd\b / .test (acceptEncoding)) {
403
+ response .writeHead (200 , { ' Content-Encoding' : ' zstd' });
404
+ pipeline (raw, zlib .createZstdCompress (), response, onError);
399
405
} else {
400
406
response .writeHead (200 , {});
401
407
pipeline (raw, response, onError);
@@ -416,6 +422,7 @@ const buffer = Buffer.from('eJzT0yMA', 'base64');
416
422
zlib .unzip (
417
423
buffer,
418
424
// For Brotli, the equivalent is zlib.constants.BROTLI_OPERATION_FLUSH.
425
+ // For Zstd, the equivalent is zlib.constants.ZSTD_e_flush.
419
426
{ finishFlush: zlib .constants .Z_SYNC_FLUSH },
420
427
(err , buffer ) => {
421
428
if (err) {
@@ -487,6 +494,16 @@ these options have different ranges than the zlib ones:
487
494
488
495
See [ below] [ Brotli parameters ] for more details on Brotli-specific options.
489
496
497
+ ### For Zstd-based streams
498
+
499
+ There are equivalents to the zlib options for Zstd-based streams, although
500
+ these options have different ranges than the zlib ones:
501
+
502
+ * zlib's ` level ` option matches Zstd's ` ZSTD_c_compressionLevel ` option.
503
+ * zlib's ` windowBits ` option matches Zstd's ` ZSTD_c_windowLog ` option.
504
+
505
+ See [ below] [ Zstd parameters ] for more details on Zstd-specific options.
506
+
490
507
## Flushing
491
508
492
509
Calling [ ` .flush() ` ] [ ] on a compression stream will make ` zlib ` return as much
@@ -701,6 +718,50 @@ These advanced options are available for controlling decompression:
701
718
* Boolean flag enabling “Large Window Brotli” mode (not compatible with the
702
719
Brotli format as standardized in [ RFC 7932] [ ] ).
703
720
721
+ ### Zstd constants
722
+
723
+ <!-- YAML
724
+ added: REPLACEME
725
+ -->
726
+
727
+ There are several options and other constants available for Zstd-based
728
+ streams:
729
+
730
+ #### Flush operations
731
+
732
+ The following values are valid flush operations for Zstd-based streams:
733
+
734
+ * ` zlib.constants.ZSTD_e_continue ` (default for all operations)
735
+ * ` zlib.constants.ZSTD_e_flush ` (default when calling ` .flush() ` )
736
+ * ` zlib.constants.ZSTD_e_end ` (default for the last chunk)
737
+
738
+ #### Compressor options
739
+
740
+ There are several options that can be set on Zstd encoders, affecting
741
+ compression efficiency and speed. Both the keys and the values can be accessed
742
+ as properties of the ` zlib.constants ` object.
743
+
744
+ The most important options are:
745
+
746
+ * ` ZSTD_c_compressionLevel `
747
+ * Set compression parameters according to pre-defined cLevel table. Default
748
+ level is ZSTD\_ CLEVEL\_ DEFAULT==3.
749
+
750
+ #### Pledged Source Size
751
+
752
+ It's possible to specify the expected total size of the uncompressed input via
753
+ ` opts.pledgedSrcSize ` . If the size doesn't match at the end of the input,
754
+ compression will fail with the code ` ZSTD_error_srcSize_wrong ` .
755
+
756
+ #### Decompressor options
757
+
758
+ These advanced options are available for controlling decompression:
759
+
760
+ * ` ZSTD_d_windowLogMax `
761
+ * Select a size limit (in power of 2) beyond which the streaming API will
762
+ refuse to allocate memory buffer in order to protect the host from
763
+ unreasonable memory requirements.
764
+
704
765
## Class: ` Options `
705
766
706
767
<!-- YAML
@@ -962,6 +1023,51 @@ added: v0.7.0
962
1023
Reset the compressor/decompressor to factory defaults. Only applicable to
963
1024
the inflate and deflate algorithms.
964
1025
1026
+ ## Class: ` ZstdOptions `
1027
+
1028
+ <!-- YAML
1029
+ added: REPLACEME
1030
+ -->
1031
+
1032
+ <!-- type=misc-->
1033
+
1034
+ Each Zstd-based class takes an ` options ` object. All options are optional.
1035
+
1036
+ * ` flush ` {integer} ** Default:** ` zlib.constants.ZSTD_e_continue `
1037
+ * ` finishFlush ` {integer} ** Default:** ` zlib.constants.ZSTD_e_end `
1038
+ * ` chunkSize ` {integer} ** Default:** ` 16 * 1024 `
1039
+ * ` params ` {Object} Key-value object containing indexed [ Zstd parameters] [ ] .
1040
+ * ` maxOutputLength ` {integer} Limits output size when using
1041
+ [ convenience methods] [ ] . ** Default:** [ ` buffer.kMaxLength ` ] [ ]
1042
+
1043
+ For example:
1044
+
1045
+ ``` js
1046
+ const stream = zlib .createZstdCompress ({
1047
+ chunkSize: 32 * 1024 ,
1048
+ params: {
1049
+ [zlib .constants .ZSTD_c_compressionLevel ]: 10 ,
1050
+ [zlib .constants .ZSTD_c_checksumFlag ]: 1 ,
1051
+ },
1052
+ });
1053
+ ```
1054
+
1055
+ ## Class: ` zlib.ZstdCompress `
1056
+
1057
+ <!-- YAML
1058
+ added: REPLACEME
1059
+ -->
1060
+
1061
+ Compress data using the Zstd algorithm.
1062
+
1063
+ ## Class: ` zlib.ZstdDecompress `
1064
+
1065
+ <!-- YAML
1066
+ added: REPLACEME
1067
+ -->
1068
+
1069
+ Decompress data using the Zstd algorithm.
1070
+
965
1071
## ` zlib.constants `
966
1072
967
1073
<!-- YAML
@@ -1135,6 +1241,26 @@ added: v0.5.8
1135
1241
1136
1242
Creates and returns a new [ ` Unzip ` ] [ ] object.
1137
1243
1244
+ ## ` zlib.createZstdCompress([options]) `
1245
+
1246
+ <!-- YAML
1247
+ added: REPLACEME
1248
+ -->
1249
+
1250
+ * ` options ` {zstd options}
1251
+
1252
+ Creates and returns a new [ ` ZstdCompress ` ] [ ] object.
1253
+
1254
+ ## ` zlib.createZstdDecompress([options]) `
1255
+
1256
+ <!-- YAML
1257
+ added: REPLACEME
1258
+ -->
1259
+
1260
+ * ` options ` {zstd options}
1261
+
1262
+ Creates and returns a new [ ` ZstdDecompress ` ] [ ] object.
1263
+
1138
1264
## Convenience methods
1139
1265
1140
1266
<!-- type=misc-->
@@ -1481,11 +1607,54 @@ changes:
1481
1607
1482
1608
Decompress a chunk of data with [ ` Unzip ` ] [ ] .
1483
1609
1610
+ ### ` zlib.zstdCompress(buffer[, options], callback) `
1611
+
1612
+ <!-- YAML
1613
+ added: REPLACEME
1614
+ -->
1615
+
1616
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1617
+ * ` options ` {zstd options}
1618
+ * ` callback ` {Function}
1619
+
1620
+ ### ` zlib.zstdCompressSync(buffer[, options]) `
1621
+
1622
+ <!-- YAML
1623
+ added: REPLACEME
1624
+ -->
1625
+
1626
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1627
+ * ` options ` {zstd options}
1628
+
1629
+ Compress a chunk of data with [ ` ZstdCompress ` ] [ ] .
1630
+
1631
+ ### ` zlib.zstdDecompress(buffer[, options], callback) `
1632
+
1633
+ <!-- YAML
1634
+ added: REPLACEME
1635
+ -->
1636
+
1637
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1638
+ * ` options ` {zstd options}
1639
+ * ` callback ` {Function}
1640
+
1641
+ ### ` zlib.zstdDecompressSync(buffer[, options]) `
1642
+
1643
+ <!-- YAML
1644
+ added: REPLACEME
1645
+ -->
1646
+
1647
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1648
+ * ` options ` {zstd options}
1649
+
1650
+ Decompress a chunk of data with [ ` ZstdDecompress ` ] [ ] .
1651
+
1484
1652
[ Brotli parameters ] : #brotli-constants
1485
1653
[ Cyclic redundancy check ] : https://en.wikipedia.org/wiki/Cyclic_redundancy_check
1486
1654
[ Memory usage tuning ] : #memory-usage-tuning
1487
1655
[ RFC 7932 ] : https://www.rfc-editor.org/rfc/rfc7932.txt
1488
1656
[ Streams API ] : stream.md
1657
+ [ Zstd parameters ] : #zstd-constants
1489
1658
[ `.flush()` ] : #zlibflushkind-callback
1490
1659
[ `Accept-Encoding` ] : https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
1491
1660
[ `BrotliCompress` ] : #class-zlibbrotlicompress
@@ -1498,6 +1667,8 @@ Decompress a chunk of data with [`Unzip`][].
1498
1667
[ `InflateRaw` ] : #class-zlibinflateraw
1499
1668
[ `Inflate` ] : #class-zlibinflate
1500
1669
[ `Unzip` ] : #class-zlibunzip
1670
+ [ `ZstdCompress` ] : #class-zlibzstdcompress
1671
+ [ `ZstdDecompress` ] : #class-zlibzstddecompress
1501
1672
[ `buffer.kMaxLength` ] : buffer.md#bufferkmaxlength
1502
1673
[ `deflateInit2` and `inflateInit2` ] : https://zlib.net/manual.html#Advanced
1503
1674
[ `stream.Transform` ] : stream.md#class-streamtransform
0 commit comments