NodeJS中可读的concat流?

时间:2019-06-30 21:57:17

标签: javascript node.js

我有一些流通过管道传输,有一次我收到许多缓冲区 并希望将它们连接在一起。因此,我尝试使用concat-stream包,但是它是不可读的流,并且在此之后无法再次执行管道操作。这是我要实现的代码:

const concat = require('concat-stream');

request({ url, ...options })
    .pipe(zlib.createGunzip())
    .pipe(concat((buffer) => {
        buffer.toString('utf-8');
    }))
    .pipe(smtaStream);

还有其他方法或软件包可以让我做到这一点吗?

1 个答案:

答案 0 :(得分:0)

contact-stream会将流中的所有数据收集到单个缓冲区中。这意味着您所有的数据都在内存中。因此,您不必再使用流。如果仍要使用流,则应将字符串更改为流,如下所示:

const Readable = require('stream').Readable;
const concat = require('concat-stream');

request({ url, ...options })
  .pipe(zlib.createGunzip())
  .pipe(concat((buffer) => {
    const rs = new Readable();
    rs.push(buffer.toString('utf-8'));
    rs.push(null);
    rs.pipe(smtaStream);
  }));