0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00

Merge pull request #1260 from verdaccio/fix-1257

fix: add new enpoind that allow download tarballs according the standard
This commit is contained in:
Juan Picado @jotadeveloper 2019-03-24 09:06:49 +01:00 committed by GitHub
commit 89b5d9a070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,20 @@ import type { Router } from 'express';
import type { Config } from '@verdaccio/types';
import type { IAuth, $ResponseExtend, $RequestExtend, $NextFunctionVer, IStorageHandler } from '../../../../types';
const downloadStream = (packageName: string, filename: string, storage: any, req: $RequestExtend, res: $ResponseExtend) => {
const stream = storage.getTarball(packageName, filename);
stream.on('content-length', function(content) {
res.header('Content-Length', content);
});
stream.on('error', function(err) {
return res.report_error(err);
});
res.header(HEADERS.CONTENT_TYPE, HEADERS.OCTET_STREAM);
stream.pipe(res);
};
export default function(route: Router, auth: IAuth, storage: IStorageHandler, config: Config) {
const can = allow(auth);
// TODO: anonymous user?
@ -51,16 +65,13 @@ export default function(route: Router, auth: IAuth, storage: IStorageHandler, co
});
});
route.get('/:package/-/:filename', can('access'), function(req: $RequestExtend, res: $ResponseExtend) {
const stream = storage.getTarball(req.params.package, req.params.filename);
route.get('/:scopedPackage/-/:scope/:filename', can('access'), function(req: $RequestExtend, res: $ResponseExtend) {
const { scopedPackage, filename } = req.params;
stream.on('content-length', function(content) {
res.header('Content-Length', content);
});
stream.on('error', function(err) {
return res.report_error(err);
});
res.header('Content-Type', HEADERS.OCTET_STREAM);
stream.pipe(res);
downloadStream(scopedPackage, filename, storage, req, res);
});
route.get('/:package/-/:filename', can('access'), function(req: $RequestExtend, res: $ResponseExtend) {
downloadStream(req.params.package, req.params.filename, storage, req, res);
});
}