@wener/utils 1.1.15 → 1.1.17
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.
- package/dist/cjs/index.cjs +5 -5
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/server.cjs +1 -1
- package/dist/cjs/server.cjs.map +1 -1
- package/dist/esm/index.js +5 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/server.js +1 -1
- package/dist/esm/server.js.map +1 -1
- package/dist/system/index.js +5 -5
- package/dist/system/index.js.map +1 -1
- package/dist/system/server.js +1 -1
- package/dist/system/server.js.map +1 -1
- package/lib/crypto/ulid.js +1 -1
- package/lib/crypto/ulid.js.map +1 -1
- package/lib/fetch/createFetchWith.js +3 -2
- package/lib/fetch/createFetchWith.js.map +1 -1
- package/lib/fetch/createFetchWithLogging.js +1 -1
- package/lib/fetch/createFetchWithLogging.js.map +1 -1
- package/lib/server.js +2 -2
- package/lib/servers/createFetchWithProxy.js +1 -7
- package/lib/servers/createFetchWithProxy.js.map +1 -1
- package/lib/servers/createFetchWithProxyByUndici.js +25 -3
- package/lib/servers/createFetchWithProxyByUndici.js.map +1 -1
- package/lib/servers/createFetchWithRetry.js +70 -0
- package/lib/servers/createFetchWithRetry.js.map +1 -0
- package/lib/servers/md5.js +8 -0
- package/lib/servers/md5.js.map +1 -0
- package/package.json +4 -9
- package/src/crypto/md5.d.ts +1 -0
- package/src/crypto/md5.js +44 -0
- package/src/crypto/ulid.ts +1 -1
- package/src/fetch/createFetchWith.ts +7 -6
- package/src/fetch/createFetchWithLogging.ts +3 -1
- package/src/fetch/index.ts +1 -1
- package/src/fetch/types.ts +1 -0
- package/src/maths/clamp.ts +1 -0
- package/src/server.ts +3 -2
- package/src/servers/createFetchWithProxy.ts +1 -10
- package/src/servers/createFetchWithProxyByUndici.ts +36 -8
- package/src/servers/createFetchWithRetry.ts +95 -0
- package/src/servers/md5.ts +5 -0
- package/lib/servers/createFetchWithProxyByNodeFetch.js +0 -38
- package/lib/servers/createFetchWithProxyByNodeFetch.js.map +0 -1
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
import { FetchLike } from '../fetch';
|
|
2
|
-
import { createFetchWithProxyByNodeFetch } from '../server';
|
|
3
1
|
import { createFetchWithProxyByUndici } from './createFetchWithProxyByUndici';
|
|
4
2
|
|
|
5
|
-
export
|
|
6
|
-
if (!proxy) {
|
|
7
|
-
return fetch || globalThis.fetch;
|
|
8
|
-
}
|
|
9
|
-
return parseInt(process.versions.node) >= 18
|
|
10
|
-
? createFetchWithProxyByUndici({ proxy, fetch })
|
|
11
|
-
: createFetchWithProxyByNodeFetch({ proxy, fetch });
|
|
12
|
-
}
|
|
3
|
+
export const createFetchWithProxy = createFetchWithProxyByUndici;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { MaybePromise } from '
|
|
1
|
+
import { MaybePromise } from '../asyncs/MaybePromise';
|
|
2
2
|
import { FetchLike } from '../fetch';
|
|
3
3
|
|
|
4
4
|
export function createFetchWithProxyByUndici({
|
|
5
5
|
proxy,
|
|
6
|
+
token: _token,
|
|
6
7
|
fetch,
|
|
7
8
|
undici,
|
|
8
9
|
}: {
|
|
9
10
|
proxy?: string;
|
|
11
|
+
token?: string;
|
|
10
12
|
fetch?: FetchLike;
|
|
11
13
|
undici?: MaybePromise<{ fetch: any; ProxyAgent: any }>;
|
|
12
14
|
} = {}): FetchLike {
|
|
@@ -14,21 +16,47 @@ export function createFetchWithProxyByUndici({
|
|
|
14
16
|
return fetch || globalThis.fetch;
|
|
15
17
|
}
|
|
16
18
|
let agent: any;
|
|
19
|
+
// https://github.com/nodejs/undici/blob/main/docs/best-practices/proxy.md
|
|
17
20
|
return async (...args) => {
|
|
18
|
-
const init
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
const init = (args[1] ||= {}) as RequestInit & {
|
|
22
|
+
duplex?: string;
|
|
23
|
+
dispatcher?: any;
|
|
24
|
+
};
|
|
25
|
+
{
|
|
26
|
+
const body = init.body;
|
|
27
|
+
if (typeof body === 'object' && body && (body instanceof ReadableStream || Symbol.asyncIterator in body)) {
|
|
28
|
+
// request.duplex must be set if request.body is ReadableStream or Async Iterables
|
|
29
|
+
init.duplex ||= 'half';
|
|
30
|
+
}
|
|
22
31
|
}
|
|
23
32
|
if (!agent) {
|
|
33
|
+
let uri = proxy;
|
|
34
|
+
let token = _token;
|
|
35
|
+
{
|
|
36
|
+
let u: URL | undefined;
|
|
37
|
+
try {
|
|
38
|
+
u = new URL(proxy);
|
|
39
|
+
} catch (e) {}
|
|
40
|
+
if (!token && u && (u.username || u.password)) {
|
|
41
|
+
token = `Basic ${btoa(`${u.username || ''}:${u.password}`)}`;
|
|
42
|
+
u.username = '';
|
|
43
|
+
u.password = '';
|
|
44
|
+
uri = u.toString();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
24
47
|
// if in next use 'next/dist/compiled/undici'
|
|
25
48
|
undici ||= import('undici');
|
|
26
49
|
const mod = await undici;
|
|
27
|
-
const ProxyAgent = mod.ProxyAgent;
|
|
28
|
-
fetch ||= mod.fetch;
|
|
29
|
-
|
|
50
|
+
const ProxyAgent = mod.ProxyAgent as new (_: any) => any;
|
|
51
|
+
fetch ||= mod.fetch as FetchLike;
|
|
52
|
+
// https://github.com/nodejs/undici/blob/main/docs/api/ProxyAgent.md
|
|
53
|
+
agent = new ProxyAgent({
|
|
54
|
+
uri,
|
|
55
|
+
token,
|
|
56
|
+
});
|
|
30
57
|
// https://github.com/nodejs/node/issues/43187#issuecomment-1134634174
|
|
31
58
|
// (global as any)[Symbol.for('undici.globalDispatcher.1')] = agent;
|
|
59
|
+
// fixme should unwrap error https://github.com/nodejs/undici/issues/1248
|
|
32
60
|
}
|
|
33
61
|
init.dispatcher = agent;
|
|
34
62
|
return await fetch!(...args);
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { FetchLike } from '../fetch';
|
|
2
|
+
import { getGlobalThis } from '../isomorphics/getGlobalThis';
|
|
3
|
+
|
|
4
|
+
type RequestDelayFunction = (attempt: number, error: Error | null, response: Response | null) => number;
|
|
5
|
+
type RequestRetryOnFunction = (
|
|
6
|
+
attempt: number,
|
|
7
|
+
error: Error | null,
|
|
8
|
+
response: Response | null,
|
|
9
|
+
) => boolean | Promise<boolean>;
|
|
10
|
+
|
|
11
|
+
export interface FetchWithRetryOptions {
|
|
12
|
+
fetch?: FetchLike;
|
|
13
|
+
retries?: number;
|
|
14
|
+
retryDelay?: number | RequestDelayFunction;
|
|
15
|
+
retryOn?: number[] | RequestRetryOnFunction;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createFetchWithRetry({
|
|
19
|
+
fetch = getGlobalThis().fetch,
|
|
20
|
+
retries = 3,
|
|
21
|
+
retryDelay = 1000,
|
|
22
|
+
retryOn = [],
|
|
23
|
+
}: FetchWithRetryOptions = {}): FetchLike {
|
|
24
|
+
// https://github.com/jonbern/fetch-retry/blob/master/index.js
|
|
25
|
+
|
|
26
|
+
return function fetchRetry(input: string | Request, init?: RequestInit) {
|
|
27
|
+
return new Promise(function (resolve, reject) {
|
|
28
|
+
var wrappedFetch = function (attempt: number) {
|
|
29
|
+
// As of node 18, this is no longer needed since node comes with native support for fetch:
|
|
30
|
+
/* istanbul ignore next */
|
|
31
|
+
var _input = typeof Request !== 'undefined' && input instanceof Request ? input.clone() : input;
|
|
32
|
+
fetch(_input, init)
|
|
33
|
+
.then(function (response) {
|
|
34
|
+
if (Array.isArray(retryOn) && retryOn.indexOf(response.status) === -1) {
|
|
35
|
+
resolve(response);
|
|
36
|
+
} else if (typeof retryOn === 'function') {
|
|
37
|
+
try {
|
|
38
|
+
return Promise.resolve(retryOn(attempt, null, response))
|
|
39
|
+
.then(function (retryOnResponse) {
|
|
40
|
+
if (retryOnResponse) {
|
|
41
|
+
retry(attempt, null, response);
|
|
42
|
+
} else {
|
|
43
|
+
resolve(response);
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
.catch(reject);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
reject(error);
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
if (attempt < retries) {
|
|
52
|
+
retry(attempt, null, response);
|
|
53
|
+
} else {
|
|
54
|
+
resolve(response);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
})
|
|
59
|
+
.catch(function (error) {
|
|
60
|
+
if (typeof retryOn === 'function') {
|
|
61
|
+
try {
|
|
62
|
+
// eslint-disable-next-line no-undef
|
|
63
|
+
Promise.resolve(retryOn(attempt, error, null))
|
|
64
|
+
.then(function (retryOnResponse) {
|
|
65
|
+
if (retryOnResponse) {
|
|
66
|
+
retry(attempt, error, null);
|
|
67
|
+
} else {
|
|
68
|
+
reject(error);
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
.catch(function (error) {
|
|
72
|
+
reject(error);
|
|
73
|
+
});
|
|
74
|
+
} catch (error) {
|
|
75
|
+
reject(error);
|
|
76
|
+
}
|
|
77
|
+
} else if (attempt < retries) {
|
|
78
|
+
retry(attempt, error, null);
|
|
79
|
+
} else {
|
|
80
|
+
reject(error);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
function retry(attempt: number, error: any, response: Response | null) {
|
|
86
|
+
let delay = typeof retryDelay === 'function' ? retryDelay(attempt, error, response) : retryDelay;
|
|
87
|
+
setTimeout(function () {
|
|
88
|
+
wrappedFetch(++attempt);
|
|
89
|
+
}, delay);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
wrappedFetch(0);
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { getGlobalThis } from '../isomorphics/getGlobalThis.js';
|
|
2
|
-
|
|
3
|
-
function createFetchWithProxyByNodeFetch({
|
|
4
|
-
proxy,
|
|
5
|
-
fetch
|
|
6
|
-
} = {}) {
|
|
7
|
-
const globalThis = getGlobalThis();
|
|
8
|
-
if (!proxy) {
|
|
9
|
-
return fetch || globalThis.fetch;
|
|
10
|
-
}
|
|
11
|
-
let agent;
|
|
12
|
-
const Request = globalThis.Request;
|
|
13
|
-
let NodeRequest;
|
|
14
|
-
let NodeFetch;
|
|
15
|
-
return async (url, init) => {
|
|
16
|
-
if (!agent) {
|
|
17
|
-
const { default: createHttpsProxyAgent } = await import('https-proxy-agent');
|
|
18
|
-
agent = createHttpsProxyAgent(proxy);
|
|
19
|
-
}
|
|
20
|
-
if (!NodeRequest) {
|
|
21
|
-
({ Request: NodeRequest, default: NodeFetch } = await import('node-fetch'));
|
|
22
|
-
}
|
|
23
|
-
fetch || (fetch = NodeFetch);
|
|
24
|
-
if (url instanceof Request) {
|
|
25
|
-
return fetch(new Request(url, { agent }));
|
|
26
|
-
}
|
|
27
|
-
if (url instanceof NodeRequest) {
|
|
28
|
-
return fetch(new NodeRequest(url, { agent }));
|
|
29
|
-
}
|
|
30
|
-
return fetch(url, {
|
|
31
|
-
...init,
|
|
32
|
-
agent
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export { createFetchWithProxyByNodeFetch };
|
|
38
|
-
//# sourceMappingURL=createFetchWithProxyByNodeFetch.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createFetchWithProxyByNodeFetch.js","sources":["../../src/servers/createFetchWithProxyByNodeFetch.ts"],"sourcesContent":["import { type FetchLike } from '../fetch';\nimport { getGlobalThis } from '../isomorphics/getGlobalThis';\n\nexport function createFetchWithProxyByNodeFetch({\n proxy,\n fetch,\n}: { proxy?: string; fetch?: FetchLike } = {}): FetchLike {\n const globalThis = getGlobalThis();\n if (!proxy) {\n return fetch || globalThis.fetch;\n }\n\n let agent: any;\n const Request = globalThis.Request;\n let NodeRequest: any;\n let NodeFetch: any;\n return async (url, init?: RequestInit) => {\n if (!agent) {\n const { default: createHttpsProxyAgent } = await import('https-proxy-agent');\n agent = (createHttpsProxyAgent as any)(proxy);\n }\n\n // node-fetch 才可以,node v18 fetch 不支持\n if (!NodeRequest) {\n ({ Request: NodeRequest, default: NodeFetch } = await import('node-fetch'));\n }\n\n fetch ||= NodeFetch;\n\n if (url instanceof Request) {\n return (fetch as any)(new Request(url, { agent } as any));\n }\n if ((url as any) instanceof NodeRequest) {\n return (fetch as any)(new NodeRequest(url, { agent } as any));\n }\n return (fetch as any)(url, {\n ...init,\n agent,\n } as any);\n };\n}\n"],"names":[],"mappings":";;AAGO,SAAS,+BAAgC,CAAA;AAAA,EAC9C,KAAA;AAAA,EACA,KAAA;AACF,CAAA,GAA2C,EAAe,EAAA;AACxD,EAAA,MAAM,aAAa,aAAc,EAAA,CAAA;AACjC,EAAA,IAAI,CAAC,KAAO,EAAA;AACV,IAAA,OAAO,SAAS,UAAW,CAAA,KAAA,CAAA;AAAA,GAC7B;AAEA,EAAI,IAAA,KAAA,CAAA;AACJ,EAAA,MAAM,UAAU,UAAW,CAAA,OAAA,CAAA;AAC3B,EAAI,IAAA,WAAA,CAAA;AACJ,EAAI,IAAA,SAAA,CAAA;AACJ,EAAO,OAAA,OAAO,KAAK,IAAuB,KAAA;AACxC,IAAA,IAAI,CAAC,KAAO,EAAA;AACV,MAAA,MAAM,EAAE,OAAS,EAAA,qBAAA,EAA0B,GAAA,MAAM,OAAO,mBAAmB,CAAA,CAAA;AAC3E,MAAA,KAAA,GAAS,sBAA8B,KAAK,CAAA,CAAA;AAAA,KAC9C;AAGA,IAAA,IAAI,CAAC,WAAa,EAAA;AAChB,MAAC,CAAA,EAAE,SAAS,WAAa,EAAA,OAAA,EAAS,WAAc,GAAA,MAAM,OAAO,YAAY,CAAA,EAAA;AAAA,KAC3E;AAEA,IAAU,KAAA,KAAA,KAAA,GAAA,SAAA,CAAA,CAAA;AAEV,IAAA,IAAI,eAAe,OAAS,EAAA;AAC1B,MAAA,OAAQ,MAAc,IAAI,OAAA,CAAQ,KAAK,EAAE,KAAA,EAAc,CAAC,CAAA,CAAA;AAAA,KAC1D;AACA,IAAA,IAAK,eAAuB,WAAa,EAAA;AACvC,MAAA,OAAQ,MAAc,IAAI,WAAA,CAAY,KAAK,EAAE,KAAA,EAAc,CAAC,CAAA,CAAA;AAAA,KAC9D;AACA,IAAA,OAAQ,MAAc,GAAK,EAAA;AAAA,MACzB,GAAG,IAAA;AAAA,MACH,KAAA;AAAA,KACM,CAAA,CAAA;AAAA,GACV,CAAA;AACF;;;;"}
|