@mastra/deployer-cloudflare 0.0.1-alpha.1 → 0.0.1-alpha.11
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 +78 -0
- package/dist/deployer-cloudflare.cjs.development.js +479 -0
- package/dist/deployer-cloudflare.cjs.development.js.map +1 -0
- package/dist/deployer-cloudflare.cjs.production.min.js +2 -0
- package/dist/deployer-cloudflare.cjs.production.min.js.map +1 -0
- package/dist/deployer-cloudflare.esm.js +475 -0
- package/dist/deployer-cloudflare.esm.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/secrets-manager/index.d.ts +25 -0
- package/dist/secrets-manager/index.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/index.ts +76 -26
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MastraDeployer } from '@mastra/core';
|
|
2
|
-
import
|
|
2
|
+
import { createChildProcessLogger } from '@mastra/deployer';
|
|
3
|
+
import { Cloudflare } from 'cloudflare';
|
|
3
4
|
import { writeFileSync } from 'fs';
|
|
4
5
|
import { join } from 'path';
|
|
5
6
|
|
|
@@ -10,21 +11,35 @@ interface CFRoute {
|
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export class CloudflareDeployer extends MastraDeployer {
|
|
14
|
+
private cloudflare: Cloudflare | undefined;
|
|
13
15
|
routes?: CFRoute[] = [];
|
|
16
|
+
workerNamespace?: string;
|
|
14
17
|
constructor({
|
|
15
18
|
scope,
|
|
16
19
|
env,
|
|
17
20
|
projectName,
|
|
18
21
|
routes,
|
|
22
|
+
workerNamespace,
|
|
23
|
+
auth,
|
|
19
24
|
}: {
|
|
20
25
|
env?: Record<string, any>;
|
|
21
26
|
scope: string;
|
|
22
27
|
projectName: string;
|
|
23
28
|
routes?: CFRoute[];
|
|
29
|
+
workerNamespace?: string;
|
|
30
|
+
auth?: {
|
|
31
|
+
apiToken: string;
|
|
32
|
+
apiEmail: string;
|
|
33
|
+
};
|
|
24
34
|
}) {
|
|
25
35
|
super({ scope, env, projectName });
|
|
26
36
|
|
|
27
37
|
this.routes = routes;
|
|
38
|
+
this.workerNamespace = workerNamespace;
|
|
39
|
+
|
|
40
|
+
if (auth) {
|
|
41
|
+
this.cloudflare = new Cloudflare(auth);
|
|
42
|
+
}
|
|
28
43
|
}
|
|
29
44
|
|
|
30
45
|
writeFiles({ dir }: { dir: string }): void {
|
|
@@ -34,25 +49,27 @@ export class CloudflareDeployer extends MastraDeployer {
|
|
|
34
49
|
|
|
35
50
|
const cfWorkerName = this.projectName || 'mastra';
|
|
36
51
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
logs: {
|
|
49
|
-
enabled: true,
|
|
50
|
-
},
|
|
52
|
+
const wranglerConfig: Record<string, any> = {
|
|
53
|
+
name: cfWorkerName,
|
|
54
|
+
main: 'index.mjs',
|
|
55
|
+
compatibility_date: '2024-12-02',
|
|
56
|
+
compatibility_flags: ['nodejs_compat'],
|
|
57
|
+
build: {
|
|
58
|
+
command: 'npm install',
|
|
59
|
+
},
|
|
60
|
+
observability: {
|
|
61
|
+
logs: {
|
|
62
|
+
enabled: true,
|
|
51
63
|
},
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
64
|
+
},
|
|
65
|
+
vars: this.env,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
if (!this.workerNamespace && this.routes) {
|
|
69
|
+
wranglerConfig.routes = this.routes;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
writeFileSync(join(dir, 'wrangler.json'), JSON.stringify(wranglerConfig));
|
|
56
73
|
}
|
|
57
74
|
|
|
58
75
|
writeIndex({ dir }: { dir: string }): void {
|
|
@@ -60,9 +77,12 @@ export class CloudflareDeployer extends MastraDeployer {
|
|
|
60
77
|
join(dir, './index.mjs'),
|
|
61
78
|
`
|
|
62
79
|
export default {
|
|
63
|
-
fetch: async (
|
|
64
|
-
|
|
65
|
-
|
|
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);
|
|
66
86
|
}
|
|
67
87
|
}
|
|
68
88
|
`,
|
|
@@ -70,15 +90,45 @@ export class CloudflareDeployer extends MastraDeployer {
|
|
|
70
90
|
}
|
|
71
91
|
|
|
72
92
|
async deploy({ dir, token }: { dir: string; token: string }): Promise<void> {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
93
|
+
const cmd = this.workerNamespace
|
|
94
|
+
? `npm exec -- wrangler deploy --dispatch-namespace ${this.workerNamespace}`
|
|
95
|
+
: 'npm exec -- wrangler deploy';
|
|
96
|
+
|
|
97
|
+
const cpLogger = createChildProcessLogger({
|
|
98
|
+
logger: this.logger,
|
|
99
|
+
root: dir,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
await cpLogger({
|
|
103
|
+
cmd,
|
|
104
|
+
args: [],
|
|
76
105
|
env: {
|
|
77
106
|
CLOUDFLARE_API_TOKEN: token,
|
|
78
107
|
CLOUDFLARE_ACCOUNT_ID: this.scope,
|
|
79
108
|
...this.env,
|
|
80
|
-
PATH: process.env.PATH
|
|
109
|
+
PATH: process.env.PATH!,
|
|
81
110
|
},
|
|
82
111
|
});
|
|
83
112
|
}
|
|
113
|
+
|
|
114
|
+
async tagWorker({
|
|
115
|
+
workerName,
|
|
116
|
+
namespace,
|
|
117
|
+
tags,
|
|
118
|
+
scope,
|
|
119
|
+
}: {
|
|
120
|
+
scope: string;
|
|
121
|
+
workerName: string;
|
|
122
|
+
namespace: string;
|
|
123
|
+
tags: string[];
|
|
124
|
+
}): Promise<void> {
|
|
125
|
+
if (!this.cloudflare) {
|
|
126
|
+
throw new Error('Cloudflare Deployer not initialized');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
await this.cloudflare.workersForPlatforms.dispatch.namespaces.scripts.tags.update(namespace, workerName, {
|
|
130
|
+
account_id: scope,
|
|
131
|
+
body: tags,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
84
134
|
}
|