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

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 (57) hide show
  1. package/dist/server.dev.js +200 -73
  2. package/dist/server.js +182 -59
  3. package/dist/server.observe.js +183 -60
  4. package/dist/web.dev.js +70 -35
  5. package/dist/web.js +43 -31
  6. package/dist/web.observe.js +43 -33
  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 +605 -1086
  11. package/frames/dist/client.js +572 -1066
  12. package/frames/dist/client.observe.js +571 -1066
  13. package/frames/dist/regions.js +130 -0
  14. package/frames/dist/server.dev.js +339 -177
  15. package/frames/dist/server.js +335 -173
  16. package/frames/dist/server.observe.js +335 -173
  17. package/frames/dist/trace.js +42 -0
  18. package/frames/dist/wire.js +133 -0
  19. package/package.json +28 -2
  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 +37 -8
  24. package/serialization/dist/serialization.js +39 -10
  25. package/serialization/types/serializer-decode.d.ts +24 -2
  26. package/server-functions/dist/client.dev.js +99 -61
  27. package/server-functions/dist/client.js +99 -61
  28. package/server-functions/dist/client.observe.js +99 -61
  29. package/server-functions/dist/server.dev.js +87 -50
  30. package/server-functions/dist/server.js +87 -50
  31. package/server-functions/dist/server.observe.js +87 -50
  32. package/skills/server-components/SKILL.md +65 -43
  33. package/types/client.d.ts +20 -0
  34. package/types/constants.d.ts +2 -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 +32 -8
  46. package/types/frames/trace-tier.d.ts +2 -0
  47. package/types/frames/wire-tier.d.ts +116 -0
  48. package/types/index.d.ts +38 -0
  49. package/types/index.server.d.ts +9 -0
  50. package/types/jsx.d.ts +1 -1
  51. package/types/observe.d.ts +4 -2
  52. package/types/serializer-decode.d.ts +24 -2
  53. package/types/server-functions/client.d.ts +26 -0
  54. package/types/server-functions/registry.d.ts +4 -1
  55. package/types/server-functions/shared.d.ts +12 -8
  56. package/types/server-mock.d.ts +13 -1
  57. package/types/server.d.ts +34 -8
@@ -342,7 +342,7 @@ function serializeCookie(name, value, options = {}) {
342
342
  return cookie;
343
343
  }
344
344
  const FLASH_COOKIE = "flash";
345
- const FLASH_MATCHER = new RegExp(`(?:^|;\\s*)${FLASH_COOKIE}=([^;]+)`);
345
+ const FLASH_MATCHER = /(?:^|;\s*)flash=([^;]+)/;
346
346
  function hasFlashCookie(cookieHeader) {
347
347
  return !!cookieHeader && FLASH_MATCHER.test(cookieHeader);
348
348
  }
@@ -660,7 +660,7 @@ async function extractBody(source, codecOptions, wire) {
660
660
  const format = source.headers.get(BODY_FORMAT_HEADER);
661
661
  switch (true) {
662
662
  case format === BodyFormat.Serialized:
663
- return await deserializeStream(source, codecOptions);
663
+ return await deserializeStream(source, codecOptions, wire);
664
664
  case format === BodyFormat.Json:
665
665
  return JSON.parse(await source.text());
666
666
  case format === BodyFormat.String:
@@ -686,26 +686,42 @@ async function extractBody(source, codecOptions, wire) {
686
686
  return undefined;
687
687
  }
688
688
  function createChunk(data) {
689
- const encoder = new TextEncoder();
690
- const encodeData = encoder.encode(data);
689
+ const encodeData = CHUNK_ENCODER.encode(data);
691
690
  const bytes = encodeData.length;
691
+ if (bytes > MAX_CHUNK_BYTES) {
692
+ throw new RangeError("Server function chunk is too large to frame.");
693
+ }
692
694
  const chunk = new Uint8Array(12 + bytes);
693
- chunk.set(encoder.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
695
+ chunk.set(CHUNK_ENCODER.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
694
696
  chunk.set(encodeData, 12);
695
697
  return chunk;
696
698
  }
699
+ const CHUNK_ENCODER = /* @__PURE__ */new TextEncoder();
700
+ const CHUNK_DECODER = /* @__PURE__ */new TextDecoder("utf-8", {
701
+ fatal: true
702
+ });
703
+ const MAX_CHUNK_BYTES = 0xffffffff;
704
+ const RETAINED_STORE_BYTES = 64 * 1024;
705
+ const DONE = {
706
+ done: true,
707
+ value: undefined
708
+ };
709
+ function malformedStream() {
710
+ return new Error("Malformed server function stream.");
711
+ }
697
712
  class ChunkReader {
698
713
  constructor(stream) {
699
714
  this.reader = stream.getReader();
700
715
  this.store = new Uint8Array(0);
701
716
  this.buffer = this.store;
702
717
  this.done = false;
718
+ this.cancelled = false;
703
719
  }
704
720
  async readChunk() {
705
721
  const chunk = await this.reader.read();
706
722
  if (chunk.done) {
707
723
  this.done = true;
708
- return;
724
+ return this.cancelled;
709
725
  }
710
726
  const incoming = chunk.value;
711
727
  const store = this.store;
@@ -726,55 +742,66 @@ class ChunkReader {
726
742
  this.store = grown;
727
743
  this.buffer = grown.subarray(0, needed);
728
744
  }
745
+ return this.cancelled;
729
746
  }
730
747
  async next() {
748
+ if (this.cancelled) return DONE;
731
749
  while (this.buffer.length < 12) {
732
750
  if (this.done) {
733
- if (this.buffer.length === 0) return {
734
- done: true,
735
- value: undefined
736
- };
737
- throw new Error("Malformed server function stream.");
751
+ if (this.buffer.length === 0) return DONE;
752
+ throw malformedStream();
738
753
  }
739
- await this.readChunk();
740
- }
741
- const decoder = new TextDecoder();
742
- const bytes = Number.parseInt(decoder.decode(this.buffer.subarray(1, 11)), 16);
743
- if (Number.isNaN(bytes)) {
744
- throw new Error("Malformed server function stream.");
754
+ if (await this.readChunk()) return DONE;
745
755
  }
756
+ const header = String.fromCharCode(...this.buffer.subarray(0, 12));
757
+ if (!/^;0x[\dA-Fa-f]{8};$/.test(header)) throw malformedStream();
758
+ const bytes = parseInt(header.slice(3, 11), 16);
746
759
  while (bytes > this.buffer.length - 12) {
747
760
  if (this.done) {
748
- throw new Error("Malformed server function stream.");
761
+ throw malformedStream();
749
762
  }
750
- await this.readChunk();
763
+ if (await this.readChunk()) return DONE;
764
+ }
765
+ let partial;
766
+ try {
767
+ partial = CHUNK_DECODER.decode(this.buffer.subarray(12, 12 + bytes));
768
+ } catch {
769
+ throw malformedStream();
751
770
  }
752
- const partial = decoder.decode(this.buffer.subarray(12, 12 + bytes));
753
771
  this.buffer = this.buffer.subarray(12 + bytes);
772
+ if (this.store.length > RETAINED_STORE_BYTES && this.buffer.length <= RETAINED_STORE_BYTES) {
773
+ this.store = this.buffer = this.buffer.slice();
774
+ }
754
775
  return {
755
776
  done: false,
756
777
  value: partial
757
778
  };
758
779
  }
759
780
  async drain(interpret) {
760
- while (true) {
761
- const result = await this.next();
762
- if (result.done) {
763
- break;
781
+ try {
782
+ while (true) {
783
+ const result = await this.next();
784
+ if (result.done) {
785
+ break;
786
+ }
787
+ interpret(result.value);
764
788
  }
765
- interpret(result.value);
789
+ } catch (error) {
790
+ this.cancel(error).catch(() => {});
791
+ throw error;
766
792
  }
767
793
  }
768
794
  cancel(reason) {
795
+ this.cancelled = true;
769
796
  return this.reader.cancel(reason);
770
797
  }
771
798
  }
772
799
  function createEventChunk(data, id) {
773
800
  let event = id !== undefined && !id.includes("\0") ? `id: ${id}\n` : "";
774
801
  for (const line of data.split(/\r\n|\r|\n/)) event += `data: ${line}\n`;
775
- return new TextEncoder().encode(event + "\n");
802
+ return CHUNK_ENCODER.encode(event + "\n");
776
803
  }
777
- const EVENT_STREAM_HEARTBEAT = new TextEncoder().encode(":\n\n");
804
+ const EVENT_STREAM_HEARTBEAT = /* @__PURE__ */new TextEncoder().encode(":\n\n");
778
805
  function positionDigest(value) {
779
806
  if (value === undefined || !isJSONSafe(value)) return undefined;
780
807
  const text = JSON.stringify(value);
@@ -838,29 +865,36 @@ async function deserializeStream(source, codecOptions, wire) {
838
865
  throw new Error("missing body");
839
866
  }
840
867
  const reader = new ChunkReader(source.body);
841
- const result = await reader.next();
842
- if (!result.done) {
843
- if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
844
- throw errorFromTrailer(result.value);
845
- }
846
- const {
847
- createJSONDeserializer
848
- } = await import('@solidjs/web/serialization/decode');
849
- const deserializeChunk = createJSONDeserializer(codecOptions);
850
- function interpretChunk(chunk) {
851
- if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
852
- throw errorFromTrailer(chunk);
868
+ try {
869
+ const result = await reader.next();
870
+ if (!result.done) {
871
+ if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
872
+ throw errorFromTrailer(result.value);
853
873
  }
854
- return deserializeChunk(JSON.parse(chunk));
874
+ const {
875
+ createJSONDeserializer
876
+ } = await import('@solidjs/web/serialization/decode');
877
+ const deserializeChunk = createJSONDeserializer(codecOptions);
878
+ function interpretChunk(chunk) {
879
+ if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
880
+ throw errorFromTrailer(chunk);
881
+ }
882
+ return deserializeChunk(JSON.parse(chunk));
883
+ }
884
+ const connection = wire && wire.connection;
885
+ let endConnection;
886
+ if (connection) ;
887
+ const end = error => {
888
+ const sweep = () => deserializeChunk.abort(error);
889
+ if (connection) ; else sweep();
890
+ };
891
+ reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
892
+ return interpretChunk(result.value);
855
893
  }
856
- const end = error => {
857
- const sweep = () => deserializeChunk.abort(error);
858
- sweep();
859
- };
860
- reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
861
- return interpretChunk(result.value);
894
+ } catch (error) {
895
+ reader.cancel(error).catch(() => {});
896
+ throw error;
862
897
  }
863
- return undefined;
864
898
  }
865
899
  async function deserializeString(text, codecOptions) {
866
900
  return await deserializeStream(new Response(text), codecOptions);
@@ -948,10 +982,13 @@ function copyInitHeaders(init) {
948
982
  for (const cookie of init.getSetCookie()) headers.append("Set-Cookie", cookie);
949
983
  return headers;
950
984
  }
951
- const STUB_GAP_FILL_EXCLUDED = /*#__PURE__*/new Set([ERROR_HEADER, BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER, REVALIDATE_HEADER, REDIRECT_HEADER, "Location",
952
- ...COMPOSED_BODY_FRAMING].map(header => header.toLowerCase()));
985
+ let stubGapFillExcluded;
986
+ function isStubGapFillExcluded(key) {
987
+ return (stubGapFillExcluded ||= new Set([ERROR_HEADER, BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER, REVALIDATE_HEADER, REDIRECT_HEADER, "Location",
988
+ ...COMPOSED_BODY_FRAMING].map(header => header.toLowerCase()))).has(key);
989
+ }
953
990
  function fillsStubGap(key, headers, response) {
954
- if (key === "set-cookie" || STUB_GAP_FILL_EXCLUDED.has(key)) return false;
991
+ if (key === "set-cookie" || isStubGapFillExcluded(key)) return false;
955
992
  if (response.body === null && (key === "content-type" || key === "content-length")) return false;
956
993
  return !headers.has(key);
957
994
  }
@@ -372,7 +372,7 @@ function serializeCookie(name, value, options = {}) {
372
372
  return cookie;
373
373
  }
374
374
  const FLASH_COOKIE = "flash";
375
- const FLASH_MATCHER = new RegExp(`(?:^|;\\s*)${FLASH_COOKIE}=([^;]+)`);
375
+ const FLASH_MATCHER = /(?:^|;\s*)flash=([^;]+)/;
376
376
  function hasFlashCookie(cookieHeader) {
377
377
  return !!cookieHeader && FLASH_MATCHER.test(cookieHeader);
378
378
  }
@@ -690,7 +690,7 @@ async function extractBody(source, codecOptions, wire) {
690
690
  const format = source.headers.get(BODY_FORMAT_HEADER);
691
691
  switch (true) {
692
692
  case format === BodyFormat.Serialized:
693
- return await deserializeStream(source, codecOptions);
693
+ return await deserializeStream(source, codecOptions, wire);
694
694
  case format === BodyFormat.Json:
695
695
  return JSON.parse(await source.text());
696
696
  case format === BodyFormat.String:
@@ -716,26 +716,42 @@ async function extractBody(source, codecOptions, wire) {
716
716
  return undefined;
717
717
  }
718
718
  function createChunk(data) {
719
- const encoder = new TextEncoder();
720
- const encodeData = encoder.encode(data);
719
+ const encodeData = CHUNK_ENCODER.encode(data);
721
720
  const bytes = encodeData.length;
721
+ if (bytes > MAX_CHUNK_BYTES) {
722
+ throw new RangeError("Server function chunk is too large to frame.");
723
+ }
722
724
  const chunk = new Uint8Array(12 + bytes);
723
- chunk.set(encoder.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
725
+ chunk.set(CHUNK_ENCODER.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
724
726
  chunk.set(encodeData, 12);
725
727
  return chunk;
726
728
  }
729
+ const CHUNK_ENCODER = /* @__PURE__ */new TextEncoder();
730
+ const CHUNK_DECODER = /* @__PURE__ */new TextDecoder("utf-8", {
731
+ fatal: true
732
+ });
733
+ const MAX_CHUNK_BYTES = 0xffffffff;
734
+ const RETAINED_STORE_BYTES = 64 * 1024;
735
+ const DONE = {
736
+ done: true,
737
+ value: undefined
738
+ };
739
+ function malformedStream() {
740
+ return new Error("Malformed server function stream.");
741
+ }
727
742
  class ChunkReader {
728
743
  constructor(stream) {
729
744
  this.reader = stream.getReader();
730
745
  this.store = new Uint8Array(0);
731
746
  this.buffer = this.store;
732
747
  this.done = false;
748
+ this.cancelled = false;
733
749
  }
734
750
  async readChunk() {
735
751
  const chunk = await this.reader.read();
736
752
  if (chunk.done) {
737
753
  this.done = true;
738
- return;
754
+ return this.cancelled;
739
755
  }
740
756
  const incoming = chunk.value;
741
757
  const store = this.store;
@@ -756,55 +772,66 @@ class ChunkReader {
756
772
  this.store = grown;
757
773
  this.buffer = grown.subarray(0, needed);
758
774
  }
775
+ return this.cancelled;
759
776
  }
760
777
  async next() {
778
+ if (this.cancelled) return DONE;
761
779
  while (this.buffer.length < 12) {
762
780
  if (this.done) {
763
- if (this.buffer.length === 0) return {
764
- done: true,
765
- value: undefined
766
- };
767
- throw new Error("Malformed server function stream.");
781
+ if (this.buffer.length === 0) return DONE;
782
+ throw malformedStream();
768
783
  }
769
- await this.readChunk();
770
- }
771
- const decoder = new TextDecoder();
772
- const bytes = Number.parseInt(decoder.decode(this.buffer.subarray(1, 11)), 16);
773
- if (Number.isNaN(bytes)) {
774
- throw new Error("Malformed server function stream.");
784
+ if (await this.readChunk()) return DONE;
775
785
  }
786
+ const header = String.fromCharCode(...this.buffer.subarray(0, 12));
787
+ if (!/^;0x[\dA-Fa-f]{8};$/.test(header)) throw malformedStream();
788
+ const bytes = parseInt(header.slice(3, 11), 16);
776
789
  while (bytes > this.buffer.length - 12) {
777
790
  if (this.done) {
778
- throw new Error("Malformed server function stream.");
791
+ throw malformedStream();
779
792
  }
780
- await this.readChunk();
793
+ if (await this.readChunk()) return DONE;
794
+ }
795
+ let partial;
796
+ try {
797
+ partial = CHUNK_DECODER.decode(this.buffer.subarray(12, 12 + bytes));
798
+ } catch {
799
+ throw malformedStream();
781
800
  }
782
- const partial = decoder.decode(this.buffer.subarray(12, 12 + bytes));
783
801
  this.buffer = this.buffer.subarray(12 + bytes);
802
+ if (this.store.length > RETAINED_STORE_BYTES && this.buffer.length <= RETAINED_STORE_BYTES) {
803
+ this.store = this.buffer = this.buffer.slice();
804
+ }
784
805
  return {
785
806
  done: false,
786
807
  value: partial
787
808
  };
788
809
  }
789
810
  async drain(interpret) {
790
- while (true) {
791
- const result = await this.next();
792
- if (result.done) {
793
- break;
811
+ try {
812
+ while (true) {
813
+ const result = await this.next();
814
+ if (result.done) {
815
+ break;
816
+ }
817
+ interpret(result.value);
794
818
  }
795
- interpret(result.value);
819
+ } catch (error) {
820
+ this.cancel(error).catch(() => {});
821
+ throw error;
796
822
  }
797
823
  }
798
824
  cancel(reason) {
825
+ this.cancelled = true;
799
826
  return this.reader.cancel(reason);
800
827
  }
801
828
  }
802
829
  function createEventChunk(data, id) {
803
830
  let event = id !== undefined && !id.includes("\0") ? `id: ${id}\n` : "";
804
831
  for (const line of data.split(/\r\n|\r|\n/)) event += `data: ${line}\n`;
805
- return new TextEncoder().encode(event + "\n");
832
+ return CHUNK_ENCODER.encode(event + "\n");
806
833
  }
807
- const EVENT_STREAM_HEARTBEAT = new TextEncoder().encode(":\n\n");
834
+ const EVENT_STREAM_HEARTBEAT = /* @__PURE__ */new TextEncoder().encode(":\n\n");
808
835
  function positionDigest(value) {
809
836
  if (value === undefined || !isJSONSafe(value)) return undefined;
810
837
  const text = JSON.stringify(value);
@@ -868,29 +895,36 @@ async function deserializeStream(source, codecOptions, wire) {
868
895
  throw new Error("missing body");
869
896
  }
870
897
  const reader = new ChunkReader(source.body);
871
- const result = await reader.next();
872
- if (!result.done) {
873
- if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
874
- throw errorFromTrailer(result.value);
875
- }
876
- const {
877
- createJSONDeserializer
878
- } = await import('@solidjs/web/serialization/decode');
879
- const deserializeChunk = createJSONDeserializer(codecOptions);
880
- function interpretChunk(chunk) {
881
- if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
882
- throw errorFromTrailer(chunk);
898
+ try {
899
+ const result = await reader.next();
900
+ if (!result.done) {
901
+ if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
902
+ throw errorFromTrailer(result.value);
883
903
  }
884
- return deserializeChunk(JSON.parse(chunk));
904
+ const {
905
+ createJSONDeserializer
906
+ } = await import('@solidjs/web/serialization/decode');
907
+ const deserializeChunk = createJSONDeserializer(codecOptions);
908
+ function interpretChunk(chunk) {
909
+ if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
910
+ throw errorFromTrailer(chunk);
911
+ }
912
+ return deserializeChunk(JSON.parse(chunk));
913
+ }
914
+ const connection = wire && wire.connection;
915
+ let endConnection;
916
+ if (connection) ;
917
+ const end = error => {
918
+ const sweep = () => deserializeChunk.abort(error);
919
+ if (connection) ; else sweep();
920
+ };
921
+ reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
922
+ return interpretChunk(result.value);
885
923
  }
886
- const end = error => {
887
- const sweep = () => deserializeChunk.abort(error);
888
- sweep();
889
- };
890
- reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
891
- return interpretChunk(result.value);
924
+ } catch (error) {
925
+ reader.cancel(error).catch(() => {});
926
+ throw error;
892
927
  }
893
- return undefined;
894
928
  }
895
929
  async function deserializeString(text, codecOptions) {
896
930
  return await deserializeStream(new Response(text), codecOptions);
@@ -1000,10 +1034,13 @@ function copyInitHeaders(init) {
1000
1034
  for (const cookie of init.getSetCookie()) headers.append("Set-Cookie", cookie);
1001
1035
  return headers;
1002
1036
  }
1003
- const STUB_GAP_FILL_EXCLUDED = /*#__PURE__*/new Set([ERROR_HEADER, BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER, REVALIDATE_HEADER, REDIRECT_HEADER, "Location",
1004
- ...COMPOSED_BODY_FRAMING].map(header => header.toLowerCase()));
1037
+ let stubGapFillExcluded;
1038
+ function isStubGapFillExcluded(key) {
1039
+ return (stubGapFillExcluded ||= new Set([ERROR_HEADER, BODY_FORMAT_HEADER, SINGLE_FLIGHT_HEADER, REVALIDATE_HEADER, REDIRECT_HEADER, "Location",
1040
+ ...COMPOSED_BODY_FRAMING].map(header => header.toLowerCase()))).has(key);
1041
+ }
1005
1042
  function fillsStubGap(key, headers, response) {
1006
- if (key === "set-cookie" || STUB_GAP_FILL_EXCLUDED.has(key)) return false;
1043
+ if (key === "set-cookie" || isStubGapFillExcluded(key)) return false;
1007
1044
  if (response.body === null && (key === "content-type" || key === "content-length")) return false;
1008
1045
  return !headers.has(key);
1009
1046
  }