1
1
'use strict' ;
2
2
3
3
const {
4
+ ArrayIsArray,
4
5
ArrayPrototypePush,
5
6
JSONParse,
6
7
RegExpPrototypeExec,
@@ -15,7 +16,7 @@ let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
15
16
debug = fn ;
16
17
} ) ;
17
18
18
- const { validateBoolean } = require ( 'internal/validators' ) ;
19
+ const { validateBoolean, validateCallSite } = require ( 'internal/validators' ) ;
19
20
const {
20
21
setSourceMapsEnabled : setSourceMapsNative ,
21
22
} = internalBinding ( 'errors' ) ;
@@ -351,11 +352,64 @@ function findSourceMap(sourceURL) {
351
352
return sourceMap ;
352
353
}
353
354
355
+ /**
356
+ * @typedef {object } CallSite // The call site
357
+ * @property {string } scriptName // The name of the resource that contains the
358
+ * script for the function for this StackFrame
359
+ * @property {string } functionName // The name of the function associated with this stack frame
360
+ * @property {number } lineNumber // The number, 1-based, of the line for the associate function call
361
+ * @property {number } columnNumber // The 1-based column offset on the line for the associated function call
362
+ */
363
+
364
+ /**
365
+ * @param {CallSite } callSite // The call site object to reconstruct from source map
366
+ * @returns {CallSite | undefined } // The reconstructed call site object
367
+ */
368
+ function reconstructCallSite ( callSite ) {
369
+ const { scriptName, lineNumber, column } = callSite ;
370
+ const sourceMap = findSourceMap ( scriptName ) ;
371
+ if ( ! sourceMap ) return ;
372
+ const entry = sourceMap . findEntry ( lineNumber - 1 , column - 1 ) ;
373
+ if ( ! entry ?. originalSource ) return ;
374
+ return {
375
+ __proto__ : null ,
376
+ // If the name is not found, it is an empty string to match the behavior of `util.getCallSite()`
377
+ functionName : entry . name ?? '' ,
378
+ scriptName : entry . originalSource ,
379
+ lineNumber : entry . originalLine + 1 ,
380
+ column : entry . originalColumn + 1 ,
381
+
382
+ } ;
383
+ }
384
+
385
+ /**
386
+ *
387
+ * The call site object or array of object to map (ex `util.getCallSite()`)
388
+ * @param {CallSite | CallSite[] } callSites
389
+ * An object or array of objects with the reconstructed call site
390
+ * @returns {CallSite | CallSite[] }
391
+ */
392
+ function mapCallSite ( callSites ) {
393
+ if ( ArrayIsArray ( callSites ) ) {
394
+ const result = [ ] ;
395
+ for ( let i = 0 ; i < callSites . length ; ++ i ) {
396
+ const callSite = callSites [ i ] ;
397
+ validateCallSite ( callSite ) ;
398
+ const found = reconstructCallSite ( callSite ) ;
399
+ ArrayPrototypePush ( result , found ?? callSite ) ;
400
+ }
401
+ return result ;
402
+ }
403
+ validateCallSite ( callSites ) ;
404
+ return reconstructCallSite ( callSites ) ?? callSites ;
405
+ }
406
+
354
407
module . exports = {
355
408
findSourceMap,
356
409
getSourceMapsEnabled,
357
410
setSourceMapsEnabled,
358
411
maybeCacheSourceMap,
359
412
maybeCacheGeneratedSourceMap,
413
+ mapCallSite,
360
414
sourceMapCacheToObject,
361
415
} ;
0 commit comments