@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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _orkestrel_contract = require("@orkestrel/contract");
|
|
3
|
+
let node_events = require("node:events");
|
|
3
4
|
let node_net = require("node:net");
|
|
4
5
|
let node_http = require("node:http");
|
|
5
6
|
let _orkestrel_abort = require("@orkestrel/abort");
|
|
@@ -141,7 +142,7 @@ var HTTPError = class extends Error {
|
|
|
141
142
|
super(message);
|
|
142
143
|
this.name = "HTTPError";
|
|
143
144
|
this.status = status;
|
|
144
|
-
this.context = context;
|
|
145
|
+
if (context !== void 0) this.context = context;
|
|
145
146
|
}
|
|
146
147
|
};
|
|
147
148
|
/**
|
|
@@ -241,19 +242,36 @@ function isHTTPError(value) {
|
|
|
241
242
|
* ```
|
|
242
243
|
*/
|
|
243
244
|
function compose(middleware, terminal) {
|
|
245
|
+
return middleware.reduceRight((next, layer) => wrapMiddleware(layer, next), terminal);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Wrap one middleware layer around its downstream handler.
|
|
249
|
+
*
|
|
250
|
+
* @remarks
|
|
251
|
+
* The returned handler enforces the one-call `next` invariant while preserving
|
|
252
|
+
* request substitution and the middleware's synchronous or asynchronous result.
|
|
253
|
+
*
|
|
254
|
+
* @typeParam TState - The consumer's opaque per-request state type
|
|
255
|
+
* @param layer - The middleware layer to invoke
|
|
256
|
+
* @param next - The downstream handler wrapped by `layer`
|
|
257
|
+
* @returns A handler with `layer` composed around `next`
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* const handle = wrapMiddleware(
|
|
262
|
+
* async (_request, _context, next) => next(),
|
|
263
|
+
* async () => new Response('ok'),
|
|
264
|
+
* )
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
function wrapMiddleware(layer, next) {
|
|
244
268
|
return async (request, context) => {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
if (
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
called = true;
|
|
252
|
-
return dispatch(index + 1, nextRequest ?? currentRequest);
|
|
253
|
-
};
|
|
254
|
-
return Promise.resolve(layer(currentRequest, context, next));
|
|
255
|
-
};
|
|
256
|
-
return dispatch(0, request);
|
|
269
|
+
let called = false;
|
|
270
|
+
return Promise.resolve(layer(request, context, (nextRequest) => {
|
|
271
|
+
if (called) return Promise.reject(/* @__PURE__ */ new Error("next() was already called by this middleware"));
|
|
272
|
+
called = true;
|
|
273
|
+
return next(nextRequest ?? request, context);
|
|
274
|
+
}));
|
|
257
275
|
};
|
|
258
276
|
}
|
|
259
277
|
/**
|
|
@@ -1027,7 +1045,9 @@ function parseRange(header, size) {
|
|
|
1027
1045
|
if (header === void 0) return void 0;
|
|
1028
1046
|
const match = /^bytes=(.*)$/.exec(header.trim());
|
|
1029
1047
|
if (match === null) return void 0;
|
|
1030
|
-
const
|
|
1048
|
+
const capture = match[1];
|
|
1049
|
+
if (capture === void 0) return void 0;
|
|
1050
|
+
const spec = capture.trim();
|
|
1031
1051
|
if (spec.includes(",")) return void 0;
|
|
1032
1052
|
const dash = spec.indexOf("-");
|
|
1033
1053
|
if (dash < 0) return void 0;
|
|
@@ -1183,8 +1203,12 @@ function ipv6Network(address) {
|
|
|
1183
1203
|
const percent = address.indexOf("%");
|
|
1184
1204
|
const halves = (percent === -1 ? address : address.slice(0, percent)).split("::");
|
|
1185
1205
|
if (halves.length > 2) return void 0;
|
|
1186
|
-
const
|
|
1187
|
-
|
|
1206
|
+
const first = halves[0];
|
|
1207
|
+
if (first === void 0) return void 0;
|
|
1208
|
+
const second = halves[1];
|
|
1209
|
+
if (halves.length === 2 && second === void 0) return void 0;
|
|
1210
|
+
const head = first === "" ? [] : first.split(":");
|
|
1211
|
+
const tail = second === void 0 || second === "" ? [] : second.split(":");
|
|
1188
1212
|
let groups;
|
|
1189
1213
|
if (halves.length === 2) {
|
|
1190
1214
|
const missing = 8 - head.length - tail.length;
|
|
@@ -1254,6 +1278,28 @@ function serializeEvent(message) {
|
|
|
1254
1278
|
return `${lines.join("\n")}\n\n`;
|
|
1255
1279
|
}
|
|
1256
1280
|
/**
|
|
1281
|
+
* Enqueue encoded text into an open byte stream.
|
|
1282
|
+
*
|
|
1283
|
+
* @param controller - The stream controller, when the stream has started
|
|
1284
|
+
* @param encoder - The encoder used for the stream's byte representation
|
|
1285
|
+
* @param closed - Whether the stream has already ended or been cancelled
|
|
1286
|
+
* @param text - The text to encode and enqueue
|
|
1287
|
+
* @returns Nothing
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* ```ts
|
|
1291
|
+
* const body = new ReadableStream<Uint8Array>({
|
|
1292
|
+
* start(controller) {
|
|
1293
|
+
* enqueueStreamText(controller, new TextEncoder(), false, 'hello')
|
|
1294
|
+
* },
|
|
1295
|
+
* })
|
|
1296
|
+
* ```
|
|
1297
|
+
*/
|
|
1298
|
+
function enqueueStreamText(controller, encoder, closed, text) {
|
|
1299
|
+
if (closed || controller === void 0) return;
|
|
1300
|
+
controller.enqueue(encoder.encode(text));
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1257
1303
|
* Open a generic Server-Sent-Events stream — a fetch-standard `Response`
|
|
1258
1304
|
* whose body is a `ReadableStream` a caller writes {@link SSEMessage}s into.
|
|
1259
1305
|
*
|
|
@@ -1294,24 +1340,19 @@ function openStream(options) {
|
|
|
1294
1340
|
...SSE_HEADERS,
|
|
1295
1341
|
...options?.headers
|
|
1296
1342
|
});
|
|
1297
|
-
const response = new Response(body, {
|
|
1298
|
-
status: options?.status ?? 200,
|
|
1299
|
-
headers
|
|
1300
|
-
});
|
|
1301
|
-
const enqueue = (text) => {
|
|
1302
|
-
if (closed || controller === void 0) return;
|
|
1303
|
-
controller.enqueue(encoder.encode(text));
|
|
1304
|
-
};
|
|
1305
1343
|
return {
|
|
1306
|
-
response,
|
|
1344
|
+
response: new Response(body, {
|
|
1345
|
+
status: options?.status ?? 200,
|
|
1346
|
+
headers
|
|
1347
|
+
}),
|
|
1307
1348
|
get closed() {
|
|
1308
1349
|
return closed;
|
|
1309
1350
|
},
|
|
1310
1351
|
write(message) {
|
|
1311
|
-
|
|
1352
|
+
enqueueStreamText(controller, encoder, closed, serializeEvent(message));
|
|
1312
1353
|
},
|
|
1313
1354
|
comment(text) {
|
|
1314
|
-
|
|
1355
|
+
enqueueStreamText(controller, encoder, closed, `: ${text}\n\n`);
|
|
1315
1356
|
},
|
|
1316
1357
|
end() {
|
|
1317
1358
|
if (closed) return;
|
|
@@ -1564,6 +1605,29 @@ function isAddressInfo(value) {
|
|
|
1564
1605
|
return (0, _orkestrel_contract.isRecord)(value) && (0, _orkestrel_contract.isNumber)(value.port);
|
|
1565
1606
|
}
|
|
1566
1607
|
/**
|
|
1608
|
+
* Bind and close a throwaway TCP server to resolve one available port.
|
|
1609
|
+
*
|
|
1610
|
+
* @param port - The requested port, with `0` selecting an ephemeral port
|
|
1611
|
+
* @returns The bound port after the probe server has closed
|
|
1612
|
+
*
|
|
1613
|
+
* @example
|
|
1614
|
+
* ```ts
|
|
1615
|
+
* const port = await probePort(0)
|
|
1616
|
+
* ```
|
|
1617
|
+
*/
|
|
1618
|
+
async function probePort(port) {
|
|
1619
|
+
const probe = (0, node_net.createServer)();
|
|
1620
|
+
const listening = (0, node_events.once)(probe, "listening");
|
|
1621
|
+
probe.listen(port);
|
|
1622
|
+
await listening;
|
|
1623
|
+
const address = probe.address();
|
|
1624
|
+
const resolved = isAddressInfo(address) ? address.port : 0;
|
|
1625
|
+
const closed = (0, node_events.once)(probe, "close");
|
|
1626
|
+
probe.close();
|
|
1627
|
+
await closed;
|
|
1628
|
+
return resolved;
|
|
1629
|
+
}
|
|
1630
|
+
/**
|
|
1567
1631
|
* Find a FREE TCP port — bind a throwaway `node:net` server, read the
|
|
1568
1632
|
* OS-assigned port, close it, and resolve that port.
|
|
1569
1633
|
*
|
|
@@ -1588,25 +1652,14 @@ function isAddressInfo(value) {
|
|
|
1588
1652
|
* const port = await discoverPort() // a guaranteed-free ephemeral port
|
|
1589
1653
|
* ```
|
|
1590
1654
|
*/
|
|
1591
|
-
function discoverPort(preferred) {
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
else reject(error);
|
|
1600
|
-
};
|
|
1601
|
-
probe.once("error", onError);
|
|
1602
|
-
probe.listen(port, () => {
|
|
1603
|
-
const address = probe.address();
|
|
1604
|
-
const resolved = isAddressInfo(address) ? address.port : 0;
|
|
1605
|
-
probe.close(() => resolve(resolved));
|
|
1606
|
-
});
|
|
1607
|
-
};
|
|
1608
|
-
bind(preferred ?? 0, preferred !== void 0);
|
|
1609
|
-
});
|
|
1655
|
+
async function discoverPort(preferred) {
|
|
1656
|
+
if (preferred === void 0) return probePort(0);
|
|
1657
|
+
try {
|
|
1658
|
+
return await probePort(preferred);
|
|
1659
|
+
} catch (error) {
|
|
1660
|
+
if (error instanceof Error && "code" in error && error.code === "EADDRINUSE") return probePort(0);
|
|
1661
|
+
throw error;
|
|
1662
|
+
}
|
|
1610
1663
|
}
|
|
1611
1664
|
//#endregion
|
|
1612
1665
|
//#region src/server/Negotiator.ts
|
|
@@ -1745,10 +1798,10 @@ var Server = class {
|
|
|
1745
1798
|
#timeouts;
|
|
1746
1799
|
#http;
|
|
1747
1800
|
#abort = (0, _orkestrel_abort.createAbort)();
|
|
1801
|
+
#wakeup = (0, _orkestrel_abort.createAbort)();
|
|
1748
1802
|
#status = "idle";
|
|
1749
1803
|
#port;
|
|
1750
1804
|
#pending = 0;
|
|
1751
|
-
#waiters = [];
|
|
1752
1805
|
constructor(options) {
|
|
1753
1806
|
if (!(0, _orkestrel_contract.isFunction)(options.state)) throw new TypeError("ServerOptions.state must be a function");
|
|
1754
1807
|
if (options.report !== void 0 && !(0, _orkestrel_contract.isFunction)(options.report)) throw new TypeError("ServerOptions.report must be a function");
|
|
@@ -1770,8 +1823,8 @@ var Server = class {
|
|
|
1770
1823
|
this.#report = options.report;
|
|
1771
1824
|
this.#timeouts = timeouts;
|
|
1772
1825
|
this.#emitter = new _orkestrel_emitter.Emitter({
|
|
1773
|
-
on: options.on,
|
|
1774
|
-
error: options.error
|
|
1826
|
+
...options.on === void 0 ? {} : { on: options.on },
|
|
1827
|
+
...options.error === void 0 ? {} : { error: options.error }
|
|
1775
1828
|
});
|
|
1776
1829
|
}
|
|
1777
1830
|
get status() {
|
|
@@ -1803,25 +1856,7 @@ var Server = class {
|
|
|
1803
1856
|
if (this.#timeouts.keepalive !== void 0) server.keepAliveTimeout = this.#timeouts.keepalive;
|
|
1804
1857
|
server.on("upgrade", (request, socket, head) => this.#onUpgrade(request, socket, head));
|
|
1805
1858
|
this.#http = server;
|
|
1806
|
-
return
|
|
1807
|
-
const onError = (error) => {
|
|
1808
|
-
server.off("listening", onListening);
|
|
1809
|
-
this.#http = void 0;
|
|
1810
|
-
this.#status = "idle";
|
|
1811
|
-
reject(error);
|
|
1812
|
-
};
|
|
1813
|
-
const onListening = () => {
|
|
1814
|
-
server.off("error", onError);
|
|
1815
|
-
const port = this.#resolvePort(server);
|
|
1816
|
-
this.#port = port;
|
|
1817
|
-
this.#status = "listening";
|
|
1818
|
-
this.#emitter.emit("start", port);
|
|
1819
|
-
resolve(port);
|
|
1820
|
-
};
|
|
1821
|
-
server.once("error", onError);
|
|
1822
|
-
server.once("listening", onListening);
|
|
1823
|
-
server.listen(this.#configuredPort ?? 0, this.#host);
|
|
1824
|
-
});
|
|
1859
|
+
return this.#listen(server);
|
|
1825
1860
|
}
|
|
1826
1861
|
async stop() {
|
|
1827
1862
|
if (this.#status !== "listening") return;
|
|
@@ -1874,19 +1909,16 @@ var Server = class {
|
|
|
1874
1909
|
const linked = (0, _orkestrel_abort.linkSignal)(raw.signal, this.#abort.signal);
|
|
1875
1910
|
const request = new Request(raw, { signal: linked });
|
|
1876
1911
|
this.#emitter.emit("request", method, url.pathname);
|
|
1912
|
+
const ip = message.socket.remoteAddress;
|
|
1877
1913
|
const connection = {
|
|
1878
|
-
ip:
|
|
1914
|
+
...ip === void 0 ? {} : { ip },
|
|
1879
1915
|
encrypted: (0, _orkestrel_router_server.isEncryptedSocket)(message.socket)
|
|
1880
1916
|
};
|
|
1881
|
-
let cached;
|
|
1882
1917
|
const context = {
|
|
1883
1918
|
url,
|
|
1884
1919
|
method,
|
|
1885
1920
|
state: this.#state(connection),
|
|
1886
|
-
body: ()
|
|
1887
|
-
cached ??= readBody(request, { limit: this.#limit });
|
|
1888
|
-
return cached;
|
|
1889
|
-
}
|
|
1921
|
+
body: this.#createBody(request)
|
|
1890
1922
|
};
|
|
1891
1923
|
const result = await compose(this.#middleware, (currentRequest, currentContext) => this.#dispatcher.handle(currentRequest, currentContext.state))(request, context);
|
|
1892
1924
|
await this.#respond(result, response);
|
|
@@ -1949,6 +1981,29 @@ var Server = class {
|
|
|
1949
1981
|
if (isAddressInfo(address)) return address.port;
|
|
1950
1982
|
return 0;
|
|
1951
1983
|
}
|
|
1984
|
+
async #listen(server) {
|
|
1985
|
+
try {
|
|
1986
|
+
const listening = (0, node_events.once)(server, "listening");
|
|
1987
|
+
server.listen(this.#configuredPort ?? 0, this.#host);
|
|
1988
|
+
await listening;
|
|
1989
|
+
} catch (error) {
|
|
1990
|
+
this.#http = void 0;
|
|
1991
|
+
this.#status = "idle";
|
|
1992
|
+
throw error;
|
|
1993
|
+
}
|
|
1994
|
+
const port = this.#resolvePort(server);
|
|
1995
|
+
this.#port = port;
|
|
1996
|
+
this.#status = "listening";
|
|
1997
|
+
this.#emitter.emit("start", port);
|
|
1998
|
+
return port;
|
|
1999
|
+
}
|
|
2000
|
+
#createBody(request) {
|
|
2001
|
+
let cached;
|
|
2002
|
+
return () => {
|
|
2003
|
+
cached ??= readBody(request, { limit: this.#limit });
|
|
2004
|
+
return cached;
|
|
2005
|
+
};
|
|
2006
|
+
}
|
|
1952
2007
|
#close(server, force) {
|
|
1953
2008
|
return new Promise((resolve) => {
|
|
1954
2009
|
server.close(() => resolve());
|
|
@@ -1957,29 +2012,19 @@ var Server = class {
|
|
|
1957
2012
|
});
|
|
1958
2013
|
}
|
|
1959
2014
|
#trackStart() {
|
|
2015
|
+
if (this.#pending === 0) this.#wakeup = (0, _orkestrel_abort.createAbort)();
|
|
1960
2016
|
this.#pending += 1;
|
|
1961
2017
|
let finished = false;
|
|
1962
2018
|
return () => {
|
|
1963
2019
|
if (finished) return;
|
|
1964
2020
|
finished = true;
|
|
1965
2021
|
this.#pending -= 1;
|
|
1966
|
-
if (this.#pending === 0)
|
|
1967
|
-
const waiters = this.#waiters;
|
|
1968
|
-
this.#waiters = [];
|
|
1969
|
-
for (const wake of waiters) wake();
|
|
1970
|
-
}
|
|
2022
|
+
if (this.#pending === 0) this.#wakeup.abort();
|
|
1971
2023
|
};
|
|
1972
2024
|
}
|
|
1973
|
-
#drainPending(signal) {
|
|
1974
|
-
if (this.#pending === 0 || signal.aborted) return
|
|
1975
|
-
|
|
1976
|
-
const onDone = () => {
|
|
1977
|
-
signal.removeEventListener("abort", onDone);
|
|
1978
|
-
resolve();
|
|
1979
|
-
};
|
|
1980
|
-
this.#waiters.push(onDone);
|
|
1981
|
-
signal.addEventListener("abort", onDone, { once: true });
|
|
1982
|
-
});
|
|
2025
|
+
async #drainPending(signal) {
|
|
2026
|
+
if (this.#pending === 0 || signal.aborted) return;
|
|
2027
|
+
await (0, node_events.once)(AbortSignal.any([signal, this.#wakeup.signal]), "abort");
|
|
1983
2028
|
}
|
|
1984
2029
|
};
|
|
1985
2030
|
//#endregion
|
|
@@ -2061,6 +2106,7 @@ exports.decodeTokenPayload = decodeTokenPayload;
|
|
|
2061
2106
|
exports.decompressRequestBody = decompressRequestBody;
|
|
2062
2107
|
exports.discoverPort = discoverPort;
|
|
2063
2108
|
exports.encodeBase64Url = encodeBase64Url;
|
|
2109
|
+
exports.enqueueStreamText = enqueueStreamText;
|
|
2064
2110
|
exports.ipv6Network = ipv6Network;
|
|
2065
2111
|
exports.isAddressInfo = isAddressInfo;
|
|
2066
2112
|
exports.isCompressibleType = isCompressibleType;
|
|
@@ -2079,6 +2125,7 @@ exports.openStream = openStream;
|
|
|
2079
2125
|
exports.parseAcceptHeader = parseAcceptHeader;
|
|
2080
2126
|
exports.parseCookies = parseCookies;
|
|
2081
2127
|
exports.parseRange = parseRange;
|
|
2128
|
+
exports.probePort = probePort;
|
|
2082
2129
|
exports.readBody = readBody;
|
|
2083
2130
|
exports.readSignedCookie = readSignedCookie;
|
|
2084
2131
|
exports.requestEncoding = requestEncoding;
|
|
@@ -2091,6 +2138,7 @@ exports.serializeEvent = serializeEvent;
|
|
|
2091
2138
|
exports.signToken = signToken;
|
|
2092
2139
|
exports.unwrapETag = unwrapETag;
|
|
2093
2140
|
exports.verifyToken = verifyToken;
|
|
2141
|
+
exports.wrapMiddleware = wrapMiddleware;
|
|
2094
2142
|
exports.writeSignedCookie = writeSignedCookie;
|
|
2095
2143
|
|
|
2096
2144
|
//# sourceMappingURL=index.cjs.map
|