vigor-fetch 1.0.6 → 1.0.7
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/index.d.mts +76 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +283 -187
- package/dist/index.mjs +264 -186
- package/package.json +7 -7
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
interface VigorErrorOptions {
|
|
2
|
+
url?: string | null;
|
|
3
|
+
status?: number;
|
|
4
|
+
message?: string;
|
|
5
|
+
data?: any;
|
|
6
|
+
}
|
|
7
|
+
interface VigorFetchConfig {
|
|
8
|
+
path: string;
|
|
9
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | null;
|
|
10
|
+
offset: RequestInit;
|
|
11
|
+
headers: Record<string, string>;
|
|
12
|
+
body: any;
|
|
13
|
+
count: number;
|
|
14
|
+
max: number;
|
|
15
|
+
wait: number;
|
|
16
|
+
backoff: number;
|
|
17
|
+
unretry: Set<number>;
|
|
18
|
+
retryHeader: string[];
|
|
19
|
+
original: boolean;
|
|
20
|
+
parse: keyof Response | null;
|
|
21
|
+
beforeRequest: Array<(opt: RequestInit) => Promise<Partial<RequestInit> | void> | Partial<RequestInit> | void>;
|
|
22
|
+
afterRequest: Array<(res: Response) => Promise<Response | void> | Response | void>;
|
|
23
|
+
beforeResponse: Array<(res: Response) => Promise<Response> | Response>;
|
|
24
|
+
afterResponse: Array<(data: any) => Promise<any> | any>;
|
|
25
|
+
onError: Array<(err: any) => Promise<any> | any>;
|
|
26
|
+
query: Record<string, any>;
|
|
27
|
+
jitter: number;
|
|
28
|
+
}
|
|
29
|
+
declare class VigorFetch<T = any> {
|
|
30
|
+
private _origin;
|
|
31
|
+
private _config;
|
|
32
|
+
constructor(origin: string, config?: VigorFetchConfig);
|
|
33
|
+
private _next;
|
|
34
|
+
path(arg: string): VigorFetch<T>;
|
|
35
|
+
method(arg: VigorFetchConfig['method']): VigorFetch<T>;
|
|
36
|
+
offset(arg: RequestInit): VigorFetch<T>;
|
|
37
|
+
headers(arg: Record<string, string>): VigorFetch<T>;
|
|
38
|
+
body(arg: any): VigorFetch<T>;
|
|
39
|
+
count(arg: number): VigorFetch<T>;
|
|
40
|
+
max(arg: number): VigorFetch<T>;
|
|
41
|
+
wait(arg: number): VigorFetch<T>;
|
|
42
|
+
backoff(arg: number): VigorFetch<T>;
|
|
43
|
+
unretry(arg: number[]): VigorFetch<T>;
|
|
44
|
+
retryHeader(...arg: string[]): VigorFetch<T>;
|
|
45
|
+
original(arg: boolean): VigorFetch<T>;
|
|
46
|
+
parse(arg: keyof Response): VigorFetch<T>;
|
|
47
|
+
query(arg: Record<string, any>): VigorFetch<T>;
|
|
48
|
+
jitter(arg: number): VigorFetch<T>;
|
|
49
|
+
beforeRequest(...arg: VigorFetchConfig['beforeRequest']): VigorFetch<T>;
|
|
50
|
+
afterRequest(...arg: VigorFetchConfig['afterRequest']): VigorFetch<T>;
|
|
51
|
+
beforeResponse(...arg: VigorFetchConfig['beforeResponse']): VigorFetch<T>;
|
|
52
|
+
afterResponse(...arg: VigorFetchConfig['afterResponse']): VigorFetch<T>;
|
|
53
|
+
onError(...arg: VigorFetchConfig['onError']): VigorFetch<T>;
|
|
54
|
+
request(): Promise<T>;
|
|
55
|
+
}
|
|
56
|
+
interface VigorAllConfig {
|
|
57
|
+
limit: number;
|
|
58
|
+
jitter: number;
|
|
59
|
+
promises: Array<() => Promise<any>>;
|
|
60
|
+
}
|
|
61
|
+
declare class VigorAll {
|
|
62
|
+
private _config;
|
|
63
|
+
constructor(config?: VigorAllConfig);
|
|
64
|
+
private _next;
|
|
65
|
+
limit(arg: number): VigorAll;
|
|
66
|
+
jitter(arg: number): VigorAll;
|
|
67
|
+
promises(...args: Array<() => Promise<any>>): VigorAll;
|
|
68
|
+
request(): Promise<any[]>;
|
|
69
|
+
}
|
|
70
|
+
declare class Vigor {
|
|
71
|
+
fetch<T = any>(origin: string, config?: VigorFetchConfig): VigorFetch<T>;
|
|
72
|
+
all(config?: VigorAllConfig): VigorAll;
|
|
73
|
+
}
|
|
74
|
+
declare const vigor: Vigor;
|
|
75
|
+
|
|
76
|
+
export { type VigorAllConfig, type VigorErrorOptions, type VigorFetchConfig, vigor as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
interface VigorErrorOptions {
|
|
2
2
|
url?: string | null;
|
|
3
3
|
status?: number;
|
|
4
4
|
message?: string;
|
|
5
5
|
data?: any;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
interface VigorFetchConfig {
|
|
8
8
|
path: string;
|
|
9
9
|
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | null;
|
|
10
10
|
offset: RequestInit;
|
|
@@ -53,7 +53,7 @@ declare class VigorFetch<T = any> {
|
|
|
53
53
|
onError(...arg: VigorFetchConfig['onError']): VigorFetch<T>;
|
|
54
54
|
request(): Promise<T>;
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
interface VigorAllConfig {
|
|
57
57
|
limit: number;
|
|
58
58
|
jitter: number;
|
|
59
59
|
promises: Array<() => Promise<any>>;
|
|
@@ -71,4 +71,6 @@ declare class Vigor {
|
|
|
71
71
|
fetch<T = any>(origin: string, config?: VigorFetchConfig): VigorFetch<T>;
|
|
72
72
|
all(config?: VigorAllConfig): VigorAll;
|
|
73
73
|
}
|
|
74
|
-
|
|
74
|
+
declare const vigor: Vigor;
|
|
75
|
+
|
|
76
|
+
export { type VigorAllConfig, type VigorErrorOptions, type VigorFetchConfig, vigor as default };
|
package/dist/index.js
CHANGED
|
@@ -1,194 +1,290 @@
|
|
|
1
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
2
18
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
default: () => index_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(index_exports);
|
|
25
|
+
var VigorError = class extends Error {
|
|
26
|
+
url;
|
|
27
|
+
status;
|
|
28
|
+
data;
|
|
29
|
+
constructor(text, { url = null, status = 0, message, data = null }) {
|
|
30
|
+
super(text);
|
|
31
|
+
this.name = "VigorError";
|
|
32
|
+
this.url = url;
|
|
33
|
+
this.status = status;
|
|
34
|
+
this.message = message || text;
|
|
35
|
+
this.data = data;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var VigorFetch = class _VigorFetch {
|
|
39
|
+
_origin;
|
|
40
|
+
_config;
|
|
41
|
+
constructor(origin, config) {
|
|
42
|
+
this._origin = origin;
|
|
43
|
+
this._config = config || {
|
|
44
|
+
path: "",
|
|
45
|
+
method: null,
|
|
46
|
+
offset: {},
|
|
47
|
+
headers: {},
|
|
48
|
+
body: null,
|
|
49
|
+
count: 5,
|
|
50
|
+
max: 5e3,
|
|
51
|
+
wait: 1e4,
|
|
52
|
+
backoff: 1.3,
|
|
53
|
+
unretry: /* @__PURE__ */ new Set([400, 401, 403, 404, 405, 413, 422]),
|
|
54
|
+
retryHeader: ["retry-after", "ratelimit-reset", "x-ratelimit-reset", "x-retry-after", "x-amz-retry-after", "chrome-proxy-next-link"],
|
|
55
|
+
original: false,
|
|
56
|
+
parse: null,
|
|
57
|
+
query: {},
|
|
58
|
+
jitter: 500,
|
|
59
|
+
beforeRequest: [],
|
|
60
|
+
afterRequest: [],
|
|
61
|
+
beforeResponse: [],
|
|
62
|
+
afterResponse: [],
|
|
63
|
+
onError: []
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
_next(changes) {
|
|
67
|
+
return new _VigorFetch(this._origin, { ...this._config, ...changes });
|
|
68
|
+
}
|
|
69
|
+
path(arg) {
|
|
70
|
+
return this._next({ path: arg });
|
|
71
|
+
}
|
|
72
|
+
method(arg) {
|
|
73
|
+
return this._next({ method: arg });
|
|
74
|
+
}
|
|
75
|
+
offset(arg) {
|
|
76
|
+
return this._next({ offset: arg });
|
|
77
|
+
}
|
|
78
|
+
headers(arg) {
|
|
79
|
+
return this._next({ headers: arg });
|
|
80
|
+
}
|
|
81
|
+
body(arg) {
|
|
82
|
+
return this._next({ body: arg });
|
|
83
|
+
}
|
|
84
|
+
count(arg) {
|
|
85
|
+
return this._next({ count: arg });
|
|
86
|
+
}
|
|
87
|
+
max(arg) {
|
|
88
|
+
return this._next({ max: arg });
|
|
89
|
+
}
|
|
90
|
+
wait(arg) {
|
|
91
|
+
return this._next({ wait: arg });
|
|
92
|
+
}
|
|
93
|
+
backoff(arg) {
|
|
94
|
+
return this._next({ backoff: arg });
|
|
95
|
+
}
|
|
96
|
+
unretry(arg) {
|
|
97
|
+
return this._next({ unretry: new Set(arg) });
|
|
98
|
+
}
|
|
99
|
+
retryHeader(...arg) {
|
|
100
|
+
return this._next({ retryHeader: [...this._config.retryHeader, ...arg] });
|
|
101
|
+
}
|
|
102
|
+
original(arg) {
|
|
103
|
+
return this._next({ original: arg });
|
|
104
|
+
}
|
|
105
|
+
parse(arg) {
|
|
106
|
+
return this._next({ parse: arg });
|
|
107
|
+
}
|
|
108
|
+
query(arg) {
|
|
109
|
+
return this._next({ query: { ...this._config.query, ...arg } });
|
|
110
|
+
}
|
|
111
|
+
jitter(arg) {
|
|
112
|
+
return this._next({ jitter: arg });
|
|
113
|
+
}
|
|
114
|
+
beforeRequest(...arg) {
|
|
115
|
+
return this._next({ beforeRequest: [...this._config.beforeRequest, ...arg] });
|
|
116
|
+
}
|
|
117
|
+
afterRequest(...arg) {
|
|
118
|
+
return this._next({ afterRequest: [...this._config.afterRequest, ...arg] });
|
|
119
|
+
}
|
|
120
|
+
beforeResponse(...arg) {
|
|
121
|
+
return this._next({ beforeResponse: [...this._config.beforeResponse, ...arg] });
|
|
122
|
+
}
|
|
123
|
+
afterResponse(...arg) {
|
|
124
|
+
return this._next({ afterResponse: [...this._config.afterResponse, ...arg] });
|
|
125
|
+
}
|
|
126
|
+
onError(...arg) {
|
|
127
|
+
return this._next({ onError: [...this._config.onError, ...arg] });
|
|
128
|
+
}
|
|
129
|
+
async request() {
|
|
130
|
+
const {
|
|
131
|
+
path,
|
|
132
|
+
method,
|
|
133
|
+
offset,
|
|
134
|
+
headers,
|
|
135
|
+
body,
|
|
136
|
+
query,
|
|
137
|
+
count,
|
|
138
|
+
max,
|
|
139
|
+
wait,
|
|
140
|
+
backoff,
|
|
141
|
+
unretry,
|
|
142
|
+
jitter,
|
|
143
|
+
original,
|
|
144
|
+
parse,
|
|
145
|
+
retryHeader,
|
|
146
|
+
beforeRequest,
|
|
147
|
+
afterRequest,
|
|
148
|
+
beforeResponse,
|
|
149
|
+
afterResponse,
|
|
150
|
+
onError
|
|
151
|
+
} = this._config;
|
|
152
|
+
try {
|
|
153
|
+
if (!/^(https?|data|blob|file|about):\/\//.test(this._origin)) {
|
|
154
|
+
throw new VigorError(`[vigor] ${this._origin} >> Invalid Protocol`, {
|
|
155
|
+
url: this._origin,
|
|
156
|
+
status: 0,
|
|
157
|
+
message: "Invalid Protocol"
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
const urlObj = new URL(path.replace(/^\//, ""), this._origin + "/");
|
|
161
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
162
|
+
if (value !== null && value !== void 0) urlObj.searchParams.append(key, String(value));
|
|
163
|
+
});
|
|
164
|
+
const url = urlObj.href;
|
|
165
|
+
const isJson = Array.isArray(body) || !!body && Object.getPrototypeOf(body) === Object.prototype;
|
|
166
|
+
const waitTimeout = (time) => new Promise((resolve) => setTimeout(resolve, time));
|
|
167
|
+
let option = {
|
|
168
|
+
...offset,
|
|
169
|
+
method: method || (body ? "POST" : "GET"),
|
|
170
|
+
headers: { ...isJson && { "Content-Type": "application/json" }, ...headers },
|
|
171
|
+
...body && { body: isJson ? JSON.stringify(body) : body }
|
|
172
|
+
};
|
|
173
|
+
for (const hook of beforeRequest) {
|
|
174
|
+
const modified = await hook(option);
|
|
175
|
+
if (modified) option = { ...option, ...modified };
|
|
176
|
+
}
|
|
177
|
+
let req;
|
|
178
|
+
for (let i = 0; i < count; i++) {
|
|
179
|
+
const controller = new AbortController();
|
|
180
|
+
const abort = setTimeout(() => controller.abort(), max);
|
|
181
|
+
option.signal = controller.signal;
|
|
51
182
|
try {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
let option = {
|
|
66
|
-
...offset,
|
|
67
|
-
method: method || (body ? "POST" : "GET"),
|
|
68
|
-
headers: { ...(isJson && { "Content-Type": "application/json" }), ...headers },
|
|
69
|
-
...(body && { body: isJson ? JSON.stringify(body) : body }),
|
|
70
|
-
};
|
|
71
|
-
for (const hook of beforeRequest) {
|
|
72
|
-
const modified = await hook(option);
|
|
73
|
-
if (modified)
|
|
74
|
-
option = { ...option, ...modified };
|
|
75
|
-
}
|
|
76
|
-
let req;
|
|
77
|
-
for (let i = 0; i < count; i++) {
|
|
78
|
-
const controller = new AbortController();
|
|
79
|
-
const abort = setTimeout(() => controller.abort(), max);
|
|
80
|
-
option.signal = controller.signal;
|
|
81
|
-
try {
|
|
82
|
-
req = await fetch(url, option);
|
|
83
|
-
for (const hook of afterRequest) {
|
|
84
|
-
req = (await hook(req)) || req;
|
|
85
|
-
}
|
|
86
|
-
if (req.ok) {
|
|
87
|
-
clearTimeout(abort);
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
catch (error) {
|
|
92
|
-
clearTimeout(abort);
|
|
93
|
-
if (i === count - 1)
|
|
94
|
-
throw new VigorError(`[vigor] ${url} >> Network Error`, { url, status: 0, message: "Network Error" });
|
|
95
|
-
}
|
|
96
|
-
finally {
|
|
97
|
-
clearTimeout(abort);
|
|
98
|
-
}
|
|
99
|
-
if (req) {
|
|
100
|
-
const status = req.status;
|
|
101
|
-
if (unretry.has(status))
|
|
102
|
-
throw new VigorError(`[vigor] ${url} >> Unretry ${status}`, { url, status, message: "Unretry", data: status });
|
|
103
|
-
const basic = Math.min(Math.pow(backoff, i) * 1000, wait) + Math.random() * jitter;
|
|
104
|
-
if (status === 429) {
|
|
105
|
-
const rHeader = retryHeader.map(h => req?.headers.get(h)).find(Boolean);
|
|
106
|
-
const delay = rHeader ? (isNaN(Number(rHeader)) ? new Date(rHeader).getTime() - Date.now() : Number(rHeader) * 1000) : 0;
|
|
107
|
-
const parsedDelay = Math.max(0, delay) + Math.random() * jitter;
|
|
108
|
-
if (parsedDelay > wait)
|
|
109
|
-
throw new VigorError(`[vigor] ${url} >> Timeouted ${parsedDelay}ms`, { url, status, message: "Timeouted", data: parsedDelay });
|
|
110
|
-
await waitTimeout(parsedDelay || basic);
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
await waitTimeout(basic);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (!req)
|
|
118
|
-
throw new Error("No response");
|
|
119
|
-
let currentReq = req;
|
|
120
|
-
for (const hook of beforeResponse) {
|
|
121
|
-
currentReq = await hook(currentReq);
|
|
122
|
-
}
|
|
123
|
-
if (!currentReq.ok)
|
|
124
|
-
throw new VigorError(`[vigor] ${url} >> Failed`, { url, status: currentReq.status, message: "Failed" });
|
|
125
|
-
let res = await (async () => {
|
|
126
|
-
if (original)
|
|
127
|
-
return currentReq;
|
|
128
|
-
if (parse) {
|
|
129
|
-
const target = currentReq[parse];
|
|
130
|
-
return typeof target === 'function' ? await target.call(currentReq) : target;
|
|
131
|
-
}
|
|
132
|
-
const contentType = currentReq.headers.get("Content-Type") || "";
|
|
133
|
-
if (/json/.test(contentType))
|
|
134
|
-
return await currentReq.json();
|
|
135
|
-
if (/(image|video|audio|pdf)/.test(contentType))
|
|
136
|
-
return await currentReq.blob();
|
|
137
|
-
return await currentReq.text();
|
|
138
|
-
})();
|
|
139
|
-
for (const hook of afterResponse) {
|
|
140
|
-
res = await hook(res);
|
|
141
|
-
}
|
|
142
|
-
return res;
|
|
183
|
+
req = await fetch(url, option);
|
|
184
|
+
for (const hook of afterRequest) {
|
|
185
|
+
req = await hook(req) || req;
|
|
186
|
+
}
|
|
187
|
+
if (req.ok) {
|
|
188
|
+
clearTimeout(abort);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
} catch (error) {
|
|
192
|
+
clearTimeout(abort);
|
|
193
|
+
if (i === count - 1) throw new VigorError(`[vigor] ${url} >> Network Error`, { url, status: 0, message: "Network Error" });
|
|
194
|
+
} finally {
|
|
195
|
+
clearTimeout(abort);
|
|
143
196
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
throw
|
|
197
|
+
if (req) {
|
|
198
|
+
const status = req.status;
|
|
199
|
+
if (unretry.has(status)) throw new VigorError(`[vigor] ${url} >> Unretry ${status}`, { url, status, message: "Unretry", data: status });
|
|
200
|
+
const basic = Math.min(Math.pow(backoff, i) * 1e3, wait) + Math.random() * jitter;
|
|
201
|
+
if (status === 429) {
|
|
202
|
+
const rHeader = retryHeader.map((h) => req == null ? void 0 : req.headers.get(h)).find(Boolean);
|
|
203
|
+
const delay = rHeader ? isNaN(Number(rHeader)) ? new Date(rHeader).getTime() - Date.now() : Number(rHeader) * 1e3 : 0;
|
|
204
|
+
const parsedDelay = Math.max(0, delay) + Math.random() * jitter;
|
|
205
|
+
if (parsedDelay > wait) throw new VigorError(`[vigor] ${url} >> Timeouted ${parsedDelay}ms`, { url, status, message: "Timeouted", data: parsedDelay });
|
|
206
|
+
await waitTimeout(parsedDelay || basic);
|
|
207
|
+
} else {
|
|
208
|
+
await waitTimeout(basic);
|
|
209
|
+
}
|
|
153
210
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const results = [];
|
|
167
|
-
const executing = new Set();
|
|
168
|
-
for (const task of promises) {
|
|
169
|
-
const p = Promise.resolve()
|
|
170
|
-
.then(() => new Promise(res => setTimeout(res, Math.random() * jitter)))
|
|
171
|
-
.then(() => task());
|
|
172
|
-
results.push(p);
|
|
173
|
-
executing.add(p);
|
|
174
|
-
p.finally(() => executing.delete(p));
|
|
175
|
-
if (executing.size >= limit)
|
|
176
|
-
await Promise.race(executing);
|
|
211
|
+
}
|
|
212
|
+
if (!req) throw new Error("No response");
|
|
213
|
+
let currentReq = req;
|
|
214
|
+
for (const hook of beforeResponse) {
|
|
215
|
+
currentReq = await hook(currentReq);
|
|
216
|
+
}
|
|
217
|
+
if (!currentReq.ok) throw new VigorError(`[vigor] ${url} >> Failed`, { url, status: currentReq.status, message: "Failed" });
|
|
218
|
+
let res = await (async () => {
|
|
219
|
+
if (original) return currentReq;
|
|
220
|
+
if (parse) {
|
|
221
|
+
const target = currentReq[parse];
|
|
222
|
+
return typeof target === "function" ? await target.call(currentReq) : target;
|
|
177
223
|
}
|
|
178
|
-
const
|
|
179
|
-
return
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
224
|
+
const contentType = currentReq.headers.get("Content-Type") || "";
|
|
225
|
+
if (/json/.test(contentType)) return await currentReq.json();
|
|
226
|
+
if (/(image|video|audio|pdf)/.test(contentType)) return await currentReq.blob();
|
|
227
|
+
return await currentReq.text();
|
|
228
|
+
})();
|
|
229
|
+
for (const hook of afterResponse) {
|
|
230
|
+
res = await hook(res);
|
|
231
|
+
}
|
|
232
|
+
return res;
|
|
233
|
+
} catch (error) {
|
|
234
|
+
let currentError = error;
|
|
235
|
+
for (const hook of onError) {
|
|
236
|
+
const result = await hook(currentError);
|
|
237
|
+
if (result !== void 0 && !(result instanceof Error)) return result;
|
|
238
|
+
currentError = result || currentError;
|
|
239
|
+
}
|
|
240
|
+
throw currentError;
|
|
189
241
|
}
|
|
190
|
-
|
|
191
|
-
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
var VigorAll = class _VigorAll {
|
|
245
|
+
_config;
|
|
246
|
+
constructor(config) {
|
|
247
|
+
this._config = config || { limit: 10, jitter: 1e3, promises: [] };
|
|
248
|
+
}
|
|
249
|
+
_next(changes) {
|
|
250
|
+
return new _VigorAll({ ...this._config, ...changes });
|
|
251
|
+
}
|
|
252
|
+
limit(arg) {
|
|
253
|
+
return this._next({ limit: arg });
|
|
254
|
+
}
|
|
255
|
+
jitter(arg) {
|
|
256
|
+
return this._next({ jitter: arg });
|
|
257
|
+
}
|
|
258
|
+
promises(...args) {
|
|
259
|
+
return this._next({ promises: [...this._config.promises, ...args] });
|
|
260
|
+
}
|
|
261
|
+
async request() {
|
|
262
|
+
const { limit, jitter, promises } = this._config;
|
|
263
|
+
const results = [];
|
|
264
|
+
const executing = /* @__PURE__ */ new Set();
|
|
265
|
+
for (const task of promises) {
|
|
266
|
+
const p = Promise.resolve().then(() => new Promise((res) => setTimeout(res, Math.random() * jitter))).then(() => task());
|
|
267
|
+
results.push(p);
|
|
268
|
+
executing.add(p);
|
|
269
|
+
p.finally(() => executing.delete(p));
|
|
270
|
+
if (executing.size >= limit) await Promise.race(executing);
|
|
192
271
|
}
|
|
193
|
-
|
|
194
|
-
|
|
272
|
+
const ready = await Promise.allSettled(results);
|
|
273
|
+
return ready.map((i) => {
|
|
274
|
+
var _a, _b;
|
|
275
|
+
if (i.status === "fulfilled") return i.value;
|
|
276
|
+
return i.reason instanceof VigorError ? i.reason : new VigorError(((_a = i.reason) == null ? void 0 : _a.message) || "Unknown", { message: ((_b = i.reason) == null ? void 0 : _b.message) || "Unknown" });
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
var Vigor = class {
|
|
281
|
+
fetch(origin, config) {
|
|
282
|
+
return new VigorFetch(origin, config);
|
|
283
|
+
}
|
|
284
|
+
all(config) {
|
|
285
|
+
return new VigorAll(config);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
var vigor = new Vigor();
|
|
289
|
+
var index_default = vigor;
|
|
290
|
+
if (module.exports.default) module.exports = module.exports.default;
|
package/dist/index.mjs
CHANGED
|
@@ -1,192 +1,270 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var VigorError = class extends Error {
|
|
3
|
+
url;
|
|
4
|
+
status;
|
|
5
|
+
data;
|
|
6
|
+
constructor(text, { url = null, status = 0, message, data = null }) {
|
|
7
|
+
super(text);
|
|
8
|
+
this.name = "VigorError";
|
|
9
|
+
this.url = url;
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.message = message || text;
|
|
12
|
+
this.data = data;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var VigorFetch = class _VigorFetch {
|
|
16
|
+
_origin;
|
|
17
|
+
_config;
|
|
18
|
+
constructor(origin, config) {
|
|
19
|
+
this._origin = origin;
|
|
20
|
+
this._config = config || {
|
|
21
|
+
path: "",
|
|
22
|
+
method: null,
|
|
23
|
+
offset: {},
|
|
24
|
+
headers: {},
|
|
25
|
+
body: null,
|
|
26
|
+
count: 5,
|
|
27
|
+
max: 5e3,
|
|
28
|
+
wait: 1e4,
|
|
29
|
+
backoff: 1.3,
|
|
30
|
+
unretry: /* @__PURE__ */ new Set([400, 401, 403, 404, 405, 413, 422]),
|
|
31
|
+
retryHeader: ["retry-after", "ratelimit-reset", "x-ratelimit-reset", "x-retry-after", "x-amz-retry-after", "chrome-proxy-next-link"],
|
|
32
|
+
original: false,
|
|
33
|
+
parse: null,
|
|
34
|
+
query: {},
|
|
35
|
+
jitter: 500,
|
|
36
|
+
beforeRequest: [],
|
|
37
|
+
afterRequest: [],
|
|
38
|
+
beforeResponse: [],
|
|
39
|
+
afterResponse: [],
|
|
40
|
+
onError: []
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
_next(changes) {
|
|
44
|
+
return new _VigorFetch(this._origin, { ...this._config, ...changes });
|
|
45
|
+
}
|
|
46
|
+
path(arg) {
|
|
47
|
+
return this._next({ path: arg });
|
|
48
|
+
}
|
|
49
|
+
method(arg) {
|
|
50
|
+
return this._next({ method: arg });
|
|
51
|
+
}
|
|
52
|
+
offset(arg) {
|
|
53
|
+
return this._next({ offset: arg });
|
|
54
|
+
}
|
|
55
|
+
headers(arg) {
|
|
56
|
+
return this._next({ headers: arg });
|
|
57
|
+
}
|
|
58
|
+
body(arg) {
|
|
59
|
+
return this._next({ body: arg });
|
|
60
|
+
}
|
|
61
|
+
count(arg) {
|
|
62
|
+
return this._next({ count: arg });
|
|
63
|
+
}
|
|
64
|
+
max(arg) {
|
|
65
|
+
return this._next({ max: arg });
|
|
66
|
+
}
|
|
67
|
+
wait(arg) {
|
|
68
|
+
return this._next({ wait: arg });
|
|
69
|
+
}
|
|
70
|
+
backoff(arg) {
|
|
71
|
+
return this._next({ backoff: arg });
|
|
72
|
+
}
|
|
73
|
+
unretry(arg) {
|
|
74
|
+
return this._next({ unretry: new Set(arg) });
|
|
75
|
+
}
|
|
76
|
+
retryHeader(...arg) {
|
|
77
|
+
return this._next({ retryHeader: [...this._config.retryHeader, ...arg] });
|
|
78
|
+
}
|
|
79
|
+
original(arg) {
|
|
80
|
+
return this._next({ original: arg });
|
|
81
|
+
}
|
|
82
|
+
parse(arg) {
|
|
83
|
+
return this._next({ parse: arg });
|
|
84
|
+
}
|
|
85
|
+
query(arg) {
|
|
86
|
+
return this._next({ query: { ...this._config.query, ...arg } });
|
|
87
|
+
}
|
|
88
|
+
jitter(arg) {
|
|
89
|
+
return this._next({ jitter: arg });
|
|
90
|
+
}
|
|
91
|
+
beforeRequest(...arg) {
|
|
92
|
+
return this._next({ beforeRequest: [...this._config.beforeRequest, ...arg] });
|
|
93
|
+
}
|
|
94
|
+
afterRequest(...arg) {
|
|
95
|
+
return this._next({ afterRequest: [...this._config.afterRequest, ...arg] });
|
|
96
|
+
}
|
|
97
|
+
beforeResponse(...arg) {
|
|
98
|
+
return this._next({ beforeResponse: [...this._config.beforeResponse, ...arg] });
|
|
99
|
+
}
|
|
100
|
+
afterResponse(...arg) {
|
|
101
|
+
return this._next({ afterResponse: [...this._config.afterResponse, ...arg] });
|
|
102
|
+
}
|
|
103
|
+
onError(...arg) {
|
|
104
|
+
return this._next({ onError: [...this._config.onError, ...arg] });
|
|
105
|
+
}
|
|
106
|
+
async request() {
|
|
107
|
+
const {
|
|
108
|
+
path,
|
|
109
|
+
method,
|
|
110
|
+
offset,
|
|
111
|
+
headers,
|
|
112
|
+
body,
|
|
113
|
+
query,
|
|
114
|
+
count,
|
|
115
|
+
max,
|
|
116
|
+
wait,
|
|
117
|
+
backoff,
|
|
118
|
+
unretry,
|
|
119
|
+
jitter,
|
|
120
|
+
original,
|
|
121
|
+
parse,
|
|
122
|
+
retryHeader,
|
|
123
|
+
beforeRequest,
|
|
124
|
+
afterRequest,
|
|
125
|
+
beforeResponse,
|
|
126
|
+
afterResponse,
|
|
127
|
+
onError
|
|
128
|
+
} = this._config;
|
|
129
|
+
try {
|
|
130
|
+
if (!/^(https?|data|blob|file|about):\/\//.test(this._origin)) {
|
|
131
|
+
throw new VigorError(`[vigor] ${this._origin} >> Invalid Protocol`, {
|
|
132
|
+
url: this._origin,
|
|
133
|
+
status: 0,
|
|
134
|
+
message: "Invalid Protocol"
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
const urlObj = new URL(path.replace(/^\//, ""), this._origin + "/");
|
|
138
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
139
|
+
if (value !== null && value !== void 0) urlObj.searchParams.append(key, String(value));
|
|
140
|
+
});
|
|
141
|
+
const url = urlObj.href;
|
|
142
|
+
const isJson = Array.isArray(body) || !!body && Object.getPrototypeOf(body) === Object.prototype;
|
|
143
|
+
const waitTimeout = (time) => new Promise((resolve) => setTimeout(resolve, time));
|
|
144
|
+
let option = {
|
|
145
|
+
...offset,
|
|
146
|
+
method: method || (body ? "POST" : "GET"),
|
|
147
|
+
headers: { ...isJson && { "Content-Type": "application/json" }, ...headers },
|
|
148
|
+
...body && { body: isJson ? JSON.stringify(body) : body }
|
|
149
|
+
};
|
|
150
|
+
for (const hook of beforeRequest) {
|
|
151
|
+
const modified = await hook(option);
|
|
152
|
+
if (modified) option = { ...option, ...modified };
|
|
153
|
+
}
|
|
154
|
+
let req;
|
|
155
|
+
for (let i = 0; i < count; i++) {
|
|
156
|
+
const controller = new AbortController();
|
|
157
|
+
const abort = setTimeout(() => controller.abort(), max);
|
|
158
|
+
option.signal = controller.signal;
|
|
49
159
|
try {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
let option = {
|
|
64
|
-
...offset,
|
|
65
|
-
method: method || (body ? "POST" : "GET"),
|
|
66
|
-
headers: { ...(isJson && { "Content-Type": "application/json" }), ...headers },
|
|
67
|
-
...(body && { body: isJson ? JSON.stringify(body) : body }),
|
|
68
|
-
};
|
|
69
|
-
for (const hook of beforeRequest) {
|
|
70
|
-
const modified = await hook(option);
|
|
71
|
-
if (modified)
|
|
72
|
-
option = { ...option, ...modified };
|
|
73
|
-
}
|
|
74
|
-
let req;
|
|
75
|
-
for (let i = 0; i < count; i++) {
|
|
76
|
-
const controller = new AbortController();
|
|
77
|
-
const abort = setTimeout(() => controller.abort(), max);
|
|
78
|
-
option.signal = controller.signal;
|
|
79
|
-
try {
|
|
80
|
-
req = await fetch(url, option);
|
|
81
|
-
for (const hook of afterRequest) {
|
|
82
|
-
req = (await hook(req)) || req;
|
|
83
|
-
}
|
|
84
|
-
if (req.ok) {
|
|
85
|
-
clearTimeout(abort);
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
catch (error) {
|
|
90
|
-
clearTimeout(abort);
|
|
91
|
-
if (i === count - 1)
|
|
92
|
-
throw new VigorError(`[vigor] ${url} >> Network Error`, { url, status: 0, message: "Network Error" });
|
|
93
|
-
}
|
|
94
|
-
finally {
|
|
95
|
-
clearTimeout(abort);
|
|
96
|
-
}
|
|
97
|
-
if (req) {
|
|
98
|
-
const status = req.status;
|
|
99
|
-
if (unretry.has(status))
|
|
100
|
-
throw new VigorError(`[vigor] ${url} >> Unretry ${status}`, { url, status, message: "Unretry", data: status });
|
|
101
|
-
const basic = Math.min(Math.pow(backoff, i) * 1000, wait) + Math.random() * jitter;
|
|
102
|
-
if (status === 429) {
|
|
103
|
-
const rHeader = retryHeader.map(h => req?.headers.get(h)).find(Boolean);
|
|
104
|
-
const delay = rHeader ? (isNaN(Number(rHeader)) ? new Date(rHeader).getTime() - Date.now() : Number(rHeader) * 1000) : 0;
|
|
105
|
-
const parsedDelay = Math.max(0, delay) + Math.random() * jitter;
|
|
106
|
-
if (parsedDelay > wait)
|
|
107
|
-
throw new VigorError(`[vigor] ${url} >> Timeouted ${parsedDelay}ms`, { url, status, message: "Timeouted", data: parsedDelay });
|
|
108
|
-
await waitTimeout(parsedDelay || basic);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
await waitTimeout(basic);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (!req)
|
|
116
|
-
throw new Error("No response");
|
|
117
|
-
let currentReq = req;
|
|
118
|
-
for (const hook of beforeResponse) {
|
|
119
|
-
currentReq = await hook(currentReq);
|
|
120
|
-
}
|
|
121
|
-
if (!currentReq.ok)
|
|
122
|
-
throw new VigorError(`[vigor] ${url} >> Failed`, { url, status: currentReq.status, message: "Failed" });
|
|
123
|
-
let res = await (async () => {
|
|
124
|
-
if (original)
|
|
125
|
-
return currentReq;
|
|
126
|
-
if (parse) {
|
|
127
|
-
const target = currentReq[parse];
|
|
128
|
-
return typeof target === 'function' ? await target.call(currentReq) : target;
|
|
129
|
-
}
|
|
130
|
-
const contentType = currentReq.headers.get("Content-Type") || "";
|
|
131
|
-
if (/json/.test(contentType))
|
|
132
|
-
return await currentReq.json();
|
|
133
|
-
if (/(image|video|audio|pdf)/.test(contentType))
|
|
134
|
-
return await currentReq.blob();
|
|
135
|
-
return await currentReq.text();
|
|
136
|
-
})();
|
|
137
|
-
for (const hook of afterResponse) {
|
|
138
|
-
res = await hook(res);
|
|
139
|
-
}
|
|
140
|
-
return res;
|
|
160
|
+
req = await fetch(url, option);
|
|
161
|
+
for (const hook of afterRequest) {
|
|
162
|
+
req = await hook(req) || req;
|
|
163
|
+
}
|
|
164
|
+
if (req.ok) {
|
|
165
|
+
clearTimeout(abort);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
clearTimeout(abort);
|
|
170
|
+
if (i === count - 1) throw new VigorError(`[vigor] ${url} >> Network Error`, { url, status: 0, message: "Network Error" });
|
|
171
|
+
} finally {
|
|
172
|
+
clearTimeout(abort);
|
|
141
173
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
throw
|
|
174
|
+
if (req) {
|
|
175
|
+
const status = req.status;
|
|
176
|
+
if (unretry.has(status)) throw new VigorError(`[vigor] ${url} >> Unretry ${status}`, { url, status, message: "Unretry", data: status });
|
|
177
|
+
const basic = Math.min(Math.pow(backoff, i) * 1e3, wait) + Math.random() * jitter;
|
|
178
|
+
if (status === 429) {
|
|
179
|
+
const rHeader = retryHeader.map((h) => req == null ? void 0 : req.headers.get(h)).find(Boolean);
|
|
180
|
+
const delay = rHeader ? isNaN(Number(rHeader)) ? new Date(rHeader).getTime() - Date.now() : Number(rHeader) * 1e3 : 0;
|
|
181
|
+
const parsedDelay = Math.max(0, delay) + Math.random() * jitter;
|
|
182
|
+
if (parsedDelay > wait) throw new VigorError(`[vigor] ${url} >> Timeouted ${parsedDelay}ms`, { url, status, message: "Timeouted", data: parsedDelay });
|
|
183
|
+
await waitTimeout(parsedDelay || basic);
|
|
184
|
+
} else {
|
|
185
|
+
await waitTimeout(basic);
|
|
186
|
+
}
|
|
151
187
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const results = [];
|
|
165
|
-
const executing = new Set();
|
|
166
|
-
for (const task of promises) {
|
|
167
|
-
const p = Promise.resolve()
|
|
168
|
-
.then(() => new Promise(res => setTimeout(res, Math.random() * jitter)))
|
|
169
|
-
.then(() => task());
|
|
170
|
-
results.push(p);
|
|
171
|
-
executing.add(p);
|
|
172
|
-
p.finally(() => executing.delete(p));
|
|
173
|
-
if (executing.size >= limit)
|
|
174
|
-
await Promise.race(executing);
|
|
188
|
+
}
|
|
189
|
+
if (!req) throw new Error("No response");
|
|
190
|
+
let currentReq = req;
|
|
191
|
+
for (const hook of beforeResponse) {
|
|
192
|
+
currentReq = await hook(currentReq);
|
|
193
|
+
}
|
|
194
|
+
if (!currentReq.ok) throw new VigorError(`[vigor] ${url} >> Failed`, { url, status: currentReq.status, message: "Failed" });
|
|
195
|
+
let res = await (async () => {
|
|
196
|
+
if (original) return currentReq;
|
|
197
|
+
if (parse) {
|
|
198
|
+
const target = currentReq[parse];
|
|
199
|
+
return typeof target === "function" ? await target.call(currentReq) : target;
|
|
175
200
|
}
|
|
176
|
-
const
|
|
177
|
-
return
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
201
|
+
const contentType = currentReq.headers.get("Content-Type") || "";
|
|
202
|
+
if (/json/.test(contentType)) return await currentReq.json();
|
|
203
|
+
if (/(image|video|audio|pdf)/.test(contentType)) return await currentReq.blob();
|
|
204
|
+
return await currentReq.text();
|
|
205
|
+
})();
|
|
206
|
+
for (const hook of afterResponse) {
|
|
207
|
+
res = await hook(res);
|
|
208
|
+
}
|
|
209
|
+
return res;
|
|
210
|
+
} catch (error) {
|
|
211
|
+
let currentError = error;
|
|
212
|
+
for (const hook of onError) {
|
|
213
|
+
const result = await hook(currentError);
|
|
214
|
+
if (result !== void 0 && !(result instanceof Error)) return result;
|
|
215
|
+
currentError = result || currentError;
|
|
216
|
+
}
|
|
217
|
+
throw currentError;
|
|
187
218
|
}
|
|
188
|
-
|
|
189
|
-
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
var VigorAll = class _VigorAll {
|
|
222
|
+
_config;
|
|
223
|
+
constructor(config) {
|
|
224
|
+
this._config = config || { limit: 10, jitter: 1e3, promises: [] };
|
|
225
|
+
}
|
|
226
|
+
_next(changes) {
|
|
227
|
+
return new _VigorAll({ ...this._config, ...changes });
|
|
228
|
+
}
|
|
229
|
+
limit(arg) {
|
|
230
|
+
return this._next({ limit: arg });
|
|
231
|
+
}
|
|
232
|
+
jitter(arg) {
|
|
233
|
+
return this._next({ jitter: arg });
|
|
234
|
+
}
|
|
235
|
+
promises(...args) {
|
|
236
|
+
return this._next({ promises: [...this._config.promises, ...args] });
|
|
237
|
+
}
|
|
238
|
+
async request() {
|
|
239
|
+
const { limit, jitter, promises } = this._config;
|
|
240
|
+
const results = [];
|
|
241
|
+
const executing = /* @__PURE__ */ new Set();
|
|
242
|
+
for (const task of promises) {
|
|
243
|
+
const p = Promise.resolve().then(() => new Promise((res) => setTimeout(res, Math.random() * jitter))).then(() => task());
|
|
244
|
+
results.push(p);
|
|
245
|
+
executing.add(p);
|
|
246
|
+
p.finally(() => executing.delete(p));
|
|
247
|
+
if (executing.size >= limit) await Promise.race(executing);
|
|
190
248
|
}
|
|
191
|
-
|
|
192
|
-
|
|
249
|
+
const ready = await Promise.allSettled(results);
|
|
250
|
+
return ready.map((i) => {
|
|
251
|
+
var _a, _b;
|
|
252
|
+
if (i.status === "fulfilled") return i.value;
|
|
253
|
+
return i.reason instanceof VigorError ? i.reason : new VigorError(((_a = i.reason) == null ? void 0 : _a.message) || "Unknown", { message: ((_b = i.reason) == null ? void 0 : _b.message) || "Unknown" });
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
var Vigor = class {
|
|
258
|
+
fetch(origin, config) {
|
|
259
|
+
return new VigorFetch(origin, config);
|
|
260
|
+
}
|
|
261
|
+
all(config) {
|
|
262
|
+
return new VigorAll(config);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
var vigor = new Vigor();
|
|
266
|
+
var index_default = vigor;
|
|
267
|
+
export {
|
|
268
|
+
index_default as default
|
|
269
|
+
};
|
|
270
|
+
if (module.exports.default) module.exports = module.exports.default;
|
package/package.json
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vigor-fetch",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Smart, zero-dependency HTTP client with self-healing retries for rate-limited servers.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.mjs",
|
|
12
|
-
"
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup"
|
|
17
|
+
},
|
|
15
18
|
"files": [
|
|
16
19
|
"dist"
|
|
17
20
|
],
|
|
18
|
-
"scripts": {
|
|
19
|
-
"build": "rollup -c",
|
|
20
|
-
"dev": "rollup -c -w"
|
|
21
|
-
},
|
|
22
21
|
"keywords": [
|
|
23
22
|
"fetch",
|
|
24
23
|
"PromiseAll",
|
|
@@ -34,6 +33,7 @@
|
|
|
34
33
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
35
34
|
"rollup": "^4.59.0",
|
|
36
35
|
"tslib": "^2.8.1",
|
|
36
|
+
"tsup": "^8.5.1",
|
|
37
37
|
"typescript": "^5.9.3"
|
|
38
38
|
},
|
|
39
39
|
"engines": {
|