-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrotli.ts
More file actions
236 lines (219 loc) · 7.74 KB
/
brotli.ts
File metadata and controls
236 lines (219 loc) · 7.74 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* oxlint-disable socket/sort-source-methods -- functions ordered by call graph (compress/decompress variants share helpers); type / const declarations between them block autofix. */
/**
* @fileoverview Brotli compression / decompression — in-memory,
* file-to-file, and raw-stream variants. Default quality is 11 (max
* compression, slow) on the assumption these are one-shot CLI calls.
* Override via `options.level` for hot paths.
*
* await compressBrotli(JSON.stringify(payload))
* await compressBrotliFile('input.json', 'input.json.br')
* readable.pipe(createBrotliCompressor()).pipe(writable)
*/
import { Buffer } from 'node:buffer'
import { createReadStream, createWriteStream } from 'node:fs'
import path from 'node:path'
import { pipeline } from 'node:stream/promises'
import { promisify } from 'node:util'
import {
brotliCompress,
brotliDecompress,
constants as zlibConstants,
createBrotliCompress,
createBrotliDecompress,
type BrotliOptions,
} from 'node:zlib'
import { safeDelete } from '../fs/safe'
import { ErrorCtor } from '../primordials/error'
import { StringPrototypeToLowerCase } from '../primordials/string'
import { resolveFileArgs, stripExt } from './_internal'
import type { CompressFileOptions, CompressOptions } from './types'
const brotliCompressAsync = promisify(brotliCompress)
const brotliDecompressAsync = promisify(brotliDecompress)
interface ResolvedBrotliOptions extends BrotliOptions {
params: NonNullable<BrotliOptions['params']>
}
/**
* Translate `CompressOptions` into the `BrotliOptions` zlib expects.
* Defaults `quality` to 11 (max) when not provided, and forwards a
* positive `size` hint. Exposed for callers building their own zlib
* pipelines and for unit-test coverage.
*/
export function resolveBrotliOptions(
options: CompressOptions | undefined,
): ResolvedBrotliOptions {
const level = options?.level ?? 11
const params: NonNullable<BrotliOptions['params']> = {
[zlibConstants.BROTLI_PARAM_QUALITY]: level,
}
if (options?.size !== undefined && options.size > 0) {
params[zlibConstants.BROTLI_PARAM_SIZE_HINT] = options.size
}
return { params }
}
/**
* Compress a string or Buffer with brotli. Strings are encoded as UTF-8
* before compression — pass an explicit Buffer if you have non-UTF-8
* input.
*/
export async function compressBrotli(
input: string | Buffer,
options?: CompressOptions | undefined,
): Promise<Buffer> {
const buf = typeof input === 'string' ? Buffer.from(input, 'utf8') : input
const opts = resolveBrotliOptions(options)
// Auto-fill size hint when not provided — brotli picks better
// blocking when it knows the input size up front.
if (opts.params[zlibConstants.BROTLI_PARAM_SIZE_HINT] === undefined) {
opts.params[zlibConstants.BROTLI_PARAM_SIZE_HINT] = buf.byteLength
}
return await brotliCompressAsync(buf, opts)
}
/**
* Decompress a brotli-compressed Buffer.
*/
export async function decompressBrotli(input: Buffer): Promise<Buffer> {
return await brotliDecompressAsync(input)
}
/**
* Stream-compress a file with brotli. Two call shapes:
*
* compressBrotliFile(src, dest, options?)
* Writes compressed output to `dest`. Source is left intact.
*
* compressBrotliFile(src, { inPlace: true, ...options })
* Writes to `<src>.br` and deletes `src` after the write
* succeeds. Returns the new path.
*
* Returns the destination path in both shapes — same string the
* caller passed in or the computed `.br` path for inPlace.
*/
export async function compressBrotliFile(
srcPath: string,
destPath: string,
options?: CompressOptions | undefined,
): Promise<string>
export async function compressBrotliFile(
srcPath: string,
options: CompressFileOptions,
): Promise<string>
export async function compressBrotliFile(
srcPath: string,
destOrOptions?: string | CompressFileOptions,
maybeOptions?: CompressOptions | undefined,
): Promise<string> {
const { destPath, options, inPlace } = resolveFileArgs(
'compressBrotliFile',
srcPath,
destOrOptions,
maybeOptions,
p => `${p}.br`,
)
await pipeline(
createReadStream(srcPath),
createBrotliCompress(resolveBrotliOptions(options)),
createWriteStream(destPath),
)
if (inPlace) {
await safeDelete(srcPath)
}
return destPath
}
/**
* Stream-decompress a brotli file. Two call shapes:
*
* decompressBrotliFile(src, dest)
* Writes decompressed output to `dest`. Source is left intact.
*
* decompressBrotliFile(src, { inPlace: true })
* Strips the `.br`/`.brotli` suffix to derive the destination,
* then deletes the compressed source after the write succeeds.
* Throws if `src` has no recognizable extension.
*
* Returns the destination path in both shapes.
*/
export async function decompressBrotliFile(
srcPath: string,
destPath: string,
): Promise<string>
export async function decompressBrotliFile(
srcPath: string,
options: CompressFileOptions,
): Promise<string>
export async function decompressBrotliFile(
srcPath: string,
destOrOptions?: string | CompressFileOptions,
): Promise<string> {
const { destPath, inPlace } = resolveFileArgs(
'decompressBrotliFile',
srcPath,
destOrOptions,
undefined,
p => {
if (!hasBrotliExt(p)) {
throw new ErrorCtor(
`decompressBrotliFile: ${p} has no .br/.brotli extension; can't infer destination`,
)
}
return stripExt(p, BROTLI_EXTS)
},
)
await pipeline(
createReadStream(srcPath),
createBrotliDecompress(),
createWriteStream(destPath),
)
if (inPlace) {
await safeDelete(srcPath)
}
return destPath
}
/**
* Create a brotli compress transform stream. Compose into your own
* pipeline. The `pipeline` from `node:stream/promises` is the safe
* way to wire it up — it handles error propagation across all stages.
*/
export function createBrotliCompressor(options?: CompressOptions | undefined) {
return createBrotliCompress(resolveBrotliOptions(options))
}
/**
* Create a brotli decompress transform stream.
*/
export function createBrotliDecompressor() {
return createBrotliDecompress()
}
// Brotli has no defined magic bytes — the format starts with header
// data that can technically be anything. The closest signal is the
// last-byte stream-end pattern. We reject obviously-not-brotli input
// (empty / under 4 bytes) and let the caller catch decode errors as
// the authoritative "is it brotli?" check.
//
// Use this only for cheap pre-flight rejection; never as a security
// or correctness gate.
const BROTLI_MIN_LEN = 4
/**
* Cheap pre-flight check: does the buffer look like it could be
* brotli? Returns false for inputs too short to be valid. Brotli has
* no fixed magic bytes, so this is intentionally permissive — the
* authoritative test is `decompressBrotli(buf)` succeeding. Use for
* UI hints, not correctness.
*/
export function isBrotliCompressed(input: Buffer): boolean {
return Buffer.isBuffer(input) && input.byteLength >= BROTLI_MIN_LEN
}
// Use Sets — O(1) lookup, sorted alphanumerically per CLAUDE.md so
// adding a new extension stays a one-line append. node:path's extname
// is case-sensitive (it reflects the OS path semantics), but our
// extension classifier is policy: ".BR" should classify the same as
// ".br" regardless of host OS. Lowercase the extname before lookup.
//
// Exported so callers can introspect what counts as a "brotli"
// extension without re-implementing the list, and so tests can pin
// the recognized set.
export const BROTLI_EXTS: ReadonlySet<string> = new Set(['.br', '.brotli'])
/**
* Extension check for brotli paths — matches `.br` / `.brotli`
* (case-insensitive). Naming follows node:path's `extname`.
*/
export function hasBrotliExt(filePath: string): boolean {
return BROTLI_EXTS.has(StringPrototypeToLowerCase(path.extname(filePath)))
}