alchemy 0.43.5 → 0.44.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.
Files changed (144) hide show
  1. package/bin/alchemy.mjs +430 -343
  2. package/bin/alchemy.ts +7 -21
  3. package/bin/commands/create.ts +79 -23
  4. package/bin/services/template-manager.ts +9 -4
  5. package/bin/services/vibe-rules.ts +47 -0
  6. package/bin/types.ts +25 -5
  7. package/lib/alchemy.d.ts.map +1 -1
  8. package/lib/alchemy.js +8 -11
  9. package/lib/alchemy.js.map +1 -1
  10. package/lib/apply.js +2 -0
  11. package/lib/apply.js.map +1 -1
  12. package/lib/build-date.d.ts +1 -1
  13. package/lib/build-date.js +1 -1
  14. package/lib/cloudflare/assets.d.ts +1 -1
  15. package/lib/cloudflare/assets.d.ts.map +1 -1
  16. package/lib/cloudflare/bucket.d.ts.map +1 -1
  17. package/lib/cloudflare/bucket.js +9 -6
  18. package/lib/cloudflare/bucket.js.map +1 -1
  19. package/lib/cloudflare/bundle/bundle-worker-dev.d.ts.map +1 -1
  20. package/lib/cloudflare/bundle/bundle-worker-dev.js +4 -1
  21. package/lib/cloudflare/bundle/bundle-worker-dev.js.map +1 -1
  22. package/lib/cloudflare/bundle/bundle-worker.d.ts.map +1 -1
  23. package/lib/cloudflare/bundle/bundle-worker.js +4 -1
  24. package/lib/cloudflare/bundle/bundle-worker.js.map +1 -1
  25. package/lib/cloudflare/bundle/external.d.ts +1 -0
  26. package/lib/cloudflare/bundle/external.d.ts.map +1 -1
  27. package/lib/cloudflare/bundle/external.js +1 -1
  28. package/lib/cloudflare/bundle/external.js.map +1 -1
  29. package/lib/cloudflare/bundle/nodejs-import-warning-plugin.d.ts +10 -0
  30. package/lib/cloudflare/bundle/nodejs-import-warning-plugin.d.ts.map +1 -0
  31. package/lib/cloudflare/bundle/nodejs-import-warning-plugin.js +67 -0
  32. package/lib/cloudflare/bundle/nodejs-import-warning-plugin.js.map +1 -0
  33. package/lib/cloudflare/container.d.ts +27 -2
  34. package/lib/cloudflare/container.d.ts.map +1 -1
  35. package/lib/cloudflare/container.js +82 -24
  36. package/lib/cloudflare/container.js.map +1 -1
  37. package/lib/cloudflare/do-state-store/store.d.ts.map +1 -1
  38. package/lib/cloudflare/do-state-store/store.js +5 -1
  39. package/lib/cloudflare/do-state-store/store.js.map +1 -1
  40. package/lib/cloudflare/redirect-rule.js +1 -1
  41. package/lib/cloudflare/redirect-rule.js.map +1 -1
  42. package/lib/cloudflare/website.d.ts +17 -0
  43. package/lib/cloudflare/website.d.ts.map +1 -1
  44. package/lib/cloudflare/website.js +1 -0
  45. package/lib/cloudflare/website.js.map +1 -1
  46. package/lib/cloudflare/worker/miniflare.d.ts +4 -1
  47. package/lib/cloudflare/worker/miniflare.d.ts.map +1 -1
  48. package/lib/cloudflare/worker/miniflare.js +22 -2
  49. package/lib/cloudflare/worker/miniflare.js.map +1 -1
  50. package/lib/cloudflare/worker-metadata.d.ts.map +1 -1
  51. package/lib/cloudflare/worker-metadata.js +12 -0
  52. package/lib/cloudflare/worker-metadata.js.map +1 -1
  53. package/lib/cloudflare/worker.d.ts +1 -1
  54. package/lib/cloudflare/wrangler.json.d.ts +24 -0
  55. package/lib/cloudflare/wrangler.json.d.ts.map +1 -1
  56. package/lib/cloudflare/wrangler.json.js +20 -2
  57. package/lib/cloudflare/wrangler.json.js.map +1 -1
  58. package/lib/context.d.ts +6 -1
  59. package/lib/context.d.ts.map +1 -1
  60. package/lib/context.js +2 -1
  61. package/lib/context.js.map +1 -1
  62. package/lib/destroy.d.ts.map +1 -1
  63. package/lib/destroy.js +1 -0
  64. package/lib/destroy.js.map +1 -1
  65. package/lib/docker/api.d.ts +11 -0
  66. package/lib/docker/api.d.ts.map +1 -1
  67. package/lib/docker/api.js +41 -1
  68. package/lib/docker/api.js.map +1 -1
  69. package/lib/docker/image.d.ts +1 -1
  70. package/lib/docker/image.d.ts.map +1 -1
  71. package/lib/docker/image.js +11 -5
  72. package/lib/docker/image.js.map +1 -1
  73. package/lib/esbuild/bundle.d.ts +2 -2
  74. package/lib/index.d.ts.map +1 -1
  75. package/lib/index.js.map +1 -1
  76. package/lib/llms.cloudflare.txt +379 -0
  77. package/lib/llms.d.ts +10 -0
  78. package/lib/llms.d.ts.map +1 -0
  79. package/lib/llms.js +12 -0
  80. package/lib/llms.js.map +1 -0
  81. package/lib/planetscale/index.d.ts +3 -2
  82. package/lib/planetscale/index.d.ts.map +1 -1
  83. package/lib/planetscale/index.js +3 -2
  84. package/lib/planetscale/index.js.map +1 -1
  85. package/lib/planetscale/password.d.ts +233 -0
  86. package/lib/planetscale/password.d.ts.map +1 -0
  87. package/lib/planetscale/password.js +238 -0
  88. package/lib/planetscale/password.js.map +1 -0
  89. package/lib/scope.d.ts +6 -5
  90. package/lib/scope.d.ts.map +1 -1
  91. package/lib/scope.js +18 -7
  92. package/lib/scope.js.map +1 -1
  93. package/lib/test/bun.d.ts.map +1 -1
  94. package/lib/test/bun.js +1 -0
  95. package/lib/test/bun.js.map +1 -1
  96. package/lib/test/vitest.d.ts.map +1 -1
  97. package/lib/test/vitest.js +1 -0
  98. package/lib/test/vitest.js.map +1 -1
  99. package/lib/util/nanoid.d.ts +3 -0
  100. package/lib/util/nanoid.d.ts.map +1 -0
  101. package/lib/util/nanoid.js +13 -0
  102. package/lib/util/nanoid.js.map +1 -0
  103. package/lib/util/safe-fetch.d.ts.map +1 -1
  104. package/lib/util/safe-fetch.js +0 -1
  105. package/lib/util/safe-fetch.js.map +1 -1
  106. package/lib/util/telemetry/client.d.ts.map +1 -1
  107. package/lib/util/telemetry/client.js +13 -3
  108. package/lib/util/telemetry/client.js.map +1 -1
  109. package/lib/util/telemetry/constants.d.ts +2 -1
  110. package/lib/util/telemetry/constants.d.ts.map +1 -1
  111. package/lib/util/telemetry/constants.js +3 -2
  112. package/lib/util/telemetry/constants.js.map +1 -1
  113. package/package.json +10 -2
  114. package/src/alchemy.ts +8 -13
  115. package/src/apply.ts +2 -0
  116. package/src/build-date.ts +1 -1
  117. package/src/cloudflare/bucket.ts +22 -14
  118. package/src/cloudflare/bundle/bundle-worker-dev.ts +4 -1
  119. package/src/cloudflare/bundle/bundle-worker.ts +4 -1
  120. package/src/cloudflare/bundle/external.ts +1 -1
  121. package/src/cloudflare/bundle/nodejs-import-warning-plugin.ts +94 -0
  122. package/src/cloudflare/container.ts +113 -28
  123. package/src/cloudflare/do-state-store/store.ts +5 -1
  124. package/src/cloudflare/redirect-rule.ts +1 -1
  125. package/src/cloudflare/website.ts +21 -1
  126. package/src/cloudflare/worker/miniflare.ts +33 -8
  127. package/src/cloudflare/worker-metadata.ts +15 -0
  128. package/src/cloudflare/wrangler.json.ts +61 -5
  129. package/src/context.ts +7 -0
  130. package/src/destroy.ts +1 -0
  131. package/src/docker/api.ts +57 -1
  132. package/src/docker/image.ts +15 -6
  133. package/src/index.ts +1 -0
  134. package/src/llms.cloudflare.txt +379 -0
  135. package/src/llms.ts +25 -0
  136. package/src/planetscale/index.ts +3 -2
  137. package/src/planetscale/password.ts +366 -0
  138. package/src/scope.ts +25 -11
  139. package/src/test/bun.ts +1 -0
  140. package/src/test/vitest.ts +1 -0
  141. package/src/util/nanoid.ts +13 -0
  142. package/src/util/safe-fetch.ts +0 -1
  143. package/src/util/telemetry/client.ts +17 -3
  144. package/src/util/telemetry/constants.ts +5 -3
@@ -0,0 +1,379 @@
1
+ # Alchemy - TypeScript-native Infrastructure as Code
2
+
3
+ Alchemy is a TypeScript-native Infrastructure-as-Code framework that allows you to define cloud resources using familiar TypeScript patterns. This guide focuses on Cloudflare integration.
4
+
5
+ ## Core Concepts
6
+
7
+ **Resources**: Async functions that manage cloud infrastructure lifecycle (create, update, delete)
8
+ **Bindings**: Type-safe connections between resources (workers, KV stores, databases)
9
+ **State Management**: Tracks resource state for consistent deployments
10
+ **Finalization**: Always call `app.finalize()` to clean up orphaned resources
11
+
12
+ ## Basic Setup
13
+
14
+ ### Project Structure
15
+ ```typescript
16
+ // alchemy.run.ts - Your infrastructure definition
17
+ import alchemy from "alchemy";
18
+ import { Worker, KVNamespace } from "alchemy/cloudflare";
19
+
20
+ const app = await alchemy("my-app");
21
+
22
+ export const worker = await Worker("api", {
23
+ entrypoint: "./src/worker.ts",
24
+ bindings: {
25
+ CACHE: await KVNamespace("cache", { title: "cache-store" })
26
+ }
27
+ });
28
+
29
+ console.log({ url: worker.url });
30
+ await app.finalize(); // ⚠️ ALWAYS call finalize()
31
+ ```
32
+
33
+ ### Environment Setup
34
+ ```bash
35
+ # .env - Required for secret encryption
36
+ ALCHEMY_PASSWORD=your-secure-password
37
+
38
+ # Optional: Use Cloudflare state store in production
39
+ ALCHEMY_STATE_STORE=cloudflare
40
+ ```
41
+
42
+ ### Commands
43
+ ```bash
44
+ bun ./alchemy.run.ts # Deploy
45
+ bun ./alchemy.run.ts --destroy # Destroy all resources
46
+ ```
47
+
48
+ ## Workers
49
+
50
+ ### Basic Worker
51
+ ```typescript
52
+ const worker = await Worker("api", {
53
+ entrypoint: "./src/worker.ts",
54
+ url: true, // Get public URL
55
+ });
56
+ ```
57
+
58
+ ### Worker with Bindings
59
+ ```typescript
60
+ const worker = await Worker("api", {
61
+ entrypoint: "./src/worker.ts",
62
+ bindings: {
63
+ // Resource bindings
64
+ CACHE: kvNamespace,
65
+ STORAGE: r2Bucket,
66
+ COUNTER: durableObjectNamespace,
67
+ QUEUE: queue,
68
+ API: otherWorker,
69
+
70
+ // Environment variables
71
+ API_KEY: alchemy.secret(process.env.API_KEY),
72
+ DEBUG: "true"
73
+ }
74
+ });
75
+ ```
76
+
77
+ ### Worker Implementation
78
+ ```typescript
79
+ // src/worker.ts
80
+ import type { worker } from "../alchemy.run";
81
+
82
+ export default {
83
+ async fetch(request: Request, env: typeof worker.Env): Promise<Response> {
84
+ // Type-safe access to all bindings
85
+ const cached = await env.CACHE.get("key");
86
+ const apiKey = env.API_KEY;
87
+
88
+ return new Response("Hello World");
89
+ }
90
+ };
91
+ ```
92
+
93
+ ### Type Safety Setup
94
+ ```typescript
95
+ // types/env.d.ts
96
+ import type { worker } from "../alchemy.run";
97
+
98
+ declare module "cloudflare:workers" {
99
+ namespace Cloudflare {
100
+ export interface Env extends typeof worker.Env {}
101
+ }
102
+ }
103
+ ```
104
+
105
+ ## Durable Objects
106
+
107
+ ### Creating Durable Object Namespace
108
+ ```typescript
109
+ // Use 'new' instead of 'await' for DurableObjectNamespace
110
+ const counter = new DurableObjectNamespace("counter", {
111
+ className: "Counter",
112
+ sqlite: true, // Enable SQLite storage
113
+ });
114
+
115
+ const worker = await Worker("api", {
116
+ entrypoint: "./src/worker.ts",
117
+ bindings: {
118
+ COUNTER: counter, // Bind to worker
119
+ }
120
+ });
121
+ ```
122
+
123
+ ### Durable Object Implementation
124
+ ```typescript
125
+ // src/counter.ts
126
+ import { DurableObject } from "cloudflare:workers";
127
+
128
+ export class Counter extends DurableObject {
129
+ async increment(): Promise<number> {
130
+ let count = (await this.ctx.storage.get("count")) || 0;
131
+ count++;
132
+ await this.ctx.storage.put("count", count);
133
+ return count;
134
+ }
135
+
136
+ async fetch(request: Request): Promise<Response> {
137
+ const count = await this.increment();
138
+ return new Response(JSON.stringify({ count }));
139
+ }
140
+ }
141
+ ```
142
+
143
+ ### Using Durable Objects in Worker
144
+ ```typescript
145
+ // src/worker.ts
146
+ export default {
147
+ async fetch(request: Request, env: typeof worker.Env) {
148
+ // Get a Durable Object instance
149
+ const id = env.COUNTER.idFromName("global-counter");
150
+ const obj = env.COUNTER.get(id);
151
+
152
+ // Call the Durable Object
153
+ return obj.fetch(request);
154
+ }
155
+ };
156
+ ```
157
+
158
+ ### Cross-Script Durable Objects
159
+ ```typescript
160
+ // Method 1: Re-export from provider worker
161
+ const sharedCounter = host.bindings.SHARED_COUNTER;
162
+
163
+ // Method 2: Reference by worker script name
164
+ const counter = new DurableObjectNamespace("counter", {
165
+ className: "Counter",
166
+ scriptName: "provider-worker"
167
+ });
168
+ ```
169
+
170
+ ## Key Resources
171
+
172
+ ### KV Namespace
173
+ ```typescript
174
+ const cache = await KVNamespace("cache", {
175
+ title: "my-cache-store"
176
+ });
177
+
178
+ // Usage in worker
179
+ const value = await env.CACHE.get("key");
180
+ await env.CACHE.put("key", "value", { expirationTtl: 3600 });
181
+ ```
182
+
183
+ ### R2 Bucket
184
+ ```typescript
185
+ const storage = await R2Bucket("storage", {
186
+ allowPublicAccess: false
187
+ });
188
+
189
+ // Usage in worker
190
+ const object = await env.STORAGE.get("file.txt");
191
+ await env.STORAGE.put("file.txt", "content");
192
+ ```
193
+
194
+ ### Queue
195
+ ```typescript
196
+ // Typed queue
197
+ const queue = await Queue<{ name: string; email: string }>("notifications");
198
+
199
+ // Producer worker
200
+ const producer = await Worker("producer", {
201
+ bindings: { QUEUE: queue },
202
+ // Queue sending code
203
+ });
204
+
205
+ // Consumer worker
206
+ const consumer = await Worker("consumer", {
207
+ eventSources: [queue], // Register as consumer
208
+ // Queue processing code
209
+ });
210
+
211
+ // Worker implementation for queue processing
212
+ export default {
213
+ async queue(batch: typeof queue.Batch, env: Env) {
214
+ for (const message of batch.messages) {
215
+ console.log("Processing:", message.body);
216
+ message.ack(); // Acknowledge message
217
+ }
218
+ }
219
+ };
220
+ ```
221
+
222
+ ### Custom Domains
223
+ ```typescript
224
+ const worker = await Worker("api", {
225
+ entrypoint: "./src/worker.ts",
226
+ routes: [
227
+ { pattern: "api.example.com/*", zone: "example.com" },
228
+ { pattern: "example.com/api/*", zone: "example.com" }
229
+ ]
230
+ });
231
+ ```
232
+
233
+ ## Converting Existing Cloudflare Projects
234
+
235
+ ### 1. Migrate wrangler.toml Configuration
236
+ ```toml
237
+ # Old wrangler.toml
238
+ name = "my-worker"
239
+ main = "src/index.js"
240
+
241
+ [env.production]
242
+ kv_namespaces = [
243
+ { binding = "CACHE", id = "abc123" }
244
+ ]
245
+
246
+ [[env.production.r2_buckets]]
247
+ binding = "STORAGE"
248
+ bucket_name = "my-bucket"
249
+ ```
250
+
251
+ ```typescript
252
+ // New alchemy.run.ts
253
+ const worker = await Worker("my-worker", {
254
+ entrypoint: "./src/index.js",
255
+ bindings: {
256
+ CACHE: await KVNamespace("cache", {
257
+ title: "cache-store",
258
+ adopt: true // Use existing KV namespace
259
+ }),
260
+ STORAGE: await R2Bucket("storage", {
261
+ name: "my-bucket",
262
+ adopt: true // Use existing bucket
263
+ })
264
+ }
265
+ });
266
+ ```
267
+
268
+ ### 2. Enable Development Compatibility
269
+ ```typescript
270
+ // Generate wrangler.json for local development
271
+ await WranglerJson("wrangler.json", {
272
+ worker,
273
+ });
274
+ ```
275
+
276
+ ### 3. Adopt Existing Resources
277
+ ```typescript
278
+ // Use adopt: true to use existing resources instead of failing
279
+ const bucket = await R2Bucket("storage", {
280
+ name: "existing-bucket-name",
281
+ adopt: true // Won't fail if bucket already exists
282
+ });
283
+ ```
284
+
285
+ ### 4. State Store Migration
286
+ ```typescript
287
+ // Local development (default)
288
+ const app = await alchemy("my-app");
289
+
290
+ // Production with Cloudflare state store
291
+ const app = await alchemy("my-app", {
292
+ stateStore: process.env.NODE_ENV === "production"
293
+ ? (scope) => new DOStateStore(scope)
294
+ : undefined
295
+ });
296
+ ```
297
+
298
+ ## Advanced Patterns
299
+
300
+ ### Framework Integration (Vite/React/etc.)
301
+ ```typescript
302
+ const website = await Vite("website", {
303
+ main: "./src/worker.ts",
304
+ command: "bun run build", // Build command
305
+ bindings: {
306
+ API: await Worker("api", { entrypoint: "./api/worker.ts" }),
307
+ STORAGE: await R2Bucket("assets")
308
+ }
309
+ });
310
+ ```
311
+
312
+ ### Resource Scoping
313
+ ```typescript
314
+ // Organize resources into logical groups
315
+ await alchemy.run("backend", async () => {
316
+ await Worker("api", { entrypoint: "./api.ts" });
317
+ await KVNamespace("cache", { title: "api-cache" });
318
+ });
319
+
320
+ await alchemy.run("frontend", async () => {
321
+ await Vite("website", { main: "./src/worker.ts" });
322
+ });
323
+ ```
324
+
325
+ ### Testing Pattern
326
+ ```typescript
327
+ import { alchemy } from "../../src/alchemy";
328
+ const test = alchemy.test(import.meta, { prefix: "test" });
329
+
330
+ describe("Worker Tests", () => {
331
+ test("creates worker", async (scope) => {
332
+ const worker = await Worker("test-worker", {
333
+ entrypoint: "./src/worker.ts"
334
+ });
335
+
336
+ expect(worker.url).toBeTruthy();
337
+ // Resources auto-cleaned after test
338
+ });
339
+ });
340
+ ```
341
+
342
+ ## Best Practices
343
+
344
+ 1. **Always call finalize()**: `await app.finalize()` at the end of your script
345
+ 2. **Use adoption for migrations**: `adopt: true` when converting existing projects
346
+ 3. **Type-safe bindings**: Set up env.d.ts for full type safety
347
+ 4. **Resource naming**: Use consistent, descriptive names for resources
348
+ 5. **State store**: Use DOStateStore for production deployments
349
+ 6. **Secrets**: Use `alchemy.secret()` for sensitive values
350
+ 7. **Scoping**: Organize related resources using `alchemy.run()`
351
+ 8. **Testing**: Use `alchemy.test()` for integration tests
352
+
353
+ ## Common Issues
354
+
355
+ - **Missing finalize()**: Resources won't be cleaned up properly
356
+ - **Wrong DurableObject syntax**: Use `new DurableObjectNamespace()` not `await`
357
+ - **Type errors**: Set up env.d.ts file for binding types
358
+ - **State conflicts**: Use different stages/prefixes for different environments
359
+ - **Resource adoption**: Use `adopt: true` when migrating existing resources
360
+
361
+ ## Development Workflow
362
+
363
+ ```bash
364
+ # Create new project
365
+ bunx alchemy create my-app --template=vite
366
+ cd my-app
367
+
368
+ # Set up authentication (one-time)
369
+ bun wrangler login
370
+
371
+ # Deploy infrastructure
372
+ bun run deploy
373
+
374
+ # Local development
375
+ bun run dev
376
+
377
+ # Clean up
378
+ bun run destroy
379
+ ```
package/src/llms.ts ADDED
@@ -0,0 +1,25 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ export interface AlchemyRule {
5
+ name: string;
6
+ rule: string;
7
+ description?: string;
8
+ alwaysApply?: boolean;
9
+ globs?: string | string[];
10
+ }
11
+
12
+ const alchemy: AlchemyRule = {
13
+ name: "cloudflare",
14
+ description: "Core Alchemy development guidelines and patterns",
15
+ alwaysApply: true,
16
+ globs: ["*.ts"],
17
+ rule: await fs.readFile(
18
+ path.join(import.meta.dirname, "llms.cloudflare.txt"),
19
+ "utf-8",
20
+ ),
21
+ };
22
+
23
+ const alchemyRules: AlchemyRule[] = [alchemy];
24
+
25
+ export default alchemyRules;
@@ -1,4 +1,5 @@
1
1
  export * from "./api.ts";
2
- export * from "./utils.ts";
3
- export * from "./database.ts";
4
2
  export * from "./branch.ts";
3
+ export * from "./database.ts";
4
+ export * from "./password.ts";
5
+ export * from "./utils.ts";