forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress-large-files.js
More file actions
executable file
·63 lines (55 loc) · 1.96 KB
/
compress-large-files.js
File metadata and controls
executable file
·63 lines (55 loc) · 1.96 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
62
63
#!/usr/bin/env node
import path from 'path'
import fs from 'fs'
import zlib from 'zlib'
import walk from 'walk-sync'
const DRY_RUN = Boolean(JSON.parse(process.env.DRY_RUN || 'false'))
// Roughly 100KiB means about 25 files at the moment.
// Set this too low and the overheads will be more than the disk and
// network I/O that this intends to serve.
const MIN_GZIP_SIZE = Number(process.env.MIN_GZIP_SIZE || 1024 * 100)
const BROTLI_OPTIONS = {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: 6,
},
}
main()
async function main() {
compressFromPattern('lib/**/static/**/*.json')
}
async function compressFromPattern(pattern) {
const glob = pattern.includes('*') ? pattern.split(path.sep).slice(1).join(path.sep) : undefined
const walkOptions = {
globs: glob ? [glob] : undefined,
directories: false,
includeBasePath: true,
}
const root = path.resolve(pattern.includes('*') ? pattern.split(path.sep)[0] : pattern)
const filePaths = walk(root, walkOptions).filter((filePath) => {
return fs.statSync(filePath).size > MIN_GZIP_SIZE
})
if (!DRY_RUN) {
console.time(`Compress ${filePaths.length} files`)
const compressed = await Promise.all(filePaths.map(compressFile))
console.timeEnd(`Compress ${filePaths.length} files`)
console.time(`Delete ${compressed.length} files`)
compressed.forEach((filePath) => fs.unlinkSync(filePath))
console.timeEnd(`Delete ${compressed.length} files`)
}
}
function compressFile(filePath) {
return new Promise((resolve, reject) => {
const contentStream = fs.createReadStream(filePath)
const newFilePath = `${filePath}.br`
const writeStream = fs.createWriteStream(newFilePath)
const compressor = zlib.createBrotliCompress(BROTLI_OPTIONS)
contentStream
.pipe(compressor)
.pipe(writeStream)
.on('finish', (err) => {
if (err) return reject(err)
resolve(filePath)
})
})
}