0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/lib/streams.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

2013-09-27 03:54:16 -05:00
var stream = require('stream');
var util = require('util');
//
// This stream is used to read tarballs from repository
//
function ReadTarball(options) {
stream.PassThrough.call(this, options);
// called when data is not needed anymore
add_abstract_method(this, 'abort');
}
2013-09-27 03:54:16 -05:00
util.inherits(ReadTarball, stream.PassThrough);
module.exports.ReadTarballStream = ReadTarball;
//
// This stream is used to upload tarballs to a repository
//
function UploadTarball(options) {
2013-09-27 06:31:28 -05:00
stream.PassThrough.call(this, options);
// called when user closes connection before upload finishes
add_abstract_method(this, 'abort');
// called when upload finishes successfully
add_abstract_method(this, 'done');
2013-09-27 03:54:16 -05:00
}
2013-09-27 06:31:28 -05:00
util.inherits(UploadTarball, stream.PassThrough);
2013-09-27 03:54:16 -05:00
module.exports.UploadTarballStream = UploadTarball;
//
// This function intercepts abstract calls and replays them allowing
// us to attach those functions after we are ready to do so
//
function add_abstract_method(self, name) {
self._called_methods = self._called_methods || {};
self.__defineGetter__(name, function() {
return function() {
self._called_methods[name] = true;
}
});
self.__defineSetter__(name, function(fn) {
delete self[name];
self[name] = fn;
if (self._called_methods && self._called_methods[name]) {
delete self._called_methods[name];
self[name]();
}
});
}
2013-09-27 06:31:28 -05:00
function __test() {
var test = new ReadTarball();
test.abort();
setTimeout(function() {
test.abort = function() {
console.log('ok');
};
test.abort = function() {
throw 'fail';
};
}, 100);
}