@verdaccio/proxy 6.0.0-6-next.20 → 6.0.0-6-next.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,240 +2,178 @@ import nock from 'nock';
2
2
  import path from 'path';
3
3
 
4
4
  import { Config, parseConfigFile } from '@verdaccio/config';
5
- import { API_ERROR, HEADER_TYPE, HTTP_STATUS, VerdaccioError, errorUtils } from '@verdaccio/core';
5
+ import { setup } from '@verdaccio/logger';
6
6
 
7
- import { ProxyStorage } from '../src/up-storage';
7
+ import { ProxyStorage } from '../src';
8
+
9
+ setup();
8
10
 
9
11
  const getConf = (name) => path.join(__dirname, '/conf', name);
10
12
 
11
- const mockDebug = jest.fn();
12
- const mockInfo = jest.fn();
13
- const mockHttp = jest.fn();
14
- const mockError = jest.fn();
15
- const mockWarn = jest.fn();
16
- jest.mock('@verdaccio/logger', () => {
17
- const originalLogger = jest.requireActual('@verdaccio/logger');
13
+ // // mock to get the headers fixed value
14
+ jest.mock('crypto', () => {
18
15
  return {
19
- ...originalLogger,
20
- logger: {
21
- child: () => ({
22
- debug: (arg) => mockDebug(arg),
23
- info: (arg) => mockInfo(arg),
24
- http: (arg) => mockHttp(arg),
25
- error: (arg) => mockError(arg),
26
- warn: (arg) => mockWarn(arg),
27
- }),
16
+ randomBytes: (): { toString: () => string } => {
17
+ return {
18
+ toString: (): string => 'foo-random-bytes',
19
+ };
20
+ },
21
+ pseudoRandomBytes: (): { toString: () => string } => {
22
+ return {
23
+ toString: (): string => 'foo-phseudo-bytes',
24
+ };
28
25
  },
29
26
  };
30
27
  });
31
28
 
32
- const domain = 'https://registry.npmjs.org';
33
-
34
- describe('proxy', () => {
29
+ describe('tarball proxy', () => {
35
30
  beforeEach(() => {
36
31
  nock.cleanAll();
37
32
  nock.abortPendingRequests();
38
33
  jest.clearAllMocks();
39
34
  });
40
35
  const defaultRequestOptions = {
41
- url: domain,
36
+ url: 'https://registry.verdaccio.org',
42
37
  };
38
+
43
39
  const proxyPath = getConf('proxy1.yaml');
44
40
  const conf = new Config(parseConfigFile(proxyPath));
45
41
 
46
- describe('fetchTarball', () => {
47
- test('get file tarball no content-length', (done) => {
48
- nock(domain)
42
+ describe('fetchTarballNext', () => {
43
+ test('get file tarball fetch', (done) => {
44
+ nock('https://registry.verdaccio.org')
49
45
  .get('/jquery/-/jquery-0.0.1.tgz')
50
46
  .replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'));
51
47
  const prox1 = new ProxyStorage(defaultRequestOptions, conf);
52
- const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
53
- stream.on('data', (data) => {
54
- expect(data).toBeDefined();
48
+ const stream = prox1.fetchTarballNext(
49
+ 'https://registry.verdaccio.org/jquery/-/jquery-0.0.1.tgz',
50
+ {}
51
+ );
52
+ stream.on('response', () => {
55
53
  done();
56
54
  });
55
+ stream.on('error', (err) => {
56
+ done(err);
57
+ });
57
58
  });
58
59
 
59
- test('get file tarball correct content-length', (done) => {
60
- nock(domain)
60
+ test.skip('get file tarball handle retries', (done) => {
61
+ nock('https://registry.verdaccio.org')
61
62
  .get('/jquery/-/jquery-0.0.1.tgz')
62
- // types does not match here with documentation
63
- // @ts-expect-error
64
- .replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'), {
65
- [HEADER_TYPE.CONTENT_LENGTH]: 277,
66
- });
63
+ .twice()
64
+ .reply(500, 'some-text')
65
+ .get('/jquery/-/jquery-0.0.1.tgz')
66
+ .once()
67
+ .replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'));
67
68
  const prox1 = new ProxyStorage(defaultRequestOptions, conf);
68
- const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
69
- stream.on(HEADER_TYPE.CONTENT_LENGTH, (data) => {
70
- expect(data).toEqual('277');
69
+ const stream = prox1.fetchTarballNext(
70
+ 'https://registry.verdaccio.org/jquery/-/jquery-0.0.1.tgz',
71
+ { retry: { limit: 2 } }
72
+ );
73
+ stream.on('error', () => {
74
+ // FIXME: stream should have handle 2 retry
71
75
  done();
72
76
  });
73
77
  });
74
-
75
- describe('error handling', () => {
76
- test('should be offline uplink', (done) => {
77
- const tarball = 'https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz';
78
- nock(domain).get('/jquery/-/jquery-0.0.1.tgz').times(100).replyWithError('some error');
79
- const proxy = new ProxyStorage(defaultRequestOptions, conf);
80
- const stream = proxy.fetchTarball(tarball);
81
- // to test a uplink is offline we have to be try 3 times
82
- // the default failed request are set to 2
83
- process.nextTick(function () {
84
- stream.on('error', function (err) {
85
- expect(err).not.toBeNull();
86
- // expect(err.statusCode).toBe(404);
87
- expect(proxy.failed_requests).toBe(1);
88
-
89
- const streamSecondTry = proxy.fetchTarball(tarball);
90
- streamSecondTry.on('error', function (err) {
91
- expect(err).not.toBeNull();
92
- /*
93
- code: 'ENOTFOUND',
94
- errno: 'ENOTFOUND',
95
- */
96
- // expect(err.statusCode).toBe(404);
97
- expect(proxy.failed_requests).toBe(2);
98
- const streamThirdTry = proxy.fetchTarball(tarball);
99
- streamThirdTry.on('error', function (err: VerdaccioError) {
100
- expect(err).not.toBeNull();
101
- expect(err.statusCode).toBe(HTTP_STATUS.INTERNAL_ERROR);
102
- expect(proxy.failed_requests).toBe(2);
103
- expect(err.message).toMatch(API_ERROR.UPLINK_OFFLINE);
104
- done();
105
- });
106
- });
107
- });
108
- });
109
- });
110
-
111
- test('not found tarball', (done) => {
112
- nock(domain).get('/jquery/-/jquery-0.0.1.tgz').reply(404);
113
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
114
- const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
115
- stream.on('error', (response) => {
116
- expect(response).toEqual(errorUtils.getNotFound(API_ERROR.NOT_FILE_UPLINK));
117
- done();
118
- });
119
- });
120
-
121
- test('fail tarball request', (done) => {
122
- nock(domain).get('/jquery/-/jquery-0.0.1.tgz').replyWithError('boom file');
123
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
124
- const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
125
- stream.on('error', (response) => {
126
- expect(response).toEqual(Error('boom file'));
127
- done();
128
- });
129
- });
130
-
131
- test('bad uplink request', (done) => {
132
- nock(domain).get('/jquery/-/jquery-0.0.1.tgz').reply(409);
133
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
134
- const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
135
- stream.on('error', (response) => {
136
- expect(response).toEqual(errorUtils.getInternalError(`bad uplink status code: 409`));
137
- done();
138
- });
139
- });
140
-
141
- test('content length header mismatch', (done) => {
142
- nock(domain)
143
- .get('/jquery/-/jquery-0.0.1.tgz')
144
- // types does not match here with documentation
145
- // @ts-expect-error
146
- .replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'), {
147
- [HEADER_TYPE.CONTENT_LENGTH]: 0,
148
- });
149
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
150
- const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
151
- stream.on('error', (response) => {
152
- expect(response).toEqual(errorUtils.getInternalError(API_ERROR.CONTENT_MISMATCH));
153
- done();
154
- });
155
- });
156
- });
157
- });
158
-
159
- describe('getRemoteMetadata', () => {
160
- describe('basic requests', () => {
161
- test('proxy call with etag', (done) => {
162
- nock(domain)
163
- .get('/jquery')
164
- .reply(
165
- 200,
166
- { body: 'test' },
167
- {
168
- etag: () => `_ref_4444`,
169
- }
170
- );
171
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
172
- prox1.getRemoteMetadata('jquery', {}, (_error, body, etag) => {
173
- expect(etag).toEqual('_ref_4444');
174
- expect(body).toEqual({ body: 'test' });
175
- done();
176
- });
177
- });
178
-
179
- test('proxy call with etag as option', (done) => {
180
- nock(domain)
181
- .get('/jquery')
182
- .reply(
183
- 200,
184
- { body: 'test' },
185
- {
186
- etag: () => `_ref_4444`,
187
- }
188
- );
189
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
190
- prox1.getRemoteMetadata('jquery', { etag: 'rev_3333' }, (_error, body, etag) => {
191
- expect(etag).toEqual('_ref_4444');
192
- expect(body).toEqual({ body: 'test' });
193
- done();
194
- });
195
- });
196
-
197
- test('proxy not found', (done) => {
198
- nock(domain).get('/jquery').reply(404);
199
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
200
- prox1.getRemoteMetadata('jquery', { etag: 'rev_3333' }, (error) => {
201
- expect(error).toEqual(errorUtils.getNotFound(API_ERROR.NOT_PACKAGE_UPLINK));
202
- done();
203
- });
204
- });
205
- });
206
-
207
- describe('error handling', () => {
208
- test('reply with error', (done) => {
209
- nock(domain).get('/jquery').replyWithError('something awful happened');
210
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
211
- prox1.getRemoteMetadata('jquery', {}, (error) => {
212
- expect(error).toEqual(new Error('something awful happened'));
213
- done();
214
- });
215
- });
216
-
217
- test('reply with bad body json format', (done) => {
218
- nock(domain).get('/jquery').reply(200, 'some-text');
219
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
220
- prox1.getRemoteMetadata('jquery', {}, (error) => {
221
- expect(error).toEqual(new SyntaxError('Unexpected token s in JSON at position 0'));
222
- done();
223
- });
224
- });
225
-
226
- test('400 error proxy call', (done) => {
227
- nock(domain).get('/jquery').reply(409);
228
- const prox1 = new ProxyStorage(defaultRequestOptions, conf);
229
- prox1.getRemoteMetadata('jquery', {}, (error) => {
230
- expect(error.statusCode).toEqual(500);
231
- expect(mockInfo).toHaveBeenCalledTimes(1);
232
- expect(mockHttp).toHaveBeenCalledWith({
233
- request: { method: 'GET', url: `${domain}/jquery` },
234
- status: 409,
235
- });
236
- done();
237
- });
238
- });
239
- });
240
78
  });
241
79
  });
80
+ // test('get file tarball correct content-length', (done) => {
81
+ // nock(domain)
82
+ // .get('/jquery/-/jquery-0.0.1.tgz')
83
+ // // types does not match here with documentation
84
+ // // @ts-expect-error
85
+ // .replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'), {
86
+ // [HEADER_TYPE.CONTENT_LENGTH]: 277,
87
+ // });
88
+ // const prox1 = new ProxyStorage(defaultRequestOptions, conf);
89
+ // const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
90
+ // stream.on(HEADER_TYPE.CONTENT_LENGTH, (data) => {
91
+ // expect(data).toEqual('277');
92
+ // done();
93
+ // });
94
+ // });
95
+
96
+ // describe('error handling', () => {
97
+ // test('should be offline uplink', (done) => {
98
+ // const tarball = 'https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz';
99
+ // nock(domain).get('/jquery/-/jquery-0.0.1.tgz').times(100).replyWithError('some error');
100
+ // const proxy = new ProxyStorage(defaultRequestOptions, conf);
101
+ // const stream = proxy.fetchTarball(tarball);
102
+ // // to test a uplink is offline we have to be try 3 times
103
+ // // the default failed request are set to 2
104
+ // process.nextTick(function () {
105
+ // stream.on('error', function (err) {
106
+ // expect(err).not.toBeNull();
107
+ // // expect(err.statusCode).toBe(404);
108
+ // expect(proxy.failed_requests).toBe(1);
109
+
110
+ // const streamSecondTry = proxy.fetchTarball(tarball);
111
+ // streamSecondTry.on('error', function (err) {
112
+ // expect(err).not.toBeNull();
113
+ // /*
114
+ // code: 'ENOTFOUND',
115
+ // errno: 'ENOTFOUND',
116
+ // */
117
+ // // expect(err.statusCode).toBe(404);
118
+ // expect(proxy.failed_requests).toBe(2);
119
+ // const streamThirdTry = proxy.fetchTarball(tarball);
120
+ // streamThirdTry.on('error', function (err: VerdaccioError) {
121
+ // expect(err).not.toBeNull();
122
+ // expect(err.statusCode).toBe(HTTP_STATUS.INTERNAL_ERROR);
123
+ // expect(proxy.failed_requests).toBe(2);
124
+ // expect(err.message).toMatch(API_ERROR.UPLINK_OFFLINE);
125
+ // done();
126
+ // });
127
+ // });
128
+ // });
129
+ // });
130
+ // });
131
+
132
+ // test('not found tarball', (done) => {
133
+ // nock(domain).get('/jquery/-/jquery-0.0.1.tgz').reply(404);
134
+ // const prox1 = new ProxyStorage(defaultRequestOptions, conf);
135
+ // const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
136
+ // stream.on('error', (response) => {
137
+ // expect(response).toEqual(errorUtils.getNotFound(API_ERROR.NOT_FILE_UPLINK));
138
+ // done();
139
+ // });
140
+ // });
141
+
142
+ // test('fail tarball request', (done) => {
143
+ // nock(domain).get('/jquery/-/jquery-0.0.1.tgz').replyWithError('boom file');
144
+ // const prox1 = new ProxyStorage(defaultRequestOptions, conf);
145
+ // const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
146
+ // stream.on('error', (response) => {
147
+ // expect(response).toEqual(Error('boom file'));
148
+ // done();
149
+ // });
150
+ // });
151
+
152
+ // test('bad uplink request', (done) => {
153
+ // nock(domain).get('/jquery/-/jquery-0.0.1.tgz').reply(409);
154
+ // const prox1 = new ProxyStorage(defaultRequestOptions, conf);
155
+ // const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
156
+ // stream.on('error', (response) => {
157
+ // expect(response).toEqual(errorUtils.getInternalError(`bad uplink status code: 409`));
158
+ // done();
159
+ // });
160
+ // });
161
+
162
+ // test('content length header mismatch', (done) => {
163
+ // nock(domain)
164
+ // .get('/jquery/-/jquery-0.0.1.tgz')
165
+ // // types does not match here with documentation
166
+ // // @ts-expect-error
167
+ // .replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'), {
168
+ // [HEADER_TYPE.CONTENT_LENGTH]: 0,
169
+ // });
170
+ // const prox1 = new ProxyStorage(defaultRequestOptions, conf);
171
+ // const stream = prox1.fetchTarball('https://registry.npmjs.org/jquery/-/jquery-0.0.1.tgz');
172
+ // stream.on('error', (response) => {
173
+ // expect(response).toEqual(errorUtils.getInternalError(API_ERROR.CONTENT_MISMATCH));
174
+ // done();
175
+ // });
176
+ // });
177
+ // });
178
+ // });
179
+ // });
package/tsconfig.json CHANGED
@@ -10,12 +10,6 @@
10
10
  {
11
11
  "path": "../config"
12
12
  },
13
- {
14
- "path": "../core/commons-api"
15
- },
16
- {
17
- "path": "../core/local-storage"
18
- },
19
13
  {
20
14
  "path": "../core/streams"
21
15
  },