fix: nodejs wrapper передаёт require в new Function (v1.3.95)

This commit is contained in:
“Naeel”
2026-05-19 16:48:19 +04:00
parent 27dbe3bccb
commit fe8870af71
5 changed files with 20 additions and 16 deletions
+5 -3
View File
@@ -23,10 +23,12 @@ func BuildJSDeployZip(code string) ([]byte, error) {
// main.js — CommonJS wrapper:
// 1. new Function создаёт функцию в пустом модульном контексте (нет import/export)
// 2. Передаём ей module и exports как параметры → пользовательский CJS код работает
// 3. Экспортируем main/handler/default через module.exports
// 2. Передаём ей module, exports и require как параметры → пользовательский CJS код работает
// 3. require передаётся явно, т.к. new Function выполняется в глобальном scope
// и не имеет доступа к module-local require из CJS контекста main.js
// 4. Экспортируем main/handler/default через module.exports
wrapper := fmt.Sprintf(`const __mod = { exports: {} };
(new Function('module', 'exports', %s))(__mod, __mod.exports);
(new Function('module', 'exports', 'require', %s))(__mod, __mod.exports, require);
const _fn = __mod.exports;
async function __invoke(ctx) {
+11 -9
View File
@@ -33,18 +33,20 @@ func TestBuildJSDeployZipExportsMainAndHandler(t *testing.T) {
files[file.Name] = string(content)
}
if files["package.json"] != `{"type":"module"}` {
t.Fatalf("package.json = %q, want ESM marker", files["package.json"])
}
mainJS := files["main.js"]
if !strings.Contains(mainJS, `export { __invoke as main, __invoke as handler };`) {
t.Fatalf("main.js does not export both main and handler: %s", mainJS)
if mainJS == "" {
t.Fatalf("main.js not found in zip, files: %v", files)
}
if !strings.Contains(mainJS, `export default __invoke;`) {
t.Fatalf("main.js does not export default invoke: %s", mainJS)
if !strings.Contains(mainJS, `new Function('module', 'exports', 'require',`) {
t.Fatalf("main.js does not pass require to new Function: %s", mainJS)
}
if !strings.Contains(mainJS, `__mod.exports, require)`) {
t.Fatalf("main.js does not forward require: %s", mainJS)
}
if !strings.Contains(mainJS, `module.exports = __invoke`) {
t.Fatalf("main.js does not export __invoke via module.exports: %s", mainJS)
}
if !strings.Contains(mainJS, `_fn.default || _fn.handler || _fn.main`) {
t.Fatalf("main.js lost user export resolution: %s", mainJS)
}
}
}