Created
July 19, 2012 21:26
-
-
Save juandopazo/3146931 to your computer and use it in GitHub Desktop.
Streaming combo handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
Stream = require('stream'), | |
util = require('util'); | |
function ComboStream(files, opts) { | |
Stream.call(this); | |
opts = opts || {}; | |
if (!Array.isArray(files)) { | |
throw new TypeError('First argument must be an Array'); | |
} | |
if (Object(opts) !== opts) { | |
throw new TypeError('Second argument must be an Object'); | |
} | |
this._stack = files.concat(); | |
this._opts = opts; | |
this._readNext(); | |
} | |
util.inherits(ComboStream, Stream); | |
util._extend(ComboStream.prototype, { | |
readable: true, | |
writable: false, | |
paused: false, | |
pause: function () { | |
this.paused = true; | |
if (this._current) { | |
this._current.pause(); | |
} | |
}, | |
resume: function () { | |
this.paused = false; | |
if (this._current) { | |
this._current.resume(); | |
} | |
}, | |
setEncoding: function (encoding) { | |
this._opts.encoding = encoding; | |
if (this._current) { | |
this._current.setEncoding(encoding); | |
} | |
}, | |
_readNext: function () { | |
var file = this._stack.shift(); | |
if (file) { | |
this._current = fs.createReadStream(file, this._opts) | |
.on('data', this._onData.bind(this)) | |
.on('error', this._onError.bind(this)) | |
.on('end', this._onEnd.bind(this)); | |
this._current.on('close', this._onClose.bind(this, this._current)); | |
} | |
}, | |
_onData: function (chunk) { | |
this.emit('data', chunk); | |
}, | |
_onClose: function (stream) { | |
this.emit('close', stream); | |
}, | |
_onError: function (err) { | |
this.emit('error', err); | |
}, | |
_onEnd: function () { | |
this._current.removeAllListeners().destroy(); | |
this._current = undefined; | |
if (this._stack.length === 0) { | |
this.readable = false; | |
this.emit('end'); | |
} else { | |
this._readNext(); | |
} | |
}, | |
destroy: function () { | |
if (this._current) { | |
this._current.removeAllListeners().destroy(); | |
this._current = undefined; | |
} | |
this.readable = false; | |
this._stack = []; | |
} | |
}); | |
ComboStream.createCombo = function (files, opts) { | |
return new ComboStream(files, opts); | |
}; | |
module.exports = ComboStream; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var combo = require('./combo.js'), | |
http = require('http'), | |
zlib = require('zlib'); | |
http.createServer(function (req, res) { | |
if (req.url == '/') { | |
res.writeHead(200, { | |
'Content-Type': 'application/javascript', | |
'content-encoding': 'deflate' | |
}); | |
combo.createCombo(['hello.js', 'world.js']).pipe(zlib.createDeflate()).pipe(res); | |
} else { | |
res.end(); | |
} | |
}).listen(3000, '127.0.0.1'); | |
console.log('Server running at http://127.0.0.1:3000/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's probably a bad idea because it reads files sequentially