GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — doc: add full example for zlib.flush() · nodejs/node@02a84e2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • zlib.markdown
Expand file treeCollapse file tree
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)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.