Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 02a84e2

Browse filesBrowse files
addaleaxMylesBorins
authored andcommitted
doc: add full example for zlib.flush()
Add a full example using `zlib.flush()` for the common use case of writing partial compressed HTTP output to the client. PR-URL: #6172 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Robert Jefe Lindstädt <robert.lindstaedt@gmail.com>
1 parent 9f9371b commit 02a84e2
Copy full SHA for 02a84e2

File tree

Expand file treeCollapse file tree

1 file changed

+31
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+31
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/zlib.markdown‎

Copy file name to clipboardExpand all lines: doc/api/zlib.markdown
+31Lines changed: 31 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,36 @@ fewer calls to zlib, since it'll be able to process more data in a
180180
single `write` operation. So, this is another factor that affects the
181181
speed, at the cost of memory usage.
182182

183+
## Flushing
184+
185+
Calling [`.flush()`][] on a compression stream will make zlib return as much
186+
output as currently possible. This may come at the cost of degraded compression
187+
quality, but can be useful when data needs to be available as soon as possible.
188+
189+
In the following example, `flush()` is used to write a compressed partial
190+
HTTP response to the client:
191+
```js
192+
const zlib = require('zlib');
193+
const http = require('http');
194+
195+
http.createServer((request, response) => {
196+
// For the sake of simplicity, the Accept-Encoding checks are omitted.
197+
response.writeHead(200, { 'content-encoding': 'gzip' });
198+
const output = zlib.createGzip();
199+
output.pipe(response);
200+
201+
setInterval(() => {
202+
output.write(`The current time is ${Date()}\n`, () => {
203+
// The data has been passed to zlib, but the compression algorithm may
204+
// have decided to buffer the data for more efficient compression.
205+
// Calling .flush() will make the data available as soon as the client
206+
// is ready to receive it.
207+
output.flush();
208+
});
209+
}, 1000);
210+
}).listen(1337);
211+
```
212+
183213
## Constants
184214

185215
<!--type=misc-->
@@ -409,4 +439,5 @@ Decompress a Buffer or string with Unzip.
409439
[Inflate]: #zlib_class_zlib_inflate
410440
[InflateRaw]: #zlib_class_zlib_inflateraw
411441
[Unzip]: #zlib_class_zlib_unzip
442+
[`.flush()`]: #zlib_zlib_flush_kind_callback
412443
[Buffer]: buffer.html

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.