From c415558270d835d162b4e92cc20b94644e08d368 Mon Sep 17 00:00:00 2001 From: Jan Kaifer Date: Sun, 16 Apr 2023 15:59:27 +0200 Subject: [PATCH] feat(pack): add PNPM_PACK_GZIP_LEVEL This options allows specifying custom compression level. Underlying node implementation enforces correct number ranges, but silently ignores `NaN`, so we explicitly check that provided value is valid. fix #6393 --- .../plugin-commands-publishing/src/pack.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/releasing/plugin-commands-publishing/src/pack.ts b/releasing/plugin-commands-publishing/src/pack.ts index 2aaa72af6a2..defa125bfae 100644 --- a/releasing/plugin-commands-publishing/src/pack.ts +++ b/releasing/plugin-commands-publishing/src/pack.ts @@ -1,6 +1,6 @@ import fs from 'fs' import path from 'path' -import { createGzip } from 'zlib' +import { createGzip, type ZlibOptions } from 'zlib' import { PnpmError } from '@pnpm/error' import { types as allTypes, type UniversalOptions, type Config } from '@pnpm/config' import { readProjectManifest } from '@pnpm/cli-utils' @@ -127,6 +127,21 @@ async function readReadmeFile (filesMap: Record) { return readmeFile } +function getGzipConfig () { + const gzipConfig: ZlibOptions = {} + + const level = process.env.PNPM_PACK_GZIP_LEVEL + if (level !== undefined) { + const parsedLevel = parseInt(level) + if (isNaN(parsedLevel)) { + throw new PnpmError('INVALID_GZIP_LEVEL', `Invalid gzip level: '${level}'`) + } + gzipConfig.level = parsedLevel + } + + return gzipConfig +} + async function packPkg (opts: { destFile: string filesMap: Record @@ -160,7 +175,7 @@ async function packPkg (opts: { pack.entry({ mode, mtime, name }, fs.readFileSync(source)) } const tarball = fs.createWriteStream(destFile) - pack.pipe(createGzip()).pipe(tarball) + pack.pipe(createGzip(getGzipConfig())).pipe(tarball) pack.finalize() return new Promise((resolve, reject) => { tarball.on('close', () => {