可写流示例
¥An example writable stream
下面说明了一个相当简单(并且有些毫无意义)的自定义 Writable
流实现。虽然这个特定的 Writable
流实例没有任何真正特别的用处,但该示例说明了自定义 Writable
流实例的每个必需元素:
¥The following illustrates a rather simplistic (and somewhat pointless) custom
Writable
stream implementation. While this specific Writable
stream instance
is not of any real particular usefulness, the example illustrates each of the
required elements of a custom Writable
stream instance:
const { Writable } = require('node:stream');
class MyWritable extends Writable {
_write(chunk, encoding, callback) {
if (chunk.toString().indexOf('a') >= 0) {
callback(new Error('chunk is invalid'));
} else {
callback();
}
}
}