@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Bundler } from '../_tsup-dts-rollup.cjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Bundler } from '../_tsup-dts-rollup.js';
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Deps, writeTelemetryConfig } from './chunk-AXS5WSIK.js';
|
|
2
|
+
import { analyzeBundle } from './chunk-JMH7HCD6.js';
|
|
3
|
+
import { createBundler, getInputOptions } from './chunk-DTSFVNIF.js';
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { writeFile } from 'node:fs/promises';
|
|
6
|
+
import { join, dirname } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { MastraBundler } from '@mastra/core/bundler';
|
|
9
|
+
import virtual from '@rollup/plugin-virtual';
|
|
10
|
+
import { ensureDir, copy } from 'fs-extra';
|
|
11
|
+
import fsExtra from 'fs-extra/esm';
|
|
12
|
+
|
|
13
|
+
var Bundler = class extends MastraBundler {
|
|
14
|
+
analyzeOutputDir = ".build";
|
|
15
|
+
outputDir = "output";
|
|
16
|
+
constructor(name, component = "BUNDLER") {
|
|
17
|
+
super({ name, component });
|
|
18
|
+
}
|
|
19
|
+
async prepare(outputDirectory) {
|
|
20
|
+
await fsExtra.emptyDir(outputDirectory);
|
|
21
|
+
await ensureDir(join(outputDirectory, this.analyzeOutputDir));
|
|
22
|
+
await ensureDir(join(outputDirectory, this.outputDir));
|
|
23
|
+
}
|
|
24
|
+
async writeInstrumentationFile(outputDirectory) {
|
|
25
|
+
const instrumentationFile = join(outputDirectory, "instrumentation.mjs");
|
|
26
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
27
|
+
await copy(join(__dirname, "templates", "instrumentation-template.js"), instrumentationFile);
|
|
28
|
+
}
|
|
29
|
+
async writePackageJson(outputDirectory, dependencies) {
|
|
30
|
+
this.logger.debug(`Writing project's package.json`);
|
|
31
|
+
await ensureDir(outputDirectory);
|
|
32
|
+
const pkgPath = join(outputDirectory, "package.json");
|
|
33
|
+
const dependenciesMap = /* @__PURE__ */ new Map();
|
|
34
|
+
for (const [key, value] of dependencies.entries()) {
|
|
35
|
+
if (key.startsWith("@")) {
|
|
36
|
+
const pkgChunks = key.split("/");
|
|
37
|
+
dependenciesMap.set(`${pkgChunks[0]}/${pkgChunks[1]}`, value);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
dependenciesMap.set(key, value);
|
|
41
|
+
}
|
|
42
|
+
dependenciesMap.set("@opentelemetry/instrumentation", "latest");
|
|
43
|
+
await writeFile(
|
|
44
|
+
pkgPath,
|
|
45
|
+
JSON.stringify(
|
|
46
|
+
{
|
|
47
|
+
name: "server",
|
|
48
|
+
version: "1.0.0",
|
|
49
|
+
description: "",
|
|
50
|
+
type: "module",
|
|
51
|
+
main: "index.mjs",
|
|
52
|
+
scripts: {
|
|
53
|
+
start: "node ./index.mjs"
|
|
54
|
+
},
|
|
55
|
+
author: "Mastra",
|
|
56
|
+
license: "ISC",
|
|
57
|
+
dependencies: Object.fromEntries(dependenciesMap.entries())
|
|
58
|
+
},
|
|
59
|
+
null,
|
|
60
|
+
2
|
|
61
|
+
)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
createBundler(inputOptions, outputOptions) {
|
|
65
|
+
return createBundler(inputOptions, outputOptions);
|
|
66
|
+
}
|
|
67
|
+
async analyze(entry, mastraFile, outputDirectory) {
|
|
68
|
+
return await analyzeBundle(entry, mastraFile, join(outputDirectory, this.analyzeOutputDir), "node", this.logger);
|
|
69
|
+
}
|
|
70
|
+
async installDependencies(outputDirectory, rootDir = process.cwd()) {
|
|
71
|
+
const deps = new Deps(rootDir);
|
|
72
|
+
deps.__setLogger(this.logger);
|
|
73
|
+
await deps.install({ dir: join(outputDirectory, this.outputDir) });
|
|
74
|
+
}
|
|
75
|
+
async _bundle(serverFile, mastraEntryFile, outputDirectory, bundleLocation = join(outputDirectory, this.outputDir)) {
|
|
76
|
+
this.logger.info("Start bundling Mastra");
|
|
77
|
+
const isVirtual = serverFile.includes("\n") || existsSync(serverFile);
|
|
78
|
+
const analyzedBundleInfo = await analyzeBundle(
|
|
79
|
+
serverFile,
|
|
80
|
+
mastraEntryFile,
|
|
81
|
+
join(outputDirectory, this.analyzeOutputDir),
|
|
82
|
+
"node",
|
|
83
|
+
this.logger
|
|
84
|
+
);
|
|
85
|
+
await writeTelemetryConfig(mastraEntryFile, join(outputDirectory, this.outputDir));
|
|
86
|
+
const dependenciesToInstall = Array.from(analyzedBundleInfo.externalDependencies).reduce((acc, dep) => {
|
|
87
|
+
acc.set(dep, "latest");
|
|
88
|
+
return acc;
|
|
89
|
+
}, /* @__PURE__ */ new Map());
|
|
90
|
+
await this.writePackageJson(join(outputDirectory, this.outputDir), dependenciesToInstall);
|
|
91
|
+
await this.writeInstrumentationFile(join(outputDirectory, this.outputDir));
|
|
92
|
+
this.logger.info("Bundling Mastra application");
|
|
93
|
+
const inputOptions = await getInputOptions(mastraEntryFile, analyzedBundleInfo, "node");
|
|
94
|
+
if (isVirtual) {
|
|
95
|
+
inputOptions.input = { index: "#entry" };
|
|
96
|
+
if (Array.isArray(inputOptions.plugins)) {
|
|
97
|
+
inputOptions.plugins.unshift(virtual({ "#entry": serverFile }));
|
|
98
|
+
} else {
|
|
99
|
+
inputOptions.plugins = [virtual({ "#entry": serverFile })];
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
inputOptions.input = { index: serverFile };
|
|
103
|
+
}
|
|
104
|
+
const bundler = await this.createBundler(inputOptions, { dir: bundleLocation });
|
|
105
|
+
await bundler.write();
|
|
106
|
+
this.logger.info("Bundling Mastra done");
|
|
107
|
+
this.logger.info("Installing dependencies");
|
|
108
|
+
await this.installDependencies(outputDirectory);
|
|
109
|
+
this.logger.info("Done installing dependencies");
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export { Bundler };
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { Transform } from 'stream';
|
|
3
|
+
import { MastraBase } from '@mastra/core/base';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path, { dirname } from 'path';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
import fsExtra from 'fs-extra/esm';
|
|
8
|
+
import fsPromises from 'fs/promises';
|
|
9
|
+
import * as babel from '@babel/core';
|
|
10
|
+
import babel__default from '@babel/core';
|
|
11
|
+
import { rollup } from 'rollup';
|
|
12
|
+
import esbuild from 'rollup-plugin-esbuild';
|
|
13
|
+
|
|
14
|
+
// src/deploy/log.ts
|
|
15
|
+
var createPinoStream = (logger) => {
|
|
16
|
+
return new Transform({
|
|
17
|
+
transform(chunk, _encoding, callback) {
|
|
18
|
+
const line = chunk.toString().trim();
|
|
19
|
+
if (line) {
|
|
20
|
+
console.log(line);
|
|
21
|
+
logger.info(line);
|
|
22
|
+
}
|
|
23
|
+
callback(null, chunk);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
function createChildProcessLogger({ logger, root }) {
|
|
28
|
+
const pinoStream = createPinoStream(logger);
|
|
29
|
+
return async ({ cmd, args, env }) => {
|
|
30
|
+
try {
|
|
31
|
+
const subprocess = spawn(cmd, args, {
|
|
32
|
+
cwd: root,
|
|
33
|
+
shell: true,
|
|
34
|
+
env
|
|
35
|
+
});
|
|
36
|
+
subprocess.stdout?.pipe(pinoStream);
|
|
37
|
+
subprocess.stderr?.pipe(pinoStream);
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
subprocess.on("close", (code) => {
|
|
40
|
+
pinoStream.end();
|
|
41
|
+
if (code === 0) {
|
|
42
|
+
resolve({ success: true });
|
|
43
|
+
} else {
|
|
44
|
+
reject(new Error(`Process exited with code ${code}`));
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
subprocess.on("error", (error) => {
|
|
48
|
+
pinoStream.end();
|
|
49
|
+
logger.error("Process failed", { error });
|
|
50
|
+
reject(error);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.log(error);
|
|
55
|
+
logger.error("Process failed", { error });
|
|
56
|
+
pinoStream.end();
|
|
57
|
+
return { success: false, error };
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
var Deps = class extends MastraBase {
|
|
62
|
+
packageManager;
|
|
63
|
+
rootDir;
|
|
64
|
+
constructor(rootDir = process.cwd()) {
|
|
65
|
+
super({ component: "DEPLOYER", name: "DEPS" });
|
|
66
|
+
this.rootDir = rootDir;
|
|
67
|
+
this.packageManager = this.getPackageManager();
|
|
68
|
+
}
|
|
69
|
+
findLockFile(dir) {
|
|
70
|
+
const lockFiles = ["pnpm-lock.yaml", "package-lock.json", "yarn.lock", "bun.lock"];
|
|
71
|
+
for (const file of lockFiles) {
|
|
72
|
+
if (fs.existsSync(path.join(dir, file))) {
|
|
73
|
+
return file;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const parentDir = path.resolve(dir, "..");
|
|
77
|
+
if (parentDir !== dir) {
|
|
78
|
+
return this.findLockFile(parentDir);
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
getPackageManager() {
|
|
83
|
+
const lockFile = this.findLockFile(this.rootDir);
|
|
84
|
+
switch (lockFile) {
|
|
85
|
+
case "pnpm-lock.yaml":
|
|
86
|
+
return "pnpm";
|
|
87
|
+
case "package-lock.json":
|
|
88
|
+
return "npm";
|
|
89
|
+
case "yarn.lock":
|
|
90
|
+
return "yarn";
|
|
91
|
+
case "bun.lock":
|
|
92
|
+
return "bun";
|
|
93
|
+
default:
|
|
94
|
+
return "npm";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async install({ dir = this.rootDir, packages = [] }) {
|
|
98
|
+
let runCommand = this.packageManager;
|
|
99
|
+
if (this.packageManager === "npm") {
|
|
100
|
+
runCommand = `${this.packageManager} i`;
|
|
101
|
+
} else {
|
|
102
|
+
runCommand = `${this.packageManager} ${packages?.length > 0 ? `add` : `install`}`;
|
|
103
|
+
}
|
|
104
|
+
const cpLogger = createChildProcessLogger({
|
|
105
|
+
logger: this.logger,
|
|
106
|
+
root: dir
|
|
107
|
+
});
|
|
108
|
+
return cpLogger({
|
|
109
|
+
cmd: runCommand,
|
|
110
|
+
args: packages,
|
|
111
|
+
env: {
|
|
112
|
+
PATH: process.env.PATH
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async installPackages(packages) {
|
|
117
|
+
let runCommand = this.packageManager;
|
|
118
|
+
if (this.packageManager === "npm") {
|
|
119
|
+
runCommand = `${this.packageManager} i`;
|
|
120
|
+
} else {
|
|
121
|
+
runCommand = `${this.packageManager} add`;
|
|
122
|
+
}
|
|
123
|
+
const cpLogger = createChildProcessLogger({
|
|
124
|
+
logger: this.logger,
|
|
125
|
+
root: ""
|
|
126
|
+
});
|
|
127
|
+
return cpLogger({
|
|
128
|
+
cmd: `${runCommand}`,
|
|
129
|
+
args: packages,
|
|
130
|
+
env: {
|
|
131
|
+
PATH: process.env.PATH
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async checkDependencies(dependencies) {
|
|
136
|
+
try {
|
|
137
|
+
const packageJsonPath = path.join(this.rootDir, "package.json");
|
|
138
|
+
try {
|
|
139
|
+
await fsPromises.access(packageJsonPath);
|
|
140
|
+
} catch {
|
|
141
|
+
return "No package.json file found in the current directory";
|
|
142
|
+
}
|
|
143
|
+
const packageJson = JSON.parse(await fsPromises.readFile(packageJsonPath, "utf-8"));
|
|
144
|
+
for (const dependency of dependencies) {
|
|
145
|
+
if (!packageJson.dependencies || !packageJson.dependencies[dependency]) {
|
|
146
|
+
return `Please install ${dependency} before running this command (${this.packageManager} install ${dependency})`;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return "ok";
|
|
150
|
+
} catch (err) {
|
|
151
|
+
console.error(err);
|
|
152
|
+
return "Could not check dependencies";
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async getProjectName() {
|
|
156
|
+
try {
|
|
157
|
+
const packageJsonPath = path.join(this.rootDir, "package.json");
|
|
158
|
+
const packageJson = await fsPromises.readFile(packageJsonPath, "utf-8");
|
|
159
|
+
const pkg = JSON.parse(packageJson);
|
|
160
|
+
return pkg.name;
|
|
161
|
+
} catch (err) {
|
|
162
|
+
throw err;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
async getPackageVersion() {
|
|
166
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
167
|
+
const __dirname = dirname(__filename);
|
|
168
|
+
const pkgJsonPath = path.join(__dirname, "..", "..", "package.json");
|
|
169
|
+
const content = await fsExtra.readJSON(pkgJsonPath);
|
|
170
|
+
return content.version;
|
|
171
|
+
}
|
|
172
|
+
async addScriptsToPackageJson(scripts) {
|
|
173
|
+
const packageJson = JSON.parse(await fsPromises.readFile("package.json", "utf-8"));
|
|
174
|
+
packageJson.scripts = {
|
|
175
|
+
...packageJson.scripts,
|
|
176
|
+
...scripts
|
|
177
|
+
};
|
|
178
|
+
await fsPromises.writeFile("package.json", JSON.stringify(packageJson, null, 2));
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
function removeAllExceptTelemetryConfig(result) {
|
|
182
|
+
let mastraClass = null;
|
|
183
|
+
const t = babel__default.types;
|
|
184
|
+
return {
|
|
185
|
+
name: "remove-all-except-telemetry-config",
|
|
186
|
+
visitor: {
|
|
187
|
+
ExportNamedDeclaration: {
|
|
188
|
+
// remove all exports
|
|
189
|
+
exit(path2) {
|
|
190
|
+
path2.remove();
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
ImportDeclaration(path2) {
|
|
194
|
+
if ((path2.node.source.value === "@mastra/core" || path2.node.source.value === "@mastra/core/mastra") && path2.node.specifiers) {
|
|
195
|
+
mastraClass = path2.node.specifiers.find(
|
|
196
|
+
// @ts-ignore - no need to type
|
|
197
|
+
(p) => p.imported?.name === "Mastra"
|
|
198
|
+
)?.local?.name ?? null;
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
NewExpression(path2) {
|
|
202
|
+
if (mastraClass && path2.node.callee.name === mastraClass) {
|
|
203
|
+
let telemetry = path2.node.arguments[0]?.properties?.find(
|
|
204
|
+
// @ts-ignore
|
|
205
|
+
(prop) => prop.key.name === "telemetry"
|
|
206
|
+
);
|
|
207
|
+
const programPath = path2.scope.getProgramParent().path;
|
|
208
|
+
if (!programPath) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (telemetry) {
|
|
212
|
+
result.hasCustomConfig = true;
|
|
213
|
+
} else {
|
|
214
|
+
telemetry = {
|
|
215
|
+
value: t.objectExpression([])
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const exportDeclaration = t.exportNamedDeclaration(
|
|
219
|
+
t.variableDeclaration("const", [t.variableDeclarator(t.identifier("telemetry"), telemetry.value)]),
|
|
220
|
+
[]
|
|
221
|
+
);
|
|
222
|
+
programPath.node.body.push(exportDeclaration);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/build/telemetry.ts
|
|
230
|
+
async function writeTelemetryConfig(entryFile, outputDir) {
|
|
231
|
+
const result = {
|
|
232
|
+
hasCustomConfig: false
|
|
233
|
+
};
|
|
234
|
+
const bundle = await rollup({
|
|
235
|
+
input: {
|
|
236
|
+
"telemetry-config": entryFile
|
|
237
|
+
},
|
|
238
|
+
treeshake: true,
|
|
239
|
+
plugins: [
|
|
240
|
+
// transpile typescript to something we understand
|
|
241
|
+
esbuild({
|
|
242
|
+
target: "node20",
|
|
243
|
+
platform: "node",
|
|
244
|
+
minify: false
|
|
245
|
+
}),
|
|
246
|
+
{
|
|
247
|
+
name: "get-telemetry-config",
|
|
248
|
+
transform(code, id) {
|
|
249
|
+
if (!this.getModuleInfo(id)?.isEntry) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
return new Promise((resolve, reject) => {
|
|
253
|
+
babel.transform(
|
|
254
|
+
code,
|
|
255
|
+
{
|
|
256
|
+
babelrc: false,
|
|
257
|
+
configFile: false,
|
|
258
|
+
filename: id,
|
|
259
|
+
plugins: [removeAllExceptTelemetryConfig(result)]
|
|
260
|
+
},
|
|
261
|
+
(err, result2) => {
|
|
262
|
+
if (err) {
|
|
263
|
+
return reject(err);
|
|
264
|
+
}
|
|
265
|
+
resolve({
|
|
266
|
+
code: result2.code,
|
|
267
|
+
map: result2.map
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
// let esbuild remove all unused imports
|
|
275
|
+
esbuild({
|
|
276
|
+
target: "node20",
|
|
277
|
+
platform: "node",
|
|
278
|
+
minify: false
|
|
279
|
+
})
|
|
280
|
+
]
|
|
281
|
+
});
|
|
282
|
+
await bundle.write({
|
|
283
|
+
dir: outputDir,
|
|
284
|
+
format: "es",
|
|
285
|
+
entryFileNames: "[name].mjs"
|
|
286
|
+
});
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export { Deps, createChildProcessLogger, createPinoStream, writeTelemetryConfig };
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { removeDeployer } from './chunk-YNXJO2XU.js';
|
|
2
|
+
import alias from '@rollup/plugin-alias';
|
|
3
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
4
|
+
import json from '@rollup/plugin-json';
|
|
5
|
+
import nodeResolve from '@rollup/plugin-node-resolve';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { rollup } from 'rollup';
|
|
8
|
+
import esbuild from 'rollup-plugin-esbuild';
|
|
9
|
+
import * as babel from '@babel/core';
|
|
10
|
+
import babel__default from '@babel/core';
|
|
11
|
+
import { currentTarget } from '@neon-rs/load';
|
|
12
|
+
import { familySync, GLIBC } from 'detect-libc';
|
|
13
|
+
import { dirname, join, resolve } from 'path';
|
|
14
|
+
import fsExtra from 'fs-extra/esm';
|
|
15
|
+
import { addNamed } from '@babel/helper-module-imports';
|
|
16
|
+
import { platform } from 'process';
|
|
17
|
+
|
|
18
|
+
function rewriteLibsqlImport() {
|
|
19
|
+
const t = babel__default.types;
|
|
20
|
+
let hasReplaced = false;
|
|
21
|
+
return {
|
|
22
|
+
name: "rewrite-libsql-import",
|
|
23
|
+
visitor: {
|
|
24
|
+
FunctionDeclaration(path) {
|
|
25
|
+
if (path.node.id?.name === "requireNative" && !hasReplaced) {
|
|
26
|
+
hasReplaced = true;
|
|
27
|
+
const createRequire = addNamed(path, "createRequire", "module");
|
|
28
|
+
const requireIdentifier = t.identifier("require");
|
|
29
|
+
path.replaceWith(
|
|
30
|
+
t.functionDeclaration(
|
|
31
|
+
t.identifier("requireNative"),
|
|
32
|
+
[],
|
|
33
|
+
t.blockStatement([
|
|
34
|
+
t.variableDeclaration("const", [
|
|
35
|
+
t.variableDeclarator(
|
|
36
|
+
requireIdentifier,
|
|
37
|
+
t.callExpression(createRequire, [
|
|
38
|
+
t.memberExpression(
|
|
39
|
+
t.metaProperty(t.identifier("import"), t.identifier("meta")),
|
|
40
|
+
t.identifier("url")
|
|
41
|
+
)
|
|
42
|
+
])
|
|
43
|
+
)
|
|
44
|
+
]),
|
|
45
|
+
t.returnStatement(t.callExpression(requireIdentifier, [t.stringLiteral("./libsql.node")]))
|
|
46
|
+
])
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/build/plugins/fix-libsql.ts
|
|
56
|
+
function libSqlFix() {
|
|
57
|
+
return {
|
|
58
|
+
name: "libSqlFix",
|
|
59
|
+
transform(code, id) {
|
|
60
|
+
if (!id.includes("\\libsql\\index.js") && !id.includes("/libsql/index.js")) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return new Promise((resolve2, reject) => {
|
|
64
|
+
babel.transform(
|
|
65
|
+
code,
|
|
66
|
+
{
|
|
67
|
+
babelrc: false,
|
|
68
|
+
configFile: false,
|
|
69
|
+
filename: id,
|
|
70
|
+
plugins: [rewriteLibsqlImport]
|
|
71
|
+
},
|
|
72
|
+
(err, result) => {
|
|
73
|
+
if (err) {
|
|
74
|
+
return reject(err);
|
|
75
|
+
}
|
|
76
|
+
resolve2({
|
|
77
|
+
code: result.code,
|
|
78
|
+
map: result.map
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
async generateBundle({ file, dir }) {
|
|
85
|
+
if (!file && !dir) {
|
|
86
|
+
throw new Error("No output options were given.");
|
|
87
|
+
}
|
|
88
|
+
const outputDirectory = dir || dirname(file);
|
|
89
|
+
let target = currentTarget();
|
|
90
|
+
if (familySync() == GLIBC) {
|
|
91
|
+
switch (target) {
|
|
92
|
+
case "linux-x64-musl":
|
|
93
|
+
target = "linux-x64-gnu";
|
|
94
|
+
break;
|
|
95
|
+
case "linux-arm64-musl":
|
|
96
|
+
target = "linux-arm64-gnu";
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const fileToCopy = await this.resolve(`@libsql/${target}/index.node`);
|
|
101
|
+
if (fileToCopy) {
|
|
102
|
+
await fsExtra.copy(fileToCopy.id, join(outputDirectory, "libsql.node"));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function getTelemetryMachineFile() {
|
|
108
|
+
switch (platform) {
|
|
109
|
+
case "darwin":
|
|
110
|
+
return "getMachineId-darwin";
|
|
111
|
+
case "linux":
|
|
112
|
+
return "getMachineId-linux";
|
|
113
|
+
case "freebsd":
|
|
114
|
+
return "getMachineId-bsd";
|
|
115
|
+
case "win32":
|
|
116
|
+
return "getMachineId-win";
|
|
117
|
+
default:
|
|
118
|
+
return "getMachineId-unsupported";
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function telemetryFix() {
|
|
122
|
+
return {
|
|
123
|
+
name: "telemetry-fix",
|
|
124
|
+
transform(code, id) {
|
|
125
|
+
if (id.includes("require-in-the-middle")) {
|
|
126
|
+
return code.replace(
|
|
127
|
+
`const path = require('path')`,
|
|
128
|
+
`const path = require('path');
|
|
129
|
+
const { createRequire } = require('module');
|
|
130
|
+
const realRequire = createRequire(import.meta.url)`
|
|
131
|
+
).replaceAll(`require.resolve`, `realRequire.resolve`);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
resolveId(id, importer) {
|
|
135
|
+
if (id === "./machine-id/getMachineId" && importer) {
|
|
136
|
+
return { id: resolve(dirname(importer), `./machine-id/${getTelemetryMachineFile()}.js`) };
|
|
137
|
+
}
|
|
138
|
+
if (id === "formdata-node") {
|
|
139
|
+
return { id: "formdata-node", external: false };
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
load(id) {
|
|
143
|
+
if (id.startsWith("formdata-node")) {
|
|
144
|
+
return "export default {};";
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/build/bundler.ts
|
|
152
|
+
async function getInputOptions(entryFile, analyzedBundleInfo, platform2) {
|
|
153
|
+
let nodeResolvePlugin = platform2 === "node" ? nodeResolve({
|
|
154
|
+
preferBuiltins: true,
|
|
155
|
+
exportConditions: ["node", "import", "require"],
|
|
156
|
+
mainFields: ["module", "main"]
|
|
157
|
+
}) : nodeResolve({
|
|
158
|
+
preferBuiltins: false,
|
|
159
|
+
exportConditions: ["browser", "import", "require"],
|
|
160
|
+
mainFields: ["module", "main"],
|
|
161
|
+
browser: true
|
|
162
|
+
});
|
|
163
|
+
const externals = Array.from(analyzedBundleInfo.externalDependencies).concat(["@mastra/core/hooks"]);
|
|
164
|
+
return {
|
|
165
|
+
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
166
|
+
treeshake: true,
|
|
167
|
+
preserveSymlinks: true,
|
|
168
|
+
external: externals,
|
|
169
|
+
plugins: [
|
|
170
|
+
telemetryFix(),
|
|
171
|
+
libSqlFix(),
|
|
172
|
+
{
|
|
173
|
+
name: "alias-optimized-deps",
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
resolveId(id) {
|
|
176
|
+
if (!analyzedBundleInfo.dependencies.has(id)) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
const isInvalidChunk = analyzedBundleInfo.invalidChunks.has(analyzedBundleInfo.dependencies.get(id));
|
|
180
|
+
if (isInvalidChunk) {
|
|
181
|
+
return {
|
|
182
|
+
id,
|
|
183
|
+
external: true
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
id: ".mastra/.build/" + analyzedBundleInfo.dependencies.get(id),
|
|
188
|
+
external: false
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
alias({
|
|
193
|
+
entries: [
|
|
194
|
+
{
|
|
195
|
+
find: /^\#server$/,
|
|
196
|
+
replacement: fileURLToPath(import.meta.resolve("@mastra/deployer/server")).replaceAll("\\", "/")
|
|
197
|
+
},
|
|
198
|
+
{ find: /^\#mastra$/, replacement: entryFile.replaceAll("\\", "/") }
|
|
199
|
+
]
|
|
200
|
+
}),
|
|
201
|
+
esbuild({
|
|
202
|
+
target: "node20",
|
|
203
|
+
platform: platform2,
|
|
204
|
+
minify: false,
|
|
205
|
+
define: {
|
|
206
|
+
"process.env.NODE_ENV": JSON.stringify("production")
|
|
207
|
+
}
|
|
208
|
+
}),
|
|
209
|
+
commonjs({
|
|
210
|
+
extensions: [".js", ".ts"],
|
|
211
|
+
transformMixedEsModules: true,
|
|
212
|
+
esmExternals(id) {
|
|
213
|
+
return externals.includes(id);
|
|
214
|
+
}
|
|
215
|
+
}),
|
|
216
|
+
nodeResolvePlugin,
|
|
217
|
+
// for debugging
|
|
218
|
+
// {
|
|
219
|
+
// name: 'logger',
|
|
220
|
+
// //@ts-ignore
|
|
221
|
+
// resolveId(id, ...args) {
|
|
222
|
+
// console.log({ id, args });
|
|
223
|
+
// },
|
|
224
|
+
// // @ts-ignore
|
|
225
|
+
// transform(code, id) {
|
|
226
|
+
// if (code.includes('class Duplexify ')) {
|
|
227
|
+
// console.log({ duplex: id });
|
|
228
|
+
// }
|
|
229
|
+
// },
|
|
230
|
+
// },
|
|
231
|
+
json(),
|
|
232
|
+
removeDeployer(entryFile),
|
|
233
|
+
// treeshake unused imports
|
|
234
|
+
esbuild({
|
|
235
|
+
include: entryFile,
|
|
236
|
+
target: "node20",
|
|
237
|
+
platform: platform2,
|
|
238
|
+
minify: false
|
|
239
|
+
})
|
|
240
|
+
].filter(Boolean)
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async function createBundler(inputOptions, outputOptions) {
|
|
244
|
+
const bundler = await rollup(inputOptions);
|
|
245
|
+
return {
|
|
246
|
+
write: () => {
|
|
247
|
+
return bundler.write({
|
|
248
|
+
...outputOptions,
|
|
249
|
+
format: "esm",
|
|
250
|
+
entryFileNames: "[name].mjs",
|
|
251
|
+
chunkFileNames: "[name].mjs"
|
|
252
|
+
});
|
|
253
|
+
},
|
|
254
|
+
close: () => {
|
|
255
|
+
return bundler.close();
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export { createBundler, getInputOptions, libSqlFix, telemetryFix };
|