From f1e9456016410530875f56d737c4e9c0f44a7ee0 Mon Sep 17 00:00:00 2001 From: Jeff Haynie Date: Thu, 14 Aug 2025 09:28:05 -0700 Subject: [PATCH] Add node-fetch patch for isAbortSignal --- internal/bundler/node-fetch.go | 17 +++++++++++++++++ internal/bundler/patch.go | 29 +++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 internal/bundler/node-fetch.go diff --git a/internal/bundler/node-fetch.go b/internal/bundler/node-fetch.go new file mode 100644 index 00000000..45a62d37 --- /dev/null +++ b/internal/bundler/node-fetch.go @@ -0,0 +1,17 @@ +package bundler + +func init() { + patches["node-fetch"] = patchModule{ + Module: "node-fetch", + Filename: "src/utils/is", + Functions: map[string]patchAction{ + "isAbortSignal": { + After: `if (result) { return true; } + if (_args[0] && _args[0].constructor.name === 'AbortSignal') { + return true; + } + `, + }, + }, + } +} diff --git a/internal/bundler/patch.go b/internal/bundler/patch.go index d5386d7c..d25f4aa4 100644 --- a/internal/bundler/patch.go +++ b/internal/bundler/patch.go @@ -101,11 +101,18 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug } contents := string(buf) var suffix strings.Builder + isJS := strings.HasSuffix(args.Path, ".js") for fn, mod := range mod.Functions { fnname := "function " + fn index := strings.Index(contents, fnname) + var isConstVariable bool if index == -1 { - continue + fnname = "const " + fn + " = " + index = strings.Index(contents, fnname) + isConstVariable = true + if index == -1 { + continue + } } eol := searchBackwards(contents, index, '\n') if eol < 0 { @@ -113,14 +120,28 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug } prefix := strings.TrimSpace(contents[eol+1 : index]) isAsync := strings.Contains(prefix, "async") + isExport := strings.Contains(prefix, "export") newname := "__agentuity_" + fn - newfnname := "function " + newname + var newfnname string + if isConstVariable { + newfnname = "const " + newname + " = " + } else { + newfnname = "function " + newname + } var fnprefix string if isAsync { fnprefix = "async " } + if isExport { + fnprefix += "export " + fnprefix + } contents = strings.Replace(contents, fnname, newfnname, 1) - suffix.WriteString(fnprefix + fnname + "(...args) {\n") + if isJS { + suffix.WriteString(fnprefix + fnname + "() => {\n") + suffix.WriteString("let args = arguments;\n") + } else { + suffix.WriteString(fnprefix + fnname + "(...args) {\n") + } suffix.WriteString("\tlet _args = args;\n") if mod.Before != "" { suffix.WriteString(mod.Before) @@ -150,7 +171,7 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug } } loader := api.LoaderJS - if strings.HasSuffix(args.Path, ".ts") { + if !isJS { loader = api.LoaderTS } return api.OnLoadResult{