@blamejs/core 0.7.4 → 0.7.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +423 -395
- package/README.md +150 -149
- package/bin/blamejs.js +0 -0
- package/index.js +308 -284
- package/lib/api-key.js +660 -663
- package/lib/api-snapshot.js +338 -338
- package/lib/app-shutdown.js +385 -385
- package/lib/app.js +365 -365
- package/lib/archive.js +250 -250
- package/lib/atomic-file.js +544 -544
- package/lib/audit-chain.js +177 -177
- package/lib/audit-sign.js +344 -344
- package/lib/audit-tools.js +677 -677
- package/lib/audit.js +766 -766
- package/lib/auth/jwt.js +311 -311
- package/lib/auth/lockout.js +436 -436
- package/lib/auth/oauth.js +721 -721
- package/lib/auth/passkey.js +181 -181
- package/lib/auth/password.js +594 -594
- package/lib/backup/bundle.js +217 -217
- package/lib/backup/crypto.js +176 -176
- package/lib/backup/index.js +515 -515
- package/lib/backup/manifest.js +282 -282
- package/lib/break-glass.js +1338 -1338
- package/lib/bundler.js +441 -441
- package/lib/cache-redis.js +256 -256
- package/lib/cache.js +1206 -1206
- package/lib/canonical-json.js +115 -115
- package/lib/chain-writer.js +234 -234
- package/lib/cli-helpers.js +206 -206
- package/lib/cli.js +2334 -2334
- package/lib/cluster-provider-db.js +317 -317
- package/lib/cluster-storage.js +226 -226
- package/lib/cluster.js +703 -703
- package/lib/codepoint-class.js +196 -0
- package/lib/config-drift.js +301 -301
- package/lib/consent.js +222 -222
- package/lib/constants.js +191 -191
- package/lib/cookies.js +315 -315
- package/lib/credential-hash.js +322 -322
- package/lib/crypto.js +266 -266
- package/lib/csv.js +275 -286
- package/lib/db-declare-row-policy.js +267 -267
- package/lib/db-declare-view.js +420 -421
- package/lib/db-query.js +406 -406
- package/lib/db-schema.js +319 -319
- package/lib/db.js +1288 -1288
- package/lib/deprecate.js +222 -222
- package/lib/dev.js +335 -335
- package/lib/dual-control.js +473 -473
- package/lib/error-page.js +420 -420
- package/lib/external-db-migrate.js +441 -441
- package/lib/external-db.js +1061 -1061
- package/lib/file-type.js +273 -273
- package/lib/file-upload.js +213 -10
- package/lib/forms.js +422 -422
- package/lib/framework-error.js +293 -215
- package/lib/framework-schema.js +717 -717
- package/lib/gate-contract.js +971 -0
- package/lib/guard-all.js +405 -0
- package/lib/guard-archive.js +739 -0
- package/lib/guard-csv.js +816 -0
- package/lib/guard-email.js +744 -0
- package/lib/guard-filename.js +724 -0
- package/lib/guard-html.js +976 -0
- package/lib/guard-json.js +729 -0
- package/lib/guard-markdown.js +586 -0
- package/lib/guard-svg.js +976 -0
- package/lib/guard-xml.js +405 -0
- package/lib/guard-yaml.js +529 -0
- package/lib/handlers.js +350 -350
- package/lib/http-client-cookie-jar.js +508 -508
- package/lib/http-client.js +1195 -1195
- package/lib/i18n.js +878 -878
- package/lib/jobs.js +185 -185
- package/lib/log-stream-cloudwatch.js +369 -369
- package/lib/log-stream-local.js +146 -146
- package/lib/log-stream-otlp-grpc.js +410 -410
- package/lib/log-stream-otlp.js +286 -286
- package/lib/log-stream-syslog.js +302 -302
- package/lib/log-stream-webhook.js +199 -199
- package/lib/log-stream.js +330 -330
- package/lib/log.js +500 -500
- package/lib/mail-bounce.js +528 -528
- package/lib/mail-dkim.js +369 -362
- package/lib/mail.js +981 -962
- package/lib/metrics.js +683 -683
- package/lib/middleware/api-encrypt.js +936 -936
- package/lib/middleware/attach-user.js +157 -157
- package/lib/middleware/body-parser.js +1170 -1091
- package/lib/middleware/bot-guard.js +178 -178
- package/lib/middleware/compression.js +452 -452
- package/lib/middleware/cors.js +314 -314
- package/lib/middleware/csp-nonce.js +348 -348
- package/lib/middleware/csrf-protect.js +316 -316
- package/lib/middleware/db-role-for.js +264 -264
- package/lib/middleware/health.js +392 -392
- package/lib/middleware/index.js +79 -79
- package/lib/middleware/rate-limit.js +358 -358
- package/lib/middleware/request-id.js +61 -61
- package/lib/middleware/request-log.js +168 -168
- package/lib/middleware/require-auth.js +104 -104
- package/lib/middleware/security-headers.js +116 -116
- package/lib/middleware/sse.js +166 -166
- package/lib/migrations.js +383 -383
- package/lib/mtls-ca.js +518 -518
- package/lib/mtls-engine-default.js +481 -481
- package/lib/network-dns.js +632 -632
- package/lib/network-heartbeat.js +290 -290
- package/lib/network-nts.js +574 -574
- package/lib/network-proxy.js +265 -265
- package/lib/network-tls.js +328 -328
- package/lib/network.js +233 -233
- package/lib/notify.js +612 -612
- package/lib/ntp-check.js +229 -229
- package/lib/numeric-bounds.js +111 -91
- package/lib/object-store/azure-blob-bucket-ops.js +349 -349
- package/lib/object-store/azure-blob.js +488 -488
- package/lib/object-store/gcs-bucket-ops.js +351 -351
- package/lib/object-store/gcs.js +519 -519
- package/lib/object-store/http-put.js +153 -153
- package/lib/object-store/index.js +197 -197
- package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
- package/lib/object-store/sigv4.js +903 -903
- package/lib/observability.js +151 -151
- package/lib/otel-export.js +269 -269
- package/lib/pagination.js +464 -464
- package/lib/parsers/index.js +80 -80
- package/lib/parsers/safe-env.js +642 -642
- package/lib/parsers/safe-ini.js +292 -292
- package/lib/parsers/safe-toml.js +784 -784
- package/lib/parsers/safe-xml.js +390 -390
- package/lib/parsers/safe-yaml.js +1015 -1015
- package/lib/permissions.js +708 -708
- package/lib/pqc-agent.js +87 -87
- package/lib/pqc-gate.js +279 -279
- package/lib/protobuf-encoder.js +190 -190
- package/lib/protocol-dispatcher.js +161 -161
- package/lib/pubsub-redis.js +167 -167
- package/lib/pubsub.js +429 -429
- package/lib/queue-local.js +476 -476
- package/lib/queue-redis.js +745 -745
- package/lib/queue-sqs.js +319 -319
- package/lib/queue.js +695 -695
- package/lib/redis-client.js +519 -519
- package/lib/request-helpers.js +340 -340
- package/lib/restore-bundle.js +237 -237
- package/lib/restore-rollback.js +259 -259
- package/lib/restore.js +409 -409
- package/lib/retry.js +376 -376
- package/lib/router.js +748 -748
- package/lib/safe-async.js +735 -735
- package/lib/safe-buffer.js +237 -237
- package/lib/safe-json.js +541 -541
- package/lib/safe-schema.js +1266 -1266
- package/lib/safe-url.js +159 -159
- package/lib/scheduler.js +706 -706
- package/lib/security-assert.js +373 -373
- package/lib/seeders.js +618 -618
- package/lib/session.js +478 -478
- package/lib/slug.js +269 -269
- package/lib/ssrf-guard.js +401 -401
- package/lib/static.js +184 -4
- package/lib/storage.js +471 -471
- package/lib/subject.js +281 -281
- package/lib/template.js +791 -791
- package/lib/testing.js +798 -798
- package/lib/time.js +310 -310
- package/lib/totp.js +302 -302
- package/lib/tracing.js +494 -494
- package/lib/uuid.js +132 -132
- package/lib/validate-opts.js +340 -319
- package/lib/vault/index.js +308 -308
- package/lib/vault/rotate.js +784 -784
- package/lib/vault/wrap.js +296 -296
- package/lib/vendor/noble-ciphers.cjs +9 -9
- package/lib/webhook.js +595 -595
- package/lib/websocket.js +1048 -1048
- package/package.json +77 -77
- package/sbom.cyclonedx.json +7 -7
|
@@ -1,452 +1,452 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* compression — gzip + brotli response compression.
|
|
4
|
-
*
|
|
5
|
-
* Intercepts the response stream and pipes it through node:zlib's
|
|
6
|
-
* Brotli or gzip transform when the client supports it. Brotli is
|
|
7
|
-
* preferred (better ratio for text); gzip is the fallback. Operators
|
|
8
|
-
* tune which Content-Types are eligible, the size threshold below
|
|
9
|
-
* which compression is skipped (overhead-of-compression > savings),
|
|
10
|
-
* and the per-encoding quality knobs.
|
|
11
|
-
*
|
|
12
|
-
* The middleware wraps res.writeHead, res.write, and res.end. On the
|
|
13
|
-
* first call to whichever of those happens first, it inspects the
|
|
14
|
-
* response shape (Accept-Encoding, Content-Type, Content-Length,
|
|
15
|
-
* existing Content-Encoding, status code, operator filter) and
|
|
16
|
-
* decides:
|
|
17
|
-
*
|
|
18
|
-
* - "compress with brotli/gzip" → set Content-Encoding, drop
|
|
19
|
-
* Content-Length (chunked output), pipe writes through a zlib
|
|
20
|
-
* transform stream
|
|
21
|
-
* - "skip" → pass through unchanged
|
|
22
|
-
*
|
|
23
|
-
* This decision happens once per response. After it's made, every
|
|
24
|
-
* subsequent write/end uses the chosen path.
|
|
25
|
-
*
|
|
26
|
-
* var compress = b.middleware.compression({
|
|
27
|
-
* // Bytes — don't compress responses smaller than this when
|
|
28
|
-
* // Content-Length is known. Absent Content-Length, compress.
|
|
29
|
-
* threshold: 1024,
|
|
30
|
-
* // Encodings considered, in fallback order. Operators who don't
|
|
31
|
-
* // want brotli (older clients in their fleet, etc.) drop it.
|
|
32
|
-
* encodings: ["br", "gzip"],
|
|
33
|
-
* // Content-Type allowlist. Anything matching is compressed;
|
|
34
|
-
* // anything outside (image/*, video/*, application/zip etc.)
|
|
35
|
-
* // is left alone — they're already entropy-dense.
|
|
36
|
-
* contentTypes: [
|
|
37
|
-
* "text/*",
|
|
38
|
-
* "application/json",
|
|
39
|
-
* "application/xml",
|
|
40
|
-
* "application/javascript",
|
|
41
|
-
* "application/wasm",
|
|
42
|
-
* "image/svg+xml",
|
|
43
|
-
* "application/x-blamejs-bundle",
|
|
44
|
-
* ],
|
|
45
|
-
* // gzip level 1 (fast) – 9 (max). Default 6 matches zlib default.
|
|
46
|
-
* gzipLevel: 6,
|
|
47
|
-
* // Brotli quality 0 (fast) – 11 (max). Default 4 is the standard
|
|
48
|
-
* // HTTP-server tradeoff: noticeable ratio improvement vs gzip
|
|
49
|
-
* // without the latency cost of higher levels.
|
|
50
|
-
* brotliQuality: 4,
|
|
51
|
-
* // Operator escape hatch — return false to skip compression for
|
|
52
|
-
* // this request specifically (e.g. server-sent events, where
|
|
53
|
-
* // chunked compression breaks the eventstream framing).
|
|
54
|
-
* filter: function (req, res) { return true; },
|
|
55
|
-
* });
|
|
56
|
-
* router.use(compress);
|
|
57
|
-
*
|
|
58
|
-
* What the middleware skips automatically:
|
|
59
|
-
* - Status 204 / 304 (no body)
|
|
60
|
-
* - Response already has Content-Encoding (operator pre-compressed)
|
|
61
|
-
* - Content-Type doesn't match the allowlist
|
|
62
|
-
* - Content-Length present AND < threshold
|
|
63
|
-
* - No supported encoding in Accept-Encoding (q=0 also respected)
|
|
64
|
-
* - filter(req, res) returned false
|
|
65
|
-
*
|
|
66
|
-
* Why both encodings ship by default:
|
|
67
|
-
* - Brotli is universally supported by every browser shipped after
|
|
68
|
-
* 2017 and gives ~20% better ratio on JSON/HTML than gzip.
|
|
69
|
-
* - gzip is universal; it's the fallback for any client that
|
|
70
|
-
* doesn't advertise brotli support (rare but possible — internal
|
|
71
|
-
* curl usage, ancient libraries, prom scrapers, etc.).
|
|
72
|
-
*
|
|
73
|
-
* Headers set on a compressed response:
|
|
74
|
-
* Content-Encoding: br | gzip
|
|
75
|
-
* Vary: Accept-Encoding (so caches don't serve wrong)
|
|
76
|
-
* (Content-Length is removed; chunked transfer is used)
|
|
77
|
-
*
|
|
78
|
-
* Wire-format: chunked transfer; that's standard for compressed
|
|
79
|
-
* responses since the compressed length isn't known until the
|
|
80
|
-
* compressor flushes.
|
|
81
|
-
*
|
|
82
|
-
* Out of scope for v1 (with structural reasons):
|
|
83
|
-
* - deflate encoding: superseded by gzip + brotli; no operator we
|
|
84
|
-
* know wants it. Adding it later is one constant + one Transform.
|
|
85
|
-
* - zstd: not in browser HTTP yet (Cloudflare ships it for
|
|
86
|
-
* server-to-server only); revisit when broadly supported.
|
|
87
|
-
* - dictionary compression (br with shared dictionaries): no operator
|
|
88
|
-
* demand yet; the spec is still being finalized.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
var zlib = require("node:zlib");
|
|
92
|
-
var C = require("../constants");
|
|
93
|
-
var nb = require("../numeric-bounds");
|
|
94
|
-
var requestHelpers = require("../request-helpers");
|
|
95
|
-
var validateOpts = require("../validate-opts");
|
|
96
|
-
var { defineClass } = require("../framework-error");
|
|
97
|
-
|
|
98
|
-
var CompressionError = defineClass("CompressionError", { alwaysPermanent: true });
|
|
99
|
-
|
|
100
|
-
var DEFAULT_OPTS = Object.freeze({
|
|
101
|
-
threshold: C.BYTES.kib(1),
|
|
102
|
-
encodings: ["br", "gzip"],
|
|
103
|
-
contentTypes: [
|
|
104
|
-
"text/*",
|
|
105
|
-
"application/json",
|
|
106
|
-
"application/xml",
|
|
107
|
-
"application/javascript",
|
|
108
|
-
"application/wasm",
|
|
109
|
-
"image/svg+xml",
|
|
110
|
-
"application/x-blamejs-bundle",
|
|
111
|
-
],
|
|
112
|
-
gzipLevel: 6,
|
|
113
|
-
brotliQuality: 4,
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
// Encodings the framework knows how to produce. Adding "deflate" or
|
|
117
|
-
// "zstd" later is: extend this map, the createCompressor switch, and
|
|
118
|
-
// the encodings default.
|
|
119
|
-
var SUPPORTED_ENCODINGS = new Set(["br", "gzip"]);
|
|
120
|
-
|
|
121
|
-
var HTTP_STATUS = requestHelpers.HTTP_STATUS;
|
|
122
|
-
// 205 Reset Content is rarely-used — local hex constant rather than
|
|
123
|
-
// expanding the framework's HTTP_STATUS table for a single call site.
|
|
124
|
-
var HTTP_RESET_CONTENT = 0xCD;
|
|
125
|
-
|
|
126
|
-
// Status codes that NEVER carry a body — RFC 7230 §3.3.3.
|
|
127
|
-
var NO_BODY_STATUS = new Set([HTTP_STATUS.NO_CONTENT, HTTP_RESET_CONTENT, HTTP_STATUS.NOT_MODIFIED]);
|
|
128
|
-
|
|
129
|
-
// Parse Accept-Encoding into [{ encoding, q }] sorted by q descending.
|
|
130
|
-
// Built on the shared `requestHelpers.parseQualityList` (RFC 9110
|
|
131
|
-
// §12.5). Per RFC: absent header = client accepts any encoding, which
|
|
132
|
-
// the negotiator handles via the `*` wildcard fallthrough — so an empty
|
|
133
|
-
// parsed array becomes [{ encoding: "*", q: 1 }] here for downstream
|
|
134
|
-
// uniformity. q=0 explicitly excludes an encoding (RFC 9110 §12.4.2).
|
|
135
|
-
function _parseAcceptEncoding(headerValue) {
|
|
136
|
-
if (typeof headerValue !== "string" || headerValue.length === 0) {
|
|
137
|
-
return [{ encoding: "*", q: 1 }];
|
|
138
|
-
}
|
|
139
|
-
var parsed = requestHelpers.parseQualityList(headerValue);
|
|
140
|
-
// Re-key to the legacy field name so the rest of compression.js
|
|
141
|
-
// doesn't need to change. Field renaming would churn tests for no
|
|
142
|
-
// operator-visible benefit.
|
|
143
|
-
var out = new Array(parsed.length);
|
|
144
|
-
for (var i = 0; i < parsed.length; i++) {
|
|
145
|
-
out[i] = { encoding: parsed[i].value, q: parsed[i].q };
|
|
146
|
-
}
|
|
147
|
-
return out;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// Pick the highest-q encoding from `available` that the client accepts.
|
|
151
|
-
// `available` is the operator's preferred ordering ["br", "gzip"];
|
|
152
|
-
// matching against the parsed Accept-Encoding respects q=0 exclusions.
|
|
153
|
-
function _negotiateEncoding(parsed, available) {
|
|
154
|
-
// Build a quick lookup: encoding → q
|
|
155
|
-
var clientQ = {};
|
|
156
|
-
var hasStar = false;
|
|
157
|
-
var starQ = 0;
|
|
158
|
-
for (var i = 0; i < parsed.length; i++) {
|
|
159
|
-
if (parsed[i].encoding === "*") {
|
|
160
|
-
hasStar = true;
|
|
161
|
-
starQ = parsed[i].q;
|
|
162
|
-
} else {
|
|
163
|
-
clientQ[parsed[i].encoding] = parsed[i].q;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
// Walk the operator's preference list. The first encoding the client
|
|
167
|
-
// accepts (q > 0) wins — we don't pick max-q across all options
|
|
168
|
-
// because the operator's order encodes their priority (brotli first
|
|
169
|
-
// for text-y workloads).
|
|
170
|
-
for (var j = 0; j < available.length; j++) {
|
|
171
|
-
var enc = available[j];
|
|
172
|
-
if (Object.prototype.hasOwnProperty.call(clientQ, enc)) {
|
|
173
|
-
if (clientQ[enc] > 0) return enc;
|
|
174
|
-
// q=0 explicitly excludes this encoding even if "*" would match.
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
if (hasStar && starQ > 0) return enc;
|
|
178
|
-
}
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function _typeMatches(actual, allowed) {
|
|
183
|
-
if (typeof actual !== "string") return false;
|
|
184
|
-
// Drop charset / boundary parameters — we match on the type alone.
|
|
185
|
-
var semi = actual.indexOf(";");
|
|
186
|
-
var bare = (semi === -1 ? actual : actual.slice(0, semi)).trim().toLowerCase();
|
|
187
|
-
for (var i = 0; i < allowed.length; i++) {
|
|
188
|
-
var a = allowed[i].toLowerCase();
|
|
189
|
-
if (a === bare) return true;
|
|
190
|
-
var slash = a.indexOf("/");
|
|
191
|
-
if (slash !== -1 && a.slice(slash + 1) === "*") {
|
|
192
|
-
var prefix = a.slice(0, slash + 1);
|
|
193
|
-
if (bare.indexOf(prefix) === 0) return true;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function _createCompressor(encoding, opts) {
|
|
200
|
-
if (encoding === "br") {
|
|
201
|
-
return zlib.createBrotliCompress({
|
|
202
|
-
params: {
|
|
203
|
-
[zlib.constants.BROTLI_PARAM_QUALITY]: opts.brotliQuality,
|
|
204
|
-
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
|
|
205
|
-
},
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
if (encoding === "gzip") {
|
|
209
|
-
return zlib.createGzip({ level: opts.gzipLevel });
|
|
210
|
-
}
|
|
211
|
-
throw new CompressionError("compression/unsupported-encoding",
|
|
212
|
-
"no compressor available for encoding '" + encoding + "'");
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function _appendVary(existing, token) {
|
|
216
|
-
if (!existing) return token;
|
|
217
|
-
var lc = String(existing).toLowerCase();
|
|
218
|
-
if (lc === "*") return "*"; // Vary: * already stops caches; don't dilute.
|
|
219
|
-
var parts = requestHelpers.parseListHeader(existing);
|
|
220
|
-
var lcToken = token.toLowerCase();
|
|
221
|
-
for (var i = 0; i < parts.length; i++) {
|
|
222
|
-
if (parts[i].toLowerCase() === lcToken) return String(existing);
|
|
223
|
-
}
|
|
224
|
-
parts.push(token);
|
|
225
|
-
return parts.join(", ");
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function create(opts) {
|
|
229
|
-
opts = opts || {};
|
|
230
|
-
validateOpts(opts, [
|
|
231
|
-
"threshold", "encodings", "contentTypes",
|
|
232
|
-
"gzipLevel", "brotliQuality", "filter",
|
|
233
|
-
], "middleware.compression");
|
|
234
|
-
var threshold;
|
|
235
|
-
if (opts.threshold === undefined) {
|
|
236
|
-
threshold = DEFAULT_OPTS.threshold;
|
|
237
|
-
} else if (nb.isNonNegativeFiniteInt(opts.threshold)) {
|
|
238
|
-
threshold = opts.threshold;
|
|
239
|
-
} else {
|
|
240
|
-
throw new CompressionError("compression/bad-opt",
|
|
241
|
-
"middleware.compression: threshold must be a non-negative finite integer; got " +
|
|
242
|
-
nb.shape(opts.threshold));
|
|
243
|
-
}
|
|
244
|
-
var encodings = Array.isArray(opts.encodings) && opts.encodings.length > 0
|
|
245
|
-
? opts.encodings.slice() : DEFAULT_OPTS.encodings.slice();
|
|
246
|
-
var contentTypes = Array.isArray(opts.contentTypes) && opts.contentTypes.length > 0
|
|
247
|
-
? opts.contentTypes.slice() : DEFAULT_OPTS.contentTypes.slice();
|
|
248
|
-
var gzipLevel = typeof opts.gzipLevel === "number" ? opts.gzipLevel : DEFAULT_OPTS.gzipLevel;
|
|
249
|
-
var brotliQuality = typeof opts.brotliQuality === "number" ? opts.brotliQuality : DEFAULT_OPTS.brotliQuality;
|
|
250
|
-
var filter = typeof opts.filter === "function" ? opts.filter : null;
|
|
251
|
-
|
|
252
|
-
for (var i = 0; i < encodings.length; i++) {
|
|
253
|
-
if (!SUPPORTED_ENCODINGS.has(encodings[i])) {
|
|
254
|
-
throw new CompressionError("compression/bad-encoding",
|
|
255
|
-
"encoding '" + encodings[i] + "' is not supported (allowed: " +
|
|
256
|
-
Array.from(SUPPORTED_ENCODINGS).join(", ") + ")");
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
return function compression(req, res, next) {
|
|
261
|
-
if (filter) {
|
|
262
|
-
try { if (!filter(req, res)) return next(); }
|
|
263
|
-
catch (_e) { /* operator filter threw — fall through and skip compression */ return next(); }
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// Negotiate encoding up-front from Accept-Encoding. If the client
|
|
267
|
-
// doesn't accept anything we produce, skip immediately — no need
|
|
268
|
-
// to wrap res at all.
|
|
269
|
-
var accept = _parseAcceptEncoding(req.headers && req.headers["accept-encoding"]);
|
|
270
|
-
var encoding = _negotiateEncoding(accept, encodings);
|
|
271
|
-
if (!encoding) return next();
|
|
272
|
-
|
|
273
|
-
// Capture the original methods. We replace each method with a
|
|
274
|
-
// version that decides (on first invocation) whether to compress;
|
|
275
|
-
// once decided, subsequent calls take the fast path.
|
|
276
|
-
var originalWriteHead = res.writeHead;
|
|
277
|
-
var originalWrite = res.write;
|
|
278
|
-
var originalEnd = res.end;
|
|
279
|
-
var originalSetHeader = res.setHeader ? res.setHeader.bind(res) : null;
|
|
280
|
-
var originalGetHeader = res.getHeader ? res.getHeader.bind(res) : null;
|
|
281
|
-
var originalRemoveHdr = res.removeHeader ? res.removeHeader.bind(res) : null;
|
|
282
|
-
|
|
283
|
-
var decided = false; // has the compress/skip decision been made?
|
|
284
|
-
var compress = false; // outcome of the decision
|
|
285
|
-
var compressor = null; // zlib transform when compressing
|
|
286
|
-
|
|
287
|
-
function _decide(statusCode, headersObj) {
|
|
288
|
-
if (decided) return;
|
|
289
|
-
decided = true;
|
|
290
|
-
|
|
291
|
-
// Status precludes body? skip.
|
|
292
|
-
if (NO_BODY_STATUS.has(statusCode)) { compress = false; return; }
|
|
293
|
-
|
|
294
|
-
// Already compressed by the handler / earlier middleware? skip.
|
|
295
|
-
var existingCE = (headersObj && headersObj["content-encoding"]) ||
|
|
296
|
-
(originalGetHeader && originalGetHeader("Content-Encoding"));
|
|
297
|
-
if (existingCE) { compress = false; return; }
|
|
298
|
-
|
|
299
|
-
// Content-Type allowlist
|
|
300
|
-
var ct = (headersObj && headersObj["content-type"]) ||
|
|
301
|
-
(originalGetHeader && originalGetHeader("Content-Type"));
|
|
302
|
-
if (!_typeMatches(ct, contentTypes)) { compress = false; return; }
|
|
303
|
-
|
|
304
|
-
// Content-Length below threshold? skip. If absent, compress.
|
|
305
|
-
var clRaw = (headersObj && headersObj["content-length"]) ||
|
|
306
|
-
(originalGetHeader && originalGetHeader("Content-Length"));
|
|
307
|
-
if (clRaw != null) {
|
|
308
|
-
var cl = parseInt(clRaw, 10);
|
|
309
|
-
if (!isNaN(cl) && cl < threshold) { compress = false; return; }
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
compress = true;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
function _wireCompressor() {
|
|
316
|
-
compressor = _createCompressor(encoding, { gzipLevel: gzipLevel, brotliQuality: brotliQuality });
|
|
317
|
-
// Pipe compressed bytes back to the original socket via the
|
|
318
|
-
// pre-wrap res.write. We bypass our wrapped res.write on purpose
|
|
319
|
-
// — the wrapped version would re-feed into the compressor and
|
|
320
|
-
// create a loop.
|
|
321
|
-
compressor.on("data", function (chunk) {
|
|
322
|
-
originalWrite.call(res, chunk);
|
|
323
|
-
});
|
|
324
|
-
compressor.on("end", function () {
|
|
325
|
-
originalEnd.call(res);
|
|
326
|
-
});
|
|
327
|
-
compressor.on("error", function () {
|
|
328
|
-
// On compressor error the response is already half-written;
|
|
329
|
-
// there's not much we can do beyond closing the underlying
|
|
330
|
-
// socket. Best-effort end the response.
|
|
331
|
-
try { originalEnd.call(res); } catch (_e) { /* response already ended */ }
|
|
332
|
-
});
|
|
333
|
-
// Forward drain. Without this, source streams piped into our
|
|
334
|
-
// wrapped res (e.g. fs.createReadStream from staticServe) stall:
|
|
335
|
-
// when compressor's internal buffer hits highWaterMark,
|
|
336
|
-
// compressor.write returns false, our wrapped res.write returns
|
|
337
|
-
// false, and pipe pauses the source waiting for res 'drain' —
|
|
338
|
-
// which never fires unless we emit it ourselves.
|
|
339
|
-
compressor.on("drain", function () {
|
|
340
|
-
if (typeof res.emit === "function") res.emit("drain");
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// Normalize header object keys to lowercase for consistent inspection.
|
|
345
|
-
function _lowerObj(o) {
|
|
346
|
-
if (!o) return null;
|
|
347
|
-
var out = {};
|
|
348
|
-
var keys = Object.keys(o);
|
|
349
|
-
for (var i = 0; i < keys.length; i++) out[keys[i].toLowerCase()] = o[keys[i]];
|
|
350
|
-
return out;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// Apply the compression-side header tweaks (Content-Encoding,
|
|
354
|
-
// Vary, drop Content-Length) right before the headers go on the
|
|
355
|
-
// wire. We modify either the writeHead-supplied `headers` object,
|
|
356
|
-
// or fall back to res.setHeader / res.removeHeader for the
|
|
357
|
-
// implicit-headers path.
|
|
358
|
-
function _applyCompressedHeaders(headersObj) {
|
|
359
|
-
if (headersObj) {
|
|
360
|
-
// Walk and remove existing Content-Length (any case).
|
|
361
|
-
var hk = Object.keys(headersObj);
|
|
362
|
-
for (var i = 0; i < hk.length; i++) {
|
|
363
|
-
if (hk[i].toLowerCase() === "content-length") delete headersObj[hk[i]];
|
|
364
|
-
}
|
|
365
|
-
headersObj["Content-Encoding"] = encoding;
|
|
366
|
-
// Vary: append Accept-Encoding without clobbering an operator-set Vary.
|
|
367
|
-
var existingVary = headersObj["Vary"] || headersObj["vary"];
|
|
368
|
-
headersObj["Vary"] = _appendVary(existingVary, "Accept-Encoding");
|
|
369
|
-
} else {
|
|
370
|
-
if (originalRemoveHdr) {
|
|
371
|
-
try { originalRemoveHdr("Content-Length"); } catch (_e) { /* header may not be set */ }
|
|
372
|
-
}
|
|
373
|
-
if (originalSetHeader) {
|
|
374
|
-
try { originalSetHeader("Content-Encoding", encoding); } catch (_e) { /* headers already sent */ }
|
|
375
|
-
var existing = originalGetHeader && originalGetHeader("Vary");
|
|
376
|
-
try { originalSetHeader("Vary", _appendVary(existing, "Accept-Encoding")); } catch (_e) { /* headers already sent */ }
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
res.writeHead = function (statusCode, statusMessageOrHeaders, headersIfMessage) {
|
|
382
|
-
// Node accepts: writeHead(status), writeHead(status, headers),
|
|
383
|
-
// writeHead(status, statusMessage, headers).
|
|
384
|
-
var headersObj = null;
|
|
385
|
-
if (headersIfMessage && typeof headersIfMessage === "object") {
|
|
386
|
-
headersObj = headersIfMessage;
|
|
387
|
-
} else if (statusMessageOrHeaders && typeof statusMessageOrHeaders === "object" && !Array.isArray(statusMessageOrHeaders)) {
|
|
388
|
-
headersObj = statusMessageOrHeaders;
|
|
389
|
-
}
|
|
390
|
-
_decide(statusCode, _lowerObj(headersObj));
|
|
391
|
-
if (compress) _applyCompressedHeaders(headersObj);
|
|
392
|
-
return originalWriteHead.apply(res, arguments);
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
res.write = function (chunk, encArg, cbArg) {
|
|
396
|
-
if (!decided) {
|
|
397
|
-
// Implicit header path — handler did res.write() before
|
|
398
|
-
// res.writeHead(). Use the implicit Content-Type from setHeader
|
|
399
|
-
// (or absence) to decide. Default Node behavior triggers
|
|
400
|
-
// writeHead with statusCode=200 here; we mirror that.
|
|
401
|
-
_decide(HTTP_STATUS.OK, null);
|
|
402
|
-
if (compress) _applyCompressedHeaders(null);
|
|
403
|
-
}
|
|
404
|
-
if (!compress) return originalWrite.call(res, chunk, encArg, cbArg);
|
|
405
|
-
if (!compressor) _wireCompressor();
|
|
406
|
-
// Coerce string→Buffer with the operator-supplied encoding so
|
|
407
|
-
// the compressor sees raw bytes consistently.
|
|
408
|
-
var buf;
|
|
409
|
-
if (Buffer.isBuffer(chunk)) buf = chunk;
|
|
410
|
-
else if (typeof chunk === "string") buf = Buffer.from(chunk, typeof encArg === "string" ? encArg : "utf8");
|
|
411
|
-
else if (chunk != null) buf = Buffer.from(String(chunk));
|
|
412
|
-
else buf = null;
|
|
413
|
-
var ret = buf ? compressor.write(buf) : true;
|
|
414
|
-
if (typeof cbArg === "function") cbArg();
|
|
415
|
-
else if (typeof encArg === "function") encArg();
|
|
416
|
-
return ret;
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
res.end = function (chunk, encArg, cbArg) {
|
|
420
|
-
if (!decided) {
|
|
421
|
-
_decide(HTTP_STATUS.OK, null);
|
|
422
|
-
if (compress) _applyCompressedHeaders(null);
|
|
423
|
-
}
|
|
424
|
-
if (!compress) return originalEnd.call(res, chunk, encArg, cbArg);
|
|
425
|
-
if (!compressor) _wireCompressor();
|
|
426
|
-
if (chunk != null) {
|
|
427
|
-
var buf;
|
|
428
|
-
if (Buffer.isBuffer(chunk)) buf = chunk;
|
|
429
|
-
else if (typeof chunk === "string") buf = Buffer.from(chunk, typeof encArg === "string" ? encArg : "utf8");
|
|
430
|
-
else buf = Buffer.from(String(chunk));
|
|
431
|
-
compressor.write(buf);
|
|
432
|
-
}
|
|
433
|
-
compressor.end();
|
|
434
|
-
if (typeof cbArg === "function") cbArg();
|
|
435
|
-
else if (typeof encArg === "function") encArg();
|
|
436
|
-
return res;
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
return next();
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
module.exports = {
|
|
444
|
-
create: create,
|
|
445
|
-
CompressionError: CompressionError,
|
|
446
|
-
// Internal helpers exposed for tests.
|
|
447
|
-
_parseAcceptEncoding: _parseAcceptEncoding,
|
|
448
|
-
_negotiateEncoding: _negotiateEncoding,
|
|
449
|
-
_typeMatches: _typeMatches,
|
|
450
|
-
_appendVary: _appendVary,
|
|
451
|
-
SUPPORTED_ENCODINGS: SUPPORTED_ENCODINGS,
|
|
452
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* compression — gzip + brotli response compression.
|
|
4
|
+
*
|
|
5
|
+
* Intercepts the response stream and pipes it through node:zlib's
|
|
6
|
+
* Brotli or gzip transform when the client supports it. Brotli is
|
|
7
|
+
* preferred (better ratio for text); gzip is the fallback. Operators
|
|
8
|
+
* tune which Content-Types are eligible, the size threshold below
|
|
9
|
+
* which compression is skipped (overhead-of-compression > savings),
|
|
10
|
+
* and the per-encoding quality knobs.
|
|
11
|
+
*
|
|
12
|
+
* The middleware wraps res.writeHead, res.write, and res.end. On the
|
|
13
|
+
* first call to whichever of those happens first, it inspects the
|
|
14
|
+
* response shape (Accept-Encoding, Content-Type, Content-Length,
|
|
15
|
+
* existing Content-Encoding, status code, operator filter) and
|
|
16
|
+
* decides:
|
|
17
|
+
*
|
|
18
|
+
* - "compress with brotli/gzip" → set Content-Encoding, drop
|
|
19
|
+
* Content-Length (chunked output), pipe writes through a zlib
|
|
20
|
+
* transform stream
|
|
21
|
+
* - "skip" → pass through unchanged
|
|
22
|
+
*
|
|
23
|
+
* This decision happens once per response. After it's made, every
|
|
24
|
+
* subsequent write/end uses the chosen path.
|
|
25
|
+
*
|
|
26
|
+
* var compress = b.middleware.compression({
|
|
27
|
+
* // Bytes — don't compress responses smaller than this when
|
|
28
|
+
* // Content-Length is known. Absent Content-Length, compress.
|
|
29
|
+
* threshold: 1024,
|
|
30
|
+
* // Encodings considered, in fallback order. Operators who don't
|
|
31
|
+
* // want brotli (older clients in their fleet, etc.) drop it.
|
|
32
|
+
* encodings: ["br", "gzip"],
|
|
33
|
+
* // Content-Type allowlist. Anything matching is compressed;
|
|
34
|
+
* // anything outside (image/*, video/*, application/zip etc.)
|
|
35
|
+
* // is left alone — they're already entropy-dense.
|
|
36
|
+
* contentTypes: [
|
|
37
|
+
* "text/*",
|
|
38
|
+
* "application/json",
|
|
39
|
+
* "application/xml",
|
|
40
|
+
* "application/javascript",
|
|
41
|
+
* "application/wasm",
|
|
42
|
+
* "image/svg+xml",
|
|
43
|
+
* "application/x-blamejs-bundle",
|
|
44
|
+
* ],
|
|
45
|
+
* // gzip level 1 (fast) – 9 (max). Default 6 matches zlib default.
|
|
46
|
+
* gzipLevel: 6,
|
|
47
|
+
* // Brotli quality 0 (fast) – 11 (max). Default 4 is the standard
|
|
48
|
+
* // HTTP-server tradeoff: noticeable ratio improvement vs gzip
|
|
49
|
+
* // without the latency cost of higher levels.
|
|
50
|
+
* brotliQuality: 4,
|
|
51
|
+
* // Operator escape hatch — return false to skip compression for
|
|
52
|
+
* // this request specifically (e.g. server-sent events, where
|
|
53
|
+
* // chunked compression breaks the eventstream framing).
|
|
54
|
+
* filter: function (req, res) { return true; },
|
|
55
|
+
* });
|
|
56
|
+
* router.use(compress);
|
|
57
|
+
*
|
|
58
|
+
* What the middleware skips automatically:
|
|
59
|
+
* - Status 204 / 304 (no body)
|
|
60
|
+
* - Response already has Content-Encoding (operator pre-compressed)
|
|
61
|
+
* - Content-Type doesn't match the allowlist
|
|
62
|
+
* - Content-Length present AND < threshold
|
|
63
|
+
* - No supported encoding in Accept-Encoding (q=0 also respected)
|
|
64
|
+
* - filter(req, res) returned false
|
|
65
|
+
*
|
|
66
|
+
* Why both encodings ship by default:
|
|
67
|
+
* - Brotli is universally supported by every browser shipped after
|
|
68
|
+
* 2017 and gives ~20% better ratio on JSON/HTML than gzip.
|
|
69
|
+
* - gzip is universal; it's the fallback for any client that
|
|
70
|
+
* doesn't advertise brotli support (rare but possible — internal
|
|
71
|
+
* curl usage, ancient libraries, prom scrapers, etc.).
|
|
72
|
+
*
|
|
73
|
+
* Headers set on a compressed response:
|
|
74
|
+
* Content-Encoding: br | gzip
|
|
75
|
+
* Vary: Accept-Encoding (so caches don't serve wrong)
|
|
76
|
+
* (Content-Length is removed; chunked transfer is used)
|
|
77
|
+
*
|
|
78
|
+
* Wire-format: chunked transfer; that's standard for compressed
|
|
79
|
+
* responses since the compressed length isn't known until the
|
|
80
|
+
* compressor flushes.
|
|
81
|
+
*
|
|
82
|
+
* Out of scope for v1 (with structural reasons):
|
|
83
|
+
* - deflate encoding: superseded by gzip + brotli; no operator we
|
|
84
|
+
* know wants it. Adding it later is one constant + one Transform.
|
|
85
|
+
* - zstd: not in browser HTTP yet (Cloudflare ships it for
|
|
86
|
+
* server-to-server only); revisit when broadly supported.
|
|
87
|
+
* - dictionary compression (br with shared dictionaries): no operator
|
|
88
|
+
* demand yet; the spec is still being finalized.
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
var zlib = require("node:zlib");
|
|
92
|
+
var C = require("../constants");
|
|
93
|
+
var nb = require("../numeric-bounds");
|
|
94
|
+
var requestHelpers = require("../request-helpers");
|
|
95
|
+
var validateOpts = require("../validate-opts");
|
|
96
|
+
var { defineClass } = require("../framework-error");
|
|
97
|
+
|
|
98
|
+
var CompressionError = defineClass("CompressionError", { alwaysPermanent: true });
|
|
99
|
+
|
|
100
|
+
var DEFAULT_OPTS = Object.freeze({
|
|
101
|
+
threshold: C.BYTES.kib(1),
|
|
102
|
+
encodings: ["br", "gzip"],
|
|
103
|
+
contentTypes: [
|
|
104
|
+
"text/*",
|
|
105
|
+
"application/json",
|
|
106
|
+
"application/xml",
|
|
107
|
+
"application/javascript",
|
|
108
|
+
"application/wasm",
|
|
109
|
+
"image/svg+xml",
|
|
110
|
+
"application/x-blamejs-bundle",
|
|
111
|
+
],
|
|
112
|
+
gzipLevel: 6,
|
|
113
|
+
brotliQuality: 4,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Encodings the framework knows how to produce. Adding "deflate" or
|
|
117
|
+
// "zstd" later is: extend this map, the createCompressor switch, and
|
|
118
|
+
// the encodings default.
|
|
119
|
+
var SUPPORTED_ENCODINGS = new Set(["br", "gzip"]);
|
|
120
|
+
|
|
121
|
+
var HTTP_STATUS = requestHelpers.HTTP_STATUS;
|
|
122
|
+
// 205 Reset Content is rarely-used — local hex constant rather than
|
|
123
|
+
// expanding the framework's HTTP_STATUS table for a single call site.
|
|
124
|
+
var HTTP_RESET_CONTENT = 0xCD;
|
|
125
|
+
|
|
126
|
+
// Status codes that NEVER carry a body — RFC 7230 §3.3.3.
|
|
127
|
+
var NO_BODY_STATUS = new Set([HTTP_STATUS.NO_CONTENT, HTTP_RESET_CONTENT, HTTP_STATUS.NOT_MODIFIED]);
|
|
128
|
+
|
|
129
|
+
// Parse Accept-Encoding into [{ encoding, q }] sorted by q descending.
|
|
130
|
+
// Built on the shared `requestHelpers.parseQualityList` (RFC 9110
|
|
131
|
+
// §12.5). Per RFC: absent header = client accepts any encoding, which
|
|
132
|
+
// the negotiator handles via the `*` wildcard fallthrough — so an empty
|
|
133
|
+
// parsed array becomes [{ encoding: "*", q: 1 }] here for downstream
|
|
134
|
+
// uniformity. q=0 explicitly excludes an encoding (RFC 9110 §12.4.2).
|
|
135
|
+
function _parseAcceptEncoding(headerValue) {
|
|
136
|
+
if (typeof headerValue !== "string" || headerValue.length === 0) {
|
|
137
|
+
return [{ encoding: "*", q: 1 }];
|
|
138
|
+
}
|
|
139
|
+
var parsed = requestHelpers.parseQualityList(headerValue);
|
|
140
|
+
// Re-key to the legacy field name so the rest of compression.js
|
|
141
|
+
// doesn't need to change. Field renaming would churn tests for no
|
|
142
|
+
// operator-visible benefit.
|
|
143
|
+
var out = new Array(parsed.length);
|
|
144
|
+
for (var i = 0; i < parsed.length; i++) {
|
|
145
|
+
out[i] = { encoding: parsed[i].value, q: parsed[i].q };
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Pick the highest-q encoding from `available` that the client accepts.
|
|
151
|
+
// `available` is the operator's preferred ordering ["br", "gzip"];
|
|
152
|
+
// matching against the parsed Accept-Encoding respects q=0 exclusions.
|
|
153
|
+
function _negotiateEncoding(parsed, available) {
|
|
154
|
+
// Build a quick lookup: encoding → q
|
|
155
|
+
var clientQ = {};
|
|
156
|
+
var hasStar = false;
|
|
157
|
+
var starQ = 0;
|
|
158
|
+
for (var i = 0; i < parsed.length; i++) {
|
|
159
|
+
if (parsed[i].encoding === "*") {
|
|
160
|
+
hasStar = true;
|
|
161
|
+
starQ = parsed[i].q;
|
|
162
|
+
} else {
|
|
163
|
+
clientQ[parsed[i].encoding] = parsed[i].q;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Walk the operator's preference list. The first encoding the client
|
|
167
|
+
// accepts (q > 0) wins — we don't pick max-q across all options
|
|
168
|
+
// because the operator's order encodes their priority (brotli first
|
|
169
|
+
// for text-y workloads).
|
|
170
|
+
for (var j = 0; j < available.length; j++) {
|
|
171
|
+
var enc = available[j];
|
|
172
|
+
if (Object.prototype.hasOwnProperty.call(clientQ, enc)) {
|
|
173
|
+
if (clientQ[enc] > 0) return enc;
|
|
174
|
+
// q=0 explicitly excludes this encoding even if "*" would match.
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (hasStar && starQ > 0) return enc;
|
|
178
|
+
}
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function _typeMatches(actual, allowed) {
|
|
183
|
+
if (typeof actual !== "string") return false;
|
|
184
|
+
// Drop charset / boundary parameters — we match on the type alone.
|
|
185
|
+
var semi = actual.indexOf(";");
|
|
186
|
+
var bare = (semi === -1 ? actual : actual.slice(0, semi)).trim().toLowerCase();
|
|
187
|
+
for (var i = 0; i < allowed.length; i++) {
|
|
188
|
+
var a = allowed[i].toLowerCase();
|
|
189
|
+
if (a === bare) return true;
|
|
190
|
+
var slash = a.indexOf("/");
|
|
191
|
+
if (slash !== -1 && a.slice(slash + 1) === "*") {
|
|
192
|
+
var prefix = a.slice(0, slash + 1);
|
|
193
|
+
if (bare.indexOf(prefix) === 0) return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function _createCompressor(encoding, opts) {
|
|
200
|
+
if (encoding === "br") {
|
|
201
|
+
return zlib.createBrotliCompress({
|
|
202
|
+
params: {
|
|
203
|
+
[zlib.constants.BROTLI_PARAM_QUALITY]: opts.brotliQuality,
|
|
204
|
+
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
if (encoding === "gzip") {
|
|
209
|
+
return zlib.createGzip({ level: opts.gzipLevel });
|
|
210
|
+
}
|
|
211
|
+
throw new CompressionError("compression/unsupported-encoding",
|
|
212
|
+
"no compressor available for encoding '" + encoding + "'");
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function _appendVary(existing, token) {
|
|
216
|
+
if (!existing) return token;
|
|
217
|
+
var lc = String(existing).toLowerCase();
|
|
218
|
+
if (lc === "*") return "*"; // Vary: * already stops caches; don't dilute.
|
|
219
|
+
var parts = requestHelpers.parseListHeader(existing);
|
|
220
|
+
var lcToken = token.toLowerCase();
|
|
221
|
+
for (var i = 0; i < parts.length; i++) {
|
|
222
|
+
if (parts[i].toLowerCase() === lcToken) return String(existing);
|
|
223
|
+
}
|
|
224
|
+
parts.push(token);
|
|
225
|
+
return parts.join(", ");
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function create(opts) {
|
|
229
|
+
opts = opts || {};
|
|
230
|
+
validateOpts(opts, [
|
|
231
|
+
"threshold", "encodings", "contentTypes",
|
|
232
|
+
"gzipLevel", "brotliQuality", "filter",
|
|
233
|
+
], "middleware.compression");
|
|
234
|
+
var threshold;
|
|
235
|
+
if (opts.threshold === undefined) {
|
|
236
|
+
threshold = DEFAULT_OPTS.threshold;
|
|
237
|
+
} else if (nb.isNonNegativeFiniteInt(opts.threshold)) {
|
|
238
|
+
threshold = opts.threshold;
|
|
239
|
+
} else {
|
|
240
|
+
throw new CompressionError("compression/bad-opt",
|
|
241
|
+
"middleware.compression: threshold must be a non-negative finite integer; got " +
|
|
242
|
+
nb.shape(opts.threshold));
|
|
243
|
+
}
|
|
244
|
+
var encodings = Array.isArray(opts.encodings) && opts.encodings.length > 0
|
|
245
|
+
? opts.encodings.slice() : DEFAULT_OPTS.encodings.slice();
|
|
246
|
+
var contentTypes = Array.isArray(opts.contentTypes) && opts.contentTypes.length > 0
|
|
247
|
+
? opts.contentTypes.slice() : DEFAULT_OPTS.contentTypes.slice();
|
|
248
|
+
var gzipLevel = typeof opts.gzipLevel === "number" ? opts.gzipLevel : DEFAULT_OPTS.gzipLevel;
|
|
249
|
+
var brotliQuality = typeof opts.brotliQuality === "number" ? opts.brotliQuality : DEFAULT_OPTS.brotliQuality;
|
|
250
|
+
var filter = typeof opts.filter === "function" ? opts.filter : null;
|
|
251
|
+
|
|
252
|
+
for (var i = 0; i < encodings.length; i++) {
|
|
253
|
+
if (!SUPPORTED_ENCODINGS.has(encodings[i])) {
|
|
254
|
+
throw new CompressionError("compression/bad-encoding",
|
|
255
|
+
"encoding '" + encodings[i] + "' is not supported (allowed: " +
|
|
256
|
+
Array.from(SUPPORTED_ENCODINGS).join(", ") + ")");
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return function compression(req, res, next) {
|
|
261
|
+
if (filter) {
|
|
262
|
+
try { if (!filter(req, res)) return next(); }
|
|
263
|
+
catch (_e) { /* operator filter threw — fall through and skip compression */ return next(); }
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Negotiate encoding up-front from Accept-Encoding. If the client
|
|
267
|
+
// doesn't accept anything we produce, skip immediately — no need
|
|
268
|
+
// to wrap res at all.
|
|
269
|
+
var accept = _parseAcceptEncoding(req.headers && req.headers["accept-encoding"]);
|
|
270
|
+
var encoding = _negotiateEncoding(accept, encodings);
|
|
271
|
+
if (!encoding) return next();
|
|
272
|
+
|
|
273
|
+
// Capture the original methods. We replace each method with a
|
|
274
|
+
// version that decides (on first invocation) whether to compress;
|
|
275
|
+
// once decided, subsequent calls take the fast path.
|
|
276
|
+
var originalWriteHead = res.writeHead;
|
|
277
|
+
var originalWrite = res.write;
|
|
278
|
+
var originalEnd = res.end;
|
|
279
|
+
var originalSetHeader = res.setHeader ? res.setHeader.bind(res) : null;
|
|
280
|
+
var originalGetHeader = res.getHeader ? res.getHeader.bind(res) : null;
|
|
281
|
+
var originalRemoveHdr = res.removeHeader ? res.removeHeader.bind(res) : null;
|
|
282
|
+
|
|
283
|
+
var decided = false; // has the compress/skip decision been made?
|
|
284
|
+
var compress = false; // outcome of the decision
|
|
285
|
+
var compressor = null; // zlib transform when compressing
|
|
286
|
+
|
|
287
|
+
function _decide(statusCode, headersObj) {
|
|
288
|
+
if (decided) return;
|
|
289
|
+
decided = true;
|
|
290
|
+
|
|
291
|
+
// Status precludes body? skip.
|
|
292
|
+
if (NO_BODY_STATUS.has(statusCode)) { compress = false; return; }
|
|
293
|
+
|
|
294
|
+
// Already compressed by the handler / earlier middleware? skip.
|
|
295
|
+
var existingCE = (headersObj && headersObj["content-encoding"]) ||
|
|
296
|
+
(originalGetHeader && originalGetHeader("Content-Encoding"));
|
|
297
|
+
if (existingCE) { compress = false; return; }
|
|
298
|
+
|
|
299
|
+
// Content-Type allowlist
|
|
300
|
+
var ct = (headersObj && headersObj["content-type"]) ||
|
|
301
|
+
(originalGetHeader && originalGetHeader("Content-Type"));
|
|
302
|
+
if (!_typeMatches(ct, contentTypes)) { compress = false; return; }
|
|
303
|
+
|
|
304
|
+
// Content-Length below threshold? skip. If absent, compress.
|
|
305
|
+
var clRaw = (headersObj && headersObj["content-length"]) ||
|
|
306
|
+
(originalGetHeader && originalGetHeader("Content-Length"));
|
|
307
|
+
if (clRaw != null) {
|
|
308
|
+
var cl = parseInt(clRaw, 10);
|
|
309
|
+
if (!isNaN(cl) && cl < threshold) { compress = false; return; }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
compress = true;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function _wireCompressor() {
|
|
316
|
+
compressor = _createCompressor(encoding, { gzipLevel: gzipLevel, brotliQuality: brotliQuality });
|
|
317
|
+
// Pipe compressed bytes back to the original socket via the
|
|
318
|
+
// pre-wrap res.write. We bypass our wrapped res.write on purpose
|
|
319
|
+
// — the wrapped version would re-feed into the compressor and
|
|
320
|
+
// create a loop.
|
|
321
|
+
compressor.on("data", function (chunk) {
|
|
322
|
+
originalWrite.call(res, chunk);
|
|
323
|
+
});
|
|
324
|
+
compressor.on("end", function () {
|
|
325
|
+
originalEnd.call(res);
|
|
326
|
+
});
|
|
327
|
+
compressor.on("error", function () {
|
|
328
|
+
// On compressor error the response is already half-written;
|
|
329
|
+
// there's not much we can do beyond closing the underlying
|
|
330
|
+
// socket. Best-effort end the response.
|
|
331
|
+
try { originalEnd.call(res); } catch (_e) { /* response already ended */ }
|
|
332
|
+
});
|
|
333
|
+
// Forward drain. Without this, source streams piped into our
|
|
334
|
+
// wrapped res (e.g. fs.createReadStream from staticServe) stall:
|
|
335
|
+
// when compressor's internal buffer hits highWaterMark,
|
|
336
|
+
// compressor.write returns false, our wrapped res.write returns
|
|
337
|
+
// false, and pipe pauses the source waiting for res 'drain' —
|
|
338
|
+
// which never fires unless we emit it ourselves.
|
|
339
|
+
compressor.on("drain", function () {
|
|
340
|
+
if (typeof res.emit === "function") res.emit("drain");
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Normalize header object keys to lowercase for consistent inspection.
|
|
345
|
+
function _lowerObj(o) {
|
|
346
|
+
if (!o) return null;
|
|
347
|
+
var out = {};
|
|
348
|
+
var keys = Object.keys(o);
|
|
349
|
+
for (var i = 0; i < keys.length; i++) out[keys[i].toLowerCase()] = o[keys[i]];
|
|
350
|
+
return out;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Apply the compression-side header tweaks (Content-Encoding,
|
|
354
|
+
// Vary, drop Content-Length) right before the headers go on the
|
|
355
|
+
// wire. We modify either the writeHead-supplied `headers` object,
|
|
356
|
+
// or fall back to res.setHeader / res.removeHeader for the
|
|
357
|
+
// implicit-headers path.
|
|
358
|
+
function _applyCompressedHeaders(headersObj) {
|
|
359
|
+
if (headersObj) {
|
|
360
|
+
// Walk and remove existing Content-Length (any case).
|
|
361
|
+
var hk = Object.keys(headersObj);
|
|
362
|
+
for (var i = 0; i < hk.length; i++) {
|
|
363
|
+
if (hk[i].toLowerCase() === "content-length") delete headersObj[hk[i]];
|
|
364
|
+
}
|
|
365
|
+
headersObj["Content-Encoding"] = encoding;
|
|
366
|
+
// Vary: append Accept-Encoding without clobbering an operator-set Vary.
|
|
367
|
+
var existingVary = headersObj["Vary"] || headersObj["vary"];
|
|
368
|
+
headersObj["Vary"] = _appendVary(existingVary, "Accept-Encoding");
|
|
369
|
+
} else {
|
|
370
|
+
if (originalRemoveHdr) {
|
|
371
|
+
try { originalRemoveHdr("Content-Length"); } catch (_e) { /* header may not be set */ }
|
|
372
|
+
}
|
|
373
|
+
if (originalSetHeader) {
|
|
374
|
+
try { originalSetHeader("Content-Encoding", encoding); } catch (_e) { /* headers already sent */ }
|
|
375
|
+
var existing = originalGetHeader && originalGetHeader("Vary");
|
|
376
|
+
try { originalSetHeader("Vary", _appendVary(existing, "Accept-Encoding")); } catch (_e) { /* headers already sent */ }
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
res.writeHead = function (statusCode, statusMessageOrHeaders, headersIfMessage) {
|
|
382
|
+
// Node accepts: writeHead(status), writeHead(status, headers),
|
|
383
|
+
// writeHead(status, statusMessage, headers).
|
|
384
|
+
var headersObj = null;
|
|
385
|
+
if (headersIfMessage && typeof headersIfMessage === "object") {
|
|
386
|
+
headersObj = headersIfMessage;
|
|
387
|
+
} else if (statusMessageOrHeaders && typeof statusMessageOrHeaders === "object" && !Array.isArray(statusMessageOrHeaders)) {
|
|
388
|
+
headersObj = statusMessageOrHeaders;
|
|
389
|
+
}
|
|
390
|
+
_decide(statusCode, _lowerObj(headersObj));
|
|
391
|
+
if (compress) _applyCompressedHeaders(headersObj);
|
|
392
|
+
return originalWriteHead.apply(res, arguments);
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
res.write = function (chunk, encArg, cbArg) {
|
|
396
|
+
if (!decided) {
|
|
397
|
+
// Implicit header path — handler did res.write() before
|
|
398
|
+
// res.writeHead(). Use the implicit Content-Type from setHeader
|
|
399
|
+
// (or absence) to decide. Default Node behavior triggers
|
|
400
|
+
// writeHead with statusCode=200 here; we mirror that.
|
|
401
|
+
_decide(HTTP_STATUS.OK, null);
|
|
402
|
+
if (compress) _applyCompressedHeaders(null);
|
|
403
|
+
}
|
|
404
|
+
if (!compress) return originalWrite.call(res, chunk, encArg, cbArg);
|
|
405
|
+
if (!compressor) _wireCompressor();
|
|
406
|
+
// Coerce string→Buffer with the operator-supplied encoding so
|
|
407
|
+
// the compressor sees raw bytes consistently.
|
|
408
|
+
var buf;
|
|
409
|
+
if (Buffer.isBuffer(chunk)) buf = chunk;
|
|
410
|
+
else if (typeof chunk === "string") buf = Buffer.from(chunk, typeof encArg === "string" ? encArg : "utf8");
|
|
411
|
+
else if (chunk != null) buf = Buffer.from(String(chunk));
|
|
412
|
+
else buf = null;
|
|
413
|
+
var ret = buf ? compressor.write(buf) : true;
|
|
414
|
+
if (typeof cbArg === "function") cbArg();
|
|
415
|
+
else if (typeof encArg === "function") encArg();
|
|
416
|
+
return ret;
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
res.end = function (chunk, encArg, cbArg) {
|
|
420
|
+
if (!decided) {
|
|
421
|
+
_decide(HTTP_STATUS.OK, null);
|
|
422
|
+
if (compress) _applyCompressedHeaders(null);
|
|
423
|
+
}
|
|
424
|
+
if (!compress) return originalEnd.call(res, chunk, encArg, cbArg);
|
|
425
|
+
if (!compressor) _wireCompressor();
|
|
426
|
+
if (chunk != null) {
|
|
427
|
+
var buf;
|
|
428
|
+
if (Buffer.isBuffer(chunk)) buf = chunk;
|
|
429
|
+
else if (typeof chunk === "string") buf = Buffer.from(chunk, typeof encArg === "string" ? encArg : "utf8");
|
|
430
|
+
else buf = Buffer.from(String(chunk));
|
|
431
|
+
compressor.write(buf);
|
|
432
|
+
}
|
|
433
|
+
compressor.end();
|
|
434
|
+
if (typeof cbArg === "function") cbArg();
|
|
435
|
+
else if (typeof encArg === "function") encArg();
|
|
436
|
+
return res;
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
return next();
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
module.exports = {
|
|
444
|
+
create: create,
|
|
445
|
+
CompressionError: CompressionError,
|
|
446
|
+
// Internal helpers exposed for tests.
|
|
447
|
+
_parseAcceptEncoding: _parseAcceptEncoding,
|
|
448
|
+
_negotiateEncoding: _negotiateEncoding,
|
|
449
|
+
_typeMatches: _typeMatches,
|
|
450
|
+
_appendVary: _appendVary,
|
|
451
|
+
SUPPORTED_ENCODINGS: SUPPORTED_ENCODINGS,
|
|
452
|
+
};
|