0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-20 22:52:46 -05:00
verdaccio/packages/api/src/package.ts

126 lines
4 KiB
TypeScript
Raw Normal View History

import buildDebug from 'debug';
import { Router } from 'express';
import _ from 'lodash';
2020-03-03 23:59:19 +01:00
import { IAuth } from '@verdaccio/auth';
import { API_ERROR, DIST_TAGS, HEADERS, errorUtils } from '@verdaccio/core';
import { allow } from '@verdaccio/middleware';
import { Storage } from '@verdaccio/store';
import { convertDistRemoteToLocalTarballUrls } from '@verdaccio/tarball';
import { Config, Package } from '@verdaccio/types';
import { getVersion } from '@verdaccio/utils';
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types/custom';
2020-03-03 23:59:19 +01:00
const debug = buildDebug('verdaccio:api:package');
const downloadStream = (
packageName: string,
filename: string,
storage: any,
req: $RequestExtend,
res: $ResponseExtend
): void => {
2020-03-03 23:59:19 +01:00
const stream = storage.getTarball(packageName, filename);
stream.on('content-length', function (content): void {
2020-03-03 23:59:19 +01:00
res.header('Content-Length', content);
});
stream.on('error', function (err): void {
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);
};
export default function (route: Router, auth: IAuth, storage: Storage, config: Config): void {
2020-03-03 23:59:19 +01:00
const can = allow(auth);
// TODO: anonymous user?
route.get(
'/:package/:version?',
can('access'),
function (req: $RequestExtend, _res: $ResponseExtend, next: $NextFunctionVer): void {
debug('init package by version');
const name = req.params.package;
let queryVersion = req.params.version;
const requestOptions = {
protocol: req.protocol,
headers: req.headers as any,
// FIXME: if we migrate to req.hostname, the port is not longer included.
host: req.host,
};
const getPackageMetaCallback = function (err, metadata: Package): void {
if (err) {
debug('error on fetch metadata for %o with error %o', name, err.message);
return next(err);
}
debug('convert dist remote to local with prefix %o', config?.url_prefix);
metadata = convertDistRemoteToLocalTarballUrls(
metadata,
requestOptions,
config?.url_prefix
);
2020-03-03 23:59:19 +01:00
debug('query by param version: %o', queryVersion);
if (_.isNil(queryVersion)) {
debug('param %o version found', queryVersion);
return next(metadata);
}
2020-03-03 23:59:19 +01:00
let version = getVersion(metadata.versions, queryVersion);
debug('query by latest version %o and result %o', queryVersion, version);
if (_.isNil(version) === false) {
debug('latest version found %o', version);
return next(version);
}
2020-03-03 23:59:19 +01:00
if (_.isNil(metadata[DIST_TAGS]) === false) {
if (_.isNil(metadata[DIST_TAGS][queryVersion]) === false) {
queryVersion = metadata[DIST_TAGS][queryVersion];
debug('dist-tag version found %o', queryVersion);
version = getVersion(metadata.versions, queryVersion);
if (_.isNil(version) === false) {
debug('dist-tag found %o', version);
return next(version);
}
2020-03-03 23:59:19 +01:00
}
} else {
debug('dist tag not detected');
2020-03-03 23:59:19 +01:00
}
debug('package version not found %o', queryVersion);
return next(errorUtils.getNotFound(`${API_ERROR.VERSION_NOT_EXIST}: ${queryVersion}`));
};
2020-03-03 23:59:19 +01:00
debug('get package name %o', name);
debug('uplinks look up enabled');
storage.getPackage({
name,
uplinksLook: true,
req,
callback: getPackageMetaCallback,
});
}
);
2020-03-03 23:59:19 +01:00
route.get(
'/:scopedPackage/-/:scope/:filename',
can('access'),
function (req: $RequestExtend, res: $ResponseExtend): void {
const { scopedPackage, filename } = req.params;
2020-03-03 23:59:19 +01:00
downloadStream(scopedPackage, filename, storage, req, res);
}
);
2020-03-03 23:59:19 +01:00
route.get(
'/:package/-/:filename',
can('access'),
function (req: $RequestExtend, res: $ResponseExtend): void {
downloadStream(req.params.package, req.params.filename, storage, req, res);
}
);
2020-03-03 23:59:19 +01:00
}