@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,521 @@
|
|
|
1
|
+
import { createPublicKey, timingSafeEqual, verify as verifySignature } from "node:crypto";
|
|
2
|
+
import { isRecord, sha256, tryParseJson } from "./util.js";
|
|
3
|
+
import { isLoopbackHostname } from "./preview-hosts.js";
|
|
4
|
+
import { requestUrl } from "./public-origin.js";
|
|
5
|
+
// Relyon-issued hand-off tokens. In Test, Relyon links every agent
|
|
6
|
+
// checkout to `https://<verified store host>/checkout?relyon_cart=<token>`,
|
|
7
|
+
// with its own token: `rc_test_<base64url JSON {v, ac, cs, h, r, exp}>.<MAC>`
|
|
8
|
+
// (agent checkout, checkout session, hostname, cart revision, expiry). The MAC
|
|
9
|
+
// key is Relyon's alone and the v1 contract has no operation that resolves or
|
|
10
|
+
// verifies a cart token, so the kit verifies locally: it accepts a Relyon token
|
|
11
|
+
// only when it is exactly the token it saw Relyon issue in a relayed response
|
|
12
|
+
// for that agent checkout, and only once. The payload only finds the kit's
|
|
13
|
+
// record; the cart and the attribution always come from that record.
|
|
14
|
+
//
|
|
15
|
+
// Live (`rc_live_`, a compact ES256 JWS signed with the merchant's Live
|
|
16
|
+
// signing key) follows the same rule, and adds checks: a strict parse, the
|
|
17
|
+
// kit's own mode (a Live secret key with the explicit `live: true` opt-in,
|
|
18
|
+
// never the token's prefix), and the JWS signature against the Live keys in
|
|
19
|
+
// the store's Relyon UCP profile. In a Live kit a Relyon Test token is
|
|
20
|
+
// refused; in a Test kit a Live token is refused. Every Live failure is
|
|
21
|
+
// `refused`, never `not_ours`.
|
|
22
|
+
const PREFIX = "rc_test_";
|
|
23
|
+
const MAX_LENGTH = 2000;
|
|
24
|
+
const SEGMENT = /^[A-Za-z0-9_-]+$/;
|
|
25
|
+
const PROTOCOLS = ["acp", "ucp", "mcp"];
|
|
26
|
+
/** Reads a token in Relyon's dotted shape; null for anything else (including the kit's own sealed tokens). */
|
|
27
|
+
export function parseRelyonCartToken(token) {
|
|
28
|
+
if (typeof token !== "string" || token.length > MAX_LENGTH || !token.startsWith(PREFIX))
|
|
29
|
+
return null;
|
|
30
|
+
const parts = token.slice(PREFIX.length).split(".");
|
|
31
|
+
if (parts.length !== 2 || !SEGMENT.test(parts[0]) || !SEGMENT.test(parts[1]))
|
|
32
|
+
return null;
|
|
33
|
+
let payload;
|
|
34
|
+
try {
|
|
35
|
+
payload = JSON.parse(Buffer.from(parts[0], "base64url").toString("utf8"));
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
if (!isRecord(payload) || payload.v !== 1 || typeof payload.ac !== "string" || typeof payload.cs !== "string"
|
|
41
|
+
|| typeof payload.h !== "string" || typeof payload.r !== "string" || !Number.isInteger(payload.exp)) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return { ac: payload.ac, cs: payload.cs, h: payload.h.toLowerCase(), r: payload.r, exp: payload.exp };
|
|
45
|
+
}
|
|
46
|
+
const LIVE_PREFIX = "rc_live_";
|
|
47
|
+
const LIVE_TYP = "rc-live+jws";
|
|
48
|
+
const LIVE_HEADER_KEYS = ["alg", "kid", "typ"];
|
|
49
|
+
const LIVE_PAYLOAD_KEYS = ["ac", "aud", "cs", "exp", "h", "r", "v"];
|
|
50
|
+
const HOSTNAME = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
|
|
51
|
+
/** Strict, canonical base64url (no padding; re-encodes to the same text), or null. */
|
|
52
|
+
function base64url(segment) {
|
|
53
|
+
if (!SEGMENT.test(segment))
|
|
54
|
+
return null;
|
|
55
|
+
const bytes = Buffer.from(segment, "base64url");
|
|
56
|
+
return bytes.toString("base64url") === segment ? bytes : null;
|
|
57
|
+
}
|
|
58
|
+
// Every JSON string token, in order; one followed by ":" is a member name.
|
|
59
|
+
const JSON_STRINGS = /"(?:[^"\\]|\\.)*"(\s*:)?/g;
|
|
60
|
+
/** A JSON object whose member names are all distinct (a duplicate name is refused, never last-wins). */
|
|
61
|
+
function jsonObject(bytes) {
|
|
62
|
+
let text, value;
|
|
63
|
+
try {
|
|
64
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
65
|
+
value = JSON.parse(text);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
if (!isRecord(value) || Array.isArray(value))
|
|
71
|
+
return null;
|
|
72
|
+
let names = 0;
|
|
73
|
+
for (const match of text.matchAll(JSON_STRINGS))
|
|
74
|
+
if (match[1] !== undefined)
|
|
75
|
+
names += 1;
|
|
76
|
+
return names === Object.keys(value).length ? value : null;
|
|
77
|
+
}
|
|
78
|
+
const exactKeys = (value, keys) => JSON.stringify(Object.keys(value).sort()) === JSON.stringify(keys);
|
|
79
|
+
/**
|
|
80
|
+
* Reads a Relyon Live token, `rc_live_<protected>.<payload>.<signature>`,
|
|
81
|
+
* strictly: at most 2000 characters; canonical base64url segments; a
|
|
82
|
+
* protected header of exactly `{alg: "ES256", kid, typ: "rc-live+jws"}` (so no
|
|
83
|
+
* `b64`, `crit`, `jwk`, `jku`, `x5u`, `x5c` or `x5t`); a payload of exactly
|
|
84
|
+
* `{v: 1, ac, cs, h, r, exp, aud}` with a lower-case hostname `h` and
|
|
85
|
+
* `aud === "https://" + h`; a 64-byte signature. null for anything else. The
|
|
86
|
+
* signature is not checked here.
|
|
87
|
+
*/
|
|
88
|
+
export function parseRelyonLiveCartToken(token) {
|
|
89
|
+
if (typeof token !== "string" || token.length > MAX_LENGTH || !token.startsWith(LIVE_PREFIX))
|
|
90
|
+
return null;
|
|
91
|
+
const parts = token.slice(LIVE_PREFIX.length).split(".");
|
|
92
|
+
if (parts.length !== 3)
|
|
93
|
+
return null;
|
|
94
|
+
const [protectedPart, payloadPart, signaturePart] = parts;
|
|
95
|
+
const headerBytes = base64url(protectedPart), payloadBytes = base64url(payloadPart), signature = base64url(signaturePart);
|
|
96
|
+
if (!headerBytes || !payloadBytes || !signature || signature.length !== 64)
|
|
97
|
+
return null;
|
|
98
|
+
const header = jsonObject(headerBytes), payload = jsonObject(payloadBytes);
|
|
99
|
+
if (!header || !payload || !exactKeys(header, LIVE_HEADER_KEYS) || !exactKeys(payload, LIVE_PAYLOAD_KEYS))
|
|
100
|
+
return null;
|
|
101
|
+
if (header.alg !== "ES256" || header.typ !== LIVE_TYP || typeof header.kid !== "string"
|
|
102
|
+
|| header.kid.length < 1 || header.kid.length > 256)
|
|
103
|
+
return null;
|
|
104
|
+
if (payload.v !== 1 || typeof payload.ac !== "string" || typeof payload.cs !== "string" || typeof payload.r !== "string"
|
|
105
|
+
|| typeof payload.h !== "string" || !HOSTNAME.test(payload.h) || !Number.isSafeInteger(payload.exp)
|
|
106
|
+
|| payload.aud !== `https://${payload.h}`)
|
|
107
|
+
return null;
|
|
108
|
+
return {
|
|
109
|
+
kid: header.kid,
|
|
110
|
+
payload: { ac: payload.ac, cs: payload.cs, h: payload.h, r: payload.r, exp: payload.exp, aud: payload.aud },
|
|
111
|
+
signingInput: `${protectedPart}.${payloadPart}`,
|
|
112
|
+
signature,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/** The payload a hand-off token names, read the way the kit's mode reads it: Live tokens in Live, Test tokens in Test. */
|
|
116
|
+
function payloadFor(token, livemode) {
|
|
117
|
+
return livemode ? parseRelyonLiveCartToken(token)?.payload ?? null : parseRelyonCartToken(token);
|
|
118
|
+
}
|
|
119
|
+
/** The Relyon token of one hand-off link (`…?relyon_cart=`), when the link's host is the one the token names. */
|
|
120
|
+
function tokenOfLink(link, livemode = false) {
|
|
121
|
+
let url;
|
|
122
|
+
try {
|
|
123
|
+
url = new URL(link);
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const token = url.searchParams.get("relyon_cart");
|
|
129
|
+
if (!token)
|
|
130
|
+
return null;
|
|
131
|
+
const payload = payloadFor(token, livemode);
|
|
132
|
+
return payload && payload.h === url.hostname.toLowerCase() ? token : null;
|
|
133
|
+
}
|
|
134
|
+
const LINKS = /https?:\/\/[^\s"'<>]+/g;
|
|
135
|
+
/** Every Relyon hand-off token in a string (a URL, or prose that contains one). */
|
|
136
|
+
function tokensIn(text, livemode) {
|
|
137
|
+
if (!text.includes("relyon_cart="))
|
|
138
|
+
return [];
|
|
139
|
+
const found = [];
|
|
140
|
+
for (const match of text.match(LINKS) ?? []) {
|
|
141
|
+
// Prose may end the link with punctuation that isn't part of it.
|
|
142
|
+
const token = tokenOfLink(match, livemode) ?? tokenOfLink(match.replace(/[.,;:!?)\]]+$/, ""), livemode);
|
|
143
|
+
if (token)
|
|
144
|
+
found.push(token);
|
|
145
|
+
}
|
|
146
|
+
return found;
|
|
147
|
+
}
|
|
148
|
+
/** Whether a token's payload names this checkout (its agent checkout or checkout session) on one of the store's hostnames. */
|
|
149
|
+
function boundTo(token, binding) {
|
|
150
|
+
const payload = payloadFor(token, binding.livemode === true);
|
|
151
|
+
if (!payload || !binding.hostnames.includes(payload.h))
|
|
152
|
+
return false;
|
|
153
|
+
const ids = binding.ids.filter((id) => typeof id === "string" && id.length > 0);
|
|
154
|
+
return ids.includes(payload.ac) || ids.includes(payload.cs);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The Relyon hand-off token a relayed response body carries, or null. Only a
|
|
158
|
+
* token whose payload names this checkout (`binding`) counts, so a link that
|
|
159
|
+
* names another checkout is never recorded for this one. Where Relyon itself
|
|
160
|
+
* puts the link ranks first: a top-level `continue_url` (or MCP's
|
|
161
|
+
* `structuredContent.checkout_url`), then a top-level message, then anywhere
|
|
162
|
+
* else (an answer may echo agent-supplied text). Two different tokens at the
|
|
163
|
+
* first rank that has any is ambiguous: none is taken. Reads only; the body is
|
|
164
|
+
* never changed.
|
|
165
|
+
*/
|
|
166
|
+
export function issuedCartToken(body, binding) {
|
|
167
|
+
if (!body || !body.includes("relyon_cart="))
|
|
168
|
+
return null;
|
|
169
|
+
const parsed = tryParseJson(body);
|
|
170
|
+
const ranks = [new Set(), new Set(), new Set()];
|
|
171
|
+
const add = (rank, text) => {
|
|
172
|
+
if (typeof text !== "string")
|
|
173
|
+
return;
|
|
174
|
+
for (const token of tokensIn(text, binding.livemode === true))
|
|
175
|
+
if (boundTo(token, binding))
|
|
176
|
+
ranks[rank].add(token);
|
|
177
|
+
};
|
|
178
|
+
if (parsed === undefined || parsed === null || typeof parsed !== "object") {
|
|
179
|
+
add(2, body);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
if (isRecord(parsed)) {
|
|
183
|
+
add(0, parsed.continue_url);
|
|
184
|
+
const result = isRecord(parsed.result) ? parsed.result : null;
|
|
185
|
+
const structured = result && isRecord(result.structuredContent) ? result.structuredContent : null;
|
|
186
|
+
if (structured) {
|
|
187
|
+
add(0, structured.checkout_url);
|
|
188
|
+
add(0, structured.continue_url);
|
|
189
|
+
}
|
|
190
|
+
if (Array.isArray(parsed.messages))
|
|
191
|
+
for (const message of parsed.messages)
|
|
192
|
+
if (isRecord(message))
|
|
193
|
+
add(1, message.content);
|
|
194
|
+
}
|
|
195
|
+
const seen = new Set();
|
|
196
|
+
const walk = (value, depth) => {
|
|
197
|
+
if (depth > 32)
|
|
198
|
+
return;
|
|
199
|
+
if (typeof value === "string") {
|
|
200
|
+
add(2, value);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (value === null || typeof value !== "object" || seen.has(value))
|
|
204
|
+
return;
|
|
205
|
+
seen.add(value);
|
|
206
|
+
for (const entry of Array.isArray(value) ? value : Object.values(value))
|
|
207
|
+
walk(entry, depth + 1);
|
|
208
|
+
};
|
|
209
|
+
walk(parsed, 0);
|
|
210
|
+
}
|
|
211
|
+
for (const rank of ranks) {
|
|
212
|
+
if (rank.size === 1)
|
|
213
|
+
return [...rank][0];
|
|
214
|
+
if (rank.size > 1)
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
function sameToken(a, b) {
|
|
220
|
+
const left = Buffer.from(a, "utf8");
|
|
221
|
+
const right = Buffer.from(b, "utf8");
|
|
222
|
+
return left.length === right.length && timingSafeEqual(left, right);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* The store hostname a request is for: its URL host when that is one of the
|
|
226
|
+
* store's hostnames, else the first X-Forwarded-Host value when that is (a
|
|
227
|
+
* proxy in front of the store's server). null when it names none: Relyon
|
|
228
|
+
* tokens then land unattributed, never on an unchecked host.
|
|
229
|
+
*/
|
|
230
|
+
function requestHost(ctx, request, wanted) {
|
|
231
|
+
let urlHost;
|
|
232
|
+
try {
|
|
233
|
+
// A dev server bound to 0.0.0.0 or [::]: the validated Host header (Test only).
|
|
234
|
+
urlHost = requestUrl(request, ctx.hostnames, !ctx.gateway.livemode).hostname.toLowerCase();
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
// A loopback URL host (a Test store's local address) is the request's host
|
|
240
|
+
// only for a token issued for it; otherwise it is the internal side of a
|
|
241
|
+
// proxy, and the forwarded hostname decides, as before.
|
|
242
|
+
if (ctx.hostnames.includes(urlHost) && (!isLoopbackHostname(urlHost) || urlHost === wanted))
|
|
243
|
+
return urlHost;
|
|
244
|
+
const forwarded = request.headers.get("x-forwarded-host")?.split(",")[0]?.trim().split(":")[0]?.toLowerCase();
|
|
245
|
+
return forwarded && ctx.hostnames.includes(forwarded) ? forwarded : null;
|
|
246
|
+
}
|
|
247
|
+
/** The kit's records the payload may name: by agent checkout ID, then by checkout session ID. */
|
|
248
|
+
async function candidateRecords(ctx, payload) {
|
|
249
|
+
const records = [];
|
|
250
|
+
const add = (record) => {
|
|
251
|
+
if (record && !records.some((known) => known.protocol === record.protocol && known.sessionId === record.sessionId))
|
|
252
|
+
records.push(record);
|
|
253
|
+
};
|
|
254
|
+
add(await ctx.state.byAgentCheckoutId(payload.ac));
|
|
255
|
+
// Today's server may answer without an agent checkout ID: the kit then keys
|
|
256
|
+
// the record by Relyon's checkout session ID (and the protocol's checkout
|
|
257
|
+
// ID is usually that same ID).
|
|
258
|
+
add(await ctx.state.byAgentCheckoutId(payload.cs));
|
|
259
|
+
for (const protocol of PROTOCOLS)
|
|
260
|
+
add(await ctx.state.get(protocol, payload.cs));
|
|
261
|
+
return records;
|
|
262
|
+
}
|
|
263
|
+
/** Whether the payload names this record: the token was captured for it, so it must say so. */
|
|
264
|
+
function namesRecord(payload, record) {
|
|
265
|
+
return (record.agentCheckoutId !== null && payload.ac === record.agentCheckoutId)
|
|
266
|
+
|| payload.cs === record.sessionId || payload.cs === record.agentCheckoutId;
|
|
267
|
+
}
|
|
268
|
+
// Single use. Each Relyon token lands attributed once; this process remembers
|
|
269
|
+
// every token it accepted (by digest, until it expires) per state store, and a
|
|
270
|
+
// state store with `claimHandoffToken` makes that hold across servers.
|
|
271
|
+
const MAX_CONSUMED = 100_000;
|
|
272
|
+
const consumedByStore = new WeakMap();
|
|
273
|
+
const tokenDigest = (token) => sha256(`relyon-handoff-token\n${token}`);
|
|
274
|
+
function consumedHere(ctx) {
|
|
275
|
+
let consumed = consumedByStore.get(ctx.state);
|
|
276
|
+
if (!consumed) {
|
|
277
|
+
consumed = new Map();
|
|
278
|
+
consumedByStore.set(ctx.state, consumed);
|
|
279
|
+
}
|
|
280
|
+
return consumed;
|
|
281
|
+
}
|
|
282
|
+
/** Whether this process already accepted the token (it can't land attributed again). */
|
|
283
|
+
export function handoffTokenConsumed(ctx, token) {
|
|
284
|
+
const expiresAt = consumedHere(ctx).get(tokenDigest(token));
|
|
285
|
+
return expiresAt !== undefined && ctx.now() < expiresAt;
|
|
286
|
+
}
|
|
287
|
+
/** Claims the token's single landing; false when it was already used (or the claim can't be made). */
|
|
288
|
+
async function claimSingleUse(ctx, token, expiresAt) {
|
|
289
|
+
const now = ctx.now();
|
|
290
|
+
const consumed = consumedHere(ctx);
|
|
291
|
+
const digest = tokenDigest(token);
|
|
292
|
+
const known = consumed.get(digest);
|
|
293
|
+
if (known !== undefined && now < known)
|
|
294
|
+
return false;
|
|
295
|
+
// Synchronous check-and-set: of simultaneous landings in this process only one gets past here.
|
|
296
|
+
consumed.delete(digest);
|
|
297
|
+
consumed.set(digest, expiresAt);
|
|
298
|
+
if (consumed.size > MAX_CONSUMED) {
|
|
299
|
+
for (const [key, until] of consumed)
|
|
300
|
+
if (until <= now)
|
|
301
|
+
consumed.delete(key);
|
|
302
|
+
while (consumed.size > MAX_CONSUMED)
|
|
303
|
+
consumed.delete(consumed.keys().next().value);
|
|
304
|
+
}
|
|
305
|
+
if (typeof ctx.state.claimHandoffToken !== "function")
|
|
306
|
+
return true;
|
|
307
|
+
try {
|
|
308
|
+
return (await ctx.state.claimHandoffToken(digest, { now, expiresAt })) === true;
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
ctx.logger.error("The checkout state store failed to claim a Relyon hand-off link; it lands unattributed", { error: describeError(error) });
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* How long after the kit first saw a Test hand-off link relayed it still lands
|
|
317
|
+
* when the link's own expiry (on the Test sandbox clock) is already past on
|
|
318
|
+
* the kit's clock: Relyon's quote lifetime.
|
|
319
|
+
*/
|
|
320
|
+
export const TEST_LANDING_WINDOW_MS = 30 * 60 * 1000;
|
|
321
|
+
/** Resolves a Relyon-issued hand-off token (see `RelyonCartTokenCheck`). Never throws. */
|
|
322
|
+
export async function resolveRelyonCartToken(ctx, token, request) {
|
|
323
|
+
// The environment comes from the kit's own mode, never from the token.
|
|
324
|
+
if (typeof token === "string" && token.startsWith(LIVE_PREFIX)) {
|
|
325
|
+
return ctx.gateway.livemode ? resolveLiveToken(ctx, token, request) : { kind: "refused" };
|
|
326
|
+
}
|
|
327
|
+
// A Relyon-shaped Test token (dotted) in a Live kit is refused. The kit's
|
|
328
|
+
// own sealed tokens (`SealedCartTokens`, no dot) still reach the resolver.
|
|
329
|
+
if (ctx.gateway.livemode && typeof token === "string" && token.startsWith(PREFIX) && token.includes("."))
|
|
330
|
+
return { kind: "refused" };
|
|
331
|
+
const payload = parseRelyonCartToken(token);
|
|
332
|
+
if (!payload || !ctx.hostnames.includes(payload.h))
|
|
333
|
+
return { kind: "not_ours" };
|
|
334
|
+
if (handoffTokenConsumed(ctx, token))
|
|
335
|
+
return { kind: "refused" };
|
|
336
|
+
const host = requestHost(ctx, request, payload.h);
|
|
337
|
+
if (host === null) {
|
|
338
|
+
ctx.logger.warn("A Relyon hand-off link landed on a request that names none of the store's hostnames; it lands unattributed. Behind a proxy, keep the Host header or send X-Forwarded-Host", {
|
|
339
|
+
hostnames: ctx.hostnames,
|
|
340
|
+
});
|
|
341
|
+
return { kind: "refused" };
|
|
342
|
+
}
|
|
343
|
+
if (host !== payload.h)
|
|
344
|
+
return { kind: "refused" };
|
|
345
|
+
// The link's expiry is on Relyon's clock for this mode. In Test that is the
|
|
346
|
+
// sandbox clock, which stays at its start date until the sandbox moves it,
|
|
347
|
+
// so a link can look expired on the store's clock the moment it is issued.
|
|
348
|
+
// A Test link past its expiry on the kit's clock therefore lands only
|
|
349
|
+
// within TEST_LANDING_WINDOW_MS of the kit first seeing it relayed
|
|
350
|
+
// (`handoffTokenAt`, which repeating a step never moves); without that
|
|
351
|
+
// time it is refused (Relyon still decides whether the checkout can be paid).
|
|
352
|
+
const linkExpiry = payload.exp * 1000;
|
|
353
|
+
const pastExpiry = ctx.now() >= linkExpiry;
|
|
354
|
+
if (pastExpiry && ctx.gateway.livemode)
|
|
355
|
+
return { kind: "refused" };
|
|
356
|
+
let records;
|
|
357
|
+
try {
|
|
358
|
+
records = await candidateRecords(ctx, payload);
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
ctx.logger.error("The checkout state store failed to load a checkout for a Relyon hand-off link; it lands unattributed", { error: describeError(error) });
|
|
362
|
+
return { kind: "refused" };
|
|
363
|
+
}
|
|
364
|
+
const record = records.find((entry) => typeof entry.handoffToken === "string" && sameToken(entry.handoffToken, token) && namesRecord(payload, entry));
|
|
365
|
+
if (!record)
|
|
366
|
+
return pastExpiry ? { kind: "refused" } : { kind: "not_ours" };
|
|
367
|
+
if (pastExpiry && typeof record.handoffTokenAt !== "number")
|
|
368
|
+
return { kind: "refused" };
|
|
369
|
+
const expiresAt = pastExpiry ? record.handoffTokenAt + TEST_LANDING_WINDOW_MS : linkExpiry;
|
|
370
|
+
if (ctx.now() >= expiresAt)
|
|
371
|
+
return { kind: "refused" };
|
|
372
|
+
if (!(await claimSingleUse(ctx, token, expiresAt)))
|
|
373
|
+
return { kind: "refused" };
|
|
374
|
+
// The attribution comes from the kit's record, never from the payload.
|
|
375
|
+
return { kind: "accepted", cart: { agentCheckoutId: record.agentCheckoutId ?? record.sessionId, protocol: record.protocol, inputs: record.inputs } };
|
|
376
|
+
}
|
|
377
|
+
/** How long a forced key refetch (an unknown `kid`) is not repeated for the same hostname. */
|
|
378
|
+
const KEY_REFETCH_INTERVAL_MS = 5_000;
|
|
379
|
+
const lastKeyRefetch = new WeakMap();
|
|
380
|
+
/** The ES256 P-256 signing key with this `kid`, from a published JWK set; null when absent or unusable. */
|
|
381
|
+
export function liveSigningKey(keys, kid) {
|
|
382
|
+
const matches = keys.filter((key) => isRecord(key) && key.kid === kid);
|
|
383
|
+
if (matches.length !== 1)
|
|
384
|
+
return null;
|
|
385
|
+
const jwk = matches[0];
|
|
386
|
+
if (jwk.kty !== "EC" || jwk.crv !== "P-256" || typeof jwk.x !== "string" || typeof jwk.y !== "string"
|
|
387
|
+
|| (jwk.use !== undefined && jwk.use !== "sig") || (jwk.alg !== undefined && jwk.alg !== "ES256")
|
|
388
|
+
|| "d" in jwk)
|
|
389
|
+
return null;
|
|
390
|
+
const x = base64url(jwk.x), y = base64url(jwk.y);
|
|
391
|
+
if (!x || !y || x.length !== 32 || y.length !== 32)
|
|
392
|
+
return null;
|
|
393
|
+
try {
|
|
394
|
+
return createPublicKey({ key: { kty: "EC", crv: "P-256", x: jwk.x, y: jwk.y }, format: "jwk" });
|
|
395
|
+
}
|
|
396
|
+
catch {
|
|
397
|
+
return null;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Whether the Live token's ES256 signature verifies against the store's
|
|
402
|
+
* published Live keys for its hostname: a fresh copy of the store's Relyon
|
|
403
|
+
* UCP profile (cached for its lifetime; a stale copy served while Relyon is
|
|
404
|
+
* unreachable is not used), refetched once when the `kid` is unknown.
|
|
405
|
+
*/
|
|
406
|
+
async function liveSignatureVerifies(ctx, parsed) {
|
|
407
|
+
const lookup = ctx.liveSigningKeys;
|
|
408
|
+
if (typeof lookup !== "function")
|
|
409
|
+
return false;
|
|
410
|
+
const hostname = parsed.payload.h;
|
|
411
|
+
let key;
|
|
412
|
+
try {
|
|
413
|
+
key = liveSigningKey(await lookup(hostname, { refresh: false }), parsed.kid);
|
|
414
|
+
if (!key) {
|
|
415
|
+
let byHost = lastKeyRefetch.get(ctx.state);
|
|
416
|
+
if (!byHost) {
|
|
417
|
+
byHost = new Map();
|
|
418
|
+
lastKeyRefetch.set(ctx.state, byHost);
|
|
419
|
+
}
|
|
420
|
+
const last = byHost.get(hostname);
|
|
421
|
+
if (last !== undefined && ctx.now() - last < KEY_REFETCH_INTERVAL_MS)
|
|
422
|
+
return false;
|
|
423
|
+
byHost.set(hostname, ctx.now());
|
|
424
|
+
key = liveSigningKey(await lookup(hostname, { refresh: true }), parsed.kid);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
catch (error) {
|
|
428
|
+
ctx.logger.error("The store's Live signing keys couldn't be read from Relyon; a Live hand-off link lands unattributed", { error: describeError(error) });
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
if (!key)
|
|
432
|
+
return false;
|
|
433
|
+
try {
|
|
434
|
+
return verifySignature("sha256", Buffer.from(parsed.signingInput, "ascii"), { key, dsaEncoding: "ieee-p1363" }, parsed.signature);
|
|
435
|
+
}
|
|
436
|
+
catch {
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* A Live token in a Live kit. Accepted only when it parses strictly, names
|
|
442
|
+
* one of the store's hostnames and the request's, is unexpired, is exactly
|
|
443
|
+
* (constant time) the latest token the kit saw in a relayed Live answer for
|
|
444
|
+
* that checkout, its signature verifies against the store's Live keys, and
|
|
445
|
+
* its single landing is claimed now. Anything else is `refused`.
|
|
446
|
+
*/
|
|
447
|
+
async function resolveLiveToken(ctx, token, request) {
|
|
448
|
+
const parsed = parseRelyonLiveCartToken(token);
|
|
449
|
+
if (!parsed)
|
|
450
|
+
return { kind: "refused" };
|
|
451
|
+
const { payload } = parsed;
|
|
452
|
+
if (!ctx.hostnames.includes(payload.h))
|
|
453
|
+
return { kind: "refused" };
|
|
454
|
+
if (handoffTokenConsumed(ctx, token))
|
|
455
|
+
return { kind: "refused" };
|
|
456
|
+
const host = requestHost(ctx, request, payload.h);
|
|
457
|
+
if (host === null) {
|
|
458
|
+
ctx.logger.warn("A Relyon hand-off link landed on a request that names none of the store's hostnames; it lands unattributed. Behind a proxy, keep the Host header or send X-Forwarded-Host", {
|
|
459
|
+
hostnames: ctx.hostnames,
|
|
460
|
+
});
|
|
461
|
+
return { kind: "refused" };
|
|
462
|
+
}
|
|
463
|
+
if (host !== payload.h)
|
|
464
|
+
return { kind: "refused" };
|
|
465
|
+
if (ctx.now() >= payload.exp * 1000)
|
|
466
|
+
return { kind: "refused" };
|
|
467
|
+
let records;
|
|
468
|
+
try {
|
|
469
|
+
records = await candidateRecords(ctx, payload);
|
|
470
|
+
}
|
|
471
|
+
catch (error) {
|
|
472
|
+
ctx.logger.error("The checkout state store failed to load a checkout for a Relyon hand-off link; it lands unattributed", { error: describeError(error) });
|
|
473
|
+
return { kind: "refused" };
|
|
474
|
+
}
|
|
475
|
+
const record = records.find((entry) => typeof entry.handoffToken === "string" && sameToken(entry.handoffToken, token) && namesRecord(payload, entry));
|
|
476
|
+
if (!record)
|
|
477
|
+
return { kind: "refused" };
|
|
478
|
+
// Only a token the kit saw Relyon issue gets this far, so arbitrary tokens can't make the kit fetch keys.
|
|
479
|
+
if (!(await liveSignatureVerifies(ctx, parsed)))
|
|
480
|
+
return { kind: "refused" };
|
|
481
|
+
// The key lookup may have waited on Relyon: the token must still be unexpired when claimed.
|
|
482
|
+
if (ctx.now() >= payload.exp * 1000)
|
|
483
|
+
return { kind: "refused" };
|
|
484
|
+
if (!(await claimSingleUse(ctx, token, payload.exp * 1000)))
|
|
485
|
+
return { kind: "refused" };
|
|
486
|
+
// After the claim, the token must still be the checkout's latest and unexpired: a cart that
|
|
487
|
+
// changed while the keys were read (or the claim was made) supersedes it, and the claimed
|
|
488
|
+
// token lands unattributed. The cart comes from this single read of the record, never from an
|
|
489
|
+
// earlier snapshot: a later update is ordered after the landing.
|
|
490
|
+
let current;
|
|
491
|
+
try {
|
|
492
|
+
current = await ctx.state.get(record.protocol, record.sessionId);
|
|
493
|
+
}
|
|
494
|
+
catch (error) {
|
|
495
|
+
ctx.logger.error("The checkout state store failed to load a checkout for a Relyon hand-off link; it lands unattributed", { error: describeError(error) });
|
|
496
|
+
return { kind: "refused" };
|
|
497
|
+
}
|
|
498
|
+
if (!current || typeof current.handoffToken !== "string" || !sameToken(current.handoffToken, token)
|
|
499
|
+
|| !namesRecord(payload, current) || ctx.now() >= payload.exp * 1000)
|
|
500
|
+
return { kind: "refused" };
|
|
501
|
+
return { kind: "accepted", cart: { agentCheckoutId: current.agentCheckoutId ?? current.sessionId, protocol: current.protocol, inputs: current.inputs } };
|
|
502
|
+
}
|
|
503
|
+
// Cart tokens in text: Relyon's and the kit's sealed ones share the prefix.
|
|
504
|
+
const TOKEN_TEXT = /rc_(?:test|live)_[A-Za-z0-9_.%-]+/g;
|
|
505
|
+
const TOKEN_PARAM = /(relyon_cart(?:=|%3D|"\s*:\s*"))[^&\s"'<>]+/gi;
|
|
506
|
+
/** Text with every cart token (and every `relyon_cart` value) replaced, for logs. */
|
|
507
|
+
export function redactCartTokens(text) {
|
|
508
|
+
return text.replace(TOKEN_PARAM, "$1[redacted]").replace(TOKEN_TEXT, "rc_[redacted]");
|
|
509
|
+
}
|
|
510
|
+
// Secret keys, webhook secrets and ownership-link tokens never reach a log line either.
|
|
511
|
+
const SECRET_TEXT = /\b(?:(?:rly|rk|sk)_(?:test|live)|whsec)_[A-Za-z0-9_-]+/g;
|
|
512
|
+
const CLAIM_TOKEN = /([#?&]token=)[^&\s"'<>]+/gi;
|
|
513
|
+
/** Text with cart tokens, secret keys, webhook secrets and ownership-link tokens replaced, for logs. */
|
|
514
|
+
export function redactLogText(text) {
|
|
515
|
+
return redactCartTokens(text).replace(SECRET_TEXT, "[redacted secret]").replace(CLAIM_TOKEN, "$1[redacted]");
|
|
516
|
+
}
|
|
517
|
+
/** An error's message for a log line, with cart tokens and secrets redacted. */
|
|
518
|
+
export function describeError(error, stack = false) {
|
|
519
|
+
const text = error instanceof Error ? (stack ? error.stack ?? error.message : error.message) : String(error);
|
|
520
|
+
return redactLogText(text);
|
|
521
|
+
}
|
package/dist/relyon.d.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { type RelyonClient } from "./sdk/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Relyon could not give a usable answer: it was unreachable, timed out,
|
|
4
|
+
* answered 5xx, or answered something that doesn't match the contract.
|
|
5
|
+
* Decision calls (checkout steps, payments) are refused when this happens;
|
|
6
|
+
* discovery reads fall back to their last good copy.
|
|
7
|
+
*/
|
|
8
|
+
export declare class RelyonUnavailableError extends Error {
|
|
9
|
+
readonly kind: "unreachable" | "timeout" | "server_error" | "malformed";
|
|
10
|
+
readonly status?: number | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* True only when the request provably never left the store: the
|
|
13
|
+
* connection was refused or the hostname didn't resolve. Anything else
|
|
14
|
+
* (a reset, a timeout, an unknown network error) may have been sent.
|
|
15
|
+
*/
|
|
16
|
+
readonly notSent: boolean;
|
|
17
|
+
constructor(kind: "unreachable" | "timeout" | "server_error" | "malformed", message: string, status?: number | undefined,
|
|
18
|
+
/**
|
|
19
|
+
* True only when the request provably never left the store: the
|
|
20
|
+
* connection was refused or the hostname didn't resolve. Anything else
|
|
21
|
+
* (a reset, a timeout, an unknown network error) may have been sent.
|
|
22
|
+
*/
|
|
23
|
+
notSent?: boolean);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* True when a fetch failure provably happened before the request was sent.
|
|
27
|
+
* Follows `cause` chains (Node's fetch wraps the socket error) and requires
|
|
28
|
+
* every attempt of an AggregateError (happy eyeballs) to be pre-send.
|
|
29
|
+
*/
|
|
30
|
+
export declare function failedBeforeSending(error: unknown, depth?: number): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* The mode a connection runs in. Live needs both a Live key and the explicit
|
|
33
|
+
* `live: true` opt-in; either one alone is refused with a `TypeError`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function liveModeFor(connection: Pick<RelyonConnection, "apiKey" | "live">): boolean;
|
|
36
|
+
/** Relyon refused the store's request with a contract Problem (4xx). */
|
|
37
|
+
export declare class RelyonProblemError extends Error {
|
|
38
|
+
readonly status: number;
|
|
39
|
+
readonly code: string;
|
|
40
|
+
readonly param?: string | undefined;
|
|
41
|
+
/** The whole Problem body Relyon answered with (extra fields included). */
|
|
42
|
+
readonly problem?: Record<string, unknown> | undefined;
|
|
43
|
+
constructor(status: number, code: string, message: string, param?: string | undefined,
|
|
44
|
+
/** The whole Problem body Relyon answered with (extra fields included). */
|
|
45
|
+
problem?: Record<string, unknown> | undefined);
|
|
46
|
+
/** For the store's builder when Relyon refused the key itself (never shown to shoppers). */
|
|
47
|
+
readonly developer_hint?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Problems meaning this store's Relyon account can't do this in this mode
|
|
51
|
+
* yet. They are final: retrying (or retrying a charge) can't change the
|
|
52
|
+
* answer until Relyon changes the account.
|
|
53
|
+
*/
|
|
54
|
+
export declare const ACCOUNT_NOT_READY_CODES: readonly ["processor_not_configured", "sandbox_test_only", "live_not_available"];
|
|
55
|
+
export type AccountNotReadyCode = typeof ACCOUNT_NOT_READY_CODES[number];
|
|
56
|
+
/**
|
|
57
|
+
* Relyon refused because the store's account isn't ready for this mode
|
|
58
|
+
* (`processor_not_configured`, `sandbox_test_only`, `live_not_available`).
|
|
59
|
+
* Fail closed: never retried, and a payment refused this way is never resent.
|
|
60
|
+
* `message` is the kit's explanation; `relyonMessage` is Relyon's own.
|
|
61
|
+
*/
|
|
62
|
+
export declare class RelyonAccountNotReadyError extends RelyonProblemError {
|
|
63
|
+
readonly relyonMessage: string;
|
|
64
|
+
readonly retryable: false;
|
|
65
|
+
readonly code: AccountNotReadyCode;
|
|
66
|
+
constructor(status: number, code: AccountNotReadyCode, relyonMessage: string, param?: string, problem?: Record<string, unknown>);
|
|
67
|
+
}
|
|
68
|
+
export declare function isAccountNotReadyCode(code: unknown): code is AccountNotReadyCode;
|
|
69
|
+
export type FetchLike = (input: Request, init?: {
|
|
70
|
+
signal?: AbortSignal;
|
|
71
|
+
}) => Promise<Response>;
|
|
72
|
+
export interface RelyonConnection {
|
|
73
|
+
/** Relyon's API origin, optionally with a gateway prefix such as `/api`. */
|
|
74
|
+
baseUrl: string;
|
|
75
|
+
/**
|
|
76
|
+
* The store's secret key: a Test key (`rly_test_…`), or a Live key
|
|
77
|
+
* (`rly_live_…`) together with `live: true`.
|
|
78
|
+
*/
|
|
79
|
+
apiKey?: string | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Test-only build mode without a key. Since 0.1.7 the kit also waits when
|
|
82
|
+
* `apiKey` is missing, empty or a placeholder (never in Live); this flag is
|
|
83
|
+
* kept for compatibility and changes nothing.
|
|
84
|
+
*/
|
|
85
|
+
waitingForSandboxKey?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Explicit Live opt-in. Required with a Live key and refused with any other
|
|
88
|
+
* key: the mode never comes from the key prefix alone. Default false (Test).
|
|
89
|
+
*/
|
|
90
|
+
live?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* The project space's public hostname, sent as `Relyon-Project-Space-Hostname`
|
|
93
|
+
* on every Relyon call that doesn't already name its project space, so an
|
|
94
|
+
* unbound workspace key writes and reads this space's own catalog. The kit
|
|
95
|
+
* sets it from the public URL; setting it directly is rarely needed.
|
|
96
|
+
*/
|
|
97
|
+
storeHostname?: string;
|
|
98
|
+
/**
|
|
99
|
+
* The project space's ID (`prj_…`), sent as `Relyon-Project-Space` on every
|
|
100
|
+
* Relyon call that doesn't already name its project space. When left out,
|
|
101
|
+
* RELYON_PROJECT_SPACE is used; an empty string sends none. Optional in a
|
|
102
|
+
* workspace with one project space.
|
|
103
|
+
*/
|
|
104
|
+
projectSpace?: string | undefined;
|
|
105
|
+
/** Pinned `Relyon-Version`. Defaults to the SDK's. */
|
|
106
|
+
version?: string;
|
|
107
|
+
/** Timeout for every call to Relyon, in milliseconds. Default 10000. */
|
|
108
|
+
timeoutMs?: number;
|
|
109
|
+
/** Replaces the global fetch, for tests and custom transports. */
|
|
110
|
+
fetch?: FetchLike;
|
|
111
|
+
}
|
|
112
|
+
export interface Answer<T> {
|
|
113
|
+
data: T;
|
|
114
|
+
response: Response;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The SDK client with a timeout on every call and one strict rule: anything
|
|
118
|
+
* but a well-formed 2xx answer or a well-formed Problem is "unavailable".
|
|
119
|
+
*/
|
|
120
|
+
export declare class RelyonGateway {
|
|
121
|
+
private readonly configuredClient;
|
|
122
|
+
readonly waitingForSandboxKey: boolean;
|
|
123
|
+
get client(): RelyonClient;
|
|
124
|
+
readonly baseUrl: string;
|
|
125
|
+
/** The timeout for every call to Relyon, in milliseconds. */
|
|
126
|
+
readonly timeoutMs: number;
|
|
127
|
+
/** True only when Live was switched on explicitly (`live: true`) with a Live key. */
|
|
128
|
+
readonly livemode: boolean;
|
|
129
|
+
constructor(given: RelyonConnection);
|
|
130
|
+
/**
|
|
131
|
+
* Runs one SDK call. `valid` checks the 2xx body against the contract shape
|
|
132
|
+
* the kit relies on; a body that fails it is treated as Relyon being down.
|
|
133
|
+
*/
|
|
134
|
+
call<T>(run: (client: RelyonClient) => Promise<{
|
|
135
|
+
data?: unknown;
|
|
136
|
+
error?: unknown;
|
|
137
|
+
response: Response;
|
|
138
|
+
}>, valid: (data: unknown) => data is T): Promise<Answer<T>>;
|
|
139
|
+
}
|
|
140
|
+
/** The script URL for `<relyon-card-fields>`, served by Relyon next to its API. */
|
|
141
|
+
export declare function cardFieldsScriptUrl(baseUrl: string): string;
|