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

@tapjs/stack

v4.0.0

Published

Utility for working with stack traces

Downloads

502,675

Readme

@tapjs/stack

A comprehensive library for dealing with stack traces, supporting source map lookup (when enabled in node with --enable-source-maps), filtering of specified packages and/or Node.js internals, and capturing call sites relative to a given function.

The spiritual descendant of stack-utils.

Class CallSiteLike

This is similar to the CallSite class in V8. However, rather than provide methods to get all the relevant details about the call site, it has them set as properties. Additionally, it may be generated by parsing a line from a string stack trace, which is useful when tracking down the source of thrown errors.

See the typedocs for more information.

Class CallSiteLikeJSON

This is the "plain old JavaScript object" form of a CallSiteLike object, for use when serializing a CallSiteLike to YAML or JSON.

It contains all the same fields as CallSiteLike, but fields are unset rather than being set to null or undefined if they are not relevant to the call site, to reduce noise when printing test diagnostics.

The toString() value of CallSiteLike objects is a much terser representation of the call site than the standard Error.stack string, and it contains both the generated and origin call sites in the case of source mapped files.

See the typedocs for more information.

at(fn?: Function): CallSiteLike | undefined

Get the call site in the stack either where at() is called, or where the supplied fn function is called.

If fn is provided, and is not in the current call stack, then undefined will be returned.

capture(limit = 0, fn?: Function): CallSiteLike[]

Get an array of CallSiteLike objects for the current location, from the call to the fn argument if supplied, limited to the number of frames specified by limit.

If fn is supplied, and not in the current call stack, then an empty array will be returned.

If the limit argument is 0, then the current Error.stackTraceLimit value will be used.

This method is not re-entry safe, due to the fact that it relies on temporarily overriding the global Error.prepareStackTrace. As a result, if a capture() is triggered in any of the methods used by the CallSiteLike constructor (for example, if @tapjs/intercept is used to capture the process.cwd() method, which is used by path.resolve()), then that will cause problems. To work around this, if a re-entry is detected, then an empty stack of [] is returned.

Even if it was made re-entry safe, it would be easy to accidentally trigger an infinite recursion and stack overflow in such a scenario, so returning an empty stack in the case of re-entry is the best workaround.

captureString(limit = 0, fn?: Function): string

The same as capture(), but returns a string stack where each line is the toString() of the CallSiteLike object.

captureError(er: Error): CallSiteLike[]

Get a stack of CallSiteLike objects by parsing the stack property of the supplied Error object.

This does not actually look at the current call site, or do anything magical with the V8 engine. It's just parsing a string.

While some effort is made to interpret stacks correctly when an Error contains a name and message, remember that the Error.stack property in JavaScript is remarkably sloppy. In some cases, if the Error.message contains \n and some lines after the first look like stack trace lines, incorrect data may result. It's only as good as the stack you pass to it.

captureErrorString(er: Error): string

The same as captureError (with the same caveats) but presenting the string stack where each line is the toString() value of the CallSiteLike object.

parseStack(stack: string): CallSiteLike[]

Turn a string stack (either from @tapjs/stack or from a native Error object) into an array of CallSiteLike objects.

Only useful in some niche situations, most of the time you're better off using capture() or captureError().

expandStack(stack?: string | CallSiteLike[]): string

Expand a stack string (either from @tapjs/stack or from a JS Error object) into its conventional Error.stack form, complete with absolute paths, indentation, and repetitive at prefixes.

When a call site is source mapped, the origin source will be shown if the generated source is outside the cwd. If the cwd is not set on the CallSiteLike object, then generated location is always shown.

setCwd(cwd: string | undefined)

Set the current working directory used to shorten filename paths in CallSiteLike objects.

Default value is process.cwd(). Setting it to undefined will cause the CallSiteLike objects returned by the capture methods to display full file paths.

getCwd(): string | undefined

Get the current value of the effective cwd used by capture methods.

getFilterNodeInternals(): boolean

Tells you whether or not node internals are being filtered out.

Defaults to true

setFilterNodeInternals(f: boolean)

Specify whether node internals should be filtered out.

getFilterIgnoredPackages(): boolean

Tells you whether or not the set of ignored packages are being filtered out.

Defaults to true

setFilterIgnoredPackages(f: boolean)

Specify whether or not ignored packages should be filtered out.

getIgnoredPackages(): string[]

Get the list of packages that are filtered out of captured stacks.

addIgnoredPackage(pkg: string)

Add a package to the filter set.

removeIgnoredPackage(pkg: string)

Remove a package from the filter set.