@solidjs/web 2.0.0-beta.2 → 2.0.0-beta.21
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/README.md +27 -4
- package/dist/dev.cjs +790 -223
- package/dist/dev.js +764 -217
- package/dist/server.cjs +742 -186
- package/dist/server.js +714 -183
- package/dist/web.cjs +779 -196
- package/dist/web.js +753 -190
- package/package.json +193 -38
- package/serialization/dist/serialization.cjs +83 -0
- package/serialization/dist/serialization.js +75 -0
- package/serialization/package.json +20 -0
- package/serialization/types/index.d.ts +139 -0
- package/serialization/types-cjs/index.d.cts +139 -0
- package/serialization/types-cjs/package.json +3 -0
- package/server-functions/dist/client.cjs +448 -0
- package/server-functions/dist/client.js +435 -0
- package/server-functions/dist/server.cjs +632 -0
- package/server-functions/dist/server.js +615 -0
- package/server-functions/package.json +30 -0
- package/storage/package.json +8 -3
- package/storage/types/index.d.ts +26 -0
- package/storage/types-cjs/index.d.cts +28 -0
- package/storage/types-cjs/package.json +3 -0
- package/types/client.d.ts +64 -21
- package/types/core.d.ts +3 -3
- package/types/index.d.ts +156 -24
- package/types/jsx-properties.d.ts +93 -0
- package/types/jsx.d.ts +4135 -1
- package/types/response.d.ts +93 -0
- package/types/serializer.d.ts +139 -0
- package/types/server-functions/client.d.ts +137 -0
- package/types/server-functions/server.d.ts +307 -0
- package/types/server-functions/shared.d.ts +342 -0
- package/types/server-mock.d.ts +89 -0
- package/types/server.d.ts +123 -28
- package/types-cjs/client.d.cts +131 -0
- package/types-cjs/core.d.cts +3 -0
- package/types-cjs/index.d.cts +178 -0
- package/types-cjs/jsx-properties.d.cts +93 -0
- package/types-cjs/jsx.d.cts +4135 -0
- package/types-cjs/package.json +3 -0
- package/types-cjs/response.d.cts +93 -0
- package/types-cjs/serializer.d.cts +139 -0
- package/types-cjs/server-functions/client.d.cts +137 -0
- package/types-cjs/server-functions/server.d.cts +307 -0
- package/types-cjs/server-functions/shared.d.cts +342 -0
- package/types-cjs/server-mock.d.cts +161 -0
- package/types-cjs/server.d.cts +251 -0
- package/storage/types/src/client.d.ts +0 -1
- package/storage/types/src/index.d.ts +0 -46
- package/storage/types/src/server-mock.d.ts +0 -72
- package/storage/types/storage/src/index.d.ts +0 -2
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Envelope pairing HTTP metadata (a `Response`) with an in-memory value.
|
|
3
|
+
* Produced by `respond()` and by server-function `transformResult`
|
|
4
|
+
* implementations (e.g. single-flight payloads). The HTTP handler forwards
|
|
5
|
+
* `response`'s headers and (non-redirect) status and encodes `value` as the
|
|
6
|
+
* body through the codec, while client-only integrations read `value`
|
|
7
|
+
* directly — no reparse. Mostly consumed by integrations (routers);
|
|
8
|
+
* application code usually just returns what `respond()` gives it.
|
|
9
|
+
*/
|
|
10
|
+
export class ResponseEnvelope<T = unknown> {
|
|
11
|
+
constructor(response: Response | undefined, value: T);
|
|
12
|
+
/** The HTTP metadata: status and headers to forward (body ignored by integrations). */
|
|
13
|
+
response: Response | undefined;
|
|
14
|
+
/** The structured value the caller receives. */
|
|
15
|
+
value: T;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Whether `value` is a `ResponseEnvelope`. Uses a registered-symbol brand
|
|
20
|
+
* rather than `instanceof`, so it stays correct when separately bundled
|
|
21
|
+
* entries each carry a copy of the class. Integrations should always use
|
|
22
|
+
* this over `instanceof`.
|
|
23
|
+
*/
|
|
24
|
+
export function isResponseEnvelope(value: unknown): value is ResponseEnvelope;
|
|
25
|
+
|
|
26
|
+
/** `ResponseInit` accepted by the response helpers, plus `revalidate`. */
|
|
27
|
+
export interface ResponseHelperInit extends ResponseInit {
|
|
28
|
+
/**
|
|
29
|
+
* Cache keys the mutation invalidated, carried in the `X-Revalidate`
|
|
30
|
+
* header. Opaque to the protocol — the integration's keyed cache (e.g.
|
|
31
|
+
* the router's `query` cache) assigns them meaning.
|
|
32
|
+
*/
|
|
33
|
+
revalidate?: string | string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Response redirecting to `url` (default status 302). Return (or throw) it
|
|
38
|
+
* from a server function — the HTTP handler forwards the redirect for the
|
|
39
|
+
* client integration to follow — or return it from a client-side action,
|
|
40
|
+
* where the integration interprets it in memory. Same object, same
|
|
41
|
+
* meaning, both sides.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { redirect } from "@solidjs/web";
|
|
46
|
+
*
|
|
47
|
+
* async function login(form: FormData) {
|
|
48
|
+
* "use server";
|
|
49
|
+
* // ...
|
|
50
|
+
* return redirect("/dashboard", { revalidate: "session" });
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function redirect(url: string, init?: number | ResponseHelperInit): Response;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Empty response requesting revalidation of the named cache keys — all of
|
|
58
|
+
* them when omitted. For mutations whose only effect the caller needs is
|
|
59
|
+
* "refetch your data".
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { reload } from "@solidjs/web";
|
|
64
|
+
*
|
|
65
|
+
* async function addTodo(title: string) {
|
|
66
|
+
* "use server";
|
|
67
|
+
* await db.insert(title);
|
|
68
|
+
* return reload({ revalidate: "todos" });
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export function reload(init?: ResponseHelperInit): Response;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A value paired with response metadata (status, headers, `revalidate`) —
|
|
76
|
+
* for the things a naked return can't express. Scripted callers receive
|
|
77
|
+
* `value` transparently (the transport unwraps the envelope), and
|
|
78
|
+
* progressive enhancement stays invisible: the carried response holds a
|
|
79
|
+
* plain JSON body so consumers without the client runtime (no-JS form
|
|
80
|
+
* posts, direct HTTP) get real JSON.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { respond } from "@solidjs/web";
|
|
85
|
+
*
|
|
86
|
+
* async function createItem(input: Item) {
|
|
87
|
+
* "use server";
|
|
88
|
+
* const item = await db.create(input);
|
|
89
|
+
* return respond(item, { status: 201, revalidate: "items" });
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function respond<T>(value: T, init?: ResponseHelperInit): ResponseEnvelope<T>;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Plugin, Serializer, SerovalNode } from "seroval";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Seroval's node shape — the intermediate representation `serializeJSON`
|
|
5
|
+
* emits and `createJSONDeserializer` consumes. Safe to `JSON.stringify`.
|
|
6
|
+
*/
|
|
7
|
+
export type { SerovalNode };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A Seroval plugin usable with the web serializers — teaches the codec how
|
|
11
|
+
* to encode/decode a custom value type. Supply matching plugins on both
|
|
12
|
+
* peers of a transport.
|
|
13
|
+
*/
|
|
14
|
+
export type SerializerPlugin = Plugin<any, any>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Baseline plugin set for serializing web-platform values (AbortSignal,
|
|
18
|
+
* Event, FormData, Headers, ReadableStream, Request, Response, URL, ...).
|
|
19
|
+
* Applied by every serializer in this module; custom plugins compose ahead
|
|
20
|
+
* of it via `resolveSerializerPlugins`.
|
|
21
|
+
*/
|
|
22
|
+
export const DEFAULT_WEB_PLUGINS: readonly SerializerPlugin[];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Composes custom plugins with `DEFAULT_WEB_PLUGINS`. Custom plugins come
|
|
26
|
+
* first so they can shadow a default for values both would match. Returns a
|
|
27
|
+
* fresh array; the defaults are never mutated. Useful when handing a full
|
|
28
|
+
* plugin list to another serialization layer.
|
|
29
|
+
*/
|
|
30
|
+
export function resolveSerializerPlugins(customPlugins?: SerializerPlugin[]): SerializerPlugin[];
|
|
31
|
+
|
|
32
|
+
/** Options for `createSerializer`. */
|
|
33
|
+
export interface WebSerializerOptions {
|
|
34
|
+
/** Name of the global object the emitted scripts write resolved values into. */
|
|
35
|
+
globalIdentifier: string;
|
|
36
|
+
/** Cross-reference scope id, for isolating multiple streams on one page. */
|
|
37
|
+
scopeId?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Seroval feature bitflags to exclude from output. Defaults to disabling
|
|
40
|
+
* post-ES2017 features (AggregateError, BigInt typed arrays).
|
|
41
|
+
*/
|
|
42
|
+
disabledFeatures?: number;
|
|
43
|
+
/** Extra plugins, composed ahead of `DEFAULT_WEB_PLUGINS`. */
|
|
44
|
+
plugins?: SerializerPlugin[];
|
|
45
|
+
/** Receives each emitted script chunk. */
|
|
46
|
+
onData: (result: string) => void;
|
|
47
|
+
onError?: (error: unknown) => void;
|
|
48
|
+
/** Fires once all async values have settled. */
|
|
49
|
+
onDone?: () => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a streaming Seroval serializer preconfigured with the web plugin
|
|
54
|
+
* set and the default feature policy. Emits JavaScript chunks (through
|
|
55
|
+
* `onData`) that reconstruct the values under `globalIdentifier` when
|
|
56
|
+
* evaluated — the script-injection form of serialization renderers build
|
|
57
|
+
* on. For a JSON-based wire codec (no eval on the receiving side), use
|
|
58
|
+
* `serializeJSON` / `createJSONDeserializer` instead.
|
|
59
|
+
*/
|
|
60
|
+
export function createSerializer(options: WebSerializerOptions): Serializer;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Options for `createHydrationSerializer` — `WebSerializerOptions` minus
|
|
64
|
+
* the knobs hydration pins (`globalIdentifier`, `disabledFeatures`).
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
export type HydrationSerializerOptions = Omit<
|
|
68
|
+
WebSerializerOptions,
|
|
69
|
+
"globalIdentifier" | "disabledFeatures"
|
|
70
|
+
>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Renderer primitive — the serializer SSR uses for hydration output. Pins
|
|
74
|
+
* the hydration global (`_$HY.r`) and feature policy; only the wiring
|
|
75
|
+
* options (callbacks, scope, extra plugins) are configurable. Not meant
|
|
76
|
+
* for hand-written code — custom serialization should use
|
|
77
|
+
* `createSerializer` or the JSON codec.
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
export function createHydrationSerializer(options: HydrationSerializerOptions): Serializer;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Renderer primitive — returns the cross-reference bootstrap script SSR
|
|
84
|
+
* emits ahead of hydration data for a render scope. Not meant for
|
|
85
|
+
* hand-written code.
|
|
86
|
+
* @internal
|
|
87
|
+
*/
|
|
88
|
+
export function getLocalHeaderScript(id?: string): string;
|
|
89
|
+
|
|
90
|
+
// ---- JSON codec (server function transports) ----
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Options shared by both halves of the JSON codec. All of them must match
|
|
94
|
+
* on the serializing and deserializing peer or payloads will not
|
|
95
|
+
* round-trip — for server functions, set them once through the
|
|
96
|
+
* client/server `codec` config option.
|
|
97
|
+
*/
|
|
98
|
+
export interface JSONCodecOptions {
|
|
99
|
+
/** Extra plugins, composed ahead of `DEFAULT_WEB_PLUGINS`. Must match on both peers. */
|
|
100
|
+
plugins?: SerializerPlugin[];
|
|
101
|
+
/**
|
|
102
|
+
* Seroval feature bitflags to exclude. Defaults to disabling `RegExp`
|
|
103
|
+
* (payloads may come from an untrusted peer). Must match on both peers.
|
|
104
|
+
*/
|
|
105
|
+
disabledFeatures?: number;
|
|
106
|
+
/** Maximum parse/deserialize depth. Defaults to 64. Must match on both peers. */
|
|
107
|
+
depthLimit?: number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Options for `serializeJSON`. */
|
|
111
|
+
export interface JSONSerializeOptions extends JSONCodecOptions {
|
|
112
|
+
/**
|
|
113
|
+
* Receives each serialized node; `initial` is true for the first chunk
|
|
114
|
+
* (the source value itself). Async values produce additional chunks as
|
|
115
|
+
* they resolve.
|
|
116
|
+
*/
|
|
117
|
+
onParse: (node: SerovalNode, initial: boolean) => void;
|
|
118
|
+
onError?: (error: unknown) => void;
|
|
119
|
+
/** Fires once all async values have settled. */
|
|
120
|
+
onDone?: () => void;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Serializes `value` as SerovalNode chunks delivered through `onParse` —
|
|
125
|
+
* the encoding half of the eval-free JSON codec (RPC-style transports;
|
|
126
|
+
* the deserializing peer needs no script evaluation, so CSP-safe). Wire
|
|
127
|
+
* framing of the nodes is the transport's concern. Returns a cancel
|
|
128
|
+
* function that aborts pending async serialization.
|
|
129
|
+
*/
|
|
130
|
+
export function serializeJSON(value: unknown, options: JSONSerializeOptions): () => void;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Creates the decoding counterpart of `serializeJSON`. Cross-references
|
|
134
|
+
* between chunks resolve through state shared across calls, so all chunks
|
|
135
|
+
* from one stream must go through the same deserializer instance. The first
|
|
136
|
+
* chunk's return value is the decoded source value; feeding later chunks
|
|
137
|
+
* settles the async values referenced inside it.
|
|
138
|
+
*/
|
|
139
|
+
export function createJSONDeserializer(options?: JSONCodecOptions): <T>(node: SerovalNode) => T;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { JSONCodecOptions } from "../serializer.cjs";
|
|
2
|
+
import { ServerFunction, ServerFunctionMetadata } from "./shared.cjs";
|
|
3
|
+
|
|
4
|
+
export {
|
|
5
|
+
FUNCTION_HEADER,
|
|
6
|
+
INSTANCE_HEADER,
|
|
7
|
+
SINGLE_FLIGHT_HEADER,
|
|
8
|
+
decodeResponse,
|
|
9
|
+
getServerFunctionMetadata,
|
|
10
|
+
isServerFunction,
|
|
11
|
+
subscribeFlightData,
|
|
12
|
+
withMeta
|
|
13
|
+
} from "./shared.cjs";
|
|
14
|
+
export type {
|
|
15
|
+
FlightDataConsumer,
|
|
16
|
+
FlightDataContext,
|
|
17
|
+
ServerFunction,
|
|
18
|
+
ServerFunctionMetadata,
|
|
19
|
+
SingleFlightPayload
|
|
20
|
+
} from "./shared.cjs";
|
|
21
|
+
|
|
22
|
+
/** The context `prepareRequest` receives alongside the outgoing RequestInit. */
|
|
23
|
+
export interface PrepareRequestContext {
|
|
24
|
+
/** The build-stable id of the function being called. */
|
|
25
|
+
id: string;
|
|
26
|
+
/**
|
|
27
|
+
* The reference's declaration metadata (e.g. `method: "GET"` for
|
|
28
|
+
* `GET(fn)` references). Plain references carry an empty object.
|
|
29
|
+
*/
|
|
30
|
+
meta: ServerFunctionMetadata | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Client-side session-dynamic transport hook: runs before every
|
|
35
|
+
* server-function fetch. Return (or mutate and return) the RequestInit the
|
|
36
|
+
* transport will use — the hook sees the final init, transport headers
|
|
37
|
+
* included. The motivating case is dynamic credentials that rotate during
|
|
38
|
+
* a session and apply uniformly to every call (OAuth bearer tokens); it is
|
|
39
|
+
* the client-side symmetric of the server handler hooks. Single hook, not
|
|
40
|
+
* a chain — compose by wrapping functions in userland.
|
|
41
|
+
*/
|
|
42
|
+
export type PrepareRequestHook = (
|
|
43
|
+
init: RequestInit,
|
|
44
|
+
context: PrepareRequestContext
|
|
45
|
+
) => RequestInit | Promise<RequestInit>;
|
|
46
|
+
|
|
47
|
+
/** Options for `configureServerFunctionsClient`. */
|
|
48
|
+
export interface ServerFunctionsClientConfig {
|
|
49
|
+
/**
|
|
50
|
+
* Endpoint the server's HTTP handler is mounted on. Must match the
|
|
51
|
+
* server configuration — SSR'd reference `url`s (e.g. form actions) and
|
|
52
|
+
* client fetches both derive from it. Prefix it when the app serves from
|
|
53
|
+
* a base path (e.g. `` `${BASE_URL}_server` ``).
|
|
54
|
+
* @default "/_server"
|
|
55
|
+
*/
|
|
56
|
+
endpoint?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Codec options (extra plugins etc.) for encoding arguments and decoding
|
|
59
|
+
* results — must match the server's. Stored in the shared layer, so
|
|
60
|
+
* `decodeResponse` sees them too.
|
|
61
|
+
*/
|
|
62
|
+
codec?: JSONCodecOptions;
|
|
63
|
+
/**
|
|
64
|
+
* Runs before every server-function fetch. Return (or mutate and return)
|
|
65
|
+
* the RequestInit the transport will use; `context.meta` is the
|
|
66
|
+
* reference's declaration metadata (e.g. method). For session-dynamic
|
|
67
|
+
* cross-cutting concerns — bearer tokens, tracing headers:
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* configureServerFunctionsClient({
|
|
71
|
+
* prepareRequest(init) {
|
|
72
|
+
* return {
|
|
73
|
+
* ...init,
|
|
74
|
+
* headers: { ...init.headers, Authorization: `Bearer ${session.token()}` }
|
|
75
|
+
* };
|
|
76
|
+
* }
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
prepareRequest?: PrepareRequestHook;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Configures the client transport. Call once, before any server function is
|
|
85
|
+
* invoked — typically in the client entry, next to `hydrate()`. Only needed
|
|
86
|
+
* when deviating from the defaults (custom endpoint, codec plugins, or a
|
|
87
|
+
* `prepareRequest` hook).
|
|
88
|
+
*/
|
|
89
|
+
export function configureServerFunctionsClient(config?: ServerFunctionsClientConfig): void;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Declares a server function callable over HTTP GET: calls to the returned
|
|
93
|
+
* reference go out as GET requests with the arguments codec-encoded in the
|
|
94
|
+
* query string — cacheable by HTTP infrastructure. Cache headers flow
|
|
95
|
+
* through the handler's header forwarding
|
|
96
|
+
* (`respond(data, { headers: { "cache-control": "max-age=60" } })`).
|
|
97
|
+
*
|
|
98
|
+
* The declaration rides the metadata channel
|
|
99
|
+
* (`getServerFunctionMetadata(fn)?.method === "GET"`) for routers and
|
|
100
|
+
* integrations to detect, and the server enforces it: GET-declared
|
|
101
|
+
* functions accept GET requests (and only GET), everything else answers
|
|
102
|
+
* 405. Server-side the wrapper is identity-flavored — SSR calls stay
|
|
103
|
+
* in-process.
|
|
104
|
+
*
|
|
105
|
+
* Wrap the reference at its declaration; the compiler round-trips the call
|
|
106
|
+
* in both builds:
|
|
107
|
+
*
|
|
108
|
+
* ```ts
|
|
109
|
+
* export const getUser = GET(async (id: string) => {
|
|
110
|
+
* "use server";
|
|
111
|
+
* return db.users.find(id);
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export function GET<A extends readonly any[], R>(
|
|
116
|
+
fn: (...args: A) => R
|
|
117
|
+
): ServerFunction<A, Awaited<R>>;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Compiler ABI — emitted by compiled `"use server"` client output where a
|
|
121
|
+
* server function was referenced; produces the fetch-backed callable for
|
|
122
|
+
* the function's build-stable id. Development builds pass the function's
|
|
123
|
+
* source name as the trailing argument (dev-only metadata seeded on the
|
|
124
|
+
* metadata channel; never emitted in production). Not meant for
|
|
125
|
+
* hand-written code.
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
export function createServerReference(id: string, name?: string): ServerFunction;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Compiler ABI — only ever referenced by server-mode compiler output;
|
|
132
|
+
* throws so a misconfigured build (server transform feeding a client
|
|
133
|
+
* bundle) fails loudly instead of with a missing-export error. Not meant
|
|
134
|
+
* for hand-written code.
|
|
135
|
+
* @internal
|
|
136
|
+
*/
|
|
137
|
+
export function registerServerReference(): never;
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { ResponseEnvelope } from "../response.cjs";
|
|
2
|
+
import { JSONCodecOptions } from "../serializer.cjs";
|
|
3
|
+
import { RequestEvent } from "../server.cjs";
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
FUNCTION_HEADER,
|
|
7
|
+
INSTANCE_HEADER,
|
|
8
|
+
SINGLE_FLIGHT_HEADER,
|
|
9
|
+
decodeResponse,
|
|
10
|
+
getServerFunctionMetadata,
|
|
11
|
+
isServerFunction,
|
|
12
|
+
subscribeFlightData,
|
|
13
|
+
withMeta
|
|
14
|
+
} from "./shared.cjs";
|
|
15
|
+
export type {
|
|
16
|
+
FlightDataConsumer,
|
|
17
|
+
FlightDataContext,
|
|
18
|
+
ServerFunction,
|
|
19
|
+
ServerFunctionMetadata,
|
|
20
|
+
SingleFlightPayload
|
|
21
|
+
} from "./shared.cjs";
|
|
22
|
+
import { ServerFunction } from "./shared.cjs";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The request event a server function call runs under: the base
|
|
26
|
+
* `RequestEvent` (request + locals) plus `serverOnly`, set when the call is
|
|
27
|
+
* an in-process SSR invocation whose result never serializes to a client.
|
|
28
|
+
*/
|
|
29
|
+
export interface ServerFunctionEvent extends RequestEvent {
|
|
30
|
+
serverOnly?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What a server function call resolved to, as seen by the single-flight
|
|
35
|
+
* hook — enough context for any data-production strategy without core
|
|
36
|
+
* assuming one.
|
|
37
|
+
*/
|
|
38
|
+
export interface ServerFunctionOutcome {
|
|
39
|
+
/** The build-stable id of the function that ran. */
|
|
40
|
+
id: string;
|
|
41
|
+
/**
|
|
42
|
+
* The value the caller will receive: the raw return for plain results,
|
|
43
|
+
* the unwrapped `value` for `ResponseEnvelope`s, `null` for body-less
|
|
44
|
+
* control-flow `Response`s (redirect/reload).
|
|
45
|
+
*/
|
|
46
|
+
value: unknown;
|
|
47
|
+
/**
|
|
48
|
+
* The `Response` carrying the result's HTTP metadata, when there is one
|
|
49
|
+
* (from a returned/thrown `Response` or a `ResponseEnvelope`). Read
|
|
50
|
+
* `Location` here for redirect-with-data — the data should describe the
|
|
51
|
+
* destination route — and `X-Revalidate` for the invalidated keys.
|
|
52
|
+
* Undefined for plain values.
|
|
53
|
+
*/
|
|
54
|
+
response: Response | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* The original HTTP request, untouched: headers the client integration
|
|
57
|
+
* sent (referrer, custom route context) ride here for the hook to read —
|
|
58
|
+
* core assigns them no meaning.
|
|
59
|
+
*/
|
|
60
|
+
request: Request;
|
|
61
|
+
/** Whether the result was thrown rather than returned. */
|
|
62
|
+
thrown: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The single-flight server hook: given the request event and the function's
|
|
67
|
+
* outcome, optionally produce a data payload (possibly async) to fold into
|
|
68
|
+
* the response alongside the return value. Data production is a black box
|
|
69
|
+
* to the protocol — render data-only, run route preloads, query a cache,
|
|
70
|
+
* whatever the integration chooses; the payload just has to be
|
|
71
|
+
* codec-serializable. Return undefined to send the response unchanged
|
|
72
|
+
* (byte-identical to a call without the hook).
|
|
73
|
+
*
|
|
74
|
+
* Runs after `transformResult`, only for scripted calls that sent
|
|
75
|
+
* `SINGLE_FLIGHT_HEADER` on the request, on returned results and thrown
|
|
76
|
+
* `Response`/`ResponseEnvelope` control-flow signals alike (plain thrown
|
|
77
|
+
* errors never collect). The handler owns the enveloping: contributed data
|
|
78
|
+
* ships as `{ value, data }` under the single-flight response header.
|
|
79
|
+
*/
|
|
80
|
+
export type CollectFlightDataHook = (
|
|
81
|
+
event: ServerFunctionEvent,
|
|
82
|
+
outcome: ServerFunctionOutcome
|
|
83
|
+
) => unknown | Promise<unknown>;
|
|
84
|
+
|
|
85
|
+
/** Options for `configureServerFunctionsServer`. */
|
|
86
|
+
export interface ServerFunctionsServerConfig {
|
|
87
|
+
/**
|
|
88
|
+
* Establishes the request-event scope for a call — the function passed
|
|
89
|
+
* runs with `event` visible to `getRequestEvent()`. Wire it to
|
|
90
|
+
* `provideRequestEvent` from `@solidjs/web/storage` (or the framework's
|
|
91
|
+
* equivalent). When omitted, falls back to the AsyncLocalStorage instance
|
|
92
|
+
* an established request scope parks on the global.
|
|
93
|
+
*/
|
|
94
|
+
provideEvent?: <T>(event: ServerFunctionEvent, fn: () => T) => T;
|
|
95
|
+
/**
|
|
96
|
+
* The single-flight hook: produces the data payload folded into
|
|
97
|
+
* responses of calls that opted in (see `CollectFlightDataHook`).
|
|
98
|
+
* Registered once by the integration that owns data production (a
|
|
99
|
+
* router); per-handler `collectFlightData` options override it.
|
|
100
|
+
*/
|
|
101
|
+
collectFlightData?: CollectFlightDataHook;
|
|
102
|
+
/**
|
|
103
|
+
* Endpoint the HTTP handler is mounted on, used for the `url` of SSR'd
|
|
104
|
+
* references (e.g. form actions) — must match the client configuration.
|
|
105
|
+
* Prefix it when the app serves from a base path (e.g.
|
|
106
|
+
* `` `${BASE_URL}_server` ``).
|
|
107
|
+
* @default "/_server"
|
|
108
|
+
*/
|
|
109
|
+
endpoint?: string;
|
|
110
|
+
/**
|
|
111
|
+
* Codec options (extra plugins etc.) for decoding arguments and encoding
|
|
112
|
+
* results — must match the client's. Stored in the shared layer, so
|
|
113
|
+
* `decodeResponse` sees them too.
|
|
114
|
+
*/
|
|
115
|
+
codec?: JSONCodecOptions;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Configures the server runtime. Call once at server startup, before
|
|
120
|
+
* handling requests. Only needed when deviating from the defaults (custom
|
|
121
|
+
* endpoint, codec plugins, an explicit event provider, or a single-flight
|
|
122
|
+
* hook).
|
|
123
|
+
*/
|
|
124
|
+
export function configureServerFunctionsServer(config?: ServerFunctionsServerConfig): void;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A registered server function: its build-stable id paired with the
|
|
128
|
+
* original implementation. Returned by `registerServerReference` and
|
|
129
|
+
* consumed by the server-side `createServerReference`.
|
|
130
|
+
*
|
|
131
|
+
* Compiler ABI shape; hand-written code rarely constructs these.
|
|
132
|
+
* @internal
|
|
133
|
+
*/
|
|
134
|
+
export interface ServerFunctionReference<T extends any[] = any[], R = any> {
|
|
135
|
+
id: string;
|
|
136
|
+
fn: (...args: T) => R;
|
|
137
|
+
/**
|
|
138
|
+
* The function's source name, emitted by development builds only —
|
|
139
|
+
* `createServerReference` seeds the metadata channel with it.
|
|
140
|
+
* @internal
|
|
141
|
+
*/
|
|
142
|
+
name?: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Adds a function to the dispatch registry under an id and returns it
|
|
147
|
+
* unchanged. The low-level registry write for integrations registering
|
|
148
|
+
* functions outside the compiler (e.g. a router registering its own
|
|
149
|
+
* endpoints); compiled output goes through `registerServerReference`
|
|
150
|
+
* instead. Ids must be stable across the client and server builds.
|
|
151
|
+
*/
|
|
152
|
+
export function registerServerFunction<T extends any[], R>(
|
|
153
|
+
id: string,
|
|
154
|
+
callback: (...args: T) => R
|
|
155
|
+
): (...args: T) => R;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Looks up a registered server function by id; throws for unknown ids.
|
|
159
|
+
* The HTTP handler uses this for dispatch — integrations building custom
|
|
160
|
+
* dispatch (or introspection) can too.
|
|
161
|
+
*/
|
|
162
|
+
export function getServerFunction<T extends any[], R>(id: string): (...args: T) => R;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Compiler ABI — emitted by compiled `"use server"` server output for
|
|
166
|
+
* every server function: registers `fn` for HTTP dispatch under its
|
|
167
|
+
* build-stable id and returns the reference the server-side
|
|
168
|
+
* `createServerReference` consumes. Development builds pass the function's
|
|
169
|
+
* source name as the trailing argument (dev-only metadata; never emitted in
|
|
170
|
+
* production). Not meant for hand-written code.
|
|
171
|
+
* @internal
|
|
172
|
+
*/
|
|
173
|
+
export function registerServerReference<T extends any[], R>(
|
|
174
|
+
id: string,
|
|
175
|
+
fn: (...args: T) => R,
|
|
176
|
+
name?: string
|
|
177
|
+
): ServerFunctionReference<T, R>;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Compiler ABI — emitted by compiled `"use server"` server output where
|
|
181
|
+
* the function was referenced; produces the server-side callable. Calling
|
|
182
|
+
* it during SSR runs the original function in-process (no HTTP), under a
|
|
183
|
+
* request event derived from the current one — marked `serverOnly` and
|
|
184
|
+
* carrying the function's meta. Not meant for hand-written code.
|
|
185
|
+
* @internal
|
|
186
|
+
*/
|
|
187
|
+
export function createServerReference<T extends any[], R>(
|
|
188
|
+
reference: ServerFunctionReference<T, R>
|
|
189
|
+
): (...args: T) => R;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Declares a server function callable over HTTP GET. The server half is
|
|
193
|
+
* identity-flavored — SSR calls stay in-process — but it brands the
|
|
194
|
+
* declaration on the reference's metadata channel
|
|
195
|
+
* (`getServerFunctionMetadata(fn)?.method === "GET"`) and records the
|
|
196
|
+
* declared method for the function's id so `handleServerFunctionRequest`
|
|
197
|
+
* enforces it: GET-declared functions accept GET requests (and only GET),
|
|
198
|
+
* everything else answers 405.
|
|
199
|
+
*
|
|
200
|
+
* Wrap the reference at its declaration; the compiler round-trips the call
|
|
201
|
+
* in both builds:
|
|
202
|
+
*
|
|
203
|
+
* ```ts
|
|
204
|
+
* export const getUser = GET(async (id: string) => {
|
|
205
|
+
* "use server";
|
|
206
|
+
* return db.users.find(id);
|
|
207
|
+
* });
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
export function GET<A extends readonly any[], R>(
|
|
211
|
+
fn: (...args: A) => R
|
|
212
|
+
): ServerFunction<A, Awaited<R>>;
|
|
213
|
+
|
|
214
|
+
/** Identity of the currently executing server function. */
|
|
215
|
+
export interface ServerFunctionMeta {
|
|
216
|
+
id: string;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Reads the calling server function's meta (its id) off the current request
|
|
221
|
+
* event — usable inside a server function body, e.g. to key caches or logs
|
|
222
|
+
* by function. Returns undefined outside a server function call.
|
|
223
|
+
*/
|
|
224
|
+
export function getServerFunctionMeta(): ServerFunctionMeta | undefined;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Hooks layering framework policy onto `handleServerFunctionRequest`.
|
|
228
|
+
* All are optional — the bare handler dispatches, scopes events, and
|
|
229
|
+
* encodes results on its own.
|
|
230
|
+
*/
|
|
231
|
+
export interface HandleServerFunctionOptions {
|
|
232
|
+
/**
|
|
233
|
+
* Builds the request event a call runs under (default: bare
|
|
234
|
+
* `{ request, locals: {} }`). Integrations supply their richer event
|
|
235
|
+
* (cookies, response helpers, platform handles).
|
|
236
|
+
*/
|
|
237
|
+
createEvent?(request: Request): ServerFunctionEvent;
|
|
238
|
+
/**
|
|
239
|
+
* Overrides the configured event provider for this handler — same
|
|
240
|
+
* contract as the `provideEvent` config option.
|
|
241
|
+
*/
|
|
242
|
+
provideEvent?<T>(event: ServerFunctionEvent, fn: () => T): T;
|
|
243
|
+
/**
|
|
244
|
+
* Observes or replaces the function's result before encoding — the
|
|
245
|
+
* extension point for response metadata policies (headers, statuses,
|
|
246
|
+
* substituted results). Runs for returned and thrown results alike
|
|
247
|
+
* (`context.thrown` distinguishes); `context.instance` is null for no-JS
|
|
248
|
+
* calls. Return the result unchanged to pass through, or a
|
|
249
|
+
* `ResponseEnvelope` (exposed through the core entry) to send HTTP
|
|
250
|
+
* metadata plus a structured payload. Runs before `collectFlightData`,
|
|
251
|
+
* so the flight hook sees the transformed outcome — use
|
|
252
|
+
* `collectFlightData`, not this, to fold data into the response.
|
|
253
|
+
*/
|
|
254
|
+
transformResult?(
|
|
255
|
+
event: ServerFunctionEvent,
|
|
256
|
+
result: unknown,
|
|
257
|
+
context: { instance: string | null; request: Request; thrown?: boolean }
|
|
258
|
+
): unknown | ResponseEnvelope | Promise<unknown | ResponseEnvelope>;
|
|
259
|
+
/**
|
|
260
|
+
* Overrides the configured single-flight hook for this handler — same
|
|
261
|
+
* contract as the `collectFlightData` config option (see
|
|
262
|
+
* `CollectFlightDataHook`).
|
|
263
|
+
*/
|
|
264
|
+
collectFlightData?: CollectFlightDataHook;
|
|
265
|
+
/**
|
|
266
|
+
* Builds the response for calls made without the client runtime (no
|
|
267
|
+
* instance header — no-JS form posts, direct HTTP) — the extension
|
|
268
|
+
* point for conventions like redirect-with-flash-cookie. Receives the
|
|
269
|
+
* (transformed) result, the request, and the decoded arguments; `thrown`
|
|
270
|
+
* is set when the result was thrown rather than returned. Defaults to
|
|
271
|
+
* the normal serialized response.
|
|
272
|
+
*/
|
|
273
|
+
handleNoJS?(
|
|
274
|
+
result: unknown,
|
|
275
|
+
request: Request,
|
|
276
|
+
args: unknown[],
|
|
277
|
+
thrown?: boolean
|
|
278
|
+
): Response | Promise<Response>;
|
|
279
|
+
/** Overrides the configured codec options for this handler. */
|
|
280
|
+
codec?: JSONCodecOptions;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Web-standard HTTP handler for server function calls: resolves the
|
|
285
|
+
* function id from the request, enforces the declared method (405 when the
|
|
286
|
+
* request method contradicts a `GET` declaration — or uses GET without
|
|
287
|
+
* one), decodes arguments, runs the function under a request-event scope,
|
|
288
|
+
* and encodes the result (forwarding redirect/revalidation metadata
|
|
289
|
+
* through headers). Mount it on the endpoint the client transport targets
|
|
290
|
+
* (default `/_server`); platform adapters (h3, express, ...) convert their
|
|
291
|
+
* request shape to a web `Request` around it.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* import { handleServerFunctionRequest } from "@solidjs/web/server-functions";
|
|
296
|
+
* import "virtual:solid-server-function-manifest";
|
|
297
|
+
*
|
|
298
|
+
* // in the server's request handling:
|
|
299
|
+
* if (url.pathname.startsWith("/_server")) {
|
|
300
|
+
* return handleServerFunctionRequest(request);
|
|
301
|
+
* }
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
export function handleServerFunctionRequest(
|
|
305
|
+
request: Request,
|
|
306
|
+
options?: HandleServerFunctionOptions
|
|
307
|
+
): Promise<Response>;
|