How to use the wrapCallSite function from source-map-support

Find comprehensive JavaScript source-map-support.wrapCallSite code examples handpicked from public code repositorys.

source-map-support.wrapCallSite is a function that takes a CallSite object, such as a stack trace frame, and enhances it with source map information to improve error stack trace output.

127
128
129
130
131
132
133
134
135
136
137
    return { map: parsedSourceMaps[pathForSourceMap] };
  }
  return null;
}


var origWrapper = sourcemap_support.wrapCallSite;
var wrapCallSite = function (frame) {
  var frame = origWrapper(frame);
  var wrapGetter = function (name) {
    var origGetter = frame[name];
fork icon305
star icon993
watch icon40

+ 4 other calls in file

17
18
19
20
21
22
23
24
25
26
27
28
  )
} else {
  settings.atTap = true
}


settings.stackUtils.wrapCallSite = sourceMapSupport.wrapCallSite


/* istanbul ignore next - version specific */
if (settings.rimrafNeeded) {
  const rimraf = require('rimraf')
fork icon0
star icon0
watch icon0

How does source-map-support.wrapCallSite work?

source-map-support.wrapCallSite is a function provided by the source-map-support library that enhances stack trace output by adding source map information to each stack trace frame.

When source-map-support.wrapCallSite is called, it takes a single argument, a CallSite object, which represents a single frame in a stack trace.

source-map-support.wrapCallSite enhances this CallSite object by adding information about the original source location of the code that the frame corresponds to. This information is obtained from a source map, which maps the original source code to the generated code that is actually executed.

By adding this source map information, source-map-support.wrapCallSite allows error stack traces to be displayed in terms of the original source code, rather than the generated code. This can help make error messages more meaningful and easier to understand, especially in large or complex codebases.

Overall, source-map-support.wrapCallSite provides a valuable tool for improving the quality and readability of error stack trace output, making it easier to diagnose and fix errors in Node.js applications.

24
25
26
27
28
29
30
31
32


sourceMapSupport.install({environment:'node'})


module.exports = new StackUtils({
  internals: StackUtils.nodeInternals().concat(skip),
  wrapCallSite: sourceMapSupport.wrapCallSite
})
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const sourceMapSupport = require("source-map-support");
const sourceMap = require("source-map");

// Load a source map from a file or URL
const rawSourceMap = JSON.parse(fs.readFileSync("bundle.map"));

// Create a new SourceMapConsumer object from the source map
const consumer = await new sourceMap.SourceMapConsumer(rawSourceMap);

// Enhance the error stack trace with source map information
sourceMapSupport.install({
  environment: "node",
  retrieveSourceMap: (source) => {
    return { url: "bundle.js", map: rawSourceMap };
  },
  wrapCallSite: (frame) => {
    const functionName = frame.getFunctionName() || "anonymous";
    const fileName = frame.getFileName();
    const lineNumber = frame.getLineNumber();
    const columnNumber = frame.getColumnNumber();

    const originalPosition = consumer.originalPositionFor({
      line: lineNumber,
      column: columnNumber,
    });

    return Object.assign({}, frame, {
      getFileName: () => originalPosition.source,
      getLineNumber: () => originalPosition.line,
      getColumnNumber: () => originalPosition.column,
      getFunctionName: () => functionName,
    });
  },
});

// Generate an error to test the enhanced stack trace
function foo() {
  throw new Error("Something went wrong");
}

function bar() {
  foo();
}

bar();

In this example, we load a source map from a file or URL using the source-map library, and create a SourceMapConsumer object from it. We then use sourceMapSupport.install to enhance the error stack trace with source map information. This function takes an options object with three properties: environment specifies the environment the source maps are for (node, browser, or auto). retrieveSourceMap is a function that takes a URL and returns an object with url and map properties representing the source map for that URL. wrapCallSite is a function that takes a CallSite object and returns a new CallSite object with additional source map information. In this example, our wrapCallSite function uses the SourceMapConsumer object to look up the original source location for each frame in the stack trace, and creates a new CallSite object with this information. Once sourceMapSupport.install is called, any errors that occur will include enhanced stack trace information, allowing developers to see the original source code and location of the error, rather than the generated code. In this example, we generate an error by calling the bar function, which in turn calls the foo function and throws an Error. The resulting error will include an enhanced stack trace with source map information, making it easier to diagnose and fix the error.