Skip to content

Commit 8a72e43

Browse files
committed
Fix wasm-opt, allow pkg name rewrite
1 parent ec12605 commit 8a72e43

File tree

6 files changed

+84
-66
lines changed

6 files changed

+84
-66
lines changed

instrumentation-wasm/Cargo.lock

Lines changed: 49 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

instrumentation-wasm/Cargo.toml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ name = "node_code_instrumentation"
1010
crate-type = ["cdylib", "rlib"]
1111

1212
[dependencies]
13-
oxc_allocator = "0.68.1"
14-
oxc_ast = "0.68.1"
15-
oxc_codegen = "0.68.1"
16-
oxc_parser = "0.68.1"
17-
oxc_semantic = "0.68.1"
18-
oxc_span = "0.68.1"
19-
oxc_traverse = "0.68.1"
13+
oxc_allocator = "0.72.0"
14+
oxc_ast = "0.72.0"
15+
oxc_codegen = "0.72.0"
16+
oxc_parser = "0.72.0"
17+
oxc_semantic = "0.72.0"
18+
oxc_span = "0.72.0"
19+
oxc_traverse = "0.72.0"
2020
serde = "1.0.219"
2121
serde_json = "1.0.140"
2222
wasm-bindgen = "0.2.100"
@@ -25,3 +25,6 @@ wasm-bindgen = "0.2.100"
2525
strip = true
2626
opt-level = "s"
2727
lto = true
28+
29+
[package.metadata.wasm-pack.profile.release]
30+
wasm-opt = ["-O", "--enable-bulk-memory"]

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,7 @@ t.test("typescript code in a js file", async (t) => {
287287
t.ok(error instanceof Error);
288288
if (error instanceof Error) {
289289
t.match(error.message, "Error transforming code: #ERR:");
290-
t.match(
291-
error.message,
292-
"Expected a semicolon or an implicit semicolon after a statement, but found none"
293-
);
290+
t.match(error.message, 'Expected `;` but found `:`"');
294291
}
295292
}
296293
});

library/agent/hooks/instrumentation/codeTransformation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PackageFileInstrumentationInstructionJSON } from "./types";
33
import { wasm_transform_code_str } from "./wasm/node_code_instrumentation";
44
import { getSourceType, PackageLoadFormat } from "./getSourceType";
55
import { envToBool } from "../../../helpers/envToBool";
6+
import { join } from "path";
67

78
export function transformCode(
89
pkgName: string,
@@ -28,7 +29,7 @@ export function transformCode(
2829
if (envToBool(process.env.AIKIDO_TEST_NEW_INSTRUMENTATION)) {
2930
return result.replace(
3031
"@aikidosec/firewall/instrument/internals",
31-
"../../../../agent/hooks/instrumentation/injectedFunctions.ts"
32+
join(__dirname, "injectedFunctions.ts")
3233
);
3334
}
3435

library/agent/hooks/instrumentation/instructions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ let builtinRequireInterceptors = new Map<string, RequireInterceptor[]>();
1717
// Identifier for the function is: packageName.relativePath.functionName.nodeType.matchingVersion
1818
let packageCallbackInfo = new Map<string, IntereptorCallbackInfoObj>();
1919

20+
// In order to support multiple versions of the same package in unit tests, we need to rewrite the package name
21+
// e.g. In our sources and sinks, we use the real package name `hooks.addPackage("undici")`
22+
// but in the tests we want to `require("undici-v6")` instead of `require("undici")`
23+
let __packageNamesToRewrite: Record<string, string> = {};
24+
2025
export function setPackagesToInstrument(_packages: Package[]) {
2126
// Clear the previous packages
2227
packages = new Map();
2328
packageCallbackInfo = new Map();
2429

2530
for (const pkg of _packages) {
31+
if (pkg.getName() in __packageNamesToRewrite) {
32+
// If the package name is in the rewrite map, we use the rewritten name
33+
pkg.setName(__packageNamesToRewrite[pkg.getName()]);
34+
}
35+
2636
const packageInstructions = pkg
2737
.getVersions()
2838
.map((versionedPackage) => {
@@ -125,3 +135,9 @@ export function getBuiltinInterceptors(name: string): RequireInterceptor[] {
125135
export function shouldPatchBuiltin(name: string): boolean {
126136
return builtinRequireInterceptors.has(name);
127137
}
138+
139+
export function __internalRewritePackageNamesForTesting(
140+
rewrite: Record<string, string>
141+
) {
142+
__packageNamesToRewrite = rewrite;
143+
}

0 commit comments

Comments
 (0)