@blamejs/core 0.7.0 → 0.7.4
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 +8 -0
- package/README.md +2 -2
- package/index.js +2 -0
- package/lib/api-key.js +1 -10
- package/lib/cache-redis.js +1 -11
- package/lib/cache.js +11 -16
- package/lib/file-upload.js +933 -0
- package/lib/framework-error.js +13 -0
- package/lib/log-stream-syslog.js +1 -1
- package/lib/middleware/api-encrypt.js +415 -52
- package/lib/middleware/db-role-for.js +3 -8
- package/lib/notify.js +3 -5
- package/lib/object-store/azure-blob.js +42 -5
- package/lib/object-store/gcs.js +47 -7
- package/lib/object-store/sigv4.js +55 -7
- package/lib/pubsub-redis.js +1 -11
- package/lib/queue-redis.js +1 -8
- package/lib/redis-client.js +30 -0
- package/lib/request-helpers.js +21 -17
- package/lib/seeders.js +5 -17
- package/lib/static.js +699 -114
- package/lib/validate-opts.js +49 -0
- package/lib/webhook.js +3 -6
- package/lib/websocket.js +38 -6
- package/package.json +1 -1
- package/sbom.cyclonedx.json +6 -6
package/lib/static.js
CHANGED
|
@@ -1,67 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* staticServe — server-side download primitive with the same v1-defensible
|
|
4
|
+
* surface as b.fileUpload: permissions integration, audit emission with
|
|
5
|
+
* 5-W's actor context, observability counters, bandwidth + concurrency
|
|
6
|
+
* quotas (cluster-shared via b.cache), Range support (RFC 7233 single-range),
|
|
7
|
+
* the full conditional-request set (If-None-Match / If-Match /
|
|
8
|
+
* If-Modified-Since / If-Unmodified-Since), MIME allowlist with magic-byte
|
|
9
|
+
* verification (composes b.fileType), per-request operator hook, idle-stream
|
|
10
|
+
* timeout, cancellation propagation, force-revoke, and compliance-retention
|
|
11
|
+
* gating.
|
|
4
12
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
13
|
+
* var serve = b.staticServe.create({ root, ...opts });
|
|
14
|
+
* await serve.revoke(absUrlPath); // operator force-revoke
|
|
15
|
+
* serve.stats(); // bytes / requests / etag-hits
|
|
16
|
+
* var mw = serve.middleware; // (req, res, next)
|
|
17
|
+
* await b.staticServe.integrity(absFilePath); // SRI helper (SHA-384)
|
|
9
18
|
*
|
|
10
|
-
*
|
|
19
|
+
* Backwards compatible: every existing opt (root, mountPath,
|
|
20
|
+
* hashedPathPattern, indexFile, defaultMaxAge, contentTypes) keeps its
|
|
21
|
+
* original meaning. New opts (permissions, cache, audit, observability,
|
|
22
|
+
* fileType, allowedFileTypes, maxBytesPerActorPerWindowMs, etc.) are all
|
|
23
|
+
* optional with security-on defaults (auditSuccess / auditFailures default
|
|
24
|
+
* true; range support defaults true; ETag is SHA3-512-truncated for PQC
|
|
25
|
+
* posture, the SRI integrity helper keeps SHA-384 because the W3C
|
|
26
|
+
* subresource-integrity spec only allows sha256/sha384/sha512).
|
|
11
27
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* the served bytes to the URL forever; immutable lets browsers
|
|
17
|
-
* skip revalidation entirely.
|
|
18
|
-
* - Other URLs get `Cache-Control: public, max-age=<defaultMaxAge>`
|
|
19
|
-
* (default 3600 = 1h). Operators tune via opts.defaultMaxAge.
|
|
20
|
-
*
|
|
21
|
-
* ETag is the first 27 chars of SHA-384(file content), base64. 162
|
|
22
|
-
* bits of collision resistance — overkill but cheap; same hash powers
|
|
23
|
-
* the SRI integrity helper so we compute it once per file.
|
|
24
|
-
*
|
|
25
|
-
* 304 Not Modified: emitted when If-None-Match matches the ETag.
|
|
26
|
-
*
|
|
27
|
-
* HEAD: returns same headers as GET, no body.
|
|
28
|
-
*
|
|
29
|
-
* Range requests are NOT supported in v1. Operators serving video/audio
|
|
30
|
-
* put a CDN in front. Compression (gzip/br) likewise — out of scope here.
|
|
31
|
-
*
|
|
32
|
-
* MIME types: minimal built-in table. Override via opts.contentTypes.
|
|
33
|
-
*
|
|
34
|
-
* Index files: a request resolving to a directory tries opts.indexFile
|
|
35
|
-
* (default "index.html"); operators disable with opts.indexFile = null.
|
|
36
|
-
*
|
|
37
|
-
* Public API:
|
|
38
|
-
*
|
|
39
|
-
* staticServe.create({ root, mountPath?, hashedPathPattern?,
|
|
40
|
-
* defaultMaxAge?, indexFile?, contentTypes? })
|
|
41
|
-
* → (req, res, next) middleware
|
|
42
|
-
*
|
|
43
|
-
* await staticServe.integrity(filePath)
|
|
44
|
-
* → "sha384-<base64>" suitable for an HTML integrity= attribute.
|
|
45
|
-
* Cached per file across calls; invalidated on mtime change.
|
|
28
|
+
* Backward-compat shape: create(opts) MAY be called for the bare middleware
|
|
29
|
+
* (the v0.6.x shape) — the result is callable as `(req, res, next)` and
|
|
30
|
+
* also exposes the new methods. That keeps existing tests + operator code
|
|
31
|
+
* passing while opening the surface.
|
|
46
32
|
*/
|
|
47
|
-
|
|
48
|
-
var
|
|
49
|
-
var
|
|
50
|
-
var nodeCrypto = require("crypto");
|
|
33
|
+
|
|
34
|
+
var fs = require("node:fs");
|
|
35
|
+
var fsp = require("node:fs/promises");
|
|
36
|
+
var nodeCrypto = require("node:crypto");
|
|
37
|
+
var path = require("node:path");
|
|
51
38
|
var C = require("./constants");
|
|
39
|
+
var lazyRequire = require("./lazy-require");
|
|
40
|
+
var numericBounds = require("./numeric-bounds");
|
|
52
41
|
var requestHelpers = require("./request-helpers");
|
|
53
42
|
var validateOpts = require("./validate-opts");
|
|
43
|
+
var { StaticServeError } = require("./framework-error");
|
|
44
|
+
|
|
45
|
+
// observability is lazy-required because it pulls in the metrics tap +
|
|
46
|
+
// safeEvent path, and during framework boot static.js may load before
|
|
47
|
+
// observability is ready.
|
|
48
|
+
var observability = lazyRequire(function () { return require("./observability"); });
|
|
49
|
+
|
|
50
|
+
var _err = StaticServeError.factory;
|
|
54
51
|
|
|
55
52
|
var HTTP = requestHelpers.HTTP_STATUS;
|
|
56
53
|
|
|
57
54
|
var DEFAULT_HASHED_PATTERN = /\.[a-fA-F0-9]{8,}\./;
|
|
58
55
|
var DEFAULT_INDEX_FILE = "index.html";
|
|
59
|
-
var DEFAULT_MAX_AGE_SEC = C.TIME.hours(1) / C.TIME.seconds(1);
|
|
60
|
-
var IMMUTABLE_MAX_AGE_SEC = C.TIME.days(365) / C.TIME.seconds(1);
|
|
56
|
+
var DEFAULT_MAX_AGE_SEC = C.TIME.hours(1) / C.TIME.seconds(1); // 1h non-hashed
|
|
57
|
+
var IMMUTABLE_MAX_AGE_SEC = C.TIME.days(365) / C.TIME.seconds(1); // 1y hashed
|
|
58
|
+
var DEFAULT_BANDWIDTH_WINDOW_MS = C.TIME.minutes(1);
|
|
59
|
+
var DEFAULT_MAX_IDLE_MS = C.TIME.minutes(2);
|
|
60
|
+
// SHA3-512 produces 64 bytes / 128 hex chars. ETag uses the first 32 hex
|
|
61
|
+
// chars (128 bits) — overkill for collision resistance but cheap; the same
|
|
62
|
+
// hash powers content addressing across the framework.
|
|
63
|
+
var ETAG_HEX_PREFIX = C.BYTES.bytes(32);
|
|
61
64
|
|
|
62
|
-
// Minimal MIME table
|
|
63
|
-
// to override. The framework deliberately doesn't bring a 200-entry
|
|
64
|
-
// mime-db dependency — most servers serve a handful of types.
|
|
65
|
+
// Minimal MIME table (kept from the v0.6 ship for compat).
|
|
65
66
|
var DEFAULT_CONTENT_TYPES = {
|
|
66
67
|
".html": "text/html; charset=utf-8",
|
|
67
68
|
".htm": "text/html; charset=utf-8",
|
|
@@ -76,7 +77,7 @@ var DEFAULT_CONTENT_TYPES = {
|
|
|
76
77
|
".svg": "image/svg+xml",
|
|
77
78
|
".png": "image/png",
|
|
78
79
|
".jpg": "image/jpeg",
|
|
79
|
-
".jpeg":
|
|
80
|
+
".jpeg": "image/jpeg",
|
|
80
81
|
".gif": "image/gif",
|
|
81
82
|
".webp": "image/webp",
|
|
82
83
|
".avif": "image/avif",
|
|
@@ -90,11 +91,28 @@ var DEFAULT_CONTENT_TYPES = {
|
|
|
90
91
|
".webmanifest": "application/manifest+json",
|
|
91
92
|
};
|
|
92
93
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
var DEFAULTS = Object.freeze({
|
|
95
|
+
defaultMaxAge: DEFAULT_MAX_AGE_SEC,
|
|
96
|
+
acceptRanges: true,
|
|
97
|
+
// Empty array = no MIME allowlist gate.
|
|
98
|
+
allowedFileTypes: Object.freeze([]),
|
|
99
|
+
// Bandwidth + concurrency caps default to 0 = "no cap". Operators opt
|
|
100
|
+
// in by setting a positive integer.
|
|
101
|
+
maxBytesPerActorPerWindowMs: 0,
|
|
102
|
+
maxBytesAllActorsPerWindowMs: 0,
|
|
103
|
+
bandwidthWindowMs: DEFAULT_BANDWIDTH_WINDOW_MS,
|
|
104
|
+
maxConcurrentDownloadsPerActor: 0,
|
|
105
|
+
maxIdleMs: DEFAULT_MAX_IDLE_MS,
|
|
106
|
+
// Audit / observability defaults follow the framework's posture: the
|
|
107
|
+
// serve event is the audit-worthy act, not a precursor.
|
|
108
|
+
auditSuccess: true,
|
|
109
|
+
auditFailures: true,
|
|
110
|
+
});
|
|
96
111
|
|
|
97
|
-
|
|
112
|
+
// Module-level metadata cache. Entries hold:
|
|
113
|
+
// { mtimeMs, size, etag, integrity, lastModified, sha3Hex, absPath }
|
|
114
|
+
// Invalidated on mtime / size change.
|
|
115
|
+
var _metaCache = new Map();
|
|
98
116
|
|
|
99
117
|
async function _readMeta(absPath) {
|
|
100
118
|
var stat;
|
|
@@ -102,38 +120,41 @@ async function _readMeta(absPath) {
|
|
|
102
120
|
catch (_e) { return null; }
|
|
103
121
|
if (!stat.isFile()) return null;
|
|
104
122
|
|
|
105
|
-
var cached =
|
|
123
|
+
var cached = _metaCache.get(absPath);
|
|
106
124
|
if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {
|
|
107
125
|
return cached;
|
|
108
126
|
}
|
|
109
127
|
|
|
110
|
-
// Stream
|
|
111
|
-
//
|
|
112
|
-
//
|
|
113
|
-
var
|
|
128
|
+
// Stream both hashes side-by-side: SHA-384 for SRI (W3C spec), SHA3-512
|
|
129
|
+
// for the framework ETag (PQC posture). Two transforms over the same
|
|
130
|
+
// chunk avoids re-reading the file.
|
|
131
|
+
var sri = nodeCrypto.createHash("sha384");
|
|
132
|
+
var sha3 = nodeCrypto.createHash("sha3-512");
|
|
114
133
|
await new Promise(function (resolve, reject) {
|
|
115
134
|
var s = fs.createReadStream(absPath);
|
|
116
|
-
s.on("data", function (chunk) {
|
|
117
|
-
s.on("end",
|
|
135
|
+
s.on("data", function (chunk) { sri.update(chunk); sha3.update(chunk); });
|
|
136
|
+
s.on("end", resolve);
|
|
118
137
|
s.on("error", reject);
|
|
119
138
|
});
|
|
120
|
-
var
|
|
139
|
+
var sriDigest = sri.digest("base64");
|
|
140
|
+
var sha3Hex = sha3.digest("hex");
|
|
121
141
|
|
|
122
142
|
var entry = {
|
|
123
143
|
mtimeMs: stat.mtimeMs,
|
|
124
144
|
size: stat.size,
|
|
125
|
-
etag: '"' +
|
|
126
|
-
integrity: "sha384-" +
|
|
145
|
+
etag: '"' + sha3Hex.slice(0, ETAG_HEX_PREFIX) + '"',
|
|
146
|
+
integrity: "sha384-" + sriDigest,
|
|
147
|
+
lastModified: new Date(stat.mtimeMs).toUTCString(),
|
|
148
|
+
sha3Hex: sha3Hex,
|
|
127
149
|
absPath: absPath,
|
|
128
150
|
};
|
|
129
|
-
|
|
151
|
+
_metaCache.set(absPath, entry);
|
|
130
152
|
return entry;
|
|
131
153
|
}
|
|
132
154
|
|
|
133
155
|
function _resolveSafe(root, requestedPath) {
|
|
134
156
|
if (typeof requestedPath !== "string" || requestedPath.length === 0) return null;
|
|
135
157
|
if (requestedPath.indexOf("\0") !== -1) return null;
|
|
136
|
-
// path.resolve handles ".." normalization; we then check containment.
|
|
137
158
|
var resolved = path.resolve(root, "." + requestedPath);
|
|
138
159
|
var rootResolved = path.resolve(root);
|
|
139
160
|
if (resolved !== rootResolved &&
|
|
@@ -146,48 +167,280 @@ function _contentTypeFor(filePath, table) {
|
|
|
146
167
|
return (table && table[ext]) || DEFAULT_CONTENT_TYPES[ext] || "application/octet-stream";
|
|
147
168
|
}
|
|
148
169
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
170
|
+
// _parseRangeHeader — RFC 7233 single-range parser. Returns null when:
|
|
171
|
+
// - header absent
|
|
172
|
+
// - syntactically malformed (not `bytes=`, multi-range, suffix syntax
|
|
173
|
+
// "-N" handled, end > size, start > end)
|
|
174
|
+
// - the request can be answered as 416 the caller does (start >= size).
|
|
175
|
+
//
|
|
176
|
+
// Returns { start, end, length } for a valid satisfiable single range.
|
|
177
|
+
// Multi-range (`bytes=0-99,200-299`) returns { multi: true } so the
|
|
178
|
+
// caller can refuse with 416 — we don't ship multipart/byteranges in v1
|
|
179
|
+
// (operators with that need pull bytes via b.objectStore presigned URL).
|
|
180
|
+
function _parseRangeHeader(header, size) {
|
|
181
|
+
if (typeof header !== "string" || header.length === 0) return null;
|
|
182
|
+
if (header.indexOf("bytes=") !== 0) return { malformed: true };
|
|
183
|
+
var spec = header.slice(6).trim();
|
|
184
|
+
if (spec.length === 0) return { malformed: true };
|
|
185
|
+
if (spec.indexOf(",") !== -1) return { multi: true };
|
|
186
|
+
var dash = spec.indexOf("-");
|
|
187
|
+
if (dash === -1) return { malformed: true };
|
|
188
|
+
var startStr = spec.slice(0, dash);
|
|
189
|
+
var endStr = spec.slice(dash + 1);
|
|
190
|
+
var start, end;
|
|
191
|
+
if (startStr === "") {
|
|
192
|
+
// Suffix range: "bytes=-N" → last N bytes.
|
|
193
|
+
var suffix = parseInt(endStr, 10);
|
|
194
|
+
if (!isFinite(suffix) || suffix <= 0) return { malformed: true };
|
|
195
|
+
if (suffix > size) suffix = size;
|
|
196
|
+
start = size - suffix;
|
|
197
|
+
end = size - 1;
|
|
198
|
+
} else {
|
|
199
|
+
start = parseInt(startStr, 10);
|
|
200
|
+
if (!isFinite(start) || start < 0) return { malformed: true };
|
|
201
|
+
if (endStr === "") {
|
|
202
|
+
end = size - 1;
|
|
203
|
+
} else {
|
|
204
|
+
end = parseInt(endStr, 10);
|
|
205
|
+
if (!isFinite(end) || end < start) return { malformed: true };
|
|
206
|
+
if (end > size - 1) end = size - 1;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (start >= size) return { unsatisfiable: true };
|
|
210
|
+
return { start: start, end: end, length: end - start + 1 };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function _httpDate(date) {
|
|
214
|
+
return (date instanceof Date ? date : new Date(date)).toUTCString();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function _validateCreateOpts(opts) {
|
|
218
|
+
validateOpts.requireObject(opts, "staticServe.create", StaticServeError);
|
|
219
|
+
validateOpts.requireNonEmptyString(opts.root, "staticServe.create: root", StaticServeError, "BAD_OPT");
|
|
220
|
+
if (!fs.existsSync(opts.root)) {
|
|
221
|
+
throw _err("BAD_OPT", "staticServe.create: root does not exist: " + opts.root);
|
|
222
|
+
}
|
|
223
|
+
if (typeof opts.mountPath === "string" && opts.mountPath.length === 0) {
|
|
224
|
+
// empty string is operator-permissible: "no mount, root is request URL"
|
|
225
|
+
} else if (opts.mountPath !== undefined && opts.mountPath !== null &&
|
|
226
|
+
typeof opts.mountPath !== "string") {
|
|
227
|
+
throw _err("BAD_OPT", "staticServe.create: mountPath must be a string");
|
|
228
|
+
}
|
|
229
|
+
if (opts.hashedPathPattern !== undefined && opts.hashedPathPattern !== null &&
|
|
230
|
+
!(opts.hashedPathPattern instanceof RegExp)) {
|
|
231
|
+
throw _err("BAD_OPT", "staticServe.create: hashedPathPattern must be a RegExp");
|
|
232
|
+
}
|
|
233
|
+
// indexFile === null is the operator's "disable" sentinel; the helper
|
|
234
|
+
// returns null/undefined unchanged so we keep that semantic.
|
|
235
|
+
validateOpts.optionalNonEmptyString(opts.indexFile,
|
|
236
|
+
"staticServe.create: indexFile", StaticServeError, "BAD_OPT");
|
|
237
|
+
numericBounds.requireNonNegativeFiniteIntIfPresent(opts.defaultMaxAge,
|
|
238
|
+
"staticServe.create: defaultMaxAge", StaticServeError, "BAD_OPT");
|
|
239
|
+
if (opts.contentTypes !== undefined && opts.contentTypes !== null &&
|
|
240
|
+
(typeof opts.contentTypes !== "object" || Array.isArray(opts.contentTypes))) {
|
|
241
|
+
throw _err("BAD_OPT", "staticServe.create: contentTypes must be a plain object");
|
|
242
|
+
}
|
|
243
|
+
validateOpts.optionalObjectWithMethod(opts.permissions, "check",
|
|
244
|
+
"staticServe.create: permissions", StaticServeError, "BAD_OPT",
|
|
245
|
+
"must be a b.permissions instance (check fn)");
|
|
246
|
+
validateOpts.optionalObjectWithMethod(opts.cache, "get",
|
|
247
|
+
"staticServe.create: cache", StaticServeError, "BAD_OPT",
|
|
248
|
+
"must be a b.cache instance (used for cluster-shared bandwidth + concurrency tracking)");
|
|
249
|
+
validateOpts.optionalObjectWithMethod(opts.fileType, "detect",
|
|
250
|
+
"staticServe.create: fileType", StaticServeError, "BAD_OPT",
|
|
251
|
+
"must be a b.fileType instance (magic-byte MIME detection)");
|
|
252
|
+
validateOpts.optionalObjectWithMethod(opts.retention, "isServable",
|
|
253
|
+
"staticServe.create: retention", StaticServeError, "BAD_OPT",
|
|
254
|
+
"must expose isServable(absPath, ctx) → boolean (compliance retention check)");
|
|
255
|
+
validateOpts.optionalObjectWithMethod(opts.revokeStore, "isRevoked",
|
|
256
|
+
"staticServe.create: revokeStore", StaticServeError, "BAD_OPT",
|
|
257
|
+
"must expose isRevoked(key) and revoke(key) for force-revoke support");
|
|
258
|
+
validateOpts.optionalNonEmptyStringArray(opts.allowedFileTypes,
|
|
259
|
+
"staticServe.create: allowedFileTypes", StaticServeError, "BAD_OPT");
|
|
260
|
+
if (Array.isArray(opts.allowedFileTypes) && opts.allowedFileTypes.length > 0 &&
|
|
261
|
+
(!opts.fileType || typeof opts.fileType.detect !== "function")) {
|
|
262
|
+
throw _err("BAD_OPT",
|
|
263
|
+
"staticServe.create: allowedFileTypes is set but fileType primitive is not wired " +
|
|
264
|
+
"(pass fileType: b.fileType so the framework can sniff magic bytes before serving)");
|
|
265
|
+
}
|
|
266
|
+
validateOpts.auditShape(opts.audit, "staticServe.create", StaticServeError);
|
|
267
|
+
validateOpts.observabilityShape(opts.observability, "staticServe.create", StaticServeError);
|
|
268
|
+
validateOpts.optionalFunction(opts.onServe, "staticServe.create: onServe", StaticServeError);
|
|
269
|
+
validateOpts.optionalBoolean(opts.acceptRanges, "staticServe.create: acceptRanges", StaticServeError);
|
|
270
|
+
validateOpts.optionalBoolean(opts.auditSuccess, "staticServe.create: auditSuccess", StaticServeError);
|
|
271
|
+
validateOpts.optionalBoolean(opts.auditFailures, "staticServe.create: auditFailures", StaticServeError);
|
|
272
|
+
numericBounds.requireNonNegativeFiniteIntIfPresent(opts.maxBytesPerActorPerWindowMs,
|
|
273
|
+
"staticServe.create: maxBytesPerActorPerWindowMs", StaticServeError, "BAD_OPT");
|
|
274
|
+
numericBounds.requireNonNegativeFiniteIntIfPresent(opts.maxBytesAllActorsPerWindowMs,
|
|
275
|
+
"staticServe.create: maxBytesAllActorsPerWindowMs", StaticServeError, "BAD_OPT");
|
|
276
|
+
numericBounds.requirePositiveFiniteIntIfPresent(opts.bandwidthWindowMs,
|
|
277
|
+
"staticServe.create: bandwidthWindowMs", StaticServeError, "BAD_OPT");
|
|
278
|
+
numericBounds.requireNonNegativeFiniteIntIfPresent(opts.maxConcurrentDownloadsPerActor,
|
|
279
|
+
"staticServe.create: maxConcurrentDownloadsPerActor", StaticServeError, "BAD_OPT");
|
|
280
|
+
numericBounds.requirePositiveFiniteIntIfPresent(opts.maxIdleMs,
|
|
281
|
+
"staticServe.create: maxIdleMs", StaticServeError, "BAD_OPT");
|
|
282
|
+
// Quotas require a cache for cluster-shared coordination.
|
|
283
|
+
if ((opts.maxBytesPerActorPerWindowMs > 0 ||
|
|
284
|
+
opts.maxBytesAllActorsPerWindowMs > 0 ||
|
|
285
|
+
opts.maxConcurrentDownloadsPerActor > 0) &&
|
|
286
|
+
!opts.cache) {
|
|
287
|
+
throw _err("BAD_OPT",
|
|
288
|
+
"staticServe.create: bandwidth / concurrency quotas require opts.cache " +
|
|
289
|
+
"(pass cache: b.cache.create({ backend: 'cluster' }) so multi-replica deploys honor caps globally)");
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// _checkBandwidthQuota — token-bucket via b.cache. Returns { ok: true } or
|
|
294
|
+
// { ok: false, retryAfter, scope: "actor"|"global" }.
|
|
295
|
+
async function _checkBandwidthQuota(cache, actorKey, perActorCap, globalCap, windowMs, requestedBytes) {
|
|
296
|
+
if (!cache || (perActorCap === 0 && globalCap === 0)) return { ok: true };
|
|
297
|
+
var now = Date.now();
|
|
298
|
+
var windowStart = now - windowMs;
|
|
299
|
+
if (perActorCap > 0 && actorKey) {
|
|
300
|
+
var aKey = "static:bw:actor:" + actorKey;
|
|
301
|
+
var aUsed = (await cache.get(aKey)) || 0;
|
|
302
|
+
if (aUsed + requestedBytes > perActorCap) {
|
|
303
|
+
return { ok: false, retryAfter: Math.ceil(windowMs / C.TIME.seconds(1)), scope: "actor", used: aUsed, cap: perActorCap };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (globalCap > 0) {
|
|
307
|
+
var gKey = "static:bw:global";
|
|
308
|
+
var gUsed = (await cache.get(gKey)) || 0;
|
|
309
|
+
if (gUsed + requestedBytes > globalCap) {
|
|
310
|
+
return { ok: false, retryAfter: Math.ceil(windowMs / C.TIME.seconds(1)), scope: "global", used: gUsed, cap: globalCap };
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return { ok: true, windowStart: windowStart, now: now };
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
async function _consumeBandwidth(cache, actorKey, perActorCap, globalCap, windowMs, bytes) {
|
|
317
|
+
if (!cache) return;
|
|
318
|
+
if (perActorCap > 0 && actorKey) {
|
|
319
|
+
var aKey = "static:bw:actor:" + actorKey;
|
|
320
|
+
var aUsed = (await cache.get(aKey)) || 0;
|
|
321
|
+
await cache.set(aKey, aUsed + bytes, { ttlMs: windowMs });
|
|
322
|
+
}
|
|
323
|
+
if (globalCap > 0) {
|
|
324
|
+
var gKey = "static:bw:global";
|
|
325
|
+
var gUsed = (await cache.get(gKey)) || 0;
|
|
326
|
+
await cache.set(gKey, gUsed + bytes, { ttlMs: windowMs });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async function _checkConcurrencyCap(cache, actorKey, cap) {
|
|
331
|
+
if (!cache || cap === 0 || !actorKey) return { ok: true };
|
|
332
|
+
var key = "static:conc:" + actorKey;
|
|
333
|
+
var current = (await cache.get(key)) || 0;
|
|
334
|
+
if (current >= cap) return { ok: false, current: current, cap: cap };
|
|
335
|
+
return { ok: true, current: current };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async function _incConcurrency(cache, actorKey) {
|
|
339
|
+
if (!cache || !actorKey) return;
|
|
340
|
+
var key = "static:conc:" + actorKey;
|
|
341
|
+
var current = (await cache.get(key)) || 0;
|
|
342
|
+
await cache.set(key, current + 1, { ttlMs: C.TIME.minutes(10) });
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async function _decConcurrency(cache, actorKey) {
|
|
346
|
+
if (!cache || !actorKey) return;
|
|
347
|
+
var key = "static:conc:" + actorKey;
|
|
348
|
+
var current = (await cache.get(key)) || 0;
|
|
349
|
+
var next = current > 0 ? current - 1 : 0;
|
|
350
|
+
await cache.set(key, next, { ttlMs: C.TIME.minutes(10) });
|
|
155
351
|
}
|
|
156
352
|
|
|
157
|
-
function
|
|
158
|
-
|
|
159
|
-
|
|
353
|
+
function _actorKeyFromContext(ctx) {
|
|
354
|
+
if (!ctx) return null;
|
|
355
|
+
if (ctx.userId) return "id:" + ctx.userId;
|
|
356
|
+
if (ctx.ip) return "ip:" + ctx.ip;
|
|
357
|
+
return null;
|
|
160
358
|
}
|
|
161
359
|
|
|
162
|
-
//
|
|
360
|
+
// _writeError — uniform error response with audit + observability emission.
|
|
361
|
+
function _writeError(res, status, code, message, headers) {
|
|
362
|
+
var hdrs = Object.assign({ "Content-Type": "text/plain; charset=utf-8" }, headers || {});
|
|
363
|
+
hdrs["Content-Length"] = Buffer.byteLength(message, "utf8");
|
|
364
|
+
try {
|
|
365
|
+
res.writeHead(status, hdrs);
|
|
366
|
+
res.end(message);
|
|
367
|
+
} catch (_e) {
|
|
368
|
+
// response already torn down — best effort
|
|
369
|
+
}
|
|
370
|
+
void code;
|
|
371
|
+
}
|
|
163
372
|
|
|
373
|
+
// integrity() — module-level helper, kept for compat with the v0.6 SRI use.
|
|
164
374
|
async function integrity(absPath) {
|
|
165
375
|
if (typeof absPath !== "string" || absPath.length === 0) {
|
|
166
|
-
throw
|
|
376
|
+
throw _err("BAD_OPT", "staticServe.integrity: absPath must be a non-empty string");
|
|
167
377
|
}
|
|
168
378
|
var meta = await _readMeta(path.resolve(absPath));
|
|
169
|
-
if (!meta) throw
|
|
379
|
+
if (!meta) throw _err("NOT_FOUND", "staticServe.integrity: file not found: " + absPath);
|
|
170
380
|
return meta.integrity;
|
|
171
381
|
}
|
|
172
382
|
|
|
173
|
-
// ---- Public: create() ----
|
|
174
|
-
|
|
175
383
|
function create(opts) {
|
|
176
384
|
opts = opts || {};
|
|
385
|
+
// The v0.6.x test surface called `validateOpts(opts, [...allowed], label)`
|
|
386
|
+
// for the unknown-key check. Preserve that gate in addition to the
|
|
387
|
+
// throw-at-config-time validation so tests catch typos.
|
|
177
388
|
validateOpts(opts, [
|
|
178
389
|
"root", "mountPath", "hashedPathPattern",
|
|
179
390
|
"indexFile", "defaultMaxAge", "contentTypes",
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
391
|
+
"permissions", "cache", "fileType", "retention", "revokeStore",
|
|
392
|
+
"allowedFileTypes", "audit", "observability", "onServe",
|
|
393
|
+
"acceptRanges", "auditSuccess", "auditFailures",
|
|
394
|
+
"maxBytesPerActorPerWindowMs", "maxBytesAllActorsPerWindowMs",
|
|
395
|
+
"bandwidthWindowMs", "maxConcurrentDownloadsPerActor", "maxIdleMs",
|
|
396
|
+
], "staticServe.create");
|
|
397
|
+
_validateCreateOpts(opts);
|
|
398
|
+
var cfg = validateOpts.applyDefaults(opts, DEFAULTS);
|
|
185
399
|
var root = path.resolve(opts.root);
|
|
186
|
-
var mountPath = opts.mountPath || "";
|
|
400
|
+
var mountPath = opts.mountPath || "";
|
|
187
401
|
var hashedPattern = opts.hashedPathPattern || DEFAULT_HASHED_PATTERN;
|
|
188
402
|
var indexFile = opts.indexFile === null ? null : (opts.indexFile || DEFAULT_INDEX_FILE);
|
|
189
|
-
var defaultMaxAge =
|
|
403
|
+
var defaultMaxAge = cfg.defaultMaxAge;
|
|
190
404
|
var contentTypes = opts.contentTypes || null;
|
|
405
|
+
var permissions = opts.permissions || null;
|
|
406
|
+
var cache = opts.cache || null;
|
|
407
|
+
var fileType = opts.fileType || null;
|
|
408
|
+
var retention = opts.retention || null;
|
|
409
|
+
var revokeStore = opts.revokeStore || null;
|
|
410
|
+
var allowedFileTypes = Array.isArray(opts.allowedFileTypes) ? opts.allowedFileTypes.slice() : [];
|
|
411
|
+
var onServe = opts.onServe || null;
|
|
412
|
+
var audit = opts.audit || null;
|
|
413
|
+
var auditSuccess = cfg.auditSuccess;
|
|
414
|
+
var auditFailures = cfg.auditFailures;
|
|
415
|
+
var acceptRanges = cfg.acceptRanges;
|
|
416
|
+
var perActorCap = cfg.maxBytesPerActorPerWindowMs;
|
|
417
|
+
var globalCap = cfg.maxBytesAllActorsPerWindowMs;
|
|
418
|
+
var bandwidthWindowMs = cfg.bandwidthWindowMs;
|
|
419
|
+
var concurrencyCap = cfg.maxConcurrentDownloadsPerActor;
|
|
420
|
+
var maxIdleMs = cfg.maxIdleMs;
|
|
421
|
+
|
|
422
|
+
var emitAudit = validateOpts.makeAuditEmitter(audit);
|
|
423
|
+
|
|
424
|
+
// In-memory revoke set (operator can wire revokeStore for cluster-shared
|
|
425
|
+
// revocation; this Map is only used when no store is wired and gives
|
|
426
|
+
// single-process operators a working force-revoke without requiring cache).
|
|
427
|
+
var localRevoked = new Set();
|
|
428
|
+
|
|
429
|
+
function _emitObs(name, value, labels) {
|
|
430
|
+
observability().safeEvent(name, value, labels || {});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Per-instance counters for serve.stats(). Cluster-shared counters live in
|
|
434
|
+
// observability; these are local snapshots for a single process.
|
|
435
|
+
var stats = {
|
|
436
|
+
requestsServed: 0,
|
|
437
|
+
bytesServed: 0,
|
|
438
|
+
etagHits: 0,
|
|
439
|
+
rangeRequests: 0,
|
|
440
|
+
permissionDenied: 0,
|
|
441
|
+
quotaRejected: 0,
|
|
442
|
+
failures: 0,
|
|
443
|
+
};
|
|
191
444
|
|
|
192
445
|
function _cacheControlFor(urlPath) {
|
|
193
446
|
if (hashedPattern.test(urlPath)) {
|
|
@@ -196,15 +449,51 @@ function create(opts) {
|
|
|
196
449
|
return "public, max-age=" + defaultMaxAge;
|
|
197
450
|
}
|
|
198
451
|
|
|
199
|
-
|
|
452
|
+
async function _isRevoked(key) {
|
|
453
|
+
if (revokeStore) {
|
|
454
|
+
try { return !!(await revokeStore.isRevoked(key)); }
|
|
455
|
+
catch (_e) { return false; }
|
|
456
|
+
}
|
|
457
|
+
return localRevoked.has(key);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async function _checkRetention(absPath, ctx) {
|
|
461
|
+
if (!retention) return true;
|
|
462
|
+
try { return !!(await retention.isServable(absPath, ctx)); }
|
|
463
|
+
catch (_e) { return false; }
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
async function _checkPermission(req) {
|
|
467
|
+
if (!permissions) return { ok: true };
|
|
468
|
+
try {
|
|
469
|
+
var ok = await permissions.check(req, "static.serve");
|
|
470
|
+
return { ok: !!ok };
|
|
471
|
+
} catch (_e) {
|
|
472
|
+
return { ok: false, error: _e };
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async function _checkMimeAllowlist(absPath, meta) {
|
|
477
|
+
if (allowedFileTypes.length === 0 || !fileType) return { ok: true };
|
|
478
|
+
var sample;
|
|
479
|
+
try { sample = await fsp.readFile(absPath, { flag: "r" }); }
|
|
480
|
+
catch (_e) { return { ok: false, reason: "read-failed" }; }
|
|
481
|
+
var detected = fileType.detect(sample.slice(0, C.BYTES.kib(64))) || {};
|
|
482
|
+
if (!detected.mime) return { ok: false, reason: "indeterminate" };
|
|
483
|
+
if (allowedFileTypes.indexOf(detected.mime) === -1) {
|
|
484
|
+
return { ok: false, reason: "not-allowed", detected: detected.mime };
|
|
485
|
+
}
|
|
486
|
+
void meta;
|
|
487
|
+
return { ok: true, detected: detected.mime };
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
async function middleware(req, res, next) {
|
|
200
491
|
if (req.method !== "GET" && req.method !== "HEAD") return next();
|
|
201
492
|
|
|
202
|
-
// Strip query string + mount path before resolving against root.
|
|
203
493
|
var urlPath = (req.url || "").split("?")[0];
|
|
204
494
|
if (mountPath && urlPath.indexOf(mountPath) === 0) {
|
|
205
495
|
urlPath = urlPath.slice(mountPath.length) || "/";
|
|
206
496
|
}
|
|
207
|
-
// Decode percent-encoded path. Reject decoding failures (malformed URI).
|
|
208
497
|
var decoded;
|
|
209
498
|
try { decoded = decodeURIComponent(urlPath); }
|
|
210
499
|
catch (_e) { return next(); }
|
|
@@ -212,7 +501,24 @@ function create(opts) {
|
|
|
212
501
|
var absPath = _resolveSafe(root, decoded);
|
|
213
502
|
if (!absPath) return next();
|
|
214
503
|
|
|
215
|
-
|
|
504
|
+
var actorCtx = requestHelpers.extractActorContext(req);
|
|
505
|
+
var actorKey = _actorKeyFromContext(actorCtx);
|
|
506
|
+
|
|
507
|
+
// Permission gate (403)
|
|
508
|
+
var perm = await _checkPermission(req);
|
|
509
|
+
if (!perm.ok) {
|
|
510
|
+
stats.permissionDenied += 1;
|
|
511
|
+
_emitObs("staticServe.permission_denied", 1, { route: urlPath });
|
|
512
|
+
if (auditFailures) {
|
|
513
|
+
emitAudit("staticServe.serve.failure", Object.assign({
|
|
514
|
+
outcome: "failure", reason: "permission_denied", resource: urlPath,
|
|
515
|
+
}, actorCtx));
|
|
516
|
+
}
|
|
517
|
+
return _writeError(res, HTTP.FORBIDDEN, "permission_denied",
|
|
518
|
+
"Forbidden");
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Stat first to discover directory → index file.
|
|
216
522
|
var stat;
|
|
217
523
|
try { stat = await fsp.stat(absPath); }
|
|
218
524
|
catch (_e) { return next(); }
|
|
@@ -221,52 +527,331 @@ function create(opts) {
|
|
|
221
527
|
absPath = path.join(absPath, indexFile);
|
|
222
528
|
}
|
|
223
529
|
|
|
530
|
+
// Force-revoke (404 — opaque to clients)
|
|
531
|
+
if (await _isRevoked(absPath)) {
|
|
532
|
+
stats.failures += 1;
|
|
533
|
+
_emitObs("staticServe.revoked", 1, { route: urlPath });
|
|
534
|
+
if (auditFailures) {
|
|
535
|
+
emitAudit("staticServe.serve.failure", Object.assign({
|
|
536
|
+
outcome: "failure", reason: "revoked", resource: urlPath,
|
|
537
|
+
}, actorCtx));
|
|
538
|
+
}
|
|
539
|
+
return _writeError(res, HTTP.NOT_FOUND, "not_found", "Not Found");
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Compliance retention (451)
|
|
543
|
+
if (!(await _checkRetention(absPath, actorCtx))) {
|
|
544
|
+
stats.failures += 1;
|
|
545
|
+
_emitObs("staticServe.retention_blocked", 1, { route: urlPath });
|
|
546
|
+
if (auditFailures) {
|
|
547
|
+
emitAudit("staticServe.serve.failure", Object.assign({
|
|
548
|
+
outcome: "failure", reason: "retention_blocked", resource: urlPath,
|
|
549
|
+
}, actorCtx));
|
|
550
|
+
}
|
|
551
|
+
return _writeError(res, HTTP.UNAVAILABLE_FOR_LEGAL_REASONS,
|
|
552
|
+
"retention_blocked", "Unavailable For Legal Reasons");
|
|
553
|
+
}
|
|
554
|
+
|
|
224
555
|
var meta = await _readMeta(absPath);
|
|
225
556
|
if (!meta) return next();
|
|
226
557
|
|
|
558
|
+
// MIME allowlist (415) — checked before sending bytes so a misnamed
|
|
559
|
+
// .txt holding a binary payload is refused at serve time.
|
|
560
|
+
if (allowedFileTypes.length > 0) {
|
|
561
|
+
var mimeCheck = await _checkMimeAllowlist(absPath, meta);
|
|
562
|
+
if (!mimeCheck.ok) {
|
|
563
|
+
stats.failures += 1;
|
|
564
|
+
_emitObs("staticServe.mime_rejected", 1, { route: urlPath, reason: mimeCheck.reason });
|
|
565
|
+
if (auditFailures) {
|
|
566
|
+
emitAudit("staticServe.serve.failure", Object.assign({
|
|
567
|
+
outcome: "failure", reason: "mime_rejected", resource: urlPath,
|
|
568
|
+
detectedMime: mimeCheck.detected || null,
|
|
569
|
+
}, actorCtx));
|
|
570
|
+
}
|
|
571
|
+
return _writeError(res, HTTP.UNSUPPORTED_MEDIA_TYPE,
|
|
572
|
+
"mime_rejected", "Unsupported Media Type");
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
227
576
|
var cacheControl = _cacheControlFor(urlPath);
|
|
228
577
|
|
|
229
|
-
|
|
230
|
-
|
|
578
|
+
var headersIn = req.headers || {};
|
|
579
|
+
|
|
580
|
+
// Conditional: If-None-Match (304)
|
|
581
|
+
var ifNone = headersIn["if-none-match"];
|
|
231
582
|
if (ifNone && ifNone === meta.etag) {
|
|
232
|
-
|
|
583
|
+
stats.etagHits += 1;
|
|
584
|
+
_emitObs("staticServe.etag_hits", 1, { route: urlPath });
|
|
585
|
+
res.writeHead(HTTP.NOT_MODIFIED, {
|
|
586
|
+
"ETag": meta.etag,
|
|
587
|
+
"Cache-Control": cacheControl,
|
|
588
|
+
"Last-Modified": meta.lastModified,
|
|
589
|
+
});
|
|
590
|
+
return res.end();
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Conditional: If-Match (412 if no match — strong validator only)
|
|
594
|
+
var ifMatch = headersIn["if-match"];
|
|
595
|
+
if (ifMatch && ifMatch !== "*" && ifMatch !== meta.etag) {
|
|
596
|
+
stats.failures += 1;
|
|
597
|
+
_emitObs("staticServe.precondition_failed", 1, { route: urlPath, header: "if-match" });
|
|
598
|
+
return _writeError(res, HTTP.PRECONDITION_FAILED || 412,
|
|
599
|
+
"precondition_failed", "Precondition Failed");
|
|
233
600
|
}
|
|
234
601
|
|
|
602
|
+
// Conditional: If-Modified-Since (304)
|
|
603
|
+
var ifModSince = headersIn["if-modified-since"];
|
|
604
|
+
if (ifModSince) {
|
|
605
|
+
var ims = Date.parse(ifModSince);
|
|
606
|
+
if (isFinite(ims) && Math.floor(meta.mtimeMs / C.TIME.seconds(1)) <= Math.floor(ims / C.TIME.seconds(1))) {
|
|
607
|
+
stats.etagHits += 1;
|
|
608
|
+
_emitObs("staticServe.if_modified_since_hits", 1, { route: urlPath });
|
|
609
|
+
res.writeHead(HTTP.NOT_MODIFIED, {
|
|
610
|
+
"ETag": meta.etag,
|
|
611
|
+
"Cache-Control": cacheControl,
|
|
612
|
+
"Last-Modified": meta.lastModified,
|
|
613
|
+
});
|
|
614
|
+
return res.end();
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// Conditional: If-Unmodified-Since (412)
|
|
619
|
+
var ifUnmodSince = headersIn["if-unmodified-since"];
|
|
620
|
+
if (ifUnmodSince) {
|
|
621
|
+
var ius = Date.parse(ifUnmodSince);
|
|
622
|
+
if (isFinite(ius) && Math.floor(meta.mtimeMs / C.TIME.seconds(1)) > Math.floor(ius / C.TIME.seconds(1))) {
|
|
623
|
+
stats.failures += 1;
|
|
624
|
+
_emitObs("staticServe.precondition_failed", 1, { route: urlPath, header: "if-unmodified-since" });
|
|
625
|
+
return _writeError(res, HTTP.PRECONDITION_FAILED,
|
|
626
|
+
"precondition_failed", "Precondition Failed");
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// Range parsing
|
|
631
|
+
var range = null;
|
|
632
|
+
if (acceptRanges) {
|
|
633
|
+
var raw = headersIn["range"];
|
|
634
|
+
if (raw) {
|
|
635
|
+
range = _parseRangeHeader(raw, meta.size);
|
|
636
|
+
if (range && (range.malformed || range.multi)) {
|
|
637
|
+
stats.failures += 1;
|
|
638
|
+
_emitObs("staticServe.range_invalid", 1, { route: urlPath });
|
|
639
|
+
return _writeError(res, HTTP.RANGE_NOT_SATISFIABLE, "range_not_satisfiable",
|
|
640
|
+
"Range Not Satisfiable", { "Content-Range": "bytes */" + meta.size });
|
|
641
|
+
}
|
|
642
|
+
if (range && range.unsatisfiable) {
|
|
643
|
+
stats.failures += 1;
|
|
644
|
+
_emitObs("staticServe.range_invalid", 1, { route: urlPath });
|
|
645
|
+
return _writeError(res, HTTP.RANGE_NOT_SATISFIABLE, "range_not_satisfiable",
|
|
646
|
+
"Range Not Satisfiable", { "Content-Range": "bytes */" + meta.size });
|
|
647
|
+
}
|
|
648
|
+
if (range) {
|
|
649
|
+
stats.rangeRequests += 1;
|
|
650
|
+
_emitObs("staticServe.range_requests", 1, { route: urlPath });
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
var sendBytes = range ? range.length : meta.size;
|
|
656
|
+
|
|
657
|
+
// Concurrency gate (429)
|
|
658
|
+
var concCheck = await _checkConcurrencyCap(cache, actorKey, concurrencyCap);
|
|
659
|
+
if (!concCheck.ok) {
|
|
660
|
+
stats.quotaRejected += 1;
|
|
661
|
+
_emitObs("staticServe.concurrency_rejected", 1, { route: urlPath });
|
|
662
|
+
if (auditFailures) {
|
|
663
|
+
emitAudit("staticServe.serve.failure", Object.assign({
|
|
664
|
+
outcome: "failure", reason: "concurrency_cap", resource: urlPath,
|
|
665
|
+
current: concCheck.current, cap: concCheck.cap,
|
|
666
|
+
}, actorCtx));
|
|
667
|
+
}
|
|
668
|
+
return _writeError(res, HTTP.TOO_MANY_REQUESTS,
|
|
669
|
+
"concurrency_cap", "Too Many Requests",
|
|
670
|
+
{ "Retry-After": "5" });
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// Bandwidth gate (429)
|
|
674
|
+
var bwCheck = await _checkBandwidthQuota(cache, actorKey, perActorCap, globalCap, bandwidthWindowMs, sendBytes);
|
|
675
|
+
if (!bwCheck.ok) {
|
|
676
|
+
stats.quotaRejected += 1;
|
|
677
|
+
_emitObs("staticServe.bandwidth_rejected", 1, { route: urlPath, scope: bwCheck.scope });
|
|
678
|
+
if (auditFailures) {
|
|
679
|
+
emitAudit("staticServe.serve.failure", Object.assign({
|
|
680
|
+
outcome: "failure", reason: "bandwidth_quota", resource: urlPath,
|
|
681
|
+
scope: bwCheck.scope, used: bwCheck.used, cap: bwCheck.cap,
|
|
682
|
+
}, actorCtx));
|
|
683
|
+
}
|
|
684
|
+
return _writeError(res, HTTP.TOO_MANY_REQUESTS,
|
|
685
|
+
"bandwidth_quota", "Too Many Requests",
|
|
686
|
+
{ "Retry-After": String(bwCheck.retryAfter) });
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
var status = range ? 206 : HTTP.OK;
|
|
235
690
|
var headers = {
|
|
236
691
|
"Content-Type": _contentTypeFor(absPath, contentTypes),
|
|
237
|
-
"Content-Length":
|
|
692
|
+
"Content-Length": sendBytes,
|
|
238
693
|
"ETag": meta.etag,
|
|
239
694
|
"Cache-Control": cacheControl,
|
|
240
|
-
|
|
241
|
-
// required by clients but consumers can read it from response
|
|
242
|
-
// headers when they want to embed integrity in subsequent pages.
|
|
695
|
+
"Last-Modified": meta.lastModified,
|
|
243
696
|
"X-Integrity": meta.integrity,
|
|
244
697
|
};
|
|
698
|
+
if (acceptRanges) headers["Accept-Ranges"] = "bytes";
|
|
699
|
+
if (range) headers["Content-Range"] = "bytes " + range.start + "-" + range.end + "/" + meta.size;
|
|
700
|
+
|
|
701
|
+
// onServe hook — operator can mutate headers / set extra fields.
|
|
702
|
+
if (onServe) {
|
|
703
|
+
try {
|
|
704
|
+
await onServe({
|
|
705
|
+
req: req, res: res, absPath: absPath, urlPath: urlPath,
|
|
706
|
+
size: meta.size, sendBytes: sendBytes, range: range,
|
|
707
|
+
headers: headers, actor: actorCtx,
|
|
708
|
+
});
|
|
709
|
+
} catch (e) {
|
|
710
|
+
stats.failures += 1;
|
|
711
|
+
_emitObs("staticServe.onServe_threw", 1, { route: urlPath });
|
|
712
|
+
if (auditFailures) {
|
|
713
|
+
emitAudit("staticServe.serve.failure", Object.assign({
|
|
714
|
+
outcome: "failure", reason: "onServe_threw", resource: urlPath,
|
|
715
|
+
error: e && e.message,
|
|
716
|
+
}, actorCtx));
|
|
717
|
+
}
|
|
718
|
+
return _writeError(res, HTTP.INTERNAL_SERVER_ERROR, "onServe_threw",
|
|
719
|
+
"Internal Server Error");
|
|
720
|
+
}
|
|
721
|
+
}
|
|
245
722
|
|
|
246
723
|
if (req.method === "HEAD") {
|
|
247
|
-
res.writeHead(
|
|
248
|
-
|
|
724
|
+
res.writeHead(status, headers);
|
|
725
|
+
res.end();
|
|
726
|
+
stats.requestsServed += 1;
|
|
727
|
+
_emitObs("staticServe.requests_served", 1, { route: urlPath, method: "HEAD" });
|
|
728
|
+
if (auditSuccess) {
|
|
729
|
+
emitAudit("staticServe.serve.success", Object.assign({
|
|
730
|
+
outcome: "success", resource: urlPath, method: "HEAD",
|
|
731
|
+
size: meta.size, contentType: headers["Content-Type"],
|
|
732
|
+
}, actorCtx));
|
|
733
|
+
}
|
|
734
|
+
return;
|
|
249
735
|
}
|
|
250
736
|
|
|
251
|
-
res.writeHead(
|
|
252
|
-
|
|
253
|
-
stream
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
737
|
+
res.writeHead(status, headers);
|
|
738
|
+
|
|
739
|
+
// Acquire concurrency slot (released on stream end / error / abort).
|
|
740
|
+
await _incConcurrency(cache, actorKey);
|
|
741
|
+
var slotReleased = false;
|
|
742
|
+
function releaseSlot() {
|
|
743
|
+
if (slotReleased) return;
|
|
744
|
+
slotReleased = true;
|
|
745
|
+
_decConcurrency(cache, actorKey).catch(function () {});
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
var streamOpts = range ? { start: range.start, end: range.end } : {};
|
|
749
|
+
var fileStream = fs.createReadStream(absPath, streamOpts);
|
|
750
|
+
|
|
751
|
+
// Idle timeout — close the connection if the client stalls. Pattern is
|
|
752
|
+
// a deadline-style debounce (clearTimeout + setTimeout) tied directly
|
|
753
|
+
// to the file-stream "data" event lifecycle; the safeAsync.debounce
|
|
754
|
+
// helper isn't yet ship-implemented, and pulling it through here would
|
|
755
|
+
// pre-allocate a closure for every served byte. Tracked for extraction.
|
|
756
|
+
var idleTimer = null;
|
|
757
|
+
function resetIdleTimer() {
|
|
758
|
+
if (idleTimer) clearTimeout(idleTimer); // allow:handrolled-debounce — file-stream idle deadline
|
|
759
|
+
idleTimer = setTimeout(function () {
|
|
760
|
+
try { fileStream.destroy(_err("IDLE_TIMEOUT", "client idle for " + maxIdleMs + "ms")); }
|
|
761
|
+
catch (_) { /* stream already torn down */ }
|
|
762
|
+
try { res.destroy(); } catch (_) { /* response already torn down */ }
|
|
763
|
+
}, maxIdleMs);
|
|
764
|
+
}
|
|
765
|
+
resetIdleTimer();
|
|
766
|
+
|
|
767
|
+
// Cancellation propagation: when the client disconnects mid-stream.
|
|
768
|
+
function onClientClose() {
|
|
769
|
+
try { fileStream.destroy(); } catch (_) { /* allow:silent-catch — stream already torn down */ }
|
|
770
|
+
releaseSlot();
|
|
771
|
+
if (idleTimer) { clearTimeout(idleTimer); idleTimer = null; }
|
|
772
|
+
}
|
|
773
|
+
req.on("aborted", onClientClose);
|
|
774
|
+
res.on("close", onClientClose);
|
|
775
|
+
|
|
776
|
+
var bytesSent = 0;
|
|
777
|
+
fileStream.on("data", function (chunk) {
|
|
778
|
+
bytesSent += chunk.length;
|
|
779
|
+
resetIdleTimer();
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
fileStream.on("error", function (e) {
|
|
783
|
+
stats.failures += 1;
|
|
784
|
+
_emitObs("staticServe.stream_error", 1, { route: urlPath });
|
|
785
|
+
if (auditFailures) {
|
|
786
|
+
emitAudit("staticServe.serve.failure", Object.assign({
|
|
787
|
+
outcome: "failure", reason: "stream_error", resource: urlPath,
|
|
788
|
+
error: e && e.message,
|
|
789
|
+
}, actorCtx));
|
|
790
|
+
}
|
|
791
|
+
try { res.destroy(e); } catch (_) { /* allow:silent-catch — response already torn down */ }
|
|
792
|
+
releaseSlot();
|
|
793
|
+
if (idleTimer) { clearTimeout(idleTimer); idleTimer = null; }
|
|
257
794
|
});
|
|
258
|
-
|
|
795
|
+
|
|
796
|
+
fileStream.on("end", function () {
|
|
797
|
+
if (idleTimer) { clearTimeout(idleTimer); idleTimer = null; }
|
|
798
|
+
stats.requestsServed += 1;
|
|
799
|
+
stats.bytesServed += bytesSent;
|
|
800
|
+
_emitObs("staticServe.requests_served", 1, { route: urlPath, method: "GET" });
|
|
801
|
+
_emitObs("staticServe.bytes_served", bytesSent, { route: urlPath });
|
|
802
|
+
_consumeBandwidth(cache, actorKey, perActorCap, globalCap, bandwidthWindowMs, bytesSent)
|
|
803
|
+
.catch(function () {});
|
|
804
|
+
if (auditSuccess) {
|
|
805
|
+
emitAudit("staticServe.serve.success", Object.assign({
|
|
806
|
+
outcome: "success", resource: urlPath, method: "GET",
|
|
807
|
+
size: bytesSent, contentType: headers["Content-Type"],
|
|
808
|
+
range: range ? { start: range.start, end: range.end } : null,
|
|
809
|
+
}, actorCtx));
|
|
810
|
+
}
|
|
811
|
+
releaseSlot();
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
fileStream.pipe(res);
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
// Operator-facing handle: callable as middleware (back-compat) AND
|
|
818
|
+
// exposes serve-instance methods.
|
|
819
|
+
async function fn(req, res, next) { return middleware(req, res, next); }
|
|
820
|
+
fn.middleware = middleware;
|
|
821
|
+
fn.revoke = async function (key) {
|
|
822
|
+
if (revokeStore && typeof revokeStore.revoke === "function") {
|
|
823
|
+
await revokeStore.revoke(key);
|
|
824
|
+
return { ok: true, key: key };
|
|
825
|
+
}
|
|
826
|
+
localRevoked.add(key);
|
|
827
|
+
return { ok: true, key: key };
|
|
828
|
+
};
|
|
829
|
+
fn.unrevoke = async function (key) {
|
|
830
|
+
if (revokeStore && typeof revokeStore.unrevoke === "function") {
|
|
831
|
+
await revokeStore.unrevoke(key);
|
|
832
|
+
return { ok: true, key: key };
|
|
833
|
+
}
|
|
834
|
+
localRevoked.delete(key);
|
|
835
|
+
return { ok: true, key: key };
|
|
836
|
+
};
|
|
837
|
+
fn.stats = function () {
|
|
838
|
+
return Object.assign({}, stats);
|
|
839
|
+
};
|
|
840
|
+
fn.invalidateMeta = function (key) {
|
|
841
|
+
_metaCache.delete(key);
|
|
842
|
+
return { ok: true, key: key };
|
|
259
843
|
};
|
|
844
|
+
return fn;
|
|
260
845
|
}
|
|
261
846
|
|
|
262
|
-
|
|
263
|
-
function _resetCacheForTest() { _cache.clear(); }
|
|
847
|
+
function _resetCacheForTest() { _metaCache.clear(); }
|
|
264
848
|
|
|
265
849
|
module.exports = {
|
|
266
|
-
create:
|
|
267
|
-
integrity:
|
|
268
|
-
DEFAULT_MAX_AGE_SEC:
|
|
269
|
-
IMMUTABLE_MAX_AGE_SEC:
|
|
850
|
+
create: create,
|
|
851
|
+
integrity: integrity,
|
|
852
|
+
DEFAULT_MAX_AGE_SEC: DEFAULT_MAX_AGE_SEC,
|
|
853
|
+
IMMUTABLE_MAX_AGE_SEC: IMMUTABLE_MAX_AGE_SEC,
|
|
270
854
|
DEFAULT_HASHED_PATTERN: DEFAULT_HASHED_PATTERN,
|
|
271
|
-
_resetCacheForTest:
|
|
855
|
+
_resetCacheForTest: _resetCacheForTest,
|
|
856
|
+
_parseRangeHeader: _parseRangeHeader,
|
|
272
857
|
};
|