@orkestrel/server 0.0.23 → 0.0.24
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/dist/src/server/index.cjs +213 -158
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +64 -14
- package/dist/src/server/index.d.ts +64 -14
- package/dist/src/server/index.js +209 -160
- package/dist/src/server/index.js.map +1 -1
- package/package.json +19 -19
|
@@ -9,7 +9,7 @@ let _orkestrel_abort = require("@orkestrel/abort");
|
|
|
9
9
|
let _orkestrel_timeout = require("@orkestrel/timeout");
|
|
10
10
|
let _orkestrel_router_server = require("@orkestrel/router/server");
|
|
11
11
|
let _orkestrel_emitter = require("@orkestrel/emitter");
|
|
12
|
-
|
|
12
|
+
// src/server/constants.ts
|
|
13
13
|
/**
|
|
14
14
|
* Names the default graceful-stop deadline `stop()` gives in-flight requests and
|
|
15
15
|
* claimed upgraded sockets, `10_000` ms.
|
|
@@ -121,8 +121,11 @@ var COMPRESSIBLE_TYPES = Object.freeze(/* @__PURE__ */ new Set([
|
|
|
121
121
|
* shared default.
|
|
122
122
|
*/
|
|
123
123
|
var DEFAULT_ENCODINGS = Object.freeze(["gzip", "deflate"]);
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
/** Names the identity brand carried by lifecycle errors across package copies. */
|
|
125
|
+
var SERVER_ERROR_BRAND = Symbol.for("@orkestrel/server.ServerError");
|
|
126
|
+
/** Names the identity brand carried by body-limit errors across package copies. */
|
|
127
|
+
var CONTENT_TOO_LARGE_ERROR_BRAND = Symbol.for("@orkestrel/server.ContentTooLargeError");
|
|
128
|
+
// src/server/errors.ts
|
|
126
129
|
/**
|
|
127
130
|
* Represents an error a handler (or middleware) throws to produce an HTTP response of a
|
|
128
131
|
* specific status.
|
|
@@ -152,9 +155,9 @@ var DEFAULT_ENCODINGS = Object.freeze(["gzip", "deflate"]);
|
|
|
152
155
|
var HTTPError = class extends Error {
|
|
153
156
|
status;
|
|
154
157
|
context;
|
|
155
|
-
[HTTP_ERROR_BRAND] = true;
|
|
156
158
|
constructor(status, message, context) {
|
|
157
159
|
super(message);
|
|
160
|
+
Object.defineProperty(this, HTTP_ERROR_BRAND, { value: this });
|
|
158
161
|
this.name = "HTTPError";
|
|
159
162
|
this.status = status;
|
|
160
163
|
if (context !== void 0) this.context = context;
|
|
@@ -184,26 +187,23 @@ var HTTPError = class extends Error {
|
|
|
184
187
|
var ContentTooLargeError = class extends HTTPError {
|
|
185
188
|
constructor(limit) {
|
|
186
189
|
super(413, `request body exceeds the ${limit}-byte limit`, { limit });
|
|
190
|
+
Object.defineProperty(this, CONTENT_TOO_LARGE_ERROR_BRAND, { value: this });
|
|
187
191
|
this.name = "ContentTooLargeError";
|
|
188
192
|
}
|
|
189
193
|
};
|
|
190
194
|
/**
|
|
191
195
|
* Narrows an unknown caught value to an {@link HTTPError}, including a subclass such
|
|
192
196
|
* as {@link ContentTooLargeError}, and recognizes an instance from another copy of
|
|
193
|
-
* this package through
|
|
197
|
+
* this package through its own identity brand.
|
|
194
198
|
*
|
|
195
199
|
* @param value - The value to test (typically a `catch` binding)
|
|
196
200
|
* @returns True if `value` is an {@link HTTPError}; false otherwise
|
|
197
201
|
*
|
|
198
202
|
* @remarks
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* the cross-copy brand (interned by using `Symbol.for`, so every copy resolves the
|
|
204
|
-
* same key) and expose the exact fields the server's error boundary reads off
|
|
205
|
-
* a recognized `HTTPError` — a numeric `status` and a string `message`. A
|
|
206
|
-
* plain object that merely carries a `status` without the brand is rejected.
|
|
203
|
+
* Requires a global Error instance with a derived prototype, its own identity brand,
|
|
204
|
+
* a numeric status, a string message, and an absent or object-valued context.
|
|
205
|
+
* Callable contexts are accepted. Subclass names are unrestricted.
|
|
206
|
+
* Transparent proxies and branded plain objects are rejected.
|
|
207
207
|
*
|
|
208
208
|
* @example
|
|
209
209
|
* ```ts
|
|
@@ -217,11 +217,24 @@ var ContentTooLargeError = class extends HTTPError {
|
|
|
217
217
|
* ```
|
|
218
218
|
*/
|
|
219
219
|
function isHTTPError(value) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
220
|
+
return (0, _orkestrel_contract.holds)(() => {
|
|
221
|
+
if (!(0, _orkestrel_contract.isInstance)(value, Error) || _orkestrel_contract.INTRINSICS.prototype(value) === Error.prototype) return false;
|
|
222
|
+
if (!_orkestrel_contract.INTRINSICS.own(value, HTTP_ERROR_BRAND)) return false;
|
|
223
|
+
if (_orkestrel_contract.INTRINSICS.describe(value, HTTP_ERROR_BRAND)?.value !== value) return false;
|
|
224
|
+
const context = _orkestrel_contract.INTRINSICS.reflect.read(value, "context");
|
|
225
|
+
return (0, _orkestrel_contract.isNumber)(_orkestrel_contract.INTRINSICS.reflect.read(value, "status")) && (0, _orkestrel_contract.isString)(_orkestrel_contract.INTRINSICS.reflect.read(value, "message")) && (context === void 0 || (0, _orkestrel_contract.isObject)(context) || (0, _orkestrel_contract.isFunction)(context));
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Recognizes a branded body-limit error, including subclasses and other package copies.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* isContentTooLargeError(new ContentTooLargeError(1024)) // true
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
function isContentTooLargeError(value) {
|
|
237
|
+
return (0, _orkestrel_contract.holds)(() => isHTTPError(value) && _orkestrel_contract.INTRINSICS.own(value, CONTENT_TOO_LARGE_ERROR_BRAND) && _orkestrel_contract.INTRINSICS.describe(value, CONTENT_TOO_LARGE_ERROR_BRAND)?.value === value);
|
|
225
238
|
}
|
|
226
239
|
/**
|
|
227
240
|
* Represents the error this package raises when a caller programmed a call the
|
|
@@ -252,6 +265,7 @@ var ServerError = class extends Error {
|
|
|
252
265
|
context;
|
|
253
266
|
constructor(code, message, context) {
|
|
254
267
|
super(message);
|
|
268
|
+
Object.defineProperty(this, SERVER_ERROR_BRAND, { value: this });
|
|
255
269
|
this.name = "ServerError";
|
|
256
270
|
this.code = code;
|
|
257
271
|
if (context !== void 0) this.context = context;
|
|
@@ -265,10 +279,9 @@ var ServerError = class extends Error {
|
|
|
265
279
|
* @returns True if `value` is a {@link ServerError}; false otherwise
|
|
266
280
|
*
|
|
267
281
|
* @remarks
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
* consumer here.
|
|
282
|
+
* Requires the own identity brand, a declared code, a string message, and an absent
|
|
283
|
+
* or object-valued context on a global Error with a derived prototype.
|
|
284
|
+
* Callable contexts and subclasses are accepted; transparent proxies are rejected.
|
|
272
285
|
*
|
|
273
286
|
* @example
|
|
274
287
|
* ```ts
|
|
@@ -286,10 +299,16 @@ var ServerError = class extends Error {
|
|
|
286
299
|
* ```
|
|
287
300
|
*/
|
|
288
301
|
function isServerError(value) {
|
|
289
|
-
return (0, _orkestrel_contract.
|
|
302
|
+
return (0, _orkestrel_contract.holds)(() => {
|
|
303
|
+
if (!(0, _orkestrel_contract.isInstance)(value, Error) || _orkestrel_contract.INTRINSICS.prototype(value) === Error.prototype) return false;
|
|
304
|
+
if (!_orkestrel_contract.INTRINSICS.own(value, SERVER_ERROR_BRAND)) return false;
|
|
305
|
+
if (_orkestrel_contract.INTRINSICS.describe(value, SERVER_ERROR_BRAND)?.value !== value) return false;
|
|
306
|
+
const code = _orkestrel_contract.INTRINSICS.reflect.read(value, "code");
|
|
307
|
+
const context = _orkestrel_contract.INTRINSICS.reflect.read(value, "context");
|
|
308
|
+
return (code === "STATUS" || code === "NEXT") && (0, _orkestrel_contract.isString)(_orkestrel_contract.INTRINSICS.reflect.read(value, "message")) && (context === void 0 || (0, _orkestrel_contract.isObject)(context) || (0, _orkestrel_contract.isFunction)(context));
|
|
309
|
+
});
|
|
290
310
|
}
|
|
291
|
-
|
|
292
|
-
//#region src/server/validators.ts
|
|
311
|
+
// src/server/validators.ts
|
|
293
312
|
/**
|
|
294
313
|
* Checks whether a `node:net` `server.address()` return is the structured
|
|
295
314
|
* {@link AddressInfo} (carrying a numeric `port`) rather than a pipe `string`
|
|
@@ -314,8 +333,7 @@ function isServerError(value) {
|
|
|
314
333
|
function isAddressInfo(value) {
|
|
315
334
|
return (0, _orkestrel_contract.isRecord)(value) && (0, _orkestrel_contract.isString)(value.address) && (0, _orkestrel_contract.isString)(value.family) && (0, _orkestrel_contract.isNumber)(value.port);
|
|
316
335
|
}
|
|
317
|
-
|
|
318
|
-
//#region src/server/helpers.ts
|
|
336
|
+
// src/server/helpers.ts
|
|
319
337
|
/**
|
|
320
338
|
* Composes an ordered chain of {@link MiddlewareHandler} layers around a `terminal`
|
|
321
339
|
* handler into one request handler — the frozen middleware seam.
|
|
@@ -374,11 +392,12 @@ function compose(middleware, terminal) {
|
|
|
374
392
|
*/
|
|
375
393
|
function wrapMiddleware(layer, next) {
|
|
376
394
|
return async (request, context) => {
|
|
377
|
-
|
|
395
|
+
const calls = [next].values();
|
|
378
396
|
return Promise.resolve(layer(request, context, (nextRequest) => {
|
|
379
|
-
|
|
380
|
-
called
|
|
381
|
-
|
|
397
|
+
const invocation = calls.next();
|
|
398
|
+
if (invocation.done) return Promise.reject(new ServerError("NEXT", "next() was already called by this middleware"));
|
|
399
|
+
const downstream = invocation.value;
|
|
400
|
+
return downstream(nextRequest ?? request, context);
|
|
382
401
|
}));
|
|
383
402
|
};
|
|
384
403
|
}
|
|
@@ -407,8 +426,7 @@ function parseCookies(header) {
|
|
|
407
426
|
const out = {};
|
|
408
427
|
if (header === void 0) return out;
|
|
409
428
|
const segments = header.split(";");
|
|
410
|
-
for (
|
|
411
|
-
const raw = segments[index] ?? "";
|
|
429
|
+
for (const [index, raw] of segments.entries()) {
|
|
412
430
|
const segment = index === 0 ? raw : raw.replace(/^[ \t]+/, "");
|
|
413
431
|
const eq = segment.indexOf("=");
|
|
414
432
|
if (eq < 0) continue;
|
|
@@ -796,14 +814,9 @@ function parseAcceptHeader(header) {
|
|
|
796
814
|
const semicolon = trimmed.indexOf(";");
|
|
797
815
|
const value = (semicolon === -1 ? trimmed : trimmed.slice(0, semicolon)).trim().toLowerCase();
|
|
798
816
|
if (value.length === 0) continue;
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
if (match !== null) {
|
|
803
|
-
const parsed = Number(match[1]);
|
|
804
|
-
if ((0, _orkestrel_contract.isFiniteNumber)(parsed)) q = Math.min(1, Math.max(0, parsed));
|
|
805
|
-
}
|
|
806
|
-
}
|
|
817
|
+
const match = semicolon === -1 ? null : /;\s*q=(-?[0-9.]+)/i.exec(trimmed.slice(semicolon));
|
|
818
|
+
const parsed = match === null ? void 0 : Number(match[1]);
|
|
819
|
+
const q = (0, _orkestrel_contract.isFiniteNumber)(parsed) ? Math.min(1, Math.max(0, parsed)) : 1;
|
|
807
820
|
entries.push({
|
|
808
821
|
value,
|
|
809
822
|
q
|
|
@@ -867,16 +880,13 @@ function computeCodingQuality(entries, coding) {
|
|
|
867
880
|
* ```
|
|
868
881
|
*/
|
|
869
882
|
function resolveCoding(entries, available) {
|
|
870
|
-
|
|
871
|
-
let bestQuality = 0;
|
|
872
|
-
for (const coding of available) {
|
|
883
|
+
return Iterator.from(available).reduce((previous, coding) => {
|
|
873
884
|
const quality = computeCodingQuality(entries, coding);
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
return best;
|
|
885
|
+
return quality > (previous?.quality ?? 0) ? {
|
|
886
|
+
value: coding,
|
|
887
|
+
quality
|
|
888
|
+
} : previous;
|
|
889
|
+
}, void 0)?.value;
|
|
880
890
|
}
|
|
881
891
|
/**
|
|
882
892
|
* Selects the best content-coding for a raw `Accept-Encoding` header from the
|
|
@@ -930,25 +940,54 @@ function negotiateEncoding(header, available) {
|
|
|
930
940
|
* ```
|
|
931
941
|
*/
|
|
932
942
|
function matchMediaType(entries, candidate) {
|
|
943
|
+
return iterateMediaMatches(entries, candidate).reduce((best, match) => {
|
|
944
|
+
if (match.q === 0) return void 0;
|
|
945
|
+
return best === void 0 || match.rank < best.rank || match.rank === best.rank && match.q > best.q ? match : best;
|
|
946
|
+
}, void 0);
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Yields matching media ranges through the first exact rejection.
|
|
950
|
+
*
|
|
951
|
+
* @example
|
|
952
|
+
* ```ts
|
|
953
|
+
* Array.from(iterateMediaMatches([{ value: 'text/plain', q: 1 }], 'text/plain'))
|
|
954
|
+
* // [{ q: 1, rank: 0 }]
|
|
955
|
+
* ```
|
|
956
|
+
*/
|
|
957
|
+
function* iterateMediaMatches(entries, candidate) {
|
|
933
958
|
const slash = candidate.indexOf("/");
|
|
934
959
|
const type = slash === -1 ? candidate : candidate.slice(0, slash);
|
|
935
|
-
let best;
|
|
936
960
|
for (const entry of entries) {
|
|
937
|
-
|
|
938
|
-
if (entry.value === candidate) rank = 0;
|
|
939
|
-
else if (entry.value === `${type}/*`) rank = 1;
|
|
940
|
-
else if (entry.value === "*/*") rank = 2;
|
|
961
|
+
const rank = entry.value === candidate ? 0 : entry.value === `${type}/*` ? 1 : entry.value === "*/*" ? 2 : void 0;
|
|
941
962
|
if (rank === void 0) continue;
|
|
942
963
|
if (entry.q === 0) {
|
|
943
|
-
if (rank === 0)
|
|
964
|
+
if (rank === 0) {
|
|
965
|
+
yield {
|
|
966
|
+
q: 0,
|
|
967
|
+
rank
|
|
968
|
+
};
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
944
971
|
continue;
|
|
945
972
|
}
|
|
946
|
-
|
|
973
|
+
yield {
|
|
947
974
|
q: entry.q,
|
|
948
975
|
rank
|
|
949
976
|
};
|
|
950
977
|
}
|
|
951
|
-
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Yields the same lazily collected request-body promise on every read.
|
|
981
|
+
*
|
|
982
|
+
* @example
|
|
983
|
+
* ```ts
|
|
984
|
+
* const bodies = iterateRequestBody(new Request('http://localhost/'), 1024)
|
|
985
|
+
* bodies.next().value === bodies.next().value // true
|
|
986
|
+
* ```
|
|
987
|
+
*/
|
|
988
|
+
function* iterateRequestBody(request, limit) {
|
|
989
|
+
const body = readBody(request, { limit });
|
|
990
|
+
for (;;) yield body;
|
|
952
991
|
}
|
|
953
992
|
/**
|
|
954
993
|
* Computes the client's quality for one `candidate` language from the parsed
|
|
@@ -974,19 +1013,22 @@ function matchMediaType(entries, candidate) {
|
|
|
974
1013
|
function computeLanguageQuality(entries, candidate) {
|
|
975
1014
|
const lower = candidate.toLowerCase();
|
|
976
1015
|
const primary = lower.split("-")[0];
|
|
977
|
-
|
|
978
|
-
let prefixed;
|
|
979
|
-
let wildcard;
|
|
980
|
-
for (const entry of entries) {
|
|
1016
|
+
const qualities = Iterator.from(entries).reduce((previous, entry) => {
|
|
981
1017
|
const entryPrimary = entry.value.split("-")[0];
|
|
982
|
-
if (entry.value === lower)
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1018
|
+
if (entry.value === lower) return {
|
|
1019
|
+
...previous,
|
|
1020
|
+
named: entry.q
|
|
1021
|
+
};
|
|
1022
|
+
if (entryPrimary === primary) return previous.prefixed === void 0 ? {
|
|
1023
|
+
...previous,
|
|
1024
|
+
prefixed: entry.q
|
|
1025
|
+
} : previous;
|
|
1026
|
+
return entry.value === "*" ? {
|
|
1027
|
+
...previous,
|
|
1028
|
+
wildcard: entry.q
|
|
1029
|
+
} : previous;
|
|
1030
|
+
}, {});
|
|
1031
|
+
return qualities.named ?? qualities.prefixed ?? qualities.wildcard ?? 0;
|
|
990
1032
|
}
|
|
991
1033
|
/**
|
|
992
1034
|
* Checks whether a `Content-Type` is worth compressing.
|
|
@@ -1283,16 +1325,13 @@ function computeIPv6Network(address) {
|
|
|
1283
1325
|
if (halves.length === 2 && second === void 0) return void 0;
|
|
1284
1326
|
const head = first === "" ? [] : first.split(":");
|
|
1285
1327
|
const tail = second === void 0 || second === "" ? [] : second.split(":");
|
|
1286
|
-
|
|
1287
|
-
if (halves.length === 2)
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
...tail
|
|
1294
|
-
];
|
|
1295
|
-
} else groups = head;
|
|
1328
|
+
const missing = 8 - head.length - tail.length;
|
|
1329
|
+
if (halves.length === 2 && missing < 0) return void 0;
|
|
1330
|
+
const groups = halves.length === 2 ? [
|
|
1331
|
+
...head,
|
|
1332
|
+
...Array.from({ length: missing }, () => "0"),
|
|
1333
|
+
...tail
|
|
1334
|
+
] : head;
|
|
1296
1335
|
if (groups.length !== 8) return void 0;
|
|
1297
1336
|
const prefix = [];
|
|
1298
1337
|
for (const group of groups.slice(0, 4)) {
|
|
@@ -1430,23 +1469,24 @@ async function collectRequestBody(request, limit) {
|
|
|
1430
1469
|
if (request.body === null) return /* @__PURE__ */ new Uint8Array(0);
|
|
1431
1470
|
const reader = request.body.getReader();
|
|
1432
1471
|
const chunks = [];
|
|
1433
|
-
let total = 0;
|
|
1434
1472
|
for (;;) {
|
|
1435
1473
|
const { done, value } = await reader.read();
|
|
1436
1474
|
if (done) break;
|
|
1437
|
-
total
|
|
1475
|
+
const total = (chunks.at(-1)?.total ?? 0) + value.byteLength;
|
|
1438
1476
|
if (limit > 0 && total > limit) {
|
|
1439
1477
|
await reader.cancel();
|
|
1440
1478
|
throw new ContentTooLargeError(limit);
|
|
1441
1479
|
}
|
|
1442
|
-
chunks.push(
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
for (const chunk of chunks) {
|
|
1447
|
-
merged.set(chunk, offset);
|
|
1448
|
-
offset += chunk.byteLength;
|
|
1480
|
+
chunks.push({
|
|
1481
|
+
bytes: value,
|
|
1482
|
+
total
|
|
1483
|
+
});
|
|
1449
1484
|
}
|
|
1485
|
+
const merged = new Uint8Array(chunks.at(-1)?.total ?? 0);
|
|
1486
|
+
chunks.reduce((offset, chunk) => {
|
|
1487
|
+
merged.set(chunk.bytes, offset);
|
|
1488
|
+
return offset + chunk.bytes.byteLength;
|
|
1489
|
+
}, 0);
|
|
1450
1490
|
return merged;
|
|
1451
1491
|
}
|
|
1452
1492
|
/**
|
|
@@ -1497,38 +1537,37 @@ function parseEncoding(header) {
|
|
|
1497
1537
|
* @throws {HTTPError} When the compressed stream is corrupt/truncated (400)
|
|
1498
1538
|
*/
|
|
1499
1539
|
async function decompressRequestBody(bytes, encoding, cap) {
|
|
1500
|
-
|
|
1540
|
+
const chunks = [];
|
|
1501
1541
|
const capStream = new TransformStream({ transform(chunk, controller) {
|
|
1502
|
-
total
|
|
1542
|
+
const total = (chunks.at(-1)?.total ?? 0) + chunk.byteLength;
|
|
1503
1543
|
if (cap > 0 && total > cap) {
|
|
1504
1544
|
controller.error(new ContentTooLargeError(cap));
|
|
1505
1545
|
return;
|
|
1506
1546
|
}
|
|
1547
|
+
chunks.push({
|
|
1548
|
+
bytes: chunk,
|
|
1549
|
+
total
|
|
1550
|
+
});
|
|
1507
1551
|
controller.enqueue(chunk);
|
|
1508
1552
|
} });
|
|
1509
1553
|
const reader = new ReadableStream({ start(controller) {
|
|
1510
1554
|
controller.enqueue(bytes);
|
|
1511
1555
|
controller.close();
|
|
1512
1556
|
} }).pipeThrough(new DecompressionStream(encoding)).pipeThrough(capStream).getReader();
|
|
1513
|
-
const chunks = [];
|
|
1514
|
-
let size = 0;
|
|
1515
1557
|
try {
|
|
1516
1558
|
for (;;) {
|
|
1517
|
-
const { done
|
|
1559
|
+
const { done } = await reader.read();
|
|
1518
1560
|
if (done) break;
|
|
1519
|
-
chunks.push(value);
|
|
1520
|
-
size += value.byteLength;
|
|
1521
1561
|
}
|
|
1522
1562
|
} catch (error) {
|
|
1523
1563
|
if ((0, _orkestrel_contract.isInstance)(error, ContentTooLargeError)) throw error;
|
|
1524
1564
|
throw new HTTPError(400, "malformed compressed request body");
|
|
1525
1565
|
}
|
|
1526
|
-
const merged = new Uint8Array(
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
}
|
|
1566
|
+
const merged = new Uint8Array(chunks.at(-1)?.total ?? 0);
|
|
1567
|
+
chunks.reduce((offset, chunk) => {
|
|
1568
|
+
merged.set(chunk.bytes, offset);
|
|
1569
|
+
return offset + chunk.bytes.byteLength;
|
|
1570
|
+
}, 0);
|
|
1532
1571
|
return merged;
|
|
1533
1572
|
}
|
|
1534
1573
|
/**
|
|
@@ -1648,8 +1687,7 @@ async function discoverPort(preferred) {
|
|
|
1648
1687
|
throw error;
|
|
1649
1688
|
}
|
|
1650
1689
|
}
|
|
1651
|
-
|
|
1652
|
-
//#region src/server/Negotiator.ts
|
|
1690
|
+
// src/server/Negotiator.ts
|
|
1653
1691
|
/**
|
|
1654
1692
|
* Represents the content-negotiation machine over the weighted `Accept` family — a
|
|
1655
1693
|
* reusable, cross-middleware entity rather than a middleware. Implements exactly
|
|
@@ -1675,20 +1713,16 @@ var Negotiator = class {
|
|
|
1675
1713
|
if (available.length === 0) return void 0;
|
|
1676
1714
|
const entries = parseAcceptHeader(header);
|
|
1677
1715
|
if (entries.length === 0) return available[0];
|
|
1678
|
-
|
|
1679
|
-
let bestQuality = 0;
|
|
1680
|
-
let bestRank = Number.POSITIVE_INFINITY;
|
|
1681
|
-
for (const candidate of available) {
|
|
1716
|
+
return Iterator.from(available).reduce((previous, candidate) => {
|
|
1682
1717
|
const match = matchMediaType(entries, candidate);
|
|
1683
|
-
if (match === void 0)
|
|
1718
|
+
if (match === void 0) return previous;
|
|
1684
1719
|
const { q, rank } = match;
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
}
|
|
1690
|
-
}
|
|
1691
|
-
return best;
|
|
1720
|
+
return q > (previous?.q ?? 0) || q === (previous?.q ?? 0) && rank < (previous?.rank ?? Number.POSITIVE_INFINITY) ? {
|
|
1721
|
+
value: candidate,
|
|
1722
|
+
q,
|
|
1723
|
+
rank
|
|
1724
|
+
} : previous;
|
|
1725
|
+
}, void 0)?.value;
|
|
1692
1726
|
}
|
|
1693
1727
|
encoding(header, available) {
|
|
1694
1728
|
if (available.length === 0) return void 0;
|
|
@@ -1698,16 +1732,13 @@ var Negotiator = class {
|
|
|
1698
1732
|
if (available.length === 0) return void 0;
|
|
1699
1733
|
const entries = parseAcceptHeader(header);
|
|
1700
1734
|
if (entries.length === 0) return available[0];
|
|
1701
|
-
|
|
1702
|
-
let bestQuality = 0;
|
|
1703
|
-
for (const candidate of available) {
|
|
1735
|
+
return Iterator.from(available).reduce((previous, candidate) => {
|
|
1704
1736
|
const quality = computeLanguageQuality(entries, candidate);
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
}
|
|
1709
|
-
}
|
|
1710
|
-
return best;
|
|
1737
|
+
return quality > (previous?.quality ?? 0) ? {
|
|
1738
|
+
value: candidate,
|
|
1739
|
+
quality
|
|
1740
|
+
} : previous;
|
|
1741
|
+
}, void 0)?.value;
|
|
1711
1742
|
}
|
|
1712
1743
|
/**
|
|
1713
1744
|
* Dispatches the request to the handler its `Accept` header negotiates,
|
|
@@ -1727,8 +1758,36 @@ var Negotiator = class {
|
|
|
1727
1758
|
return handler(request, context);
|
|
1728
1759
|
}
|
|
1729
1760
|
};
|
|
1730
|
-
|
|
1731
|
-
|
|
1761
|
+
// src/server/ServerBinding.ts
|
|
1762
|
+
/**
|
|
1763
|
+
* Owns one startup attempt's listening subscription and validated bound port.
|
|
1764
|
+
*
|
|
1765
|
+
* @remarks
|
|
1766
|
+
* Create an instance per bind attempt. Subscribe before calling the native server's
|
|
1767
|
+
* `listen`, retain `pending` for failure cleanup, and record the native address after
|
|
1768
|
+
* listening settles. Reading `port` before a valid address was recorded throws.
|
|
1769
|
+
*/
|
|
1770
|
+
var ServerBinding = class {
|
|
1771
|
+
#pending;
|
|
1772
|
+
#port;
|
|
1773
|
+
get pending() {
|
|
1774
|
+
return this.#pending;
|
|
1775
|
+
}
|
|
1776
|
+
get port() {
|
|
1777
|
+
if (this.#port === void 0) throw new TypeError("server bound a listener with no resolvable AddressInfo");
|
|
1778
|
+
return this.#port;
|
|
1779
|
+
}
|
|
1780
|
+
subscribe(server, signal) {
|
|
1781
|
+
const pending = (0, node_events.once)(server, "listening", { signal });
|
|
1782
|
+
this.#pending = pending;
|
|
1783
|
+
return pending;
|
|
1784
|
+
}
|
|
1785
|
+
record(address) {
|
|
1786
|
+
if (!isAddressInfo(address)) throw new TypeError("server bound a listener with no resolvable AddressInfo");
|
|
1787
|
+
this.#port = address.port;
|
|
1788
|
+
}
|
|
1789
|
+
};
|
|
1790
|
+
// src/server/Server.ts
|
|
1732
1791
|
/**
|
|
1733
1792
|
* Represents the HTTP server facade — an observable `node:http` lifecycle composing this
|
|
1734
1793
|
* module's own middleware onion around a consumed `@orkestrel/router`
|
|
@@ -1947,13 +2006,12 @@ var Server = class {
|
|
|
1947
2006
|
this.#accept(message, response);
|
|
1948
2007
|
}
|
|
1949
2008
|
async #accept(message, response) {
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
raw = (0, _orkestrel_router_server.buildRequest)(message, { response });
|
|
1953
|
-
} catch {
|
|
2009
|
+
const created = (0, _orkestrel_contract.attempt)(() => (0, _orkestrel_router_server.buildRequest)(message, { response }));
|
|
2010
|
+
if (!created.success) {
|
|
1954
2011
|
await this.#respond(this.#boundary(new HTTPError(400, "invalid request")), response);
|
|
1955
2012
|
return;
|
|
1956
2013
|
}
|
|
2014
|
+
const raw = created.value;
|
|
1957
2015
|
const method = raw.method;
|
|
1958
2016
|
const url = new URL(raw.url);
|
|
1959
2017
|
const start = performance.now();
|
|
@@ -2020,15 +2078,14 @@ var Server = class {
|
|
|
2020
2078
|
socket.destroy();
|
|
2021
2079
|
return;
|
|
2022
2080
|
}
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2081
|
+
const handled = Iterator.from(this.#upgradeHandlers).some((handler) => {
|
|
2082
|
+
try {
|
|
2083
|
+
return handler(request, socket, head);
|
|
2084
|
+
} catch (error) {
|
|
2085
|
+
this.#emitter.emit("error", error);
|
|
2086
|
+
return false;
|
|
2028
2087
|
}
|
|
2029
|
-
}
|
|
2030
|
-
this.#emitter.emit("error", error);
|
|
2031
|
-
}
|
|
2088
|
+
});
|
|
2032
2089
|
if (handled) this.#trackSocket(socket);
|
|
2033
2090
|
else socket.destroy();
|
|
2034
2091
|
this.#emitter.emit("upgrade", request, handled);
|
|
@@ -2038,14 +2095,13 @@ var Server = class {
|
|
|
2038
2095
|
const startup = deadline === void 0 ? signal : (0, _orkestrel_abort.linkSignal)(deadline.signal, signal);
|
|
2039
2096
|
const binding = (0, _orkestrel_abort.createAbort)();
|
|
2040
2097
|
const relay = startup === void 0 ? void 0 : (0, node_events.addAbortListener)(startup, () => binding.abort(startup.reason));
|
|
2041
|
-
|
|
2042
|
-
let port;
|
|
2098
|
+
const operation = new ServerBinding();
|
|
2043
2099
|
try {
|
|
2044
2100
|
signal?.throwIfAborted();
|
|
2045
2101
|
deadline?.start();
|
|
2046
2102
|
if (deadline?.ms === 0 && startup !== void 0) await (0, node_events.once)(startup, "abort");
|
|
2047
2103
|
startup?.throwIfAborted();
|
|
2048
|
-
listening =
|
|
2104
|
+
const listening = operation.subscribe(server, binding.signal);
|
|
2049
2105
|
if (startup === void 0) server.listen(this.#configuredPort ?? 0, this.#host);
|
|
2050
2106
|
else server.listen({
|
|
2051
2107
|
port: this.#configuredPort ?? 0,
|
|
@@ -2053,14 +2109,13 @@ var Server = class {
|
|
|
2053
2109
|
signal: binding.signal
|
|
2054
2110
|
});
|
|
2055
2111
|
await listening;
|
|
2056
|
-
|
|
2057
|
-
if (!isAddressInfo(address)) throw new TypeError("server bound a listener with no resolvable AddressInfo");
|
|
2058
|
-
port = address.port;
|
|
2112
|
+
operation.record(server.address());
|
|
2059
2113
|
} catch (error) {
|
|
2060
2114
|
const expired = deadline?.expired === true;
|
|
2061
2115
|
const cancelled = startup?.aborted === true;
|
|
2062
2116
|
server.on("error", () => void 0);
|
|
2063
2117
|
binding.abort(error);
|
|
2118
|
+
const listening = operation.pending;
|
|
2064
2119
|
if (listening !== void 0) await listening.catch(() => void 0);
|
|
2065
2120
|
await this.#close(server, true);
|
|
2066
2121
|
this.#http = void 0;
|
|
@@ -2073,17 +2128,15 @@ var Server = class {
|
|
|
2073
2128
|
relay?.[Symbol.dispose]();
|
|
2074
2129
|
deadline?.clear();
|
|
2075
2130
|
}
|
|
2131
|
+
const port = operation.port;
|
|
2076
2132
|
this.#port = port;
|
|
2077
2133
|
this.#status = "listening";
|
|
2078
2134
|
this.#emitter.emit("start", port);
|
|
2079
2135
|
return port;
|
|
2080
2136
|
}
|
|
2081
2137
|
#createBody(request) {
|
|
2082
|
-
|
|
2083
|
-
return () =>
|
|
2084
|
-
cached ??= readBody(request, { limit: this.#limit });
|
|
2085
|
-
return cached;
|
|
2086
|
-
};
|
|
2138
|
+
const bodies = iterateRequestBody(request, this.#limit);
|
|
2139
|
+
return () => bodies.next().value;
|
|
2087
2140
|
}
|
|
2088
2141
|
#close(server, force) {
|
|
2089
2142
|
return new Promise((resolve) => {
|
|
@@ -2126,10 +2179,9 @@ var Server = class {
|
|
|
2126
2179
|
#trackStart() {
|
|
2127
2180
|
this.#enter();
|
|
2128
2181
|
this.#pending += 1;
|
|
2129
|
-
|
|
2182
|
+
const completions = [void 0].values();
|
|
2130
2183
|
return () => {
|
|
2131
|
-
if (done) return;
|
|
2132
|
-
done = true;
|
|
2184
|
+
if (completions.next().done) return;
|
|
2133
2185
|
this.#pending -= 1;
|
|
2134
2186
|
this.#settle();
|
|
2135
2187
|
};
|
|
@@ -2148,8 +2200,7 @@ var Server = class {
|
|
|
2148
2200
|
await (0, node_events.once)(AbortSignal.any([signal, this.#wakeup.signal]), "abort");
|
|
2149
2201
|
}
|
|
2150
2202
|
};
|
|
2151
|
-
|
|
2152
|
-
//#region src/server/Stream.ts
|
|
2203
|
+
// src/server/Stream.ts
|
|
2153
2204
|
/**
|
|
2154
2205
|
* Represents the Server-Sent-Events handle over an open, fetch-standard streaming
|
|
2155
2206
|
* `Response`. Implements exactly {@link StreamInterface}.
|
|
@@ -2260,8 +2311,7 @@ var Stream = class {
|
|
|
2260
2311
|
this.#wakeup = void 0;
|
|
2261
2312
|
}
|
|
2262
2313
|
};
|
|
2263
|
-
|
|
2264
|
-
//#region src/server/factories.ts
|
|
2314
|
+
// src/server/factories.ts
|
|
2265
2315
|
/**
|
|
2266
2316
|
* Creates a {@link NegotiatorInterface} — the reusable content-negotiation
|
|
2267
2317
|
* machine over the weighted `Accept` family.
|
|
@@ -2351,8 +2401,8 @@ function createServer(options) {
|
|
|
2351
2401
|
function createStream(options) {
|
|
2352
2402
|
return new Stream(options);
|
|
2353
2403
|
}
|
|
2354
|
-
//#endregion
|
|
2355
2404
|
exports.COMPRESSIBLE_TYPES = COMPRESSIBLE_TYPES;
|
|
2405
|
+
exports.CONTENT_TOO_LARGE_ERROR_BRAND = CONTENT_TOO_LARGE_ERROR_BRAND;
|
|
2356
2406
|
exports.ContentTooLargeError = ContentTooLargeError;
|
|
2357
2407
|
exports.DEFAULT_BODY_LIMIT = DEFAULT_BODY_LIMIT;
|
|
2358
2408
|
exports.DEFAULT_DECOMPRESSED_LIMIT = DEFAULT_DECOMPRESSED_LIMIT;
|
|
@@ -2362,8 +2412,10 @@ exports.HTTPError = HTTPError;
|
|
|
2362
2412
|
exports.HTTP_ERROR_BRAND = HTTP_ERROR_BRAND;
|
|
2363
2413
|
exports.Negotiator = Negotiator;
|
|
2364
2414
|
exports.REQUEST_ID_PATTERN = REQUEST_ID_PATTERN;
|
|
2415
|
+
exports.SERVER_ERROR_BRAND = SERVER_ERROR_BRAND;
|
|
2365
2416
|
exports.SSE_HEADERS = SSE_HEADERS;
|
|
2366
2417
|
exports.Server = Server;
|
|
2418
|
+
exports.ServerBinding = ServerBinding;
|
|
2367
2419
|
exports.ServerError = ServerError;
|
|
2368
2420
|
exports.Stream = Stream;
|
|
2369
2421
|
exports.clearCookie = clearCookie;
|
|
@@ -2383,12 +2435,15 @@ exports.decompressRequestBody = decompressRequestBody;
|
|
|
2383
2435
|
exports.discoverPort = discoverPort;
|
|
2384
2436
|
exports.isAddressInfo = isAddressInfo;
|
|
2385
2437
|
exports.isCompressibleType = isCompressibleType;
|
|
2438
|
+
exports.isContentTooLargeError = isContentTooLargeError;
|
|
2386
2439
|
exports.isCookieAttribute = isCookieAttribute;
|
|
2387
2440
|
exports.isCookieName = isCookieName;
|
|
2388
2441
|
exports.isDangerousKey = isDangerousKey;
|
|
2389
2442
|
exports.isHTTPError = isHTTPError;
|
|
2390
2443
|
exports.isServerError = isServerError;
|
|
2391
2444
|
exports.isValidRequestId = isValidRequestId;
|
|
2445
|
+
exports.iterateMediaMatches = iterateMediaMatches;
|
|
2446
|
+
exports.iterateRequestBody = iterateRequestBody;
|
|
2392
2447
|
exports.matchMediaType = matchMediaType;
|
|
2393
2448
|
exports.matchesETag = matchesETag;
|
|
2394
2449
|
exports.mergeVary = mergeVary;
|