如何在 NodeJS 中创建空的(立即完成的)ReadWriteStream?
如何创建空的 NodeJS.ReadWriteStream 它将立即停止?
如果您感兴趣为什么我需要这样做,我正在开发该功能 对于 Gulp,它基本上通过 Gulp.src()< 返回 NodeJS.ReadWriteStream /代码>。 但在某些条件下,任务一定不能执行。伪代码是
function provideStylesProcessing(
config: { /* various settings */ }
): () => NodeJS.ReadWriteStream {
if (/* certain conditions */) {
Return immideately ended NodeJS.ReadWriteStream
}
// Else use Gulp functionality as usual
return (): NodeJS.ReadWriteStream => Gulp.src(entryPointsSourceFilesAbsolutePaths).
pipe(/* ... */);
}
您可能会推荐“您可以返回空的 Promise”。是的,这是一个替代方案, 但我不想使功能复杂化:如果任务是基于steam的,它应该返回 溪流;如果基于回调 - 回调(它包装到函数以允许添加参数)等等。
How to create empty NodeJS.ReadWriteStream which will immideately stopped?
If you are interesting why I need to do this, I am developing the function
for Gulp, which basically returns NodeJS.ReadWriteStream via Gulp.src()
.
But under certain conditions, the task must not be executed. The pseudo code is
function provideStylesProcessing(
config: { /* various settings */ }
): () => NodeJS.ReadWriteStream {
if (/* certain conditions */) {
Return immideately ended NodeJS.ReadWriteStream
}
// Else use Gulp functionality as usual
return (): NodeJS.ReadWriteStream => Gulp.src(entryPointsSourceFilesAbsolutePaths).
pipe(/* ... */);
}
You may reccomend "you can return the empty Promise instead". Yes, this is an alternative,
but I don't want to complicate the function: if task is steam based, it should return the
stream; if callback based - callback (it wrapped to function to allows the adding of parameters) and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用任何空的双工流,例如虚拟
PassThrough
流,并使用.end()
手动结束它。You can use any empty duplex stream, for example a dummy
PassThrough
stream, and end it manually with.end()
.您可以使用 PassThrough 流来执行此操作:
PassThrough 流是一种 Transform 流,它仅传递数据而不对其进行转换。
You can use a PassThrough stream to do this:
A PassThrough stream is a Transform stream that just passes the data through without transforming it.