@@ -16,12 +16,19 @@ import {
1616 discoverWasmPairs ,
1717 syncWasmSourcemap ,
1818} from "../../../src/lib/sourcemap/wasm.js" ;
19+ import { parseSections } from "../../../src/lib/wasm/binary.js" ;
1920import {
20- encodeModule ,
21- makeBuildIdSection ,
22- makeCustomSection ,
23- } from "../../../src/lib/wasm/binary.js" ;
24- import { uuidToBytes } from "../../../src/lib/wasm/build-id.js" ;
21+ buildIdFromSections ,
22+ debugIdFromBuildId ,
23+ formatBuildId ,
24+ uuidToBytes ,
25+ } from "../../../src/lib/wasm/build-id.js" ;
26+ import {
27+ byteVector ,
28+ customSection ,
29+ fromHex ,
30+ wasmModule ,
31+ } from "../wasm/helpers.js" ;
2532
2633/** Deterministic build id used wherever the exact debug ID matters. */
2734const FIXED_UUID = "00000000-0000-4000-8000-000000000000" ;
@@ -37,12 +44,14 @@ beforeEach(async () => {
3744
3845/** Write a wasm module, optionally carrying a `build_id`. */
3946async function writeWasm ( name : string , buildId ?: string ) : Promise < string > {
40- const sections = [ makeCustomSection ( "name" , Uint8Array . from ( [ 0x00 ] ) ) ] ;
47+ const sections = [ customSection ( "name" , fromHex ( "00" ) ) ] ;
4148 if ( buildId ) {
42- sections . push ( makeBuildIdSection ( uuidToBytes ( buildId ) as Uint8Array ) ) ;
49+ sections . push (
50+ customSection ( "build_id" , byteVector ( uuidToBytes ( buildId ) as Uint8Array ) )
51+ ) ;
4352 }
4453 const path = join ( dir , name ) ;
45- await writeFile ( path , encodeModule ( sections ) ) ;
54+ await writeFile ( path , wasmModule ( sections ) ) ;
4655 return path ;
4756}
4857
@@ -67,6 +76,12 @@ async function readMap(path: string): Promise<Record<string, unknown>> {
6776 return JSON . parse ( await readFile ( path , "utf-8" ) ) ;
6877}
6978
79+ /** Read the debug ID a module's `build_id` section stands for. */
80+ async function readModuleDebugId ( path : string ) : Promise < string | undefined > {
81+ const buildId = buildIdFromSections ( parseSections ( await readFile ( path ) ) ) ;
82+ return buildId ? debugIdFromBuildId ( formatBuildId ( buildId ) ) : undefined ;
83+ }
84+
7085describe ( "syncWasmSourcemap" , ( ) => {
7186 test ( "stamps the map with the id the module already carries" , async ( ) => {
7287 const wasmPath = await writeWasm ( "app.wasm" , FIXED_UUID ) ;
@@ -89,14 +104,13 @@ describe("syncWasmSourcemap", () => {
89104 const wasmPath = await writeWasm ( "bare.wasm" ) ;
90105 const mapPath = await writeMap ( "bare.wasm.map" ) ;
91106
92- const result = await syncWasmSourcemap (
93- { wasmPath, mapPath } ,
94- { buildId : uuidToBytes ( FIXED_UUID ) as Uint8Array }
95- ) ;
107+ const result = await syncWasmSourcemap ( { wasmPath, mapPath } ) ;
96108
97109 expect ( result . moduleStamped ) . toBe ( true ) ;
98- expect ( result . debugId ) . toBe ( FIXED_UUID ) ;
99- expect ( ( await readMap ( mapPath ) ) . debug_id ) . toBe ( FIXED_UUID ) ;
110+ // The minted id is random, so the invariant to check is that both sides
111+ // ended up with the one now on the module.
112+ expect ( result . debugId ) . toBe ( await readModuleDebugId ( wasmPath ) ) ;
113+ expect ( ( await readMap ( mapPath ) ) . debug_id ) . toBe ( result . debugId ) ;
100114 } ) ;
101115
102116 test ( "writes nothing when the map already carries the module's id" , async ( ) => {
@@ -192,7 +206,12 @@ describe("discoverWasmPairs", () => {
192206 await mkdir ( join ( dir , "vendor" ) ) ;
193207 await writeFile (
194208 join ( dir , "vendor" , "lib.wasm" ) ,
195- encodeModule ( [ makeBuildIdSection ( uuidToBytes ( FIXED_UUID ) as Uint8Array ) ] )
209+ wasmModule ( [
210+ customSection (
211+ "build_id" ,
212+ byteVector ( uuidToBytes ( FIXED_UUID ) as Uint8Array )
213+ ) ,
214+ ] )
196215 ) ;
197216 await writeFile ( join ( dir , "vendor" , "lib.wasm.map" ) , '{"version":3}' ) ;
198217 const wasmPath = await writeWasm ( "app.wasm" , FIXED_UUID ) ;
0 commit comments