Skip to content

Commit

Permalink
feat(pack): add PNPM_PACK_GZIP_LEVEL
Browse files Browse the repository at this point in the history
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 pnpm#6393
  • Loading branch information
jankaifer committed Apr 16, 2023
1 parent d43ccc4 commit c415558
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions 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'
Expand Down Expand Up @@ -127,6 +127,21 @@ async function readReadmeFile (filesMap: Record<string, string>) {
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<string, string>
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit c415558

Please sign in to comment.