[英]Piping/streaming JavaScript objects in Node.js
I'm trying to wrap my head around Node.js streams, not that I'm pretty new to JavaScript and node, the last languages I really got were Perl and PHP :-D 我试图绕过Node.js流,而不是我对JavaScript和节点很新,我真正得到的最后一种语言是Perl和PHP :-D
I've read the Buffer/Streams documentation @ nodejs.org, watched James Halliday @ LXJS , read his stream-handbook and Thorsten Lorenz event-stream post . 我已经阅读了Buffer / Streams文档@ nodejs.org,观看了James Halliday @ LXJS ,阅读了他的流手册和Thorsten Lorenz 事件流帖子 。 I start to understand the basics :)
我开始了解基础知识:)
I process data which is serialized in RDF (which is neither JSON nor XML). 我处理在RDF中序列化的数据(既不是JSON也不是XML)。 I manage to fetch the data (in real code via request) and parse it into a JS object using
rdfstore
module. 我设法获取数据(通过请求在实际代码中)并使用
rdfstore
模块将其解析为JS对象。
So far I do this: 到目前为止我这样做:
s.createReadStream('myRDFdata.ttl').pipe(serialize()).pipe(process.stdout);
Where serialize()
does the job of parsing an serializing the code at the same time right now. 其中
serialize()
完成了同时解析序列化代码的工作。 I use through
module to interface to the stream. 我使用
through
模块来连接到流。
Now I have some more methods (not the real function declaration but I hope you get the point): 现在我有更多的方法(不是真正的函数声明,但我希望你明白这一点):
getRecipe(parsedRDF)
-> takes the parsed RDF (as a JavaScript object) and tells me how to use it getRecipe(parsedRDF)
- >获取解析后的RDF(作为JavaScript对象)并告诉我如何使用它 createMeal(parsedRDF, recipe)
-> takes the parsed RDF and the recipe from above and creates a new RDF object out of it createMeal(parsedRDF, recipe)
- >从上面获取解析后的RDF和配方,并从中创建一个新的RDF对象 getRecipe
will have to do a user interaction in the browser) getRecipe
必须在浏览器中进行用户交互) I like the idea of chaining this together via pipes for higher flexibility when I enhance the code later. 我喜欢通过管道将它链接在一起的想法,以便在我稍后增强代码时获得更高的灵活性。 But I don't want to serialize it to a RDF serialization every time but just send around the JS object.
但是我不希望每次都将它序列化为RDF序列化,而只是发送JS对象。 From what I've read in the documentation I could use the
stringify
module to get a string out of each step for piping it to the next step. 从我在文档中读到的内容中,我可以使用
stringify
模块从每个步骤中获取一个字符串,以便将其传递给下一步。 But: 但:
getRecipe
would have to be called first and the output is input for createMeal
as well. getRecipe
,并且输出也是createMeal
输入。 Are there modules which help me on that? I hope this shows what I'm trying to do, if not I will try to give more details/rephrase. 我希望这表明我正在尝试做什么,如果不是,我会尝试提供更多细节/改述。
Update: After sleeping over it I figured out some more things: 更新:睡了之后我想出了更多的东西:
stringify
I will simply pass an official RDF serialization between the steps stringify
getRecipe
and createMeal
by simply adding some information from getRecipe
to parseRDF
, this can be done very easily with RDF without breaking the original data model. getRecipe
一些信息添加到parseRDF
来解决getRecipe
和createMeal
之间的依赖关系问题,这可以通过RDF轻松完成,而不会破坏原始数据模型。 But I would still be interested to know if I could handle dependencies like this with pipes yes, It's okay to make a stream of js objects, you just have to remember to pipe it through something that will serialize the stream again after before writing it to IO. 是的,可以创建一个js对象流,你只需要记住管它通过在将它写入IO之前再次序列化流的东西。
I'd recomend writing a module called rdfStream that parses and serializes rdf, you would use it like this 我建议写一个名为rdfStream的模块来解析和序列化rdf,你会像这样使用它
var rdf = require('rdf-stream')
fs.createReadStream(file) //get a text stream
.pipe(rdf.parse()) //turn it into objects
.pipe(transform) //optional, do something with the objects
.pipe(rdf.stringify()) //turn back into text
.pipe(process.stdout) //write to IO.
and it could also be used by other people working with rdf in node, awesome! 它也可以被其他在节点中使用rdf的人使用,太棒了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.