0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/lib/up-storage.js

182 lines
4.2 KiB
JavaScript
Raw Normal View History

var URL = require('url');
2013-06-07 20:16:28 -05:00
var request = require('request');
2013-06-14 03:34:29 -05:00
var UError = require('./error').UserError;
var mystreams = require('./streams');
2013-06-07 20:16:28 -05:00
2013-09-25 04:12:33 -05:00
//
// Implements Storage interface
// (same for storage.js, local-storage.js, up-storage.js)
//
2013-06-19 11:58:16 -05:00
function Storage(config, mainconfig) {
2013-06-07 20:16:28 -05:00
if (!(this instanceof Storage)) return new Storage(config);
this.config = config;
2013-06-19 11:58:16 -05:00
this.ua = mainconfig.user_agent;
2013-06-07 20:16:28 -05:00
this.ca;
2013-06-19 11:58:16 -05:00
this.url = URL.parse(this.config.url);
if (this.url.hostname === 'registry.npmjs.org') {
2013-06-07 20:16:28 -05:00
this.ca = require('./npmsslkeys');
2013-06-20 08:41:07 -05:00
// npm registry is too slow working with ssl :(
/*if (this.config._autogenerated) {
2013-06-19 11:58:16 -05:00
// encrypt all the things!
this.url.protocol = 'https';
this.config.url = URL.format(this.url);
2013-06-20 08:41:07 -05:00
}*/
2013-06-07 20:16:28 -05:00
}
2013-06-19 11:58:16 -05:00
this.config.url = this.config.url.replace(/\/$/, '');
2013-06-07 20:16:28 -05:00
return this;
}
2013-06-19 11:58:16 -05:00
Storage.prototype.can_fetch_url = function(url) {
url = URL.parse(url);
return url.protocol === this.url.protocol
&& url.host === this.url.host
2013-06-20 08:41:07 -05:00
&& url.path.indexOf(this.url.path) === 0
2013-06-19 11:58:16 -05:00
}
2013-09-25 04:18:38 -05:00
Storage.prototype.add_package = function(name, metadata, callback) {
request({
url: this.config.url + '/' + escape(name),
headers: {
'User-Agent': this.ua,
},
method: 'PUT',
ca: this.ca,
json: metadata,
}, function(err, res, body) {
if (err) return callback(err);
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
return callback(new Error('bad status code: ' + res.statusCode));
}
callback(null, body);
});
2013-09-25 04:18:38 -05:00
}
Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
request({
url: this.config.url + '/' + escape(name) + '/' + escape(version) + '/-tag/' + escape(tag),
headers: {
'User-Agent': this.ua,
},
method: 'PUT',
ca: this.ca,
json: metadata,
}, function(err, res, body) {
if (err) return callback(err);
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
return callback(new Error('bad status code: ' + res.statusCode));
}
callback(null, body);
});
2013-09-25 04:18:38 -05:00
}
Storage.prototype.add_tarball = function(name, filename) {
2013-09-28 07:19:40 -05:00
var stream = new mystreams.UploadTarballStream();
var self = this;
var wstream = request({
uri: this.config.url + '/' + escape(name) + '/-/' + escape(filename) + '/whatever',
method: 'PUT',
headers: {
'User-Agent': this.ua,
'content-type': 'application/octet-stream'
},
ca: this.ca,
});
wstream.on('response', function(res) {
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
return stream.emit('error', new UError({
msg: 'bad uplink status code: ' + res.statusCode,
status: 500,
}));
}
stream.emit('success');
});
wstream.on('error', function(err) {
stream.emit('error', err);
});
stream.abort = function() {
2013-09-28 07:37:24 -05:00
process.nextTick(function() {
if (wstream.req) {
wstream.req.abort();
}
});
2013-09-28 07:19:40 -05:00
};
stream.done = function() {};
stream.pipe(wstream);
return stream;
2013-09-25 04:18:38 -05:00
}
2013-06-07 20:16:28 -05:00
Storage.prototype.get_package = function(name, callback) {
request({
url: this.config.url + '/' + escape(name),
2013-06-07 20:16:28 -05:00
json: true,
headers: {
2013-06-19 11:58:16 -05:00
'User-Agent': this.ua,
2013-06-07 20:16:28 -05:00
},
ca: this.ca,
}, function(err, res, body) {
if (err) return callback(err);
2013-06-14 03:34:29 -05:00
if (res.statusCode === 404) {
return callback(new UError({
msg: 'package doesn\'t exist on uplink',
status: 404,
}));
}
2013-06-14 02:56:02 -05:00
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
return callback(new Error('bad status code: ' + res.statusCode));
}
2013-06-07 20:16:28 -05:00
callback(null, body);
});
}
2013-06-20 08:41:07 -05:00
Storage.prototype.get_tarball = function(name, filename) {
return this.get_url(this.config.url + '/' + name + '/-/' + filename);
}
Storage.prototype.get_url = function(url) {
var stream = new mystreams.ReadTarballStream();
stream.abort = function() {};
2013-06-20 08:41:07 -05:00
var rstream = request({
url: url,
2013-06-19 11:58:16 -05:00
headers: {
'User-Agent': this.ua,
},
ca: this.ca,
encoding: null,
2013-06-20 08:41:07 -05:00
});
rstream.on('response', function(res) {
2013-06-19 11:58:16 -05:00
if (res.statusCode === 404) {
2013-06-20 08:41:07 -05:00
return stream.emit('error', new UError({
2013-06-19 11:58:16 -05:00
msg: 'file doesn\'t exist on uplink',
status: 404,
}));
}
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
2013-06-20 08:41:07 -05:00
return stream.emit('error', new UError({
msg: 'bad uplink status code: ' + res.statusCode,
status: 500,
}));
2013-06-19 11:58:16 -05:00
}
2013-06-20 08:41:07 -05:00
rstream.pipe(stream);
});
rstream.on('error', function(err) {
stream.emit('error', err);
2013-06-19 11:58:16 -05:00
});
2013-06-20 08:41:07 -05:00
return stream;
2013-06-19 11:58:16 -05:00
}
2013-06-07 20:16:28 -05:00
module.exports = Storage;