forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (53 loc) · 1.39 KB
/
Copy pathindex.js
File metadata and controls
58 lines (53 loc) · 1.39 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
import { createReadStream } from 'node:fs'
import { stat } from 'node:fs/promises'
import { constants, createBrotliCompress, createGzip } from 'node:zlib'
async function sum(array, fn) {
return (await Promise.all(array.map(fn))).reduce((all, i) => all + i, 0)
}
function brotliSize(path) {
return new Promise((resolve, reject) => {
let size = 0
let pipe = createReadStream(path).pipe(
createBrotliCompress({
params: {
[constants.BROTLI_PARAM_QUALITY]: 11
}
})
)
pipe.on('error', reject)
pipe.on('data', buf => {
size += buf.length
})
pipe.on('end', () => {
resolve(size)
})
})
}
function gzipSize(path) {
return new Promise((resolve, reject) => {
let size = 0
let pipe = createReadStream(path).pipe(createGzip({ level: 9 }))
pipe.on('error', reject)
pipe.on('data', buf => {
size += buf.length
})
pipe.on('end', () => {
resolve(size)
})
})
}
export default [
{
name: '@size-limit/file',
async step60(_config, check) {
let files = check.bundles || check.files
if (check.gzip === true) {
check.size = await sum(files, async i => gzipSize(i))
} else if (check.brotli === false) {
check.size = await sum(files, async i => (await stat(i)).size)
} else {
check.size = await sum(files, async i => brotliSize(i))
}
}
}
]