@relyon/kit 0.0.0-stage → 1.0.0
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.
- package/AGENTS.md +75 -0
- package/CHANGELOG.md +1053 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +239 -0
- package/README.md +117 -2
- package/dist/adapters/express.d.ts +39 -0
- package/dist/adapters/express.js +106 -0
- package/dist/adapters/hono.d.ts +16 -0
- package/dist/adapters/hono.js +15 -0
- package/dist/adapters/next.d.ts +29 -0
- package/dist/adapters/next.js +38 -0
- package/dist/agent-card.d.ts +14 -0
- package/dist/agent-card.js +58 -0
- package/dist/agent-docs.d.ts +36 -0
- package/dist/agent-docs.js +78 -0
- package/dist/agent-hooks.d.ts +66 -0
- package/dist/agent-hooks.js +280 -0
- package/dist/agents.d.ts +24 -0
- package/dist/agents.js +120 -0
- package/dist/api-key.d.ts +6 -0
- package/dist/api-key.js +33 -0
- package/dist/cache.d.ts +46 -0
- package/dist/cache.js +86 -0
- package/dist/cart-tokens.d.ts +31 -0
- package/dist/cart-tokens.js +58 -0
- package/dist/cart-transfer.d.ts +95 -0
- package/dist/cart-transfer.js +387 -0
- package/dist/catalog-enrichment.d.ts +6 -0
- package/dist/catalog-enrichment.js +71 -0
- package/dist/catalog-reads.d.ts +126 -0
- package/dist/catalog-reads.js +104 -0
- package/dist/catalog.d.ts +78 -0
- package/dist/catalog.js +288 -0
- package/dist/charge-claims.d.ts +11 -0
- package/dist/charge-claims.js +47 -0
- package/dist/check.d.ts +44 -0
- package/dist/check.js +152 -0
- package/dist/checklist-manifest.d.ts +72 -0
- package/dist/checklist-manifest.js +341 -0
- package/dist/checkout.d.ts +222 -0
- package/dist/checkout.js +353 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +727 -0
- package/dist/confirmation.d.ts +78 -0
- package/dist/confirmation.js +124 -0
- package/dist/context.d.ts +81 -0
- package/dist/context.js +10 -0
- package/dist/discovery.d.ts +151 -0
- package/dist/discovery.js +346 -0
- package/dist/doctor-cart-transfer.d.ts +33 -0
- package/dist/doctor-cart-transfer.js +186 -0
- package/dist/doctor-catalog.d.ts +8 -0
- package/dist/doctor-catalog.js +47 -0
- package/dist/doctor-evidence.d.ts +22 -0
- package/dist/doctor-evidence.js +57 -0
- package/dist/doctor-merchant.d.ts +48 -0
- package/dist/doctor-merchant.js +74 -0
- package/dist/doctor-source.d.ts +5 -0
- package/dist/doctor-source.js +142 -0
- package/dist/doctor-variants.d.ts +70 -0
- package/dist/doctor-variants.js +102 -0
- package/dist/doctor.d.ts +215 -0
- package/dist/doctor.js +1689 -0
- package/dist/events.d.ts +95 -0
- package/dist/events.js +200 -0
- package/dist/feed-mapping.d.ts +114 -0
- package/dist/feed-mapping.js +152 -0
- package/dist/feed-values.d.ts +13 -0
- package/dist/feed-values.js +40 -0
- package/dist/feeds.d.ts +104 -0
- package/dist/feeds.js +366 -0
- package/dist/identify.d.ts +102 -0
- package/dist/identify.js +124 -0
- package/dist/identity.d.ts +37 -0
- package/dist/identity.js +54 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +41 -0
- package/dist/init.d.ts +157 -0
- package/dist/init.js +912 -0
- package/dist/kit-config.d.ts +19 -0
- package/dist/kit-config.js +43 -0
- package/dist/kit-environment.d.ts +20 -0
- package/dist/kit-environment.js +32 -0
- package/dist/kit-updates.d.ts +47 -0
- package/dist/kit-updates.js +77 -0
- package/dist/kit-version-project.d.ts +18 -0
- package/dist/kit-version-project.js +214 -0
- package/dist/kit-version.d.ts +37 -0
- package/dist/kit-version.js +162 -0
- package/dist/kit.d.ts +516 -0
- package/dist/kit.js +1309 -0
- package/dist/layers/pages.d.ts +3 -0
- package/dist/layers/pages.js +5 -0
- package/dist/layers/payments.d.ts +7 -0
- package/dist/layers/payments.js +9 -0
- package/dist/layers/products.d.ts +10 -0
- package/dist/layers/products.js +13 -0
- package/dist/live.d.ts +13 -0
- package/dist/live.js +52 -0
- package/dist/mcp.d.ts +270 -0
- package/dist/mcp.js +312 -0
- package/dist/next-steps.d.ts +101 -0
- package/dist/next-steps.js +257 -0
- package/dist/order-pending.d.ts +57 -0
- package/dist/order-pending.js +127 -0
- package/dist/orders.d.ts +12 -0
- package/dist/orders.js +21 -0
- package/dist/payments-init.d.ts +19 -0
- package/dist/payments-init.js +70 -0
- package/dist/payments-routes.d.ts +54 -0
- package/dist/payments-routes.js +123 -0
- package/dist/payments.d.ts +165 -0
- package/dist/payments.js +192 -0
- package/dist/pnpm-workspace.d.ts +58 -0
- package/dist/pnpm-workspace.js +250 -0
- package/dist/policies.d.ts +111 -0
- package/dist/policies.js +317 -0
- package/dist/preview-hosts.d.ts +64 -0
- package/dist/preview-hosts.js +175 -0
- package/dist/private-config.d.ts +169 -0
- package/dist/private-config.js +820 -0
- package/dist/progress.d.ts +54 -0
- package/dist/progress.js +153 -0
- package/dist/protocol-inputs.d.ts +22 -0
- package/dist/protocol-inputs.js +151 -0
- package/dist/public-origin.d.ts +50 -0
- package/dist/public-origin.js +180 -0
- package/dist/refusals.d.ts +86 -0
- package/dist/refusals.js +157 -0
- package/dist/relay.d.ts +107 -0
- package/dist/relay.js +655 -0
- package/dist/release-notes.d.ts +2 -0
- package/dist/release-notes.js +290 -0
- package/dist/relyon-cart-tokens.d.ts +101 -0
- package/dist/relyon-cart-tokens.js +521 -0
- package/dist/relyon.d.ts +141 -0
- package/dist/relyon.js +238 -0
- package/dist/robots.d.ts +27 -0
- package/dist/robots.js +127 -0
- package/dist/rules.d.ts +144 -0
- package/dist/rules.js +347 -0
- package/dist/sdk/generated/public-api.d.ts +4816 -0
- package/dist/sdk/generated/public-api.js +1 -0
- package/dist/sdk/index.d.ts +24 -0
- package/dist/sdk/index.js +34 -0
- package/dist/sdk/signatures.d.ts +17 -0
- package/dist/sdk/signatures.js +33 -0
- package/dist/setup.d.ts +37 -0
- package/dist/setup.js +94 -0
- package/dist/signatures.d.ts +112 -0
- package/dist/signatures.js +443 -0
- package/dist/state.d.ts +185 -0
- package/dist/state.js +96 -0
- package/dist/store-payments.d.ts +48 -0
- package/dist/store-payments.js +143 -0
- package/dist/structured-fields.d.ts +26 -0
- package/dist/structured-fields.js +200 -0
- package/dist/templates.d.ts +19 -0
- package/dist/templates.js +193 -0
- package/dist/test-checkout-command.d.ts +32 -0
- package/dist/test-checkout-command.js +161 -0
- package/dist/test-checkout.d.ts +88 -0
- package/dist/test-checkout.js +292 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.js +18 -0
- package/dist/ucp-versions.d.ts +57 -0
- package/dist/ucp-versions.js +129 -0
- package/dist/util.d.ts +23 -0
- package/dist/util.js +68 -0
- package/dist/variants.d.ts +67 -0
- package/dist/variants.js +146 -0
- package/dist/version.d.ts +9 -0
- package/dist/version.js +10 -0
- package/dist/waiting.d.ts +43 -0
- package/dist/waiting.js +57 -0
- package/dist/webhook-command.d.ts +48 -0
- package/dist/webhook-command.js +367 -0
- package/dist/webhook-registration.d.ts +47 -0
- package/dist/webhook-registration.js +69 -0
- package/dist/webmcp-schema.d.ts +14 -0
- package/dist/webmcp-schema.js +140 -0
- package/dist/webmcp-script.d.ts +3 -0
- package/dist/webmcp-script.js +176 -0
- package/dist/webmcp-store.d.ts +102 -0
- package/dist/webmcp-store.js +116 -0
- package/dist/webmcp.d.ts +242 -0
- package/dist/webmcp.js +866 -0
- package/dist/wire-names.d.ts +85 -0
- package/dist/wire-names.js +109 -0
- package/package.json +118 -4
- package/release-notes.json +290 -0
- package/scripts/install-notice.cjs +7 -0
- package/skills/relyon-agents/SKILL.md +327 -0
- package/skills/relyon-catalog/SKILL.md +499 -0
- package/skills/relyon-catalog/references/example-store.md +218 -0
- package/skills/relyon-checkout/SKILL.md +603 -0
- package/skills/relyon-checkout/references/checkout-flow.md +257 -0
- package/skills/relyon-checkout/references/checkout-page.md +315 -0
- package/skills/relyon-go-live/SKILL.md +344 -0
- package/skills/relyon-orders/SKILL.md +191 -0
- package/skills/relyon-orders/references/webhook-receiver.md +113 -0
- package/skills/relyon-payments/SKILL.md +117 -0
- package/skills/relyon-setup/SKILL.md +1061 -0
- package/skills/relyon-setup/references/QUICKSTART.md +239 -0
- package/skills/relyon-setup/references/durable-state.md +209 -0
- package/skills/relyon-setup/references/express-server.md +388 -0
- package/skills/relyon-setup/references/next-app-router.md +251 -0
- package/templates/common/STORE-README.md +67 -0
- package/templates/common/gitignore +8 -0
- package/templates/common/scripts/check-pricing.ts +22 -0
- package/templates/common/scripts/serve.mjs +13 -0
- package/templates/common/server/catalog.ts +79 -0
- package/templates/common/server/checkout-flow.d.ts +34 -0
- package/templates/common/server/pages.ts +184 -0
- package/templates/common/server/storage.ts +114 -0
- package/templates/common/server/store.ts +67 -0
- package/templates/express/server/express.ts +20 -0
- package/templates/express/tsconfig.build.json +6 -0
- package/templates/express/tsconfig.json +9 -0
- package/templates/next/app/[[...path]]/route.ts +24 -0
- package/templates/next/next.config.mjs +19 -0
- package/templates/next/tsconfig.json +12 -0
- package/templates/payments/express.ts +39 -0
- package/templates/payments/next.ts +38 -0
- package/templates/payments/node.ts +39 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { StoreDoctorItem } from "./doctor.js";
|
|
2
|
+
import type { FetchLike } from "./relyon.js";
|
|
3
|
+
/** The doctor item's name. */
|
|
4
|
+
export declare const PROGRESS_ITEM = "Test checkout confirmed by Relyon";
|
|
5
|
+
/** The only origin a saved (not environment-provided) Test key is ever sent to. */
|
|
6
|
+
export declare const OFFICIAL_RELYON_ORIGIN = "https://core.getrelyon.com";
|
|
7
|
+
/** What GET /v1/sandboxes/progress answers. `store` is present only because this kit asks for it. */
|
|
8
|
+
export interface SandboxProgress {
|
|
9
|
+
test_key_used: boolean;
|
|
10
|
+
test_key_first_used_at: string | null;
|
|
11
|
+
test_checkout_completed: boolean;
|
|
12
|
+
test_checkout_first_completed_at: string | null;
|
|
13
|
+
/** The connected store (the key's bound store, or the workspace's default), when requested. */
|
|
14
|
+
store?: {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
} | undefined;
|
|
18
|
+
}
|
|
19
|
+
/** Where the doctor's key came from: the environment (RELYON_API_KEY) or the private saved key. */
|
|
20
|
+
export type KeySource = "env" | "saved";
|
|
21
|
+
export type ProgressOutcome = {
|
|
22
|
+
kind: "read";
|
|
23
|
+
progress: SandboxProgress;
|
|
24
|
+
} | {
|
|
25
|
+
kind: "skipped";
|
|
26
|
+
reason: "live" | "no-test-key" | "saved-key-origin";
|
|
27
|
+
} | {
|
|
28
|
+
kind: "unreachable";
|
|
29
|
+
detail: string;
|
|
30
|
+
};
|
|
31
|
+
export interface ProgressOptions {
|
|
32
|
+
apiKey?: string | undefined;
|
|
33
|
+
keySource?: KeySource | undefined;
|
|
34
|
+
/** Relyon's API base URL, as the doctor uses it (default https://core.getrelyon.com/api). */
|
|
35
|
+
baseUrl?: string | undefined;
|
|
36
|
+
live?: boolean | undefined;
|
|
37
|
+
fetch?: FetchLike | undefined;
|
|
38
|
+
timeoutMs?: number | undefined;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* True for the four documented fields with their documented types. Unknown
|
|
42
|
+
* extra fields are ignored, so a newer Relyon (or the opt-in `store` field)
|
|
43
|
+
* never breaks an older kit; a `store` field, when present, must be well-formed.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isSandboxProgress(value: unknown): value is SandboxProgress;
|
|
46
|
+
/**
|
|
47
|
+
* The saved Test key is sent only to Relyon's official https origin; a key the
|
|
48
|
+
* merchant put in RELYON_API_KEY follows the base URL they chose. Never in Live.
|
|
49
|
+
*/
|
|
50
|
+
export declare function progressUrl(baseUrl: string, keySource: KeySource | undefined): string | null;
|
|
51
|
+
/** Reads Relyon's Test progress record. Never throws; never sends a Live or malformed key. */
|
|
52
|
+
export declare function readSandboxProgress(options: ProgressOptions): Promise<ProgressOutcome>;
|
|
53
|
+
/** The doctor item for an outcome. Wording names no key and no URL beyond Relyon's origin. */
|
|
54
|
+
export declare function progressItem(outcome: ProgressOutcome): StoreDoctorItem;
|
package/dist/progress.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { SANDBOX_PROGRESS_PATH } from "./wire-names.js";
|
|
2
|
+
import { usableTestKey } from "./api-key.js";
|
|
3
|
+
import { checkIdForItem } from "./checklist-manifest.js";
|
|
4
|
+
import { isRecord } from "./util.js";
|
|
5
|
+
import { KIT_VERSION } from "./version.js";
|
|
6
|
+
// The doctor's "Test checkout confirmed by Relyon" check. Relyon records, per
|
|
7
|
+
// organization and in Test only, two timestamps: the first authenticated use of
|
|
8
|
+
// a Test secret key and the first completed Test checkout. The doctor reads them
|
|
9
|
+
// from GET /v1/sandboxes/progress (outside the /v1 contract document) with the
|
|
10
|
+
// Test secret key. Nothing else is sent or read; the key is never printed.
|
|
11
|
+
/** The doctor item's name. */
|
|
12
|
+
export const PROGRESS_ITEM = "Test checkout confirmed by Relyon";
|
|
13
|
+
/** The only origin a saved (not environment-provided) Test key is ever sent to. */
|
|
14
|
+
export const OFFICIAL_RELYON_ORIGIN = "https://core.getrelyon.com";
|
|
15
|
+
/** The longest answer read from the progress endpoint. */
|
|
16
|
+
const MAX_PROGRESS_BYTES = 4096;
|
|
17
|
+
const iso = (value) => value === null || (typeof value === "string" && value.length <= 40 && !Number.isNaN(Date.parse(value)));
|
|
18
|
+
/**
|
|
19
|
+
* True for the four documented fields with their documented types. Unknown
|
|
20
|
+
* extra fields are ignored, so a newer Relyon (or the opt-in `store` field)
|
|
21
|
+
* never breaks an older kit; a `store` field, when present, must be well-formed.
|
|
22
|
+
*/
|
|
23
|
+
export function isSandboxProgress(value) {
|
|
24
|
+
if (!isRecord(value))
|
|
25
|
+
return false;
|
|
26
|
+
if (typeof value.test_key_used !== "boolean" || typeof value.test_checkout_completed !== "boolean")
|
|
27
|
+
return false;
|
|
28
|
+
if (!iso(value.test_key_first_used_at) || !iso(value.test_checkout_first_completed_at))
|
|
29
|
+
return false;
|
|
30
|
+
if ((value.test_checkout_first_completed_at === null) === value.test_checkout_completed)
|
|
31
|
+
return false;
|
|
32
|
+
if ((value.test_key_first_used_at === null) === value.test_key_used)
|
|
33
|
+
return false;
|
|
34
|
+
if (value.store !== undefined) {
|
|
35
|
+
const store = value.store;
|
|
36
|
+
if (!isRecord(store) || typeof store.id !== "string" || typeof store.name !== "string")
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The saved Test key is sent only to Relyon's official https origin; a key the
|
|
43
|
+
* merchant put in RELYON_API_KEY follows the base URL they chose. Never in Live.
|
|
44
|
+
*/
|
|
45
|
+
export function progressUrl(baseUrl, keySource) {
|
|
46
|
+
let base;
|
|
47
|
+
try {
|
|
48
|
+
base = new URL(baseUrl);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
if (base.username || base.password || base.search || base.hash)
|
|
54
|
+
return null;
|
|
55
|
+
if (base.protocol !== "https:" && base.protocol !== "http:")
|
|
56
|
+
return null;
|
|
57
|
+
if (keySource !== "env" && base.origin !== OFFICIAL_RELYON_ORIGIN)
|
|
58
|
+
return null;
|
|
59
|
+
// `store=1` opts in to the connected store's id and name alongside the four
|
|
60
|
+
// documented fields; a Relyon that predates it simply answers the four.
|
|
61
|
+
return `${base.origin}${base.pathname.replace(/\/+$/, "")}${SANDBOX_PROGRESS_PATH}?store=1`;
|
|
62
|
+
}
|
|
63
|
+
/** The body as text, or null when it exceeds MAX_PROGRESS_BYTES (never buffered whole). */
|
|
64
|
+
async function boundedText(response) {
|
|
65
|
+
if (!response.body)
|
|
66
|
+
return "";
|
|
67
|
+
const reader = response.body.getReader();
|
|
68
|
+
const chunks = [];
|
|
69
|
+
let total = 0;
|
|
70
|
+
for (;;) {
|
|
71
|
+
const { done, value } = await reader.read();
|
|
72
|
+
if (done)
|
|
73
|
+
break;
|
|
74
|
+
chunks.push(value);
|
|
75
|
+
total += value.byteLength;
|
|
76
|
+
if (total > MAX_PROGRESS_BYTES) {
|
|
77
|
+
void reader.cancel().catch(() => { });
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
82
|
+
}
|
|
83
|
+
/** Reads Relyon's Test progress record. Never throws; never sends a Live or malformed key. */
|
|
84
|
+
export async function readSandboxProgress(options) {
|
|
85
|
+
if (options.live)
|
|
86
|
+
return { kind: "skipped", reason: "live" };
|
|
87
|
+
const key = usableTestKey(options.apiKey);
|
|
88
|
+
if (!key)
|
|
89
|
+
return { kind: "skipped", reason: "no-test-key" };
|
|
90
|
+
const url = progressUrl(options.baseUrl ?? `${OFFICIAL_RELYON_ORIGIN}/api`, options.keySource);
|
|
91
|
+
if (!url)
|
|
92
|
+
return { kind: "skipped", reason: "saved-key-origin" };
|
|
93
|
+
const fetchImpl = options.fetch ?? ((request, init) => fetch(request, init));
|
|
94
|
+
const controller = new AbortController();
|
|
95
|
+
const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? 10_000);
|
|
96
|
+
try {
|
|
97
|
+
const response = await fetchImpl(new Request(url, {
|
|
98
|
+
method: "GET", redirect: "manual",
|
|
99
|
+
headers: { authorization: `Bearer ${key}`, accept: "application/json", "user-agent": `Relyon-Doctor/${KIT_VERSION}` },
|
|
100
|
+
}), { signal: controller.signal });
|
|
101
|
+
const text = await boundedText(response);
|
|
102
|
+
if (response.status >= 300 && response.status < 400)
|
|
103
|
+
return { kind: "unreachable", detail: `Relyon answered with a redirect (HTTP ${response.status}) instead of its record` };
|
|
104
|
+
if (response.status === 401 || response.status === 403)
|
|
105
|
+
return { kind: "unreachable", detail: `Relyon refused the progress read (HTTP ${response.status}); check that RELYON_API_KEY is this store's Test secret key` };
|
|
106
|
+
if (response.status !== 200)
|
|
107
|
+
return { kind: "unreachable", detail: `Relyon's progress record answered HTTP ${response.status}` };
|
|
108
|
+
if (text === null)
|
|
109
|
+
return { kind: "unreachable", detail: "Relyon's progress answer was too large" };
|
|
110
|
+
let data;
|
|
111
|
+
try {
|
|
112
|
+
data = JSON.parse(text);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return { kind: "unreachable", detail: "Relyon's progress answer wasn't JSON" };
|
|
116
|
+
}
|
|
117
|
+
if (!isSandboxProgress(data))
|
|
118
|
+
return { kind: "unreachable", detail: "Relyon's progress answer wasn't the expected record" };
|
|
119
|
+
return { kind: "read", progress: data };
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
const aborted = controller.signal.aborted || error?.name === "AbortError";
|
|
123
|
+
return { kind: "unreachable", detail: aborted ? "Relyon did not answer in time" : "Relyon could not be reached" };
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
clearTimeout(timer);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** The doctor item for an outcome. Wording names no key and no URL beyond Relyon's origin. */
|
|
130
|
+
export function progressItem(outcome) {
|
|
131
|
+
const item = buildProgressItem(outcome);
|
|
132
|
+
// The progress item is a checklist-manifest check too ("Test checkout confirmed by Relyon").
|
|
133
|
+
const checkId = checkIdForItem(item.name);
|
|
134
|
+
return checkId ? { ...item, checkId } : item;
|
|
135
|
+
}
|
|
136
|
+
function buildProgressItem(outcome) {
|
|
137
|
+
if (outcome.kind === "read") {
|
|
138
|
+
const store = outcome.progress.store;
|
|
139
|
+
const suffix = store ? ` Store: ${store.name}.` : "";
|
|
140
|
+
return outcome.progress.test_checkout_completed
|
|
141
|
+
? { name: PROGRESS_ITEM, status: "PASS", detail: `Relyon recorded a completed Test checkout (first at ${outcome.progress.test_checkout_first_completed_at}).${suffix}` }
|
|
142
|
+
: { name: PROGRESS_ITEM, status: "NOT DONE", detail: `Relyon answered: no completed Test checkout is recorded for this store's sandbox yet.${suffix}` };
|
|
143
|
+
}
|
|
144
|
+
if (outcome.kind === "skipped") {
|
|
145
|
+
if (outcome.reason === "live")
|
|
146
|
+
return { name: PROGRESS_ITEM, status: "N/A", detail: "Live run: Relyon's Test checkout confirmation applies to Test only." };
|
|
147
|
+
if (outcome.reason === "no-test-key")
|
|
148
|
+
return { name: PROGRESS_ITEM, status: "UNVERIFIED", detail: "Needs a Test key: Relyon's confirmation is read with the store's Test secret key." };
|
|
149
|
+
return { name: PROGRESS_ITEM, status: "UNVERIFIED",
|
|
150
|
+
detail: `The saved Test key is sent only to ${OFFICIAL_RELYON_ORIGIN}, so Relyon's confirmation wasn't read from this run's Relyon base URL. Put the store's Test key in RELYON_API_KEY to check against that base URL.` };
|
|
151
|
+
}
|
|
152
|
+
return { name: PROGRESS_ITEM, status: "UNVERIFIED", detail: `${outcome.detail}. Rerun doctor when Relyon can be reached; until then this is not DONE.` };
|
|
153
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AgentProtocol, CartInputs, CheckoutOperation } from "./types.js";
|
|
2
|
+
export interface RoutedStep {
|
|
3
|
+
operation: CheckoutOperation;
|
|
4
|
+
/** The protocol's checkout ID from the path; null for `create`. */
|
|
5
|
+
sessionId: string | null;
|
|
6
|
+
}
|
|
7
|
+
/** Maps a UCP REST path (below the UCP prefix) to its checkout operation. */
|
|
8
|
+
export declare function routeUcp(method: string, rest: string): RoutedStep | null;
|
|
9
|
+
/** Maps an ACP REST path (below the ACP prefix) to its checkout operation. */
|
|
10
|
+
export declare function routeAcp(method: string, rest: string): RoutedStep | null;
|
|
11
|
+
/** The path (below the protocol's prefix) where an agent reads one UCP or ACP checkout. */
|
|
12
|
+
export declare function checkoutSessionPath(protocol: "ucp" | "acp", sessionId: string): string;
|
|
13
|
+
/** The cart inputs a request changes. A field that is absent leaves the previous value. */
|
|
14
|
+
export type InputPatch = Partial<CartInputs>;
|
|
15
|
+
/** UCP 2026-08-25 checkout create/update body → cart inputs. */
|
|
16
|
+
export declare function ucpInputs(body: unknown): InputPatch;
|
|
17
|
+
/** ACP 2026-04-17 checkout create/update body → cart inputs. */
|
|
18
|
+
export declare function acpInputs(body: unknown): InputPatch;
|
|
19
|
+
/** The store kit's own MCP checkout tool arguments → cart inputs. */
|
|
20
|
+
export declare function mcpInputs(args: Record<string, unknown>): InputPatch;
|
|
21
|
+
export declare function mergeInputs(previous: CartInputs | null, patch: InputPatch): CartInputs;
|
|
22
|
+
export declare const inputParsers: Record<Exclude<AgentProtocol, "mcp">, (body: unknown) => InputPatch>;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { isRecord } from "./util.js";
|
|
2
|
+
/** Maps a UCP REST path (below the UCP prefix) to its checkout operation. */
|
|
3
|
+
export function routeUcp(method, rest) {
|
|
4
|
+
const match = /^\/checkout-sessions(?:\/([^/]+)(?:\/(complete|cancel))?)?\/?$/.exec(rest);
|
|
5
|
+
if (!match)
|
|
6
|
+
return null;
|
|
7
|
+
const [, id, action] = match;
|
|
8
|
+
const sessionId = id ? decodeURIComponent(id) : null;
|
|
9
|
+
if (!sessionId)
|
|
10
|
+
return method === "POST" ? { operation: "create", sessionId } : null;
|
|
11
|
+
if (action)
|
|
12
|
+
return method === "POST" ? { operation: action, sessionId } : null;
|
|
13
|
+
if (method === "GET")
|
|
14
|
+
return { operation: "get", sessionId };
|
|
15
|
+
if (method === "PUT")
|
|
16
|
+
return { operation: "update", sessionId };
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
/** Maps an ACP REST path (below the ACP prefix) to its checkout operation. */
|
|
20
|
+
export function routeAcp(method, rest) {
|
|
21
|
+
const match = /^\/checkout_sessions(?:\/([^/]+)(?:\/(complete|cancel))?)?\/?$/.exec(rest);
|
|
22
|
+
if (!match)
|
|
23
|
+
return null;
|
|
24
|
+
const [, id, action] = match;
|
|
25
|
+
const sessionId = id ? decodeURIComponent(id) : null;
|
|
26
|
+
if (!sessionId)
|
|
27
|
+
return method === "POST" ? { operation: "create", sessionId } : null;
|
|
28
|
+
if (action)
|
|
29
|
+
return method === "POST" ? { operation: action, sessionId } : null;
|
|
30
|
+
if (method === "GET")
|
|
31
|
+
return { operation: "get", sessionId };
|
|
32
|
+
if (method === "POST")
|
|
33
|
+
return { operation: "update", sessionId };
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/** The path (below the protocol's prefix) where an agent reads one UCP or ACP checkout. */
|
|
37
|
+
export function checkoutSessionPath(protocol, sessionId) {
|
|
38
|
+
return `/${protocol === "ucp" ? "checkout-sessions" : "checkout_sessions"}/${encodeURIComponent(sessionId)}`;
|
|
39
|
+
}
|
|
40
|
+
const str = (value) => (typeof value === "string" ? value : undefined);
|
|
41
|
+
const quantityOf = (value) => (typeof value === "number" && Number.isInteger(value) && value > 0 ? value : 1);
|
|
42
|
+
const codesOf = (value) => Array.isArray(value) ? value.filter((code) => typeof code === "string") : undefined;
|
|
43
|
+
function address(fields) {
|
|
44
|
+
return {
|
|
45
|
+
name: fields.name ?? "",
|
|
46
|
+
line1: fields.line1 ?? "",
|
|
47
|
+
...(fields.line2 ? { line2: fields.line2 } : {}),
|
|
48
|
+
city: fields.city ?? "",
|
|
49
|
+
...(fields.region ? { region: fields.region } : {}),
|
|
50
|
+
postal_code: fields.postal_code ?? "",
|
|
51
|
+
country: fields.country ?? "",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** UCP 2026-08-25 checkout create/update body → cart inputs. */
|
|
55
|
+
export function ucpInputs(body) {
|
|
56
|
+
if (!isRecord(body))
|
|
57
|
+
return {};
|
|
58
|
+
const patch = {};
|
|
59
|
+
if (Array.isArray(body.line_items)) {
|
|
60
|
+
patch.lines = body.line_items.filter(isRecord).map((line) => {
|
|
61
|
+
const item = isRecord(line.item) ? line.item : {};
|
|
62
|
+
return { product_id: str(item.id) ?? "", quantity: quantityOf(line.quantity) };
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const fulfillment = isRecord(body.fulfillment) ? body.fulfillment : undefined;
|
|
66
|
+
if (fulfillment) {
|
|
67
|
+
const method = Array.isArray(fulfillment.methods) ? fulfillment.methods.find(isRecord) : undefined;
|
|
68
|
+
const destinations = method && Array.isArray(method.destinations) ? method.destinations.filter(isRecord) : [];
|
|
69
|
+
const chosen = destinations.find((destination) => destination.id !== undefined && destination.id === method?.selected_destination_id)
|
|
70
|
+
?? destinations[0];
|
|
71
|
+
const buyer = isRecord(body.buyer) ? body.buyer : {};
|
|
72
|
+
const buyerName = [str(buyer.first_name), str(buyer.last_name)].filter(Boolean).join(" ");
|
|
73
|
+
patch.address = chosen
|
|
74
|
+
? address({
|
|
75
|
+
name: str(chosen.name) ?? (buyerName || undefined), line1: str(chosen.street_address), line2: str(chosen.extended_address),
|
|
76
|
+
city: str(chosen.address_locality), region: str(chosen.address_region), postal_code: str(chosen.postal_code),
|
|
77
|
+
country: str(chosen.address_country),
|
|
78
|
+
})
|
|
79
|
+
: null;
|
|
80
|
+
const group = method && Array.isArray(method.groups) ? method.groups.find(isRecord) : undefined;
|
|
81
|
+
patch.shipping_option_id = str(group?.selected_option_id) ?? null;
|
|
82
|
+
}
|
|
83
|
+
if (isRecord(body.discounts)) {
|
|
84
|
+
const codes = codesOf(body.discounts.codes);
|
|
85
|
+
if (codes)
|
|
86
|
+
patch.coupons = codes;
|
|
87
|
+
}
|
|
88
|
+
return patch;
|
|
89
|
+
}
|
|
90
|
+
/** ACP 2026-04-17 checkout create/update body → cart inputs. */
|
|
91
|
+
export function acpInputs(body) {
|
|
92
|
+
if (!isRecord(body))
|
|
93
|
+
return {};
|
|
94
|
+
const patch = {};
|
|
95
|
+
const items = Array.isArray(body.line_items) ? body.line_items : Array.isArray(body.items) ? body.items : undefined;
|
|
96
|
+
if (items) {
|
|
97
|
+
patch.lines = items.filter(isRecord).map((line) => ({ product_id: str(line.id) ?? "", quantity: quantityOf(line.quantity) }));
|
|
98
|
+
}
|
|
99
|
+
const details = isRecord(body.fulfillment_details) ? body.fulfillment_details : undefined;
|
|
100
|
+
const raw = details && isRecord(details.address) ? details.address : isRecord(body.fulfillment_address) ? body.fulfillment_address : undefined;
|
|
101
|
+
if (raw) {
|
|
102
|
+
patch.address = address({
|
|
103
|
+
name: str(raw.name) ?? str(details?.name), line1: str(raw.line_one), line2: str(raw.line_two), city: str(raw.city),
|
|
104
|
+
region: str(raw.state), postal_code: str(raw.postal_code), country: str(raw.country),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const selected = Array.isArray(body.selected_fulfillment_options) ? body.selected_fulfillment_options.find(isRecord) : undefined;
|
|
108
|
+
const option = str(selected?.option_id) ?? str(body.fulfillment_option_id);
|
|
109
|
+
if (option !== undefined)
|
|
110
|
+
patch.shipping_option_id = option;
|
|
111
|
+
const coupons = isRecord(body.discounts) ? codesOf(body.discounts.codes) : codesOf(body.coupons);
|
|
112
|
+
if (coupons)
|
|
113
|
+
patch.coupons = coupons;
|
|
114
|
+
return patch;
|
|
115
|
+
}
|
|
116
|
+
/** The store kit's own MCP checkout tool arguments → cart inputs. */
|
|
117
|
+
export function mcpInputs(args) {
|
|
118
|
+
const patch = {};
|
|
119
|
+
if (Array.isArray(args.lines)) {
|
|
120
|
+
patch.lines = args.lines.filter(isRecord).map((line) => ({
|
|
121
|
+
product_id: str(line.product_id) ?? "",
|
|
122
|
+
quantity: quantityOf(line.quantity),
|
|
123
|
+
...(isRecord(line.options) ? { options: line.options } : {}),
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
if (isRecord(args.shipping_address)) {
|
|
127
|
+
const raw = args.shipping_address;
|
|
128
|
+
patch.address = address({
|
|
129
|
+
name: str(raw.name), line1: str(raw.line1), line2: str(raw.line2), city: str(raw.city),
|
|
130
|
+
region: str(raw.region), postal_code: str(raw.postal_code), country: str(raw.country),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (typeof args.shipping_option_id === "string")
|
|
134
|
+
patch.shipping_option_id = args.shipping_option_id;
|
|
135
|
+
const coupons = codesOf(args.coupons);
|
|
136
|
+
if (coupons)
|
|
137
|
+
patch.coupons = coupons;
|
|
138
|
+
return patch;
|
|
139
|
+
}
|
|
140
|
+
export function mergeInputs(previous, patch) {
|
|
141
|
+
return {
|
|
142
|
+
lines: patch.lines ?? previous?.lines ?? [],
|
|
143
|
+
address: patch.address !== undefined ? patch.address : previous?.address ?? null,
|
|
144
|
+
coupons: patch.coupons ?? previous?.coupons ?? [],
|
|
145
|
+
shipping_option_id: patch.shipping_option_id !== undefined ? patch.shipping_option_id : previous?.shipping_option_id ?? null,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
export const inputParsers = {
|
|
149
|
+
ucp: ucpInputs,
|
|
150
|
+
acp: acpInputs,
|
|
151
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { StoreProduct } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Checks `store.publicUrl` (Test and Live) and returns its origin: an origin
|
|
4
|
+
* only (no path, query, fragment or credentials), https (plain http only on a
|
|
5
|
+
* loopback name), and naming one of the store's hostnames. undefined when unset.
|
|
6
|
+
*/
|
|
7
|
+
export declare function publicUrlOrigin(publicUrl: string | undefined, hostnames: readonly string[]): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* The request's URL as the kit reads it. A dev server bound to every
|
|
10
|
+
* interface (`next dev --hostname 0.0.0.0`, `listen(port, "::")`) can hand
|
|
11
|
+
* the app a request URL on the unspecified address (host `0.0.0.0:43123`)
|
|
12
|
+
* while the browser asked for `127.0.0.1:43123`. In Test, such a URL takes
|
|
13
|
+
* its host and port from the Host header, but only when that header is a
|
|
14
|
+
* plain host[:port] naming one of `knownHosts` (the store's own, preview or
|
|
15
|
+
* loopback hostnames). Otherwise, and always in Live, the URL is returned
|
|
16
|
+
* unchanged: the unspecified host is no known hostname, so the kit falls
|
|
17
|
+
* back to the primary one, as it does for any unknown host.
|
|
18
|
+
*/
|
|
19
|
+
export declare function requestUrl(request: Request, knownHosts: readonly string[], test: boolean): URL;
|
|
20
|
+
/**
|
|
21
|
+
* A store-own URL moved onto `origin`: a root-relative path (`/products/x`),
|
|
22
|
+
* or an absolute http(s) URL on one of the store's hostnames, a loopback
|
|
23
|
+
* name or an unspecified bind address (dev addresses). The path and query are kept; a fragment is dropped.
|
|
24
|
+
* undefined for anything else (another site, a non-web scheme, credentials).
|
|
25
|
+
*/
|
|
26
|
+
export declare function rebaseStoreUrl(value: unknown, origin: string, ownHosts: readonly string[]): string | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* The hostname when `value` is a URL on an unspecified bind address
|
|
29
|
+
* (`0.0.0.0`, `[::]`), which no browser or agent can open; else undefined.
|
|
30
|
+
*/
|
|
31
|
+
export declare function bindAddressOf(value: unknown): string | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* A link as published to agents: a store-own URL moved onto `origin`; any
|
|
34
|
+
* other value unchanged, except one on a bind address, which is dropped
|
|
35
|
+
* (undefined) even when it can't be moved.
|
|
36
|
+
*/
|
|
37
|
+
export declare function publishedUrl(value: string, origin: string, ownHosts: readonly string[]): string | undefined;
|
|
38
|
+
/** True when `value` is a store-own URL (see `rebaseStoreUrl`). */
|
|
39
|
+
export declare function isStoreOwnUrl(value: unknown, ownHosts: readonly string[]): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* An image URL for agents: a store-own image moved onto `origin`, another
|
|
42
|
+
* site's https image (a CDN) unchanged, anything else dropped.
|
|
43
|
+
*/
|
|
44
|
+
export declare function publicImageUrl(value: unknown, origin: string, ownHosts: readonly string[]): string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* A copy of a product with its page and image URLs on `origin`. A product
|
|
47
|
+
* URL that isn't the store's own is left as it is (callers that need a
|
|
48
|
+
* store-own page check it themselves); images that can't be published are dropped.
|
|
49
|
+
*/
|
|
50
|
+
export declare function onPublicOrigin<T extends StoreProduct>(product: T, origin: string, ownHosts: readonly string[]): T;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// The store's one public address for agent-facing links. Every URL the kit
|
|
2
|
+
// hands to an agent or crawler (llms.txt, sitemaps, feeds, the UCP profile,
|
|
3
|
+
// the MCP tools, the agent card, product structured data) is built on it, so
|
|
4
|
+
// no address from build or dev time is ever published.
|
|
5
|
+
//
|
|
6
|
+
// The address is `store.publicUrl` (the app's STORE_PUBLIC_URL) when set, or
|
|
7
|
+
// else the request's own address when it arrived on one of the store's known
|
|
8
|
+
// hostnames. An unknown Host header is never trusted: it falls back to the
|
|
9
|
+
// primary hostname, as the kit always has.
|
|
10
|
+
import { isLoopbackHostname, isUnspecifiedHostname } from "./preview-hosts.js";
|
|
11
|
+
const UNSAFE = /[\s<>"'\\\u0000-\u001f]/u;
|
|
12
|
+
/**
|
|
13
|
+
* Checks `store.publicUrl` (Test and Live) and returns its origin: an origin
|
|
14
|
+
* only (no path, query, fragment or credentials), https (plain http only on a
|
|
15
|
+
* loopback name), and naming one of the store's hostnames. undefined when unset.
|
|
16
|
+
*/
|
|
17
|
+
export function publicUrlOrigin(publicUrl, hostnames) {
|
|
18
|
+
if (publicUrl === undefined || publicUrl === "")
|
|
19
|
+
return undefined;
|
|
20
|
+
let url;
|
|
21
|
+
try {
|
|
22
|
+
url = new URL(publicUrl);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
throw new TypeError("store.publicUrl is not a valid URL.");
|
|
26
|
+
}
|
|
27
|
+
if (url.username || url.password || url.search || url.hash || (url.pathname !== "/" && url.pathname !== "")) {
|
|
28
|
+
throw new TypeError("store.publicUrl must be the store's origin only, such as https://shop.example.com, with no path, query or credentials.");
|
|
29
|
+
}
|
|
30
|
+
const host = url.hostname.toLowerCase();
|
|
31
|
+
if (isUnspecifiedHostname(host)) {
|
|
32
|
+
throw new TypeError(`store.publicUrl can't be ${host}: that is the address a server listens on, not one a browser or agent can open. Use the address the store is opened at: https://shop.example.com, or for a local store plain http on localhost with its port.`);
|
|
33
|
+
}
|
|
34
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && isLoopbackHostname(host))) {
|
|
35
|
+
throw new TypeError("store.publicUrl must be https (plain http only for localhost).");
|
|
36
|
+
}
|
|
37
|
+
if (!hostnames.includes(host))
|
|
38
|
+
throw new TypeError("store.publicUrl's hostname must be one of store.hostnames.");
|
|
39
|
+
return url.origin;
|
|
40
|
+
}
|
|
41
|
+
/** A Host header value: a hostname or bracketed IPv6 literal, with an optional port. Nothing else. */
|
|
42
|
+
const HOST_HEADER = /^(?:[a-z0-9](?:[a-z0-9.-]{0,251}[a-z0-9])?|\[[0-9a-f:.]{2,45}\])(?::\d{1,5})?$/;
|
|
43
|
+
/**
|
|
44
|
+
* The request's URL as the kit reads it. A dev server bound to every
|
|
45
|
+
* interface (`next dev --hostname 0.0.0.0`, `listen(port, "::")`) can hand
|
|
46
|
+
* the app a request URL on the unspecified address (host `0.0.0.0:43123`)
|
|
47
|
+
* while the browser asked for `127.0.0.1:43123`. In Test, such a URL takes
|
|
48
|
+
* its host and port from the Host header, but only when that header is a
|
|
49
|
+
* plain host[:port] naming one of `knownHosts` (the store's own, preview or
|
|
50
|
+
* loopback hostnames). Otherwise, and always in Live, the URL is returned
|
|
51
|
+
* unchanged: the unspecified host is no known hostname, so the kit falls
|
|
52
|
+
* back to the primary one, as it does for any unknown host.
|
|
53
|
+
*/
|
|
54
|
+
export function requestUrl(request, knownHosts, test) {
|
|
55
|
+
const url = new URL(request.url);
|
|
56
|
+
if (!test || !isUnspecifiedHostname(url.hostname))
|
|
57
|
+
return url;
|
|
58
|
+
const host = request.headers.get("host")?.trim().toLowerCase();
|
|
59
|
+
if (!host || host.length > 260 || !HOST_HEADER.test(host))
|
|
60
|
+
return url;
|
|
61
|
+
let named;
|
|
62
|
+
try {
|
|
63
|
+
named = new URL(`${url.protocol}//${host}/`);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return url;
|
|
67
|
+
}
|
|
68
|
+
if (isUnspecifiedHostname(named.hostname) || !knownHosts.includes(named.hostname))
|
|
69
|
+
return url;
|
|
70
|
+
const rebuilt = new URL(url.href);
|
|
71
|
+
rebuilt.hostname = named.hostname;
|
|
72
|
+
// Set the port explicitly: assigning `host` without a port keeps the bind port.
|
|
73
|
+
rebuilt.port = named.port;
|
|
74
|
+
return rebuilt;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* True when `hostname` is one of the store's own, a loopback name or an
|
|
78
|
+
* unspecified bind address (`0.0.0.0`, `[::]`): dev addresses, never ones
|
|
79
|
+
* to publish.
|
|
80
|
+
*/
|
|
81
|
+
function ownHost(hostname, ownHosts) {
|
|
82
|
+
const host = hostname.toLowerCase();
|
|
83
|
+
return ownHosts.includes(host) || isLoopbackHostname(host) || isUnspecifiedHostname(host);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A store-own URL moved onto `origin`: a root-relative path (`/products/x`),
|
|
87
|
+
* or an absolute http(s) URL on one of the store's hostnames, a loopback
|
|
88
|
+
* name or an unspecified bind address (dev addresses). The path and query are kept; a fragment is dropped.
|
|
89
|
+
* undefined for anything else (another site, a non-web scheme, credentials).
|
|
90
|
+
*/
|
|
91
|
+
export function rebaseStoreUrl(value, origin, ownHosts) {
|
|
92
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 2000 || UNSAFE.test(value))
|
|
93
|
+
return undefined;
|
|
94
|
+
if (value.startsWith("/") && !value.startsWith("//")) {
|
|
95
|
+
try {
|
|
96
|
+
const url = new URL(value, origin);
|
|
97
|
+
if (url.origin !== origin)
|
|
98
|
+
return undefined;
|
|
99
|
+
return `${origin}${url.pathname}${url.search}`;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
let url;
|
|
106
|
+
try {
|
|
107
|
+
url = new URL(value);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
if ((url.protocol !== "https:" && url.protocol !== "http:") || url.username || url.password)
|
|
113
|
+
return undefined;
|
|
114
|
+
if (!ownHost(url.hostname, ownHosts))
|
|
115
|
+
return undefined;
|
|
116
|
+
return `${origin}${url.pathname}${url.search}`;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The hostname when `value` is a URL on an unspecified bind address
|
|
120
|
+
* (`0.0.0.0`, `[::]`), which no browser or agent can open; else undefined.
|
|
121
|
+
*/
|
|
122
|
+
export function bindAddressOf(value) {
|
|
123
|
+
if (typeof value !== "string")
|
|
124
|
+
return undefined;
|
|
125
|
+
try {
|
|
126
|
+
const host = new URL(value).hostname;
|
|
127
|
+
return isUnspecifiedHostname(host) ? host : undefined;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* A link as published to agents: a store-own URL moved onto `origin`; any
|
|
135
|
+
* other value unchanged, except one on a bind address, which is dropped
|
|
136
|
+
* (undefined) even when it can't be moved.
|
|
137
|
+
*/
|
|
138
|
+
export function publishedUrl(value, origin, ownHosts) {
|
|
139
|
+
return rebaseStoreUrl(value, origin, ownHosts) ?? (bindAddressOf(value) ? undefined : value);
|
|
140
|
+
}
|
|
141
|
+
/** True when `value` is a store-own URL (see `rebaseStoreUrl`). */
|
|
142
|
+
export function isStoreOwnUrl(value, ownHosts) {
|
|
143
|
+
// Any fixed origin works: only whether the URL rebases matters, not where.
|
|
144
|
+
return rebaseStoreUrl(value, "https://shop.example.com", ownHosts) !== undefined;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* An image URL for agents: a store-own image moved onto `origin`, another
|
|
148
|
+
* site's https image (a CDN) unchanged, anything else dropped.
|
|
149
|
+
*/
|
|
150
|
+
export function publicImageUrl(value, origin, ownHosts) {
|
|
151
|
+
const own = rebaseStoreUrl(value, origin, ownHosts);
|
|
152
|
+
if (own)
|
|
153
|
+
return own;
|
|
154
|
+
if (typeof value !== "string" || value.length > 2000 || UNSAFE.test(value))
|
|
155
|
+
return undefined;
|
|
156
|
+
try {
|
|
157
|
+
const url = new URL(value);
|
|
158
|
+
if (url.protocol !== "https:" || url.username || url.password)
|
|
159
|
+
return undefined;
|
|
160
|
+
return url.href;
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* A copy of a product with its page and image URLs on `origin`. A product
|
|
168
|
+
* URL that isn't the store's own is left as it is (callers that need a
|
|
169
|
+
* store-own page check it themselves); images that can't be published are dropped.
|
|
170
|
+
*/
|
|
171
|
+
export function onPublicOrigin(product, origin, ownHosts) {
|
|
172
|
+
const url = product.url === undefined ? undefined : publishedUrl(product.url, origin, ownHosts);
|
|
173
|
+
const images = product.images?.map((image) => publicImageUrl(image, origin, ownHosts)).filter((image) => Boolean(image));
|
|
174
|
+
const { url: _original, ...rest } = product;
|
|
175
|
+
return {
|
|
176
|
+
...rest,
|
|
177
|
+
...(url !== undefined ? { url } : {}),
|
|
178
|
+
...(product.images ? { images } : {}),
|
|
179
|
+
};
|
|
180
|
+
}
|