32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
import * as zlib from "node:zlib";
|
|
import { promisify } from "node:util";
|
|
const gzip = promisify(zlib.gzip);
|
|
const brotliCompress = promisify(zlib.brotliCompress);
|
|
const gzipOptions = (options) => ({
|
|
level: 9,
|
|
...options,
|
|
});
|
|
const brotliOptions = (options, buffer) => ({
|
|
params: {
|
|
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
|
|
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
|
|
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: buffer.length,
|
|
},
|
|
...options,
|
|
});
|
|
const createGzipCompressor = (options) => (buffer) => gzip(buffer, gzipOptions(options || {}));
|
|
export const createGzipSizeGetter = (options) => {
|
|
const compress = createGzipCompressor(options);
|
|
return async (code) => {
|
|
const data = await compress(Buffer.from(code, "utf-8"));
|
|
return data.length;
|
|
};
|
|
};
|
|
const createBrotliCompressor = (options) => (buffer) => brotliCompress(buffer, brotliOptions(options || {}, buffer));
|
|
export const createBrotliSizeGetter = (options) => {
|
|
const compress = createBrotliCompressor(options);
|
|
return async (code) => {
|
|
const data = await compress(Buffer.from(code, "utf-8"));
|
|
return data.length;
|
|
};
|
|
};
|