Skip to content

Commit 58e645d

Browse files
authored
stream: remove thenable support
Remove support for returning thenables in stream implementation methods. This is causing more confusion and issues than it's worth. Refs: #39535 PR-URL: #40773 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent d36b60e commit 58e645d

File tree

6 files changed

+10
-360
lines changed

6 files changed

+10
-360
lines changed

doc/api/deprecations.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3019,14 +3019,17 @@ it was an aborted or graceful destroy.
30193019

30203020
<!-- YAML
30213021
changes:
3022+
- version: REPLACEME
3023+
pr-url: https://github.com/nodejs/node/pull/40773
3024+
description: End-of-life.
30223025
- version:
30233026
- v17.2.0
30243027
- v16.14.0
30253028
pr-url: https://github.com/nodejs/node/pull/40860
30263029
description: Documentation-only deprecation.
30273030
-->
30283031

3029-
Type: Documentation-only
3032+
Type: End-of-Life
30303033

30313034
An undocumented feature of Node.js streams was to support thenables in
30323035
implementation methods. This is now deprecated, use callbacks instead and avoid

lib/internal/streams/destroy.js

+2-32
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,7 @@ function _destroy(self, err, cb) {
106106
}
107107
}
108108
try {
109-
const result = self._destroy(err || null, onDestroy);
110-
if (result != null) {
111-
const then = result.then;
112-
if (typeof then === 'function') {
113-
then.call(
114-
result,
115-
function() {
116-
process.nextTick(onDestroy, null);
117-
},
118-
function(err) {
119-
process.nextTick(onDestroy, err);
120-
});
121-
}
122-
}
109+
self._destroy(err || null, onDestroy);
123110
} catch (err) {
124111
onDestroy(err);
125112
}
@@ -285,24 +272,7 @@ function constructNT(stream) {
285272
}
286273

287274
try {
288-
const result = stream._construct(onConstruct);
289-
if (result != null) {
290-
const then = result.then;
291-
if (typeof then === 'function') {
292-
then.call(
293-
result,
294-
function() {
295-
if (!called) {
296-
process.nextTick(onConstruct, null);
297-
}
298-
},
299-
function(err) {
300-
if (!called) {
301-
process.nextTick(onConstruct, err);
302-
}
303-
});
304-
}
305-
}
275+
stream._construct(onConstruct);
306276
} catch (err) {
307277
onConstruct(err);
308278
}

lib/internal/streams/readable.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -493,18 +493,7 @@ Readable.prototype.read = function(n) {
493493

494494
// Call internal read method
495495
try {
496-
const result = this._read(state.highWaterMark);
497-
if (result != null) {
498-
const then = result.then;
499-
if (typeof then === 'function') {
500-
then.call(
501-
result,
502-
nop,
503-
function(err) {
504-
errorOrDestroy(this, err);
505-
});
506-
}
507-
}
496+
this._read(state.highWaterMark);
508497
} catch (err) {
509498
errorOrDestroy(this, err);
510499
}

lib/internal/streams/transform.js

+2-65
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ function Transform(options) {
107107
}
108108

109109
function final(cb) {
110-
let called = false;
111110
if (typeof this._flush === 'function' && !this.destroyed) {
112-
const result = this._flush((er, data) => {
113-
called = true;
111+
this._flush((er, data) => {
114112
if (er) {
115113
if (cb) {
116114
cb(er);
@@ -128,33 +126,6 @@ function final(cb) {
128126
cb();
129127
}
130128
});
131-
if (result !== undefined && result !== null) {
132-
try {
133-
const then = result.then;
134-
if (typeof then === 'function') {
135-
then.call(
136-
result,
137-
(data) => {
138-
if (called)
139-
return;
140-
if (data != null)
141-
this.push(data);
142-
this.push(null);
143-
if (cb)
144-
process.nextTick(cb);
145-
},
146-
(err) => {
147-
if (cb) {
148-
process.nextTick(cb, err);
149-
} else {
150-
process.nextTick(() => this.destroy(err));
151-
}
152-
});
153-
}
154-
} catch (err) {
155-
process.nextTick(() => this.destroy(err));
156-
}
157-
}
158129
} else {
159130
this.push(null);
160131
if (cb) {
@@ -180,9 +151,7 @@ Transform.prototype._write = function(chunk, encoding, callback) {
180151
const wState = this._writableState;
181152
const length = rState.length;
182153

183-
let called = false;
184-
const result = this._transform(chunk, encoding, (err, val) => {
185-
called = true;
154+
this._transform(chunk, encoding, (err, val) => {
186155
if (err) {
187156
callback(err);
188157
return;
@@ -204,38 +173,6 @@ Transform.prototype._write = function(chunk, encoding, callback) {
204173
this[kCallback] = callback;
205174
}
206175
});
207-
if (result !== undefined && result != null) {
208-
try {
209-
const then = result.then;
210-
if (typeof then === 'function') {
211-
then.call(
212-
result,
213-
(val) => {
214-
if (called)
215-
return;
216-
217-
if (val != null) {
218-
this.push(val);
219-
}
220-
221-
if (
222-
wState.ended ||
223-
length === rState.length ||
224-
rState.length < rState.highWaterMark ||
225-
rState.length === 0) {
226-
process.nextTick(callback);
227-
} else {
228-
this[kCallback] = callback;
229-
}
230-
},
231-
(err) => {
232-
process.nextTick(callback, err);
233-
});
234-
}
235-
} catch (err) {
236-
process.nextTick(callback, err);
237-
}
238-
}
239176
};
240177

241178
Transform.prototype._read = function() {

lib/internal/streams/writable.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -693,24 +693,7 @@ function callFinal(stream, state) {
693693
state.pendingcb++;
694694

695695
try {
696-
const result = stream._final(onFinish);
697-
if (result != null) {
698-
const then = result.then;
699-
if (typeof then === 'function') {
700-
then.call(
701-
result,
702-
function() {
703-
if (!called) {
704-
process.nextTick(onFinish, null);
705-
}
706-
},
707-
function(err) {
708-
if (!called) {
709-
process.nextTick(onFinish, err);
710-
}
711-
});
712-
}
713-
}
696+
stream._final(onFinish);
714697
} catch (err) {
715698
onFinish(err);
716699
}

0 commit comments

Comments
 (0)