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,63 @@ 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
+ lineNumber : entry . originalLine + 1 ,
379
+ column : entry . originalColumn + 1 ,
380
+ scriptName : entry . originalSource ,
381
+ } ;
382
+ }
383
+
384
+ /**
385
+ *
386
+ * The call site object or array of object to map (ex `util.getCallSite()`)
387
+ * @param {CallSite | CallSite[] } callSites
388
+ * An object or array of objects with the reconstructed call site
389
+ * @returns {CallSite | CallSite[] }
390
+ */
391
+ function mapCallSite ( callSites ) {
392
+ if ( ArrayIsArray ( callSites ) ) {
393
+ const result = [ ] ;
394
+ for ( let i = 0 ; i < callSites . length ; ++ i ) {
395
+ const callSite = callSites [ i ] ;
396
+ validateCallSite ( callSite ) ;
397
+ const found = reconstructCallSite ( callSite ) ;
398
+ ArrayPrototypePush ( result , found ?? callSite ) ;
399
+ }
400
+ return result ;
401
+ }
402
+ validateCallSite ( callSites ) ;
403
+ return reconstructCallSite ( callSites ) ?? callSites ;
404
+ }
405
+
354
406
module . exports = {
355
407
findSourceMap,
356
408
getSourceMapsEnabled,
357
409
setSourceMapsEnabled,
358
410
maybeCacheSourceMap,
359
411
maybeCacheGeneratedSourceMap,
412
+ mapCallSite,
360
413
sourceMapCacheToObject,
361
414
} ;
0 commit comments