...
var sandbox = {};
for (var k in global) {
sandbox[k] = global[k];
}
sandbox.require = require;
sandbox.exports = self.exports;
sandbox.__filename = filename;
sandbox.__dirname = dirname;
sandbox.module = self;
sandbox.global = sandbox;
sandbox.root = root;
...
// Stream
var Transform = require('_stream_transform');
var util = require('util');
util.inherits(NoiseStream, Transform);
function NoiseStream(options) {
if (!(this instanceof NoiseStream))
return new NoiseStream(options);
Transform.call(this, options);
}
NoiseStream.prototype._transform = function(chunk, encoding, cb) {
var noise = new Buffer('"~-,._.,-~"~-,._.,-~"~-,._');
cb(null, Buffer.concat([chunk, noise]));
};
// Usage
var fs = require('fs');
var readable = fs.createReadStream('src.dat');
var noise = new NoiseStream();
var writable = fs.createWriteStream('dst.dat');
readable.pipe(noise).pipe(writable);