From f6b3475d85dd49945ee0d560dc232d352ae5918b Mon Sep 17 00:00:00 2001 From: 0hmX Date: Mon, 9 Jun 2025 10:56:18 +0530 Subject: [PATCH 1/3] docs: add example of respecting backpressure in Readable stream --- .../en/learn/modules/backpressuring-in-streams.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/site/pages/en/learn/modules/backpressuring-in-streams.md b/apps/site/pages/en/learn/modules/backpressuring-in-streams.md index 6d7f44869901c..85e5124bf1ab4 100644 --- a/apps/site/pages/en/learn/modules/backpressuring-in-streams.md +++ b/apps/site/pages/en/learn/modules/backpressuring-in-streams.md @@ -557,6 +557,20 @@ class MyReadable extends Readable { } ``` +Here is an example of good practice, where the `Readable` stream respects backpressure by checking the return value of `this.push()`: + +```javascript +class MyReadable extends Readable { + _read(size) { + let chunk; + let canPushMore = true; + while (canPushMore && null !== (chunk = getNextChunk())) { + canPushMore = this.push(chunk); + } + } +} +``` + Additionally, from outside the custom stream, there are pitfalls to ignoring backpressure. In this counter-example of good practice, the application's code forces data through whenever it is available (signaled by the From 3ceb028f54ff03a260bb13cff9d01dab4d5c8d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?0hm=E2=98=98=EF=B8=8F?= <109351887+0hmX@users.noreply.github.com> Date: Wed, 11 Jun 2025 08:28:43 +0530 Subject: [PATCH 2/3] Update apps/site/pages/en/learn/modules/backpressuring-in-streams.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Aviv Keller Signed-off-by: 0hm☘️ <109351887+0hmX@users.noreply.github.com> --- apps/site/pages/en/learn/modules/backpressuring-in-streams.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/modules/backpressuring-in-streams.md b/apps/site/pages/en/learn/modules/backpressuring-in-streams.md index 85e5124bf1ab4..141c32571e5a2 100644 --- a/apps/site/pages/en/learn/modules/backpressuring-in-streams.md +++ b/apps/site/pages/en/learn/modules/backpressuring-in-streams.md @@ -559,7 +559,7 @@ class MyReadable extends Readable { Here is an example of good practice, where the `Readable` stream respects backpressure by checking the return value of `this.push()`: -```javascript +```js class MyReadable extends Readable { _read(size) { let chunk; From f67580e4ad79b62e93d2383e5a0fa369e2e5c3ca Mon Sep 17 00:00:00 2001 From: 0hmX Date: Wed, 11 Jun 2025 08:39:23 +0530 Subject: [PATCH 3/3] fix: correct indentation in MyReadable class example --- apps/site/pages/en/learn/modules/backpressuring-in-streams.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/modules/backpressuring-in-streams.md b/apps/site/pages/en/learn/modules/backpressuring-in-streams.md index 141c32571e5a2..bd783a59e527d 100644 --- a/apps/site/pages/en/learn/modules/backpressuring-in-streams.md +++ b/apps/site/pages/en/learn/modules/backpressuring-in-streams.md @@ -563,7 +563,7 @@ Here is an example of good practice, where the `Readable` stream respects backpr class MyReadable extends Readable { _read(size) { let chunk; - let canPushMore = true; + let canPushMore = true; while (canPushMore && null !== (chunk = getNextChunk())) { canPushMore = this.push(chunk); }