@guren/plugin-cloudflare 0.2.1 → 0.2.2

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 CHANGED
@@ -20,8 +20,8 @@ bunx wrangler deploy
20
20
 
21
21
  ## API
22
22
 
23
- - **`createWorkersHandler(app)`** — wraps a Guren `Application` in a Workers module handler. Boot is lazy and deduplicated on the first request, because bindings only arrive with `fetch` and cannot be read at module scope.
24
- - **`getWorkersEnv<Env>()`** — exposes the first request's bindings to boot-time config through a write-once holder. Use it to hand a D1 binding to the ORM:
23
+ - **`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`.
24
+ - **`getWorkersEnv<Env>()`** — exposes the first request's bindings to boot-time config through a write-once holder. workerd can also expose bindings at module scope via `cloudflare:workers`, but importing that from shared config would break every other runtime (Bun, Lambda, Vercel) — the holder keeps `config/*.ts` portable. Use it to hand a D1 binding to the ORM:
25
25
 
26
26
  ```typescript
27
27
  import { createD1Database } from '@guren/core'
@@ -181,10 +181,6 @@ function renderWorkerModule(input) {
181
181
  }
182
182
  function scaffoldWranglerConfig(root, out, packageName) {
183
183
  const configPath = resolve(root, "wrangler.jsonc");
184
- if (existsSync(configPath)) {
185
- warnMissingBuildOwnedKeys(configPath, relative(root, out).split(sep).join("/"));
186
- return;
187
- }
188
184
  const appName = (packageName ?? "guren-app").replace(/^@[^/]+\//, "");
189
185
  const outRelative = relative(root, out).split(sep).join("/");
190
186
  const config = {
@@ -219,8 +215,16 @@ function scaffoldWranglerConfig(root, out, packageName) {
219
215
  ],
220
216
  vars: { NODE_ENV: "production" }
221
217
  };
222
- writeFileSync(configPath, `${JSON.stringify(config, null, 2)}
223
- `);
218
+ try {
219
+ writeFileSync(configPath, `${JSON.stringify(config, null, 2)}
220
+ `, { flag: "wx" });
221
+ } catch (error) {
222
+ if (error.code === "EEXIST") {
223
+ warnMissingBuildOwnedKeys(configPath, outRelative);
224
+ return;
225
+ }
226
+ throw error;
227
+ }
224
228
  console.log(`Cloudflare build: scaffolded ${configPath} \u2014 fill in d1_databases[0].database_id before deploying.`);
225
229
  }
226
230
  function warnMissingBuildOwnedKeys(configPath, outRelative) {
package/dist/commands.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  buildCloudflareOutput
3
- } from "./chunk-N2ARYMZQ.js";
3
+ } from "./chunk-32YY6FCY.js";
4
4
 
5
5
  // src/commands.ts
6
6
  import { defineCommand } from "citty";
package/dist/index.d.ts CHANGED
@@ -12,6 +12,11 @@ interface WorkersExecutionContext {
12
12
  waitUntil(promise: Promise<unknown>): void;
13
13
  passThroughOnException?(): void;
14
14
  }
15
+ /**
16
+ * The shape `createWorkersHandler` needs from an app. Structural rather than
17
+ * `Application` itself, so `boot()` is not assumed to be idempotent — each
18
+ * handler dedupes boot across its own requests for anything conforming here.
19
+ */
15
20
  interface WorkersAppLike {
16
21
  boot(): Promise<void>;
17
22
  fetch(request: Request, env?: unknown, executionCtx?: unknown): Response | Promise<Response>;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  buildCloudflareOutput,
3
3
  flattenD1Migrations
4
- } from "./chunk-N2ARYMZQ.js";
4
+ } from "./chunk-32YY6FCY.js";
5
5
 
6
6
  // src/index.ts
7
7
  import { definePlugin } from "@guren/core";
@@ -29,12 +29,15 @@ function createWorkersHandler(app) {
29
29
  return {
30
30
  async fetch(request, env, ctx) {
31
31
  captureWorkersEnv(env);
32
- bootPromise ??= app.boot();
32
+ let attempt;
33
33
  try {
34
- await bootPromise;
34
+ attempt = bootPromise ??= app.boot();
35
+ await attempt;
35
36
  } catch (error) {
36
- bootPromise = void 0;
37
- resetWorkersEnv();
37
+ if (bootPromise === attempt) {
38
+ bootPromise = void 0;
39
+ resetWorkersEnv();
40
+ }
38
41
  throw error;
39
42
  }
40
43
  return app.fetch(request, env, ctx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guren/plugin-cloudflare",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -41,7 +41,7 @@
41
41
  "typecheck": "tsc --noEmit"
42
42
  },
43
43
  "dependencies": {
44
- "@guren/core": "^1.4.0",
44
+ "@guren/core": "^1.5.2",
45
45
  "citty": "^0.1.6"
46
46
  },
47
47
  "devDependencies": {