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

node-inspect-extracted

v3.0.2

Published

Node's util.inspect, extracted for use on the Web

Downloads

105,726

Readme

node-inspect-extracted

This library provides an as-faithful-as-possible implementation of Node.js's util.inspect function.

It was built in such a way that it can be kept up-to-date with node's implementation, by taking the code directly from node's repo, and changing nothing but the require() statements. All of the node built-in functions are emulated. Many of the incompatibilities generated from that emulation are not interesting for Web use cases.

Installation

npm install node-inspect-extracted

Use

This should work in node (for testing) and browsers, using either require, import, or as window.Inspect if you include this in your page as a script tag.

With require:

const util = require('node-inspect-extracted');
console.log(util.inspect(1));

With import:

import util from 'node-inspect-extracted';
console.log(util.inspect(2));

From the browser:

<script src="https://unpkg.com/node-inspect-extracted/dist/inspect.js"></script>
<script>
  console.log(util.inspect(3));
</script>

API

The following util functions:

And these extras:

  • Proxy(target, handler): a wrapper for the normal Proxy class that allows the showProxy option of inspect to work.
  • stylizeWithColor(str, styleType): colorize str with ANSI escapes according to the styleType
  • stylizeWithHTML(str, styleType): colorize str with HTML span tags

Colors

If you specify {colors: true} in the inspect options, you will get ANSI escape codes, just as you would in Node. That's unlikely to be helpful to you on the Web, so you might want stylizeWithHTML, which is also exported from the package:

inspect({ a: 1 }, {
  compact: false,
  stylize: stylizeWithHTML
});

which yields this ugly HTML:

{
  a: <span style="color:yellow;">1</span>
}

If you want better HTML, the lightly-documented stylize option requires a function that takes two parameters, a string, and a class name. The mappings from class names to colors is in inspect.styles, so start with this:

function stylizeWithHTML(str, styleType) {
  const style = inspect.styles[styleType];
  if (style !== undefined) {
    return `<span style="color:${style};">${str}</span>`;
  }
  return str;
}

Known Limitations

  • If you want your Proxy objects to have their internal object inspected, you may use the Proxy constructor exported by this project. That was done mostly for test coverage purposes. It is not recommended for production code.
  • arguments objects are not treated specially. [bug]
  • Several of the existing type checks (corresponding to Node's util.types) are weaker than the ones in Node, which has the freedom to use internal capabilities of the runtime. This means you can fake out the type detection to get output different than node. [bug]
  • Objects that have been mangled with Object.setPrototypeOf do not retain their original type information. [bug]
  • Promise state is not visible. All Promises will show up as Promise< pending > no matter what state they are in.
  • Map and Set iterators will not show their internal state because that cannot be done from unprivileged code without modifying the iterator. Entry iterators are not distinguished from value iterators. [bug]
  • WeakMap and WeakSet will not show their contents, because those contents cannot be iterated over in unprivileged code.
  • Colorful stack traces are not completely accurate with respect to what modules are Node-internal. This doesn't matter on the Web.

Developing

Check out NodeJS and this package next to one another:

git clone https://github.com/hildjj/node-inspect-extracted.git
git clone https://github.com/nodejs/node.git
cd node-inspect-extracted
npm install -g pnpm
pnpm install
  • npm start to build, run all tests and start an auto-refreshing web server to watch coverage change.
  • npm run check to see if there have been any changes to node that need to be integrated.
  • npm run check -- -d to see the diffs with node
  • npm run check -- -u to indicate that we have merged the current changes

Tests run mostly against the pre-webpack source at the moment, but there are some spot checks for the webpack output.

Supported Node.js versions

This project only supports versions of Node that the Node team is currently supporting. Ava's support statement is what we will be using as well. Currently, that means Node 10+ is required.

LICENSE

This code is an adaptation of the Node.js internal implementation, mostly from the file lib/internal/util/inspect.js, which does not have the Joyent copyright header. The maintainers of this package will not assert copyright over this code, but will assign ownership to the Node.js contributors, with the same license as specified in the Node.js codebase; the portion adapted here should all be plain MIT license.

Tests Coverage Status