对此流可读语法有些困惑 [英] A little confused on this stream readable syntax

查看:45
本文介绍了对此流可读语法有些困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了流手册,他们有这个示例:

I read through the stream handbook and they had this example:

var Readable = require('stream').Readable;

var rs = new Readable;
rs.push('beep ');
rs.push('boop\n');
rs.push(null);

rs.pipe(process.stdout);

看起来确实正是我所需要的(将对象推送到可读流中,然后将其通过管道写入可写内容.)

It looks like exactly what I needed (push objects into a readable stream and pipe it to a writable.)

这就是我想出的

var Readable = require('stream').Readable;
var rs = new Readable;

app.get('/:foo', function(req, res) {
oboe(fs.createReadStream("/file"))
    .node( "{}", function(data) { 
          rs.push(data) 
     })
rs.pipe(res);

我正在使用双簧管在fsStream上侦听对象并对其进行修改.目前,我正在将新发现的对象推入数组,然后在完成"流事件中,我告诉Express.res.json新创建的数组.这占用了太多的内存,我想知道是否可以在找到并修改对象时将流清空,将其推入可读文件,而当可读流获取数据时,它将通过管道将其传给res,但仍然维护一个对象.

I am using oboe to listen on the fsStream for objects and modifying them. Currently I am pushing the newly found objects into an array and then on "done" stream event I would tell express to res.json the newly created array. This got too memory heavy and i was wondering if I could just empty the stream as the objects are found and modified, push it into a readable and as the readable stream gets data, it would pipe it out to res but still maintain one object.

但是我收到此错误

Error: not implemented at Readable._read

这是否意味着信息流手册已过期?

Does this mean the stream handbook is out of date?

推荐答案

您不应直接使用 Readable .您应该将其子类化,并编写自己的 _read 实现;这就是为什么节点抱怨未实现 Readable._read 的原因.另外,节点流需要 String s或 Buffer s,而不是 JSON Object s,因此需要从双簧管的<流中的code> node 事件可能会引起问题.

You're not supposed to use Readable directly. You're supposed to subclass it and write your own implementation of _read; that's why node is complaining that Readable._read is not implemented. Also, node streams want Strings or Buffers, not JSON Objects, so writing data from oboe's node event to a stream will probably cause problems.

我认为比这里使用 Readable 流更简单的解决方案.听起来好像您想获取双簧管为您提供的 JSON对象,对其进行更改,然后将其流式传输到express的 res 对象. res 本身是一个 Writable 流,因此您无需遍历中间流,只需对其进行 write 次即可直到完成为止.请注意,当双簧管完成时,您还必须在响应流( res )上调用 end().

I think there's an easier solution than using a Readable stream here. It sounds like you want to take the JSON Object that oboe is giving you, change it, and stream it out express's res object. res itself is a Writable stream so, instead of going through an intermediary stream, you can simply write to it as many times as you'd like until you're done. Be aware though that you'll also have to call end() on the response stream (res) when when oboe is finished.

app.get('/:foo', function(req, res) {
    oboe(fs.createReadStream("/file"))
        .node( "{}", function(data) { 
            // ... transform data however you need ...
            res.write(JSON.stringify(data)); // Note that data is JSON.stringified here since res.write wants a String or a Buffer, not an Object
        })
        .done(function() {
            res.end();
        })
})

注意: 我假设您在响应的另一端可以处理这样的JSON对象流.如果您还没有双簧管,那么您可能还需要像双簧管这样的东西.

这篇关于对此流可读语法有些困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆