Skip to content

Commit

Permalink
stream: runtime deprecate Transform._transformState
Browse files Browse the repository at this point in the history
Transform._transformState is removed in future version as part
of a refactoring.

Refs: #32763
Refs: #33105 (comment)

Backport-PR-URL: #33126
PR-URL: #32763
  • Loading branch information
ronag authored and codebytere committed Jun 30, 2020
1 parent 2c568c8 commit db2d1ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
12 changes: 12 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

<a id="DEP0143"></a>
### DEP0143: `Transform._transformState`
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/33126
description: Runtime deprecation.
-->
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
Expand Down
26 changes: 20 additions & 6 deletions lib/_stream_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
'use strict';

const {
ObjectDefineProperty,
ObjectSetPrototypeOf,
Symbol
} = primordials;

module.exports = Transform;
Expand All @@ -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;
Expand Down Expand Up @@ -111,7 +116,7 @@ function Transform(options) {

Duplex.call(this, options);

this._transformState = {
this[kTransformState] = {
afterTransform: afterTransform.bind(this),
needTransform: false,
transforming: false,
Expand Down Expand Up @@ -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);
};

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

0 comments on commit db2d1ca

Please sign in to comment.