如何在 NodeJS 中创建空的(立即完成的)ReadWriteStream?

发布于 2025-01-14 14:33:33 字数 878 浏览 4 评论 0原文

如何创建空的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兲鉂ぱ嘚淚 2025-01-21 14:33:33

您可以使用任何空的双工流,例如虚拟 PassThrough 流,并使用 .end() 手动结束它。

const { PassThrough } = require('stream');

function provideStylesProcessing(
  config: { /* various settings */ }
): () => NodeJS.ReadWriteStream {
    
    if (/* certain conditions */) {
      return () => new PassThrough().end();
    }

    ...
}

You can use any empty duplex stream, for example a dummy PassThrough stream, and end it manually with .end().

const { PassThrough } = require('stream');

function provideStylesProcessing(
  config: { /* various settings */ }
): () => NodeJS.ReadWriteStream {
    
    if (/* certain conditions */) {
      return () => new PassThrough().end();
    }

    ...
}
天荒地未老 2025-01-21 14:33:33

您可以使用 PassThrough 流来执行此操作:

PassThrough 流是一种 Transform 流,它仅传递数据而不对其进行转换。

import { PassThrough } from 'stream';

const xs = new PassThrough({
  objectMode: true
});

xs.end();

xs.on('finish', () => {
  console.log('This stream has ended');
});

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.

import { PassThrough } from 'stream';

const xs = new PassThrough({
  objectMode: true
});

xs.end();

xs.on('finish', () => {
  console.log('This stream has ended');
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文