@mastra/deployer-cloudflare 0.0.1-alpha.7 → 0.1.0-alpha.36

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/package.json CHANGED
@@ -1,21 +1,18 @@
1
1
  {
2
2
  "name": "@mastra/deployer-cloudflare",
3
- "version": "0.0.1-alpha.7",
3
+ "version": "0.1.0-alpha.36",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
- "module": "dist/deployer-cloudflare.esm.js",
8
7
  "types": "dist/index.d.ts",
9
8
  "exports": {
10
9
  ".": {
11
- "import": {
12
- "types": "./dist/index.d.ts",
13
- "default": "./dist/deployer-cloudflare.esm.js"
14
- },
15
- "require": {
16
- "types": "./dist/index.d.ts",
17
- "default": "./dist/index.js"
18
- }
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./secrets-manager": {
14
+ "types": "./dist/secrets-manager/index.d.ts",
15
+ "default": "./dist/secrets-manager/index.js"
19
16
  },
20
17
  "./package.json": "./package.json"
21
18
  },
@@ -23,27 +20,29 @@
23
20
  "author": "",
24
21
  "license": "ISC",
25
22
  "dependencies": {
23
+ "@rollup/plugin-virtual": "^3.0.2",
24
+ "cloudflare": "^4.0.0",
26
25
  "date-fns": "^4.1.0",
27
26
  "dotenv": "^16.3.1",
28
27
  "execa": "^9.3.1",
28
+ "rollup-plugin-polyfill-node": "^0.13.0",
29
+ "rollup-plugin-shim": "^1.0.0",
29
30
  "wrangler": "^3.103.2",
30
31
  "zod": "^3.24.1",
31
- "@mastra/core": "0.1.27-alpha.68",
32
- "@mastra/deployer": "0.0.1-alpha.6"
32
+ "@mastra/core": "^0.2.0-alpha.86",
33
+ "@mastra/deployer": "^0.1.0-alpha.32"
33
34
  },
34
35
  "devDependencies": {
35
36
  "@babel/preset-env": "^7.26.0",
36
37
  "@babel/preset-typescript": "^7.26.0",
37
38
  "@tsconfig/recommended": "^1.0.7",
38
- "@types/jsdom": "^21.1.7",
39
39
  "@types/node": "^22.9.0",
40
- "@types/pg": "^8.11.10",
41
- "dts-cli": "^2.0.5",
42
- "vitest": "^2.1.8"
40
+ "tsup": "^8.0.1",
41
+ "vitest": "^3.0.4"
43
42
  },
44
43
  "scripts": {
45
- "build": "dts build",
46
- "build:dev": "dts watch",
44
+ "build": "tsup-node src/index.ts src/secrets-manager/index.ts --format esm --dts --clean --treeshake",
45
+ "dev": "tsup-node src/index.ts src/secrets-manager/index.ts --format esm --dts --clean --treeshake --watch",
47
46
  "test": "vitest run"
48
47
  }
49
48
  }
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { MastraDeployer } from '@mastra/core';
2
- import * as child_process from 'child_process';
1
+ import { Deployer, createChildProcessLogger } from '@mastra/deployer';
2
+ import { getBundler } from '@mastra/deployer/build';
3
+ import virtual from '@rollup/plugin-virtual';
4
+ import { Cloudflare } from 'cloudflare';
3
5
  import { writeFileSync } from 'fs';
4
6
  import { join } from 'path';
5
7
 
@@ -9,88 +11,182 @@ interface CFRoute {
9
11
  custom_domain?: boolean;
10
12
  }
11
13
 
12
- export class CloudflareDeployer extends MastraDeployer {
14
+ export class CloudflareDeployer extends Deployer {
15
+ private cloudflare: Cloudflare | undefined;
13
16
  routes?: CFRoute[] = [];
14
17
  workerNamespace?: string;
18
+ scope: string;
19
+ env?: Record<string, any>;
20
+ projectName?: string;
21
+
15
22
  constructor({
16
23
  scope,
17
24
  env,
18
- projectName,
25
+ projectName = 'mastra',
19
26
  routes,
20
27
  workerNamespace,
28
+ auth,
21
29
  }: {
22
30
  env?: Record<string, any>;
23
31
  scope: string;
24
- projectName: string;
32
+ projectName?: string;
25
33
  routes?: CFRoute[];
26
34
  workerNamespace?: string;
35
+ auth: {
36
+ apiToken: string;
37
+ apiEmail?: string;
38
+ };
27
39
  }) {
28
- super({ scope, env, projectName });
40
+ super({ name: 'CLOUDFLARE' });
29
41
 
42
+ this.scope = scope;
43
+ this.projectName = projectName;
30
44
  this.routes = routes;
31
45
  this.workerNamespace = workerNamespace;
46
+
47
+ if (env) {
48
+ this.env = env;
49
+ }
50
+
51
+ this.cloudflare = new Cloudflare(auth);
32
52
  }
33
53
 
34
- writeFiles({ dir }: { dir: string }): void {
35
- this.loadEnvVars();
54
+ async writePackageJson(outputDirectory: string) {
55
+ this.logger.debug(`Writing package.json`);
56
+ const pkgPath = join(outputDirectory, 'package.json');
36
57
 
37
- this.writeIndex({ dir });
58
+ writeFileSync(
59
+ pkgPath,
60
+ JSON.stringify(
61
+ {
62
+ name: 'server',
63
+ version: '1.0.0',
64
+ description: '',
65
+ type: 'module',
66
+ main: 'index.mjs',
67
+ scripts: {
68
+ start: 'node ./index.mjs',
69
+ build: 'echo "Already built"',
70
+ },
71
+ author: 'Mastra',
72
+ license: 'ISC',
73
+ dependencies: {
74
+ '@mastra/core': 'latest',
75
+ },
76
+ },
77
+ null,
78
+ 2,
79
+ ),
80
+ );
81
+ }
38
82
 
39
- const cfWorkerName = this.projectName || 'mastra';
83
+ async writeFiles(outputDirectory: string): Promise<void> {
84
+ const env = await this.loadEnvVars();
85
+
86
+ const envsAsObject = Object.assign({}, Object.fromEntries(env.entries()), this.env);
87
+
88
+ const cfWorkerName = this.projectName;
40
89
 
41
90
  const wranglerConfig: Record<string, any> = {
42
91
  name: cfWorkerName,
43
92
  main: 'index.mjs',
44
93
  compatibility_date: '2024-12-02',
45
94
  compatibility_flags: ['nodejs_compat'],
46
- build: {
47
- command: 'npm install',
48
- },
49
95
  observability: {
50
96
  logs: {
51
97
  enabled: true,
52
98
  },
53
99
  },
54
- vars: this.env,
100
+ vars: envsAsObject,
55
101
  };
56
102
 
57
103
  if (!this.workerNamespace && this.routes) {
58
104
  wranglerConfig.routes = this.routes;
59
105
  }
60
106
 
61
- writeFileSync(join(dir, 'wrangler.json'), JSON.stringify(wranglerConfig));
107
+ writeFileSync(join(outputDirectory, 'wrangler.json'), JSON.stringify(wranglerConfig));
62
108
  }
63
109
 
64
- writeIndex({ dir }: { dir: string }): void {
65
- writeFileSync(
66
- join(dir, './index.mjs'),
67
- `
68
- export default {
69
- fetch: async (request, env, context) => {
70
- Object.keys(env).forEach(key => {
71
- process.env[key] = env[key]
72
- })
73
- const { app } = await import('./hono.mjs');
74
- return app.fetch(request, env, context);
75
- }
76
- }
77
- `,
110
+ private getEntry(): string {
111
+ return `
112
+ export default {
113
+ fetch: async (request, env, context) => {
114
+ Object.keys(env).forEach(key => {
115
+ process.env[key] = env[key]
116
+ })
117
+
118
+ const { mastra } = await import('#mastra')
119
+ const { createHonoServer } = await import('#server')
120
+ const app = await createHonoServer(mastra)
121
+ return app.fetch(request, env, context);
122
+ }
123
+ }
124
+ `;
125
+ }
126
+
127
+ async bundle(mastraDir: string, outputDirectory: string): Promise<void> {
128
+ const bundler = await getBundler(
129
+ {
130
+ input: '#entry',
131
+ plugins: [virtual({ '#entry': this.getEntry() })],
132
+ external: [/^@opentelemetry\//],
133
+ treeshake: 'smallest',
134
+ },
135
+ { platform: 'browser' },
78
136
  );
137
+
138
+ await bundler.write({
139
+ inlineDynamicImports: true,
140
+ file: join(outputDirectory, 'index.mjs'),
141
+ format: 'es',
142
+ });
79
143
  }
80
144
 
81
- async deploy({ dir, token }: { dir: string; token: string }): Promise<void> {
145
+ async prepare(outputDirectory: string): Promise<void> {
146
+ await super.prepare(outputDirectory);
147
+ await this.writeFiles(outputDirectory);
148
+ }
149
+
150
+ async deploy(outputDirectory: string): Promise<void> {
82
151
  const cmd = this.workerNamespace
83
152
  ? `npm exec -- wrangler deploy --dispatch-namespace ${this.workerNamespace}`
84
153
  : 'npm exec -- wrangler deploy';
85
- child_process.execSync(cmd, {
86
- cwd: dir,
87
- stdio: 'inherit',
154
+
155
+ const cpLogger = createChildProcessLogger({
156
+ logger: this.logger,
157
+ root: outputDirectory,
158
+ });
159
+
160
+ await cpLogger({
161
+ cmd,
162
+ args: [],
88
163
  env: {
89
- CLOUDFLARE_API_TOKEN: token,
164
+ CLOUDFLARE_API_TOKEN: this.cloudflare!.apiToken!,
90
165
  CLOUDFLARE_ACCOUNT_ID: this.scope,
91
166
  ...this.env,
92
- PATH: process.env.PATH,
167
+ PATH: process.env.PATH!,
93
168
  },
94
169
  });
95
170
  }
171
+
172
+ async tagWorker({
173
+ workerName,
174
+ namespace,
175
+ tags,
176
+ scope,
177
+ }: {
178
+ scope: string;
179
+ workerName: string;
180
+ namespace: string;
181
+ tags: string[];
182
+ }): Promise<void> {
183
+ if (!this.cloudflare) {
184
+ throw new Error('Cloudflare Deployer not initialized');
185
+ }
186
+
187
+ await this.cloudflare.workersForPlatforms.dispatch.namespaces.scripts.tags.update(namespace, workerName, {
188
+ account_id: scope,
189
+ body: tags,
190
+ });
191
+ }
96
192
  }
@@ -33,7 +33,7 @@ export class CloudflareSecretsManager {
33
33
  }),
34
34
  });
35
35
 
36
- const data = await response.json();
36
+ const data = (await response.json()) as { success: boolean; result: any; errors: any[] };
37
37
 
38
38
  if (!data.success) {
39
39
  throw new Error(data.errors[0].message);
@@ -72,7 +72,7 @@ export class CloudflareSecretsManager {
72
72
  },
73
73
  });
74
74
 
75
- const data = await response.json();
75
+ const data = (await response.json()) as { success: boolean; result: any; errors: any[] };
76
76
 
77
77
  if (!data.success) {
78
78
  throw new Error(data.errors[0].message);
@@ -95,7 +95,7 @@ export class CloudflareSecretsManager {
95
95
  },
96
96
  });
97
97
 
98
- const data = await response.json();
98
+ const data = (await response.json()) as { success: boolean; result: any; errors: any[] };
99
99
 
100
100
  if (!data.success) {
101
101
  throw new Error(data.errors[0].message);
package/tsconfig.json CHANGED
@@ -1,10 +1,5 @@
1
1
  {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "moduleResolution": "bundler",
5
- "outDir": "./dist",
6
- "rootDir": "./src"
7
- },
8
- "include": ["src/**/*"],
2
+ "extends": "../../tsconfig.node.json",
3
+ "include": ["./global.d.ts", "src/**/*"],
9
4
  "exclude": ["node_modules", "**/*.test.ts"]
10
5
  }
package/vitest.config.ts CHANGED
@@ -2,7 +2,7 @@ import { defineConfig } from 'vitest/config';
2
2
 
3
3
  export default defineConfig({
4
4
  test: {
5
- globals: true,
5
+ environment: 'node',
6
6
  include: ['src/**/*.test.ts'],
7
7
  },
8
8
  });