@mastra/deployer 0.0.0-commonjs-20250227130920
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/.turbo/turbo-build.log +41 -0
- package/CHANGELOG.md +897 -0
- package/LICENSE +44 -0
- package/README.md +159 -0
- package/dist/_tsup-dts-rollup.d.cts +360 -0
- package/dist/_tsup-dts-rollup.d.ts +360 -0
- package/dist/build/analyze.cjs +367 -0
- package/dist/build/analyze.d.cts +1 -0
- package/dist/build/analyze.d.ts +1 -0
- package/dist/build/analyze.js +2 -0
- package/dist/build/bundler.cjs +353 -0
- package/dist/build/bundler.d.cts +2 -0
- package/dist/build/bundler.d.ts +2 -0
- package/dist/build/bundler.js +2 -0
- package/dist/build/index.cjs +1146 -0
- package/dist/build/index.d.cts +10 -0
- package/dist/build/index.d.ts +10 -0
- package/dist/build/index.js +5 -0
- package/dist/bundler/index.cjs +999 -0
- package/dist/bundler/index.d.cts +1 -0
- package/dist/bundler/index.d.ts +1 -0
- package/dist/bundler/index.js +5 -0
- package/dist/chunk-3ONBKVC4.js +113 -0
- package/dist/chunk-AXS5WSIK.js +290 -0
- package/dist/chunk-DTSFVNIF.js +260 -0
- package/dist/chunk-JMH7HCD6.js +274 -0
- package/dist/chunk-SGK37ZWD.js +254 -0
- package/dist/chunk-YNXJO2XU.js +69 -0
- package/dist/index.cjs +1229 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +131 -0
- package/dist/server/index.cjs +4930 -0
- package/dist/server/index.d.cts +2 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +4923 -0
- package/dist/templates/instrumentation-template.js +86 -0
- package/eslint.config.js +6 -0
- package/global.d.ts +0 -0
- package/package.json +112 -0
- package/public/templates/instrumentation-template.js +86 -0
- package/src/build/analyze.ts +279 -0
- package/src/build/babel/fix-libsql.ts +41 -0
- package/src/build/babel/get-deployer.ts +54 -0
- package/src/build/babel/get-telemetry-config.ts +62 -0
- package/src/build/babel/remove-deployer.ts +43 -0
- package/src/build/bundle.ts +139 -0
- package/src/build/bundler.ts +135 -0
- package/src/build/deployer.ts +67 -0
- package/src/build/deps.ts +149 -0
- package/src/build/env.ts +76 -0
- package/src/build/fs.ts +66 -0
- package/src/build/index.ts +7 -0
- package/src/build/isNodeBuiltin.ts +7 -0
- package/src/build/plugins/fix-libsql.ts +69 -0
- package/src/build/plugins/hono-alias.ts +17 -0
- package/src/build/plugins/pino.ts +93 -0
- package/src/build/plugins/remove-deployer.ts +37 -0
- package/src/build/plugins/telemetry-fix.ts +54 -0
- package/src/build/telemetry.ts +76 -0
- package/src/build/utils.ts +12 -0
- package/src/build/watcher.ts +43 -0
- package/src/bundler/index.ts +144 -0
- package/src/deploy/base.ts +30 -0
- package/src/deploy/index.ts +2 -0
- package/src/deploy/log.ts +61 -0
- package/src/index.ts +3 -0
- package/src/server/handlers/agents.ts +209 -0
- package/src/server/handlers/client.ts +36 -0
- package/src/server/handlers/error.ts +29 -0
- package/src/server/handlers/logs.ts +53 -0
- package/src/server/handlers/memory.ts +196 -0
- package/src/server/handlers/prompt.ts +128 -0
- package/src/server/handlers/root.ts +6 -0
- package/src/server/handlers/telemetry.ts +48 -0
- package/src/server/handlers/tools.ts +114 -0
- package/src/server/handlers/utils.ts +15 -0
- package/src/server/handlers/vector.ts +149 -0
- package/src/server/handlers/workflows.ts +119 -0
- package/src/server/index.ts +1355 -0
- package/src/server/openapi.json +434 -0
- package/src/server/openapi.script.js +22 -0
- package/src/server/types.ts +4 -0
- package/src/server/welcome.ts +105 -0
- package/tsconfig.json +5 -0
- package/vitest.config.ts +8 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1229 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var base = require('@mastra/core/base');
|
|
4
|
+
var fs3 = require('fs');
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var url = require('url');
|
|
7
|
+
var fsExtra2 = require('fs-extra/esm');
|
|
8
|
+
var fsPromises = require('fs/promises');
|
|
9
|
+
var child_process = require('child_process');
|
|
10
|
+
var stream = require('stream');
|
|
11
|
+
var bundler = require('@mastra/core/bundler');
|
|
12
|
+
var virtual = require('@rollup/plugin-virtual');
|
|
13
|
+
var fsExtra = require('fs-extra');
|
|
14
|
+
var commonjs = require('@rollup/plugin-commonjs');
|
|
15
|
+
var json = require('@rollup/plugin-json');
|
|
16
|
+
var nodeResolve2 = require('@rollup/plugin-node-resolve');
|
|
17
|
+
var rollup = require('rollup');
|
|
18
|
+
var esbuild = require('rollup-plugin-esbuild');
|
|
19
|
+
var module$1 = require('module');
|
|
20
|
+
var babel = require('@babel/core');
|
|
21
|
+
var alias = require('@rollup/plugin-alias');
|
|
22
|
+
var load = require('@neon-rs/load');
|
|
23
|
+
var detectLibc = require('detect-libc');
|
|
24
|
+
var helperModuleImports = require('@babel/helper-module-imports');
|
|
25
|
+
var process$1 = require('process');
|
|
26
|
+
|
|
27
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
28
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
29
|
+
|
|
30
|
+
function _interopNamespace(e) {
|
|
31
|
+
if (e && e.__esModule) return e;
|
|
32
|
+
var n = Object.create(null);
|
|
33
|
+
if (e) {
|
|
34
|
+
Object.keys(e).forEach(function (k) {
|
|
35
|
+
if (k !== 'default') {
|
|
36
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
37
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () { return e[k]; }
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
n.default = e;
|
|
45
|
+
return Object.freeze(n);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
var fs3__namespace = /*#__PURE__*/_interopNamespace(fs3);
|
|
49
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
50
|
+
var fsExtra2__default = /*#__PURE__*/_interopDefault(fsExtra2);
|
|
51
|
+
var fsPromises__default = /*#__PURE__*/_interopDefault(fsPromises);
|
|
52
|
+
var virtual__default = /*#__PURE__*/_interopDefault(virtual);
|
|
53
|
+
var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
|
|
54
|
+
var json__default = /*#__PURE__*/_interopDefault(json);
|
|
55
|
+
var nodeResolve2__default = /*#__PURE__*/_interopDefault(nodeResolve2);
|
|
56
|
+
var esbuild__default = /*#__PURE__*/_interopDefault(esbuild);
|
|
57
|
+
var babel__namespace = /*#__PURE__*/_interopNamespace(babel);
|
|
58
|
+
var alias__default = /*#__PURE__*/_interopDefault(alias);
|
|
59
|
+
|
|
60
|
+
// src/build/deps.ts
|
|
61
|
+
var createPinoStream = (logger) => {
|
|
62
|
+
return new stream.Transform({
|
|
63
|
+
transform(chunk, _encoding, callback) {
|
|
64
|
+
const line = chunk.toString().trim();
|
|
65
|
+
if (line) {
|
|
66
|
+
console.log(line);
|
|
67
|
+
logger.info(line);
|
|
68
|
+
}
|
|
69
|
+
callback(null, chunk);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
function createChildProcessLogger({ logger, root }) {
|
|
74
|
+
const pinoStream = createPinoStream(logger);
|
|
75
|
+
return async ({ cmd, args, env }) => {
|
|
76
|
+
try {
|
|
77
|
+
const subprocess = child_process.spawn(cmd, args, {
|
|
78
|
+
cwd: root,
|
|
79
|
+
shell: true,
|
|
80
|
+
env
|
|
81
|
+
});
|
|
82
|
+
subprocess.stdout?.pipe(pinoStream);
|
|
83
|
+
subprocess.stderr?.pipe(pinoStream);
|
|
84
|
+
return new Promise((resolve2, reject) => {
|
|
85
|
+
subprocess.on("close", (code) => {
|
|
86
|
+
pinoStream.end();
|
|
87
|
+
if (code === 0) {
|
|
88
|
+
resolve2({ success: true });
|
|
89
|
+
} else {
|
|
90
|
+
reject(new Error(`Process exited with code ${code}`));
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
subprocess.on("error", (error) => {
|
|
94
|
+
pinoStream.end();
|
|
95
|
+
logger.error("Process failed", { error });
|
|
96
|
+
reject(error);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.log(error);
|
|
101
|
+
logger.error("Process failed", { error });
|
|
102
|
+
pinoStream.end();
|
|
103
|
+
return { success: false, error };
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/build/deps.ts
|
|
109
|
+
var Deps = class extends base.MastraBase {
|
|
110
|
+
packageManager;
|
|
111
|
+
rootDir;
|
|
112
|
+
constructor(rootDir = process.cwd()) {
|
|
113
|
+
super({ component: "DEPLOYER", name: "DEPS" });
|
|
114
|
+
this.rootDir = rootDir;
|
|
115
|
+
this.packageManager = this.getPackageManager();
|
|
116
|
+
}
|
|
117
|
+
findLockFile(dir) {
|
|
118
|
+
const lockFiles = ["pnpm-lock.yaml", "package-lock.json", "yarn.lock", "bun.lock"];
|
|
119
|
+
for (const file of lockFiles) {
|
|
120
|
+
if (fs3__namespace.default.existsSync(path__default.default.join(dir, file))) {
|
|
121
|
+
return file;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const parentDir = path__default.default.resolve(dir, "..");
|
|
125
|
+
if (parentDir !== dir) {
|
|
126
|
+
return this.findLockFile(parentDir);
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
getPackageManager() {
|
|
131
|
+
const lockFile = this.findLockFile(this.rootDir);
|
|
132
|
+
switch (lockFile) {
|
|
133
|
+
case "pnpm-lock.yaml":
|
|
134
|
+
return "pnpm";
|
|
135
|
+
case "package-lock.json":
|
|
136
|
+
return "npm";
|
|
137
|
+
case "yarn.lock":
|
|
138
|
+
return "yarn";
|
|
139
|
+
case "bun.lock":
|
|
140
|
+
return "bun";
|
|
141
|
+
default:
|
|
142
|
+
return "npm";
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async install({ dir = this.rootDir, packages = [] }) {
|
|
146
|
+
let runCommand = this.packageManager;
|
|
147
|
+
if (this.packageManager === "npm") {
|
|
148
|
+
runCommand = `${this.packageManager} i`;
|
|
149
|
+
} else {
|
|
150
|
+
runCommand = `${this.packageManager} ${packages?.length > 0 ? `add` : `install`}`;
|
|
151
|
+
}
|
|
152
|
+
const cpLogger = createChildProcessLogger({
|
|
153
|
+
logger: this.logger,
|
|
154
|
+
root: dir
|
|
155
|
+
});
|
|
156
|
+
return cpLogger({
|
|
157
|
+
cmd: runCommand,
|
|
158
|
+
args: packages,
|
|
159
|
+
env: {
|
|
160
|
+
PATH: process.env.PATH
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
async installPackages(packages) {
|
|
165
|
+
let runCommand = this.packageManager;
|
|
166
|
+
if (this.packageManager === "npm") {
|
|
167
|
+
runCommand = `${this.packageManager} i`;
|
|
168
|
+
} else {
|
|
169
|
+
runCommand = `${this.packageManager} add`;
|
|
170
|
+
}
|
|
171
|
+
const cpLogger = createChildProcessLogger({
|
|
172
|
+
logger: this.logger,
|
|
173
|
+
root: ""
|
|
174
|
+
});
|
|
175
|
+
return cpLogger({
|
|
176
|
+
cmd: `${runCommand}`,
|
|
177
|
+
args: packages,
|
|
178
|
+
env: {
|
|
179
|
+
PATH: process.env.PATH
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
async checkDependencies(dependencies) {
|
|
184
|
+
try {
|
|
185
|
+
const packageJsonPath = path__default.default.join(this.rootDir, "package.json");
|
|
186
|
+
try {
|
|
187
|
+
await fsPromises__default.default.access(packageJsonPath);
|
|
188
|
+
} catch {
|
|
189
|
+
return "No package.json file found in the current directory";
|
|
190
|
+
}
|
|
191
|
+
const packageJson = JSON.parse(await fsPromises__default.default.readFile(packageJsonPath, "utf-8"));
|
|
192
|
+
for (const dependency of dependencies) {
|
|
193
|
+
if (!packageJson.dependencies || !packageJson.dependencies[dependency]) {
|
|
194
|
+
return `Please install ${dependency} before running this command (${this.packageManager} install ${dependency})`;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return "ok";
|
|
198
|
+
} catch (err) {
|
|
199
|
+
console.error(err);
|
|
200
|
+
return "Could not check dependencies";
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
async getProjectName() {
|
|
204
|
+
try {
|
|
205
|
+
const packageJsonPath = path__default.default.join(this.rootDir, "package.json");
|
|
206
|
+
const packageJson = await fsPromises__default.default.readFile(packageJsonPath, "utf-8");
|
|
207
|
+
const pkg = JSON.parse(packageJson);
|
|
208
|
+
return pkg.name;
|
|
209
|
+
} catch (err) {
|
|
210
|
+
throw err;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async getPackageVersion() {
|
|
214
|
+
const __filename = url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
215
|
+
const __dirname = path.dirname(__filename);
|
|
216
|
+
const pkgJsonPath = path__default.default.join(__dirname, "..", "..", "package.json");
|
|
217
|
+
const content = await fsExtra2__default.default.readJSON(pkgJsonPath);
|
|
218
|
+
return content.version;
|
|
219
|
+
}
|
|
220
|
+
async addScriptsToPackageJson(scripts) {
|
|
221
|
+
const packageJson = JSON.parse(await fsPromises__default.default.readFile("package.json", "utf-8"));
|
|
222
|
+
packageJson.scripts = {
|
|
223
|
+
...packageJson.scripts,
|
|
224
|
+
...scripts
|
|
225
|
+
};
|
|
226
|
+
await fsPromises__default.default.writeFile("package.json", JSON.stringify(packageJson, null, 2));
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
var EnvService = class {
|
|
230
|
+
};
|
|
231
|
+
var FileEnvService = class extends EnvService {
|
|
232
|
+
filePath;
|
|
233
|
+
constructor(filePath) {
|
|
234
|
+
super();
|
|
235
|
+
this.filePath = filePath;
|
|
236
|
+
}
|
|
237
|
+
readFile(filePath) {
|
|
238
|
+
return new Promise((resolve2, reject) => {
|
|
239
|
+
fs3__namespace.readFile(filePath, "utf8", (err, data) => {
|
|
240
|
+
if (err) reject(err);
|
|
241
|
+
else resolve2(data);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
writeFile({ filePath, data }) {
|
|
246
|
+
return new Promise((resolve2, reject) => {
|
|
247
|
+
fs3__namespace.writeFile(filePath, data, "utf8", (err) => {
|
|
248
|
+
if (err) reject(err);
|
|
249
|
+
else resolve2();
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
async updateEnvData({
|
|
254
|
+
key,
|
|
255
|
+
value,
|
|
256
|
+
filePath = this.filePath,
|
|
257
|
+
data
|
|
258
|
+
}) {
|
|
259
|
+
const regex = new RegExp(`^${key}=.*$`, "m");
|
|
260
|
+
if (data.match(regex)) {
|
|
261
|
+
data = data.replace(regex, `${key}=${value}`);
|
|
262
|
+
} else {
|
|
263
|
+
data += `
|
|
264
|
+
${key}=${value}`;
|
|
265
|
+
}
|
|
266
|
+
await this.writeFile({ filePath, data });
|
|
267
|
+
console.log(`${key} set to ${value} in ENV file.`);
|
|
268
|
+
return data;
|
|
269
|
+
}
|
|
270
|
+
async getEnvValue(key) {
|
|
271
|
+
try {
|
|
272
|
+
const data = await this.readFile(this.filePath);
|
|
273
|
+
const regex = new RegExp(`^${key}=(.*)$`, "m");
|
|
274
|
+
const match = data.match(regex);
|
|
275
|
+
return match?.[1] || null;
|
|
276
|
+
} catch (err) {
|
|
277
|
+
console.error(`Error reading ENV value: ${err}`);
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
async setEnvValue(key, value) {
|
|
282
|
+
try {
|
|
283
|
+
const data = await this.readFile(this.filePath);
|
|
284
|
+
await this.updateEnvData({ key, value, data });
|
|
285
|
+
} catch (err) {
|
|
286
|
+
console.error(`Error writing ENV value: ${err}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// src/build/fs.ts
|
|
292
|
+
var FileService = class {
|
|
293
|
+
/**
|
|
294
|
+
*
|
|
295
|
+
* @param inputFile the file in the starter files directory to copy
|
|
296
|
+
* @param outputFilePath the destination path
|
|
297
|
+
* @param replaceIfExists flag to replace if it exists
|
|
298
|
+
* @returns
|
|
299
|
+
*/
|
|
300
|
+
async copyStarterFile(inputFile, outputFilePath, replaceIfExists) {
|
|
301
|
+
const __filename = url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
302
|
+
const __dirname = path__default.default.dirname(__filename);
|
|
303
|
+
const filePath = path__default.default.resolve(__dirname, "..", "starter-files", inputFile);
|
|
304
|
+
const fileString = fs3__namespace.default.readFileSync(filePath, "utf8");
|
|
305
|
+
if (fs3__namespace.default.existsSync(outputFilePath) && !replaceIfExists) {
|
|
306
|
+
console.log(`${outputFilePath} already exists`);
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
await fsExtra2__default.default.outputFile(outputFilePath, fileString);
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
async setupEnvFile({ dbUrl }) {
|
|
313
|
+
const envPath = path__default.default.join(process.cwd(), ".env.development");
|
|
314
|
+
await fsExtra2__default.default.ensureFile(envPath);
|
|
315
|
+
const fileEnvService = new FileEnvService(envPath);
|
|
316
|
+
await fileEnvService.setEnvValue("DB_URL", dbUrl);
|
|
317
|
+
}
|
|
318
|
+
getFirstExistingFile(files) {
|
|
319
|
+
for (const f of files) {
|
|
320
|
+
if (fs3__namespace.default.existsSync(f)) {
|
|
321
|
+
return f;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
throw new Error("Missing required file, checked the following paths: " + files.join(", "));
|
|
325
|
+
}
|
|
326
|
+
replaceValuesInFile({
|
|
327
|
+
filePath,
|
|
328
|
+
replacements
|
|
329
|
+
}) {
|
|
330
|
+
let fileContent = fs3__namespace.default.readFileSync(filePath, "utf8");
|
|
331
|
+
replacements.forEach(({ search, replace }) => {
|
|
332
|
+
fileContent = fileContent.replaceAll(search, replace);
|
|
333
|
+
});
|
|
334
|
+
fs3__namespace.default.writeFileSync(filePath, fileContent);
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
function isNodeBuiltin(dep) {
|
|
338
|
+
const [pkg] = dep.split("/");
|
|
339
|
+
return dep.startsWith("node:") || module$1.builtinModules.includes(dep) || module$1.builtinModules.includes(pkg);
|
|
340
|
+
}
|
|
341
|
+
function aliasHono() {
|
|
342
|
+
return {
|
|
343
|
+
name: "hono-alias",
|
|
344
|
+
resolveId(id) {
|
|
345
|
+
if (!id.startsWith("@hono/") && !id.startsWith("hono/") && id !== "hono" && id !== "hono-openapi") {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const path3 = undefined(id);
|
|
349
|
+
return url.fileURLToPath(path3);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/build/plugins/pino.ts
|
|
355
|
+
function pino() {
|
|
356
|
+
const workerFiles = [
|
|
357
|
+
{
|
|
358
|
+
id: "thread-stream-worker",
|
|
359
|
+
file: "pino-thread-stream-worker"
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
id: "pino-worker",
|
|
363
|
+
file: "pino-worker"
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
id: "pino/file",
|
|
367
|
+
file: "pino-file"
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
id: "pino-pretty",
|
|
371
|
+
file: "pino-pretty"
|
|
372
|
+
}
|
|
373
|
+
];
|
|
374
|
+
const fileReferences = /* @__PURE__ */ new Map();
|
|
375
|
+
return {
|
|
376
|
+
name: "rollup-plugin-pino",
|
|
377
|
+
async resolveId(id, importee) {
|
|
378
|
+
if (id === "pino") {
|
|
379
|
+
const resolvedPino = await this.resolve(id, importee);
|
|
380
|
+
if (resolvedPino) {
|
|
381
|
+
await Promise.all(
|
|
382
|
+
workerFiles.map(async (file) => {
|
|
383
|
+
const resolvedEntry = await this.resolve(file.id, resolvedPino.id);
|
|
384
|
+
if (!resolvedEntry) {
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
const reference = this.emitFile({
|
|
388
|
+
type: "chunk",
|
|
389
|
+
id: resolvedEntry.id,
|
|
390
|
+
name: `${file.file}`
|
|
391
|
+
});
|
|
392
|
+
fileReferences.set(file.id, reference);
|
|
393
|
+
})
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
renderChunk(code, chunk) {
|
|
399
|
+
if (chunk.type === "chunk" && chunk.isEntry && fileReferences.size && chunk.name === "index") {
|
|
400
|
+
const importRegex = /^(?:import(?:["'\s]*[\w*${}\n\r\t, ]+from\s*)?["'\s].+[;"'\s]*)$/gm;
|
|
401
|
+
const codeToInject = `globalThis.__bundlerPathsOverrides = {
|
|
402
|
+
${Array.from(fileReferences.entries()).map(([key, file]) => {
|
|
403
|
+
return '"' + key + '": import.meta.ROLLUP_FILE_URL_' + file;
|
|
404
|
+
}).join(",\n")}
|
|
405
|
+
};`;
|
|
406
|
+
const matches = Array.from(code.matchAll(importRegex));
|
|
407
|
+
if (matches.length > 0) {
|
|
408
|
+
const lastImport = matches[matches.length - 1];
|
|
409
|
+
const lastImportEnd = lastImport.index + lastImport[0].length;
|
|
410
|
+
const newCode = code.slice(0, lastImportEnd) + "\n\n" + codeToInject + "\n\n" + code.slice(lastImportEnd);
|
|
411
|
+
return {
|
|
412
|
+
code: newCode,
|
|
413
|
+
map: null
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
return {
|
|
417
|
+
code: `${codeToInject}
|
|
418
|
+
|
|
419
|
+
${code}`,
|
|
420
|
+
map: null
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
function removeDeployer() {
|
|
427
|
+
const t = babel__namespace.default.types;
|
|
428
|
+
let mastraClass = null;
|
|
429
|
+
return {
|
|
430
|
+
name: "remove-deployer",
|
|
431
|
+
visitor: {
|
|
432
|
+
ImportDeclaration(path3) {
|
|
433
|
+
if ((path3.node.source.value === "@mastra/core" || path3.node.source.value === "@mastra/core/mastra") && path3.node.specifiers) {
|
|
434
|
+
const mastraObj = path3.node.specifiers.find(
|
|
435
|
+
(p) => t.isImportSpecifier(p) && t.isIdentifier(p.imported) && p.imported.name === "Mastra"
|
|
436
|
+
);
|
|
437
|
+
if (mastraObj?.local?.name) {
|
|
438
|
+
mastraClass = mastraObj.local.name;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
NewExpression(path3, state) {
|
|
443
|
+
if (mastraClass && t.isIdentifier(path3.node.callee) && path3.node.callee.name === mastraClass && !state.hasReplaced) {
|
|
444
|
+
state.hasReplaced = true;
|
|
445
|
+
const newMastraObj = t.cloneNode(path3.node);
|
|
446
|
+
if (t.isObjectExpression(newMastraObj.arguments[0]) && newMastraObj.arguments[0].properties?.[0]) {
|
|
447
|
+
newMastraObj.arguments[0].properties = newMastraObj.arguments[0].properties.filter(
|
|
448
|
+
(prop) => t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name !== "deployer"
|
|
449
|
+
);
|
|
450
|
+
path3.replaceWith(newMastraObj);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// src/build/plugins/remove-deployer.ts
|
|
459
|
+
function removeDeployer2(mastraEntry) {
|
|
460
|
+
return {
|
|
461
|
+
name: "remove-deployer",
|
|
462
|
+
transform(code, id) {
|
|
463
|
+
if (id !== mastraEntry) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
return new Promise((resolve2, reject) => {
|
|
467
|
+
babel__namespace.transform(
|
|
468
|
+
code,
|
|
469
|
+
{
|
|
470
|
+
babelrc: false,
|
|
471
|
+
configFile: false,
|
|
472
|
+
filename: id,
|
|
473
|
+
plugins: [removeDeployer]
|
|
474
|
+
},
|
|
475
|
+
(err, result) => {
|
|
476
|
+
if (err) {
|
|
477
|
+
return reject(err);
|
|
478
|
+
}
|
|
479
|
+
resolve2({
|
|
480
|
+
code: result.code,
|
|
481
|
+
map: result.map
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
);
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// src/build/analyze.ts
|
|
491
|
+
async function analyze(entry, mastraEntry, isVirtualFile, platform2, logger) {
|
|
492
|
+
logger.info("Analyzing dependencies...");
|
|
493
|
+
let virtualPlugin = null;
|
|
494
|
+
if (isVirtualFile) {
|
|
495
|
+
virtualPlugin = virtual__default.default({
|
|
496
|
+
"#entry": entry
|
|
497
|
+
});
|
|
498
|
+
entry = "#entry";
|
|
499
|
+
}
|
|
500
|
+
const optimizerBundler = await rollup.rollup({
|
|
501
|
+
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
502
|
+
input: isVirtualFile ? "#entry" : entry,
|
|
503
|
+
treeshake: true,
|
|
504
|
+
preserveSymlinks: true,
|
|
505
|
+
plugins: [
|
|
506
|
+
virtualPlugin,
|
|
507
|
+
{
|
|
508
|
+
name: "custom-alias-resolver",
|
|
509
|
+
resolveId(id) {
|
|
510
|
+
if (id === "#server") {
|
|
511
|
+
return url.fileURLToPath(undefined("@mastra/deployer/server")).replaceAll("\\", "/");
|
|
512
|
+
}
|
|
513
|
+
if (id === "#mastra") {
|
|
514
|
+
return mastraEntry.replaceAll("\\", "/");
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
json__default.default(),
|
|
519
|
+
commonjs__default.default({
|
|
520
|
+
strictRequires: "debug",
|
|
521
|
+
ignoreTryCatch: false,
|
|
522
|
+
transformMixedEsModules: true,
|
|
523
|
+
extensions: [".js", ".ts"]
|
|
524
|
+
}),
|
|
525
|
+
esbuild__default.default({
|
|
526
|
+
target: "node20",
|
|
527
|
+
platform: platform2,
|
|
528
|
+
minify: false
|
|
529
|
+
}),
|
|
530
|
+
removeDeployer2(mastraEntry),
|
|
531
|
+
esbuild__default.default({
|
|
532
|
+
target: "node20",
|
|
533
|
+
platform: platform2,
|
|
534
|
+
minify: false
|
|
535
|
+
})
|
|
536
|
+
].filter(Boolean)
|
|
537
|
+
});
|
|
538
|
+
const { output } = await optimizerBundler.generate({
|
|
539
|
+
format: "esm",
|
|
540
|
+
inlineDynamicImports: true
|
|
541
|
+
});
|
|
542
|
+
await optimizerBundler.close();
|
|
543
|
+
const depsToOptimize = new Map(Object.entries(output[0].importedBindings));
|
|
544
|
+
for (const dep of depsToOptimize.keys()) {
|
|
545
|
+
if (isNodeBuiltin(dep)) {
|
|
546
|
+
depsToOptimize.delete(dep);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return depsToOptimize;
|
|
550
|
+
}
|
|
551
|
+
async function bundleExternals(depsToOptimize, outputDir, logger) {
|
|
552
|
+
logger.info("Optimizing dependencies...");
|
|
553
|
+
logger.debug(
|
|
554
|
+
`${Array.from(depsToOptimize.keys()).map((key) => `- ${key}`).join("\n")}`
|
|
555
|
+
);
|
|
556
|
+
const reverseVirtualReferenceMap = /* @__PURE__ */ new Map();
|
|
557
|
+
const virtualDependencies = /* @__PURE__ */ new Map();
|
|
558
|
+
for (const [dep, exports] of depsToOptimize.entries()) {
|
|
559
|
+
const name = dep.replaceAll("/", "-");
|
|
560
|
+
reverseVirtualReferenceMap.set(name, dep);
|
|
561
|
+
const virtualFile = [];
|
|
562
|
+
let exportStringBuilder = [];
|
|
563
|
+
for (const local of exports) {
|
|
564
|
+
if (local === "*") {
|
|
565
|
+
virtualFile.push(`export * from '${dep}';`);
|
|
566
|
+
} else if (local === "default") {
|
|
567
|
+
virtualFile.push(`export * from '${dep}';`);
|
|
568
|
+
} else {
|
|
569
|
+
exportStringBuilder.push(local);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (exportStringBuilder.length > 0) {
|
|
573
|
+
virtualFile.push(`export { ${exportStringBuilder.join(", ")} } from '${dep}';`);
|
|
574
|
+
}
|
|
575
|
+
virtualDependencies.set(dep, {
|
|
576
|
+
name,
|
|
577
|
+
virtual: virtualFile.join("\n")
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
const bundler = await rollup.rollup({
|
|
581
|
+
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
582
|
+
input: Array.from(virtualDependencies.entries()).reduce(
|
|
583
|
+
(acc, [dep, virtualDep]) => {
|
|
584
|
+
acc[virtualDep.name] = `#virtual-${dep}`;
|
|
585
|
+
return acc;
|
|
586
|
+
},
|
|
587
|
+
{}
|
|
588
|
+
),
|
|
589
|
+
// this dependency breaks the build, so we need to exclude it
|
|
590
|
+
// TODO actually fix this so we don't need to exclude it
|
|
591
|
+
external: ["jsdom"],
|
|
592
|
+
treeshake: "smallest",
|
|
593
|
+
preserveSymlinks: true,
|
|
594
|
+
plugins: [
|
|
595
|
+
virtual__default.default(
|
|
596
|
+
Array.from(virtualDependencies.entries()).reduce(
|
|
597
|
+
(acc, [dep, virtualDep]) => {
|
|
598
|
+
acc[`#virtual-${dep}`] = virtualDep.virtual;
|
|
599
|
+
return acc;
|
|
600
|
+
},
|
|
601
|
+
{}
|
|
602
|
+
)
|
|
603
|
+
),
|
|
604
|
+
pino(),
|
|
605
|
+
commonjs__default.default({
|
|
606
|
+
strictRequires: "strict",
|
|
607
|
+
transformMixedEsModules: true,
|
|
608
|
+
ignoreTryCatch: false
|
|
609
|
+
}),
|
|
610
|
+
nodeResolve2__default.default({
|
|
611
|
+
preferBuiltins: true,
|
|
612
|
+
exportConditions: ["node", "import", "require"],
|
|
613
|
+
mainFields: ["module", "main"]
|
|
614
|
+
}),
|
|
615
|
+
// hono is imported from deployer, so we need to resolve from here instead of the project root
|
|
616
|
+
aliasHono(),
|
|
617
|
+
json__default.default()
|
|
618
|
+
].filter(Boolean)
|
|
619
|
+
});
|
|
620
|
+
const { output } = await bundler.write({
|
|
621
|
+
format: "esm",
|
|
622
|
+
dir: outputDir,
|
|
623
|
+
entryFileNames: "[name].mjs",
|
|
624
|
+
chunkFileNames: "[name].mjs"
|
|
625
|
+
});
|
|
626
|
+
await bundler.close();
|
|
627
|
+
return { output, reverseVirtualReferenceMap };
|
|
628
|
+
}
|
|
629
|
+
async function validateOutput(output, reverseVirtualReferenceMap, outputDir, logger) {
|
|
630
|
+
const result = {
|
|
631
|
+
invalidChunks: /* @__PURE__ */ new Set(),
|
|
632
|
+
dependencies: /* @__PURE__ */ new Map(),
|
|
633
|
+
externalDependencies: /* @__PURE__ */ new Set()
|
|
634
|
+
};
|
|
635
|
+
for (const file of output) {
|
|
636
|
+
if (file.type === "asset") {
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
try {
|
|
640
|
+
logger.debug(`Validating if ${file.fileName} is a valid module.`);
|
|
641
|
+
if (file.isEntry && reverseVirtualReferenceMap.has(file.name)) {
|
|
642
|
+
result.dependencies.set(reverseVirtualReferenceMap.get(file.name), file.fileName);
|
|
643
|
+
}
|
|
644
|
+
await import(`file:${outputDir}/${file.fileName}`);
|
|
645
|
+
} catch (err) {
|
|
646
|
+
result.invalidChunks.add(file.fileName);
|
|
647
|
+
if (file.isEntry && reverseVirtualReferenceMap.has(file.name)) {
|
|
648
|
+
result.externalDependencies.add(reverseVirtualReferenceMap.get(file.name));
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return result;
|
|
653
|
+
}
|
|
654
|
+
async function analyzeBundle(entry, mastraEntry, outputDir, platform2, logger) {
|
|
655
|
+
const isVirtualFile = entry.includes("\n") || !fs3.existsSync(entry);
|
|
656
|
+
const depsToOptimize = await analyze(entry, mastraEntry, isVirtualFile, platform2, logger);
|
|
657
|
+
const { output, reverseVirtualReferenceMap } = await bundleExternals(depsToOptimize, outputDir, logger);
|
|
658
|
+
const result = await validateOutput(output, reverseVirtualReferenceMap, outputDir, logger);
|
|
659
|
+
return result;
|
|
660
|
+
}
|
|
661
|
+
function rewriteLibsqlImport() {
|
|
662
|
+
const t = babel__namespace.default.types;
|
|
663
|
+
let hasReplaced = false;
|
|
664
|
+
return {
|
|
665
|
+
name: "rewrite-libsql-import",
|
|
666
|
+
visitor: {
|
|
667
|
+
FunctionDeclaration(path3) {
|
|
668
|
+
if (path3.node.id?.name === "requireNative" && !hasReplaced) {
|
|
669
|
+
hasReplaced = true;
|
|
670
|
+
const createRequire = helperModuleImports.addNamed(path3, "createRequire", "module");
|
|
671
|
+
const requireIdentifier = t.identifier("require");
|
|
672
|
+
path3.replaceWith(
|
|
673
|
+
t.functionDeclaration(
|
|
674
|
+
t.identifier("requireNative"),
|
|
675
|
+
[],
|
|
676
|
+
t.blockStatement([
|
|
677
|
+
t.variableDeclaration("const", [
|
|
678
|
+
t.variableDeclarator(
|
|
679
|
+
requireIdentifier,
|
|
680
|
+
t.callExpression(createRequire, [
|
|
681
|
+
t.memberExpression(
|
|
682
|
+
t.metaProperty(t.identifier("import"), t.identifier("meta")),
|
|
683
|
+
t.identifier("url")
|
|
684
|
+
)
|
|
685
|
+
])
|
|
686
|
+
)
|
|
687
|
+
]),
|
|
688
|
+
t.returnStatement(t.callExpression(requireIdentifier, [t.stringLiteral("./libsql.node")]))
|
|
689
|
+
])
|
|
690
|
+
)
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// src/build/plugins/fix-libsql.ts
|
|
699
|
+
function libSqlFix() {
|
|
700
|
+
return {
|
|
701
|
+
name: "libSqlFix",
|
|
702
|
+
transform(code, id) {
|
|
703
|
+
if (!id.includes("\\libsql\\index.js") && !id.includes("/libsql/index.js")) {
|
|
704
|
+
return null;
|
|
705
|
+
}
|
|
706
|
+
return new Promise((resolve2, reject) => {
|
|
707
|
+
babel__namespace.transform(
|
|
708
|
+
code,
|
|
709
|
+
{
|
|
710
|
+
babelrc: false,
|
|
711
|
+
configFile: false,
|
|
712
|
+
filename: id,
|
|
713
|
+
plugins: [rewriteLibsqlImport]
|
|
714
|
+
},
|
|
715
|
+
(err, result) => {
|
|
716
|
+
if (err) {
|
|
717
|
+
return reject(err);
|
|
718
|
+
}
|
|
719
|
+
resolve2({
|
|
720
|
+
code: result.code,
|
|
721
|
+
map: result.map
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
);
|
|
725
|
+
});
|
|
726
|
+
},
|
|
727
|
+
async generateBundle({ file, dir }) {
|
|
728
|
+
if (!file && !dir) {
|
|
729
|
+
throw new Error("No output options were given.");
|
|
730
|
+
}
|
|
731
|
+
const outputDirectory = dir || path.dirname(file);
|
|
732
|
+
let target = load.currentTarget();
|
|
733
|
+
if (detectLibc.familySync() == detectLibc.GLIBC) {
|
|
734
|
+
switch (target) {
|
|
735
|
+
case "linux-x64-musl":
|
|
736
|
+
target = "linux-x64-gnu";
|
|
737
|
+
break;
|
|
738
|
+
case "linux-arm64-musl":
|
|
739
|
+
target = "linux-arm64-gnu";
|
|
740
|
+
break;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
const fileToCopy = await this.resolve(`@libsql/${target}/index.node`);
|
|
744
|
+
if (fileToCopy) {
|
|
745
|
+
await fsExtra2__default.default.copy(fileToCopy.id, path.join(outputDirectory, "libsql.node"));
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
function getTelemetryMachineFile() {
|
|
751
|
+
switch (process$1.platform) {
|
|
752
|
+
case "darwin":
|
|
753
|
+
return "getMachineId-darwin";
|
|
754
|
+
case "linux":
|
|
755
|
+
return "getMachineId-linux";
|
|
756
|
+
case "freebsd":
|
|
757
|
+
return "getMachineId-bsd";
|
|
758
|
+
case "win32":
|
|
759
|
+
return "getMachineId-win";
|
|
760
|
+
default:
|
|
761
|
+
return "getMachineId-unsupported";
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
function telemetryFix() {
|
|
765
|
+
return {
|
|
766
|
+
name: "telemetry-fix",
|
|
767
|
+
transform(code, id) {
|
|
768
|
+
if (id.includes("require-in-the-middle")) {
|
|
769
|
+
return code.replace(
|
|
770
|
+
`const path = require('path')`,
|
|
771
|
+
`const path = require('path');
|
|
772
|
+
const { createRequire } = require('module');
|
|
773
|
+
const realRequire = createRequire(import.meta.url)`
|
|
774
|
+
).replaceAll(`require.resolve`, `realRequire.resolve`);
|
|
775
|
+
}
|
|
776
|
+
},
|
|
777
|
+
resolveId(id, importer) {
|
|
778
|
+
if (id === "./machine-id/getMachineId" && importer) {
|
|
779
|
+
return { id: path.resolve(path.dirname(importer), `./machine-id/${getTelemetryMachineFile()}.js`) };
|
|
780
|
+
}
|
|
781
|
+
if (id === "formdata-node") {
|
|
782
|
+
return { id: "formdata-node", external: false };
|
|
783
|
+
}
|
|
784
|
+
},
|
|
785
|
+
load(id) {
|
|
786
|
+
if (id.startsWith("formdata-node")) {
|
|
787
|
+
return "export default {};";
|
|
788
|
+
}
|
|
789
|
+
return null;
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
// src/build/bundler.ts
|
|
795
|
+
async function getInputOptions(entryFile, analyzedBundleInfo, platform2) {
|
|
796
|
+
let nodeResolvePlugin = nodeResolve2__default.default({
|
|
797
|
+
preferBuiltins: true,
|
|
798
|
+
exportConditions: ["node", "import", "require"],
|
|
799
|
+
mainFields: ["module", "main"]
|
|
800
|
+
}) ;
|
|
801
|
+
const externals = Array.from(analyzedBundleInfo.externalDependencies).concat(["@mastra/core/hooks"]);
|
|
802
|
+
return {
|
|
803
|
+
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
804
|
+
treeshake: true,
|
|
805
|
+
preserveSymlinks: true,
|
|
806
|
+
external: externals,
|
|
807
|
+
plugins: [
|
|
808
|
+
telemetryFix(),
|
|
809
|
+
libSqlFix(),
|
|
810
|
+
{
|
|
811
|
+
name: "alias-optimized-deps",
|
|
812
|
+
// @ts-ignore
|
|
813
|
+
resolveId(id) {
|
|
814
|
+
if (!analyzedBundleInfo.dependencies.has(id)) {
|
|
815
|
+
return null;
|
|
816
|
+
}
|
|
817
|
+
const isInvalidChunk = analyzedBundleInfo.invalidChunks.has(analyzedBundleInfo.dependencies.get(id));
|
|
818
|
+
if (isInvalidChunk) {
|
|
819
|
+
return {
|
|
820
|
+
id,
|
|
821
|
+
external: true
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
return {
|
|
825
|
+
id: ".mastra/.build/" + analyzedBundleInfo.dependencies.get(id),
|
|
826
|
+
external: false
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
},
|
|
830
|
+
alias__default.default({
|
|
831
|
+
entries: [
|
|
832
|
+
{
|
|
833
|
+
find: /^\#server$/,
|
|
834
|
+
replacement: url.fileURLToPath(undefined("@mastra/deployer/server")).replaceAll("\\", "/")
|
|
835
|
+
},
|
|
836
|
+
{ find: /^\#mastra$/, replacement: entryFile.replaceAll("\\", "/") }
|
|
837
|
+
]
|
|
838
|
+
}),
|
|
839
|
+
esbuild__default.default({
|
|
840
|
+
target: "node20",
|
|
841
|
+
platform: platform2,
|
|
842
|
+
minify: false,
|
|
843
|
+
define: {
|
|
844
|
+
"process.env.NODE_ENV": JSON.stringify("production")
|
|
845
|
+
}
|
|
846
|
+
}),
|
|
847
|
+
commonjs__default.default({
|
|
848
|
+
extensions: [".js", ".ts"],
|
|
849
|
+
transformMixedEsModules: true,
|
|
850
|
+
esmExternals(id) {
|
|
851
|
+
return externals.includes(id);
|
|
852
|
+
}
|
|
853
|
+
}),
|
|
854
|
+
nodeResolvePlugin,
|
|
855
|
+
// for debugging
|
|
856
|
+
// {
|
|
857
|
+
// name: 'logger',
|
|
858
|
+
// //@ts-ignore
|
|
859
|
+
// resolveId(id, ...args) {
|
|
860
|
+
// console.log({ id, args });
|
|
861
|
+
// },
|
|
862
|
+
// // @ts-ignore
|
|
863
|
+
// transform(code, id) {
|
|
864
|
+
// if (code.includes('class Duplexify ')) {
|
|
865
|
+
// console.log({ duplex: id });
|
|
866
|
+
// }
|
|
867
|
+
// },
|
|
868
|
+
// },
|
|
869
|
+
json__default.default(),
|
|
870
|
+
removeDeployer2(entryFile),
|
|
871
|
+
// treeshake unused imports
|
|
872
|
+
esbuild__default.default({
|
|
873
|
+
include: entryFile,
|
|
874
|
+
target: "node20",
|
|
875
|
+
platform: platform2,
|
|
876
|
+
minify: false
|
|
877
|
+
})
|
|
878
|
+
].filter(Boolean)
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
async function createBundler(inputOptions, outputOptions) {
|
|
882
|
+
const bundler = await rollup.rollup(inputOptions);
|
|
883
|
+
return {
|
|
884
|
+
write: () => {
|
|
885
|
+
return bundler.write({
|
|
886
|
+
...outputOptions,
|
|
887
|
+
format: "esm",
|
|
888
|
+
entryFileNames: "[name].mjs",
|
|
889
|
+
chunkFileNames: "[name].mjs"
|
|
890
|
+
});
|
|
891
|
+
},
|
|
892
|
+
close: () => {
|
|
893
|
+
return bundler.close();
|
|
894
|
+
}
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
function removeAllExceptTelemetryConfig(result) {
|
|
898
|
+
let mastraClass = null;
|
|
899
|
+
const t = babel__namespace.default.types;
|
|
900
|
+
return {
|
|
901
|
+
name: "remove-all-except-telemetry-config",
|
|
902
|
+
visitor: {
|
|
903
|
+
ExportNamedDeclaration: {
|
|
904
|
+
// remove all exports
|
|
905
|
+
exit(path3) {
|
|
906
|
+
path3.remove();
|
|
907
|
+
}
|
|
908
|
+
},
|
|
909
|
+
ImportDeclaration(path3) {
|
|
910
|
+
if ((path3.node.source.value === "@mastra/core" || path3.node.source.value === "@mastra/core/mastra") && path3.node.specifiers) {
|
|
911
|
+
mastraClass = path3.node.specifiers.find(
|
|
912
|
+
// @ts-ignore - no need to type
|
|
913
|
+
(p) => p.imported?.name === "Mastra"
|
|
914
|
+
)?.local?.name ?? null;
|
|
915
|
+
}
|
|
916
|
+
},
|
|
917
|
+
NewExpression(path3) {
|
|
918
|
+
if (mastraClass && path3.node.callee.name === mastraClass) {
|
|
919
|
+
let telemetry = path3.node.arguments[0]?.properties?.find(
|
|
920
|
+
// @ts-ignore
|
|
921
|
+
(prop) => prop.key.name === "telemetry"
|
|
922
|
+
);
|
|
923
|
+
const programPath = path3.scope.getProgramParent().path;
|
|
924
|
+
if (!programPath) {
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
if (telemetry) {
|
|
928
|
+
result.hasCustomConfig = true;
|
|
929
|
+
} else {
|
|
930
|
+
telemetry = {
|
|
931
|
+
value: t.objectExpression([])
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
const exportDeclaration = t.exportNamedDeclaration(
|
|
935
|
+
t.variableDeclaration("const", [t.variableDeclarator(t.identifier("telemetry"), telemetry.value)]),
|
|
936
|
+
[]
|
|
937
|
+
);
|
|
938
|
+
programPath.node.body.push(exportDeclaration);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
// src/build/telemetry.ts
|
|
946
|
+
async function writeTelemetryConfig(entryFile, outputDir) {
|
|
947
|
+
const result = {
|
|
948
|
+
hasCustomConfig: false
|
|
949
|
+
};
|
|
950
|
+
const bundle = await rollup.rollup({
|
|
951
|
+
input: {
|
|
952
|
+
"telemetry-config": entryFile
|
|
953
|
+
},
|
|
954
|
+
treeshake: true,
|
|
955
|
+
plugins: [
|
|
956
|
+
// transpile typescript to something we understand
|
|
957
|
+
esbuild__default.default({
|
|
958
|
+
target: "node20",
|
|
959
|
+
platform: "node",
|
|
960
|
+
minify: false
|
|
961
|
+
}),
|
|
962
|
+
{
|
|
963
|
+
name: "get-telemetry-config",
|
|
964
|
+
transform(code, id) {
|
|
965
|
+
if (!this.getModuleInfo(id)?.isEntry) {
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
return new Promise((resolve2, reject) => {
|
|
969
|
+
babel__namespace.transform(
|
|
970
|
+
code,
|
|
971
|
+
{
|
|
972
|
+
babelrc: false,
|
|
973
|
+
configFile: false,
|
|
974
|
+
filename: id,
|
|
975
|
+
plugins: [removeAllExceptTelemetryConfig(result)]
|
|
976
|
+
},
|
|
977
|
+
(err, result2) => {
|
|
978
|
+
if (err) {
|
|
979
|
+
return reject(err);
|
|
980
|
+
}
|
|
981
|
+
resolve2({
|
|
982
|
+
code: result2.code,
|
|
983
|
+
map: result2.map
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
);
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
},
|
|
990
|
+
// let esbuild remove all unused imports
|
|
991
|
+
esbuild__default.default({
|
|
992
|
+
target: "node20",
|
|
993
|
+
platform: "node",
|
|
994
|
+
minify: false
|
|
995
|
+
})
|
|
996
|
+
]
|
|
997
|
+
});
|
|
998
|
+
await bundle.write({
|
|
999
|
+
dir: outputDir,
|
|
1000
|
+
format: "es",
|
|
1001
|
+
entryFileNames: "[name].mjs"
|
|
1002
|
+
});
|
|
1003
|
+
return result;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// src/bundler/index.ts
|
|
1007
|
+
var Bundler = class extends bundler.MastraBundler {
|
|
1008
|
+
analyzeOutputDir = ".build";
|
|
1009
|
+
outputDir = "output";
|
|
1010
|
+
constructor(name, component = "BUNDLER") {
|
|
1011
|
+
super({ name, component });
|
|
1012
|
+
}
|
|
1013
|
+
async prepare(outputDirectory) {
|
|
1014
|
+
await fsExtra2__default.default.emptyDir(outputDirectory);
|
|
1015
|
+
await fsExtra.ensureDir(path.join(outputDirectory, this.analyzeOutputDir));
|
|
1016
|
+
await fsExtra.ensureDir(path.join(outputDirectory, this.outputDir));
|
|
1017
|
+
}
|
|
1018
|
+
async writeInstrumentationFile(outputDirectory) {
|
|
1019
|
+
const instrumentationFile = path.join(outputDirectory, "instrumentation.mjs");
|
|
1020
|
+
const __dirname = path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
|
|
1021
|
+
await fsExtra.copy(path.join(__dirname, "templates", "instrumentation-template.js"), instrumentationFile);
|
|
1022
|
+
}
|
|
1023
|
+
async writePackageJson(outputDirectory, dependencies) {
|
|
1024
|
+
this.logger.debug(`Writing project's package.json`);
|
|
1025
|
+
await fsExtra.ensureDir(outputDirectory);
|
|
1026
|
+
const pkgPath = path.join(outputDirectory, "package.json");
|
|
1027
|
+
const dependenciesMap = /* @__PURE__ */ new Map();
|
|
1028
|
+
for (const [key, value] of dependencies.entries()) {
|
|
1029
|
+
if (key.startsWith("@")) {
|
|
1030
|
+
const pkgChunks = key.split("/");
|
|
1031
|
+
dependenciesMap.set(`${pkgChunks[0]}/${pkgChunks[1]}`, value);
|
|
1032
|
+
continue;
|
|
1033
|
+
}
|
|
1034
|
+
dependenciesMap.set(key, value);
|
|
1035
|
+
}
|
|
1036
|
+
dependenciesMap.set("@opentelemetry/instrumentation", "latest");
|
|
1037
|
+
await fsPromises.writeFile(
|
|
1038
|
+
pkgPath,
|
|
1039
|
+
JSON.stringify(
|
|
1040
|
+
{
|
|
1041
|
+
name: "server",
|
|
1042
|
+
version: "1.0.0",
|
|
1043
|
+
description: "",
|
|
1044
|
+
type: "module",
|
|
1045
|
+
main: "index.mjs",
|
|
1046
|
+
scripts: {
|
|
1047
|
+
start: "node ./index.mjs"
|
|
1048
|
+
},
|
|
1049
|
+
author: "Mastra",
|
|
1050
|
+
license: "ISC",
|
|
1051
|
+
dependencies: Object.fromEntries(dependenciesMap.entries())
|
|
1052
|
+
},
|
|
1053
|
+
null,
|
|
1054
|
+
2
|
|
1055
|
+
)
|
|
1056
|
+
);
|
|
1057
|
+
}
|
|
1058
|
+
createBundler(inputOptions, outputOptions) {
|
|
1059
|
+
return createBundler(inputOptions, outputOptions);
|
|
1060
|
+
}
|
|
1061
|
+
async analyze(entry, mastraFile, outputDirectory) {
|
|
1062
|
+
return await analyzeBundle(entry, mastraFile, path.join(outputDirectory, this.analyzeOutputDir), "node", this.logger);
|
|
1063
|
+
}
|
|
1064
|
+
async installDependencies(outputDirectory, rootDir = process.cwd()) {
|
|
1065
|
+
const deps = new Deps(rootDir);
|
|
1066
|
+
deps.__setLogger(this.logger);
|
|
1067
|
+
await deps.install({ dir: path.join(outputDirectory, this.outputDir) });
|
|
1068
|
+
}
|
|
1069
|
+
async _bundle(serverFile, mastraEntryFile, outputDirectory, bundleLocation = path.join(outputDirectory, this.outputDir)) {
|
|
1070
|
+
this.logger.info("Start bundling Mastra");
|
|
1071
|
+
const isVirtual = serverFile.includes("\n") || fs3.existsSync(serverFile);
|
|
1072
|
+
const analyzedBundleInfo = await analyzeBundle(
|
|
1073
|
+
serverFile,
|
|
1074
|
+
mastraEntryFile,
|
|
1075
|
+
path.join(outputDirectory, this.analyzeOutputDir),
|
|
1076
|
+
"node",
|
|
1077
|
+
this.logger
|
|
1078
|
+
);
|
|
1079
|
+
await writeTelemetryConfig(mastraEntryFile, path.join(outputDirectory, this.outputDir));
|
|
1080
|
+
const dependenciesToInstall = Array.from(analyzedBundleInfo.externalDependencies).reduce((acc, dep) => {
|
|
1081
|
+
acc.set(dep, "latest");
|
|
1082
|
+
return acc;
|
|
1083
|
+
}, /* @__PURE__ */ new Map());
|
|
1084
|
+
await this.writePackageJson(path.join(outputDirectory, this.outputDir), dependenciesToInstall);
|
|
1085
|
+
await this.writeInstrumentationFile(path.join(outputDirectory, this.outputDir));
|
|
1086
|
+
this.logger.info("Bundling Mastra application");
|
|
1087
|
+
const inputOptions = await getInputOptions(mastraEntryFile, analyzedBundleInfo, "node");
|
|
1088
|
+
if (isVirtual) {
|
|
1089
|
+
inputOptions.input = { index: "#entry" };
|
|
1090
|
+
if (Array.isArray(inputOptions.plugins)) {
|
|
1091
|
+
inputOptions.plugins.unshift(virtual__default.default({ "#entry": serverFile }));
|
|
1092
|
+
} else {
|
|
1093
|
+
inputOptions.plugins = [virtual__default.default({ "#entry": serverFile })];
|
|
1094
|
+
}
|
|
1095
|
+
} else {
|
|
1096
|
+
inputOptions.input = { index: serverFile };
|
|
1097
|
+
}
|
|
1098
|
+
const bundler = await this.createBundler(inputOptions, { dir: bundleLocation });
|
|
1099
|
+
await bundler.write();
|
|
1100
|
+
this.logger.info("Bundling Mastra done");
|
|
1101
|
+
this.logger.info("Installing dependencies");
|
|
1102
|
+
await this.installDependencies(outputDirectory);
|
|
1103
|
+
this.logger.info("Done installing dependencies");
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
|
|
1107
|
+
// src/deploy/base.ts
|
|
1108
|
+
var Deployer = class extends Bundler {
|
|
1109
|
+
deps = new Deps();
|
|
1110
|
+
constructor(args) {
|
|
1111
|
+
super(args.name, "DEPLOYER");
|
|
1112
|
+
this.deps.__setLogger(this.logger);
|
|
1113
|
+
}
|
|
1114
|
+
getEnvFiles() {
|
|
1115
|
+
const possibleFiles = [".env.production", ".env"];
|
|
1116
|
+
try {
|
|
1117
|
+
const fileService = new FileService();
|
|
1118
|
+
const envFile = fileService.getFirstExistingFile(possibleFiles);
|
|
1119
|
+
return Promise.resolve([envFile]);
|
|
1120
|
+
} catch {
|
|
1121
|
+
}
|
|
1122
|
+
return Promise.resolve([]);
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
1125
|
+
function removeAllExceptDeployer() {
|
|
1126
|
+
let mastraClass = null;
|
|
1127
|
+
const t = babel__namespace.default.types;
|
|
1128
|
+
return {
|
|
1129
|
+
name: "remove-all-except-deployer",
|
|
1130
|
+
visitor: {
|
|
1131
|
+
ExportNamedDeclaration: {
|
|
1132
|
+
// remove all exports
|
|
1133
|
+
exit(path3) {
|
|
1134
|
+
path3.remove();
|
|
1135
|
+
}
|
|
1136
|
+
},
|
|
1137
|
+
ImportDeclaration(path3) {
|
|
1138
|
+
if ((path3.node.source.value === "@mastra/core" || path3.node.source.value === "@mastra/core/mastra") && path3.node.specifiers) {
|
|
1139
|
+
mastraClass = path3.node.specifiers.find(
|
|
1140
|
+
// @ts-ignore - no need to type
|
|
1141
|
+
(p) => p.imported?.name === "Mastra"
|
|
1142
|
+
)?.local?.name ?? null;
|
|
1143
|
+
}
|
|
1144
|
+
},
|
|
1145
|
+
NewExpression(path3) {
|
|
1146
|
+
if (mastraClass && path3.node.callee.name === mastraClass) {
|
|
1147
|
+
const deployer = path3.node.arguments[0]?.properties?.find(
|
|
1148
|
+
// @ts-ignore
|
|
1149
|
+
(prop) => prop.key.name === "deployer"
|
|
1150
|
+
);
|
|
1151
|
+
const programPath = path3.scope.getProgramParent().path;
|
|
1152
|
+
if (!deployer || !programPath) {
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
const exportDeclaration = t.exportNamedDeclaration(
|
|
1156
|
+
t.variableDeclaration("const", [t.variableDeclarator(t.identifier("deployer"), deployer.value)]),
|
|
1157
|
+
[]
|
|
1158
|
+
);
|
|
1159
|
+
programPath.node.body.push(exportDeclaration);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
};
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
// src/build/deployer.ts
|
|
1167
|
+
async function getDeployer(entryFile, outputDir) {
|
|
1168
|
+
const bundle = await rollup.rollup({
|
|
1169
|
+
input: {
|
|
1170
|
+
deployer: entryFile
|
|
1171
|
+
},
|
|
1172
|
+
treeshake: true,
|
|
1173
|
+
plugins: [
|
|
1174
|
+
// transpile typescript to something we understand
|
|
1175
|
+
esbuild__default.default({
|
|
1176
|
+
target: "node20",
|
|
1177
|
+
platform: "node",
|
|
1178
|
+
minify: false
|
|
1179
|
+
}),
|
|
1180
|
+
{
|
|
1181
|
+
name: "get-deployer",
|
|
1182
|
+
transform(code, id) {
|
|
1183
|
+
if (!this.getModuleInfo(id)?.isEntry) {
|
|
1184
|
+
return;
|
|
1185
|
+
}
|
|
1186
|
+
return new Promise((resolve2, reject) => {
|
|
1187
|
+
babel__namespace.transform(
|
|
1188
|
+
code,
|
|
1189
|
+
{
|
|
1190
|
+
babelrc: false,
|
|
1191
|
+
configFile: false,
|
|
1192
|
+
filename: id,
|
|
1193
|
+
plugins: [removeAllExceptDeployer]
|
|
1194
|
+
},
|
|
1195
|
+
(err, result) => {
|
|
1196
|
+
if (err) {
|
|
1197
|
+
return reject(err);
|
|
1198
|
+
}
|
|
1199
|
+
resolve2({
|
|
1200
|
+
code: result.code,
|
|
1201
|
+
map: result.map
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
);
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
},
|
|
1208
|
+
// let esbuild remove all unused imports
|
|
1209
|
+
esbuild__default.default({
|
|
1210
|
+
target: "node20",
|
|
1211
|
+
platform: "node",
|
|
1212
|
+
minify: false
|
|
1213
|
+
})
|
|
1214
|
+
]
|
|
1215
|
+
});
|
|
1216
|
+
await bundle.write({
|
|
1217
|
+
dir: outputDir,
|
|
1218
|
+
format: "es",
|
|
1219
|
+
entryFileNames: "[name].mjs"
|
|
1220
|
+
});
|
|
1221
|
+
return (await import(`file:${outputDir}/deployer.mjs`)).deployer;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
exports.Deployer = Deployer;
|
|
1225
|
+
exports.Deps = Deps;
|
|
1226
|
+
exports.FileService = FileService;
|
|
1227
|
+
exports.createChildProcessLogger = createChildProcessLogger;
|
|
1228
|
+
exports.createPinoStream = createPinoStream;
|
|
1229
|
+
exports.getDeployer = getDeployer;
|