@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
@@ -1,4 +1,5 @@
1
1
  import { sharedConfig } from 'solid-js';
2
+ import { reportServerError } from 'solid-js/internal';
2
3
 
3
4
  const COMPOSED_BODY_FRAMING = /*#__PURE__*/new Set(["content-length", "content-encoding", "transfer-encoding"]);
4
5
  function isHttpNavigationTarget(target) {
@@ -22,6 +23,135 @@ const REVALIDATE_HEADER = "X-Revalidate";
22
23
  const RESPONSE_HEADER_VALUE_LIMIT = 4096;
23
24
  const NULL_BODY_STATUSES = new Set([204, 205, 304]);
24
25
 
26
+ const TRACEPARENT = /^([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})(-.*)?$/;
27
+ const ZERO_TRACE = "00000000000000000000000000000000";
28
+ const ZERO_SPAN = "0000000000000000";
29
+ function parseTraceparent(value) {
30
+ if (!value) return undefined;
31
+ const m = TRACEPARENT.exec(value.trim());
32
+ if (!m) return undefined;
33
+ const [, version, traceId, parentId, flags, rest] = m;
34
+ if (version === "ff") return undefined;
35
+ if (version === "00" && rest !== undefined) return undefined;
36
+ if (traceId === ZERO_TRACE || parentId === ZERO_SPAN) return undefined;
37
+ return {
38
+ traceId,
39
+ parentId,
40
+ sampled: (parseInt(flags, 16) & 1) === 1
41
+ };
42
+ }
43
+ function randomHex(bytes) {
44
+ const buffer = new Uint8Array(bytes);
45
+ crypto.getRandomValues(buffer);
46
+ let out = "";
47
+ for (let i = 0; i < buffer.length; i++) out += (buffer[i] < 16 ? "0" : "") + buffer[i].toString(16);
48
+ return out;
49
+ }
50
+ function randomTraceId() {
51
+ let id = randomHex(16);
52
+ while (id === ZERO_TRACE) id = randomHex(16);
53
+ return id;
54
+ }
55
+ function randomSpanId() {
56
+ let id = randomHex(8);
57
+ while (id === ZERO_SPAN) id = randomHex(8);
58
+ return id;
59
+ }
60
+ function formatTraceparent(context) {
61
+ return `00-${context.traceId}-${context.spanId}-${context.sampled === true ? "01" : "00"}`;
62
+ }
63
+ const RECORDS_KEY = Symbol.for("@solidjs/web/trace/records");
64
+ function records$1() {
65
+ const g = globalThis;
66
+ return g[RECORDS_KEY] || (g[RECORDS_KEY] = new WeakMap());
67
+ }
68
+ function traceFor(key, request) {
69
+ const store = records$1();
70
+ let record = store.get(key);
71
+ if (record) return record;
72
+ const headers = request ? request.headers : undefined;
73
+ const incoming = headers ? parseTraceparent(headers.get("traceparent")) : undefined;
74
+ const context = {
75
+ traceId: incoming ? incoming.traceId : randomTraceId(),
76
+ spanId: randomSpanId(),
77
+ entries: {}
78
+ };
79
+ if (incoming) {
80
+ context.parentId = incoming.parentId;
81
+ context.sampled = incoming.sampled;
82
+ }
83
+ if (headers) {
84
+ const state = headers.get("tracestate");
85
+ if (state) context.state = state;
86
+ const baggage = headers.get("baggage");
87
+ if (baggage) context.baggage = baggage;
88
+ }
89
+ let answered = false;
90
+ context.entries.traceparent = formatTraceparent(context);
91
+ record = {
92
+ context,
93
+ emit: incoming !== undefined && incoming.sampled || answered
94
+ };
95
+ store.set(key, record);
96
+ return record;
97
+ }
98
+ const TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
99
+ function splitServerTiming(value) {
100
+ const entries = [];
101
+ let start = 0;
102
+ let quoted = false;
103
+ for (let i = 0; i < value.length; i++) {
104
+ const c = value[i];
105
+ if (quoted) {
106
+ if (c === "\\") i++;else if (c === '"') quoted = false;
107
+ } else if (c === '"') quoted = true;else if (c === ",") {
108
+ entries.push(value.slice(start, i));
109
+ start = i + 1;
110
+ }
111
+ }
112
+ entries.push(value.slice(start));
113
+ return entries;
114
+ }
115
+ function metricName(entry) {
116
+ return entry.split(";")[0].trim().toLowerCase();
117
+ }
118
+ function namesOnServerTiming(headers) {
119
+ const names = new Set();
120
+ const existing = headers.get("server-timing");
121
+ if (existing) {
122
+ for (const entry of splitServerTiming(existing)) {
123
+ const name = metricName(entry);
124
+ if (name) names.add(name);
125
+ }
126
+ }
127
+ return names;
128
+ }
129
+ function quoteDesc(value) {
130
+ return value.replace(/[\x00-\x1f\x7f]/g, "").replace(/[\\"]/g, m => "\\" + m);
131
+ }
132
+ function appendTraceServerTiming(headers, record) {
133
+ if (!record.emit) return;
134
+ const entries = record.context.entries;
135
+ let present;
136
+ for (const name in entries) {
137
+ if (!TOKEN.test(name)) continue;
138
+ if (!present) present = namesOnServerTiming(headers);
139
+ if (present.has(name.toLowerCase())) continue;
140
+ headers.append("Server-Timing", `${name};desc="${quoteDesc(entries[name])}"`);
141
+ present.add(name.toLowerCase());
142
+ }
143
+ }
144
+ function mergeServerTiming(target, value) {
145
+ const present = namesOnServerTiming(target);
146
+ for (const entry of splitServerTiming(value)) {
147
+ const trimmed = entry.trim();
148
+ const name = metricName(trimmed);
149
+ if (!name || present.has(name)) continue;
150
+ target.append("Server-Timing", trimmed);
151
+ present.add(name);
152
+ }
153
+ }
154
+
25
155
  const SERVER_FUNCTION_METADATA = Symbol.for("solid.ServerFunctionMetadata");
26
156
  function getServerFunctionMetadata(fn) {
27
157
  if (typeof fn !== "function") return undefined;
@@ -148,6 +278,10 @@ function serverFunctionAddress(endpoint, id) {
148
278
  const mount = endpoint.endsWith("/") ? endpoint.slice(0, -1) : endpoint;
149
279
  return `${mount}/${encodeURIComponent(id)}`;
150
280
  }
281
+ function serverFunctionDataAddress(endpoint, id) {
282
+ const mount = endpoint.endsWith("/") ? endpoint.slice(0, -1) : endpoint;
283
+ return `${mount}/data/${encodeURIComponent(id)}`;
284
+ }
151
285
  function parseServerFunctionAddress(pathname, endpoint) {
152
286
  const mount = endpoint.endsWith("/") ? endpoint.slice(0, -1) : endpoint;
153
287
  if (!pathname.startsWith(mount)) return null;
@@ -169,6 +303,40 @@ function parseServerFunctionAddress(pathname, endpoint) {
169
303
  return null;
170
304
  }
171
305
  }
306
+ const MAX_GET_URL_LENGTH = 2000;
307
+ function urlTargetId(target, helper) {
308
+ if (typeof target === "string") return target;
309
+ if (isServerFunction(target) && typeof target.id === "string") return target.id;
310
+ throw new Error(`${helper} expects a server function reference (or its id).`);
311
+ }
312
+ function serverFunctionActionUrlFor(endpoint, target, boundArgs) {
313
+ const address = serverFunctionAddress(endpoint, urlTargetId(target, "serverFunctionActionUrl"));
314
+ if (!boundArgs.length) return address;
315
+ if (!isJSONSafe(boundArgs)) {
316
+ 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.");
317
+ }
318
+ return `${address}?args=${encodeURIComponent(JSON.stringify(boundArgs))}`;
319
+ }
320
+ function serverFunctionUrlFor(endpoint, fn, args) {
321
+ if (!isServerFunction(fn) || typeof fn.id !== "string") {
322
+ throw new Error("serverFunctionUrl expects a server function reference.");
323
+ }
324
+ const metadata = getServerFunctionMetadata(fn);
325
+ if (!metadata || metadata.method !== "GET") {
326
+ 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).');
327
+ }
328
+ const address = serverFunctionDataAddress(endpoint, fn.id);
329
+ if (!args.length) return address;
330
+ if (!isJSONSafe(args)) {
331
+ 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.");
332
+ }
333
+ const url = `${address}?args=${encodeURIComponent(JSON.stringify(args))}`;
334
+ const length = new URL(url, globalThis.location?.href || "http://localhost").href.length;
335
+ if (length > MAX_GET_URL_LENGTH) {
336
+ 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.");
337
+ }
338
+ return url;
339
+ }
172
340
  const ERROR_HEADER = "X-Server-Function-Error";
173
341
  const ERROR_HEADER_MARKER = "=?1?";
174
342
  const NEEDS_ENCODING = /[^\x20-\x7e\xa0-\xff]/;
@@ -194,7 +362,6 @@ function decodeErrorHeaderValue(value) {
194
362
  return value;
195
363
  }
196
364
  }
197
- const INSTANCE_HEADER = "X-Server-Function-Instance";
198
365
  const BODY_FORMAT_HEADER = "X-Server-Function-Format";
199
366
  const UNKNOWN_HEADER = "X-Server-Function-Unknown";
200
367
  const REDIRECT_HEADER = "X-Server-Function-Redirect";
@@ -512,16 +679,35 @@ const RequestContext = Symbol.for("solid.RequestContext");
512
679
  function getRequestEvent() {
513
680
  return globalThis[RequestContext] ? globalThis[RequestContext].getStore() || sharedConfig.context && sharedConfig.context.event || console.warn("RequestEvent is missing. This is most likely due to accessing `getRequestEvent` non-managed async scope in a partially polyfilled environment. Try moving it above all `await` calls.") : undefined;
514
681
  }
682
+ function peekRequestEvent() {
683
+ const store = globalThis[RequestContext];
684
+ return store ? store.getStore() : undefined;
685
+ }
686
+ function traceForEvent(event) {
687
+ return traceFor(event.request || event, event.request);
688
+ }
689
+ function traceForStub(stub, event) {
690
+ if (!event) {
691
+ event = peekRequestEvent();
692
+ if (!event || event.response !== stub) return undefined;
693
+ }
694
+ return traceForEvent(event);
695
+ }
515
696
  function reportLostHeaderWrite(method, name) {
516
- const message = `Response header write dropped: headers.${method}(${JSON.stringify(String(name))}) ` + "ran after the response head was sent. Write headers before the shell flushes " + "(or before the handler returns).";
697
+ const message = `[LATE_HEADER_WRITE] Response header write dropped: headers.${method}(${JSON.stringify(String(name))}) ` + "ran after the response head was sent. Write headers before the shell flushes " + "(or before the handler returns).";
517
698
  console.error(message);
518
699
  }
519
700
  function commitResponseStub(stub, {
520
- allowLateLocation = false
701
+ allowLateLocation = false,
702
+ event
521
703
  } = {}) {
522
704
  if (!stub || stub.committed) return stub;
523
- stub.committed = true;
524
705
  const headers = stub.headers;
706
+ if (headers && typeof headers.append === "function") {
707
+ const trace = traceForStub(stub, event);
708
+ if (trace) appendTraceServerTiming(headers, trace);
709
+ }
710
+ stub.committed = true;
525
711
  if (!headers || typeof headers.set !== "function") return stub;
526
712
  for (const method of ["set", "append", "delete"]) {
527
713
  const original = headers[method].bind(headers);
@@ -552,19 +738,35 @@ function fillsStubGap(key, headers, response) {
552
738
  }
553
739
  function commitEventResponse(response, event = getRequestEvent()) {
554
740
  const stub = event && event.response;
555
- if (!stub || !stub.headers || stub.committed) return response;
741
+ if (!stub || !stub.headers || stub.committed) {
742
+ if (!event || stub) return response;
743
+ const trace = traceForEvent(event);
744
+ if (!trace.emit) return response;
745
+ const headers = copyInitHeaders(response.headers);
746
+ appendTraceServerTiming(headers, trace);
747
+ return new Response(response.body, {
748
+ status: response.status,
749
+ statusText: response.statusText,
750
+ headers
751
+ });
752
+ }
556
753
  const cookies = stub.headers.getSetCookie ? stub.headers.getSetCookie() : [];
557
- commitResponseStub(stub);
754
+ commitResponseStub(stub, {
755
+ event
756
+ });
558
757
  let hasGaps = false;
559
758
  stub.headers.forEach((value, key) => {
560
759
  if (fillsStubGap(key, response.headers, response)) hasGaps = true;
561
760
  });
562
- if (!cookies.length && !hasGaps) return response;
761
+ const timing = stub.headers.get("server-timing");
762
+ const mergesTiming = timing !== null && response.headers.has("server-timing");
763
+ if (!cookies.length && !hasGaps && !mergesTiming) return response;
563
764
  const headers = copyInitHeaders(response.headers);
564
765
  for (const cookie of cookies) headers.append("Set-Cookie", cookie);
565
766
  stub.headers.forEach((value, key) => {
566
767
  if (fillsStubGap(key, headers, response)) headers.set(key, value);
567
768
  });
769
+ if (mergesTiming) mergeServerTiming(headers, timing);
568
770
  return new Response(response.body, {
569
771
  status: response.status,
570
772
  statusText: response.statusText,
@@ -572,6 +774,70 @@ function commitEventResponse(response, event = getRequestEvent()) {
572
774
  });
573
775
  }
574
776
 
777
+ function records() {
778
+ return undefined;
779
+ }
780
+ function settledPromise(value) {
781
+ if (value instanceof Promise) return value;
782
+ try {
783
+ if (Object.prototype.toString.call(value) === "[object Promise]") return Promise.prototype.then.call(value, v => v);
784
+ } catch {}
785
+ return undefined;
786
+ }
787
+ function isDeferredBody(value) {
788
+ if (value === null || typeof value !== "object" && typeof value !== "function") return false;
789
+ if (typeof ReadableStream !== "undefined" && value instanceof ReadableStream) return true;
790
+ const v = value;
791
+ if (typeof v[Symbol.asyncIterator] === "function") return true;
792
+ return typeof v[Symbol.iterator] === "function" && !Array.isArray(value) && !(value instanceof Map) && !(value instanceof Set) && !ArrayBuffer.isView(value);
793
+ }
794
+
795
+ function deliver(context, at, boundary, outcome, value) {
796
+ const record = {
797
+ id: context.id,
798
+ direct: context.direct,
799
+ at,
800
+ durationMs: performance.now() - at,
801
+ outcome
802
+ };
803
+ if (boundary !== undefined) record.boundary = boundary;
804
+ const live = {
805
+ event: context.event,
806
+ args: context.args
807
+ };
808
+ if (context.request !== undefined) live.request = context.request;
809
+ if (outcome === "ok") {
810
+ live.result = value;
811
+ if (isDeferredBody(value)) record.deferred = true;
812
+ } else live.error = value;
813
+ records().emit("invocation", record, live);
814
+ }
815
+ function observeInvocation(context, execute) {
816
+ const channel = records();
817
+ if (channel === undefined || !channel.observed("invocation")) return execute();
818
+ const at = performance.now();
819
+ const boundary = context.direct ? sharedConfig.context?._currentBoundaryId ?? undefined : undefined;
820
+ let result;
821
+ try {
822
+ result = execute();
823
+ } catch (error) {
824
+ deliver(context, at, boundary, "error", error);
825
+ throw error;
826
+ }
827
+ const promised = settledPromise(result);
828
+ if (promised !== undefined) {
829
+ return promised.then(value => {
830
+ deliver(context, at, boundary, "ok", value);
831
+ return value;
832
+ }, error => {
833
+ deliver(context, at, boundary, "error", error);
834
+ throw error;
835
+ });
836
+ }
837
+ deliver(context, at, boundary, "ok", result);
838
+ return result;
839
+ }
840
+
575
841
  let configuredSecret;
576
842
  function setFlashSecret(secret) {
577
843
  configuredSecret = secret;
@@ -1073,12 +1339,17 @@ function createServerReference({
1073
1339
  const wrap = resolveWrapInvocation(config.wrapInvocation);
1074
1340
  let result = provideEventOnce(provideEvent, evt, () => {
1075
1341
  const run = () => fn.apply(thisArg, args);
1076
- return wrap ? wrap(run, {
1342
+ return observeInvocation({
1343
+ id,
1344
+ direct: true,
1345
+ event: evt,
1346
+ args
1347
+ }, () => reportDirectFailure(() => wrap ? wrap(run, {
1077
1348
  id,
1078
1349
  args,
1079
1350
  event: evt,
1080
1351
  direct: true
1081
- }) : run();
1352
+ }) : run(), id));
1082
1353
  });
1083
1354
  result = scopeDeferredResult(result, scope);
1084
1355
  const transform = config.transformDirectResult;
@@ -1524,7 +1795,7 @@ function guardFailures(value, state) {
1524
1795
  try {
1525
1796
  top.accessorValue = descriptor.get.call(top.value);
1526
1797
  } catch (error) {
1527
- throw sanitizeServerError(error);
1798
+ throw wireServerFunctionError(error, "channel");
1528
1799
  }
1529
1800
  top.accessorRead = i;
1530
1801
  }
@@ -1634,7 +1905,7 @@ function enterGuard(value, state) {
1634
1905
  } = await guardOperation(state, () => reader.read());
1635
1906
  done ? controller.close() : controller.enqueue(guardOperation(state, () => guardFailures(chunk, state)));
1636
1907
  } catch (error) {
1637
- controller.error(guardOperation(state, () => sanitizeServerError(error)));
1908
+ controller.error(guardOperation(state, () => wireServerFunctionError(error, "channel")));
1638
1909
  }
1639
1910
  },
1640
1911
  cancel(reason) {
@@ -1648,7 +1919,7 @@ function enterGuard(value, state) {
1648
1919
  }
1649
1920
  if (typeof value.then === "function") {
1650
1921
  const guardedPromise = Promise.resolve(value).then(resolved => guardOperation(state, () => guardFailures(resolved, state)), error => {
1651
- throw guardOperation(state, () => sanitizeServerError(error));
1922
+ throw guardOperation(state, () => wireServerFunctionError(error, "channel"));
1652
1923
  });
1653
1924
  guardedPromise.catch(() => {});
1654
1925
  state.seen.set(value, guardedPromise);
@@ -1683,7 +1954,7 @@ function enterGuard(value, state) {
1683
1954
  value: guardOperation(state, () => guardFailures(step.value, state))
1684
1955
  };
1685
1956
  }, error => {
1686
- throw guardOperation(state, () => sanitizeServerError(error));
1957
+ throw guardOperation(state, () => wireServerFunctionError(error, "channel"));
1687
1958
  });
1688
1959
  return {
1689
1960
  next: () => finished || !gate || gate.wantsMore() ? step() : gate.awaitDemand().then(step),
@@ -1832,7 +2103,7 @@ function serializeResponseStream(value, codecOptions, signal, scope) {
1832
2103
  if (onAbort) signal.removeEventListener("abort", onAbort);
1833
2104
  finishSource();
1834
2105
  try {
1835
- const delivered = sanitizeServerError(DEV && error instanceof Error ? new Error(`Server function result could not be encoded: ${error.message}`) : error);
2106
+ const delivered = wireServerFunctionError(DEV && error instanceof Error ? new Error(`Server function result could not be encoded: ${error.message}`) : error, "channel");
1836
2107
  controller.enqueue(createChunk(encodeErrorTrailer(delivered)));
1837
2108
  controller.close();
1838
2109
  } catch {
@@ -1926,23 +2197,56 @@ let DEV = false === true;
1926
2197
  function setServerFunctionsDev(dev) {
1927
2198
  DEV = !!dev;
1928
2199
  }
2200
+ const REQUEST_ERROR_HOOKS = new WeakMap();
2201
+ function siteFor(handling, event, direct) {
2202
+ const invocation = event ? INVOCATIONS.get(event) : undefined;
2203
+ const site = {
2204
+ kind: "server-function",
2205
+ handling,
2206
+ direct
2207
+ };
2208
+ if (invocation) site.functionId = invocation.id;
2209
+ if (event) site.event = event;
2210
+ return site;
2211
+ }
2212
+ function wireServerFunctionError(value, handling, event = currentEvent()) {
2213
+ const report = reportServerError(value, siteFor(handling, event, false), null, event ? REQUEST_ERROR_HOOKS.get(event) : undefined);
2214
+ return report.mapped ? report.value : sanitizeServerError(value);
2215
+ }
2216
+ function currentEvent() {
2217
+ const store = globalThis[RequestContext];
2218
+ return store ? store.getStore() : undefined;
2219
+ }
2220
+ function reportDirectFailure(run, id) {
2221
+ const report = error => {
2222
+ reportServerError(error, {
2223
+ kind: "server-function",
2224
+ handling: "thrown",
2225
+ functionId: id,
2226
+ direct: true
2227
+ }, null);
2228
+ throw error;
2229
+ };
2230
+ let result;
2231
+ try {
2232
+ result = run();
2233
+ } catch (error) {
2234
+ report(error);
2235
+ }
2236
+ return result && typeof result.then === "function" ? result.then(undefined, report) : result;
2237
+ }
1929
2238
  function sanitizeServerError(value) {
1930
2239
  if (DEV) return value;
1931
2240
  if (isSafeError(value)) return value;
1932
2241
  return new Error(GENERIC_SERVER_ERROR_MESSAGE);
1933
2242
  }
1934
- function observeServerFunctionCalls() {
1935
- return () => {};
2243
+ function serverFunctionUrl(fn, ...args) {
2244
+ return serverFunctionUrlFor(config.endpoint, fn, args);
1936
2245
  }
1937
- function serverFunctionUrl(id, boundArgs) {
1938
- const address = serverFunctionAddress(config.endpoint, id);
1939
- if (!boundArgs || !boundArgs.length) return address;
1940
- if (!isJSONSafe(boundArgs)) {
1941
- 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.");
1942
- }
1943
- return `${address}?args=${encodeURIComponent(JSON.stringify(boundArgs))}`;
2246
+ function serverFunctionActionUrl(fn, ...boundArgs) {
2247
+ return serverFunctionActionUrlFor(config.endpoint, fn, boundArgs);
1944
2248
  }
1945
- function parseServerFunctionUrl(url) {
2249
+ function parseServerFunctionActionUrl(url) {
1946
2250
  const parsed = parseServerFunctionAddress(new URL(url, "http://localhost").pathname, config.endpoint);
1947
2251
  return parsed && parsed.id;
1948
2252
  }
@@ -2034,7 +2338,6 @@ async function handleServerFunctionRequest(request, options = {}) {
2034
2338
  if (protectsRequest && !(await allowsServerFunctionRequest(request, csrf === true ? {} : csrf))) {
2035
2339
  return finalizeTransportResponse(forbiddenResponse(), method);
2036
2340
  }
2037
- const instance = request.headers.get(INSTANCE_HEADER);
2038
2341
  if (!functionId) {
2039
2342
  const response = new Response(DEV ? "Server function not found" : null, {
2040
2343
  status: 404
@@ -2109,6 +2412,7 @@ async function handleServerFunctionRequest(request, options = {}) {
2109
2412
  };
2110
2413
  const provide = options.provideEvent || provideEvent;
2111
2414
  const scope = run => provide(event, run);
2415
+ if (options.onError !== undefined) REQUEST_ERROR_HOOKS.set(event, options.onError);
2112
2416
  const flightHook = options.collectFlightData !== undefined ? options.collectFlightData : config.collectFlightData;
2113
2417
  const transformResult = options.transformResult !== undefined ? options.transformResult : config.transformResult;
2114
2418
  const wrapInvocation = resolveWrapInvocation(options.wrapInvocation !== undefined ? options.wrapInvocation : config.wrapInvocation);
@@ -2151,7 +2455,6 @@ async function handleServerFunctionRequest(request, options = {}) {
2151
2455
  const flightContext = {
2152
2456
  id: functionId,
2153
2457
  args: parsed,
2154
- instance,
2155
2458
  request,
2156
2459
  collectsFlight,
2157
2460
  codec,
@@ -2165,13 +2468,19 @@ async function handleServerFunctionRequest(request, options = {}) {
2165
2468
  id: functionId
2166
2469
  });
2167
2470
  const run = () => serverFunction(...parsed);
2168
- return wrapInvocation ? wrapInvocation(run, {
2471
+ return observeInvocation({
2472
+ id: functionId,
2473
+ direct: false,
2474
+ event,
2475
+ request,
2476
+ args: parsed
2477
+ }, () => wrapInvocation ? wrapInvocation(run, {
2169
2478
  id: functionId,
2170
2479
  args: parsed,
2171
2480
  event,
2172
2481
  request,
2173
2482
  direct: false
2174
- }) : run();
2483
+ }) : run());
2175
2484
  };
2176
2485
  let result = await provideEventOnce(provide, event, invokeOnce);
2177
2486
  if (transformResult) {
@@ -2235,7 +2544,7 @@ async function handleServerFunctionRequest(request, options = {}) {
2235
2544
  return encodeResult(result, headers, status, codec, request.signal, scope);
2236
2545
  } catch (x) {
2237
2546
  const respondThrown = value => {
2238
- const safe = sanitizeServerError(value);
2547
+ const safe = wireServerFunctionError(value, "thrown", event);
2239
2548
  if (!scripted) {
2240
2549
  if (handleNoJS) return handleNoJS(safe, request, parsed, true);
2241
2550
  const message = safe instanceof Error ? safe.message : String(safe);
@@ -2353,4 +2662,4 @@ function finalizeTransportResponse(response, method) {
2353
2662
  return response;
2354
2663
  }
2355
2664
 
2356
- export { ERROR_HEADER, FLASH_COOKIE, GENERIC_SERVER_ERROR_MESSAGE, GET, INSTANCE_HEADER, REDIRECT_HEADER, SERVER_FUNCTION_INVOKE, SINGLE_FLIGHT_HEADER, UNKNOWN_HEADER, clearFlashCookie, configureServerFunctionsServer, createNoJSHandler, createServerReference, decodeErrorHeaderValue, decodeFlashCookie, decodeRedirectHeaderValue, decodeResponse, decodeResponsePayload, encodeErrorHeaderValue, encodeFlashCookie, foldSetCookies, getEventServerFunctionInvocation, getServerFunction, getServerFunctionInvocation, getServerFunctionMetadata, guardFailures, handleServerFunctionRequest, hasFlashCookie, invoke, isServerFunction, live, observeServerFunctionCalls, parseServerFunctionUrl, registerFlightDataSource, registerServerFunction, registerServerReference, sanitizeServerError, serializeResponseStream, serverFunctionUrl, setServerFunctionsDev, subscribeFlightData, withMeta };
2665
+ export { ERROR_HEADER, FLASH_COOKIE, GENERIC_SERVER_ERROR_MESSAGE, GET, REDIRECT_HEADER, SERVER_FUNCTION_INVOKE, SINGLE_FLIGHT_HEADER, UNKNOWN_HEADER, clearFlashCookie, configureServerFunctionsServer, createNoJSHandler, createServerReference, decodeErrorHeaderValue, decodeFlashCookie, decodeRedirectHeaderValue, decodeResponse, decodeResponsePayload, encodeErrorHeaderValue, encodeFlashCookie, foldSetCookies, getEventServerFunctionInvocation, getServerFunction, getServerFunctionInvocation, getServerFunctionMetadata, guardFailures, handleServerFunctionRequest, hasFlashCookie, invoke, isServerFunction, live, parseServerFunctionActionUrl, registerFlightDataSource, registerServerFunction, registerServerReference, sanitizeServerError, serializeResponseStream, serverFunctionActionUrl, serverFunctionUrl, setServerFunctionsDev, subscribeFlightData, withMeta };