Skip to content

Commit

Permalink
Change sync to async compression
Browse files Browse the repository at this point in the history
  • Loading branch information
KubesDavid committed Sep 4, 2022
1 parent 0d50388 commit 780d69f
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/rollup/plugins/public-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,24 @@ export function publicAssets (nitro: Nitro): Plugin {
if (gzip) { encodings.push('gzip') }
if (brotli) { encodings.push('br') }
}

for (const encoding of encodings) {
const suffix = '.' + (encoding === 'gzip' ? 'gz' : 'br')
const compressedPath = fullPath + suffix
const compressedBuff = (encoding === 'gzip')
? zlib.gzipSync(assetData, { level: zlib.constants.Z_BEST_COMPRESSION })
: zlib.brotliCompressSync(assetData, {
[zlib.constants.BROTLI_PARAM_MODE]: isTextType
? zlib.constants.BROTLI_MODE_TEXT
: zlib.constants.BROTLI_MODE_GENERIC,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: assetData.length
})
const gzipOptions = { level: zlib.constants.Z_BEST_COMPRESSION }
const brotliOptions = {
[zlib.constants.BROTLI_PARAM_MODE]: isTextType
? zlib.constants.BROTLI_MODE_TEXT
: zlib.constants.BROTLI_MODE_GENERIC,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: assetData.length
}

const compressedBuff: Buffer = await new Promise((resolve, reject) => {
(encoding === 'gzip')
? zlib.gzip(assetData, gzipOptions, (error, result) => error ? reject(error) : resolve(result))
: zlib.brotliCompress(assetData, brotliOptions, (error, result) => error ? reject(error) : resolve(result))
})
await fsp.writeFile(compressedPath, compressedBuff)
assets[assetId + suffix] = {
...assets[assetId],
Expand Down

0 comments on commit 780d69f

Please sign in to comment.