可写流示例
【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();
}
}
}