@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
@@ -158,7 +158,7 @@ function provideServerFunctionRPC(rpc) {
158
158
  }
159
159
 
160
160
  const FLASH_COOKIE = "flash";
161
- const FLASH_MATCHER = new RegExp(`(?:^|;\\s*)${FLASH_COOKIE}=([^;]+)`);
161
+ const FLASH_MATCHER = /(?:^|;\s*)flash=([^;]+)/;
162
162
  function hasFlashCookie(cookieHeader) {
163
163
  return !!cookieHeader && FLASH_MATCHER.test(cookieHeader);
164
164
  }
@@ -204,6 +204,7 @@ function hasFlightMetadata(response) {
204
204
  return response.headers.has(REDIRECT_HEADER) || response.headers.has(REVALIDATE_HEADER);
205
205
  }
206
206
  async function deliverFlightData(response, data) {
207
+ if (data) await Promise.allSettled(Object.values(data).flatMap(s => s && typeof s == "object" ? Object.values(s) : []));
207
208
  const folded = (response.headers.get(SINGLE_FLIGHT_HEADER) || "").split(",");
208
209
  const metadata = hasFlightMetadata(response);
209
210
  for (const source of getFlightDataSourceIds()) {
@@ -523,26 +524,42 @@ async function extractBody(source, codecOptions, wire) {
523
524
  return undefined;
524
525
  }
525
526
  function createChunk(data) {
526
- const encoder = new TextEncoder();
527
- const encodeData = encoder.encode(data);
527
+ const encodeData = CHUNK_ENCODER.encode(data);
528
528
  const bytes = encodeData.length;
529
+ if (bytes > MAX_CHUNK_BYTES) {
530
+ throw new RangeError("Server function chunk is too large to frame.");
531
+ }
529
532
  const chunk = new Uint8Array(12 + bytes);
530
- chunk.set(encoder.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
533
+ chunk.set(CHUNK_ENCODER.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
531
534
  chunk.set(encodeData, 12);
532
535
  return chunk;
533
536
  }
537
+ const CHUNK_ENCODER = /* @__PURE__ */new TextEncoder();
538
+ const CHUNK_DECODER = /* @__PURE__ */new TextDecoder("utf-8", {
539
+ fatal: true
540
+ });
541
+ const MAX_CHUNK_BYTES = 0xffffffff;
542
+ const RETAINED_STORE_BYTES = 64 * 1024;
543
+ const DONE = {
544
+ done: true,
545
+ value: undefined
546
+ };
547
+ function malformedStream() {
548
+ return new Error("Malformed server function stream.");
549
+ }
534
550
  class ChunkReader {
535
551
  constructor(stream) {
536
552
  this.reader = stream.getReader();
537
553
  this.store = new Uint8Array(0);
538
554
  this.buffer = this.store;
539
555
  this.done = false;
556
+ this.cancelled = false;
540
557
  }
541
558
  async readChunk() {
542
559
  const chunk = await this.reader.read();
543
560
  if (chunk.done) {
544
561
  this.done = true;
545
- return;
562
+ return this.cancelled;
546
563
  }
547
564
  const incoming = chunk.value;
548
565
  const store = this.store;
@@ -563,55 +580,66 @@ class ChunkReader {
563
580
  this.store = grown;
564
581
  this.buffer = grown.subarray(0, needed);
565
582
  }
583
+ return this.cancelled;
566
584
  }
567
585
  async next() {
586
+ if (this.cancelled) return DONE;
568
587
  while (this.buffer.length < 12) {
569
588
  if (this.done) {
570
- if (this.buffer.length === 0) return {
571
- done: true,
572
- value: undefined
573
- };
574
- throw new Error("Malformed server function stream.");
589
+ if (this.buffer.length === 0) return DONE;
590
+ throw malformedStream();
575
591
  }
576
- await this.readChunk();
577
- }
578
- const decoder = new TextDecoder();
579
- const bytes = Number.parseInt(decoder.decode(this.buffer.subarray(1, 11)), 16);
580
- if (Number.isNaN(bytes)) {
581
- throw new Error("Malformed server function stream.");
592
+ if (await this.readChunk()) return DONE;
582
593
  }
594
+ const header = String.fromCharCode(...this.buffer.subarray(0, 12));
595
+ if (!/^;0x[\dA-Fa-f]{8};$/.test(header)) throw malformedStream();
596
+ const bytes = parseInt(header.slice(3, 11), 16);
583
597
  while (bytes > this.buffer.length - 12) {
584
598
  if (this.done) {
585
- throw new Error("Malformed server function stream.");
599
+ throw malformedStream();
586
600
  }
587
- await this.readChunk();
601
+ if (await this.readChunk()) return DONE;
602
+ }
603
+ let partial;
604
+ try {
605
+ partial = CHUNK_DECODER.decode(this.buffer.subarray(12, 12 + bytes));
606
+ } catch {
607
+ throw malformedStream();
588
608
  }
589
- const partial = decoder.decode(this.buffer.subarray(12, 12 + bytes));
590
609
  this.buffer = this.buffer.subarray(12 + bytes);
610
+ if (this.store.length > RETAINED_STORE_BYTES && this.buffer.length <= RETAINED_STORE_BYTES) {
611
+ this.store = this.buffer = this.buffer.slice();
612
+ }
591
613
  return {
592
614
  done: false,
593
615
  value: partial
594
616
  };
595
617
  }
596
618
  async drain(interpret) {
597
- while (true) {
598
- const result = await this.next();
599
- if (result.done) {
600
- break;
619
+ try {
620
+ while (true) {
621
+ const result = await this.next();
622
+ if (result.done) {
623
+ break;
624
+ }
625
+ interpret(result.value);
601
626
  }
602
- interpret(result.value);
627
+ } catch (error) {
628
+ this.cancel(error).catch(() => {});
629
+ throw error;
603
630
  }
604
631
  }
605
632
  cancel(reason) {
633
+ this.cancelled = true;
606
634
  return this.reader.cancel(reason);
607
635
  }
608
636
  }
609
637
  function createEventChunk(data, id) {
610
638
  let event = id !== undefined && !id.includes("\0") ? `id: ${id}\n` : "";
611
639
  for (const line of data.split(/\r\n|\r|\n/)) event += `data: ${line}\n`;
612
- return new TextEncoder().encode(event + "\n");
640
+ return CHUNK_ENCODER.encode(event + "\n");
613
641
  }
614
- const EVENT_STREAM_HEARTBEAT = new TextEncoder().encode(":\n\n");
642
+ const EVENT_STREAM_HEARTBEAT = /* @__PURE__ */new TextEncoder().encode(":\n\n");
615
643
  function isEventStream(source) {
616
644
  const contentType = source.headers.get("content-type");
617
645
  return !!contentType && contentType.startsWith("text/event-stream");
@@ -754,40 +782,44 @@ async function deserializeStream(source, codecOptions, wire) {
754
782
  throw new Error("missing body");
755
783
  }
756
784
  const reader = wire && isEventStream(source) ? wire.open(source.body) : new ChunkReader(source.body);
757
- const result = await reader.next();
758
- if (!result.done) {
759
- if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
760
- throw errorFromTrailer(result.value);
761
- }
762
- const {
763
- createJSONDeserializer
764
- } = await import('@solidjs/web/serialization/decode');
765
- const deserializeChunk = createJSONDeserializer(codecOptions);
766
- function interpretChunk(chunk) {
767
- if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
768
- throw errorFromTrailer(chunk);
785
+ try {
786
+ const result = await reader.next();
787
+ if (!result.done) {
788
+ if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
789
+ throw errorFromTrailer(result.value);
790
+ }
791
+ const {
792
+ createJSONDeserializer
793
+ } = await import('@solidjs/web/serialization/decode');
794
+ const deserializeChunk = createJSONDeserializer(codecOptions);
795
+ function interpretChunk(chunk) {
796
+ if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
797
+ throw errorFromTrailer(chunk);
798
+ }
799
+ return deserializeChunk(JSON.parse(chunk));
769
800
  }
770
- return deserializeChunk(JSON.parse(chunk));
801
+ const connection = wire && wire.connection;
802
+ let endConnection;
803
+ if (connection) connection.ended = new Promise(resolve => endConnection = resolve);
804
+ const end = error => {
805
+ const sweep = () => deserializeChunk.abort(error);
806
+ if (connection) {
807
+ const close = () => deserializeChunk.close();
808
+ endConnection({
809
+ open: deserializeChunk.open(),
810
+ error,
811
+ sweep,
812
+ close
813
+ });
814
+ } else sweep();
815
+ };
816
+ reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
817
+ return interpretChunk(result.value);
771
818
  }
772
- const connection = wire && wire.connection;
773
- let endConnection;
774
- if (connection) connection.ended = new Promise(resolve => endConnection = resolve);
775
- const end = error => {
776
- const sweep = () => deserializeChunk.abort(error);
777
- if (connection) {
778
- const close = () => deserializeChunk.close();
779
- endConnection({
780
- open: deserializeChunk.open(),
781
- error,
782
- sweep,
783
- close
784
- });
785
- } else sweep();
786
- };
787
- reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
788
- return interpretChunk(result.value);
819
+ } catch (error) {
820
+ reader.cancel(error).catch(() => {});
821
+ throw error;
789
822
  }
790
- return undefined;
791
823
  }
792
824
  async function decodeResponse(response, codecOptions) {
793
825
  if (!response.body) return undefined;
@@ -916,10 +948,11 @@ async function createRequest(base, id, options, meta) {
916
948
  headers
917
949
  };
918
950
  if (config.prepareRequest) {
919
- const prepared = await config.prepareRequest(init, {
951
+ let prepared = config.prepareRequest(init, {
920
952
  id,
921
953
  meta
922
954
  });
955
+ if (prepared && typeof prepared.then === "function") prepared = await prepared;
923
956
  if (prepared && prepared !== init) {
924
957
  if (typeof prepared !== "object") {
925
958
  throw new Error("prepareRequest must return (or mutate and return) the RequestInit it received; " + `it returned a ${typeof prepared}. Spread the init: ` + "init => ({ ...init, headers: { ...init.headers, ... } })");
@@ -1014,6 +1047,7 @@ async function dispatchServerFunction(base, id, options, args, meta, callArgs =
1014
1047
  id,
1015
1048
  meta
1016
1049
  }) : undefined;
1050
+ const sent = options;
1017
1051
  const controller = options.signal ? undefined : new AbortController();
1018
1052
  if (controller) options = {
1019
1053
  ...options,
@@ -1030,7 +1064,8 @@ async function dispatchServerFunction(base, id, options, args, meta, callArgs =
1030
1064
  id,
1031
1065
  meta,
1032
1066
  args: callArgs,
1033
- context
1067
+ context,
1068
+ retry: () => fetchServerFunction(base, id, sent, args, meta, callArgs)
1034
1069
  };
1035
1070
  if (options[LIVE_WIRE]) ctx[LIVE_WIRE] = options[LIVE_WIRE];
1036
1071
  const handled = handler.handle(response, ctx);
@@ -1094,7 +1129,8 @@ function createServerReference(id, name, base) {
1094
1129
  const hit = handler.intercept({
1095
1130
  id,
1096
1131
  meta: metadata,
1097
- args
1132
+ args,
1133
+ retry: send
1098
1134
  });
1099
1135
  if (hit !== undefined) return localOrSend(hit, send);
1100
1136
  }
@@ -1125,12 +1161,14 @@ function GET(fn) {
1125
1161
  const run = async (args, invokeOptions) => {
1126
1162
  const handler = config.responseHandler;
1127
1163
  if (handler && handler.intercept && !adoptedCall(invokeOptions)) {
1164
+ const retry = () => send(args, invokeOptions);
1128
1165
  const hit = handler.intercept({
1129
1166
  id,
1130
1167
  meta: metadata,
1131
- args
1168
+ args,
1169
+ retry
1132
1170
  });
1133
- if (hit !== undefined) return localOrSend(hit, () => send(args, invokeOptions));
1171
+ if (hit !== undefined) return localOrSend(hit, retry);
1134
1172
  }
1135
1173
  return send(args, invokeOptions);
1136
1174
  };
@@ -1188,6 +1226,7 @@ function live(fn) {
1188
1226
  };
1189
1227
  const makeIterable = (args, invokeOptions) => {
1190
1228
  const handler = config.responseHandler;
1229
+ handler && handler.onLive && handler.onLive();
1191
1230
  const local = handler && handler.intercept ? handler.intercept({
1192
1231
  id,
1193
1232
  meta: metadata,
@@ -393,7 +393,7 @@ function assertServableCookie(name, options) {
393
393
  }
394
394
  }
395
395
  const FLASH_COOKIE = "flash";
396
- const FLASH_MATCHER = new RegExp(`(?:^|;\\s*)${FLASH_COOKIE}=([^;]+)`);
396
+ const FLASH_MATCHER = /(?:^|;\s*)flash=([^;]+)/;
397
397
  function hasFlashCookie(cookieHeader) {
398
398
  return !!cookieHeader && FLASH_MATCHER.test(cookieHeader);
399
399
  }
@@ -711,7 +711,7 @@ async function extractBody(source, codecOptions, wire) {
711
711
  const format = source.headers.get(BODY_FORMAT_HEADER);
712
712
  switch (true) {
713
713
  case format === BodyFormat.Serialized:
714
- return await deserializeStream(source, codecOptions);
714
+ return await deserializeStream(source, codecOptions, wire);
715
715
  case format === BodyFormat.Json:
716
716
  return JSON.parse(await source.text());
717
717
  case format === BodyFormat.String:
@@ -737,26 +737,42 @@ async function extractBody(source, codecOptions, wire) {
737
737
  return undefined;
738
738
  }
739
739
  function createChunk(data) {
740
- const encoder = new TextEncoder();
741
- const encodeData = encoder.encode(data);
740
+ const encodeData = CHUNK_ENCODER.encode(data);
742
741
  const bytes = encodeData.length;
742
+ if (bytes > MAX_CHUNK_BYTES) {
743
+ throw new RangeError("Server function chunk is too large to frame.");
744
+ }
743
745
  const chunk = new Uint8Array(12 + bytes);
744
- chunk.set(encoder.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
746
+ chunk.set(CHUNK_ENCODER.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
745
747
  chunk.set(encodeData, 12);
746
748
  return chunk;
747
749
  }
750
+ const CHUNK_ENCODER = /* @__PURE__ */new TextEncoder();
751
+ const CHUNK_DECODER = /* @__PURE__ */new TextDecoder("utf-8", {
752
+ fatal: true
753
+ });
754
+ const MAX_CHUNK_BYTES = 0xffffffff;
755
+ const RETAINED_STORE_BYTES = 64 * 1024;
756
+ const DONE = {
757
+ done: true,
758
+ value: undefined
759
+ };
760
+ function malformedStream() {
761
+ return new Error("Malformed server function stream.");
762
+ }
748
763
  class ChunkReader {
749
764
  constructor(stream) {
750
765
  this.reader = stream.getReader();
751
766
  this.store = new Uint8Array(0);
752
767
  this.buffer = this.store;
753
768
  this.done = false;
769
+ this.cancelled = false;
754
770
  }
755
771
  async readChunk() {
756
772
  const chunk = await this.reader.read();
757
773
  if (chunk.done) {
758
774
  this.done = true;
759
- return;
775
+ return this.cancelled;
760
776
  }
761
777
  const incoming = chunk.value;
762
778
  const store = this.store;
@@ -777,55 +793,66 @@ class ChunkReader {
777
793
  this.store = grown;
778
794
  this.buffer = grown.subarray(0, needed);
779
795
  }
796
+ return this.cancelled;
780
797
  }
781
798
  async next() {
799
+ if (this.cancelled) return DONE;
782
800
  while (this.buffer.length < 12) {
783
801
  if (this.done) {
784
- if (this.buffer.length === 0) return {
785
- done: true,
786
- value: undefined
787
- };
788
- throw new Error("Malformed server function stream.");
802
+ if (this.buffer.length === 0) return DONE;
803
+ throw malformedStream();
789
804
  }
790
- await this.readChunk();
791
- }
792
- const decoder = new TextDecoder();
793
- const bytes = Number.parseInt(decoder.decode(this.buffer.subarray(1, 11)), 16);
794
- if (Number.isNaN(bytes)) {
795
- throw new Error("Malformed server function stream.");
805
+ if (await this.readChunk()) return DONE;
796
806
  }
807
+ const header = String.fromCharCode(...this.buffer.subarray(0, 12));
808
+ if (!/^;0x[\dA-Fa-f]{8};$/.test(header)) throw malformedStream();
809
+ const bytes = parseInt(header.slice(3, 11), 16);
797
810
  while (bytes > this.buffer.length - 12) {
798
811
  if (this.done) {
799
- throw new Error("Malformed server function stream.");
812
+ throw malformedStream();
800
813
  }
801
- await this.readChunk();
814
+ if (await this.readChunk()) return DONE;
815
+ }
816
+ let partial;
817
+ try {
818
+ partial = CHUNK_DECODER.decode(this.buffer.subarray(12, 12 + bytes));
819
+ } catch {
820
+ throw malformedStream();
802
821
  }
803
- const partial = decoder.decode(this.buffer.subarray(12, 12 + bytes));
804
822
  this.buffer = this.buffer.subarray(12 + bytes);
823
+ if (this.store.length > RETAINED_STORE_BYTES && this.buffer.length <= RETAINED_STORE_BYTES) {
824
+ this.store = this.buffer = this.buffer.slice();
825
+ }
805
826
  return {
806
827
  done: false,
807
828
  value: partial
808
829
  };
809
830
  }
810
831
  async drain(interpret) {
811
- while (true) {
812
- const result = await this.next();
813
- if (result.done) {
814
- break;
832
+ try {
833
+ while (true) {
834
+ const result = await this.next();
835
+ if (result.done) {
836
+ break;
837
+ }
838
+ interpret(result.value);
815
839
  }
816
- interpret(result.value);
840
+ } catch (error) {
841
+ this.cancel(error).catch(() => {});
842
+ throw error;
817
843
  }
818
844
  }
819
845
  cancel(reason) {
846
+ this.cancelled = true;
820
847
  return this.reader.cancel(reason);
821
848
  }
822
849
  }
823
850
  function createEventChunk(data, id) {
824
851
  let event = id !== undefined && !id.includes("\0") ? `id: ${id}\n` : "";
825
852
  for (const line of data.split(/\r\n|\r|\n/)) event += `data: ${line}\n`;
826
- return new TextEncoder().encode(event + "\n");
853
+ return CHUNK_ENCODER.encode(event + "\n");
827
854
  }
828
- const EVENT_STREAM_HEARTBEAT = new TextEncoder().encode(":\n\n");
855
+ const EVENT_STREAM_HEARTBEAT = /* @__PURE__ */new TextEncoder().encode(":\n\n");
829
856
  function positionDigest(value) {
830
857
  if (value === undefined || !isJSONSafe(value)) return undefined;
831
858
  const text = JSON.stringify(value);
@@ -889,29 +916,36 @@ async function deserializeStream(source, codecOptions, wire) {
889
916
  throw new Error("missing body");
890
917
  }
891
918
  const reader = new ChunkReader(source.body);
892
- const result = await reader.next();
893
- if (!result.done) {
894
- if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
895
- throw errorFromTrailer(result.value);
896
- }
897
- const {
898
- createJSONDeserializer
899
- } = await import('@solidjs/web/serialization/decode');
900
- const deserializeChunk = createJSONDeserializer(codecOptions);
901
- function interpretChunk(chunk) {
902
- if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
903
- throw errorFromTrailer(chunk);
919
+ try {
920
+ const result = await reader.next();
921
+ if (!result.done) {
922
+ if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
923
+ throw errorFromTrailer(result.value);
924
+ }
925
+ const {
926
+ createJSONDeserializer
927
+ } = await import('@solidjs/web/serialization/decode');
928
+ const deserializeChunk = createJSONDeserializer(codecOptions);
929
+ function interpretChunk(chunk) {
930
+ if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
931
+ throw errorFromTrailer(chunk);
932
+ }
933
+ return deserializeChunk(JSON.parse(chunk));
904
934
  }
905
- return deserializeChunk(JSON.parse(chunk));
935
+ const connection = wire && wire.connection;
936
+ let endConnection;
937
+ if (connection) ;
938
+ const end = error => {
939
+ const sweep = () => deserializeChunk.abort(error);
940
+ if (connection) ; else sweep();
941
+ };
942
+ reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
943
+ return interpretChunk(result.value);
906
944
  }
907
- const end = error => {
908
- const sweep = () => deserializeChunk.abort(error);
909
- sweep();
910
- };
911
- reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
912
- return interpretChunk(result.value);
945
+ } catch (error) {
946
+ reader.cancel(error).catch(() => {});
947
+ throw error;
913
948
  }
914
- return undefined;
915
949
  }
916
950
  async function deserializeString(text, codecOptions) {
917
951
  return await deserializeStream(new Response(text), codecOptions);
@@ -1022,10 +1056,13 @@ function copyInitHeaders(init) {
1022
1056
  for (const cookie of init.getSetCookie()) headers.append("Set-Cookie", cookie);
1023
1057
  return headers;
1024
1058
  }
1025
- const STUB_GAP_FILL_EXCLUDED = /*#__PURE__*/new Set([ERROR_HEADER, BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER, REVALIDATE_HEADER, REDIRECT_HEADER, "Location",
1026
- ...COMPOSED_BODY_FRAMING].map(header => header.toLowerCase()));
1059
+ let stubGapFillExcluded;
1060
+ function isStubGapFillExcluded(key) {
1061
+ return (stubGapFillExcluded ||= new Set([ERROR_HEADER, BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER, REVALIDATE_HEADER, REDIRECT_HEADER, "Location",
1062
+ ...COMPOSED_BODY_FRAMING].map(header => header.toLowerCase()))).has(key);
1063
+ }
1027
1064
  function fillsStubGap(key, headers, response) {
1028
- if (key === "set-cookie" || STUB_GAP_FILL_EXCLUDED.has(key)) return false;
1065
+ if (key === "set-cookie" || isStubGapFillExcluded(key)) return false;
1029
1066
  if (response.body === null && (key === "content-type" || key === "content-length")) return false;
1030
1067
  return !headers.has(key);
1031
1068
  }
@@ -1588,20 +1625,28 @@ function createServerReference({
1588
1625
  const scope = run => provideEvent(evt, run);
1589
1626
  const wrap = resolveWrapInvocation(config.wrapInvocation);
1590
1627
  const hook = requestErrorHook(ogEvt);
1591
- let result = provideEventOnce(provideEvent, evt, () => {
1592
- const run = () => fn.apply(thisArg, args);
1593
- return observeInvocation({
1594
- id,
1595
- direct: true,
1596
- event: evt,
1597
- args
1598
- }, () => reportDirectFailure(() => wrap ? wrap(run, {
1599
- id,
1600
- args,
1601
- event: evt,
1602
- direct: true
1603
- }) : run(), id, hook));
1604
- });
1628
+ let result;
1629
+ try {
1630
+ result = provideEventOnce(provideEvent, evt, () => {
1631
+ const run = () => fn.apply(thisArg, args);
1632
+ return observeInvocation({
1633
+ id,
1634
+ direct: true,
1635
+ event: evt,
1636
+ args
1637
+ }, () => reportDirectFailure(() => wrap ? wrap(run, {
1638
+ id,
1639
+ args,
1640
+ event: evt,
1641
+ direct: true
1642
+ }) : run(), id, hook));
1643
+ });
1644
+ } catch (error) {
1645
+ throw directEnvelopeValue(error, evt);
1646
+ }
1647
+ result = result && typeof result.then === "function" ? result.then(value => directEnvelopeValue(value, evt), error => {
1648
+ throw directEnvelopeValue(error, evt);
1649
+ }) : directEnvelopeValue(result, evt);
1605
1650
  result = scopeDeferredResult(result, scope);
1606
1651
  const transform = config.transformDirectResult;
1607
1652
  if (transform && result && typeof result.then === "function") {
@@ -2485,7 +2530,7 @@ function encodeLiveResult(value, headers, status, codec, request, scope) {
2485
2530
  function encodeResult(value, headers, status, codec, signal, scope) {
2486
2531
  if (NULL_BODY_STATUSES.has(status)) {
2487
2532
  if (value === undefined || value === null) {
2488
- headers.set(BODY_FORMAT_HEADER, BodyFormat.Void);
2533
+ if (status !== 304) headers.set(BODY_FORMAT_HEADER, BodyFormat.Void);
2489
2534
  return new Response(null, {
2490
2535
  status,
2491
2536
  headers
@@ -2572,6 +2617,7 @@ function currentEvent() {
2572
2617
  }
2573
2618
  function reportDirectFailure(run, id, hook) {
2574
2619
  const report = error => {
2620
+ if (error instanceof Response || isResponseEnvelope(error)) throw error;
2575
2621
  reportServerError(error, {
2576
2622
  kind: "server-function",
2577
2623
  handling: "thrown",
@@ -2588,6 +2634,20 @@ function reportDirectFailure(run, id, hook) {
2588
2634
  }
2589
2635
  return result && typeof result.then === "function" ? result.then(undefined, report) : result;
2590
2636
  }
2637
+ function directEnvelopeValue(result, event) {
2638
+ if (!isResponseEnvelope(result)) return result;
2639
+ const {
2640
+ response,
2641
+ value
2642
+ } = result;
2643
+ const stub = event.response;
2644
+ if (response && stub && stub.headers && response.headers.getSetCookie) {
2645
+ for (const cookie of response.headers.getSetCookie()) {
2646
+ stub.headers.append("Set-Cookie", cookie);
2647
+ }
2648
+ }
2649
+ return value;
2650
+ }
2591
2651
  function sanitizeServerError(value) {
2592
2652
  if (DEV) return value;
2593
2653
  if (isSafeError(value)) return value;
@@ -2836,8 +2896,14 @@ async function handleServerFunctionRequest(request, options = {}) {
2836
2896
  });
2837
2897
  return finish(protectsRequest ? withCSRFVary(response) : response);
2838
2898
  }
2899
+ const commitResponse = raw => {
2900
+ if ((method === "GET" || method === "HEAD") && (sharedCacheMayStore(departingHeader(raw, event, "Cache-Control")) || cdnMayStore(departingHeader(raw, event, "CDN-Cache-Control")))) {
2901
+ withholdTrace(event);
2902
+ }
2903
+ return commitEventResponse(raw, event);
2904
+ };
2839
2905
  const refuseCommitted = raw => {
2840
- const response = commitEventResponse(raw, event);
2906
+ const response = commitResponse(raw);
2841
2907
  return finish(protectsRequest ? withCSRFVary(response) : response);
2842
2908
  };
2843
2909
  const provide = options.provideEvent || provideEvent;
@@ -3055,9 +3121,54 @@ async function handleServerFunctionRequest(request, options = {}) {
3055
3121
  return respondThrown(x);
3056
3122
  }
3057
3123
  };
3058
- const response = commitEventResponse(enforceComposedHeaderInvariants(ownResponse(await dispatch())), event);
3124
+ const response = commitResponse(enforceComposedHeaderInvariants(ownResponse(await dispatch())));
3059
3125
  return finish(protectsRequest ? withCSRFVary(response) : response);
3060
3126
  }
3127
+ function departingHeader(response, event, name) {
3128
+ const own = response.headers.get(name);
3129
+ if (own !== null) return own;
3130
+ const stub = event.response;
3131
+ return stub && stub.headers && !stub.committed ? stub.headers.get(name) : null;
3132
+ }
3133
+ function cdnMayStore(cdnCacheControl) {
3134
+ return cdnCacheControl !== null && !forbidsStoring(cdnCacheControl);
3135
+ }
3136
+ function sharedCacheMayStore(cacheControl) {
3137
+ let shared = false;
3138
+ return !!cacheControl && !eachDirective(cacheControl, name => {
3139
+ if (name === "public" || name === "s-maxage") shared = true;
3140
+ }) && shared;
3141
+ }
3142
+ function forbidsStoring(value) {
3143
+ return eachDirective(value, () => {});
3144
+ }
3145
+ function eachDirective(value, visit) {
3146
+ let start = 0;
3147
+ let quoted = false;
3148
+ for (let i = 0; i <= value.length; i++) {
3149
+ const c = value[i];
3150
+ if (quoted) {
3151
+ if (c === "\\") i++;else if (c === '"') quoted = false;
3152
+ continue;
3153
+ }
3154
+ if (c === '"') {
3155
+ quoted = true;
3156
+ continue;
3157
+ }
3158
+ if (c !== "," && i < value.length) continue;
3159
+ const directive = value.slice(start, i);
3160
+ start = i + 1;
3161
+ const eq = directive.indexOf("=");
3162
+ const name = (eq === -1 ? directive : directive.slice(0, eq)).trim().toLowerCase();
3163
+ if (name === "no-store" || name === "private" && eq === -1) return true;
3164
+ visit(name);
3165
+ }
3166
+ return false;
3167
+ }
3168
+ function withholdTrace(event) {
3169
+ if (event.response && event.response.committed) return;
3170
+ traceForEvent(event).emit = false;
3171
+ }
3061
3172
  function ownResponse(response) {
3062
3173
  try {
3063
3174
  return new Response(response.body, response);