Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Created October 6, 2015 09:33
Show Gist options
  • Save telekosmos/8de9e72695f00ae750f7 to your computer and use it in GitHub Desktop.
Save telekosmos/8de9e72695f00ae750f7 to your computer and use it in GitHub Desktop.
node file content stream filter
if (file.isStream()) {
var
encoding = 'utf8',
contents = [];
function write (chunk, enc, done) {
contents.push(chunk);
done();
}
function end (done) {
// Concat stored buffers and convert to string.
contents = Buffer.concat(contents).toString(encoding);
// encodeURI() string.
contents = encodeURI(contents);
// Make new buffer with output of encodeURI().
contents = Buffer(contents, encoding);
// Push new buffer.
this.push(contents);
done();
}
// This assumes you want file.contents to be a stream in the end.
file.contents = file.contents.pipe(through(write, end));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment