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
I've read the Buffer/Streams documentation @ nodejs.org, watched James Halliday @ LXJS, read his stream-handbook and Thorsten Lorenz event-stream post. I start to understand the basics :)
I process data which is serialized in RDF (which is neither JSON nor XML). I manage to fetch the data (in real code via request) and parse it into a JS object using rdfstore
module.
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. I use through
module to interface to the stream.
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 itcreateMeal(parsedRDF, recipe)
-> takes the parsed RDF and the recipe from above and creates a new RDF object out of itgetRecipe
will have to do a user interaction in the browser)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. 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. But:
getRecipe
would have to be called first and the output is input for createMeal
as well. 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 stepsgetRecipe
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. But I would still be interested to know if I could handle dependencies like this with pipesyes, 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.
I'd recomend writing a module called rdfStream that parses and serializes rdf, you would use it like this
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With