alchemy 0.43.5 → 0.44.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/bin/alchemy.mjs +430 -343
- package/bin/alchemy.ts +7 -21
- package/bin/commands/create.ts +79 -23
- package/bin/services/template-manager.ts +9 -4
- package/bin/services/vibe-rules.ts +47 -0
- package/bin/types.ts +25 -5
- package/lib/alchemy.d.ts.map +1 -1
- package/lib/alchemy.js +8 -11
- package/lib/alchemy.js.map +1 -1
- package/lib/apply.js +2 -0
- package/lib/apply.js.map +1 -1
- package/lib/build-date.d.ts +1 -1
- package/lib/build-date.js +1 -1
- package/lib/cloudflare/assets.d.ts +1 -1
- package/lib/cloudflare/assets.d.ts.map +1 -1
- package/lib/cloudflare/bucket.d.ts.map +1 -1
- package/lib/cloudflare/bucket.js +9 -6
- package/lib/cloudflare/bucket.js.map +1 -1
- package/lib/cloudflare/bundle/bundle-worker-dev.d.ts.map +1 -1
- package/lib/cloudflare/bundle/bundle-worker-dev.js +4 -1
- package/lib/cloudflare/bundle/bundle-worker-dev.js.map +1 -1
- package/lib/cloudflare/bundle/bundle-worker.d.ts.map +1 -1
- package/lib/cloudflare/bundle/bundle-worker.js +4 -1
- package/lib/cloudflare/bundle/bundle-worker.js.map +1 -1
- package/lib/cloudflare/bundle/external.d.ts +1 -0
- package/lib/cloudflare/bundle/external.d.ts.map +1 -1
- package/lib/cloudflare/bundle/external.js +1 -1
- package/lib/cloudflare/bundle/external.js.map +1 -1
- package/lib/cloudflare/bundle/nodejs-import-warning-plugin.d.ts +10 -0
- package/lib/cloudflare/bundle/nodejs-import-warning-plugin.d.ts.map +1 -0
- package/lib/cloudflare/bundle/nodejs-import-warning-plugin.js +67 -0
- package/lib/cloudflare/bundle/nodejs-import-warning-plugin.js.map +1 -0
- package/lib/cloudflare/container.d.ts +27 -2
- package/lib/cloudflare/container.d.ts.map +1 -1
- package/lib/cloudflare/container.js +82 -24
- package/lib/cloudflare/container.js.map +1 -1
- package/lib/cloudflare/redirect-rule.js +1 -1
- package/lib/cloudflare/redirect-rule.js.map +1 -1
- package/lib/cloudflare/website.d.ts +17 -0
- package/lib/cloudflare/website.d.ts.map +1 -1
- package/lib/cloudflare/website.js +1 -0
- package/lib/cloudflare/website.js.map +1 -1
- package/lib/cloudflare/worker/miniflare.d.ts +4 -1
- package/lib/cloudflare/worker/miniflare.d.ts.map +1 -1
- package/lib/cloudflare/worker/miniflare.js +22 -2
- package/lib/cloudflare/worker/miniflare.js.map +1 -1
- package/lib/cloudflare/worker-metadata.d.ts.map +1 -1
- package/lib/cloudflare/worker-metadata.js +12 -0
- package/lib/cloudflare/worker-metadata.js.map +1 -1
- package/lib/cloudflare/worker.d.ts +1 -1
- package/lib/cloudflare/wrangler.json.d.ts +24 -0
- package/lib/cloudflare/wrangler.json.d.ts.map +1 -1
- package/lib/cloudflare/wrangler.json.js +20 -2
- package/lib/cloudflare/wrangler.json.js.map +1 -1
- package/lib/context.d.ts +6 -1
- package/lib/context.d.ts.map +1 -1
- package/lib/context.js +2 -1
- package/lib/context.js.map +1 -1
- package/lib/destroy.d.ts.map +1 -1
- package/lib/destroy.js +1 -0
- package/lib/destroy.js.map +1 -1
- package/lib/docker/api.d.ts +11 -0
- package/lib/docker/api.d.ts.map +1 -1
- package/lib/docker/api.js +41 -1
- package/lib/docker/api.js.map +1 -1
- package/lib/docker/image.d.ts +1 -1
- package/lib/docker/image.d.ts.map +1 -1
- package/lib/docker/image.js +11 -5
- package/lib/docker/image.js.map +1 -1
- package/lib/esbuild/bundle.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/llms.cloudflare.txt +379 -0
- package/lib/llms.d.ts +10 -0
- package/lib/llms.d.ts.map +1 -0
- package/lib/llms.js +12 -0
- package/lib/llms.js.map +1 -0
- package/lib/planetscale/index.d.ts +3 -2
- package/lib/planetscale/index.d.ts.map +1 -1
- package/lib/planetscale/index.js +3 -2
- package/lib/planetscale/index.js.map +1 -1
- package/lib/planetscale/password.d.ts +233 -0
- package/lib/planetscale/password.d.ts.map +1 -0
- package/lib/planetscale/password.js +238 -0
- package/lib/planetscale/password.js.map +1 -0
- package/lib/scope.d.ts +6 -5
- package/lib/scope.d.ts.map +1 -1
- package/lib/scope.js +18 -7
- package/lib/scope.js.map +1 -1
- package/lib/test/bun.d.ts.map +1 -1
- package/lib/test/bun.js +1 -0
- package/lib/test/bun.js.map +1 -1
- package/lib/test/vitest.d.ts.map +1 -1
- package/lib/test/vitest.js +1 -0
- package/lib/test/vitest.js.map +1 -1
- package/lib/util/nanoid.d.ts +3 -0
- package/lib/util/nanoid.d.ts.map +1 -0
- package/lib/util/nanoid.js +13 -0
- package/lib/util/nanoid.js.map +1 -0
- package/lib/util/safe-fetch.d.ts.map +1 -1
- package/lib/util/safe-fetch.js +0 -1
- package/lib/util/safe-fetch.js.map +1 -1
- package/lib/util/telemetry/client.d.ts.map +1 -1
- package/lib/util/telemetry/client.js +13 -3
- package/lib/util/telemetry/client.js.map +1 -1
- package/lib/util/telemetry/constants.d.ts +2 -1
- package/lib/util/telemetry/constants.d.ts.map +1 -1
- package/lib/util/telemetry/constants.js +3 -2
- package/lib/util/telemetry/constants.js.map +1 -1
- package/package.json +10 -2
- package/src/alchemy.ts +8 -13
- package/src/apply.ts +2 -0
- package/src/build-date.ts +1 -1
- package/src/cloudflare/bucket.ts +22 -14
- package/src/cloudflare/bundle/bundle-worker-dev.ts +4 -1
- package/src/cloudflare/bundle/bundle-worker.ts +4 -1
- package/src/cloudflare/bundle/external.ts +1 -1
- package/src/cloudflare/bundle/nodejs-import-warning-plugin.ts +94 -0
- package/src/cloudflare/container.ts +113 -28
- package/src/cloudflare/redirect-rule.ts +1 -1
- package/src/cloudflare/website.ts +21 -1
- package/src/cloudflare/worker/miniflare.ts +33 -8
- package/src/cloudflare/worker-metadata.ts +15 -0
- package/src/cloudflare/wrangler.json.ts +61 -5
- package/src/context.ts +7 -0
- package/src/destroy.ts +1 -0
- package/src/docker/api.ts +57 -1
- package/src/docker/image.ts +15 -6
- package/src/index.ts +1 -0
- package/src/llms.cloudflare.txt +379 -0
- package/src/llms.ts +25 -0
- package/src/planetscale/index.ts +3 -2
- package/src/planetscale/password.ts +366 -0
- package/src/scope.ts +25 -11
- package/src/test/bun.ts +1 -0
- package/src/test/vitest.ts +1 -0
- package/src/util/nanoid.ts +13 -0
- package/src/util/safe-fetch.ts +0 -1
- package/src/util/telemetry/client.ts +17 -3
- package/src/util/telemetry/constants.ts +5 -3
package/src/docker/api.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
1
2
|
import { spawn } from "node:child_process";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
2
5
|
import { exec } from "../os/exec.ts";
|
|
3
6
|
|
|
4
7
|
/**
|
|
@@ -9,6 +12,16 @@ export interface DockerApiOptions {
|
|
|
9
12
|
* Custom path to Docker binary
|
|
10
13
|
*/
|
|
11
14
|
dockerPath?: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Optional directory that will be used as the Docker CLI configuration
|
|
18
|
+
* directory (equivalent to setting the DOCKER_CONFIG environment variable).
|
|
19
|
+
*
|
|
20
|
+
* This makes authentication actions like `docker login` operate on an
|
|
21
|
+
* isolated credentials store which avoids race-conditions when multiple
|
|
22
|
+
* processes manipulate the default global config simultaneously.
|
|
23
|
+
*/
|
|
24
|
+
configDir?: string;
|
|
12
25
|
}
|
|
13
26
|
|
|
14
27
|
type VolumeInfo = {
|
|
@@ -27,6 +40,8 @@ type VolumeInfo = {
|
|
|
27
40
|
export class DockerApi {
|
|
28
41
|
/** Path to Docker CLI */
|
|
29
42
|
readonly dockerPath: string;
|
|
43
|
+
/** Directory to use for Docker CLI config */
|
|
44
|
+
readonly configDir?: string;
|
|
30
45
|
|
|
31
46
|
/**
|
|
32
47
|
* Create a new Docker API client
|
|
@@ -35,6 +50,7 @@ export class DockerApi {
|
|
|
35
50
|
*/
|
|
36
51
|
constructor(options: DockerApiOptions = {}) {
|
|
37
52
|
this.dockerPath = options.dockerPath || "docker";
|
|
53
|
+
this.configDir = options.configDir;
|
|
38
54
|
}
|
|
39
55
|
|
|
40
56
|
/**
|
|
@@ -44,11 +60,17 @@ export class DockerApi {
|
|
|
44
60
|
* @returns Result of the command
|
|
45
61
|
*/
|
|
46
62
|
async exec(args: string[]): Promise<{ stdout: string; stderr: string }> {
|
|
63
|
+
// If a custom config directory is provided, ensure all commands use it by
|
|
64
|
+
// setting the DOCKER_CONFIG env variable for the spawned process.
|
|
65
|
+
const env = this.configDir
|
|
66
|
+
? { ...process.env, DOCKER_CONFIG: this.configDir }
|
|
67
|
+
: process.env;
|
|
68
|
+
|
|
47
69
|
const command = `${this.dockerPath} ${args.join(" ")}`;
|
|
48
70
|
const result = (await exec(command, {
|
|
49
71
|
captureOutput: true,
|
|
50
72
|
shell: true,
|
|
51
|
-
env
|
|
73
|
+
env,
|
|
52
74
|
})) as { stdout: string; stderr: string };
|
|
53
75
|
|
|
54
76
|
return result;
|
|
@@ -377,6 +399,27 @@ export class DockerApi {
|
|
|
377
399
|
username: string,
|
|
378
400
|
password: string,
|
|
379
401
|
): Promise<void> {
|
|
402
|
+
// If we have a custom config directory, write credentials directly to
|
|
403
|
+
// config.json to avoid race conditions with the global credential store
|
|
404
|
+
if (this.configDir) {
|
|
405
|
+
const authConfigPath = path.join(this.configDir, "config.json");
|
|
406
|
+
const authToken = Buffer.from(`${username}:${password}`).toString(
|
|
407
|
+
"base64",
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
const configJson = {
|
|
411
|
+
auths: {
|
|
412
|
+
[registry]: {
|
|
413
|
+
auth: authToken,
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
await fs.writeFile(authConfigPath, JSON.stringify(configJson));
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Fallback to original docker login behavior for backwards compatibility
|
|
380
423
|
return new Promise((resolve, reject) => {
|
|
381
424
|
const args = [
|
|
382
425
|
"login",
|
|
@@ -429,6 +472,19 @@ export class DockerApi {
|
|
|
429
472
|
* @param registry Registry URL
|
|
430
473
|
*/
|
|
431
474
|
async logout(registry: string): Promise<void> {
|
|
475
|
+
// If we have a custom config directory, we can just remove the auth entry
|
|
476
|
+
// or delete the config file entirely since it's isolated
|
|
477
|
+
if (this.configDir) {
|
|
478
|
+
try {
|
|
479
|
+
const authConfigPath = path.join(this.configDir, "config.json");
|
|
480
|
+
await fs.unlink(authConfigPath);
|
|
481
|
+
} catch {
|
|
482
|
+
// Ignore errors - file might not exist or already be deleted
|
|
483
|
+
}
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Fallback to original docker logout behavior
|
|
432
488
|
try {
|
|
433
489
|
await this.exec(["logout", registry]);
|
|
434
490
|
} catch (error) {
|
package/src/docker/image.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import type { Context } from "../context.ts";
|
|
4
5
|
import { Resource } from "../resource.ts";
|
|
@@ -31,7 +32,7 @@ export interface DockerBuildOptions {
|
|
|
31
32
|
/**
|
|
32
33
|
* Build arguments as key-value pairs
|
|
33
34
|
*/
|
|
34
|
-
|
|
35
|
+
args?: Record<string, string>;
|
|
35
36
|
|
|
36
37
|
/**
|
|
37
38
|
* Target build stage in multi-stage builds
|
|
@@ -134,8 +135,16 @@ export const Image = Resource(
|
|
|
134
135
|
id: string,
|
|
135
136
|
props: ImageProps,
|
|
136
137
|
): Promise<Image> {
|
|
137
|
-
//
|
|
138
|
-
|
|
138
|
+
// Create a temporary directory that will act as an isolated Docker config
|
|
139
|
+
// (credentials) directory. This prevents race-conditions when multiple
|
|
140
|
+
// concurrent tests perform `docker login` / `logout` by ensuring each
|
|
141
|
+
// Image operation has its own credential store.
|
|
142
|
+
const tempConfigDir = await fs.mkdtemp(
|
|
143
|
+
path.join(os.tmpdir(), "docker-config-"),
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
// Initialize Docker API client with the isolated config directory
|
|
147
|
+
const api = new DockerApi({ configDir: tempConfigDir });
|
|
139
148
|
|
|
140
149
|
if (this.phase === "delete") {
|
|
141
150
|
// No action needed for delete as Docker images aren't automatically removed
|
|
@@ -166,7 +175,7 @@ export const Image = Resource(
|
|
|
166
175
|
await fs.access(dockerfile);
|
|
167
176
|
|
|
168
177
|
// Prepare build options
|
|
169
|
-
const buildOptions: Record<string, string> = props.build?.
|
|
178
|
+
const buildOptions: Record<string, string> = props.build?.args || {};
|
|
170
179
|
|
|
171
180
|
// Add platform if specified
|
|
172
181
|
let buildArgs = ["build", "-t", imageRef];
|
|
@@ -223,7 +232,7 @@ export const Image = Resource(
|
|
|
223
232
|
: `${registryHost}/${imageRef}`;
|
|
224
233
|
|
|
225
234
|
try {
|
|
226
|
-
// Authenticate to registry
|
|
235
|
+
// Authenticate to registry using the isolated config directory
|
|
227
236
|
await api.login(registryHost, username, password.unencrypted);
|
|
228
237
|
|
|
229
238
|
// Tag local image with fully qualified name if necessary
|
|
@@ -251,7 +260,7 @@ export const Image = Resource(
|
|
|
251
260
|
// Update the final image reference to point at the pushed image
|
|
252
261
|
finalImageRef = targetImage;
|
|
253
262
|
} finally {
|
|
254
|
-
//
|
|
263
|
+
// Clean up credentials from the isolated config
|
|
255
264
|
await api.logout(registryHost);
|
|
256
265
|
}
|
|
257
266
|
}
|
package/src/index.ts
CHANGED
|
@@ -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;
|