node/lib/internal/streams/lazy_transform.js
Robert Nagy 9cbf6af5b5 crypto: fix performance regression
e559842188
made writable/readable computed with a legacy mode if the properties
are written to.

LazyTransform still unecessarily wrote to these properties causing a
performance regression.

Fixes: https://github.com/nodejs/node/issues/31739

PR-URL: https://github.com/nodejs/node/pull/31742
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2020-02-13 22:50:56 +01:00

70 lines
1.6 KiB
JavaScript

// LazyTransform is a special type of Transform stream that is lazily loaded.
// This is used for performance with bi-API-ship: when two APIs are available
// for the stream, one conventional and one non-conventional.
'use strict';
const {
ObjectDefineProperties,
ObjectDefineProperty,
ObjectSetPrototypeOf,
} = primordials;
const stream = require('stream');
const {
getDefaultEncoding
} = require('internal/crypto/util');
module.exports = LazyTransform;
function LazyTransform(options) {
this._options = options;
}
ObjectSetPrototypeOf(LazyTransform.prototype, stream.Transform.prototype);
ObjectSetPrototypeOf(LazyTransform, stream.Transform);
function makeGetter(name) {
return function() {
stream.Transform.call(this, this._options);
this._writableState.decodeStrings = false;
if (!this._options || !this._options.defaultEncoding) {
this._writableState.defaultEncoding = getDefaultEncoding();
}
return this[name];
};
}
function makeSetter(name) {
return function(val) {
ObjectDefineProperty(this, name, {
value: val,
enumerable: true,
configurable: true,
writable: true
});
};
}
ObjectDefineProperties(LazyTransform.prototype, {
_readableState: {
get: makeGetter('_readableState'),
set: makeSetter('_readableState'),
configurable: true,
enumerable: true
},
_writableState: {
get: makeGetter('_writableState'),
set: makeSetter('_writableState'),
configurable: true,
enumerable: true
},
_transformState: {
get: makeGetter('_transformState'),
set: makeSetter('_transformState'),
configurable: true,
enumerable: true
}
});