🌐 Block

块模式将插槽限制为 highWaterMark,但不限制待处理写入队列。等待中的写入会阻塞,直到消费者腾出空间,就像严格模式一样。区别在于,未等待的写入会默默地永久排队,而不是抛出错误——如果生产者忘记 await,可能会导致内存泄漏。

🌐 Block mode caps slots at highWaterMark but places no limit on the pending writes queue. Awaited writes block until the consumer makes room, just like strict mode. The difference is that unawaited writes silently queue forever instead of throwing -- a potential memory leak if the producer forgets to await.

这是现有 Node.js 经典流和 Web 流默认使用的模式。当你控制生产者并且知道它会正确等待时,或者在从这些 API 迁移代码时使用它。

🌐 This is the mode that existing Node.js classic streams and Web Streams default to. Use it when you control the producer and know it awaits properly, or when migrating code from those APIs.

import { push, text } from 'node:stream/iter';

const { writer, readable } = push({
  highWaterMark: 16,
  backpressure: 'block',
});

const consuming = text(readable);

// Safe -- awaited writes block until the consumer reads.
for (const item of dataset) {
  await writer.write(item);
}
await writer.end();
console.log(await consuming);const { push, text } = require('node:stream/iter');

async function run() {
  const { writer, readable } = push({
    highWaterMark: 16,
    backpressure: 'block',
  });

  const consuming = text(readable);

  // Safe -- awaited writes block until the consumer reads.
  for (const item of dataset) {
    await writer.write(item);
  }
  await writer.end();
  console.log(await consuming);
}

run().catch(console.error);