@cparra/apex-reflection 3.0.0-dev.20260614084225 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,18 +1,35 @@
1
1
  import fs from "node:fs";
2
2
  import { compile } from "./node.mjs";
3
- async function reflectFor(type, declarationBody) {
4
- const bytes = fs.readFileSync(new URL("./node.wasm", import.meta.url));
5
- const compiledApp = await compile(bytes);
6
- const instantiatedApp = await compiledApp.instantiate();
7
- instantiatedApp.invokeMain();
8
- if (type === "reflectType") {
9
- const reflect = globalThis.reflect;
10
- return reflect(declarationBody);
11
- }
12
- else {
13
- const reflectTrigger = globalThis.reflectTrigger;
14
- return reflectTrigger(declarationBody);
3
+ // The wasm module is compiled and instantiated once and reused for every
4
+ // reflection. Without this, each call recompiled the ~380 KB module and reran
5
+ // `invokeMain`, which dominated throughput when reflecting many files (e.g. a
6
+ // whole project). Concurrent first-callers share a single instantiation by
7
+ // caching the promise rather than the resolved value; a failed load is not
8
+ // cached so a later call can retry.
9
+ let instancePromise;
10
+ function loadInstance() {
11
+ if (!instancePromise) {
12
+ instancePromise = (async () => {
13
+ const bytes = fs.readFileSync(new URL("./node.wasm", import.meta.url));
14
+ const compiledApp = await compile(bytes);
15
+ const instantiatedApp = await compiledApp.instantiate();
16
+ instantiatedApp.invokeMain();
17
+ return {
18
+ reflect: globalThis.reflect,
19
+ reflectTrigger: globalThis.reflectTrigger,
20
+ };
21
+ })().catch((error) => {
22
+ instancePromise = undefined;
23
+ throw error;
24
+ });
15
25
  }
26
+ return instancePromise;
27
+ }
28
+ async function reflectFor(type, declarationBody) {
29
+ const instance = await loadInstance();
30
+ return type === "reflectType"
31
+ ? instance.reflect(declarationBody)
32
+ : instance.reflectTrigger(declarationBody);
16
33
  }
17
34
  export async function reflect(declarationBody) {
18
35
  const result = await reflectFor("reflectType", declarationBody);
package/dist/node.wasm CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cparra/apex-reflection",
3
- "version": "3.0.0-dev.20260614084225",
3
+ "version": "3.1.0",
4
4
  "description": "Provides tools for reflecting Apex code, the language used in Salesforce development.",
5
5
  "main": "dist/index.mjs",
6
6
  "scripts": {