@mastra/deployer-cloudflare 0.0.1-alpha.8 → 0.1.0-alpha.37

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.8",
3
+ "version": "0.1.0-alpha.37",
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,28 +20,29 @@
23
20
  "author": "",
24
21
  "license": "ISC",
25
22
  "dependencies": {
23
+ "@rollup/plugin-virtual": "^3.0.2",
26
24
  "cloudflare": "^4.0.0",
27
25
  "date-fns": "^4.1.0",
28
26
  "dotenv": "^16.3.1",
29
27
  "execa": "^9.3.1",
28
+ "rollup-plugin-polyfill-node": "^0.13.0",
29
+ "rollup-plugin-shim": "^1.0.0",
30
30
  "wrangler": "^3.103.2",
31
31
  "zod": "^3.24.1",
32
- "@mastra/core": "0.1.27-alpha.69",
33
- "@mastra/deployer": "0.0.1-alpha.7"
32
+ "@mastra/core": "^0.2.0-alpha.87",
33
+ "@mastra/deployer": "^0.1.0-alpha.33"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@babel/preset-env": "^7.26.0",
37
37
  "@babel/preset-typescript": "^7.26.0",
38
38
  "@tsconfig/recommended": "^1.0.7",
39
- "@types/jsdom": "^21.1.7",
40
39
  "@types/node": "^22.9.0",
41
- "@types/pg": "^8.11.10",
42
- "dts-cli": "^2.0.5",
43
- "vitest": "^2.1.8"
40
+ "tsup": "^8.0.1",
41
+ "vitest": "^3.0.4"
44
42
  },
45
43
  "scripts": {
46
- "build": "dts build",
47
- "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",
48
46
  "test": "vitest run"
49
47
  }
50
48
  }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
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';
3
4
  import { Cloudflare } from 'cloudflare';
4
5
  import { writeFileSync } from 'fs';
5
6
  import { join } from 'path';
@@ -10,97 +11,160 @@ interface CFRoute {
10
11
  custom_domain?: boolean;
11
12
  }
12
13
 
13
- export class CloudflareDeployer extends MastraDeployer {
14
+ export class CloudflareDeployer extends Deployer {
14
15
  private cloudflare: Cloudflare | undefined;
15
16
  routes?: CFRoute[] = [];
16
17
  workerNamespace?: string;
18
+ scope: string;
19
+ env?: Record<string, any>;
20
+ projectName?: string;
21
+
17
22
  constructor({
18
23
  scope,
19
24
  env,
20
- projectName,
25
+ projectName = 'mastra',
21
26
  routes,
22
27
  workerNamespace,
23
28
  auth,
24
29
  }: {
25
30
  env?: Record<string, any>;
26
31
  scope: string;
27
- projectName: string;
32
+ projectName?: string;
28
33
  routes?: CFRoute[];
29
34
  workerNamespace?: string;
30
- auth?: {
35
+ auth: {
31
36
  apiToken: string;
32
- apiEmail: string;
37
+ apiEmail?: string;
33
38
  };
34
39
  }) {
35
- super({ scope, env, projectName });
40
+ super({ name: 'CLOUDFLARE' });
36
41
 
42
+ this.scope = scope;
43
+ this.projectName = projectName;
37
44
  this.routes = routes;
38
45
  this.workerNamespace = workerNamespace;
39
46
 
40
- if (auth) {
41
- this.cloudflare = new Cloudflare(auth);
47
+ if (env) {
48
+ this.env = env;
42
49
  }
50
+
51
+ this.cloudflare = new Cloudflare(auth);
52
+ }
53
+
54
+ async writePackageJson(outputDirectory: string) {
55
+ this.logger.debug(`Writing package.json`);
56
+ const pkgPath = join(outputDirectory, 'package.json');
57
+
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
+ );
43
81
  }
44
82
 
45
- writeFiles({ dir }: { dir: string }): void {
46
- this.loadEnvVars();
83
+ async writeFiles(outputDirectory: string): Promise<void> {
84
+ const env = await this.loadEnvVars();
47
85
 
48
- this.writeIndex({ dir });
86
+ const envsAsObject = Object.assign({}, Object.fromEntries(env.entries()), this.env);
49
87
 
50
- const cfWorkerName = this.projectName || 'mastra';
88
+ const cfWorkerName = this.projectName;
51
89
 
52
90
  const wranglerConfig: Record<string, any> = {
53
91
  name: cfWorkerName,
54
92
  main: 'index.mjs',
55
93
  compatibility_date: '2024-12-02',
56
94
  compatibility_flags: ['nodejs_compat'],
57
- build: {
58
- command: 'npm install',
59
- },
60
95
  observability: {
61
96
  logs: {
62
97
  enabled: true,
63
98
  },
64
99
  },
65
- vars: this.env,
100
+ vars: envsAsObject,
66
101
  };
67
102
 
68
103
  if (!this.workerNamespace && this.routes) {
69
104
  wranglerConfig.routes = this.routes;
70
105
  }
71
106
 
72
- writeFileSync(join(dir, 'wrangler.json'), JSON.stringify(wranglerConfig));
107
+ writeFileSync(join(outputDirectory, 'wrangler.json'), JSON.stringify(wranglerConfig));
73
108
  }
74
109
 
75
- writeIndex({ dir }: { dir: string }): void {
76
- writeFileSync(
77
- join(dir, './index.mjs'),
78
- `
79
- export default {
80
- fetch: async (request, env, context) => {
81
- Object.keys(env).forEach(key => {
82
- process.env[key] = env[key]
83
- })
84
- const { app } = await import('./hono.mjs');
85
- return app.fetch(request, env, context);
86
- }
87
- }
88
- `,
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' },
89
136
  );
137
+
138
+ await bundler.write({
139
+ inlineDynamicImports: true,
140
+ file: join(outputDirectory, 'index.mjs'),
141
+ format: 'es',
142
+ });
90
143
  }
91
144
 
92
- 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> {
93
151
  const cmd = this.workerNamespace
94
152
  ? `npm exec -- wrangler deploy --dispatch-namespace ${this.workerNamespace}`
95
153
  : 'npm exec -- wrangler deploy';
96
- child_process.execSync(cmd, {
97
- cwd: dir,
98
- stdio: 'inherit',
154
+
155
+ const cpLogger = createChildProcessLogger({
156
+ logger: this.logger,
157
+ root: outputDirectory,
158
+ });
159
+
160
+ await cpLogger({
161
+ cmd,
162
+ args: [],
99
163
  env: {
100
- CLOUDFLARE_API_TOKEN: token,
164
+ CLOUDFLARE_API_TOKEN: this.cloudflare!.apiToken!,
101
165
  CLOUDFLARE_ACCOUNT_ID: this.scope,
102
166
  ...this.env,
103
- PATH: process.env.PATH,
167
+ PATH: process.env.PATH!,
104
168
  },
105
169
  });
106
170
  }
@@ -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
  });