@krate/runtime 0.5.0-alpha.2 → 0.5.0-alpha.3
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/dist/action-context.d.ts +49 -0
- package/dist/action-context.js +37 -0
- package/dist/action-context.test.d.ts +1 -0
- package/dist/action-context.test.js +47 -0
- package/dist/actions.d.ts +120 -0
- package/dist/actions.js +533 -0
- package/dist/actions.test.d.ts +1 -0
- package/dist/actions.test.js +391 -0
- package/dist/boundary.d.ts +121 -0
- package/dist/boundary.js +429 -0
- package/dist/class-component.d.ts +66 -0
- package/dist/class-component.js +633 -0
- package/dist/class-component.test.d.ts +1 -0
- package/dist/class-component.test.js +411 -0
- package/dist/context.d.ts +13 -0
- package/dist/context.js +58 -32
- package/dist/context.test.d.ts +1 -0
- package/dist/context.test.js +57 -0
- package/dist/derived.test.d.ts +1 -0
- package/dist/derived.test.js +140 -0
- package/dist/discrete.test.d.ts +1 -0
- package/dist/discrete.test.js +100 -0
- package/dist/dom.d.ts +55 -0
- package/dist/dom.js +414 -63
- package/dist/effects.d.ts +88 -0
- package/dist/effects.js +719 -0
- package/dist/effects.test.d.ts +1 -0
- package/dist/effects.test.js +485 -0
- package/dist/events.d.ts +58 -0
- package/dist/events.js +503 -0
- package/dist/events.test.d.ts +1 -0
- package/dist/events.test.js +447 -0
- package/dist/index.d.ts +12 -2
- package/dist/index.js +8 -2
- package/dist/jsx-types.d.ts +2 -0
- package/dist/krate-hydrate-lite.js +7 -1
- package/dist/krate-hydrate.js +7 -1
- package/dist/krate-runtime.js +7 -1
- package/dist/krate-suspense.js +2 -0
- package/dist/lanes.test.d.ts +1 -0
- package/dist/lanes.test.js +59 -0
- package/dist/list.d.ts +20 -0
- package/dist/list.js +466 -0
- package/dist/list.test.d.ts +1 -0
- package/dist/list.test.js +344 -0
- package/dist/optimistic-lanes.test.d.ts +1 -0
- package/dist/optimistic-lanes.test.js +45 -0
- package/dist/portal.d.ts +43 -0
- package/dist/portal.js +178 -0
- package/dist/portal.test.d.ts +1 -0
- package/dist/portal.test.js +99 -0
- package/dist/react-effects.test.d.ts +1 -0
- package/dist/react-effects.test.js +96 -0
- package/dist/react-suspense.test.d.ts +1 -0
- package/dist/react-suspense.test.js +194 -0
- package/dist/react.d.ts +132 -32
- package/dist/react.js +335 -81
- package/dist/react.test.js +12 -2
- package/dist/reconcile.js +13 -0
- package/dist/refs.d.ts +33 -0
- package/dist/refs.js +142 -0
- package/dist/refs.test.d.ts +1 -0
- package/dist/refs.test.js +257 -0
- package/dist/resource.js +20 -1
- package/dist/server-renderer.js +39 -1
- package/dist/server.d.ts +4 -1
- package/dist/server.js +30 -1
- package/dist/server.test.js +20 -0
- package/dist/signal-lanes.test.d.ts +1 -0
- package/dist/signal-lanes.test.js +32 -0
- package/dist/signal.d.ts +150 -1
- package/dist/signal.js +750 -65
- package/dist/slots.d.ts +18 -0
- package/dist/slots.js +108 -0
- package/dist/slots.test.d.ts +1 -0
- package/dist/slots.test.js +169 -0
- package/dist/snapshots.test.d.ts +1 -0
- package/dist/snapshots.test.js +208 -0
- package/dist/suspense-actions.test.d.ts +1 -0
- package/dist/suspense-actions.test.js +72 -0
- package/dist/suspense.d.ts +220 -0
- package/dist/suspense.js +901 -0
- package/dist/suspense.test.d.ts +1 -0
- package/dist/suspense.test.js +518 -0
- package/package.json +1 -1
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request context for server actions, exported from `@krate/runtime/server`.
|
|
3
|
+
*
|
|
4
|
+
* The action sidecar runs every action inside an AsyncLocalStorage scope and
|
|
5
|
+
* publishes it on `globalThis[Symbol.for('krate.action.store')]`. These helpers
|
|
6
|
+
* read that scope, so they only work while a server action is executing.
|
|
7
|
+
*/
|
|
8
|
+
/** Options for `cookies().set()`. HttpOnly, Path=/ and SameSite=Lax are the defaults. */
|
|
9
|
+
export interface ActionCookieOptions {
|
|
10
|
+
path?: string;
|
|
11
|
+
domain?: string;
|
|
12
|
+
maxAge?: number;
|
|
13
|
+
expires?: Date;
|
|
14
|
+
httpOnly?: boolean;
|
|
15
|
+
secure?: boolean;
|
|
16
|
+
sameSite?: 'Strict' | 'Lax' | 'None' | boolean;
|
|
17
|
+
}
|
|
18
|
+
/** Cookie access for the current action request. */
|
|
19
|
+
export interface ActionCookies {
|
|
20
|
+
get(name: string): string | undefined;
|
|
21
|
+
getAll(): Array<{
|
|
22
|
+
name: string;
|
|
23
|
+
value: string;
|
|
24
|
+
}>;
|
|
25
|
+
has(name: string): boolean;
|
|
26
|
+
/** Adds a Set-Cookie header to the action response. */
|
|
27
|
+
set(name: string, value: string, options?: ActionCookieOptions): void;
|
|
28
|
+
delete(name: string, options?: ActionCookieOptions): void;
|
|
29
|
+
}
|
|
30
|
+
/** The request a server action is running for. */
|
|
31
|
+
export interface ActionContext {
|
|
32
|
+
actionId: string;
|
|
33
|
+
url: string;
|
|
34
|
+
method: string;
|
|
35
|
+
headers: Headers;
|
|
36
|
+
cookies: ActionCookies;
|
|
37
|
+
}
|
|
38
|
+
/** The current action context, or undefined outside a server action. */
|
|
39
|
+
export declare function getActionContext(): ActionContext | undefined;
|
|
40
|
+
/** Request headers of the current server action call. */
|
|
41
|
+
export declare function headers(): Headers;
|
|
42
|
+
/** Cookies of the current server action call. */
|
|
43
|
+
export declare function cookies(): ActionCookies;
|
|
44
|
+
/**
|
|
45
|
+
* Ends the server action and navigates the client to `url` (a JSON
|
|
46
|
+
* `redirect` for enhanced forms, a 303 for native form posts). Returning
|
|
47
|
+
* `{ redirect: url }` from the action has the same effect.
|
|
48
|
+
*/
|
|
49
|
+
export declare function redirect(url: string): never;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request context for server actions, exported from `@krate/runtime/server`.
|
|
3
|
+
*
|
|
4
|
+
* The action sidecar runs every action inside an AsyncLocalStorage scope and
|
|
5
|
+
* publishes it on `globalThis[Symbol.for('krate.action.store')]`. These helpers
|
|
6
|
+
* read that scope, so they only work while a server action is executing.
|
|
7
|
+
*/
|
|
8
|
+
/** The current action context, or undefined outside a server action. */
|
|
9
|
+
export function getActionContext() {
|
|
10
|
+
const store = globalThis[Symbol.for('krate.action.store')];
|
|
11
|
+
return store && typeof store.getStore === 'function' ? store.getStore() : undefined;
|
|
12
|
+
}
|
|
13
|
+
function requireContext(api) {
|
|
14
|
+
const ctx = getActionContext();
|
|
15
|
+
if (!ctx) {
|
|
16
|
+
throw new Error(`${api}() can only be called while a server action runs (inside a "use server" export)`);
|
|
17
|
+
}
|
|
18
|
+
return ctx;
|
|
19
|
+
}
|
|
20
|
+
/** Request headers of the current server action call. */
|
|
21
|
+
export function headers() {
|
|
22
|
+
return requireContext('headers').headers;
|
|
23
|
+
}
|
|
24
|
+
/** Cookies of the current server action call. */
|
|
25
|
+
export function cookies() {
|
|
26
|
+
return requireContext('cookies').cookies;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Ends the server action and navigates the client to `url` (a JSON
|
|
30
|
+
* `redirect` for enhanced forms, a 303 for native form posts). Returning
|
|
31
|
+
* `{ redirect: url }` from the action has the same effect.
|
|
32
|
+
*/
|
|
33
|
+
export function redirect(url) {
|
|
34
|
+
const err = new Error('Server action redirect to ' + url);
|
|
35
|
+
err.__krateRedirect = url;
|
|
36
|
+
throw err;
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import { cookies, getActionContext, headers, redirect } from './action-context.js';
|
|
3
|
+
import { jsx, renderToString } from './server.js';
|
|
4
|
+
const KEY = Symbol.for('krate.action.store');
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
delete globalThis[KEY];
|
|
7
|
+
});
|
|
8
|
+
describe('server action context', () => {
|
|
9
|
+
it('throws outside an action with an actionable message', () => {
|
|
10
|
+
expect(getActionContext()).toBeUndefined();
|
|
11
|
+
expect(() => headers()).toThrow(/only be called while a server action runs/);
|
|
12
|
+
});
|
|
13
|
+
it('reads headers and cookies from the sidecar store', () => {
|
|
14
|
+
const jar = new Map([['sid', 'abc']]);
|
|
15
|
+
const ctx = {
|
|
16
|
+
actionId: 'a1',
|
|
17
|
+
url: '/__krate/action/a1',
|
|
18
|
+
method: 'POST',
|
|
19
|
+
headers: new Headers({ 'x-test': 'yes' }),
|
|
20
|
+
cookies: {
|
|
21
|
+
get: (n) => jar.get(n),
|
|
22
|
+
getAll: () => [],
|
|
23
|
+
has: (n) => jar.has(n),
|
|
24
|
+
set: () => { },
|
|
25
|
+
delete: () => { },
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
globalThis[KEY] = { getStore: () => ctx };
|
|
29
|
+
expect(headers().get('x-test')).toBe('yes');
|
|
30
|
+
expect(cookies().get('sid')).toBe('abc');
|
|
31
|
+
expect(getActionContext()?.actionId).toBe('a1');
|
|
32
|
+
});
|
|
33
|
+
it('redirect() throws the marker the sidecar turns into a navigation', () => {
|
|
34
|
+
let caught;
|
|
35
|
+
try {
|
|
36
|
+
redirect('/done');
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
caught = e;
|
|
40
|
+
}
|
|
41
|
+
expect(caught.__krateRedirect).toBe('/done');
|
|
42
|
+
});
|
|
43
|
+
it('does not render client function actions as attributes', () => {
|
|
44
|
+
const html = renderToString(jsx('form', { action: async () => { }, children: 'x' }));
|
|
45
|
+
expect(html).toBe('<form>x</form>');
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/** Field and query parameter carrying the JSON leading arguments of a form call. */
|
|
2
|
+
export declare const ACTION_ARGS_FIELD = "__krate_args";
|
|
3
|
+
/** A client proxy for a server action. */
|
|
4
|
+
export interface ServerActionProxy {
|
|
5
|
+
(...args: unknown[]): Promise<unknown>;
|
|
6
|
+
readonly actionUrl: string;
|
|
7
|
+
readonly $$krateAction: string;
|
|
8
|
+
readonly $$krateBound: unknown[];
|
|
9
|
+
bind(thisArg: unknown, ...args: unknown[]): ServerActionProxy;
|
|
10
|
+
}
|
|
11
|
+
/** Error raised on the client when a server action fails. */
|
|
12
|
+
export declare class ServerActionError extends Error {
|
|
13
|
+
/** Opaque id logged next to the full error on the server. */
|
|
14
|
+
digest?: string;
|
|
15
|
+
status: number;
|
|
16
|
+
constructor(message: string, status: number, digest?: string);
|
|
17
|
+
}
|
|
18
|
+
/** True for a server action proxy created by `__krateAction`. */
|
|
19
|
+
export declare function isServerAction(value: unknown): value is ServerActionProxy;
|
|
20
|
+
/**
|
|
21
|
+
* POSTs one action call. A trailing FormData argument is sent as multipart
|
|
22
|
+
* with the leading arguments JSON-encoded in `__krate_args`; anything else is
|
|
23
|
+
* sent as a JSON `{args}` body. Resolves with the action's return value and
|
|
24
|
+
* follows a `redirect` from the response.
|
|
25
|
+
*/
|
|
26
|
+
export declare function callServerAction(url: string, args: unknown[]): Promise<unknown>;
|
|
27
|
+
/** Client proxy for the server action with the given id. */
|
|
28
|
+
export declare function __krateAction(id: string): ServerActionProxy;
|
|
29
|
+
/**
|
|
30
|
+
* Marks the start of an action or async transition and returns its end
|
|
31
|
+
* callback. Optimistic updates stay applied while any scope is open. Exposed so
|
|
32
|
+
* `startTransition` implementations can join the same scope.
|
|
33
|
+
*/
|
|
34
|
+
export declare function __krateBeginAction(): () => void;
|
|
35
|
+
/** True while an action or async transition is pending. */
|
|
36
|
+
export declare function isActionPending(): boolean;
|
|
37
|
+
/** Runs fn as an async transition: the action scope stays open until it settles. */
|
|
38
|
+
export type TransitionRunner = <T>(fn: () => T | Promise<T>) => Promise<T>;
|
|
39
|
+
/**
|
|
40
|
+
* Replaces the transition runner used for form actions and `useActionState`
|
|
41
|
+
* dispatches, so a framework-wide async `startTransition` can own them. Passing
|
|
42
|
+
* null restores the built-in runner.
|
|
43
|
+
*/
|
|
44
|
+
export declare function setActionTransitionRunner(next: TransitionRunner | null): void;
|
|
45
|
+
/** Runs fn inside the current action transition runner. */
|
|
46
|
+
export declare function runActionTransition<T>(fn: () => T | Promise<T>): Promise<T>;
|
|
47
|
+
/**
|
|
48
|
+
* Reports an action error. An error boundary integration can install
|
|
49
|
+
* `globalThis.__krateActionError(error, element)` and return true to claim the
|
|
50
|
+
* error; otherwise a bubbling `krate:action-error` event is dispatched on the
|
|
51
|
+
* element (or document) and the error is logged.
|
|
52
|
+
*/
|
|
53
|
+
export declare function reportActionError(error: unknown, el?: Element | null): void;
|
|
54
|
+
/** The value `useFormStatus()` returns. */
|
|
55
|
+
export interface FormStatus {
|
|
56
|
+
pending: boolean;
|
|
57
|
+
data: FormData | null;
|
|
58
|
+
method: string | null;
|
|
59
|
+
action: unknown;
|
|
60
|
+
}
|
|
61
|
+
/** Setters a lowered `useFormStatus()` hands to the runtime. */
|
|
62
|
+
export interface FormStatusSetters {
|
|
63
|
+
status?: (s: FormStatus) => void;
|
|
64
|
+
pending?: (v: boolean) => void;
|
|
65
|
+
data?: (v: FormData | null) => void;
|
|
66
|
+
method?: (v: string | null) => void;
|
|
67
|
+
action?: (v: unknown) => void;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Subscribes a component's `useFormStatus()` to the nearest form that contains
|
|
71
|
+
* its root element `el` (not `el` itself, matching React: a component reads the
|
|
72
|
+
* status of a parent form). Returns the unsubscribe function, used as the
|
|
73
|
+
* callback-ref cleanup when the element detaches.
|
|
74
|
+
*/
|
|
75
|
+
export declare function __krateFormStatusMount(el: Element | null, setters: FormStatusSetters): () => void;
|
|
76
|
+
/**
|
|
77
|
+
* Assigns an element to a ref value (callback or `{ current }` object). Used
|
|
78
|
+
* when the compiler composes a component's own ref with the useFormStatus
|
|
79
|
+
* mount ref.
|
|
80
|
+
*/
|
|
81
|
+
export declare function __krateAssignRef(ref: unknown, el: Element | null): void;
|
|
82
|
+
/** Current status of a form (idle unless an action submitted from it is pending). */
|
|
83
|
+
export declare function getFormStatus(form: HTMLFormElement): FormStatus;
|
|
84
|
+
/**
|
|
85
|
+
* React DOM's `requestFormReset(form)`: resets the form's uncontrolled fields
|
|
86
|
+
* once the action currently pending on it settles, or immediately when none is.
|
|
87
|
+
*/
|
|
88
|
+
export declare function requestFormReset(form: HTMLFormElement): void;
|
|
89
|
+
type Getter = () => unknown;
|
|
90
|
+
/** Binds a function action to a form or submit button (`action={fn}` / `formAction={fn}`). */
|
|
91
|
+
export declare function __krateBindFormAction(el: Element, get: Getter): void;
|
|
92
|
+
interface RunnableAction {
|
|
93
|
+
(payload: unknown): unknown;
|
|
94
|
+
__krateRun?: (payload: unknown) => Promise<boolean>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Runs a form action: sets the form status, invokes the action with the form
|
|
98
|
+
* data in an async transition, resets the form on success and reports errors.
|
|
99
|
+
* Resolves true when the action succeeded.
|
|
100
|
+
*/
|
|
101
|
+
export declare function submitFormAction(form: HTMLFormElement, action: unknown, data: FormData, submitter?: HTMLElement | null, url?: string): Promise<boolean>;
|
|
102
|
+
/** Installs the document submit listener once per window. */
|
|
103
|
+
export declare function installActionForms(doc?: Document | undefined): void;
|
|
104
|
+
/**
|
|
105
|
+
* Backs `const [state, formAction, isPending] = useActionState(fn, initial)`.
|
|
106
|
+
* The returned dispatch can be a form action, a button formAction, or called
|
|
107
|
+
* directly. Calls run one at a time in dispatch order; each receives the state
|
|
108
|
+
* the previous call produced. An error cancels the queued calls and is
|
|
109
|
+
* reported to the nearest error boundary hook.
|
|
110
|
+
*/
|
|
111
|
+
export declare function __krateActionState<S>(action: (prev: S, payload: unknown) => S | Promise<S>, getState: () => S, setState: (next: S) => void, setPending: (pending: boolean) => void): RunnableAction;
|
|
112
|
+
/**
|
|
113
|
+
* Backs `const [value, addOptimistic] = useOptimistic(state, update?)`.
|
|
114
|
+
* Optimistic updates are applied on top of the latest base state while an
|
|
115
|
+
* action or async transition is pending and dropped when every pending action
|
|
116
|
+
* has settled. Updates made outside any action are dropped on the next
|
|
117
|
+
* microtask unless an action starts first.
|
|
118
|
+
*/
|
|
119
|
+
export declare function __krateOptimistic<T, A>(base: () => T, set: (value: T) => void, update?: (state: T, action: A) => T): (action: A) => void;
|
|
120
|
+
export {};
|