@aihu/adapter-cloudflare 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fellwork
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # @aihu/adapter-cloudflare
2
+
3
+ > Cloudflare Workers/Pages deployment adapter for @aihu/app.
4
+
5
+ Part of the [aihu](https://github.com/fellwork/aihu) framework — agentic discovery and interaction, for human purpose.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @aihu/adapter-cloudflare
11
+ # or
12
+ bun add @aihu/adapter-cloudflare
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ // vite.config.ts
19
+ import { defineConfig } from 'vite';
20
+ import { aihu } from '@aihu/app';
21
+ import cloudflare from '@aihu/adapter-cloudflare';
22
+
23
+ export default defineConfig({ plugins: [aihu({ adapter: cloudflare() })] });
24
+ ```
25
+
26
+ ## Status
27
+
28
+ Early access (`0.1.x`). API may evolve before v1.1 GA. See the [v1.1 roadmap](https://github.com/fellwork/aihu/tree/main/docs/roadmap) for stability commitments.
29
+
30
+ ## License
31
+
32
+ MIT — see [LICENSE](https://github.com/fellwork/aihu/blob/main/LICENSE).
@@ -0,0 +1,42 @@
1
+ import { AihuAdapter } from "@aihu/app";
2
+
3
+ //#region src/index.d.ts
4
+ interface CloudflareAdapterOptions {
5
+ /**
6
+ * Cloudflare Worker name. Used in generated wrangler.toml.
7
+ * Default: value of "name" in the project's package.json, or 'aihu-app'.
8
+ */
9
+ name?: string;
10
+ /**
11
+ * Deployment mode.
12
+ * - 'workers': Cloudflare Workers with static assets via env.ASSETS binding
13
+ * - 'pages': Cloudflare Pages (_worker.js convention — same output, different deploy target)
14
+ * Default: 'workers'
15
+ */
16
+ mode?: 'workers' | 'pages';
17
+ /**
18
+ * Whether to write wrangler.toml in the project root if it does not exist.
19
+ * Never overwrites an existing wrangler.toml.
20
+ * Default: true
21
+ */
22
+ generateWrangler?: boolean;
23
+ }
24
+ /**
25
+ * Cloudflare Workers / Pages adapter for aihu.
26
+ *
27
+ * Writes `_worker.js` to the Vite output directory and optionally creates
28
+ * `wrangler.toml` in the project root (if absent).
29
+ *
30
+ * @example
31
+ * // aihu.config.ts
32
+ * import { defineConfig } from '@aihu/app'
33
+ * import { cloudflare } from '@aihu/adapter-cloudflare'
34
+ *
35
+ * export default defineConfig({
36
+ * adapter: cloudflare({ name: 'my-worker' }),
37
+ * })
38
+ */
39
+ declare function cloudflare(options?: CloudflareAdapterOptions): AihuAdapter;
40
+ //#endregion
41
+ export { CloudflareAdapterOptions, cloudflare };
42
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;UAIiB,wBAAA;;AAAjB;;;EAKE,IAAA;EAAA;;;;;AAmFF;EA5EE,IAAA;;;;;;EAMA,gBAAA;AAAA;;;;;;;;;;;;;;;;iBAsEc,UAAA,CAAW,OAAA,GAAU,wBAAA,GAA2B,WAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import{existsSync as e,readFileSync as t}from"node:fs";import{resolve as n}from"node:path";function r(r){let i=n(r,`package.json`);if(!e(i))return null;try{let e=JSON.parse(t(i,`utf8`));return typeof e.name==`string`?e.name:null}catch{return null}}function i(){return[`// AUTO-GENERATED by @aihu/adapter-cloudflare — do not edit`,`// Cloudflare Workers entry — SPA mode`,`//`,`// All page requests are served from the ASSETS binding (Cloudflare CDN).`,`// For SSR mode, upgrade to @aihu/adapter-cloudflare with ssr: true (V1+).`,``,`export default {`,` async fetch(request, env, _ctx) {`,` try {`,` return await env.ASSETS.fetch(request)`,` } catch {`,` // Fallback to index.html for client-side routing (404 from ASSETS).`,` const url = new URL(request.url)`,` const indexUrl = new URL('/index.html', url.origin)`,` return env.ASSETS.fetch(new Request(indexUrl, request))`,` }`,` },`,`}`].join(`
2
+ `)}function a(e){return[`# AUTO-GENERATED by @aihu/adapter-cloudflare`,`# Edit this file to customize your Worker configuration.`,`# Reference: https://developers.cloudflare.com/workers/wrangler/configuration/`,``,`name = "${e}"`,`main = "_worker.js"`,`compatibility_date = "2024-01-01"`,``,`[assets]`,`directory = "."`,`binding = "ASSETS"`].join(`
3
+ `)+`
4
+ `}function o(t){return{name:`cloudflare`,async adapt(o){let s=t?.name??r(o.root)??`aihu-app`,c=i();await o.emitFile(`_worker.js`,c),console.log(`[@aihu/adapter-cloudflare] Wrote _worker.js (SPA mode)`);let l=n(o.root,`wrangler.toml`);if(t?.generateWrangler!==!1&&!e(l)){let e=a(s);await o.writeFile(l,e),console.log(`[@aihu/adapter-cloudflare] Created wrangler.toml (name = "${s}")`)}console.log(`[@aihu/adapter-cloudflare] Build complete → ${o.outDir}`),console.log(`[@aihu/adapter-cloudflare] Deploy with: wrangler deploy --config wrangler.toml`)}}}export{o as cloudflare};
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["resolvePath"],"sources":["../src/index.ts"],"sourcesContent":["import { existsSync, readFileSync } from 'node:fs'\nimport { resolve as resolvePath } from 'node:path'\nimport type { AdapterContext, AihuAdapter } from '@aihu/app'\n\nexport interface CloudflareAdapterOptions {\n /**\n * Cloudflare Worker name. Used in generated wrangler.toml.\n * Default: value of \"name\" in the project's package.json, or 'aihu-app'.\n */\n name?: string\n /**\n * Deployment mode.\n * - 'workers': Cloudflare Workers with static assets via env.ASSETS binding\n * - 'pages': Cloudflare Pages (_worker.js convention — same output, different deploy target)\n * Default: 'workers'\n */\n mode?: 'workers' | 'pages'\n /**\n * Whether to write wrangler.toml in the project root if it does not exist.\n * Never overwrites an existing wrangler.toml.\n * Default: true\n */\n generateWrangler?: boolean\n}\n\nfunction readPackageJsonName(root: string): string | null {\n const pkgPath = resolvePath(root, 'package.json')\n if (!existsSync(pkgPath)) return null\n try {\n const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as { name?: string }\n return typeof pkg.name === 'string' ? pkg.name : null\n } catch {\n return null\n }\n}\n\nfunction generateWorkerEntry(): string {\n return [\n '// AUTO-GENERATED by @aihu/adapter-cloudflare — do not edit',\n '// Cloudflare Workers entry — SPA mode',\n '//',\n '// All page requests are served from the ASSETS binding (Cloudflare CDN).',\n '// For SSR mode, upgrade to @aihu/adapter-cloudflare with ssr: true (V1+).',\n '',\n 'export default {',\n ' async fetch(request, env, _ctx) {',\n ' try {',\n ' return await env.ASSETS.fetch(request)',\n ' } catch {',\n ' // Fallback to index.html for client-side routing (404 from ASSETS).',\n ' const url = new URL(request.url)',\n \" const indexUrl = new URL('/index.html', url.origin)\",\n ' return env.ASSETS.fetch(new Request(indexUrl, request))',\n ' }',\n ' },',\n '}',\n ].join('\\n')\n}\n\nfunction generateWranglerToml(name: string): string {\n return (\n [\n '# AUTO-GENERATED by @aihu/adapter-cloudflare',\n '# Edit this file to customize your Worker configuration.',\n '# Reference: https://developers.cloudflare.com/workers/wrangler/configuration/',\n '',\n `name = \"${name}\"`,\n 'main = \"_worker.js\"',\n 'compatibility_date = \"2024-01-01\"',\n '',\n '[assets]',\n 'directory = \".\"',\n 'binding = \"ASSETS\"',\n ].join('\\n') + '\\n'\n )\n}\n\n/**\n * Cloudflare Workers / Pages adapter for aihu.\n *\n * Writes `_worker.js` to the Vite output directory and optionally creates\n * `wrangler.toml` in the project root (if absent).\n *\n * @example\n * // aihu.config.ts\n * import { defineConfig } from '@aihu/app'\n * import { cloudflare } from '@aihu/adapter-cloudflare'\n *\n * export default defineConfig({\n * adapter: cloudflare({ name: 'my-worker' }),\n * })\n */\nexport function cloudflare(options?: CloudflareAdapterOptions): AihuAdapter {\n return {\n name: 'cloudflare',\n\n async adapt(context: AdapterContext): Promise<void> {\n const workerName = options?.name ?? readPackageJsonName(context.root) ?? 'aihu-app'\n\n // Step 1 — Write _worker.js\n const workerEntry = generateWorkerEntry()\n await context.emitFile('_worker.js', workerEntry)\n console.log(`[@aihu/adapter-cloudflare] Wrote _worker.js (SPA mode)`)\n\n // Step 2 — Write wrangler.toml (only if absent, never overwrites)\n const wranglerPath = resolvePath(context.root, 'wrangler.toml')\n if (options?.generateWrangler !== false && !existsSync(wranglerPath)) {\n const toml = generateWranglerToml(workerName)\n await context.writeFile(wranglerPath, toml)\n console.log(`[@aihu/adapter-cloudflare] Created wrangler.toml (name = \"${workerName}\")`)\n }\n\n console.log(`[@aihu/adapter-cloudflare] Build complete → ${context.outDir}`)\n console.log(`[@aihu/adapter-cloudflare] Deploy with: wrangler deploy --config wrangler.toml`)\n },\n }\n}\n"],"mappings":"2FAyBA,SAAS,EAAoB,EAA6B,CACxD,IAAM,EAAUA,EAAY,EAAM,eAAe,CACjD,GAAI,CAAC,EAAW,EAAQ,CAAE,OAAO,KACjC,GAAI,CACF,IAAM,EAAM,KAAK,MAAM,EAAa,EAAS,OAAO,CAAC,CACrD,OAAO,OAAO,EAAI,MAAS,SAAW,EAAI,KAAO,UAC3C,CACN,OAAO,MAIX,SAAS,GAA8B,CACrC,MAAO,CACL,8DACA,yCACA,KACA,4EACA,6EACA,GACA,mBACA,sCACA,YACA,+CACA,gBACA,6EACA,yCACA,4DACA,gEACA,QACA,OACA,IACD,CAAC,KAAK;EAAK,CAGd,SAAS,EAAqB,EAAsB,CAClD,MACE,CACE,+CACA,2DACA,iFACA,GACA,WAAW,EAAK,GAChB,sBACA,oCACA,GACA,WACA,kBACA,qBACD,CAAC,KAAK;EAAK,CAAG;EAmBnB,SAAgB,EAAW,EAAiD,CAC1E,MAAO,CACL,KAAM,aAEN,MAAM,MAAM,EAAwC,CAClD,IAAM,EAAa,GAAS,MAAQ,EAAoB,EAAQ,KAAK,EAAI,WAGnE,EAAc,GAAqB,CACzC,MAAM,EAAQ,SAAS,aAAc,EAAY,CACjD,QAAQ,IAAI,yDAAyD,CAGrE,IAAM,EAAeA,EAAY,EAAQ,KAAM,gBAAgB,CAC/D,GAAI,GAAS,mBAAqB,IAAS,CAAC,EAAW,EAAa,CAAE,CACpE,IAAM,EAAO,EAAqB,EAAW,CAC7C,MAAM,EAAQ,UAAU,EAAc,EAAK,CAC3C,QAAQ,IAAI,6DAA6D,EAAW,IAAI,CAG1F,QAAQ,IAAI,+CAA+C,EAAQ,SAAS,CAC5E,QAAQ,IAAI,iFAAiF,EAEhG"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@aihu/adapter-cloudflare",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "sideEffects": false,
21
+ "scripts": {
22
+ "build": "rolldown -c",
23
+ "typecheck": "tsc --noEmit",
24
+ "test": "vitest run",
25
+ "prepublishOnly": "bun run build"
26
+ },
27
+ "peerDependencies": {
28
+ "@aihu/app": "workspace:*",
29
+ "vite": ">=5.0.0"
30
+ },
31
+ "description": "Cloudflare Workers/Pages deployment adapter for @aihu/app.",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/fellwork/aihu.git",
35
+ "directory": "packages/adapter-cloudflare"
36
+ },
37
+ "homepage": "https://github.com/fellwork/aihu/tree/main/packages/adapter-cloudflare#readme",
38
+ "bugs": "https://github.com/fellwork/aihu/issues",
39
+ "publishConfig": {
40
+ "access": "public"
41
+ }
42
+ }