How can I take a minified javascript stack trace and run it against a source map to get the proper error?

后端 未结 5 1389
遇见更好的自我
遇见更好的自我 2020-12-13 17:46

On our production server, I have minified javascript published and I\'m not including a map file with it, because I don\'t want the user to be able to understand what\'s hap

相关标签:
5条回答
  • 2020-12-13 18:25

    What you want to do is parse the source maps. This has nothing to do with web browsers. All you need to do is translate the minified reference into the unminified resource.

    If you have any experience with NodeJS there is already a package that does this for you.

    https://github.com/mozilla/source-map/

    To install the library

    npm install -g source-map
    

    or

    yarn global add source-map
    

    Create a file named "issue.js"

    fs = require('fs');
    var sourceMap = require('source-map');
    var smc = new sourceMap.SourceMapConsumer(fs.readFileSync("./app.min.js.map","utf8"));
    console.log(smc.originalPositionFor({line: 1, column: 3284}));
    

    Run the file with node

    node issue.js
    

    It should output the location in the original file to the console for first line from the stack trace.

    Note: I tell you install source-map globally for ease of use, but you could create a node project that does what you need and installs it locally.

    0 讨论(0)
  • 2020-12-13 18:34

    I figured there was no super simple tool for converting a minified stack trace into a readable one using a source map (without having to use a web service), so I created a tool for it:

    https://github.com/mifi/stacktracify

    Install and use it as follows:

    npm install -g stacktracify
    

    Now copy a minified stacktrace to your clipboard - then run:

    stacktracify /path/to/js.map
    
    0 讨论(0)
  • 2020-12-13 18:35

    If you had access to the source map file externally and could get the same file structure you could work it out I guess, but I'm not aware of any tools outside the browser that will help you with that.

    The added advantage of having the data in a running browser will allow checking of locals which you won't get even with a source map.

    You might want to consider a tool such as rollbar to do error reporting. This will report all the locals in each frame to help debugging. It has support for sourcemaps outside the browser to address your security concerns.

    0 讨论(0)
  • 2020-12-13 18:36

    Adding to @Reactgular's answer, the below snippet will work with the latest version of source-map

      const rawSourceMap = fs.readFileSync("./app.min.js.map","utf8");
    
      const whatever = sourceMap.SourceMapConsumer.with(rawSourceMap, null, consumer => {
        console.log(consumer.originalPositionFor({
          line: 1,
          column: 3284
        }));
      });
    

    And to add to the discussion on the thread a simple regex like /\/(\w*[-\.]?\w*).js:\d*:\d*/g

    Below is a very simple regex to find all line numbers in a stacktrace.

      //regex for patterns like utils.js, utils123.js, utils-name.js, utils.version.js
      var patt = /\/(\w*[-\.]?\w*).js:\d*:\d*/g; 
      // returns matches like ['/app.min.js:1:3284', '/bower.min.js:44:193', ..]
      var errorPositions = line.match(patt);
      console.log(errorPositions);
      if(!errorPositions || errorPositions.length === 0) {
          console.log("No error line numbers detected in the file. Ensure your stack trace file is proper");
          return;
      }
      errorPositions.forEach(function(error) {
        findInSourceMap(error);
      });
    });
    
    0 讨论(0)
  • 2020-12-13 18:36

    Append comment directive for the JS running in the page.

    //# sourceMappingURL=/path/to/your/sourcemap.map

    In firefox (not sure about chrome) to tell the Debugger to use source maps if they are available, click the "Debugger settings" button and select "Show original sources" from the list of settings that pops up:

    0 讨论(0)
提交回复
热议问题