@mastra/deployer-cloudflare 0.0.1-alpha.8 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,246 @@
1
1
  # @mastra/deployer-cloudflare
2
2
 
3
+ ## 0.1.0-alpha.36
4
+
5
+ ### Minor Changes
6
+
7
+ - 5916f9d: Update deps from fixed to ^
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [6fa4bd2]
12
+ - Updated dependencies [5916f9d]
13
+ - Updated dependencies [e2e76de]
14
+ - Updated dependencies [7f24c29]
15
+ - Updated dependencies [67637ba]
16
+ - Updated dependencies [04f3171]
17
+ - @mastra/core@0.2.0-alpha.86
18
+ - @mastra/deployer@0.1.0-alpha.32
19
+
20
+ ## 0.0.1-alpha.35
21
+
22
+ ### Patch Changes
23
+
24
+ - Updated dependencies [e9d1b47]
25
+ - Updated dependencies [c5f2d50]
26
+ - @mastra/core@0.2.0-alpha.85
27
+ - @mastra/deployer@0.0.1-alpha.31
28
+
29
+ ## 0.0.1-alpha.34
30
+
31
+ ### Patch Changes
32
+
33
+ - 32d15ec: Fix cloudflareDeployer build
34
+
35
+ ## 0.0.1-alpha.33
36
+
37
+ ### Patch Changes
38
+
39
+ - e27fe69: Add dir to deployer
40
+ - Updated dependencies [e27fe69]
41
+ - @mastra/deployer@0.0.1-alpha.30
42
+
43
+ ## 0.0.1-alpha.32
44
+
45
+ ### Patch Changes
46
+
47
+ - 38b7f66: Update deployer logic
48
+ - Updated dependencies [2f17a5f]
49
+ - Updated dependencies [0696eeb]
50
+ - Updated dependencies [cb290ee]
51
+ - Updated dependencies [b4d7416]
52
+ - Updated dependencies [38b7f66]
53
+ - @mastra/core@0.2.0-alpha.84
54
+ - @mastra/deployer@0.0.1-alpha.29
55
+
56
+ ## 0.0.1-alpha.31
57
+
58
+ ### Patch Changes
59
+
60
+ - 9625602: Use mastra core splitted bundles in other packages
61
+ - Updated dependencies [2ab57d6]
62
+ - Updated dependencies [30322ce]
63
+ - Updated dependencies [78eec7c]
64
+ - Updated dependencies [9625602]
65
+ - Updated dependencies [8769a62]
66
+ - @mastra/deployer@0.0.1-alpha.28
67
+ - @mastra/core@0.2.0-alpha.83
68
+
69
+ ## 0.0.1-alpha.30
70
+
71
+ ### Patch Changes
72
+
73
+ - Updated dependencies [73d112c]
74
+ - Updated dependencies [ac8c61a]
75
+ - @mastra/deployer@0.0.1-alpha.27
76
+ - @mastra/core@0.1.27-alpha.82
77
+
78
+ ## 0.0.1-alpha.29
79
+
80
+ ### Patch Changes
81
+
82
+ - Updated dependencies [9fb3039]
83
+ - @mastra/core@0.1.27-alpha.81
84
+ - @mastra/deployer@0.0.1-alpha.26
85
+
86
+ ## 0.0.1-alpha.28
87
+
88
+ ### Patch Changes
89
+
90
+ - Updated dependencies [327ece7]
91
+ - @mastra/core@0.1.27-alpha.80
92
+ - @mastra/deployer@0.0.1-alpha.25
93
+
94
+ ## 0.0.1-alpha.27
95
+
96
+ ### Patch Changes
97
+
98
+ - Updated dependencies [21fe536]
99
+ - @mastra/core@0.1.27-alpha.79
100
+ - @mastra/deployer@0.0.1-alpha.24
101
+
102
+ ## 0.0.1-alpha.26
103
+
104
+ ### Patch Changes
105
+
106
+ - Updated dependencies [88f18d7]
107
+ - @mastra/deployer@0.0.1-alpha.23
108
+
109
+ ## 0.0.1-alpha.25
110
+
111
+ ### Patch Changes
112
+
113
+ - 44c7c26: Rebuild
114
+
115
+ ## 0.0.1-alpha.24
116
+
117
+ ### Patch Changes
118
+
119
+ - Updated dependencies [685108a]
120
+ - Updated dependencies [685108a]
121
+ - @mastra/deployer@0.0.1-alpha.22
122
+ - @mastra/core@0.1.27-alpha.78
123
+
124
+ ## 0.0.1-alpha.23
125
+
126
+ ### Patch Changes
127
+
128
+ - 2b75edf: mastra deployers tsup bundling
129
+ - Updated dependencies [8105fae]
130
+ - Updated dependencies [cfb966f]
131
+ - @mastra/core@0.1.27-alpha.77
132
+ - @mastra/deployer@0.0.1-alpha.21
133
+
134
+ ## 0.0.1-alpha.22
135
+
136
+ ### Patch Changes
137
+
138
+ - Updated dependencies [ae7bf94]
139
+ - Updated dependencies [ae7bf94]
140
+ - @mastra/deployer@0.0.1-alpha.20
141
+ - @mastra/core@0.1.27-alpha.76
142
+
143
+ ## 0.0.1-alpha.21
144
+
145
+ ### Patch Changes
146
+
147
+ - Updated dependencies [23dcb23]
148
+ - Updated dependencies [7064554]
149
+ - @mastra/core@0.1.27-alpha.75
150
+ - @mastra/deployer@0.0.1-alpha.19
151
+
152
+ ## 0.0.1-alpha.20
153
+
154
+ ### Patch Changes
155
+
156
+ - Updated dependencies [7b87567]
157
+ - @mastra/core@0.1.27-alpha.74
158
+ - @mastra/deployer@0.0.1-alpha.18
159
+
160
+ ## 0.0.1-alpha.19
161
+
162
+ ### Patch Changes
163
+
164
+ - Updated dependencies [3427b95]
165
+ - @mastra/core@0.1.27-alpha.73
166
+ - @mastra/deployer@0.0.1-alpha.17
167
+
168
+ ## 0.0.1-alpha.18
169
+
170
+ ### Patch Changes
171
+
172
+ - Updated dependencies [e4d4ede]
173
+ - Updated dependencies [06b2c0a]
174
+ - @mastra/core@0.1.27-alpha.72
175
+ - @mastra/deployer@0.0.1-alpha.16
176
+
177
+ ## 0.0.1-alpha.17
178
+
179
+ ### Patch Changes
180
+
181
+ - Updated dependencies [d9c8dd0]
182
+ - @mastra/deployer@0.0.1-alpha.15
183
+ - @mastra/core@0.1.27-alpha.71
184
+
185
+ ## 0.0.1-alpha.16
186
+
187
+ ### Patch Changes
188
+
189
+ - Updated dependencies [ad2cd74]
190
+ - @mastra/deployer@0.0.1-alpha.14
191
+
192
+ ## 0.0.1-alpha.15
193
+
194
+ ### Patch Changes
195
+
196
+ - Updated dependencies [a1774e7]
197
+ - @mastra/deployer@0.0.1-alpha.13
198
+
199
+ ## 0.0.1-alpha.14
200
+
201
+ ### Patch Changes
202
+
203
+ - Updated dependencies [28dceab]
204
+ - @mastra/deployer@0.0.1-alpha.12
205
+
206
+ ## 0.0.1-alpha.13
207
+
208
+ ### Patch Changes
209
+
210
+ - bdaf834: publish packages
211
+ - Updated dependencies [bdaf834]
212
+ - @mastra/deployer@0.0.1-alpha.11
213
+
214
+ ## 0.0.1-alpha.12
215
+
216
+ ### Patch Changes
217
+
218
+ - Updated dependencies [dd6d87f]
219
+ - Updated dependencies [04434b6]
220
+ - @mastra/core@0.1.27-alpha.70
221
+ - @mastra/deployer@0.0.1-alpha.10
222
+
223
+ ## 0.0.1-alpha.11
224
+
225
+ ### Patch Changes
226
+
227
+ - 9066f95: CF deployer fixes
228
+ - Updated dependencies [9066f95]
229
+ - @mastra/deployer@0.0.1-alpha.9
230
+
231
+ ## 0.0.1-alpha.10
232
+
233
+ ### Patch Changes
234
+
235
+ - 0d5a03d: Vector store modules
236
+
237
+ ## 0.0.1-alpha.9
238
+
239
+ ### Patch Changes
240
+
241
+ - Updated dependencies [b425845]
242
+ - @mastra/deployer@0.0.1-alpha.8
243
+
3
244
  ## 0.0.1-alpha.8
4
245
 
5
246
  ### Patch Changes
package/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # @mastra/deployer-cloudflare
2
+
3
+ A Cloudflare Workers deployer for Mastra applications.
4
+
5
+ ## Features
6
+
7
+ - Deploy Mastra applications to Cloudflare Workers
8
+ - Configure custom domains and routes
9
+ - Support for worker namespaces
10
+ - Automatic environment variable configuration
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ pnpm add @mastra/deployer-cloudflare
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ The Cloudflare deployer is used as part of the Mastra framework:
21
+
22
+ ```typescript
23
+ import { Mastra } from '@mastra/core';
24
+ import { CloudflareDeployer } from '@mastra/deployer-cloudflare';
25
+
26
+ const deployer = new CloudflareDeployer({
27
+ scope: 'your-account-id',
28
+ projectName: 'your-project-name',
29
+ routes: [
30
+ {
31
+ pattern: 'example.com/*',
32
+ zone_name: 'example.com',
33
+ custom_domain: true,
34
+ },
35
+ ],
36
+ workerNamespace: 'your-namespace',
37
+ auth: {
38
+ apiToken: 'your-api-token',
39
+ apiEmail: 'your-email',
40
+ },
41
+ });
42
+
43
+ const mastra = new Mastra({
44
+ deployer,
45
+ // ... other Mastra configuration options
46
+ });
47
+ ```
48
+
49
+ ## Configuration
50
+
51
+ ### Constructor Options
52
+
53
+ - `scope` (required): Your Cloudflare account ID
54
+ - `projectName`: Name of your worker project
55
+ - `routes`: Array of route configurations for your worker
56
+ - `pattern`: URL pattern to match
57
+ - `zone_name`: Domain zone name
58
+ - `custom_domain`: Whether to use a custom domain
59
+ - `workerNamespace`: Namespace for your worker
60
+ - `auth`: Cloudflare authentication details
61
+ - `apiToken`: Your Cloudflare API token
62
+ - `apiEmail`: Your Cloudflare account email
63
+
64
+ ## Environment Variables
65
+
66
+ The deployer will automatically load environment variables from:
67
+
68
+ - `.env` files in your project
69
+ - Environment variables passed through the Mastra configuration
70
+
71
+ ## Routes
72
+
73
+ Routes can be configured to direct traffic to your worker based on URL patterns and domains:
74
+
75
+ ```typescript
76
+ const routes = [
77
+ {
78
+ pattern: 'api.example.com/*',
79
+ zone_name: 'example.com',
80
+ custom_domain: true,
81
+ },
82
+ {
83
+ pattern: 'example.com/api/*',
84
+ zone_name: 'example.com',
85
+ },
86
+ ];
87
+ ```
88
+
89
+ Each route requires:
90
+
91
+ - `pattern`: URL pattern to match
92
+ - `zone_name`: Domain zone name
93
+ - `custom_domain`: (optional) Set to true to use a custom domain
94
+
95
+ ## Requirements
96
+
97
+ - Cloudflare account with Workers enabled
98
+ - API token with appropriate permissions
99
+ - Domain(s) configured in Cloudflare (for custom domains)
package/dist/index.d.ts CHANGED
@@ -1,34 +1,34 @@
1
- import { MastraDeployer } from '@mastra/core';
1
+ import { Deployer } from '@mastra/deployer';
2
+
2
3
  interface CFRoute {
3
4
  pattern: string;
4
5
  zone_name: string;
5
6
  custom_domain?: boolean;
6
7
  }
7
- export declare class CloudflareDeployer extends MastraDeployer {
8
+ declare class CloudflareDeployer extends Deployer {
8
9
  private cloudflare;
9
10
  routes?: CFRoute[];
10
11
  workerNamespace?: string;
12
+ scope: string;
13
+ env?: Record<string, any>;
14
+ projectName?: string;
11
15
  constructor({ scope, env, projectName, routes, workerNamespace, auth, }: {
12
16
  env?: Record<string, any>;
13
17
  scope: string;
14
- projectName: string;
18
+ projectName?: string;
15
19
  routes?: CFRoute[];
16
20
  workerNamespace?: string;
17
- auth?: {
21
+ auth: {
18
22
  apiToken: string;
19
- apiEmail: string;
23
+ apiEmail?: string;
20
24
  };
21
25
  });
22
- writeFiles({ dir }: {
23
- dir: string;
24
- }): void;
25
- writeIndex({ dir }: {
26
- dir: string;
27
- }): void;
28
- deploy({ dir, token }: {
29
- dir: string;
30
- token: string;
31
- }): Promise<void>;
26
+ writePackageJson(outputDirectory: string): Promise<void>;
27
+ writeFiles(outputDirectory: string): Promise<void>;
28
+ private getEntry;
29
+ bundle(mastraDir: string, outputDirectory: string): Promise<void>;
30
+ prepare(outputDirectory: string): Promise<void>;
31
+ deploy(outputDirectory: string): Promise<void>;
32
32
  tagWorker({ workerName, namespace, tags, scope, }: {
33
33
  scope: string;
34
34
  workerName: string;
@@ -36,5 +36,5 @@ export declare class CloudflareDeployer extends MastraDeployer {
36
36
  tags: string[];
37
37
  }): Promise<void>;
38
38
  }
39
- export {};
40
- //# sourceMappingURL=index.d.ts.map
39
+
40
+ export { CloudflareDeployer };
package/dist/index.js CHANGED
@@ -1,8 +1,151 @@
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';
5
+ import { writeFileSync } from 'fs';
6
+ import { join } from 'path';
1
7
 
2
- 'use strict'
8
+ // src/index.ts
9
+ var CloudflareDeployer = class extends Deployer {
10
+ cloudflare;
11
+ routes = [];
12
+ workerNamespace;
13
+ scope;
14
+ env;
15
+ projectName;
16
+ constructor({
17
+ scope,
18
+ env,
19
+ projectName = "mastra",
20
+ routes,
21
+ workerNamespace,
22
+ auth
23
+ }) {
24
+ super({ name: "CLOUDFLARE" });
25
+ this.scope = scope;
26
+ this.projectName = projectName;
27
+ this.routes = routes;
28
+ this.workerNamespace = workerNamespace;
29
+ if (env) {
30
+ this.env = env;
31
+ }
32
+ this.cloudflare = new Cloudflare(auth);
33
+ }
34
+ async writePackageJson(outputDirectory) {
35
+ this.logger.debug(`Writing package.json`);
36
+ const pkgPath = join(outputDirectory, "package.json");
37
+ writeFileSync(
38
+ pkgPath,
39
+ JSON.stringify(
40
+ {
41
+ name: "server",
42
+ version: "1.0.0",
43
+ description: "",
44
+ type: "module",
45
+ main: "index.mjs",
46
+ scripts: {
47
+ start: "node ./index.mjs",
48
+ build: 'echo "Already built"'
49
+ },
50
+ author: "Mastra",
51
+ license: "ISC",
52
+ dependencies: {
53
+ "@mastra/core": "latest"
54
+ }
55
+ },
56
+ null,
57
+ 2
58
+ )
59
+ );
60
+ }
61
+ async writeFiles(outputDirectory) {
62
+ const env = await this.loadEnvVars();
63
+ const envsAsObject = Object.assign({}, Object.fromEntries(env.entries()), this.env);
64
+ const cfWorkerName = this.projectName;
65
+ const wranglerConfig = {
66
+ name: cfWorkerName,
67
+ main: "index.mjs",
68
+ compatibility_date: "2024-12-02",
69
+ compatibility_flags: ["nodejs_compat"],
70
+ observability: {
71
+ logs: {
72
+ enabled: true
73
+ }
74
+ },
75
+ vars: envsAsObject
76
+ };
77
+ if (!this.workerNamespace && this.routes) {
78
+ wranglerConfig.routes = this.routes;
79
+ }
80
+ writeFileSync(join(outputDirectory, "wrangler.json"), JSON.stringify(wranglerConfig));
81
+ }
82
+ getEntry() {
83
+ return `
84
+ export default {
85
+ fetch: async (request, env, context) => {
86
+ Object.keys(env).forEach(key => {
87
+ process.env[key] = env[key]
88
+ })
3
89
 
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./deployer-cloudflare.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./deployer-cloudflare.cjs.development.js')
90
+ const { mastra } = await import('#mastra')
91
+ const { createHonoServer } = await import('#server')
92
+ const app = await createHonoServer(mastra)
93
+ return app.fetch(request, env, context);
94
+ }
8
95
  }
96
+ `;
97
+ }
98
+ async bundle(mastraDir, outputDirectory) {
99
+ const bundler = await getBundler(
100
+ {
101
+ input: "#entry",
102
+ plugins: [virtual({ "#entry": this.getEntry() })],
103
+ external: [/^@opentelemetry\//],
104
+ treeshake: "smallest"
105
+ },
106
+ { platform: "browser" }
107
+ );
108
+ await bundler.write({
109
+ inlineDynamicImports: true,
110
+ file: join(outputDirectory, "index.mjs"),
111
+ format: "es"
112
+ });
113
+ }
114
+ async prepare(outputDirectory) {
115
+ await super.prepare(outputDirectory);
116
+ await this.writeFiles(outputDirectory);
117
+ }
118
+ async deploy(outputDirectory) {
119
+ const cmd = this.workerNamespace ? `npm exec -- wrangler deploy --dispatch-namespace ${this.workerNamespace}` : "npm exec -- wrangler deploy";
120
+ const cpLogger = createChildProcessLogger({
121
+ logger: this.logger,
122
+ root: outputDirectory
123
+ });
124
+ await cpLogger({
125
+ cmd,
126
+ args: [],
127
+ env: {
128
+ CLOUDFLARE_API_TOKEN: this.cloudflare.apiToken,
129
+ CLOUDFLARE_ACCOUNT_ID: this.scope,
130
+ ...this.env,
131
+ PATH: process.env.PATH
132
+ }
133
+ });
134
+ }
135
+ async tagWorker({
136
+ workerName,
137
+ namespace,
138
+ tags,
139
+ scope
140
+ }) {
141
+ if (!this.cloudflare) {
142
+ throw new Error("Cloudflare Deployer not initialized");
143
+ }
144
+ await this.cloudflare.workersForPlatforms.dispatch.namespaces.scripts.tags.update(namespace, workerName, {
145
+ account_id: scope,
146
+ body: tags
147
+ });
148
+ }
149
+ };
150
+
151
+ export { CloudflareDeployer };
@@ -1,4 +1,4 @@
1
- export declare class CloudflareSecretsManager {
1
+ declare class CloudflareSecretsManager {
2
2
  accountId: string;
3
3
  apiToken: string;
4
4
  baseUrl: string;
@@ -22,4 +22,5 @@ export declare class CloudflareSecretsManager {
22
22
  }): Promise<any>;
23
23
  listSecrets(workerId: string): Promise<any>;
24
24
  }
25
- //# sourceMappingURL=index.d.ts.map
25
+
26
+ export { CloudflareSecretsManager };
@@ -0,0 +1,87 @@
1
+ // src/secrets-manager/index.ts
2
+ var CloudflareSecretsManager = class {
3
+ accountId;
4
+ apiToken;
5
+ baseUrl;
6
+ constructor({ accountId, apiToken }) {
7
+ this.accountId = accountId;
8
+ this.apiToken = apiToken;
9
+ this.baseUrl = "https://api.cloudflare.com/client/v4";
10
+ }
11
+ async createSecret({
12
+ workerId,
13
+ secretName,
14
+ secretValue
15
+ }) {
16
+ const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets`;
17
+ try {
18
+ const response = await fetch(url, {
19
+ method: "PUT",
20
+ headers: {
21
+ Authorization: `Bearer ${this.apiToken}`,
22
+ "Content-Type": "application/json"
23
+ },
24
+ body: JSON.stringify({
25
+ name: secretName,
26
+ text: secretValue
27
+ })
28
+ });
29
+ const data = await response.json();
30
+ if (!data.success) {
31
+ throw new Error(data.errors[0].message);
32
+ }
33
+ return data.result;
34
+ } catch (error) {
35
+ console.error("Failed to create secret:", error);
36
+ throw error;
37
+ }
38
+ }
39
+ async createProjectSecrets({
40
+ workerId,
41
+ customerId,
42
+ envVars
43
+ }) {
44
+ const secretName = `PROJECT_${customerId.toUpperCase()}`;
45
+ const secretValue = JSON.stringify(envVars);
46
+ return this.createSecret({ workerId, secretName, secretValue });
47
+ }
48
+ async deleteSecret({ workerId, secretName }) {
49
+ const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets/${secretName}`;
50
+ try {
51
+ const response = await fetch(url, {
52
+ method: "DELETE",
53
+ headers: {
54
+ Authorization: `Bearer ${this.apiToken}`
55
+ }
56
+ });
57
+ const data = await response.json();
58
+ if (!data.success) {
59
+ throw new Error(data.errors[0].message);
60
+ }
61
+ return data.result;
62
+ } catch (error) {
63
+ console.error("Failed to delete secret:", error);
64
+ throw error;
65
+ }
66
+ }
67
+ async listSecrets(workerId) {
68
+ const url = `${this.baseUrl}/accounts/${this.accountId}/workers/scripts/${workerId}/secrets`;
69
+ try {
70
+ const response = await fetch(url, {
71
+ headers: {
72
+ Authorization: `Bearer ${this.apiToken}`
73
+ }
74
+ });
75
+ const data = await response.json();
76
+ if (!data.success) {
77
+ throw new Error(data.errors[0].message);
78
+ }
79
+ return data.result;
80
+ } catch (error) {
81
+ console.error("Failed to list secrets:", error);
82
+ throw error;
83
+ }
84
+ }
85
+ };
86
+
87
+ export { CloudflareSecretsManager };
package/global.d.ts ADDED
@@ -0,0 +1 @@
1
+ declare module 'rollup-plugin-shim';