0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-27 22:59:51 -05:00

Merge pull request #197 from BartDubois/url_prefix-as-path

Allow url_prefix to be only the path
This commit is contained in:
jotadeveloper 2017-05-21 23:00:09 +02:00 committed by GitHub
commit 16f8ae0f30
3 changed files with 33 additions and 10 deletions

View file

@ -28,6 +28,12 @@ url_prefix: 'https://your-domain:8888'
# or
url_prefix: 'https://your-domain:8888/your-path'
```
If you do not know the protocol and host, you can define only subpath as `url_prefix`. Full URL will be constructed from the request.
```yaml
url_prefix: '/your-path'
```
> Nginx or Apache configure? Please check out Wiki ;-)
## Keeping verdaccio running forever

View file

@ -9,6 +9,7 @@ let Handlebars = require('handlebars');
let renderReadme = require('render-readme');
let Search = require('./search');
let Middleware = require('./middleware');
let Utils = require('./utils');
let match = Middleware.match;
let validate_name = Middleware.validate_name;
let validate_pkg = Middleware.validate_package;
@ -44,9 +45,7 @@ module.exports = function(config, auth, storage) {
template = Handlebars.compile(fs.readFileSync(require.resolve('./GUI/index.hbs'), 'utf8'));
}
app.get('/', function(req, res, next) {
let base = config.url_prefix
? config.url_prefix.replace(/\/$/, '')
: req.protocol + '://' + req.get('host');
let base = Utils.combineBaseUrl(req.protocol, req.get('host'), config.url_prefix);
res.setHeader('Content-Type', 'text/html');
storage.get_local(function(err, packages) {

View file

@ -80,6 +80,27 @@ function validate_metadata(object, name) {
return object;
}
/**
* Create base url for registry.
* @param {String} protocol
* @param {String} host
* @param {String} prefix
* @return {String} base registry url
*/
function combineBaseUrl(protocol, host, prefix) {
let result = `${protocol}://${host}`;
if (prefix) {
prefix = prefix.replace(/\/$/, '');
result = (prefix.indexOf('/') === 0)
? `${result}${prefix}`
: prefix;
}
return result;
}
/**
* Iterate a packages's versions and filter each original tarbal url.
* @param {*} pkg
@ -98,13 +119,9 @@ function filter_tarball_urls(pkg, req, config) {
return _url;
}
const filename = URL.parse(_url).pathname.replace(/^.*\//, '');
let result;
if (config.url_prefix != null) {
result = config.url_prefix.replace(/\/$/, '');
} else {
result = `${req.protocol}://${req.headers.host}`;
}
return `${result}/${pkg.name.replace(/\//g, '%2f')}/-/${filename}`;
const base = combineBaseUrl(req.protocol, req.headers.host, config.url_prefix);
return `${base}/${pkg.name.replace(/\//g, '%2f')}/-/${filename}`;
};
for (let ver in pkg.versions) {
@ -263,6 +280,7 @@ module.exports.parse_address = parse_address;
module.exports.get_version = get_version;
module.exports.normalize_dist_tags = normalize_dist_tags;
module.exports.tag_version = tag_version;
module.exports.combineBaseUrl = combineBaseUrl;
module.exports.filter_tarball_urls = filter_tarball_urls;
module.exports.validate_metadata = validate_metadata;
module.exports.is_object = is_object;