brumate 0.0.1 → 0.1.1
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 +43 -72
- package/dist/boot-DN1ZU8Hg.d.ts +37 -0
- package/dist/cli.js +2 -2
- package/dist/{client-DuDTDePF.d.ts → client-DwJInP_a.d.ts} +133 -44
- package/dist/client-routes-DakuM32C.js +94 -0
- package/dist/client-routes-DakuM32C.js.map +1 -0
- package/dist/{cookies-rgIAc8IE.js → cookies-Av5WFmcf.js} +18 -8
- package/dist/cookies-Av5WFmcf.js.map +1 -0
- package/dist/{devtools-Bm8cS7hX.js → devtools-BKjnHrnv.js} +10 -5
- package/dist/devtools-BKjnHrnv.js.map +1 -0
- package/dist/{devtools-panel-CTbDSBBA.js → devtools-panel-AWOn0VK2.js} +23 -26
- package/dist/devtools-panel-AWOn0VK2.js.map +1 -0
- package/dist/{files-D_JUcSU5.js → files-DdT84mPy.js} +22 -3
- package/dist/{files-D_JUcSU5.js.map → files-DdT84mPy.js.map} +1 -1
- package/dist/{html-CxWpXngy.js → html-DEeoepg3.js} +47 -10
- package/dist/html-DEeoepg3.js.map +1 -0
- package/dist/{html-DV-Tezgq.d.ts → html-Du8jY2Cc.d.ts} +14 -2
- package/dist/index.d.ts +65 -6
- package/dist/index.js +20 -4
- package/dist/index.js.map +1 -1
- package/dist/{inspect-CGCWfaEx.js → inspect-BPchdWbU.js} +887 -172
- package/dist/inspect-BPchdWbU.js.map +1 -0
- package/dist/loader-Dc9OwY5I.js +101 -0
- package/dist/loader-Dc9OwY5I.js.map +1 -0
- package/dist/{navigate-C5dP89YL.js → navigate-CzLjnoPH.js} +112 -59
- package/dist/navigate-CzLjnoPH.js.map +1 -0
- package/dist/{produce-BjQGMpqL.d.ts → produce-CtZ32UGG.d.ts} +13 -3
- package/dist/{rpc-B86KKjzx.js → rpc-9fSqL_YK.js} +57 -12
- package/dist/rpc-9fSqL_YK.js.map +1 -0
- package/dist/runtime/client.d.ts +4 -4
- package/dist/runtime/client.js +599 -306
- package/dist/runtime/client.js.map +1 -1
- package/dist/runtime/devtools.d.ts +3 -1
- package/dist/runtime/devtools.js +1 -1
- package/dist/runtime/hot.d.ts +1 -1
- package/dist/runtime/hot.js +9 -5
- package/dist/runtime/hot.js.map +1 -1
- package/dist/runtime/html.d.ts +2 -2
- package/dist/runtime/html.js +1 -1
- package/dist/runtime/loader.d.ts +12 -2
- package/dist/runtime/loader.js +3 -1
- package/dist/runtime/server.d.ts +4 -4
- package/dist/runtime/server.js +5 -5
- package/dist/runtime/signals.d.ts +2 -2
- package/dist/runtime/signals.js +5 -5
- package/dist/runtime/signals.js.map +1 -1
- package/dist/runtime/static-loader.d.ts +3 -0
- package/dist/runtime/static-loader.js +1 -0
- package/dist/runtime/testing.d.ts +21 -2
- package/dist/runtime/testing.js +83 -7
- package/dist/runtime/testing.js.map +1 -1
- package/dist/{server-sQCvduOz.d.ts → server-BxqjLUnV.d.ts} +211 -20
- package/dist/{server-CX3JnPCs.js → server-DZsgnPKS.js} +595 -185
- package/dist/server-DZsgnPKS.js.map +1 -0
- package/dist/shared-CWYOd0H3.js +40 -0
- package/dist/shared-CWYOd0H3.js.map +1 -0
- package/dist/static-loader-BcFh4ebX.d.ts +11 -0
- package/dist/static-loader-ClNkHZGn.js +185 -0
- package/dist/static-loader-ClNkHZGn.js.map +1 -0
- package/dist/stream-bgNCMcaA.js +280 -0
- package/dist/stream-bgNCMcaA.js.map +1 -0
- package/dist/vite.d.ts +30 -5
- package/dist/vite.js +501 -62
- package/dist/vite.js.map +1 -1
- package/package.json +7 -2
- package/types.d.ts +5 -0
- package/dist/cookies-rgIAc8IE.js.map +0 -1
- package/dist/devtools-Bm8cS7hX.js.map +0 -1
- package/dist/devtools-panel-CTbDSBBA.js.map +0 -1
- package/dist/html-CxWpXngy.js.map +0 -1
- package/dist/inspect-CGCWfaEx.js.map +0 -1
- package/dist/loader-BUhNqDHw.d.ts +0 -30
- package/dist/loader-py4qu6tQ.js +0 -215
- package/dist/loader-py4qu6tQ.js.map +0 -1
- package/dist/navigate-C5dP89YL.js.map +0 -1
- package/dist/rpc-B86KKjzx.js.map +0 -1
- package/dist/server-CX3JnPCs.js.map +0 -1
- package/dist/shared-BE_oBSDM.js +0 -176
- package/dist/shared-BE_oBSDM.js.map +0 -1
- package/dist/stream-cvhqOzfs.js +0 -145
- package/dist/stream-cvhqOzfs.js.map +0 -1
|
@@ -30,9 +30,9 @@ export declare function $isPending(_memo: unknown): boolean;
|
|
|
30
30
|
/** Returns an ID that is unique in the page and the same on the server and the client. */
|
|
31
31
|
export declare function $id(): string;
|
|
32
32
|
/** Provides a value of a context to the components inside this one. */
|
|
33
|
-
export declare function $
|
|
33
|
+
export declare function $setContext<T>(_context: Context<T>, _value: T): void;
|
|
34
34
|
/** Reads a context from the nearest component above that provides it, or its default value. */
|
|
35
|
-
export declare function $
|
|
35
|
+
export declare function $getContext<T>(_context: Context<T>): T;
|
|
36
36
|
/** Changes an object or an array in a signal through a draft, and writes the signal with the result. */
|
|
37
37
|
export declare function $produce<T>(_signal: T, _recipe: (draft: T) => T | void): void;
|
|
38
38
|
/** Sets the title of the page. The last component that sets it wins. */
|
package/dist/runtime/signals.js
CHANGED
|
@@ -46,12 +46,12 @@ function $id() {
|
|
|
46
46
|
return compileTimeOnly("$id");
|
|
47
47
|
}
|
|
48
48
|
/** Provides a value of a context to the components inside this one. */
|
|
49
|
-
function $
|
|
50
|
-
compileTimeOnly("$
|
|
49
|
+
function $setContext(_context, _value) {
|
|
50
|
+
compileTimeOnly("$setContext");
|
|
51
51
|
}
|
|
52
52
|
/** Reads a context from the nearest component above that provides it, or its default value. */
|
|
53
|
-
function $
|
|
54
|
-
return compileTimeOnly("$
|
|
53
|
+
function $getContext(_context) {
|
|
54
|
+
return compileTimeOnly("$getContext");
|
|
55
55
|
}
|
|
56
56
|
/** Changes an object or an array in a signal through a draft, and writes the signal with the result. */
|
|
57
57
|
function $produce(_signal, _recipe) {
|
|
@@ -69,6 +69,6 @@ function $action(_fn) {
|
|
|
69
69
|
return compileTimeOnly("$action");
|
|
70
70
|
}
|
|
71
71
|
//#endregion
|
|
72
|
-
export { $action, $client, $
|
|
72
|
+
export { $action, $client, $effect, $getContext, $id, $isPending, $memo, $optimistic, $produce, $props, $setContext, $signal, $title, $untrack };
|
|
73
73
|
|
|
74
74
|
//# sourceMappingURL=signals.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signals.js","names":[],"sources":["../../src/runtime/signals.ts"],"sourcesContent":["// The compile-time functions of `.brm` files. The compiler replaces every\n// call and removes the import, so this module only gives them their types.\n// A call outside a `.brm` file throws.\n\nimport type { Context } from './context.ts';\n\nfunction compileTimeOnly(name: string): never {\n throw new Error(`${name} works only in .brm files, which the brumate compiler compiles`);\n}\n\n/** Returns the props of the component. Destructure the call to keep each prop reactive. */\n// oxlint-disable-next-line typescript/no-explicit-any -- Props are any object unless typed.\nexport function $props<T = Record<string, any>>(): T {\n return compileTimeOnly('$props');\n}\n\n/** Declares writable state. The variable reads and writes the signal. */\nexport function $signal<T>(value: T): T;\nexport function $signal<T>(): T | undefined;\nexport function $signal(): never {\n return compileTimeOnly('$signal');\n}\n\n/** The value of a memo: what a promise settles to, or the latest value of an async iterable. */\nexport type MemoValue<T> = Awaited<T> extends AsyncIterable<infer U> ? U : Awaited<T>;\n\n/**\n * Declares a value computed from other values. A promise is awaited where it\n * is read. An async iterable gives each of its values in turn.\n */\nexport function $memo<T>(_value: T): MemoValue<T> {\n return compileTimeOnly('$memo');\n}\n\n/**\n * Declares a value that follows a memo, and that handlers can write. A write\n * shows a guess, such as a new item while the form that adds it posts. The\n * value goes back to the memo when the memo changes, or when the server\n * calls of the page finish, such as after a failed post.\n */\nexport function $optimistic<T>(_source: T): T {\n return compileTimeOnly('$optimistic');\n}\n\n/** Runs a callback when the values it reads change, after the page resumes. Never on load. */\nexport function $effect(_callback: () => void | (() => void)): void {\n compileTimeOnly('$effect');\n}\n\n/** Runs a callback in the browser when a trigger fires. */\nexport function $client(\n _callback: () => void | (() => void),\n _trigger?: 'visible' | 'idle' | 'load',\n): void {\n compileTimeOnly('$client');\n}\n\n/** Reads a value without depending on it. */\nexport function $untrack<T>(_value: T): T {\n return compileTimeOnly('$untrack');\n}\n\n/** True while a new value of a memo, or of a memo it reads, loads on the client. Always false on the server. */\nexport function $isPending(_memo: unknown): boolean {\n return compileTimeOnly('$isPending');\n}\n\n/** Returns an ID that is unique in the page and the same on the server and the client. */\nexport function $id(): string {\n return compileTimeOnly('$id');\n}\n\n/** Provides a value of a context to the components inside this one. */\nexport function $
|
|
1
|
+
{"version":3,"file":"signals.js","names":[],"sources":["../../src/runtime/signals.ts"],"sourcesContent":["// The compile-time functions of `.brm` files. The compiler replaces every\n// call and removes the import, so this module only gives them their types.\n// A call outside a `.brm` file throws.\n\nimport type { Context } from './context.ts';\n\nfunction compileTimeOnly(name: string): never {\n throw new Error(`${name} works only in .brm files, which the brumate compiler compiles`);\n}\n\n/** Returns the props of the component. Destructure the call to keep each prop reactive. */\n// oxlint-disable-next-line typescript/no-explicit-any -- Props are any object unless typed.\nexport function $props<T = Record<string, any>>(): T {\n return compileTimeOnly('$props');\n}\n\n/** Declares writable state. The variable reads and writes the signal. */\nexport function $signal<T>(value: T): T;\nexport function $signal<T>(): T | undefined;\nexport function $signal(): never {\n return compileTimeOnly('$signal');\n}\n\n/** The value of a memo: what a promise settles to, or the latest value of an async iterable. */\nexport type MemoValue<T> = Awaited<T> extends AsyncIterable<infer U> ? U : Awaited<T>;\n\n/**\n * Declares a value computed from other values. A promise is awaited where it\n * is read. An async iterable gives each of its values in turn.\n */\nexport function $memo<T>(_value: T): MemoValue<T> {\n return compileTimeOnly('$memo');\n}\n\n/**\n * Declares a value that follows a memo, and that handlers can write. A write\n * shows a guess, such as a new item while the form that adds it posts. The\n * value goes back to the memo when the memo changes, or when the server\n * calls of the page finish, such as after a failed post.\n */\nexport function $optimistic<T>(_source: T): T {\n return compileTimeOnly('$optimistic');\n}\n\n/** Runs a callback when the values it reads change, after the page resumes. Never on load. */\nexport function $effect(_callback: () => void | (() => void)): void {\n compileTimeOnly('$effect');\n}\n\n/** Runs a callback in the browser when a trigger fires. */\nexport function $client(\n _callback: () => void | (() => void),\n _trigger?: 'visible' | 'idle' | 'load',\n): void {\n compileTimeOnly('$client');\n}\n\n/** Reads a value without depending on it. */\nexport function $untrack<T>(_value: T): T {\n return compileTimeOnly('$untrack');\n}\n\n/** True while a new value of a memo, or of a memo it reads, loads on the client. Always false on the server. */\nexport function $isPending(_memo: unknown): boolean {\n return compileTimeOnly('$isPending');\n}\n\n/** Returns an ID that is unique in the page and the same on the server and the client. */\nexport function $id(): string {\n return compileTimeOnly('$id');\n}\n\n/** Provides a value of a context to the components inside this one. */\nexport function $setContext<T>(_context: Context<T>, _value: T): void {\n compileTimeOnly('$setContext');\n}\n\n/** Reads a context from the nearest component above that provides it, or its default value. */\nexport function $getContext<T>(_context: Context<T>): T {\n return compileTimeOnly('$getContext');\n}\n\n/** Changes an object or an array in a signal through a draft, and writes the signal with the result. */\nexport function $produce<T>(_signal: T, _recipe: (draft: T) => T | void): void {\n compileTimeOnly('$produce');\n}\n\n/** Sets the title of the page. The last component that sets it wins. */\nexport function $title(_value: unknown): void {\n compileTimeOnly('$title');\n}\n\n/**\n * The latest result of a server function that a form of this component posts\n * to, or undefined before the first post. Pending while the form sends.\n */\nexport function $action<F extends (...args: never[]) => unknown>(\n _fn: F,\n): Awaited<ReturnType<F>> | undefined {\n return compileTimeOnly('$action');\n}\n"],"mappings":";AAMA,SAAS,gBAAgB,MAAqB;CAC5C,MAAM,IAAI,MAAM,GAAG,KAAK,+DAA+D;AACzF;;AAIA,SAAgB,SAAqC;CACnD,OAAO,gBAAgB,QAAQ;AACjC;AAKA,SAAgB,UAAiB;CAC/B,OAAO,gBAAgB,SAAS;AAClC;;;;;AASA,SAAgB,MAAS,QAAyB;CAChD,OAAO,gBAAgB,OAAO;AAChC;;;;;;;AAQA,SAAgB,YAAe,SAAe;CAC5C,OAAO,gBAAgB,aAAa;AACtC;;AAGA,SAAgB,QAAQ,WAA4C;CAClE,gBAAgB,SAAS;AAC3B;;AAGA,SAAgB,QACd,WACA,UACM;CACN,gBAAgB,SAAS;AAC3B;;AAGA,SAAgB,SAAY,QAAc;CACxC,OAAO,gBAAgB,UAAU;AACnC;;AAGA,SAAgB,WAAW,OAAyB;CAClD,OAAO,gBAAgB,YAAY;AACrC;;AAGA,SAAgB,MAAc;CAC5B,OAAO,gBAAgB,KAAK;AAC9B;;AAGA,SAAgB,YAAe,UAAsB,QAAiB;CACpE,gBAAgB,aAAa;AAC/B;;AAGA,SAAgB,YAAe,UAAyB;CACtD,OAAO,gBAAgB,aAAa;AACtC;;AAGA,SAAgB,SAAY,SAAY,SAAuC;CAC7E,gBAAgB,UAAU;AAC5B;;AAGA,SAAgB,OAAO,QAAuB;CAC5C,gBAAgB,QAAQ;AAC1B;;;;;AAMA,SAAgB,QACd,KACoC;CACpC,OAAO,gBAAgB,SAAS;AAClC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as startWithoutNavigation } from "../static-loader-ClNkHZGn.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as ClientModule } from "../client-
|
|
2
|
-
import {
|
|
1
|
+
import { t as ClientModule } from "../client-DwJInP_a.js";
|
|
2
|
+
import { l as RenderOptions, t as Component } from "../server-BxqjLUnV.js";
|
|
3
3
|
//#region src/runtime/testing.d.ts
|
|
4
4
|
export interface TestingOptions {
|
|
5
5
|
/** Imports the client module of a component, by component ID. */
|
|
@@ -32,5 +32,24 @@ export interface Mounted {
|
|
|
32
32
|
export type Mount = <P>(component: Component<P>, props: P, renderOptions?: RenderOptions) => Promise<Mounted>;
|
|
33
33
|
/** Returns `mount`, which loads client modules with `options.load`. */
|
|
34
34
|
export declare function createMount(options: TestingOptions): Mount;
|
|
35
|
+
/** Server functions by ID, as `functions` of `virtual:brumate/server` has them. */
|
|
36
|
+
export type ServerFunctions = Record<string, (...args: any[]) => unknown>;
|
|
37
|
+
export interface AnswerOptions {
|
|
38
|
+
/** The URL prefix of server function calls. Defaults to `/_brumate`. */
|
|
39
|
+
rpc?: string;
|
|
40
|
+
/** Called with the ID of each call, such as `issues_1a2b#toggleStar`, before it runs. */
|
|
41
|
+
onCall?: (id: string) => void;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Answers the server function calls of the page in the test, with
|
|
45
|
+
* `functions`, through the same request and answer as a server. Calls go
|
|
46
|
+
* through `fetch`, so it replaces `fetch` until the returned function
|
|
47
|
+
* restores it. Other requests go to the `fetch` from before.
|
|
48
|
+
*
|
|
49
|
+
* `functions` can be a function that returns them, so they are imported on
|
|
50
|
+
* the first call. A server function that calls a server function through
|
|
51
|
+
* `fetch`, as a browser stub does, throws: it would call itself without end.
|
|
52
|
+
*/
|
|
53
|
+
export declare function answerCalls(functions: ServerFunctions | (() => Promise<ServerFunctions>), options?: AnswerOptions): () => void;
|
|
35
54
|
//#endregion
|
|
36
55
|
//# sourceMappingURL=testing.d.ts.map
|
package/dist/runtime/testing.js
CHANGED
|
@@ -1,15 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as start } from "../loader-
|
|
1
|
+
import { g as render, s as handleRequest } from "../server-DZsgnPKS.js";
|
|
2
|
+
import { t as start } from "../loader-Dc9OwY5I.js";
|
|
3
3
|
//#region src/runtime/testing.ts
|
|
4
4
|
/**
|
|
5
|
+
* Runs an inline script, as a browser does when it adds one. The DOM of a
|
|
6
|
+
* test does not run scripts. The script is `document.currentScript` while it
|
|
7
|
+
* runs, so a state record hands its script to the core.
|
|
8
|
+
*/
|
|
9
|
+
function run(script) {
|
|
10
|
+
Object.defineProperty(document, "currentScript", {
|
|
11
|
+
configurable: true,
|
|
12
|
+
value: script
|
|
13
|
+
});
|
|
14
|
+
try {
|
|
15
|
+
(0, eval)(script.textContent);
|
|
16
|
+
} finally {
|
|
17
|
+
delete document.currentScript;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
5
21
|
* Appends markup that streams in later, such as a `#loading` body, and runs
|
|
6
|
-
* its inline scripts
|
|
22
|
+
* its inline scripts in order.
|
|
7
23
|
*/
|
|
8
24
|
function append(container, html) {
|
|
9
25
|
const template = document.createElement("template");
|
|
10
26
|
template.innerHTML = html;
|
|
11
|
-
for (const node of [...template.content.childNodes])
|
|
12
|
-
|
|
27
|
+
for (const node of [...template.content.childNodes]) {
|
|
28
|
+
container.append(node);
|
|
29
|
+
if (node.nodeName === "SCRIPT" && !node.hasAttribute("type")) run(node);
|
|
30
|
+
}
|
|
13
31
|
}
|
|
14
32
|
/** Mounts so far, so each starts its scope IDs at a number of its own. */
|
|
15
33
|
let mounts = 0;
|
|
@@ -31,11 +49,13 @@ function createMount(options) {
|
|
|
31
49
|
const container = document.createElement("div");
|
|
32
50
|
container.innerHTML = html + state;
|
|
33
51
|
document.body.append(container);
|
|
52
|
+
container.querySelectorAll("script[b-state]").forEach(run);
|
|
34
53
|
loader ??= {
|
|
35
54
|
stop: start({
|
|
36
55
|
load: options.load,
|
|
37
56
|
rpc: options.rpc,
|
|
38
|
-
navigate: false
|
|
57
|
+
navigate: false,
|
|
58
|
+
preload: false
|
|
39
59
|
}),
|
|
40
60
|
users: 0
|
|
41
61
|
};
|
|
@@ -86,11 +106,67 @@ function createMount(options) {
|
|
|
86
106
|
unmount: () => {
|
|
87
107
|
stop();
|
|
88
108
|
container.remove();
|
|
109
|
+
import("./client.js").then((core) => core.removeRoot(String(base)));
|
|
89
110
|
}
|
|
90
111
|
};
|
|
91
112
|
};
|
|
92
113
|
}
|
|
114
|
+
let storage;
|
|
115
|
+
/**
|
|
116
|
+
* Tells the calls that a server function makes while it is answered. Node
|
|
117
|
+
* has `AsyncLocalStorage`. Elsewhere, such as in a browser, there is none,
|
|
118
|
+
* and nested calls are not told apart.
|
|
119
|
+
*/
|
|
120
|
+
function callStorage() {
|
|
121
|
+
storage ??= import("node:async_hooks").then(({ AsyncLocalStorage }) => new AsyncLocalStorage(), () => void 0);
|
|
122
|
+
return storage;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Answers the server function calls of the page in the test, with
|
|
126
|
+
* `functions`, through the same request and answer as a server. Calls go
|
|
127
|
+
* through `fetch`, so it replaces `fetch` until the returned function
|
|
128
|
+
* restores it. Other requests go to the `fetch` from before.
|
|
129
|
+
*
|
|
130
|
+
* `functions` can be a function that returns them, so they are imported on
|
|
131
|
+
* the first call. A server function that calls a server function through
|
|
132
|
+
* `fetch`, as a browser stub does, throws: it would call itself without end.
|
|
133
|
+
*/
|
|
134
|
+
function answerCalls(functions, options = {}) {
|
|
135
|
+
const prefix = options.rpc ?? "/_brumate";
|
|
136
|
+
const original = globalThis.fetch;
|
|
137
|
+
let loaded;
|
|
138
|
+
/** The error of a call that a server function made through `fetch`. */
|
|
139
|
+
let failure;
|
|
140
|
+
const answer = async (input, init) => {
|
|
141
|
+
const base = globalThis.location?.href ?? "http://localhost/";
|
|
142
|
+
const url = new URL(input instanceof Request ? input.url : String(input), base);
|
|
143
|
+
if (!url.pathname.startsWith(`${prefix}/`)) return original(input, init);
|
|
144
|
+
const id = decodeURIComponent(url.pathname.slice(prefix.length + 1));
|
|
145
|
+
const calls = await callStorage();
|
|
146
|
+
const caller = calls?.getStore();
|
|
147
|
+
if (caller !== void 0) {
|
|
148
|
+
failure ??= /* @__PURE__ */ new Error(`The server function "${caller}" called "${id}" through fetch, as a browser stub does. In a test, server code imports the server code of a 'use server' file: import components with ?brumate-server, and answer calls with the functions of virtual:brumate/server`);
|
|
149
|
+
throw failure;
|
|
150
|
+
}
|
|
151
|
+
options.onCall?.(id);
|
|
152
|
+
loaded ??= Promise.resolve(typeof functions === "function" ? functions() : functions);
|
|
153
|
+
const registry = await loaded;
|
|
154
|
+
const request = input instanceof Request ? new Request(input, init) : new Request(url, init);
|
|
155
|
+
const handle = () => handleRequest(registry, request, prefix);
|
|
156
|
+
const response = await (calls ? calls.run(id, handle) : handle());
|
|
157
|
+
if (failure) {
|
|
158
|
+
const error = failure;
|
|
159
|
+
failure = void 0;
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
return response ?? original(input, init);
|
|
163
|
+
};
|
|
164
|
+
globalThis.fetch = answer;
|
|
165
|
+
return () => {
|
|
166
|
+
if (globalThis.fetch === answer) globalThis.fetch = original;
|
|
167
|
+
};
|
|
168
|
+
}
|
|
93
169
|
//#endregion
|
|
94
|
-
export { createMount };
|
|
170
|
+
export { answerCalls, createMount };
|
|
95
171
|
|
|
96
172
|
//# sourceMappingURL=testing.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.js","names":[],"sources":["../../src/runtime/testing.ts"],"sourcesContent":["// Renders a component on the server and resumes it in the DOM of a test, as\n// a page does: the HTML and the state first, then the loader, which loads\n// client code when an event fires.\n\nimport type { ClientModule } from './client.ts';\nimport { start } from './loader.ts';\nimport { render } from './server.ts';\nimport type { Component, RenderOptions } from './server.ts';\n\nexport interface TestingOptions {\n /** Imports the client module of a component, by component ID. */\n load: (id: string) => Promise<ClientModule>;\n /** The URL prefix of server function calls. */\n rpc?: string;\n}\n\nexport interface Mounted {\n /** The element that holds the component. */\n container: HTMLElement;\n /** The first element that matches a selector. Throws if none does. */\n get: (selector: string) => HTMLElement;\n /** The text of the first element that matches a selector. */\n text: (selector: string) => string;\n /** Clicks an element. */\n click: (selector: string) => void;\n /** Types a value into a field, and fires `input`. */\n input: (selector: string, value: string) => void;\n /** Submits a form, as its submit button does. */\n submit: (selector: string) => void;\n /**\n * Retries an assertion until it passes. The first event of a test loads\n * client code, so its effects arrive a moment later.\n */\n waitFor: (assertion: () => void, timeout?: number) => Promise<void>;\n /** Stops the loader and removes the component. */\n unmount: () => void;\n}\n\n/**\n * Appends markup that streams in later, such as a `#loading` body, and runs\n * its inline scripts. The DOM of a test does not run them.\n */\nfunction append(container: HTMLElement, html: string): void {\n const template = document.createElement('template');\n template.innerHTML = html;\n // A copy, because appending moves nodes out of the live list.\n // oxlint-disable-next-line unicorn/no-useless-spread\n for (const node of [...template.content.childNodes]) {\n if (node.nodeName === 'SCRIPT' && !(node as Element).hasAttribute('type')) {\n // oxlint-disable-next-line no-eval -- The script is the swap code from the server.\n (0, eval)(node.textContent!);\n } else {\n container.append(node);\n }\n }\n}\n\n/** Renders a component on the server and resumes it in the document of a test. */\nexport type Mount = <P>(\n component: Component<P>,\n props: P,\n renderOptions?: RenderOptions,\n) => Promise<Mounted>;\n\n/** Mounts so far, so each starts its scope IDs at a number of its own. */\nlet mounts = 0;\n/** The loader that the mounts of the document share, and how many mounts use it. */\nlet loader: { stop: () => void; users: number } | undefined;\n\n/** Returns `mount`, which loads client modules with `options.load`. */\nexport function createMount(options: TestingOptions): Mount {\n /**\n * Renders a component on the server, puts it in the document with its\n * state, and starts the loader. Markup that streams in later is added as\n * it arrives.\n */\n return async function mount<P>(\n component: Component<P>,\n props: P,\n renderOptions: RenderOptions = {},\n ): Promise<Mounted> {\n // IDs of their own, so two mounts in a file do not share scopes or state references.\n const base = renderOptions.base ?? ++mounts * 1_000_000;\n const { html, state, pending } = await render(component, props, { ...renderOptions, base });\n const container = document.createElement('div');\n container.innerHTML = html + state;\n document.body.append(container);\n // One loader for the document, as a page has, however many mounts are in it.\n loader ??= {\n stop: start({ load: options.load, rpc: options.rpc, navigate: false }),\n users: 0,\n };\n loader.users++;\n let mounted = true;\n const stop = (): void => {\n if (!mounted || !loader) {\n return;\n }\n mounted = false;\n if (--loader.users === 0) {\n loader.stop();\n loader = undefined;\n }\n };\n void (async () => {\n for await (const chunk of pending) {\n append(container, chunk);\n }\n })();\n const get = (selector: string): HTMLElement => {\n const element = container.querySelector<HTMLElement>(selector);\n if (!element) {\n throw new Error(`No element matches ${selector}`);\n }\n return element;\n };\n return {\n container,\n get,\n text: (selector) => get(selector).textContent ?? '',\n click: (selector) => get(selector).click(),\n input: (selector, value) => {\n const field = get(selector) as HTMLInputElement;\n field.value = value;\n field.dispatchEvent(new Event('input', { bubbles: true }));\n },\n submit: (selector) => {\n get(selector).dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));\n },\n waitFor: async (assertion, timeout = 2000) => {\n const until = Date.now() + timeout;\n for (;;) {\n try {\n assertion();\n return;\n } catch (error) {\n if (Date.now() > until) {\n throw error;\n }\n // oxlint-disable-next-line no-await-in-loop -- It waits between tries.\n await new Promise((resolve) => setTimeout(resolve, 10));\n }\n }\n },\n unmount: () => {\n stop();\n container.remove();\n },\n };\n };\n}\n"],"mappings":";;;;;;;AA0CA,SAAS,OAAO,WAAwB,MAAoB;CAC1D,MAAM,WAAW,SAAS,cAAc,UAAU;CAClD,SAAS,YAAY;CAGrB,KAAK,MAAM,QAAQ,CAAC,GAAG,SAAS,QAAQ,UAAU,GAChD,IAAI,KAAK,aAAa,YAAY,CAAE,KAAiB,aAAa,MAAM,GAEtE,CAAC,GAAG,KAAA,CAAM,KAAK,WAAY;MAE3B,UAAU,OAAO,IAAI;AAG3B;;AAUA,IAAI,SAAS;;AAEb,IAAI;;AAGJ,SAAgB,YAAY,SAAgC;;;;;;CAM1D,OAAO,eAAe,MACpB,WACA,OACA,gBAA+B,CAAC,GACd;EAElB,MAAM,OAAO,cAAc,QAAQ,EAAE,SAAS;EAC9C,MAAM,EAAE,MAAM,OAAO,YAAY,MAAM,OAAO,WAAW,OAAO;GAAE,GAAG;GAAe;EAAK,CAAC;EAC1F,MAAM,YAAY,SAAS,cAAc,KAAK;EAC9C,UAAU,YAAY,OAAO;EAC7B,SAAS,KAAK,OAAO,SAAS;EAE9B,WAAW;GACT,MAAM,MAAM;IAAE,MAAM,QAAQ;IAAM,KAAK,QAAQ;IAAK,UAAU;GAAM,CAAC;GACrE,OAAO;EACT;EACA,OAAO;EACP,IAAI,UAAU;EACd,MAAM,aAAmB;GACvB,IAAI,CAAC,WAAW,CAAC,QACf;GAEF,UAAU;GACV,IAAI,EAAE,OAAO,UAAU,GAAG;IACxB,OAAO,KAAK;IACZ,SAAS,KAAA;GACX;EACF;EACA,CAAM,YAAY;GAChB,WAAW,MAAM,SAAS,SACxB,OAAO,WAAW,KAAK;EAE3B,EAAA,CAAG;EACH,MAAM,OAAO,aAAkC;GAC7C,MAAM,UAAU,UAAU,cAA2B,QAAQ;GAC7D,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,sBAAsB,UAAU;GAElD,OAAO;EACT;EACA,OAAO;GACL;GACA;GACA,OAAO,aAAa,IAAI,QAAQ,CAAC,CAAC,eAAe;GACjD,QAAQ,aAAa,IAAI,QAAQ,CAAC,CAAC,MAAM;GACzC,QAAQ,UAAU,UAAU;IAC1B,MAAM,QAAQ,IAAI,QAAQ;IAC1B,MAAM,QAAQ;IACd,MAAM,cAAc,IAAI,MAAM,SAAS,EAAE,SAAS,KAAK,CAAC,CAAC;GAC3D;GACA,SAAS,aAAa;IACpB,IAAI,QAAQ,CAAC,CAAC,cAAc,IAAI,MAAM,UAAU;KAAE,SAAS;KAAM,YAAY;IAAK,CAAC,CAAC;GACtF;GACA,SAAS,OAAO,WAAW,UAAU,QAAS;IAC5C,MAAM,QAAQ,KAAK,IAAI,IAAI;IAC3B,SACE,IAAI;KACF,UAAU;KACV;IACF,SAAS,OAAO;KACd,IAAI,KAAK,IAAI,IAAI,OACf,MAAM;KAGR,MAAM,IAAI,SAAS,YAAY,WAAW,SAAS,EAAE,CAAC;IACxD;GAEJ;GACA,eAAe;IACb,KAAK;IACL,UAAU,OAAO;GACnB;EACF;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"testing.js","names":[],"sources":["../../src/runtime/testing.ts"],"sourcesContent":["// Renders a component on the server and resumes it in the DOM of a test, as\n// a page does: the HTML and the state first, then the loader, which loads\n// client code when an event fires.\n\nimport type { ClientModule } from './client.ts';\nimport { start } from './loader.ts';\nimport { handleRequest, render } from './server.ts';\nimport type { Component, RenderOptions } from './server.ts';\n\nexport interface TestingOptions {\n /** Imports the client module of a component, by component ID. */\n load: (id: string) => Promise<ClientModule>;\n /** The URL prefix of server function calls. */\n rpc?: string;\n}\n\nexport interface Mounted {\n /** The element that holds the component. */\n container: HTMLElement;\n /** The first element that matches a selector. Throws if none does. */\n get: (selector: string) => HTMLElement;\n /** The text of the first element that matches a selector. */\n text: (selector: string) => string;\n /** Clicks an element. */\n click: (selector: string) => void;\n /** Types a value into a field, and fires `input`. */\n input: (selector: string, value: string) => void;\n /** Submits a form, as its submit button does. */\n submit: (selector: string) => void;\n /**\n * Retries an assertion until it passes. The first event of a test loads\n * client code, so its effects arrive a moment later.\n */\n waitFor: (assertion: () => void, timeout?: number) => Promise<void>;\n /** Stops the loader and removes the component. */\n unmount: () => void;\n}\n\n/**\n * Runs an inline script, as a browser does when it adds one. The DOM of a\n * test does not run scripts. The script is `document.currentScript` while it\n * runs, so a state record hands its script to the core.\n */\nfunction run(script: Element): void {\n Object.defineProperty(document, 'currentScript', { configurable: true, value: script });\n try {\n // oxlint-disable-next-line no-eval -- The script is a state record or the swap code from the server.\n (0, eval)(script.textContent!);\n } finally {\n delete (document as { currentScript?: unknown }).currentScript;\n }\n}\n\n/**\n * Appends markup that streams in later, such as a `#loading` body, and runs\n * its inline scripts in order.\n */\nfunction append(container: HTMLElement, html: string): void {\n const template = document.createElement('template');\n template.innerHTML = html;\n // A copy, because appending moves nodes out of the live list.\n // oxlint-disable-next-line unicorn/no-useless-spread\n for (const node of [...template.content.childNodes]) {\n container.append(node);\n if (node.nodeName === 'SCRIPT' && !(node as Element).hasAttribute('type')) {\n run(node as Element);\n }\n }\n}\n\n/** Renders a component on the server and resumes it in the document of a test. */\nexport type Mount = <P>(\n component: Component<P>,\n props: P,\n renderOptions?: RenderOptions,\n) => Promise<Mounted>;\n\n/** Mounts so far, so each starts its scope IDs at a number of its own. */\nlet mounts = 0;\n/** The loader that the mounts of the document share, and how many mounts use it. */\nlet loader: { stop: () => void; users: number } | undefined;\n\n/** Returns `mount`, which loads client modules with `options.load`. */\nexport function createMount(options: TestingOptions): Mount {\n /**\n * Renders a component on the server, puts it in the document with its\n * state, and starts the loader. Markup that streams in later is added as\n * it arrives.\n */\n return async function mount<P>(\n component: Component<P>,\n props: P,\n renderOptions: RenderOptions = {},\n ): Promise<Mounted> {\n // IDs of their own, so two mounts in a file do not share scopes or state references.\n const base = renderOptions.base ?? ++mounts * 1_000_000;\n const { html, state, pending } = await render(component, props, { ...renderOptions, base });\n const container = document.createElement('div');\n container.innerHTML = html + state;\n document.body.append(container);\n // The page state, which waits for the core.\n container.querySelectorAll('script[b-state]').forEach(run);\n // One loader for the document, as a page has, however many mounts are in it.\n loader ??= {\n stop: start({ load: options.load, rpc: options.rpc, navigate: false, preload: false }),\n users: 0,\n };\n loader.users++;\n let mounted = true;\n const stop = (): void => {\n if (!mounted || !loader) {\n return;\n }\n mounted = false;\n if (--loader.users === 0) {\n loader.stop();\n loader = undefined;\n }\n };\n void (async () => {\n for await (const chunk of pending) {\n append(container, chunk);\n }\n })();\n const get = (selector: string): HTMLElement => {\n const element = container.querySelector<HTMLElement>(selector);\n if (!element) {\n throw new Error(`No element matches ${selector}`);\n }\n return element;\n };\n return {\n container,\n get,\n text: (selector) => get(selector).textContent ?? '',\n click: (selector) => get(selector).click(),\n input: (selector, value) => {\n const field = get(selector) as HTMLInputElement;\n field.value = value;\n field.dispatchEvent(new Event('input', { bubbles: true }));\n },\n submit: (selector) => {\n get(selector).dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));\n },\n waitFor: async (assertion, timeout = 2000) => {\n const until = Date.now() + timeout;\n for (;;) {\n try {\n assertion();\n return;\n } catch (error) {\n if (Date.now() > until) {\n throw error;\n }\n // oxlint-disable-next-line no-await-in-loop -- It waits between tries.\n await new Promise((resolve) => setTimeout(resolve, 10));\n }\n }\n },\n unmount: () => {\n stop();\n container.remove();\n // The core forgets the scopes of this mount, so a refresh later does not load its memos.\n void import('./client.ts').then((core) => core.removeRoot(String(base)));\n },\n };\n };\n}\n\n/** Server functions by ID, as `functions` of `virtual:brumate/server` has them. */\n// oxlint-disable-next-line typescript/no-explicit-any -- Server functions take any arguments.\nexport type ServerFunctions = Record<string, (...args: any[]) => unknown>;\n\nexport interface AnswerOptions {\n /** The URL prefix of server function calls. Defaults to `/_brumate`. */\n rpc?: string;\n /** Called with the ID of each call, such as `issues_1a2b#toggleStar`, before it runs. */\n onCall?: (id: string) => void;\n}\n\n/** What `run` of `AsyncLocalStorage` looks like, the part that `answerCalls` uses. */\ninterface Storage {\n run: <T>(store: string, callback: () => T) => T;\n getStore: () => string | undefined;\n}\n\nlet storage: Promise<Storage | undefined> | undefined;\n\n/**\n * Tells the calls that a server function makes while it is answered. Node\n * has `AsyncLocalStorage`. Elsewhere, such as in a browser, there is none,\n * and nested calls are not told apart.\n */\nfunction callStorage(): Promise<Storage | undefined> {\n storage ??= import('node:async_hooks').then(\n ({ AsyncLocalStorage }) => new AsyncLocalStorage<string>() as Storage,\n () => undefined,\n );\n return storage;\n}\n\n/**\n * Answers the server function calls of the page in the test, with\n * `functions`, through the same request and answer as a server. Calls go\n * through `fetch`, so it replaces `fetch` until the returned function\n * restores it. Other requests go to the `fetch` from before.\n *\n * `functions` can be a function that returns them, so they are imported on\n * the first call. A server function that calls a server function through\n * `fetch`, as a browser stub does, throws: it would call itself without end.\n */\nexport function answerCalls(\n functions: ServerFunctions | (() => Promise<ServerFunctions>),\n options: AnswerOptions = {},\n): () => void {\n const prefix = options.rpc ?? '/_brumate';\n const original = globalThis.fetch;\n let loaded: Promise<ServerFunctions> | undefined;\n /** The error of a call that a server function made through `fetch`. */\n let failure: Error | undefined;\n const answer = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {\n const base = globalThis.location?.href ?? 'http://localhost/';\n const url = new URL(input instanceof Request ? input.url : String(input), base);\n if (!url.pathname.startsWith(`${prefix}/`)) {\n return original(input, init);\n }\n const id = decodeURIComponent(url.pathname.slice(prefix.length + 1));\n const calls = await callStorage();\n const caller = calls?.getStore();\n if (caller !== undefined) {\n // The outer call answers with a generic error, so the error goes to its caller too.\n failure ??= new Error(\n `The server function \"${caller}\" called \"${id}\" through fetch, as a browser stub does. In a test, server code imports the server code of a 'use server' file: import components with ?brumate-server, and answer calls with the functions of virtual:brumate/server`,\n );\n throw failure;\n }\n options.onCall?.(id);\n loaded ??= Promise.resolve(typeof functions === 'function' ? functions() : functions);\n const registry = await loaded;\n const request = input instanceof Request ? new Request(input, init) : new Request(url, init);\n const handle = (): Promise<Response | undefined> => handleRequest(registry, request, prefix);\n const response = await (calls ? calls.run(id, handle) : handle());\n if (failure) {\n const error = failure;\n failure = undefined;\n throw error;\n }\n return response ?? original(input, init);\n };\n globalThis.fetch = answer as typeof fetch;\n return () => {\n // A later `answerCalls` or stub keeps its own `fetch`.\n if (globalThis.fetch === answer) {\n globalThis.fetch = original;\n }\n };\n}\n"],"mappings":"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"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { t as Raw } from "./html-
|
|
2
|
-
import {
|
|
1
|
+
import { t as Raw } from "./html-Du8jY2Cc.js";
|
|
2
|
+
import { a as PageState, i as PackedState, n as ElementReference } from "./produce-CtZ32UGG.js";
|
|
3
3
|
import { t as Context$1 } from "./context-aeo9esY9.js";
|
|
4
|
-
import { SerovalNode } from "seroval";
|
|
5
4
|
//#region src/runtime/cookies.d.ts
|
|
6
5
|
interface CookieOptions {
|
|
7
6
|
/** Seconds until the cookie expires. Without it, the cookie lasts until the browser closes. */
|
|
@@ -241,14 +240,147 @@ interface RouteMatch {
|
|
|
241
240
|
/**
|
|
242
241
|
* Views that show an error in place of the route, from the innermost
|
|
243
242
|
* `_error.brm` to the outermost. Each renders inside the layouts above it.
|
|
244
|
-
* The error view is the `errorView` of its tree.
|
|
245
243
|
*/
|
|
246
244
|
errors: RouteView[];
|
|
247
245
|
}
|
|
248
|
-
/** Finds the route for a path. */
|
|
246
|
+
/** Finds the route for a path. A path with a malformed escape, such as `/%E0`, matches none. */
|
|
249
247
|
declare function matchRoute(tree: RouteNode, pathname: string): RouteMatch | undefined;
|
|
250
248
|
//#endregion
|
|
249
|
+
//#region src/runtime/standard-schema.d.ts
|
|
250
|
+
/** The Standard Schema interface. */
|
|
251
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
252
|
+
/** The Standard Schema properties. */
|
|
253
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
254
|
+
}
|
|
255
|
+
declare namespace StandardSchemaV1 {
|
|
256
|
+
/** The Standard Schema properties interface. */
|
|
257
|
+
export interface Props<Input = unknown, Output = Input> {
|
|
258
|
+
/** The version number of the standard. */
|
|
259
|
+
readonly version: 1;
|
|
260
|
+
/** The vendor name of the schema library. */
|
|
261
|
+
readonly vendor: string;
|
|
262
|
+
/** Validates unknown input values. */
|
|
263
|
+
readonly validate: (value: unknown, options?: StandardSchemaV1.Options | undefined) => Result<Output> | Promise<Result<Output>>;
|
|
264
|
+
/** Inferred types associated with the schema. */
|
|
265
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
266
|
+
}
|
|
267
|
+
/** The result interface of the validate function. */
|
|
268
|
+
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
269
|
+
/** The result interface if validation succeeds. */
|
|
270
|
+
export interface SuccessResult<Output> {
|
|
271
|
+
/** The typed output value. */
|
|
272
|
+
readonly value: Output;
|
|
273
|
+
/** A falsy value for `issues` indicates success. */
|
|
274
|
+
readonly issues?: undefined;
|
|
275
|
+
}
|
|
276
|
+
export interface Options {
|
|
277
|
+
/** Explicit support for additional vendor-specific parameters, if needed. */
|
|
278
|
+
readonly libraryOptions?: Record<string, unknown> | undefined;
|
|
279
|
+
}
|
|
280
|
+
/** The result interface if validation fails. */
|
|
281
|
+
export interface FailureResult {
|
|
282
|
+
/** The issues of failed validation. */
|
|
283
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
284
|
+
}
|
|
285
|
+
/** The issue interface of the failure output. */
|
|
286
|
+
export interface Issue {
|
|
287
|
+
/** The error message of the issue. */
|
|
288
|
+
readonly message: string;
|
|
289
|
+
/** The path of the issue, if any. */
|
|
290
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
291
|
+
}
|
|
292
|
+
/** The path segment interface of the issue. */
|
|
293
|
+
export interface PathSegment {
|
|
294
|
+
/** The key representing a path segment. */
|
|
295
|
+
readonly key: PropertyKey;
|
|
296
|
+
}
|
|
297
|
+
/** The Standard Schema types interface. */
|
|
298
|
+
export interface Types<Input = unknown, Output = Input> {
|
|
299
|
+
/** The input type of the schema. */
|
|
300
|
+
readonly input: Input;
|
|
301
|
+
/** The output type of the schema. */
|
|
302
|
+
readonly output: Output;
|
|
303
|
+
}
|
|
304
|
+
/** Infers the input type of a Standard Schema. */
|
|
305
|
+
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
306
|
+
/** Infers the output type of a Standard Schema. */
|
|
307
|
+
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
308
|
+
}
|
|
309
|
+
//#endregion
|
|
310
|
+
//#region src/runtime/validate.d.ts
|
|
311
|
+
/**
|
|
312
|
+
* A problem that a schema found, as plain data that the server can send to
|
|
313
|
+
* the browser. A path segment is a string or a number.
|
|
314
|
+
*/
|
|
315
|
+
interface ValidationIssue {
|
|
316
|
+
message: string;
|
|
317
|
+
path?: (string | number)[];
|
|
318
|
+
}
|
|
319
|
+
/** The output of a schema, or the issues that it found. */
|
|
320
|
+
type ParseResult<T> = {
|
|
321
|
+
value: T;
|
|
322
|
+
issues?: undefined;
|
|
323
|
+
} | {
|
|
324
|
+
value?: undefined;
|
|
325
|
+
issues: ValidationIssue[];
|
|
326
|
+
};
|
|
327
|
+
/**
|
|
328
|
+
* The error that `parse` throws when the value does not match. It answers
|
|
329
|
+
* with status 400 and its message, as any `ServerError` does. Its `issues`
|
|
330
|
+
* stay on the server.
|
|
331
|
+
*/
|
|
332
|
+
declare class ValidationError extends ServerError {
|
|
333
|
+
readonly issues: ValidationIssue[];
|
|
334
|
+
constructor(issues: ValidationIssue[]);
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Validates a value with a Standard Schema. Returns `{ value }` with the
|
|
338
|
+
* output of the schema, or `{ issues }` as plain data. It does not throw for
|
|
339
|
+
* a value that does not match.
|
|
340
|
+
*/
|
|
341
|
+
declare function safeParse<S extends StandardSchemaV1>(schema: S, value: unknown): Promise<ParseResult<StandardSchemaV1.InferOutput<S>>>;
|
|
342
|
+
/**
|
|
343
|
+
* Validates a value with a Standard Schema, and returns the output of the
|
|
344
|
+
* schema. A value that does not match throws a `ValidationError`, which
|
|
345
|
+
* answers with status 400 and a message built from the first issue.
|
|
346
|
+
*
|
|
347
|
+
* ```ts
|
|
348
|
+
* async function save(input: unknown) {
|
|
349
|
+
* 'use server';
|
|
350
|
+
* const todo = await parse(Todo, input);
|
|
351
|
+
* await db.todos.save(todo);
|
|
352
|
+
* }
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
declare function parse<S extends StandardSchemaV1>(schema: S, value: unknown): Promise<StandardSchemaV1.InferOutput<S>>;
|
|
356
|
+
/**
|
|
357
|
+
* Validates the data of a form with a Standard Schema. The entries become a
|
|
358
|
+
* plain object first. Returns `{ value }` or `{ issues }`, and does not throw
|
|
359
|
+
* for data that does not match, so a function of `$action` can return the
|
|
360
|
+
* issues for the page to show.
|
|
361
|
+
*
|
|
362
|
+
* ```ts
|
|
363
|
+
* async function signUp(form: FormData) {
|
|
364
|
+
* 'use server';
|
|
365
|
+
* const result = await parseForm(SignUp, form);
|
|
366
|
+
* if (result.issues) {
|
|
367
|
+
* return { errors: fieldErrors(result.issues) };
|
|
368
|
+
* }
|
|
369
|
+
* }
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
declare function parseForm<S extends StandardSchemaV1>(schema: S, form: FormData): Promise<ParseResult<StandardSchemaV1.InferOutput<S>>>;
|
|
373
|
+
/**
|
|
374
|
+
* The messages of issues by field, for a page to show next to each field.
|
|
375
|
+
* The key is the path joined with dots, such as `address.city`. Issues
|
|
376
|
+
* without a path, and a path of `__proto__`, are under the key `''`. The
|
|
377
|
+
* result is plain data, so a server function can return it.
|
|
378
|
+
*/
|
|
379
|
+
declare function fieldErrors(issues: readonly (StandardSchemaV1.Issue | ValidationIssue)[]): Record<string, string[]>;
|
|
380
|
+
//#endregion
|
|
251
381
|
//#region src/runtime/server.d.ts
|
|
382
|
+
/** The scope and the slot that hold the list of a list item. */
|
|
383
|
+
type ItemSource = readonly [scope: number, slot: number];
|
|
252
384
|
/** A `#loading` body that renders after the rest of the page. */
|
|
253
385
|
interface Boundary {
|
|
254
386
|
id: number;
|
|
@@ -257,6 +389,14 @@ interface Boundary {
|
|
|
257
389
|
html: string;
|
|
258
390
|
ctx: Context;
|
|
259
391
|
}>;
|
|
392
|
+
/** True when a `#try` body around it failed, so its markers were never sent. */
|
|
393
|
+
cancelled?: boolean;
|
|
394
|
+
}
|
|
395
|
+
/** A point in a render that `restore` goes back to. */
|
|
396
|
+
interface Mark {
|
|
397
|
+
scope: number;
|
|
398
|
+
undo: number;
|
|
399
|
+
owner: number | undefined;
|
|
260
400
|
}
|
|
261
401
|
/**
|
|
262
402
|
* Tracks the scopes of one render. A `#loading` body renders in a child
|
|
@@ -279,6 +419,8 @@ declare class Context {
|
|
|
279
419
|
private readonly tree;
|
|
280
420
|
/** The scope that renders the next component, which becomes its owner. */
|
|
281
421
|
private owner;
|
|
422
|
+
/** Takes back what this context added to the page, from the newest, for `restore`. */
|
|
423
|
+
private readonly undo;
|
|
282
424
|
/** The URL prefix of server function calls. */
|
|
283
425
|
private rpcPrefix;
|
|
284
426
|
/** The names of the slots of each scope, so an error can name a value that cannot be sent. */
|
|
@@ -320,10 +462,35 @@ declare class Context {
|
|
|
320
462
|
consume<T>(context: Context$1<T>, scope: number, slot?: number): T;
|
|
321
463
|
/** The ID that the next call to `scope()` returns. */
|
|
322
464
|
peek(): number;
|
|
465
|
+
/** Marks the render so far, such as before a `#try` body, for `restore`. */
|
|
466
|
+
mark(): Mark;
|
|
467
|
+
/**
|
|
468
|
+
* Takes back what the render added to the page since `mark`, such as a
|
|
469
|
+
* `#try` body that threw, whose markup is not sent: its scopes, the
|
|
470
|
+
* stylesheets it counted as sent, its `#portal` and `#head` markup, its
|
|
471
|
+
* title, its `#loading` bodies, and its links to child components.
|
|
472
|
+
*/
|
|
473
|
+
restore(mark: Mark): void;
|
|
474
|
+
/**
|
|
475
|
+
* Forgets the scopes of this render from `from` on, such as those of a
|
|
476
|
+
* `#try` body that threw, so the client does not resume markup that was
|
|
477
|
+
* never sent. `#loading` bodies render in contexts of their own, so their
|
|
478
|
+
* scopes stay.
|
|
479
|
+
*/
|
|
480
|
+
private drop;
|
|
323
481
|
private record;
|
|
324
482
|
slots(scope: number, values: unknown[], names?: string[]): void;
|
|
325
|
-
/**
|
|
326
|
-
|
|
483
|
+
/**
|
|
484
|
+
* The list of each list item scope whose list is a slot of an enclosing
|
|
485
|
+
* scope, as `[scope, slot]`. The items of a list share one array.
|
|
486
|
+
*/
|
|
487
|
+
readonly sources: Map<number, ItemSource>;
|
|
488
|
+
/**
|
|
489
|
+
* Records the item of a list that can change on the client. `source` is
|
|
490
|
+
* the scope and slot that hold the list, when the compiler found one, so
|
|
491
|
+
* the client can take the item from there.
|
|
492
|
+
*/
|
|
493
|
+
item(scope: number, value: unknown, source?: ItemSource): void;
|
|
327
494
|
/** Records the scope of a child component at an anchor of its parent. */
|
|
328
495
|
link(scope: number, anchor: number, child: number): void;
|
|
329
496
|
/** Returns the reference that a `ref` signal holds on the server. */
|
|
@@ -384,6 +551,11 @@ declare function memo<T>(compute: () => T, ctx?: Context): Memo<T>;
|
|
|
384
551
|
declare function read(node: Memo): any;
|
|
385
552
|
/** Waits for the value of a memo, through every load it reads. */
|
|
386
553
|
declare function wait(node: Memo): Promise<unknown>;
|
|
554
|
+
/**
|
|
555
|
+
* The value of a memo when it is ready, or a promise of it while it loads.
|
|
556
|
+
* The render then awaits only a memo that is loading.
|
|
557
|
+
*/
|
|
558
|
+
declare function ready(node: Memo): unknown;
|
|
387
559
|
declare const LAZY: unique symbol;
|
|
388
560
|
/** Markup that renders where it is inserted, so it can read the contexts there. */
|
|
389
561
|
interface Lazy {
|
|
@@ -402,7 +574,7 @@ declare function actionResult(ctx: Context, fn: unknown): unknown;
|
|
|
402
574
|
* Renders a value inserted in markup. `ctx` and `scope` are where it is
|
|
403
575
|
* inserted, for markup that renders there.
|
|
404
576
|
*/
|
|
405
|
-
declare function insert(value: unknown, ctx?: Context, scope?: number): Promise<string>;
|
|
577
|
+
declare function insert(value: unknown, ctx?: Context, scope?: number): string | Promise<string>;
|
|
406
578
|
type Component<P = any> = (props: P, ctx: Context) => Promise<string>;
|
|
407
579
|
/**
|
|
408
580
|
* Renders a child component. With `scope` and `anchor`, records the child's
|
|
@@ -410,21 +582,40 @@ type Component<P = any> = (props: P, ctx: Context) => Promise<string>;
|
|
|
410
582
|
*/
|
|
411
583
|
declare function component<P>(Child: Component<P>, props: P, ctx: Context, scope?: number, anchor?: number): Promise<string>;
|
|
412
584
|
/** Renders a dynamic tag, which can be an element name or a component. */
|
|
413
|
-
declare function dynamic(tag: string | Component, attributes: Record<string, unknown>, children: Raw | undefined, ctx: Context, scope?: number): Promise<string>;
|
|
414
|
-
declare function streamState(state: PageState, onRecord: (record: SerovalNode) => void): Promise<void>;
|
|
585
|
+
declare function dynamic(tag: string | Component, attributes: Record<string, unknown>, children: Raw | undefined, ctx: Context, scope?: number): string | Promise<string>;
|
|
415
586
|
/**
|
|
416
|
-
*
|
|
417
|
-
*
|
|
587
|
+
* Writes the records of scopes with the same kind, parent and shape, such as
|
|
588
|
+
* the items of a list, in groups. Each one then costs its slots and the gap
|
|
589
|
+
* to the ID before it. The items of a list whose list is in `sources` and in
|
|
590
|
+
* the state are not written. See `PackedState`.
|
|
418
591
|
*/
|
|
592
|
+
declare function packState(state: PageState, sources?: Map<number, ItemSource>): PackedState;
|
|
419
593
|
/**
|
|
420
|
-
*
|
|
421
|
-
*
|
|
594
|
+
* Serializes the page state as the code of state records. `onRecord`
|
|
595
|
+
* receives each record as soon as it is ready. The promise resolves when
|
|
596
|
+
* every promise in the state has settled.
|
|
422
597
|
*/
|
|
423
|
-
declare function
|
|
598
|
+
declare function streamState(state: PageState, onRecord: (code: string, initial: boolean) => void): Promise<void>;
|
|
599
|
+
interface StateAttributes {
|
|
600
|
+
/** A number above every ID so far, on each record with scopes. */
|
|
424
601
|
next?: number;
|
|
602
|
+
/** The first record of a render defines `$bR`. Later ones call it. */
|
|
603
|
+
define?: boolean;
|
|
604
|
+
/** The route of the page. */
|
|
425
605
|
route?: string;
|
|
606
|
+
/** The base of the render, when it is above 0. */
|
|
426
607
|
root?: number;
|
|
427
|
-
|
|
608
|
+
/** The Content Security Policy nonce of the script. */
|
|
609
|
+
nonce?: string;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Returns the script of a state record. `scopes` marks a record with scopes,
|
|
613
|
+
* as opposed to one that settles a promise. The record is code that seroval
|
|
614
|
+
* wrote, as a function of the references of its render, `$R`, and of the
|
|
615
|
+
* functions that make brumate's values, `$b`. seroval escapes every `<` in
|
|
616
|
+
* the strings in it.
|
|
617
|
+
*/
|
|
618
|
+
declare function stateScript(code: string, scopes?: boolean, attributes?: StateAttributes): string;
|
|
428
619
|
interface RenderResult {
|
|
429
620
|
/** The markup. A `#loading` body that is not ready shows its fallback. */
|
|
430
621
|
html: string;
|
|
@@ -468,8 +659,8 @@ interface RenderOptions {
|
|
|
468
659
|
/** The event of the request, in place of `request`, such as one that middleware already set cookies on. */
|
|
469
660
|
event?: RequestEvent;
|
|
470
661
|
/**
|
|
471
|
-
* A Content Security Policy nonce for the inline scripts
|
|
472
|
-
*
|
|
662
|
+
* A Content Security Policy nonce for the inline scripts: the state
|
|
663
|
+
* records, and the scripts that swap in `#loading` bodies.
|
|
473
664
|
*/
|
|
474
665
|
nonce?: string;
|
|
475
666
|
}
|
|
@@ -558,5 +749,5 @@ interface RouteOptions extends DocumentOptions, RequestOptions {
|
|
|
558
749
|
*/
|
|
559
750
|
declare function handleRoute(tree: RouteNode, request: Request, options?: RouteOptions): Promise<Response | undefined>;
|
|
560
751
|
//#endregion
|
|
561
|
-
export {
|
|
562
|
-
//# sourceMappingURL=server-
|
|
752
|
+
export { ServerError as $, stateScript as A, safeParse as B, lazy as C, ready as D, read as E, ValidationError as F, Middleware as G, EndpointLoader as H, ValidationIssue as I, RouteNode as J, MiddlewareLoader as K, fieldErrors as L, streamState as M, wait as N, render as O, ParseResult as P, RequestEvent as Q, parse as R, isCrossSite as S, packState as T, Handler as U, StandardSchemaV1 as V, Loader as W, matchRoute as X, RouteView as Y, Redirect as Z, component as _, Lazy as a, CookieOptions as at, handleRoute as b, NotReady as c, createSession as ct, RequestOptions as d, toNodeHeaders as dt, dedupe as et, RouteOptions as f, actionResult as g, action as h, ItemSource as i, withEvent as it, streamParts as j, renderToStream as k, RenderOptions as l, parseCookies as lt, StreamOptions as m, Context as n, isRedirect as nt, Mark as o, Session as ot, StateAttributes as p, RouteMatch as q, DocumentOptions as r, redirect as rt, Memo as s, SessionOptions as st, Component as t, getEvent as tt, RenderResult as u, serializeCookie as ut, dynamic as v, memo as w, insert as x, handleRequest as y, parseForm as z };
|
|
753
|
+
//# sourceMappingURL=server-BxqjLUnV.d.ts.map
|