@ultimabase/http 0.8.0-canary.20261114123635 → 1.0.0
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 +21 -2
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +41 -3
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -13
- package/dist/server.d.ts +20 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +94 -15
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +12 -3
- package/package.json +3 -3
- package/src/app.ts +47 -3
- package/src/index.ts +38 -13
- package/src/server.ts +102 -15
- package/src/upgrade.ts +12 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,25 +1,44 @@
|
|
|
1
1
|
# @ultimabase/http
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## 1.0.0
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
7
7
|
- 20aec1b: The application's code logs with the application's logger, its lines named `app` (D-364): a function's `run` receives `logger`, bound to `{ function: name }`; each hook receives `logger`; a route reads `c.var.logger`; a job's `run` receives `logger`, bound to `{ job: name, run: id }`.
|
|
8
|
+
- 4b2278d: The maintenance ends the open event streams as it begins: a live list's
|
|
9
|
+
stream no longer goes on serving changes during maintenance. The HTTP plugin's
|
|
10
|
+
new `plugins.http.closeStreams()` ends every `text/event-stream` answer.
|
|
8
11
|
|
|
9
12
|
### Patch Changes
|
|
10
13
|
|
|
11
14
|
- 458361b: `hono` 4.13.16 fixes its advisories: a `multipart/form-data` body with an extra parameter, as `charset`, is now validated as a form, and wildcard middleware now runs for every route the default router matches.
|
|
12
15
|
- 4c9e506: `@hono/node-server` 2.1.6 fixes two advisories: a connection reset during a WebSocket upgrade no longer crashes the process, and a backslash in a request's path no longer passes a reverse proxy's path rules as a separator.
|
|
16
|
+
- 4b2278d: A request still running when `stop` begins is answered before any plugin stops: an upload, or a sign-up that sends its welcome email, no longer fails because the plugin it uses stopped first. Live streams and WebSockets still end with their plugin's `stop`, and the whole stop still takes 10 seconds at most (D-395).
|
|
17
|
+
- 4b2278d: A route that fails on its own while its client leaves answers `500` and is logged again; only the
|
|
18
|
+
client leaving, its body read cut, gives `499`.
|
|
19
|
+
- 4b2278d: A request whose headers end once the stop began answers 503 and closes its connection, as `/ready` does: it ran after the plugins it used had stopped. An upgrade is refused the same way, and one whose gate runs when the stop begins is taken before its plugin stops.
|
|
20
|
+
- 4b2278d: An idle connection is kept 65 s, past a load balancer's 60 s, which no longer answers `502` on a connection the server just closed. A request's body arrives within 120 s, not 300, and a socket quiet for 120 s is closed. A connection that turns idle once the stop began is closed at once, rather than holding the stop.
|
|
21
|
+
- 4b2278d: An upgrade whose `Host` header holds a path or a query, such as `localhost/admin?`, is refused with 400: it picked the plugin of that path, whatever path the request named.
|
|
22
|
+
- 4b2278d: During maintenance, `PUT /_ultimabase/maintenance` passes the gate with the
|
|
23
|
+
master key or the maintenance key only: an application route matching that
|
|
24
|
+
path no longer runs for an anonymous caller. The HTTP plugin refuses to start
|
|
25
|
+
when an application route answers a plugin's path under `/_ultimabase/`.
|
|
13
26
|
- 1520a09: The kernel publishes each model operation, hook, virtual field and function on Node's tracing channels `ultimabase:operation`, `ultimabase:hook`, `ultimabase:virtual` and `ultimabase:function`, nested, and each error the server logs at error level on `ultimabase:error` with its correlation id. A tool such as an error tracker subscribes to them; without a subscriber, nothing is built.
|
|
14
27
|
- Updated dependencies [20aec1b]
|
|
15
28
|
- Updated dependencies [b2aa312]
|
|
16
29
|
- Updated dependencies [94145f9]
|
|
17
30
|
- Updated dependencies [8f82820]
|
|
18
31
|
- Updated dependencies [1b9515a]
|
|
32
|
+
- Updated dependencies [4b2278d]
|
|
19
33
|
- Updated dependencies [51f8f04]
|
|
20
34
|
- Updated dependencies [3099475]
|
|
35
|
+
- Updated dependencies [fd85be5]
|
|
36
|
+
- Updated dependencies [4b2278d]
|
|
37
|
+
- Updated dependencies [4b2278d]
|
|
21
38
|
- Updated dependencies [1520a09]
|
|
22
|
-
|
|
39
|
+
- Updated dependencies [4b2278d]
|
|
40
|
+
- Updated dependencies [4b2278d]
|
|
41
|
+
- ultimabase@1.0.0
|
|
23
42
|
|
|
24
43
|
## 0.7.0
|
|
25
44
|
|
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;AAI3D,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEpD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAKzC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAGxC;;;;GAIG;AACH,MAAM,MAAM,OAAO,GAAG;IACrB,SAAS,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;CAC9D,CAAA;AAED,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,C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
|
|
10
10
|
import { getPath } from 'hono/utils/url';
|
|
11
|
-
import { isLocalHost, PublicError } from 'ultimabase';
|
|
11
|
+
import { isLocalHost, PublicError, Refused } from 'ultimabase';
|
|
12
12
|
import { logFailure } from 'ultimabase/internal';
|
|
13
13
|
import { isOwnOrigin } from './address.js';
|
|
14
14
|
import { tooMany } from './limit.js';
|
|
@@ -42,11 +42,20 @@ function refused(error, c) {
|
|
|
42
42
|
const message = error.message === '' ? (name ?? code) : error.message;
|
|
43
43
|
return c.json({ code, message }, error.status);
|
|
44
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Whether `error` came from the client leaving: its body read cut, or the
|
|
47
|
+
* request's signal, as a `fetch` given it throws.
|
|
48
|
+
*/
|
|
49
|
+
const isLeaving = (error, signal) => signal.aborted &&
|
|
50
|
+
(error === signal.reason ||
|
|
51
|
+
(error instanceof Error && Reflect.get(error, 'code') === 'ECONNRESET'));
|
|
45
52
|
/** A `PublicError` as is; any other error masked, logged with its id. */
|
|
46
53
|
function failed(error, c, logger) {
|
|
47
|
-
//
|
|
48
|
-
|
|
54
|
+
// Its request failed, not the server (499, as nginx); a failure of the
|
|
55
|
+
// server's own meanwhile is still logged (D-108).
|
|
56
|
+
if (isLeaving(error, c.req.raw.signal)) {
|
|
49
57
|
return new Response(null, { status: 499 });
|
|
58
|
+
}
|
|
50
59
|
if (error instanceof HTTPException)
|
|
51
60
|
return refused(error, c);
|
|
52
61
|
if (error instanceof PublicError) {
|
|
@@ -109,12 +118,41 @@ function stamp(c, name, value, append = false) {
|
|
|
109
118
|
c.header(name, value, { append });
|
|
110
119
|
}
|
|
111
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Refuses an application route that answers a plugin's path under
|
|
123
|
+
* `/_ultimabase/`, as `PUT /:a/:b` would the maintenance's switch: it comes
|
|
124
|
+
* first, so it would run in the plugin's place (D-205).
|
|
125
|
+
*/
|
|
126
|
+
function refuseShadows(own, mounted) {
|
|
127
|
+
const app = new Hono();
|
|
128
|
+
for (const routes of own) {
|
|
129
|
+
for (const { method, path } of routes.routes) {
|
|
130
|
+
if (method !== 'ALL')
|
|
131
|
+
app.on(method, path, (c) => c.body(null));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
for (const routes of mounted) {
|
|
135
|
+
for (const { method, path } of routes.routes) {
|
|
136
|
+
if (method === 'ALL' || !path.startsWith('/_ultimabase/'))
|
|
137
|
+
continue;
|
|
138
|
+
const [[first]] = app.router.match(method, path);
|
|
139
|
+
if (first === undefined)
|
|
140
|
+
continue;
|
|
141
|
+
const [, route] = first[0];
|
|
142
|
+
throw new Refused(`The application's route ${route.method} ${route.path} answers ${method} ${path}, which a plugin serves: give it another path.`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
112
146
|
/**
|
|
113
147
|
* Reads `NODE_ENV` once, when the application starts. The application's
|
|
114
148
|
* routes come before those plugins mounted (D-107).
|
|
115
149
|
*/
|
|
116
150
|
export function appOf(options, mounted, { probes, gates, rateLimit, addressOf, }, identify, logger) {
|
|
117
151
|
const local = process.env['NODE_ENV'] !== 'production';
|
|
152
|
+
refuseShadows(options.routes ?? [], [
|
|
153
|
+
...mounted.map((mount) => mount.routes),
|
|
154
|
+
...(local ? [resetRoutes] : []),
|
|
155
|
+
]);
|
|
118
156
|
const allows = allowing(options);
|
|
119
157
|
const maxSize = bodyLimitOf(options.bodyLimit);
|
|
120
158
|
const app = new Hono({ getPath: pathOf });
|
package/dist/index.d.ts
CHANGED
|
@@ -31,6 +31,11 @@ export interface HttpApi {
|
|
|
31
31
|
* those of a plugin that gave no `close` are cut (D-166).
|
|
32
32
|
*/
|
|
33
33
|
closeUpgrades(closing: Closing): void;
|
|
34
|
+
/**
|
|
35
|
+
* Ends the open event streams (`text/event-stream` answers): each client
|
|
36
|
+
* reconnects, as maintenance needs (D-205).
|
|
37
|
+
*/
|
|
38
|
+
closeStreams(): void;
|
|
34
39
|
/**
|
|
35
40
|
* Adds a check `GET /ready` runs on each request: it answers 503 while
|
|
36
41
|
* one fails; call it from your `api` (D-204).
|
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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAkB,QAAQ,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAGvE,OAAO,KAAK,EAAW,WAAW,EAAW,MAAM,EAAE,MAAM,UAAU,CAAA;AAIrE,OAAO,KAAK,EAAU,UAAU,EAAE,MAAM,aAAa,CAAA;AAWrD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAS,MAAM,cAAc,CAAA;AAGvE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAC5D,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAC3C,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAC7C,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACzE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,yDAAyD;AACzD,MAAM,WAAW,OAAO;IACvB;;;OAGG;IACH,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,SAAS,CAAC,EAAE,KAAK,CAAA;KAAE,GAAG,IAAI,CAAA;IACrE;;;;OAIG;IACH,OAAO,CACN,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE,GAClC,IAAI,CAAA;IACP;;;OAGG;IACH,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAA;IACrC;;;OAGG;IACH,YAAY,IAAI,IAAI,CAAA;IACpB;;;OAGG;IACH,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAA;IAC9B;;;;OAIG;IACH,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAAA;IACtB,0EAA0E;IAC1E,GAAG,IAAI,MAAM,CAAA;CACb;AAED,sCAAsC;AACtC,MAAM,WAAW,IAAK,SAAQ,WAAW;IACxC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAA;IAChC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAA;IACrB,QAAQ,CAAC,MAAM,EACZ,aAAa,GACb,gBAAgB,GAChB,mBAAmB,GACnB,wBAAwB,CAAA;CAC3B;AA4JD,mDAAmD;AACnD,eAAO,MAAM,UAAU,EAAE,QAAQ,CAAC,IAAI,CAAe,CAAA;AAIrD;;;GAGG;AACH,eAAO,MAAM,MAAM,QAAO,MAIvB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { allowing, appOf } from './app.js';
|
|
|
6
6
|
import { limiter } from './limit.js';
|
|
7
7
|
import { startingGate } from './probes.js';
|
|
8
8
|
import { hold } from './request.js';
|
|
9
|
-
import {
|
|
9
|
+
import { closeStreams, drainOf, listening, portOf, serverOf, shutdown, } from './server.js';
|
|
10
10
|
import { closeUpgrades, upgrading } from './upgrade.js';
|
|
11
11
|
export { clientOf } from './client.js';
|
|
12
12
|
export { validate } from './validate.js';
|
|
@@ -19,6 +19,17 @@ const taken = new WeakMap();
|
|
|
19
19
|
const urls = new WeakMap();
|
|
20
20
|
const probing = new WeakMap();
|
|
21
21
|
const gated = new WeakMap();
|
|
22
|
+
const closings = new WeakMap();
|
|
23
|
+
/** The server's close, begun once; none before it listens. */
|
|
24
|
+
function closingOf(context) {
|
|
25
|
+
const server = servers.get(context);
|
|
26
|
+
if (server === undefined)
|
|
27
|
+
return undefined;
|
|
28
|
+
const begun = closings.get(context) ??
|
|
29
|
+
shutdown(server, context.logger, taken.get(context) ?? new Map());
|
|
30
|
+
closings.set(context, begun);
|
|
31
|
+
return begun;
|
|
32
|
+
}
|
|
22
33
|
const http = definePlugin({
|
|
23
34
|
name: 'http',
|
|
24
35
|
manifest,
|
|
@@ -64,6 +75,11 @@ const http = definePlugin({
|
|
|
64
75
|
closeUpgrades: (closing) => {
|
|
65
76
|
closeUpgrades(takers, taken.get(context), closing);
|
|
66
77
|
},
|
|
78
|
+
closeStreams: () => {
|
|
79
|
+
const server = servers.get(context);
|
|
80
|
+
if (server !== undefined)
|
|
81
|
+
closeStreams(server);
|
|
82
|
+
},
|
|
67
83
|
url: () => {
|
|
68
84
|
const url = urls.get(context);
|
|
69
85
|
if (url === undefined) {
|
|
@@ -109,26 +125,22 @@ const http = definePlugin({
|
|
|
109
125
|
probesOf(context).started = true;
|
|
110
126
|
},
|
|
111
127
|
// `/ready` answers 503, and the load balancer has `drain` to leave (D-204).
|
|
128
|
+
// Then answers the running requests before any plugin stops (D-395).
|
|
112
129
|
stopping: async (context) => {
|
|
113
130
|
const probes = probesOf(context);
|
|
114
131
|
probes.stopping = true;
|
|
115
132
|
if (probes.drain > 0) {
|
|
116
133
|
await new Promise((resolve) => setTimeout(resolve, probes.drain));
|
|
117
134
|
}
|
|
135
|
+
await closingOf(context)?.answered;
|
|
118
136
|
},
|
|
119
|
-
//
|
|
137
|
+
// Then waits for the streams and sockets the plugins' stop ended (D-59, D-106).
|
|
120
138
|
stop: async (context) => {
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
urls.delete(context);
|
|
127
|
-
probing.delete(context);
|
|
128
|
-
gated.delete(context);
|
|
129
|
-
const open = taken.get(context) ?? new Map();
|
|
130
|
-
taken.delete(context);
|
|
131
|
-
await closed(server, logger, open);
|
|
139
|
+
const closed = closingOf(context)?.closed;
|
|
140
|
+
for (const map of [servers, urls, probing, gated, taken, closings]) {
|
|
141
|
+
map.delete(context);
|
|
142
|
+
}
|
|
143
|
+
await closed;
|
|
132
144
|
},
|
|
133
145
|
});
|
|
134
146
|
/** The probes' state of the context `api` received. */
|
package/dist/server.d.ts
CHANGED
|
@@ -9,11 +9,28 @@ export declare function bodyLimitOf(option: number | undefined): number;
|
|
|
9
9
|
/** The `drain` option, in milliseconds: 5000 in production, 0 otherwise. */
|
|
10
10
|
export declare function drainOf(option: number | undefined): number;
|
|
11
11
|
export declare function listening(server: Server, port: number): Promise<number>;
|
|
12
|
+
/**
|
|
13
|
+
* What `work` gives, as a request `stop` waits for: a request until its
|
|
14
|
+
* handler returns, a stream going on after (D-395); an upgrade until its
|
|
15
|
+
* plugin takes it (D-399).
|
|
16
|
+
*/
|
|
17
|
+
export declare function counted<T>(server: Server, work: () => Promise<T>): Promise<T>;
|
|
18
|
+
/** The answer to a request that comes once the stop began, as `/ready`'s. */
|
|
19
|
+
export declare const stopping: {
|
|
20
|
+
readonly code: 'UNAVAILABLE';
|
|
21
|
+
readonly message: 'The application is stopping.';
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Ends `server`'s event streams, as maintenance does (D-205): their client
|
|
25
|
+
* reconnects, then gets the answer the server gives at that time.
|
|
26
|
+
*/
|
|
27
|
+
export declare function closeStreams(server: Server): void;
|
|
12
28
|
/** A Node server for `fetch`, Hono's handler. */
|
|
13
29
|
export declare function serverOf(fetch: Parameters<typeof getRequestListener>[0]): Server;
|
|
14
30
|
/**
|
|
15
|
-
* Stops accepting connections
|
|
16
|
-
*
|
|
31
|
+
* Stops accepting connections: `answered` once no request awaits its handler,
|
|
32
|
+
* before any plugin stops (D-395); `closed` once the streams and sockets the
|
|
33
|
+
* plugins' `stop` ends are closed (D-59). 10 seconds at most in all (D-106).
|
|
17
34
|
*/
|
|
18
|
-
export declare function
|
|
35
|
+
export declare function shutdown(server: Server, logger: Logger, taken: ReadonlyMap<Duplex, string>): Record<'answered' | 'closed', Promise<unknown>>;
|
|
19
36
|
//# 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":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAMvC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAGxC,iDAAiD;AACjD,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAWzD;AAED,gFAAgF;AAChF,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAM9D;AAED,4EAA4E;AAC5E,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAI1D;AAyDD,wBAAsB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAsB7E;
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAMvC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAGxC,iDAAiD;AACjD,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAWzD;AAED,gFAAgF;AAChF,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAM9D;AAED,4EAA4E;AAC5E,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAI1D;AAyDD,wBAAsB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAsB7E;AAoBD;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC9B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACpB,OAAO,CAAC,CAAC,CAAC,CASZ;AAED,6EAA6E;AAC7E,eAAO,MAAM,QAAQ;aACpB,IAAI,EAAE,aAAa;aACnB,OAAO,EAAE,8BAA8B;CAC9B,CAAA;AAaV;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAEjD;AAED,iDAAiD;AACjD,wBAAgB,QAAQ,CACvB,KAAK,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC,CAAC,CAAC,GAC7C,MAAM,CA+BR;AAKD;;;;GAIG;AACH,wBAAgB,QAAQ,CACvB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,MAAM,CAAC,UAAU,GAAG,QAAQ,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAyBjD"}
|
package/dist/server.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// The port the server listens on, and its listening (D-106); the body
|
|
2
2
|
// limit and the drain (D-223); its connection queue (D-276); its listeners
|
|
3
|
-
// (D-292); how long it waits for headers (D-348).
|
|
3
|
+
// (D-292); how long it waits for headers (D-348); its event streams (D-205).
|
|
4
4
|
import cluster from 'node:cluster';
|
|
5
|
-
import { createServer } from 'node:http';
|
|
5
|
+
import { createServer, ServerResponse } from 'node:http';
|
|
6
6
|
import { createServer as createListener, } from 'node:net';
|
|
7
7
|
import { getRequestListener } from '@hono/node-server';
|
|
8
8
|
import { Refused } from 'ultimabase';
|
|
@@ -100,13 +100,79 @@ export async function listening(server, port) {
|
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
102
|
* A client that never ends its headers holds a socket 10 s, not Node's 60;
|
|
103
|
-
* checked every second, not every 30.
|
|
103
|
+
* checked every second, not every 30. An idle connection stays 65 s, past a
|
|
104
|
+
* load balancer's 60, which would otherwise reuse one Node closed: a 502. A
|
|
105
|
+
* request's body arrives within 120 s, not 300, a mobile upload included.
|
|
104
106
|
*/
|
|
105
|
-
const waits = {
|
|
107
|
+
const waits = {
|
|
108
|
+
headersTimeout: 10_000,
|
|
109
|
+
connectionsCheckingInterval: 1000,
|
|
110
|
+
keepAliveTimeout: 65_000,
|
|
111
|
+
requestTimeout: 120_000,
|
|
112
|
+
};
|
|
113
|
+
/** A socket quiet that long, as a client that no longer reads, is closed. */
|
|
114
|
+
const idle = 120_000;
|
|
115
|
+
/** Each server's requests not answered yet, and who waits for none. */
|
|
116
|
+
const answering = new WeakMap();
|
|
117
|
+
/**
|
|
118
|
+
* What `work` gives, as a request `stop` waits for: a request until its
|
|
119
|
+
* handler returns, a stream going on after (D-395); an upgrade until its
|
|
120
|
+
* plugin takes it (D-399).
|
|
121
|
+
*/
|
|
122
|
+
export async function counted(server, work) {
|
|
123
|
+
const running = answering.get(server) ?? { count: 0 };
|
|
124
|
+
running.count += 1;
|
|
125
|
+
try {
|
|
126
|
+
return await work();
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
running.count -= 1;
|
|
130
|
+
if (running.count === 0)
|
|
131
|
+
running.none?.();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** The answer to a request that comes once the stop began, as `/ready`'s. */
|
|
135
|
+
export const stopping = {
|
|
136
|
+
code: 'UNAVAILABLE',
|
|
137
|
+
message: 'The application is stopping.',
|
|
138
|
+
};
|
|
139
|
+
/** Each server's event streams still open. */
|
|
140
|
+
const streams = new WeakMap();
|
|
141
|
+
/** `response` as an event stream `closeStreams` ends. */
|
|
142
|
+
function held(server, response) {
|
|
143
|
+
const open = streams.get(server) ?? new Set();
|
|
144
|
+
streams.set(server, open);
|
|
145
|
+
open.add(response);
|
|
146
|
+
response.once('close', () => open.delete(response));
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Ends `server`'s event streams, as maintenance does (D-205): their client
|
|
150
|
+
* reconnects, then gets the answer the server gives at that time.
|
|
151
|
+
*/
|
|
152
|
+
export function closeStreams(server) {
|
|
153
|
+
for (const response of streams.get(server) ?? [])
|
|
154
|
+
response.destroy();
|
|
155
|
+
}
|
|
106
156
|
/** A Node server for `fetch`, Hono's handler. */
|
|
107
157
|
export function serverOf(fetch) {
|
|
108
|
-
const listener = getRequestListener(
|
|
158
|
+
const listener = getRequestListener(async (request, env) => {
|
|
159
|
+
const answer = await counted(server, async () => fetch(request, env));
|
|
160
|
+
if (answer instanceof Response &&
|
|
161
|
+
answer.headers.get('content-type')?.startsWith('text/event-stream') ===
|
|
162
|
+
true &&
|
|
163
|
+
env.outgoing instanceof ServerResponse) {
|
|
164
|
+
held(server, env.outgoing);
|
|
165
|
+
}
|
|
166
|
+
return answer;
|
|
167
|
+
});
|
|
109
168
|
const server = createServer(waits, (request, response) => {
|
|
169
|
+
// Once `stop` began, it would run after the plugins it uses (D-399).
|
|
170
|
+
if (!server.listening) {
|
|
171
|
+
response.shouldKeepAlive = false;
|
|
172
|
+
response.writeHead(503, { 'content-type': 'application/json' });
|
|
173
|
+
response.end(JSON.stringify(stopping));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
110
176
|
// A request running when `stop` began leaves its connection idle after
|
|
111
177
|
// its response: close it, rather than wait for the client to.
|
|
112
178
|
response.once('finish', () => {
|
|
@@ -115,27 +181,40 @@ export function serverOf(fetch) {
|
|
|
115
181
|
});
|
|
116
182
|
void listener(request, response);
|
|
117
183
|
});
|
|
184
|
+
server.setTimeout(idle);
|
|
185
|
+
answering.set(server, { count: 0 });
|
|
118
186
|
return server;
|
|
119
187
|
}
|
|
120
188
|
/** How long `stop` waits for running requests: a stream never ends. */
|
|
121
189
|
const grace = 10_000;
|
|
122
190
|
/**
|
|
123
|
-
* Stops accepting connections
|
|
124
|
-
*
|
|
191
|
+
* Stops accepting connections: `answered` once no request awaits its handler,
|
|
192
|
+
* before any plugin stops (D-395); `closed` once the streams and sockets the
|
|
193
|
+
* plugins' `stop` ends are closed (D-59). 10 seconds at most in all (D-106).
|
|
125
194
|
*/
|
|
126
|
-
export
|
|
195
|
+
export function shutdown(server, logger, taken) {
|
|
196
|
+
const running = answering.get(server) ?? { count: 0 };
|
|
127
197
|
const cutting = setTimeout(() => {
|
|
128
198
|
logger.warn(`Stopped ${grace / 1000} s after stop began: closed the requests still running.`);
|
|
129
199
|
server.closeAllConnections();
|
|
130
200
|
for (const socket of taken.keys())
|
|
131
201
|
socket.destroy();
|
|
202
|
+
running.none?.();
|
|
132
203
|
}, grace);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
finally {
|
|
204
|
+
const shutting = [server, ...(others.get(server) ?? [])].map(shut);
|
|
205
|
+
server.closeIdleConnections();
|
|
206
|
+
// A connection still reading a body it was answered for turns idle later,
|
|
207
|
+
// with no event: it would stay open the keep-alive timeout, 65 s.
|
|
208
|
+
const idling = setInterval(() => server.closeIdleConnections(), 100);
|
|
209
|
+
const closed = Promise.all(shutting).finally(() => {
|
|
139
210
|
clearTimeout(cutting);
|
|
140
|
-
|
|
211
|
+
clearInterval(idling);
|
|
212
|
+
});
|
|
213
|
+
closed.catch(() => undefined); // Read by `stop`, once the plugins stopped.
|
|
214
|
+
const answered = new Promise((done) => {
|
|
215
|
+
running.none = done;
|
|
216
|
+
if (running.count === 0)
|
|
217
|
+
done();
|
|
218
|
+
});
|
|
219
|
+
return { answered, closed };
|
|
141
220
|
}
|
package/dist/upgrade.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;AAGxC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;
|
|
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;AAGxC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAIzC;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACvB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAA;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;CACjD;AAED,6EAA6E;AAC7E,MAAM,MAAM,MAAM,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAA;AAE/C,qEAAqE;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SAAS,CACxB,MAAM,EAAE,MAAM,EACd,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,SAAS,GAChE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAkD7B;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
|
@@ -2,6 +2,7 @@ import { STATUS_CODES } from 'node:http';
|
|
|
2
2
|
import { logFailure } from 'ultimabase/internal';
|
|
3
3
|
import { isOwnOrigin } from './address.js';
|
|
4
4
|
import { tooMany } from './limit.js';
|
|
5
|
+
import { counted, stopping } from './server.js';
|
|
5
6
|
/** The first gate's refusal of `request`, or `undefined` when all let it through. */
|
|
6
7
|
export async function answer(gates, request) {
|
|
7
8
|
for (const gate of gates) {
|
|
@@ -43,7 +44,11 @@ function requestOf(incoming) {
|
|
|
43
44
|
}
|
|
44
45
|
// Outside any catch, a URL a request refuses would end the process.
|
|
45
46
|
try {
|
|
46
|
-
|
|
47
|
+
const request = new Request(url, { headers });
|
|
48
|
+
// A host holding a path or a query would pick the plugin: refused, as
|
|
49
|
+
// `@hono/node-server` refuses it on a request.
|
|
50
|
+
const named = host.replace(/:\d+$/, '').toLowerCase();
|
|
51
|
+
return new URL(request.url).hostname === named ? request : undefined;
|
|
47
52
|
}
|
|
48
53
|
catch {
|
|
49
54
|
return undefined;
|
|
@@ -59,6 +64,10 @@ export function upgrading(server, { takers, gates, rateLimit, allows, addressOf,
|
|
|
59
64
|
server.on('upgrade', (incoming, socket, head) => {
|
|
60
65
|
// A client that leaves mid-answer must not stop the server.
|
|
61
66
|
socket.on('error', () => socket.destroy());
|
|
67
|
+
// Its plugin may have stopped already (D-399).
|
|
68
|
+
if (!server.listening) {
|
|
69
|
+
return refuse(socket, 503, stopping.code, stopping.message);
|
|
70
|
+
}
|
|
62
71
|
const request = requestOf(incoming);
|
|
63
72
|
if (request === undefined) {
|
|
64
73
|
return refuse(socket, 400, 'BAD_REQUEST', 'The upgrade has no URL.');
|
|
@@ -77,7 +86,7 @@ export function upgrading(server, { takers, gates, rateLimit, allows, addressOf,
|
|
|
77
86
|
// Open while the gate answers, so `stop` waits for it too.
|
|
78
87
|
open.set(socket, pathname);
|
|
79
88
|
socket.once('close', () => open.delete(socket));
|
|
80
|
-
void (async () => {
|
|
89
|
+
void counted(server, async () => {
|
|
81
90
|
try {
|
|
82
91
|
const address = addressOf({ incoming });
|
|
83
92
|
const limit = async () => rateLimit(request, address);
|
|
@@ -91,7 +100,7 @@ export function upgrading(server, { takers, gates, rateLimit, allows, addressOf,
|
|
|
91
100
|
logFailure(logger, error, 'An upgrade failed.');
|
|
92
101
|
refuse(socket, 500, 'INTERNAL', 'Internal error.');
|
|
93
102
|
}
|
|
94
|
-
})
|
|
103
|
+
});
|
|
95
104
|
});
|
|
96
105
|
return open;
|
|
97
106
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimabase/http",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
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",
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
"ipaddr.js": "2.5.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"ultimabase": "0.
|
|
41
|
+
"ultimabase": "^1.0.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"ultimabase": "0.
|
|
44
|
+
"ultimabase": "1.0.0",
|
|
45
45
|
"vite": "8.3.4",
|
|
46
46
|
"vitest": "5.0.5"
|
|
47
47
|
}
|
package/src/app.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { HTTPException } from 'hono/http-exception'
|
|
|
11
11
|
import type { ContentfulStatusCode } from 'hono/utils/http-status'
|
|
12
12
|
import { getPath } from 'hono/utils/url'
|
|
13
13
|
import type { Caller, ErrorCode, Logger } from 'ultimabase'
|
|
14
|
-
import { isLocalHost, PublicError } from 'ultimabase'
|
|
14
|
+
import { isLocalHost, PublicError, Refused } from 'ultimabase'
|
|
15
15
|
import { logFailure } from 'ultimabase/internal'
|
|
16
16
|
import { isOwnOrigin } from './address.ts'
|
|
17
17
|
import type { Limiter, RateLimit } from './limit.ts'
|
|
@@ -101,10 +101,22 @@ function refused(error: HTTPException, c: Context): Response {
|
|
|
101
101
|
return c.json({ code, message }, error.status)
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Whether `error` came from the client leaving: its body read cut, or the
|
|
106
|
+
* request's signal, as a `fetch` given it throws.
|
|
107
|
+
*/
|
|
108
|
+
const isLeaving = (error: unknown, signal: AbortSignal) =>
|
|
109
|
+
signal.aborted &&
|
|
110
|
+
(error === signal.reason ||
|
|
111
|
+
(error instanceof Error && Reflect.get(error, 'code') === 'ECONNRESET'))
|
|
112
|
+
|
|
104
113
|
/** A `PublicError` as is; any other error masked, logged with its id. */
|
|
105
114
|
function failed(error: unknown, c: Context, logger: Logger): Response {
|
|
106
|
-
//
|
|
107
|
-
|
|
115
|
+
// Its request failed, not the server (499, as nginx); a failure of the
|
|
116
|
+
// server's own meanwhile is still logged (D-108).
|
|
117
|
+
if (isLeaving(error, c.req.raw.signal)) {
|
|
118
|
+
return new Response(null, { status: 499 })
|
|
119
|
+
}
|
|
108
120
|
if (error instanceof HTTPException) return refused(error, c)
|
|
109
121
|
if (error instanceof PublicError) {
|
|
110
122
|
const { code, message, details } = error
|
|
@@ -176,6 +188,34 @@ function stamp(c: Context, name: string, value: string, append = false): void {
|
|
|
176
188
|
}
|
|
177
189
|
}
|
|
178
190
|
|
|
191
|
+
/**
|
|
192
|
+
* Refuses an application route that answers a plugin's path under
|
|
193
|
+
* `/_ultimabase/`, as `PUT /:a/:b` would the maintenance's switch: it comes
|
|
194
|
+
* first, so it would run in the plugin's place (D-205).
|
|
195
|
+
*/
|
|
196
|
+
function refuseShadows(
|
|
197
|
+
own: readonly Routes[],
|
|
198
|
+
mounted: readonly Routes[],
|
|
199
|
+
): void {
|
|
200
|
+
const app = new Hono()
|
|
201
|
+
for (const routes of own) {
|
|
202
|
+
for (const { method, path } of routes.routes) {
|
|
203
|
+
if (method !== 'ALL') app.on(method, path, (c) => c.body(null))
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
for (const routes of mounted) {
|
|
207
|
+
for (const { method, path } of routes.routes) {
|
|
208
|
+
if (method === 'ALL' || !path.startsWith('/_ultimabase/')) continue
|
|
209
|
+
const [[first]] = app.router.match(method, path)
|
|
210
|
+
if (first === undefined) continue
|
|
211
|
+
const [, route] = first[0]
|
|
212
|
+
throw new Refused(
|
|
213
|
+
`The application's route ${route.method} ${route.path} answers ${method} ${path}, which a plugin serves: give it another path.`,
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
179
219
|
/**
|
|
180
220
|
* Reads `NODE_ENV` once, when the application starts. The application's
|
|
181
221
|
* routes come before those plugins mounted (D-107).
|
|
@@ -198,6 +238,10 @@ export function appOf(
|
|
|
198
238
|
logger: Logger,
|
|
199
239
|
): Hono<HttpEnv> {
|
|
200
240
|
const local = process.env['NODE_ENV'] !== 'production'
|
|
241
|
+
refuseShadows(options.routes ?? [], [
|
|
242
|
+
...mounted.map((mount) => mount.routes),
|
|
243
|
+
...(local ? [resetRoutes] : []),
|
|
244
|
+
])
|
|
201
245
|
const allows = allowing(options)
|
|
202
246
|
const maxSize = bodyLimitOf(options.bodyLimit)
|
|
203
247
|
const app = new Hono<HttpEnv>({ getPath: pathOf })
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import type { Server } from 'node:http'
|
|
4
4
|
import type { Duplex } from 'node:stream'
|
|
5
5
|
import { Hono } from 'hono'
|
|
6
|
-
import type { Caller, PluginOf, PluginTypes } from 'ultimabase'
|
|
6
|
+
import type { Caller, Logger, PluginOf, PluginTypes } from 'ultimabase'
|
|
7
7
|
import { definePlugin } from 'ultimabase'
|
|
8
8
|
import manifest from '../package.json' with { type: 'json' }
|
|
9
9
|
import type { HttpEnv, HttpOptions, Mounted, Routes } from './app.ts'
|
|
@@ -13,7 +13,14 @@ import { limiter } from './limit.ts'
|
|
|
13
13
|
import type { Probes, ReadyCheck } from './probes.ts'
|
|
14
14
|
import { startingGate } from './probes.ts'
|
|
15
15
|
import { hold } from './request.ts'
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
closeStreams,
|
|
18
|
+
drainOf,
|
|
19
|
+
listening,
|
|
20
|
+
portOf,
|
|
21
|
+
serverOf,
|
|
22
|
+
shutdown,
|
|
23
|
+
} from './server.ts'
|
|
17
24
|
import type { Accept, Close, Closing, Gate, Taker } from './upgrade.ts'
|
|
18
25
|
import { closeUpgrades, upgrading } from './upgrade.ts'
|
|
19
26
|
|
|
@@ -47,6 +54,11 @@ export interface HttpApi {
|
|
|
47
54
|
* those of a plugin that gave no `close` are cut (D-166).
|
|
48
55
|
*/
|
|
49
56
|
closeUpgrades(closing: Closing): void
|
|
57
|
+
/**
|
|
58
|
+
* Ends the open event streams (`text/event-stream` answers): each client
|
|
59
|
+
* reconnects, as maintenance needs (D-205).
|
|
60
|
+
*/
|
|
61
|
+
closeStreams(): void
|
|
50
62
|
/**
|
|
51
63
|
* Adds a check `GET /ready` runs on each request: it answers 503 while
|
|
52
64
|
* one fails; call it from your `api` (D-204).
|
|
@@ -83,6 +95,18 @@ const taken = new WeakMap<object, ReadonlyMap<Duplex, string>>()
|
|
|
83
95
|
const urls = new WeakMap<object, string>()
|
|
84
96
|
const probing = new WeakMap<object, Probes & { drain: number }>()
|
|
85
97
|
const gated = new WeakMap<object, Gate[]>()
|
|
98
|
+
const closings = new WeakMap<object, ReturnType<typeof shutdown>>()
|
|
99
|
+
|
|
100
|
+
/** The server's close, begun once; none before it listens. */
|
|
101
|
+
function closingOf(context: { readonly logger: Logger }) {
|
|
102
|
+
const server = servers.get(context)
|
|
103
|
+
if (server === undefined) return undefined
|
|
104
|
+
const begun =
|
|
105
|
+
closings.get(context) ??
|
|
106
|
+
shutdown(server, context.logger, taken.get(context) ?? new Map())
|
|
107
|
+
closings.set(context, begun)
|
|
108
|
+
return begun
|
|
109
|
+
}
|
|
86
110
|
|
|
87
111
|
const http = definePlugin<Http>({
|
|
88
112
|
name: 'http',
|
|
@@ -130,6 +154,10 @@ const http = definePlugin<Http>({
|
|
|
130
154
|
closeUpgrades: (closing) => {
|
|
131
155
|
closeUpgrades(takers, taken.get(context), closing)
|
|
132
156
|
},
|
|
157
|
+
closeStreams: () => {
|
|
158
|
+
const server = servers.get(context)
|
|
159
|
+
if (server !== undefined) closeStreams(server)
|
|
160
|
+
},
|
|
133
161
|
url: () => {
|
|
134
162
|
const url = urls.get(context)
|
|
135
163
|
if (url === undefined) {
|
|
@@ -186,25 +214,22 @@ const http = definePlugin<Http>({
|
|
|
186
214
|
probesOf(context).started = true
|
|
187
215
|
},
|
|
188
216
|
// `/ready` answers 503, and the load balancer has `drain` to leave (D-204).
|
|
217
|
+
// Then answers the running requests before any plugin stops (D-395).
|
|
189
218
|
stopping: async (context) => {
|
|
190
219
|
const probes = probesOf(context)
|
|
191
220
|
probes.stopping = true
|
|
192
221
|
if (probes.drain > 0) {
|
|
193
222
|
await new Promise((resolve) => setTimeout(resolve, probes.drain))
|
|
194
223
|
}
|
|
224
|
+
await closingOf(context)?.answered
|
|
195
225
|
},
|
|
196
|
-
//
|
|
226
|
+
// Then waits for the streams and sockets the plugins' stop ended (D-59, D-106).
|
|
197
227
|
stop: async (context) => {
|
|
198
|
-
const
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
probing.delete(context)
|
|
204
|
-
gated.delete(context)
|
|
205
|
-
const open = taken.get(context) ?? new Map<Duplex, string>()
|
|
206
|
-
taken.delete(context)
|
|
207
|
-
await closed(server, logger, open)
|
|
228
|
+
const closed = closingOf(context)?.closed
|
|
229
|
+
for (const map of [servers, urls, probing, gated, taken, closings]) {
|
|
230
|
+
map.delete(context)
|
|
231
|
+
}
|
|
232
|
+
await closed
|
|
208
233
|
},
|
|
209
234
|
})
|
|
210
235
|
|
package/src/server.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// The port the server listens on, and its listening (D-106); the body
|
|
2
2
|
// limit and the drain (D-223); its connection queue (D-276); its listeners
|
|
3
|
-
// (D-292); how long it waits for headers (D-348).
|
|
3
|
+
// (D-292); how long it waits for headers (D-348); its event streams (D-205).
|
|
4
4
|
import cluster from 'node:cluster'
|
|
5
5
|
import type { Server } from 'node:http'
|
|
6
|
-
import { createServer } from 'node:http'
|
|
6
|
+
import { createServer, ServerResponse } from 'node:http'
|
|
7
7
|
import {
|
|
8
8
|
createServer as createListener,
|
|
9
9
|
type Server as Listener,
|
|
@@ -124,16 +124,90 @@ export async function listening(server: Server, port: number): Promise<number> {
|
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
126
|
* A client that never ends its headers holds a socket 10 s, not Node's 60;
|
|
127
|
-
* checked every second, not every 30.
|
|
127
|
+
* checked every second, not every 30. An idle connection stays 65 s, past a
|
|
128
|
+
* load balancer's 60, which would otherwise reuse one Node closed: a 502. A
|
|
129
|
+
* request's body arrives within 120 s, not 300, a mobile upload included.
|
|
128
130
|
*/
|
|
129
|
-
const waits = {
|
|
131
|
+
const waits = {
|
|
132
|
+
headersTimeout: 10_000,
|
|
133
|
+
connectionsCheckingInterval: 1000,
|
|
134
|
+
keepAliveTimeout: 65_000,
|
|
135
|
+
requestTimeout: 120_000,
|
|
136
|
+
}
|
|
137
|
+
/** A socket quiet that long, as a client that no longer reads, is closed. */
|
|
138
|
+
const idle = 120_000
|
|
139
|
+
|
|
140
|
+
/** Each server's requests not answered yet, and who waits for none. */
|
|
141
|
+
const answering = new WeakMap<Server, { count: number; none?: () => void }>()
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* What `work` gives, as a request `stop` waits for: a request until its
|
|
145
|
+
* handler returns, a stream going on after (D-395); an upgrade until its
|
|
146
|
+
* plugin takes it (D-399).
|
|
147
|
+
*/
|
|
148
|
+
export async function counted<T>(
|
|
149
|
+
server: Server,
|
|
150
|
+
work: () => Promise<T>,
|
|
151
|
+
): Promise<T> {
|
|
152
|
+
const running = answering.get(server) ?? { count: 0 }
|
|
153
|
+
running.count += 1
|
|
154
|
+
try {
|
|
155
|
+
return await work()
|
|
156
|
+
} finally {
|
|
157
|
+
running.count -= 1
|
|
158
|
+
if (running.count === 0) running.none?.()
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The answer to a request that comes once the stop began, as `/ready`'s. */
|
|
163
|
+
export const stopping = {
|
|
164
|
+
code: 'UNAVAILABLE',
|
|
165
|
+
message: 'The application is stopping.',
|
|
166
|
+
} as const
|
|
167
|
+
|
|
168
|
+
/** Each server's event streams still open. */
|
|
169
|
+
const streams = new WeakMap<Server, Set<ServerResponse>>()
|
|
170
|
+
|
|
171
|
+
/** `response` as an event stream `closeStreams` ends. */
|
|
172
|
+
function held(server: Server, response: ServerResponse): void {
|
|
173
|
+
const open = streams.get(server) ?? new Set<ServerResponse>()
|
|
174
|
+
streams.set(server, open)
|
|
175
|
+
open.add(response)
|
|
176
|
+
response.once('close', () => open.delete(response))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Ends `server`'s event streams, as maintenance does (D-205): their client
|
|
181
|
+
* reconnects, then gets the answer the server gives at that time.
|
|
182
|
+
*/
|
|
183
|
+
export function closeStreams(server: Server): void {
|
|
184
|
+
for (const response of streams.get(server) ?? []) response.destroy()
|
|
185
|
+
}
|
|
130
186
|
|
|
131
187
|
/** A Node server for `fetch`, Hono's handler. */
|
|
132
188
|
export function serverOf(
|
|
133
189
|
fetch: Parameters<typeof getRequestListener>[0],
|
|
134
190
|
): Server {
|
|
135
|
-
const listener = getRequestListener(
|
|
191
|
+
const listener = getRequestListener(async (request, env) => {
|
|
192
|
+
const answer = await counted(server, async () => fetch(request, env))
|
|
193
|
+
if (
|
|
194
|
+
answer instanceof Response &&
|
|
195
|
+
answer.headers.get('content-type')?.startsWith('text/event-stream') ===
|
|
196
|
+
true &&
|
|
197
|
+
env.outgoing instanceof ServerResponse
|
|
198
|
+
) {
|
|
199
|
+
held(server, env.outgoing)
|
|
200
|
+
}
|
|
201
|
+
return answer
|
|
202
|
+
})
|
|
136
203
|
const server = createServer(waits, (request, response) => {
|
|
204
|
+
// Once `stop` began, it would run after the plugins it uses (D-399).
|
|
205
|
+
if (!server.listening) {
|
|
206
|
+
response.shouldKeepAlive = false
|
|
207
|
+
response.writeHead(503, { 'content-type': 'application/json' })
|
|
208
|
+
response.end(JSON.stringify(stopping))
|
|
209
|
+
return
|
|
210
|
+
}
|
|
137
211
|
// A request running when `stop` began leaves its connection idle after
|
|
138
212
|
// its response: close it, rather than wait for the client to.
|
|
139
213
|
response.once('finish', () => {
|
|
@@ -141,6 +215,8 @@ export function serverOf(
|
|
|
141
215
|
})
|
|
142
216
|
void listener(request, response)
|
|
143
217
|
})
|
|
218
|
+
server.setTimeout(idle)
|
|
219
|
+
answering.set(server, { count: 0 })
|
|
144
220
|
return server
|
|
145
221
|
}
|
|
146
222
|
|
|
@@ -148,26 +224,37 @@ export function serverOf(
|
|
|
148
224
|
const grace = 10_000
|
|
149
225
|
|
|
150
226
|
/**
|
|
151
|
-
* Stops accepting connections
|
|
152
|
-
*
|
|
227
|
+
* Stops accepting connections: `answered` once no request awaits its handler,
|
|
228
|
+
* before any plugin stops (D-395); `closed` once the streams and sockets the
|
|
229
|
+
* plugins' `stop` ends are closed (D-59). 10 seconds at most in all (D-106).
|
|
153
230
|
*/
|
|
154
|
-
export
|
|
231
|
+
export function shutdown(
|
|
155
232
|
server: Server,
|
|
156
233
|
logger: Logger,
|
|
157
234
|
taken: ReadonlyMap<Duplex, string>,
|
|
158
|
-
): Promise<
|
|
235
|
+
): Record<'answered' | 'closed', Promise<unknown>> {
|
|
236
|
+
const running = answering.get(server) ?? { count: 0 }
|
|
159
237
|
const cutting = setTimeout(() => {
|
|
160
238
|
logger.warn(
|
|
161
239
|
`Stopped ${grace / 1000} s after stop began: closed the requests still running.`,
|
|
162
240
|
)
|
|
163
241
|
server.closeAllConnections()
|
|
164
242
|
for (const socket of taken.keys()) socket.destroy()
|
|
243
|
+
running.none?.()
|
|
165
244
|
}, grace)
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
245
|
+
const shutting = [server, ...(others.get(server) ?? [])].map(shut)
|
|
246
|
+
server.closeIdleConnections()
|
|
247
|
+
// A connection still reading a body it was answered for turns idle later,
|
|
248
|
+
// with no event: it would stay open the keep-alive timeout, 65 s.
|
|
249
|
+
const idling = setInterval(() => server.closeIdleConnections(), 100)
|
|
250
|
+
const closed = Promise.all(shutting).finally(() => {
|
|
171
251
|
clearTimeout(cutting)
|
|
172
|
-
|
|
252
|
+
clearInterval(idling)
|
|
253
|
+
})
|
|
254
|
+
closed.catch(() => undefined) // Read by `stop`, once the plugins stopped.
|
|
255
|
+
const answered = new Promise<void>((done) => {
|
|
256
|
+
running.none = done
|
|
257
|
+
if (running.count === 0) done()
|
|
258
|
+
})
|
|
259
|
+
return { answered, closed }
|
|
173
260
|
}
|
package/src/upgrade.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { logFailure } from 'ultimabase/internal'
|
|
|
10
10
|
import { isOwnOrigin } from './address.ts'
|
|
11
11
|
import type { Limiter } from './limit.ts'
|
|
12
12
|
import { tooMany } from './limit.ts'
|
|
13
|
+
import { counted, stopping } from './server.ts'
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* An upgrade a plugin takes: Node's request, its socket and first bytes, as
|
|
@@ -118,7 +119,11 @@ function requestOf(incoming: IncomingMessage): Request | undefined {
|
|
|
118
119
|
}
|
|
119
120
|
// Outside any catch, a URL a request refuses would end the process.
|
|
120
121
|
try {
|
|
121
|
-
|
|
122
|
+
const request = new Request(url, { headers })
|
|
123
|
+
// A host holding a path or a query would pick the plugin: refused, as
|
|
124
|
+
// `@hono/node-server` refuses it on a request.
|
|
125
|
+
const named = host.replace(/:\d+$/, '').toLowerCase()
|
|
126
|
+
return new URL(request.url).hostname === named ? request : undefined
|
|
122
127
|
} catch {
|
|
123
128
|
return undefined
|
|
124
129
|
}
|
|
@@ -137,6 +142,10 @@ export function upgrading(
|
|
|
137
142
|
server.on('upgrade', (incoming: IncomingMessage, socket: Duplex, head) => {
|
|
138
143
|
// A client that leaves mid-answer must not stop the server.
|
|
139
144
|
socket.on('error', () => socket.destroy())
|
|
145
|
+
// Its plugin may have stopped already (D-399).
|
|
146
|
+
if (!server.listening) {
|
|
147
|
+
return refuse(socket, 503, stopping.code, stopping.message)
|
|
148
|
+
}
|
|
140
149
|
const request = requestOf(incoming)
|
|
141
150
|
if (request === undefined) {
|
|
142
151
|
return refuse(socket, 400, 'BAD_REQUEST', 'The upgrade has no URL.')
|
|
@@ -162,7 +171,7 @@ export function upgrading(
|
|
|
162
171
|
// Open while the gate answers, so `stop` waits for it too.
|
|
163
172
|
open.set(socket, pathname)
|
|
164
173
|
socket.once('close', () => open.delete(socket))
|
|
165
|
-
void (async () => {
|
|
174
|
+
void counted(server, async () => {
|
|
166
175
|
try {
|
|
167
176
|
const address = addressOf({ incoming })
|
|
168
177
|
const limit = async () => rateLimit(request, address)
|
|
@@ -175,7 +184,7 @@ export function upgrading(
|
|
|
175
184
|
logFailure(logger, error, 'An upgrade failed.')
|
|
176
185
|
refuse(socket, 500, 'INTERNAL', 'Internal error.')
|
|
177
186
|
}
|
|
178
|
-
})
|
|
187
|
+
})
|
|
179
188
|
})
|
|
180
189
|
return open
|
|
181
190
|
}
|