2020-08-19 13:27:35 -05:00
|
|
|
import buildDebug from 'debug';
|
2021-10-29 10:33:05 -05:00
|
|
|
import { Router } from 'express';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2020-09-16 23:48:16 -05:00
|
|
|
import { IAuth } from '@verdaccio/auth';
|
2021-12-12 12:00:19 -05:00
|
|
|
import { HEADERS, errorUtils } from '@verdaccio/core';
|
2021-10-29 10:33:05 -05:00
|
|
|
import { allow } from '@verdaccio/middleware';
|
2021-09-08 12:06:37 -05:00
|
|
|
import { Storage } from '@verdaccio/store';
|
2021-10-29 10:33:05 -05:00
|
|
|
|
|
|
|
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types/custom';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2020-08-19 13:27:35 -05:00
|
|
|
const debug = buildDebug('verdaccio:api:package');
|
|
|
|
|
2020-09-16 23:48:16 -05:00
|
|
|
const downloadStream = (
|
|
|
|
packageName: string,
|
|
|
|
filename: string,
|
|
|
|
storage: any,
|
2021-12-12 12:00:19 -05:00
|
|
|
_req: $RequestExtend,
|
2020-09-16 23:48:16 -05:00
|
|
|
res: $ResponseExtend
|
|
|
|
): void => {
|
2020-03-03 17:59:19 -05:00
|
|
|
const stream = storage.getTarball(packageName, filename);
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('content-length', function (content): void {
|
2020-03-03 17:59:19 -05:00
|
|
|
res.header('Content-Length', content);
|
|
|
|
});
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('error', function (err): void {
|
2020-06-30 14:55:14 -05:00
|
|
|
return res.locals.report_error(err);
|
2020-03-03 17:59:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
res.header(HEADERS.CONTENT_TYPE, HEADERS.OCTET_STREAM);
|
|
|
|
stream.pipe(res);
|
|
|
|
};
|
|
|
|
|
2021-12-12 12:00:19 -05:00
|
|
|
export default function (route: Router, auth: IAuth, storage: Storage): void {
|
2020-03-03 17:59:19 -05:00
|
|
|
const can = allow(auth);
|
2021-12-12 12:00:19 -05:00
|
|
|
|
2021-01-16 04:40:43 -05:00
|
|
|
route.get(
|
|
|
|
'/:package/:version?',
|
|
|
|
can('access'),
|
2021-12-12 12:00:19 -05:00
|
|
|
async function (
|
|
|
|
req: $RequestExtend,
|
|
|
|
_res: $ResponseExtend,
|
|
|
|
next: $NextFunctionVer
|
|
|
|
): Promise<void> {
|
2021-01-16 04:40:43 -05:00
|
|
|
debug('init package by version');
|
|
|
|
const name = req.params.package;
|
2021-11-09 14:38:44 -05:00
|
|
|
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,
|
|
|
|
};
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2021-12-12 12:00:19 -05:00
|
|
|
try {
|
|
|
|
// TODO: this is just temporary while I migrate all plugins to use the new API
|
|
|
|
// the method will be renamed to getPackage again but Promise Based.
|
2022-02-20 12:39:38 -05:00
|
|
|
if (!storage.getPackageByOptions) {
|
2021-12-12 12:00:19 -05:00
|
|
|
throw errorUtils.getInternalError(
|
2022-02-20 12:39:38 -05:00
|
|
|
'getPackageByOptions not implemented, check pr-2750 for more details'
|
2021-12-12 12:00:19 -05:00
|
|
|
);
|
2021-01-16 04:40:43 -05:00
|
|
|
}
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2022-02-20 12:39:38 -05:00
|
|
|
const manifest = await storage.getPackageByOptions({
|
2021-12-12 12:00:19 -05:00
|
|
|
name,
|
|
|
|
uplinksLook: true,
|
|
|
|
req,
|
|
|
|
version: queryVersion,
|
|
|
|
requestOptions,
|
|
|
|
});
|
|
|
|
next(manifest);
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
2021-01-16 04:40:43 -05:00
|
|
|
}
|
|
|
|
);
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2021-01-16 04:40:43 -05:00
|
|
|
route.get(
|
|
|
|
'/:scopedPackage/-/:scope/:filename',
|
|
|
|
can('access'),
|
|
|
|
function (req: $RequestExtend, res: $ResponseExtend): void {
|
|
|
|
const { scopedPackage, filename } = req.params;
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2021-01-16 04:40:43 -05:00
|
|
|
downloadStream(scopedPackage, filename, storage, req, res);
|
|
|
|
}
|
|
|
|
);
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2021-01-16 04:40:43 -05: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 17:59:19 -05:00
|
|
|
}
|