@@ -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,50 @@ 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
+ #### Pledged Source Size
537
+
538
+ It's possible to specify the expected total size of the uncompressed input via
539
+ ` opts.pledgedSrcSize ` . If the size doesn't match at the end of the input,
540
+ compression will fail with the code ` ZSTD_error_srcSize_wrong ` .
541
+
542
+ #### Decompressor options
543
+
544
+ These advanced options are available for controlling decompression:
545
+
546
+ * ` ZSTD_d_windowLogMax `
547
+ * Select a size limit (in power of 2) beyond which the streaming API will
548
+ refuse to allocate memory buffer in order to protect the host from
549
+ unreasonable memory requirements.
550
+
490
551
## Class: ` Options `
491
552
492
553
<!-- YAML
@@ -684,6 +745,51 @@ base class of the compressor/decompressor classes.
684
745
This class inherits from [ ` stream.Transform ` ] [ ] , allowing ` node:zlib ` objects to
685
746
be used in pipes and similar stream operations.
686
747
748
+ ## Class: ` ZstdOptions `
749
+
750
+ <!-- YAML
751
+ added: REPLACEME
752
+ -->
753
+
754
+ <!-- type=misc-->
755
+
756
+ Each Zstd-based class takes an ` options ` object. All options are optional.
757
+
758
+ * ` flush ` {integer} ** Default:** ` zlib.constants.ZSTD_e_continue `
759
+ * ` finishFlush ` {integer} ** Default:** ` zlib.constants.ZSTD_e_end `
760
+ * ` chunkSize ` {integer} ** Default:** ` 16 * 1024 `
761
+ * ` params ` {Object} Key-value object containing indexed [ Zstd parameters] [ ] .
762
+ * ` maxOutputLength ` {integer} Limits output size when using
763
+ [ convenience methods] [ ] . ** Default:** [ ` buffer.kMaxLength ` ] [ ]
764
+
765
+ For example:
766
+
767
+ ``` js
768
+ const stream = zlib .createZstdCompress ({
769
+ chunkSize: 32 * 1024 ,
770
+ params: {
771
+ [zlib .constants .ZSTD_c_compressionLevel ]: 10 ,
772
+ [zlib .constants .ZSTD_c_checksumFlag ]: 1 ,
773
+ },
774
+ });
775
+ ```
776
+
777
+ ## Class: ` zlib.ZstdCompress `
778
+
779
+ <!-- YAML
780
+ added: REPLACEME
781
+ -->
782
+
783
+ Compress data using the Zstd algorithm.
784
+
785
+ ## Class: ` zlib.ZstdDecompress `
786
+
787
+ <!-- YAML
788
+ added: REPLACEME
789
+ -->
790
+
791
+ Decompress data using the Zstd algorithm.
792
+
687
793
### ` zlib.bytesRead `
688
794
689
795
<!-- YAML
@@ -937,6 +1043,26 @@ added: v0.5.8
937
1043
938
1044
Creates and returns a new [ ` Unzip ` ] [ ] object.
939
1045
1046
+ ## ` zlib.createZstdCompress([options]) `
1047
+
1048
+ <!-- YAML
1049
+ added: REPLACEME
1050
+ -->
1051
+
1052
+ * ` options ` {zstd options}
1053
+
1054
+ Creates and returns a new [ ` ZstdCompress ` ] [ ] object.
1055
+
1056
+ ## ` zlib.createZstdDecompress([options]) `
1057
+
1058
+ <!-- YAML
1059
+ added: REPLACEME
1060
+ -->
1061
+
1062
+ * ` options ` {zstd options}
1063
+
1064
+ Creates and returns a new [ ` ZstdDecompress ` ] [ ] object.
1065
+
940
1066
## Convenience methods
941
1067
942
1068
<!-- type=misc-->
@@ -1283,11 +1409,54 @@ changes:
1283
1409
1284
1410
Decompress a chunk of data with [ ` Unzip ` ] [ ] .
1285
1411
1412
+ ### ` zlib.zstdCompress(buffer[, options], callback) `
1413
+
1414
+ <!-- YAML
1415
+ added: REPLACEME
1416
+ -->
1417
+
1418
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1419
+ * ` options ` {zstd options}
1420
+ * ` callback ` {Function}
1421
+
1422
+ ### ` zlib.zstdCompressSync(buffer[, options]) `
1423
+
1424
+ <!-- YAML
1425
+ added: REPLACEME
1426
+ -->
1427
+
1428
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1429
+ * ` options ` {zstd options}
1430
+
1431
+ Compress a chunk of data with [ ` ZstdCompress ` ] [ ] .
1432
+
1433
+ ### ` zlib.zstdDecompress(buffer[, options], callback) `
1434
+
1435
+ <!-- YAML
1436
+ added: REPLACEME
1437
+ -->
1438
+
1439
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1440
+ * ` options ` {zstd options}
1441
+ * ` callback ` {Function}
1442
+
1443
+ ### ` zlib.zstdDecompressSync(buffer[, options]) `
1444
+
1445
+ <!-- YAML
1446
+ added: REPLACEME
1447
+ -->
1448
+
1449
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
1450
+ * ` options ` {zstd options}
1451
+
1452
+ Decompress a chunk of data with [ ` ZstdDecompress ` ] [ ] .
1453
+
1286
1454
[ Brotli parameters ] : #brotli-constants
1287
1455
[ Cyclic redundancy check ] : https://en.wikipedia.org/wiki/Cyclic_redundancy_check
1288
1456
[ Memory usage tuning ] : #memory-usage-tuning
1289
1457
[ RFC 7932 ] : https://www.rfc-editor.org/rfc/rfc7932.txt
1290
1458
[ Streams API ] : stream.md
1459
+ [ Zstd parameters ] : #zstd-constants
1291
1460
[ `.flush()` ] : #zlibflushkind-callback
1292
1461
[ `Accept-Encoding` ] : https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
1293
1462
[ `ArrayBuffer` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
@@ -1304,6 +1473,8 @@ Decompress a chunk of data with [`Unzip`][].
1304
1473
[ `Inflate` ] : #class-zlibinflate
1305
1474
[ `TypedArray` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
1306
1475
[ `Unzip` ] : #class-zlibunzip
1476
+ [ `ZstdCompress` ] : #class-zlibzstdcompress
1477
+ [ `ZstdDecompress` ] : #class-zlibzstddecompress
1307
1478
[ `buffer.kMaxLength` ] : buffer.md#bufferkmaxlength
1308
1479
[ `deflateInit2` and `inflateInit2` ] : https://zlib.net/manual.html#Advanced
1309
1480
[ `stream.Transform` ] : stream.md#class-streamtransform
0 commit comments