Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: deprecate thenable support #40860

Merged
merged 6 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
ronag marked this conversation as resolved.
Show resolved Hide resolved

aduh95 marked this conversation as resolved.
Show resolved Hide resolved
[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