@@ -124,8 +124,8 @@ operations be cached to avoid duplication of effort.
124
124
125
125
## Compressing HTTP requests and responses
126
126
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
129
129
[ HTTP] ( https://tools.ietf.org/html/rfc7230#section-4.2 ) .
130
130
131
131
The HTTP [ ` Accept-Encoding ` ] [ ] header is used within an HTTP request to identify
@@ -148,7 +148,7 @@ const { pipeline } = require('node:stream');
148
148
const request = http .get ({ host: ' example.com' ,
149
149
path: ' /' ,
150
150
port: 80 ,
151
- headers: { ' Accept-Encoding' : ' br,gzip,deflate' } });
151
+ headers: { ' Accept-Encoding' : ' br,gzip,deflate,zstd ' } });
152
152
request .on (' response' , (response ) => {
153
153
const output = fs .createWriteStream (' example.com_index.html' );
154
154
@@ -170,6 +170,9 @@ request.on('response', (response) => {
170
170
case ' deflate' :
171
171
pipeline (response, zlib .createInflate (), output, onError);
172
172
break ;
173
+ case ' zstd' :
174
+ pipeline (response, zlib .createZstdDecompress (), output, onError);
175
+ break ;
173
176
default :
174
177
pipeline (response, output, onError);
175
178
break ;
@@ -218,6 +221,9 @@ http.createServer((request, response) => {
218
221
} else if (/ \b br\b / .test (acceptEncoding)) {
219
222
response .writeHead (200 , { ' Content-Encoding' : ' br' });
220
223
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);
221
227
} else {
222
228
response .writeHead (200 , {});
223
229
pipeline (raw, response, onError);
@@ -238,6 +244,7 @@ const buffer = Buffer.from('eJzT0yMA', 'base64');
238
244
zlib .unzip (
239
245
buffer,
240
246
// For Brotli, the equivalent is zlib.constants.BROTLI_OPERATION_FLUSH.
247
+ // For Zstd, the equivalent is zlib.constants.ZSTD_e_flush.
241
248
{ finishFlush: zlib .constants .Z_SYNC_FLUSH },
242
249
(err , buffer ) => {
243
250
if (err) {
@@ -309,6 +316,16 @@ these options have different ranges than the zlib ones:
309
316
310
317
See [ below] [ Brotli parameters ] for more details on Brotli-specific options.
311
318
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
+
312
329
## Flushing
313
330
314
331
Calling [ ` .flush() ` ] [ ] on a compression stream will make ` zlib ` return as much
@@ -487,6 +504,44 @@ These advanced options are available for controlling decompression:
487
504
* Boolean flag enabling “Large Window Brotli” mode (not compatible with the
488
505
Brotli format as standardized in [ RFC 7932] [ ] ).
489
506
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
+
490
545
## Class: ` Options `
491
546
492
547
<!-- YAML
@@ -684,6 +739,51 @@ base class of the compressor/decompressor classes.
684
739
This class inherits from [ ` stream.Transform ` ] [ ] , allowing ` node:zlib ` objects to
685
740
be used in pipes and similar stream operations.
686
741
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
+
687
787
### ` zlib.bytesRead `
688
788
689
789
<!-- YAML
@@ -874,6 +974,26 @@ added: v0.5.8
874
974
875
975
Creates and returns a new [ ` Unzip ` ] [ ] object.
876
976
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
+
877
997
## Convenience methods
878
998
879
999
<!-- type=misc-->
@@ -1220,10 +1340,53 @@ changes:
1220
1340
1221
1341
Decompress a chunk of data with [ ` Unzip ` ] [ ] .
1222
1342
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
+
1223
1385
[ Brotli parameters ] : #brotli-constants
1224
1386
[ Memory usage tuning ] : #memory-usage-tuning
1225
1387
[ RFC 7932 ] : https://www.rfc-editor.org/rfc/rfc7932.txt
1226
1388
[ Streams API ] : stream.md
1389
+ [ Zstd parameters ] : #zstd-constants
1227
1390
[ `.flush()` ] : #zlibflushkind-callback
1228
1391
[ `Accept-Encoding` ] : https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
1229
1392
[ `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`][].
1240
1403
[ `Inflate` ] : #class-zlibinflate
1241
1404
[ `TypedArray` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
1242
1405
[ `Unzip` ] : #class-zlibunzip
1406
+ [ `ZstdCompress` ] : #class-zlibzstdcompress
1407
+ [ `ZstdDecompress` ] : #class-zlibzstddecompress
1243
1408
[ `buffer.kMaxLength` ] : buffer.md#bufferkmaxlength
1244
1409
[ `deflateInit2` and `inflateInit2` ] : https://zlib.net/manual.html#Advanced
1245
1410
[ `stream.Transform` ] : stream.md#class-streamtransform
0 commit comments