@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,128 +0,0 @@
1
- /**
2
- * Ref:
3
- * @see https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/
4
- */
5
-
6
- const DEFAULT_PORTS = Object.freeze({
7
- 'ftp:': 21,
8
- 'http:': 80,
9
- 'https:': 443,
10
- 'ws:': 80,
11
- 'wss:': 443,
12
- } as Record<string, number>);
13
-
14
- const NEVER_PROXY_PROTOCOL = new Set(['file:', 'data:', 'blob:']);
15
-
16
- /**
17
- * Get the value for an environment variable.
18
- */
19
- function getEnv(key: string): string {
20
- return process.env[key.toLowerCase()] || process.env[key.toUpperCase()] || '';
21
- }
22
-
23
- /** parsed no proxy pattern */
24
- type Pattern = {
25
- suffix: string;
26
- port: number | undefined;
27
- };
28
-
29
- /** parse no proxy */
30
- function parseNoProxy(noProxy: string | undefined): boolean | Pattern[] {
31
- if (!noProxy) return true;
32
- noProxy = noProxy.trim().toLowerCase();
33
- if (noProxy === '*') return false;
34
-
35
- const records = noProxy.split(/[,\s]+/);
36
- const patterns: Pattern[] = [];
37
- let all = false;
38
- for (const record of records) {
39
- const trimmed = record.trim();
40
- if (!trimmed) continue;
41
- const parsed = /^(.+):(\d+)$/.exec(trimmed);
42
- let parsedHostname = parsed ? parsed[1]! : trimmed;
43
- if (parsedHostname.startsWith('*.')) {
44
- parsedHostname = parsedHostname.slice(2);
45
- } else if (parsedHostname.startsWith('.') || parsedHostname.startsWith('*')) {
46
- parsedHostname = parsedHostname.slice(1);
47
- }
48
- const parsedPort = parsed ? Number.parseInt(parsed[2]!) : undefined;
49
- if (!parsedHostname && !parsedPort) {
50
- all = true;
51
- break;
52
- }
53
- // deduplicate
54
- if (patterns.some((item) => item.suffix === parsedHostname && item.port === parsedPort)) continue;
55
- patterns.push({
56
- suffix: parsedHostname,
57
- port: parsedPort,
58
- });
59
- }
60
- if (all) return false;
61
- if (!patterns.length) return true;
62
- return patterns;
63
- }
64
-
65
- let noProxyPatternsCache: boolean | readonly Pattern[];
66
- let noProxyCache: string;
67
-
68
- /**
69
- * Determines whether a given URL should be proxied.
70
- */
71
- function shouldProxy(hostname: string, port: number): boolean {
72
- const noProxy = getEnv('npm_config_no_proxy') || getEnv('no_proxy');
73
- let patterns;
74
- if (noProxy === noProxyCache) {
75
- patterns = noProxyPatternsCache;
76
- } else {
77
- noProxyCache = noProxy;
78
- patterns = noProxyPatternsCache = parseNoProxy(noProxy);
79
- }
80
-
81
- if (typeof patterns == 'boolean') {
82
- return patterns;
83
- }
84
- for (const pattern of patterns) {
85
- if (pattern.port && pattern.port !== port) continue;
86
- if (hostname === pattern.suffix) return false;
87
- if (
88
- hostname.length > pattern.suffix.length &&
89
- hostname.endsWith(pattern.suffix) &&
90
- hostname[hostname.length - pattern.suffix.length - 1] === '.'
91
- )
92
- return false;
93
- }
94
- return true;
95
- }
96
-
97
- /**
98
- * getProxyForUrl
99
- */
100
- export function getProxyForUrl(url: URL | string): string {
101
- try {
102
- if (typeof url === 'string') url = new URL(url);
103
- } catch {
104
- return '';
105
- }
106
-
107
- const { protocol, hostname, port } = url;
108
- if (!hostname || NEVER_PROXY_PROTOCOL.has(protocol)) {
109
- return '';
110
- }
111
-
112
- const portNumber = (port ? Number.parseInt(port) : DEFAULT_PORTS[protocol]!) || 0;
113
- if (!shouldProxy(hostname, portNumber)) {
114
- return ''; // Don't proxy URLs that match NO_PROXY.
115
- }
116
-
117
- const proto = protocol.slice(0, -1);
118
- const proxy =
119
- getEnv('npm_config_' + proto + '_proxy') ||
120
- getEnv(proto + '_proxy') ||
121
- getEnv('npm_config_proxy') ||
122
- getEnv('all_proxy');
123
- if (proxy && !proxy.includes('://')) {
124
- // Missing scheme in proxy, default to the requested URL's scheme.
125
- return proto + '://' + proxy;
126
- }
127
- return proxy;
128
- }
package/src/undici.d.ts DELETED
@@ -1,30 +0,0 @@
1
- /* eslint-disable jsdoc/require-jsdoc */
2
-
3
- declare module 'undici/lib/core/symbols.js' {
4
- const kClose: unique symbol;
5
- const kDestroy: unique symbol;
6
- const kDispatch: unique symbol;
7
-
8
- export = {
9
- kClose,
10
- kDestroy,
11
- kDispatch,
12
- } as const;
13
- }
14
-
15
- declare module 'undici/lib/dispatcher/dispatcher-base.js' {
16
- import { Dispatcher } from 'undici';
17
- import symbols from 'undici/lib/core/symbols.js';
18
- class DispatcherBase extends Dispatcher {
19
- // Should not error.
20
- // eslint-disable-next-line @typescript-eslint/unified-signatures
21
- protected [symbols.kDestroy](err?: Error | null): Promise<void>;
22
- // Should not error.
23
- protected [symbols.kClose](): Promise<void>;
24
- protected [symbols.kDispatch](
25
- options: Dispatcher.DispatchOptions,
26
- handler: Dispatcher.DispatchHandlers,
27
- ): boolean;
28
- }
29
- export = DispatcherBase;
30
- }
package/tests/e2e.js DELETED
@@ -1,75 +0,0 @@
1
- import * as Index from '../dist/index.js';
2
- import * as Node from '../dist/impl-node.js';
3
- import * as Browser from '../dist/impl-browser.js';
4
-
5
- describe('exports defined', () => {
6
- it('index', () => {
7
- expect(typeof Index.File).toBe('function');
8
- expect(typeof Index.FormData).toBe('function');
9
- expect(typeof Index.Headers).toBe('function');
10
- expect(typeof Index.Request).toBe('function');
11
- expect(typeof Index.Response).toBe('function');
12
- expect(typeof Index.WebSocket).toBe('function');
13
- expect(typeof Index.fetch).toBe('function');
14
- expect(Index.default).toBe(Index.fetch);
15
- });
16
- it('node', () => {
17
- expect(typeof Node.File).toBe('function');
18
- expect(typeof Node.FormData).toBe('function');
19
- expect(typeof Node.Headers).toBe('function');
20
- expect(typeof Node.Request).toBe('function');
21
- expect(typeof Node.Response).toBe('function');
22
- expect(typeof Node.WebSocket).toBe('function');
23
- expect(typeof Node.fetch).toBe('function');
24
- expect(Node.default).toBe(Node.fetch);
25
- });
26
- it('browser', () => {
27
- expect(typeof Browser.File).toBe('function');
28
- expect(typeof Browser.FormData).toBe('function');
29
- expect(typeof Browser.Headers).toBe('function');
30
- expect(typeof Browser.Request).toBe('function');
31
- expect(typeof Browser.Response).toBe('function');
32
- // expect(typeof Browser.WebSocket).toBe('function');
33
- expect(typeof Browser.fetch).toBe('function');
34
- expect(Browser.default).toBe(Browser.fetch);
35
- });
36
- });
37
-
38
- describe('node fetch works', () => {
39
- it('can visit http://downloads.cloudpss.net', async () => {
40
- const resp = await Node.fetch('http://downloads.cloudpss.net', {
41
- headers: { connection: 'close' },
42
- });
43
- expect(resp).toBeInstanceOf(Node.Response);
44
- expect(resp.url).toBe('https://downloads.cloudpss.net/');
45
- const data = await resp.json();
46
- expect(data).toEqual(
47
- expect.objectContaining({
48
- name: '/',
49
- path: '/',
50
- items: expect.any(Array),
51
- can_go_up: false,
52
- }),
53
- );
54
- });
55
-
56
- it('can visit via proxy', async () => {
57
- const resp = await Node.fetch('http://api.github.com/repos/CloudPSS/docs', {
58
- headers: { connection: 'close' },
59
- });
60
- expect(resp).toBeInstanceOf(Node.Response);
61
- expect(resp.url).toBe('https://api.github.com/repos/CloudPSS/docs');
62
- const data = await resp.json();
63
- expect(data).toEqual(
64
- expect.objectContaining({
65
- id: expect.any(Number),
66
- name: expect.any(String),
67
- full_name: expect.any(String),
68
- owner: expect.objectContaining({
69
- id: expect.any(Number),
70
- login: expect.any(String),
71
- }),
72
- }),
73
- );
74
- });
75
- });