2020-03-03 23:59:19 +01:00
|
|
|
import _ from 'lodash';
|
|
|
|
import { Router } from 'express';
|
2020-08-19 20:27:35 +02:00
|
|
|
import buildDebug from 'debug';
|
2020-03-03 23:59:19 +01:00
|
|
|
|
|
|
|
import { allow } from '@verdaccio/middleware';
|
|
|
|
import { convertDistRemoteToLocalTarballUrls, getVersion, ErrorCode } from '@verdaccio/utils';
|
|
|
|
import { HEADERS, DIST_TAGS, API_ERROR } from '@verdaccio/dev-commons';
|
|
|
|
import { Config, Package } from '@verdaccio/types';
|
|
|
|
import { IAuth, $ResponseExtend, $RequestExtend, $NextFunctionVer, IStorageHandler } from '@verdaccio/dev-types';
|
|
|
|
|
2020-08-19 20:27:35 +02:00
|
|
|
const debug = buildDebug('verdaccio:api:package');
|
|
|
|
|
2020-03-03 23:59:19 +01:00
|
|
|
const downloadStream = (packageName: string, filename: string, storage: any, req: $RequestExtend, res: $ResponseExtend): void => {
|
|
|
|
const stream = storage.getTarball(packageName, filename);
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
stream.on('content-length', function (content): void {
|
2020-03-03 23:59:19 +01:00
|
|
|
res.header('Content-Length', content);
|
|
|
|
});
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
stream.on('error', function (err): void {
|
2020-06-30 21:55:14 +02:00
|
|
|
return res.locals.report_error(err);
|
2020-03-03 23:59:19 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
res.header(HEADERS.CONTENT_TYPE, HEADERS.OCTET_STREAM);
|
|
|
|
stream.pipe(res);
|
|
|
|
};
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
export default function (route: Router, auth: IAuth, storage: IStorageHandler, config: Config): void {
|
2020-03-03 23:59:19 +01:00
|
|
|
const can = allow(auth);
|
|
|
|
// TODO: anonymous user?
|
2020-08-13 23:27:00 +02:00
|
|
|
route.get('/:package/:version?', can('access'), function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('init package by version');
|
|
|
|
const name = req.params.package;
|
2020-08-13 23:27:00 +02:00
|
|
|
const getPackageMetaCallback = function (err, metadata: Package): void {
|
2020-03-03 23:59:19 +01:00
|
|
|
if (err) {
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('error on fetch metadata for %o with error %o', name, err.message);
|
2020-03-03 23:59:19 +01:00
|
|
|
return next(err);
|
|
|
|
}
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('convert dist remote to local with prefix %o', config?.url_prefix);
|
|
|
|
metadata = convertDistRemoteToLocalTarballUrls(metadata, req, config?.url_prefix);
|
2020-03-03 23:59:19 +01:00
|
|
|
|
|
|
|
let queryVersion = req.params.version;
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('query by param version: %o', queryVersion);
|
2020-03-03 23:59:19 +01:00
|
|
|
if (_.isNil(queryVersion)) {
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('param %o version found', queryVersion);
|
2020-03-03 23:59:19 +01:00
|
|
|
return next(metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
let version = getVersion(metadata, queryVersion);
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('query by latest version %o and result %o', queryVersion, version);
|
2020-03-03 23:59:19 +01:00
|
|
|
if (_.isNil(version) === false) {
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('latest version found %o', version);
|
2020-03-03 23:59:19 +01:00
|
|
|
return next(version);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isNil(metadata[DIST_TAGS]) === false) {
|
|
|
|
if (_.isNil(metadata[DIST_TAGS][queryVersion]) === false) {
|
|
|
|
queryVersion = metadata[DIST_TAGS][queryVersion];
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('dist-tag version found %o', queryVersion);
|
2020-03-03 23:59:19 +01:00
|
|
|
version = getVersion(metadata, queryVersion);
|
|
|
|
if (_.isNil(version) === false) {
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('dist-tag found %o', version);
|
2020-03-03 23:59:19 +01:00
|
|
|
return next(version);
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 20:27:35 +02:00
|
|
|
} else {
|
|
|
|
debug('dist tag not detected');
|
2020-03-03 23:59:19 +01:00
|
|
|
}
|
2020-08-19 20:27:35 +02:00
|
|
|
|
|
|
|
debug('package version not found %o', queryVersion);
|
|
|
|
return next(ErrorCode.getNotFound(`${API_ERROR.VERSION_NOT_EXIST}: ${queryVersion}`));
|
2020-03-03 23:59:19 +01:00
|
|
|
};
|
|
|
|
|
2020-08-19 20:27:35 +02:00
|
|
|
debug('get package name %o', name);
|
|
|
|
debug('uplinks look up enabled');
|
2020-03-03 23:59:19 +01:00
|
|
|
storage.getPackage({
|
2020-08-19 20:27:35 +02:00
|
|
|
name,
|
2020-03-03 23:59:19 +01:00
|
|
|
uplinksLook: true,
|
|
|
|
req,
|
|
|
|
callback: getPackageMetaCallback,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
route.get('/:scopedPackage/-/:scope/:filename', can('access'), function (req: $RequestExtend, res: $ResponseExtend): void {
|
2020-03-03 23:59:19 +01:00
|
|
|
const { scopedPackage, filename } = req.params;
|
|
|
|
|
|
|
|
downloadStream(scopedPackage, filename, storage, req, res);
|
|
|
|
});
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
route.get('/:package/-/:filename', can('access'), function (req: $RequestExtend, res: $ResponseExtend): void {
|
2020-03-03 23:59:19 +01:00
|
|
|
downloadStream(req.params.package, req.params.filename, storage, req, res);
|
|
|
|
});
|
|
|
|
}
|