@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
2
  import { OBSERVE, DEV, getOwner, NoHydration, 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, SLOT_VALUE } from '@solidjs/web';
3
+ import { isResponseEnvelope, renderToStream, createLiveHoles, CLAIMS_STREAM, CLAIMS_DOCUMENT, SLOT_FACE_DATA, SLOT_FACE_MARKUP, slotValue, SLOT_FACE_STREAM, isSlotValue, SLOT_VALUE } 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
  }
@@ -209,14 +207,14 @@ setContainerTraceStreamMint(iterable => {
209
207
  })();
210
208
  return stream;
211
209
  });
212
- const DEFAULT_WEB_PLUGINS = Object.freeze([AbortSignalPlugin,
210
+ const DEFAULT_WEB_PLUGINS = /* @__PURE__ */Object.freeze([AbortSignalPlugin,
213
211
  CustomEventPlugin, DOMExceptionPlugin, EventPlugin,
214
212
  FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin,
215
213
  ContainerTracePlugin]);
216
214
  function resolveSerializerPlugins(customPlugins) {
217
215
  return customPlugins ? [...customPlugins, ...DEFAULT_WEB_PLUGINS] : [...DEFAULT_WEB_PLUGINS];
218
216
  }
219
- const JSON_CODEC_DISABLED_FEATURES = Feature.RegExp;
217
+ const jsonCodecDisabledFeatures = () => Feature.RegExp;
220
218
  const JSON_CODEC_DEPTH_LIMIT = 64;
221
219
  function resolveCodecOptions({
222
220
  plugins,
@@ -226,13 +224,12 @@ function resolveCodecOptions({
226
224
  } = {}) {
227
225
  return {
228
226
  plugins: resolveSerializerPlugins(plugins),
229
- disabledFeatures: disabledFeatures === undefined ? JSON_CODEC_DISABLED_FEATURES : disabledFeatures,
227
+ disabledFeatures: disabledFeatures === undefined ? jsonCodecDisabledFeatures() : disabledFeatures,
230
228
  depthLimit: depthLimit === undefined ? JSON_CODEC_DEPTH_LIMIT : depthLimit,
231
229
  serializeErrorStacks
232
230
  };
233
231
  }
234
232
 
235
- Feature.AggregateError | Feature.BigIntTypedArray;
236
233
  const serializeOnlyDisabledFeatures = serializeErrorStacks => (serializeErrorStacks === undefined ? process.env.NODE_ENV === "development" : serializeErrorStacks) ? 0 : Feature.ErrorPrototypeStack;
237
234
  function createJSONSerializer({
238
235
  onData,
@@ -359,6 +356,7 @@ function observeFrame(frame) {
359
356
  }
360
357
 
361
358
  const FRAME_STREAM_HEADER = "X-Frame-Stream";
359
+ const FRAME_TIERS_HEADER = "X-Frame-Tiers";
362
360
  const FRAME_HAVE_HEADER = "X-Frame-Have";
363
361
  const FRAME_HAVE_BUDGET = 4096;
364
362
  function decodeHaveList(text) {
@@ -489,11 +487,27 @@ function withHoles(chunk, holes) {
489
487
  }
490
488
  return chunk;
491
489
  }
492
- function createFrameSink(emit, frame, have) {
490
+ function createFrameSink(write, frame, have, hooks) {
493
491
  const {
494
492
  id,
495
493
  version
496
494
  } = frame;
495
+ const tiers = new Set();
496
+ let staged = [];
497
+ const emit = chunk => {
498
+ if (swept) swept.emitted = true;
499
+ if (staged.length) {
500
+ chunk.tiers = staged;
501
+ staged = [];
502
+ }
503
+ write(chunk);
504
+ };
505
+ const needs = name => {
506
+ if (!tiers.has(name)) {
507
+ tiers.add(name);
508
+ staged.push(name);
509
+ }
510
+ };
497
511
  let conditional = false;
498
512
  const skipped = new Set();
499
513
  const attrText = new Map();
@@ -533,15 +547,38 @@ function createFrameSink(emit, frame, have) {
533
547
  let sweepScheduled = false;
534
548
  let closed = false;
535
549
  let epoch = 0;
550
+ let swept = null;
536
551
  const sweep = () => {
537
552
  epoch++;
538
- for (const b of [...bindings.values()]) {
539
- try {
540
- b.sweep();
541
- } catch (_) {
553
+ const pass = swept = {
554
+ ops: [],
555
+ emitted: false
556
+ };
557
+ try {
558
+ for (const b of [...bindings.values()]) {
559
+ try {
560
+ b.sweep();
561
+ } catch (_) {
562
+ }
542
563
  }
564
+ emitOps(pass.ops);
565
+ if (pass.emitted && hooks && hooks.onYield) hooks.onYield();
566
+ } finally {
567
+ swept = null;
543
568
  }
544
569
  };
570
+ const emitOps = ops => {
571
+ if (ops.length === 1) emit(Object.assign({
572
+ type: ops[0].type,
573
+ id,
574
+ version
575
+ }, ops[0]));else if (ops.length) emit({
576
+ type: "ops",
577
+ id,
578
+ version,
579
+ ops
580
+ });
581
+ };
545
582
  const scheduleSweep = () => {
546
583
  if (closed || sweepScheduled || !bindings.size) return;
547
584
  sweepScheduled = true;
@@ -585,6 +622,7 @@ function createFrameSink(emit, frame, have) {
585
622
  chunk.preloads.push(wirePreload(entry));
586
623
  }
587
624
  }
625
+ needs("assets");
588
626
  emit(chunk);
589
627
  }
590
628
  emit(withHoles({
@@ -660,6 +698,7 @@ function createFrameSink(emit, frame, have) {
660
698
  attrs: e.attrs
661
699
  }));
662
700
  }
701
+ needs("assets");
663
702
  emit(chunk);
664
703
  }
665
704
  emit(withHoles({
@@ -717,6 +756,7 @@ function createFrameSink(emit, frame, have) {
717
756
  },
718
757
  asset(type, value) {
719
758
  if (type === "module") {
759
+ needs("assets");
720
760
  emit({
721
761
  type: "assets",
722
762
  id,
@@ -725,6 +765,7 @@ function createFrameSink(emit, frame, have) {
725
765
  modules: [value]
726
766
  });
727
767
  } else if (type === "preload") {
768
+ needs("assets");
728
769
  emit({
729
770
  type: "assets",
730
771
  id,
@@ -734,14 +775,17 @@ function createFrameSink(emit, frame, have) {
734
775
  });
735
776
  }
736
777
  },
737
- end() {
738
- if (bindings.size) sweep();
778
+ end(bound) {
779
+ if (closed) return;
780
+ if (bindings.size && !swept) sweep();
739
781
  closed = true;
740
- emit({
782
+ const chunk = {
741
783
  type: "complete",
742
784
  id,
743
785
  version
744
- });
786
+ };
787
+ if (bound) chunk.bound = bound;
788
+ emit(chunk);
745
789
  },
746
790
  error(errorId, error) {
747
791
  emit({
@@ -762,6 +806,7 @@ function createFrameSink(emit, frame, have) {
762
806
  });
763
807
  },
764
808
  region(childId, html) {
809
+ needs("regions");
765
810
  emit({
766
811
  type: "html",
767
812
  id: childId,
@@ -770,26 +815,23 @@ function createFrameSink(emit, frame, have) {
770
815
  });
771
816
  },
772
817
  hole(key, html) {
773
- emit({
818
+ const op = {
774
819
  type: "hole",
775
- id,
776
- version,
777
820
  key,
778
821
  html,
779
822
  digest: textDigest(html)
780
- });
823
+ };
824
+ swept ? swept.ops.push(op) : emitOps([op]);
781
825
  },
782
826
  attr(key, attrs, removed) {
783
- const chunk = {
827
+ const op = {
784
828
  type: "attr",
785
- id,
786
- version,
787
829
  key,
788
830
  attrs,
789
831
  digest: textDigest(attrs)
790
832
  };
791
- if (removed && removed.length) chunk.removed = removed;
792
- emit(chunk);
833
+ if (removed && removed.length) op.removed = removed;
834
+ swept ? swept.ops.push(op) : emitOps([op]);
793
835
  },
794
836
  attrBaseline(key, text) {
795
837
  attrText.set(key, text);
@@ -811,12 +853,14 @@ function createFrameSink(emit, frame, have) {
811
853
  commit: scheduleSweep,
812
854
  get epoch() {
813
855
  return epoch;
856
+ },
857
+ needs,
858
+ announce() {
859
+ staged = [];
860
+ return [...tiers];
814
861
  }
815
862
  };
816
863
  }
817
- function renderToFrameStream(code, options = {}) {
818
- return frameStream(() => code, options);
819
- }
820
864
  function renderServerComponent(component, options = {}) {
821
865
  return frameStream((sink, frame) => {
822
866
  const props = createSlotProps(sink, frame);
@@ -832,6 +876,10 @@ function renderServerComponent(component, options = {}) {
832
876
  };
833
877
  }, options);
834
878
  }
879
+ const DEFAULT_MAX_YIELDS = 64;
880
+ const DEFAULT_MAX_DURATION_MS = 30_000;
881
+ const DISCONNECTED = Symbol("solid.frames.disconnected");
882
+ const ANNOUNCE = Symbol("solid.frames.announce");
835
883
  function frameStream(makeCode, options) {
836
884
  const {
837
885
  id = "",
@@ -841,30 +889,77 @@ function frameStream(makeCode, options) {
841
889
  id,
842
890
  version
843
891
  };
892
+ const bounded = !options.live;
893
+ const maxYields = bounded ? options.maxYields ?? DEFAULT_MAX_YIELDS : 0;
894
+ const maxDurationMs = bounded ? options.maxDurationMs ?? DEFAULT_MAX_DURATION_MS : 0;
844
895
  function stream(w) {
845
896
  const observation = observeFrame(frame);
846
- const emit = observation ? chunk => {
847
- observation.chunk(chunk);
897
+ const render = new AbortController();
898
+ const upstream = options.signal;
899
+ let ended = false;
900
+ let flushed = false;
901
+ let yields = 0;
902
+ let timer;
903
+ function finish() {
904
+ if (ended) return;
905
+ ended = true;
906
+ if (timer) clearTimeout(timer);
907
+ if (upstream) upstream.removeEventListener("abort", onSignal);
908
+ w.end && w.end();
909
+ }
910
+ function cut(bound) {
911
+ if (ended) return;
912
+ sink.end(bound);
913
+ observation && observation.settle("complete");
914
+ finish();
915
+ render.abort({
916
+ quiet: true,
917
+ bound
918
+ });
919
+ }
920
+ function onSignal() {
921
+ const gone = upstream.reason && upstream.reason[DISCONNECTED];
922
+ if (bounded && flushed && !gone) return cut("time");
923
+ finish();
924
+ render.abort(upstream.reason);
925
+ }
926
+ const emit = chunk => {
927
+ if (observation) observation.chunk(chunk);
848
928
  w.write(chunk);
849
- } : chunk => w.write(chunk);
850
- const sink = createFrameSink(emit, frame, options.resume && options.resume.have);
929
+ if (bounded && !flushed && chunk.type === "html" && chunk.id === id) {
930
+ flushed = true;
931
+ if (maxDurationMs > 0 && maxDurationMs !== Infinity) timer = setTimeout(() => cut("time"), maxDurationMs);
932
+ }
933
+ };
934
+ const sink = createFrameSink(emit, frame, options.resume && options.resume.have, bounded ? {
935
+ onYield() {
936
+ if (flushed && maxYields > 0 && ++yields >= maxYields) cut("yields");
937
+ }
938
+ } : undefined);
939
+ if (options.live) sink.needs("wire");
851
940
  w.write({
852
941
  type: "start",
853
942
  id,
854
943
  version
855
944
  });
945
+ if (upstream && upstream.aborted) return finish();
946
+ if (upstream) upstream.addEventListener("abort", onSignal, {
947
+ once: true
948
+ });
856
949
  const code = makeCode(sink, frame);
857
950
  try {
858
951
  renderToStream(() => serverOwned(code), {
859
952
  serializer: createJSONSerializer,
860
953
  ...options,
954
+ signal: render.signal,
861
955
  sink
862
956
  }).pipe({
863
957
  write() {},
864
958
  end() {
959
+ if (ended) return;
865
960
  sink.end();
866
961
  observation && observation.settle("complete");
867
- w.end && w.end();
962
+ finish();
868
963
  }
869
964
  });
870
965
  } catch (err) {
@@ -872,8 +967,9 @@ function frameStream(makeCode, options) {
872
967
  sink.error("", wire instanceof Error ? wire.message : String(wire));
873
968
  sink.end();
874
969
  observation && observation.settle("error", err);
875
- w.end && w.end();
970
+ finish();
876
971
  }
972
+ if (w[ANNOUNCE]) w[ANNOUNCE](sink.announce());
877
973
  }
878
974
  return {
879
975
  pipe: stream,
@@ -905,21 +1001,25 @@ function isRangeKey(key) {
905
1001
  const c = key.charCodeAt(0);
906
1002
  return c === 36 || c >= 48 && c <= 57 || RANGE_KEYS.has(key);
907
1003
  }
908
- function slotProxy(range, occurrence, face, content, onData) {
1004
+ function slotProxy(range, occurrence, face, content, onData, needs) {
909
1005
  return new Proxy(range, {
910
1006
  get(target, key, receiver) {
911
1007
  if (typeof key !== "string" || isRangeKey(key)) return Reflect.get(target, key, receiver);
912
1008
  if (onData) onData = void onData();
1009
+ if (face !== SLOT_FACE_MARKUP) needs("bind");
913
1010
  return slotValue(occurrence, key, content ? content[key] : undefined, face);
914
1011
  }
915
1012
  });
916
1013
  }
917
- function argBorderForm(value, key, occurrence) {
918
- return toBorderForm(withoutStandIns(value, key, occurrence));
1014
+ function argBorderForm(value, key, occurrence, needs) {
1015
+ return toBorderForm(withoutStandIns(value, key, occurrence, needs));
919
1016
  }
920
- function withoutStandIns(value, key, occurrence) {
1017
+ function withoutStandIns(value, key, occurrence, needs) {
921
1018
  return rewriteTree(value, (v, path) => {
922
- if (isContainerTraced(v)) return v;
1019
+ if (isContainerTraced(v)) {
1020
+ needs("trace");
1021
+ return v;
1022
+ }
923
1023
  if (Object.getPrototypeOf(v) === Object.prototype && isSlotValue(v)) {
924
1024
  standInArgFinding(v, key, occurrence, path);
925
1025
  return undefined;
@@ -938,10 +1038,10 @@ function standInArgFinding(sv, key, occurrence, path) {
938
1038
  }
939
1039
  const where = path === "" ? `Arg \`${key}\`` : `Arg \`${key}${path}\``;
940
1040
  devCheck({
941
- code: "ATTRIBUTE_SLOT_POSITION",
1041
+ code: "BINDING_SLOT_POSITION",
942
1042
  kind: "ssr",
943
1043
  severity: "warn",
944
- message: `[ATTRIBUTE_SLOT_POSITION] ${where} of \`${occurrence}\` is another slot's value ` + `(\`${sv.k}\` of \`${sv[SLOT_VALUE]}\`). The server does not have it — the client owns ` + `it — so it cannot be passed as data; the arg carries \`undefined\` there on both faces. ` + `Pass the server's own value, or have the client fill read it from its own state.`,
1044
+ message: `[BINDING_SLOT_POSITION] ${where} of \`${occurrence}\` is another slot's value ` + `(\`${sv.k}\` of \`${sv[SLOT_VALUE]}\`). The server does not have it — the client owns ` + `it — so it cannot be passed as data; the arg carries \`undefined\` there on both faces. ` + `Pass the server's own value, or have the client fill read it from its own state.`,
945
1045
  data: {
946
1046
  reason: "arg",
947
1047
  occurrence,
@@ -970,10 +1070,10 @@ function slotFace(range, content) {
970
1070
  for (const key of Object.keys(content)) {
971
1071
  if (isRangeKey(key)) {
972
1072
  devCheck({
973
- code: "ATTRIBUTE_SLOT_POSITION",
1073
+ code: "BINDING_SLOT_POSITION",
974
1074
  kind: "ssr",
975
1075
  severity: "warn",
976
- message: `[ATTRIBUTE_SLOT_POSITION] The fill for \`${range.$occurrence}\` returned a key named \`${key}\`, ` + `which is reserved (keys beginning with \`$\` or a digit, \`length\`, \`slice\`, the node keys \`t\`/\`h\`/\`p\`, ` + `\`then\`, \`constructor\`, \`toString\`, \`valueOf\`, \`toJSON\`): the server reads it as the ` + `slot's range, not as a value. Rename it.`,
1076
+ message: `[BINDING_SLOT_POSITION] The fill for \`${range.$occurrence}\` returned a key named \`${key}\`, ` + `which is reserved (keys beginning with \`$\` or a digit, \`length\`, \`slice\`, the node keys \`t\`/\`h\`/\`p\`, ` + `\`then\`, \`constructor\`, \`toString\`, \`valueOf\`, \`toJSON\`): the server reads it as the ` + `slot's range, not as a value. Rename it.`,
977
1077
  data: {
978
1078
  reason: "reserved-key",
979
1079
  occurrence: range.$occurrence,
@@ -1084,7 +1184,7 @@ function createDocumentSlotProps(clientProps, frameId) {
1084
1184
  r.$occurrence = occurrence;
1085
1185
  const face = slotFace(r, content);
1086
1186
  r.$face = face;
1087
- return slotProxy(r, occurrence, face, face === SLOT_FACE_DATA ? content : undefined);
1187
+ return slotProxy(r, occurrence, face, face === SLOT_FACE_DATA ? content : undefined, undefined, documentNeeds);
1088
1188
  };
1089
1189
  const zoneOwner = getOwner ? getOwner() : null;
1090
1190
  const scoped = (occurrence, render) => {
@@ -1125,136 +1225,151 @@ function createDocumentSlotProps(clientProps, frameId) {
1125
1225
  const occurrence = occurrenceId(prop, raw, counts);
1126
1226
  const slot = clientProps[prop];
1127
1227
  if (typeof slot !== "function") return range(occurrence, undefined);
1128
- const resolved = {};
1129
- const liveArgs = sharedConfig.context && sharedConfig.context.live && sharedConfig.context.live.args;
1130
- const vals = {};
1131
- const evals = {};
1132
- const states = {};
1133
- const minted = {};
1134
- for (const key of Object.keys(raw)) {
1135
- if (key === "$key") continue;
1136
- const desc = Object.getOwnPropertyDescriptor(raw, key);
1137
- let evaluate = null;
1138
- let value;
1139
- if (desc.get) {
1140
- const get = desc.get;
1141
- evaluate = () => unwrapThunks(get.call(raw));
1142
- } else {
1143
- value = desc.value;
1144
- if (typeof value === "function") {
1145
- const fn = value;
1146
- evaluate = () => unwrapThunks(fn);
1228
+ const context = sharedConfig.context;
1229
+ const recordKey = `sc:slot:${frameId}:${occurrence}`;
1230
+ let settleRecord;
1231
+ if (context && context.async) {
1232
+ context.serialize(recordKey, new Promise(resolve => settleRecord = resolve));
1233
+ }
1234
+ try {
1235
+ const resolved = {};
1236
+ const liveArgs = sharedConfig.context && sharedConfig.context.live && sharedConfig.context.live.args;
1237
+ const vals = {};
1238
+ const evals = {};
1239
+ const states = {};
1240
+ const minted = {};
1241
+ for (const key of Object.keys(raw)) {
1242
+ if (key === "$key") continue;
1243
+ const desc = Object.getOwnPropertyDescriptor(raw, key);
1244
+ let evaluate = null;
1245
+ let value;
1246
+ if (desc.get) {
1247
+ const get = desc.get;
1248
+ evaluate = () => unwrapThunks(get.call(raw));
1249
+ } else {
1250
+ value = desc.value;
1251
+ if (typeof value === "function") {
1252
+ const fn = value;
1253
+ evaluate = () => unwrapThunks(fn);
1254
+ }
1255
+ }
1256
+ if (evaluate) {
1257
+ evals[key] = evaluate;
1258
+ const stampBefore = scopeStamp();
1259
+ try {
1260
+ value = evaluate();
1261
+ states[key] = {
1262
+ settled: true,
1263
+ last: value
1264
+ };
1265
+ } catch (err) {
1266
+ const blocked = ssrHandleError && ssrHandleError(err);
1267
+ if (!blocked) throw err;
1268
+ const state = states[key] = {
1269
+ settled: false,
1270
+ last: undefined
1271
+ };
1272
+ value = retryArgUntilSettled(evaluate, blocked, key, occurrence, v => {
1273
+ state.settled = true;
1274
+ state.last = v;
1275
+ if (liveArgs) liveArgs.commit();
1276
+ });
1277
+ }
1278
+ if (scopeStamp() !== stampBefore) minted[key] = true;
1147
1279
  }
1280
+ vals[key] = value;
1148
1281
  }
1149
- if (evaluate) {
1150
- evals[key] = evaluate;
1151
- const stampBefore = scopeStamp();
1152
- try {
1153
- value = evaluate();
1154
- states[key] = {
1155
- settled: true,
1156
- last: value
1282
+ const regions = [];
1283
+ for (const key of Object.keys(vals)) {
1284
+ const value = vals[key];
1285
+ if (isContainerTraced(value)) {
1286
+ documentNeeds("trace");
1287
+ resolved[key] = value;
1288
+ } else if (isServerContent(value)) {
1289
+ const childId = `${frameId}.${occurrence}.${key}`;
1290
+ const region = {
1291
+ key,
1292
+ childId,
1293
+ value,
1294
+ used: false,
1295
+ locked: false
1157
1296
  };
1158
- } catch (err) {
1159
- const blocked = ssrHandleError && ssrHandleError(err);
1160
- if (!blocked) throw err;
1161
- const state = states[key] = {
1162
- settled: false,
1163
- last: undefined
1297
+ regions.push(region);
1298
+ documentNeeds("regions");
1299
+ resolved[key] = () => {
1300
+ if (region.locked) return [];
1301
+ region.used = true;
1302
+ return [{
1303
+ t: frameElementOpen(childId)
1304
+ }, value, {
1305
+ t: FRAME_ELEMENT_CLOSE
1306
+ }];
1164
1307
  };
1165
- value = retryArgUntilSettled(evaluate, blocked, key, occurrence, v => {
1166
- state.settled = true;
1167
- state.last = v;
1168
- if (liveArgs) liveArgs.commit();
1308
+ } else if (ssrAsyncValue && isAsyncValue(value)) {
1309
+ let readable = value;
1310
+ if (typeof value.then !== "function") {
1311
+ const {
1312
+ first,
1313
+ rest
1314
+ } = tapFirstYield(value);
1315
+ readable = first;
1316
+ vals[key] = rest;
1317
+ }
1318
+ const read = ssrAsyncValue(readable);
1319
+ Object.defineProperty(resolved, key, {
1320
+ get: read,
1321
+ enumerable: true,
1322
+ configurable: true
1169
1323
  });
1324
+ } else {
1325
+ resolved[key] = vals[key] = withoutStandIns(value, key, occurrence, documentNeeds);
1170
1326
  }
1171
- if (scopeStamp() !== stampBefore) minted[key] = true;
1172
1327
  }
1173
- vals[key] = value;
1174
- }
1175
- const regions = [];
1176
- for (const key of Object.keys(vals)) {
1177
- const value = vals[key];
1178
- if (isContainerTraced(value)) {
1179
- resolved[key] = value;
1180
- } else if (isServerContent(value)) {
1181
- const childId = `${frameId}.${occurrence}.${key}`;
1182
- const region = {
1183
- key,
1184
- childId,
1185
- value,
1186
- used: false,
1187
- locked: false
1188
- };
1189
- regions.push(region);
1190
- resolved[key] = () => {
1191
- if (region.locked) return [];
1192
- region.used = true;
1193
- return [{
1194
- t: frameElementOpen(childId)
1195
- }, value, {
1196
- t: FRAME_ELEMENT_CLOSE
1197
- }];
1198
- };
1199
- } else if (ssrAsyncValue && isAsyncValue(value)) {
1200
- let readable = value;
1201
- if (typeof value.then !== "function") {
1202
- const {
1203
- first,
1204
- rest
1205
- } = tapFirstYield(value);
1206
- readable = first;
1207
- vals[key] = rest;
1328
+ const out = suppressedFill(() => scoped(occurrence, () => range(occurrence, slot(resolved))));
1329
+ const unused = regions.filter(r => !r.used);
1330
+ if (context) {
1331
+ const args = {};
1332
+ for (const key of Object.keys(vals)) {
1333
+ const value = vals[key];
1334
+ const region = regions.find(r => r.key === key);
1335
+ if (region) {
1336
+ args[key] = {
1337
+ $frame: region.childId
1338
+ };
1339
+ continue;
1340
+ }
1341
+ if (!isContainerTraced(value) && isServerContent(value)) continue;
1342
+ args[key] = argBorderForm(value, key, occurrence, documentNeeds);
1208
1343
  }
1209
- const read = ssrAsyncValue(readable);
1210
- Object.defineProperty(resolved, key, {
1211
- get: read,
1212
- enumerable: true,
1213
- configurable: true
1344
+ settleRecord ? settleRecord({
1345
+ ...args
1346
+ }) : context.serialize(recordKey, {
1347
+ ...args
1214
1348
  });
1215
- } else {
1216
- resolved[key] = vals[key] = withoutStandIns(value, key, occurrence);
1217
- }
1218
- }
1219
- const out = suppressedFill(() => scoped(occurrence, () => range(occurrence, slot(resolved))));
1220
- const unused = regions.filter(r => !r.used);
1221
- if (sharedConfig.context) {
1222
- const args = {};
1223
- for (const key of Object.keys(vals)) {
1224
- const value = vals[key];
1225
- const region = regions.find(r => r.key === key);
1226
- if (region) {
1227
- args[key] = {
1228
- $frame: region.childId
1229
- };
1230
- continue;
1349
+ for (const region of unused) {
1350
+ region.locked = true;
1351
+ context.serialize(`sc:region:${region.childId}`, resolveRegionHtml(context, region.value));
1231
1352
  }
1232
- if (!isContainerTraced(value) && isServerContent(value)) continue;
1233
- args[key] = argBorderForm(value, key, occurrence);
1234
- }
1235
- sharedConfig.context.serialize(`sc:slot:${frameId}:${occurrence}`, {
1236
- ...args
1237
- });
1238
- for (const region of unused) {
1239
- region.locked = true;
1240
- sharedConfig.context.serialize(`sc:region:${region.childId}`, resolveRegionHtml(sharedConfig.context, region.value));
1241
- }
1242
- if (liveArgs) {
1243
- for (const key of Object.keys(evals)) {
1244
- if (regions.some(r => r.key === key)) continue;
1245
- if (minted[key]) continue;
1246
- const ledgerKey = `${frameId}:${occurrence}:${key}`;
1247
- openArgBinding(liveArgs, ledgerKey, occurrence, key, evals[key], states[key], value => {
1248
- args[key] = argBorderForm(value, key, occurrence);
1249
- liveArgs.slot(frameId, occurrence, {
1250
- ...args
1353
+ if (liveArgs) {
1354
+ for (const key of Object.keys(evals)) {
1355
+ if (regions.some(r => r.key === key)) continue;
1356
+ if (minted[key]) continue;
1357
+ const ledgerKey = `${frameId}:${occurrence}:${key}`;
1358
+ openArgBinding(liveArgs, ledgerKey, occurrence, key, evals[key], states[key], value => {
1359
+ args[key] = argBorderForm(value, key, occurrence, documentNeeds);
1360
+ liveArgs.slot(frameId, occurrence, {
1361
+ ...args
1362
+ });
1251
1363
  });
1252
- });
1364
+ }
1253
1365
  }
1254
1366
  }
1367
+ if (rk !== undefined && (rk === occurrence || out.$face === SLOT_FACE_DATA)) repeats.has(rk) || repeats.set(rk, out);
1368
+ return out;
1369
+ } catch (error) {
1370
+ settleRecord && settleRecord(undefined);
1371
+ throw error;
1255
1372
  }
1256
- if (rk !== undefined && (rk === occurrence || out.$face === SLOT_FACE_DATA)) repeats.has(rk) || repeats.set(rk, out);
1257
- return out;
1258
1373
  };
1259
1374
  fn.$lhSkip = true;
1260
1375
  getters.set(prop, fn);
@@ -1269,7 +1384,48 @@ function frameElementOpen(id) {
1269
1384
  const escaped = sharedConfig.context ? sharedConfig.context.escape(String(id), true) : String(id);
1270
1385
  return `<${FRAME_TAG} ${FRAME_ID_ATTR}="${escaped}" style="display:contents">`;
1271
1386
  }
1387
+ const DOCUMENT_SITES = Symbol();
1388
+ function contextSites(ctx) {
1389
+ if (ctx[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES];
1390
+ const proto = Object.getPrototypeOf(ctx);
1391
+ if (proto && proto[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES] = proto[DOCUMENT_SITES];
1392
+ if (proto && typeof proto.escape === "function") return ctx[DOCUMENT_SITES] = contextSites(proto);
1393
+ return ctx[DOCUMENT_SITES] = {
1394
+ byOwner: new Map(),
1395
+ counts: Object.create(null)
1396
+ };
1397
+ }
1398
+ function mintSiteId(sites, fnId) {
1399
+ const n = sites.counts[fnId] || 0;
1400
+ sites.counts[fnId] = n + 1;
1401
+ return n === 0 ? fnId : fnId + "~" + (n + 1);
1402
+ }
1403
+ function documentSiteId(fnId) {
1404
+ const ctx = sharedConfig.context;
1405
+ if (!ctx) return fnId;
1406
+ const sites = contextSites(ctx);
1407
+ const owner = getOwner ? getOwner() : null;
1408
+ if (!owner) return mintSiteId(sites, fnId);
1409
+ let byFn = sites.byOwner.get(owner);
1410
+ if (!byFn) sites.byOwner.set(owner, byFn = new Map());
1411
+ const prev = byFn.get(fnId);
1412
+ if (prev) return prev;
1413
+ const siteId = mintSiteId(sites, fnId);
1414
+ byFn.set(fnId, siteId);
1415
+ return siteId;
1416
+ }
1272
1417
  const FRAME_ELEMENT_CLOSE = `</${FRAME_TAG}>`;
1418
+ function documentNeeds(name) {
1419
+ const ctx = sharedConfig.context;
1420
+ const live = ctx && ctx.live;
1421
+ if (!live || !ctx.serialize) return;
1422
+ const tiers = live.tiers || (live.tiers = []);
1423
+ if (tiers.includes(name)) return;
1424
+ tiers.push(name);
1425
+ ctx.serialize("sc:tiers", tiers.slice());
1426
+ const url = live.tierUrls && live.tierUrls[name];
1427
+ if (url && ctx.registerAsset) ctx.registerAsset("module", url);
1428
+ }
1273
1429
  function armDocumentLiveHoles(ctx) {
1274
1430
  if (!ctx || ctx.liveHoles !== undefined) return;
1275
1431
  const live = ctx.live;
@@ -1290,14 +1446,27 @@ function armDocumentLiveHoles(ctx) {
1290
1446
  let epoch = 0;
1291
1447
  let sweepScheduled = false;
1292
1448
  let closed = false;
1449
+ let swept = null;
1293
1450
  const sweep = () => {
1294
1451
  epoch++;
1295
- for (const b of [...bindings.values()]) {
1296
- try {
1297
- b.sweep();
1298
- } catch (_) {
1452
+ const ops = swept = [];
1453
+ try {
1454
+ for (const b of [...bindings.values()]) {
1455
+ try {
1456
+ b.sweep();
1457
+ } catch (_) {
1458
+ }
1299
1459
  }
1460
+ } finally {
1461
+ swept = null;
1300
1462
  }
1463
+ pushOps(ops);
1464
+ };
1465
+ const pushOps = ops => {
1466
+ if (ops.length === 1) push(ops[0]);else if (ops.length) push({
1467
+ type: "ops",
1468
+ ops
1469
+ });
1301
1470
  };
1302
1471
  const scheduleSweep = () => {
1303
1472
  if (closed || sweepScheduled || !bindings.size) return;
@@ -1311,6 +1480,9 @@ function armDocumentLiveHoles(ctx) {
1311
1480
  ctx.serialize("sc:live", new ReadableStream({
1312
1481
  start(c) {
1313
1482
  channel = c;
1483
+ },
1484
+ cancel() {
1485
+ closed = true;
1314
1486
  }
1315
1487
  }));
1316
1488
  const push = op => {
@@ -1324,12 +1496,13 @@ function armDocumentLiveHoles(ctx) {
1324
1496
  bindings.delete(key);
1325
1497
  },
1326
1498
  hole(key, html) {
1327
- push({
1499
+ const op = {
1328
1500
  type: "hole",
1329
1501
  key,
1330
1502
  html,
1331
1503
  digest: textDigest(html)
1332
- });
1504
+ };
1505
+ swept ? swept.push(op) : pushOps([op]);
1333
1506
  },
1334
1507
  attr(key, attrs, removed) {
1335
1508
  const op = {
@@ -1339,7 +1512,7 @@ function armDocumentLiveHoles(ctx) {
1339
1512
  digest: textDigest(attrs)
1340
1513
  };
1341
1514
  if (removed && removed.length) op.removed = removed;
1342
- push(op);
1515
+ swept ? swept.push(op) : pushOps([op]);
1343
1516
  },
1344
1517
  error(key, error) {
1345
1518
  push({
@@ -1372,6 +1545,13 @@ function armDocumentLiveHoles(ctx) {
1372
1545
  },
1373
1546
  commit: scheduleSweep
1374
1547
  };
1548
+ live.error = (fid, error) => {
1549
+ push({
1550
+ type: "error",
1551
+ fid,
1552
+ error
1553
+ });
1554
+ };
1375
1555
  live.end = () => {
1376
1556
  if (closed) return;
1377
1557
  if (bindings.size) sweep();
@@ -1381,28 +1561,36 @@ function armDocumentLiveHoles(ctx) {
1381
1561
  }
1382
1562
  function frameTransformDirectResult(value, {
1383
1563
  id,
1384
- args
1564
+ args,
1565
+ tierUrls
1385
1566
  }) {
1386
1567
  if (typeof value !== "function") return value;
1387
1568
  const component = value;
1388
- const wrapped = props => [{
1389
- t: frameElementOpen(id)
1390
- },
1391
- serverOwned(() => {
1392
- const page = sharedConfig.context;
1393
- armDocumentLiveHoles(page);
1394
- const ctx = Object.create(page);
1395
- ctx.claims = CLAIMS_DOCUMENT;
1396
- sharedConfig.context = ctx;
1397
- try {
1398
- const slotProps = createDocumentSlotProps(props, id);
1399
- return serverComponentScope(() => component(slotProps), !!wrapped[LIVE_SOURCE]);
1400
- } finally {
1401
- sharedConfig.context = page;
1402
- }
1403
- }), {
1404
- t: FRAME_ELEMENT_CLOSE
1405
- }];
1569
+ const wrapped = props => {
1570
+ const siteId = documentSiteId(id);
1571
+ return [{
1572
+ t: frameElementOpen(siteId)
1573
+ },
1574
+ serverOwned(() => {
1575
+ const page = sharedConfig.context;
1576
+ armDocumentLiveHoles(page);
1577
+ if (tierUrls && page && page.live && !page.live.tierUrls) page.live.tierUrls = tierUrls;
1578
+ const ctx = Object.create(page);
1579
+ ctx.claims = CLAIMS_DOCUMENT;
1580
+ ctx.frameId = siteId;
1581
+ sharedConfig.context = ctx;
1582
+ try {
1583
+ const slotProps = createDocumentSlotProps(props, siteId);
1584
+ const live = !!wrapped[LIVE_SOURCE];
1585
+ if (live) documentNeeds("wire");
1586
+ return serverComponentScope(() => component(slotProps), live);
1587
+ } finally {
1588
+ sharedConfig.context = page;
1589
+ }
1590
+ }), {
1591
+ t: FRAME_ELEMENT_CLOSE
1592
+ }];
1593
+ };
1406
1594
  wrapped[SERVER_COMPONENT] = id;
1407
1595
  wrapped[SERVER_COMPONENT_SOURCE] = component;
1408
1596
  wrapped[SERVER_COMPONENT_ADDRESS] = frameAddress(id, args);
@@ -1508,6 +1696,7 @@ function createSlotProps(sink, frame) {
1508
1696
  const counts = Object.create(null);
1509
1697
  const getters = new Map();
1510
1698
  const repeats = new Map();
1699
+ const needs = sink.needs || (() => {});
1511
1700
  return new Proxy(Object.create(null), {
1512
1701
  has() {
1513
1702
  return true;
@@ -1519,7 +1708,7 @@ function createSlotProps(sink, frame) {
1519
1708
  if (!fn) {
1520
1709
  fn = (...callArgs) => {
1521
1710
  if (callArgs.length === 0 || callArgs[0] === undefined) {
1522
- return slotProxy(slotRange(prop), prop, SLOT_FACE_STREAM, undefined);
1711
+ return slotProxy(slotRange(prop), prop, SLOT_FACE_STREAM, undefined, undefined, needs);
1523
1712
  }
1524
1713
  const rk = repeatKey(prop, callArgs[0]);
1525
1714
  const repeat = rk !== undefined && repeats.get(rk);
@@ -1600,7 +1789,7 @@ function createSlotProps(sink, frame) {
1600
1789
  };
1601
1790
  } else {
1602
1791
  const ref = `arg:${occurrence}:${key}`;
1603
- ctx.serialize(ref, argBorderForm(value, key, occurrence));
1792
+ ctx.serialize(ref, argBorderForm(value, key, occurrence, needs));
1604
1793
  args[key] = {
1605
1794
  $ref: ref
1606
1795
  };
@@ -1632,7 +1821,7 @@ function createSlotProps(sink, frame) {
1632
1821
  } else {
1633
1822
  const ref = `arg:${occurrence}:${key}@${sink.nextArgRef(ledgerKey)}`;
1634
1823
  sink.mintRef(ref);
1635
- ctx.serialize(ref, argBorderForm(value, key, occurrence));
1824
+ ctx.serialize(ref, argBorderForm(value, key, occurrence, needs));
1636
1825
  args[key] = {
1637
1826
  $ref: ref
1638
1827
  };
@@ -1643,7 +1832,7 @@ function createSlotProps(sink, frame) {
1643
1832
  });
1644
1833
  }
1645
1834
  const keyed = rk === occurrence;
1646
- const out = slotProxy(slotRange(occurrence), occurrence, SLOT_FACE_STREAM, undefined, rk === undefined || keyed ? undefined : () => repeats.has(rk) || repeats.set(rk, out));
1835
+ const out = slotProxy(slotRange(occurrence), occurrence, SLOT_FACE_STREAM, undefined, rk === undefined || keyed ? undefined : () => repeats.has(rk) || repeats.set(rk, out), needs);
1647
1836
  if (keyed) repeats.set(rk, out);
1648
1837
  return out;
1649
1838
  };
@@ -1690,6 +1879,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1690
1879
  });
1691
1880
  let closed = false;
1692
1881
  let stopLive = null;
1882
+ let failed = false;
1693
1883
  const body = new ReadableStream({
1694
1884
  start(controller) {
1695
1885
  const end = () => {
@@ -1702,9 +1892,6 @@ function serverComponentResponse(component, options = {}, init = {}) {
1702
1892
  } catch (_) {}
1703
1893
  };
1704
1894
  if (teardown.signal.aborted) return end();
1705
- teardown.signal.addEventListener("abort", end, {
1706
- once: true
1707
- });
1708
1895
  if (live) stopLive = armLiveBody(controller, () => {
1709
1896
  closed = true;
1710
1897
  disarm();
@@ -1712,6 +1899,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1712
1899
  });
1713
1900
  stream.pipe({
1714
1901
  write(chunk) {
1902
+ if (chunk.type === "error" && !chunk.key) failed = true;
1715
1903
  if (closed) return;
1716
1904
  try {
1717
1905
  controller.enqueue(frame(JSON.stringify(chunk)));
@@ -1719,16 +1907,25 @@ function serverComponentResponse(component, options = {}, init = {}) {
1719
1907
  closed = true;
1720
1908
  }
1721
1909
  },
1722
- end
1910
+ end,
1911
+ [ANNOUNCE](tiers) {
1912
+ if (tiers.length) headers.set(FRAME_TIERS_HEADER, tiers.join(","));
1913
+ }
1723
1914
  });
1724
1915
  },
1725
1916
  cancel() {
1726
1917
  closed = true;
1727
1918
  disarm();
1728
1919
  if (stopLive) stopLive();
1729
- teardown.abort();
1920
+ teardown.abort({
1921
+ [DISCONNECTED]: true
1922
+ });
1730
1923
  }
1731
1924
  });
1925
+ if (failed) {
1926
+ headers.set("Cache-Control", "no-store");
1927
+ for (const name of [...headers.keys()]) if (name === "surrogate-control" || name.endsWith("cdn-cache-control")) headers.delete(name);
1928
+ }
1732
1929
  return new Response(body, {
1733
1930
  status: init.status || 200,
1734
1931
  headers
@@ -1928,7 +2125,9 @@ function frameFlightResponse({
1928
2125
  cancel() {
1929
2126
  closed = true;
1930
2127
  disarm();
1931
- teardown.abort();
2128
+ teardown.abort({
2129
+ [DISCONNECTED]: true
2130
+ });
1932
2131
  }
1933
2132
  });
1934
2133
  return new Response(body, {
@@ -1943,4 +2142,4 @@ function asyncArg(value) {
1943
2142
  return value;
1944
2143
  }
1945
2144
 
1946
- export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, createFrameSink, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse, renderServerComponent, renderToFrameStream, serverComponentResponse };
2145
+ export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, FRAME_TIERS_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse };