简体   繁体   English

在Node.js中管道/流式传输JavaScript对象

[英]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对象
  • this new object needs to get serialized and sent to the browser 这个新对象需要序列化并发送到浏览器
  • (In the real world 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: 但:

  • does this actually make sense? 这真的有意义吗? In terms of do I add unnecessary overhead or is this negligible? 我是否会增加不必要的开销或者这可以忽略不计?
  • I don't see how I could give the parsedRDF to both methods with the dependency that getRecipe would have to be called first and the output is input for createMeal as well. 我不知道如何将parsedRDF赋予两个方法,并且必须首先调用getRecipe ,并且输出也是createMeal输入。 Are there modules which help me on that? 是否有模块可以帮助我?
  • It might be that I have to ask the user for the final recipe selection so I might need to send stuff to the browser there to get the final answer. 可能是我必须要求用户选择最终的配方,因此我可能需要将内容发送到浏览器以获得最终答案。 Can I do something like this over sockets while the pipe is "waiting"? 当管道“等待”时,我可以在插座上做这样的事情吗?

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: 更新:睡了之后我想出了更多的东西:

  • It probably doesn't make sense to serialize a format like RDF into something non-standard if there are official serialization formats. 如果有官方序列化格式,将RDF格式序列化为非标准格式可能没有意义。 So instead of using stringify I will simply pass an official RDF serialization between the steps 因此,我将简单地在步骤之间传递正式的RDF序列化,而不是使用stringify
  • This does imply that I parse/serialize the objects in each step and this surely does add overhead. 这确实意味着我在每个步骤中解析/序列化对象,这肯定会增加开销。 Question is do I care? 问题是我在乎吗? I could extend the RDF module I use to parse from stream and serialize into one 我可以扩展我使用的RDF模块从流解析并序列化为一个
  • I can solve the problem with the dependency between 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来解决getRecipecreateMeal之间的依赖关系问题,这可以通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM