@arbol-ai/maximus 7.4.0 → 8.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/README.md +36 -38
- package/bin/maximus.js +4 -0
- package/config/oxfmt.mjs +1 -0
- package/config/oxlint.mjs +10 -0
- package/dist/analytics/analytics.d.ts +21 -9
- package/dist/analytics/analytics.js +19 -5
- package/dist/body.d.ts +1 -1
- package/dist/body.js +2 -2
- package/dist/chunks/agent-skills-Ef65jTf_.js +49 -0
- package/dist/chunks/agent-skills-m7y1EUch.js +3 -0
- package/dist/chunks/{body-6LichrQm.js → body-DOIHwIIl.js} +14 -7
- package/dist/chunks/{correlation-BEkcFFGT.js → correlation-4b23a-a4.js} +8 -5
- package/dist/chunks/define-app-CfVZBt7D.js +336 -0
- package/dist/chunks/{errors-D7zG9kx9.js → errors-WfyKhiyk.js} +9 -4
- package/dist/chunks/{execution-kVUJPy1A.js → execution-CfZEx_JP.js} +28 -15
- package/dist/chunks/generate-C-9FKW0o.js +197 -0
- package/dist/chunks/handler-DWTGoduQ.js +140 -0
- package/dist/chunks/http-layout-_h7xwgPC.js +128 -0
- package/dist/chunks/{http-paths-yKD6TSNJ.js → http-paths-DMBxwpp6.js} +50 -31
- package/dist/chunks/{json-Bu4BLT25.js → json-B14evE6-.js} +21 -5
- package/dist/chunks/names-ncD7A3DV.js +280 -0
- package/dist/chunks/openapi-CV0KmJpS.js +1588 -0
- package/dist/chunks/openapi-file-OjSs9mRv.js +83 -0
- package/dist/chunks/pages-DyC0m8Zb.js +14 -0
- package/dist/chunks/prepare-configured-BmRJLg6J.js +3 -0
- package/dist/chunks/prepare-configured-Cithjf80.js +52 -0
- package/dist/chunks/project-files-C0OX7MQQ.js +130 -0
- package/dist/chunks/query-kind-CWKAXEME.js +40 -0
- package/dist/chunks/record-DQUDTpEA.js +10 -0
- package/dist/chunks/result-CKgZHrnj.js +56 -0
- package/dist/chunks/{runtime-DlqA7J38.js → runtime-CVVf5hmp.js} +50 -26
- package/dist/chunks/scaffold-DGrcjR3h.js +3 -0
- package/dist/chunks/scaffold-DZD6dfoZ.js +138 -0
- package/dist/chunks/{schemas-CzTvbXb6.js → schemas-p_aRcMC1.js} +30 -14
- package/dist/chunks/server-CCc7fil4.js +410 -0
- package/dist/chunks/src--HZrH1U9.js +568 -0
- package/dist/chunks/{telemetry-BTwB68m5.js → telemetry-C3LfZcOB.js} +32 -11
- package/dist/chunks/{types-DQdfRF3f.d.ts → types-BQYB4Ads.d.ts} +993 -341
- package/dist/chunks/{validation-r-sgmsqb.js → validation-fTohBT6R.js} +69 -28
- package/dist/chunks/workers-types-iONp32T4.d.ts +143 -0
- package/dist/chunks/yaml-Dk2L24yQ.js +74 -0
- package/dist/diagnostics.d.ts +6 -4
- package/dist/diagnostics.js +2 -2
- package/dist/errors.d.ts +2 -2
- package/dist/errors.js +2 -2
- package/dist/http-paths.d.ts +1 -1
- package/dist/http-paths.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +12 -11
- package/dist/internals.d.ts +1 -1
- package/dist/internals.js +5 -4
- package/dist/mcp/index.d.ts +3 -2
- package/dist/mcp/index.js +5 -387
- package/dist/postgres/index.d.ts +25 -8
- package/dist/postgres/index.js +17 -8
- package/dist/project/config.d.ts +110 -72
- package/dist/project/config.js +161 -45
- package/dist/project/index.d.ts +17 -4
- package/dist/project/index.js +44 -9
- package/dist/projection.d.ts +14 -13
- package/dist/projection.js +8 -32
- package/dist/schemas.d.ts +1 -1
- package/dist/schemas.js +2 -2
- package/dist/standard-failures.d.ts +2 -89
- package/dist/tooling/cli.js +149 -105
- package/dist/tooling/testing.js +2 -2
- package/dist/vite/cloudflare-workers.js +25 -0
- package/dist/vite/index.d.ts +157 -0
- package/dist/vite/index.js +1477 -0
- package/dist/webhooks.d.ts +12 -10
- package/dist/webhooks.js +26 -12
- package/dist/workers/runtime.d.ts +1 -1
- package/dist/workers/runtime.js +1 -1
- package/package.json +26 -22
- package/skills/maximus/SKILL.md +165 -68
- package/skills/maximus/references/deploying.md +73 -0
- package/skills/maximus/references/errors-and-results.md +114 -55
- package/skills/maximus/references/project.md +202 -0
- package/skills/maximus/references/routes-and-tools.md +256 -0
- package/skills/maximus/references/testing.md +137 -50
- package/skills/maximus/references/transports.md +113 -100
- package/skills/maximus/references/writing-code.md +84 -0
- package/templates/mcp/mcp/instructions.md.template +2 -0
- package/templates/mcp/routes/v1/greeting.get.ts.template +12 -0
- package/templates/mcp/tests/mcp.test.ts.template +36 -0
- package/templates/project/.agents/skills/project/SKILL.md.template +21 -12
- package/templates/project/.github/workflows/ci.yml.template +4 -1
- package/templates/project/CLAUDE.md.template +41 -25
- package/templates/project/README.md.template +54 -48
- package/templates/project/middleware/10-response.ts.template +5 -2
- package/templates/project/package.json.template +13 -9
- package/templates/project/pages/index.ts.template +2 -4
- package/templates/project/routes/v1/greeting.get.ts.template +10 -0
- package/templates/project/routes/v1/greeting.post.ts.template +12 -0
- package/templates/project/tests/greeting.test.ts.template +45 -0
- package/templates/project/tsconfig.json.template +8 -6
- package/templates/project/vite.config.ts.template +15 -7
- package/templates/web/app/main.tsx.template +16 -0
- package/templates/web/app/root.tsx.template +17 -0
- package/templates/web/app/routes/index.tsx.template +9 -0
- package/templates/web/index.html.template +12 -0
- package/templates/web/package.json.template +12 -0
- package/templates/web/tsconfig.json.template +7 -0
- package/templates/web/vite.config.ts.template +22 -0
- package/dist/chunks/builder-BiXv7d2v.js +0 -100
- package/dist/chunks/define-app-DvD_1jUE.js +0 -625
- package/dist/chunks/prepare-project-CMyGSEtI.js +0 -236
- package/dist/chunks/prepare-project-MtS0-W_n.js +0 -3
- package/dist/chunks/record-DY-lX9w1.js +0 -34
- package/dist/chunks/src-N3oyx5QF.js +0 -1091
- package/dist/client/client.d.ts +0 -129
- package/dist/client/client.js +0 -321
- package/dist/tooling/vite.d.ts +0 -3
- package/dist/tooling/vite.js +0 -57
- package/skills/maximus/references/adding-an-operation.md +0 -179
- package/skills/maximus/references/building-a-service.md +0 -167
- package/skills/maximus/references/migration-from-v2.md +0 -62
- package/skills/maximus/references/migration-to-v5.md +0 -37
- package/skills/maximus/references/publishing-and-verification.md +0 -42
- package/templates/project/cloudflare.config.ts.template +0 -24
- package/templates/project/maximus.config.ts.template +0 -20
- package/templates/project/routes/v1/greeting.ts.template +0 -38
- package/templates/service/.agents/skills/project/SKILL.md.template +0 -10
- package/templates/service/.github/workflows/ci.yml.template +0 -21
- package/templates/service/.gitignore.template +0 -9
- package/templates/service/CLAUDE.md.template +0 -5
- package/templates/service/README.md.template +0 -45
- package/templates/service/cloudflare.config.ts.template +0 -30
- package/templates/service/package.json.template +0 -36
- package/templates/service/src/app.ts.template +0 -30
- package/templates/service/src/index.ts.template +0 -13
- package/templates/service/tests/contract.test.ts.template +0 -12
- package/templates/service/tsconfig.json.template +0 -16
- package/templates/service/vite.config.ts.template +0 -13
- package/templates/service/vitest.config.ts.template +0 -10
|
@@ -1,625 +0,0 @@
|
|
|
1
|
-
import { t as STANDARD_FAILURES } from "./standard-failures-CLjxS2ea.js";
|
|
2
|
-
import { l as hasUncompiledCodecs, r as parseInput } from "./validation-r-sgmsqb.js";
|
|
3
|
-
import { i as standardErrors } from "./errors-D7zG9kx9.js";
|
|
4
|
-
import { n as readBodyBytes } from "./body-6LichrQm.js";
|
|
5
|
-
import { n as exactLength, t as isRecord } from "./record-DY-lX9w1.js";
|
|
6
|
-
import { a as operationPathParameters, r as isInputHeaderName, t as createHttpRouteResolver } from "./http-paths-yKD6TSNJ.js";
|
|
7
|
-
import { i as UuidSchema, n as OrganizationIdSchema } from "./schemas-CzTvbXb6.js";
|
|
8
|
-
import { Kind, Type } from "@sinclair/typebox";
|
|
9
|
-
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
10
|
-
|
|
11
|
-
// src/binary.ts
|
|
12
|
-
/**
|
|
13
|
-
* Byte bodies and file attachments over HTTP: the on-demand reader of a declared byte body, the checks an
|
|
14
|
-
* attachment output passes before anything is sent, and the exact-length response that serves it.
|
|
15
|
-
*/
|
|
16
|
-
/** A request-local reader; constructing it never pulls bytes or records headers/content. */
|
|
17
|
-
function binaryBody(request, maxBytes) {
|
|
18
|
-
let reading;
|
|
19
|
-
return { read() {
|
|
20
|
-
reading ??= (async () => {
|
|
21
|
-
const encoding = request.headers.get("content-encoding");
|
|
22
|
-
if (encoding && encoding.toLowerCase() !== "identity") return {
|
|
23
|
-
ok: false,
|
|
24
|
-
error: standardErrors.unsupportedMedia.create()
|
|
25
|
-
};
|
|
26
|
-
const result = await readBodyBytes(request, {
|
|
27
|
-
maxBytes,
|
|
28
|
-
signal: request.signal
|
|
29
|
-
});
|
|
30
|
-
return result.ok ? result : {
|
|
31
|
-
ok: false,
|
|
32
|
-
error: result.error.data.reason === "too_large" ? standardErrors.tooLarge.create() : standardErrors.invalidInput.create()
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
return reading;
|
|
36
|
-
} };
|
|
37
|
-
}
|
|
38
|
-
/** Whether an output schema is `AttachmentOutputSchema`. */
|
|
39
|
-
function isAttachmentSchema(schema) {
|
|
40
|
-
return schema[Kind] === "HttpAttachment";
|
|
41
|
-
}
|
|
42
|
-
/** Reject malformed output before exposing a body, filename or media type to HTTP. */
|
|
43
|
-
function validAttachment(value, maxBytes) {
|
|
44
|
-
if (!value || typeof value !== "object") return false;
|
|
45
|
-
const keys = Object.keys(value);
|
|
46
|
-
if (keys.length !== 4 || keys.some((key) => ![
|
|
47
|
-
"body",
|
|
48
|
-
"contentType",
|
|
49
|
-
"filename",
|
|
50
|
-
"byteLength"
|
|
51
|
-
].includes(key))) return false;
|
|
52
|
-
const file = value;
|
|
53
|
-
if (!(file.body instanceof Uint8Array || file.body instanceof ReadableStream)) return false;
|
|
54
|
-
if (file.body instanceof ReadableStream && file.body.locked) return false;
|
|
55
|
-
if (!Number.isSafeInteger(file.byteLength) || file.byteLength < 0 || file.byteLength > maxBytes) return false;
|
|
56
|
-
if (file.body instanceof Uint8Array && file.body.byteLength !== file.byteLength) return false;
|
|
57
|
-
if (typeof file.contentType !== "string" || file.contentType.length > 255 || !/^[A-Za-z0-9!#$&^_.+-]+\/[A-Za-z0-9!#$&^_.+-]+$/u.test(file.contentType)) return false;
|
|
58
|
-
if (typeof file.filename !== "string" || !file.filename.length || file.filename.length > 512 || /[\p{Cc}/\\]/u.test(file.filename)) return false;
|
|
59
|
-
try {
|
|
60
|
-
encodeURIComponent(file.filename);
|
|
61
|
-
return true;
|
|
62
|
-
} catch {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
/** An exact-length stream prevents a handler from sending bytes beyond its validated attachment metadata. */
|
|
67
|
-
function attachmentResponse(file, status = 200) {
|
|
68
|
-
const encodedFilename = encodeURIComponent(file.filename).replaceAll(/[!'()*]/gu, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
69
|
-
const body = file.body instanceof Uint8Array ? file.body : exactLength(file.body, file.byteLength);
|
|
70
|
-
return new Response(body, {
|
|
71
|
-
status,
|
|
72
|
-
headers: {
|
|
73
|
-
"cache-control": "private, no-store",
|
|
74
|
-
"content-type": file.contentType,
|
|
75
|
-
"content-length": String(file.byteLength),
|
|
76
|
-
"content-disposition": `attachment; filename="${file.filename.replaceAll(/[^\u0020-\u007E]|["\\]/gu, "_")}"; filename*=UTF-8''${encodedFilename}`,
|
|
77
|
-
"content-security-policy": "default-src 'none'; sandbox",
|
|
78
|
-
"x-content-type-options": "nosniff"
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// src/internal/http-routes.ts
|
|
84
|
-
/** Every body variant must declare its path authority as a required string field. */
|
|
85
|
-
function pathParameterSchemas(input, parameter) {
|
|
86
|
-
if (input.type === "object") {
|
|
87
|
-
const schema = input.properties?.[parameter];
|
|
88
|
-
if (!schema || schema.type !== "string" || !input.required?.includes(parameter)) throw new TypeError(`Path parameter "${parameter}" must be a required string input field.`);
|
|
89
|
-
return [schema];
|
|
90
|
-
}
|
|
91
|
-
if (Array.isArray(input.anyOf) && input.anyOf.length > 0) return input.anyOf.flatMap((schema) => pathParameterSchemas(schema, parameter));
|
|
92
|
-
throw new TypeError("Path parameters require object input contracts.");
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// src/scopes.ts
|
|
96
|
-
/** `<namespace>:<resource>:<action>`, or deeper, in lowercase kebab-case segments. */
|
|
97
|
-
const SCOPE_NAME = /^[a-z][a-z0-9-]*(?::[a-z][a-z0-9-]*){2,}$/;
|
|
98
|
-
/** Whether `scope` is a well-formed scope of the service whose namespace is `namespace`. */
|
|
99
|
-
function isNamespacedScope(scope, namespace) {
|
|
100
|
-
return scope.startsWith(`${namespace}:`) && SCOPE_NAME.test(scope);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Whether granted scopes admit a requirement: any one of the required scopes, or all of them with `all`.
|
|
104
|
-
* An empty requirement admits nobody.
|
|
105
|
-
*/
|
|
106
|
-
function matchesScopes(required, granted, match = "any") {
|
|
107
|
-
if (!required.length) return false;
|
|
108
|
-
const has = (scope) => granted.includes(scope);
|
|
109
|
-
return match === "all" ? required.every(has) : match === "any" && required.some(has);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// src/manifest.ts
|
|
113
|
-
/** Whether an operation belongs in public contracts (OpenAPI, typed clients, discovery metadata). */
|
|
114
|
-
function isPublicOperation(declaration) {
|
|
115
|
-
return declaration.visibility !== "internal";
|
|
116
|
-
}
|
|
117
|
-
/** The scopes of an operation as a list; a caller needs any one of them. */
|
|
118
|
-
function operationScopes(declaration) {
|
|
119
|
-
return typeof declaration.scope === "string" ? [declaration.scope] : declaration.scope;
|
|
120
|
-
}
|
|
121
|
-
/** Shared by HTTP admission, MCP discovery and calls, and execution. */
|
|
122
|
-
function operationMatchesScopes(declaration, granted) {
|
|
123
|
-
return matchesScopes(operationScopes(declaration), granted, declaration.scopeMatch);
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Declares one operation on its own, keeping its literal types. The builder's `.operation()` is the usual way
|
|
127
|
-
* to declare one. As there, `execute` receives the decoded input, `StaticDecode<typeof input>`.
|
|
128
|
-
*/
|
|
129
|
-
function operation(definition) {
|
|
130
|
-
return definition;
|
|
131
|
-
}
|
|
132
|
-
/** Compose resource modules without silently overwriting a capability. */
|
|
133
|
-
function composeOperations(...groups) {
|
|
134
|
-
const result = {};
|
|
135
|
-
for (const group of groups) for (const [name, op] of Object.entries(group)) {
|
|
136
|
-
if (Object.hasOwn(result, name)) throw new TypeError(`Duplicate operation: ${name}`);
|
|
137
|
-
Object.defineProperty(result, name, {
|
|
138
|
-
value: op,
|
|
139
|
-
enumerable: true
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
return result;
|
|
143
|
-
}
|
|
144
|
-
/** The failure codes every operation may answer without declaring them. */
|
|
145
|
-
const STANDARD_CODES = new Set(Object.values(STANDARD_FAILURES).map(({ code }) => code));
|
|
146
|
-
/** Default request body limit when neither the operation nor the application sets one. */
|
|
147
|
-
const DEFAULT_MAX_BODY_BYTES = 32768;
|
|
148
|
-
/** Default output limit when neither the operation nor the application sets one. */
|
|
149
|
-
const DEFAULT_MAX_OUTPUT_BYTES = 262144;
|
|
150
|
-
/** Largest body limit an application or operation may declare: the body is buffered in memory. */
|
|
151
|
-
const MAX_BODY_BYTES_CEILING = 16777216;
|
|
152
|
-
/** The request body limit an operation is held to: its own, else the application's, else the default. */
|
|
153
|
-
function operationBodyLimit(application, declaration) {
|
|
154
|
-
return declaration.maxBodyBytes ?? application.maxBodyBytes ?? 32768;
|
|
155
|
-
}
|
|
156
|
-
/** The output limit an operation is held to: its own, else the application's, else the default. */
|
|
157
|
-
function operationOutputLimit(application, declaration) {
|
|
158
|
-
return declaration.maxOutputBytes ?? application.maxOutputBytes ?? 262144;
|
|
159
|
-
}
|
|
160
|
-
/** Whether an operation's MCP tool reaches outside the service: it does unless it declares otherwise. */
|
|
161
|
-
function mcpOpenWorld(tool) {
|
|
162
|
-
return tool.openWorld ?? true;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// src/internal/digest.ts
|
|
166
|
-
/**
|
|
167
|
-
* SHA-256 digests in hexadecimal, for rate limit keys and skill manifests.
|
|
168
|
-
*/
|
|
169
|
-
/** The SHA-256 digest of text or bytes, in lowercase hexadecimal. */
|
|
170
|
-
async function sha256Hex(value) {
|
|
171
|
-
const bytes = typeof value === "string" ? new TextEncoder().encode(value) : value;
|
|
172
|
-
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
173
|
-
return Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// src/result.ts
|
|
177
|
-
/** A success carrying `value`. */
|
|
178
|
-
function ok(value) {
|
|
179
|
-
return {
|
|
180
|
-
ok: true,
|
|
181
|
-
value
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
/** A failure carrying `error`, usually `catalog.someFailure.create()`. */
|
|
185
|
-
function err(error) {
|
|
186
|
-
return {
|
|
187
|
-
ok: false,
|
|
188
|
-
error
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Whether an untrusted answer (a Service Binding's, a stored one) is a Maximus Result: a success with
|
|
193
|
-
* its value, or a failure with a well-formed error. Anything else is not an answer at all.
|
|
194
|
-
*/
|
|
195
|
-
function isResult(value) {
|
|
196
|
-
if (!isRecord(value)) return false;
|
|
197
|
-
if (value.ok === true) return "value" in value;
|
|
198
|
-
const { error } = value;
|
|
199
|
-
return value.ok === false && isRecord(error) && typeof error.code === "string" && typeof error.message === "string" && typeof error.retryable === "boolean" && isRecord(error.data);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// src/tokens.ts
|
|
203
|
-
/**
|
|
204
|
-
* Token verification: the principal every operation receives, the token policy, OAuth access token
|
|
205
|
-
* verification against the issuer's JWKS, claim projection, and request admission by scope.
|
|
206
|
-
*/
|
|
207
|
-
/**
|
|
208
|
-
* Who calls, as every operation receives it: the actor, its kind, its verified organization and the request
|
|
209
|
-
* id.
|
|
210
|
-
*/
|
|
211
|
-
const PrincipalSchema = Type.Object({
|
|
212
|
-
actorId: Type.String({
|
|
213
|
-
minLength: 1,
|
|
214
|
-
maxLength: 255
|
|
215
|
-
}),
|
|
216
|
-
actorKind: Type.Union([Type.Literal("user"), Type.Literal("service")]),
|
|
217
|
-
organizationId: OrganizationIdSchema,
|
|
218
|
-
requestId: UuidSchema
|
|
219
|
-
}, { additionalProperties: false });
|
|
220
|
-
/**
|
|
221
|
-
* The OAuth client a token was issued to: its id and its class, `human`, `machine` or `dcr` (a dynamically
|
|
222
|
-
* registered client).
|
|
223
|
-
*/
|
|
224
|
-
const TokenClientSchema = Type.Object({
|
|
225
|
-
id: Type.String({
|
|
226
|
-
minLength: 1,
|
|
227
|
-
maxLength: 255
|
|
228
|
-
}),
|
|
229
|
-
class: Type.Union([
|
|
230
|
-
Type.Literal("human"),
|
|
231
|
-
Type.Literal("machine"),
|
|
232
|
-
Type.Literal("dcr")
|
|
233
|
-
])
|
|
234
|
-
}, { additionalProperties: false });
|
|
235
|
-
/**
|
|
236
|
-
* What a verified token grants: the principal without a request id, its scopes and, for an OAuth token, its
|
|
237
|
-
* client. A custom `tokens.resolve` returns this shape.
|
|
238
|
-
*/
|
|
239
|
-
const TokenPrincipalSchema = Type.Object({
|
|
240
|
-
...Type.Omit(PrincipalSchema, ["requestId"]).properties,
|
|
241
|
-
scopes: Type.Array(Type.String({ maxLength: 255 }), { maxItems: 1e3 }),
|
|
242
|
-
/** Custom resolvers must classify DCR from verified application policy, never request headers. */
|
|
243
|
-
client: Type.Optional(TokenClientSchema)
|
|
244
|
-
}, { additionalProperties: false });
|
|
245
|
-
/** Every algorithm `TokenAlgorithm` names. */
|
|
246
|
-
const TOKEN_ALGORITHMS = /* @__PURE__ */ new Set([
|
|
247
|
-
"RS256",
|
|
248
|
-
"RS384",
|
|
249
|
-
"RS512",
|
|
250
|
-
"PS256",
|
|
251
|
-
"PS384",
|
|
252
|
-
"PS512",
|
|
253
|
-
"ES256",
|
|
254
|
-
"ES384",
|
|
255
|
-
"ES512",
|
|
256
|
-
"EdDSA"
|
|
257
|
-
]);
|
|
258
|
-
/** The hosts only `cf dev`, or a test, reaches a Worker as: the internet never reaches a deployed Worker so. */
|
|
259
|
-
const LOCAL_HOSTS = /* @__PURE__ */ new Set([
|
|
260
|
-
"localhost",
|
|
261
|
-
"127.0.0.1",
|
|
262
|
-
"[::1]"
|
|
263
|
-
]);
|
|
264
|
-
/**
|
|
265
|
-
* Whether a token is the development token, presented to a local host. The digests have one length, so the
|
|
266
|
-
* comparison takes the same time however much of the token matches.
|
|
267
|
-
*/
|
|
268
|
-
async function isDevelopmentToken(development, token, request, env) {
|
|
269
|
-
const expected = isRecord(env) ? env[development.binding] : void 0;
|
|
270
|
-
return typeof expected === "string" && expected.length >= 32 && LOCAL_HOSTS.has(new URL(request.url).hostname) && await sha256Hex(token) === await sha256Hex(expected);
|
|
271
|
-
}
|
|
272
|
-
/** Whether the application issues its own bearer tokens instead of OAuth access tokens. */
|
|
273
|
-
function usesOwnBearer(policy) {
|
|
274
|
-
return policy.scheme?.type === "bearer";
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* A claim's own value under its name, else at a dotted path through object claims; undefined when absent.
|
|
278
|
-
* Only own properties are read, so no path reaches a prototype.
|
|
279
|
-
*/
|
|
280
|
-
function claimAt(claims, path) {
|
|
281
|
-
if (Object.hasOwn(claims, path)) return claims[path];
|
|
282
|
-
let value = claims;
|
|
283
|
-
for (const key of path.split(".")) {
|
|
284
|
-
if (!isRecord(value) || !Object.hasOwn(value, key)) return;
|
|
285
|
-
value = value[key];
|
|
286
|
-
}
|
|
287
|
-
return value;
|
|
288
|
-
}
|
|
289
|
-
/** A claim's scopes: a space-separated string or a list of strings, none when absent; null when malformed. */
|
|
290
|
-
function scopeList(value) {
|
|
291
|
-
if (value === void 0) return [];
|
|
292
|
-
if (typeof value === "string") return value.split(" ").filter(Boolean);
|
|
293
|
-
return Array.isArray(value) && value.every((item) => typeof item === "string") ? value : null;
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* The OAuth client a token names: the mapped claim, else `azp` or `client_id`, which must agree when both are
|
|
297
|
-
* present. Undefined when it names none; null when a client claim is malformed or the two disagree.
|
|
298
|
-
*/
|
|
299
|
-
function tokenClient(claims, path) {
|
|
300
|
-
const present = (path === void 0 ? [claims.azp, claims.client_id] : [claimAt(claims, path)]).filter((value) => value !== void 0);
|
|
301
|
-
if (present.some((value) => typeof value !== "string" || !value) || new Set(present).size > 1) return null;
|
|
302
|
-
return present[0];
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* The principal a verified access token grants, read where `mapping` says. A subject of the shape
|
|
306
|
-
* `mapping.machineSubject` names, `<client id>@clients` by default, is a machine; anyone else is a person,
|
|
307
|
-
* who holds only the granted scopes their permissions claim also lists. Malformed or inconsistent claims
|
|
308
|
-
* are refused.
|
|
309
|
-
*/
|
|
310
|
-
function projectToken(claims, mapping = {}) {
|
|
311
|
-
const refused = err(standardErrors.unauthorized.create());
|
|
312
|
-
if (!isRecord(claims) || typeof claims.sub !== "string") return refused;
|
|
313
|
-
const subject = claims.sub;
|
|
314
|
-
const client = tokenClient(claims, mapping.client);
|
|
315
|
-
const granted = scopeList(claimAt(claims, mapping.scopes ?? "scope"));
|
|
316
|
-
if (client === null || granted === null) return refused;
|
|
317
|
-
const clientsSuffix = mapping.machineSubject !== "{client}";
|
|
318
|
-
const machine = client !== void 0 && subject === (clientsSuffix ? `${client}@clients` : client);
|
|
319
|
-
if (clientsSuffix && !machine && subject.endsWith("@clients")) return refused;
|
|
320
|
-
let scopes = [...new Set(granted)];
|
|
321
|
-
const listed = mapping.permissions ?? "permissions";
|
|
322
|
-
if (!machine && listed !== false) {
|
|
323
|
-
const permissions = scopeList(claimAt(claims, listed));
|
|
324
|
-
if (permissions === null) return refused;
|
|
325
|
-
scopes = scopes.filter((scope) => permissions.includes(scope));
|
|
326
|
-
}
|
|
327
|
-
const principal = parseInput(TokenPrincipalSchema, {
|
|
328
|
-
organizationId: claimAt(claims, mapping.organization ?? "org_id"),
|
|
329
|
-
actorId: subject,
|
|
330
|
-
actorKind: machine ? "service" : "user",
|
|
331
|
-
scopes,
|
|
332
|
-
...client === void 0 ? {} : { client: {
|
|
333
|
-
id: client,
|
|
334
|
-
class: machine ? "machine" : "human"
|
|
335
|
-
} }
|
|
336
|
-
});
|
|
337
|
-
return principal.ok ? ok(principal.value) : refused;
|
|
338
|
-
}
|
|
339
|
-
/** The OAuth authorization server; there is no default, so a deployment always names its own. */
|
|
340
|
-
function tokenIssuer(policy) {
|
|
341
|
-
if (!policy.issuer) throw new TypeError("tokens.issuer is required to verify or document OAuth access tokens.");
|
|
342
|
-
return policy.issuer;
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* The audiences a token may carry, the canonical one first: the `resource` OAuth discovery names.
|
|
346
|
-
* Refuses an empty list, an empty audience and a repeated one.
|
|
347
|
-
*/
|
|
348
|
-
function tokenAudiences(policy) {
|
|
349
|
-
const listed = policy.audience;
|
|
350
|
-
const audiences = Array.isArray(listed) ? listed : [listed];
|
|
351
|
-
const valid = audiences.filter((value) => typeof value === "string" && value !== "");
|
|
352
|
-
const [canonical, ...others] = valid;
|
|
353
|
-
if (canonical === void 0 || valid.length !== audiences.length || new Set(valid).size !== valid.length) throw new TypeError("tokens.audience must be a non-empty string or a non-empty list of distinct non-empty strings.");
|
|
354
|
-
return [canonical, ...others];
|
|
355
|
-
}
|
|
356
|
-
/** Whether a value, resolved against `base`, is an HTTPS URL. */
|
|
357
|
-
function httpsUrl(value, base) {
|
|
358
|
-
try {
|
|
359
|
-
return new URL(value, base).protocol === "https:";
|
|
360
|
-
} catch {
|
|
361
|
-
return false;
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
/**
|
|
365
|
-
* Refuse, at construction, an OAuth policy that could never verify or document a token. Verification
|
|
366
|
-
* is built lazily, so without this check a missing issuer surfaced only at run time, as a 401 for
|
|
367
|
-
* every token and a 500 for the OpenAPI document. An application that issues its own bearer tokens
|
|
368
|
-
* names no authorization server.
|
|
369
|
-
*/
|
|
370
|
-
function checkTokenAuthority(policy) {
|
|
371
|
-
const { development } = policy;
|
|
372
|
-
if (development !== void 0 && (typeof development.binding !== "string" || !development.binding || development.organizationId !== void 0 && !parseInput(OrganizationIdSchema, development.organizationId).ok)) throw new TypeError("tokens.development names the binding of its token and a valid organization id.");
|
|
373
|
-
if (usesOwnBearer(policy)) return;
|
|
374
|
-
const { algorithms, claims } = policy;
|
|
375
|
-
if (algorithms !== void 0 && (!algorithms.length || algorithms.some((name) => !TOKEN_ALGORITHMS.has(name)))) throw new TypeError("tokens.algorithms lists one or more asymmetric JWS algorithms.");
|
|
376
|
-
if (claims !== void 0) {
|
|
377
|
-
const { permissions, machineSubject } = claims;
|
|
378
|
-
if ([
|
|
379
|
-
claims.organization,
|
|
380
|
-
claims.scopes,
|
|
381
|
-
claims.client,
|
|
382
|
-
permissions === false ? void 0 : permissions
|
|
383
|
-
].some((path) => path !== void 0 && (typeof path !== "string" || !path)) || machineSubject !== void 0 && !["{client}@clients", "{client}"].includes(machineSubject)) throw new TypeError("tokens.claims names each claim by a non-empty string (permissions may be false), and machineSubject is {client}@clients or {client}.");
|
|
384
|
-
}
|
|
385
|
-
const issuer = tokenIssuer(policy);
|
|
386
|
-
if (!httpsUrl(issuer) || [
|
|
387
|
-
policy.jwksUri,
|
|
388
|
-
policy.authorizationEndpoint,
|
|
389
|
-
policy.tokenEndpoint
|
|
390
|
-
].some((endpoint) => endpoint !== void 0 && !httpsUrl(endpoint, issuer))) throw new TypeError("tokens.issuer, jwksUri, authorizationEndpoint and tokenEndpoint must be absolute HTTPS URLs, the last three resolved against the issuer.");
|
|
391
|
-
}
|
|
392
|
-
/** The authorization server's authorization and token endpoints, which OpenAPI publishes. */
|
|
393
|
-
function tokenEndpoints(policy) {
|
|
394
|
-
const issuer = tokenIssuer(policy);
|
|
395
|
-
return {
|
|
396
|
-
authorization: new URL(policy.authorizationEndpoint ?? "authorize", issuer).href,
|
|
397
|
-
token: new URL(policy.tokenEndpoint ?? "oauth/token", issuer).href
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
/** A token is accepted when its `aud` names any one of the policy's audiences. */
|
|
401
|
-
function createTokenVerifier(policy) {
|
|
402
|
-
const issuer = tokenIssuer(policy);
|
|
403
|
-
const audience = tokenAudiences(policy);
|
|
404
|
-
const uri = new URL(policy.jwksUri ?? ".well-known/jwks.json", issuer);
|
|
405
|
-
if (new URL(issuer).protocol !== "https:" || uri.protocol !== "https:") throw new TypeError("Token authority must use HTTPS.");
|
|
406
|
-
const jwks = createRemoteJWKSet(uri, {
|
|
407
|
-
timeoutDuration: 1e4,
|
|
408
|
-
cacheMaxAge: 6e5,
|
|
409
|
-
cooldownDuration: 3e4
|
|
410
|
-
});
|
|
411
|
-
return async (token) => {
|
|
412
|
-
const { payload } = await jwtVerify(token, jwks, {
|
|
413
|
-
issuer,
|
|
414
|
-
audience,
|
|
415
|
-
algorithms: [...policy.algorithms ?? ["RS256"]],
|
|
416
|
-
clockTolerance: 5,
|
|
417
|
-
requiredClaims: ["exp", "sub"]
|
|
418
|
-
});
|
|
419
|
-
return payload;
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Admits a request by its bearer token: verifies it (OAuth by JWKS, or the policy's own `resolve`) and checks
|
|
424
|
-
* that it carries the required scope. Refusals are `UNAUTHORIZED` or `FORBIDDEN` failures, never exceptions.
|
|
425
|
-
*/
|
|
426
|
-
function createTokenAuthorization(policy, given) {
|
|
427
|
-
let verifier = given;
|
|
428
|
-
/** Verifies an OAuth access token, building the verifier on first use. */
|
|
429
|
-
const verify = (token) => {
|
|
430
|
-
verifier ??= createTokenVerifier(policy);
|
|
431
|
-
return verifier(token);
|
|
432
|
-
};
|
|
433
|
-
/** Admit the request when its token carries `scope`, or any one of `scope` when it is a list. */
|
|
434
|
-
return async (request, scope, context, scopeMatch = "any") => {
|
|
435
|
-
const accepted = typeof scope === "string" ? [scope] : scope;
|
|
436
|
-
/** Whether granted scopes admit the request. */
|
|
437
|
-
const admits = (scopes) => matchesScopes(accepted, scopes, scopeMatch);
|
|
438
|
-
const token = (policy.resolve || policy.development ? /^Bearer (\S+)$/i : /^Bearer ([A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+)$/i).exec(request.headers.get("authorization") ?? "")?.[1];
|
|
439
|
-
if (!token || token.length > 16384 || request.headers.has("x-api-key")) return err(standardErrors.unauthorized.create());
|
|
440
|
-
if (policy.development && context && await isDevelopmentToken(policy.development, token, request, context.env)) {
|
|
441
|
-
const developer = {
|
|
442
|
-
actorId: "local-developer",
|
|
443
|
-
actorKind: "user",
|
|
444
|
-
organizationId: policy.development.organizationId ?? "local-development",
|
|
445
|
-
scopes: [...context.applicationScopes],
|
|
446
|
-
client: {
|
|
447
|
-
id: "local-development",
|
|
448
|
-
class: "human"
|
|
449
|
-
}
|
|
450
|
-
};
|
|
451
|
-
return admits(developer.scopes) ? ok(developer) : err(standardErrors.forbidden.create());
|
|
452
|
-
}
|
|
453
|
-
if (policy.resolve) try {
|
|
454
|
-
const outcome = await policy.resolve(token, context);
|
|
455
|
-
if ("ok" in outcome && !outcome.ok) return err(outcome.error.code === "FORBIDDEN" ? standardErrors.forbidden.create() : standardErrors.unauthorized.create());
|
|
456
|
-
const resolved = parseInput(TokenPrincipalSchema, "ok" in outcome ? outcome.value : outcome);
|
|
457
|
-
if (!resolved.ok) return err(standardErrors.unauthorized.create());
|
|
458
|
-
const { client } = resolved.value;
|
|
459
|
-
if (client && (client.class === "machine" !== (resolved.value.actorKind === "service") || policy.scheme?.type !== "oauth+bearer" && client.class === "machine" && resolved.value.actorId !== client.id && resolved.value.actorId !== `${client.id}@clients`)) return err(standardErrors.unauthorized.create());
|
|
460
|
-
return admits(resolved.value.scopes) ? ok(resolved.value) : err(standardErrors.forbidden.create());
|
|
461
|
-
} catch {
|
|
462
|
-
return err(standardErrors.unauthorized.create());
|
|
463
|
-
}
|
|
464
|
-
let claims;
|
|
465
|
-
try {
|
|
466
|
-
claims = await verify(token);
|
|
467
|
-
} catch {
|
|
468
|
-
return err(standardErrors.unauthorized.create());
|
|
469
|
-
}
|
|
470
|
-
const principal = projectToken(claims, policy.claims);
|
|
471
|
-
if (!principal.ok) return principal;
|
|
472
|
-
return admits(principal.value.scopes) ? principal : err(standardErrors.forbidden.create());
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
// src/define-app.ts
|
|
477
|
-
/** Names a Worker entrypoint already uses, or that would break a class or a promise. */
|
|
478
|
-
const RESERVED_NAMES = /* @__PURE__ */ new Set([
|
|
479
|
-
"fetch",
|
|
480
|
-
"connect",
|
|
481
|
-
"constructor",
|
|
482
|
-
"then",
|
|
483
|
-
"scheduled",
|
|
484
|
-
"queue",
|
|
485
|
-
"alarm",
|
|
486
|
-
"__proto__",
|
|
487
|
-
"prototype"
|
|
488
|
-
]);
|
|
489
|
-
/** An MCP tool name: a letter, then up to 63 letters, digits and underscores. */
|
|
490
|
-
const TOOL_NAME = /^[a-z][A-Za-z0-9_]{0,63}$/;
|
|
491
|
-
/** Every field an operation has: any other, such as a misspelled or retired one, would do nothing. */
|
|
492
|
-
const OPERATION_FIELDS = new Set(Object.keys({
|
|
493
|
-
actorKinds: true,
|
|
494
|
-
http: true,
|
|
495
|
-
visibility: true,
|
|
496
|
-
mcp: true,
|
|
497
|
-
rpc: true,
|
|
498
|
-
scope: true,
|
|
499
|
-
scopeMatch: true,
|
|
500
|
-
conditionalScopes: true,
|
|
501
|
-
failures: true,
|
|
502
|
-
title: true,
|
|
503
|
-
description: true,
|
|
504
|
-
group: true,
|
|
505
|
-
effect: true,
|
|
506
|
-
input: true,
|
|
507
|
-
output: true,
|
|
508
|
-
maxBodyBytes: true,
|
|
509
|
-
maxOutputBytes: true,
|
|
510
|
-
execute: true,
|
|
511
|
-
pipeline: true
|
|
512
|
-
}));
|
|
513
|
-
/** Checks the application, then hands it back unchanged. */
|
|
514
|
-
function defineApp() {
|
|
515
|
-
return (manifest) => {
|
|
516
|
-
checkApplication(manifest);
|
|
517
|
-
return manifest;
|
|
518
|
-
};
|
|
519
|
-
}
|
|
520
|
-
/**
|
|
521
|
-
* Every rule, in the order they always ran, so an application that breaks several meets the same refusal
|
|
522
|
-
* first.
|
|
523
|
-
*/
|
|
524
|
-
function checkApplication(manifest) {
|
|
525
|
-
if (!/^[a-z][a-z0-9-]{1,63}$/.test(manifest.namespace)) throw new TypeError("A scope namespace is required.");
|
|
526
|
-
checkDeclaredFailures(manifest.operations, manifest.errors);
|
|
527
|
-
const claimed = {
|
|
528
|
-
routes: /* @__PURE__ */ new Set(),
|
|
529
|
-
tools: /* @__PURE__ */ new Set()
|
|
530
|
-
};
|
|
531
|
-
for (const [name, op] of Object.entries(manifest.operations)) checkOperation(name, op, manifest, claimed);
|
|
532
|
-
createHttpRouteResolver(Object.values(manifest.operations));
|
|
533
|
-
checkGuides(manifest.mcp?.guides ?? []);
|
|
534
|
-
checkSkills(manifest.mcp?.skills ?? []);
|
|
535
|
-
checkSkillTool(manifest.mcp, claimed.tools);
|
|
536
|
-
if (!validBodyLimit(manifest.maxBodyBytes)) throw new TypeError("The application body limit is a positive integer up to 16 MiB.");
|
|
537
|
-
if (!validBodyLimit(manifest.maxOutputBytes)) throw new TypeError("The application output limit is a positive integer up to 16 MiB.");
|
|
538
|
-
if (new URL(manifest.origin).protocol !== "https:") throw new TypeError("Application origin must use HTTPS.");
|
|
539
|
-
checkTokenAuthority(manifest.tokens);
|
|
540
|
-
tokenAudiences(manifest.tokens);
|
|
541
|
-
if (Object.keys(manifest.tokens.resources ?? {}).length > 0 && manifest.tokens.resourceMetadataUrl === void 0) throw new TypeError("tokens.resources requires tokens.resourceMetadataUrl: name the metadata a 401 challenge points at.");
|
|
542
|
-
}
|
|
543
|
-
/** An operation's `failures` name domain codes of the catalog, each once. */
|
|
544
|
-
function checkDeclaredFailures(operations, errors) {
|
|
545
|
-
const domainCodes = new Set(Object.values(errors.definitions).map(({ code }) => code).filter((code) => !STANDARD_CODES.has(code)));
|
|
546
|
-
for (const [name, op] of Object.entries(operations)) if (op.failures && (new Set(op.failures).size !== op.failures.length || op.failures.some((code) => !domainCodes.has(code)))) throw new TypeError(`Operation "${name}": failures lists each domain failure code of the error catalog once; standard failures are always allowed.`);
|
|
547
|
-
}
|
|
548
|
-
/** Every rule one operation keeps, in the order a definition is usually written. */
|
|
549
|
-
function checkOperation(name, op, application, claimed) {
|
|
550
|
-
checkOperationContract(name, op, application.namespace);
|
|
551
|
-
checkHttpExposure(name, op, claimed.routes);
|
|
552
|
-
if (op.conditionalScopes && (!op.conditionalScopes.length || new Set(op.conditionalScopes).size !== op.conditionalScopes.length || op.conditionalScopes.some((scope) => operationScopes(op).includes(scope) || !isNamespacedScope(scope, application.namespace)))) throw new TypeError("Conditional scopes require unique namespaced declarations distinct from the admission scope.");
|
|
553
|
-
if (op.mcp) {
|
|
554
|
-
if (!TOOL_NAME.test(op.mcp.name) || claimed.tools.has(op.mcp.name)) throw new TypeError("MCP tools require unique safe names.");
|
|
555
|
-
claimed.tools.add(op.mcp.name);
|
|
556
|
-
}
|
|
557
|
-
if (op.rpc && op.rpc !== true && op.rpc.callers.some((caller) => !application.rpc.callers.includes(caller))) throw new TypeError("Operation RPC callers must be allowed by the application.");
|
|
558
|
-
}
|
|
559
|
-
/** The operation's fields, name, scopes, eligible actors and schemas. */
|
|
560
|
-
function checkOperationContract(name, op, namespace) {
|
|
561
|
-
const unknown = Object.keys(op).find((field) => !OPERATION_FIELDS.has(field));
|
|
562
|
-
if (unknown !== void 0) throw new TypeError(`Operation "${name}": "${unknown}" is not an operation field.`);
|
|
563
|
-
if (op.scopeMatch !== void 0 && op.scopeMatch !== "any" && op.scopeMatch !== "all") throw new TypeError(`Operation "${name}": scopeMatch must be any or all.`);
|
|
564
|
-
if (hasUncompiledCodecs(op.input)) throw new TypeError(`Operation "${name}": its input uses codecs (normalize, transform, inspect, refine, pipe or stripObject) that run only inside inputSchema(); wrap the input in inputSchema().`);
|
|
565
|
-
if (op.actorKinds && (!op.actorKinds.length || new Set(op.actorKinds).size !== op.actorKinds.length || op.actorKinds.some((kind) => kind !== "user" && kind !== "service"))) throw new TypeError(`Operation "${name}": actorKinds must explicitly name eligible users or services.`);
|
|
566
|
-
if (!/^[a-z][a-zA-Z0-9]*$/.test(name) || RESERVED_NAMES.has(name)) throw new TypeError(`Operation "${name}": use a camelCase name that is not a reserved Worker method. Use safe operation names, namespaced scopes and strict object schemas.`);
|
|
567
|
-
const scopes = operationScopes(op);
|
|
568
|
-
if (!scopes.length || new Set(scopes).size !== scopes.length || scopes.some((scope) => !isNamespacedScope(scope, namespace))) throw new TypeError(`Operation "${name}": every scope is "${namespace}:<resource>:<action>", listed once. Use safe operation names, namespaced scopes and strict object schemas.`);
|
|
569
|
-
if (!(strictObject(op.input, op.http?.method !== "GET") && (isAttachmentSchema(op.output) || strictObject(op.output, true)))) throw new TypeError(`Operation "${name}": input and output are strict objects or unions of strict objects; a GET input is one strict object (its query). Use safe operation names, namespaced scopes and strict object schemas.`);
|
|
570
|
-
}
|
|
571
|
-
/** Byte bodies, attachments, limits, header-sourced fields and the route itself. */
|
|
572
|
-
function checkHttpExposure(name, op, routes) {
|
|
573
|
-
if (op.http?.body !== void 0 && op.http.body !== "bytes" || op.http?.response !== void 0 && op.http.response !== "attachment") throw new TypeError("Unsupported HTTP binary contract.");
|
|
574
|
-
const binary = op.http?.body === "bytes";
|
|
575
|
-
const attachment = isAttachmentSchema(op.output);
|
|
576
|
-
if ((binary || attachment) && (!op.http || op.mcp || op.rpc)) throw new TypeError(`Operation "${name}": byte bodies and attachments are HTTP-only.`);
|
|
577
|
-
if (op.http?.response === "attachment" !== attachment || binary && op.http?.method === "GET") throw new TypeError(`Operation "${name}": declare matching attachment output and non-GET byte bodies.`);
|
|
578
|
-
if (!validBodyLimit(op.maxOutputBytes)) throw new TypeError("Invalid operation output byte limit.");
|
|
579
|
-
if (op.http?.successStatus !== void 0 && ![
|
|
580
|
-
200,
|
|
581
|
-
201,
|
|
582
|
-
202
|
|
583
|
-
].includes(op.http.successStatus)) throw new TypeError("Invalid operation success status.");
|
|
584
|
-
if (op.http?.headers) {
|
|
585
|
-
const pathFields = new Set(operationPathParameters(op.http.path));
|
|
586
|
-
const headers = Object.entries(op.http.headers);
|
|
587
|
-
if (!binary || op.input.type !== "object" || new Set(headers.map(([, wire]) => wire.toLowerCase())).size !== headers.length || headers.some(([field, wire]) => !Object.hasOwn(op.input.properties, field) || pathFields.has(field) || !isInputHeaderName(wire))) throw new TypeError("Byte operation headers must name unique, non-authority metadata fields.");
|
|
588
|
-
}
|
|
589
|
-
if (!validBodyLimit(op.maxBodyBytes) || op.maxBodyBytes !== void 0 && op.http?.method === "GET") throw new TypeError(`Operation "${name}": an operation body limit is a positive integer up to 16 MiB, on a non-GET route.`);
|
|
590
|
-
if (op.http) {
|
|
591
|
-
for (const parameter of operationPathParameters(op.http.path)) pathParameterSchemas(op.input, parameter);
|
|
592
|
-
const route = `${op.http.method} ${op.http.path}`;
|
|
593
|
-
if (routes.has(route) || op.effect !== "read" && op.http.method === "GET") throw new TypeError(`Operation "${name}": use unique versioned routes; mutations cannot use GET.`);
|
|
594
|
-
routes.add(route);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
/** Guides are named resources and prompts: each name safe and unique. */
|
|
598
|
-
function checkGuides(guides) {
|
|
599
|
-
if (new Set(guides.map((guide) => guide.name)).size !== guides.length || guides.some((guide) => !/^[a-z][a-z0-9-]{0,63}$/.test(guide.name))) throw new TypeError("Guides require unique safe names.");
|
|
600
|
-
}
|
|
601
|
-
/** The MCP skills: unique Agent Skills names, one-line descriptions and instructions. */
|
|
602
|
-
function checkSkills(skills) {
|
|
603
|
-
if (new Set(skills.map((skill) => skill.name)).size !== skills.length || skills.some((skill) => skill.name.length > 64 || !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(skill.name) || !skill.description.trim() || skill.description.length > 1024 || /[\r\n]/.test(skill.description) || !skill.text.trim())) throw new TypeError("Skills require unique Agent Skills names, a one-line description and instructions.");
|
|
604
|
-
}
|
|
605
|
-
/** The skill tool serves the MCP skills under a name no operation's tool takes. */
|
|
606
|
-
function checkSkillTool(mcp, tools) {
|
|
607
|
-
const skillTool = mcp?.skillTool;
|
|
608
|
-
if (skillTool !== void 0 && (!(mcp?.skills ?? []).length || !TOOL_NAME.test(skillTool) || tools.has(skillTool))) throw new TypeError("A skill tool requires skills and a safe name no operation uses.");
|
|
609
|
-
}
|
|
610
|
-
/** Whether a body or output limit is absent, or a positive integer up to 16 MiB. */
|
|
611
|
-
function validBodyLimit(value) {
|
|
612
|
-
return value === void 0 || Number.isInteger(value) && value > 0 && value <= 16777216;
|
|
613
|
-
}
|
|
614
|
-
/**
|
|
615
|
-
* A strict object (`additionalProperties: false`), or — when `unions` is true —
|
|
616
|
-
* a union whose every member is a strict object, so a request body or a result
|
|
617
|
-
* can be discriminated by a field (`{ channel: 'email', … } | { channel: 'sms', … }`).
|
|
618
|
-
*/
|
|
619
|
-
function strictObject(schema, unions) {
|
|
620
|
-
if (schema.type === "object") return schema.additionalProperties === false;
|
|
621
|
-
const members = schema.anyOf;
|
|
622
|
-
return unions && Array.isArray(members) && members.length > 0 && members.every((member) => strictObject(member, false));
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
export { validAttachment as A, operationOutputLimit as C, attachmentResponse as D, pathParameterSchemas as E, binaryBody as O, operationMatchesScopes as S, matchesScopes as T, composeOperations as _, projectToken as a, operation as b, tokenIssuer as c, isResult as d, ok as f, STANDARD_CODES as g, MAX_BODY_BYTES_CEILING as h, createTokenVerifier as i, isAttachmentSchema as k, usesOwnBearer as l, DEFAULT_MAX_BODY_BYTES as m, PrincipalSchema as n, tokenAudiences as o, sha256Hex as p, createTokenAuthorization as r, tokenEndpoints as s, defineApp as t, err as u, isPublicOperation as v, operationScopes as w, operationBodyLimit as x, mcpOpenWorld as y };
|