@cloudpss/fetch 0.5.53 → 0.6.0-alpha.1

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.
@@ -1,460 +0,0 @@
1
- import { getProxyForUrl } from '../dist/proxy-from-env.js';
2
-
3
- /**
4
- * Runs the callback with process.env temporarily set to env.
5
- * @param {Record<string, string>} env env
6
- * @param {()=>void} callback callback
7
- */
8
- function runWithEnv(env, callback) {
9
- var originalEnv = process.env;
10
- process.env = env;
11
- try {
12
- callback();
13
- } finally {
14
- process.env = originalEnv;
15
- }
16
- }
17
-
18
- /**
19
- * Defines a test case that checks whether getProxyForUrl(input) === expected.
20
- * @param {Record<string, string>} env env
21
- * @param {string} expected expected
22
- * @param {string | URL} input input
23
- */
24
- function testProxyUrl(env, expected, input) {
25
- // Copy object to make sure that the in param does not get modified between
26
- // the call of this function and the use of it below.
27
- env = { ...env };
28
-
29
- var title = 'getProxyForUrl(' + JSON.stringify(input) + ')' + ' === ' + JSON.stringify(expected);
30
-
31
- it(title, function () {
32
- var actual;
33
- runWithEnv(env, function () {
34
- actual = getProxyForUrl(input);
35
- });
36
- expect(actual).toBe(expected);
37
- });
38
- }
39
-
40
- describe('getProxyForUrl', function () {
41
- describe('No proxy variables', function () {
42
- var env = { TZ: '' };
43
- testProxyUrl(env, '', 'http://example.com');
44
- testProxyUrl(env, '', 'https://example.com');
45
- testProxyUrl(env, '', 'ftp://example.com');
46
- });
47
-
48
- describe('Invalid URLs', function () {
49
- var env = { ALL_PROXY: 'http://unexpected.proxy' };
50
- testProxyUrl(env, '', 'bogus');
51
- testProxyUrl(env, '', '//example.com');
52
- testProxyUrl(env, '', '://example.com');
53
- testProxyUrl(env, '', '://');
54
- testProxyUrl(env, '', '/path');
55
- testProxyUrl(env, '', '');
56
- testProxyUrl(env, '', 'http:');
57
- testProxyUrl(env, '', 'http:/');
58
- testProxyUrl(env, '', 'http://');
59
- testProxyUrl(env, '', 'prototype://');
60
- testProxyUrl(env, '', 'hasOwnProperty://');
61
- testProxyUrl(env, '', '__proto__://');
62
- });
63
-
64
- describe('http_proxy and HTTP_PROXY', function () {
65
- var env = { HTTP_PROXY: 'http://http-proxy', http_proxy: '' };
66
-
67
- testProxyUrl(env, '', 'https://example');
68
- testProxyUrl(env, 'http://http-proxy', new URL('http://example'));
69
-
70
- env.http_proxy = 'http://priority';
71
- testProxyUrl(env, 'http://priority', 'http://example');
72
- });
73
-
74
- describe('http_proxy with non-sensical value', function () {
75
- // Crazy values should be passed as-is. It is the responsibility of the
76
- // one who launches the application that the value makes sense.
77
- // TODO: Should we be stricter and perform validation?
78
- var env = { HTTP_PROXY: 'Crazy \n!() { ::// }' };
79
- testProxyUrl(env, 'Crazy \n!() { ::// }', 'http://wow');
80
-
81
- // The implementation assumes that the HTTP_PROXY environment variable is
82
- // somewhat reasonable, and if the scheme is missing, it is added.
83
- // Garbage in, garbage out some would say...
84
- env.HTTP_PROXY = 'crazy without colon slash slash';
85
- testProxyUrl(env, 'http://crazy without colon slash slash', 'http://wow');
86
- });
87
-
88
- describe('https_proxy and HTTPS_PROXY', function () {
89
- var env = { HTTP_PROXY: '', HTTPS_PROXY: '', https_proxy: '' };
90
- // Assert that there is no fall back to http_proxy
91
- env.HTTP_PROXY = 'http://unexpected.proxy';
92
- testProxyUrl(env, '', 'https://example');
93
-
94
- env.HTTPS_PROXY = 'http://https-proxy';
95
- testProxyUrl(env, 'http://https-proxy', 'https://example');
96
-
97
- env.https_proxy = 'http://priority';
98
- testProxyUrl(env, 'http://priority', 'https://example');
99
- });
100
-
101
- describe('ftp_proxy', function () {
102
- var env = { FTP_PROXY: '' };
103
- // Something else than http_proxy / https, as a sanity check.
104
- env.FTP_PROXY = 'http://ftp-proxy';
105
-
106
- testProxyUrl(env, 'http://ftp-proxy', 'ftp://example');
107
- testProxyUrl(env, '', 'ftps://example');
108
- });
109
-
110
- describe('all_proxy', function () {
111
- var env = { ALL_PROXY: '', all_proxy: '' };
112
- env.ALL_PROXY = 'http://catch-all';
113
- testProxyUrl(env, 'http://catch-all', 'https://example');
114
-
115
- env.all_proxy = 'http://priority';
116
- testProxyUrl(env, 'http://priority', 'https://example');
117
- });
118
-
119
- describe('all_proxy without scheme', function () {
120
- var env = { ALL_PROXY: '' };
121
- env.ALL_PROXY = 'noscheme';
122
- testProxyUrl(env, 'http://noscheme', 'http://example');
123
- testProxyUrl(env, 'https://noscheme', 'https://example');
124
-
125
- // The module does not impose restrictions on the scheme.
126
- testProxyUrl(env, 'bogus-scheme://noscheme', 'bogus-scheme://example');
127
-
128
- // But the URL should still be valid.
129
- testProxyUrl(env, '', 'bogus');
130
- });
131
-
132
- describe('no_proxy empty', function () {
133
- var env = { HTTPS_PROXY: '', NO_PROXY: '' };
134
- env.HTTPS_PROXY = 'http://proxy';
135
-
136
- // NO_PROXY set but empty.
137
- env.NO_PROXY = '';
138
- testProxyUrl(env, 'http://proxy', 'https://example');
139
-
140
- // No entries in NO_PROXY (comma).
141
- env.NO_PROXY = ',';
142
- testProxyUrl(env, 'http://proxy', 'https://example');
143
-
144
- // No entries in NO_PROXY (whitespace).
145
- env.NO_PROXY = ' ';
146
- testProxyUrl(env, 'http://proxy', 'https://example');
147
-
148
- // No entries in NO_PROXY (multiple whitespace / commas).
149
- env.NO_PROXY = ',\t,,,\n, ,\r';
150
- testProxyUrl(env, 'http://proxy', 'https://example');
151
- });
152
-
153
- describe('no_proxy=example (single host)', function () {
154
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
155
- env.HTTP_PROXY = 'http://proxy';
156
-
157
- env.NO_PROXY = 'example';
158
- testProxyUrl(env, '', 'http://example');
159
- testProxyUrl(env, '', 'http://example:80');
160
- testProxyUrl(env, '', 'http://example:0');
161
- testProxyUrl(env, '', 'http://example:1337');
162
- testProxyUrl(env, '', 'http://sub.example');
163
- testProxyUrl(env, '', 'http://a.b.example');
164
- testProxyUrl(env, 'http://proxy', 'http://prefexample');
165
- testProxyUrl(env, 'http://proxy', 'http://example.no');
166
- testProxyUrl(env, 'http://proxy', 'http://host/example');
167
- });
168
-
169
- describe('no_proxy=sub.example (subdomain)', function () {
170
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
171
- env.HTTP_PROXY = 'http://proxy';
172
-
173
- env.NO_PROXY = 'sub.example';
174
- testProxyUrl(env, 'http://proxy', 'http://example');
175
- testProxyUrl(env, 'http://proxy', 'http://example:80');
176
- testProxyUrl(env, 'http://proxy', 'http://example:0');
177
- testProxyUrl(env, 'http://proxy', 'http://example:1337');
178
- testProxyUrl(env, '', 'http://sub.example');
179
- testProxyUrl(env, '', 'http://no.sub.example');
180
- testProxyUrl(env, 'http://proxy', 'http://sub-example');
181
- testProxyUrl(env, 'http://proxy', 'http://example.sub');
182
- });
183
-
184
- describe('no_proxy=example:80 (host + port)', function () {
185
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
186
- env.HTTP_PROXY = 'http://proxy';
187
-
188
- env.NO_PROXY = 'example:80';
189
- testProxyUrl(env, '', 'http://example');
190
- testProxyUrl(env, '', 'http://example:80');
191
- testProxyUrl(env, 'http://proxy', 'http://example:0');
192
- testProxyUrl(env, 'http://proxy', 'http://example:1337');
193
- testProxyUrl(env, '', 'http://sub.example');
194
- testProxyUrl(env, 'http://proxy', 'http://prefexample');
195
- testProxyUrl(env, 'http://proxy', 'http://example.no');
196
- testProxyUrl(env, '', 'http://a.b.example');
197
- });
198
-
199
- describe('no_proxy=.example (host suffix)', function () {
200
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
201
- env.HTTP_PROXY = 'http://proxy';
202
-
203
- env.NO_PROXY = '.example';
204
- testProxyUrl(env, '', 'http://example');
205
- testProxyUrl(env, '', 'http://example:80');
206
- testProxyUrl(env, '', 'http://example:1337');
207
- testProxyUrl(env, '', 'http://sub.example');
208
- testProxyUrl(env, '', 'http://sub.example:80');
209
- testProxyUrl(env, '', 'http://sub.example:1337');
210
- testProxyUrl(env, '', 'http://a.b.example');
211
- testProxyUrl(env, 'http://proxy', 'http://prefexample');
212
- testProxyUrl(env, 'http://proxy', 'http://example.no');
213
- });
214
-
215
- describe('no_proxy=*', function () {
216
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
217
- env.HTTP_PROXY = 'http://proxy';
218
- env.NO_PROXY = '*';
219
- testProxyUrl(env, '', 'http://example.com');
220
-
221
- env.NO_PROXY = '* , * ,*';
222
- testProxyUrl(env, '', 'http://example.com');
223
-
224
- env.NO_PROXY = '* , * , idk';
225
- testProxyUrl(env, '', 'http://example.com');
226
- });
227
-
228
- describe('no_proxy=*.example (host suffix with *.)', function () {
229
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
230
- env.HTTP_PROXY = 'http://proxy';
231
-
232
- env.NO_PROXY = '*.example';
233
- testProxyUrl(env, '', 'http://example');
234
- testProxyUrl(env, '', 'http://example:80');
235
- testProxyUrl(env, '', 'http://example:1337');
236
- testProxyUrl(env, '', 'http://sub.example');
237
- testProxyUrl(env, '', 'http://sub.example:80');
238
- testProxyUrl(env, '', 'http://sub.example:1337');
239
- testProxyUrl(env, '', 'http://a.b.example');
240
- testProxyUrl(env, 'http://proxy', 'http://prefexample');
241
- testProxyUrl(env, 'http://proxy', 'http://example.no');
242
- });
243
-
244
- describe('no_proxy=*example (substring suffix)', function () {
245
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
246
- env.HTTP_PROXY = 'http://proxy';
247
-
248
- env.NO_PROXY = '*example';
249
- testProxyUrl(env, '', 'http://example');
250
- testProxyUrl(env, '', 'http://example:80');
251
- testProxyUrl(env, '', 'http://example:1337');
252
- testProxyUrl(env, '', 'http://sub.example');
253
- testProxyUrl(env, '', 'http://sub.example:80');
254
- testProxyUrl(env, '', 'http://sub.example:1337');
255
- testProxyUrl(env, '', 'http://a.b.example');
256
- testProxyUrl(env, 'http://proxy', 'http://prefexample');
257
- testProxyUrl(env, 'http://proxy', 'http://example.no');
258
- testProxyUrl(env, 'http://proxy', 'http://host/example');
259
- });
260
-
261
- describe('no_proxy=.*example (arbitrary wildcards are NOT supported)', function () {
262
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
263
- env.HTTP_PROXY = 'http://proxy';
264
-
265
- env.NO_PROXY = '.*example';
266
- testProxyUrl(env, 'http://proxy', 'http://example');
267
- testProxyUrl(env, 'http://proxy', 'http://sub.example');
268
- testProxyUrl(env, 'http://proxy', 'http://sub.example');
269
- testProxyUrl(env, 'http://proxy', 'http://prefexample');
270
- testProxyUrl(env, 'http://proxy', 'http://x.prefexample');
271
- testProxyUrl(env, 'http://proxy', 'http://a.b.example');
272
- });
273
-
274
- describe('no_proxy=.example,*.example (dedupe)', function () {
275
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
276
- env.HTTP_PROXY = 'http://proxy';
277
-
278
- env.NO_PROXY = '*.example,.example';
279
- testProxyUrl(env, '', 'http://example');
280
- testProxyUrl(env, '', 'http://example:80');
281
- testProxyUrl(env, '', 'http://example:1337');
282
- testProxyUrl(env, '', 'http://sub.example');
283
- testProxyUrl(env, '', 'http://sub.example:80');
284
- testProxyUrl(env, '', 'http://sub.example:1337');
285
- testProxyUrl(env, '', 'http://a.b.example');
286
- testProxyUrl(env, 'http://proxy', 'http://prefexample');
287
- testProxyUrl(env, 'http://proxy', 'http://example.no');
288
- testProxyUrl(env, 'http://proxy', 'http://host/example');
289
- });
290
-
291
- describe('no_proxy=[::1],[::2]:80,10.0.0.1,10.0.0.2:80 (IP addresses)', function () {
292
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
293
- env.HTTP_PROXY = 'http://proxy';
294
-
295
- env.NO_PROXY = '[::1],[::2]:80,10.0.0.1,10.0.0.2:80';
296
- testProxyUrl(env, '', 'http://[::1]/');
297
- testProxyUrl(env, '', 'http://[::1]:80/');
298
- testProxyUrl(env, '', 'http://[::1]:1337/');
299
-
300
- testProxyUrl(env, '', 'http://[::2]/');
301
- testProxyUrl(env, '', 'http://[::2]:80/');
302
- testProxyUrl(env, 'http://proxy', 'http://[::2]:1337/');
303
-
304
- testProxyUrl(env, '', 'http://10.0.0.1/');
305
- testProxyUrl(env, '', 'http://10.0.0.1:80/');
306
- testProxyUrl(env, '', 'http://10.0.0.1:1337/');
307
-
308
- testProxyUrl(env, '', 'http://10.0.0.2/');
309
- testProxyUrl(env, '', 'http://10.0.0.2:80/');
310
- testProxyUrl(env, 'http://proxy', 'http://10.0.0.2:1337/');
311
- });
312
-
313
- describe('no_proxy=127.0.0.1/32 (CIDR is NOT supported)', function () {
314
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
315
- env.HTTP_PROXY = 'http://proxy';
316
-
317
- env.NO_PROXY = '127.0.0.1/32';
318
- testProxyUrl(env, 'http://proxy', 'http://127.0.0.1');
319
- testProxyUrl(env, 'http://proxy', 'http://127.0.0.1/32');
320
- });
321
-
322
- describe('no_proxy=127.0.0.1 does NOT match localhost', function () {
323
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
324
- env.HTTP_PROXY = 'http://proxy';
325
-
326
- env.NO_PROXY = '127.0.0.1';
327
- testProxyUrl(env, '', 'http://127.0.0.1');
328
- // We're not performing DNS queries, so this shouldn't match.
329
- testProxyUrl(env, 'http://proxy', 'http://localhost');
330
- });
331
-
332
- describe('no_proxy with protocols that have a default port', function () {
333
- /** @type {Record<string, string>} */
334
- var env = {};
335
- env['WS_PROXY'] = 'http://ws';
336
- env['WSS_PROXY'] = 'http://wss';
337
- env['HTTP_PROXY'] = 'http://http';
338
- env['HTTPS_PROXY'] = 'http://https';
339
- env['FTP_PROXY'] = 'http://ftp';
340
- env['ALL_PROXY'] = 'http://all';
341
-
342
- env['NO_PROXY'] = 'xxx:21,xxx:70,xxx:80,xxx:443';
343
-
344
- testProxyUrl(env, '', 'http://xxx');
345
- testProxyUrl(env, '', 'http://xxx:80');
346
- testProxyUrl(env, 'http://http', 'http://xxx:1337');
347
-
348
- testProxyUrl(env, '', 'ws://xxx');
349
- testProxyUrl(env, '', 'ws://xxx:80');
350
- testProxyUrl(env, 'http://ws', 'ws://xxx:1337');
351
-
352
- testProxyUrl(env, '', 'https://xxx');
353
- testProxyUrl(env, '', 'https://xxx:443');
354
- testProxyUrl(env, 'http://https', 'https://xxx:1337');
355
-
356
- testProxyUrl(env, '', 'wss://xxx');
357
- testProxyUrl(env, '', 'wss://xxx:443');
358
- testProxyUrl(env, 'http://wss', 'wss://xxx:1337');
359
-
360
- testProxyUrl(env, '', 'ftp://xxx');
361
- testProxyUrl(env, '', 'ftp://xxx:21');
362
- testProxyUrl(env, 'http://ftp', 'ftp://xxx:1337');
363
- });
364
-
365
- describe('no_proxy should not be case-sensitive', function () {
366
- var env = { HTTP_PROXY: '', NO_PROXY: '' };
367
- env.HTTP_PROXY = 'http://proxy';
368
- env.NO_PROXY = 'XXX,YYY,ZzZ';
369
-
370
- testProxyUrl(env, '', 'http://xxx');
371
- testProxyUrl(env, '', 'http://XXX');
372
- testProxyUrl(env, '', 'http://yyy');
373
- testProxyUrl(env, '', 'http://YYY');
374
- testProxyUrl(env, '', 'http://ZzZ');
375
- testProxyUrl(env, '', 'http://zZz');
376
- });
377
-
378
- describe('NPM proxy configuration', function () {
379
- describe('npm_config_http_proxy should work', function () {
380
- /** @type {Record<string, string>} */
381
- var env = {};
382
- env['npm_config_http_proxy'] = 'http://http-proxy';
383
-
384
- testProxyUrl(env, '', 'https://example');
385
- testProxyUrl(env, 'http://http-proxy', 'http://example');
386
-
387
- env['npm_config_http_proxy'] = 'http://priority';
388
- testProxyUrl(env, 'http://priority', 'http://example');
389
- });
390
- describe('npm_config_http_proxy should take precedence over HTTP_PROXY and npm_config_proxy', function () {
391
- /** @type {Record<string, string>} */
392
- var env = {};
393
- env['npm_config_http_proxy'] = 'http://http-proxy';
394
- env['npm_config_proxy'] = 'http://unexpected-proxy';
395
- env['HTTP_PROXY'] = 'http://unexpected-proxy';
396
-
397
- testProxyUrl(env, 'http://http-proxy', 'http://example');
398
- });
399
- describe('npm_config_https_proxy should work', function () {
400
- /** @type {Record<string, string>} */
401
- var env = {};
402
- env['npm_config_http_proxy'] = 'http://unexpected.proxy';
403
- testProxyUrl(env, '', 'https://example');
404
-
405
- env['npm_config_https_proxy'] = 'http://https-proxy';
406
- testProxyUrl(env, 'http://https-proxy', 'https://example');
407
-
408
- env['npm_config_https_proxy'] = 'http://priority';
409
- testProxyUrl(env, 'http://priority', 'https://example');
410
- });
411
- describe('npm_config_https_proxy should take precedence over HTTPS_PROXY and npm_config_proxy', function () {
412
- /** @type {Record<string, string>} */
413
- var env = {};
414
- env['npm_config_https_proxy'] = 'http://https-proxy';
415
- env['npm_config_proxy'] = 'http://unexpected-proxy';
416
- env['HTTPS_PROXY'] = 'http://unexpected-proxy';
417
-
418
- testProxyUrl(env, 'http://https-proxy', 'https://example');
419
- });
420
- describe('npm_config_proxy should work', function () {
421
- /** @type {Record<string, string>} */
422
- var env = {};
423
- env['npm_config_proxy'] = 'http://http-proxy';
424
- testProxyUrl(env, 'http://http-proxy', 'http://example');
425
- testProxyUrl(env, 'http://http-proxy', 'https://example');
426
-
427
- env['npm_config_proxy'] = 'http://priority';
428
- testProxyUrl(env, 'http://priority', 'http://example');
429
- testProxyUrl(env, 'http://priority', 'https://example');
430
- });
431
- describe('HTTP_PROXY and HTTPS_PROXY should take precedence over npm_config_proxy', function () {
432
- /** @type {Record<string, string>} */
433
- var env = {};
434
- env['HTTP_PROXY'] = 'http://http-proxy';
435
- env['HTTPS_PROXY'] = 'http://https-proxy';
436
- env['npm_config_proxy'] = 'http://unexpected-proxy';
437
- testProxyUrl(env, 'http://http-proxy', 'http://example');
438
- testProxyUrl(env, 'http://https-proxy', 'https://example');
439
- });
440
- describe('npm_config_no_proxy should work', function () {
441
- /** @type {Record<string, string>} */
442
- var env = {};
443
- env['HTTP_PROXY'] = 'http://proxy';
444
- env['npm_config_no_proxy'] = 'example';
445
-
446
- testProxyUrl(env, '', 'http://example');
447
- testProxyUrl(env, 'http://proxy', 'http://otherwebsite');
448
- });
449
- describe('npm_config_no_proxy should take precedence over NO_PROXY', function () {
450
- /** @type {Record<string, string>} */
451
- var env = {};
452
- env['HTTP_PROXY'] = 'http://proxy';
453
- env['NO_PROXY'] = 'otherwebsite';
454
- env['npm_config_no_proxy'] = 'example';
455
-
456
- testProxyUrl(env, '', 'http://example');
457
- testProxyUrl(env, 'http://proxy', 'http://otherwebsite');
458
- });
459
- });
460
- });
File without changes
File without changes
File without changes
File without changes