@ultimabase/http 0.1.2 → 0.3.0-canary.20261031104207
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 +24 -0
- package/dist/app.d.ts +6 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/index.d.ts +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -9
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +1 -1
- package/dist/upgrade.d.ts +37 -5
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +58 -24
- package/package.json +3 -3
- package/src/app.ts +6 -0
- package/src/index.ts +36 -15
- package/src/server.ts +2 -2
- package/src/upgrade.ts +91 -30
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @ultimabase/http
|
|
2
2
|
|
|
3
|
+
## 0.3.0-canary.20261031104207
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 2eb7b9d: `httpPlugin({ upgrade })` refuses WebSocket upgrades with a response, as
|
|
8
|
+
during maintenance, and `closeUpgrades({ code, reason })` closes the sockets
|
|
9
|
+
open: GraphQL's with that code, those of a plugin without `close` cut (D-166).
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [41cfff6]
|
|
14
|
+
- Updated dependencies [6e8ebf6]
|
|
15
|
+
- Updated dependencies [9cd2dd4]
|
|
16
|
+
- Updated dependencies [353c2bf]
|
|
17
|
+
- ultimabase@0.3.0-canary.20261031104207
|
|
18
|
+
|
|
19
|
+
## 0.2.0
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- Updated dependencies [2c3bf4e]
|
|
24
|
+
- Updated dependencies [fd1263e]
|
|
25
|
+
- ultimabase@0.2.0
|
|
26
|
+
|
|
3
27
|
## 0.1.2
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
package/dist/app.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Hono } from 'hono';
|
|
2
2
|
import type { Caller, Logger } from 'ultimabase';
|
|
3
|
+
import type { Gate } from './upgrade.ts';
|
|
3
4
|
/**
|
|
4
5
|
* What a route's context holds: `c.var.caller` (D-106) and `c.var.address`,
|
|
5
6
|
* the client's, `''` when unknown (D-113, D-116).
|
|
@@ -29,6 +30,11 @@ export interface HttpOptions {
|
|
|
29
30
|
readonly bodyLimit?: number;
|
|
30
31
|
/** In front of the server, which then reads the client's address from `X-Forwarded-For`; 0 by default (D-116). */
|
|
31
32
|
readonly proxies?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Answers each WebSocket upgrade before the plugin that takes it: a
|
|
35
|
+
* response refuses it, as during maintenance (D-166).
|
|
36
|
+
*/
|
|
37
|
+
readonly upgrade?: Gate;
|
|
32
38
|
}
|
|
33
39
|
/**
|
|
34
40
|
* Whether a browser may call from `origin`, with its cookies: a listed one,
|
package/dist/app.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAM3B,OAAO,KAAK,EAAE,MAAM,EAAa,MAAM,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAM3B,OAAO,KAAK,EAAE,MAAM,EAAa,MAAM,EAAE,MAAM,YAAY,CAAA;AAI3D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAExC;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG;IAAE,SAAS,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAAA;AAExE,uEAAuE;AACvE,MAAM,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,CAAA;AAElC,yFAAyF;AACzF,MAAM,WAAW,OAAO;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAA;CAC3B;AAED,6CAA6C;AAC7C,MAAM,WAAW,WAAW;IAC3B,2CAA2C;IAC3C,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IACnC,0EAA0E;IAC1E,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IACjC,8BAA8B;IAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;IACtB,iCAAiC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,kHAAkH;IAClH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;IACzB;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,IAAI,CAAA;CACvB;AAkED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAI1E;AAgBD;;;GAGG;AACH,wBAAgB,KAAK,CACpB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,SAAS,OAAO,EAAE,EAC3B,QAAQ,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,MAAM,CAAC,EAC/C,MAAM,EAAE,MAAM,GACZ,IAAI,CAAC,OAAO,CAAC,CAuHf"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { PluginOf, PluginTypes } from 'ultimabase';
|
|
2
2
|
import type { HttpOptions, Routes } from './app.ts';
|
|
3
|
-
import type { Accept } from './upgrade.ts';
|
|
3
|
+
import type { Accept, Close, Closing } from './upgrade.ts';
|
|
4
4
|
export type { HttpEnv, HttpOptions, Routes } from './app.ts';
|
|
5
|
-
export type { Accept, Upgrade } from './upgrade.ts';
|
|
5
|
+
export type { Accept, Close, Closing, Gate, Upgrade } from './upgrade.ts';
|
|
6
6
|
export { clientOf } from './client.ts';
|
|
7
7
|
export type { StandardSchema } from './validate.ts';
|
|
8
8
|
export { validate } from './validate.ts';
|
|
@@ -17,9 +17,17 @@ export interface HttpApi {
|
|
|
17
17
|
}): void;
|
|
18
18
|
/**
|
|
19
19
|
* Takes the WebSocket upgrades of `path`, once their origin is one CORS
|
|
20
|
-
* allows; call it from your
|
|
20
|
+
* allows and the `upgrade` gate let them through; call it from your
|
|
21
|
+
* `api` (D-154). `close` closes its sockets with a code (D-166).
|
|
21
22
|
*/
|
|
22
|
-
upgrade(path: string, accept: Accept
|
|
23
|
+
upgrade(path: string, accept: Accept, options?: {
|
|
24
|
+
readonly close?: Close;
|
|
25
|
+
}): void;
|
|
26
|
+
/**
|
|
27
|
+
* Closes the WebSockets open on every path, or on `path`, with `code`:
|
|
28
|
+
* those of a plugin that gave no `close` are cut (D-166).
|
|
29
|
+
*/
|
|
30
|
+
closeUpgrades(closing: Closing): void;
|
|
23
31
|
/** Where the server listens, `http://localhost:3000`, once it started. */
|
|
24
32
|
url(): string;
|
|
25
33
|
}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAU,QAAQ,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAG/D,OAAO,KAAK,EAAW,WAAW,EAAW,MAAM,EAAE,MAAM,UAAU,CAAA;AAKrE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAU,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AAe,CAAA;AAIrD;;;GAGG;AACH,eAAO,MAAM,MAAM,QAAO,MAIvB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { addresses, proxiesOf } from './address.js';
|
|
|
5
5
|
import { allowing, appOf } from './app.js';
|
|
6
6
|
import { hold } from './request.js';
|
|
7
7
|
import { closed, listening, portOf, serverOf } from './server.js';
|
|
8
|
-
import { upgrading } from './upgrade.js';
|
|
8
|
+
import { closeUpgrades, upgrading } from './upgrade.js';
|
|
9
9
|
export { clientOf } from './client.js';
|
|
10
10
|
export { validate } from './validate.js';
|
|
11
11
|
// Each running application's server and mounted routes, by the context its
|
|
@@ -20,9 +20,9 @@ const http = definePlugin({
|
|
|
20
20
|
manifest,
|
|
21
21
|
api: (context) => {
|
|
22
22
|
const mounted = [];
|
|
23
|
-
const
|
|
23
|
+
const takers = new Map();
|
|
24
24
|
mounts.set(context, mounted);
|
|
25
|
-
upgrades.set(context,
|
|
25
|
+
upgrades.set(context, takers);
|
|
26
26
|
const building = (what) => {
|
|
27
27
|
if (mounts.has(context))
|
|
28
28
|
return;
|
|
@@ -33,12 +33,15 @@ const http = definePlugin({
|
|
|
33
33
|
building('mount routes');
|
|
34
34
|
mounted.push({ routes, bodyLimit: mountOptions?.bodyLimit ?? true });
|
|
35
35
|
},
|
|
36
|
-
upgrade: (path, accept) => {
|
|
36
|
+
upgrade: (path, accept, upgradeOptions) => {
|
|
37
37
|
building('take upgrades');
|
|
38
|
-
if (
|
|
38
|
+
if (takers.has(path)) {
|
|
39
39
|
throw new Error(`Two plugins take the upgrades of ${path}.`);
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
takers.set(path, { accept, close: upgradeOptions?.close });
|
|
42
|
+
},
|
|
43
|
+
closeUpgrades: (closing) => {
|
|
44
|
+
closeUpgrades(takers, taken.get(context), closing);
|
|
42
45
|
},
|
|
43
46
|
url: () => {
|
|
44
47
|
const url = urls.get(context);
|
|
@@ -55,9 +58,15 @@ const http = definePlugin({
|
|
|
55
58
|
const app = appOf(options, mounts.get(context) ?? [], identify, logger);
|
|
56
59
|
mounts.delete(context);
|
|
57
60
|
const server = serverOf(app.fetch);
|
|
58
|
-
const
|
|
61
|
+
const takers = upgrades.get(context) ?? new Map();
|
|
59
62
|
const addressOf = addresses(proxiesOf(options.proxies), logger);
|
|
60
|
-
taken.set(context, upgrading(server,
|
|
63
|
+
taken.set(context, upgrading(server, {
|
|
64
|
+
takers,
|
|
65
|
+
gate: options.upgrade,
|
|
66
|
+
allows: allowing(options),
|
|
67
|
+
addressOf,
|
|
68
|
+
logger,
|
|
69
|
+
}));
|
|
61
70
|
const bound = await listening(server, port);
|
|
62
71
|
servers.set(context, server);
|
|
63
72
|
const url = `http://localhost:${bound}`;
|
|
@@ -72,7 +81,9 @@ const http = definePlugin({
|
|
|
72
81
|
return;
|
|
73
82
|
servers.delete(context);
|
|
74
83
|
urls.delete(context);
|
|
75
|
-
|
|
84
|
+
const open = taken.get(context) ?? new Map();
|
|
85
|
+
taken.delete(context);
|
|
86
|
+
await closed(server, logger, open);
|
|
76
87
|
},
|
|
77
88
|
});
|
|
78
89
|
/** The application's HTTP server (D-66, D-106). */
|
package/dist/server.d.ts
CHANGED
|
@@ -11,5 +11,5 @@ export declare function serverOf(fetch: Parameters<typeof getRequestListener>[0]
|
|
|
11
11
|
* Stops accepting connections, then waits for running requests and the
|
|
12
12
|
* sockets plugins took (D-59), 10 seconds at most (D-106).
|
|
13
13
|
*/
|
|
14
|
-
export declare function closed(server: Server, logger: Logger, taken:
|
|
14
|
+
export declare function closed(server: Server, logger: Logger, taken: ReadonlyMap<Duplex, string>): Promise<void>;
|
|
15
15
|
//# sourceMappingURL=server.d.ts.map
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AA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BAAsB,MAAM,CAC3B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,OAAO,CAAC,IAAI,CAAC,CAiBf"}
|
package/dist/server.js
CHANGED
|
@@ -66,7 +66,7 @@ export async function closed(server, logger, taken) {
|
|
|
66
66
|
cutting = setTimeout(() => {
|
|
67
67
|
logger.warn(`Stopped ${grace / 1000} s after stop began: closed the requests still running.`);
|
|
68
68
|
server.closeAllConnections();
|
|
69
|
-
for (const socket of taken)
|
|
69
|
+
for (const socket of taken.keys())
|
|
70
70
|
socket.destroy();
|
|
71
71
|
}, grace);
|
|
72
72
|
});
|
package/dist/upgrade.d.ts
CHANGED
|
@@ -15,11 +15,43 @@ export interface Upgrade {
|
|
|
15
15
|
}
|
|
16
16
|
/** Takes an upgrade on its path; the socket is the plugin's from then on. */
|
|
17
17
|
export type Accept = (upgrade: Upgrade) => void;
|
|
18
|
+
/** Closes a plugin's sockets with a WebSocket close code (D-166). */
|
|
19
|
+
export type Close = (code: number, reason: string) => void;
|
|
18
20
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
+
* The application's answer to an upgrade, before the plugin takes it: a
|
|
22
|
+
* response refuses it, `undefined` lets it through (D-166).
|
|
21
23
|
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
export type Gate = (request: Request) => Response | undefined | Promise<Response | undefined>;
|
|
25
|
+
/** What `closeUpgrades` takes: every path's sockets, or `path`'s (D-166). */
|
|
26
|
+
export interface Closing {
|
|
27
|
+
readonly code: number;
|
|
28
|
+
readonly reason?: string;
|
|
29
|
+
readonly path?: string;
|
|
30
|
+
}
|
|
31
|
+
/** The plugin that takes a path's upgrades, and how it closes them. */
|
|
32
|
+
export interface Taker {
|
|
33
|
+
readonly accept: Accept;
|
|
34
|
+
readonly close: Close | undefined;
|
|
35
|
+
}
|
|
36
|
+
/** What `upgrading` reads from the server's options and plugins. */
|
|
37
|
+
export interface Upgrading {
|
|
38
|
+
readonly takers: ReadonlyMap<string, Taker>;
|
|
39
|
+
readonly gate: Gate | undefined;
|
|
40
|
+
readonly allows: (origin: string) => boolean;
|
|
41
|
+
readonly addressOf: (env: {
|
|
42
|
+
readonly incoming: IncomingMessage;
|
|
43
|
+
}) => string;
|
|
44
|
+
readonly logger: Logger;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Hands each upgrade to its path's plugin, past the origin check and the
|
|
48
|
+
* gate; returns the sockets taken and still open, with their path, which
|
|
49
|
+
* `stop` and `closeUpgrades` close.
|
|
50
|
+
*/
|
|
51
|
+
export declare function upgrading(server: Server, { takers, gate, allows, addressOf, logger }: Upgrading): ReadonlyMap<Duplex, string>;
|
|
52
|
+
/**
|
|
53
|
+
* Closes the sockets open on every path, or on `path`: with each plugin's
|
|
54
|
+
* `close`, or destroyed when it gave none; none before the start.
|
|
55
|
+
*/
|
|
56
|
+
export declare function closeUpgrades(takers: ReadonlyMap<string, Taker>, open: ReadonlyMap<Duplex, string> | undefined, { code, reason, path }: Closing): void;
|
|
25
57
|
//# sourceMappingURL=upgrade.d.ts.map
|
package/dist/upgrade.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upgrade.d.ts","sourceRoot":"","sources":["../src/upgrade.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"upgrade.d.ts","sourceRoot":"","sources":["../src/upgrade.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAExD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACvB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM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CD;;;;GAIG;AACH,wBAAgB,SAAS,CACxB,MAAM,EAAE,MAAM,EACd,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,SAAS,GACpD,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAuC7B;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC5B,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,EAClC,IAAI,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EAC7C,EAAE,IAAI,EAAE,MAAW,EAAE,IAAI,EAAE,EAAE,OAAO,GAClC,IAAI,CAUN"}
|
package/dist/upgrade.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { STATUS_CODES } from 'node:http';
|
|
2
|
+
/** Sends `response` on the raw socket, then closes it. */
|
|
3
|
+
async function send(socket, response) {
|
|
4
|
+
const body = Buffer.from(await response.arrayBuffer());
|
|
5
|
+
const headers = new Headers(response.headers);
|
|
6
|
+
headers.delete('transfer-encoding');
|
|
7
|
+
headers.set('connection', 'close');
|
|
8
|
+
headers.set('content-length', String(body.length));
|
|
9
|
+
const head = [
|
|
10
|
+
`HTTP/1.1 ${response.status} ${STATUS_CODES[response.status] ?? ''}`,
|
|
11
|
+
];
|
|
12
|
+
for (const [name, value] of headers)
|
|
13
|
+
head.push(`${name}: ${value}`);
|
|
14
|
+
socket.end(Buffer.concat([Buffer.from(`${head.join('\r\n')}\r\n\r\n`), body]));
|
|
15
|
+
}
|
|
2
16
|
/** Answers `status` with an error as D-72 sends it, then closes the socket. */
|
|
3
17
|
function refuse(socket, status, code, message) {
|
|
4
|
-
|
|
5
|
-
socket.end([
|
|
6
|
-
`HTTP/1.1 ${status} ${STATUS_CODES[status] ?? ''}`,
|
|
7
|
-
'connection: close',
|
|
8
|
-
'content-type: application/json',
|
|
9
|
-
`content-length: ${Buffer.byteLength(body)}`,
|
|
10
|
-
'',
|
|
11
|
-
body,
|
|
12
|
-
].join('\r\n'));
|
|
18
|
+
void send(socket, Response.json({ code, message }, { status }));
|
|
13
19
|
}
|
|
14
20
|
/** The request `incoming` is, without a body; `undefined` for no valid URL. */
|
|
15
21
|
function requestOf(incoming) {
|
|
@@ -27,11 +33,12 @@ function requestOf(incoming) {
|
|
|
27
33
|
return new Request(url, { headers });
|
|
28
34
|
}
|
|
29
35
|
/**
|
|
30
|
-
* Hands each upgrade to its path's plugin, past the origin check
|
|
31
|
-
* the sockets taken and still open,
|
|
36
|
+
* Hands each upgrade to its path's plugin, past the origin check and the
|
|
37
|
+
* gate; returns the sockets taken and still open, with their path, which
|
|
38
|
+
* `stop` and `closeUpgrades` close.
|
|
32
39
|
*/
|
|
33
|
-
export function upgrading(server,
|
|
34
|
-
const open = new
|
|
40
|
+
export function upgrading(server, { takers, gate, allows, addressOf, logger }) {
|
|
41
|
+
const open = new Map();
|
|
35
42
|
server.on('upgrade', (incoming, socket, head) => {
|
|
36
43
|
// A client that leaves mid-answer must not stop the server.
|
|
37
44
|
socket.on('error', () => socket.destroy());
|
|
@@ -40,24 +47,51 @@ export function upgrading(server, accepts, allows, addressOf, logger) {
|
|
|
40
47
|
return refuse(socket, 400, 'BAD_REQUEST', 'The upgrade has no URL.');
|
|
41
48
|
}
|
|
42
49
|
const { origin, pathname } = new URL(request.url);
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
50
|
+
const taker = takers.get(pathname);
|
|
51
|
+
if (taker === undefined) {
|
|
45
52
|
return refuse(socket, 404, 'NOT_FOUND', `No upgrade at ${pathname}.`);
|
|
46
53
|
}
|
|
47
54
|
const from = incoming.headers.origin;
|
|
48
55
|
if (from !== undefined && from !== origin && !allows(from)) {
|
|
49
56
|
return refuse(socket, 403, 'ACCESS_DENIED', 'A WebSocket from another origin is refused.');
|
|
50
57
|
}
|
|
51
|
-
|
|
58
|
+
// Open while the gate answers, so `stop` waits for it too.
|
|
59
|
+
open.set(socket, pathname);
|
|
52
60
|
socket.once('close', () => open.delete(socket));
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
void (async () => {
|
|
62
|
+
try {
|
|
63
|
+
const refused = await gate?.(request);
|
|
64
|
+
if (refused !== undefined)
|
|
65
|
+
return await send(socket, refused);
|
|
66
|
+
const address = addressOf({ incoming });
|
|
67
|
+
taker.accept({ request, address, incoming, socket, head });
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
logger.error({ err: error }, 'An upgrade failed.');
|
|
71
|
+
refuse(socket, 500, 'INTERNAL', 'Internal error.');
|
|
72
|
+
}
|
|
73
|
+
})();
|
|
61
74
|
});
|
|
62
75
|
return open;
|
|
63
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Closes the sockets open on every path, or on `path`: with each plugin's
|
|
79
|
+
* `close`, or destroyed when it gave none; none before the start.
|
|
80
|
+
*/
|
|
81
|
+
export function closeUpgrades(takers, open, { code, reason = '', path }) {
|
|
82
|
+
if (path !== undefined && !takers.has(path)) {
|
|
83
|
+
throw new Error(`No plugin takes the upgrades of ${path}.`);
|
|
84
|
+
}
|
|
85
|
+
if (open === undefined)
|
|
86
|
+
return;
|
|
87
|
+
for (const [taken, { close }] of takers) {
|
|
88
|
+
if (path !== undefined && taken !== path)
|
|
89
|
+
continue;
|
|
90
|
+
if (close !== undefined)
|
|
91
|
+
close(code, reason);
|
|
92
|
+
else
|
|
93
|
+
for (const [socket, at] of open)
|
|
94
|
+
if (at === taken)
|
|
95
|
+
socket.destroy();
|
|
96
|
+
}
|
|
97
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimabase/http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-canary.20261031104207",
|
|
4
4
|
"description": "An HTTP server for UltimaBase: Hono routes that read the caller, CORS and a body limit.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -37,10 +37,10 @@
|
|
|
37
37
|
"hono": "4.13.13"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"ultimabase": "
|
|
40
|
+
"ultimabase": "0.3.0-canary.20261031104207"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"ultimabase": "0.
|
|
43
|
+
"ultimabase": "0.3.0-canary.20261031104207",
|
|
44
44
|
"vite": "8.3.4",
|
|
45
45
|
"vitest": "5.0.5"
|
|
46
46
|
}
|
package/src/app.ts
CHANGED
|
@@ -14,6 +14,7 @@ import type { Caller, ErrorCode, Logger } from 'ultimabase'
|
|
|
14
14
|
import { isLocalHost, PublicError } from 'ultimabase'
|
|
15
15
|
import { addresses, proxiesOf } from './address.ts'
|
|
16
16
|
import { hold } from './request.ts'
|
|
17
|
+
import type { Gate } from './upgrade.ts'
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* What a route's context holds: `c.var.caller` (D-106) and `c.var.address`,
|
|
@@ -42,6 +43,11 @@ export interface HttpOptions {
|
|
|
42
43
|
readonly bodyLimit?: number
|
|
43
44
|
/** In front of the server, which then reads the client's address from `X-Forwarded-For`; 0 by default (D-116). */
|
|
44
45
|
readonly proxies?: number
|
|
46
|
+
/**
|
|
47
|
+
* Answers each WebSocket upgrade before the plugin that takes it: a
|
|
48
|
+
* response refuses it, as during maintenance (D-166).
|
|
49
|
+
*/
|
|
50
|
+
readonly upgrade?: Gate
|
|
45
51
|
}
|
|
46
52
|
|
|
47
53
|
const statuses: ReadonlyMap<string, ContentfulStatusCode> = new Map<
|
package/src/index.ts
CHANGED
|
@@ -11,11 +11,11 @@ import { addresses, proxiesOf } from './address.ts'
|
|
|
11
11
|
import { allowing, appOf } from './app.ts'
|
|
12
12
|
import { hold } from './request.ts'
|
|
13
13
|
import { closed, listening, portOf, serverOf } from './server.ts'
|
|
14
|
-
import type { Accept } from './upgrade.ts'
|
|
15
|
-
import { upgrading } from './upgrade.ts'
|
|
14
|
+
import type { Accept, Close, Closing, Taker } from './upgrade.ts'
|
|
15
|
+
import { closeUpgrades, upgrading } from './upgrade.ts'
|
|
16
16
|
|
|
17
17
|
export type { HttpEnv, HttpOptions, Routes } from './app.ts'
|
|
18
|
-
export type { Accept, Upgrade } from './upgrade.ts'
|
|
18
|
+
export type { Accept, Close, Closing, Gate, Upgrade } from './upgrade.ts'
|
|
19
19
|
export { clientOf } from './client.ts'
|
|
20
20
|
export type { StandardSchema } from './validate.ts'
|
|
21
21
|
export { validate } from './validate.ts'
|
|
@@ -29,9 +29,19 @@ export interface HttpApi {
|
|
|
29
29
|
mount(routes: Routes, options?: { readonly bodyLimit?: false }): void
|
|
30
30
|
/**
|
|
31
31
|
* Takes the WebSocket upgrades of `path`, once their origin is one CORS
|
|
32
|
-
* allows; call it from your
|
|
32
|
+
* allows and the `upgrade` gate let them through; call it from your
|
|
33
|
+
* `api` (D-154). `close` closes its sockets with a code (D-166).
|
|
33
34
|
*/
|
|
34
|
-
upgrade(
|
|
35
|
+
upgrade(
|
|
36
|
+
path: string,
|
|
37
|
+
accept: Accept,
|
|
38
|
+
options?: { readonly close?: Close },
|
|
39
|
+
): void
|
|
40
|
+
/**
|
|
41
|
+
* Closes the WebSockets open on every path, or on `path`, with `code`:
|
|
42
|
+
* those of a plugin that gave no `close` are cut (D-166).
|
|
43
|
+
*/
|
|
44
|
+
closeUpgrades(closing: Closing): void
|
|
35
45
|
/** Where the server listens, `http://localhost:3000`, once it started. */
|
|
36
46
|
url(): string
|
|
37
47
|
}
|
|
@@ -47,8 +57,8 @@ export interface Http extends PluginTypes {
|
|
|
47
57
|
// plugin received.
|
|
48
58
|
const servers = new WeakMap<object, Server>()
|
|
49
59
|
const mounts = new WeakMap<object, Mounted[]>()
|
|
50
|
-
const upgrades = new WeakMap<object, Map<string,
|
|
51
|
-
const taken = new WeakMap<object,
|
|
60
|
+
const upgrades = new WeakMap<object, Map<string, Taker>>()
|
|
61
|
+
const taken = new WeakMap<object, ReadonlyMap<Duplex, string>>()
|
|
52
62
|
const urls = new WeakMap<object, string>()
|
|
53
63
|
|
|
54
64
|
const http = definePlugin<Http>({
|
|
@@ -56,9 +66,9 @@ const http = definePlugin<Http>({
|
|
|
56
66
|
manifest,
|
|
57
67
|
api: (context) => {
|
|
58
68
|
const mounted: Mounted[] = []
|
|
59
|
-
const
|
|
69
|
+
const takers = new Map<string, Taker>()
|
|
60
70
|
mounts.set(context, mounted)
|
|
61
|
-
upgrades.set(context,
|
|
71
|
+
upgrades.set(context, takers)
|
|
62
72
|
const building = (what: string) => {
|
|
63
73
|
if (mounts.has(context)) return
|
|
64
74
|
throw new Error(
|
|
@@ -70,12 +80,15 @@ const http = definePlugin<Http>({
|
|
|
70
80
|
building('mount routes')
|
|
71
81
|
mounted.push({ routes, bodyLimit: mountOptions?.bodyLimit ?? true })
|
|
72
82
|
},
|
|
73
|
-
upgrade: (path, accept) => {
|
|
83
|
+
upgrade: (path, accept, upgradeOptions) => {
|
|
74
84
|
building('take upgrades')
|
|
75
|
-
if (
|
|
85
|
+
if (takers.has(path)) {
|
|
76
86
|
throw new Error(`Two plugins take the upgrades of ${path}.`)
|
|
77
87
|
}
|
|
78
|
-
|
|
88
|
+
takers.set(path, { accept, close: upgradeOptions?.close })
|
|
89
|
+
},
|
|
90
|
+
closeUpgrades: (closing) => {
|
|
91
|
+
closeUpgrades(takers, taken.get(context), closing)
|
|
79
92
|
},
|
|
80
93
|
url: () => {
|
|
81
94
|
const url = urls.get(context)
|
|
@@ -94,11 +107,17 @@ const http = definePlugin<Http>({
|
|
|
94
107
|
const app = appOf(options, mounts.get(context) ?? [], identify, logger)
|
|
95
108
|
mounts.delete(context)
|
|
96
109
|
const server = serverOf(app.fetch)
|
|
97
|
-
const
|
|
110
|
+
const takers = upgrades.get(context) ?? new Map<string, Taker>()
|
|
98
111
|
const addressOf = addresses(proxiesOf(options.proxies), logger)
|
|
99
112
|
taken.set(
|
|
100
113
|
context,
|
|
101
|
-
upgrading(server,
|
|
114
|
+
upgrading(server, {
|
|
115
|
+
takers,
|
|
116
|
+
gate: options.upgrade,
|
|
117
|
+
allows: allowing(options),
|
|
118
|
+
addressOf,
|
|
119
|
+
logger,
|
|
120
|
+
}),
|
|
102
121
|
)
|
|
103
122
|
const bound = await listening(server, port)
|
|
104
123
|
servers.set(context, server)
|
|
@@ -113,7 +132,9 @@ const http = definePlugin<Http>({
|
|
|
113
132
|
if (server === undefined) return
|
|
114
133
|
servers.delete(context)
|
|
115
134
|
urls.delete(context)
|
|
116
|
-
|
|
135
|
+
const open = taken.get(context) ?? new Map<Duplex, string>()
|
|
136
|
+
taken.delete(context)
|
|
137
|
+
await closed(server, logger, open)
|
|
117
138
|
},
|
|
118
139
|
})
|
|
119
140
|
|
package/src/server.ts
CHANGED
|
@@ -69,7 +69,7 @@ const grace = 10_000
|
|
|
69
69
|
export async function closed(
|
|
70
70
|
server: Server,
|
|
71
71
|
logger: Logger,
|
|
72
|
-
taken:
|
|
72
|
+
taken: ReadonlyMap<Duplex, string>,
|
|
73
73
|
): Promise<void> {
|
|
74
74
|
let cutting: NodeJS.Timeout | undefined
|
|
75
75
|
await new Promise<void>((done, fail) => {
|
|
@@ -84,7 +84,7 @@ export async function closed(
|
|
|
84
84
|
`Stopped ${grace / 1000} s after stop began: closed the requests still running.`,
|
|
85
85
|
)
|
|
86
86
|
server.closeAllConnections()
|
|
87
|
-
for (const socket of taken) socket.destroy()
|
|
87
|
+
for (const socket of taken.keys()) socket.destroy()
|
|
88
88
|
}, grace)
|
|
89
89
|
})
|
|
90
90
|
}
|
package/src/upgrade.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// WebSocket upgrades (D-154): a plugin takes a path's. One from an origin
|
|
2
2
|
// CORS does not allow is refused before any caller is read, so another site
|
|
3
|
-
// cannot open a socket with its visitor's cookies
|
|
3
|
+
// cannot open a socket with its visitor's cookies; the application's gate
|
|
4
|
+
// may refuse the rest, and close those open (D-166).
|
|
4
5
|
import type { IncomingMessage, Server } from 'node:http'
|
|
5
6
|
import { STATUS_CODES } from 'node:http'
|
|
6
7
|
import type { Duplex } from 'node:stream'
|
|
@@ -22,6 +23,53 @@ export interface Upgrade {
|
|
|
22
23
|
/** Takes an upgrade on its path; the socket is the plugin's from then on. */
|
|
23
24
|
export type Accept = (upgrade: Upgrade) => void
|
|
24
25
|
|
|
26
|
+
/** Closes a plugin's sockets with a WebSocket close code (D-166). */
|
|
27
|
+
export type Close = (code: number, reason: string) => void
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The application's answer to an upgrade, before the plugin takes it: a
|
|
31
|
+
* response refuses it, `undefined` lets it through (D-166).
|
|
32
|
+
*/
|
|
33
|
+
export type Gate = (
|
|
34
|
+
request: Request,
|
|
35
|
+
) => Response | undefined | Promise<Response | undefined>
|
|
36
|
+
|
|
37
|
+
/** What `closeUpgrades` takes: every path's sockets, or `path`'s (D-166). */
|
|
38
|
+
export interface Closing {
|
|
39
|
+
readonly code: number
|
|
40
|
+
readonly reason?: string
|
|
41
|
+
readonly path?: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The plugin that takes a path's upgrades, and how it closes them. */
|
|
45
|
+
export interface Taker {
|
|
46
|
+
readonly accept: Accept
|
|
47
|
+
readonly close: Close | undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** What `upgrading` reads from the server's options and plugins. */
|
|
51
|
+
export interface Upgrading {
|
|
52
|
+
readonly takers: ReadonlyMap<string, Taker>
|
|
53
|
+
readonly gate: Gate | undefined
|
|
54
|
+
readonly allows: (origin: string) => boolean
|
|
55
|
+
readonly addressOf: (env: { readonly incoming: IncomingMessage }) => string
|
|
56
|
+
readonly logger: Logger
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Sends `response` on the raw socket, then closes it. */
|
|
60
|
+
async function send(socket: Duplex, response: Response): Promise<void> {
|
|
61
|
+
const body = Buffer.from(await response.arrayBuffer())
|
|
62
|
+
const headers = new Headers(response.headers)
|
|
63
|
+
headers.delete('transfer-encoding')
|
|
64
|
+
headers.set('connection', 'close')
|
|
65
|
+
headers.set('content-length', String(body.length))
|
|
66
|
+
const head = [
|
|
67
|
+
`HTTP/1.1 ${response.status} ${STATUS_CODES[response.status] ?? ''}`,
|
|
68
|
+
]
|
|
69
|
+
for (const [name, value] of headers) head.push(`${name}: ${value}`)
|
|
70
|
+
socket.end(Buffer.concat([Buffer.from(`${head.join('\r\n')}\r\n\r\n`), body]))
|
|
71
|
+
}
|
|
72
|
+
|
|
25
73
|
/** Answers `status` with an error as D-72 sends it, then closes the socket. */
|
|
26
74
|
function refuse(
|
|
27
75
|
socket: Duplex,
|
|
@@ -29,17 +77,7 @@ function refuse(
|
|
|
29
77
|
code: string,
|
|
30
78
|
message: string,
|
|
31
79
|
): void {
|
|
32
|
-
|
|
33
|
-
socket.end(
|
|
34
|
-
[
|
|
35
|
-
`HTTP/1.1 ${status} ${STATUS_CODES[status] ?? ''}`,
|
|
36
|
-
'connection: close',
|
|
37
|
-
'content-type: application/json',
|
|
38
|
-
`content-length: ${Buffer.byteLength(body)}`,
|
|
39
|
-
'',
|
|
40
|
-
body,
|
|
41
|
-
].join('\r\n'),
|
|
42
|
-
)
|
|
80
|
+
void send(socket, Response.json({ code, message }, { status }))
|
|
43
81
|
}
|
|
44
82
|
|
|
45
83
|
/** The request `incoming` is, without a body; `undefined` for no valid URL. */
|
|
@@ -59,17 +97,15 @@ function requestOf(incoming: IncomingMessage): Request | undefined {
|
|
|
59
97
|
}
|
|
60
98
|
|
|
61
99
|
/**
|
|
62
|
-
* Hands each upgrade to its path's plugin, past the origin check
|
|
63
|
-
* the sockets taken and still open,
|
|
100
|
+
* Hands each upgrade to its path's plugin, past the origin check and the
|
|
101
|
+
* gate; returns the sockets taken and still open, with their path, which
|
|
102
|
+
* `stop` and `closeUpgrades` close.
|
|
64
103
|
*/
|
|
65
104
|
export function upgrading(
|
|
66
105
|
server: Server,
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
logger: Logger,
|
|
71
|
-
): ReadonlySet<Duplex> {
|
|
72
|
-
const open = new Set<Duplex>()
|
|
106
|
+
{ takers, gate, allows, addressOf, logger }: Upgrading,
|
|
107
|
+
): ReadonlyMap<Duplex, string> {
|
|
108
|
+
const open = new Map<Duplex, string>()
|
|
73
109
|
server.on('upgrade', (incoming: IncomingMessage, socket: Duplex, head) => {
|
|
74
110
|
// A client that leaves mid-answer must not stop the server.
|
|
75
111
|
socket.on('error', () => socket.destroy())
|
|
@@ -78,8 +114,8 @@ export function upgrading(
|
|
|
78
114
|
return refuse(socket, 400, 'BAD_REQUEST', 'The upgrade has no URL.')
|
|
79
115
|
}
|
|
80
116
|
const { origin, pathname } = new URL(request.url)
|
|
81
|
-
const
|
|
82
|
-
if (
|
|
117
|
+
const taker = takers.get(pathname)
|
|
118
|
+
if (taker === undefined) {
|
|
83
119
|
return refuse(socket, 404, 'NOT_FOUND', `No upgrade at ${pathname}.`)
|
|
84
120
|
}
|
|
85
121
|
const from = incoming.headers.origin
|
|
@@ -91,15 +127,40 @@ export function upgrading(
|
|
|
91
127
|
'A WebSocket from another origin is refused.',
|
|
92
128
|
)
|
|
93
129
|
}
|
|
94
|
-
|
|
130
|
+
// Open while the gate answers, so `stop` waits for it too.
|
|
131
|
+
open.set(socket, pathname)
|
|
95
132
|
socket.once('close', () => open.delete(socket))
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
133
|
+
void (async () => {
|
|
134
|
+
try {
|
|
135
|
+
const refused = await gate?.(request)
|
|
136
|
+
if (refused !== undefined) return await send(socket, refused)
|
|
137
|
+
const address = addressOf({ incoming })
|
|
138
|
+
taker.accept({ request, address, incoming, socket, head })
|
|
139
|
+
} catch (error) {
|
|
140
|
+
logger.error({ err: error }, 'An upgrade failed.')
|
|
141
|
+
refuse(socket, 500, 'INTERNAL', 'Internal error.')
|
|
142
|
+
}
|
|
143
|
+
})()
|
|
103
144
|
})
|
|
104
145
|
return open
|
|
105
146
|
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Closes the sockets open on every path, or on `path`: with each plugin's
|
|
150
|
+
* `close`, or destroyed when it gave none; none before the start.
|
|
151
|
+
*/
|
|
152
|
+
export function closeUpgrades(
|
|
153
|
+
takers: ReadonlyMap<string, Taker>,
|
|
154
|
+
open: ReadonlyMap<Duplex, string> | undefined,
|
|
155
|
+
{ code, reason = '', path }: Closing,
|
|
156
|
+
): void {
|
|
157
|
+
if (path !== undefined && !takers.has(path)) {
|
|
158
|
+
throw new Error(`No plugin takes the upgrades of ${path}.`)
|
|
159
|
+
}
|
|
160
|
+
if (open === undefined) return
|
|
161
|
+
for (const [taken, { close }] of takers) {
|
|
162
|
+
if (path !== undefined && taken !== path) continue
|
|
163
|
+
if (close !== undefined) close(code, reason)
|
|
164
|
+
else for (const [socket, at] of open) if (at === taken) socket.destroy()
|
|
165
|
+
}
|
|
166
|
+
}
|