From deff75ee52d27ea9c7c97eb0489efb48f03f2e0b Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 15 Sep 2025 13:14:23 +0200 Subject: [PATCH 1/2] feat: Load wasm lazily --- .gitignore | 4 +++- index.ts | 31 +++++++++++++++++++++++++++++++ package.json | 10 +++++++--- yarn.lock | 12 ++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 index.ts diff --git a/.gitignore b/.gitignore index 56fbe1c..568cc28 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ target tests/*/instrumented.* pkg/ node_modules/ -package-lock.json \ No newline at end of file +package-lock.json +index.js +index.d.ts \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..031f8e7 --- /dev/null +++ b/index.ts @@ -0,0 +1,31 @@ +import type { InstrumentationConfig, InstrumentationMatcher, create as internalCreate } from './pkg/orchestrion_js'; + +// ./pkg/orchestrion_js.js has a side effect of loading the wasm binary. +// We only want that if the library is actually used! +let cachedCreate: typeof internalCreate | undefined; + +/** + * Create a new instrumentation matcher from an array of instrumentation configs. + */ +export function create(configs: InstrumentationConfig[], dc_module?: string | null): InstrumentationMatcher { + if (!cachedCreate) { + cachedCreate = require('./pkg/orchestrion_js.js').create; + } + + if (cachedCreate === undefined) { + throw new Error("Failed to load '@apm-js-collab/code-transformer'"); + } + + return cachedCreate(configs, dc_module); +} + +export type { + FunctionKind, + FunctionQuery, + InstrumentationConfig, + InstrumentationMatcher, + ModuleMatcher, + ModuleType, + TransformOutput, + Transformer, +} from './pkg/orchestrion_js' diff --git a/package.json b/package.json index fc9f18b..676d7f1 100644 --- a/package.json +++ b/package.json @@ -10,17 +10,21 @@ "./pkg/orchestrion_js_bg.wasm", "./pkg/orchestrion_js.js", "./pkg/orchestrion_js.d.ts", + "./index.js", + "./index.d.ts", "LICENSE", "NOTICE" ], - "main": "./pkg/orchestrion_js.js", - "types": "./pkg/orchestrion_js.d.ts", + "main": "./index.js", + "types": "./index.d.ts", "scripts": { - "build": "wasm-pack build --target nodejs --release -- --features wasm", + "build": "wasm-pack build --target nodejs --release -- --features wasm && yarn build:wrapper", + "build:wrapper": "tsc index.ts --declaration --module commonjs", "test": "vitest run", "test:watch": "vitest" }, "devDependencies": { + "@types/node": "^18.0.0", "source-map": "^0.7.6", "typescript": "^5.8.3", "vitest": "^3.2.4", diff --git a/yarn.lock b/yarn.lock index b0030a6..24a26bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -254,6 +254,13 @@ resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.8.tgz#958b91c991b1867ced318bedea0e215ee050726e" integrity sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w== +"@types/node@^18.0.0": + version "18.19.124" + resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.124.tgz#6f49e4fab8274910691a900e8a14316cbf3c7a31" + integrity sha512-hY4YWZFLs3ku6D2Gqo3RchTd9VRCcrjqp/I0mmohYeUVA5Y8eCXKJEasHxLAJVZRJuQogfd1GiJ9lgogBgKeuQ== + dependencies: + undici-types "~5.26.4" + "@vitest/expect@3.2.4": version "3.2.4" resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-3.2.4.tgz#8362124cd811a5ee11c5768207b9df53d34f2433" @@ -711,6 +718,11 @@ typescript@^5.8.3: resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.8.3.tgz#92f8a3e5e3cf497356f4178c34cd65a7f5e8440e" integrity sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ== +undici-types@~5.26.4: + version "5.26.5" + resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617" + integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA== + vite-node@3.2.4: version "3.2.4" resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-3.2.4.tgz#f3676d94c4af1e76898c162c92728bca65f7bb07" From 64ce36eec7f25855dd42edfcd4be4453161649f7 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 17 Sep 2025 22:30:49 +0200 Subject: [PATCH 2/2] Test via the new entry point --- tests/wasm/tests.test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wasm/tests.test.mjs b/tests/wasm/tests.test.mjs index 2788436..53ed49d 100644 --- a/tests/wasm/tests.test.mjs +++ b/tests/wasm/tests.test.mjs @@ -1,4 +1,4 @@ -import { create } from "../../pkg/orchestrion_js.js"; +import { create } from "../../index.js"; import { describe, test, expect } from "vitest"; import tsc from "typescript"; import { SourceMapConsumer } from "source-map";