In short: I have a callback function that has a string parameter and has to return a string. I need to transform this string using a library that only works with streams. How do I do it?
Longer: I'm using Node.js replacestream which has a feature to match RegEx and just like String.replace it allows to specify a callback function for replacing. What I need to do, is take the matched string, run it through another library, and then return the transformed string for replace.
The problem is that this library only works with streams. Normally I could make the entire thing asynchronous but I don't see any way to do it with String.replace callback.
src(['*.js'])
.pipe(replace(/`(.*?)`/gs, function(match, p1, offset, string) {
var ostream = stringtostream(p1);
ostream.pipe(glslminify());
//???code that waits for ostream to finish and converts it to string???
return string_from_ostream;
}))
.pipe(dest(jsdest));
There must be a better way to do this, so I'm looking for any suggestions.