@solidjs/web 2.0.0-rc.8 → 2.0.0-rc.9

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 (36) hide show
  1. package/dist/server.dev.js +626 -125
  2. package/dist/server.js +433 -95
  3. package/dist/server.observe.js +3980 -0
  4. package/dist/web.dev.js +208 -60
  5. package/dist/web.js +175 -58
  6. package/dist/web.observe.js +175 -58
  7. package/frames/dist/client.dev.js +103 -4
  8. package/frames/dist/client.js +5 -2
  9. package/frames/dist/client.observe.js +2123 -0
  10. package/frames/dist/server.dev.js +521 -79
  11. package/frames/dist/server.js +352 -52
  12. package/frames/dist/server.observe.js +4946 -0
  13. package/frames/package.json +12 -0
  14. package/package.json +87 -3
  15. package/server-functions/dist/client.dev.js +1102 -0
  16. package/server-functions/dist/client.js +74 -69
  17. package/server-functions/dist/client.observe.js +1102 -0
  18. package/server-functions/dist/server.dev.js +402 -31
  19. package/server-functions/dist/server.js +339 -30
  20. package/server-functions/dist/server.observe.js +2726 -0
  21. package/server-functions/package.json +12 -0
  22. package/types/client.d.ts +22 -1
  23. package/types/diagnostics.d.ts +24 -0
  24. package/types/frames/frame-transport.d.ts +1 -1
  25. package/types/index.d.ts +25 -15
  26. package/types/index.server.d.ts +35 -8
  27. package/types/jsx.d.ts +15 -4
  28. package/types/observe.d.ts +294 -0
  29. package/types/response.d.ts +18 -0
  30. package/types/server-functions/client.d.ts +32 -22
  31. package/types/server-functions/server.d.ts +28 -37
  32. package/types/server-functions/shared.d.ts +39 -9
  33. package/types/server-mock.d.ts +26 -4
  34. package/types/server-observe.d.ts +39 -0
  35. package/types/server.d.ts +104 -5
  36. package/types/trace.d.ts +79 -0
@@ -164,6 +164,40 @@ function parseServerFunctionAddress(pathname, endpoint) {
164
164
  return null;
165
165
  }
166
166
  }
167
+ const MAX_GET_URL_LENGTH = 2000;
168
+ function urlTargetId(target, helper) {
169
+ if (typeof target === "string") return target;
170
+ if (isServerFunction(target) && typeof target.id === "string") return target.id;
171
+ throw new Error(`${helper} expects a server function reference (or its id).`);
172
+ }
173
+ function serverFunctionActionUrlFor(endpoint, target, boundArgs) {
174
+ const address = serverFunctionAddress(endpoint, urlTargetId(target, "serverFunctionActionUrl"));
175
+ if (!boundArgs.length) return address;
176
+ if (!isJSONSafe(boundArgs)) {
177
+ throw new Error("Bound arguments in an action url must be JSON-safe: the server reads them the way it " + "reads a form post's, and that convention has no codec. Pass the value through the " + "function's body, or call the reference instead of rendering a url for it.");
178
+ }
179
+ return `${address}?args=${encodeURIComponent(JSON.stringify(boundArgs))}`;
180
+ }
181
+ function serverFunctionUrlFor(endpoint, fn, args) {
182
+ if (!isServerFunction(fn) || typeof fn.id !== "string") {
183
+ throw new Error("serverFunctionUrl expects a server function reference.");
184
+ }
185
+ const metadata = getServerFunctionMetadata(fn);
186
+ if (!metadata || metadata.method !== "GET") {
187
+ throw new Error(`serverFunctionUrl: "${fn.id}" is not a declared read. A call over the default ` + "transport is a POST — its arguments travel in the body, so no url describes it. " + "Declare the function with GET(fn) for a cacheable read that has a url, or start " + 'the call itself: invoke(fn, { priority: "low" }, ...args).');
188
+ }
189
+ const address = serverFunctionDataAddress(endpoint, fn.id);
190
+ if (!args.length) return address;
191
+ if (!isJSONSafe(args)) {
192
+ throw new Error("Arguments in a server function url must be JSON-safe: the transport encodes anything " + "else through the codec asynchronously, and a url rendered as a value cannot wait for " + "it. Pass JSON-safe arguments, or call the reference instead of rendering a url for it.");
193
+ }
194
+ const url = `${address}?args=${encodeURIComponent(JSON.stringify(args))}`;
195
+ const length = new URL(url, globalThis.location?.href || "http://localhost").href.length;
196
+ if (length > MAX_GET_URL_LENGTH) {
197
+ throw new Error(`serverFunctionUrl: the url for "${fn.id}" with these arguments is ${length} characters; ` + `past ${MAX_GET_URL_LENGTH} the call dispatches over POST (a cache miss rather than a ` + "414), so no url describes it. Shorten the arguments, or call the reference instead.");
198
+ }
199
+ return url;
200
+ }
167
201
  const ERROR_HEADER = "X-Server-Function-Error";
168
202
  const ERROR_HEADER_MARKER = "=?1?";
169
203
  const NEEDS_ENCODING = /[^\x20-\x7e\xa0-\xff]/;
@@ -189,7 +223,6 @@ function decodeErrorHeaderValue(value) {
189
223
  return value;
190
224
  }
191
225
  }
192
- const INSTANCE_HEADER = "X-Server-Function-Instance";
193
226
  const BODY_FORMAT_HEADER = "X-Server-Function-Format";
194
227
  const UNKNOWN_HEADER = "X-Server-Function-Unknown";
195
228
  const REDIRECT_HEADER = "X-Server-Function-Redirect";
@@ -522,39 +555,13 @@ const config = {
522
555
  responseHandler: undefined,
523
556
  serializeArgs: undefined
524
557
  };
525
- const CALL_OBSERVERS = new Set();
526
- function notifyCallObservers(type, id, instance, value, meta) {
527
- if (CALL_OBSERVERS.size === 0) return;
528
- const field = type === "request" ? "request" : "response";
529
- const time = performance.now();
530
- for (const observer of new Set(CALL_OBSERVERS)) {
531
- try {
532
- observer({
533
- type,
534
- id,
535
- instance,
536
- [field]: value.clone(),
537
- meta,
538
- time
539
- });
540
- } catch (error) {
541
- console.error(error);
542
- }
543
- }
558
+ function serverFunctionUrl(fn, ...args) {
559
+ return serverFunctionUrlFor(config.endpoint, fn, args);
544
560
  }
545
- function observeServerFunctionCalls(observer) {
546
- CALL_OBSERVERS.add(observer);
547
- return () => CALL_OBSERVERS.delete(observer);
561
+ function serverFunctionActionUrl(fn, ...boundArgs) {
562
+ return serverFunctionActionUrlFor(config.endpoint, fn, boundArgs);
548
563
  }
549
- function serverFunctionUrl(id, boundArgs) {
550
- const address = serverFunctionAddress(config.endpoint, id);
551
- if (!boundArgs || !boundArgs.length) return address;
552
- if (!isJSONSafe(boundArgs)) {
553
- throw new Error("Bound arguments in an action url must be JSON-safe: the server reads them the way it " + "reads a form post's, and that convention has no codec. Pass the value through the " + "function's body, or call the reference instead of rendering a url for it.");
554
- }
555
- return `${address}?args=${encodeURIComponent(JSON.stringify(boundArgs))}`;
556
- }
557
- function parseServerFunctionUrl(url) {
564
+ function parseServerFunctionActionUrl(url) {
558
565
  const parsed = parseServerFunctionAddress(new URL(url, globalThis.location?.href || "http://localhost").pathname, config.endpoint);
559
566
  return parsed && parsed.id;
560
567
  }
@@ -579,8 +586,6 @@ function configureServerFunctionsClient({
579
586
  if (responseHandler !== undefined) config.responseHandler = responseHandler;
580
587
  if (serializeArgs !== undefined) config.serializeArgs = serializeArgs;
581
588
  }
582
- let INSTANCE = 0;
583
- const MAX_GET_URL_LENGTH = 2000;
584
589
  let rpcProvided = false;
585
590
  function provideRPC() {
586
591
  if (rpcProvided) return;
@@ -617,13 +622,15 @@ function parseRetryAfter(header) {
617
622
  if (!Number.isNaN(date)) return Math.max(0, Math.ceil((date - Date.now()) / 1000));
618
623
  return undefined;
619
624
  }
620
- async function createRequest(base, id, instance, options, meta) {
625
+ function isReadCall(options) {
626
+ return !!options.read || !!options.method && options.method.toUpperCase() === "GET";
627
+ }
628
+ async function createRequest(base, id, options, meta) {
621
629
  const headers = {
622
- ...options.headers,
623
- [INSTANCE_HEADER]: instance
630
+ ...options.headers
624
631
  };
625
632
  const flightSources = getFlightDataSourceIds();
626
- if (flightSources.length > 0 && !options.read && (!options.method || options.method.toUpperCase() !== "GET")) {
633
+ if (flightSources.length > 0 && !isReadCall(options)) {
627
634
  headers[SINGLE_FLIGHT_HEADER] = flightSources.join(",");
628
635
  }
629
636
  let init = {
@@ -640,31 +647,23 @@ async function createRequest(base, id, instance, options, meta) {
640
647
  if (typeof prepared !== "object") {
641
648
  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, ... } })");
642
649
  }
643
- if (!new Headers(prepared.headers).has(INSTANCE_HEADER)) {
644
- throw new Error("prepareRequest returned an init without the transport headers " + `(${INSTANCE_HEADER}), which would send the call without its payload or ` + "protocol. Spread the init it received: " + "init => ({ ...init, headers: { ...init.headers, ... } })");
650
+ if (typeof prepared.method !== "string" || prepared.method.toUpperCase() !== init.method.toUpperCase()) {
651
+ throw new Error(`prepareRequest returned an init without the transport's ${init.method} method, ` + "which would send the call without its payload or protocol. Spread the init it " + "received: init => ({ ...init, headers: { ...init.headers, ... } })");
645
652
  }
646
653
  }
647
654
  init = prepared || init;
648
655
  }
649
656
  const send = config.fetch || fetch;
650
- if (CALL_OBSERVERS.size === 0) return send(base, init);
651
- const request = new Request(new URL(base, globalThis.location?.href || "http://localhost"), {
652
- ...init,
653
- body: init.body instanceof ReadableStream ? undefined : init.body
654
- });
655
- notifyCallObservers("request", id, instance, request, meta);
656
- const response = await send(base, init);
657
- notifyCallObservers("response", id, instance, response, meta);
658
- return response;
657
+ return send(base, init);
659
658
  }
660
- async function initializeResponse(base, id, instance, options, args, meta) {
659
+ async function initializeResponse(base, id, options, args, meta) {
661
660
  if (args.length === 0) {
662
- return createRequest(base, id, instance, options, meta);
661
+ return createRequest(base, id, options, meta);
663
662
  }
664
663
  if (args.length === 1) {
665
664
  const result = getHeadersAndBody(args[0]);
666
665
  if (result) {
667
- return createRequest(base, id, instance, {
666
+ return createRequest(base, id, {
668
667
  ...options,
669
668
  body: result.body,
670
669
  headers: {
@@ -676,7 +675,7 @@ async function initializeResponse(base, id, instance, options, args, meta) {
676
675
  }
677
676
  try {
678
677
  if (isJSONSafe(args)) {
679
- return createRequest(base, id, instance, {
678
+ return createRequest(base, id, {
680
679
  ...options,
681
680
  body: JSON.stringify(args),
682
681
  headers: {
@@ -694,7 +693,7 @@ async function initializeResponse(base, id, instance, options, args, meta) {
694
693
  const leading = args.slice(0, -1).map(arg => arg === undefined ? null : arg);
695
694
  if (trailing && isJSONSafe(leading)) {
696
695
  const target = base + (base.includes("?") ? "&" : "?") + "args=" + encodeURIComponent(JSON.stringify(leading));
697
- return createRequest(target, id, instance, {
696
+ return createRequest(target, id, {
698
697
  ...options,
699
698
  body: trailing.body,
700
699
  headers: {
@@ -706,7 +705,7 @@ async function initializeResponse(base, id, instance, options, args, meta) {
706
705
  } catch {
707
706
  }
708
707
  }
709
- return createRequest(base, id, instance, {
708
+ return createRequest(base, id, {
710
709
  ...options,
711
710
  body: await serializeArguments(args),
712
711
  headers: {
@@ -716,8 +715,8 @@ async function initializeResponse(base, id, instance, options, args, meta) {
716
715
  }
717
716
  }, meta);
718
717
  }
719
- async function fetchServerFunction(base, id, options, args, meta, callArgs = args) {
720
- const instance = `server-function:${INSTANCE++}`;
718
+ const fetchServerFunction = dispatchServerFunction;
719
+ async function dispatchServerFunction(base, id, options, args, meta, callArgs = args, observation) {
721
720
  const handler = config.responseHandler;
722
721
  const context = handler && handler.capture ? handler.capture({
723
722
  id,
@@ -728,7 +727,7 @@ async function fetchServerFunction(base, id, options, args, meta, callArgs = arg
728
727
  ...options,
729
728
  signal: controller.signal
730
729
  };
731
- const response = await initializeResponse(base, id, instance, options, args, meta);
730
+ const response = await initializeResponse(base, id, options, args, meta);
732
731
  if (handler) {
733
732
  const handled = handler.handle(response, {
734
733
  id,
@@ -742,20 +741,26 @@ async function fetchServerFunction(base, id, options, args, meta, callArgs = arg
742
741
  throw serverFunctionFailure(response, undefined);
743
742
  }
744
743
  const failed = response.headers.has(ERROR_HEADER);
745
- if (response.headers.has(SINGLE_FLIGHT_HEADER)) {
746
- const folded = response.headers.get(SINGLE_FLIGHT_HEADER).split(",");
747
- const consumers = folded.map(source => [source, getFlightDataConsumer(source)]).filter(([, consumer]) => consumer);
748
- if (consumers.length > 0) {
749
- const payload = response.body ? await extractBody(response, getServerFunctionsCodec()) : undefined;
750
- for (const [source, consumer] of consumers) {
751
- await consumer(payload.data[source], {
744
+ const registered = isReadCall(options) ? [] : getFlightDataSourceIds();
745
+ if (registered.length > 0) {
746
+ const folded = response.headers.has(SINGLE_FLIGHT_HEADER) ? response.headers.get(SINGLE_FLIGHT_HEADER).split(",") : [];
747
+ const metadata = response.headers.has(REDIRECT_HEADER) || response.headers.has(REVALIDATE_HEADER);
748
+ if (metadata || folded.length > 0) {
749
+ const decoded = response.body ? await extractBody(response, getServerFunctionsCodec()) : undefined;
750
+ const enveloped = folded.length > 0 && decoded !== undefined;
751
+ const value = enveloped ? decoded.value : decoded;
752
+ const data = enveloped ? decoded.data : undefined;
753
+ for (const source of registered) {
754
+ if (!metadata && !folded.includes(source)) continue;
755
+ const consumer = getFlightDataConsumer(source);
756
+ if (consumer) await consumer(data ? data[source] : undefined, {
752
757
  response
753
758
  });
754
759
  }
755
- if (failed && !response.headers.has(REDIRECT_HEADER) && !response.headers.has(REVALIDATE_HEADER)) {
756
- throw serverFunctionFailure(response, payload.value);
760
+ if (failed && !metadata) {
761
+ throw serverFunctionFailure(response, value);
757
762
  }
758
- return payload.value;
763
+ return value;
759
764
  }
760
765
  }
761
766
  if (response.headers.has(REDIRECT_HEADER) || response.headers.has(REVALIDATE_HEADER) || response.headers.has(SINGLE_FLIGHT_HEADER) || response.status >= 300 && response.status < 400 && response.status !== 304) {
@@ -1028,4 +1033,4 @@ function getServerFunctionInvocation() {
1028
1033
  return undefined;
1029
1034
  }
1030
1035
 
1031
- export { ChunkReader, ERROR_HEADER, FLASH_COOKIE, GET, INSTANCE_HEADER, REDIRECT_HEADER, REVALIDATE_HEADER, SERVER_FUNCTION_INVOKE, SINGLE_FLIGHT_HEADER, UNKNOWN_HEADER, clearFlashCookie, configureServerFunctionsClient, createChunk, createServerReference, decodeErrorHeaderValue, decodeRedirectHeaderValue, decodeResponse, decodeResponsePayload, deserializeStream, encodeErrorHeaderValue, frameAddress, getFlightDataConsumer, getFlightDataSourceIds, getServerFunctionInvocation, getServerFunctionMetadata, getServerFunctionsCodec, hasFlashCookie, invoke, isServerFunction, live, observeServerFunctionCalls, parseServerFunctionUrl, registerServerReference, serializeString, serverFunctionUrl, subscribeFlightData, withMeta };
1036
+ export { ChunkReader, ERROR_HEADER, FLASH_COOKIE, GET, REDIRECT_HEADER, REVALIDATE_HEADER, SERVER_FUNCTION_INVOKE, SINGLE_FLIGHT_HEADER, UNKNOWN_HEADER, clearFlashCookie, configureServerFunctionsClient, createChunk, createServerReference, decodeErrorHeaderValue, decodeRedirectHeaderValue, decodeResponse, decodeResponsePayload, deserializeStream, encodeErrorHeaderValue, frameAddress, getFlightDataConsumer, getFlightDataSourceIds, getServerFunctionInvocation, getServerFunctionMetadata, getServerFunctionsCodec, hasFlashCookie, invoke, isServerFunction, live, parseServerFunctionActionUrl, registerServerReference, serializeString, serverFunctionActionUrl, serverFunctionUrl, subscribeFlightData, withMeta };