mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-06 22:40:26 -05:00
459b6fa72b
* Refactor local-storage async refactor local storage search stream Remove async from local-storage, refactor search with streams refactor search with undici fetch finish search refactor stream multiple request to single stream refactor storage types remove async dependency #1225 add score and refactor metadata remove old search async fix missing stream local data clean up clean up refactor folder search format fix some test fix issue on publish filter preview update ci delete package folder refactor refactor get packages methods fix tests fix lock file add changeset fix test windows disable some test update package json versions * fix merge * fix e2e cli * restore e2e * Update process.ts * Update process.ts * add improvement * format * Update utils.ts * test * test * Update search.spec.ts * Update search.spec.ts * Update search.spec.ts * test * Update ci.yml * clean up * fix tests * Update tags.ts * Update index.spec.ts * document changeset * format
125 lines
4.6 KiB
TypeScript
125 lines
4.6 KiB
TypeScript
import path from 'path';
|
|
import nock from 'nock';
|
|
import { Config, parseConfigFile } from '@verdaccio/config';
|
|
import { ErrorCode } from '@verdaccio/utils';
|
|
import { API_ERROR, HEADER_TYPE, HTTP_STATUS, VerdaccioError } from '@verdaccio/commons-api';
|
|
import { ProxyStorage } from '../src/up-storage';
|
|
|
|
const getConf = (name) => path.join(__dirname, '/conf', name);
|
|
|
|
const mockDebug = jest.fn();
|
|
const mockInfo = jest.fn();
|
|
const mockHttp = jest.fn();
|
|
const mockError = jest.fn();
|
|
const mockWarn = jest.fn();
|
|
jest.mock('@verdaccio/logger', () => {
|
|
const originalLogger = jest.requireActual('@verdaccio/logger');
|
|
return {
|
|
...originalLogger,
|
|
logger: {
|
|
child: () => ({
|
|
debug: (arg) => mockDebug(arg),
|
|
info: (arg) => mockInfo(arg),
|
|
http: (arg) => mockHttp(arg),
|
|
error: (arg) => mockError(arg),
|
|
warn: (arg) => mockWarn(arg),
|
|
}),
|
|
},
|
|
};
|
|
});
|
|
|
|
const domain = 'https://registry.npmjs.org';
|
|
|
|
describe('proxy', () => {
|
|
beforeEach(() => {
|
|
nock.cleanAll();
|
|
});
|
|
const defaultRequestOptions = {
|
|
url: 'https://registry.npmjs.org',
|
|
};
|
|
const proxyPath = getConf('proxy1.yaml');
|
|
const conf = new Config(parseConfigFile(proxyPath));
|
|
|
|
describe('error handling', () => {
|
|
test('should be offline uplink', (done) => {
|
|
const tarball = 'https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz';
|
|
nock(domain).get('/jquery/-/jquery-0.0.1.tgz').times(100).replyWithError('some error');
|
|
const proxy = new ProxyStorage(defaultRequestOptions, conf);
|
|
const stream = proxy.fetchTarball(tarball);
|
|
// to test a uplink is offline we have to be try 3 times
|
|
// the default failed request are set to 2
|
|
process.nextTick(function () {
|
|
stream.on('error', function (err) {
|
|
expect(err).not.toBeNull();
|
|
// expect(err.statusCode).toBe(404);
|
|
expect(proxy.failed_requests).toBe(1);
|
|
|
|
const streamSecondTry = proxy.fetchTarball(tarball);
|
|
streamSecondTry.on('error', function (err) {
|
|
expect(err).not.toBeNull();
|
|
/*
|
|
code: 'ENOTFOUND',
|
|
errno: 'ENOTFOUND',
|
|
*/
|
|
// expect(err.statusCode).toBe(404);
|
|
expect(proxy.failed_requests).toBe(2);
|
|
const streamThirdTry = proxy.fetchTarball(tarball);
|
|
streamThirdTry.on('error', function (err: VerdaccioError) {
|
|
expect(err).not.toBeNull();
|
|
expect(err.statusCode).toBe(HTTP_STATUS.INTERNAL_ERROR);
|
|
expect(proxy.failed_requests).toBe(2);
|
|
expect(err.message).toMatch(API_ERROR.UPLINK_OFFLINE);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
test('not found tarball', (done) => {
|
|
nock(domain).get('/jquery/-/jquery-0.0.1.tgz').reply(404);
|
|
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
|
|
const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
|
|
stream.on('error', (response) => {
|
|
expect(response).toEqual(ErrorCode.getNotFound(API_ERROR.NOT_FILE_UPLINK));
|
|
done();
|
|
});
|
|
});
|
|
|
|
test('fail tarball request', (done) => {
|
|
nock(domain).get('/jquery/-/jquery-0.0.1.tgz').replyWithError('boom file');
|
|
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
|
|
const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
|
|
stream.on('error', (response) => {
|
|
expect(response).toEqual(Error('boom file'));
|
|
done();
|
|
});
|
|
});
|
|
|
|
test('bad uplink request', (done) => {
|
|
nock(domain).get('/jquery/-/jquery-0.0.1.tgz').reply(409);
|
|
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
|
|
const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
|
|
stream.on('error', (response) => {
|
|
expect(response).toEqual(ErrorCode.getInternalError(`bad uplink status code: 409`));
|
|
done();
|
|
});
|
|
});
|
|
|
|
test('content length header mismatch', (done) => {
|
|
nock(domain)
|
|
.get('/jquery/-/jquery-0.0.1.tgz')
|
|
// types does not match here with documentation
|
|
// @ts-expect-error
|
|
.replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'), {
|
|
[HEADER_TYPE.CONTENT_LENGTH]: 0,
|
|
});
|
|
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
|
|
const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
|
|
stream.on('error', (response) => {
|
|
expect(response).toEqual(ErrorCode.getInternalError(API_ERROR.CONTENT_MISMATCH));
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|