Skip to content

Commit acc42c5

Browse files
fix: repair wasm browser entrypoint
1 parent ea97ee4 commit acc42c5

5 files changed

Lines changed: 52 additions & 76 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"typos": "typos",
2929
"deny": "cargo deny check",
3030
"test": "pnpm run test:rust && pnpm run test:js",
31-
"test:js": "node --test packages/codec/__tests__/codec.test.mjs packages/sourcemap/__tests__/sourcemap.test.mjs packages/sourcemap-wasm/__tests__/sourcemap-wasm.test.mjs packages/sourcemap-wasm/__tests__/coverage-utils.test.mjs packages/generator-wasm/__tests__/generator-wasm.test.mjs packages/remapping-wasm/__tests__/remapping-wasm.test.mjs packages/trace-mapping/__tests__/trace-mapping.test.mjs packages/trace-mapping/__tests__/compat.test.mjs packages/source-map/__tests__/source-map.test.mjs packages/gen-mapping/__tests__/gen-mapping.test.mjs packages/gen-mapping/__tests__/gen-mapping.cjs.test.cjs packages/remapping/__tests__/remapping.test.mjs packages/remapping/__tests__/remapping.cjs.test.cjs packages/remapping/__tests__/compat.test.mjs",
31+
"test:js": "node --test packages/codec/__tests__/codec.test.mjs packages/sourcemap/__tests__/sourcemap.test.mjs packages/sourcemap-wasm/__tests__/sourcemap-wasm.test.mjs packages/sourcemap-wasm/__tests__/coverage-utils.test.mjs packages/sourcemap-wasm/__tests__/browser.test.mjs packages/generator-wasm/__tests__/generator-wasm.test.mjs packages/remapping-wasm/__tests__/remapping-wasm.test.mjs packages/trace-mapping/__tests__/trace-mapping.test.mjs packages/trace-mapping/__tests__/compat.test.mjs packages/source-map/__tests__/source-map.test.mjs packages/gen-mapping/__tests__/gen-mapping.test.mjs packages/gen-mapping/__tests__/gen-mapping.cjs.test.cjs packages/remapping/__tests__/remapping.test.mjs packages/remapping/__tests__/remapping.cjs.test.cjs packages/remapping/__tests__/compat.test.mjs",
3232
"test:rust": "cargo test",
3333
"coverage": "pnpm run coverage:rust && pnpm run coverage:js",
3434
"coverage:js": "mkdir -p coverage && node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/js-lcov.info --test-reporter=spec --test-reporter-destination=stdout packages/codec/__tests__/codec.test.mjs packages/sourcemap/__tests__/sourcemap.test.mjs packages/sourcemap-wasm/__tests__/sourcemap-wasm.test.mjs packages/sourcemap-wasm/__tests__/coverage-utils.test.mjs packages/generator-wasm/__tests__/generator-wasm.test.mjs packages/remapping-wasm/__tests__/remapping-wasm.test.mjs packages/trace-mapping/__tests__/trace-mapping.test.mjs packages/trace-mapping/__tests__/compat.test.mjs packages/source-map/__tests__/source-map.test.mjs packages/gen-mapping/__tests__/gen-mapping.test.mjs packages/gen-mapping/__tests__/gen-mapping.cjs.test.cjs packages/remapping/__tests__/remapping.test.mjs packages/remapping/__tests__/remapping.cjs.test.cjs packages/remapping/__tests__/compat.test.mjs",

packages/sourcemap-wasm/README.md

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,17 @@ Parse a source map from a JSON string.
128128

129129
A fast-scan alternative to `SourceMap` that defers VLQ decoding until lookup time. On construction it only parses JSON metadata and byte-scans the mappings string for semicolons (to identify line boundaries). VLQ decoding happens per-line on demand with progressive state tracking. This makes parse time near-instant at the cost of slightly slower first lookups.
130130

131-
`LazySourceMap` does **not** parse `sourcesContent` -- use the fast-scan wrapper (see below) if you need it.
131+
Import it directly from the supported package entrypoint:
132+
133+
```js
134+
import { LazySourceMap } from "@srcmap/sourcemap-wasm";
135+
136+
const sm = new LazySourceMap(jsonString);
137+
const loc = sm.originalPositionFor(42, 10);
138+
sm.free();
139+
```
140+
141+
`LazySourceMap` does **not** parse `sourcesContent`. Use `SourceMap` when source contents are required.
132142

133143
#### Static methods
134144

@@ -161,59 +171,6 @@ A fast-scan alternative to `SourceMap` that defers VLQ decoding until lookup tim
161171
| `debugId` | `string \| undefined` | `debugId` proposal field |
162172
| `lineCount` | `number` | Number of generated lines |
163173

164-
---
165-
166-
### Fast-scan mode (`fast.js`)
167-
168-
The `fast.js` entry point wraps `LazySourceMap` with lazy `sourcesContent` extraction from the original JSON string. It exposes a `SourceMap` class with the same interface you would expect, but internally:
169-
170-
1. Parses using fast-scan mode (no VLQ decode at parse time).
171-
2. Keeps the raw JSON string on the JS side and only calls `JSON.parse()` to extract `sourcesContent` on first access (then releases the JSON for GC).
172-
3. Forwards all lookups to the underlying `LazySourceMap`.
173-
174-
This is ideal when you only need to look up a few positions and want the fastest possible parse time.
175-
176-
```js
177-
const { SourceMap } = require('@srcmap/sourcemap-wasm/pkg/fast.js');
178-
179-
const sm = new SourceMap(jsonString);
180-
181-
// Lookups work the same as the regular SourceMap
182-
const loc = sm.originalPositionFor(42, 10);
183-
184-
// sourcesContent is extracted lazily from JSON on first access
185-
const content = sm.sourceContentFor(0);
186-
187-
sm.free();
188-
```
189-
190-
#### Instance methods
191-
192-
| Method | Returns | Description |
193-
|--------|---------|-------------|
194-
| `originalPositionFor(line, column)` | `{ source, line, column, name } \| null` | Forward lookup (0-based) |
195-
| `originalPositionFlat(line, column)` | `Int32Array` | Forward lookup returning flat indices |
196-
| `originalPositionBuf(line, column)` | `boolean` | Zero-allocation lookup via static buffer |
197-
| `originalPositionsFor(positions: Int32Array)` | `Int32Array` | Batch forward lookup |
198-
| `source(index)` | `string \| null` | Resolve source index to filename |
199-
| `name(index)` | `string \| null` | Resolve name index to string |
200-
| `sourceContentFor(index)` | `string \| null` | Get source content by index (triggers lazy JSON parse on first call) |
201-
| `isIgnoredIndex(index)` | `boolean` | Check if a source index is in the `ignoreList` |
202-
| `free()` | `void` | Release WASM memory |
203-
204-
#### Instance properties
205-
206-
| Property | Type | Description |
207-
|----------|------|-------------|
208-
| `sources` | `string[]` | All source filenames |
209-
| `names` | `string[]` | All names |
210-
| `sourcesContent` | `(string \| null)[] \| null` | Source file contents (lazily extracted from JSON on first access) |
211-
| `ignoreList` | `number[]` | Source ignore list indices |
212-
| `file` | `string \| undefined` | Output filename |
213-
| `sourceRoot` | `string \| undefined` | Source root prefix |
214-
| `debugId` | `string \| undefined` | `debugId` proposal field |
215-
| `lineCount` | `number` | Number of generated lines |
216-
217174
## Performance
218175

219176
### Batch API vs trace-mapping
@@ -231,7 +188,7 @@ The batch API (`originalPositionsFor`) returns a flat `Int32Array`, avoiding per
231188
| Use case | Recommended package |
232189
|----------|-------------------|
233190
| Batch lookups (error stacks, coverage) | **@srcmap/sourcemap-wasm** (batch API) |
234-
| Few lookups, fast parse | **@srcmap/sourcemap-wasm** (fast-scan mode via `fast.js`) |
191+
| Few lookups, fast parse | **@srcmap/sourcemap-wasm** (`LazySourceMap`) |
235192
| Few individual lookups | `@jridgewell/trace-mapping` (lower per-call overhead) |
236193
| Native Node.js addons | `@srcmap/sourcemap` (NAPI) |
237194
| Browser environments | **@srcmap/sourcemap-wasm** |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import assert from "node:assert/strict";
2+
import { readFileSync } from "node:fs";
3+
import { it } from "node:test";
4+
import init, { LazySourceMap, SourceMap, resultPtr, wasmMemory } from "../browser/index.mjs";
5+
6+
it("exposes the browser API as static ESM exports before initialization", () => {
7+
assert.equal(typeof init, "function");
8+
assert.equal(typeof SourceMap, "function");
9+
assert.equal(typeof LazySourceMap, "function");
10+
assert.equal(typeof resultPtr, "function");
11+
assert.equal(typeof wasmMemory, "function");
12+
});
13+
14+
it("initializes the generated module only once", async () => {
15+
const wasm = readFileSync(new URL("../web/srcmap_sourcemap_wasm_bg.wasm", import.meta.url));
16+
17+
const input = { module_or_path: wasm };
18+
const first = init(input);
19+
const second = init(input);
20+
21+
assert.equal(second, first);
22+
await first;
23+
});

packages/sourcemap-wasm/browser/index.mjs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,27 @@
1111
* call init() before using any exports.
1212
*/
1313

14-
let initialized = false;
14+
import initWasm, {
15+
LazySourceMap,
16+
SourceMap,
17+
resultPtr,
18+
wasmMemory,
19+
} from "../web/srcmap_sourcemap_wasm.js";
20+
21+
export { LazySourceMap, SourceMap, resultPtr, wasmMemory };
22+
1523
let initPromise = null;
1624

1725
/**
1826
* Initialize the WASM module. Must be called before using any exports.
19-
* Safe to call multiple timessubsequent calls return immediately.
20-
* @param {string|URL|Request|BufferSource} [input] - Optional WASM module source
27+
* Safe to call multiple times, subsequent calls return the same promise.
28+
* @param {string|URL|Request|BufferSource} [input] Optional WASM module source
2129
* @returns {Promise<void>}
2230
*/
23-
export default async function init(input) {
24-
if (initialized) return;
25-
if (initPromise) return initPromise;
26-
27-
initPromise = (async () => {
28-
const wasm = await import("../web/srcmap_sourcemap_wasm.js");
29-
await wasm.default(input);
30-
initialized = true;
31-
32-
// Re-export all WASM exports
33-
Object.assign(exports, wasm);
34-
})();
31+
export default function init(input) {
32+
if (!initPromise) {
33+
initPromise = initWasm(input).then(() => undefined);
34+
}
3535

3636
return initPromise;
3737
}
38-
39-
const exports = {};
40-
41-
export { exports as wasm };

packages/sourcemap-wasm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"build:web": "wasm-pack build --target web --out-dir web",
5252
"build:bundler": "wasm-pack build --target bundler --out-dir bundler",
5353
"build:all": "pnpm run build && pnpm run build:web",
54-
"test": "node --test __tests__/sourcemap-wasm.test.mjs __tests__/coverage-utils.test.mjs",
54+
"test": "node --test __tests__/sourcemap-wasm.test.mjs __tests__/coverage-utils.test.mjs __tests__/browser.test.mjs",
5555
"test:coverage": "mkdir -p coverage && node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-reporter=spec --test-reporter-destination=stdout __tests__/sourcemap-wasm.test.mjs __tests__/coverage-utils.test.mjs"
5656
}
5757
}

0 commit comments

Comments
 (0)