mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
- 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!
72 lines
2.0 KiB
TypeScript
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);
|
|
});
|
|
});
|