@solidjs/web 2.0.0-rc.13 → 2.0.0-rc.15

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.
Files changed (59) hide show
  1. package/dist/server.dev.js +317 -185
  2. package/dist/server.js +298 -161
  3. package/dist/server.observe.js +299 -171
  4. package/dist/web.dev.js +207 -189
  5. package/dist/web.js +180 -178
  6. package/dist/web.observe.js +180 -180
  7. package/frames/dist/assets.js +92 -0
  8. package/frames/dist/bind.dev.js +326 -0
  9. package/frames/dist/bind.js +295 -0
  10. package/frames/dist/client.dev.js +665 -1114
  11. package/frames/dist/client.js +630 -1087
  12. package/frames/dist/client.observe.js +629 -1087
  13. package/frames/dist/regions.js +130 -0
  14. package/frames/dist/server.dev.js +400 -201
  15. package/frames/dist/server.js +397 -198
  16. package/frames/dist/server.observe.js +397 -198
  17. package/frames/dist/trace.js +45 -0
  18. package/frames/dist/wire.js +133 -0
  19. package/package.json +30 -4
  20. package/performance-tracks/dist/performance-tracks.dev.js +2 -1
  21. package/performance-tracks/dist/performance-tracks.observe.js +2 -1
  22. package/performance-tracks/types/index.d.ts +8 -6
  23. package/serialization/dist/decode.js +38 -9
  24. package/serialization/dist/serialization.js +40 -11
  25. package/serialization/types/serializer-decode.d.ts +24 -2
  26. package/server-functions/dist/client.dev.js +101 -62
  27. package/server-functions/dist/client.js +101 -62
  28. package/server-functions/dist/client.observe.js +101 -62
  29. package/server-functions/dist/server.dev.js +178 -67
  30. package/server-functions/dist/server.js +178 -67
  31. package/server-functions/dist/server.observe.js +178 -67
  32. package/skills/server-components/SKILL.md +65 -43
  33. package/types/client.d.ts +120 -8
  34. package/types/constants.d.ts +3 -1
  35. package/types/diagnostics.d.ts +8 -0
  36. package/types/frames/assets-tier.d.ts +78 -0
  37. package/types/frames/bind-tier.d.ts +60 -0
  38. package/types/frames/client.d.ts +37 -3
  39. package/types/frames/frame-client.d.ts +249 -78
  40. package/types/frames/frame-container-plugin.d.ts +2 -2
  41. package/types/frames/frame-sink.d.ts +33 -0
  42. package/types/frames/frame-transport.d.ts +66 -18
  43. package/types/frames/regions-tier.d.ts +45 -0
  44. package/types/frames/serializer-decode.d.ts +24 -2
  45. package/types/frames/server.d.ts +33 -9
  46. package/types/frames/trace-tier.d.ts +2 -0
  47. package/types/frames/wire-tier.d.ts +116 -0
  48. package/types/head.d.ts +1 -0
  49. package/types/index.d.ts +61 -16
  50. package/types/index.server.d.ts +3 -18
  51. package/types/jsx.d.ts +4 -13
  52. package/types/observe.d.ts +4 -2
  53. package/types/response.d.ts +26 -4
  54. package/types/serializer-decode.d.ts +24 -2
  55. package/types/server-functions/client.d.ts +30 -0
  56. package/types/server-functions/registry.d.ts +4 -1
  57. package/types/server-functions/shared.d.ts +12 -8
  58. package/types/server-mock.d.ts +22 -3
  59. package/types/server.d.ts +119 -17
@@ -1,9 +1,9 @@
1
1
  import { sharedConfig, ssrSanitizeError, creationStamp, ssrHandleError, runInServerComponentScope, shareAsyncIterable, getProjectionTrace } from 'solid-js/internal';
2
- import { NoHydration, getOwner, runWithOwner, createMemo, Hydration, createOwner } from 'solid-js';
3
- import { isResponseEnvelope, renderToStream, createLiveHoles, CLAIMS_STREAM, CLAIMS_DOCUMENT, SLOT_FACE_DATA, slotValue, SLOT_FACE_STREAM, SLOT_FACE_MARKUP, isSlotValue } from '@solidjs/web';
2
+ import { getOwner, NoHydration, runWithOwner, createMemo, Hydration, createOwner } from 'solid-js';
3
+ import { isResponseEnvelope, renderToStream, createLiveHoles, CLAIMS_STREAM, CLAIMS_DOCUMENT, SLOT_FACE_DATA, SLOT_FACE_MARKUP, slotValue, SLOT_FACE_STREAM, isSlotValue } from '@solidjs/web';
4
4
  import { Feature, createStream, toCrossJSONStream } from 'seroval';
5
5
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
6
- import { textDigest, frameAddress, getEventServerFunctionInvocation, armLiveBody, createEventChunk, createChunk, ChunkReader, serializeStream, guardFailures } from '@solidjs/web/server-functions/server';
6
+ import { frameAddress, getEventServerFunctionInvocation, ChunkReader, serializeStream, guardFailures, armLiveBody, createEventChunk, createChunk, textDigest } from '@solidjs/web/server-functions/server';
7
7
 
8
8
  const DESCEND = Symbol("descend");
9
9
  const CYCLE = {};
@@ -132,17 +132,15 @@ function toBorderForm(value, envelopeContainers) {
132
132
  }
133
133
  function isShareableIterable(value) {
134
134
  try {
135
- if (value.__SEROVAL_STREAM__ === true) return false;
136
- if (typeof ReadableStream !== "undefined" && value instanceof ReadableStream) return false;
137
- return typeof value[Symbol.asyncIterator] === "function";
135
+ return typeof value[Symbol.asyncIterator] === "function" && !(typeof ReadableStream !== "undefined" && value instanceof ReadableStream);
138
136
  } catch {
139
137
  return false;
140
138
  }
141
139
  }
142
- function materialize(marker) {
140
+ function materialize(marker, claiming) {
143
141
  let value = state.materialized.get(marker.$tr);
144
142
  if (value === undefined) {
145
- value = state.materializeTrace(marker);
143
+ value = state.materializeTrace(marker, claiming);
146
144
  state.materialized.set(marker.$tr, value);
147
145
  if (value !== null && typeof value === "object") state.materializedValues.add(value);
148
146
  }
@@ -200,14 +198,14 @@ setContainerTraceStreamMint(iterable => {
200
198
  })();
201
199
  return stream;
202
200
  });
203
- const DEFAULT_WEB_PLUGINS = Object.freeze([AbortSignalPlugin,
201
+ const DEFAULT_WEB_PLUGINS = /* @__PURE__ */Object.freeze([AbortSignalPlugin,
204
202
  CustomEventPlugin, DOMExceptionPlugin, EventPlugin,
205
203
  FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin,
206
204
  ContainerTracePlugin]);
207
205
  function resolveSerializerPlugins(customPlugins) {
208
206
  return customPlugins ? [...customPlugins, ...DEFAULT_WEB_PLUGINS] : [...DEFAULT_WEB_PLUGINS];
209
207
  }
210
- const JSON_CODEC_DISABLED_FEATURES = Feature.RegExp;
208
+ const jsonCodecDisabledFeatures = () => Feature.RegExp;
211
209
  const JSON_CODEC_DEPTH_LIMIT = 64;
212
210
  function resolveCodecOptions({
213
211
  plugins,
@@ -217,13 +215,12 @@ function resolveCodecOptions({
217
215
  } = {}) {
218
216
  return {
219
217
  plugins: resolveSerializerPlugins(plugins),
220
- disabledFeatures: disabledFeatures === undefined ? JSON_CODEC_DISABLED_FEATURES : disabledFeatures,
218
+ disabledFeatures: disabledFeatures === undefined ? jsonCodecDisabledFeatures() : disabledFeatures,
221
219
  depthLimit: depthLimit === undefined ? JSON_CODEC_DEPTH_LIMIT : depthLimit,
222
220
  serializeErrorStacks
223
221
  };
224
222
  }
225
223
 
226
- Feature.AggregateError | Feature.BigIntTypedArray;
227
224
  const serializeOnlyDisabledFeatures = serializeErrorStacks => (serializeErrorStacks === undefined ? process.env.NODE_ENV === "development" : serializeErrorStacks) ? 0 : Feature.ErrorPrototypeStack;
228
225
  function createJSONSerializer({
229
226
  onData,
@@ -350,6 +347,7 @@ function observeFrame(frame) {
350
347
  }
351
348
 
352
349
  const FRAME_STREAM_HEADER = "X-Frame-Stream";
350
+ const FRAME_TIERS_HEADER = "X-Frame-Tiers";
353
351
  const FRAME_HAVE_HEADER = "X-Frame-Have";
354
352
  const FRAME_HAVE_BUDGET = 4096;
355
353
  function decodeHaveList(text) {
@@ -480,11 +478,27 @@ function withHoles(chunk, holes) {
480
478
  }
481
479
  return chunk;
482
480
  }
483
- function createFrameSink(emit, frame, have) {
481
+ function createFrameSink(write, frame, have, hooks) {
484
482
  const {
485
483
  id,
486
484
  version
487
485
  } = frame;
486
+ const tiers = new Set();
487
+ let staged = [];
488
+ const emit = chunk => {
489
+ if (swept) swept.emitted = true;
490
+ if (staged.length) {
491
+ chunk.tiers = staged;
492
+ staged = [];
493
+ }
494
+ write(chunk);
495
+ };
496
+ const needs = name => {
497
+ if (!tiers.has(name)) {
498
+ tiers.add(name);
499
+ staged.push(name);
500
+ }
501
+ };
488
502
  let conditional = false;
489
503
  const skipped = new Set();
490
504
  const attrText = new Map();
@@ -524,15 +538,38 @@ function createFrameSink(emit, frame, have) {
524
538
  let sweepScheduled = false;
525
539
  let closed = false;
526
540
  let epoch = 0;
541
+ let swept = null;
527
542
  const sweep = () => {
528
543
  epoch++;
529
- for (const b of [...bindings.values()]) {
530
- try {
531
- b.sweep();
532
- } catch (_) {
544
+ const pass = swept = {
545
+ ops: [],
546
+ emitted: false
547
+ };
548
+ try {
549
+ for (const b of [...bindings.values()]) {
550
+ try {
551
+ b.sweep();
552
+ } catch (_) {
553
+ }
533
554
  }
555
+ emitOps(pass.ops);
556
+ if (pass.emitted && hooks && hooks.onYield) hooks.onYield();
557
+ } finally {
558
+ swept = null;
534
559
  }
535
560
  };
561
+ const emitOps = ops => {
562
+ if (ops.length === 1) emit(Object.assign({
563
+ type: ops[0].type,
564
+ id,
565
+ version
566
+ }, ops[0]));else if (ops.length) emit({
567
+ type: "ops",
568
+ id,
569
+ version,
570
+ ops
571
+ });
572
+ };
536
573
  const scheduleSweep = () => {
537
574
  if (closed || sweepScheduled || !bindings.size) return;
538
575
  sweepScheduled = true;
@@ -576,6 +613,7 @@ function createFrameSink(emit, frame, have) {
576
613
  chunk.preloads.push(wirePreload(entry));
577
614
  }
578
615
  }
616
+ needs("assets");
579
617
  emit(chunk);
580
618
  }
581
619
  emit(withHoles({
@@ -651,6 +689,7 @@ function createFrameSink(emit, frame, have) {
651
689
  attrs: e.attrs
652
690
  }));
653
691
  }
692
+ needs("assets");
654
693
  emit(chunk);
655
694
  }
656
695
  emit(withHoles({
@@ -708,6 +747,7 @@ function createFrameSink(emit, frame, have) {
708
747
  },
709
748
  asset(type, value) {
710
749
  if (type === "module") {
750
+ needs("assets");
711
751
  emit({
712
752
  type: "assets",
713
753
  id,
@@ -716,6 +756,7 @@ function createFrameSink(emit, frame, have) {
716
756
  modules: [value]
717
757
  });
718
758
  } else if (type === "preload") {
759
+ needs("assets");
719
760
  emit({
720
761
  type: "assets",
721
762
  id,
@@ -725,14 +766,17 @@ function createFrameSink(emit, frame, have) {
725
766
  });
726
767
  }
727
768
  },
728
- end() {
729
- if (bindings.size) sweep();
769
+ end(bound) {
770
+ if (closed) return;
771
+ if (bindings.size && !swept) sweep();
730
772
  closed = true;
731
- emit({
773
+ const chunk = {
732
774
  type: "complete",
733
775
  id,
734
776
  version
735
- });
777
+ };
778
+ if (bound) chunk.bound = bound;
779
+ emit(chunk);
736
780
  },
737
781
  error(errorId, error) {
738
782
  emit({
@@ -753,6 +797,7 @@ function createFrameSink(emit, frame, have) {
753
797
  });
754
798
  },
755
799
  region(childId, html) {
800
+ needs("regions");
756
801
  emit({
757
802
  type: "html",
758
803
  id: childId,
@@ -761,26 +806,23 @@ function createFrameSink(emit, frame, have) {
761
806
  });
762
807
  },
763
808
  hole(key, html) {
764
- emit({
809
+ const op = {
765
810
  type: "hole",
766
- id,
767
- version,
768
811
  key,
769
812
  html,
770
813
  digest: textDigest(html)
771
- });
814
+ };
815
+ swept ? swept.ops.push(op) : emitOps([op]);
772
816
  },
773
817
  attr(key, attrs, removed) {
774
- const chunk = {
818
+ const op = {
775
819
  type: "attr",
776
- id,
777
- version,
778
820
  key,
779
821
  attrs,
780
822
  digest: textDigest(attrs)
781
823
  };
782
- if (removed && removed.length) chunk.removed = removed;
783
- emit(chunk);
824
+ if (removed && removed.length) op.removed = removed;
825
+ swept ? swept.ops.push(op) : emitOps([op]);
784
826
  },
785
827
  attrBaseline(key, text) {
786
828
  attrText.set(key, text);
@@ -802,12 +844,14 @@ function createFrameSink(emit, frame, have) {
802
844
  commit: scheduleSweep,
803
845
  get epoch() {
804
846
  return epoch;
847
+ },
848
+ needs,
849
+ announce() {
850
+ staged = [];
851
+ return [...tiers];
805
852
  }
806
853
  };
807
854
  }
808
- function renderToFrameStream(code, options = {}) {
809
- return frameStream(() => code, options);
810
- }
811
855
  function renderServerComponent(component, options = {}) {
812
856
  return frameStream((sink, frame) => {
813
857
  const props = createSlotProps(sink, frame);
@@ -823,6 +867,10 @@ function renderServerComponent(component, options = {}) {
823
867
  };
824
868
  }, options);
825
869
  }
870
+ const DEFAULT_MAX_YIELDS = 64;
871
+ const DEFAULT_MAX_DURATION_MS = 30_000;
872
+ const DISCONNECTED = Symbol("solid.frames.disconnected");
873
+ const ANNOUNCE = Symbol("solid.frames.announce");
826
874
  function frameStream(makeCode, options) {
827
875
  const {
828
876
  id = "",
@@ -832,30 +880,77 @@ function frameStream(makeCode, options) {
832
880
  id,
833
881
  version
834
882
  };
883
+ const bounded = !options.live;
884
+ const maxYields = bounded ? options.maxYields ?? DEFAULT_MAX_YIELDS : 0;
885
+ const maxDurationMs = bounded ? options.maxDurationMs ?? DEFAULT_MAX_DURATION_MS : 0;
835
886
  function stream(w) {
836
887
  const observation = observeFrame(frame);
837
- const emit = observation ? chunk => {
838
- observation.chunk(chunk);
888
+ const render = new AbortController();
889
+ const upstream = options.signal;
890
+ let ended = false;
891
+ let flushed = false;
892
+ let yields = 0;
893
+ let timer;
894
+ function finish() {
895
+ if (ended) return;
896
+ ended = true;
897
+ if (timer) clearTimeout(timer);
898
+ if (upstream) upstream.removeEventListener("abort", onSignal);
899
+ w.end && w.end();
900
+ }
901
+ function cut(bound) {
902
+ if (ended) return;
903
+ sink.end(bound);
904
+ observation && observation.settle("complete");
905
+ finish();
906
+ render.abort({
907
+ quiet: true,
908
+ bound
909
+ });
910
+ }
911
+ function onSignal() {
912
+ const gone = upstream.reason && upstream.reason[DISCONNECTED];
913
+ if (bounded && flushed && !gone) return cut("time");
914
+ finish();
915
+ render.abort(upstream.reason);
916
+ }
917
+ const emit = chunk => {
918
+ if (observation) observation.chunk(chunk);
839
919
  w.write(chunk);
840
- } : chunk => w.write(chunk);
841
- const sink = createFrameSink(emit, frame, options.resume && options.resume.have);
920
+ if (bounded && !flushed && chunk.type === "html" && chunk.id === id) {
921
+ flushed = true;
922
+ if (maxDurationMs > 0 && maxDurationMs !== Infinity) timer = setTimeout(() => cut("time"), maxDurationMs);
923
+ }
924
+ };
925
+ const sink = createFrameSink(emit, frame, options.resume && options.resume.have, bounded ? {
926
+ onYield() {
927
+ if (flushed && maxYields > 0 && ++yields >= maxYields) cut("yields");
928
+ }
929
+ } : undefined);
930
+ if (options.live) sink.needs("wire");
842
931
  w.write({
843
932
  type: "start",
844
933
  id,
845
934
  version
846
935
  });
936
+ if (upstream && upstream.aborted) return finish();
937
+ if (upstream) upstream.addEventListener("abort", onSignal, {
938
+ once: true
939
+ });
847
940
  const code = makeCode(sink, frame);
848
941
  try {
849
942
  renderToStream(() => serverOwned(code), {
850
943
  serializer: createJSONSerializer,
851
944
  ...options,
945
+ signal: render.signal,
852
946
  sink
853
947
  }).pipe({
854
948
  write() {},
855
949
  end() {
950
+ if (ended) return;
856
951
  sink.end();
857
952
  observation && observation.settle("complete");
858
- w.end && w.end();
953
+ finish();
859
954
  }
860
955
  });
861
956
  } catch (err) {
@@ -863,8 +958,9 @@ function frameStream(makeCode, options) {
863
958
  sink.error("", wire instanceof Error ? wire.message : String(wire));
864
959
  sink.end();
865
960
  observation && observation.settle("error", err);
866
- w.end && w.end();
961
+ finish();
867
962
  }
963
+ if (w[ANNOUNCE]) w[ANNOUNCE](sink.announce());
868
964
  }
869
965
  return {
870
966
  pipe: stream,
@@ -896,21 +992,25 @@ function isRangeKey(key) {
896
992
  const c = key.charCodeAt(0);
897
993
  return c === 36 || c >= 48 && c <= 57 || RANGE_KEYS.has(key);
898
994
  }
899
- function slotProxy(range, occurrence, face, content, onData) {
995
+ function slotProxy(range, occurrence, face, content, onData, needs) {
900
996
  return new Proxy(range, {
901
997
  get(target, key, receiver) {
902
998
  if (typeof key !== "string" || isRangeKey(key)) return Reflect.get(target, key, receiver);
903
999
  if (onData) onData = void onData();
1000
+ if (face !== SLOT_FACE_MARKUP) needs("bind");
904
1001
  return slotValue(occurrence, key, content ? content[key] : undefined, face);
905
1002
  }
906
1003
  });
907
1004
  }
908
- function argBorderForm(value, key, occurrence) {
909
- return toBorderForm(withoutStandIns(value));
1005
+ function argBorderForm(value, key, occurrence, needs) {
1006
+ return toBorderForm(withoutStandIns(value, key, occurrence, needs));
910
1007
  }
911
- function withoutStandIns(value, key, occurrence) {
1008
+ function withoutStandIns(value, key, occurrence, needs) {
912
1009
  return rewriteTree(value, (v, path) => {
913
- if (isContainerTraced(v)) return v;
1010
+ if (isContainerTraced(v)) {
1011
+ needs("trace");
1012
+ return v;
1013
+ }
914
1014
  if (Object.getPrototypeOf(v) === Object.prototype && isSlotValue(v)) {
915
1015
  return undefined;
916
1016
  }
@@ -1031,7 +1131,7 @@ function createDocumentSlotProps(clientProps, frameId) {
1031
1131
  r.$occurrence = occurrence;
1032
1132
  const face = slotFace(r, content);
1033
1133
  r.$face = face;
1034
- return slotProxy(r, occurrence, face, face === SLOT_FACE_DATA ? content : undefined);
1134
+ return slotProxy(r, occurrence, face, face === SLOT_FACE_DATA ? content : undefined, undefined, documentNeeds);
1035
1135
  };
1036
1136
  const zoneOwner = getOwner ? getOwner() : null;
1037
1137
  const scoped = (occurrence, render) => {
@@ -1072,136 +1172,151 @@ function createDocumentSlotProps(clientProps, frameId) {
1072
1172
  const occurrence = occurrenceId(prop, raw, counts);
1073
1173
  const slot = clientProps[prop];
1074
1174
  if (typeof slot !== "function") return range(occurrence, undefined);
1075
- const resolved = {};
1076
- const liveArgs = sharedConfig.context && sharedConfig.context.live && sharedConfig.context.live.args;
1077
- const vals = {};
1078
- const evals = {};
1079
- const states = {};
1080
- const minted = {};
1081
- for (const key of Object.keys(raw)) {
1082
- if (key === "$key") continue;
1083
- const desc = Object.getOwnPropertyDescriptor(raw, key);
1084
- let evaluate = null;
1085
- let value;
1086
- if (desc.get) {
1087
- const get = desc.get;
1088
- evaluate = () => unwrapThunks(get.call(raw));
1089
- } else {
1090
- value = desc.value;
1091
- if (typeof value === "function") {
1092
- const fn = value;
1093
- evaluate = () => unwrapThunks(fn);
1175
+ const context = sharedConfig.context;
1176
+ const recordKey = `sc:slot:${frameId}:${occurrence}`;
1177
+ let settleRecord;
1178
+ if (context && context.async) {
1179
+ context.serialize(recordKey, new Promise(resolve => settleRecord = resolve));
1180
+ }
1181
+ try {
1182
+ const resolved = {};
1183
+ const liveArgs = sharedConfig.context && sharedConfig.context.live && sharedConfig.context.live.args;
1184
+ const vals = {};
1185
+ const evals = {};
1186
+ const states = {};
1187
+ const minted = {};
1188
+ for (const key of Object.keys(raw)) {
1189
+ if (key === "$key") continue;
1190
+ const desc = Object.getOwnPropertyDescriptor(raw, key);
1191
+ let evaluate = null;
1192
+ let value;
1193
+ if (desc.get) {
1194
+ const get = desc.get;
1195
+ evaluate = () => unwrapThunks(get.call(raw));
1196
+ } else {
1197
+ value = desc.value;
1198
+ if (typeof value === "function") {
1199
+ const fn = value;
1200
+ evaluate = () => unwrapThunks(fn);
1201
+ }
1202
+ }
1203
+ if (evaluate) {
1204
+ evals[key] = evaluate;
1205
+ const stampBefore = scopeStamp();
1206
+ try {
1207
+ value = evaluate();
1208
+ states[key] = {
1209
+ settled: true,
1210
+ last: value
1211
+ };
1212
+ } catch (err) {
1213
+ const blocked = ssrHandleError && ssrHandleError(err);
1214
+ if (!blocked) throw err;
1215
+ const state = states[key] = {
1216
+ settled: false,
1217
+ last: undefined
1218
+ };
1219
+ value = retryArgUntilSettled(evaluate, blocked, key, occurrence, v => {
1220
+ state.settled = true;
1221
+ state.last = v;
1222
+ if (liveArgs) liveArgs.commit();
1223
+ });
1224
+ }
1225
+ if (scopeStamp() !== stampBefore) minted[key] = true;
1094
1226
  }
1227
+ vals[key] = value;
1095
1228
  }
1096
- if (evaluate) {
1097
- evals[key] = evaluate;
1098
- const stampBefore = scopeStamp();
1099
- try {
1100
- value = evaluate();
1101
- states[key] = {
1102
- settled: true,
1103
- last: value
1229
+ const regions = [];
1230
+ for (const key of Object.keys(vals)) {
1231
+ const value = vals[key];
1232
+ if (isContainerTraced(value)) {
1233
+ documentNeeds("trace");
1234
+ resolved[key] = value;
1235
+ } else if (isServerContent(value)) {
1236
+ const childId = `${frameId}.${occurrence}.${key}`;
1237
+ const region = {
1238
+ key,
1239
+ childId,
1240
+ value,
1241
+ used: false,
1242
+ locked: false
1104
1243
  };
1105
- } catch (err) {
1106
- const blocked = ssrHandleError && ssrHandleError(err);
1107
- if (!blocked) throw err;
1108
- const state = states[key] = {
1109
- settled: false,
1110
- last: undefined
1244
+ regions.push(region);
1245
+ documentNeeds("regions");
1246
+ resolved[key] = () => {
1247
+ if (region.locked) return [];
1248
+ region.used = true;
1249
+ return [{
1250
+ t: frameElementOpen(childId)
1251
+ }, value, {
1252
+ t: FRAME_ELEMENT_CLOSE
1253
+ }];
1111
1254
  };
1112
- value = retryArgUntilSettled(evaluate, blocked, key, occurrence, v => {
1113
- state.settled = true;
1114
- state.last = v;
1115
- if (liveArgs) liveArgs.commit();
1255
+ } else if (ssrAsyncValue && isAsyncValue(value)) {
1256
+ let readable = value;
1257
+ if (typeof value.then !== "function") {
1258
+ const {
1259
+ first,
1260
+ rest
1261
+ } = tapFirstYield(value);
1262
+ readable = first;
1263
+ vals[key] = rest;
1264
+ }
1265
+ const read = ssrAsyncValue(readable);
1266
+ Object.defineProperty(resolved, key, {
1267
+ get: read,
1268
+ enumerable: true,
1269
+ configurable: true
1116
1270
  });
1271
+ } else {
1272
+ resolved[key] = vals[key] = withoutStandIns(value, key, occurrence, documentNeeds);
1117
1273
  }
1118
- if (scopeStamp() !== stampBefore) minted[key] = true;
1119
1274
  }
1120
- vals[key] = value;
1121
- }
1122
- const regions = [];
1123
- for (const key of Object.keys(vals)) {
1124
- const value = vals[key];
1125
- if (isContainerTraced(value)) {
1126
- resolved[key] = value;
1127
- } else if (isServerContent(value)) {
1128
- const childId = `${frameId}.${occurrence}.${key}`;
1129
- const region = {
1130
- key,
1131
- childId,
1132
- value,
1133
- used: false,
1134
- locked: false
1135
- };
1136
- regions.push(region);
1137
- resolved[key] = () => {
1138
- if (region.locked) return [];
1139
- region.used = true;
1140
- return [{
1141
- t: frameElementOpen(childId)
1142
- }, value, {
1143
- t: FRAME_ELEMENT_CLOSE
1144
- }];
1145
- };
1146
- } else if (ssrAsyncValue && isAsyncValue(value)) {
1147
- let readable = value;
1148
- if (typeof value.then !== "function") {
1149
- const {
1150
- first,
1151
- rest
1152
- } = tapFirstYield(value);
1153
- readable = first;
1154
- vals[key] = rest;
1275
+ const out = suppressedFill(() => scoped(occurrence, () => range(occurrence, slot(resolved))));
1276
+ const unused = regions.filter(r => !r.used);
1277
+ if (context) {
1278
+ const args = {};
1279
+ for (const key of Object.keys(vals)) {
1280
+ const value = vals[key];
1281
+ const region = regions.find(r => r.key === key);
1282
+ if (region) {
1283
+ args[key] = {
1284
+ $frame: region.childId
1285
+ };
1286
+ continue;
1287
+ }
1288
+ if (!isContainerTraced(value) && isServerContent(value)) continue;
1289
+ args[key] = argBorderForm(value, key, occurrence, documentNeeds);
1155
1290
  }
1156
- const read = ssrAsyncValue(readable);
1157
- Object.defineProperty(resolved, key, {
1158
- get: read,
1159
- enumerable: true,
1160
- configurable: true
1291
+ settleRecord ? settleRecord({
1292
+ ...args
1293
+ }) : context.serialize(recordKey, {
1294
+ ...args
1161
1295
  });
1162
- } else {
1163
- resolved[key] = vals[key] = withoutStandIns(value);
1164
- }
1165
- }
1166
- const out = suppressedFill(() => scoped(occurrence, () => range(occurrence, slot(resolved))));
1167
- const unused = regions.filter(r => !r.used);
1168
- if (sharedConfig.context) {
1169
- const args = {};
1170
- for (const key of Object.keys(vals)) {
1171
- const value = vals[key];
1172
- const region = regions.find(r => r.key === key);
1173
- if (region) {
1174
- args[key] = {
1175
- $frame: region.childId
1176
- };
1177
- continue;
1296
+ for (const region of unused) {
1297
+ region.locked = true;
1298
+ context.serialize(`sc:region:${region.childId}`, resolveRegionHtml(context, region.value));
1178
1299
  }
1179
- if (!isContainerTraced(value) && isServerContent(value)) continue;
1180
- args[key] = argBorderForm(value);
1181
- }
1182
- sharedConfig.context.serialize(`sc:slot:${frameId}:${occurrence}`, {
1183
- ...args
1184
- });
1185
- for (const region of unused) {
1186
- region.locked = true;
1187
- sharedConfig.context.serialize(`sc:region:${region.childId}`, resolveRegionHtml(sharedConfig.context, region.value));
1188
- }
1189
- if (liveArgs) {
1190
- for (const key of Object.keys(evals)) {
1191
- if (regions.some(r => r.key === key)) continue;
1192
- if (minted[key]) continue;
1193
- const ledgerKey = `${frameId}:${occurrence}:${key}`;
1194
- openArgBinding(liveArgs, ledgerKey, occurrence, key, evals[key], states[key], value => {
1195
- args[key] = argBorderForm(value);
1196
- liveArgs.slot(frameId, occurrence, {
1197
- ...args
1300
+ if (liveArgs) {
1301
+ for (const key of Object.keys(evals)) {
1302
+ if (regions.some(r => r.key === key)) continue;
1303
+ if (minted[key]) continue;
1304
+ const ledgerKey = `${frameId}:${occurrence}:${key}`;
1305
+ openArgBinding(liveArgs, ledgerKey, occurrence, key, evals[key], states[key], value => {
1306
+ args[key] = argBorderForm(value, key, occurrence, documentNeeds);
1307
+ liveArgs.slot(frameId, occurrence, {
1308
+ ...args
1309
+ });
1198
1310
  });
1199
- });
1311
+ }
1200
1312
  }
1201
1313
  }
1314
+ if (rk !== undefined && (rk === occurrence || out.$face === SLOT_FACE_DATA)) repeats.has(rk) || repeats.set(rk, out);
1315
+ return out;
1316
+ } catch (error) {
1317
+ settleRecord && settleRecord(undefined);
1318
+ throw error;
1202
1319
  }
1203
- if (rk !== undefined && (rk === occurrence || out.$face === SLOT_FACE_DATA)) repeats.has(rk) || repeats.set(rk, out);
1204
- return out;
1205
1320
  };
1206
1321
  fn.$lhSkip = true;
1207
1322
  getters.set(prop, fn);
@@ -1216,7 +1331,48 @@ function frameElementOpen(id) {
1216
1331
  const escaped = sharedConfig.context ? sharedConfig.context.escape(String(id), true) : String(id);
1217
1332
  return `<${FRAME_TAG} ${FRAME_ID_ATTR}="${escaped}" style="display:contents">`;
1218
1333
  }
1334
+ const DOCUMENT_SITES = Symbol();
1335
+ function contextSites(ctx) {
1336
+ if (ctx[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES];
1337
+ const proto = Object.getPrototypeOf(ctx);
1338
+ if (proto && proto[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES] = proto[DOCUMENT_SITES];
1339
+ if (proto && typeof proto.escape === "function") return ctx[DOCUMENT_SITES] = contextSites(proto);
1340
+ return ctx[DOCUMENT_SITES] = {
1341
+ byOwner: new Map(),
1342
+ counts: Object.create(null)
1343
+ };
1344
+ }
1345
+ function mintSiteId(sites, fnId) {
1346
+ const n = sites.counts[fnId] || 0;
1347
+ sites.counts[fnId] = n + 1;
1348
+ return n === 0 ? fnId : fnId + "~" + (n + 1);
1349
+ }
1350
+ function documentSiteId(fnId) {
1351
+ const ctx = sharedConfig.context;
1352
+ if (!ctx) return fnId;
1353
+ const sites = contextSites(ctx);
1354
+ const owner = getOwner ? getOwner() : null;
1355
+ if (!owner) return mintSiteId(sites, fnId);
1356
+ let byFn = sites.byOwner.get(owner);
1357
+ if (!byFn) sites.byOwner.set(owner, byFn = new Map());
1358
+ const prev = byFn.get(fnId);
1359
+ if (prev) return prev;
1360
+ const siteId = mintSiteId(sites, fnId);
1361
+ byFn.set(fnId, siteId);
1362
+ return siteId;
1363
+ }
1219
1364
  const FRAME_ELEMENT_CLOSE = `</${FRAME_TAG}>`;
1365
+ function documentNeeds(name) {
1366
+ const ctx = sharedConfig.context;
1367
+ const live = ctx && ctx.live;
1368
+ if (!live || !ctx.serialize) return;
1369
+ const tiers = live.tiers || (live.tiers = []);
1370
+ if (tiers.includes(name)) return;
1371
+ tiers.push(name);
1372
+ ctx.serialize("sc:tiers", tiers.slice());
1373
+ const url = live.tierUrls && live.tierUrls[name];
1374
+ if (url && ctx.registerAsset) ctx.registerAsset("module", url);
1375
+ }
1220
1376
  function armDocumentLiveHoles(ctx) {
1221
1377
  if (!ctx || ctx.liveHoles !== undefined) return;
1222
1378
  const live = ctx.live;
@@ -1237,14 +1393,27 @@ function armDocumentLiveHoles(ctx) {
1237
1393
  let epoch = 0;
1238
1394
  let sweepScheduled = false;
1239
1395
  let closed = false;
1396
+ let swept = null;
1240
1397
  const sweep = () => {
1241
1398
  epoch++;
1242
- for (const b of [...bindings.values()]) {
1243
- try {
1244
- b.sweep();
1245
- } catch (_) {
1399
+ const ops = swept = [];
1400
+ try {
1401
+ for (const b of [...bindings.values()]) {
1402
+ try {
1403
+ b.sweep();
1404
+ } catch (_) {
1405
+ }
1246
1406
  }
1407
+ } finally {
1408
+ swept = null;
1247
1409
  }
1410
+ pushOps(ops);
1411
+ };
1412
+ const pushOps = ops => {
1413
+ if (ops.length === 1) push(ops[0]);else if (ops.length) push({
1414
+ type: "ops",
1415
+ ops
1416
+ });
1248
1417
  };
1249
1418
  const scheduleSweep = () => {
1250
1419
  if (closed || sweepScheduled || !bindings.size) return;
@@ -1258,6 +1427,9 @@ function armDocumentLiveHoles(ctx) {
1258
1427
  ctx.serialize("sc:live", new ReadableStream({
1259
1428
  start(c) {
1260
1429
  channel = c;
1430
+ },
1431
+ cancel() {
1432
+ closed = true;
1261
1433
  }
1262
1434
  }));
1263
1435
  const push = op => {
@@ -1271,12 +1443,13 @@ function armDocumentLiveHoles(ctx) {
1271
1443
  bindings.delete(key);
1272
1444
  },
1273
1445
  hole(key, html) {
1274
- push({
1446
+ const op = {
1275
1447
  type: "hole",
1276
1448
  key,
1277
1449
  html,
1278
1450
  digest: textDigest(html)
1279
- });
1451
+ };
1452
+ swept ? swept.push(op) : pushOps([op]);
1280
1453
  },
1281
1454
  attr(key, attrs, removed) {
1282
1455
  const op = {
@@ -1286,7 +1459,7 @@ function armDocumentLiveHoles(ctx) {
1286
1459
  digest: textDigest(attrs)
1287
1460
  };
1288
1461
  if (removed && removed.length) op.removed = removed;
1289
- push(op);
1462
+ swept ? swept.push(op) : pushOps([op]);
1290
1463
  },
1291
1464
  error(key, error) {
1292
1465
  push({
@@ -1319,6 +1492,13 @@ function armDocumentLiveHoles(ctx) {
1319
1492
  },
1320
1493
  commit: scheduleSweep
1321
1494
  };
1495
+ live.error = (fid, error) => {
1496
+ push({
1497
+ type: "error",
1498
+ fid,
1499
+ error
1500
+ });
1501
+ };
1322
1502
  live.end = () => {
1323
1503
  if (closed) return;
1324
1504
  if (bindings.size) sweep();
@@ -1328,28 +1508,36 @@ function armDocumentLiveHoles(ctx) {
1328
1508
  }
1329
1509
  function frameTransformDirectResult(value, {
1330
1510
  id,
1331
- args
1511
+ args,
1512
+ tierUrls
1332
1513
  }) {
1333
1514
  if (typeof value !== "function") return value;
1334
1515
  const component = value;
1335
- const wrapped = props => [{
1336
- t: frameElementOpen(id)
1337
- },
1338
- serverOwned(() => {
1339
- const page = sharedConfig.context;
1340
- armDocumentLiveHoles(page);
1341
- const ctx = Object.create(page);
1342
- ctx.claims = CLAIMS_DOCUMENT;
1343
- sharedConfig.context = ctx;
1344
- try {
1345
- const slotProps = createDocumentSlotProps(props, id);
1346
- return serverComponentScope(() => component(slotProps), !!wrapped[LIVE_SOURCE]);
1347
- } finally {
1348
- sharedConfig.context = page;
1349
- }
1350
- }), {
1351
- t: FRAME_ELEMENT_CLOSE
1352
- }];
1516
+ const wrapped = props => {
1517
+ const siteId = documentSiteId(id);
1518
+ return [{
1519
+ t: frameElementOpen(siteId)
1520
+ },
1521
+ serverOwned(() => {
1522
+ const page = sharedConfig.context;
1523
+ armDocumentLiveHoles(page);
1524
+ if (tierUrls && page && page.live && !page.live.tierUrls) page.live.tierUrls = tierUrls;
1525
+ const ctx = Object.create(page);
1526
+ ctx.claims = CLAIMS_DOCUMENT;
1527
+ ctx.frameId = siteId;
1528
+ sharedConfig.context = ctx;
1529
+ try {
1530
+ const slotProps = createDocumentSlotProps(props, siteId);
1531
+ const live = !!wrapped[LIVE_SOURCE];
1532
+ if (live) documentNeeds("wire");
1533
+ return serverComponentScope(() => component(slotProps), live);
1534
+ } finally {
1535
+ sharedConfig.context = page;
1536
+ }
1537
+ }), {
1538
+ t: FRAME_ELEMENT_CLOSE
1539
+ }];
1540
+ };
1353
1541
  wrapped[SERVER_COMPONENT] = id;
1354
1542
  wrapped[SERVER_COMPONENT_SOURCE] = component;
1355
1543
  wrapped[SERVER_COMPONENT_ADDRESS] = frameAddress(id, args);
@@ -1455,6 +1643,7 @@ function createSlotProps(sink, frame) {
1455
1643
  const counts = Object.create(null);
1456
1644
  const getters = new Map();
1457
1645
  const repeats = new Map();
1646
+ const needs = sink.needs || (() => {});
1458
1647
  return new Proxy(Object.create(null), {
1459
1648
  has() {
1460
1649
  return true;
@@ -1466,7 +1655,7 @@ function createSlotProps(sink, frame) {
1466
1655
  if (!fn) {
1467
1656
  fn = (...callArgs) => {
1468
1657
  if (callArgs.length === 0 || callArgs[0] === undefined) {
1469
- return slotProxy(slotRange(prop), prop, SLOT_FACE_STREAM, undefined);
1658
+ return slotProxy(slotRange(prop), prop, SLOT_FACE_STREAM, undefined, undefined, needs);
1470
1659
  }
1471
1660
  const rk = repeatKey(prop, callArgs[0]);
1472
1661
  const repeat = rk !== undefined && repeats.get(rk);
@@ -1547,7 +1736,7 @@ function createSlotProps(sink, frame) {
1547
1736
  };
1548
1737
  } else {
1549
1738
  const ref = `arg:${occurrence}:${key}`;
1550
- ctx.serialize(ref, argBorderForm(value, key, occurrence));
1739
+ ctx.serialize(ref, argBorderForm(value, key, occurrence, needs));
1551
1740
  args[key] = {
1552
1741
  $ref: ref
1553
1742
  };
@@ -1579,7 +1768,7 @@ function createSlotProps(sink, frame) {
1579
1768
  } else {
1580
1769
  const ref = `arg:${occurrence}:${key}@${sink.nextArgRef(ledgerKey)}`;
1581
1770
  sink.mintRef(ref);
1582
- ctx.serialize(ref, argBorderForm(value));
1771
+ ctx.serialize(ref, argBorderForm(value, key, occurrence, needs));
1583
1772
  args[key] = {
1584
1773
  $ref: ref
1585
1774
  };
@@ -1590,7 +1779,7 @@ function createSlotProps(sink, frame) {
1590
1779
  });
1591
1780
  }
1592
1781
  const keyed = rk === occurrence;
1593
- const out = slotProxy(slotRange(occurrence), occurrence, SLOT_FACE_STREAM, undefined, rk === undefined || keyed ? undefined : () => repeats.has(rk) || repeats.set(rk, out));
1782
+ const out = slotProxy(slotRange(occurrence), occurrence, SLOT_FACE_STREAM, undefined, rk === undefined || keyed ? undefined : () => repeats.has(rk) || repeats.set(rk, out), needs);
1594
1783
  if (keyed) repeats.set(rk, out);
1595
1784
  return out;
1596
1785
  };
@@ -1637,6 +1826,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1637
1826
  });
1638
1827
  let closed = false;
1639
1828
  let stopLive = null;
1829
+ let failed = false;
1640
1830
  const body = new ReadableStream({
1641
1831
  start(controller) {
1642
1832
  const end = () => {
@@ -1649,9 +1839,6 @@ function serverComponentResponse(component, options = {}, init = {}) {
1649
1839
  } catch (_) {}
1650
1840
  };
1651
1841
  if (teardown.signal.aborted) return end();
1652
- teardown.signal.addEventListener("abort", end, {
1653
- once: true
1654
- });
1655
1842
  if (live) stopLive = armLiveBody(controller, () => {
1656
1843
  closed = true;
1657
1844
  disarm();
@@ -1659,6 +1846,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1659
1846
  });
1660
1847
  stream.pipe({
1661
1848
  write(chunk) {
1849
+ if (chunk.type === "error" && !chunk.key) failed = true;
1662
1850
  if (closed) return;
1663
1851
  try {
1664
1852
  controller.enqueue(frame(JSON.stringify(chunk)));
@@ -1666,16 +1854,25 @@ function serverComponentResponse(component, options = {}, init = {}) {
1666
1854
  closed = true;
1667
1855
  }
1668
1856
  },
1669
- end
1857
+ end,
1858
+ [ANNOUNCE](tiers) {
1859
+ if (tiers.length) headers.set(FRAME_TIERS_HEADER, tiers.join(","));
1860
+ }
1670
1861
  });
1671
1862
  },
1672
1863
  cancel() {
1673
1864
  closed = true;
1674
1865
  disarm();
1675
1866
  if (stopLive) stopLive();
1676
- teardown.abort();
1867
+ teardown.abort({
1868
+ [DISCONNECTED]: true
1869
+ });
1677
1870
  }
1678
1871
  });
1872
+ if (failed) {
1873
+ headers.set("Cache-Control", "no-store");
1874
+ for (const name of [...headers.keys()]) if (name === "surrogate-control" || name.endsWith("cdn-cache-control")) headers.delete(name);
1875
+ }
1679
1876
  return new Response(body, {
1680
1877
  status: init.status || 200,
1681
1878
  headers
@@ -1875,7 +2072,9 @@ function frameFlightResponse({
1875
2072
  cancel() {
1876
2073
  closed = true;
1877
2074
  disarm();
1878
- teardown.abort();
2075
+ teardown.abort({
2076
+ [DISCONNECTED]: true
2077
+ });
1879
2078
  }
1880
2079
  });
1881
2080
  return new Response(body, {
@@ -1890,4 +2089,4 @@ function asyncArg(value) {
1890
2089
  return value;
1891
2090
  }
1892
2091
 
1893
- export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, createFrameSink, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse, renderServerComponent, renderToFrameStream, serverComponentResponse };
2092
+ export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, FRAME_TIERS_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse };