@solidjs/web 2.0.0-rc.17 → 2.0.0-rc.19
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/server.dev.js +191 -35
- package/dist/server.js +186 -35
- package/dist/server.observe.js +186 -35
- package/dist/web.dev.js +53 -71
- package/dist/web.js +52 -70
- package/dist/web.observe.js +52 -70
- package/frames/dist/server.dev.js +75 -40
- package/frames/dist/server.js +75 -40
- package/frames/dist/server.observe.js +75 -40
- package/frames/dist/wire.js +110 -14
- package/package.json +4 -4
- package/serialization/dist/decode.js +10 -5
- package/serialization/dist/serialization.js +121 -61
- package/serialization/types/index.d.ts +12 -4
- package/serialization/types/serializer.d.ts +12 -4
- package/server-functions/dist/server.dev.js +245 -8
- package/server-functions/dist/server.js +245 -8
- package/server-functions/dist/server.observe.js +245 -8
- package/types/frames/frame-morph.d.ts +1 -0
- package/types/frames/serializer.d.ts +12 -4
- package/types/serializer.d.ts +12 -4
- package/types/server-functions/server.d.ts +19 -3
- package/types/server-functions/shared.d.ts +9 -0
|
@@ -520,6 +520,7 @@ function decodeErrorHeaderValue(value) {
|
|
|
520
520
|
const BODY_FORMAT_HEADER = "X-Server-Function-Format";
|
|
521
521
|
const LAST_EVENT_ID_HEADER = "Last-Event-ID";
|
|
522
522
|
const UNKNOWN_HEADER = "X-Server-Function-Unknown";
|
|
523
|
+
const BUILD_HEADER = "X-Server-Function-Build";
|
|
523
524
|
const REDIRECT_HEADER = "X-Server-Function-Redirect";
|
|
524
525
|
function decodeRedirectHeaderValue(value) {
|
|
525
526
|
if (typeof value !== "string") return undefined;
|
|
@@ -1295,7 +1296,8 @@ const config = {
|
|
|
1295
1296
|
csrf: true,
|
|
1296
1297
|
bodySizeLimit: 1_048_576,
|
|
1297
1298
|
maxArguments: 1000,
|
|
1298
|
-
chaosReconnectEvery: 0
|
|
1299
|
+
chaosReconnectEvery: 0,
|
|
1300
|
+
buildId: undefined
|
|
1299
1301
|
};
|
|
1300
1302
|
function configureServerFunctionsServer({
|
|
1301
1303
|
provideEvent,
|
|
@@ -1311,7 +1313,8 @@ function configureServerFunctionsServer({
|
|
|
1311
1313
|
bodySizeLimit,
|
|
1312
1314
|
maxArguments,
|
|
1313
1315
|
secret,
|
|
1314
|
-
chaosReconnectEvery
|
|
1316
|
+
chaosReconnectEvery,
|
|
1317
|
+
buildId
|
|
1315
1318
|
} = {}) {
|
|
1316
1319
|
if (provideEvent !== undefined) config.provideEvent = provideEvent;
|
|
1317
1320
|
if (wrapInvocation !== undefined) config.wrapInvocation = wrapInvocation;
|
|
@@ -1327,6 +1330,7 @@ function configureServerFunctionsServer({
|
|
|
1327
1330
|
if (maxArguments !== undefined) config.maxArguments = maxArguments;
|
|
1328
1331
|
if (secret !== undefined) setFlashSecret(secret);
|
|
1329
1332
|
if (chaosReconnectEvery !== undefined) config.chaosReconnectEvery = chaosReconnectEvery;
|
|
1333
|
+
if (buildId !== undefined) config.buildId = buildId;
|
|
1330
1334
|
}
|
|
1331
1335
|
const flightSources = new Map();
|
|
1332
1336
|
function registerFlightDataSource(source, hook) {
|
|
@@ -2451,7 +2455,7 @@ function serializeResponseStream(value, codecOptions, signal, scope, live) {
|
|
|
2451
2455
|
return new ReadableStream({
|
|
2452
2456
|
async start(controller) {
|
|
2453
2457
|
streamController = controller;
|
|
2454
|
-
if (live) stopLive = armLiveBody(controller, teardown);
|
|
2458
|
+
if (live && !live.nodes) stopLive = armLiveBody(controller, teardown);
|
|
2455
2459
|
if (signal) {
|
|
2456
2460
|
if (signal.aborted) {
|
|
2457
2461
|
teardown();
|
|
@@ -2481,7 +2485,10 @@ function serializeResponseStream(value, codecOptions, signal, scope, live) {
|
|
|
2481
2485
|
cancelSerialize = serializeJSON(value, {
|
|
2482
2486
|
...codecOptions,
|
|
2483
2487
|
onParse(node) {
|
|
2484
|
-
if (
|
|
2488
|
+
if (closed) return;
|
|
2489
|
+
const chunk = frame(JSON.stringify(node));
|
|
2490
|
+
if (live && live.nodes) live.nodes.set(chunk, node);
|
|
2491
|
+
controller.enqueue(chunk);
|
|
2485
2492
|
},
|
|
2486
2493
|
onDone() {
|
|
2487
2494
|
if (closed) return;
|
|
@@ -2552,7 +2559,7 @@ function armLiveBody(controller, teardown) {
|
|
|
2552
2559
|
if (chaos !== null) clearTimeout(chaos);
|
|
2553
2560
|
};
|
|
2554
2561
|
}
|
|
2555
|
-
function liveSource(value, lastEventId, position, scope) {
|
|
2562
|
+
function liveSource(value, lastEventId, position, scope, refs) {
|
|
2556
2563
|
const digestOf = v => scope ? scope(() => positionDigest(v)) : positionDigest(v);
|
|
2557
2564
|
if (value === null || typeof value !== "object" || typeof value[Symbol.asyncIterator] !== "function") {
|
|
2558
2565
|
const digest = digestOf(value);
|
|
@@ -2567,7 +2574,12 @@ function liveSource(value, lastEventId, position, scope) {
|
|
|
2567
2574
|
let first = true;
|
|
2568
2575
|
const step = () => iterator.next().then(result => {
|
|
2569
2576
|
if (result.done) return result;
|
|
2577
|
+
if (refs) refs.turn();
|
|
2570
2578
|
const digest = digestOf(result.value);
|
|
2579
|
+
if (refs) position.yields.push({
|
|
2580
|
+
value: result.value,
|
|
2581
|
+
digest
|
|
2582
|
+
});
|
|
2571
2583
|
if (first) {
|
|
2572
2584
|
first = false;
|
|
2573
2585
|
if (digest !== undefined && digest === lastEventId) return step();
|
|
@@ -2586,10 +2598,14 @@ function liveSource(value, lastEventId, position, scope) {
|
|
|
2586
2598
|
}
|
|
2587
2599
|
};
|
|
2588
2600
|
}
|
|
2589
|
-
function encodeLiveResult(value, headers, status, codec, request, scope) {
|
|
2601
|
+
function encodeLiveResult(value, headers, status, codec, request, scope, shareKey) {
|
|
2590
2602
|
if (NULL_BODY_STATUSES.has(status) || value === undefined || getHeadersAndBody(value)) {
|
|
2591
2603
|
return encodeResult(value, headers, status, codec, request.signal, scope);
|
|
2592
2604
|
}
|
|
2605
|
+
if (shareKey !== undefined && value !== null && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function") {
|
|
2606
|
+
const shared = sharedSourceResponse(shareKey, value, headers, status, codec, request);
|
|
2607
|
+
if (shared) return shared;
|
|
2608
|
+
}
|
|
2593
2609
|
const position = {
|
|
2594
2610
|
pending: undefined
|
|
2595
2611
|
};
|
|
@@ -2607,6 +2623,213 @@ function encodeLiveResult(value, headers, status, codec, request, scope) {
|
|
|
2607
2623
|
throw DEV && error instanceof Error ? new Error(`Server function result could not be encoded: ${error.message}`) : error;
|
|
2608
2624
|
}
|
|
2609
2625
|
}
|
|
2626
|
+
const SHARED_SOURCES = new Map();
|
|
2627
|
+
class GenerationalRefs extends Map {
|
|
2628
|
+
count = 0;
|
|
2629
|
+
touched = new Set();
|
|
2630
|
+
get size() {
|
|
2631
|
+
return this.count;
|
|
2632
|
+
}
|
|
2633
|
+
get(key) {
|
|
2634
|
+
const id = super.get(key);
|
|
2635
|
+
if (id !== undefined) this.touched.add(key);
|
|
2636
|
+
return id;
|
|
2637
|
+
}
|
|
2638
|
+
set(key, value) {
|
|
2639
|
+
if (!super.has(key)) this.count++;
|
|
2640
|
+
this.touched.add(key);
|
|
2641
|
+
return super.set(key, value);
|
|
2642
|
+
}
|
|
2643
|
+
turn() {
|
|
2644
|
+
for (const key of [...super.keys()]) if (!this.touched.has(key)) super.delete(key);
|
|
2645
|
+
this.touched = new Set();
|
|
2646
|
+
}
|
|
2647
|
+
idOf(key) {
|
|
2648
|
+
return super.get(key);
|
|
2649
|
+
}
|
|
2650
|
+
reserve() {
|
|
2651
|
+
return this.count++;
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
2654
|
+
function sharedSourceResponse(key, value, headers, status, codec, request) {
|
|
2655
|
+
let shared = SHARED_SOURCES.get(key);
|
|
2656
|
+
let snapshot;
|
|
2657
|
+
if (shared && !shared.ended) {
|
|
2658
|
+
snapshot = sharedSnapshot(shared);
|
|
2659
|
+
if (snapshot === null) return undefined;
|
|
2660
|
+
if (value !== shared.source) closeUnread(value);
|
|
2661
|
+
} else shared = startSharedSource(key, value, codec);
|
|
2662
|
+
return tapSharedSource(shared, request, headers, status, snapshot);
|
|
2663
|
+
}
|
|
2664
|
+
function closeUnread(value) {
|
|
2665
|
+
try {
|
|
2666
|
+
const closing = typeof value.return === "function" ? value.return() : typeof value.cancel === "function" ? value.cancel() : undefined;
|
|
2667
|
+
if (closing && typeof closing.then === "function") closing.then(undefined, () => {});
|
|
2668
|
+
} catch {}
|
|
2669
|
+
}
|
|
2670
|
+
function startSharedSource(key, value, codec) {
|
|
2671
|
+
const refs = new GenerationalRefs();
|
|
2672
|
+
const shared = {
|
|
2673
|
+
key,
|
|
2674
|
+
codec,
|
|
2675
|
+
refs,
|
|
2676
|
+
encode: null,
|
|
2677
|
+
head: null,
|
|
2678
|
+
node: null,
|
|
2679
|
+
value: undefined,
|
|
2680
|
+
position: undefined,
|
|
2681
|
+
stream: undefined,
|
|
2682
|
+
source: value,
|
|
2683
|
+
taps: new Set(),
|
|
2684
|
+
ended: false,
|
|
2685
|
+
teardown: new AbortController()
|
|
2686
|
+
};
|
|
2687
|
+
SHARED_SOURCES.set(key, shared);
|
|
2688
|
+
const event = broadcastEvent();
|
|
2689
|
+
const store = globalThis[RequestContext];
|
|
2690
|
+
const scope = run => store ? store.run(event, run) : run();
|
|
2691
|
+
const live = {
|
|
2692
|
+
pending: undefined,
|
|
2693
|
+
yields: [],
|
|
2694
|
+
nodes: new WeakMap()
|
|
2695
|
+
};
|
|
2696
|
+
const body = serializeResponseStream(liveSource(value, undefined, live, scope, refs), {
|
|
2697
|
+
...codec,
|
|
2698
|
+
refs
|
|
2699
|
+
}, shared.teardown.signal, scope, live);
|
|
2700
|
+
pumpSharedSource(shared, body.getReader(), live);
|
|
2701
|
+
return shared;
|
|
2702
|
+
}
|
|
2703
|
+
async function pumpSharedSource(shared, reader, {
|
|
2704
|
+
nodes,
|
|
2705
|
+
yields
|
|
2706
|
+
}) {
|
|
2707
|
+
try {
|
|
2708
|
+
shared.encode = (await import('@solidjs/web/serialization')).serializeJSON;
|
|
2709
|
+
while (true) {
|
|
2710
|
+
const {
|
|
2711
|
+
done,
|
|
2712
|
+
value: chunk
|
|
2713
|
+
} = await reader.read();
|
|
2714
|
+
if (done) break;
|
|
2715
|
+
const node = nodes.get(chunk);
|
|
2716
|
+
if (shared.head === null) shared.head = chunk;else if (node && node.t === 32 && (shared.stream ??= node.i) === node.i) {
|
|
2717
|
+
const {
|
|
2718
|
+
value,
|
|
2719
|
+
digest
|
|
2720
|
+
} = yields.shift();
|
|
2721
|
+
shared.node = node;
|
|
2722
|
+
shared.value = value;
|
|
2723
|
+
shared.position = digest;
|
|
2724
|
+
}
|
|
2725
|
+
for (const tap of shared.taps) tap.write(chunk);
|
|
2726
|
+
}
|
|
2727
|
+
} catch {}
|
|
2728
|
+
shared.ended = true;
|
|
2729
|
+
if (SHARED_SOURCES.get(shared.key) === shared) SHARED_SOURCES.delete(shared.key);
|
|
2730
|
+
for (const tap of [...shared.taps]) tap.end();
|
|
2731
|
+
}
|
|
2732
|
+
function sharedSnapshot(shared) {
|
|
2733
|
+
if (shared.node === null) return undefined;
|
|
2734
|
+
const own = new Map();
|
|
2735
|
+
let root;
|
|
2736
|
+
let done = false;
|
|
2737
|
+
const cancel = shared.encode(shared.value, {
|
|
2738
|
+
...shared.codec,
|
|
2739
|
+
refs: own,
|
|
2740
|
+
onParse(node) {
|
|
2741
|
+
root ??= node;
|
|
2742
|
+
},
|
|
2743
|
+
onDone() {
|
|
2744
|
+
done = true;
|
|
2745
|
+
},
|
|
2746
|
+
onError() {}
|
|
2747
|
+
});
|
|
2748
|
+
if (!done || root === undefined) {
|
|
2749
|
+
cancel();
|
|
2750
|
+
return null;
|
|
2751
|
+
}
|
|
2752
|
+
const ids = new Map();
|
|
2753
|
+
for (const [value, id] of own) ids.set(id, shared.refs.idOf(value) ?? shared.refs.reserve());
|
|
2754
|
+
return createEventChunk(JSON.stringify({
|
|
2755
|
+
...shared.node,
|
|
2756
|
+
f: renumberNode(root, ids)
|
|
2757
|
+
}), shared.position);
|
|
2758
|
+
}
|
|
2759
|
+
function renumberNode(node, ids) {
|
|
2760
|
+
if (Array.isArray(node)) return node.map(item => renumberNode(item, ids));
|
|
2761
|
+
if (node === null || typeof node !== "object") return node;
|
|
2762
|
+
const out = {};
|
|
2763
|
+
for (const key in node) out[key] = key === "i" && typeof node[key] === "number" ? ids.get(node[key]) ?? node[key] : renumberNode(node[key], ids);
|
|
2764
|
+
return out;
|
|
2765
|
+
}
|
|
2766
|
+
function tapSharedSource(shared, request, headers, status, snapshot) {
|
|
2767
|
+
headers.set(BODY_FORMAT_HEADER, BodyFormat.Serialized);
|
|
2768
|
+
headers.set("Content-Type", "text/event-stream");
|
|
2769
|
+
headers.set("Cache-Control", "no-store");
|
|
2770
|
+
headers.set("X-Accel-Buffering", "no");
|
|
2771
|
+
const signal = request.signal;
|
|
2772
|
+
let closed = false;
|
|
2773
|
+
let stopLive = null;
|
|
2774
|
+
let tap = null;
|
|
2775
|
+
const leave = () => {
|
|
2776
|
+
if (closed) return;
|
|
2777
|
+
closed = true;
|
|
2778
|
+
if (stopLive) stopLive();
|
|
2779
|
+
if (signal) signal.removeEventListener("abort", leave);
|
|
2780
|
+
shared.taps.delete(tap);
|
|
2781
|
+
if (!shared.taps.size && !shared.ended) {
|
|
2782
|
+
shared.ended = true;
|
|
2783
|
+
if (SHARED_SOURCES.get(shared.key) === shared) SHARED_SOURCES.delete(shared.key);
|
|
2784
|
+
shared.teardown.abort();
|
|
2785
|
+
}
|
|
2786
|
+
};
|
|
2787
|
+
const body = new ReadableStream({
|
|
2788
|
+
start(controller) {
|
|
2789
|
+
tap = {
|
|
2790
|
+
write(chunk) {
|
|
2791
|
+
if (closed) return;
|
|
2792
|
+
try {
|
|
2793
|
+
controller.enqueue(chunk);
|
|
2794
|
+
} catch {
|
|
2795
|
+
leave();
|
|
2796
|
+
}
|
|
2797
|
+
},
|
|
2798
|
+
end() {
|
|
2799
|
+
leave();
|
|
2800
|
+
try {
|
|
2801
|
+
controller.close();
|
|
2802
|
+
} catch {}
|
|
2803
|
+
}
|
|
2804
|
+
};
|
|
2805
|
+
stopLive = armLiveBody(controller, leave);
|
|
2806
|
+
if (shared.head) controller.enqueue(shared.head);
|
|
2807
|
+
if (snapshot) controller.enqueue(snapshot);
|
|
2808
|
+
if (shared.ended || signal && signal.aborted) return tap.end();
|
|
2809
|
+
shared.taps.add(tap);
|
|
2810
|
+
if (signal) signal.addEventListener("abort", leave);
|
|
2811
|
+
},
|
|
2812
|
+
cancel() {
|
|
2813
|
+
leave();
|
|
2814
|
+
}
|
|
2815
|
+
});
|
|
2816
|
+
return new Response(body, {
|
|
2817
|
+
status,
|
|
2818
|
+
headers
|
|
2819
|
+
});
|
|
2820
|
+
}
|
|
2821
|
+
function broadcastEvent() {
|
|
2822
|
+
const event = {
|
|
2823
|
+
broadcast: true
|
|
2824
|
+
};
|
|
2825
|
+
for (const name of ["request", "locals", "clientAddress", "nativeEvent"]) Object.defineProperty(event, name, {
|
|
2826
|
+
get() {
|
|
2827
|
+
if (DEV) console.warn(`A broadcast server function read the request (\`event.${name}\`) while producing ` + "its answer, which every viewer shares; read per-viewer state before returning it.");
|
|
2828
|
+
return undefined;
|
|
2829
|
+
}
|
|
2830
|
+
});
|
|
2831
|
+
return event;
|
|
2832
|
+
}
|
|
2610
2833
|
function encodeResult(value, headers, status, codec, signal, scope) {
|
|
2611
2834
|
if (NULL_BODY_STATUSES.has(status)) {
|
|
2612
2835
|
if (value === undefined || value === null) {
|
|
@@ -2800,7 +3023,7 @@ function withHeaders(response, write) {
|
|
|
2800
3023
|
function withCSRFVary(response) {
|
|
2801
3024
|
return withHeaders(response, headers => appendVary(headers, CSRF_VARY));
|
|
2802
3025
|
}
|
|
2803
|
-
const TRANSPORT_REQUEST_HEADERS = ["Content-Type", BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER].join(", ");
|
|
3026
|
+
const TRANSPORT_REQUEST_HEADERS = ["Content-Type", BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER, BUILD_HEADER].join(", ");
|
|
2804
3027
|
const PREFLIGHT_MAX_AGE = "600";
|
|
2805
3028
|
const CORS_SAFELISTED_RESPONSE_HEADERS = new Set(["cache-control", "content-language", "content-length", "content-type", "expires", "last-modified", "pragma"]);
|
|
2806
3029
|
function withCORS(response, cors) {
|
|
@@ -2902,6 +3125,17 @@ async function handleServerFunctionRequest(request, options = {}) {
|
|
|
2902
3125
|
});
|
|
2903
3126
|
return finish(protectsRequest ? withCSRFVary(response) : response);
|
|
2904
3127
|
}
|
|
3128
|
+
const callerBuild = request.headers.get(BUILD_HEADER);
|
|
3129
|
+
if (config.buildId && callerBuild !== null && callerBuild !== config.buildId) {
|
|
3130
|
+
return finish(new Response(DEV ? `Server function call from build "${callerBuild}" refused by build "${config.buildId}"` : null, {
|
|
3131
|
+
status: 409,
|
|
3132
|
+
headers: {
|
|
3133
|
+
[UNKNOWN_HEADER]: "true",
|
|
3134
|
+
[BUILD_HEADER]: config.buildId,
|
|
3135
|
+
"Cache-Control": "no-store"
|
|
3136
|
+
}
|
|
3137
|
+
}));
|
|
3138
|
+
}
|
|
2905
3139
|
const scripted = address.data || address.live;
|
|
2906
3140
|
const live = address.live;
|
|
2907
3141
|
if (method !== "POST" && !declaredRead) {
|
|
@@ -3112,7 +3346,10 @@ async function handleServerFunctionRequest(request, options = {}) {
|
|
|
3112
3346
|
return encodeResult(result, headers, status, codec, request.signal, scope);
|
|
3113
3347
|
}
|
|
3114
3348
|
if (status === 304) warnScripted304(functionId);
|
|
3115
|
-
if (live)
|
|
3349
|
+
if (live) {
|
|
3350
|
+
const invocation = INVOCATIONS.get(event);
|
|
3351
|
+
return encodeLiveResult(result, headers, status, codec, request, scope, invocation && invocation.broadcast && invocation.shareKey !== undefined ? invocation.id + "\0" + invocation.shareKey : undefined);
|
|
3352
|
+
}
|
|
3116
3353
|
return encodeResult(result, headers, status, codec, request.signal, scope);
|
|
3117
3354
|
} catch (x) {
|
|
3118
3355
|
const respondThrown = value => {
|
|
@@ -550,6 +550,7 @@ function decodeErrorHeaderValue(value) {
|
|
|
550
550
|
const BODY_FORMAT_HEADER = "X-Server-Function-Format";
|
|
551
551
|
const LAST_EVENT_ID_HEADER = "Last-Event-ID";
|
|
552
552
|
const UNKNOWN_HEADER = "X-Server-Function-Unknown";
|
|
553
|
+
const BUILD_HEADER = "X-Server-Function-Build";
|
|
553
554
|
const REDIRECT_HEADER = "X-Server-Function-Redirect";
|
|
554
555
|
function decodeRedirectHeaderValue(value) {
|
|
555
556
|
if (typeof value !== "string") return undefined;
|
|
@@ -1347,7 +1348,8 @@ const config = {
|
|
|
1347
1348
|
csrf: true,
|
|
1348
1349
|
bodySizeLimit: 1_048_576,
|
|
1349
1350
|
maxArguments: 1000,
|
|
1350
|
-
chaosReconnectEvery: 0
|
|
1351
|
+
chaosReconnectEvery: 0,
|
|
1352
|
+
buildId: undefined
|
|
1351
1353
|
};
|
|
1352
1354
|
function configureServerFunctionsServer({
|
|
1353
1355
|
provideEvent,
|
|
@@ -1363,7 +1365,8 @@ function configureServerFunctionsServer({
|
|
|
1363
1365
|
bodySizeLimit,
|
|
1364
1366
|
maxArguments,
|
|
1365
1367
|
secret,
|
|
1366
|
-
chaosReconnectEvery
|
|
1368
|
+
chaosReconnectEvery,
|
|
1369
|
+
buildId
|
|
1367
1370
|
} = {}) {
|
|
1368
1371
|
if (provideEvent !== undefined) config.provideEvent = provideEvent;
|
|
1369
1372
|
if (wrapInvocation !== undefined) config.wrapInvocation = wrapInvocation;
|
|
@@ -1379,6 +1382,7 @@ function configureServerFunctionsServer({
|
|
|
1379
1382
|
if (maxArguments !== undefined) config.maxArguments = maxArguments;
|
|
1380
1383
|
if (secret !== undefined) setFlashSecret(secret);
|
|
1381
1384
|
if (chaosReconnectEvery !== undefined) config.chaosReconnectEvery = chaosReconnectEvery;
|
|
1385
|
+
if (buildId !== undefined) config.buildId = buildId;
|
|
1382
1386
|
}
|
|
1383
1387
|
const flightSources = new Map();
|
|
1384
1388
|
function registerFlightDataSource(source, hook) {
|
|
@@ -2503,7 +2507,7 @@ function serializeResponseStream(value, codecOptions, signal, scope, live) {
|
|
|
2503
2507
|
return new ReadableStream({
|
|
2504
2508
|
async start(controller) {
|
|
2505
2509
|
streamController = controller;
|
|
2506
|
-
if (live) stopLive = armLiveBody(controller, teardown);
|
|
2510
|
+
if (live && !live.nodes) stopLive = armLiveBody(controller, teardown);
|
|
2507
2511
|
if (signal) {
|
|
2508
2512
|
if (signal.aborted) {
|
|
2509
2513
|
teardown();
|
|
@@ -2533,7 +2537,10 @@ function serializeResponseStream(value, codecOptions, signal, scope, live) {
|
|
|
2533
2537
|
cancelSerialize = serializeJSON(value, {
|
|
2534
2538
|
...codecOptions,
|
|
2535
2539
|
onParse(node) {
|
|
2536
|
-
if (
|
|
2540
|
+
if (closed) return;
|
|
2541
|
+
const chunk = frame(JSON.stringify(node));
|
|
2542
|
+
if (live && live.nodes) live.nodes.set(chunk, node);
|
|
2543
|
+
controller.enqueue(chunk);
|
|
2537
2544
|
},
|
|
2538
2545
|
onDone() {
|
|
2539
2546
|
if (closed) return;
|
|
@@ -2604,7 +2611,7 @@ function armLiveBody(controller, teardown) {
|
|
|
2604
2611
|
if (chaos !== null) clearTimeout(chaos);
|
|
2605
2612
|
};
|
|
2606
2613
|
}
|
|
2607
|
-
function liveSource(value, lastEventId, position, scope) {
|
|
2614
|
+
function liveSource(value, lastEventId, position, scope, refs) {
|
|
2608
2615
|
const digestOf = v => scope ? scope(() => positionDigest(v)) : positionDigest(v);
|
|
2609
2616
|
if (value === null || typeof value !== "object" || typeof value[Symbol.asyncIterator] !== "function") {
|
|
2610
2617
|
const digest = digestOf(value);
|
|
@@ -2619,7 +2626,12 @@ function liveSource(value, lastEventId, position, scope) {
|
|
|
2619
2626
|
let first = true;
|
|
2620
2627
|
const step = () => iterator.next().then(result => {
|
|
2621
2628
|
if (result.done) return result;
|
|
2629
|
+
if (refs) refs.turn();
|
|
2622
2630
|
const digest = digestOf(result.value);
|
|
2631
|
+
if (refs) position.yields.push({
|
|
2632
|
+
value: result.value,
|
|
2633
|
+
digest
|
|
2634
|
+
});
|
|
2623
2635
|
if (first) {
|
|
2624
2636
|
first = false;
|
|
2625
2637
|
if (digest !== undefined && digest === lastEventId) return step();
|
|
@@ -2638,10 +2650,14 @@ function liveSource(value, lastEventId, position, scope) {
|
|
|
2638
2650
|
}
|
|
2639
2651
|
};
|
|
2640
2652
|
}
|
|
2641
|
-
function encodeLiveResult(value, headers, status, codec, request, scope) {
|
|
2653
|
+
function encodeLiveResult(value, headers, status, codec, request, scope, shareKey) {
|
|
2642
2654
|
if (NULL_BODY_STATUSES.has(status) || value === undefined || getHeadersAndBody(value)) {
|
|
2643
2655
|
return encodeResult(value, headers, status, codec, request.signal, scope);
|
|
2644
2656
|
}
|
|
2657
|
+
if (shareKey !== undefined && value !== null && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function") {
|
|
2658
|
+
const shared = sharedSourceResponse(shareKey, value, headers, status, codec, request);
|
|
2659
|
+
if (shared) return shared;
|
|
2660
|
+
}
|
|
2645
2661
|
const position = {
|
|
2646
2662
|
pending: undefined
|
|
2647
2663
|
};
|
|
@@ -2659,6 +2675,213 @@ function encodeLiveResult(value, headers, status, codec, request, scope) {
|
|
|
2659
2675
|
throw DEV && error instanceof Error ? new Error(`Server function result could not be encoded: ${error.message}`) : error;
|
|
2660
2676
|
}
|
|
2661
2677
|
}
|
|
2678
|
+
const SHARED_SOURCES = new Map();
|
|
2679
|
+
class GenerationalRefs extends Map {
|
|
2680
|
+
count = 0;
|
|
2681
|
+
touched = new Set();
|
|
2682
|
+
get size() {
|
|
2683
|
+
return this.count;
|
|
2684
|
+
}
|
|
2685
|
+
get(key) {
|
|
2686
|
+
const id = super.get(key);
|
|
2687
|
+
if (id !== undefined) this.touched.add(key);
|
|
2688
|
+
return id;
|
|
2689
|
+
}
|
|
2690
|
+
set(key, value) {
|
|
2691
|
+
if (!super.has(key)) this.count++;
|
|
2692
|
+
this.touched.add(key);
|
|
2693
|
+
return super.set(key, value);
|
|
2694
|
+
}
|
|
2695
|
+
turn() {
|
|
2696
|
+
for (const key of [...super.keys()]) if (!this.touched.has(key)) super.delete(key);
|
|
2697
|
+
this.touched = new Set();
|
|
2698
|
+
}
|
|
2699
|
+
idOf(key) {
|
|
2700
|
+
return super.get(key);
|
|
2701
|
+
}
|
|
2702
|
+
reserve() {
|
|
2703
|
+
return this.count++;
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
function sharedSourceResponse(key, value, headers, status, codec, request) {
|
|
2707
|
+
let shared = SHARED_SOURCES.get(key);
|
|
2708
|
+
let snapshot;
|
|
2709
|
+
if (shared && !shared.ended) {
|
|
2710
|
+
snapshot = sharedSnapshot(shared);
|
|
2711
|
+
if (snapshot === null) return undefined;
|
|
2712
|
+
if (value !== shared.source) closeUnread(value);
|
|
2713
|
+
} else shared = startSharedSource(key, value, codec);
|
|
2714
|
+
return tapSharedSource(shared, request, headers, status, snapshot);
|
|
2715
|
+
}
|
|
2716
|
+
function closeUnread(value) {
|
|
2717
|
+
try {
|
|
2718
|
+
const closing = typeof value.return === "function" ? value.return() : typeof value.cancel === "function" ? value.cancel() : undefined;
|
|
2719
|
+
if (closing && typeof closing.then === "function") closing.then(undefined, () => {});
|
|
2720
|
+
} catch {}
|
|
2721
|
+
}
|
|
2722
|
+
function startSharedSource(key, value, codec) {
|
|
2723
|
+
const refs = new GenerationalRefs();
|
|
2724
|
+
const shared = {
|
|
2725
|
+
key,
|
|
2726
|
+
codec,
|
|
2727
|
+
refs,
|
|
2728
|
+
encode: null,
|
|
2729
|
+
head: null,
|
|
2730
|
+
node: null,
|
|
2731
|
+
value: undefined,
|
|
2732
|
+
position: undefined,
|
|
2733
|
+
stream: undefined,
|
|
2734
|
+
source: value,
|
|
2735
|
+
taps: new Set(),
|
|
2736
|
+
ended: false,
|
|
2737
|
+
teardown: new AbortController()
|
|
2738
|
+
};
|
|
2739
|
+
SHARED_SOURCES.set(key, shared);
|
|
2740
|
+
const event = broadcastEvent();
|
|
2741
|
+
const store = globalThis[RequestContext];
|
|
2742
|
+
const scope = run => store ? store.run(event, run) : run();
|
|
2743
|
+
const live = {
|
|
2744
|
+
pending: undefined,
|
|
2745
|
+
yields: [],
|
|
2746
|
+
nodes: new WeakMap()
|
|
2747
|
+
};
|
|
2748
|
+
const body = serializeResponseStream(liveSource(value, undefined, live, scope, refs), {
|
|
2749
|
+
...codec,
|
|
2750
|
+
refs
|
|
2751
|
+
}, shared.teardown.signal, scope, live);
|
|
2752
|
+
pumpSharedSource(shared, body.getReader(), live);
|
|
2753
|
+
return shared;
|
|
2754
|
+
}
|
|
2755
|
+
async function pumpSharedSource(shared, reader, {
|
|
2756
|
+
nodes,
|
|
2757
|
+
yields
|
|
2758
|
+
}) {
|
|
2759
|
+
try {
|
|
2760
|
+
shared.encode = (await import('@solidjs/web/serialization')).serializeJSON;
|
|
2761
|
+
while (true) {
|
|
2762
|
+
const {
|
|
2763
|
+
done,
|
|
2764
|
+
value: chunk
|
|
2765
|
+
} = await reader.read();
|
|
2766
|
+
if (done) break;
|
|
2767
|
+
const node = nodes.get(chunk);
|
|
2768
|
+
if (shared.head === null) shared.head = chunk;else if (node && node.t === 32 && (shared.stream ??= node.i) === node.i) {
|
|
2769
|
+
const {
|
|
2770
|
+
value,
|
|
2771
|
+
digest
|
|
2772
|
+
} = yields.shift();
|
|
2773
|
+
shared.node = node;
|
|
2774
|
+
shared.value = value;
|
|
2775
|
+
shared.position = digest;
|
|
2776
|
+
}
|
|
2777
|
+
for (const tap of shared.taps) tap.write(chunk);
|
|
2778
|
+
}
|
|
2779
|
+
} catch {}
|
|
2780
|
+
shared.ended = true;
|
|
2781
|
+
if (SHARED_SOURCES.get(shared.key) === shared) SHARED_SOURCES.delete(shared.key);
|
|
2782
|
+
for (const tap of [...shared.taps]) tap.end();
|
|
2783
|
+
}
|
|
2784
|
+
function sharedSnapshot(shared) {
|
|
2785
|
+
if (shared.node === null) return undefined;
|
|
2786
|
+
const own = new Map();
|
|
2787
|
+
let root;
|
|
2788
|
+
let done = false;
|
|
2789
|
+
const cancel = shared.encode(shared.value, {
|
|
2790
|
+
...shared.codec,
|
|
2791
|
+
refs: own,
|
|
2792
|
+
onParse(node) {
|
|
2793
|
+
root ??= node;
|
|
2794
|
+
},
|
|
2795
|
+
onDone() {
|
|
2796
|
+
done = true;
|
|
2797
|
+
},
|
|
2798
|
+
onError() {}
|
|
2799
|
+
});
|
|
2800
|
+
if (!done || root === undefined) {
|
|
2801
|
+
cancel();
|
|
2802
|
+
return null;
|
|
2803
|
+
}
|
|
2804
|
+
const ids = new Map();
|
|
2805
|
+
for (const [value, id] of own) ids.set(id, shared.refs.idOf(value) ?? shared.refs.reserve());
|
|
2806
|
+
return createEventChunk(JSON.stringify({
|
|
2807
|
+
...shared.node,
|
|
2808
|
+
f: renumberNode(root, ids)
|
|
2809
|
+
}), shared.position);
|
|
2810
|
+
}
|
|
2811
|
+
function renumberNode(node, ids) {
|
|
2812
|
+
if (Array.isArray(node)) return node.map(item => renumberNode(item, ids));
|
|
2813
|
+
if (node === null || typeof node !== "object") return node;
|
|
2814
|
+
const out = {};
|
|
2815
|
+
for (const key in node) out[key] = key === "i" && typeof node[key] === "number" ? ids.get(node[key]) ?? node[key] : renumberNode(node[key], ids);
|
|
2816
|
+
return out;
|
|
2817
|
+
}
|
|
2818
|
+
function tapSharedSource(shared, request, headers, status, snapshot) {
|
|
2819
|
+
headers.set(BODY_FORMAT_HEADER, BodyFormat.Serialized);
|
|
2820
|
+
headers.set("Content-Type", "text/event-stream");
|
|
2821
|
+
headers.set("Cache-Control", "no-store");
|
|
2822
|
+
headers.set("X-Accel-Buffering", "no");
|
|
2823
|
+
const signal = request.signal;
|
|
2824
|
+
let closed = false;
|
|
2825
|
+
let stopLive = null;
|
|
2826
|
+
let tap = null;
|
|
2827
|
+
const leave = () => {
|
|
2828
|
+
if (closed) return;
|
|
2829
|
+
closed = true;
|
|
2830
|
+
if (stopLive) stopLive();
|
|
2831
|
+
if (signal) signal.removeEventListener("abort", leave);
|
|
2832
|
+
shared.taps.delete(tap);
|
|
2833
|
+
if (!shared.taps.size && !shared.ended) {
|
|
2834
|
+
shared.ended = true;
|
|
2835
|
+
if (SHARED_SOURCES.get(shared.key) === shared) SHARED_SOURCES.delete(shared.key);
|
|
2836
|
+
shared.teardown.abort();
|
|
2837
|
+
}
|
|
2838
|
+
};
|
|
2839
|
+
const body = new ReadableStream({
|
|
2840
|
+
start(controller) {
|
|
2841
|
+
tap = {
|
|
2842
|
+
write(chunk) {
|
|
2843
|
+
if (closed) return;
|
|
2844
|
+
try {
|
|
2845
|
+
controller.enqueue(chunk);
|
|
2846
|
+
} catch {
|
|
2847
|
+
leave();
|
|
2848
|
+
}
|
|
2849
|
+
},
|
|
2850
|
+
end() {
|
|
2851
|
+
leave();
|
|
2852
|
+
try {
|
|
2853
|
+
controller.close();
|
|
2854
|
+
} catch {}
|
|
2855
|
+
}
|
|
2856
|
+
};
|
|
2857
|
+
stopLive = armLiveBody(controller, leave);
|
|
2858
|
+
if (shared.head) controller.enqueue(shared.head);
|
|
2859
|
+
if (snapshot) controller.enqueue(snapshot);
|
|
2860
|
+
if (shared.ended || signal && signal.aborted) return tap.end();
|
|
2861
|
+
shared.taps.add(tap);
|
|
2862
|
+
if (signal) signal.addEventListener("abort", leave);
|
|
2863
|
+
},
|
|
2864
|
+
cancel() {
|
|
2865
|
+
leave();
|
|
2866
|
+
}
|
|
2867
|
+
});
|
|
2868
|
+
return new Response(body, {
|
|
2869
|
+
status,
|
|
2870
|
+
headers
|
|
2871
|
+
});
|
|
2872
|
+
}
|
|
2873
|
+
function broadcastEvent() {
|
|
2874
|
+
const event = {
|
|
2875
|
+
broadcast: true
|
|
2876
|
+
};
|
|
2877
|
+
for (const name of ["request", "locals", "clientAddress", "nativeEvent"]) Object.defineProperty(event, name, {
|
|
2878
|
+
get() {
|
|
2879
|
+
if (DEV) console.warn(`A broadcast server function read the request (\`event.${name}\`) while producing ` + "its answer, which every viewer shares; read per-viewer state before returning it.");
|
|
2880
|
+
return undefined;
|
|
2881
|
+
}
|
|
2882
|
+
});
|
|
2883
|
+
return event;
|
|
2884
|
+
}
|
|
2662
2885
|
function encodeResult(value, headers, status, codec, signal, scope) {
|
|
2663
2886
|
if (NULL_BODY_STATUSES.has(status)) {
|
|
2664
2887
|
if (value === undefined || value === null) {
|
|
@@ -2863,7 +3086,7 @@ function withHeaders(response, write) {
|
|
|
2863
3086
|
function withCSRFVary(response) {
|
|
2864
3087
|
return withHeaders(response, headers => appendVary(headers, CSRF_VARY));
|
|
2865
3088
|
}
|
|
2866
|
-
const TRANSPORT_REQUEST_HEADERS = ["Content-Type", BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER].join(", ");
|
|
3089
|
+
const TRANSPORT_REQUEST_HEADERS = ["Content-Type", BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER, BUILD_HEADER].join(", ");
|
|
2867
3090
|
const PREFLIGHT_MAX_AGE = "600";
|
|
2868
3091
|
const CORS_SAFELISTED_RESPONSE_HEADERS = new Set(["cache-control", "content-language", "content-length", "content-type", "expires", "last-modified", "pragma"]);
|
|
2869
3092
|
function withCORS(response, cors) {
|
|
@@ -2965,6 +3188,17 @@ async function handleServerFunctionRequest(request, options = {}) {
|
|
|
2965
3188
|
});
|
|
2966
3189
|
return finish(protectsRequest ? withCSRFVary(response) : response);
|
|
2967
3190
|
}
|
|
3191
|
+
const callerBuild = request.headers.get(BUILD_HEADER);
|
|
3192
|
+
if (config.buildId && callerBuild !== null && callerBuild !== config.buildId) {
|
|
3193
|
+
return finish(new Response(DEV ? `Server function call from build "${callerBuild}" refused by build "${config.buildId}"` : null, {
|
|
3194
|
+
status: 409,
|
|
3195
|
+
headers: {
|
|
3196
|
+
[UNKNOWN_HEADER]: "true",
|
|
3197
|
+
[BUILD_HEADER]: config.buildId,
|
|
3198
|
+
"Cache-Control": "no-store"
|
|
3199
|
+
}
|
|
3200
|
+
}));
|
|
3201
|
+
}
|
|
2968
3202
|
const scripted = address.data || address.live;
|
|
2969
3203
|
const live = address.live;
|
|
2970
3204
|
if (method !== "POST" && !declaredRead) {
|
|
@@ -3175,7 +3409,10 @@ async function handleServerFunctionRequest(request, options = {}) {
|
|
|
3175
3409
|
return encodeResult(result, headers, status, codec, request.signal, scope);
|
|
3176
3410
|
}
|
|
3177
3411
|
if (status === 304) warnScripted304(functionId);
|
|
3178
|
-
if (live)
|
|
3412
|
+
if (live) {
|
|
3413
|
+
const invocation = INVOCATIONS.get(event);
|
|
3414
|
+
return encodeLiveResult(result, headers, status, codec, request, scope, invocation && invocation.broadcast && invocation.shareKey !== undefined ? invocation.id + "\0" + invocation.shareKey : undefined);
|
|
3415
|
+
}
|
|
3179
3416
|
return encodeResult(result, headers, status, codec, request.signal, scope);
|
|
3180
3417
|
} catch (x) {
|
|
3181
3418
|
const respondThrown = value => {
|
|
@@ -8,6 +8,7 @@ export declare function morph(parent: any, fragment: any, claim: any, ranges: an
|
|
|
8
8
|
/** Land each displaced range a morph pass left in `ranges` where its
|
|
9
9
|
* occurrence now is under `roots` (see `flushGrafts`). @internal */
|
|
10
10
|
export declare function graft(roots: any, ranges: any): void;
|
|
11
|
+
export declare function morphAttributes(oldEl: any, newEl: any, claim: any): void;
|
|
11
12
|
/**
|
|
12
13
|
* Reconcile the children of `parent` toward the children of `source`, reusing
|
|
13
14
|
* existing DOM in place. `source` is a freshly parsed, disposable node whose
|