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

trace-record

v0.2.0

Published

Get the stack trace of the creation of objects

Downloads

9

Readme

trace-record

npm version npm downloads bundle JSDocs License

Get the stack trace of the creation of objects.

Usages

// foo.ts
import { record } from 'trace-record'

export const obj = record({ some: 'obj' })
// bar.ts
import { getTrace } from 'trace-record'
import { obj } from './foo'

console.log(getTrace(obj)) // [{ file: 'foo.ts', line: 3, column: 0 }, ...]

APIs

record<T>(obj: T): T

record() is a bypass function that returns the object passed in. But also records the current stack trace and binds it to the object with an internal WeakMap. To retrieve the stack trace, use getTrace() and pass the object instance to it.

import { record } from 'trace-record'

const obj = record({ some: 'obj' })
const arr = record([1, 2, 3])
const fn = record(() => {})

Since it uses WeakMap under the hood, it requires the object to be a reference type (object-like). Primitive types like string, number, boolean, null, undefined will not work.

getTrace(obj: any): StackFrame[] | undefined

getTrace() retrieves the stack trace of the object created by record(). It returns an array of StackFrame array. Returns undefined if the object is not recorded. Stacktrace parsing is powered by error-stack-parser-es

getTraceRaw(obj: any): string | undefined

Same as getTrace() but returns a raw, unparsed, stacktrace string provided by the the JavaScript engine. The format may vary between engines and runtimes.

Noop Exports

To make it easier to bail out the tracing in production, you can alias the package to the noop export in your build tool.

defineConfig({
  alias: {
    'trace-record': 'trace-record/noop'
  }
})

Transpiler

I have a plan to rewrite a transpiler to automatically insert the record() for specific types of objects. For now, you might need to manually add record() to the objects you want to trace.

Sponsors

License

MIT License © 2023-PRESENT Anthony Fu