双工流的例子
下面说明了一个简单的 Duplex
流的示例,它封装了一个假设的低层源对象,可以向其中写入数据,也可以从中读取数据,尽管使用的 API 与 Node.js 流不兼容。
下面说明了一个简单的 Duplex
流的示例,它缓冲通过 Writable
接口传入的写入数据,然后通过 Readable
接口读回。
const { Duplex } = require('node:stream');
const kSource = Symbol('source');
class MyDuplex extends Duplex {
constructor(source, options) {
super(options);
this[kSource] = source;
}
_write(chunk, encoding, callback) {
// 底层源代码只处理字符串。
if (Buffer.isBuffer(chunk))
chunk = chunk.toString();
this[kSource].writeSomeData(chunk);
callback();
}
_read(size) {
this[kSource].fetchSomeData(size, (data, encoding) => {
this.push(Buffer.from(data, encoding));
});
}
}
Duplex
流最重要的方面是 Readable
和 Writable
端彼此独立运行,尽管它们共存于单个对象实例中。
The following illustrates a simple example of a Duplex
stream that wraps a
hypothetical lower-level source object to which data can be written, and
from which data can be read, albeit using an API that is not compatible with
Node.js streams.
The following illustrates a simple example of a Duplex
stream that buffers
incoming written data via the Writable
interface that is read back out
via the Readable
interface.
const { Duplex } = require('node:stream');
const kSource = Symbol('source');
class MyDuplex extends Duplex {
constructor(source, options) {
super(options);
this[kSource] = source;
}
_write(chunk, encoding, callback) {
// The underlying source only deals with strings.
if (Buffer.isBuffer(chunk))
chunk = chunk.toString();
this[kSource].writeSomeData(chunk);
callback();
}
_read(size) {
this[kSource].fetchSomeData(size, (data, encoding) => {
this.push(Buffer.from(data, encoding));
});
}
}
The most important aspect of a Duplex
stream is that the Readable
and
Writable
sides operate independently of one another despite co-existing within
a single object instance.