5
5
> Stability: 2 - Stable
6
6
7
7
The ` zlib ` module provides compression functionality implemented using Gzip and
8
- Deflate/Inflate. It can be accessed using:
8
+ Deflate/Inflate, as well as Brotli . It can be accessed using:
9
9
10
10
``` js
11
11
const zlib = require (' zlib' );
@@ -54,8 +54,8 @@ and/or unrecoverable and catastrophic memory fragmentation.
54
54
55
55
## Compressing HTTP requests and responses
56
56
57
- The ` zlib ` module can be used to implement support for the ` gzip ` and ` deflate `
58
- content-encoding mechanisms defined by
57
+ The ` zlib ` module can be used to implement support for the ` gzip ` , ` deflate `
58
+ and ` br ` content-encoding mechanisms defined by
59
59
[ HTTP] ( https://tools.ietf.org/html/rfc7230#section-4.2 ) .
60
60
61
61
The HTTP [ ` Accept-Encoding ` ] [ ] header is used within an http request to identify
@@ -76,12 +76,15 @@ const fs = require('fs');
76
76
const request = http .get ({ host: ' example.com' ,
77
77
path: ' /' ,
78
78
port: 80 ,
79
- headers: { ' Accept-Encoding' : ' gzip,deflate' } });
79
+ headers: { ' Accept-Encoding' : ' br, gzip,deflate' } });
80
80
request .on (' response' , (response ) => {
81
81
const output = fs .createWriteStream (' example.com_index.html' );
82
82
83
83
switch (response .headers [' content-encoding' ]) {
84
- // or, just use zlib.createUnzip() to handle both cases
84
+ case ' br' :
85
+ response .pipe (zlib .createBrotliDecompress ()).pipe (output);
86
+ break ;
87
+ // Or, just use zlib.createUnzip() to handle both of the following cases:
85
88
case ' gzip' :
86
89
response .pipe (zlib .createGunzip ()).pipe (output);
87
90
break ;
@@ -117,6 +120,9 @@ http.createServer((request, response) => {
117
120
} else if (/ \b gzip\b / .test (acceptEncoding)) {
118
121
response .writeHead (200 , { ' Content-Encoding' : ' gzip' });
119
122
raw .pipe (zlib .createGzip ()).pipe (response);
123
+ } else if (/ \b br\b / .test (acceptEncoding)) {
124
+ response .writeHead (200 , { ' Content-Encoding' : ' br' });
125
+ raw .pipe (zlib .createBrotliCompress ()).pipe (response);
120
126
} else {
121
127
response .writeHead (200 , {});
122
128
raw .pipe (response);
@@ -136,6 +142,7 @@ const buffer = Buffer.from('eJzT0yMA', 'base64');
136
142
137
143
zlib .unzip (
138
144
buffer,
145
+ // For Brotli, the equivalent is zlib.constants.BROTLI_OPERATION_FLUSH.
139
146
{ finishFlush: zlib .constants .Z_SYNC_FLUSH },
140
147
(err , buffer ) => {
141
148
if (! err) {
@@ -156,6 +163,8 @@ decompressed result is valid.
156
163
157
164
<!-- type=misc-->
158
165
166
+ ### For zlib-based streams
167
+
159
168
From ` zlib/zconf.h ` , modified to Node.js's usage:
160
169
161
170
The memory requirements for deflate are (in bytes):
@@ -194,6 +203,16 @@ fewer calls to `zlib` because it will be able to process more data on
194
203
each ` write ` operation. So, this is another factor that affects the
195
204
speed, at the cost of memory usage.
196
205
206
+ ### For Brotli-based streams
207
+
208
+ There are equivalents to the zlib options for Brotli-based streams, although
209
+ these options have different ranges than the zlib ones:
210
+
211
+ - zlib’s ` level ` option matches Brotli’s ` BROTLI_PARAM_QUALITY ` option.
212
+ - zlib’s ` windowBits ` option matches Brotli’s ` BROTLI_PARAM_LGWIN ` option.
213
+
214
+ See [ below] [ Brotli parameters ] for more details on Brotli-specific options.
215
+
197
216
## Flushing
198
217
199
218
Calling [ ` .flush() ` ] [ ] on a compression stream will make ` zlib ` return as much
@@ -231,6 +250,8 @@ added: v0.5.8
231
250
232
251
<!-- type=misc-->
233
252
253
+ ### zlib constants
254
+
234
255
All of the constants defined in ` zlib.h ` are also defined on
235
256
` require('zlib').constants ` . In the normal course of operations, it will not be
236
257
necessary to use these constants. They are documented so that their presence is
@@ -281,6 +302,77 @@ Compression strategy.
281
302
* ` zlib.constants.Z_FIXED `
282
303
* ` zlib.constants.Z_DEFAULT_STRATEGY `
283
304
305
+ ### Brotli constants
306
+ <!-- YAML
307
+ added: REPLACEME
308
+ -->
309
+
310
+ There are several options and other constants available for Brotli-based
311
+ streams:
312
+
313
+ #### Flush operations
314
+
315
+ The following values are valid flush operations for Brotli-based streams:
316
+
317
+ * ` zlib.constants.BROTLI_OPERATION_PROCESS ` (default for all operations)
318
+ * ` zlib.constants.BROTLI_OPERATION_FLUSH ` (default when calling ` .flush() ` )
319
+ * ` zlib.constants.BROTLI_OPERATION_FINISH ` (default for the last chunk)
320
+ * ` zlib.constants.BROTLI_OPERATION_EMIT_METADATA `
321
+ * This particular operation may be hard to use in a Node.js context,
322
+ as the streaming layer makes it hard to know which data will end up
323
+ in this frame. Also, there is currently no way to consume this data through
324
+ the Node.js API.
325
+
326
+ #### Compressor options
327
+
328
+ There are several options that can be set on Brotli encoders, affecting
329
+ compression efficiency and speed. Both the keys and the values can be accessed
330
+ as properties of the ` zlib.constants ` object.
331
+
332
+ The most important options are:
333
+
334
+ * ` BROTLI_PARAM_MODE `
335
+ * ` BROTLI_MODE_GENERIC ` (default)
336
+ * ` BROTLI_MODE_TEXT ` , adjusted for UTF-8 text
337
+ * ` BROTLI_MODE_FONT ` , adjusted for WOFF 2.0 fonts
338
+ * ` BROTLI_PARAM_QUALITY `
339
+ * Ranges from ` BROTLI_MIN_QUALITY ` to ` BROTLI_MAX_QUALITY ` ,
340
+ with a default of ` BROTLI_DEFAULT_QUALITY ` .
341
+ * ` BROTLI_PARAM_SIZE_HINT `
342
+ * Integer value representing the expected input size;
343
+ defaults to ` 0 ` for an unknown input size.
344
+
345
+ The following flags can be set for advanced control over the compression
346
+ algorithm and memory usage tuning:
347
+
348
+ * ` BROTLI_PARAM_LGWIN `
349
+ * Ranges from ` BROTLI_MIN_WINDOW_BITS ` to ` BROTLI_MAX_WINDOW_BITS ` ,
350
+ with a default of ` BROTLI_DEFAULT_WINDOW ` , or up to
351
+ ` BROTLI_LARGE_MAX_WINDOW_BITS ` if the ` BROTLI_PARAM_LARGE_WINDOW ` flag
352
+ is set.
353
+ * ` BROTLI_PARAM_LGBLOCK `
354
+ * Ranges from ` BROTLI_MIN_INPUT_BLOCK_BITS ` to ` BROTLI_MAX_INPUT_BLOCK_BITS ` .
355
+ * ` BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING `
356
+ * Boolean flag that decreases compression ratio in favour of
357
+ decompression speed.
358
+ * ` BROTLI_PARAM_LARGE_WINDOW `
359
+ * Boolean flag enabling “Large Window Brotli” mode (not compatible with the
360
+ Brotli format as standardized in [ RFC 7932] [ ] ).
361
+ * ` BROTLI_PARAM_NPOSTFIX `
362
+ * Ranges from ` 0 ` to ` BROTLI_MAX_NPOSTFIX ` .
363
+ * ` BROTLI_PARAM_NDIRECT `
364
+ * Ranges from ` 0 ` to ` 15 << NPOSTFIX ` in steps of ` 1 << NPOSTFIX ` .
365
+
366
+ #### Decompressor options
367
+
368
+ These advanced options are available for controlling decompression:
369
+
370
+ * ` BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION `
371
+ * Boolean flag that affects internal memory allocation patterns.
372
+ * ` BROTLI_DECODER_PARAM_LARGE_WINDOW `
373
+ * Boolean flag enabling “Large Window Brotli” mode (not compatible with the
374
+ Brotli format as standardized in [ RFC 7932] [ ] ).
375
+
284
376
## Class: Options
285
377
<!-- YAML
286
378
added: v0.11.1
@@ -298,7 +390,7 @@ changes:
298
390
299
391
<!-- type=misc-->
300
392
301
- Each class takes an ` options ` object. All options are optional.
393
+ Each zlib-based class takes an ` options ` object. All options are optional.
302
394
303
395
Note that some options are only relevant when compressing, and are
304
396
ignored by the decompression classes.
@@ -317,6 +409,47 @@ ignored by the decompression classes.
317
409
See the description of ` deflateInit2 ` and ` inflateInit2 ` at
318
410
< https://zlib.net/manual.html#Advanced > for more information on these.
319
411
412
+ ## Class: BrotliOptions
413
+ <!-- YAML
414
+ added: REPLACEME
415
+ -->
416
+
417
+ <!-- type=misc-->
418
+
419
+ Each Brotli-based class takes an ` options ` object. All options are optional.
420
+
421
+ * ` flush ` {integer} ** Default:** ` zlib.constants.BROTLI_OPERATION_PROCESS `
422
+ * ` finishFlush ` {integer} ** Default:** ` zlib.constants.BROTLI_OPERATION_FINISH `
423
+ * ` chunkSize ` {integer} ** Default:** ` 16 * 1024 `
424
+ * ` params ` {Object} Key-value object containing indexed [ Brotli parameters] [ ] .
425
+
426
+ For example:
427
+
428
+ ``` js
429
+ const stream = zlib .createBrotliCompress ({
430
+ chunkSize: 32 * 1024 ,
431
+ params: {
432
+ [zlib .constants .BROTLI_PARAM_MODE ]: zlib .constants .BROTLI_MODE_TEXT ,
433
+ [zlib .constants .BROTLI_PARAM_QUALITY ]: 4 ,
434
+ [zlib .constants .BROTLI_PARAM_SIZE_HINT ]: fs .statSync (inputFile).size
435
+ }
436
+ });
437
+ ```
438
+
439
+ ## Class: zlib.BrotliCompress
440
+ <!-- YAML
441
+ added: REPLACEME
442
+ -->
443
+
444
+ Compress data using the Brotli algorithm.
445
+
446
+ ## Class: zlib.BrotliDecompress
447
+ <!-- YAML
448
+ added: REPLACEME
449
+ -->
450
+
451
+ Decompress data using the Brotli algorithm.
452
+
320
453
## Class: zlib.Deflate
321
454
<!-- YAML
322
455
added: v0.5.8
@@ -389,9 +522,13 @@ added: v0.5.8
389
522
Decompress either a Gzip- or Deflate-compressed stream by auto-detecting
390
523
the header.
391
524
392
- ## Class: zlib.Zlib
525
+ ## Class: zlib.ZlibBase
393
526
<!-- YAML
394
527
added: v0.5.8
528
+ changes:
529
+ - version: REPLACEME
530
+ pr-url: https://github.com/nodejs/node/pull/24939
531
+ description: This class was renamed from `Zlib` to `ZlibBase`.
395
532
-->
396
533
397
534
Not exported by the ` zlib ` module. It is documented here because it is the base
@@ -440,7 +577,8 @@ Close the underlying handle.
440
577
added: v0.5.8
441
578
-->
442
579
443
- * ` kind ` ** Default:** ` zlib.constants.Z_FULL_FLUSH `
580
+ * ` kind ` ** Default:** ` zlib.constants.Z_FULL_FLUSH ` for zlib-based streams,
581
+ ` zlib.constants.BROTLI_OPERATION_FLUSH ` for Brotli-based streams.
444
582
* ` callback ` {Function}
445
583
446
584
Flush pending data. Don't call this frivolously, premature flushes negatively
@@ -460,6 +598,8 @@ added: v0.11.4
460
598
* ` strategy ` {integer}
461
599
* ` callback ` {Function}
462
600
601
+ This function is only available for zlib-based streams, i.e. not Brotli.
602
+
463
603
Dynamically update the compression level and compression strategy.
464
604
Only applicable to deflate algorithm.
465
605
@@ -478,6 +618,24 @@ added: v7.0.0
478
618
479
619
Provides an object enumerating Zlib-related constants.
480
620
621
+ ## zlib.createBrotliCompress([ options] )
622
+ <!-- YAML
623
+ added: REPLACEME
624
+ -->
625
+
626
+ * ` options ` {brotli options}
627
+
628
+ Creates and returns a new [ ` BrotliCompress ` ] [ ] object.
629
+
630
+ ## zlib.createBrotliDecompress([ options] )
631
+ <!-- YAML
632
+ added: REPLACEME
633
+ -->
634
+
635
+ * ` options ` {brotli options}
636
+
637
+ Creates and returns a new [ ` BrotliDecompress ` ] [ ] object.
638
+
481
639
## zlib.createDeflate([ options] )
482
640
<!-- YAML
483
641
added: v0.5.8
@@ -560,6 +718,40 @@ with `callback(error, result)`.
560
718
Every method has a ` *Sync ` counterpart, which accept the same arguments, but
561
719
without a callback.
562
720
721
+ ### zlib.brotliCompress(buffer[ , options] , callback)
722
+ <!-- YAML
723
+ added: REPLACEME
724
+ -->
725
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
726
+ * ` options ` {brotli options}
727
+ * ` callback ` {Function}
728
+
729
+ ### zlib.brotliCompressSync(buffer[ , options] )
730
+ <!-- YAML
731
+ added: REPLACEME
732
+ -->
733
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
734
+ * ` options ` {brotli options}
735
+
736
+ Compress a chunk of data with [ ` BrotliCompress ` ] [ ] .
737
+
738
+ ### zlib.brotliDecompress(buffer[ , options] , callback)
739
+ <!-- YAML
740
+ added: REPLACEME
741
+ -->
742
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
743
+ * ` options ` {brotli options}
744
+ * ` callback ` {Function}
745
+
746
+ ### zlib.brotliDecompressSync(buffer[ , options] )
747
+ <!-- YAML
748
+ added: REPLACEME
749
+ -->
750
+ * ` buffer ` {Buffer|TypedArray|DataView|ArrayBuffer|string}
751
+ * ` options ` {brotli options}
752
+
753
+ Decompress a chunk of data with [ ` BrotliDecompress ` ] [ ] .
754
+
563
755
### zlib.deflate(buffer[ , options] , callback)
564
756
<!-- YAML
565
757
added: v0.6.0
@@ -832,6 +1024,8 @@ Decompress a chunk of data with [`Unzip`][].
832
1024
[ `.flush()` ] : #zlib_zlib_flush_kind_callback
833
1025
[ `Accept-Encoding` ] : https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
834
1026
[ `ArrayBuffer` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
1027
+ [ `BrotliCompress` ] : #zlib_class_zlib_brotlicompress
1028
+ [ `BrotliDecompress` ] : #zlib_class_zlib_brotlidecompress
835
1029
[ `Buffer` ] : buffer.html#buffer_class_buffer
836
1030
[ `Content-Encoding` ] : https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
837
1031
[ `DataView` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
@@ -845,6 +1039,8 @@ Decompress a chunk of data with [`Unzip`][].
845
1039
[ `Unzip` ] : #zlib_class_zlib_unzip
846
1040
[ `stream.Transform` ] : stream.html#stream_class_stream_transform
847
1041
[ `zlib.bytesWritten` ] : #zlib_zlib_byteswritten
1042
+ [ Brotli parameters ] : #zlib_brotli_constants
848
1043
[ Memory Usage Tuning ] : #zlib_memory_usage_tuning
1044
+ [ RFC 7932 ] : https://www.rfc-editor.org/rfc/rfc7932.txt
849
1045
[ pool size ] : cli.html#cli_uv_threadpool_size_size
850
1046
[ zlib documentation ] : https://zlib.net/manual.html#Constants
0 commit comments