diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index a224b3cd4e9966..e0c28de88a0066 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2695,6 +2695,18 @@ The `repl` module exports a `_builtinLibs` property that contains an array with native modules. It was incomplete so far and instead it's better to rely upon `require('module').builtinModules`. + +### DEP0143: `Transform._transformState` + +Type: Runtime +`Transform._transformState` will be removed in future versions where it is +no longer required due to simplification of the implementation. + [`--pending-deprecation`]: cli.html#cli_pending_deprecation [`--throw-deprecation`]: cli.html#cli_throw_deprecation [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js index 7bfbb04091fd5d..9ea35194d486d8 100644 --- a/lib/_stream_transform.js +++ b/lib/_stream_transform.js @@ -64,7 +64,9 @@ 'use strict'; const { + ObjectDefineProperty, ObjectSetPrototypeOf, + Symbol } = primordials; module.exports = Transform; @@ -75,12 +77,15 @@ const { ERR_TRANSFORM_WITH_LENGTH_0 } = require('internal/errors').codes; const Duplex = require('_stream_duplex'); +const internalUtil = require('internal/util'); + ObjectSetPrototypeOf(Transform.prototype, Duplex.prototype); ObjectSetPrototypeOf(Transform, Duplex); +const kTransformState = Symbol('kTransformState'); function afterTransform(er, data) { - const ts = this._transformState; + const ts = this[kTransformState]; ts.transforming = false; const cb = ts.writecb; @@ -111,7 +116,7 @@ function Transform(options) { Duplex.call(this, options); - this._transformState = { + this[kTransformState] = { afterTransform: afterTransform.bind(this), needTransform: false, transforming: false, @@ -147,8 +152,17 @@ function prefinish() { } } +ObjectDefineProperty(Transform.prototype, '_transformState', { + get: internalUtil.deprecate(function() { + return this[kTransformState]; + }, 'Transform.prototype._transformState is deprecated', 'DEP0143'), + set: internalUtil.deprecate(function(val) { + this[kTransformState] = val; + }, 'Transform.prototype._transformState is deprecated', 'DEP0143') +}); + Transform.prototype.push = function(chunk, encoding) { - this._transformState.needTransform = false; + this[kTransformState].needTransform = false; return Duplex.prototype.push.call(this, chunk, encoding); }; @@ -167,7 +181,7 @@ Transform.prototype._transform = function(chunk, encoding, cb) { }; Transform.prototype._write = function(chunk, encoding, cb) { - const ts = this._transformState; + const ts = this[kTransformState]; ts.writecb = cb; ts.writechunk = chunk; ts.writeencoding = encoding; @@ -184,7 +198,7 @@ Transform.prototype._write = function(chunk, encoding, cb) { // _transform does all the work. // That we got here means that the readable side wants more data. Transform.prototype._read = function(n) { - const ts = this._transformState; + const ts = this[kTransformState]; if (ts.writechunk !== null && !ts.transforming) { ts.transforming = true; @@ -215,7 +229,7 @@ function done(stream, er, data) { if (stream._writableState.length) throw new ERR_TRANSFORM_WITH_LENGTH_0(); - if (stream._transformState.transforming) + if (stream[kTransformState].transforming) throw new ERR_TRANSFORM_ALREADY_TRANSFORMING(); return stream.push(null); }