Node: Support util.inspect.custom as a public symbol

Created on 18 May 2018  路  8Comments  路  Source: nodejs/node

  • Version: v10.1.0
  • Platform: Linux (Arch)
  • Subsystem: util

Please consider supporting util.inspect.custom as a public/global symbol either by:

1) changing util.inspect.custom from a private symbol (e.g. Symbol('util.inspect.custom')) to a public symbol (e.g. Symbol.for('util.inspect.custom'))

or:

2) accepting a public symbol as an alternative to the private symbol, just as "inspect" is currently accepted as a (deprecated) fallback for the symbol

Changing it from a string to a symbol was a great idea :tada: and it's a perfect use case for a symbol, but, as mentioned here, making it private makes it painful to write code that both a) provides an inspect hook if it's loaded in node and b) works seamlessly in the browser if it's not.

A workaround has been implemented as an NPM module, inspect-custom-symbol (cc @mafintosh), which uses the browser field in its package.json to provide a substitute symbol without pulling in the util library in bundlers such as Browserify and Webpack. It's much better than trying to work around this with a tower of late-bound typeof util.inspect.custom checks, but it doesn't fix the underlying issue and it's not ideal:

  • it doesn't work in all situations/environments
  • it requires an extra dependency
  • most people with this dilemma won't be aware of it
feature request util

Most helpful comment

I think the first option would be okay. Do you want to open a PR with it?

All 8 comments

I think the first option would be okay. Do you want to open a PR with it?

@addaleax Sure!

I like the idea of the first option!

I'm also in favor of the first option, though I am not sure to see if this would harm node's core as a side effect.

First option sounds good to me as well.

/ping @jakearchibald

Using a global name kinda gives us the same problem we had the in the first place, it's just that the name is longer so less likely to clash with existing code.

However, it's probably the best thing to do for multi-realm environments like the browser.

Yeah running into this now. Unfortunately all my proxies just print like this:

Set:  0 {}
Changes:  0
true
Set:  1 {}
Changes:  1
true
Set:  2 {}
Changes:  2
true
Set:  3 {}
Changes:  3
true

Since this lib should work everywhere, I dont want to import util, but would definitely like it to print the appropriate value! Glad to see progress is being made here. Only solution I could come up with makes me sad :( lol

// in the proxy get 
if (typeof key === 'symbol' && String(key) === 'Symbol(util.inspect.custom)') {
    // super hacky method of supporting nodejs printing values
    return () => descriptor.copy || descriptor.base;
  }

( Happy to hear of another solution if there is one :) )

Thanks for your hard work everyone!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cong88 picture cong88  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments

seishun picture seishun  路  3Comments

mcollina picture mcollina  路  3Comments

srl295 picture srl295  路  3Comments