veryfront 0.1.261 → 0.1.263
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/esm/cli/commands/knowledge/command.d.ts.map +1 -1
- package/esm/cli/commands/knowledge/command.js +19 -26
- package/esm/cli/templates/manifest.d.ts +470 -470
- package/esm/cli/templates/manifest.js +519 -519
- package/esm/deno.js +1 -1
- package/esm/src/agent/ag-ui-detached-start.d.ts +1 -4
- package/esm/src/agent/ag-ui-detached-start.d.ts.map +1 -1
- package/esm/src/agent/ag-ui-detached-start.js +4 -67
- package/esm/src/agent/ag-ui-handler.d.ts +1 -4
- package/esm/src/agent/ag-ui-handler.d.ts.map +1 -1
- package/esm/src/agent/ag-ui-handler.js +3 -61
- package/esm/src/agent/ag-ui-host-support.d.ts.map +1 -1
- package/esm/src/agent/ag-ui-host-support.js +2 -21
- package/esm/src/agent/ag-ui-request-shared.d.ts +4 -0
- package/esm/src/agent/ag-ui-request-shared.d.ts.map +1 -0
- package/esm/src/agent/ag-ui-request-shared.js +47 -0
- package/esm/src/agent/ag-ui-run-control.d.ts.map +1 -1
- package/esm/src/agent/ag-ui-run-control.js +1 -23
- package/esm/src/agent/ag-ui-runtime-handler.d.ts +1 -5
- package/esm/src/agent/ag-ui-runtime-handler.d.ts.map +1 -1
- package/esm/src/agent/ag-ui-runtime-handler.js +3 -67
- package/esm/src/agent/ag-ui-tool-shared.d.ts +15 -0
- package/esm/src/agent/ag-ui-tool-shared.d.ts.map +1 -0
- package/esm/src/agent/ag-ui-tool-shared.js +47 -0
- package/esm/src/agent/conversation-run-event-preparation.d.ts +3 -1
- package/esm/src/agent/conversation-run-event-preparation.d.ts.map +1 -1
- package/esm/src/agent/conversation-run-event-preparation.js +40 -0
- package/esm/src/agent/index.d.ts +3 -1
- package/esm/src/agent/index.d.ts.map +1 -1
- package/esm/src/agent/index.js +2 -1
- package/esm/src/agent/runtime-ag-ui-contract.d.ts.map +1 -1
- package/esm/src/agent/runtime-ag-ui-contract.js +2 -21
- package/esm/src/chat/chat-ui-message-helpers.d.ts +20 -0
- package/esm/src/chat/chat-ui-message-helpers.d.ts.map +1 -0
- package/esm/src/chat/chat-ui-message-helpers.js +167 -0
- package/esm/src/chat/index.d.ts +2 -0
- package/esm/src/chat/index.d.ts.map +1 -1
- package/esm/src/chat/index.js +1 -0
- package/esm/src/chat/protocol.d.ts +109 -0
- package/esm/src/chat/protocol.d.ts.map +1 -1
- package/esm/src/provider/runtime-loader/provider-http.d.ts +47 -0
- package/esm/src/provider/runtime-loader/provider-http.d.ts.map +1 -0
- package/esm/src/provider/runtime-loader/provider-http.js +171 -0
- package/esm/src/provider/runtime-loader/provider-records.d.ts +2 -0
- package/esm/src/provider/runtime-loader/provider-records.d.ts.map +1 -0
- package/esm/src/provider/runtime-loader/provider-records.js +6 -0
- package/esm/src/provider/runtime-loader.d.ts +2 -32
- package/esm/src/provider/runtime-loader.d.ts.map +1 -1
- package/esm/src/provider/runtime-loader.js +3 -176
- package/esm/src/routing/api/module-loader/external-import-rewriter.d.ts +27 -0
- package/esm/src/routing/api/module-loader/external-import-rewriter.d.ts.map +1 -0
- package/esm/src/routing/api/module-loader/external-import-rewriter.js +339 -0
- package/esm/src/routing/api/module-loader/loader.d.ts +1 -22
- package/esm/src/routing/api/module-loader/loader.d.ts.map +1 -1
- package/esm/src/routing/api/module-loader/loader.js +4 -336
- package/esm/src/server/dev-ui/manifest.d.ts +17 -17
- package/esm/src/server/dev-ui/manifest.js +17 -17
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/package.json +1 -1
- package/src/cli/commands/knowledge/command.ts +27 -32
- package/src/cli/templates/manifest.js +519 -519
- package/src/deno.js +1 -1
- package/src/src/agent/ag-ui-detached-start.ts +4 -92
- package/src/src/agent/ag-ui-handler.ts +3 -81
- package/src/src/agent/ag-ui-host-support.ts +5 -28
- package/src/src/agent/ag-ui-request-shared.ts +62 -0
- package/src/src/agent/ag-ui-run-control.ts +1 -27
- package/src/src/agent/ag-ui-runtime-handler.ts +3 -86
- package/src/src/agent/ag-ui-tool-shared.ts +77 -0
- package/src/src/agent/conversation-run-event-preparation.ts +57 -1
- package/src/src/agent/index.ts +19 -0
- package/src/src/agent/runtime-ag-ui-contract.ts +5 -28
- package/src/src/chat/chat-ui-message-helpers.ts +232 -0
- package/src/src/chat/index.ts +19 -0
- package/src/src/chat/protocol.ts +148 -0
- package/src/src/provider/runtime-loader/provider-http.ts +207 -0
- package/src/src/provider/runtime-loader/provider-records.ts +7 -0
- package/src/src/provider/runtime-loader.ts +10 -214
- package/src/src/routing/api/module-loader/external-import-rewriter.ts +461 -0
- package/src/src/routing/api/module-loader/loader.ts +19 -462
- package/src/src/server/dev-ui/manifest.js +17 -17
- package/src/src/utils/version-constant.ts +1 -1
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { isCompiledBinary, serverLogger } from "../../../utils/index.js";
|
|
2
|
+
import * as pathHelper from "../../../platform/compat/path/index.js";
|
|
3
|
+
import { isDeno, isNode } from "../../../platform/compat/runtime.js";
|
|
4
|
+
import { rewriteNpmImports } from "../../../transforms/npm-import-rewrites.js";
|
|
5
|
+
import { resolveExportEntry, toCjsDestructureBindings } from "./loader-helpers.js";
|
|
6
|
+
const logger = serverLogger.component("api");
|
|
7
|
+
/** Node.js built-in module names — shared across the CJS shim, esbuild externals, and Deno rewrites. */
|
|
8
|
+
export const NODE_BUILTINS = [
|
|
9
|
+
"assert",
|
|
10
|
+
"buffer",
|
|
11
|
+
"child_process",
|
|
12
|
+
"cluster",
|
|
13
|
+
"console",
|
|
14
|
+
"constants",
|
|
15
|
+
"crypto",
|
|
16
|
+
"dgram",
|
|
17
|
+
"dns",
|
|
18
|
+
"events",
|
|
19
|
+
"fs",
|
|
20
|
+
"http",
|
|
21
|
+
"http2",
|
|
22
|
+
"https",
|
|
23
|
+
"module",
|
|
24
|
+
"net",
|
|
25
|
+
"os",
|
|
26
|
+
"path",
|
|
27
|
+
"perf_hooks",
|
|
28
|
+
"process",
|
|
29
|
+
"querystring",
|
|
30
|
+
"readline",
|
|
31
|
+
"stream",
|
|
32
|
+
"string_decoder",
|
|
33
|
+
"timers",
|
|
34
|
+
"tls",
|
|
35
|
+
"tty",
|
|
36
|
+
"url",
|
|
37
|
+
"util",
|
|
38
|
+
"v8",
|
|
39
|
+
"vm",
|
|
40
|
+
"worker_threads",
|
|
41
|
+
"zlib",
|
|
42
|
+
];
|
|
43
|
+
export async function readProjectDependencies(projectDir, fs) {
|
|
44
|
+
try {
|
|
45
|
+
const content = await fs.readTextFile(pathHelper.join(projectDir, "package.json"));
|
|
46
|
+
const pkg = JSON.parse(content);
|
|
47
|
+
return new Map(Object.entries(pkg.dependencies ?? {}));
|
|
48
|
+
}
|
|
49
|
+
catch (_) {
|
|
50
|
+
/* expected: package.json may not exist */
|
|
51
|
+
return new Map();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Generates a CJS module loader shim for compiled Deno binaries.
|
|
56
|
+
*
|
|
57
|
+
* In compiled binaries, `createRequire()` can resolve module paths and load
|
|
58
|
+
* built-in modules (fs, path, etc.), but cannot load CJS files from disk
|
|
59
|
+
* (loadMaybeCjs fails with "path not found"). This shim works around that
|
|
60
|
+
* limitation by using `Deno.readTextFileSync` to read CJS files and
|
|
61
|
+
* `new Function` to evaluate them in a proper CJS wrapper with require,
|
|
62
|
+
* exports, module, __filename, and __dirname bindings.
|
|
63
|
+
*/
|
|
64
|
+
export function generateCompiledBinaryRequireShim(projectDir) {
|
|
65
|
+
const builtinSet = JSON.stringify(NODE_BUILTINS);
|
|
66
|
+
const safeProjectDir = JSON.stringify(projectDir + "/package.json");
|
|
67
|
+
const safeProjectRoot = JSON.stringify(pathHelper.resolve(projectDir));
|
|
68
|
+
return `
|
|
69
|
+
import { createRequire as __vf_createRequire } from "node:module";
|
|
70
|
+
import { dirname as __vf_dirname, resolve as __vf_resolve } from "node:path";
|
|
71
|
+
var __vf_builtinRequire = __vf_createRequire(${safeProjectDir});
|
|
72
|
+
var __vf_builtinSet = new Set(${builtinSet});
|
|
73
|
+
var __vf_projectRoot = ${safeProjectRoot};
|
|
74
|
+
// VULN-FS-5: Canonicalize the project root so containment checks using
|
|
75
|
+
// Deno.realPathSync(resolved) compare canonical-vs-canonical. Without this,
|
|
76
|
+
// when the project itself is opened via a symlink, the realpath'd resolved
|
|
77
|
+
// module path has a different prefix than the non-canonical projectRoot and
|
|
78
|
+
// legitimate dependencies would be rejected.
|
|
79
|
+
try { __vf_projectRoot = Deno.realPathSync(__vf_projectRoot); } catch (_) { /* expected: projectRoot may not exist at shim init in some environments */ }
|
|
80
|
+
var __vf_cache = Object.create(null);
|
|
81
|
+
function __vf_assertContained(resolved) {
|
|
82
|
+
var norm = __vf_resolve(resolved).replace(/\\\\/g, "/");
|
|
83
|
+
var root = __vf_projectRoot.replace(/\\\\/g, "/");
|
|
84
|
+
if (!norm.startsWith(root + "/") && norm !== root) {
|
|
85
|
+
throw new Error("CJS loader blocked path outside project: " + resolved);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function __vf_loadCjs(id, parentDir) {
|
|
89
|
+
if (id.startsWith("node:")) return __vf_builtinRequire(id);
|
|
90
|
+
if (__vf_builtinSet.has(id)) return __vf_builtinRequire(id);
|
|
91
|
+
var slashIdx = id.indexOf("/");
|
|
92
|
+
if (slashIdx > 0 && __vf_builtinSet.has(id.slice(0, slashIdx))) return __vf_builtinRequire(id);
|
|
93
|
+
var resolved;
|
|
94
|
+
if (id.startsWith(".") || id.startsWith("/")) {
|
|
95
|
+
resolved = __vf_resolve(parentDir, id);
|
|
96
|
+
if (!resolved.match(/\\.[a-zA-Z0-9]+$/)) {
|
|
97
|
+
var exts = [".js", ".cjs", ".json", "/index.js", "/index.cjs", "/index.json"];
|
|
98
|
+
for (var i = 0; i < exts.length; i++) {
|
|
99
|
+
try { Deno.statSync(resolved + exts[i]); resolved += exts[i]; break; } catch (_) { /* expected: probing file extensions */ }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
resolved = __vf_builtinRequire.resolve(id);
|
|
104
|
+
}
|
|
105
|
+
// VULN-FS-5: Always assert containment after resolution (both branches),
|
|
106
|
+
// then re-canonicalize via realPathSync to resist symlinked node_modules
|
|
107
|
+
// entries that could point outside the project root.
|
|
108
|
+
__vf_assertContained(resolved);
|
|
109
|
+
try {
|
|
110
|
+
var real = Deno.realPathSync(resolved);
|
|
111
|
+
__vf_assertContained(real);
|
|
112
|
+
resolved = real;
|
|
113
|
+
} catch (_) {
|
|
114
|
+
/* expected: realPathSync fails for non-existent paths — assertContained above already held */
|
|
115
|
+
}
|
|
116
|
+
if (resolved in __vf_cache) return __vf_cache[resolved];
|
|
117
|
+
var code = Deno.readTextFileSync(resolved);
|
|
118
|
+
if (resolved.endsWith(".json")) {
|
|
119
|
+
var json = JSON.parse(code);
|
|
120
|
+
__vf_cache[resolved] = json;
|
|
121
|
+
return json;
|
|
122
|
+
}
|
|
123
|
+
var mod = { exports: {} };
|
|
124
|
+
__vf_cache[resolved] = mod.exports;
|
|
125
|
+
var dir = __vf_dirname(resolved);
|
|
126
|
+
var childReq = function(childId) { return __vf_loadCjs(childId, dir); };
|
|
127
|
+
childReq.resolve = function(childId) {
|
|
128
|
+
if (childId.startsWith(".") || childId.startsWith("/")) return __vf_resolve(dir, childId);
|
|
129
|
+
return __vf_builtinRequire.resolve(childId);
|
|
130
|
+
};
|
|
131
|
+
childReq.ensure = function(mods, cb) { cb(); };
|
|
132
|
+
var fn = new Function("exports", "require", "module", "__filename", "__dirname", "global", "globalThis", "Worker", code);
|
|
133
|
+
fn(mod.exports, childReq, mod, resolved, dir, globalThis, globalThis, undefined);
|
|
134
|
+
__vf_cache[resolved] = mod.exports;
|
|
135
|
+
return mod.exports;
|
|
136
|
+
}
|
|
137
|
+
function __vf_interopDefault(m) { return m && m.__esModule && m.default !== undefined ? m.default : m; }
|
|
138
|
+
var require = function(id) { return __vf_loadCjs(id, ${JSON.stringify(projectDir)}); };
|
|
139
|
+
require.resolve = function(id) { return __vf_builtinRequire.resolve(id); };
|
|
140
|
+
require.ensure = function(mods, cb) { cb(); };
|
|
141
|
+
`.trim();
|
|
142
|
+
}
|
|
143
|
+
export function getNodeExternalPackagesToResolve(userDeps) {
|
|
144
|
+
const externalPackagesToResolve = ["zod"];
|
|
145
|
+
for (const name of userDeps.keys()) {
|
|
146
|
+
if (!externalPackagesToResolve.includes(name)) {
|
|
147
|
+
externalPackagesToResolve.push(name);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return externalPackagesToResolve;
|
|
151
|
+
}
|
|
152
|
+
export async function resolveNodePackageToFileUrl(projectDir, packageName, fs, pathToFileURL) {
|
|
153
|
+
const packagePath = pathHelper.join(projectDir, "node_modules", packageName);
|
|
154
|
+
const packageJsonPath = pathHelper.join(packagePath, "package.json");
|
|
155
|
+
try {
|
|
156
|
+
const pkgJson = JSON.parse(await fs.readTextFile(packageJsonPath));
|
|
157
|
+
let entryPoint;
|
|
158
|
+
if (pkgJson.exports) {
|
|
159
|
+
entryPoint = resolveExportEntry(pkgJson.exports["."]);
|
|
160
|
+
}
|
|
161
|
+
entryPoint ||= pkgJson.module || pkgJson.main || "index.js";
|
|
162
|
+
if (!entryPoint)
|
|
163
|
+
return null;
|
|
164
|
+
return pathToFileURL(pathHelper.join(packagePath, entryPoint)).href;
|
|
165
|
+
}
|
|
166
|
+
catch (_) {
|
|
167
|
+
/* expected: package.json may not exist or be invalid */
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
export async function loadVeryfrontExportsMap(projectDir, fs) {
|
|
172
|
+
const vfPackagePath = pathHelper.join(projectDir, "node_modules", "veryfront");
|
|
173
|
+
const vfPackageJsonPath = pathHelper.join(vfPackagePath, "package.json");
|
|
174
|
+
try {
|
|
175
|
+
const pkgJson = JSON.parse(await fs.readTextFile(vfPackageJsonPath));
|
|
176
|
+
return pkgJson.exports || {};
|
|
177
|
+
}
|
|
178
|
+
catch (_error) {
|
|
179
|
+
logger.debug("Could not read veryfront package.json");
|
|
180
|
+
return {};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
export async function rewriteNodeExternalImports(code, projectDir, fs, userDeps) {
|
|
184
|
+
const { pathToFileURL } = await import("node:url");
|
|
185
|
+
let transformed = code;
|
|
186
|
+
logger.debug(`Rewriting external imports for Node.js, projectDir: ${projectDir}`);
|
|
187
|
+
for (const pkg of getNodeExternalPackagesToResolve(userDeps)) {
|
|
188
|
+
const escapedPkg = pkg.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
189
|
+
const staticImportRegex = new RegExp(`from\\s*["']${escapedPkg}(/[^"']*)?["']`, "g");
|
|
190
|
+
const dynamicImportRegex = new RegExp(`import\\s*\\(\\s*["']${escapedPkg}(/[^"']*)?["']\\s*\\)`, "g");
|
|
191
|
+
const needsStatic = staticImportRegex.test(transformed);
|
|
192
|
+
staticImportRegex.lastIndex = 0;
|
|
193
|
+
const needsDynamic = dynamicImportRegex.test(transformed);
|
|
194
|
+
dynamicImportRegex.lastIndex = 0;
|
|
195
|
+
if (!needsStatic && !needsDynamic)
|
|
196
|
+
continue;
|
|
197
|
+
const packageDir = pathToFileURL(pathHelper.join(projectDir, "node_modules", pkg)).href;
|
|
198
|
+
const resolvedUrl = await resolveNodePackageToFileUrl(projectDir, pkg, fs, pathToFileURL);
|
|
199
|
+
if (needsStatic) {
|
|
200
|
+
transformed = transformed.replace(staticImportRegex, (_, subpath) => {
|
|
201
|
+
if (subpath) {
|
|
202
|
+
const subUrl = `${packageDir}${subpath}`;
|
|
203
|
+
logger.debug(`Resolved ${pkg}${subpath} -> ${subUrl}`);
|
|
204
|
+
return `from "${subUrl}"`;
|
|
205
|
+
}
|
|
206
|
+
if (!resolvedUrl)
|
|
207
|
+
return `from "${pkg}"`;
|
|
208
|
+
logger.debug(`Resolved ${pkg} -> ${resolvedUrl}`);
|
|
209
|
+
return `from "${resolvedUrl}"`;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
if (needsDynamic) {
|
|
213
|
+
transformed = transformed.replace(dynamicImportRegex, (_, subpath) => {
|
|
214
|
+
if (subpath) {
|
|
215
|
+
return `import("${packageDir}${subpath}")`;
|
|
216
|
+
}
|
|
217
|
+
if (!resolvedUrl)
|
|
218
|
+
return `import("${pkg}")`;
|
|
219
|
+
return `import("${resolvedUrl}")`;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const vfPackagePath = pathHelper.join(projectDir, "node_modules", "veryfront");
|
|
224
|
+
const exportsMap = await loadVeryfrontExportsMap(projectDir, fs);
|
|
225
|
+
transformed = transformed.replace(/from\s+["'](veryfront\/[^"']+)["']/g, (match, fullSpecifier) => {
|
|
226
|
+
const subpath = "./" + fullSpecifier.replace("veryfront/", "");
|
|
227
|
+
const exportEntry = exportsMap[subpath];
|
|
228
|
+
if (!exportEntry?.import) {
|
|
229
|
+
logger.warn(`No export found for ${subpath}`);
|
|
230
|
+
return match;
|
|
231
|
+
}
|
|
232
|
+
const resolvedPath = pathHelper.join(vfPackagePath, exportEntry.import);
|
|
233
|
+
logger.debug(`Resolved ${fullSpecifier} -> ${resolvedPath}`);
|
|
234
|
+
return `from "${pathToFileURL(resolvedPath).href}"`;
|
|
235
|
+
});
|
|
236
|
+
transformed = transformed.replace(/from\s+["']veryfront["']/g, () => {
|
|
237
|
+
const exportEntry = exportsMap["."];
|
|
238
|
+
if (!exportEntry?.import)
|
|
239
|
+
return 'from "veryfront"';
|
|
240
|
+
const resolvedPath = pathHelper.join(vfPackagePath, exportEntry.import);
|
|
241
|
+
logger.debug(`Resolved veryfront -> ${resolvedPath}`);
|
|
242
|
+
return `from "${pathToFileURL(resolvedPath).href}"`;
|
|
243
|
+
});
|
|
244
|
+
return transformed;
|
|
245
|
+
}
|
|
246
|
+
export function rewriteCompiledBinaryVeryfrontImports(code) {
|
|
247
|
+
let transformed = code;
|
|
248
|
+
transformed = transformed.replace(/from\s+["']veryfront["']/g, 'from "./_vf_runtime.mjs"');
|
|
249
|
+
transformed = transformed.replace(/import\s*\(\s*["']veryfront["']\s*\)/g, 'import("./_vf_runtime.mjs")');
|
|
250
|
+
transformed = transformed.replace(/from\s+["']veryfront\/([^"']+)["']/g, (_match, subpath) => `from "./_vf_${subpath.replace(/\//g, "_")}.mjs"`);
|
|
251
|
+
transformed = transformed.replace(/import\s*\(\s*["']veryfront\/([^"']+)["']\s*\)/g, (_match, subpath) => `import("./_vf_${subpath.replace(/\//g, "_")}.mjs")`);
|
|
252
|
+
return transformed;
|
|
253
|
+
}
|
|
254
|
+
export function rewriteCompiledBinaryUserDependencyImports(code, userDeps) {
|
|
255
|
+
let transformed = code;
|
|
256
|
+
for (const name of userDeps.keys()) {
|
|
257
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
258
|
+
transformed = transformed.replace(new RegExp(`import\\s+(\\w+)\\s+from\\s+["']${escaped}["']`, "g"), (_, localName) => `const ${localName} = __vf_interopDefault(require("${name}"))`);
|
|
259
|
+
transformed = transformed.replace(new RegExp(`import\\s+(\\{[^}]+\\})\\s+from\\s+["']${escaped}["']`, "g"), (_, bindings) => `const ${toCjsDestructureBindings(bindings)} = require("${name}")`);
|
|
260
|
+
transformed = transformed.replace(new RegExp(`import\\s+\\*\\s+as\\s+(\\w+)\\s+from\\s+["']${escaped}["']`, "g"), (_, localName) => `const ${localName} = require("${name}")`);
|
|
261
|
+
transformed = transformed.replace(new RegExp(`import\\s+(\\w+)\\s*,\\s*(\\{[^}]+\\})\\s+from\\s+["']${escaped}["']`, "g"), (_, defaultName, bindings) => {
|
|
262
|
+
const tmp = `__vf_tmp_${defaultName}`;
|
|
263
|
+
return `const ${tmp} = require("${name}"); const ${defaultName} = __vf_interopDefault(${tmp}); const ${toCjsDestructureBindings(bindings)} = ${tmp}`;
|
|
264
|
+
});
|
|
265
|
+
transformed = transformed.replace(new RegExp(`import\\s+(\\w+|\\*\\s+as\\s+\\w+|\\{[^}]+\\})\\s+from\\s+["']${escaped}(/[^"']+)["']`, "g"), (_, binding, subpath) => {
|
|
266
|
+
const trimmedBinding = String(binding).trim();
|
|
267
|
+
if (trimmedBinding.startsWith("{")) {
|
|
268
|
+
return `const ${toCjsDestructureBindings(trimmedBinding)} = require("${name}${subpath}")`;
|
|
269
|
+
}
|
|
270
|
+
const name_ = trimmedBinding.startsWith("*")
|
|
271
|
+
? trimmedBinding.replace(/\*\s+as\s+/, "")
|
|
272
|
+
: trimmedBinding;
|
|
273
|
+
return `const ${name_} = require("${name}${subpath}")`;
|
|
274
|
+
});
|
|
275
|
+
transformed = transformed.replace(new RegExp(`import\\s*\\(\\s*["']${escaped}(/[^"']*)?["']\\s*\\)`, "g"), (_, subpath) => `Promise.resolve(require("${name}${subpath || ""}"))`);
|
|
276
|
+
}
|
|
277
|
+
return transformed;
|
|
278
|
+
}
|
|
279
|
+
export async function rewriteDenoNpmDependencyImports(code, projectDir, fs, userDeps) {
|
|
280
|
+
let transformed = code;
|
|
281
|
+
for (const [name, version] of userDeps) {
|
|
282
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
283
|
+
let resolvedVersion = version;
|
|
284
|
+
try {
|
|
285
|
+
const pkgPath = pathHelper.join(projectDir, "node_modules", name, "package.json");
|
|
286
|
+
const pkgContent = await fs.readTextFile(pkgPath);
|
|
287
|
+
const pkg = JSON.parse(pkgContent);
|
|
288
|
+
if (pkg.version)
|
|
289
|
+
resolvedVersion = pkg.version;
|
|
290
|
+
}
|
|
291
|
+
catch (_) {
|
|
292
|
+
/* expected: installed package.json may not exist, fall back to declared range */
|
|
293
|
+
}
|
|
294
|
+
transformed = transformed.replace(new RegExp(`from\\s+["']${escaped}(/[^"']*)?["']`, "g"), (_, subpath) => `from "npm:${name}@${resolvedVersion}${subpath || ""}"`);
|
|
295
|
+
transformed = transformed.replace(new RegExp(`import\\s*\\(\\s*["']${escaped}(/[^"']*)?["']\\s*\\)`, "g"), (_, subpath) => `import("npm:${name}@${resolvedVersion}${subpath || ""}")`);
|
|
296
|
+
}
|
|
297
|
+
return transformed;
|
|
298
|
+
}
|
|
299
|
+
export function rewriteDenoNodeBuiltinImports(code) {
|
|
300
|
+
let transformed = code;
|
|
301
|
+
for (const mod of NODE_BUILTINS) {
|
|
302
|
+
const escaped = mod.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
303
|
+
transformed = transformed.replace(new RegExp(`from\\s+["']${escaped}["']`, "g"), `from "node:${mod}"`);
|
|
304
|
+
transformed = transformed.replace(new RegExp(`import\\s*\\(\\s*["']${escaped}["']\\s*\\)`, "g"), `import("node:${mod}")`);
|
|
305
|
+
}
|
|
306
|
+
return transformed;
|
|
307
|
+
}
|
|
308
|
+
export async function rewriteExternalImports(code, projectDir, fs, userDeps = new Map()) {
|
|
309
|
+
let transformed = code;
|
|
310
|
+
if (isNode) {
|
|
311
|
+
try {
|
|
312
|
+
transformed = await rewriteNodeExternalImports(transformed, projectDir, fs, userDeps);
|
|
313
|
+
}
|
|
314
|
+
catch (e) {
|
|
315
|
+
logger.warn(`Failed to import node:module: ${e}`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (isDeno) {
|
|
319
|
+
transformed = rewriteNpmImports(transformed);
|
|
320
|
+
transformed = rewriteDenoNodeBuiltinImports(transformed);
|
|
321
|
+
// Rewrite user-installed npm dependencies.
|
|
322
|
+
// In non-compiled Deno: use npm: specifiers (resolved by Deno's npm support).
|
|
323
|
+
// In compiled binaries: use the createRequire-based `require` shim (already
|
|
324
|
+
// injected by the esbuild banner) to load CJS packages from node_modules,
|
|
325
|
+
// since npm: specifiers only work for packages embedded at compile time.
|
|
326
|
+
if (isCompiledBinary()) {
|
|
327
|
+
transformed = rewriteCompiledBinaryUserDependencyImports(transformed, userDeps);
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
transformed = await rewriteDenoNpmDependencyImports(transformed, projectDir, fs, userDeps);
|
|
331
|
+
}
|
|
332
|
+
// In compiled binaries, "veryfront" resolves to embedded source that can't be
|
|
333
|
+
// imported from external temp files. Rewrite to use local runtime shims.
|
|
334
|
+
if (isCompiledBinary()) {
|
|
335
|
+
transformed = rewriteCompiledBinaryVeryfrontImports(transformed);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return transformed;
|
|
339
|
+
}
|
|
@@ -1,26 +1,5 @@
|
|
|
1
1
|
import type { APIRoute, LoadModuleOptions } from "./types.js";
|
|
2
|
-
|
|
2
|
+
export { generateCompiledBinaryRequireShim, getNodeExternalPackagesToResolve, loadVeryfrontExportsMap, resolveNodePackageToFileUrl, rewriteCompiledBinaryUserDependencyImports, rewriteCompiledBinaryVeryfrontImports, rewriteDenoNodeBuiltinImports, rewriteDenoNpmDependencyImports, rewriteNodeExternalImports, } from "./external-import-rewriter.js";
|
|
3
3
|
export { toCjsDestructureBindings } from "./loader-helpers.js";
|
|
4
|
-
/**
|
|
5
|
-
* Generates a CJS module loader shim for compiled Deno binaries.
|
|
6
|
-
*
|
|
7
|
-
* In compiled binaries, `createRequire()` can resolve module paths and load
|
|
8
|
-
* built-in modules (fs, path, etc.), but cannot load CJS files from disk
|
|
9
|
-
* (loadMaybeCjs fails with "path not found"). This shim works around that
|
|
10
|
-
* limitation by using `Deno.readTextFileSync` to read CJS files and
|
|
11
|
-
* `new Function` to evaluate them in a proper CJS wrapper with require,
|
|
12
|
-
* exports, module, __filename, and __dirname bindings.
|
|
13
|
-
*/
|
|
14
|
-
export declare function generateCompiledBinaryRequireShim(projectDir: string): string;
|
|
15
4
|
export declare function loadHandlerModule(options: LoadModuleOptions): Promise<APIRoute | null>;
|
|
16
|
-
export declare function getNodeExternalPackagesToResolve(userDeps: Map<string, string>): string[];
|
|
17
|
-
export declare function resolveNodePackageToFileUrl(projectDir: string, packageName: string, fs: FileSystem, pathToFileURL: typeof import("node:url").pathToFileURL): Promise<string | null>;
|
|
18
|
-
export declare function loadVeryfrontExportsMap(projectDir: string, fs: FileSystem): Promise<Record<string, {
|
|
19
|
-
import?: string;
|
|
20
|
-
}>>;
|
|
21
|
-
export declare function rewriteNodeExternalImports(code: string, projectDir: string, fs: FileSystem, userDeps: Map<string, string>): Promise<string>;
|
|
22
|
-
export declare function rewriteCompiledBinaryVeryfrontImports(code: string): string;
|
|
23
|
-
export declare function rewriteCompiledBinaryUserDependencyImports(code: string, userDeps: Map<string, string>): string;
|
|
24
|
-
export declare function rewriteDenoNpmDependencyImports(code: string, projectDir: string, fs: FileSystem, userDeps: Map<string, string>): Promise<string>;
|
|
25
|
-
export declare function rewriteDenoNodeBuiltinImports(code: string): string;
|
|
26
5
|
//# sourceMappingURL=loader.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../../../src/src/routing/api/module-loader/loader.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../../../src/src/routing/api/module-loader/loader.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAkB9D,OAAO,EACL,iCAAiC,EACjC,gCAAgC,EAChC,uBAAuB,EACvB,2BAA2B,EAC3B,0CAA0C,EAC1C,qCAAqC,EACrC,6BAA6B,EAC7B,+BAA+B,EAC/B,0BAA0B,GAC3B,MAAM,+BAA+B,CAAC;AAIvC,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAE/D,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAyBtF"}
|