-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
lib: hide lots of internal properties #23126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,16 +26,20 @@ const { normalizeEncoding } = require('internal/util'); | |
| const { isArrayBufferView } = require('internal/util/types'); | ||
| const LazyTransform = require('internal/streams/lazy_transform'); | ||
| const kState = Symbol('kState'); | ||
| const kFinalized = Symbol('kFinalized'); | ||
|
|
||
| function Hash(algorithm, options) { | ||
| if (!(this instanceof Hash)) | ||
| return new Hash(algorithm, options); | ||
| validateString(algorithm, 'algorithm'); | ||
| this[kHandle] = new _Hash(algorithm); | ||
| this[kState] = { | ||
| [kFinalized]: false | ||
| }; | ||
| Object.defineProperty(this, kState, { | ||
| value: { | ||
| finalized: false | ||
| }, | ||
| configurable: true, | ||
| writable: true, | ||
| enumerable: false | ||
| }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pretty cheap entry and useful as debugging information, I’d keep it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is always possible to view the entry when using But I don't have a strong opinion about it and if you think it's best to be visible for everyone, I'll change it back.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What I meant, was: A lot of the information being hidden here is useful not only to Node.js core developers, even if we don’t encourage people to access it. That’s the point. :)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May I ask in what way this information is valuable if it should not be accessed?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Being able to inspect things for debugging, and to access them programmatically are pretty different things and I wouldn’t compare them? |
||
| LazyTransform.call(this, options); | ||
| } | ||
|
|
||
|
|
@@ -53,7 +57,7 @@ Hash.prototype._flush = function _flush(callback) { | |
|
|
||
| Hash.prototype.update = function update(data, encoding) { | ||
| const state = this[kState]; | ||
| if (state[kFinalized]) | ||
| if (state.finalized) | ||
| throw new ERR_CRYPTO_HASH_FINALIZED(); | ||
|
|
||
| if (typeof data !== 'string' && !isArrayBufferView(data)) { | ||
|
|
@@ -69,15 +73,15 @@ Hash.prototype.update = function update(data, encoding) { | |
|
|
||
| Hash.prototype.digest = function digest(outputEncoding) { | ||
| const state = this[kState]; | ||
| if (state[kFinalized]) | ||
| if (state.finalized) | ||
| throw new ERR_CRYPTO_HASH_FINALIZED(); | ||
| outputEncoding = outputEncoding || getDefaultEncoding(); | ||
| if (normalizeEncoding(outputEncoding) === 'utf16le') | ||
| throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16(); | ||
|
|
||
| // Explicit conversion for backward compatibility. | ||
| const ret = this[kHandle].digest(`${outputEncoding}`); | ||
| state[kFinalized] = true; | ||
| state.finalized = true; | ||
| return ret; | ||
| }; | ||
|
|
||
|
|
@@ -94,9 +98,14 @@ function Hmac(hmac, key, options) { | |
| } | ||
| this[kHandle] = new _Hmac(); | ||
| this[kHandle].init(hmac, toBuf(key)); | ||
| this[kState] = { | ||
| [kFinalized]: false | ||
| }; | ||
| Object.defineProperty(this, kState, { | ||
| value: { | ||
| finalized: false | ||
| }, | ||
| configurable: true, | ||
| writable: true, | ||
| enumerable: false | ||
| }); | ||
| LazyTransform.call(this, options); | ||
| } | ||
|
|
||
|
|
@@ -110,14 +119,14 @@ Hmac.prototype.digest = function digest(outputEncoding) { | |
| if (normalizeEncoding(outputEncoding) === 'utf16le') | ||
| throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16(); | ||
|
|
||
| if (state[kFinalized]) { | ||
| if (state.finalized) { | ||
| const buf = Buffer.from(''); | ||
| return outputEncoding === 'buffer' ? buf : buf.toString(outputEncoding); | ||
| } | ||
|
|
||
| // Explicit conversion for backward compatibility. | ||
| const ret = this[kHandle].digest(`${outputEncoding}`); | ||
| state[kFinalized] = true; | ||
| state.finalized = true; | ||
| return ret; | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,8 +33,20 @@ function StatWatcher(bigint) { | |
| EventEmitter.call(this); | ||
|
|
||
| this._handle = null; | ||
| this[kOldStatus] = -1; | ||
| this[kUseBigint] = bigint; | ||
| Object.defineProperties(this, { | ||
| [kOldStatus]: { | ||
| value: -1, | ||
| configurable: true, | ||
| writable: true, | ||
| enumerable: false | ||
| }, | ||
| [kUseBigint]: { | ||
| value: bigint, | ||
| configurable: true, | ||
| writable: true, | ||
| enumerable: false | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess my main issue with these kinds of simple properties that are now hidden is that there’s no way to tell their state from the default
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well... I am not really sure what to say here. The whole reason for this PR to do exactly that: hide these properties from regular inspection while still keeping the possible to inspect them if done explicitly by using |
||
| }); | ||
| } | ||
| util.inherits(StatWatcher, EventEmitter); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be worthwhile to put these into a helper and then document about
util.inspect.defaultOptionsin the definition of that helper because to understand what those properties are one has to look into the source code anywayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall I do this in this specific PR or is that fine for a separate one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be in this PR.