3
3
import { readFile } from "node:fs/promises" ;
4
4
import { parseSourceMapPath } from "./wasmparser.js" ;
5
5
import { BasicSourceMapConsumer , IndexedSourceMapConsumer , SourceMapConsumer } from "source-map" ;
6
+ import chalk from "chalk" ;
6
7
7
8
export interface WebAssemblyCallSite {
8
9
functionName : string ;
@@ -47,7 +48,7 @@ function getOriginLocationWithSourceMap(
47
48
}
48
49
49
50
function getWebAssemblyFunctionName ( callSite : NodeJS . CallSite ) : string {
50
- return callSite . getFunctionName ( ) ?? `wasm-function[${ callSite . getFunction ( ) } ]` ;
51
+ return callSite . getFunctionName ( ) ?? `wasm-function[${ callSite . getFunction ( ) ?. toString ( ) ?? "unknown" } ]` ;
51
52
}
52
53
53
54
function createWebAssemblyCallSite (
@@ -87,15 +88,24 @@ export interface ExecutionError {
87
88
stacks : WebAssemblyCallSite [ ] ;
88
89
}
89
90
90
- async function getSourceMapConsumer ( sourceMapPath : string ) : Promise < BasicSourceMapConsumer | IndexedSourceMapConsumer > {
91
- return new Promise < BasicSourceMapConsumer | IndexedSourceMapConsumer > ( async ( resolve , reject ) => {
92
- let sourceMapContent : string | null = null ;
91
+ async function getSourceMapConsumer ( sourceMapPath : string | null ) : Promise < SourceMapHandler | null > {
92
+ if ( sourceMapPath === null ) {
93
+ return null ;
94
+ }
95
+ const sourceMapContent : string | null = await ( async ( ) => {
93
96
try {
94
- sourceMapContent = await readFile ( sourceMapPath , "utf8" ) ;
97
+ return await readFile ( sourceMapPath , "utf8" ) ;
95
98
} catch ( error ) {
96
- reject ( error ) ;
97
- return ;
99
+ if ( error instanceof Error ) {
100
+ console . log ( chalk . yellow ( `Failed to read source map file: ${ sourceMapPath } due to ${ error } ` ) ) ;
101
+ }
102
+ return null ;
98
103
}
104
+ } ) ( ) ;
105
+ if ( sourceMapContent === null ) {
106
+ return null ;
107
+ }
108
+ return new Promise < SourceMapHandler > ( ( resolve ) => {
99
109
SourceMapConsumer . with ( sourceMapContent , null , ( consumer ) => {
100
110
resolve ( consumer ) ;
101
111
} ) ;
@@ -108,7 +118,7 @@ export async function handleWebAssemblyError(
108
118
) : Promise < ExecutionError > {
109
119
const wasmBuffer = await readFile ( wasmPath ) ;
110
120
const sourceMapPath = parseSourceMapPath ( wasmBuffer . buffer as ArrayBuffer ) ;
111
- const sourceMapConsumer = sourceMapPath ? await getSourceMapConsumer ( sourceMapPath ) : null ;
121
+ const sourceMapConsumer : SourceMapHandler | null = await getSourceMapConsumer ( sourceMapPath ) ;
112
122
let stacks : NodeJS . CallSite [ ] = [ ] ;
113
123
const originalPrepareStackTrace = Error . prepareStackTrace ;
114
124
Error . prepareStackTrace = ( _ : Error , structuredStackTrace : NodeJS . CallSite [ ] ) => {
@@ -120,6 +130,6 @@ export async function handleWebAssemblyError(
120
130
message : error . message ,
121
131
stacks : stacks
122
132
. map ( ( callSite ) => createWebAssemblyCallSite ( callSite , { wasmPath, sourceMapConsumer } ) )
123
- . filter ( ( callSite ) => callSite != null ) ,
133
+ . filter ( ( callSite ) => callSite !== null ) ,
124
134
} ;
125
135
}
0 commit comments