Since your using var asset = new ProjectAsset('myFile', __dirname + '/image.jpg') I suppose your ProjectAsset responsibility is to take some input stream do some transformations and write that to a file. You could implement a transform stream because you receive some input from a stream and generate some output of it that can be saved to a file or to some other write stream.
You could of course implement a transform stream by inheriting from node.js Transform Stream but inheriting is quite cumbersome so my implementation uses through2 to implement the transform stream:
module.exports = through2(function (chunk, enc, callback) {
// This function is called whenever a piece of data from the incoming stream is read
// Transform the chunk or buffer the chunk in case you need more data to transform
// Emit a data package to the next stream in the pipe or omit this call if you need more data from the input stream to be read
this.push(chunk);
// Signal through2 that you processed the incoming data package
callback();
}))
Usage
var stream = fs.createReadStream(__dirname + '/image.jpg', { encoding: 'base64' })
.pipe(projectAsset)
.pipe(fs.createWriteStream(__dirname + '/image.jpg'));
As you can see in this example implementing a stream pipeline fully decouples data transformation and saving of the data.
Factory Function
If you prefer to use a constructor like approach in the project asset module because you need to pass some values or things you could easily export a constructor function as shown below
var through2 = require('through2');
module.exports = function(someData) {
// New stream is returned that can use someData argument for doing things
return through2(function (chunk, enc, callback) {
// This function is called whenever a piece of data from the incoming stream is read
// Transform the chunk or buffer the chunk in case you need more data to transform
// Emit a data package to the next stream in the pipe or omit this call if you need more data from the input stream to be read
this.push(chunk);
// Signal through2 that you processed the incoming data package
callback();
});
}
Usage
var stream = fs.createReadStream(__dirname + '/image.jpg', { encoding: 'base64' })
.pipe(projectAsset({ foo: 'bar' }))
.pipe(fs.createWriteStream(__dirname + '/image.jpg'));