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

echo-proxy

v1.12.0

Published

Fun with proxies. Repeats after you.

Downloads

20

Readme

Echo license npm Try echo-proxy on RunKit

Fun with proxies. Do any combination of calls, constructs, and dot-walks starting with Echo, and an approximation of what you typed is logged to the console (with syntax highlighting!) Play with it in your browser on RunKit!

Demo of Echo in action

/* > */ Echo
/* < */ Echo

/* > */ new Echo.foo
/* < */ new Echo.foo

/* > */ new ((new Echo).bar.baz(1, Echo.qux, new Echo(7)))
/* < */ new ((new Echo).bar.baz(1, Echo.qux, new Echo(7)))

/* > */ Echo.gfdgfd[4][' fff '](undefined, true, -Infinity)
/* < */ Echo.gfdgfd[4][' fff '](undefined, true, -Infinity)

/* > */ Echo`foo${[1, 2, 3, 4]}bar`.baz
/* < */ Echo`foo${[1, 2, 3, 4]}bar`.baz

Usage

Requirements: Browsers released after 2018. Node 11 or newer.

RunKit

The easiest way to play with Echo is in your browser on RunKit!

Browser

Another way to use Echo is to paste dist/echo.min.js into your console (note: Echo is harmless, but you should always be careful about pasting code into your console!).

Node

npm install echo-proxy
node --require echo-proxy

You can also import it into your code. Note that Echo attaches itself to the global, so you don't actually want the import to be named Echo.

// in ES module (package.json type=module)
import {} from 'echo-proxy';

// or in script (non-module)
require('echo-proxy');

API

The public API gets special treatment, and is not echoed. This also goes for JS-internal things like __proto__ that lead to weird behavior when messed with. The following builtin functions are overridden to return the stringified Echo:

  • Echo.toString
  • Echo[Symbol.toPrimitive]
  • Echo[Symbol.for('nodejs.util.inspect.custom')]

Echo.render()

Example: const { tokens } = Echo.foo().render()

Returns an object with the following shape:

{
  tokens: {value: string, type: string}[] -- The output broken up into typed "tokens," which is what Echo uses to do syntax highlighting.
  plaintext: string -- The tokens strung together without any syntax highlighting.
  formatted: string[] -- The tokens with syntax highlighting, formatted as arguments to console.log.
  theme: { [tokenType]: [colorString] } -- The "theme" object that maps "token types" to either ANSI colors or hsl colors (or null if options.colorMode='off').
}

Echo.print()

Example: const value = Echo.foo(); value.print();

Logs the value to the console.

Echo.then(...)

Example: const { tokens } = await Echo.foo()

Resolves to the value of Echo.render().

Echo.options

Example: Echo.options.theme = 'firefox'

Can be used to configure Echo. This includes options to make it reflect how you would write code (for example, to always use parentheses with a constructor), and options that affect its behavior in other ways.

| Property | Type/Options | Default | Explanation | | -------- | ------- | ------- | ----------- | | bracketNotationOptional | Boolean | true | When true, use heuristics to decide when to use dot notation. Otherwise, always use square-bracket notation. | | parensOptional | Boolean | true | When true, use heuristics to decide whether to wrap sub-expressions in parentheses. When false, always wrap them. | | constructorParensOptional | Boolean | true | When true, drop parentheses for constructors with no arguments. | | stringDelimiter | String | "'" | What character is used to delimit (bookend) strings, and is escaped from strings. If the string contains a newline, this is ignored and the string is rendered as a template string. | | colorMode | "browser", "ansi", "off" | [auto detected] | Enable or disable syntax highlighting and the mode used to set colors. | | theme | "chrome", "firefox", Object | [auto detected] | Which DevTools' syntax highlighting theme to use. Defaults to Chrome unless you're using Firefox. Can also be a custom theme object whose keys are TokenTypes and whose values are ANSI color codes or CSS styles for the given token type (depending on colorMode). | | autoLog | Boolean | true | When true, automatically console.log the output. See below. | | autoLogMinLength | Number | 1 | The minimum number of tokens required to log to console. You can use this to avoid printing in some autocomplete situations. See below. |

The Auto-Logging Problem

When Echo.options.autoLog=true, the output is automatically logged to the console. If you're in an environment with autocomplete (like Chrome console), this mode may cause Echo to be logged several times as you type.

A little explanation of the way Echo's auto-logging works: it never knows if the code has finished evaluating and if it's time to print the output. Here are a couple examples to illustrate the problem:

  • Consider the expression Echo.foo.bar. The sub-expression Echo.foo is completely evaluated before it looks for .bar. While this sub-expression is evaluating, all Echo knows is that the user tried to access Echo.foo, it can never tell if that getter is the last one in the chain or if there are more to come.
  • Consider the expression Echo(Echo.foo). We don't want to print the inner Echo on its own line, we want them both to appear in one line.

Echo circumvents this by using setTimeout to wait 1 tick. All the various Echoes in your code calculate their outputs individually, and then the longest output is printed.

This timeout process is skipped when you manually call Echo.render() or Echo.print(). Instead of waiting a tick and logging, everything is handled immediately.

So that's how auto-logging works. So why does your browser sometimes cause it to print while you're typing? This happens when your browser tries to inspect the Echo object to populate the console's autocomplete/intellisense system. From Echo's perspective, this snooping looks the same as accessing values on it, so it tries to render the output.

To mitigate the extra rendering, we have Echo.options.autoLogMinLength. This says that any Echo output less than minLength items long shouldn't be printed.