@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
@@ -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);
873
+ }
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));
853
883
  }
854
- return deserializeChunk(JSON.parse(chunk));
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
  }
@@ -1506,20 +1543,28 @@ function createServerReference({
1506
1543
  const scope = run => provideEvent(evt, run);
1507
1544
  const wrap = resolveWrapInvocation(config.wrapInvocation);
1508
1545
  const hook = requestErrorHook(ogEvt);
1509
- let result = provideEventOnce(provideEvent, evt, () => {
1510
- const run = () => fn.apply(thisArg, args);
1511
- return observeInvocation({
1512
- id,
1513
- direct: true,
1514
- event: evt,
1515
- args
1516
- }, () => reportDirectFailure(() => wrap ? wrap(run, {
1517
- id,
1518
- args,
1519
- event: evt,
1520
- direct: true
1521
- }) : run(), id, hook));
1522
- });
1546
+ let result;
1547
+ try {
1548
+ result = provideEventOnce(provideEvent, evt, () => {
1549
+ const run = () => fn.apply(thisArg, args);
1550
+ return observeInvocation({
1551
+ id,
1552
+ direct: true,
1553
+ event: evt,
1554
+ args
1555
+ }, () => reportDirectFailure(() => wrap ? wrap(run, {
1556
+ id,
1557
+ args,
1558
+ event: evt,
1559
+ direct: true
1560
+ }) : run(), id, hook));
1561
+ });
1562
+ } catch (error) {
1563
+ throw directEnvelopeValue(error, evt);
1564
+ }
1565
+ result = result && typeof result.then === "function" ? result.then(value => directEnvelopeValue(value, evt), error => {
1566
+ throw directEnvelopeValue(error, evt);
1567
+ }) : directEnvelopeValue(result, evt);
1523
1568
  result = scopeDeferredResult(result, scope);
1524
1569
  const transform = config.transformDirectResult;
1525
1570
  if (transform && result && typeof result.then === "function") {
@@ -2403,7 +2448,7 @@ function encodeLiveResult(value, headers, status, codec, request, scope) {
2403
2448
  function encodeResult(value, headers, status, codec, signal, scope) {
2404
2449
  if (NULL_BODY_STATUSES.has(status)) {
2405
2450
  if (value === undefined || value === null) {
2406
- headers.set(BODY_FORMAT_HEADER, BodyFormat.Void);
2451
+ if (status !== 304) headers.set(BODY_FORMAT_HEADER, BodyFormat.Void);
2407
2452
  return new Response(null, {
2408
2453
  status,
2409
2454
  headers
@@ -2490,6 +2535,7 @@ function currentEvent() {
2490
2535
  }
2491
2536
  function reportDirectFailure(run, id, hook) {
2492
2537
  const report = error => {
2538
+ if (error instanceof Response || isResponseEnvelope(error)) throw error;
2493
2539
  reportServerError(error, {
2494
2540
  kind: "server-function",
2495
2541
  handling: "thrown",
@@ -2506,6 +2552,20 @@ function reportDirectFailure(run, id, hook) {
2506
2552
  }
2507
2553
  return result && typeof result.then === "function" ? result.then(undefined, report) : result;
2508
2554
  }
2555
+ function directEnvelopeValue(result, event) {
2556
+ if (!isResponseEnvelope(result)) return result;
2557
+ const {
2558
+ response,
2559
+ value
2560
+ } = result;
2561
+ const stub = event.response;
2562
+ if (response && stub && stub.headers && response.headers.getSetCookie) {
2563
+ for (const cookie of response.headers.getSetCookie()) {
2564
+ stub.headers.append("Set-Cookie", cookie);
2565
+ }
2566
+ }
2567
+ return value;
2568
+ }
2509
2569
  function sanitizeServerError(value) {
2510
2570
  if (DEV) return value;
2511
2571
  if (isSafeError(value)) return value;
@@ -2743,8 +2803,14 @@ async function handleServerFunctionRequest(request, options = {}) {
2743
2803
  });
2744
2804
  return finish(protectsRequest ? withCSRFVary(response) : response);
2745
2805
  }
2806
+ const commitResponse = raw => {
2807
+ if ((method === "GET" || method === "HEAD") && (sharedCacheMayStore(departingHeader(raw, event, "Cache-Control")) || cdnMayStore(departingHeader(raw, event, "CDN-Cache-Control")))) {
2808
+ withholdTrace(event);
2809
+ }
2810
+ return commitEventResponse(raw, event);
2811
+ };
2746
2812
  const refuseCommitted = raw => {
2747
- const response = commitEventResponse(raw, event);
2813
+ const response = commitResponse(raw);
2748
2814
  return finish(protectsRequest ? withCSRFVary(response) : response);
2749
2815
  };
2750
2816
  const provide = options.provideEvent || provideEvent;
@@ -2962,9 +3028,54 @@ async function handleServerFunctionRequest(request, options = {}) {
2962
3028
  return respondThrown(x);
2963
3029
  }
2964
3030
  };
2965
- const response = commitEventResponse(enforceComposedHeaderInvariants(ownResponse(await dispatch())), event);
3031
+ const response = commitResponse(enforceComposedHeaderInvariants(ownResponse(await dispatch())));
2966
3032
  return finish(protectsRequest ? withCSRFVary(response) : response);
2967
3033
  }
3034
+ function departingHeader(response, event, name) {
3035
+ const own = response.headers.get(name);
3036
+ if (own !== null) return own;
3037
+ const stub = event.response;
3038
+ return stub && stub.headers && !stub.committed ? stub.headers.get(name) : null;
3039
+ }
3040
+ function cdnMayStore(cdnCacheControl) {
3041
+ return cdnCacheControl !== null && !forbidsStoring(cdnCacheControl);
3042
+ }
3043
+ function sharedCacheMayStore(cacheControl) {
3044
+ let shared = false;
3045
+ return !!cacheControl && !eachDirective(cacheControl, name => {
3046
+ if (name === "public" || name === "s-maxage") shared = true;
3047
+ }) && shared;
3048
+ }
3049
+ function forbidsStoring(value) {
3050
+ return eachDirective(value, () => {});
3051
+ }
3052
+ function eachDirective(value, visit) {
3053
+ let start = 0;
3054
+ let quoted = false;
3055
+ for (let i = 0; i <= value.length; i++) {
3056
+ const c = value[i];
3057
+ if (quoted) {
3058
+ if (c === "\\") i++;else if (c === '"') quoted = false;
3059
+ continue;
3060
+ }
3061
+ if (c === '"') {
3062
+ quoted = true;
3063
+ continue;
3064
+ }
3065
+ if (c !== "," && i < value.length) continue;
3066
+ const directive = value.slice(start, i);
3067
+ start = i + 1;
3068
+ const eq = directive.indexOf("=");
3069
+ const name = (eq === -1 ? directive : directive.slice(0, eq)).trim().toLowerCase();
3070
+ if (name === "no-store" || name === "private" && eq === -1) return true;
3071
+ visit(name);
3072
+ }
3073
+ return false;
3074
+ }
3075
+ function withholdTrace(event) {
3076
+ if (event.response && event.response.committed) return;
3077
+ traceForEvent(event).emit = false;
3078
+ }
2968
3079
  function ownResponse(response) {
2969
3080
  try {
2970
3081
  return new Response(response.body, response);