@mastra/deployer-cloudflare 0.1.0-alpha.56 → 0.1.0-alpha.57
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 +8 -0
- package/dist/_tsup-dts-rollup.d.ts +65 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +111 -0
- package/dist/secrets-manager/index.d.ts +1 -0
- package/dist/secrets-manager/index.js +87 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Deployer } from '@mastra/deployer';
|
|
2
|
+
|
|
3
|
+
declare interface CFRoute {
|
|
4
|
+
pattern: string;
|
|
5
|
+
zone_name: string;
|
|
6
|
+
custom_domain?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export declare class CloudflareDeployer extends Deployer {
|
|
10
|
+
private cloudflare;
|
|
11
|
+
routes?: CFRoute[];
|
|
12
|
+
workerNamespace?: string;
|
|
13
|
+
scope: string;
|
|
14
|
+
env?: Record<string, any>;
|
|
15
|
+
projectName?: string;
|
|
16
|
+
constructor({ scope, env, projectName, routes, workerNamespace, auth, }: {
|
|
17
|
+
env?: Record<string, any>;
|
|
18
|
+
scope: string;
|
|
19
|
+
projectName?: string;
|
|
20
|
+
routes?: CFRoute[];
|
|
21
|
+
workerNamespace?: string;
|
|
22
|
+
auth: {
|
|
23
|
+
apiToken: string;
|
|
24
|
+
apiEmail?: string;
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
writeFiles(outputDirectory: string): Promise<void>;
|
|
28
|
+
private getEntry;
|
|
29
|
+
prepare(outputDirectory: string): Promise<void>;
|
|
30
|
+
bundle(entryFile: string, outputDirectory: string): Promise<void>;
|
|
31
|
+
deploy(outputDirectory: string): Promise<void>;
|
|
32
|
+
tagWorker({ workerName, namespace, tags, scope, }: {
|
|
33
|
+
scope: string;
|
|
34
|
+
workerName: string;
|
|
35
|
+
namespace: string;
|
|
36
|
+
tags: string[];
|
|
37
|
+
}): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export declare class CloudflareSecretsManager {
|
|
41
|
+
accountId: string;
|
|
42
|
+
apiToken: string;
|
|
43
|
+
baseUrl: string;
|
|
44
|
+
constructor({ accountId, apiToken }: {
|
|
45
|
+
accountId: string;
|
|
46
|
+
apiToken: string;
|
|
47
|
+
});
|
|
48
|
+
createSecret({ workerId, secretName, secretValue, }: {
|
|
49
|
+
workerId: string;
|
|
50
|
+
secretName: string;
|
|
51
|
+
secretValue: string;
|
|
52
|
+
}): Promise<any>;
|
|
53
|
+
createProjectSecrets({ workerId, customerId, envVars, }: {
|
|
54
|
+
workerId: string;
|
|
55
|
+
customerId: string;
|
|
56
|
+
envVars: Record<string, string>;
|
|
57
|
+
}): Promise<any>;
|
|
58
|
+
deleteSecret({ workerId, secretName }: {
|
|
59
|
+
workerId: string;
|
|
60
|
+
secretName: string;
|
|
61
|
+
}): Promise<any>;
|
|
62
|
+
listSecrets(workerId: string): Promise<any>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { }
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CloudflareDeployer } from './_tsup-dts-rollup.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Deployer, createChildProcessLogger } from '@mastra/deployer';
|
|
2
|
+
import '@mastra/deployer/build';
|
|
3
|
+
import '@rollup/plugin-virtual';
|
|
4
|
+
import { Cloudflare } from 'cloudflare';
|
|
5
|
+
import { writeFileSync } from 'fs';
|
|
6
|
+
import { join } from 'path';
|
|
7
|
+
|
|
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 writeFiles(outputDirectory) {
|
|
35
|
+
const env = await this.loadEnvVars();
|
|
36
|
+
const envsAsObject = Object.assign({}, Object.fromEntries(env.entries()), this.env);
|
|
37
|
+
const cfWorkerName = this.projectName;
|
|
38
|
+
const wranglerConfig = {
|
|
39
|
+
name: cfWorkerName,
|
|
40
|
+
main: "index.mjs",
|
|
41
|
+
compatibility_date: "2024-12-02",
|
|
42
|
+
compatibility_flags: ["nodejs_compat"],
|
|
43
|
+
observability: {
|
|
44
|
+
logs: {
|
|
45
|
+
enabled: true
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
vars: envsAsObject
|
|
49
|
+
};
|
|
50
|
+
if (!this.workerNamespace && this.routes) {
|
|
51
|
+
wranglerConfig.routes = this.routes;
|
|
52
|
+
}
|
|
53
|
+
writeFileSync(join(outputDirectory, "wrangler.json"), JSON.stringify(wranglerConfig));
|
|
54
|
+
}
|
|
55
|
+
getEntry() {
|
|
56
|
+
return `
|
|
57
|
+
export default {
|
|
58
|
+
fetch: async (request, env, context) => {
|
|
59
|
+
Object.keys(env).forEach(key => {
|
|
60
|
+
process.env[key] = env[key]
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
const { mastra } = await import('#mastra')
|
|
64
|
+
const { createHonoServer } = await import('#server')
|
|
65
|
+
const app = await createHonoServer(mastra)
|
|
66
|
+
return app.fetch(request, env, context);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
`;
|
|
70
|
+
}
|
|
71
|
+
async prepare(outputDirectory) {
|
|
72
|
+
await super.prepare(outputDirectory);
|
|
73
|
+
await this.writeFiles(outputDirectory);
|
|
74
|
+
}
|
|
75
|
+
async bundle(entryFile, outputDirectory) {
|
|
76
|
+
return this._bundle(this.getEntry(), entryFile, outputDirectory);
|
|
77
|
+
}
|
|
78
|
+
async deploy(outputDirectory) {
|
|
79
|
+
const cmd = this.workerNamespace ? `npm exec -- wrangler deploy --dispatch-namespace ${this.workerNamespace}` : "npm exec -- wrangler deploy";
|
|
80
|
+
const cpLogger = createChildProcessLogger({
|
|
81
|
+
logger: this.logger,
|
|
82
|
+
root: outputDirectory
|
|
83
|
+
});
|
|
84
|
+
await cpLogger({
|
|
85
|
+
cmd,
|
|
86
|
+
args: [],
|
|
87
|
+
env: {
|
|
88
|
+
CLOUDFLARE_API_TOKEN: this.cloudflare.apiToken,
|
|
89
|
+
CLOUDFLARE_ACCOUNT_ID: this.scope,
|
|
90
|
+
...this.env,
|
|
91
|
+
PATH: process.env.PATH
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async tagWorker({
|
|
96
|
+
workerName,
|
|
97
|
+
namespace,
|
|
98
|
+
tags,
|
|
99
|
+
scope
|
|
100
|
+
}) {
|
|
101
|
+
if (!this.cloudflare) {
|
|
102
|
+
throw new Error("Cloudflare Deployer not initialized");
|
|
103
|
+
}
|
|
104
|
+
await this.cloudflare.workersForPlatforms.dispatch.namespaces.scripts.tags.update(namespace, workerName, {
|
|
105
|
+
account_id: scope,
|
|
106
|
+
body: tags
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export { CloudflareDeployer };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CloudflareSecretsManager } from '../_tsup-dts-rollup.js';
|
|
@@ -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/deployer-cloudflare",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.57",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"rollup-plugin-shim": "^1.0.0",
|
|
29
29
|
"wrangler": "^3.103.2",
|
|
30
30
|
"zod": "^3.24.1",
|
|
31
|
-
"@mastra/core": "^0.2.0-alpha.
|
|
32
|
-
"@mastra/deployer": "^0.1.0-alpha.
|
|
31
|
+
"@mastra/core": "^0.2.0-alpha.102",
|
|
32
|
+
"@mastra/deployer": "^0.1.0-alpha.52"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@microsoft/api-extractor": "^7.49.2",
|