function compression({ threshold = 1024, level = -1, brotli = false, gzip = true, mimes = MIMES } = {}) {
	const brotliOpts = (typeof brotli === 'object' && brotli) || {};
	const gzipOpts = (typeof gzip === 'object' && gzip) || {};

	// disable Brotli on Node<12.7 where it is unsupported:
	if (!zlib.createBrotliCompress) brotli = false;

	return (req, res, next = noop) => {
		const accept = req.headers['accept-encoding'] + '';
		const encoding = ((brotli && accept.match(/\bbr\b/)) || (gzip && accept.match(/\bgzip\b/)) || [])[0];

		// skip if no response body or no supported encoding:
		if (req.method === 'HEAD' || !encoding) return next();

		/** @type {zlib.Gzip | zlib.BrotliCompress} */
		let compress;
		let pendingStatus;
		/** @type {[string, function][]?} */
		let pendingListeners = [];
		let started = false;
		let size = 0;

		function start() {
			started = true;
			// @ts-ignore
			size = res.getHeader('Content-Length') | 0 || size;
			const compressible = mimes.test(String(res.getHeader('Content-Type') || 'text/plain'));
			const cleartext = !res.getHeader('Content-Encoding');
			const listeners = pendingListeners || [];
			if (compressible && cleartext && size >= threshold) {
				res.setHeader('Content-Encoding', encoding);
				res.removeHeader('Content-Length');
				if (encoding === 'br') {
					const params = {
						[zlib.constants.BROTLI_PARAM_QUALITY]: level,
						[zlib.constants.BROTLI_PARAM_SIZE_HINT]: size
					};
					compress = zlib.createBrotliCompress({ params: Object.assign(params, brotliOpts) });
				} else {
					compress = zlib.createGzip(Object.assign({ level }, gzipOpts));
				}
				// backpressure
				compress.on('data', chunk => write.call(res, chunk) === false && compress.pause());
				on.call(res, 'drain', () => compress.resume());
				compress.on('end', () => end.call(res));
				listeners.forEach(p => compress.on.apply(compress, p));
			} else {
				pendingListeners = null;
				listeners.forEach(p => on.apply(res, p));
			}

			writeHead.call(res, pendingStatus || res.statusCode);
		}

		const { end, write, on, writeHead } = res;

		res.writeHead = function (status, reason, headers) {
			if (typeof reason !== 'string') [headers, reason] = [reason, headers];
			if (headers) for (let i in headers) res.setHeader(i, headers[i]);
			pendingStatus = status;
			return this;
		};

		res.write = function (chunk, enc, cb) {
			size += getChunkSize(chunk, enc);
			if (!started) start();
			if (!compress) return write.apply(this, arguments);
			return compress.write.apply(compress, arguments);
		};

		res.end = function (chunk, enc, cb) {
			if (arguments.length > 0 && typeof chunk !== 'function') {
				size += getChunkSize(chunk, enc);
			}
			if (!started) start();
			if (!compress) return end.apply(this, arguments);
			return compress.end.apply(compress, arguments);
		};

		res.on = function (type, listener) {
			if (!pendingListeners || type !== 'drain') on.call(this, type, listener);
			else if (compress) compress.on(type, listener);
			else pendingListeners.push([type, listener]);
			return this;
		};

		next();
	};
}
@param {object} [options]
@param {number} [options.threshold = 1024] Don't compress responses below this size (in bytes)
@param {number} [options.level = -1] Gzip/Brotli compression effort (1-11, or -1 for default)
@param {boolean} [options.brotli = false] Generate and serve Brotli-compressed responses
@param {boolean} [options.gzip = true] Generate and serve Gzip-compressed responses
@param {RegExp} [options.mimes] Regular expression of response MIME types to compress (default: text|javascript|json|xml)
@returns {(req: Pick<import('http').IncomingMessage, 'method'|'headers'>, res: import('http').ServerResponse, next?:Function) => void}
@retur {import('polka').Middleware}