From fd9d8182a608399ff12cd8a226ad687157ed86b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6ssler?= Date: Thu, 3 Oct 2024 10:19:05 +0200 Subject: [PATCH] Add dual cjs / esm support to path test --- library/agent/hooks/wrapDefaultOrNamed.ts | 5 +++-- library/agent/hooks/wrapExport.ts | 3 ++- library/helpers/wrap.ts | 9 +++++++-- library/sinks/Path.test.ts | 8 +++++--- library/sinks/Path.ts | 15 +++++++++++---- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/library/agent/hooks/wrapDefaultOrNamed.ts b/library/agent/hooks/wrapDefaultOrNamed.ts index 3c6c932b4..dd52030d5 100644 --- a/library/agent/hooks/wrapDefaultOrNamed.ts +++ b/library/agent/hooks/wrapDefaultOrNamed.ts @@ -7,10 +7,11 @@ import { createWrappedFunction, wrap } from "../../helpers/wrap"; export function wrapDefaultOrNamed( module: any, name: string | undefined, - wrapper: (original: Function) => Function + wrapper: (original: Function) => Function, + isESMImport = false ) { if (typeof name === "undefined") { return createWrappedFunction(module, wrapper); } - return wrap(module, name, wrapper); + return wrap(module, name, wrapper, isESMImport); } diff --git a/library/agent/hooks/wrapExport.ts b/library/agent/hooks/wrapExport.ts index f39824363..6489ba23e 100644 --- a/library/agent/hooks/wrapExport.ts +++ b/library/agent/hooks/wrapExport.ts @@ -113,7 +113,8 @@ export function wrapExport( return returnVal; }; - } + }, + pkgInfo.isESMImport ); } catch (error) { agent.onFailedToWrapMethod(pkgInfo.name, methodName); diff --git a/library/helpers/wrap.ts b/library/helpers/wrap.ts index c27fc3178..b5b015968 100644 --- a/library/helpers/wrap.ts +++ b/library/helpers/wrap.ts @@ -5,7 +5,8 @@ type WrappedFunction = T & { export function wrap( module: any, name: string, - wrapper: (original: Function) => Function + wrapper: (original: Function) => Function, + isESMImport = false ) { if (!module[name]) { throw new Error(`no original function ${name} to wrap`); @@ -20,7 +21,11 @@ export function wrap( const original = module[name]; const wrapped = createWrappedFunction(original, wrapper); - defineProperty(module, name, wrapped); + if (!isESMImport) { + defineProperty(module, name, wrapped); + } else { + module[name] = wrapped; + } return wrapped; } diff --git a/library/sinks/Path.test.ts b/library/sinks/Path.test.ts index a104df230..44fd6c355 100644 --- a/library/sinks/Path.test.ts +++ b/library/sinks/Path.test.ts @@ -1,9 +1,10 @@ -import * as t from "tap"; +import t from "tap"; import { Agent } from "../agent/Agent"; import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting"; import { Context, runWithContext } from "../agent/Context"; import { LoggerNoop } from "../agent/logger/LoggerNoop"; import { Path } from "./Path"; +import { isCJS } from "../helpers/isCJS"; const unsafeContext: Context = { remoteAddress: "::1", @@ -33,12 +34,13 @@ t.test("it works", async (t) => { new LoggerNoop(), new ReportingAPIForTesting(), undefined, - undefined + undefined, + !isCJS() ); agent.start([new Path()]); - const { join, resolve } = require("path"); + const { join, resolve } = isCJS() ? require("path") : await import("path"); function safeCalls() { t.same(join("test.txt"), "test.txt"); diff --git a/library/sinks/Path.ts b/library/sinks/Path.ts index c5a4b5148..1846bd17c 100644 --- a/library/sinks/Path.ts +++ b/library/sinks/Path.ts @@ -36,11 +36,18 @@ export class Path implements Wrapper { wrap(hooks: Hooks): void { const functions = ["join", "resolve", "normalize"]; + // Todo after merging main: check path/posix and path/win32 hooks.addBuiltinModule("path").onRequire((exports, pkgInfo) => { - for (const func of functions) { - wrapExport(exports, func, pkgInfo, { - inspectArgs: (args) => this.inspectPath(args, func), - }); + const wrapParts = pkgInfo.isESMImport + ? [exports, exports.default] + : [exports.posix, exports.win32]; + + for (const toWrap of wrapParts) { + for (const func of functions) { + wrapExport(toWrap, func, pkgInfo, { + inspectArgs: (args) => this.inspectPath(args, func), + }); + } } }); }