Skip to content

Commit

Permalink
stream: deprecate thenable support
Browse files Browse the repository at this point in the history
Deprecate support for returning thenables in stream
implementation methods. This is causing more confusion
and issues than it's worth, and never was documented.

Refs: nodejs#39535
  • Loading branch information
ronag authored and aduh95 committed Nov 18, 2021
1 parent 340b770 commit 5064fdc
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3019,6 +3019,20 @@ should have the same effect. The receiving end should also check the
[`readable.readableEnded`][] value on [`http.IncomingMessage`][] to get whether
it was an aborted or graceful destroy.

### DEP0157: Thenable support in streams

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/00000
description: Runtime deprecation.
-->

Type: Runtime

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

[Legacy URL API]: url.md#legacy-url-api
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
Symbol,
} = primordials;
const {
emitThenableDeprecationWarning,
kDestroyed,
isDestroyed,
isFinished,
Expand Down Expand Up @@ -110,6 +111,7 @@ function _destroy(self, err, cb) {
if (result != null) {
const then = result.then;
if (typeof then === 'function') {
emitThenableDeprecationWarning();
then.call(
result,
function() {
Expand Down Expand Up @@ -289,6 +291,7 @@ function constructNT(stream) {
if (result != null) {
const then = result.then;
if (typeof then === 'function') {
emitThenableDeprecationWarning();
then.call(
result,
function() {
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const kPaused = Symbol('kPaused');

const { StringDecoder } = require('string_decoder');
const from = require('internal/streams/from');
const { emitThenableDeprecationWarning } = require('internal/streams/utils');

ObjectSetPrototypeOf(Readable.prototype, Stream.prototype);
ObjectSetPrototypeOf(Readable, Stream);
Expand Down Expand Up @@ -497,6 +498,7 @@ Readable.prototype.read = function(n) {
if (result != null) {
const then = result.then;
if (typeof then === 'function') {
emitThenableDeprecationWarning();
then.call(
result,
nop,
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/streams/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const {
ERR_METHOD_NOT_IMPLEMENTED
} = require('internal/errors').codes;
const Duplex = require('internal/streams/duplex');
const { emitThenableDeprecationWarning } = require('internal/streams/utils');
ObjectSetPrototypeOf(Transform.prototype, Duplex.prototype);
ObjectSetPrototypeOf(Transform, Duplex);

Expand Down Expand Up @@ -132,6 +133,7 @@ function final(cb) {
try {
const then = result.then;
if (typeof then === 'function') {
emitThenableDeprecationWarning();
then.call(
result,
(data) => {
Expand Down Expand Up @@ -207,6 +209,7 @@ Transform.prototype._write = function(chunk, encoding, callback) {
try {
const then = result.then;
if (typeof then === 'function') {
emitThenableDeprecationWarning();
then.call(
result,
(val) => {
Expand Down
13 changes: 13 additions & 0 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,20 @@ function isDisturbed(stream) {
));
}

let thenableDeprecationWarningEmitted = false;
function emitThenableDeprecationWarning() {
if (!thenableDeprecationWarningEmitted) {
process.emitWarning(
'Returning a thenable is deprecated, use callbacks instead.',
'DeprecationWarning',
'DEP0157'
);
thenableDeprecationWarningEmitted = true;
}
}

module.exports = {
emitThenableDeprecationWarning,
kDestroyed,
isDisturbed,
kIsDisturbed,
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const EE = require('events');
const Stream = require('internal/streams/legacy').Stream;
const { Buffer } = require('buffer');
const destroyImpl = require('internal/streams/destroy');
const { emitThenableDeprecationWarning } = require('internal/streams/utils');

const {
addAbortSignal,
Expand Down Expand Up @@ -696,6 +697,7 @@ function callFinal(stream, state) {
if (result != null) {
const then = result.then;
if (typeof then === 'function') {
emitThenableDeprecationWarning();
then.call(
result,
function() {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-stream-construct-async-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const {
const { setTimeout } = require('timers/promises');
const assert = require('assert');

common.expectWarning(
'DeprecationWarning',
'Returning a thenable is deprecated, use callbacks instead.',
'DEP0157'
);

{
class Foo extends Duplex {
async _destroy(err, cb) {
Expand Down

0 comments on commit 5064fdc

Please sign in to comment.