-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (46 loc) · 1.41 KB
/
index.js
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
'use strict'
const fs = require('fs')
const stream = require('stream')
const zlib = require('zlib')
const duplexer = require('duplexer')
const pify = require('pify')
module.exports = (input, options) => {
if (!input) {
return Promise.resolve(0)
}
return pify(zlib.brotliCompress)(input, options)
.then(data => data.length)
.catch(_ => 0)
}
module.exports.sync = (input, options) => zlib.brotliCompressSync(input, options).length
module.exports.stream = (options) => {
const input = new stream.PassThrough()
const output = new stream.PassThrough()
const wrapper = duplexer(input, output)
let brotliSize = 0
const brotli = zlib.createBrotliCompress(options)
.on('data', (buf) => {
brotliSize += buf.length
})
.on('error', () => {
wrapper.brotliSize = 0
})
.on('end', () => {
wrapper.brotliSize = brotliSize
wrapper.emit('brotli-size', brotliSize)
output.end()
})
input.pipe(brotli)
input.pipe(output, { end: false })
return wrapper
}
module.exports.file = (path, options) => {
return new Promise((resolve, reject) => {
const stream = fs.createReadStream(path)
stream.on('error', reject)
const brotliStream = stream.pipe(module.exports.stream(options))
brotliStream.on('error', reject)
brotliStream.on('brotli-size', resolve)
})
}
module.exports.fileSync = (path, options) => module.exports.sync(fs.readFileSync(path), options)