@guren/plugin-cloudflare 0.5.0 → 0.7.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 +6 -4
- package/dist/build-DLJgpyN5.js +950 -0
- package/dist/commands.d.ts +8 -0
- package/dist/commands.js +12 -2
- package/dist/env.d.ts +35 -0
- package/dist/env.js +45 -0
- package/dist/index.d.ts +60 -48
- package/dist/index.js +60 -56
- package/package.json +10 -3
- package/templates/mcp-oauth/app/Http/Controllers/McpOAuthController.ts +220 -0
- package/templates/mcp-oauth/app/View/McpOAuthConsentPage.tsx +117 -0
- package/templates/mcp-oauth/app/View/McpOAuthErrorPage.tsx +45 -0
- package/templates/mcp-oauth/routes/mcp-oauth.ts +20 -0
- package/dist/build-B7zQ13ai.js +0 -321
package/dist/commands.d.ts
CHANGED
package/dist/commands.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as buildCloudflareOutput } from "./build-
|
|
1
|
+
import { t as buildCloudflareOutput } from "./build-DLJgpyN5.js";
|
|
2
2
|
import { defineCommand } from "citty";
|
|
3
3
|
var commands_default = { "cloudflare:build": defineCommand({
|
|
4
4
|
meta: {
|
|
@@ -13,12 +13,22 @@ var commands_default = { "cloudflare:build": defineCommand({
|
|
|
13
13
|
"skip-app-build": {
|
|
14
14
|
type: "boolean",
|
|
15
15
|
description: "Skip running the app's build script before assembling output"
|
|
16
|
+
},
|
|
17
|
+
"mcp-oauth": {
|
|
18
|
+
type: "boolean",
|
|
19
|
+
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)"
|
|
20
|
+
},
|
|
21
|
+
"mcp-path": {
|
|
22
|
+
type: "string",
|
|
23
|
+
description: "Path the App MCP endpoint is mounted at, protected as the OAuth apiRoute (default /mcp; must match mcpPlugin({ path }))"
|
|
16
24
|
}
|
|
17
25
|
},
|
|
18
26
|
async run({ args }) {
|
|
19
27
|
await buildCloudflareOutput({
|
|
20
28
|
rootDir: args.root || process.cwd(),
|
|
21
|
-
skipAppBuild: Boolean(args["skip-app-build"])
|
|
29
|
+
skipAppBuild: Boolean(args["skip-app-build"]),
|
|
30
|
+
mcpOAuth: Boolean(args["mcp-oauth"]),
|
|
31
|
+
...args["mcp-path"] ? { mcpPath: args["mcp-path"] } : {}
|
|
22
32
|
});
|
|
23
33
|
}
|
|
24
34
|
}) };
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region src/env.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The Workers env holder, published as `@guren/plugin-cloudflare/env`.
|
|
4
|
+
*
|
|
5
|
+
* **This module must stay import-free.** It is what application code reaches for
|
|
6
|
+
* at runtime, while the package root also re-exports `buildCloudflareOutput` and
|
|
7
|
+
* pulls `node:fs`, `node:path` and the deploy generator behind it — importing
|
|
8
|
+
* these names from the root drags that into the route graph and the wrangler
|
|
9
|
+
* bundle. The root keeps re-exporting them for compatibility;
|
|
10
|
+
* `tests/lean-env-subpath.test.ts` holds the boundary.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* First call wins; later calls are ignored (never overwrite a live env). Not the
|
|
14
|
+
* hard error RFC 0017 §6 asks for: under the Workers Vitest integration a Worker
|
|
15
|
+
* entrypoint and a Durable Object of one deployment receive *different* `env`
|
|
16
|
+
* objects, so identity cannot mean "another environment". The per-app boot
|
|
17
|
+
* latch in `boot.ts` is what keeps one isolate on one application.
|
|
18
|
+
*/
|
|
19
|
+
declare function captureWorkersEnv(env: unknown): void;
|
|
20
|
+
/**
|
|
21
|
+
* Throws with a clear message if called before the first request captured env.
|
|
22
|
+
* Inside a Durable Object prefer `this.env`: the holder keeps whichever
|
|
23
|
+
* entrypoint booted first, which may be the Worker's rather than the object's.
|
|
24
|
+
*/
|
|
25
|
+
declare function getWorkersEnv<E = unknown>(): E;
|
|
26
|
+
/** Clears the holder. Used by createWorkersHandler on boot failure and by tests. */
|
|
27
|
+
declare function resetWorkersEnv(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Whether this code is running on workerd, which identifies itself through the
|
|
30
|
+
* standard navigator user agent. The switch an app's `config/database.ts` needs
|
|
31
|
+
* to pick D1 over a local driver, and to keep filesystem work out of the Worker.
|
|
32
|
+
*/
|
|
33
|
+
declare function isWorkersRuntime(): boolean;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { captureWorkersEnv, getWorkersEnv, isWorkersRuntime, resetWorkersEnv };
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/env.ts
|
|
2
|
+
/**
|
|
3
|
+
* The Workers env holder, published as `@guren/plugin-cloudflare/env`.
|
|
4
|
+
*
|
|
5
|
+
* **This module must stay import-free.** It is what application code reaches for
|
|
6
|
+
* at runtime, while the package root also re-exports `buildCloudflareOutput` and
|
|
7
|
+
* pulls `node:fs`, `node:path` and the deploy generator behind it — importing
|
|
8
|
+
* these names from the root drags that into the route graph and the wrangler
|
|
9
|
+
* bundle. The root keeps re-exporting them for compatibility;
|
|
10
|
+
* `tests/lean-env-subpath.test.ts` holds the boundary.
|
|
11
|
+
*/
|
|
12
|
+
let holder;
|
|
13
|
+
/**
|
|
14
|
+
* First call wins; later calls are ignored (never overwrite a live env). Not the
|
|
15
|
+
* hard error RFC 0017 §6 asks for: under the Workers Vitest integration a Worker
|
|
16
|
+
* entrypoint and a Durable Object of one deployment receive *different* `env`
|
|
17
|
+
* objects, so identity cannot mean "another environment". The per-app boot
|
|
18
|
+
* latch in `boot.ts` is what keeps one isolate on one application.
|
|
19
|
+
*/
|
|
20
|
+
function captureWorkersEnv(env) {
|
|
21
|
+
holder ??= { env };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Throws with a clear message if called before the first request captured env.
|
|
25
|
+
* Inside a Durable Object prefer `this.env`: the holder keeps whichever
|
|
26
|
+
* entrypoint booted first, which may be the Worker's rather than the object's.
|
|
27
|
+
*/
|
|
28
|
+
function getWorkersEnv() {
|
|
29
|
+
if (!holder) throw new Error("getWorkersEnv() was called before the first request captured the Workers env. Defer access behind a closure instead of reading it at module scope, e.g. binding: () => getWorkersEnv<Env>().DB");
|
|
30
|
+
return holder.env;
|
|
31
|
+
}
|
|
32
|
+
/** Clears the holder. Used by createWorkersHandler on boot failure and by tests. */
|
|
33
|
+
function resetWorkersEnv() {
|
|
34
|
+
holder = void 0;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether this code is running on workerd, which identifies itself through the
|
|
38
|
+
* standard navigator user agent. The switch an app's `config/database.ts` needs
|
|
39
|
+
* to pick D1 over a local driver, and to keep filesystem work out of the Worker.
|
|
40
|
+
*/
|
|
41
|
+
function isWorkersRuntime() {
|
|
42
|
+
return typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers";
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { captureWorkersEnv, getWorkersEnv, isWorkersRuntime, resetWorkersEnv };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,30 +1,45 @@
|
|
|
1
|
+
import { captureWorkersEnv, getWorkersEnv, isWorkersRuntime, resetWorkersEnv } from "./env.js";
|
|
1
2
|
import { FileMetadata, GetStreamOptions, PutOptions, ServiceProviderConstructor, StorageDriver, StorageDriverCapabilities, TemporaryUrlOptions } from "@guren/core";
|
|
2
3
|
import { PathLike } from "@guren/core/internal/deploy-build";
|
|
3
|
-
//#region src/
|
|
4
|
-
/** First call wins; later calls are ignored (never overwrite a live env). */
|
|
5
|
-
declare function captureWorkersEnv(env: unknown): void;
|
|
6
|
-
/** Throws with a clear message if called before the first request captured env. */
|
|
7
|
-
declare function getWorkersEnv<E = unknown>(): E;
|
|
8
|
-
/** Clears the holder. Used by createWorkersHandler on boot failure and by tests. */
|
|
9
|
-
declare function resetWorkersEnv(): void;
|
|
10
|
-
//#endregion
|
|
11
|
-
//#region src/handler.d.ts
|
|
4
|
+
//#region src/boot.d.ts
|
|
12
5
|
interface WorkersExecutionContext {
|
|
13
6
|
waitUntil(promise: Promise<unknown>): void;
|
|
14
7
|
passThroughOnException?(): void;
|
|
15
8
|
}
|
|
16
9
|
/**
|
|
17
|
-
* The shape
|
|
18
|
-
* `Application` itself, so `boot()` is not assumed to be idempotent
|
|
19
|
-
* handler dedupes boot across its own requests for anything conforming here.
|
|
10
|
+
* The shape the Workers entrypoints need from an app. Structural rather than
|
|
11
|
+
* `Application` itself, so `boot()` is not assumed to be idempotent.
|
|
20
12
|
*/
|
|
21
13
|
interface WorkersAppLike {
|
|
22
14
|
boot(): Promise<void>;
|
|
23
15
|
fetch(request: Request, env?: unknown, executionCtx?: unknown): Response | Promise<Response>;
|
|
24
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Boot `app` against `env`, once per isolate.
|
|
19
|
+
*
|
|
20
|
+
* Boot is deferred to the first request or wake because it performs I/O (ORM
|
|
21
|
+
* setup against D1), which workerd forbids in global scope (RFC 0003).
|
|
22
|
+
* @throws Whatever `boot()` threw, and `captureWorkersEnv`'s refusal of a second env.
|
|
23
|
+
*/
|
|
24
|
+
declare function bootWorkersApp(app: WorkersAppLike, env: unknown): Promise<void>;
|
|
25
|
+
/** Boot, then dispatch — the topology every Workers entrypoint shares. */
|
|
26
|
+
declare function bootAndFetch(app: WorkersAppLike, request: Request, env: unknown, ctx?: WorkersExecutionContext): Promise<Response>;
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/handler.d.ts
|
|
25
29
|
interface WorkersHandler {
|
|
26
30
|
fetch(request: Request, env: unknown, ctx: WorkersExecutionContext): Promise<Response>;
|
|
31
|
+
/**
|
|
32
|
+
* Boot without dispatching, for an entrypoint that holds `env` but no request
|
|
33
|
+
* — an agent Durable Object woken by an alarm (RFC 0017 §6).
|
|
34
|
+
*/
|
|
35
|
+
boot(env: unknown): Promise<void>;
|
|
27
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* The worker entry `guren cloudflare:build` generates.
|
|
39
|
+
*
|
|
40
|
+
* One per module: it carries the isolate's boot slot, and `boot.ts` is where
|
|
41
|
+
* that invariant is specified.
|
|
42
|
+
*/
|
|
28
43
|
declare function createWorkersHandler(app: WorkersAppLike): WorkersHandler;
|
|
29
44
|
//#endregion
|
|
30
45
|
//#region src/build.d.ts
|
|
@@ -45,6 +60,22 @@ interface BuildCloudflareOutputOptions {
|
|
|
45
60
|
ssrEntryKey?: string;
|
|
46
61
|
/** Skip running the app's `build` script before assembling output. */
|
|
47
62
|
skipAppBuild?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Front the worker with `@cloudflare/workers-oauth-provider`, so the App MCP
|
|
65
|
+
* endpoint is reached by OAuth-authorized clients instead of bearer tokens
|
|
66
|
+
* (RFC 0016 §7). A **build** option, not plugin configuration: the generator
|
|
67
|
+
* runs in another process and cannot read what `mcpPlugin()` was passed, and
|
|
68
|
+
* nothing records the choice, so pass it on every build that wants it.
|
|
69
|
+
*/
|
|
70
|
+
mcpOAuth?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Path the App MCP endpoint is mounted at, used as the OAuth provider's
|
|
73
|
+
* protected `apiRoute`. Only read when `mcpOAuth` is on. Defaults to
|
|
74
|
+
* `mcpPlugin()`'s own default; an app that passed `mcpPlugin({ path })` must
|
|
75
|
+
* repeat it here — a provider protecting a path the endpoint does not serve
|
|
76
|
+
* leaves the endpoint outside the OAuth boundary silently.
|
|
77
|
+
*/
|
|
78
|
+
mcpPath?: string;
|
|
48
79
|
}
|
|
49
80
|
/**
|
|
50
81
|
* Assemble a deployable Cloudflare Workers directory (`.cloudflare/`) from a
|
|
@@ -54,19 +85,17 @@ interface BuildCloudflareOutputOptions {
|
|
|
54
85
|
*/
|
|
55
86
|
declare function buildCloudflareOutput(options?: BuildCloudflareOutputOptions): Promise<void>;
|
|
56
87
|
/**
|
|
57
|
-
* Wrangler's `migrations_dir` only discovers flat `*.sql` files, but
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* every build — run `cloudflare:build` after adding a migration.
|
|
88
|
+
* Wrangler's `migrations_dir` only discovers flat `*.sql` files, but drizzle-kit
|
|
89
|
+
* (1.x) emits one `<timestamp>_<name>/migration.sql` folder per migration.
|
|
90
|
+
* Flatten each folder into `<folder-name>.sql` (plain `*.sql` files pass
|
|
91
|
+
* through, `meta/` is skipped) so `wrangler d1 migrations apply` sees them in
|
|
92
|
+
* filename order. Regenerated on every build — run `cloudflare:build` after adding one.
|
|
63
93
|
*/
|
|
64
94
|
declare function flattenD1Migrations(migrationsDir: string, outDir: string): void;
|
|
65
95
|
//#endregion
|
|
66
96
|
//#region src/storage/R2Driver.d.ts
|
|
67
97
|
/**
|
|
68
|
-
* Structural view of the R2 binding
|
|
69
|
-
* local `S3Client { send() }`: only what this driver calls, so
|
|
98
|
+
* Structural view of the R2 binding: only what this driver calls, so
|
|
70
99
|
* `@cloudflare/workers-types` stays a devDependency. `R2Driver.types.test.ts`
|
|
71
100
|
* pins that the real `R2Bucket` satisfies it.
|
|
72
101
|
*/
|
|
@@ -86,11 +115,10 @@ interface R2ObjectLike {
|
|
|
86
115
|
customMetadata?: Record<string, string>;
|
|
87
116
|
}
|
|
88
117
|
/**
|
|
89
|
-
* Streams and blobs are typed structurally
|
|
90
|
-
* `ReadableStream`/`Blob
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* the `R2BucketLike` check while being perfectly usable at runtime.
|
|
118
|
+
* Streams and blobs are typed structurally: `@cloudflare/workers-types` declares
|
|
119
|
+
* its own `ReadableStream`/`Blob`, assignable in neither direction to the runtime
|
|
120
|
+
* globals, so naming the globals would fail the `R2BucketLike` check on a real
|
|
121
|
+
* `R2Bucket` that works fine at runtime.
|
|
94
122
|
*/
|
|
95
123
|
interface R2StreamLike {
|
|
96
124
|
locked: boolean;
|
|
@@ -157,21 +185,17 @@ interface R2DriverOptions {
|
|
|
157
185
|
*/
|
|
158
186
|
binding: () => unknown;
|
|
159
187
|
/**
|
|
160
|
-
* Base URL for `url()`: the bucket's custom domain (recommended) or its
|
|
161
|
-
*
|
|
162
|
-
* `https://<bucket>.s3.<region>.amazonaws.com` — so `url()` throws with
|
|
163
|
-
* guidance when this is unset.
|
|
188
|
+
* Base URL for `url()`: the bucket's custom domain (recommended) or its r2.dev
|
|
189
|
+
* subdomain. R2 has no derivable default, so `url()` throws when this is unset.
|
|
164
190
|
*/
|
|
165
191
|
publicUrl?: string;
|
|
166
192
|
/** Key prefix, same semantics as `S3DriverOptions.prefix`. */
|
|
167
193
|
prefix?: string;
|
|
168
194
|
/**
|
|
169
195
|
* The visibility every object in this bucket effectively has. R2 has no
|
|
170
|
-
* per-object ACL
|
|
196
|
+
* per-object ACL, so `put({ visibility })` and `setVisibility()` throw when
|
|
197
|
+
* asked for the other value rather than pretend to enforce it.
|
|
171
198
|
* Defaults to `'public'` when `publicUrl` is set, `'private'` otherwise.
|
|
172
|
-
* `put({ visibility })` and `setVisibility()` throw when asked for the
|
|
173
|
-
* other value — the driver refuses to pretend it can enforce a per-object
|
|
174
|
-
* flag it cannot.
|
|
175
199
|
*/
|
|
176
200
|
visibility?: 'public' | 'private';
|
|
177
201
|
/**
|
|
@@ -182,14 +206,9 @@ interface R2DriverOptions {
|
|
|
182
206
|
}
|
|
183
207
|
/**
|
|
184
208
|
* Cloudflare R2 storage driver over the Workers binding.
|
|
185
|
-
*
|
|
186
209
|
* @example
|
|
187
210
|
* ```ts
|
|
188
|
-
*
|
|
189
|
-
* storage.registerDisk('media', () => new R2Driver({
|
|
190
|
-
* binding: () => getWorkersEnv<Env>().MEDIA,
|
|
191
|
-
* publicUrl: 'https://media.example.com',
|
|
192
|
-
* }))
|
|
211
|
+
* new R2Driver({ binding: () => getWorkersEnv<Env>().MEDIA, publicUrl: 'https://media.example.com' })
|
|
193
212
|
* ```
|
|
194
213
|
*/
|
|
195
214
|
declare class R2Driver implements StorageDriver {
|
|
@@ -254,14 +273,7 @@ declare class R2Driver implements StorageDriver {
|
|
|
254
273
|
* upcoming RFC 0003 parts (session/OAuth-state store wiring on Workers).
|
|
255
274
|
*/
|
|
256
275
|
interface CloudflarePluginConfig {}
|
|
257
|
-
/**
|
|
258
|
-
* Register the Cloudflare plugin.
|
|
259
|
-
*
|
|
260
|
-
* @example
|
|
261
|
-
* ```typescript
|
|
262
|
-
* createApp({ providers: [cloudflarePlugin()] })
|
|
263
|
-
* ```
|
|
264
|
-
*/
|
|
276
|
+
/** Register the Cloudflare plugin: `createApp({ providers: [cloudflarePlugin()] })`. */
|
|
265
277
|
declare function cloudflarePlugin(config?: CloudflarePluginConfig): ServiceProviderConstructor;
|
|
266
278
|
//#endregion
|
|
267
|
-
export { type BuildCloudflareOutputOptions, CloudflarePluginConfig, type R2BucketLike, R2Driver, type R2DriverOptions, type R2PresignOptions, type WorkersAppLike, type WorkersExecutionContext, type WorkersHandler, buildCloudflareOutput, captureWorkersEnv, cloudflarePlugin, createWorkersHandler, flattenD1Migrations, getWorkersEnv, resetWorkersEnv };
|
|
279
|
+
export { type BuildCloudflareOutputOptions, CloudflarePluginConfig, type R2BucketLike, R2Driver, type R2DriverOptions, type R2PresignOptions, type WorkersAppLike, type WorkersExecutionContext, type WorkersHandler, bootAndFetch, bootWorkersApp, buildCloudflareOutput, captureWorkersEnv, cloudflarePlugin, createWorkersHandler, flattenD1Migrations, getWorkersEnv, isWorkersRuntime, resetWorkersEnv };
|
package/dist/index.js
CHANGED
|
@@ -1,39 +1,61 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { captureWorkersEnv, getWorkersEnv, isWorkersRuntime, resetWorkersEnv } from "./env.js";
|
|
2
|
+
import { n as flattenD1Migrations, t as buildCloudflareOutput } from "./build-DLJgpyN5.js";
|
|
2
3
|
import { definePlugin } from "@guren/core";
|
|
3
|
-
//#region src/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
//#region src/boot.ts
|
|
5
|
+
/**
|
|
6
|
+
* The isolate's one boot slot (RFC 0017 §6).
|
|
7
|
+
*
|
|
8
|
+
* A worker's `fetch` export is not the only entrypoint: an alarm can wake an
|
|
9
|
+
* agent Durable Object before any request has arrived, and a second entrypoint
|
|
10
|
+
* with its own boot would race the module-global env holder. So boot is a
|
|
11
|
+
* shared primitive — promise-latched, first caller wins, everyone awaits the
|
|
12
|
+
* same latch. Keyed per app (a Bun test suite boots many); the env holder stays
|
|
13
|
+
* module-global, since `getWorkersEnv()` has no app to key on — one per isolate.
|
|
14
|
+
*/
|
|
15
|
+
const latches = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
/**
|
|
17
|
+
* Boot `app` against `env`, once per isolate.
|
|
18
|
+
*
|
|
19
|
+
* Boot is deferred to the first request or wake because it performs I/O (ORM
|
|
20
|
+
* setup against D1), which workerd forbids in global scope (RFC 0003).
|
|
21
|
+
* @throws Whatever `boot()` threw, and `captureWorkersEnv`'s refusal of a second env.
|
|
22
|
+
*/
|
|
23
|
+
async function bootWorkersApp(app, env) {
|
|
24
|
+
captureWorkersEnv(env);
|
|
25
|
+
let attempt;
|
|
26
|
+
try {
|
|
27
|
+
attempt = latches.get(app);
|
|
28
|
+
if (!attempt) {
|
|
29
|
+
attempt = app.boot();
|
|
30
|
+
latches.set(app, attempt);
|
|
31
|
+
}
|
|
32
|
+
await attempt;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (latches.get(app) === attempt) {
|
|
35
|
+
latches.delete(app);
|
|
36
|
+
resetWorkersEnv();
|
|
37
|
+
}
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
13
40
|
}
|
|
14
|
-
/**
|
|
15
|
-
function
|
|
16
|
-
|
|
41
|
+
/** Boot, then dispatch — the topology every Workers entrypoint shares. */
|
|
42
|
+
async function bootAndFetch(app, request, env, ctx) {
|
|
43
|
+
await bootWorkersApp(app, env);
|
|
44
|
+
return app.fetch(request, env, ctx);
|
|
17
45
|
}
|
|
18
46
|
//#endregion
|
|
19
47
|
//#region src/handler.ts
|
|
48
|
+
/**
|
|
49
|
+
* The worker entry `guren cloudflare:build` generates.
|
|
50
|
+
*
|
|
51
|
+
* One per module: it carries the isolate's boot slot, and `boot.ts` is where
|
|
52
|
+
* that invariant is specified.
|
|
53
|
+
*/
|
|
20
54
|
function createWorkersHandler(app) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
attempt = bootPromise ??= app.boot();
|
|
27
|
-
await attempt;
|
|
28
|
-
} catch (error) {
|
|
29
|
-
if (bootPromise === attempt) {
|
|
30
|
-
bootPromise = void 0;
|
|
31
|
-
resetWorkersEnv();
|
|
32
|
-
}
|
|
33
|
-
throw error;
|
|
34
|
-
}
|
|
35
|
-
return app.fetch(request, env, ctx);
|
|
36
|
-
} };
|
|
55
|
+
return {
|
|
56
|
+
fetch: (request, env, ctx) => bootAndFetch(app, request, env, ctx),
|
|
57
|
+
boot: (env) => bootWorkersApp(app, env)
|
|
58
|
+
};
|
|
37
59
|
}
|
|
38
60
|
//#endregion
|
|
39
61
|
//#region src/storage/sigv4.ts
|
|
@@ -82,12 +104,10 @@ async function hmac(key, value) {
|
|
|
82
104
|
return crypto.subtle.sign("HMAC", cryptoKey, new TextEncoder().encode(value));
|
|
83
105
|
}
|
|
84
106
|
/**
|
|
85
|
-
* `AWS4<secret>` → date → region → service → terminator, cached per
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* key id — never the secret — plus scope; self-bounding, but capped anyway
|
|
90
|
-
* so a pathological key rotation cannot grow it.
|
|
107
|
+
* `AWS4<secret>` → date → region → service → terminator, cached per credential
|
|
108
|
+
* and day: the four-step HMAC chain (eight `crypto.subtle` calls) only changes
|
|
109
|
+
* when the date stamp rolls over. Keyed by access key id — never the secret —
|
|
110
|
+
* plus scope, and capped so a pathological key rotation cannot grow it.
|
|
91
111
|
*/
|
|
92
112
|
const signingKeyCache = /* @__PURE__ */ new Map();
|
|
93
113
|
const SIGNING_KEY_CACHE_MAX = 8;
|
|
@@ -103,11 +123,7 @@ function signingKey(accessKeyId, secretAccessKey, dateStamp, region, service) {
|
|
|
103
123
|
derived.catch(() => signingKeyCache.delete(cacheKey));
|
|
104
124
|
return derived;
|
|
105
125
|
}
|
|
106
|
-
/**
|
|
107
|
-
* Presign a GET so the URL alone authorizes the read until it expires.
|
|
108
|
-
*
|
|
109
|
-
* @returns the signed absolute URL
|
|
110
|
-
*/
|
|
126
|
+
/** Presign a GET so the URL alone authorizes the read until it expires. */
|
|
111
127
|
async function presignGetUrl(options) {
|
|
112
128
|
const { accessKeyId, secretAccessKey, region, service, expiresIn } = options;
|
|
113
129
|
const amzDate = `${(options.date ?? /* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").slice(0, 15)}Z`;
|
|
@@ -147,14 +163,9 @@ const DELETE_BATCH_SIZE = 1e3;
|
|
|
147
163
|
const DELETE_CONCURRENCY = 6;
|
|
148
164
|
/**
|
|
149
165
|
* Cloudflare R2 storage driver over the Workers binding.
|
|
150
|
-
*
|
|
151
166
|
* @example
|
|
152
167
|
* ```ts
|
|
153
|
-
*
|
|
154
|
-
* storage.registerDisk('media', () => new R2Driver({
|
|
155
|
-
* binding: () => getWorkersEnv<Env>().MEDIA,
|
|
156
|
-
* publicUrl: 'https://media.example.com',
|
|
157
|
-
* }))
|
|
168
|
+
* new R2Driver({ binding: () => getWorkersEnv<Env>().MEDIA, publicUrl: 'https://media.example.com' })
|
|
158
169
|
* ```
|
|
159
170
|
*/
|
|
160
171
|
var R2Driver = class {
|
|
@@ -398,16 +409,9 @@ const factory = definePlugin({
|
|
|
398
409
|
name: "cloudflare",
|
|
399
410
|
register() {}
|
|
400
411
|
});
|
|
401
|
-
/**
|
|
402
|
-
* Register the Cloudflare plugin.
|
|
403
|
-
*
|
|
404
|
-
* @example
|
|
405
|
-
* ```typescript
|
|
406
|
-
* createApp({ providers: [cloudflarePlugin()] })
|
|
407
|
-
* ```
|
|
408
|
-
*/
|
|
412
|
+
/** Register the Cloudflare plugin: `createApp({ providers: [cloudflarePlugin()] })`. */
|
|
409
413
|
function cloudflarePlugin(config = {}) {
|
|
410
414
|
return factory(config);
|
|
411
415
|
}
|
|
412
416
|
//#endregion
|
|
413
|
-
export { R2Driver, buildCloudflareOutput, captureWorkersEnv, cloudflarePlugin, createWorkersHandler, flattenD1Migrations, getWorkersEnv, resetWorkersEnv };
|
|
417
|
+
export { R2Driver, bootAndFetch, bootWorkersApp, buildCloudflareOutput, captureWorkersEnv, cloudflarePlugin, createWorkersHandler, flattenD1Migrations, getWorkersEnv, isWorkersRuntime, resetWorkersEnv };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guren/plugin-cloudflare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
}
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
|
-
"dist"
|
|
28
|
+
"dist",
|
|
29
|
+
"templates"
|
|
29
30
|
],
|
|
30
31
|
"main": "dist/index.js",
|
|
31
32
|
"types": "dist/index.d.ts",
|
|
@@ -33,6 +34,10 @@
|
|
|
33
34
|
".": {
|
|
34
35
|
"types": "./dist/index.d.ts",
|
|
35
36
|
"default": "./dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./env": {
|
|
39
|
+
"types": "./dist/env.d.ts",
|
|
40
|
+
"default": "./dist/env.js"
|
|
36
41
|
}
|
|
37
42
|
},
|
|
38
43
|
"scripts": {
|
|
@@ -41,12 +46,14 @@
|
|
|
41
46
|
"typecheck": "tsc --noEmit"
|
|
42
47
|
},
|
|
43
48
|
"dependencies": {
|
|
44
|
-
"@guren/core": "^1.
|
|
49
|
+
"@guren/core": "^1.14.0",
|
|
45
50
|
"citty": "^0.1.6"
|
|
46
51
|
},
|
|
47
52
|
"devDependencies": {
|
|
53
|
+
"@cloudflare/workers-oauth-provider": "^0.10.3",
|
|
48
54
|
"@cloudflare/workers-types": "^4.20260702.1",
|
|
49
55
|
"@types/node": "^20.14.10",
|
|
56
|
+
"hono": "^4.11.7",
|
|
50
57
|
"miniflare": "5.20260801.1-alpha",
|
|
51
58
|
"tsdown": "^0.22.14",
|
|
52
59
|
"typescript": "^7.0.2"
|