@barqjs/start 0.2.0-next.0 → 0.2.0-next.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 +26 -0
- package/dist/{client-BHyIiYxt.js → client-BsyxVCyu.js} +49 -22
- package/dist/client-BsyxVCyu.js.map +1 -0
- package/dist/{client-DHCV7yvQ.d.ts → client-DidH0F2e.d.ts} +28 -18
- package/dist/client-DidH0F2e.d.ts.map +1 -0
- package/dist/client.d.ts +25 -3
- package/dist/client.js +3 -3
- package/dist/{env-qA8EyOr1.d.ts → env-Bhk_aIkx.d.ts} +1 -3
- package/dist/env-Bhk_aIkx.d.ts.map +1 -0
- package/dist/env.d.ts +1 -1
- package/dist/env.js +1 -2
- package/dist/{index-Bm_-MTj6.d.ts → index-B4o7eyCr.d.ts} +32 -22
- package/dist/index-B4o7eyCr.d.ts.map +1 -0
- package/dist/index.d.ts +18 -5
- package/dist/index.js +4 -4
- package/dist/{middleware-BdVlE8qm.d.ts → middleware-B-tI8QHQ.d.ts} +1 -3
- package/dist/middleware-B-tI8QHQ.d.ts.map +1 -0
- package/dist/middleware.d.ts +1 -1
- package/dist/middleware.js +1 -2
- package/dist/middleware.js.map +1 -1
- package/dist/prerender.d.ts +22 -2
- package/dist/prerender.d.ts.map +1 -1
- package/dist/prerender.js +4 -4
- package/dist/prerender.js.map +1 -1
- package/dist/protocol.d.ts +0 -2
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +1 -2
- package/dist/serve.d.ts +14 -5
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +3 -5
- package/dist/serve.js.map +1 -1
- package/dist/{server-BV3xKmLa.js → server-DG6jb_NA.js} +162 -57
- package/dist/server-DG6jb_NA.js.map +1 -0
- package/dist/server.d.ts +21 -4
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +1 -1
- package/dist/{src-ChnB_xug.js → src-odXG7kLA.js} +31 -20
- package/dist/src-odXG7kLA.js.map +1 -0
- package/dist/{static-fn-DgjNArBe.js → static-fn-CXjJi-It.js} +10 -11
- package/dist/{static-fn-DgjNArBe.js.map → static-fn-CXjJi-It.js.map} +1 -1
- package/package.json +11 -11
- package/src/client.ts +27 -1
- package/src/context.ts +33 -0
- package/src/fn-error.ts +25 -1
- package/src/frames-client.ts +5 -1
- package/src/index.ts +32 -6
- package/src/live-server.ts +154 -44
- package/src/server.ts +43 -7
- package/src/wire-types.ts +2 -3
- package/dist/client-BHyIiYxt.js.map +0 -1
- package/dist/client-DHCV7yvQ.d.ts.map +0 -1
- package/dist/env-qA8EyOr1.d.ts.map +0 -1
- package/dist/index-Bm_-MTj6.d.ts.map +0 -1
- package/dist/middleware-BdVlE8qm.d.ts.map +0 -1
- package/dist/server-BV3xKmLa.js.map +0 -1
- package/dist/src-ChnB_xug.js.map +0 -1
package/src/index.ts
CHANGED
|
@@ -21,6 +21,8 @@ export {
|
|
|
21
21
|
clearResponseHeaders,
|
|
22
22
|
collectRequestCss,
|
|
23
23
|
createResponseDraft,
|
|
24
|
+
devStylesheets,
|
|
25
|
+
type DevStylesheets,
|
|
24
26
|
deleteCookie,
|
|
25
27
|
draftedStatus,
|
|
26
28
|
getCookie,
|
|
@@ -107,6 +109,7 @@ export {
|
|
|
107
109
|
type ServerFnMeta,
|
|
108
110
|
type ServerFnMethod,
|
|
109
111
|
type Streamed,
|
|
112
|
+
BUILD_HEADER,
|
|
110
113
|
DATA_SUFFIX,
|
|
111
114
|
LIVE_SUFFIX,
|
|
112
115
|
NOT_FOUND,
|
|
@@ -122,6 +125,7 @@ export {
|
|
|
122
125
|
SERVER_FN,
|
|
123
126
|
STATIC_PREFIX,
|
|
124
127
|
ServerFnError,
|
|
128
|
+
UNKNOWN_HEADER,
|
|
125
129
|
clientRpc,
|
|
126
130
|
isServerFn,
|
|
127
131
|
staticFunctionMiddleware,
|
|
@@ -325,6 +329,7 @@ interface Staged {
|
|
|
325
329
|
readonly validator: Validator<unknown> | null;
|
|
326
330
|
readonly middleware: readonly Middleware[];
|
|
327
331
|
readonly method: ServerFnMethod;
|
|
332
|
+
readonly broadcast: boolean;
|
|
328
333
|
}
|
|
329
334
|
|
|
330
335
|
/**
|
|
@@ -488,6 +493,15 @@ export interface ServerFnOptions<
|
|
|
488
493
|
* `createSerializationAdapter` carries.
|
|
489
494
|
*/
|
|
490
495
|
readonly strict?: Strict;
|
|
496
|
+
/**
|
|
497
|
+
* The answer is the same for every caller with the same arguments, so a
|
|
498
|
+
* `live` call shares one rendering with every other connection asking the
|
|
499
|
+
* same thing: Solid 2.0's `broadcast`. The handler still runs per call, as the
|
|
500
|
+
* gate it is (an auth check, a redirect), and a joiner's own answer is closed
|
|
501
|
+
* unread. The shared rendering belongs to no request: it reads no cookie,
|
|
502
|
+
* header or session, and it stops when its last connection goes.
|
|
503
|
+
*/
|
|
504
|
+
readonly broadcast?: boolean;
|
|
491
505
|
}
|
|
492
506
|
|
|
493
507
|
/**
|
|
@@ -508,7 +522,12 @@ export function createServerFn<
|
|
|
508
522
|
"Use a route's `server.handlers` for any other method.",
|
|
509
523
|
);
|
|
510
524
|
}
|
|
511
|
-
return builderAt<FreshServerFn<Method, Strict>>({
|
|
525
|
+
return builderAt<FreshServerFn<Method, Strict>>({
|
|
526
|
+
validator: null,
|
|
527
|
+
middleware: [],
|
|
528
|
+
method,
|
|
529
|
+
broadcast: options.broadcast === true,
|
|
530
|
+
});
|
|
512
531
|
}
|
|
513
532
|
|
|
514
533
|
/**
|
|
@@ -554,11 +573,18 @@ function builderAt<State extends ServerFnState>(staged: Staged): ServerFnBuilder
|
|
|
554
573
|
HandlerData<State>,
|
|
555
574
|
HandlerContextOf<State>,
|
|
556
575
|
State["method"]
|
|
557
|
-
>(
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
576
|
+
>(
|
|
577
|
+
{
|
|
578
|
+
id: "",
|
|
579
|
+
...(staged.method === "GET" ? { method: "GET" as const } : {}),
|
|
580
|
+
...(staged.broadcast ? { broadcast: true as const } : {}),
|
|
581
|
+
},
|
|
582
|
+
{
|
|
583
|
+
validator: staged.validator,
|
|
584
|
+
middleware: staged.middleware,
|
|
585
|
+
handler: fn,
|
|
586
|
+
},
|
|
587
|
+
),
|
|
562
588
|
};
|
|
563
589
|
}
|
|
564
590
|
|
package/src/live-server.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
import { SharedSources, shareSources, unshareSources } from "@barqjs/core/internal";
|
|
17
17
|
import { encodeWire } from "@barqjs/server/codec";
|
|
18
|
+
import { withoutRequestEvent } from "@barqjs/server/storage";
|
|
18
19
|
|
|
19
20
|
import { type ServerFrame, frameMarkup } from "./frames-server.ts";
|
|
20
21
|
import { isFrame } from "./frames.ts";
|
|
@@ -50,54 +51,21 @@ function event(kind: "frame" | "message", id: string, data: string): string {
|
|
|
50
51
|
return `${kind === "frame" ? "event: frame\n" : ""}id: ${id}\ndata: ${data}\n\n`;
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
type Emit = (data: string, kind: "frame" | "message") => void;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Drive a live answer: each rendering of a frame, or each value of an
|
|
58
|
+
* iterable, through `emit` until it ends, then `finish(complete)`. Returns
|
|
59
|
+
* what stops it early.
|
|
60
|
+
*/
|
|
61
|
+
function produce(result: unknown, emit: Emit, finish: (complete: boolean) => void): () => void {
|
|
58
62
|
let ended = false;
|
|
59
63
|
const stops: (() => void)[] = [];
|
|
60
64
|
const end = (complete: boolean): void => {
|
|
61
65
|
if (ended) return;
|
|
62
66
|
ended = true;
|
|
63
67
|
for (const stop of stops) stop();
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
if (complete) controller.enqueue(encoder.encode("event: complete\ndata: \n\n"));
|
|
67
|
-
controller.close();
|
|
68
|
-
} catch {
|
|
69
|
-
// The reader has gone.
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
const send = (text: string): void => {
|
|
73
|
-
if (ended || controller === null) return;
|
|
74
|
-
try {
|
|
75
|
-
controller.enqueue(encoder.encode(text));
|
|
76
|
-
} catch {
|
|
77
|
-
end(false);
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
let sentId = last;
|
|
81
|
-
const emit = (data: string, kind: "frame" | "message" = "message"): void => {
|
|
82
|
-
const id = digest(data);
|
|
83
|
-
if (id === sentId) return;
|
|
84
|
-
sentId = id;
|
|
85
|
-
send(event(kind, id, data));
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const start = (): void => {
|
|
89
|
-
const heartbeat = setInterval(() => send(": still here\n\n"), HEARTBEAT_MS);
|
|
90
|
-
(heartbeat as { unref?: () => void }).unref?.();
|
|
91
|
-
stops.push(() => clearInterval(heartbeat));
|
|
92
|
-
const abort = (): void => end(false);
|
|
93
|
-
request.signal.addEventListener("abort", abort, { once: true });
|
|
94
|
-
stops.push(() => request.signal.removeEventListener("abort", abort));
|
|
95
|
-
if (isFrame(result)) void frames(result as ServerFrame);
|
|
96
|
-
else if (isIterable(result)) void values(result);
|
|
97
|
-
else {
|
|
98
|
-
emit(JSON.stringify(encodeWire(result)));
|
|
99
|
-
end(true);
|
|
100
|
-
}
|
|
68
|
+
finish(complete);
|
|
101
69
|
};
|
|
102
70
|
|
|
103
71
|
const frames = async (frame: ServerFrame): Promise<void> => {
|
|
@@ -152,7 +120,7 @@ export function liveResponse(result: unknown, request: Request): Response {
|
|
|
152
120
|
const step = await iterator.next();
|
|
153
121
|
if (ended) return;
|
|
154
122
|
if (step.done === true) break;
|
|
155
|
-
emit(JSON.stringify(encodeWire(step.value)));
|
|
123
|
+
emit(JSON.stringify(encodeWire(step.value)), "message");
|
|
156
124
|
}
|
|
157
125
|
end(true);
|
|
158
126
|
} catch (error) {
|
|
@@ -164,10 +132,71 @@ export function liveResponse(result: unknown, request: Request): Response {
|
|
|
164
132
|
}
|
|
165
133
|
};
|
|
166
134
|
|
|
135
|
+
if (isFrame(result)) void frames(result as ServerFrame);
|
|
136
|
+
else if (isIterable(result)) void values(result);
|
|
137
|
+
else {
|
|
138
|
+
emit(JSON.stringify(encodeWire(result)), "message");
|
|
139
|
+
end(true);
|
|
140
|
+
}
|
|
141
|
+
return () => {
|
|
142
|
+
if (ended) return;
|
|
143
|
+
ended = true;
|
|
144
|
+
for (const stop of stops) stop();
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface Connection {
|
|
149
|
+
readonly emit: Emit;
|
|
150
|
+
readonly end: (complete: boolean) => void;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* One server-sent event stream. `attach` is handed the connection when the
|
|
155
|
+
* stream starts and answers with what to run when it ends, either way.
|
|
156
|
+
*/
|
|
157
|
+
function connection(request: Request, attach: (connection: Connection) => () => void): Response {
|
|
158
|
+
const encoder = new TextEncoder();
|
|
159
|
+
let controller: ReadableStreamDefaultController<Uint8Array> | null = null;
|
|
160
|
+
let ended = false;
|
|
161
|
+
const stops: (() => void)[] = [];
|
|
162
|
+
const end = (complete: boolean): void => {
|
|
163
|
+
if (ended) return;
|
|
164
|
+
ended = true;
|
|
165
|
+
for (const stop of stops) stop();
|
|
166
|
+
if (controller === null) return;
|
|
167
|
+
try {
|
|
168
|
+
if (complete) controller.enqueue(encoder.encode("event: complete\ndata: \n\n"));
|
|
169
|
+
controller.close();
|
|
170
|
+
} catch {
|
|
171
|
+
// The reader has gone.
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
const send = (text: string): void => {
|
|
175
|
+
if (ended || controller === null) return;
|
|
176
|
+
try {
|
|
177
|
+
controller.enqueue(encoder.encode(text));
|
|
178
|
+
} catch {
|
|
179
|
+
end(false);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
let sentId = request.headers.get("last-event-id");
|
|
183
|
+
const emit: Emit = (data, kind) => {
|
|
184
|
+
const id = digest(data);
|
|
185
|
+
if (id === sentId) return;
|
|
186
|
+
sentId = id;
|
|
187
|
+
send(event(kind, id, data));
|
|
188
|
+
};
|
|
189
|
+
|
|
167
190
|
const body = new ReadableStream<Uint8Array>({
|
|
168
191
|
start(given) {
|
|
169
192
|
controller = given;
|
|
170
|
-
|
|
193
|
+
const heartbeat = setInterval(() => send(": still here\n\n"), HEARTBEAT_MS);
|
|
194
|
+
(heartbeat as { unref?: () => void }).unref?.();
|
|
195
|
+
stops.push(() => clearInterval(heartbeat));
|
|
196
|
+
const abort = (): void => end(false);
|
|
197
|
+
request.signal.addEventListener("abort", abort, { once: true });
|
|
198
|
+
stops.push(() => request.signal.removeEventListener("abort", abort));
|
|
199
|
+
stops.push(attach({ emit, end }));
|
|
171
200
|
},
|
|
172
201
|
cancel() {
|
|
173
202
|
end(false);
|
|
@@ -180,3 +209,84 @@ export function liveResponse(result: unknown, request: Request): Response {
|
|
|
180
209
|
answer.headers.set("x-barq-rpc", RPC_LIVE);
|
|
181
210
|
return answer;
|
|
182
211
|
}
|
|
212
|
+
|
|
213
|
+
/** The answer to a call at the live address. */
|
|
214
|
+
export function liveResponse(result: unknown, request: Request): Response {
|
|
215
|
+
return connection(request, (to) => produce(result, to.emit, to.end));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
interface Broadcast {
|
|
219
|
+
readonly connections: Set<Connection>;
|
|
220
|
+
latest: { readonly data: string; readonly kind: "frame" | "message" } | null;
|
|
221
|
+
ended: boolean;
|
|
222
|
+
stop: () => void;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** The running shared answers, by function and arguments. */
|
|
226
|
+
const BROADCASTS = new Map<string, Broadcast>();
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* A joiner's own answer, closed without reading it. Only an answer that is
|
|
230
|
+
* already an iterator, a generator object, holds anything: an iterable nobody
|
|
231
|
+
* has asked for an iterator has started nothing, and asking would start it.
|
|
232
|
+
*/
|
|
233
|
+
function closeUnread(result: unknown): void {
|
|
234
|
+
const iterator = result as { return?: () => unknown } | null;
|
|
235
|
+
if (typeof iterator?.return !== "function") return;
|
|
236
|
+
try {
|
|
237
|
+
const closing = iterator.return();
|
|
238
|
+
if (closing instanceof Promise) void closing.catch(() => {});
|
|
239
|
+
} catch {
|
|
240
|
+
// Nothing read it, and nothing will.
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The answer to a live call to a `broadcast` function: one rendering per
|
|
246
|
+
* `key`, every connection asking the same thing a reader of it. The first call
|
|
247
|
+
* starts it, outside its own request, and each later one joins it with the
|
|
248
|
+
* latest event and closes its own answer unread. It stops when its last
|
|
249
|
+
* connection goes.
|
|
250
|
+
*/
|
|
251
|
+
export function broadcastResponse(key: string, result: unknown, request: Request): Response {
|
|
252
|
+
let shared = BROADCASTS.get(key);
|
|
253
|
+
if (shared !== undefined && !shared.ended) {
|
|
254
|
+
closeUnread(result);
|
|
255
|
+
} else {
|
|
256
|
+
const started: Broadcast = {
|
|
257
|
+
connections: new Set(),
|
|
258
|
+
latest: null,
|
|
259
|
+
ended: false,
|
|
260
|
+
stop: () => {},
|
|
261
|
+
};
|
|
262
|
+
BROADCASTS.set(key, started);
|
|
263
|
+
started.stop = withoutRequestEvent(() =>
|
|
264
|
+
produce(
|
|
265
|
+
result,
|
|
266
|
+
(data, kind) => {
|
|
267
|
+
started.latest = { data, kind };
|
|
268
|
+
for (const each of started.connections) each.emit(data, kind);
|
|
269
|
+
},
|
|
270
|
+
(complete) => {
|
|
271
|
+
started.ended = true;
|
|
272
|
+
if (BROADCASTS.get(key) === started) BROADCASTS.delete(key);
|
|
273
|
+
for (const each of started.connections) each.end(complete);
|
|
274
|
+
},
|
|
275
|
+
),
|
|
276
|
+
);
|
|
277
|
+
shared = started;
|
|
278
|
+
}
|
|
279
|
+
const joined = shared;
|
|
280
|
+
return connection(request, (to) => {
|
|
281
|
+
joined.connections.add(to);
|
|
282
|
+
if (joined.latest !== null) to.emit(joined.latest.data, joined.latest.kind);
|
|
283
|
+
if (joined.ended) to.end(true);
|
|
284
|
+
return () => {
|
|
285
|
+
joined.connections.delete(to);
|
|
286
|
+
if (joined.connections.size > 0 || joined.ended) return;
|
|
287
|
+
joined.ended = true;
|
|
288
|
+
if (BROADCASTS.get(key) === joined) BROADCASTS.delete(key);
|
|
289
|
+
joined.stop();
|
|
290
|
+
};
|
|
291
|
+
});
|
|
292
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -29,6 +29,8 @@ import {
|
|
|
29
29
|
setRequestMiddlewareContext,
|
|
30
30
|
} from "./context.ts";
|
|
31
31
|
import {
|
|
32
|
+
BUILD_HEADER,
|
|
33
|
+
BUNDLED_BUILD,
|
|
32
34
|
NOT_FOUND,
|
|
33
35
|
PAYLOAD_PARAM,
|
|
34
36
|
REDIRECT,
|
|
@@ -43,6 +45,7 @@ import {
|
|
|
43
45
|
type ServerFnInputAnswer,
|
|
44
46
|
type ServerFnIssue,
|
|
45
47
|
type ServerFnThrown,
|
|
48
|
+
UNKNOWN_HEADER,
|
|
46
49
|
} from "./client.ts";
|
|
47
50
|
import {
|
|
48
51
|
type AnyServerFn,
|
|
@@ -66,7 +69,7 @@ import {
|
|
|
66
69
|
serverFrame,
|
|
67
70
|
} from "./frames-server.ts";
|
|
68
71
|
import { FRAMES_HEADER, installFrames, isFrame } from "./frames.ts";
|
|
69
|
-
import { liveResponse } from "./live-server.ts";
|
|
72
|
+
import { broadcastResponse, liveResponse } from "./live-server.ts";
|
|
70
73
|
|
|
71
74
|
installFrames(serverFrame as never);
|
|
72
75
|
|
|
@@ -217,6 +220,16 @@ export interface HandlerOptions {
|
|
|
217
220
|
* browser.
|
|
218
221
|
*/
|
|
219
222
|
revealError?: (error: unknown) => ServerFnThrown;
|
|
223
|
+
/**
|
|
224
|
+
* The build this server is, such as a commit hash. A call whose
|
|
225
|
+
* `x-barq-build` header names another build is answered `409` and its
|
|
226
|
+
* function does not run, which catches the skew an unknown id cannot: a
|
|
227
|
+
* function whose id survived a deploy while its arguments or result changed
|
|
228
|
+
* shape. The build `@barqjs/vite` stamped the bundle with when omitted, so
|
|
229
|
+
* a built application needs nothing here; a call with no header is not
|
|
230
|
+
* checked.
|
|
231
|
+
*/
|
|
232
|
+
buildId?: string;
|
|
220
233
|
/**
|
|
221
234
|
* Whether some route in this application can reach this id.
|
|
222
235
|
*
|
|
@@ -306,6 +319,11 @@ export function crossOriginRefused(request: Request, allowedOrigins?: readonly s
|
|
|
306
319
|
return site !== "same-origin" && site !== "none";
|
|
307
320
|
}
|
|
308
321
|
|
|
322
|
+
/** The labelled 404: the client reports it as skew rather than as a failed call. */
|
|
323
|
+
function unknownFunction(): Response {
|
|
324
|
+
return new Response("not found", { status: 404, headers: { [UNKNOWN_HEADER]: "true" } });
|
|
325
|
+
}
|
|
326
|
+
|
|
309
327
|
/**
|
|
310
328
|
* Handle one server-function request, or return null if the URL is not one.
|
|
311
329
|
*
|
|
@@ -355,10 +373,18 @@ export async function handleServerFn(
|
|
|
355
373
|
await MOUNTED.manifest();
|
|
356
374
|
fn = REGISTRY.get(id);
|
|
357
375
|
}
|
|
358
|
-
if (fn === undefined) return
|
|
376
|
+
if (fn === undefined) return unknownFunction();
|
|
359
377
|
// Same answer as an unknown id, so a caller cannot tell a function that
|
|
360
378
|
// exists but is unrouted from one that does not exist.
|
|
361
|
-
if (options?.reachable?.(id) === false) return
|
|
379
|
+
if (options?.reachable?.(id) === false) return unknownFunction();
|
|
380
|
+
const build = options?.buildId ?? BUNDLED_BUILD;
|
|
381
|
+
const callerBuild = request.headers.get(BUILD_HEADER);
|
|
382
|
+
if (build !== undefined && callerBuild !== null && callerBuild !== build) {
|
|
383
|
+
return new Response(`call from build ${JSON.stringify(callerBuild)} refused`, {
|
|
384
|
+
status: 409,
|
|
385
|
+
headers: { [UNKNOWN_HEADER]: "true", [BUILD_HEADER]: build, "cache-control": "no-store" },
|
|
386
|
+
});
|
|
387
|
+
}
|
|
362
388
|
// The function's own method, and only on the channel that can carry it: a
|
|
363
389
|
// form is a POST, so a GET function has the data channel alone.
|
|
364
390
|
const expected = fn.meta.method ?? "POST";
|
|
@@ -384,11 +410,15 @@ export async function handleServerFn(
|
|
|
384
410
|
const respond = async (): Promise<Response> => {
|
|
385
411
|
let input: unknown;
|
|
386
412
|
let clientContext: Record<string, unknown> | undefined;
|
|
413
|
+
// The arguments as they arrived: a broadcast's share key. Encoded rather
|
|
414
|
+
// than decoded, because two values the codec tells apart can decode to the
|
|
415
|
+
// same JSON; encodings that differ for equal values only cost sharing.
|
|
416
|
+
let encoded: string | undefined;
|
|
387
417
|
if (isData && (method === "GET" || !isFormBody(request))) {
|
|
388
418
|
try {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
) as {
|
|
419
|
+
if (method === "POST") encoded = await capped(request, bodyLimit).text();
|
|
420
|
+
else encoded = url.searchParams.get(PAYLOAD_PARAM) ?? "";
|
|
421
|
+
const body = (method === "GET" ? queryPayload(url, bodyLimit) : JSON.parse(encoded)) as {
|
|
392
422
|
input?: unknown;
|
|
393
423
|
context?: unknown;
|
|
394
424
|
};
|
|
@@ -399,7 +429,7 @@ export async function handleServerFn(
|
|
|
399
429
|
// what merging it would let through. Spread, so a body that put a string
|
|
400
430
|
// or a list here still hands the chain an object.
|
|
401
431
|
clientContext =
|
|
402
|
-
body?.context === undefined ? undefined : { ...
|
|
432
|
+
body?.context === undefined ? undefined : { ...decodeWire<object>(body.context) };
|
|
403
433
|
} catch (error) {
|
|
404
434
|
return unreadable(error);
|
|
405
435
|
}
|
|
@@ -427,6 +457,12 @@ export async function handleServerFn(
|
|
|
427
457
|
const sendContext = takeSendContext();
|
|
428
458
|
if (isLive) {
|
|
429
459
|
if (result instanceof Response) return applyResponseDraft(result, draft);
|
|
460
|
+
if (target.meta.broadcast === true && encoded !== undefined) {
|
|
461
|
+
return applyResponseDraft(
|
|
462
|
+
broadcastResponse(`${id}\0${method}\0${encoded}`, result, request),
|
|
463
|
+
draft,
|
|
464
|
+
);
|
|
465
|
+
}
|
|
430
466
|
return applyResponseDraft(liveResponse(result, request), draft);
|
|
431
467
|
}
|
|
432
468
|
// A RETURNED RESPONSE IS HONOURED ON BOTH CHANNELS, and it did not used to
|
package/src/wire-types.ts
CHANGED
|
@@ -75,9 +75,8 @@ export type WireProblem<Value, Path extends string = "the value", Left extends n
|
|
|
75
75
|
? Value extends Promise<infer Settled>
|
|
76
76
|
? WireProblem<Settled, Path, Depth[Left]>
|
|
77
77
|
: Value extends ReadonlyMap<infer Key, infer Item>
|
|
78
|
-
?
|
|
79
|
-
|
|
80
|
-
| WireProblem<Item, `${Path}'s values`, Depth[Left]>
|
|
78
|
+
? | WireProblem<Key, `${Path}'s keys`, Depth[Left]>
|
|
79
|
+
| WireProblem<Item, `${Path}'s values`, Depth[Left]>
|
|
81
80
|
: Value extends ReadonlySet<infer Item>
|
|
82
81
|
? WireProblem<Item, `${Path}'s members`, Depth[Left]>
|
|
83
82
|
: Value extends readonly unknown[]
|