@guren/plugin-cloudflare 0.11.0 → 0.12.1
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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
|
-
import { AGENTS_CONFIG_FILE, DATABASE_FACTORIES, DEV_ONLY_MODULES, DOCUMENT_ASSET_EXTENSIONS, DOCUMENT_ASSET_HEADERS, MCP_PLUGIN_PACKAGE,
|
|
4
|
+
import { AGENTS_CONFIG_FILE, DATABASE_FACTORIES, DEV_ONLY_MODULES, DOCUMENT_ASSET_EXTENSIONS, DOCUMENT_ASSET_HEADERS, MCP_PLUGIN_PACKAGE, SQL_CLIENT_MODULES, assertOutputDirOutsideRoot, bundledRuntimeEnv, detectDatabaseDialects, importSpecifier, renderDevOnlyStub, resetOutputDir, resolveClientAssetEnv, resolvePathLike, resolveSsrEntryFile, stageStaticAssets } from "@guren/core/internal/deploy-build";
|
|
5
5
|
import { reportDeployRuntimeHazards } from "@guren/core/internal/deploy-check";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
7
|
//#region src/templates.ts
|
|
@@ -55,12 +55,10 @@ async function buildCloudflareOutput(options = {}) {
|
|
|
55
55
|
assertOutputDirOutsideRoot(out, root, "Cloudflare build");
|
|
56
56
|
assertWranglerJsoncIsAuthoritative(root);
|
|
57
57
|
const packageJson = readPackageJson(root);
|
|
58
|
-
const mcpPlugin = appUsesMcpPlugin(root);
|
|
59
58
|
const mcpOAuth = options.mcpOAuth === true;
|
|
60
59
|
const mcpPath = options.mcpPath ?? DEFAULT_MCP_PATH;
|
|
61
|
-
assertMcpTransportNotAliased(root, mcpPlugin);
|
|
62
60
|
if (mcpOAuth) {
|
|
63
|
-
assertMcpOAuthUsable(root
|
|
61
|
+
assertMcpOAuthUsable(root);
|
|
64
62
|
assertOAuthKvBound(root);
|
|
65
63
|
}
|
|
66
64
|
const hostsAgents = existsSync(resolve(root, AGENTS_CONFIG_FILE));
|
|
@@ -100,9 +98,8 @@ async function buildCloudflareOutput(options = {}) {
|
|
|
100
98
|
flattenD1Migrations(resolve(root, "db/migrations"), resolve(out, "d1-migrations"));
|
|
101
99
|
writeDevOnlyStubs(out);
|
|
102
100
|
if (mcpOAuth) scaffoldConsentFlow(root);
|
|
103
|
-
scaffoldWranglerConfig(root, out, packageJson.name,
|
|
101
|
+
scaffoldWranglerConfig(root, out, packageJson.name, mcpOAuth, agents.exports);
|
|
104
102
|
}
|
|
105
|
-
const MCP_UNAVAILABLE = "The MCP endpoint is unavailable on Cloudflare Workers — it generates files on disk.";
|
|
106
103
|
/** `mcpPlugin()`'s own default mount path — see `BuildCloudflareOutputOptions.mcpPath`. */
|
|
107
104
|
const DEFAULT_MCP_PATH = "/mcp";
|
|
108
105
|
/** The OAuth provider package the generated worker imports, installed by the app. */
|
|
@@ -124,8 +121,8 @@ const OAUTH_ENDPOINTS = {
|
|
|
124
121
|
* `@cloudflare/workers-oauth-provider`, which wrangler resolves from the *app's*
|
|
125
122
|
* `node_modules` at `wrangler deploy`, from a production install — not devDeps.
|
|
126
123
|
*/
|
|
127
|
-
function assertMcpOAuthUsable(root
|
|
128
|
-
if (!
|
|
124
|
+
function assertMcpOAuthUsable(root) {
|
|
125
|
+
if (!appDependsOn(root, MCP_PLUGIN_PACKAGE)) throw new Error(`Cloudflare build: --mcp-oauth fronts the App MCP endpoint with an OAuth provider, but this app does not depend on ${MCP_PLUGIN_PACKAGE}, so it serves no such endpoint. Install and mount the plugin first:\n bun add ${MCP_PLUGIN_PACKAGE}\n # then add mcpPlugin() to createApp({ providers })
|
|
129
126
|
Or drop --mcp-oauth to build the worker without the OAuth wrapping.`);
|
|
130
127
|
if (!appDependsOn(root, OAUTH_PROVIDER_PACKAGE)) throw new Error(`Cloudflare build: --mcp-oauth generates a worker that imports ${OAUTH_PROVIDER_PACKAGE}, which this app does not depend on. Install it:\n bun add ${OAUTH_PROVIDER_PACKAGE}\nIt is not a dependency of @guren/plugin-cloudflare on purpose — only apps fronting the MCP endpoint with OAuth need it. A devDependency will not do: wrangler resolves the import at deploy time, from a production install.`);
|
|
131
128
|
}
|
|
@@ -165,9 +162,8 @@ function oauthKvNamespace() {
|
|
|
165
162
|
};
|
|
166
163
|
}
|
|
167
164
|
/**
|
|
168
|
-
* Whether the app declares `name` under `dependencies
|
|
169
|
-
*
|
|
170
|
-
* unreadable or malformed manifest answers `false`.
|
|
165
|
+
* Whether the app declares `name` under `dependencies` (devDependencies do not
|
|
166
|
+
* ship). An absent, unreadable or malformed manifest answers `false`.
|
|
171
167
|
*/
|
|
172
168
|
function appDependsOn(root, name) {
|
|
173
169
|
try {
|
|
@@ -436,7 +432,7 @@ const STUBBED_MODULES = [...DEV_ONLY_MODULES, ...SQL_CLIENT_MODULES];
|
|
|
436
432
|
const UNAVAILABLE_ON_WORKERS = {
|
|
437
433
|
sqlite: "bun:sqlite is unavailable on Cloudflare Workers — use createD1Database().",
|
|
438
434
|
vite: "The Vite dev server is unavailable on Cloudflare Workers — assets are served by Workers Static Assets.",
|
|
439
|
-
mcp:
|
|
435
|
+
mcp: "The Dev MCP endpoint and docs viewer (@guren/cli) are unavailable on Cloudflare Workers — they read and generate files on disk.",
|
|
440
436
|
"sql-driver": "This database client is unavailable on Cloudflare Workers — use createD1Database(). It is stubbed because @guren/orm names it in a dynamic import that bundlers follow even when the branch cannot be taken."
|
|
441
437
|
};
|
|
442
438
|
/**
|
|
@@ -450,8 +446,6 @@ const STUB_FILES = {
|
|
|
450
446
|
"bun:sqlite": "stub-bun-sqlite.js",
|
|
451
447
|
vite: "stub-vite.js",
|
|
452
448
|
"@guren/cli": "stub-guren-cli.js",
|
|
453
|
-
"@modelcontextprotocol/sdk/server/mcp.js": "stub-mcp-server.js",
|
|
454
|
-
"@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js": "stub-mcp-transport.js",
|
|
455
449
|
postgres: "stub-postgres.js",
|
|
456
450
|
mysql2: "stub-mysql2.js",
|
|
457
451
|
"mysql2/promise": "stub-mysql2-promise.js",
|
|
@@ -462,30 +456,10 @@ function writeDevOnlyStubs(out) {
|
|
|
462
456
|
}
|
|
463
457
|
/**
|
|
464
458
|
* A package-name alias does not cover subpaths and wrangler cannot match a
|
|
465
|
-
* prefix, so every stubbed specifier needs its own entry
|
|
466
|
-
* upstream needs a new `DEV_ONLY_MODULES` entry). `mcpPlugin` drops only the App
|
|
467
|
-
* MCP transport's alias (RFC 0016 §7; the adapter is workerd-compatible). Stub
|
|
468
|
-
* *files* are written unconditionally, so a config still pointing at one keeps finding it.
|
|
459
|
+
* prefix, so every stubbed specifier needs its own entry.
|
|
469
460
|
*/
|
|
470
|
-
function devOnlyAliases(outRelative
|
|
471
|
-
|
|
472
|
-
return Object.fromEntries(stubbed.map((module) => [module.specifier, `./${outRelative}/${STUB_FILES[module.specifier]}`]));
|
|
473
|
-
}
|
|
474
|
-
/**
|
|
475
|
-
* Fail rather than deploy an app declaring `@guren/plugin-mcp` while its committed
|
|
476
|
-
* `wrangler.jsonc` aliases the App MCP transport to a stub *this build generated*:
|
|
477
|
-
* the endpoint stays compiled shut with every gate green. The *value* decides
|
|
478
|
-
* (another target is a deliberate override), matched on the last path segment of
|
|
479
|
-
* either separator against `STUB_FILES`; `parseJsonc` keeps comments from matching.
|
|
480
|
-
*/
|
|
481
|
-
function assertMcpTransportNotAliased(root, mcpPlugin) {
|
|
482
|
-
if (!mcpPlugin) return;
|
|
483
|
-
const configPath = resolve(root, "wrangler.jsonc");
|
|
484
|
-
const alias = readWranglerConfig(configPath)?.alias;
|
|
485
|
-
if (!isRecord(alias)) return;
|
|
486
|
-
const target = alias[MCP_TRANSPORT_SPECIFIER];
|
|
487
|
-
if (typeof target !== "string" || target.split(/[\\/]/).pop() !== STUB_FILES[MCP_TRANSPORT_SPECIFIER]) return;
|
|
488
|
-
throw new Error(`Cloudflare build: ${configPath} aliases the App MCP transport to a stub, but this app depends on ${MCP_PLUGIN_PACKAGE} — the endpoint would deploy compiled shut. Delete this one line from "alias":\n ${JSON.stringify(MCP_TRANSPORT_SPECIFIER)}: ${JSON.stringify(target)}\nLeave every other alias entry in place; ${JSON.stringify("@modelcontextprotocol/sdk/server/mcp.js")} in particular must stay stubbed — that is the dev-only MCP server, which generates files on disk.`);
|
|
461
|
+
function devOnlyAliases(outRelative) {
|
|
462
|
+
return Object.fromEntries(STUBBED_MODULES.map((module) => [module.specifier, `./${outRelative}/${STUB_FILES[module.specifier]}`]));
|
|
489
463
|
}
|
|
490
464
|
/**
|
|
491
465
|
* Wrangler's `migrations_dir` only discovers flat `*.sql` files, but drizzle-kit
|
|
@@ -815,7 +789,7 @@ function renderOAuthProvider(mcpPath, defaultEntry) {
|
|
|
815
789
|
resourceMetadata: { scopes_supported: ['tools:read'] },
|
|
816
790
|
})`;
|
|
817
791
|
}
|
|
818
|
-
function scaffoldWranglerConfig(root, out, packageName,
|
|
792
|
+
function scaffoldWranglerConfig(root, out, packageName, mcpOAuth, agents) {
|
|
819
793
|
const configPath = resolve(root, "wrangler.jsonc");
|
|
820
794
|
const appName = (packageName ?? "guren-app").replace(/^@[^/]+\//, "");
|
|
821
795
|
const outRelative = relative(root, out).split(sep).join("/");
|
|
@@ -824,7 +798,7 @@ function scaffoldWranglerConfig(root, out, packageName, mcpPlugin, mcpOAuth, age
|
|
|
824
798
|
main: `${outRelative}/worker.js`,
|
|
825
799
|
compatibility_date: (/* @__PURE__ */ new Date()).toISOString().slice(0, 10),
|
|
826
800
|
compatibility_flags: ["nodejs_compat"],
|
|
827
|
-
alias: devOnlyAliases(outRelative
|
|
801
|
+
alias: devOnlyAliases(outRelative),
|
|
828
802
|
define: {
|
|
829
803
|
"process.env.NODE_ENV": "\"production\"",
|
|
830
804
|
"import.meta.url": "\"file:///worker.js\""
|
|
@@ -856,7 +830,7 @@ function scaffoldWranglerConfig(root, out, packageName, mcpPlugin, mcpOAuth, age
|
|
|
856
830
|
writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`, { flag: "wx" });
|
|
857
831
|
} catch (error) {
|
|
858
832
|
if (error.code === "EEXIST") {
|
|
859
|
-
warnMissingBuildOwnedKeys(configPath, outRelative,
|
|
833
|
+
warnMissingBuildOwnedKeys(configPath, outRelative, mcpOAuth);
|
|
860
834
|
return;
|
|
861
835
|
}
|
|
862
836
|
throw error;
|
|
@@ -954,7 +928,7 @@ function parseJsonc(text) {
|
|
|
954
928
|
* `bun:sqlite` or never applies its migrations. Named per entry, never as a whole
|
|
955
929
|
* `"alias"`/`"define"` object, which reads as one to paste over the app's own entries.
|
|
956
930
|
*/
|
|
957
|
-
function warnMissingBuildOwnedKeys(configPath, outRelative,
|
|
931
|
+
function warnMissingBuildOwnedKeys(configPath, outRelative, mcpOAuth) {
|
|
958
932
|
const config = readWranglerConfig(configPath);
|
|
959
933
|
if (!config) {
|
|
960
934
|
console.warn(`Cloudflare build: could not parse ${configPath}, so its build-owned keys went unchecked. Fix the file, or compare it against a config scaffolded in an empty directory.`);
|
|
@@ -962,7 +936,7 @@ function warnMissingBuildOwnedKeys(configPath, outRelative, mcpPlugin, mcpOAuth)
|
|
|
962
936
|
}
|
|
963
937
|
const missing = [];
|
|
964
938
|
const alias = isRecord(config.alias) ? config.alias : {};
|
|
965
|
-
for (const [specifier, target] of Object.entries(devOnlyAliases(outRelative
|
|
939
|
+
for (const [specifier, target] of Object.entries(devOnlyAliases(outRelative))) if (!(specifier in alias)) missing.push(`${JSON.stringify(specifier)}: ${JSON.stringify(target)} (inside "alias")`);
|
|
966
940
|
if (!config.define?.["process.env.NODE_ENV"]) missing.push("\"process.env.NODE_ENV\": \"\\\"production\\\"\" (inside \"define\")");
|
|
967
941
|
const d1 = config.d1_databases?.[0];
|
|
968
942
|
if (d1 && d1.migrations_dir !== `${outRelative}/d1-migrations`) missing.push(`"migrations_dir": "${outRelative}/d1-migrations" (inside d1_databases[0])`);
|
package/dist/commands.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as printBundleReport, c as buildCloudflareOutput, s as reportBundleSize } from "./bundle-size-
|
|
1
|
+
import { a as printBundleReport, c as buildCloudflareOutput, s as reportBundleSize } from "./bundle-size-i1kLeIiR.js";
|
|
2
2
|
import { defineCommand } from "citty";
|
|
3
3
|
//#region src/commands.ts
|
|
4
4
|
const rootArg = {
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { captureWorkersEnv, getWorkersEnv, isWorkersRuntime, resetWorkersEnv } from "./env.js";
|
|
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-
|
|
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-i1kLeIiR.js";
|
|
3
3
|
import { definePlugin } from "@guren/core";
|
|
4
4
|
//#region src/boot.ts
|
|
5
5
|
const latches = /* @__PURE__ */ new WeakMap();
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guren/plugin-cloudflare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
|
+
"description": "Deploy a Guren application to Cloudflare Workers, with D1 as the database.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"guren",
|
|
7
|
+
"bun",
|
|
8
|
+
"typescript",
|
|
9
|
+
"fullstack",
|
|
10
|
+
"framework",
|
|
11
|
+
"cloudflare",
|
|
12
|
+
"cloudflare-workers",
|
|
13
|
+
"d1",
|
|
14
|
+
"deploy"
|
|
15
|
+
],
|
|
4
16
|
"type": "module",
|
|
5
17
|
"license": "MIT",
|
|
6
18
|
"repository": {
|
|
@@ -47,7 +59,7 @@
|
|
|
47
59
|
"typecheck": "tsc --noEmit"
|
|
48
60
|
},
|
|
49
61
|
"dependencies": {
|
|
50
|
-
"@guren/core": "^1.
|
|
62
|
+
"@guren/core": "^1.20.1",
|
|
51
63
|
"citty": "^0.1.6"
|
|
52
64
|
},
|
|
53
65
|
"devDependencies": {
|