2013-10-26 07:18:36 -05:00
|
|
|
var stream = require('stream')
|
|
|
|
, util = require('util')
|
2013-09-27 03:54:16 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// This stream is used to read tarballs from repository
|
|
|
|
//
|
|
|
|
function ReadTarball(options) {
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.PassThrough.call(this, options)
|
2013-09-27 03:54:16 -05:00
|
|
|
|
2013-09-27 04:54:43 -05:00
|
|
|
// called when data is not needed anymore
|
2013-10-26 07:18:36 -05:00
|
|
|
add_abstract_method(this, 'abort')
|
2013-09-27 04:54:43 -05:00
|
|
|
}
|
2013-09-27 03:54:16 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
util.inherits(ReadTarball, stream.PassThrough)
|
|
|
|
module.exports.ReadTarballStream = ReadTarball
|
2013-09-27 03:54:16 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// This stream is used to upload tarballs to a repository
|
|
|
|
//
|
|
|
|
function UploadTarball(options) {
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.PassThrough.call(this, options)
|
|
|
|
|
2013-09-27 04:54:43 -05:00
|
|
|
// called when user closes connection before upload finishes
|
2013-10-26 07:18:36 -05:00
|
|
|
add_abstract_method(this, 'abort')
|
|
|
|
|
2013-09-27 04:54:43 -05:00
|
|
|
// called when upload finishes successfully
|
2013-10-26 07:18:36 -05:00
|
|
|
add_abstract_method(this, 'done')
|
2013-09-27 03:54:16 -05:00
|
|
|
}
|
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
util.inherits(UploadTarball, stream.PassThrough)
|
|
|
|
module.exports.UploadTarballStream = UploadTarball
|
2013-09-27 03:54:16 -05:00
|
|
|
|
2013-09-27 04:54:43 -05:00
|
|
|
//
|
|
|
|
// 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) {
|
2013-10-26 07:18:36 -05:00
|
|
|
self._called_methods = self._called_methods || {}
|
2013-09-27 04:54:43 -05:00
|
|
|
self.__defineGetter__(name, function() {
|
|
|
|
return function() {
|
2013-10-26 07:18:36 -05:00
|
|
|
self._called_methods[name] = true
|
2013-09-27 04:54:43 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2013-09-27 04:54:43 -05:00
|
|
|
self.__defineSetter__(name, function(fn) {
|
2013-10-26 07:18:36 -05:00
|
|
|
delete self[name]
|
|
|
|
self[name] = fn
|
2013-09-27 04:54:43 -05:00
|
|
|
if (self._called_methods && self._called_methods[name]) {
|
2013-10-26 07:18:36 -05:00
|
|
|
delete self._called_methods[name]
|
|
|
|
self[name]()
|
2013-09-27 04:54:43 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2013-09-27 04:54:43 -05:00
|
|
|
}
|
|
|
|
|
2013-09-27 06:31:28 -05:00
|
|
|
function __test() {
|
2013-10-26 07:18:36 -05:00
|
|
|
var test = new ReadTarball()
|
|
|
|
test.abort()
|
2013-09-27 04:54:43 -05:00
|
|
|
setTimeout(function() {
|
|
|
|
test.abort = function() {
|
2013-10-26 07:18:36 -05:00
|
|
|
console.log('ok')
|
|
|
|
}
|
2013-09-27 04:54:43 -05:00
|
|
|
test.abort = function() {
|
2013-10-26 07:18:36 -05:00
|
|
|
throw 'fail'
|
|
|
|
}
|
|
|
|
}, 100)
|
2013-09-27 04:54:43 -05:00
|
|
|
}
|
|
|
|
|