@tokenoftrust/storefront-runner 3.3.3 → 3.3.4

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.
@@ -2,9 +2,14 @@
2
2
  * The document's server response time for the evidence panel: the `total` the Worker stamps in
3
3
  * `Server-Timing` (`lib/observability/shared/serverTiming.ts`) next to the time this self-fetch
4
4
  * waited for the response headers (TTFB as seen from the Worker). Advisory — never gates promotion.
5
+ *
6
+ * The self-fetch carries the viewer's preview grant so it measures the render a reviewer gets. A
7
+ * gate page or any non-200 answer is not that render, so it is reported as not measured rather
8
+ * than as a number.
5
9
  */
6
10
 
7
11
  export interface ServerResponseTime {
12
+ measured: true;
8
13
  /** `total;dur` from the document's `Server-Timing`, or null when the header is absent. */
9
14
  serverTotalMs: number | null;
10
15
  /** Wall time until the response headers arrived. */
@@ -13,6 +18,11 @@ export interface ServerResponseTime {
13
18
  cold: boolean;
14
19
  }
15
20
 
21
+ export interface ServerResponseTimeUnmeasured {
22
+ measured: false;
23
+ reason: string;
24
+ }
25
+
16
26
  /** Pull `total;dur` and the cold marker out of a `Server-Timing` header value. */
17
27
  export function parseServerTiming(header: string | null | undefined): Pick<ServerResponseTime, "serverTotalMs" | "cold"> {
18
28
  if (!header) return { serverTotalMs: null, cold: false };
@@ -24,18 +34,35 @@ export function parseServerTiming(header: string | null | undefined): Pick<Serve
24
34
  };
25
35
  }
26
36
 
27
- /** Self-fetch `url` and report its server response time; null when the fetch fails. */
37
+ /**
38
+ * The URL and headers that make a self-fetch of `path` the viewer's own render: the request's
39
+ * `__preview` grant (query) and cookies are forwarded.
40
+ */
41
+ export function viewerRenderRequest(requestUrl: string, path: string, cookie: string | null): { url: URL; headers: Headers } {
42
+ const source = new URL(requestUrl);
43
+ const url = new URL(path, source.origin);
44
+ const grant = source.searchParams.get("__preview");
45
+ if (grant) url.searchParams.set("__preview", grant);
46
+ const headers = new Headers();
47
+ if (cookie) headers.set("cookie", cookie);
48
+ return { url, headers };
49
+ }
50
+
51
+ /** Self-fetch and report the server response time, or why it could not be measured. */
28
52
  export async function measureServerResponseTime(
29
- url: URL,
53
+ request: { url: URL; headers: Headers },
30
54
  now: () => number = () => performance.now(),
31
- ): Promise<ServerResponseTime | null> {
55
+ ): Promise<ServerResponseTime | ServerResponseTimeUnmeasured> {
32
56
  try {
33
57
  const started = now();
34
- const res = await fetch(url, { redirect: "manual" });
58
+ const res = await fetch(request.url, { headers: request.headers, redirect: "manual" });
35
59
  const ttfbMs = now() - started;
36
60
  await res.body?.cancel();
37
- return { ...parseServerTiming(res.headers.get("server-timing")), ttfbMs: Math.round(ttfbMs) };
61
+ const gate = res.headers.get("x-tot-gate");
62
+ if (gate) return { measured: false, reason: `the fetch was answered by the access gate (${gate}), not the store render` };
63
+ if (res.status !== 200) return { measured: false, reason: `the home document answered ${res.status}, not a render` };
64
+ return { measured: true, ...parseServerTiming(res.headers.get("server-timing")), ttfbMs: Math.round(ttfbMs) };
38
65
  } catch {
39
- return null;
66
+ return { measured: false, reason: "the home document could not be fetched" };
40
67
  }
41
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "3.3.3",
3
+ "version": "3.3.4",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
6
6
  "packageManager": "pnpm@11.9.0",