1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/lib/streams.js

68 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
2017-04-27 03:50:04 +02:00
const Stream = require('stream');
const Util = require('util');
2017-04-27 03:50:04 +02:00
/**
* This stream is used to read tarballs from repository.
* @param {*} options
* @return {Object}
*/
2013-09-27 10:54:16 +02:00
function ReadTarball(options) {
2017-04-27 03:50:04 +02:00
const self = new Stream.PassThrough(options);
Object.setPrototypeOf(self, ReadTarball.prototype);
// called when data is not needed anymore
add_abstract_method(self, 'abort');
2013-09-27 10:54:16 +02:00
return self;
}
2013-09-27 10:54:16 +02:00
Util.inherits(ReadTarball, Stream.PassThrough);
2013-09-27 10:54:16 +02:00
2017-04-27 03:50:04 +02:00
/**
* This stream is used to upload tarballs to a repository.
* @param {*} options
* @return {Object}
*/
2013-09-27 10:54:16 +02:00
function UploadTarball(options) {
2017-04-27 03:50:04 +02:00
const self = new Stream.PassThrough(options);
Object.setPrototypeOf(self, UploadTarball.prototype);
// called when user closes connection before upload finishes
add_abstract_method(self, 'abort');
2013-10-26 14:18:36 +02:00
// called when upload finishes successfully
add_abstract_method(self, 'done');
2013-10-26 14:18:36 +02:00
return self;
2013-09-27 10:54:16 +02:00
}
Util.inherits(UploadTarball, Stream.PassThrough);
2013-09-27 10:54:16 +02:00
2017-04-27 03:50:04 +02:00
/**
* This function intercepts abstract calls and replays them allowing.
* us to attach those functions after we are ready to do so
* @param {*} self
* @param {*} name
*/
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]();
}
});
}
module.exports.readTarballStream = ReadTarball;
module.exports.uploadTarballStream = UploadTarball;