-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcompress-cli.mjs
More file actions
61 lines (48 loc) · 2 KB
/
compress-cli.mjs
File metadata and controls
61 lines (48 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @fileoverview Compress build/cli.js with brotli to dist/cli.js.bz.
*
* This script compresses the CLI bundle to reduce npm package size
* from ~13MB to ~1.7MB (87% reduction).
*
* The compressed file is decompressed at runtime by dist/index.js.
*/
import crypto from 'node:crypto'
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { brotliCompressSync } from 'node:zlib'
import { getDefaultLogger } from '@socketsecurity/lib/logger'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const rootPath = path.join(__dirname, '..')
const buildPath = path.join(rootPath, 'build')
const distPath = path.join(rootPath, 'dist')
const cliPath = path.join(buildPath, 'cli.js')
const cliBzPath = path.join(distPath, 'cli.js.bz')
const logger = getDefaultLogger()
logger.log('')
logger.step('Compressing CLI with brotli...')
// Ensure dist/ directory exists.
mkdirSync(distPath, { recursive: true })
// Read the uncompressed CLI from build/.
const cliCode = readFileSync(cliPath)
const originalSize = cliCode.length
// Compress with brotli (max quality for best compression).
const compressed = brotliCompressSync(cliCode, {
params: {
[0]: 11, // BROTLI_PARAM_QUALITY: 11 (max quality).
},
})
const compressedSize = compressed.length
// Write compressed file to dist/.
writeFileSync(cliBzPath, compressed)
const compressionRatio = ((1 - compressedSize / originalSize) * 100).toFixed(1)
logger.success(
`Compressed: ${(originalSize / 1024 / 1024).toFixed(2)} MB → ${(compressedSize / 1024 / 1024).toFixed(2)} MB (${compressionRatio}% reduction)`,
)
// Generate SHA256 checksum for integrity validation.
const sha256 = crypto.createHash('sha256').update(compressed).digest('hex')
const checksumPath = path.join(distPath, 'cli.js.bz.sha256')
writeFileSync(checksumPath, `${sha256} cli.js.bz\n`)
logger.success(`SHA256: ${sha256}`)
logger.log(`Checksum written to: ${path.relative(rootPath, checksumPath)}`)
logger.log('')