DefinitelyTyped/types/compression/compression-tests.ts
Piotr Błażejewicz (Peter Blazejewicz) c85c9c89b5
update(compression): amend module definition and bump to 1.7 (#42045)
- CommonJS module refined
- added public document props
- removed implicit zlib props from defintion and opt-in for unnamed
props support
- proper support for module Express.Response.flush method added
- tests amended to reflect changes in module definition
- Prettier run on the definition file
- configuration based on current DT settings

Thanks!
2020-02-07 14:08:59 -08:00

72 lines
2.0 KiB
TypeScript

import express = require('express');
import compression = require('compression');
import zlib = require('zlib');
const app = express();
// compression
app.use(compression());
app.use(
compression({
chunkSize: zlib.constants.Z_DEFAULT_CHUNK,
filter: (req: express.Request, res: express.Response) => true,
level: zlib.constants.Z_DEFAULT_COMPRESSION,
memLevel: zlib.constants.Z_DEFAULT_MEMLEVEL,
strategy: zlib.constants.Z_DEFAULT_STRATEGY,
threshold: '1kb',
windowBits: zlib.constants.Z_DEFAULT_WINDOWBITS,
// should accept zlib options https://nodejs.org/api/zlib.html#zlib_class_options
flush: zlib.constants.Z_NO_FLUSH,
finishFlush: zlib.constants.Z_FINISH,
dictionary: Buffer.from('Hello World!'),
info: true,
}),
);
app.use(
compression({
threshold: 512,
filter: (req: express.Request, res: express.Response) => {
if (req.headers['x-no-compression']) {
// don't compress responses with this request header
return false;
}
// fallback to standard filter function
return compression.filter(req, res);
},
}),
);
// compression.filter
app.use(compression({ filter: shouldCompress }));
function shouldCompress(req: express.Request, res: express.Response): boolean {
if (req.headers['x-no-compression']) {
// don't compress responses with this request header
return false;
}
// fallback to standard filter function
return compression.filter(req, res);
}
// Express.Request.flush
app.get('/events', (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
// send a ping approx every 2 seconds
const timer = setInterval(() => {
res.write('data: ping\n\n');
// !!! this is the important part
res.flush();
}, 2000);
res.on('close', () => {
clearInterval(timer);
});
});