Skip to content

Commit fd9d818

Browse files
committed
Add dual cjs / esm support to path test
1 parent cc43622 commit fd9d818

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

library/agent/hooks/wrapDefaultOrNamed.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { createWrappedFunction, wrap } from "../../helpers/wrap";
77
export function wrapDefaultOrNamed(
88
module: any,
99
name: string | undefined,
10-
wrapper: (original: Function) => Function
10+
wrapper: (original: Function) => Function,
11+
isESMImport = false
1112
) {
1213
if (typeof name === "undefined") {
1314
return createWrappedFunction(module, wrapper);
1415
}
15-
return wrap(module, name, wrapper);
16+
return wrap(module, name, wrapper, isESMImport);
1617
}

library/agent/hooks/wrapExport.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export function wrapExport(
113113

114114
return returnVal;
115115
};
116-
}
116+
},
117+
pkgInfo.isESMImport
117118
);
118119
} catch (error) {
119120
agent.onFailedToWrapMethod(pkgInfo.name, methodName);

library/helpers/wrap.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ type WrappedFunction<T> = T & {
55
export function wrap(
66
module: any,
77
name: string,
8-
wrapper: (original: Function) => Function
8+
wrapper: (original: Function) => Function,
9+
isESMImport = false
910
) {
1011
if (!module[name]) {
1112
throw new Error(`no original function ${name} to wrap`);
@@ -20,7 +21,11 @@ export function wrap(
2021
const original = module[name];
2122
const wrapped = createWrappedFunction(original, wrapper);
2223

23-
defineProperty(module, name, wrapped);
24+
if (!isESMImport) {
25+
defineProperty(module, name, wrapped);
26+
} else {
27+
module[name] = wrapped;
28+
}
2429

2530
return wrapped;
2631
}

library/sinks/Path.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import * as t from "tap";
1+
import t from "tap";
22
import { Agent } from "../agent/Agent";
33
import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting";
44
import { Context, runWithContext } from "../agent/Context";
55
import { LoggerNoop } from "../agent/logger/LoggerNoop";
66
import { Path } from "./Path";
7+
import { isCJS } from "../helpers/isCJS";
78

89
const unsafeContext: Context = {
910
remoteAddress: "::1",
@@ -33,12 +34,13 @@ t.test("it works", async (t) => {
3334
new LoggerNoop(),
3435
new ReportingAPIForTesting(),
3536
undefined,
36-
undefined
37+
undefined,
38+
!isCJS()
3739
);
3840

3941
agent.start([new Path()]);
4042

41-
const { join, resolve } = require("path");
43+
const { join, resolve } = isCJS() ? require("path") : await import("path");
4244

4345
function safeCalls() {
4446
t.same(join("test.txt"), "test.txt");

library/sinks/Path.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ export class Path implements Wrapper {
3636
wrap(hooks: Hooks): void {
3737
const functions = ["join", "resolve", "normalize"];
3838

39+
// Todo after merging main: check path/posix and path/win32
3940
hooks.addBuiltinModule("path").onRequire((exports, pkgInfo) => {
40-
for (const func of functions) {
41-
wrapExport(exports, func, pkgInfo, {
42-
inspectArgs: (args) => this.inspectPath(args, func),
43-
});
41+
const wrapParts = pkgInfo.isESMImport
42+
? [exports, exports.default]
43+
: [exports.posix, exports.win32];
44+
45+
for (const toWrap of wrapParts) {
46+
for (const func of functions) {
47+
wrapExport(toWrap, func, pkgInfo, {
48+
inspectArgs: (args) => this.inspectPath(args, func),
49+
});
50+
}
4451
}
4552
});
4653
}

0 commit comments

Comments
 (0)