@barqjs/start 0.0.0-stage → 0.2.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +791 -2
- package/dist/client-BHyIiYxt.js +1527 -0
- package/dist/client-BHyIiYxt.js.map +1 -0
- package/dist/client-DHCV7yvQ.d.ts +440 -0
- package/dist/client-DHCV7yvQ.d.ts.map +1 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +4 -0
- package/dist/env-qA8EyOr1.d.ts +49 -0
- package/dist/env-qA8EyOr1.d.ts.map +1 -0
- package/dist/env.d.ts +2 -0
- package/dist/env.js +44 -0
- package/dist/env.js.map +1 -0
- package/dist/index-Bm_-MTj6.d.ts +915 -0
- package/dist/index-Bm_-MTj6.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +6 -0
- package/dist/middleware-BdVlE8qm.d.ts +576 -0
- package/dist/middleware-BdVlE8qm.d.ts.map +1 -0
- package/dist/middleware.d.ts +2 -0
- package/dist/middleware.js +282 -0
- package/dist/middleware.js.map +1 -0
- package/dist/prerender.d.ts +98 -0
- package/dist/prerender.d.ts.map +1 -0
- package/dist/prerender.js +289 -0
- package/dist/prerender.js.map +1 -0
- package/dist/protocol.d.ts +58 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +26 -0
- package/dist/protocol.js.map +1 -0
- package/dist/serve.d.ts +135 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +223 -0
- package/dist/serve.js.map +1 -0
- package/dist/server-BV3xKmLa.js +1148 -0
- package/dist/server-BV3xKmLa.js.map +1 -0
- package/dist/server.d.ts +142 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1 -0
- package/dist/src-ChnB_xug.js +1140 -0
- package/dist/src-ChnB_xug.js.map +1 -0
- package/dist/static-fn-DgjNArBe.js +123 -0
- package/dist/static-fn-DgjNArBe.js.map +1 -0
- package/package.json +106 -4
- package/src/client.ts +672 -0
- package/src/context.ts +666 -0
- package/src/cookies.ts +229 -0
- package/src/env.ts +86 -0
- package/src/fn-error.ts +97 -0
- package/src/frames-client.ts +1216 -0
- package/src/frames-server.ts +464 -0
- package/src/frames.ts +285 -0
- package/src/index.ts +714 -0
- package/src/live-client.ts +50 -0
- package/src/live-server.ts +182 -0
- package/src/middleware.ts +994 -0
- package/src/prerender.ts +477 -0
- package/src/protocol.ts +61 -0
- package/src/rate-limit.ts +202 -0
- package/src/serve.ts +153 -0
- package/src/server.ts +990 -0
- package/src/session.ts +449 -0
- package/src/start-instance.ts +147 -0
- package/src/static-fn.ts +181 -0
- package/src/static.ts +302 -0
- package/src/stubs.ts +32 -0
- package/src/timing.ts +29 -0
- package/src/wire-types.ts +139 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server half of frames: what a server function's component result becomes,
|
|
3
|
+
* rendered two ways.
|
|
4
|
+
*
|
|
5
|
+
* In the document, where a route renders it with the page's own props, the
|
|
6
|
+
* frame is written inline and each slot holds the browser's content rendered
|
|
7
|
+
* here for the first paint. Over the wire, answering a call from the browser,
|
|
8
|
+
* the region is rendered on its own and its slots are left empty for the
|
|
9
|
+
* browser to fill. Both write the markup `frames.ts` describes.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { HeadTag } from "@barqjs/web";
|
|
13
|
+
import { block, childScope, getOwner, runWithOwner, untrack } from "@barqjs/core";
|
|
14
|
+
import { SSR_HTML_BRAND, setHeadSink } from "@barqjs/web/internal";
|
|
15
|
+
import { setHydratable } from "@barqjs/core/internal";
|
|
16
|
+
import { SlotValue, esc, html, renderFrame, setServerComponent } from "@barqjs/server";
|
|
17
|
+
import {
|
|
18
|
+
encodeWire,
|
|
19
|
+
registerSerializationAdapters,
|
|
20
|
+
serializationAdapters,
|
|
21
|
+
} from "@barqjs/server/codec";
|
|
22
|
+
|
|
23
|
+
import { collectRequestCss } from "./context.ts";
|
|
24
|
+
import {
|
|
25
|
+
FRAME,
|
|
26
|
+
FRAME_CLOSE,
|
|
27
|
+
FLIGHT_TYPE,
|
|
28
|
+
FRAMES_BUDGET,
|
|
29
|
+
FRAME_VERSION,
|
|
30
|
+
type FramePayload,
|
|
31
|
+
RPC_FLIGHT,
|
|
32
|
+
type FrameRef,
|
|
33
|
+
RPC_FRAME,
|
|
34
|
+
type SlotRecord,
|
|
35
|
+
bindEntries,
|
|
36
|
+
frameAddress,
|
|
37
|
+
frameOpen,
|
|
38
|
+
isFrame,
|
|
39
|
+
regionMarkup,
|
|
40
|
+
slotClose,
|
|
41
|
+
slotOpen,
|
|
42
|
+
} from "./frames.ts";
|
|
43
|
+
|
|
44
|
+
/** A server component: what a frame-answering handler returns. */
|
|
45
|
+
type ServerComponent = (props: Record<string, unknown>) => unknown;
|
|
46
|
+
|
|
47
|
+
/** What a server function returns in place of its handler's component. */
|
|
48
|
+
export interface ServerFrame {
|
|
49
|
+
(props: Record<string, unknown>): unknown;
|
|
50
|
+
readonly [FRAME]: FrameRef & {
|
|
51
|
+
readonly args: unknown;
|
|
52
|
+
readonly component: ServerComponent;
|
|
53
|
+
readonly markup: (options: { readonly session?: symbol }) => Promise<string>;
|
|
54
|
+
/** Called through `live`: the page keeps it current over a connection. */
|
|
55
|
+
live?: boolean;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* How a frame crosses inside the page's own seed: by address, as a reference
|
|
61
|
+
* the browser resolves to the region already on the page. Registered when a
|
|
62
|
+
* frame is made and the registry does not hold it, so an application that has
|
|
63
|
+
* none sends the bytes it always did.
|
|
64
|
+
*/
|
|
65
|
+
const FRAME_ADAPTER = {
|
|
66
|
+
key: "barq.frame",
|
|
67
|
+
test: isFrame,
|
|
68
|
+
toSerializable: (frame: ServerFrame): FrameRef => ({
|
|
69
|
+
id: frame[FRAME].id,
|
|
70
|
+
address: frame[FRAME].address,
|
|
71
|
+
}),
|
|
72
|
+
fromSerializable: (ref: FrameRef): FrameRef => ref,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
function registerFrameAdapter(): void {
|
|
76
|
+
if (serializationAdapters().some((adapter) => adapter.key === FRAME_ADAPTER.key)) return;
|
|
77
|
+
registerSerializationAdapters([FRAME_ADAPTER]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The server function's component result, as a frame. */
|
|
81
|
+
export function serverFrame(id: string, data: unknown, component: ServerComponent): ServerFrame {
|
|
82
|
+
registerFrameAdapter();
|
|
83
|
+
// What the call was given, absent when it was given nothing: a function with
|
|
84
|
+
// no validator refuses a `data` it did not declare, `null` included.
|
|
85
|
+
const args = data === undefined ? undefined : encodeWire(data);
|
|
86
|
+
const address = frameAddress(id, encodeWire(data ?? null));
|
|
87
|
+
const state: ServerFrame[typeof FRAME] = {
|
|
88
|
+
id,
|
|
89
|
+
address,
|
|
90
|
+
args,
|
|
91
|
+
component,
|
|
92
|
+
markup: (options) => renderMarkup(state, options),
|
|
93
|
+
};
|
|
94
|
+
const render = (props: Record<string, unknown>): unknown =>
|
|
95
|
+
documentFrame(address, args, component, props, state.live === true);
|
|
96
|
+
return Object.assign(render, { [FRAME]: state });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A value read from a slot argument: a Cell is called, anything else is the value. */
|
|
100
|
+
function valueOf(argument: unknown): unknown {
|
|
101
|
+
return typeof argument === "function" ? untrack(argument as () => unknown) : argument;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** What a slot call returns that is never one of its properties: the markup's own names. */
|
|
105
|
+
const NOT_A_PROPERTY = new Set(["t", "then", "toString", "toJSON", "valueOf", "constructor"]);
|
|
106
|
+
|
|
107
|
+
type Record_ = { -readonly [K in keyof SlotRecord]: SlotRecord[K] };
|
|
108
|
+
|
|
109
|
+
/** The page's fill for one occurrence, run once, and the scope it ran under. */
|
|
110
|
+
interface Filled {
|
|
111
|
+
readonly result: unknown;
|
|
112
|
+
readonly scope: ReturnType<typeof childScope>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The props a server component is rendered with: every name is a slot.
|
|
117
|
+
*
|
|
118
|
+
* A slot call is placed as markup, `<props.comment … />`, or read as data,
|
|
119
|
+
* `props.row(args).done`, which makes it a binding slot. Which one is known
|
|
120
|
+
* only once the component uses what the call returned, so the call answers
|
|
121
|
+
* both: its markup when it is placed, a stand-in for each property read.
|
|
122
|
+
*
|
|
123
|
+
* `fills` are the page's own props, when the frame is written into the page:
|
|
124
|
+
* each slot then holds what the browser would put there, for the first paint,
|
|
125
|
+
* and a binding slot's stand-ins hold the fill's first values.
|
|
126
|
+
*/
|
|
127
|
+
class Slots {
|
|
128
|
+
private readonly records: (Record_ | null)[] = [];
|
|
129
|
+
readonly props: Record<string, unknown>;
|
|
130
|
+
|
|
131
|
+
constructor(private readonly fills: Record<string, unknown> | null) {
|
|
132
|
+
this.props = new Proxy(
|
|
133
|
+
{},
|
|
134
|
+
{ get: (_, name) => (typeof name === "string" ? this.slot(name) : undefined) },
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private slot(name: string): unknown {
|
|
139
|
+
if (name === "children") {
|
|
140
|
+
const at = this.reserve();
|
|
141
|
+
this.records[at] = { at, name, key: "" };
|
|
142
|
+
return html(this.markup(at, this.fill(name, at, {}, undefined)));
|
|
143
|
+
}
|
|
144
|
+
return (args?: Record<string, unknown>): unknown => this.call(name, args);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Taken before the region renders, so an occurrence inside it numbers after
|
|
149
|
+
* this one, in document order.
|
|
150
|
+
*/
|
|
151
|
+
private reserve(): number {
|
|
152
|
+
this.records.push(null);
|
|
153
|
+
return this.records.length - 1;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private call(name: string, args: Record<string, unknown> | undefined): unknown {
|
|
157
|
+
const at = this.reserve();
|
|
158
|
+
const values: Record<string, unknown> = {};
|
|
159
|
+
let explicit = "";
|
|
160
|
+
for (const [key, argument] of Object.entries(args ?? {})) {
|
|
161
|
+
if (key === "children") continue;
|
|
162
|
+
if (key === "$key") explicit = String(valueOf(argument));
|
|
163
|
+
else values[key] = valueOf(argument);
|
|
164
|
+
}
|
|
165
|
+
const children = args?.children;
|
|
166
|
+
const region = children === undefined ? undefined : esc(children);
|
|
167
|
+
const record: Record_ = {
|
|
168
|
+
at,
|
|
169
|
+
name,
|
|
170
|
+
key: explicit,
|
|
171
|
+
...(args === undefined ? {} : { args: encodeWire(values) }),
|
|
172
|
+
...(region === undefined ? {} : { region, hasRegion: true }),
|
|
173
|
+
};
|
|
174
|
+
this.records[at] = record;
|
|
175
|
+
const filled = this.fill(name, at, values, region);
|
|
176
|
+
let markup: string | null = null;
|
|
177
|
+
const placed = (): string => {
|
|
178
|
+
if (record.bind === true) throw readTwice(name);
|
|
179
|
+
markup ??= this.markup(at, filled);
|
|
180
|
+
return markup;
|
|
181
|
+
};
|
|
182
|
+
const standIns = new Map<string, SlotValue>();
|
|
183
|
+
const read = (key: string): SlotValue => {
|
|
184
|
+
if (markup !== null) throw readTwice(name);
|
|
185
|
+
record.bind = true;
|
|
186
|
+
let standIn = standIns.get(key);
|
|
187
|
+
if (standIn === undefined) {
|
|
188
|
+
const result = filled?.result;
|
|
189
|
+
const known = typeof result === "object" && result !== null;
|
|
190
|
+
const value = known
|
|
191
|
+
? runWithOwner(filled!.scope, () =>
|
|
192
|
+
untrack(() => (result as Record<string, unknown>)[key]),
|
|
193
|
+
)
|
|
194
|
+
: undefined;
|
|
195
|
+
standIn = new SlotValue(at, key, this.fills !== null && known, value);
|
|
196
|
+
standIns.set(key, standIn);
|
|
197
|
+
}
|
|
198
|
+
return standIn;
|
|
199
|
+
};
|
|
200
|
+
return new Proxy(
|
|
201
|
+
{},
|
|
202
|
+
{
|
|
203
|
+
get: (_, key) => {
|
|
204
|
+
if (key === SSR_HTML_BRAND) return record.bind !== true;
|
|
205
|
+
if (key === "t") return placed();
|
|
206
|
+
if (key === "toString") return placed;
|
|
207
|
+
if (typeof key === "symbol" || NOT_A_PROPERTY.has(key)) return undefined;
|
|
208
|
+
return read(key);
|
|
209
|
+
},
|
|
210
|
+
ownKeys: () => {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`[barq] A server component spread what \`props.${name}()\` returned. A binding slot's properties are placed one position at a time, ` +
|
|
213
|
+
"so the markup shows what the browser fills: name each one, as in `<li hidden={row.hidden}>`.",
|
|
214
|
+
);
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private markup(at: number, filled: Filled | null): string {
|
|
221
|
+
const content = filled === null ? "" : runWithOwner(filled.scope, () => esc(filled.result));
|
|
222
|
+
return `<!--${slotOpen(at)}-->${content}<!--${slotClose(at)}-->`;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The page's fill for a slot, run once for the first paint: its content for
|
|
227
|
+
* a slot that is placed, its values for one read as data. Nothing over the
|
|
228
|
+
* wire, where the browser fills every slot itself.
|
|
229
|
+
*/
|
|
230
|
+
private fill(
|
|
231
|
+
name: string,
|
|
232
|
+
at: number,
|
|
233
|
+
values: Record<string, unknown>,
|
|
234
|
+
region: string | undefined,
|
|
235
|
+
): Filled | null {
|
|
236
|
+
if (this.fills === null) return null;
|
|
237
|
+
const given = this.fills[name];
|
|
238
|
+
// The page's own content, which the browser claims, under a scope that
|
|
239
|
+
// hydrates again inside the server component's.
|
|
240
|
+
const scope = childScope(getOwner(), "branch");
|
|
241
|
+
setHydratable(scope, true);
|
|
242
|
+
setServerComponent(scope, false);
|
|
243
|
+
if (name === "children") return { result: given, scope };
|
|
244
|
+
const props: Record<string, unknown> = {};
|
|
245
|
+
for (const [key, value] of Object.entries(values)) props[key] = (): unknown => value;
|
|
246
|
+
if (region !== undefined) props.children = block(() => html(regionMarkup(String(at), region)));
|
|
247
|
+
// A fill that takes nothing reaches the props as the Cell it is, so reading
|
|
248
|
+
// the prop is calling it.
|
|
249
|
+
const result = runWithOwner(scope, () => {
|
|
250
|
+
const fill = valueOf(given);
|
|
251
|
+
return typeof fill === "function" ? (fill as ServerComponent)(props) : fill;
|
|
252
|
+
});
|
|
253
|
+
return result === undefined ? null : { result, scope };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The records the markup still carries, keyed: a boundary resumed during the
|
|
258
|
+
* render built its slots again, so only the occurrences in the final markup
|
|
259
|
+
* count, and a positional key is taken in that markup's order. `inline`
|
|
260
|
+
* drops each region, which the page already carries inside the slot.
|
|
261
|
+
*/
|
|
262
|
+
final(markup: string, inline: boolean): SlotRecord[] {
|
|
263
|
+
const present = new Set<number>();
|
|
264
|
+
for (const found of markup.matchAll(/<!--s:(\d+)-->/g)) present.add(Number(found[1]));
|
|
265
|
+
for (const found of markup.matchAll(/ _b-[^\s=>]+="([^"]*)"/g)) {
|
|
266
|
+
for (const entry of bindEntries(found[1])) present.add(entry.at);
|
|
267
|
+
}
|
|
268
|
+
for (const found of markup.matchAll(/<!--_b:(\d+):/g)) present.add(Number(found[1]));
|
|
269
|
+
const ordinal = new Map<string, number>();
|
|
270
|
+
const out: SlotRecord[] = [];
|
|
271
|
+
for (const record of this.records) {
|
|
272
|
+
if (record === null || !present.has(record.at)) continue;
|
|
273
|
+
const count = ordinal.get(record.name) ?? 0;
|
|
274
|
+
ordinal.set(record.name, count + 1);
|
|
275
|
+
out.push({
|
|
276
|
+
at: record.at,
|
|
277
|
+
name: record.name,
|
|
278
|
+
key: record.key === "" ? `${record.name}:${count}` : `$${record.key}`,
|
|
279
|
+
...(record.args === undefined ? {} : { args: record.args }),
|
|
280
|
+
...(record.bind === true
|
|
281
|
+
? { bind: true }
|
|
282
|
+
: {
|
|
283
|
+
...(record.hasRegion === true ? { hasRegion: true } : {}),
|
|
284
|
+
...(inline || record.region === undefined ? {} : { region: record.region }),
|
|
285
|
+
}),
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function readTwice(name: string): Error {
|
|
293
|
+
return new Error(
|
|
294
|
+
`[barq] A server component both placed \`props.${name}()\` as markup and read it as data. ` +
|
|
295
|
+
"A slot call is one or the other: place it, `<props." +
|
|
296
|
+
name +
|
|
297
|
+
" />`, for the browser's content, or read its properties, for a binding slot.",
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Render a server component: what renders under it may read binding slots. */
|
|
302
|
+
function asServerComponent<T>(render: () => T): T {
|
|
303
|
+
const scope = childScope(getOwner(), "branch");
|
|
304
|
+
setServerComponent(scope, true);
|
|
305
|
+
return runWithOwner(scope, render);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The frame written into the page, where a route renders it.
|
|
310
|
+
*
|
|
311
|
+
* What the component declares for the head goes to the page's head as any
|
|
312
|
+
* component's does, and is recorded too, so the browser takes those tags over
|
|
313
|
+
* with the frame and drops them with it.
|
|
314
|
+
*/
|
|
315
|
+
function documentFrame(
|
|
316
|
+
address: string,
|
|
317
|
+
args: unknown,
|
|
318
|
+
component: ServerComponent,
|
|
319
|
+
fills: Record<string, unknown>,
|
|
320
|
+
live: boolean,
|
|
321
|
+
): unknown {
|
|
322
|
+
const slots = new Slots(fills);
|
|
323
|
+
const head: HeadTag[] = [];
|
|
324
|
+
let outer: ReturnType<typeof setHeadSink> = null;
|
|
325
|
+
outer = setHeadSink({
|
|
326
|
+
add: (tags) => {
|
|
327
|
+
head.push(...tags);
|
|
328
|
+
outer?.add(tags);
|
|
329
|
+
},
|
|
330
|
+
module: (url) => outer?.module?.(url),
|
|
331
|
+
});
|
|
332
|
+
// Nothing the server component itself reads reaches the page's seed: the
|
|
333
|
+
// browser never runs it, so a value seeded for it is one nothing claims.
|
|
334
|
+
const scope = childScope(getOwner(), "branch");
|
|
335
|
+
setHydratable(scope, false);
|
|
336
|
+
setServerComponent(scope, true);
|
|
337
|
+
let region: string;
|
|
338
|
+
try {
|
|
339
|
+
region = runWithOwner(scope, () => esc(component(slots.props)));
|
|
340
|
+
} finally {
|
|
341
|
+
setHeadSink(outer);
|
|
342
|
+
}
|
|
343
|
+
const payload: FramePayload = {
|
|
344
|
+
v: FRAME_VERSION,
|
|
345
|
+
args,
|
|
346
|
+
...(live ? { live: true } : {}),
|
|
347
|
+
slots: slots.final(region, true),
|
|
348
|
+
head,
|
|
349
|
+
};
|
|
350
|
+
return html(frameOpen(address, payload) + region + FRAME_CLOSE);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* The answer to a call from the browser: the region rendered on its own, its
|
|
355
|
+
* slots empty. Its head carries what the region declared, the stylesheets and
|
|
356
|
+
* chunks its `lazy()` modules need, and the rules `@barqjs/css` registered
|
|
357
|
+
* while it rendered.
|
|
358
|
+
*/
|
|
359
|
+
export async function frameResponse(frame: ServerFrame): Promise<Response> {
|
|
360
|
+
const answer = new Response(await frameMarkup(frame));
|
|
361
|
+
answer.headers.set("content-type", "text/html; charset=utf-8");
|
|
362
|
+
answer.headers.set("cache-control", "no-store");
|
|
363
|
+
answer.headers.set("x-barq-rpc", RPC_FRAME);
|
|
364
|
+
return answer;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* A frame's markup over the wire: the region rendered on its own, its slots
|
|
369
|
+
* empty. Rendered by the frame itself, so by the copy of this module that made
|
|
370
|
+
* it: a host serving a built server through its own `serveBarq` answers the
|
|
371
|
+
* call from another copy, whose renderer is not the one the component was
|
|
372
|
+
* compiled against.
|
|
373
|
+
*/
|
|
374
|
+
export function frameMarkup(
|
|
375
|
+
frame: ServerFrame,
|
|
376
|
+
options: { readonly session?: symbol } = {},
|
|
377
|
+
): Promise<string> {
|
|
378
|
+
return frame[FRAME].markup(options);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async function renderMarkup(
|
|
382
|
+
frame: ServerFrame[typeof FRAME],
|
|
383
|
+
options: { readonly session?: symbol },
|
|
384
|
+
): Promise<string> {
|
|
385
|
+
const { address, args, component } = frame;
|
|
386
|
+
const slots = new Slots(null);
|
|
387
|
+
const rendered = await renderFrame(
|
|
388
|
+
() => asServerComponent(() => component(slots.props)) as never,
|
|
389
|
+
{
|
|
390
|
+
renderId: `f${address.slice(address.lastIndexOf("#") + 1)}-`,
|
|
391
|
+
session: options.session,
|
|
392
|
+
},
|
|
393
|
+
);
|
|
394
|
+
const head = [...rendered.head];
|
|
395
|
+
const css = collectRequestCss();
|
|
396
|
+
if (css !== "") head.push({ tag: "style", props: { "data-frame-css": address }, children: css });
|
|
397
|
+
const payload: FramePayload = {
|
|
398
|
+
v: FRAME_VERSION,
|
|
399
|
+
args,
|
|
400
|
+
...(frame.live === true ? { live: true } : {}),
|
|
401
|
+
slots: slots.final(rendered.html, false),
|
|
402
|
+
head,
|
|
403
|
+
};
|
|
404
|
+
return frameOpen(address, payload) + rendered.html + FRAME_CLOSE;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** A frame the page says it has mounted, from the request's `x-barq-frames`. */
|
|
408
|
+
export interface MountedFrame {
|
|
409
|
+
readonly id: string;
|
|
410
|
+
readonly args: unknown;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* The frames a request says its page has mounted. Untrusted input like any
|
|
415
|
+
* other: each one is asked for through its own function, validator and
|
|
416
|
+
* middleware included, and a malformed list is no list.
|
|
417
|
+
*/
|
|
418
|
+
export function mountedFrames(header: string | null): MountedFrame[] {
|
|
419
|
+
if (header === null || header === "") return [];
|
|
420
|
+
let parsed: unknown;
|
|
421
|
+
try {
|
|
422
|
+
parsed = JSON.parse(header);
|
|
423
|
+
} catch {
|
|
424
|
+
return [];
|
|
425
|
+
}
|
|
426
|
+
if (!Array.isArray(parsed)) return [];
|
|
427
|
+
const out: MountedFrame[] = [];
|
|
428
|
+
for (const entry of parsed.slice(0, FRAMES_BUDGET)) {
|
|
429
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
430
|
+
const { id, args } = entry as { id?: unknown; args?: unknown };
|
|
431
|
+
if (typeof id === "string") out.push({ id, args });
|
|
432
|
+
}
|
|
433
|
+
return out;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* A mutation's answer with the regions it changed: its value in a script, then
|
|
438
|
+
* each changed frame's markup, as the page applies them. `ask` calls a mounted
|
|
439
|
+
* frame's server function again with the arguments the page sent.
|
|
440
|
+
*/
|
|
441
|
+
export async function flightResponse(
|
|
442
|
+
value: unknown,
|
|
443
|
+
mounted: readonly MountedFrame[],
|
|
444
|
+
invalidated: ReadonlySet<string>,
|
|
445
|
+
ask: (frame: MountedFrame) => Promise<unknown>,
|
|
446
|
+
): Promise<Response> {
|
|
447
|
+
const seen = new Set<string>();
|
|
448
|
+
let regions = "";
|
|
449
|
+
for (const frame of mounted) {
|
|
450
|
+
if (!invalidated.has(frame.id)) continue;
|
|
451
|
+
const answered = await ask(frame);
|
|
452
|
+
if (!isFrame(answered)) continue;
|
|
453
|
+
const { address } = (answered as ServerFrame)[FRAME];
|
|
454
|
+
if (seen.has(address)) continue;
|
|
455
|
+
seen.add(address);
|
|
456
|
+
regions += await frameMarkup(answered as ServerFrame);
|
|
457
|
+
}
|
|
458
|
+
const script = `<script type="${FLIGHT_TYPE}">${JSON.stringify(encodeWire(value)).replaceAll("<", "\\u003c")}</script>`;
|
|
459
|
+
const answer = new Response(script + regions);
|
|
460
|
+
answer.headers.set("content-type", "text/html; charset=utf-8");
|
|
461
|
+
answer.headers.set("cache-control", "no-store");
|
|
462
|
+
answer.headers.set("x-barq-rpc", RPC_FLIGHT);
|
|
463
|
+
return answer;
|
|
464
|
+
}
|