npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

retrace

v1.0.0

Published

Use source maps on the server to generate readable browser stack traces

Downloads

5,458

Readme

Readable stack traces

Build Status

Using bundled and minified JavaScript code usually leaves you with hard to read stack traces. Some browser dev-tools automatically apply source maps to map locations back to the original source, but this doesn't help when you want to track all client-side errors on the server or when you have to deal with browsers that don't support source maps at all.

Retrace helps you to set up the following flow for your application:

  1. Submit unhandled client-side errors to the server
  2. Let the server parse the stack trace
  3. Fetch the source map for each mentioned file
  4. Map the location back to the original source
  5. Log a readable stack trace

Retrace works with all browsers supported by stacktrace-parser. This currently includes Chrome, Firefox, Internet Explorer, Safari, Opera and JavaScriptCore.

Sending stack traces to your server

In modern browsers we could just do something like this to submit a stack trace:

window.addEventListener('error', e =>
  fetch('/retrace?stack=' + e.error.stack)
);

To support a wider range of browsers you can use one of the clients provided by this package:

require('retrace/client/xhr')('/retrace');

This will set window.onerror to a function that uses an XMLHttpRequest to send the stack trace to the provided URL – /retrace in this case.

The package also contains another client that uses an Image beacon to transmit the data. You may use this as starting point for a custom implementation which might use an iframe, websocket or the fetch API as transport.

Using retrace to re-map the stacktraces

Here is a simple example of how to use retrace within an express app:

var express = require('express');
var retrace = require('retrace');

var app = express();

app.get('/retrace', function(req, res) {

  // Read the stack from a query parameter
  var stack = req.query.stack;

  retrace.map(stack).then(function(stack) {
    console.log(stack); // Log the re-mapped stack trace
    res.end(stack); // ... and send it back to the client
  })
  .catch(function(err) {
    console.log('Something went wrong', err);
    res.status(500).end();
  })
});

Private source maps

By default retrace downloads the sources and source maps via HTTP. If you don't want to expose your source maps you can register them manually:

var fs = require('fs');
var retrace = require('retrace');

var sourceMap = fs.readFileSync('./bundle.js.map', 'utf8');
retrace.register('https://example.com/bundle.min.js', sourceMap)

You can register() a source-map as string, as parsed JSON object or as promise that resolves to either of the two.

Alternatively you can provide a resolve function. This might be easier if you need to support multiple host names or protocols:

var retrace = require('retrace');

var sourceMap = JSON.parse(fs.readFileSync('./bundle.js.map', 'utf8'));

// Simple resolver that always returns the same source map
retrace.resolve = function(url) {
  return sourceMap;
};

License

MIT