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
@@ -978,6 +1039,51 @@ added: v0.7.0
978
1039
Reset the compressor/decompressor to factory defaults. Only applicable to
979
1040
the inflate and deflate algorithms.
980
1041
1042
+ ## Class: ` ZstdOptions `
1043
+
1044
+ <!-- YAML
1045
+ added: REPLACEME
1046
+ -->
1047
+
1048
+ <!-- type=misc-->
1049
+
1050
+ Each Zstd-based class takes an ` options ` object. All options are optional.
1051
+
1052
+ * ` flush ` {integer} ** Default:** ` zlib.constants.ZSTD_e_continue `
1053
+ * ` finishFlush ` {integer} ** Default:** ` zlib.constants.ZSTD_e_end `
1054
+ * ` chunkSize ` {integer} ** Default:** ` 16 * 1024 `
1055
+ * ` params ` {Object} Key-value object containing indexed [ Zstd parameters] [ ] .
1056
+ * ` maxOutputLength ` {integer} Limits output size when using
1057
+ [ convenience methods] [ ] . ** Default:** [ ` buffer.kMaxLength ` ] [ ]
1058
+
1059
+ For example:
1060
+
1061
+ ``` js
1062
+ const stream = zlib .createZstdCompress ({
1063
+ chunkSize: 32 * 1024 ,
1064
+ params: {
1065
+ [zlib .constants .ZSTD_c_compressionLevel ]: 10 ,
1066
+ [zlib .constants .ZSTD_c_checksumFlag ]: 1 ,
1067
+ },
1068
+ });
1069
+ ```
1070
+
1071
+ ## Class: ` zlib.ZstdCompress `
1072
+
1073
+ <!-- YAML
1074
+ added: REPLACEME
1075
+ -->
1076
+
1077
+ Compress data using the Zstd algorithm.
1078
+
1079
+ ## Class: ` zlib.ZstdDecompress `
1080
+
1081
+ <!-- YAML
1082
+ added: REPLACEME
1083
+ -->
1084
+
1085
+ Decompress data using the Zstd algorithm.
1086
+
981
1087
## ` zlib.constants `
982
1088
983
1089
<!-- YAML
@@ -1149,6 +1255,26 @@ added: v0.5.8
1149
1255
1150
1256
Creates and returns a new [ ` Unzip ` ] [ ] object.
1151
1257
1258
+ ## ` zlib.createZstdCompress([options]) `
1259
+
1260
+ <!-- YAML
1261
+ added: REPLACEME
1262
+ -->
1263
+
1264
+ * ` options ` {zstd options}
1265
+
1266
+ Creates and returns a new [ ` ZstdCompress ` ] [ ] object.
1267
+
1268
+ ## ` zlib.createZstdDecompress([options]) `
1269
+
1270
+ <!-- YAML
1271
+ added: REPLACEME
1272
+ -->
1273
+
1274
+ * ` options ` {zstd options}
1275
+
1276
+ Creates and returns a new [ ` ZstdDecompress ` ] [ ] object.
1277
+
1152
1278
## Convenience methods
1153
1279
1154
1280
<!-- type=misc-->
@@ -1495,11 +1621,54 @@ changes:
1495
1621
1496
1622
Decompress a chunk of data with [ ` Unzip ` ] [ ] .
1497
1623
1624
+ ### ` zlib.zstdCompress(buffer[, options], callback) `
1625
+
1626
+ <!-- YAML
1627
+ added: REPLACEME
1628
+ -->
1629
+
1630
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1631
+ * ` options ` {zstd options}
1632
+ * ` callback ` {Function}
1633
+
1634
+ ### ` zlib.zstdCompressSync(buffer[, options]) `
1635
+
1636
+ <!-- YAML
1637
+ added: REPLACEME
1638
+ -->
1639
+
1640
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1641
+ * ` options ` {zstd options}
1642
+
1643
+ Compress a chunk of data with [ ` ZstdCompress ` ] [ ] .
1644
+
1645
+ ### ` zlib.zstdDecompress(buffer[, options], callback) `
1646
+
1647
+ <!-- YAML
1648
+ added: REPLACEME
1649
+ -->
1650
+
1651
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1652
+ * ` options ` {zstd options}
1653
+ * ` callback ` {Function}
1654
+
1655
+ ### ` zlib.zstdDecompressSync(buffer[, options]) `
1656
+
1657
+ <!-- YAML
1658
+ added: REPLACEME
1659
+ -->
1660
+
1661
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1662
+ * ` options ` {zstd options}
1663
+
1664
+ Decompress a chunk of data with [ ` ZstdDecompress ` ] [ ] .
1665
+
1498
1666
[ Brotli parameters ] : #brotli-constants
1499
1667
[ Cyclic redundancy check ] : https://en.wikipedia.org/wiki/Cyclic_redundancy_check
1500
1668
[ Memory usage tuning ] : #memory-usage-tuning
1501
1669
[ RFC 7932 ] : https://www.rfc-editor.org/rfc/rfc7932.txt
1502
1670
[ Streams API ] : stream.md
1671
+ [ Zstd parameters ] : #zstd-constants
1503
1672
[ `.flush()` ] : #zlibflushkind-callback
1504
1673
[ `Accept-Encoding` ] : https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
1505
1674
[ `BrotliCompress` ] : #class-zlibbrotlicompress
@@ -1512,6 +1681,8 @@ Decompress a chunk of data with [`Unzip`][].
1512
1681
[ `InflateRaw` ] : #class-zlibinflateraw
1513
1682
[ `Inflate` ] : #class-zlibinflate
1514
1683
[ `Unzip` ] : #class-zlibunzip
1684
+ [ `ZstdCompress` ] : #class-zlibzstdcompress
1685
+ [ `ZstdDecompress` ] : #class-zlibzstddecompress
1515
1686
[ `buffer.kMaxLength` ] : buffer.md#bufferkmaxlength
1516
1687
[ `deflateInit2` and `inflateInit2` ] : https://zlib.net/manual.html#Advanced
1517
1688
[ `stream.Transform` ] : stream.md#class-streamtransform
0 commit comments