@orkestrel/server 0.0.6 → 0.0.7
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 +137 -89
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +55 -0
- package/dist/src/server/index.d.ts +55 -0
- package/dist/src/server/index.js +135 -90
- package/dist/src/server/index.js.map +1 -1
- package/package.json +20 -18
|
@@ -514,6 +514,26 @@ export declare function encodeBase64Url(bytes: Uint8Array): string;
|
|
|
514
514
|
*/
|
|
515
515
|
export declare type Encoding = 'gzip' | 'deflate' | 'identity';
|
|
516
516
|
|
|
517
|
+
/**
|
|
518
|
+
* Enqueue encoded text into an open byte stream.
|
|
519
|
+
*
|
|
520
|
+
* @param controller - The stream controller, when the stream has started
|
|
521
|
+
* @param encoder - The encoder used for the stream's byte representation
|
|
522
|
+
* @param closed - Whether the stream has already ended or been cancelled
|
|
523
|
+
* @param text - The text to encode and enqueue
|
|
524
|
+
* @returns Nothing
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* ```ts
|
|
528
|
+
* const body = new ReadableStream<Uint8Array>({
|
|
529
|
+
* start(controller) {
|
|
530
|
+
* enqueueStreamText(controller, new TextEncoder(), false, 'hello')
|
|
531
|
+
* },
|
|
532
|
+
* })
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
export declare function enqueueStreamText(controller: ReadableStreamDefaultController<Uint8Array> | undefined, encoder: TextEncoder, closed: boolean, text: string): void;
|
|
536
|
+
|
|
517
537
|
/**
|
|
518
538
|
* A map of media type → handler for {@link NegotiatorInterface.format} — the
|
|
519
539
|
* content-negotiation dispatch table.
|
|
@@ -1165,6 +1185,19 @@ export declare function parseCookies(header: string | undefined): Record<string,
|
|
|
1165
1185
|
*/
|
|
1166
1186
|
export declare function parseRange(header: string | undefined, size: number): RangeSpec | undefined;
|
|
1167
1187
|
|
|
1188
|
+
/**
|
|
1189
|
+
* Bind and close a throwaway TCP server to resolve one available port.
|
|
1190
|
+
*
|
|
1191
|
+
* @param port - The requested port, with `0` selecting an ephemeral port
|
|
1192
|
+
* @returns The bound port after the probe server has closed
|
|
1193
|
+
*
|
|
1194
|
+
* @example
|
|
1195
|
+
* ```ts
|
|
1196
|
+
* const port = await probePort(0)
|
|
1197
|
+
* ```
|
|
1198
|
+
*/
|
|
1199
|
+
export declare function probePort(port: number): Promise<number>;
|
|
1200
|
+
|
|
1168
1201
|
/**
|
|
1169
1202
|
* The parsed outcome of an HTTP `Range` request header.
|
|
1170
1203
|
*
|
|
@@ -1841,6 +1874,28 @@ export declare type UpgradeHandler = (request: IncomingMessage, socket: Duplex,
|
|
|
1841
1874
|
*/
|
|
1842
1875
|
export declare function verifyToken(token: string, secret: TokenSecret): Promise<string | undefined>;
|
|
1843
1876
|
|
|
1877
|
+
/**
|
|
1878
|
+
* Wrap one middleware layer around its downstream handler.
|
|
1879
|
+
*
|
|
1880
|
+
* @remarks
|
|
1881
|
+
* The returned handler enforces the one-call `next` invariant while preserving
|
|
1882
|
+
* request substitution and the middleware's synchronous or asynchronous result.
|
|
1883
|
+
*
|
|
1884
|
+
* @typeParam TState - The consumer's opaque per-request state type
|
|
1885
|
+
* @param layer - The middleware layer to invoke
|
|
1886
|
+
* @param next - The downstream handler wrapped by `layer`
|
|
1887
|
+
* @returns A handler with `layer` composed around `next`
|
|
1888
|
+
*
|
|
1889
|
+
* @example
|
|
1890
|
+
* ```ts
|
|
1891
|
+
* const handle = wrapMiddleware(
|
|
1892
|
+
* async (_request, _context, next) => next(),
|
|
1893
|
+
* async () => new Response('ok'),
|
|
1894
|
+
* )
|
|
1895
|
+
* ```
|
|
1896
|
+
*/
|
|
1897
|
+
export declare function wrapMiddleware<TState>(layer: MiddlewareHandler<TState>, next: (request: Request, context: MiddlewareContext<TState>) => Promise<Response>): (request: Request, context: MiddlewareContext<TState>) => Promise<Response>;
|
|
1898
|
+
|
|
1844
1899
|
/**
|
|
1845
1900
|
* Write a SIGNED cookie — HMAC-sign `value` with {@link signToken} and append
|
|
1846
1901
|
* it as a `Set-Cookie` (the inverse of {@link readSignedCookie}).
|
|
@@ -514,6 +514,26 @@ export declare function encodeBase64Url(bytes: Uint8Array): string;
|
|
|
514
514
|
*/
|
|
515
515
|
export declare type Encoding = 'gzip' | 'deflate' | 'identity';
|
|
516
516
|
|
|
517
|
+
/**
|
|
518
|
+
* Enqueue encoded text into an open byte stream.
|
|
519
|
+
*
|
|
520
|
+
* @param controller - The stream controller, when the stream has started
|
|
521
|
+
* @param encoder - The encoder used for the stream's byte representation
|
|
522
|
+
* @param closed - Whether the stream has already ended or been cancelled
|
|
523
|
+
* @param text - The text to encode and enqueue
|
|
524
|
+
* @returns Nothing
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* ```ts
|
|
528
|
+
* const body = new ReadableStream<Uint8Array>({
|
|
529
|
+
* start(controller) {
|
|
530
|
+
* enqueueStreamText(controller, new TextEncoder(), false, 'hello')
|
|
531
|
+
* },
|
|
532
|
+
* })
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
export declare function enqueueStreamText(controller: ReadableStreamDefaultController<Uint8Array> | undefined, encoder: TextEncoder, closed: boolean, text: string): void;
|
|
536
|
+
|
|
517
537
|
/**
|
|
518
538
|
* A map of media type → handler for {@link NegotiatorInterface.format} — the
|
|
519
539
|
* content-negotiation dispatch table.
|
|
@@ -1165,6 +1185,19 @@ export declare function parseCookies(header: string | undefined): Record<string,
|
|
|
1165
1185
|
*/
|
|
1166
1186
|
export declare function parseRange(header: string | undefined, size: number): RangeSpec | undefined;
|
|
1167
1187
|
|
|
1188
|
+
/**
|
|
1189
|
+
* Bind and close a throwaway TCP server to resolve one available port.
|
|
1190
|
+
*
|
|
1191
|
+
* @param port - The requested port, with `0` selecting an ephemeral port
|
|
1192
|
+
* @returns The bound port after the probe server has closed
|
|
1193
|
+
*
|
|
1194
|
+
* @example
|
|
1195
|
+
* ```ts
|
|
1196
|
+
* const port = await probePort(0)
|
|
1197
|
+
* ```
|
|
1198
|
+
*/
|
|
1199
|
+
export declare function probePort(port: number): Promise<number>;
|
|
1200
|
+
|
|
1168
1201
|
/**
|
|
1169
1202
|
* The parsed outcome of an HTTP `Range` request header.
|
|
1170
1203
|
*
|
|
@@ -1841,6 +1874,28 @@ export declare type UpgradeHandler = (request: IncomingMessage, socket: Duplex,
|
|
|
1841
1874
|
*/
|
|
1842
1875
|
export declare function verifyToken(token: string, secret: TokenSecret): Promise<string | undefined>;
|
|
1843
1876
|
|
|
1877
|
+
/**
|
|
1878
|
+
* Wrap one middleware layer around its downstream handler.
|
|
1879
|
+
*
|
|
1880
|
+
* @remarks
|
|
1881
|
+
* The returned handler enforces the one-call `next` invariant while preserving
|
|
1882
|
+
* request substitution and the middleware's synchronous or asynchronous result.
|
|
1883
|
+
*
|
|
1884
|
+
* @typeParam TState - The consumer's opaque per-request state type
|
|
1885
|
+
* @param layer - The middleware layer to invoke
|
|
1886
|
+
* @param next - The downstream handler wrapped by `layer`
|
|
1887
|
+
* @returns A handler with `layer` composed around `next`
|
|
1888
|
+
*
|
|
1889
|
+
* @example
|
|
1890
|
+
* ```ts
|
|
1891
|
+
* const handle = wrapMiddleware(
|
|
1892
|
+
* async (_request, _context, next) => next(),
|
|
1893
|
+
* async () => new Response('ok'),
|
|
1894
|
+
* )
|
|
1895
|
+
* ```
|
|
1896
|
+
*/
|
|
1897
|
+
export declare function wrapMiddleware<TState>(layer: MiddlewareHandler<TState>, next: (request: Request, context: MiddlewareContext<TState>) => Promise<Response>): (request: Request, context: MiddlewareContext<TState>) => Promise<Response>;
|
|
1898
|
+
|
|
1844
1899
|
/**
|
|
1845
1900
|
* Write a SIGNED cookie — HMAC-sign `value` with {@link signToken} and append
|
|
1846
1901
|
* it as a `Set-Cookie` (the inverse of {@link readSignedCookie}).
|
package/dist/src/server/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isFiniteNumber, isFunction, isNumber, isRecord, isString, parseJSON } from "@orkestrel/contract";
|
|
2
|
+
import { once } from "node:events";
|
|
2
3
|
import { createServer as createServer$1 } from "node:net";
|
|
3
4
|
import { createServer as createServer$2 } from "node:http";
|
|
4
5
|
import { createAbort, linkSignal } from "@orkestrel/abort";
|
|
@@ -140,7 +141,7 @@ var HTTPError = class extends Error {
|
|
|
140
141
|
super(message);
|
|
141
142
|
this.name = "HTTPError";
|
|
142
143
|
this.status = status;
|
|
143
|
-
this.context = context;
|
|
144
|
+
if (context !== void 0) this.context = context;
|
|
144
145
|
}
|
|
145
146
|
};
|
|
146
147
|
/**
|
|
@@ -240,19 +241,36 @@ function isHTTPError(value) {
|
|
|
240
241
|
* ```
|
|
241
242
|
*/
|
|
242
243
|
function compose(middleware, terminal) {
|
|
244
|
+
return middleware.reduceRight((next, layer) => wrapMiddleware(layer, next), terminal);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Wrap one middleware layer around its downstream handler.
|
|
248
|
+
*
|
|
249
|
+
* @remarks
|
|
250
|
+
* The returned handler enforces the one-call `next` invariant while preserving
|
|
251
|
+
* request substitution and the middleware's synchronous or asynchronous result.
|
|
252
|
+
*
|
|
253
|
+
* @typeParam TState - The consumer's opaque per-request state type
|
|
254
|
+
* @param layer - The middleware layer to invoke
|
|
255
|
+
* @param next - The downstream handler wrapped by `layer`
|
|
256
|
+
* @returns A handler with `layer` composed around `next`
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* const handle = wrapMiddleware(
|
|
261
|
+
* async (_request, _context, next) => next(),
|
|
262
|
+
* async () => new Response('ok'),
|
|
263
|
+
* )
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
function wrapMiddleware(layer, next) {
|
|
243
267
|
return async (request, context) => {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
called = true;
|
|
251
|
-
return dispatch(index + 1, nextRequest ?? currentRequest);
|
|
252
|
-
};
|
|
253
|
-
return Promise.resolve(layer(currentRequest, context, next));
|
|
254
|
-
};
|
|
255
|
-
return dispatch(0, request);
|
|
268
|
+
let called = false;
|
|
269
|
+
return Promise.resolve(layer(request, context, (nextRequest) => {
|
|
270
|
+
if (called) return Promise.reject(/* @__PURE__ */ new Error("next() was already called by this middleware"));
|
|
271
|
+
called = true;
|
|
272
|
+
return next(nextRequest ?? request, context);
|
|
273
|
+
}));
|
|
256
274
|
};
|
|
257
275
|
}
|
|
258
276
|
/**
|
|
@@ -1026,7 +1044,9 @@ function parseRange(header, size) {
|
|
|
1026
1044
|
if (header === void 0) return void 0;
|
|
1027
1045
|
const match = /^bytes=(.*)$/.exec(header.trim());
|
|
1028
1046
|
if (match === null) return void 0;
|
|
1029
|
-
const
|
|
1047
|
+
const capture = match[1];
|
|
1048
|
+
if (capture === void 0) return void 0;
|
|
1049
|
+
const spec = capture.trim();
|
|
1030
1050
|
if (spec.includes(",")) return void 0;
|
|
1031
1051
|
const dash = spec.indexOf("-");
|
|
1032
1052
|
if (dash < 0) return void 0;
|
|
@@ -1182,8 +1202,12 @@ function ipv6Network(address) {
|
|
|
1182
1202
|
const percent = address.indexOf("%");
|
|
1183
1203
|
const halves = (percent === -1 ? address : address.slice(0, percent)).split("::");
|
|
1184
1204
|
if (halves.length > 2) return void 0;
|
|
1185
|
-
const
|
|
1186
|
-
|
|
1205
|
+
const first = halves[0];
|
|
1206
|
+
if (first === void 0) return void 0;
|
|
1207
|
+
const second = halves[1];
|
|
1208
|
+
if (halves.length === 2 && second === void 0) return void 0;
|
|
1209
|
+
const head = first === "" ? [] : first.split(":");
|
|
1210
|
+
const tail = second === void 0 || second === "" ? [] : second.split(":");
|
|
1187
1211
|
let groups;
|
|
1188
1212
|
if (halves.length === 2) {
|
|
1189
1213
|
const missing = 8 - head.length - tail.length;
|
|
@@ -1253,6 +1277,28 @@ function serializeEvent(message) {
|
|
|
1253
1277
|
return `${lines.join("\n")}\n\n`;
|
|
1254
1278
|
}
|
|
1255
1279
|
/**
|
|
1280
|
+
* Enqueue encoded text into an open byte stream.
|
|
1281
|
+
*
|
|
1282
|
+
* @param controller - The stream controller, when the stream has started
|
|
1283
|
+
* @param encoder - The encoder used for the stream's byte representation
|
|
1284
|
+
* @param closed - Whether the stream has already ended or been cancelled
|
|
1285
|
+
* @param text - The text to encode and enqueue
|
|
1286
|
+
* @returns Nothing
|
|
1287
|
+
*
|
|
1288
|
+
* @example
|
|
1289
|
+
* ```ts
|
|
1290
|
+
* const body = new ReadableStream<Uint8Array>({
|
|
1291
|
+
* start(controller) {
|
|
1292
|
+
* enqueueStreamText(controller, new TextEncoder(), false, 'hello')
|
|
1293
|
+
* },
|
|
1294
|
+
* })
|
|
1295
|
+
* ```
|
|
1296
|
+
*/
|
|
1297
|
+
function enqueueStreamText(controller, encoder, closed, text) {
|
|
1298
|
+
if (closed || controller === void 0) return;
|
|
1299
|
+
controller.enqueue(encoder.encode(text));
|
|
1300
|
+
}
|
|
1301
|
+
/**
|
|
1256
1302
|
* Open a generic Server-Sent-Events stream — a fetch-standard `Response`
|
|
1257
1303
|
* whose body is a `ReadableStream` a caller writes {@link SSEMessage}s into.
|
|
1258
1304
|
*
|
|
@@ -1293,24 +1339,19 @@ function openStream(options) {
|
|
|
1293
1339
|
...SSE_HEADERS,
|
|
1294
1340
|
...options?.headers
|
|
1295
1341
|
});
|
|
1296
|
-
const response = new Response(body, {
|
|
1297
|
-
status: options?.status ?? 200,
|
|
1298
|
-
headers
|
|
1299
|
-
});
|
|
1300
|
-
const enqueue = (text) => {
|
|
1301
|
-
if (closed || controller === void 0) return;
|
|
1302
|
-
controller.enqueue(encoder.encode(text));
|
|
1303
|
-
};
|
|
1304
1342
|
return {
|
|
1305
|
-
response,
|
|
1343
|
+
response: new Response(body, {
|
|
1344
|
+
status: options?.status ?? 200,
|
|
1345
|
+
headers
|
|
1346
|
+
}),
|
|
1306
1347
|
get closed() {
|
|
1307
1348
|
return closed;
|
|
1308
1349
|
},
|
|
1309
1350
|
write(message) {
|
|
1310
|
-
|
|
1351
|
+
enqueueStreamText(controller, encoder, closed, serializeEvent(message));
|
|
1311
1352
|
},
|
|
1312
1353
|
comment(text) {
|
|
1313
|
-
|
|
1354
|
+
enqueueStreamText(controller, encoder, closed, `: ${text}\n\n`);
|
|
1314
1355
|
},
|
|
1315
1356
|
end() {
|
|
1316
1357
|
if (closed) return;
|
|
@@ -1563,6 +1604,29 @@ function isAddressInfo(value) {
|
|
|
1563
1604
|
return isRecord(value) && isNumber(value.port);
|
|
1564
1605
|
}
|
|
1565
1606
|
/**
|
|
1607
|
+
* Bind and close a throwaway TCP server to resolve one available port.
|
|
1608
|
+
*
|
|
1609
|
+
* @param port - The requested port, with `0` selecting an ephemeral port
|
|
1610
|
+
* @returns The bound port after the probe server has closed
|
|
1611
|
+
*
|
|
1612
|
+
* @example
|
|
1613
|
+
* ```ts
|
|
1614
|
+
* const port = await probePort(0)
|
|
1615
|
+
* ```
|
|
1616
|
+
*/
|
|
1617
|
+
async function probePort(port) {
|
|
1618
|
+
const probe = createServer$1();
|
|
1619
|
+
const listening = once(probe, "listening");
|
|
1620
|
+
probe.listen(port);
|
|
1621
|
+
await listening;
|
|
1622
|
+
const address = probe.address();
|
|
1623
|
+
const resolved = isAddressInfo(address) ? address.port : 0;
|
|
1624
|
+
const closed = once(probe, "close");
|
|
1625
|
+
probe.close();
|
|
1626
|
+
await closed;
|
|
1627
|
+
return resolved;
|
|
1628
|
+
}
|
|
1629
|
+
/**
|
|
1566
1630
|
* Find a FREE TCP port — bind a throwaway `node:net` server, read the
|
|
1567
1631
|
* OS-assigned port, close it, and resolve that port.
|
|
1568
1632
|
*
|
|
@@ -1587,25 +1651,14 @@ function isAddressInfo(value) {
|
|
|
1587
1651
|
* const port = await discoverPort() // a guaranteed-free ephemeral port
|
|
1588
1652
|
* ```
|
|
1589
1653
|
*/
|
|
1590
|
-
function discoverPort(preferred) {
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
else reject(error);
|
|
1599
|
-
};
|
|
1600
|
-
probe.once("error", onError);
|
|
1601
|
-
probe.listen(port, () => {
|
|
1602
|
-
const address = probe.address();
|
|
1603
|
-
const resolved = isAddressInfo(address) ? address.port : 0;
|
|
1604
|
-
probe.close(() => resolve(resolved));
|
|
1605
|
-
});
|
|
1606
|
-
};
|
|
1607
|
-
bind(preferred ?? 0, preferred !== void 0);
|
|
1608
|
-
});
|
|
1654
|
+
async function discoverPort(preferred) {
|
|
1655
|
+
if (preferred === void 0) return probePort(0);
|
|
1656
|
+
try {
|
|
1657
|
+
return await probePort(preferred);
|
|
1658
|
+
} catch (error) {
|
|
1659
|
+
if (error instanceof Error && "code" in error && error.code === "EADDRINUSE") return probePort(0);
|
|
1660
|
+
throw error;
|
|
1661
|
+
}
|
|
1609
1662
|
}
|
|
1610
1663
|
//#endregion
|
|
1611
1664
|
//#region src/server/Negotiator.ts
|
|
@@ -1744,10 +1797,10 @@ var Server = class {
|
|
|
1744
1797
|
#timeouts;
|
|
1745
1798
|
#http;
|
|
1746
1799
|
#abort = createAbort();
|
|
1800
|
+
#wakeup = createAbort();
|
|
1747
1801
|
#status = "idle";
|
|
1748
1802
|
#port;
|
|
1749
1803
|
#pending = 0;
|
|
1750
|
-
#waiters = [];
|
|
1751
1804
|
constructor(options) {
|
|
1752
1805
|
if (!isFunction(options.state)) throw new TypeError("ServerOptions.state must be a function");
|
|
1753
1806
|
if (options.report !== void 0 && !isFunction(options.report)) throw new TypeError("ServerOptions.report must be a function");
|
|
@@ -1769,8 +1822,8 @@ var Server = class {
|
|
|
1769
1822
|
this.#report = options.report;
|
|
1770
1823
|
this.#timeouts = timeouts;
|
|
1771
1824
|
this.#emitter = new Emitter({
|
|
1772
|
-
on: options.on,
|
|
1773
|
-
error: options.error
|
|
1825
|
+
...options.on === void 0 ? {} : { on: options.on },
|
|
1826
|
+
...options.error === void 0 ? {} : { error: options.error }
|
|
1774
1827
|
});
|
|
1775
1828
|
}
|
|
1776
1829
|
get status() {
|
|
@@ -1802,25 +1855,7 @@ var Server = class {
|
|
|
1802
1855
|
if (this.#timeouts.keepalive !== void 0) server.keepAliveTimeout = this.#timeouts.keepalive;
|
|
1803
1856
|
server.on("upgrade", (request, socket, head) => this.#onUpgrade(request, socket, head));
|
|
1804
1857
|
this.#http = server;
|
|
1805
|
-
return
|
|
1806
|
-
const onError = (error) => {
|
|
1807
|
-
server.off("listening", onListening);
|
|
1808
|
-
this.#http = void 0;
|
|
1809
|
-
this.#status = "idle";
|
|
1810
|
-
reject(error);
|
|
1811
|
-
};
|
|
1812
|
-
const onListening = () => {
|
|
1813
|
-
server.off("error", onError);
|
|
1814
|
-
const port = this.#resolvePort(server);
|
|
1815
|
-
this.#port = port;
|
|
1816
|
-
this.#status = "listening";
|
|
1817
|
-
this.#emitter.emit("start", port);
|
|
1818
|
-
resolve(port);
|
|
1819
|
-
};
|
|
1820
|
-
server.once("error", onError);
|
|
1821
|
-
server.once("listening", onListening);
|
|
1822
|
-
server.listen(this.#configuredPort ?? 0, this.#host);
|
|
1823
|
-
});
|
|
1858
|
+
return this.#listen(server);
|
|
1824
1859
|
}
|
|
1825
1860
|
async stop() {
|
|
1826
1861
|
if (this.#status !== "listening") return;
|
|
@@ -1873,19 +1908,16 @@ var Server = class {
|
|
|
1873
1908
|
const linked = linkSignal(raw.signal, this.#abort.signal);
|
|
1874
1909
|
const request = new Request(raw, { signal: linked });
|
|
1875
1910
|
this.#emitter.emit("request", method, url.pathname);
|
|
1911
|
+
const ip = message.socket.remoteAddress;
|
|
1876
1912
|
const connection = {
|
|
1877
|
-
ip:
|
|
1913
|
+
...ip === void 0 ? {} : { ip },
|
|
1878
1914
|
encrypted: isEncryptedSocket(message.socket)
|
|
1879
1915
|
};
|
|
1880
|
-
let cached;
|
|
1881
1916
|
const context = {
|
|
1882
1917
|
url,
|
|
1883
1918
|
method,
|
|
1884
1919
|
state: this.#state(connection),
|
|
1885
|
-
body: ()
|
|
1886
|
-
cached ??= readBody(request, { limit: this.#limit });
|
|
1887
|
-
return cached;
|
|
1888
|
-
}
|
|
1920
|
+
body: this.#createBody(request)
|
|
1889
1921
|
};
|
|
1890
1922
|
const result = await compose(this.#middleware, (currentRequest, currentContext) => this.#dispatcher.handle(currentRequest, currentContext.state))(request, context);
|
|
1891
1923
|
await this.#respond(result, response);
|
|
@@ -1948,6 +1980,29 @@ var Server = class {
|
|
|
1948
1980
|
if (isAddressInfo(address)) return address.port;
|
|
1949
1981
|
return 0;
|
|
1950
1982
|
}
|
|
1983
|
+
async #listen(server) {
|
|
1984
|
+
try {
|
|
1985
|
+
const listening = once(server, "listening");
|
|
1986
|
+
server.listen(this.#configuredPort ?? 0, this.#host);
|
|
1987
|
+
await listening;
|
|
1988
|
+
} catch (error) {
|
|
1989
|
+
this.#http = void 0;
|
|
1990
|
+
this.#status = "idle";
|
|
1991
|
+
throw error;
|
|
1992
|
+
}
|
|
1993
|
+
const port = this.#resolvePort(server);
|
|
1994
|
+
this.#port = port;
|
|
1995
|
+
this.#status = "listening";
|
|
1996
|
+
this.#emitter.emit("start", port);
|
|
1997
|
+
return port;
|
|
1998
|
+
}
|
|
1999
|
+
#createBody(request) {
|
|
2000
|
+
let cached;
|
|
2001
|
+
return () => {
|
|
2002
|
+
cached ??= readBody(request, { limit: this.#limit });
|
|
2003
|
+
return cached;
|
|
2004
|
+
};
|
|
2005
|
+
}
|
|
1951
2006
|
#close(server, force) {
|
|
1952
2007
|
return new Promise((resolve) => {
|
|
1953
2008
|
server.close(() => resolve());
|
|
@@ -1956,29 +2011,19 @@ var Server = class {
|
|
|
1956
2011
|
});
|
|
1957
2012
|
}
|
|
1958
2013
|
#trackStart() {
|
|
2014
|
+
if (this.#pending === 0) this.#wakeup = createAbort();
|
|
1959
2015
|
this.#pending += 1;
|
|
1960
2016
|
let finished = false;
|
|
1961
2017
|
return () => {
|
|
1962
2018
|
if (finished) return;
|
|
1963
2019
|
finished = true;
|
|
1964
2020
|
this.#pending -= 1;
|
|
1965
|
-
if (this.#pending === 0)
|
|
1966
|
-
const waiters = this.#waiters;
|
|
1967
|
-
this.#waiters = [];
|
|
1968
|
-
for (const wake of waiters) wake();
|
|
1969
|
-
}
|
|
2021
|
+
if (this.#pending === 0) this.#wakeup.abort();
|
|
1970
2022
|
};
|
|
1971
2023
|
}
|
|
1972
|
-
#drainPending(signal) {
|
|
1973
|
-
if (this.#pending === 0 || signal.aborted) return
|
|
1974
|
-
|
|
1975
|
-
const onDone = () => {
|
|
1976
|
-
signal.removeEventListener("abort", onDone);
|
|
1977
|
-
resolve();
|
|
1978
|
-
};
|
|
1979
|
-
this.#waiters.push(onDone);
|
|
1980
|
-
signal.addEventListener("abort", onDone, { once: true });
|
|
1981
|
-
});
|
|
2024
|
+
async #drainPending(signal) {
|
|
2025
|
+
if (this.#pending === 0 || signal.aborted) return;
|
|
2026
|
+
await once(AbortSignal.any([signal, this.#wakeup.signal]), "abort");
|
|
1982
2027
|
}
|
|
1983
2028
|
};
|
|
1984
2029
|
//#endregion
|
|
@@ -2033,6 +2078,6 @@ function createServer(options) {
|
|
|
2033
2078
|
return new Server(options);
|
|
2034
2079
|
}
|
|
2035
2080
|
//#endregion
|
|
2036
|
-
export { COMPRESSIBLE_TYPES, ContentTooLargeError, DEFAULT_BODY_LIMIT, DEFAULT_DECOMPRESSED_LIMIT, DEFAULT_DRAIN_MS, DEFAULT_ENCODINGS, HTTPError, HTTP_ERROR_BRAND, Negotiator, REQUEST_ID_PATTERN, SSE_HEADERS, Server, appendCookie, clearCookie, clientRateKey, codingQuality, collectRequestBody, compose, computeBodyETag, createNegotiator, createServer, decodeBase64Url, decodeCookieValue, decodeTokenPayload, decompressRequestBody, discoverPort, encodeBase64Url, ipv6Network, isAddressInfo, isCompressibleType, isCookieAttribute, isCookieName, isDangerousKey, isHTTPError, isValidRequestId, languageQuality, matchMediaType, matchesETag, mergeVary, negotiateEncoding, normalizeSecret, openStream, parseAcceptHeader, parseCookies, parseRange, readBody, readSignedCookie, requestEncoding, resolveOrigin, resolveSecure, resolveSecurityHeader, scrubPrototype, serializeCookie, serializeEvent, signToken, unwrapETag, verifyToken, writeSignedCookie };
|
|
2081
|
+
export { COMPRESSIBLE_TYPES, ContentTooLargeError, DEFAULT_BODY_LIMIT, DEFAULT_DECOMPRESSED_LIMIT, DEFAULT_DRAIN_MS, DEFAULT_ENCODINGS, HTTPError, HTTP_ERROR_BRAND, Negotiator, REQUEST_ID_PATTERN, SSE_HEADERS, Server, appendCookie, clearCookie, clientRateKey, codingQuality, collectRequestBody, compose, computeBodyETag, createNegotiator, createServer, decodeBase64Url, decodeCookieValue, decodeTokenPayload, decompressRequestBody, discoverPort, encodeBase64Url, enqueueStreamText, ipv6Network, isAddressInfo, isCompressibleType, isCookieAttribute, isCookieName, isDangerousKey, isHTTPError, isValidRequestId, languageQuality, matchMediaType, matchesETag, mergeVary, negotiateEncoding, normalizeSecret, openStream, parseAcceptHeader, parseCookies, parseRange, probePort, readBody, readSignedCookie, requestEncoding, resolveOrigin, resolveSecure, resolveSecurityHeader, scrubPrototype, serializeCookie, serializeEvent, signToken, unwrapETag, verifyToken, wrapMiddleware, writeSignedCookie };
|
|
2037
2082
|
|
|
2038
2083
|
//# sourceMappingURL=index.js.map
|