@substrat-run/contracts 0.137.0 → 0.138.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.
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Request rate limiting on the request path (#130).
3
+ *
4
+ * A tenant must be protected from another tenant, and from its own AI-built frontend with
5
+ * a polling bug. The limit is enforced where a request first meets the platform: the router
6
+ * on the hosted path, `@substrat-run/vertical-host`'s `rateLimit()` middleware on a node
7
+ * host with no router in front. Never in a scope host and never in the kernel: a limiter in
8
+ * a scope DO serialises behind that scope's write queue, and cannot see one token's
9
+ * traffic across scopes.
10
+ *
11
+ * ## Why the policy lives here
12
+ *
13
+ * Both of those choke points derive the same keys, speak the same headers and refuse with
14
+ * the same problem — through one call, {@link evaluateRateLimits}. This file is that grammar
15
+ * and nothing else: which counter remembers a key is the caller's ({@link RateLimiter}), so
16
+ * the router binds Cloudflare's native rate-limit binding and a node host binds
17
+ * vertical-host's `memoryRateLimiter`, and the two cannot disagree about what a key or a 429
18
+ * looks like.
19
+ *
20
+ * ## The keys
21
+ *
22
+ * Every key begins `<tenant>:<scope>:<bucket>:`, so one tenant's traffic can never spend
23
+ * another tenant's budget. That is a property of the key, not of a configuration.
24
+ *
25
+ * - **credential**: one budget per caller. A bearer token when the request carries one, else
26
+ * the session cookie a signed-in browser carries, else the client address. A credential is
27
+ * only ever represented by a truncated SHA-256 digest: the raw token or cookie is never
28
+ * part of a key, so it reaches no counter and no log line.
29
+ * - **ip**: one budget per client address, set well above the credential budget. The router
30
+ * cannot verify a bearer, so without it a caller minting a fresh fake token per request
31
+ * would get a fresh credential budget per request too.
32
+ *
33
+ * ## The fail mode
34
+ *
35
+ * Open. A limiter that throws lets the request through, and the caller logs it. The limiter
36
+ * protects capacity; it is not an authorization boundary — every request still meets the
37
+ * vertical's authentication and permission checks — and failing closed would turn an outage
38
+ * of the counter into an outage of every tenant behind it.
39
+ */
40
+ import { type Problem } from './errors.js';
41
+ /** RFC 9110 §10.2.3: seconds a client should wait before trying again. */
42
+ export declare const RETRY_AFTER_HEADER = "Retry-After";
43
+ /** The policies a response was judged against (draft-ietf-httpapi-ratelimit-headers). */
44
+ export declare const RATE_LIMIT_POLICY_HEADER = "RateLimit-Policy";
45
+ /** The state of the policy that refused (same draft): nothing remaining, and when it resets. */
46
+ export declare const RATE_LIMIT_HEADER = "RateLimit";
47
+ /**
48
+ * Exposed to a cross-origin browser client — the same trap `IDEMPOTENCY_EXPOSED_HEADERS`
49
+ * documents. An unexposed `Retry-After` is the one that matters: a frontend that cannot
50
+ * read it cannot back off by it.
51
+ */
52
+ export declare const RATE_LIMIT_EXPOSED_HEADERS: readonly ["Retry-After", "RateLimit", "RateLimit-Policy"];
53
+ /**
54
+ * The counter seam. Exactly the shape of Cloudflare's rate-limit binding, so the binding IS
55
+ * a `RateLimiter` and needs no wrapper. `success: false` means the key is over its limit.
56
+ */
57
+ export interface RateLimiter {
58
+ limit(options: {
59
+ key: string;
60
+ }): Promise<{
61
+ success: boolean;
62
+ }>;
63
+ }
64
+ /** The two buckets every request is counted in. */
65
+ export declare const RATE_LIMIT_BUCKETS: readonly ['credential', 'ip'];
66
+ export type RateLimitBucket = (typeof RATE_LIMIT_BUCKETS)[number];
67
+ /** One bucket's budget: `limit` requests per `period` seconds. */
68
+ export interface RateLimitPolicy {
69
+ readonly limit: number;
70
+ readonly period: number;
71
+ }
72
+ /**
73
+ * The defaults: conservative, so a normal client never meets them. 1200 a minute per
74
+ * credential is twenty requests a second sustained; the address budget is five times that,
75
+ * so an office behind one NAT, every person on their own session, stays well clear of it.
76
+ * A deployment sets its own (the router's `wrangler.jsonc`, a node host's options).
77
+ */
78
+ export declare const DEFAULT_RATE_LIMITS: Readonly<Record<RateLimitBucket, RateLimitPolicy>>;
79
+ /**
80
+ * Read a deployment's configured limits, or `undefined` when they are not a complete,
81
+ * well-formed set. Strict on purpose: a typo in a limit must be loud (the caller logs and
82
+ * fails open), never a silently different budget.
83
+ */
84
+ export declare function parseRateLimits(value: unknown): Record<RateLimitBucket, RateLimitPolicy> | undefined;
85
+ /**
86
+ * The cookies that ARE a signed-in browser's credential: the platform session (oidc-rp's
87
+ * `sb_session`), a capability session (`sb_capability`, one cookie per link since #1686), and
88
+ * Better Auth's session token on an instance using that provider. Any other cookie — a
89
+ * preference, an analytics id — says nothing about who is calling and is ignored.
90
+ */
91
+ export declare function isSessionCookie(name: string): boolean;
92
+ /** What a request offers to be counted by. Header values are read, never kept. */
93
+ export interface RateLimitSubject {
94
+ readonly tenantId: string;
95
+ readonly scopeId: string;
96
+ readonly headers: Headers;
97
+ /** The client address, when the host knows one. Absent ⇒ one shared anonymous budget. */
98
+ readonly clientIp?: string | null | undefined;
99
+ /** The request method. An `OPTIONS` preflight is never counted (see {@link evaluateRateLimits}). */
100
+ readonly method?: string;
101
+ }
102
+ /**
103
+ * The key one request is counted under in one bucket. Async because a credential is hashed
104
+ * (Web Crypto) before it goes anywhere — and only when the credential bucket is counted.
105
+ */
106
+ export declare function rateLimitKey(bucket: RateLimitBucket, subject: RateLimitSubject): Promise<string>;
107
+ /** A 429, as the parts a host's own `Response` is built from. */
108
+ export interface RateLimitedRefusal {
109
+ readonly status: 429;
110
+ readonly headers: Readonly<Record<string, string>>;
111
+ readonly body: Problem;
112
+ }
113
+ /** What one request's count decided, for the host to answer and log. */
114
+ export interface RateLimitEvaluation {
115
+ /** The 429 to answer instead of serving the request, when a bucket refused it. */
116
+ readonly refusal?: RateLimitedRefusal;
117
+ /** `RateLimit-Policy` for the response the request goes on to get; null when nothing counted it. */
118
+ readonly policyHeader: string | null;
119
+ /**
120
+ * Set when a counter threw and the request was let through anyway (fail open): the fields of
121
+ * the host's `rate-limit-unavailable` log line. Never a key — even a digest has no business
122
+ * in a log line.
123
+ */
124
+ readonly unavailable?: {
125
+ readonly reason: string;
126
+ readonly buckets: RateLimitBucket[];
127
+ readonly error: string[];
128
+ };
129
+ }
130
+ /**
131
+ * Count one request in the buckets that have a counter — the whole decision both hosts make.
132
+ *
133
+ * The ADDRESS is counted first, and a request it refuses is never counted against a
134
+ * credential. A credential key is minted from whatever the request sends, so a caller
135
+ * rotating made-up tokens from one address would otherwise create a fresh credential counter
136
+ * with every request it was already refused for — unbounded state in a counter that keeps
137
+ * its keys (the node host's), and spent budget on keys nobody holds. Once the address is
138
+ * over, the credential bucket is not asked.
139
+ */
140
+ export declare function evaluateRateLimits(input: {
141
+ readonly subject: RateLimitSubject;
142
+ readonly policies: Readonly<Record<RateLimitBucket, RateLimitPolicy>>;
143
+ readonly limiters: Readonly<Partial<Record<RateLimitBucket, RateLimiter>>>;
144
+ /** The request path, for the problem's `instance`. */
145
+ readonly instance?: string;
146
+ }): Promise<RateLimitEvaluation>;
147
+ //# sourceMappingURL=rate-limit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,OAAO,EAAkD,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3F,0EAA0E;AAC1E,eAAO,MAAM,kBAAkB,gBAAgB,CAAC;AAChD,yFAAyF;AACzF,eAAO,MAAM,wBAAwB,qBAAqB,CAAC;AAC3D,gGAAgG;AAChG,eAAO,MAAM,iBAAiB,cAAc,CAAC;AAE7C;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,2DAA6E,CAAC;AAErH;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,KAAK,CAAC,OAAO,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CAChE;AAED,mDAAmD;AACnD,eAAO,MAAM,kBAAkB,YAAI,YAAY,EAAE,IAAI,CAAU,CAAC;AAChE,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAA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AkB,CAAC;IACtC,oGAAoG;IACpG,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE;QAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,OAAO,EAAE,eAAe,EAAE,CAAC;QAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;CACnH;AAED;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CAAC,KAAK,EAAE;IAC9C,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC;IACnC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC,CAAC;IACtE,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,eAAe,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAC3E,sDAAsD;IACtD,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B,GAAG,OAAO,CAAC,mBAAmB,CAAC,CA4C/B"}
@@ -0,0 +1,248 @@
1
+ /**
2
+ * Request rate limiting on the request path (#130).
3
+ *
4
+ * A tenant must be protected from another tenant, and from its own AI-built frontend with
5
+ * a polling bug. The limit is enforced where a request first meets the platform: the router
6
+ * on the hosted path, `@substrat-run/vertical-host`'s `rateLimit()` middleware on a node
7
+ * host with no router in front. Never in a scope host and never in the kernel: a limiter in
8
+ * a scope DO serialises behind that scope's write queue, and cannot see one token's
9
+ * traffic across scopes.
10
+ *
11
+ * ## Why the policy lives here
12
+ *
13
+ * Both of those choke points derive the same keys, speak the same headers and refuse with
14
+ * the same problem — through one call, {@link evaluateRateLimits}. This file is that grammar
15
+ * and nothing else: which counter remembers a key is the caller's ({@link RateLimiter}), so
16
+ * the router binds Cloudflare's native rate-limit binding and a node host binds
17
+ * vertical-host's `memoryRateLimiter`, and the two cannot disagree about what a key or a 429
18
+ * looks like.
19
+ *
20
+ * ## The keys
21
+ *
22
+ * Every key begins `<tenant>:<scope>:<bucket>:`, so one tenant's traffic can never spend
23
+ * another tenant's budget. That is a property of the key, not of a configuration.
24
+ *
25
+ * - **credential**: one budget per caller. A bearer token when the request carries one, else
26
+ * the session cookie a signed-in browser carries, else the client address. A credential is
27
+ * only ever represented by a truncated SHA-256 digest: the raw token or cookie is never
28
+ * part of a key, so it reaches no counter and no log line.
29
+ * - **ip**: one budget per client address, set well above the credential budget. The router
30
+ * cannot verify a bearer, so without it a caller minting a fresh fake token per request
31
+ * would get a fresh credential budget per request too.
32
+ *
33
+ * ## The fail mode
34
+ *
35
+ * Open. A limiter that throws lets the request through, and the caller logs it. The limiter
36
+ * protects capacity; it is not an authorization boundary — every request still meets the
37
+ * vertical's authentication and permission checks — and failing closed would turn an outage
38
+ * of the counter into an outage of every tenant behind it.
39
+ */
40
+ import { substratError, toProblem, PROBLEM_CONTENT_TYPE } from './errors.js';
41
+ /** RFC 9110 §10.2.3: seconds a client should wait before trying again. */
42
+ export const RETRY_AFTER_HEADER = 'Retry-After';
43
+ /** The policies a response was judged against (draft-ietf-httpapi-ratelimit-headers). */
44
+ export const RATE_LIMIT_POLICY_HEADER = 'RateLimit-Policy';
45
+ /** The state of the policy that refused (same draft): nothing remaining, and when it resets. */
46
+ export const RATE_LIMIT_HEADER = 'RateLimit';
47
+ /**
48
+ * Exposed to a cross-origin browser client — the same trap `IDEMPOTENCY_EXPOSED_HEADERS`
49
+ * documents. An unexposed `Retry-After` is the one that matters: a frontend that cannot
50
+ * read it cannot back off by it.
51
+ */
52
+ export const RATE_LIMIT_EXPOSED_HEADERS = [RETRY_AFTER_HEADER, RATE_LIMIT_HEADER, RATE_LIMIT_POLICY_HEADER];
53
+ /** The two buckets every request is counted in. */
54
+ export const RATE_LIMIT_BUCKETS = ['credential', 'ip'];
55
+ /**
56
+ * The defaults: conservative, so a normal client never meets them. 1200 a minute per
57
+ * credential is twenty requests a second sustained; the address budget is five times that,
58
+ * so an office behind one NAT, every person on their own session, stays well clear of it.
59
+ * A deployment sets its own (the router's `wrangler.jsonc`, a node host's options).
60
+ */
61
+ export const DEFAULT_RATE_LIMITS = {
62
+ credential: { limit: 1200, period: 60 },
63
+ ip: { limit: 6000, period: 60 },
64
+ };
65
+ /**
66
+ * Read a deployment's configured limits, or `undefined` when they are not a complete,
67
+ * well-formed set. Strict on purpose: a typo in a limit must be loud (the caller logs and
68
+ * fails open), never a silently different budget.
69
+ */
70
+ export function parseRateLimits(value) {
71
+ if (typeof value !== 'object' || value === null)
72
+ return undefined;
73
+ const out = {};
74
+ for (const bucket of RATE_LIMIT_BUCKETS) {
75
+ const policy = value[bucket];
76
+ if (!isPositiveInt(policy?.limit) || !isPositiveInt(policy?.period))
77
+ return undefined;
78
+ out[bucket] = { limit: policy.limit, period: policy.period };
79
+ }
80
+ return out;
81
+ }
82
+ const isPositiveInt = (n) => Number.isInteger(n) && n > 0;
83
+ /**
84
+ * The cookies that ARE a signed-in browser's credential: the platform session (oidc-rp's
85
+ * `sb_session`), a capability session (`sb_capability`, one cookie per link since #1686), and
86
+ * Better Auth's session token on an instance using that provider. Any other cookie — a
87
+ * preference, an analytics id — says nothing about who is calling and is ignored.
88
+ */
89
+ export function isSessionCookie(name) {
90
+ return (name === 'sb_session' ||
91
+ name.startsWith('sb_capability') ||
92
+ name === 'better-auth.session_token' ||
93
+ name === '__Secure-better-auth.session_token');
94
+ }
95
+ /**
96
+ * The key one request is counted under in one bucket. Async because a credential is hashed
97
+ * (Web Crypto) before it goes anywhere — and only when the credential bucket is counted.
98
+ */
99
+ export async function rateLimitKey(bucket, subject) {
100
+ const address = subject.clientIp ? `ip:${subject.clientIp}` : 'anon';
101
+ const caller = bucket === 'credential' ? ((await credentialOf(subject.headers)) ?? address) : address;
102
+ return `${subject.tenantId}:${subject.scopeId}:${bucket}:${caller}`;
103
+ }
104
+ /**
105
+ * The caller's credential as a digest: `b:` for a bearer, `s:` for a session cookie set.
106
+ * Every session cookie the request carries is hashed together, sorted, so a browser holding
107
+ * a stale host-only `sb_session` beside its domain one is still one caller.
108
+ */
109
+ async function credentialOf(headers) {
110
+ const authorization = headers.get('authorization');
111
+ if (authorization)
112
+ return `b:${await digest(authorization)}`;
113
+ const session = sessionCookiesOf(headers.get('cookie'));
114
+ if (session.length > 0)
115
+ return `s:${await digest(session.join('\n'))}`;
116
+ return undefined;
117
+ }
118
+ function sessionCookiesOf(header) {
119
+ if (!header)
120
+ return [];
121
+ const out = [];
122
+ for (const part of header.split(';')) {
123
+ const eq = part.indexOf('=');
124
+ if (eq === -1)
125
+ continue;
126
+ const name = part.slice(0, eq).trim();
127
+ if (isSessionCookie(name))
128
+ out.push(`${name}=${part.slice(eq + 1).trim()}`);
129
+ }
130
+ return out.sort();
131
+ }
132
+ /** 128 bits of SHA-256, hex: unguessable, and short enough to be a counter key. */
133
+ async function digest(text) {
134
+ const bytes = new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)));
135
+ let hex = '';
136
+ for (const b of bytes.subarray(0, 16))
137
+ hex += b.toString(16).padStart(2, '0');
138
+ return hex;
139
+ }
140
+ /** The order buckets are counted in: the address first (see {@link evaluateRateLimits}). */
141
+ const COUNTING_ORDER = ['ip', 'credential'];
142
+ /**
143
+ * Count one request in the buckets that have a counter — the whole decision both hosts make.
144
+ *
145
+ * The ADDRESS is counted first, and a request it refuses is never counted against a
146
+ * credential. A credential key is minted from whatever the request sends, so a caller
147
+ * rotating made-up tokens from one address would otherwise create a fresh credential counter
148
+ * with every request it was already refused for — unbounded state in a counter that keeps
149
+ * its keys (the node host's), and spent budget on keys nobody holds. Once the address is
150
+ * over, the credential bucket is not asked.
151
+ */
152
+ export async function evaluateRateLimits(input) {
153
+ // A CORS preflight is never counted, so never refused: a refused preflight is opaque to the
154
+ // page whatever headers it carries, so the page could not read the 429 its real request
155
+ // would have met. It reaches the vertical's own preflight handler instead.
156
+ if (input.subject.method === 'OPTIONS')
157
+ return { policyHeader: null };
158
+ const buckets = RATE_LIMIT_BUCKETS.filter((bucket) => input.limiters[bucket]);
159
+ if (buckets.length === 0)
160
+ return { policyHeader: null };
161
+ const results = [];
162
+ for (const bucket of COUNTING_ORDER) {
163
+ const limiter = input.limiters[bucket];
164
+ if (!limiter)
165
+ continue;
166
+ try {
167
+ const key = await rateLimitKey(bucket, input.subject);
168
+ results.push({ bucket, success: (await limiter.limit({ key })).success });
169
+ }
170
+ catch (error) {
171
+ results.push({ bucket, success: true, error: error instanceof Error ? error.message : String(error) });
172
+ }
173
+ if (!results.at(-1).success)
174
+ break;
175
+ }
176
+ const policyHeader = buckets
177
+ .map((bucket) => `"${bucket}";q=${input.policies[bucket].limit};w=${input.policies[bucket].period}`)
178
+ .join(', ');
179
+ const failed = results.filter((r) => r.error !== undefined);
180
+ const unavailable = failed.length > 0
181
+ ? {
182
+ reason: 'the rate limiter threw; the request was let through',
183
+ buckets: failed.map((r) => r.bucket),
184
+ error: failed.map((r) => r.error),
185
+ }
186
+ : undefined;
187
+ const refused = results.find((r) => !r.success)?.bucket;
188
+ return {
189
+ policyHeader,
190
+ ...(unavailable ? { unavailable } : {}),
191
+ ...(refused
192
+ ? {
193
+ refusal: refusalFor(refused, input.policies[refused], policyHeader, {
194
+ instance: input.instance,
195
+ origin: input.subject.headers.get('origin'),
196
+ }),
197
+ }
198
+ : {}),
199
+ };
200
+ }
201
+ /**
202
+ * The refusal: 429 `application/problem+json` with code `rate_limited`, and the headers a
203
+ * client backs off by. Parts rather than a `Response`, as the rest of this package hands out
204
+ * data: each host builds its own.
205
+ *
206
+ * `Retry-After` is the bucket's whole period. A counter that cannot say where in its window
207
+ * a key sits (Cloudflare's binding cannot) makes the period the only bound that is never
208
+ * too short, and a retry that is too early is just another 429.
209
+ */
210
+ function refusalFor(bucket, policy, policyHeader, { instance, origin }) {
211
+ const retryAfter = policy.period;
212
+ const body = toProblem(substratError('rate_limited', `Too many requests for this ${bucket === 'ip' ? 'client address' : 'credential'}: ` +
213
+ `at most ${policy.limit} every ${policy.period} seconds. Retry after ${retryAfter} seconds.`, { retryAfter }), instance);
214
+ return {
215
+ status: 429,
216
+ headers: {
217
+ 'content-type': PROBLEM_CONTENT_TYPE,
218
+ [RETRY_AFTER_HEADER]: String(retryAfter),
219
+ [RATE_LIMIT_POLICY_HEADER]: policyHeader,
220
+ [RATE_LIMIT_HEADER]: `"${bucket}";r=0;t=${retryAfter}`,
221
+ ...(origin ? refusalCors(origin) : {}),
222
+ },
223
+ body,
224
+ };
225
+ }
226
+ /**
227
+ * CORS for the refusal, and only for it.
228
+ *
229
+ * A refusal is answered before the vertical runs, so the vertical's own CORS policy never
230
+ * sees it. Without these headers a cross-origin page reads a network error rather than a
231
+ * 429, and cannot back off by `Retry-After` — the polling loop this exists to stop keeps
232
+ * polling. The route names no origin allowlist to follow, so the refusal reflects whichever
233
+ * origin asked, credentials included.
234
+ *
235
+ * That is safe for THIS response and would not be for any other: the body and headers are
236
+ * the requester's own rate state — the budget that refused and when to retry — and no vertical
237
+ * data, which is why the policy is built here and never applied to a response the vertical
238
+ * gave.
239
+ */
240
+ function refusalCors(origin) {
241
+ return {
242
+ 'access-control-allow-origin': origin,
243
+ 'access-control-allow-credentials': 'true',
244
+ 'access-control-expose-headers': RATE_LIMIT_EXPOSED_HEADERS.join(', '),
245
+ vary: 'Origin',
246
+ };
247
+ }
248
+ //# sourceMappingURL=rate-limit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,oBAAoB,EAAgB,MAAM,aAAa,CAAC;AAE3F,0EAA0E;AAC1E,MAAM,CAAC,MAAM,kBAAkB,GAAG,aAAa,CAAC;AAChD,yFAAyF;AACzF,MAAM,CAAC,MAAM,wBAAwB,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,GAAG,MAAM,CAAC,MAAM,CAAC;IACjC,MAAM,IAAI,GAAG,SAAS,CACpB,aAAa,CACX,cAAc,EACd,8BAA8B,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,YAAY,IAAI;QACjF,WAAW,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,MAAM,yBAAyB,UAAU,WAAW,EAC9F,EAAE,UAAU,EAAE,CACf,EACD,QAAQ,CACT,CAAC;IACF,OAAO;QACL,MAAM,EAAE,GAAG;QACX,OAAO,EAAE;YACP,cAAc,EAAE,oBAAoB;YACpC,CAAC,kBAAkB,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;YACxC,CAAC,wBAAwB,CAAC,EAAE,YAAY;YACxC,CAAC,iBAAiB,CAAC,EAAE,IAAI,MAAM,WAAW,UAAU,EAAE;YACtD,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;SACvC;QACD,IAAI;KACL,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAS,WAAW,CAAC,MAAc;IACjC,OAAO;QACL,6BAA6B,EAAE,MAAM;QACrC,kCAAkC,EAAE,MAAM;QAC1C,+BAA+B,EAAE,0BAA0B,CAAC,IAAI,CAAC,IAAI,CAAC;QACtE,IAAI,EAAE,QAAQ;KACf,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@substrat-run/contracts",
3
- "version": "0.137.0",
3
+ "version": "0.138.0",
4
4
  "description": "Substrat kernel contract schemas — Zod is the source of truth (master plan D-22); OAS/JSON Schema are emitted artifacts",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {