Skip to content

Commit e258bdf

Browse files
committed
Support FunctionExpression
1 parent d734ed2 commit e258bdf

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

instrumentation-wasm/src/js_transformer/transformer_impl.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,14 @@ impl<'a> Traverse<'a, TraverseState> for Transformer<'a> {
132132
node: &mut oxc_ast::ast::Function<'a>,
133133
_ctx: &mut TraverseCtx<'a, TraverseState>,
134134
) {
135-
if node.r#type != FunctionType::FunctionDeclaration {
136-
// Only instrument function declarations here
137-
return;
138-
}
135+
let node_type = match node.r#type {
136+
FunctionType::FunctionDeclaration => "FunctionDeclaration",
137+
FunctionType::FunctionExpression => "FunctionExpression",
138+
_ => {
139+
// Ignore unknown function types
140+
return;
141+
}
142+
};
139143

140144
if node.id.is_none() || node.body.is_none() {
141145
// No identifier or body, nothing to instrument
@@ -150,7 +154,7 @@ impl<'a> Traverse<'a, TraverseState> for Transformer<'a> {
150154
.file_instructions
151155
.functions
152156
.iter()
153-
.find(|f| f.node_type == "FunctionDeclaration" && f.name == function_name);
157+
.find(|f| f.node_type == node_type && f.name == function_name);
154158

155159
if matching_instruction.is_none() {
156160
return;

library/agent/hooks/instrumentation/codeTransformation.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,3 +1568,57 @@ t.test(
15681568
);
15691569
}
15701570
);
1571+
1572+
t.test("Modify function expression (ESM)", async (t) => {
1573+
const result = transformCode(
1574+
"testpkg",
1575+
"1.0.0",
1576+
"application.js",
1577+
`
1578+
const x = 1;
1579+
1580+
const y = function test123(arg1, arg2 = "default") {
1581+
console.log("test123");
1582+
1583+
return "abc";
1584+
}
1585+
1586+
function test123() {
1587+
console.log("ignore");
1588+
}
1589+
`,
1590+
"module",
1591+
{
1592+
path: "application.js",
1593+
versionRange: "^1.0.0",
1594+
functions: [
1595+
{
1596+
nodeType: "FunctionExpression",
1597+
name: "test123",
1598+
identifier:
1599+
"testpkg.application.js.test123.FunctionExpression.v1.0.0",
1600+
inspectArgs: true,
1601+
modifyArgs: false,
1602+
modifyReturnValue: true,
1603+
modifyArgumentsObject: false,
1604+
},
1605+
],
1606+
}
1607+
);
1608+
1609+
isSameCode(
1610+
result,
1611+
`import { __instrumentInspectArgs, __instrumentModifyReturnValue } from "@aikidosec/firewall/instrument/internals";
1612+
const x = 1;
1613+
1614+
const y = function test123(arg1, arg2 = "default") {
1615+
__instrumentInspectArgs("testpkg.application.js.test123.FunctionExpression.v1.0.0", arguments, "1.0.0", this);
1616+
console.log("test123");
1617+
return __instrumentModifyReturnValue("testpkg.application.js.test123.FunctionExpression.v1.0.0", arguments, "abc", this);
1618+
};
1619+
1620+
function test123() {
1621+
console.log("ignore");
1622+
}`
1623+
);
1624+
});

library/agent/hooks/instrumentation/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ export type IntereptorCallbackInfoObj = {
6868
};
6969

7070
export type PackageFunctionInstrumentationInstruction = {
71-
nodeType: "MethodDefinition" | "FunctionAssignment" | "FunctionDeclaration";
71+
nodeType:
72+
| "MethodDefinition"
73+
| "FunctionAssignment"
74+
| "FunctionDeclaration"
75+
| "FunctionExpression";
7276
name: string;
7377
/**
7478
* Used for stats reporting to core, e.g. sql_op

0 commit comments

Comments
 (0)