@guren/plugin-cloudflare 0.9.0 → 0.10.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 +2 -0
- package/dist/{build-DN7RvfKD.js → bundle-size-NXkcJkkx.js} +189 -4
- package/dist/commands.d.ts +20 -2
- package/dist/commands.js +67 -29
- package/dist/index.d.ts +68 -1
- package/dist/index.js +2 -2
- package/package.json +5 -4
- package/templates/mcp-oauth/app/Http/Controllers/McpOAuthController.ts +17 -2
- package/templates/mcp-oauth/app/View/McpOAuthConsentPage.tsx +23 -4
package/README.md
CHANGED
|
@@ -20,6 +20,8 @@ bunx wrangler deploy
|
|
|
20
20
|
|
|
21
21
|
Before the app build it runs the deploy-runtime checks `guren doctor` reports and warns, without failing, when sessions or OAuth state would sit in process memory, a `ScryptHasher` is constructed, or providers are discovered from the filesystem. Each works locally and breaks on Workers, and the warning prints where you are still reading rather than after the Vite output.
|
|
22
22
|
|
|
23
|
+
`bunx guren cloudflare:size` (or `cloudflare:build --report-size`) measures the bundle a deploy would upload through wrangler's dry run and lists its largest sources by package. The platform limit is 64 MiB uncompressed on every plan; the report warns from half of it.
|
|
24
|
+
|
|
23
25
|
## API
|
|
24
26
|
|
|
25
27
|
- **`createWorkersHandler(app)`** — wraps a Guren `Application` in a Workers module handler. Boot is lazy and deduplicated on the first request, because `boot()` performs I/O that workerd forbids in global scope. The handler deduplicates boot itself, so boot-once holds for anything matching `WorkersAppLike`, not only Guren's `Application`. It also exposes `boot(env)`, for an entrypoint that holds `env` but no request — an agent Durable Object woken by an alarm. The latch behind both is `bootWorkersApp(app, env)` / `bootAndFetch(app, request, env, ctx)`, keyed on the app.
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { join, relative, resolve, sep } from "node:path";
|
|
1
|
+
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
|
-
import { AGENTS_CONFIG_FILE, DEV_ONLY_MODULES, DOCUMENT_ASSET_EXTENSIONS, DOCUMENT_ASSET_HEADERS, MCP_PLUGIN_PACKAGE, MCP_TRANSPORT_SPECIFIER, SQL_CLIENT_MODULES, appUsesMcpPlugin, assertOutputDirOutsideRoot, clientManifestJson, importSpecifier, renderDevOnlyStub, resetOutputDir, resolveClientAssetEnv, resolvePathLike, resolveSsrEntryFile, stageStaticAssets, stubbableDevOnlyModules } from "@guren/core/internal/deploy-build";
|
|
4
|
+
import { AGENTS_CONFIG_FILE, DATABASE_FACTORIES, DEV_ONLY_MODULES, DOCUMENT_ASSET_EXTENSIONS, DOCUMENT_ASSET_HEADERS, MCP_PLUGIN_PACKAGE, MCP_TRANSPORT_SPECIFIER, SQL_CLIENT_MODULES, appUsesMcpPlugin, assertOutputDirOutsideRoot, clientManifestJson, detectDatabaseDialects, importSpecifier, renderDevOnlyStub, resetOutputDir, resolveClientAssetEnv, resolvePathLike, resolveSsrEntryFile, stageStaticAssets, stubbableDevOnlyModules } from "@guren/core/internal/deploy-build";
|
|
5
5
|
import { reportDeployRuntimeHazards } from "@guren/core/internal/deploy-check";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
6
7
|
//#region src/templates.ts
|
|
7
8
|
/**
|
|
8
9
|
* The OAuth consent flow this plugin scaffolds, shipped as real sources under
|
|
@@ -71,6 +72,7 @@ async function buildCloudflareOutput(options = {}) {
|
|
|
71
72
|
root,
|
|
72
73
|
label: "Cloudflare build"
|
|
73
74
|
});
|
|
75
|
+
warnDatabaseWithoutD1(root);
|
|
74
76
|
if (!options.skipAppBuild) runAppBuild(root, packageJson.scripts ?? {});
|
|
75
77
|
if (!existsSync(appEntry)) throw new Error(`Cloudflare build: app entry not found at ${appEntry}. Pass "appEntry" if your Application lives elsewhere.`);
|
|
76
78
|
const ssrImport = await resolveSsrImport(ssrDir, ssrEntryKey);
|
|
@@ -410,6 +412,19 @@ function nextMigrationTag(config) {
|
|
|
410
412
|
return `v${highest + 1}`;
|
|
411
413
|
}
|
|
412
414
|
/**
|
|
415
|
+
* Every SQL client is stubbed below whether or not the app calls
|
|
416
|
+
* `createD1Database()`, so a config that never does deploys clean and throws on
|
|
417
|
+
* its first query. Advice, not a gate, and silent when detection returns no
|
|
418
|
+
* dialects: a name scan cannot see a factory reached indirectly. A config naming
|
|
419
|
+
* D1 beside another factory (`web/config/database.ts` switches at runtime) passes.
|
|
420
|
+
*/
|
|
421
|
+
function warnDatabaseWithoutD1(root) {
|
|
422
|
+
const { dialects, source } = detectDatabaseDialects(root);
|
|
423
|
+
if (!dialects || !source || dialects.includes("d1")) return;
|
|
424
|
+
const factories = Object.entries(DATABASE_FACTORIES).filter(([, dialect]) => dialects.includes(dialect)).map(([factory]) => `${factory}()`).join(", ");
|
|
425
|
+
console.warn(`Cloudflare build: ${source} calls ${factories} and never createD1Database(). On Cloudflare Workers only createD1Database() can connect — every other database client is stubbed in this worker — so the deployed worker will throw at its first database call. Switch the config to createD1Database() (keeping the other factory behind an isWorkersRuntime() branch if it serves local development).`);
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
413
428
|
* Both lists: on Workers the SQL clients are as unreachable as the dev-only
|
|
414
429
|
* modules, because D1 is the only database the platform has.
|
|
415
430
|
*/
|
|
@@ -783,6 +798,19 @@ function renderOAuthProvider(mcpPath, defaultEntry) {
|
|
|
783
798
|
// 2026-07-28 line in favour of Client ID Metadata Documents, but it is
|
|
784
799
|
// what shipping MCP SDK 1.x clients use to register themselves today.
|
|
785
800
|
clientRegistrationEndpoint: ${JSON.stringify(OAUTH_ENDPOINTS.register)},
|
|
801
|
+
// Advertised in the authorization server metadata, so a client has something to
|
|
802
|
+
// discover and request rather than sending no scope at all. Set-level only: the
|
|
803
|
+
// build has no route graph to name \`tool:<name>\` scopes from. A grant records
|
|
804
|
+
// those \`tool:<name>\` entries, so a refresh must echo the token response's own
|
|
805
|
+
// scope and never this list, which the provider downscopes to nothing.
|
|
806
|
+
scopesSupported: ['tools:*', 'tools:read'],
|
|
807
|
+
// The half a conforming MCP client actually reads. Its scope selection
|
|
808
|
+
// strategy consults the 401 \`WWW-Authenticate\` challenge and Protected
|
|
809
|
+
// Resource Metadata (RFC 9728) — never the RFC 8414 document above — and this
|
|
810
|
+
// one option feeds both. Only \`tools:read\`, because a client is told to
|
|
811
|
+
// request everything listed here and this field is specified to carry the
|
|
812
|
+
// minimum basic functionality needs; the wider scope stays discoverable above.
|
|
813
|
+
resourceMetadata: { scopes_supported: ['tools:read'] },
|
|
786
814
|
})`;
|
|
787
815
|
}
|
|
788
816
|
function scaffoldWranglerConfig(root, out, packageName, mcpPlugin, mcpOAuth, agents) {
|
|
@@ -964,4 +992,161 @@ function warnOAuthDrift(configPath, config, mcpOAuth) {
|
|
|
964
992
|
console.warn(`Cloudflare build: ${configPath} binds ${OAUTH_KV_BINDING}, which only an OAuth-fronted worker uses, but this build ran without --mcp-oauth. The worker it produced has no OAuth provider in it: ${OAUTH_ENDPOINTS.token} and ${OAUTH_ENDPOINTS.register} will 404 and already-authorized clients will stop working. Pass --mcp-oauth, or remove the binding if this app no longer fronts its MCP endpoint with OAuth.`);
|
|
965
993
|
}
|
|
966
994
|
//#endregion
|
|
967
|
-
|
|
995
|
+
//#region src/bundle-size.ts
|
|
996
|
+
/**
|
|
997
|
+
* The size of the worker wrangler would upload, and where it comes from.
|
|
998
|
+
* wrangler's dry run does the bundling and esbuild's metafile the attribution,
|
|
999
|
+
* so the numbers are the ones a deploy produces. The platform limit is 64 MiB
|
|
1000
|
+
* uncompressed on every plan since 2026-09-04, when Cloudflare dropped the
|
|
1001
|
+
* compressed limits (3 MB Free / 10 MB Paid); it is held in one place below
|
|
1002
|
+
* with its source and the date it was confirmed, because a number that moved
|
|
1003
|
+
* once will move again. Startup time and isolate memory are not measured here.
|
|
1004
|
+
*/
|
|
1005
|
+
const WORKER_SIZE_LIMIT = {
|
|
1006
|
+
bytes: 67108864,
|
|
1007
|
+
source: "https://developers.cloudflare.com/workers/platform/limits/#worker-size",
|
|
1008
|
+
confirmedOn: "2026-09-09"
|
|
1009
|
+
};
|
|
1010
|
+
/** Share of the limit at which the report turns into a warning. */
|
|
1011
|
+
const WORKER_SIZE_WARN_SHARE = .5;
|
|
1012
|
+
const UNIT_BYTES = {
|
|
1013
|
+
B: 1,
|
|
1014
|
+
KiB: 1024,
|
|
1015
|
+
MiB: 1048576
|
|
1016
|
+
};
|
|
1017
|
+
function toBytes(value, unit) {
|
|
1018
|
+
return Math.round(Number.parseFloat(value) * UNIT_BYTES[unit]);
|
|
1019
|
+
}
|
|
1020
|
+
/** wrangler prints `Total Upload: 4931.18 KiB / gzip: 899.63 KiB`; the unit is its choice. */
|
|
1021
|
+
function parseWranglerSize(output) {
|
|
1022
|
+
const total = output.match(/Total Upload:\s*([\d.]+)\s*(B|KiB|MiB)/u);
|
|
1023
|
+
if (!total) return null;
|
|
1024
|
+
const gzip = output.match(/gzip:\s*([\d.]+)\s*(B|KiB|MiB)/u);
|
|
1025
|
+
return {
|
|
1026
|
+
totalBytes: toBytes(total[1], total[2]),
|
|
1027
|
+
gzipBytes: gzip ? toBytes(gzip[1], gzip[2]) : null
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* The nearest *named* package.json above the file that is not the app's own.
|
|
1032
|
+
* Handles node_modules, bun's `.bun/<pkg>@<version>/node_modules/<pkg>` layout
|
|
1033
|
+
* and workspace siblings alike, since each carries a manifest. A nameless one
|
|
1034
|
+
* (zod's `v4/package.json` holds only `type`) is passed over. Cached per
|
|
1035
|
+
* directory: a bundle has hundreds of inputs in a few dozen directories.
|
|
1036
|
+
*/
|
|
1037
|
+
function createPackageNameResolver(root) {
|
|
1038
|
+
const rootDir = resolve(root);
|
|
1039
|
+
const byDir = /* @__PURE__ */ new Map();
|
|
1040
|
+
const lookup = (dir) => {
|
|
1041
|
+
const cached = byDir.get(dir);
|
|
1042
|
+
if (cached !== void 0) return cached;
|
|
1043
|
+
let name = null;
|
|
1044
|
+
if (dir !== rootDir) {
|
|
1045
|
+
const manifest = join(dir, "package.json");
|
|
1046
|
+
const named = existsSync(manifest) ? readPackageName(manifest) : void 0;
|
|
1047
|
+
const parent = dirname(dir);
|
|
1048
|
+
name = named ?? (parent === dir ? null : lookup(parent));
|
|
1049
|
+
}
|
|
1050
|
+
byDir.set(dir, name);
|
|
1051
|
+
return name;
|
|
1052
|
+
};
|
|
1053
|
+
return (file) => lookup(dirname(resolve(file)));
|
|
1054
|
+
}
|
|
1055
|
+
function readPackageName(manifest) {
|
|
1056
|
+
try {
|
|
1057
|
+
const parsed = JSON.parse(readFileSync(manifest, "utf8"));
|
|
1058
|
+
return typeof parsed.name === "string" && parsed.name.length > 0 ? parsed.name : void 0;
|
|
1059
|
+
} catch {
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
/** Every input's bytes in the bundle, summed per package, largest first. */
|
|
1064
|
+
function attributeBundle(metafile, options) {
|
|
1065
|
+
const root = resolve(options.root);
|
|
1066
|
+
const cwd = resolve(options.cwd ?? root);
|
|
1067
|
+
const packageNameOf = options.packageNameOf ?? createPackageNameResolver(root);
|
|
1068
|
+
const bundle = Object.values(metafile.outputs).reduce((best, output) => best === void 0 || Object.keys(output.inputs).length > Object.keys(best.inputs).length ? output : best, void 0);
|
|
1069
|
+
if (!bundle) return [];
|
|
1070
|
+
const bytesByName = /* @__PURE__ */ new Map();
|
|
1071
|
+
for (const [input, { bytesInOutput }] of Object.entries(bundle.inputs)) {
|
|
1072
|
+
const file = resolve(cwd, input);
|
|
1073
|
+
const name = packageNameOf(file) ?? relative(root, file);
|
|
1074
|
+
bytesByName.set(name, (bytesByName.get(name) ?? 0) + bytesInOutput);
|
|
1075
|
+
}
|
|
1076
|
+
return [...bytesByName.entries()].map(([name, bytes]) => ({
|
|
1077
|
+
name,
|
|
1078
|
+
bytes
|
|
1079
|
+
})).sort((a, b) => b.bytes - a.bytes || a.name.localeCompare(b.name));
|
|
1080
|
+
}
|
|
1081
|
+
function kib(bytes) {
|
|
1082
|
+
return `${Math.round(bytes / 1024).toLocaleString("en-US")} KiB`;
|
|
1083
|
+
}
|
|
1084
|
+
function percent(share) {
|
|
1085
|
+
return `${(share * 100).toFixed(1)}%`;
|
|
1086
|
+
}
|
|
1087
|
+
function renderBundleReport(size, attribution, options = {}) {
|
|
1088
|
+
const limitBytes = options.limitBytes ?? WORKER_SIZE_LIMIT.bytes;
|
|
1089
|
+
const share = size.totalBytes / limitBytes;
|
|
1090
|
+
const warn = share >= (options.warnShare ?? .5);
|
|
1091
|
+
const gzip = size.gzipBytes === null ? "" : ` (gzip ${kib(size.gzipBytes)})`;
|
|
1092
|
+
const lines = [`Worker bundle: ${kib(size.totalBytes)} uncompressed${gzip}, ${percent(share)} of the ${Math.round(limitBytes / 1024 / 1024)} MiB platform limit (uncompressed; ${WORKER_SIZE_LIMIT.source}, confirmed ${WORKER_SIZE_LIMIT.confirmedOn}).`];
|
|
1093
|
+
const top = attribution.slice(0, options.top ?? 10);
|
|
1094
|
+
if (top.length > 0) {
|
|
1095
|
+
lines.push("Largest sources:");
|
|
1096
|
+
const width = Math.max(...top.map((entry) => kib(entry.bytes).length));
|
|
1097
|
+
for (const entry of top) lines.push(` ${kib(entry.bytes).padStart(width)} ${percent(entry.bytes / size.totalBytes).padStart(6)} ${entry.name}`);
|
|
1098
|
+
}
|
|
1099
|
+
lines.push("Not measured here: startup time (1 s; `wrangler check startup`) and memory (128 MB per isolate).");
|
|
1100
|
+
return {
|
|
1101
|
+
size,
|
|
1102
|
+
attribution,
|
|
1103
|
+
share,
|
|
1104
|
+
warn,
|
|
1105
|
+
lines
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
/**
|
|
1109
|
+
* Run wrangler's dry run on the assembled output and read what it would
|
|
1110
|
+
* upload. Needs no credentials. The metafile goes to a temporary directory
|
|
1111
|
+
* because wrangler writes it beside the bundle, and the bundle must not land
|
|
1112
|
+
* in the app: `.cloudflare/` is what the deploy reads.
|
|
1113
|
+
*/
|
|
1114
|
+
function reportBundleSize(options) {
|
|
1115
|
+
const root = resolve(options.root);
|
|
1116
|
+
const outDir = mkdtempSync(join(tmpdir(), "guren-cf-size-"));
|
|
1117
|
+
const metafile = join(outDir, "meta.json");
|
|
1118
|
+
try {
|
|
1119
|
+
const result = Bun.spawnSync({
|
|
1120
|
+
cmd: [
|
|
1121
|
+
"bunx",
|
|
1122
|
+
"wrangler",
|
|
1123
|
+
"deploy",
|
|
1124
|
+
"--dry-run",
|
|
1125
|
+
"--outdir",
|
|
1126
|
+
outDir,
|
|
1127
|
+
"--metafile",
|
|
1128
|
+
metafile
|
|
1129
|
+
],
|
|
1130
|
+
cwd: root,
|
|
1131
|
+
stdout: "pipe",
|
|
1132
|
+
stderr: "pipe"
|
|
1133
|
+
});
|
|
1134
|
+
const output = `${result.stdout.toString()}${result.stderr.toString()}`;
|
|
1135
|
+
if (result.exitCode !== 0) throw new Error(`Cloudflare size: wrangler deploy --dry-run exited ${result.exitCode}.\n${output}`);
|
|
1136
|
+
const size = parseWranglerSize(output);
|
|
1137
|
+
if (!size) throw new Error(`Cloudflare size: wrangler printed no "Total Upload" line to read the size from.\n${output}`);
|
|
1138
|
+
return renderBundleReport(size, existsSync(metafile) ? attributeBundle(JSON.parse(readFileSync(metafile, "utf8")), { root }) : [], { top: options.top });
|
|
1139
|
+
} finally {
|
|
1140
|
+
rmSync(outDir, {
|
|
1141
|
+
recursive: true,
|
|
1142
|
+
force: true
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
/** The report on stdout, and the threshold crossing on stderr where a CI log highlights it. */
|
|
1147
|
+
function printBundleReport(report) {
|
|
1148
|
+
for (const line of report.lines) console.log(line);
|
|
1149
|
+
if (report.warn) console.warn(`Cloudflare size: the worker bundle is ${percent(report.share)} of the platform limit. The largest sources above are where to look; generated content belongs in Workers Static Assets, KV or R2 rather than in the bundle.`);
|
|
1150
|
+
}
|
|
1151
|
+
//#endregion
|
|
1152
|
+
export { printBundleReport as a, buildCloudflareOutput as c, parseWranglerSize as i, flattenD1Migrations as l, WORKER_SIZE_WARN_SHARE as n, renderBundleReport as o, attributeBundle as r, reportBundleSize as s, WORKER_SIZE_LIMIT as t };
|
package/dist/commands.d.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
declare const _default: {
|
|
3
3
|
'cloudflare:build': import("citty").CommandDef<{
|
|
4
4
|
root: {
|
|
5
|
-
type:
|
|
6
|
-
description:
|
|
5
|
+
readonly type: 'string';
|
|
6
|
+
readonly description: 'App root directory (defaults to the current working directory)';
|
|
7
7
|
};
|
|
8
8
|
'skip-app-build': {
|
|
9
9
|
type: "boolean";
|
|
@@ -17,6 +17,24 @@ declare const _default: {
|
|
|
17
17
|
type: "string";
|
|
18
18
|
description: string;
|
|
19
19
|
};
|
|
20
|
+
'report-size': {
|
|
21
|
+
type: "boolean";
|
|
22
|
+
description: string;
|
|
23
|
+
};
|
|
24
|
+
top: {
|
|
25
|
+
readonly type: 'string';
|
|
26
|
+
readonly description: 'How many of the largest sources to list (default 10)';
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
'cloudflare:size': import("citty").CommandDef<{
|
|
30
|
+
root: {
|
|
31
|
+
readonly type: 'string';
|
|
32
|
+
readonly description: 'App root directory (defaults to the current working directory)';
|
|
33
|
+
};
|
|
34
|
+
top: {
|
|
35
|
+
readonly type: 'string';
|
|
36
|
+
readonly description: 'How many of the largest sources to list (default 10)';
|
|
37
|
+
};
|
|
20
38
|
}>;
|
|
21
39
|
};
|
|
22
40
|
//#endregion
|
package/dist/commands.js
CHANGED
|
@@ -1,36 +1,74 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as printBundleReport, c as buildCloudflareOutput, s as reportBundleSize } from "./bundle-size-NXkcJkkx.js";
|
|
2
2
|
import { defineCommand } from "citty";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
//#region src/commands.ts
|
|
4
|
+
const rootArg = {
|
|
5
|
+
type: "string",
|
|
6
|
+
description: "App root directory (defaults to the current working directory)"
|
|
7
|
+
};
|
|
8
|
+
const topArg = {
|
|
9
|
+
type: "string",
|
|
10
|
+
description: "How many of the largest sources to list (default 10)"
|
|
11
|
+
};
|
|
12
|
+
function topOf(value) {
|
|
13
|
+
const parsed = Number.parseInt(value ?? "", 10);
|
|
14
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : void 0;
|
|
15
|
+
}
|
|
16
|
+
var commands_default = {
|
|
17
|
+
"cloudflare:build": defineCommand({
|
|
18
|
+
meta: {
|
|
19
|
+
name: "cloudflare:build",
|
|
20
|
+
description: "Assemble a Cloudflare Workers deployment (.cloudflare/) from the app build"
|
|
12
21
|
},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
args: {
|
|
23
|
+
root: rootArg,
|
|
24
|
+
"skip-app-build": {
|
|
25
|
+
type: "boolean",
|
|
26
|
+
description: "Skip running the app's build script before assembling output"
|
|
27
|
+
},
|
|
28
|
+
"mcp-oauth": {
|
|
29
|
+
type: "boolean",
|
|
30
|
+
description: "Front the App MCP endpoint with @cloudflare/workers-oauth-provider, scaffold the consent flow, and make the OAUTH_KV binding build-owned (requires @guren/plugin-mcp)"
|
|
31
|
+
},
|
|
32
|
+
"mcp-path": {
|
|
33
|
+
type: "string",
|
|
34
|
+
description: "Path the App MCP endpoint is mounted at, protected as the OAuth apiRoute (default /mcp; must match mcpPlugin({ path }))"
|
|
35
|
+
},
|
|
36
|
+
"report-size": {
|
|
37
|
+
type: "boolean",
|
|
38
|
+
description: "After assembling, run wrangler's dry run and print the bundle size with its largest sources (what cloudflare:size prints)"
|
|
39
|
+
},
|
|
40
|
+
top: topArg
|
|
16
41
|
},
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
42
|
+
async run({ args }) {
|
|
43
|
+
const rootDir = args.root || process.cwd();
|
|
44
|
+
await buildCloudflareOutput({
|
|
45
|
+
rootDir,
|
|
46
|
+
skipAppBuild: Boolean(args["skip-app-build"]),
|
|
47
|
+
mcpOAuth: Boolean(args["mcp-oauth"]),
|
|
48
|
+
...args["mcp-path"] ? { mcpPath: args["mcp-path"] } : {}
|
|
49
|
+
});
|
|
50
|
+
if (args["report-size"]) printBundleReport(reportBundleSize({
|
|
51
|
+
root: rootDir,
|
|
52
|
+
top: topOf(args.top)
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
}),
|
|
56
|
+
"cloudflare:size": defineCommand({
|
|
57
|
+
meta: {
|
|
58
|
+
name: "cloudflare:size",
|
|
59
|
+
description: "Measure the worker bundle wrangler would upload from .cloudflare/ and attribute it to its largest sources"
|
|
60
|
+
},
|
|
61
|
+
args: {
|
|
62
|
+
root: rootArg,
|
|
63
|
+
top: topArg
|
|
20
64
|
},
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
65
|
+
run({ args }) {
|
|
66
|
+
printBundleReport(reportBundleSize({
|
|
67
|
+
root: args.root || process.cwd(),
|
|
68
|
+
top: topOf(args.top)
|
|
69
|
+
}));
|
|
24
70
|
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
await buildCloudflareOutput({
|
|
28
|
-
rootDir: args.root || process.cwd(),
|
|
29
|
-
skipAppBuild: Boolean(args["skip-app-build"]),
|
|
30
|
-
mcpOAuth: Boolean(args["mcp-oauth"]),
|
|
31
|
-
...args["mcp-path"] ? { mcpPath: args["mcp-path"] } : {}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}) };
|
|
71
|
+
})
|
|
72
|
+
};
|
|
35
73
|
//#endregion
|
|
36
74
|
export { commands_default as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -169,6 +169,73 @@ declare function buildCloudflareOutput(options?: BuildCloudflareOutputOptions):
|
|
|
169
169
|
*/
|
|
170
170
|
declare function flattenD1Migrations(migrationsDir: string, outDir: string): void;
|
|
171
171
|
//#endregion
|
|
172
|
+
//#region src/bundle-size.d.ts
|
|
173
|
+
declare const WORKER_SIZE_LIMIT: {
|
|
174
|
+
readonly bytes: number;
|
|
175
|
+
readonly source: 'https://developers.cloudflare.com/workers/platform/limits/#worker-size';
|
|
176
|
+
readonly confirmedOn: '2026-09-09';
|
|
177
|
+
};
|
|
178
|
+
/** Share of the limit at which the report turns into a warning. */
|
|
179
|
+
declare const WORKER_SIZE_WARN_SHARE = 0.5;
|
|
180
|
+
interface BundleSize {
|
|
181
|
+
totalBytes: number;
|
|
182
|
+
gzipBytes: number | null;
|
|
183
|
+
}
|
|
184
|
+
/** wrangler prints `Total Upload: 4931.18 KiB / gzip: 899.63 KiB`; the unit is its choice. */
|
|
185
|
+
declare function parseWranglerSize(output: string): BundleSize | null;
|
|
186
|
+
/** The part of esbuild's metafile this reads. */
|
|
187
|
+
interface EsbuildMetafile {
|
|
188
|
+
outputs: Record<string, {
|
|
189
|
+
bytes: number;
|
|
190
|
+
inputs: Record<string, {
|
|
191
|
+
bytesInOutput: number;
|
|
192
|
+
}>;
|
|
193
|
+
}>;
|
|
194
|
+
}
|
|
195
|
+
interface BundleAttribution {
|
|
196
|
+
/** A package name, or a path relative to the app root for the app's own files. */
|
|
197
|
+
name: string;
|
|
198
|
+
bytes: number;
|
|
199
|
+
}
|
|
200
|
+
interface AttributeOptions {
|
|
201
|
+
root: string;
|
|
202
|
+
/** Where wrangler ran, which the metafile's input paths are relative to. Defaults to `root`. */
|
|
203
|
+
cwd?: string;
|
|
204
|
+
/** The package an absolute file belongs to, or null for the app's own code. */
|
|
205
|
+
packageNameOf?: (file: string) => string | null;
|
|
206
|
+
}
|
|
207
|
+
/** Every input's bytes in the bundle, summed per package, largest first. */
|
|
208
|
+
declare function attributeBundle(metafile: EsbuildMetafile, options: AttributeOptions): BundleAttribution[];
|
|
209
|
+
interface BundleReport {
|
|
210
|
+
size: BundleSize;
|
|
211
|
+
attribution: BundleAttribution[];
|
|
212
|
+
/** Uncompressed bytes over the platform limit. */
|
|
213
|
+
share: number;
|
|
214
|
+
warn: boolean;
|
|
215
|
+
lines: string[];
|
|
216
|
+
}
|
|
217
|
+
interface RenderOptions {
|
|
218
|
+
/** How many sources to list. Defaults to 10. */
|
|
219
|
+
top?: number;
|
|
220
|
+
limitBytes?: number;
|
|
221
|
+
warnShare?: number;
|
|
222
|
+
}
|
|
223
|
+
declare function renderBundleReport(size: BundleSize, attribution: BundleAttribution[], options?: RenderOptions): BundleReport;
|
|
224
|
+
interface ReportBundleSizeOptions {
|
|
225
|
+
/** The app root holding wrangler.jsonc and the assembled .cloudflare/. */
|
|
226
|
+
root: string;
|
|
227
|
+
top?: number;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Run wrangler's dry run on the assembled output and read what it would
|
|
231
|
+
* upload. Needs no credentials. The metafile goes to a temporary directory
|
|
232
|
+
* because wrangler writes it beside the bundle, and the bundle must not land
|
|
233
|
+
* in the app: `.cloudflare/` is what the deploy reads.
|
|
234
|
+
*/
|
|
235
|
+
declare function reportBundleSize(options: ReportBundleSizeOptions): BundleReport;
|
|
236
|
+
/** The report on stdout, and the threshold crossing on stderr where a CI log highlights it. */
|
|
237
|
+
declare function printBundleReport(report: BundleReport): void;
|
|
238
|
+
//#endregion
|
|
172
239
|
//#region src/storage/R2Driver.d.ts
|
|
173
240
|
/**
|
|
174
241
|
* Structural view of the R2 binding: only what this driver calls, so
|
|
@@ -352,4 +419,4 @@ interface CloudflarePluginConfig {}
|
|
|
352
419
|
/** Register the Cloudflare plugin: `createApp({ providers: [cloudflarePlugin()] })`. */
|
|
353
420
|
declare function cloudflarePlugin(config?: CloudflarePluginConfig): ServiceProviderConstructor;
|
|
354
421
|
//#endregion
|
|
355
|
-
export { type BuildCloudflareOutputOptions, CloudflarePluginConfig, OAUTH_PURGE_BATCH, OAUTH_PURGE_INTERVAL_MS, OAUTH_PURGE_MARKER, type OAuthPurgeOptions, type OAuthPurgeResult, type OAuthPurgerLike, type OAuthSweepEnv, type OAuthSweepKvLike, type R2BucketLike, R2Driver, type R2DriverOptions, type R2PresignOptions, type WorkersAppLike, type WorkersExecutionContext, type WorkersHandler, type WorkersScheduledEvent, bootAndFetch, bootAndRunDueTasks, bootWorkersApp, buildCloudflareOutput, captureWorkersEnv, cloudflarePlugin, createWorkersHandler, flattenD1Migrations, getWorkersEnv, isWorkersRuntime, resetWorkersEnv, sweepOAuthStorage };
|
|
422
|
+
export { type BuildCloudflareOutputOptions, type BundleAttribution, type BundleReport, type BundleSize, CloudflarePluginConfig, type EsbuildMetafile, OAUTH_PURGE_BATCH, OAUTH_PURGE_INTERVAL_MS, OAUTH_PURGE_MARKER, type OAuthPurgeOptions, type OAuthPurgeResult, type OAuthPurgerLike, type OAuthSweepEnv, type OAuthSweepKvLike, type R2BucketLike, R2Driver, type R2DriverOptions, type R2PresignOptions, type ReportBundleSizeOptions, WORKER_SIZE_LIMIT, WORKER_SIZE_WARN_SHARE, type WorkersAppLike, type WorkersExecutionContext, type WorkersHandler, type WorkersScheduledEvent, attributeBundle, bootAndFetch, bootAndRunDueTasks, bootWorkersApp, buildCloudflareOutput, captureWorkersEnv, cloudflarePlugin, createWorkersHandler, flattenD1Migrations, getWorkersEnv, isWorkersRuntime, parseWranglerSize, printBundleReport, renderBundleReport, reportBundleSize, resetWorkersEnv, sweepOAuthStorage };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { captureWorkersEnv, getWorkersEnv, isWorkersRuntime, resetWorkersEnv } from "./env.js";
|
|
2
|
-
import {
|
|
2
|
+
import { a as printBundleReport, c as buildCloudflareOutput, i as parseWranglerSize, l as flattenD1Migrations, n as WORKER_SIZE_WARN_SHARE, o as renderBundleReport, r as attributeBundle, s as reportBundleSize, t as WORKER_SIZE_LIMIT } from "./bundle-size-NXkcJkkx.js";
|
|
3
3
|
import { definePlugin } from "@guren/core";
|
|
4
4
|
//#region src/boot.ts
|
|
5
5
|
const latches = /* @__PURE__ */ new WeakMap();
|
|
@@ -463,4 +463,4 @@ function cloudflarePlugin(config = {}) {
|
|
|
463
463
|
return factory(config);
|
|
464
464
|
}
|
|
465
465
|
//#endregion
|
|
466
|
-
export { OAUTH_PURGE_BATCH, OAUTH_PURGE_INTERVAL_MS, OAUTH_PURGE_MARKER, R2Driver, bootAndFetch, bootAndRunDueTasks, bootWorkersApp, buildCloudflareOutput, captureWorkersEnv, cloudflarePlugin, createWorkersHandler, flattenD1Migrations, getWorkersEnv, isWorkersRuntime, resetWorkersEnv, sweepOAuthStorage };
|
|
466
|
+
export { OAUTH_PURGE_BATCH, OAUTH_PURGE_INTERVAL_MS, OAUTH_PURGE_MARKER, R2Driver, WORKER_SIZE_LIMIT, WORKER_SIZE_WARN_SHARE, attributeBundle, bootAndFetch, bootAndRunDueTasks, bootWorkersApp, buildCloudflareOutput, captureWorkersEnv, cloudflarePlugin, createWorkersHandler, flattenD1Migrations, getWorkersEnv, isWorkersRuntime, parseWranglerSize, printBundleReport, renderBundleReport, reportBundleSize, resetWorkersEnv, sweepOAuthStorage };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guren/plugin-cloudflare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
"commands": {
|
|
21
21
|
"entry": "./dist/commands.js",
|
|
22
22
|
"names": [
|
|
23
|
-
"cloudflare:build"
|
|
23
|
+
"cloudflare:build",
|
|
24
|
+
"cloudflare:size"
|
|
24
25
|
]
|
|
25
26
|
}
|
|
26
27
|
},
|
|
@@ -46,14 +47,14 @@
|
|
|
46
47
|
"typecheck": "tsc --noEmit"
|
|
47
48
|
},
|
|
48
49
|
"dependencies": {
|
|
49
|
-
"@guren/core": "^1.
|
|
50
|
+
"@guren/core": "^1.17.0",
|
|
50
51
|
"citty": "^0.1.6"
|
|
51
52
|
},
|
|
52
53
|
"devDependencies": {
|
|
53
54
|
"@cloudflare/workers-oauth-provider": "^0.10.3",
|
|
54
55
|
"@cloudflare/workers-types": "^4.20260702.1",
|
|
55
56
|
"@types/node": "^20.14.10",
|
|
56
|
-
"hono": "^4.
|
|
57
|
+
"hono": "^4.13.7",
|
|
57
58
|
"miniflare": "5.20260801.1-alpha",
|
|
58
59
|
"tsdown": "^0.22.14",
|
|
59
60
|
"typescript": "^7.0.2"
|
|
@@ -39,6 +39,15 @@ import {
|
|
|
39
39
|
*/
|
|
40
40
|
const LOGIN_PATH = '/login'
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* What a client that sent no `scope` is offered. RFC 6749 §3.3 requires a default
|
|
44
|
+
* or a refusal, and the MCP clients measured (Claude's connector, MCP Inspector)
|
|
45
|
+
* send none with no field to add one. It widens the *offer*, not the grant: the
|
|
46
|
+
* screen leaves write tools unticked, so approving it untouched still grants only
|
|
47
|
+
* the read-only set. Narrow it to `tools:read` to offer reads alone.
|
|
48
|
+
*/
|
|
49
|
+
const DEFAULT_SCOPE = 'tools:*'
|
|
50
|
+
|
|
42
51
|
interface WorkerEnvWithProvider {
|
|
43
52
|
OAUTH_PROVIDER: OAuthHelpers
|
|
44
53
|
}
|
|
@@ -181,14 +190,20 @@ export default class McpOAuthController extends Controller {
|
|
|
181
190
|
return env.OAUTH_PROVIDER
|
|
182
191
|
}
|
|
183
192
|
|
|
184
|
-
/**
|
|
193
|
+
/**
|
|
194
|
+
* Every MCP-exposed tool the requested scopes expand to, in router order; a
|
|
195
|
+
* request carrying no scope at all is offered {@link DEFAULT_SCOPE}. Only an
|
|
196
|
+
* *absent* scope defaults — a scope that expands to nothing stays empty, or a
|
|
197
|
+
* client naming one tool this app does not have would be offered every tool.
|
|
198
|
+
* Both callers go through here, so `approve()` intersects against the same set.
|
|
199
|
+
*/
|
|
185
200
|
private offeredTools(requested: string[]): DerivedAgentTool[] {
|
|
186
201
|
const { tools } = deriveAgentTools(this.make<Application>('app').router.definitions())
|
|
187
202
|
const exposed = tools.filter((tool) => tool.expose.mcp)
|
|
188
203
|
|
|
189
204
|
const allowed = new Set(
|
|
190
205
|
expandToolScopes(
|
|
191
|
-
requested,
|
|
206
|
+
requested.length > 0 ? requested : [DEFAULT_SCOPE],
|
|
192
207
|
exposed.map((tool) => ({ name: tool.toolName, readOnly: tool.annotations.readOnlyHint })),
|
|
193
208
|
),
|
|
194
209
|
)
|
|
@@ -44,6 +44,7 @@ const STYLES = `
|
|
|
44
44
|
.desc { display: block; margin: .25rem 0 0 1.75rem; opacity: .8; }
|
|
45
45
|
.badge { border: 1px solid currentColor; border-radius: .5rem; font-size: .75rem; margin-left: .5rem; padding: 0 .4rem; }
|
|
46
46
|
.empty { opacity: .8; }
|
|
47
|
+
code { font-family: ui-monospace, monospace; font-size: .9em; }
|
|
47
48
|
button { font: inherit; padding: .5rem 1rem; }
|
|
48
49
|
`
|
|
49
50
|
|
|
@@ -72,6 +73,27 @@ const ToolRow: FC<{ tool: DerivedAgentTool }> = ({ tool }) => (
|
|
|
72
73
|
</li>
|
|
73
74
|
)
|
|
74
75
|
|
|
76
|
+
/**
|
|
77
|
+
* The dead end, with the way out: a requested scope that expands to no tool
|
|
78
|
+
* here. The grammar is this application's own (RFC 0016), so naming it is what
|
|
79
|
+
* separates a dead end from something an operator can act on. It says nothing
|
|
80
|
+
* back about what was asked for — `scope` is whatever a stranger put in the
|
|
81
|
+
* link, and here a stranger's sentence would read as the application's own.
|
|
82
|
+
*/
|
|
83
|
+
const NothingToGrant: FC = () => (
|
|
84
|
+
<>
|
|
85
|
+
<p class="empty">
|
|
86
|
+
None of the requested tools are available here, so there is nothing to approve.
|
|
87
|
+
</p>
|
|
88
|
+
<p class="empty">
|
|
89
|
+
This application grants its tools by scope: <code>tools:read</code> for every read-only
|
|
90
|
+
tool, <code>tool:<name></code> for one tool by exact name, or{' '}
|
|
91
|
+
<code>tools:<prefix>.*</code> for a family. Run <code>guren tool:list</code> in the
|
|
92
|
+
application to see the names it exposes.
|
|
93
|
+
</p>
|
|
94
|
+
</>
|
|
95
|
+
)
|
|
96
|
+
|
|
75
97
|
export const McpOAuthConsentPage: FC<McpOAuthConsentPageProps> = ({
|
|
76
98
|
clientName,
|
|
77
99
|
query,
|
|
@@ -97,10 +119,7 @@ export const McpOAuthConsentPage: FC<McpOAuthConsentPageProps> = ({
|
|
|
97
119
|
<input type="hidden" name={CSRF_FORM_FIELD} value={csrfToken} />
|
|
98
120
|
<input type="hidden" name={QUERY_FIELD} value={query} />
|
|
99
121
|
{tools.length === 0 ? (
|
|
100
|
-
<
|
|
101
|
-
This application requested no tools it can be granted. Nothing here would give it
|
|
102
|
-
access, so there is nothing to approve.
|
|
103
|
-
</p>
|
|
122
|
+
<NothingToGrant />
|
|
104
123
|
) : (
|
|
105
124
|
<ul class="tools">
|
|
106
125
|
{tools.map((tool) => (
|