Skip to content

Commit 399e942

Browse files
authored
feat: Load wasm lazily (apm-js-collab#45)
1 parent 9da6297 commit 399e942

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ target
44
tests/*/instrumented.*
55
pkg/
66
node_modules/
7-
package-lock.json
7+
package-lock.json
8+
index.js
9+
index.d.ts

index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { InstrumentationConfig, InstrumentationMatcher, create as internalCreate } from './pkg/orchestrion_js';
2+
3+
// ./pkg/orchestrion_js.js has a side effect of loading the wasm binary.
4+
// We only want that if the library is actually used!
5+
let cachedCreate: typeof internalCreate | undefined;
6+
7+
/**
8+
* Create a new instrumentation matcher from an array of instrumentation configs.
9+
*/
10+
export function create(configs: InstrumentationConfig[], dc_module?: string | null): InstrumentationMatcher {
11+
if (!cachedCreate) {
12+
cachedCreate = require('./pkg/orchestrion_js.js').create;
13+
}
14+
15+
if (cachedCreate === undefined) {
16+
throw new Error("Failed to load '@apm-js-collab/code-transformer'");
17+
}
18+
19+
return cachedCreate(configs, dc_module);
20+
}
21+
22+
export type {
23+
FunctionKind,
24+
FunctionQuery,
25+
InstrumentationConfig,
26+
InstrumentationMatcher,
27+
ModuleMatcher,
28+
ModuleType,
29+
TransformOutput,
30+
Transformer,
31+
} from './pkg/orchestrion_js'

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99
"files": [
1010
"./pkg/orchestrion_js.js",
1111
"./pkg/orchestrion_js.d.ts",
12+
"./index.js",
13+
"./index.d.ts",
1214
"LICENSE",
1315
"NOTICE"
1416
],
15-
"main": "./pkg/orchestrion_js.js",
16-
"types": "./pkg/orchestrion_js.d.ts",
17+
"main": "./index.js",
18+
"types": "./index.d.ts",
1719
"scripts": {
18-
"build": "wasm-pack build --target nodejs --release -- --features wasm",
19-
"postbuild": "node inline-binary.js",
20+
"build": "wasm-pack build --target nodejs --release -- --features wasm && yarn build:wrapper && yarn build:inline-binary",
21+
"build:wrapper": "tsc index.ts --declaration --module commonjs",
22+
"build:inline-binary": "node inline-binary.js",
2023
"test": "vitest run",
2124
"test:watch": "vitest"
2225
},
2326
"devDependencies": {
27+
"@types/node": "^18.0.0",
2428
"source-map": "^0.7.6",
2529
"typescript": "^5.8.3",
2630
"vitest": "^3.2.4",

tests/wasm/tests.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { create } from "../../pkg/orchestrion_js.js";
1+
import { create } from "../../index.js";
22
import { describe, test, expect } from "vitest";
33
import tsc from "typescript";
44
import { SourceMapConsumer } from "source-map";

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@
254254
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.8.tgz#958b91c991b1867ced318bedea0e215ee050726e"
255255
integrity sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==
256256

257+
"@types/node@^18.0.0":
258+
version "18.19.124"
259+
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.124.tgz#6f49e4fab8274910691a900e8a14316cbf3c7a31"
260+
integrity sha512-hY4YWZFLs3ku6D2Gqo3RchTd9VRCcrjqp/I0mmohYeUVA5Y8eCXKJEasHxLAJVZRJuQogfd1GiJ9lgogBgKeuQ==
261+
dependencies:
262+
undici-types "~5.26.4"
263+
257264
258265
version "3.2.4"
259266
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-3.2.4.tgz#8362124cd811a5ee11c5768207b9df53d34f2433"
@@ -711,6 +718,11 @@ typescript@^5.8.3:
711718
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.8.3.tgz#92f8a3e5e3cf497356f4178c34cd65a7f5e8440e"
712719
integrity sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==
713720

721+
undici-types@~5.26.4:
722+
version "5.26.5"
723+
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
724+
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==
725+
714726
715727
version "3.2.4"
716728
resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-3.2.4.tgz#f3676d94c4af1e76898c162c92728bca65f7bb07"

0 commit comments

Comments
 (0)