NodeJS:"through2" 的实际原生替代品,用于在 2021 年干预 Steam
NodeJS: Actual native substitute for "through2" for intervention to steams in 2021
来自 through2 文档:
Do you need this?
Since Node.js introduced Simplified Stream Construction, many uses of
through2 have become redundant. Consider whether you really need to
use through2 or just want to use the 'readable-stream' package, or the
core 'stream' package (which is derived from 'readable-stream').
如果我没理解错的话,现在(从2021年开始)我们可以在没有第三方库的情况下干预流。
我没有在 Stream documentation.
中找到如何做与 through2
相同的事情
// ...
.pipe(through2(function (file, encoding, callback) {
// Do something with file ...
callback(null, file)
}))
// ↑ Possible to reach the same effect natively (with core packages)?
我想,对于 2021 年,必须有一些方法支持 async/await 语法:
// ...
.pipe(newFeatureOfModernNodeJS(async function (file) {
await doSomethingAsyncWithFile(file);
// on fail - same effect as "callback(new Error('...'))" of trough2
return file; // same effect as "callback(null, file)" of trough2
// or
return null; // same effect as `callback()` of trough2
}))
// ↑ Possible to reach the same effect natively (with core packages)?
您正在寻找的可能是转换流,它是由 Node.js 附带的本机 'stream' 库实现的。我不知道还有异步兼容版本,但肯定有基于回调的版本。你需要继承原生的Transform流并实现你的功能
这是我喜欢使用的样板文件:
const Transform = require('stream').Transform;
const util = require('util');
function TransformStream(transformFunction) {
// Set the objectMode flag here if you're planning to iterate through a set of objects rather than bytes
Transform.call(this, { objectMode: true });
this.transformFunction = transformFunction;
}
util.inherits(TransformStream, Transform);
TransformStream.prototype._transform = function(obj, enc, done) {
return this.transformFunction(this, obj, done);
};
module.exports = TransformStream;
现在您可以在您要使用的地方使用它:
const TransformStream = require('path/to/myTransformStream.js');
//...
.pipe(new TransformStream((function (file, encoding, callback) {
// Do something with file ...
callback(null, file)
}))
来自 through2 文档:
Do you need this?
Since Node.js introduced Simplified Stream Construction, many uses of through2 have become redundant. Consider whether you really need to use through2 or just want to use the 'readable-stream' package, or the core 'stream' package (which is derived from 'readable-stream').
如果我没理解错的话,现在(从2021年开始)我们可以在没有第三方库的情况下干预流。 我没有在 Stream documentation.
中找到如何做与through2
相同的事情
// ...
.pipe(through2(function (file, encoding, callback) {
// Do something with file ...
callback(null, file)
}))
// ↑ Possible to reach the same effect natively (with core packages)?
我想,对于 2021 年,必须有一些方法支持 async/await 语法:
// ...
.pipe(newFeatureOfModernNodeJS(async function (file) {
await doSomethingAsyncWithFile(file);
// on fail - same effect as "callback(new Error('...'))" of trough2
return file; // same effect as "callback(null, file)" of trough2
// or
return null; // same effect as `callback()` of trough2
}))
// ↑ Possible to reach the same effect natively (with core packages)?
您正在寻找的可能是转换流,它是由 Node.js 附带的本机 'stream' 库实现的。我不知道还有异步兼容版本,但肯定有基于回调的版本。你需要继承原生的Transform流并实现你的功能
这是我喜欢使用的样板文件:
const Transform = require('stream').Transform;
const util = require('util');
function TransformStream(transformFunction) {
// Set the objectMode flag here if you're planning to iterate through a set of objects rather than bytes
Transform.call(this, { objectMode: true });
this.transformFunction = transformFunction;
}
util.inherits(TransformStream, Transform);
TransformStream.prototype._transform = function(obj, enc, done) {
return this.transformFunction(this, obj, done);
};
module.exports = TransformStream;
现在您可以在您要使用的地方使用它:
const TransformStream = require('path/to/myTransformStream.js');
//...
.pipe(new TransformStream((function (file, encoding, callback) {
// Do something with file ...
callback(null, file)
}))