@plitzi/sdk-kernel 0.0.0-stage → 0.39.2
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 +10 -0
- package/README.md +80 -2
- package/dist/build/esbuild.d.ts +16 -0
- package/dist/build/index.d.ts +1 -0
- package/dist/build/index.mjs +35 -0
- package/dist/cache/CompressedFileCache.d.ts +18 -0
- package/dist/cache/TtlCache.d.ts +65 -0
- package/dist/cache/index.d.ts +3 -0
- package/dist/cache/index.mjs +1 -0
- package/dist/chunks/TtlCache-J_jG9oL9.mjs +200 -0
- package/dist/chunks/channel-DnVf7PJB.mjs +190 -0
- package/dist/chunks/csrf-BLWcaYOV.mjs +295 -0
- package/dist/chunks/link-C9wyDS9H.mjs +124 -0
- package/dist/chunks/query-DGbrN3mN.mjs +20 -0
- package/dist/chunks/scopes-Co6ZjhAm.mjs +46 -0
- package/dist/chunks/serverLog-BKaKjjc4.mjs +137 -0
- package/dist/chunks/session-B-dTEjeG.mjs +253 -0
- package/dist/chunks/sessionActivity-Ch7xuQ_o.mjs +43 -0
- package/dist/chunks/spaceDeployment-J2IMLfij.mjs +76 -0
- package/dist/chunks/stage-y6deE9LJ.mjs +345 -0
- package/dist/chunks/supervisor-Dh34v1vq.mjs +117 -0
- package/dist/chunks/tail-D-USrZIR.mjs +55 -0
- package/dist/chunks/tokens-CBd5tXut.mjs +262 -0
- package/dist/data/index.d.ts +2 -0
- package/dist/data/index.mjs +68 -0
- package/dist/data/projectData.d.ts +21 -0
- package/dist/deployment/hostDeployment.d.ts +8 -0
- package/dist/deployment/index.d.ts +3 -0
- package/dist/deployment/index.mjs +19 -0
- package/dist/deployment/spaceDeployment.d.ts +11 -0
- package/dist/engine/index.d.ts +2 -0
- package/dist/engine/index.mjs +1 -0
- package/dist/engine/page.d.ts +49 -0
- package/dist/engine/rsc.d.ts +32 -0
- package/dist/files/atomicFile.d.ts +10 -0
- package/dist/files/index.d.ts +1 -0
- package/dist/files/index.mjs +32 -0
- package/dist/http/bearer.d.ts +5 -0
- package/dist/http/buildResponseHelpers.d.ts +23 -0
- package/dist/http/closeOnSignals.d.ts +28 -0
- package/dist/http/compress.d.ts +20 -0
- package/dist/http/dispatcher.d.ts +15 -0
- package/dist/http/drain.d.ts +18 -0
- package/dist/http/fleet/channel.d.ts +103 -0
- package/dist/http/fleet/link.d.ts +28 -0
- package/dist/http/fleet/role.d.ts +46 -0
- package/dist/http/fleet/supervisor.d.ts +29 -0
- package/dist/http/freePort.d.ts +24 -0
- package/dist/http/health.d.ts +21 -0
- package/dist/http/index.d.ts +48 -0
- package/dist/http/index.mjs +1313 -0
- package/dist/http/middlewares/auth.d.ts +2 -0
- package/dist/http/middlewares/basicAuth.d.ts +6 -0
- package/dist/http/mimeTypes.d.ts +13 -0
- package/dist/http/navigation.d.ts +14 -0
- package/dist/http/onAbort.d.ts +12 -0
- package/dist/http/probes.d.ts +4 -0
- package/dist/http/requestParser.d.ts +22 -0
- package/dist/http/runMiddlewares.d.ts +6 -0
- package/dist/http/securityHeaders.d.ts +18 -0
- package/dist/http/server.d.ts +39 -0
- package/dist/http/socketResponse.d.ts +18 -0
- package/dist/http/sse.d.ts +26 -0
- package/dist/http/stages/crossSiteWrites.d.ts +24 -0
- package/dist/http/stages/health.d.ts +11 -0
- package/dist/http/stages/middlewares.d.ts +3 -0
- package/dist/http/stages/static.d.ts +4 -0
- package/dist/http/staticFiles.d.ts +3 -0
- package/dist/http/stopping.d.ts +11 -0
- package/dist/http/transports.d.ts +31 -0
- package/dist/http/types.d.ts +29 -0
- package/dist/http/unusedPort.d.ts +10 -0
- package/dist/http/webExchange.d.ts +18 -0
- package/dist/http/wsFrame.d.ts +3 -0
- package/dist/identity/accounts.d.ts +278 -0
- package/dist/identity/authorize.d.ts +96 -0
- package/dist/identity/credentials.d.ts +40 -0
- package/dist/identity/csrf.d.ts +139 -0
- package/dist/identity/debugAuthorization.d.ts +24 -0
- package/dist/identity/domains.d.ts +41 -0
- package/dist/identity/identity.d.ts +119 -0
- package/dist/identity/index.d.ts +21 -0
- package/dist/identity/index.mjs +731 -0
- package/dist/identity/passwords.d.ts +15 -0
- package/dist/identity/previewToken.d.ts +21 -0
- package/dist/identity/requestIdentity.d.ts +14 -0
- package/dist/identity/scopes.d.ts +56 -0
- package/dist/identity/session.d.ts +185 -0
- package/dist/identity/sessionActivity.d.ts +11 -0
- package/dist/identity/spaceTokens.d.ts +174 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.mjs +1 -0
- package/dist/log/index.d.ts +3 -0
- package/dist/log/index.mjs +3 -0
- package/dist/log/serverLog.d.ts +49 -0
- package/dist/log/tail.d.ts +43 -0
- package/dist/mysql/accounts.d.ts +4 -0
- package/dist/mysql/admin.d.ts +58 -0
- package/dist/mysql/config.d.ts +82 -0
- package/dist/mysql/identities.d.ts +35 -0
- package/dist/mysql/index.d.ts +7 -0
- package/dist/mysql/index.mjs +1203 -0
- package/dist/mysql/pool.d.ts +3 -0
- package/dist/mysql/query.d.ts +12 -0
- package/dist/mysql/schema.d.ts +43 -0
- package/dist/mysql/sessions.d.ts +23 -0
- package/dist/mysql/spaceTokens.d.ts +11 -0
- package/dist/mysql/store.d.ts +66 -0
- package/dist/outbound/index.d.ts +4 -0
- package/dist/outbound/index.mjs +314 -0
- package/dist/outbound/inside.d.ts +36 -0
- package/dist/outbound/outboundGuard.d.ts +69 -0
- package/dist/pubsub/index.d.ts +3 -0
- package/dist/pubsub/index.mjs +76 -0
- package/dist/pubsub/memoryPubSub.d.ts +10 -0
- package/dist/pubsub/redisPubSub.d.ts +24 -0
- package/dist/stats/index.d.ts +9 -0
- package/dist/stats/index.mjs +228 -0
- package/dist/stats/load.d.ts +31 -0
- package/dist/stats/memory.d.ts +20 -0
- package/dist/stats/pools.d.ts +15 -0
- package/dist/stats/recorder.d.ts +34 -0
- package/dist/stats/stage.d.ts +15 -0
- package/dist/stats/types.d.ts +309 -0
- package/dist/testing/certificates.d.ts +21 -0
- package/dist/testing/fleet.d.ts +28 -0
- package/dist/testing/goneRuns.d.ts +5 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.mjs +270 -0
- package/dist/testing/keys.d.ts +2 -0
- package/dist/testing/runNames.d.ts +10 -0
- package/dist/tokens/index.d.ts +2 -0
- package/dist/tokens/index.mjs +1 -0
- package/dist/tokens/tokens.d.ts +190 -0
- package/dist/types/adapters.d.ts +388 -0
- package/dist/types/config.d.ts +240 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/log.d.ts +119 -0
- package/dist/types/oauth.d.ts +246 -0
- package/dist/types/pipeline.d.ts +239 -0
- package/package.json +67 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const MIME_TYPES: Record<string, string>;
|
|
2
|
+
export declare const IMMUTABLE_EXTS: Set<string>;
|
|
3
|
+
export declare const getMimeType: (filePath: string) => string;
|
|
4
|
+
/**
|
|
5
|
+
* How long a browser may keep a file without asking again.
|
|
6
|
+
*
|
|
7
|
+
* `versioned` is for a directory whose URLs change with their content — the SDK bundle and the plugins, addressed
|
|
8
|
+
* with `?v=` — where a script or a stylesheet never changes under its URL and is kept for a year. `revalidate` is for
|
|
9
|
+
* files a project keeps by name and may rewrite while it runs (`public/`): kept, but asked about every time, which
|
|
10
|
+
* the `ETag` answers with an empty `304` when nothing changed.
|
|
11
|
+
*/
|
|
12
|
+
export type StaticCachePolicy = 'versioned' | 'revalidate';
|
|
13
|
+
export declare const getCacheControl: (filePath: string, policy?: StaticCachePolicy) => string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SSRRequest } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* A full-page form submission, as opposed to a script's fetch.
|
|
4
|
+
*
|
|
5
|
+
* The difference decides how an auth flow answers: a browser navigating away from a page it has already left shows
|
|
6
|
+
* its own error page for a bodyless 401, and stays on the stale one for a 204. Either way the visitor sees the wrong
|
|
7
|
+
* thing, so a navigation is answered with a redirect and the view re-renders through a GET. A fetch wants the body.
|
|
8
|
+
*/
|
|
9
|
+
export declare const isNavigation: (req: SSRRequest) => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Same-origin only: absolute URLs and protocol-relative `//host` are refused, to avoid an open redirect — and so is
|
|
12
|
+
* `/\host`, which a browser reads as `//host`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const safeRedirectTarget: (req: SSRRequest, param?: string) => string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subscribes to a signal, and covers the case every hand-rolled subscription misses: one that is ALREADY aborted.
|
|
3
|
+
*
|
|
4
|
+
* `addEventListener('abort', …)` on a signal that has already fired never calls back — the event is long gone — so
|
|
5
|
+
* a listener attached after any `await` is a cancellation silently dropped. That window is not theoretical: an
|
|
6
|
+
* action is READ before its run begins, and a page that gave up during that read left the run to carry on holding
|
|
7
|
+
* a slot and a connection for nobody.
|
|
8
|
+
*
|
|
9
|
+
* Returns the unsubscribe, so a caller that outlives the signal does not leak a listener onto it.
|
|
10
|
+
*/
|
|
11
|
+
export declare const onAbort: (signal: AbortSignal | undefined, stop: () => void) => (() => void);
|
|
12
|
+
export default onAbort;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Where a process answers its stats (`statsStage`): a path of the kernel's, read by the pools' reader. */
|
|
2
|
+
export declare const STATS_PATH = "/_stats";
|
|
3
|
+
/** Where it answers the last lines of its log, behind the same secret (`readLogTail`). */
|
|
4
|
+
export declare const STATS_LOGS_PATH = "/_stats/logs";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SSRRequest } from '../types';
|
|
2
|
+
import { IncomingMessage } from 'node:http';
|
|
3
|
+
export declare const parseRequest: (raw: IncomingMessage) => SSRRequest;
|
|
4
|
+
/**
|
|
5
|
+
* The host the request was addressed to, port included (`localhost:8080`): HTTP/2's `:authority` — an HTTP/2
|
|
6
|
+
* request has no `Host` header at all — else `Host`. What names this origin's cookies (`debugCookieName` and the
|
|
7
|
+
* rest carry the port), so a server reached over HTTP/2 must read it here, never `req.headers.host`.
|
|
8
|
+
*/
|
|
9
|
+
export declare const requestAuthority: (req: Pick<SSRRequest, "headers">) => string;
|
|
10
|
+
/** The public origin the request was addressed to (proxy-aware via x-forwarded-proto, port included), or an empty
|
|
11
|
+
* string when the request carries no usable authority. */
|
|
12
|
+
export declare const requestOrigin: (req: SSRRequest) => string;
|
|
13
|
+
/** The address the request came from, seen through the proxies a deployment sits behind: Cloudflare states the
|
|
14
|
+
* peer it accepted in `cf-connecting-ip`, the ingress in `x-real-ip`, and `x-forwarded-for` lists the chain with
|
|
15
|
+
* the original client first. All three are plain headers a direct client can forge, so this is log/diagnostic
|
|
16
|
+
* material — never an authorisation input. Falls back to the socket peer, which no client controls. */
|
|
17
|
+
export declare const clientIp: (raw: IncomingMessage, req: SSRRequest) => string;
|
|
18
|
+
/** The proxies' account of the client address, without the socket — for a caller that has headers and nothing else. */
|
|
19
|
+
export declare const forwardedIp: (headers: Record<string, string | string[] | undefined>) => string;
|
|
20
|
+
/** A request's body as it arrived, byte for byte — what a binary upload to a function's route needs. */
|
|
21
|
+
export declare const readRawBytes: (raw: IncomingMessage) => Promise<Buffer>;
|
|
22
|
+
export declare const readRawBody: (raw: IncomingMessage) => Promise<string>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SSRMiddleware, SSRRequest, SSRResponseHelpers, SSRContext } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Runs the chain, each middleware handing on by calling `next`. `timed`, when given, is told how long each one took
|
|
4
|
+
* before it handed on — its own time, not the chain's after it — or until it ended the chain.
|
|
5
|
+
*/
|
|
6
|
+
export declare const runMiddlewares: (middlewares: SSRMiddleware[], req: SSRRequest & Partial<SSRContext>, res: SSRResponseHelpers, timed?: (index: number, ms: number) => void) => Promise<boolean>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SSRRequest, SSRResponseHelpers, ServerBaseConfig, SSRSpaceDeployment } from '../types';
|
|
2
|
+
/** What of the server's configuration these headers read. */
|
|
3
|
+
type HeadersConfig = Pick<ServerBaseConfig, 'frameOptions' | 'hsts' | 'crossOriginOpenerPolicy' | 'httpVersion'>;
|
|
4
|
+
/**
|
|
5
|
+
* The `Strict-Transport-Security` a response over https carries, or none — see `ServerBaseConfig.hsts` — and on a host
|
|
6
|
+
* whose deployment says (`SSRDeploymentSecurity.hsts`): `'host'` keeps to that host alone, `'subdomains'` reaches every
|
|
7
|
+
* sub-domain of it, `'off'` asks nothing.
|
|
8
|
+
*/
|
|
9
|
+
export declare const hstsFor: (req: Pick<SSRRequest, "protocol" | "hostname">, config: HeadersConfig, chosen?: "off" | "host" | "subdomains") => string | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Who may put the page in a frame, as CSP `frame-ancestors`: the space's own list when its deployment names one — only
|
|
12
|
+
* it knows the sites its owner allowed — and the sites its domain adds (`security.frameAncestors`), else the server's
|
|
13
|
+
* `frameOptions`. One place, because the page's script policy is sent in the same header (`pagePolicies`) and a second
|
|
14
|
+
* header would replace this one.
|
|
15
|
+
*/
|
|
16
|
+
export declare const frameAncestorsOf: (config: Pick<ServerBaseConfig, "frameOptions">, deployment?: Pick<SSRSpaceDeployment, "frameAncestors" | "security">) => string | undefined;
|
|
17
|
+
export declare const applySecurityHeaders: (req: Pick<SSRRequest, "protocol" | "hostname">, res: SSRResponseHelpers, config: HeadersConfig, port: number) => void;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { FleetStores } from './fleet/link';
|
|
2
|
+
import { Handler } from './transports';
|
|
3
|
+
import { ServerBaseConfig } from '../types';
|
|
4
|
+
export interface HttpServerParts {
|
|
5
|
+
label: string;
|
|
6
|
+
/** Started once the transport is bound: background work that should not run in a server nobody listened on. */
|
|
7
|
+
onListen?: () => void;
|
|
8
|
+
/**
|
|
9
|
+
* The server has stopped taking connections and is shutting down: what holds connections open for good — realtime
|
|
10
|
+
* sockets and streams — lets them go now, rather than being cut when the grace runs out.
|
|
11
|
+
*/
|
|
12
|
+
onClosing?: () => void;
|
|
13
|
+
/**
|
|
14
|
+
* Told to stop, before it announces it: what takes work of its own accord — a job worker claiming — stops taking it,
|
|
15
|
+
* so what arrives meanwhile goes to the replicas that stay.
|
|
16
|
+
*/
|
|
17
|
+
onStopping?: () => void;
|
|
18
|
+
/** Awaited, so a server that holds work in flight — jobs, drains — can finish it before the sockets close. */
|
|
19
|
+
onDestroy?: () => void | Promise<void>;
|
|
20
|
+
/** What the primary does once, before it starts the workers — work each of them would otherwise repeat, or race
|
|
21
|
+
* the others to do. */
|
|
22
|
+
beforeFork?: () => Promise<void>;
|
|
23
|
+
/** What the primary keeps for its workers to share (`fleet/link.ts`): each store named by the module that keeps it. */
|
|
24
|
+
fleetStores?: FleetStores;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A server's transport and its lifecycle — what every server built on this shares. What a server offers beyond that
|
|
28
|
+
* (a page server's caches and plugins) it adds itself, over `channel` where it has workers to keep in step.
|
|
29
|
+
*/
|
|
30
|
+
export interface HttpServer {
|
|
31
|
+
listen: (port: number, host?: string) => Promise<void>;
|
|
32
|
+
close: () => Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* The name this server's broadcasts go by among its fleet's processes (`fleet/link.ts`), the same in the primary and
|
|
35
|
+
* each worker; undefined for a single process, which has nobody to tell.
|
|
36
|
+
*/
|
|
37
|
+
channel: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
export declare const createHttpServer: (config: ServerBaseConfig, makeHandlerForPort: (port: number) => Handler, parts: HttpServerParts) => HttpServer;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RawResponse } from './buildResponseHelpers';
|
|
2
|
+
import { Duplex } from 'node:stream';
|
|
3
|
+
/** A request asking to switch protocols, as the transport handed it over: the raw socket, and what was read past the headers. */
|
|
4
|
+
export type UpgradeRequest = {
|
|
5
|
+
socket: Duplex;
|
|
6
|
+
head: Buffer;
|
|
7
|
+
/** Set by the stage that switched protocols: from then on the socket is its connection, not an HTTP response. */
|
|
8
|
+
taken: boolean;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* An HTTP response written straight to a socket — what an upgrade request gets when it is NOT upgraded.
|
|
12
|
+
*
|
|
13
|
+
* An upgrade arrives with no `ServerResponse`: node hands over the socket and expects whoever listens to speak for
|
|
14
|
+
* it. Every stage of the pipeline writes to a `RawResponse`, so this is one: a refusal (a 403, a 404, a redirect)
|
|
15
|
+
* reaches the client as the stage wrote it, and the socket closes after it — a connection that asked to switch
|
|
16
|
+
* protocols and was answered in HTTP has nothing left to say.
|
|
17
|
+
*/
|
|
18
|
+
export declare const socketResponse: (socket: Duplex) => RawResponse;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { RawResponse } from './buildResponseHelpers';
|
|
2
|
+
export type EventStream = {
|
|
3
|
+
/** Sends one event. Silently drops once the socket is gone — writing into a closed peer is not an error. */
|
|
4
|
+
send: (event: string, data: unknown) => void;
|
|
5
|
+
/** Ends the stream and stops the heartbeat. */
|
|
6
|
+
close: () => void;
|
|
7
|
+
/** Whether the peer is still there. */
|
|
8
|
+
isOpen: () => boolean;
|
|
9
|
+
};
|
|
10
|
+
export type EventStreamOptions = {
|
|
11
|
+
/** Called once when the peer is found gone — a failed write, or a peer that stopped reading. */
|
|
12
|
+
onAbort: () => void;
|
|
13
|
+
/** Headers besides the ones every event stream sends. */
|
|
14
|
+
headers?: Record<string, string>;
|
|
15
|
+
/** What an `EventSource` should wait before reconnecting, in milliseconds. */
|
|
16
|
+
retryMs: number;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Opens a Server-Sent Events response — an action's progress, a realtime channel.
|
|
20
|
+
*
|
|
21
|
+
* Written straight to the RAW response, never through the response helpers: those compress the body, set a
|
|
22
|
+
* Content-Length and end it in one go, which is precisely what a stream cannot do. `no-transform` says the same
|
|
23
|
+
* thing to every proxy in between, and `X-Accel-Buffering: no` says it to nginx, which otherwise holds frames
|
|
24
|
+
* until its buffer fills and turns a live stream into one late burst.
|
|
25
|
+
*/
|
|
26
|
+
export declare const openEventStream: (raw: RawResponse, { onAbort, headers, retryMs }: EventStreamOptions) => EventStream;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BaseContext, Stage } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Whether this request is a write a page of another origin caused, and so is refused — answered here, 403
|
|
4
|
+
* `reason: 'foreign'`, when it is.
|
|
5
|
+
*
|
|
6
|
+
* Not only a write a session cookie could authenticate: a server holding credentials of its own (a token for somebody's
|
|
7
|
+
* calendar, an API key) or a tool with no sign-in at all acts for whoever reaches it, and a plain form POST from any
|
|
8
|
+
* page open in the same browser reaches it without a preflight. Passes:
|
|
9
|
+
*
|
|
10
|
+
* - a safe method;
|
|
11
|
+
* - `Authorization` — a page of another origin cannot set it without a preflight this server never allows;
|
|
12
|
+
* - a request that is not a browser's (no `Sec-Fetch-Site`, no `Origin`): a webhook, `curl`, another server;
|
|
13
|
+
* - a page of this origin (`same-origin` — never merely `same-site`: another port of this host, or a sibling
|
|
14
|
+
* sub-domain, is where somebody else's pages are served), or of an origin in `crossSiteWrites.origins`;
|
|
15
|
+
* - a path in `crossSiteWrites.paths` — a form other sites post to by design.
|
|
16
|
+
*/
|
|
17
|
+
export declare const refusedCrossSite: (ctx: BaseContext) => boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Every write the page server answers after it — server actions, realtime publishes, a space's `/fn/` routes and its
|
|
20
|
+
* runtime's endpoints, the deployment's own data stages — refused when a page of another origin caused it (see
|
|
21
|
+
* {@link refusedCrossSite}). After the stages that gate themselves (`preAuth`: the `/auth` flows with their own CSRF,
|
|
22
|
+
* MCP and its OAuth, the preview's shared secret), before everything else.
|
|
23
|
+
*/
|
|
24
|
+
export declare const crossSiteWritesStage: Stage;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Stage } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Liveness/readiness endpoint for standalone servers (k8s probes). Always answers at /health so every server built
|
|
4
|
+
* on sdk-server has one without the consumer wiring it. The body is the generic identity payload (from
|
|
5
|
+
* `config.health.name/version/role`) unless `payload` overrides it entirely.
|
|
6
|
+
*
|
|
7
|
+
* `check` is what makes it a readiness probe: it reports live state — the stores this process depends on, most
|
|
8
|
+
* often — and `healthy: false` answers 503 so an orchestrator stops routing to a replica that cannot serve. A
|
|
9
|
+
* server that answered 200 while its database was gone is one an orchestrator keeps sending traffic to.
|
|
10
|
+
*/
|
|
11
|
+
export declare const healthStage: Stage;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ServerBaseConfig } from '../types';
|
|
2
|
+
export declare const markStopping: (config: object) => void;
|
|
3
|
+
export declare const isStopping: (config: object) => boolean;
|
|
4
|
+
/** How long a server that is closing keeps answering, and how long what it answers may take — see `shutdown`. */
|
|
5
|
+
export declare const SHUTDOWN_ANNOUNCE_MS = 0;
|
|
6
|
+
export declare const shutdownOf: (config: Pick<ServerBaseConfig, "shutdown">, defaults: {
|
|
7
|
+
graceMs: number;
|
|
8
|
+
}) => {
|
|
9
|
+
announceMs: number;
|
|
10
|
+
graceMs: number;
|
|
11
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { default as http, IncomingMessage } from 'node:http';
|
|
2
|
+
import { default as http2 } from 'node:http2';
|
|
3
|
+
import { default as https } from 'node:https';
|
|
4
|
+
import { RawResponse } from './buildResponseHelpers';
|
|
5
|
+
import { ServerBaseConfig } from '../types';
|
|
6
|
+
import { Duplex } from 'node:stream';
|
|
7
|
+
export type CloseableServer = {
|
|
8
|
+
close: (cb?: (err?: Error) => void) => unknown;
|
|
9
|
+
listen: (port: number, host: string, cb: () => void) => unknown;
|
|
10
|
+
/** Bind failures arrive here. Without a listener Node throws the `error` event as an uncaught exception, which
|
|
11
|
+
* is how a taken port used to read as a crash in `node:net` rather than as this server not starting. */
|
|
12
|
+
on: (event: 'error', listener: (error: NodeJS.ErrnoException) => void) => unknown;
|
|
13
|
+
};
|
|
14
|
+
/** The server a port is served by: Node's own, whichever protocol — what a shutdown drains (`server/drain.ts`). */
|
|
15
|
+
export type PrimaryServer = http.Server | https.Server | http2.Http2SecureServer;
|
|
16
|
+
/** A request asking to switch protocols: node hands over the socket, and the bytes it read past the headers. */
|
|
17
|
+
export type UpgradeHandler = (req: IncomingMessage, socket: Duplex, head: Buffer) => void;
|
|
18
|
+
/** What a server answers requests with — and, when it takes any, upgrades (`upgrade`). */
|
|
19
|
+
export type Handler = ((req: IncomingMessage, res: RawResponse) => void) & {
|
|
20
|
+
upgrade?: UpgradeHandler;
|
|
21
|
+
};
|
|
22
|
+
export declare const tlsOptions: (config: ServerBaseConfig, label?: string) => {
|
|
23
|
+
key: string | Buffer<ArrayBufferLike>;
|
|
24
|
+
cert: string | Buffer<ArrayBufferLike>;
|
|
25
|
+
minVersion: "TLSv1.3";
|
|
26
|
+
};
|
|
27
|
+
export declare const protoLabel: (version: number, hasTls: boolean) => string;
|
|
28
|
+
export declare const buildTransport: (config: ServerBaseConfig, handler: Handler, port: number, label?: string) => {
|
|
29
|
+
primary: PrimaryServer;
|
|
30
|
+
h3?: CloseableServer;
|
|
31
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RawResponse } from './buildResponseHelpers';
|
|
2
|
+
import { UpgradeRequest } from './socketResponse';
|
|
3
|
+
import { SSRRequest, SSRResponseHelpers, ServerBaseConfig } from '../types';
|
|
4
|
+
import { IncomingMessage } from 'node:http';
|
|
5
|
+
export interface BaseContext {
|
|
6
|
+
raw: IncomingMessage;
|
|
7
|
+
rawRes: RawResponse;
|
|
8
|
+
req: SSRRequest;
|
|
9
|
+
res: SSRResponseHelpers;
|
|
10
|
+
config: ServerBaseConfig;
|
|
11
|
+
port: number;
|
|
12
|
+
/** Set by the stage that answers when the path alone does not identify the work (the MCP endpoint serves every
|
|
13
|
+
* JSON-RPC method on the same URL). The dispatcher folds it into the access log. */
|
|
14
|
+
operation?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Aborted when the client goes away.
|
|
17
|
+
*
|
|
18
|
+
* The pipeline had no request lifecycle at all before this: a stage that kept working after the socket closed
|
|
19
|
+
* had no way to find out, which is affordable for a page render and is not for anything long-lived. Stages that
|
|
20
|
+
* spend real work on behalf of a caller should hand this to whatever they call.
|
|
21
|
+
*/
|
|
22
|
+
signal: AbortSignal;
|
|
23
|
+
/**
|
|
24
|
+
* Present when the request asks to switch protocols (a WebSocket). The stage that takes it performs the switch on
|
|
25
|
+
* the socket and marks it `taken`; any other answer is written to the socket as HTTP and closes it.
|
|
26
|
+
*/
|
|
27
|
+
upgrade?: UpgradeRequest;
|
|
28
|
+
}
|
|
29
|
+
export type Stage<C extends BaseContext = BaseContext> = (ctx: C) => boolean | Promise<boolean>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A port nobody holds, for a test to listen on — chosen where the system never hands one out by itself.
|
|
3
|
+
*
|
|
4
|
+
* Not one the system hands out (`listen(0)`): that range is the one every outgoing connection takes its local port
|
|
5
|
+
* from, so between the moment a port is handed out and the moment the test listens on it, a request of a test running
|
|
6
|
+
* beside it — another worker's `fetch`, a database connection — can take it, and the listen fails with `EADDRINUSE` on
|
|
7
|
+
* a run that has nothing wrong with it. Below that range only a port somebody chose explicitly is ever taken, and it is
|
|
8
|
+
* looked at first: one that answers, or that this process may not listen on, is passed over.
|
|
9
|
+
*/
|
|
10
|
+
export declare const unusedPort: (host?: string) => Promise<number>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { IncomingMessage } from 'node:http';
|
|
2
|
+
/** What a response is written onto: node's own, or the page server's raw one. */
|
|
3
|
+
export type WebResponseSink = {
|
|
4
|
+
writeHead(statusCode: number, headers: Record<string, string>): unknown;
|
|
5
|
+
write(chunk: Buffer): unknown;
|
|
6
|
+
end(): unknown;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* An incoming request as a web `Request` at `url` — its headers but those named in `without` (lower case), and its
|
|
10
|
+
* body read whole, within the server's body limit. What a web-standard handler takes: an MCP transport, an endpoint a
|
|
11
|
+
* space runtime declares.
|
|
12
|
+
*/
|
|
13
|
+
export declare const webRequestOf: (raw: IncomingMessage, url: string, without?: readonly string[]) => Promise<Request>;
|
|
14
|
+
/**
|
|
15
|
+
* A web `Response` written onto a node one: its status and headers, then its body as it comes — a stream too, chunk by
|
|
16
|
+
* chunk, which is what an event stream needs — until it ends or `signal` says the caller went away.
|
|
17
|
+
*/
|
|
18
|
+
export declare const writeWebResponse: (rawRes: WebResponseSink, response: Response, signal?: AbortSignal) => Promise<void>;
|