toWritable(writer)


稳定性: 1 - 实验性

  • writer <Object> 一个流/迭代写入器。仅需要 write() 方法;end()fail()writeSync()writevSync()endSync()writev() 是可选的。
  • 返回:<stream.Writable>

创建一个由流/迭代 Writer 支持的经典 stream.Writable

🌐 Creates a classic stream.Writable backed by a stream/iter Writer.

每个 _write() / _writev() 调用首先尝试 Writer 的同步方法(writeSync / writevSync),如果同步路径返回 false 或抛出异常,则退回到异步方法。类似地,_final() 会在 end() 之前尝试 endSync()。当同步路径成功时,回调通过 queueMicrotask 延迟执行,以保持异步解析约定。

🌐 Each _write() / _writev() call attempts the Writer's synchronous method first (writeSync / writevSync), falling back to the async method if the sync path returns false or throws. Similarly, _final() tries endSync() before end(). When the sync path succeeds, the callback is deferred via queueMicrotask to preserve the async resolution contract.

Writable 的 highWaterMark 被设置为 Number.MAX_SAFE_INTEGER,以有效地禁用其内部缓冲,使底层的 Writer 能够直接管理背压。

🌐 The Writable's highWaterMark is set to Number.MAX_SAFE_INTEGER to effectively disable its internal buffering, allowing the underlying Writer to manage backpressure directly.

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

const { writer, readable } = push();
const writable = toWritable(writer);

writable.write('hello');
writable.end();const { push, toWritable } = require('node:stream/iter');

const { writer, readable } = push();
const writable = toWritable(writer);

writable.write('hello');
writable.end();