@shware/http 2.1.0 → 2.2.0
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/error/parse.cjs +13 -1
- package/dist/error/parse.cjs.map +1 -1
- package/dist/error/parse.mjs +11 -1
- package/dist/error/parse.mjs.map +1 -1
- package/dist/index.cjs +0 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -6
- package/dist/index.d.ts +0 -6
- package/dist/index.mjs +0 -12
- package/dist/index.mjs.map +1 -1
- package/dist/response.cjs +13 -1
- package/dist/response.cjs.map +1 -1
- package/dist/response.mjs +11 -1
- package/dist/response.mjs.map +1 -1
- package/package.json +3 -2
- package/dist/utils/__tests__/base62.test.cjs +0 -29
- package/dist/utils/__tests__/base62.test.cjs.map +0 -1
- package/dist/utils/__tests__/base62.test.d.cts +0 -2
- package/dist/utils/__tests__/base62.test.d.ts +0 -2
- package/dist/utils/__tests__/base62.test.mjs +0 -27
- package/dist/utils/__tests__/base62.test.mjs.map +0 -1
- package/dist/utils/__tests__/ip.test.cjs +0 -38
- package/dist/utils/__tests__/ip.test.cjs.map +0 -1
- package/dist/utils/__tests__/ip.test.d.cts +0 -2
- package/dist/utils/__tests__/ip.test.d.ts +0 -2
- package/dist/utils/__tests__/ip.test.mjs +0 -36
- package/dist/utils/__tests__/ip.test.mjs.map +0 -1
- package/dist/utils/base62.cjs +0 -85
- package/dist/utils/base62.cjs.map +0 -1
- package/dist/utils/base62.d.cts +0 -6
- package/dist/utils/base62.d.ts +0 -6
- package/dist/utils/base62.mjs +0 -60
- package/dist/utils/base62.mjs.map +0 -1
- package/dist/utils/fetch.cjs +0 -78
- package/dist/utils/fetch.cjs.map +0 -1
- package/dist/utils/fetch.d.cts +0 -16
- package/dist/utils/fetch.d.ts +0 -16
- package/dist/utils/fetch.mjs +0 -53
- package/dist/utils/fetch.mjs.map +0 -1
- package/dist/utils/invariant.cjs +0 -37
- package/dist/utils/invariant.cjs.map +0 -1
- package/dist/utils/invariant.d.cts +0 -3
- package/dist/utils/invariant.d.ts +0 -3
- package/dist/utils/invariant.mjs +0 -12
- package/dist/utils/invariant.mjs.map +0 -1
- package/dist/utils/promise.cjs +0 -47
- package/dist/utils/promise.cjs.map +0 -1
- package/dist/utils/promise.d.cts +0 -3
- package/dist/utils/promise.d.ts +0 -3
- package/dist/utils/promise.mjs +0 -22
- package/dist/utils/promise.mjs.map +0 -1
- package/dist/utils/string.cjs +0 -33
- package/dist/utils/string.cjs.map +0 -1
- package/dist/utils/string.d.cts +0 -6
- package/dist/utils/string.d.ts +0 -6
- package/dist/utils/string.mjs +0 -8
- package/dist/utils/string.mjs.map +0 -1
- package/dist/utils/token-bucket.cjs +0 -73
- package/dist/utils/token-bucket.cjs.map +0 -1
- package/dist/utils/token-bucket.d.cts +0 -20
- package/dist/utils/token-bucket.d.ts +0 -20
- package/dist/utils/token-bucket.mjs +0 -48
- package/dist/utils/token-bucket.mjs.map +0 -1
package/dist/utils/base62.d.cts
DELETED
package/dist/utils/base62.d.ts
DELETED
package/dist/utils/base62.mjs
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
// src/utils/base62.ts
|
|
2
|
-
var base62 = {
|
|
3
|
-
encode: (buffer) => {
|
|
4
|
-
const base = 62n;
|
|
5
|
-
const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
6
|
-
let leadingZeros = 0;
|
|
7
|
-
for (const byte of buffer) {
|
|
8
|
-
if (byte === 0) leadingZeros++;
|
|
9
|
-
else break;
|
|
10
|
-
}
|
|
11
|
-
let num = BigInt("0x" + buffer.toString("hex"));
|
|
12
|
-
if (num === 0n) return "0".repeat(buffer.length);
|
|
13
|
-
const chars = [];
|
|
14
|
-
while (num > 0n) {
|
|
15
|
-
const remainder = num % base;
|
|
16
|
-
chars.push(charset[Number(remainder)]);
|
|
17
|
-
num /= base;
|
|
18
|
-
}
|
|
19
|
-
return "0".repeat(leadingZeros) + chars.reverse().join("");
|
|
20
|
-
},
|
|
21
|
-
decode: (string, expectedLength) => {
|
|
22
|
-
const base = 62n;
|
|
23
|
-
const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
24
|
-
let leadingZeros = 0;
|
|
25
|
-
for (const ch of string) {
|
|
26
|
-
if (ch === "0") leadingZeros++;
|
|
27
|
-
else break;
|
|
28
|
-
}
|
|
29
|
-
if (leadingZeros === string.length) {
|
|
30
|
-
return Buffer.alloc(expectedLength ?? leadingZeros);
|
|
31
|
-
}
|
|
32
|
-
let num = 0n;
|
|
33
|
-
for (let i = leadingZeros; i < string.length; i++) {
|
|
34
|
-
const ch = string[i];
|
|
35
|
-
const idx = charset.indexOf(ch);
|
|
36
|
-
if (idx === -1) {
|
|
37
|
-
throw new Error(`Invalid base62 character: ${ch}`);
|
|
38
|
-
}
|
|
39
|
-
num = num * base + BigInt(idx);
|
|
40
|
-
}
|
|
41
|
-
let hex = num.toString(16);
|
|
42
|
-
if (hex.length % 2 !== 0) hex = "0" + hex;
|
|
43
|
-
let buf = Buffer.from(hex, "hex");
|
|
44
|
-
if (leadingZeros > 0) {
|
|
45
|
-
buf = Buffer.concat([Buffer.alloc(leadingZeros), buf]);
|
|
46
|
-
}
|
|
47
|
-
if (expectedLength !== void 0) {
|
|
48
|
-
if (buf.length < expectedLength) {
|
|
49
|
-
buf = Buffer.concat([Buffer.alloc(expectedLength - buf.length), buf]);
|
|
50
|
-
} else if (buf.length > expectedLength) {
|
|
51
|
-
buf = buf.subarray(buf.length - expectedLength);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return buf;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
export {
|
|
58
|
-
base62
|
|
59
|
-
};
|
|
60
|
-
//# sourceMappingURL=base62.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/base62.ts"],"sourcesContent":["export const base62 = {\n encode: (buffer: Buffer): string => {\n const base = 62n;\n const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n let leadingZeros = 0;\n for (const byte of buffer) {\n if (byte === 0) leadingZeros++;\n else break;\n }\n\n let num = BigInt('0x' + buffer.toString('hex'));\n if (num === 0n) return '0'.repeat(buffer.length);\n\n const chars: string[] = [];\n while (num > 0n) {\n const remainder = num % base;\n chars.push(charset[Number(remainder)]!);\n num /= base;\n }\n\n return '0'.repeat(leadingZeros) + chars.reverse().join('');\n },\n decode: (string: string, expectedLength?: number): Buffer => {\n const base = 62n;\n const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n\n let leadingZeros = 0;\n for (const ch of string) {\n if (ch === '0') leadingZeros++;\n else break;\n }\n\n if (leadingZeros === string.length) {\n return Buffer.alloc(expectedLength ?? leadingZeros);\n }\n\n let num = 0n;\n for (let i = leadingZeros; i < string.length; i++) {\n const ch = string[i];\n const idx = charset.indexOf(ch);\n if (idx === -1) {\n throw new Error(`Invalid base62 character: ${ch}`);\n }\n num = num * base + BigInt(idx);\n }\n\n let hex = num.toString(16);\n if (hex.length % 2 !== 0) hex = '0' + hex;\n let buf = Buffer.from(hex, 'hex');\n\n if (leadingZeros > 0) {\n buf = Buffer.concat([Buffer.alloc(leadingZeros), buf]);\n }\n\n if (expectedLength !== undefined) {\n if (buf.length < expectedLength) {\n buf = Buffer.concat([Buffer.alloc(expectedLength - buf.length), buf]);\n } else if (buf.length > expectedLength) {\n buf = buf.subarray(buf.length - expectedLength);\n }\n }\n\n return buf;\n },\n};\n"],"mappings":";AAAO,IAAM,SAAS;AAAA,EACpB,QAAQ,CAAC,WAA2B;AAClC,UAAM,OAAO;AACb,UAAM,UAAU;AAChB,QAAI,eAAe;AACnB,eAAW,QAAQ,QAAQ;AACzB,UAAI,SAAS,EAAG;AAAA,UACX;AAAA,IACP;AAEA,QAAI,MAAM,OAAO,OAAO,OAAO,SAAS,KAAK,CAAC;AAC9C,QAAI,QAAQ,GAAI,QAAO,IAAI,OAAO,OAAO,MAAM;AAE/C,UAAM,QAAkB,CAAC;AACzB,WAAO,MAAM,IAAI;AACf,YAAM,YAAY,MAAM;AACxB,YAAM,KAAK,QAAQ,OAAO,SAAS,CAAC,CAAE;AACtC,aAAO;AAAA,IACT;AAEA,WAAO,IAAI,OAAO,YAAY,IAAI,MAAM,QAAQ,EAAE,KAAK,EAAE;AAAA,EAC3D;AAAA,EACA,QAAQ,CAAC,QAAgB,mBAAoC;AAC3D,UAAM,OAAO;AACb,UAAM,UAAU;AAEhB,QAAI,eAAe;AACnB,eAAW,MAAM,QAAQ;AACvB,UAAI,OAAO,IAAK;AAAA,UACX;AAAA,IACP;AAEA,QAAI,iBAAiB,OAAO,QAAQ;AAClC,aAAO,OAAO,MAAM,kBAAkB,YAAY;AAAA,IACpD;AAEA,QAAI,MAAM;AACV,aAAS,IAAI,cAAc,IAAI,OAAO,QAAQ,KAAK;AACjD,YAAM,KAAK,OAAO,CAAC;AACnB,YAAM,MAAM,QAAQ,QAAQ,EAAE;AAC9B,UAAI,QAAQ,IAAI;AACd,cAAM,IAAI,MAAM,6BAA6B,EAAE,EAAE;AAAA,MACnD;AACA,YAAM,MAAM,OAAO,OAAO,GAAG;AAAA,IAC/B;AAEA,QAAI,MAAM,IAAI,SAAS,EAAE;AACzB,QAAI,IAAI,SAAS,MAAM,EAAG,OAAM,MAAM;AACtC,QAAI,MAAM,OAAO,KAAK,KAAK,KAAK;AAEhC,QAAI,eAAe,GAAG;AACpB,YAAM,OAAO,OAAO,CAAC,OAAO,MAAM,YAAY,GAAG,GAAG,CAAC;AAAA,IACvD;AAEA,QAAI,mBAAmB,QAAW;AAChC,UAAI,IAAI,SAAS,gBAAgB;AAC/B,cAAM,OAAO,OAAO,CAAC,OAAO,MAAM,iBAAiB,IAAI,MAAM,GAAG,GAAG,CAAC;AAAA,MACtE,WAAW,IAAI,SAAS,gBAAgB;AACtC,cAAM,IAAI,SAAS,IAAI,SAAS,cAAc;AAAA,MAChD;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/dist/utils/fetch.cjs
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils/fetch.ts
|
|
21
|
-
var fetch_exports = {};
|
|
22
|
-
__export(fetch_exports, {
|
|
23
|
-
fetch: () => fetch
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(fetch_exports);
|
|
26
|
-
function defaultRetryCondition(response) {
|
|
27
|
-
return response.status === 408 || response.status === 429 || response.status >= 500;
|
|
28
|
-
}
|
|
29
|
-
function parseRetryAfter(response) {
|
|
30
|
-
if (!response) return null;
|
|
31
|
-
const header = response.headers.get("retry-after");
|
|
32
|
-
if (!header) return null;
|
|
33
|
-
const seconds = Number(header);
|
|
34
|
-
if (!Number.isNaN(seconds)) return Math.max(0, seconds * 1e3);
|
|
35
|
-
const dateMs = Date.parse(header);
|
|
36
|
-
if (!Number.isNaN(dateMs)) return Math.max(0, dateMs - Date.now());
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
async function fetch(input, {
|
|
40
|
-
retries = 3,
|
|
41
|
-
delayFactor = 500,
|
|
42
|
-
maxDelay = 3e4,
|
|
43
|
-
retryCondition = defaultRetryCondition,
|
|
44
|
-
...init
|
|
45
|
-
} = {}) {
|
|
46
|
-
let retryCount = 0;
|
|
47
|
-
let lastError = null;
|
|
48
|
-
let lastResponse = null;
|
|
49
|
-
while (retryCount <= retries) {
|
|
50
|
-
try {
|
|
51
|
-
const response = await globalThis.fetch(input, init);
|
|
52
|
-
lastResponse = response;
|
|
53
|
-
if (response.ok || !retryCondition(response) || retryCount === retries) {
|
|
54
|
-
return response;
|
|
55
|
-
}
|
|
56
|
-
const retryAfter = parseRetryAfter(response);
|
|
57
|
-
const delay = delayFactor * Math.pow(2, retryCount);
|
|
58
|
-
const jitter = delay * 0.25 * (Math.random() * 2 - 1);
|
|
59
|
-
const timeout = Math.min(retryAfter ?? delay + jitter, maxDelay);
|
|
60
|
-
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
61
|
-
} catch (error) {
|
|
62
|
-
lastError = error;
|
|
63
|
-
if (retryCount === retries) throw error;
|
|
64
|
-
const delay = delayFactor * Math.pow(2, retryCount);
|
|
65
|
-
const jitter = delay * 0.25 * (Math.random() * 2 - 1);
|
|
66
|
-
const timeout = Math.min(delay + jitter, maxDelay);
|
|
67
|
-
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
68
|
-
}
|
|
69
|
-
retryCount++;
|
|
70
|
-
}
|
|
71
|
-
if (lastResponse) return lastResponse;
|
|
72
|
-
throw lastError ?? new Error("Fetch failed");
|
|
73
|
-
}
|
|
74
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
75
|
-
0 && (module.exports = {
|
|
76
|
-
fetch
|
|
77
|
-
});
|
|
78
|
-
//# sourceMappingURL=fetch.cjs.map
|
package/dist/utils/fetch.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/fetch.ts"],"sourcesContent":["export interface RetryOptions {\n /** The number of times to retry before failing, default: 3 */\n retries?: number;\n\n /** The delay factor in milliseconds, default: 500 */\n delayFactor?: number;\n\n /** The maximum delay in milliseconds, default: 30_000 (30s) */\n maxDelay?: number;\n\n /**\n * A callback to further control if a request should be retried.\n * default: 408 (Request Timeout) or 429 (Too Many Requests) or 5xx (Server Error).\n */\n retryCondition?: (response: Response) => boolean;\n}\n\nfunction defaultRetryCondition(response: Response): boolean {\n return response.status === 408 || response.status === 429 || response.status >= 500;\n}\n\nfunction parseRetryAfter(response: Response | null): number | null {\n if (!response) return null;\n const header = response.headers.get('retry-after');\n if (!header) return null;\n\n // if the retry after header is a number, convert it to milliseconds\n const seconds = Number(header);\n if (!Number.isNaN(seconds)) return Math.max(0, seconds * 1000);\n\n // if the retry after header is a date, get the number of milliseconds until that date\n const dateMs = Date.parse(header);\n if (!Number.isNaN(dateMs)) return Math.max(0, dateMs - Date.now());\n\n return null;\n}\n\nexport async function fetch(\n input: RequestInfo,\n {\n retries = 3,\n delayFactor = 500,\n maxDelay = 30_000,\n retryCondition = defaultRetryCondition,\n ...init\n }: RequestInit & RetryOptions = {}\n): Promise<Response> {\n let retryCount = 0;\n let lastError: unknown | null = null;\n let lastResponse: Response | null = null;\n\n while (retryCount <= retries) {\n try {\n const response = await globalThis.fetch(input, init);\n lastResponse = response;\n if (response.ok || !retryCondition(response) || retryCount === retries) {\n return response;\n }\n\n const retryAfter = parseRetryAfter(response);\n const delay = delayFactor * Math.pow(2, retryCount);\n const jitter = delay * 0.25 * (Math.random() * 2 - 1); // 25% jitter\n\n const timeout = Math.min(retryAfter ?? delay + jitter, maxDelay);\n await new Promise((resolve) => setTimeout(resolve, timeout));\n } catch (error) {\n lastError = error;\n if (retryCount === retries) throw error;\n\n const delay = delayFactor * Math.pow(2, retryCount);\n const jitter = delay * 0.25 * (Math.random() * 2 - 1); // 25% jitter\n\n const timeout = Math.min(delay + jitter, maxDelay);\n await new Promise((resolve) => setTimeout(resolve, timeout));\n }\n\n retryCount++;\n }\n\n if (lastResponse) return lastResponse;\n throw lastError ?? new Error('Fetch failed');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBA,SAAS,sBAAsB,UAA6B;AAC1D,SAAO,SAAS,WAAW,OAAO,SAAS,WAAW,OAAO,SAAS,UAAU;AAClF;AAEA,SAAS,gBAAgB,UAA0C;AACjE,MAAI,CAAC,SAAU,QAAO;AACtB,QAAM,SAAS,SAAS,QAAQ,IAAI,aAAa;AACjD,MAAI,CAAC,OAAQ,QAAO;AAGpB,QAAM,UAAU,OAAO,MAAM;AAC7B,MAAI,CAAC,OAAO,MAAM,OAAO,EAAG,QAAO,KAAK,IAAI,GAAG,UAAU,GAAI;AAG7D,QAAM,SAAS,KAAK,MAAM,MAAM;AAChC,MAAI,CAAC,OAAO,MAAM,MAAM,EAAG,QAAO,KAAK,IAAI,GAAG,SAAS,KAAK,IAAI,CAAC;AAEjE,SAAO;AACT;AAEA,eAAsB,MACpB,OACA;AAAA,EACE,UAAU;AAAA,EACV,cAAc;AAAA,EACd,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,GAAG;AACL,IAAgC,CAAC,GACd;AACnB,MAAI,aAAa;AACjB,MAAI,YAA4B;AAChC,MAAI,eAAgC;AAEpC,SAAO,cAAc,SAAS;AAC5B,QAAI;AACF,YAAM,WAAW,MAAM,WAAW,MAAM,OAAO,IAAI;AACnD,qBAAe;AACf,UAAI,SAAS,MAAM,CAAC,eAAe,QAAQ,KAAK,eAAe,SAAS;AACtE,eAAO;AAAA,MACT;AAEA,YAAM,aAAa,gBAAgB,QAAQ;AAC3C,YAAM,QAAQ,cAAc,KAAK,IAAI,GAAG,UAAU;AAClD,YAAM,SAAS,QAAQ,QAAQ,KAAK,OAAO,IAAI,IAAI;AAEnD,YAAM,UAAU,KAAK,IAAI,cAAc,QAAQ,QAAQ,QAAQ;AAC/D,YAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,OAAO,CAAC;AAAA,IAC7D,SAAS,OAAO;AACd,kBAAY;AACZ,UAAI,eAAe,QAAS,OAAM;AAElC,YAAM,QAAQ,cAAc,KAAK,IAAI,GAAG,UAAU;AAClD,YAAM,SAAS,QAAQ,QAAQ,KAAK,OAAO,IAAI,IAAI;AAEnD,YAAM,UAAU,KAAK,IAAI,QAAQ,QAAQ,QAAQ;AACjD,YAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,OAAO,CAAC;AAAA,IAC7D;AAEA;AAAA,EACF;AAEA,MAAI,aAAc,QAAO;AACzB,QAAM,aAAa,IAAI,MAAM,cAAc;AAC7C;","names":[]}
|
package/dist/utils/fetch.d.cts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
interface RetryOptions {
|
|
2
|
-
/** The number of times to retry before failing, default: 3 */
|
|
3
|
-
retries?: number;
|
|
4
|
-
/** The delay factor in milliseconds, default: 500 */
|
|
5
|
-
delayFactor?: number;
|
|
6
|
-
/** The maximum delay in milliseconds, default: 30_000 (30s) */
|
|
7
|
-
maxDelay?: number;
|
|
8
|
-
/**
|
|
9
|
-
* A callback to further control if a request should be retried.
|
|
10
|
-
* default: 408 (Request Timeout) or 429 (Too Many Requests) or 5xx (Server Error).
|
|
11
|
-
*/
|
|
12
|
-
retryCondition?: (response: Response) => boolean;
|
|
13
|
-
}
|
|
14
|
-
declare function fetch(input: RequestInfo, { retries, delayFactor, maxDelay, retryCondition, ...init }?: RequestInit & RetryOptions): Promise<Response>;
|
|
15
|
-
|
|
16
|
-
export { type RetryOptions, fetch };
|
package/dist/utils/fetch.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
interface RetryOptions {
|
|
2
|
-
/** The number of times to retry before failing, default: 3 */
|
|
3
|
-
retries?: number;
|
|
4
|
-
/** The delay factor in milliseconds, default: 500 */
|
|
5
|
-
delayFactor?: number;
|
|
6
|
-
/** The maximum delay in milliseconds, default: 30_000 (30s) */
|
|
7
|
-
maxDelay?: number;
|
|
8
|
-
/**
|
|
9
|
-
* A callback to further control if a request should be retried.
|
|
10
|
-
* default: 408 (Request Timeout) or 429 (Too Many Requests) or 5xx (Server Error).
|
|
11
|
-
*/
|
|
12
|
-
retryCondition?: (response: Response) => boolean;
|
|
13
|
-
}
|
|
14
|
-
declare function fetch(input: RequestInfo, { retries, delayFactor, maxDelay, retryCondition, ...init }?: RequestInit & RetryOptions): Promise<Response>;
|
|
15
|
-
|
|
16
|
-
export { type RetryOptions, fetch };
|
package/dist/utils/fetch.mjs
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
// src/utils/fetch.ts
|
|
2
|
-
function defaultRetryCondition(response) {
|
|
3
|
-
return response.status === 408 || response.status === 429 || response.status >= 500;
|
|
4
|
-
}
|
|
5
|
-
function parseRetryAfter(response) {
|
|
6
|
-
if (!response) return null;
|
|
7
|
-
const header = response.headers.get("retry-after");
|
|
8
|
-
if (!header) return null;
|
|
9
|
-
const seconds = Number(header);
|
|
10
|
-
if (!Number.isNaN(seconds)) return Math.max(0, seconds * 1e3);
|
|
11
|
-
const dateMs = Date.parse(header);
|
|
12
|
-
if (!Number.isNaN(dateMs)) return Math.max(0, dateMs - Date.now());
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
async function fetch(input, {
|
|
16
|
-
retries = 3,
|
|
17
|
-
delayFactor = 500,
|
|
18
|
-
maxDelay = 3e4,
|
|
19
|
-
retryCondition = defaultRetryCondition,
|
|
20
|
-
...init
|
|
21
|
-
} = {}) {
|
|
22
|
-
let retryCount = 0;
|
|
23
|
-
let lastError = null;
|
|
24
|
-
let lastResponse = null;
|
|
25
|
-
while (retryCount <= retries) {
|
|
26
|
-
try {
|
|
27
|
-
const response = await globalThis.fetch(input, init);
|
|
28
|
-
lastResponse = response;
|
|
29
|
-
if (response.ok || !retryCondition(response) || retryCount === retries) {
|
|
30
|
-
return response;
|
|
31
|
-
}
|
|
32
|
-
const retryAfter = parseRetryAfter(response);
|
|
33
|
-
const delay = delayFactor * Math.pow(2, retryCount);
|
|
34
|
-
const jitter = delay * 0.25 * (Math.random() * 2 - 1);
|
|
35
|
-
const timeout = Math.min(retryAfter ?? delay + jitter, maxDelay);
|
|
36
|
-
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
37
|
-
} catch (error) {
|
|
38
|
-
lastError = error;
|
|
39
|
-
if (retryCount === retries) throw error;
|
|
40
|
-
const delay = delayFactor * Math.pow(2, retryCount);
|
|
41
|
-
const jitter = delay * 0.25 * (Math.random() * 2 - 1);
|
|
42
|
-
const timeout = Math.min(delay + jitter, maxDelay);
|
|
43
|
-
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
44
|
-
}
|
|
45
|
-
retryCount++;
|
|
46
|
-
}
|
|
47
|
-
if (lastResponse) return lastResponse;
|
|
48
|
-
throw lastError ?? new Error("Fetch failed");
|
|
49
|
-
}
|
|
50
|
-
export {
|
|
51
|
-
fetch
|
|
52
|
-
};
|
|
53
|
-
//# sourceMappingURL=fetch.mjs.map
|
package/dist/utils/fetch.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/fetch.ts"],"sourcesContent":["export interface RetryOptions {\n /** The number of times to retry before failing, default: 3 */\n retries?: number;\n\n /** The delay factor in milliseconds, default: 500 */\n delayFactor?: number;\n\n /** The maximum delay in milliseconds, default: 30_000 (30s) */\n maxDelay?: number;\n\n /**\n * A callback to further control if a request should be retried.\n * default: 408 (Request Timeout) or 429 (Too Many Requests) or 5xx (Server Error).\n */\n retryCondition?: (response: Response) => boolean;\n}\n\nfunction defaultRetryCondition(response: Response): boolean {\n return response.status === 408 || response.status === 429 || response.status >= 500;\n}\n\nfunction parseRetryAfter(response: Response | null): number | null {\n if (!response) return null;\n const header = response.headers.get('retry-after');\n if (!header) return null;\n\n // if the retry after header is a number, convert it to milliseconds\n const seconds = Number(header);\n if (!Number.isNaN(seconds)) return Math.max(0, seconds * 1000);\n\n // if the retry after header is a date, get the number of milliseconds until that date\n const dateMs = Date.parse(header);\n if (!Number.isNaN(dateMs)) return Math.max(0, dateMs - Date.now());\n\n return null;\n}\n\nexport async function fetch(\n input: RequestInfo,\n {\n retries = 3,\n delayFactor = 500,\n maxDelay = 30_000,\n retryCondition = defaultRetryCondition,\n ...init\n }: RequestInit & RetryOptions = {}\n): Promise<Response> {\n let retryCount = 0;\n let lastError: unknown | null = null;\n let lastResponse: Response | null = null;\n\n while (retryCount <= retries) {\n try {\n const response = await globalThis.fetch(input, init);\n lastResponse = response;\n if (response.ok || !retryCondition(response) || retryCount === retries) {\n return response;\n }\n\n const retryAfter = parseRetryAfter(response);\n const delay = delayFactor * Math.pow(2, retryCount);\n const jitter = delay * 0.25 * (Math.random() * 2 - 1); // 25% jitter\n\n const timeout = Math.min(retryAfter ?? delay + jitter, maxDelay);\n await new Promise((resolve) => setTimeout(resolve, timeout));\n } catch (error) {\n lastError = error;\n if (retryCount === retries) throw error;\n\n const delay = delayFactor * Math.pow(2, retryCount);\n const jitter = delay * 0.25 * (Math.random() * 2 - 1); // 25% jitter\n\n const timeout = Math.min(delay + jitter, maxDelay);\n await new Promise((resolve) => setTimeout(resolve, timeout));\n }\n\n retryCount++;\n }\n\n if (lastResponse) return lastResponse;\n throw lastError ?? new Error('Fetch failed');\n}\n"],"mappings":";AAiBA,SAAS,sBAAsB,UAA6B;AAC1D,SAAO,SAAS,WAAW,OAAO,SAAS,WAAW,OAAO,SAAS,UAAU;AAClF;AAEA,SAAS,gBAAgB,UAA0C;AACjE,MAAI,CAAC,SAAU,QAAO;AACtB,QAAM,SAAS,SAAS,QAAQ,IAAI,aAAa;AACjD,MAAI,CAAC,OAAQ,QAAO;AAGpB,QAAM,UAAU,OAAO,MAAM;AAC7B,MAAI,CAAC,OAAO,MAAM,OAAO,EAAG,QAAO,KAAK,IAAI,GAAG,UAAU,GAAI;AAG7D,QAAM,SAAS,KAAK,MAAM,MAAM;AAChC,MAAI,CAAC,OAAO,MAAM,MAAM,EAAG,QAAO,KAAK,IAAI,GAAG,SAAS,KAAK,IAAI,CAAC;AAEjE,SAAO;AACT;AAEA,eAAsB,MACpB,OACA;AAAA,EACE,UAAU;AAAA,EACV,cAAc;AAAA,EACd,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,GAAG;AACL,IAAgC,CAAC,GACd;AACnB,MAAI,aAAa;AACjB,MAAI,YAA4B;AAChC,MAAI,eAAgC;AAEpC,SAAO,cAAc,SAAS;AAC5B,QAAI;AACF,YAAM,WAAW,MAAM,WAAW,MAAM,OAAO,IAAI;AACnD,qBAAe;AACf,UAAI,SAAS,MAAM,CAAC,eAAe,QAAQ,KAAK,eAAe,SAAS;AACtE,eAAO;AAAA,MACT;AAEA,YAAM,aAAa,gBAAgB,QAAQ;AAC3C,YAAM,QAAQ,cAAc,KAAK,IAAI,GAAG,UAAU;AAClD,YAAM,SAAS,QAAQ,QAAQ,KAAK,OAAO,IAAI,IAAI;AAEnD,YAAM,UAAU,KAAK,IAAI,cAAc,QAAQ,QAAQ,QAAQ;AAC/D,YAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,OAAO,CAAC;AAAA,IAC7D,SAAS,OAAO;AACd,kBAAY;AACZ,UAAI,eAAe,QAAS,OAAM;AAElC,YAAM,QAAQ,cAAc,KAAK,IAAI,GAAG,UAAU;AAClD,YAAM,SAAS,QAAQ,QAAQ,KAAK,OAAO,IAAI,IAAI;AAEnD,YAAM,UAAU,KAAK,IAAI,QAAQ,QAAQ,QAAQ;AACjD,YAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,OAAO,CAAC;AAAA,IAC7D;AAEA;AAAA,EACF;AAEA,MAAI,aAAc,QAAO;AACzB,QAAM,aAAa,IAAI,MAAM,cAAc;AAC7C;","names":[]}
|
package/dist/utils/invariant.cjs
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils/invariant.ts
|
|
21
|
-
var invariant_exports = {};
|
|
22
|
-
__export(invariant_exports, {
|
|
23
|
-
invariant: () => invariant
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(invariant_exports);
|
|
26
|
-
var prefix = "Invariant failed";
|
|
27
|
-
function invariant(condition, message) {
|
|
28
|
-
if (condition) return;
|
|
29
|
-
const provided = typeof message === "function" ? message() : message;
|
|
30
|
-
const value = provided ? `${prefix}: ${provided}` : prefix;
|
|
31
|
-
throw new Error(value);
|
|
32
|
-
}
|
|
33
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
-
0 && (module.exports = {
|
|
35
|
-
invariant
|
|
36
|
-
});
|
|
37
|
-
//# sourceMappingURL=invariant.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/invariant.ts"],"sourcesContent":["const prefix = 'Invariant failed';\n\nexport function invariant(\n condition: unknown,\n message?: string | (() => string)\n): asserts condition {\n if (condition) return;\n const provided: string | undefined = typeof message === 'function' ? message() : message;\n const value: string = provided ? `${prefix}: ${provided}` : prefix;\n throw new Error(value);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAM,SAAS;AAER,SAAS,UACd,WACA,SACmB;AACnB,MAAI,UAAW;AACf,QAAM,WAA+B,OAAO,YAAY,aAAa,QAAQ,IAAI;AACjF,QAAM,QAAgB,WAAW,GAAG,MAAM,KAAK,QAAQ,KAAK;AAC5D,QAAM,IAAI,MAAM,KAAK;AACvB;","names":[]}
|
package/dist/utils/invariant.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// src/utils/invariant.ts
|
|
2
|
-
var prefix = "Invariant failed";
|
|
3
|
-
function invariant(condition, message) {
|
|
4
|
-
if (condition) return;
|
|
5
|
-
const provided = typeof message === "function" ? message() : message;
|
|
6
|
-
const value = provided ? `${prefix}: ${provided}` : prefix;
|
|
7
|
-
throw new Error(value);
|
|
8
|
-
}
|
|
9
|
-
export {
|
|
10
|
-
invariant
|
|
11
|
-
};
|
|
12
|
-
//# sourceMappingURL=invariant.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/invariant.ts"],"sourcesContent":["const prefix = 'Invariant failed';\n\nexport function invariant(\n condition: unknown,\n message?: string | (() => string)\n): asserts condition {\n if (condition) return;\n const provided: string | undefined = typeof message === 'function' ? message() : message;\n const value: string = provided ? `${prefix}: ${provided}` : prefix;\n throw new Error(value);\n}\n"],"mappings":";AAAA,IAAM,SAAS;AAER,SAAS,UACd,WACA,SACmB;AACnB,MAAI,UAAW;AACf,QAAM,WAA+B,OAAO,YAAY,aAAa,QAAQ,IAAI;AACjF,QAAM,QAAgB,WAAW,GAAG,MAAM,KAAK,QAAQ,KAAK;AAC5D,QAAM,IAAI,MAAM,KAAK;AACvB;","names":[]}
|
package/dist/utils/promise.cjs
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils/promise.ts
|
|
21
|
-
var promise_exports = {};
|
|
22
|
-
__export(promise_exports, {
|
|
23
|
-
once: () => once
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(promise_exports);
|
|
26
|
-
function once(fn) {
|
|
27
|
-
let cache = null;
|
|
28
|
-
let promise = null;
|
|
29
|
-
return async (...args) => {
|
|
30
|
-
if (cache !== null) return cache;
|
|
31
|
-
if (!promise) {
|
|
32
|
-
promise = fn(...args).then((result) => {
|
|
33
|
-
cache = result;
|
|
34
|
-
return result;
|
|
35
|
-
}).catch((error) => {
|
|
36
|
-
promise = null;
|
|
37
|
-
throw error;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
return promise;
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
-
0 && (module.exports = {
|
|
45
|
-
once
|
|
46
|
-
});
|
|
47
|
-
//# sourceMappingURL=promise.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/promise.ts"],"sourcesContent":["export function once<A extends unknown[], T>(fn: (...args: A) => Promise<T>) {\n let cache: T | null = null;\n let promise: Promise<T> | null = null;\n\n return async (...args: A) => {\n if (cache !== null) return cache;\n if (!promise) {\n promise = fn(...args)\n .then((result) => {\n cache = result;\n return result;\n })\n .catch((error) => {\n promise = null;\n throw error;\n });\n }\n\n return promise;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,SAAS,KAA6B,IAAgC;AAC3E,MAAI,QAAkB;AACtB,MAAI,UAA6B;AAEjC,SAAO,UAAU,SAAY;AAC3B,QAAI,UAAU,KAAM,QAAO;AAC3B,QAAI,CAAC,SAAS;AACZ,gBAAU,GAAG,GAAG,IAAI,EACjB,KAAK,CAAC,WAAW;AAChB,gBAAQ;AACR,eAAO;AAAA,MACT,CAAC,EACA,MAAM,CAAC,UAAU;AAChB,kBAAU;AACV,cAAM;AAAA,MACR,CAAC;AAAA,IACL;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/dist/utils/promise.d.cts
DELETED
package/dist/utils/promise.d.ts
DELETED
package/dist/utils/promise.mjs
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
// src/utils/promise.ts
|
|
2
|
-
function once(fn) {
|
|
3
|
-
let cache = null;
|
|
4
|
-
let promise = null;
|
|
5
|
-
return async (...args) => {
|
|
6
|
-
if (cache !== null) return cache;
|
|
7
|
-
if (!promise) {
|
|
8
|
-
promise = fn(...args).then((result) => {
|
|
9
|
-
cache = result;
|
|
10
|
-
return result;
|
|
11
|
-
}).catch((error) => {
|
|
12
|
-
promise = null;
|
|
13
|
-
throw error;
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return promise;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
export {
|
|
20
|
-
once
|
|
21
|
-
};
|
|
22
|
-
//# sourceMappingURL=promise.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/promise.ts"],"sourcesContent":["export function once<A extends unknown[], T>(fn: (...args: A) => Promise<T>) {\n let cache: T | null = null;\n let promise: Promise<T> | null = null;\n\n return async (...args: A) => {\n if (cache !== null) return cache;\n if (!promise) {\n promise = fn(...args)\n .then((result) => {\n cache = result;\n return result;\n })\n .catch((error) => {\n promise = null;\n throw error;\n });\n }\n\n return promise;\n };\n}\n"],"mappings":";AAAO,SAAS,KAA6B,IAAgC;AAC3E,MAAI,QAAkB;AACtB,MAAI,UAA6B;AAEjC,SAAO,UAAU,SAAY;AAC3B,QAAI,UAAU,KAAM,QAAO;AAC3B,QAAI,CAAC,SAAS;AACZ,gBAAU,GAAG,GAAG,IAAI,EACjB,KAAK,CAAC,WAAW;AAChB,gBAAQ;AACR,eAAO;AAAA,MACT,CAAC,EACA,MAAM,CAAC,UAAU;AAChB,kBAAU;AACV,cAAM;AAAA,MACR,CAAC;AAAA,IACL;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/dist/utils/string.cjs
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils/string.ts
|
|
21
|
-
var string_exports = {};
|
|
22
|
-
__export(string_exports, {
|
|
23
|
-
hasText: () => hasText
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(string_exports);
|
|
26
|
-
function hasText(str) {
|
|
27
|
-
return typeof str === "string" && str.trim().length !== 0;
|
|
28
|
-
}
|
|
29
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
-
0 && (module.exports = {
|
|
31
|
-
hasText
|
|
32
|
-
});
|
|
33
|
-
//# sourceMappingURL=string.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/string.ts"],"sourcesContent":["/**\n * Check whether the given String contains actual text.\n * */\nexport function hasText(str: string | null | undefined): str is string {\n return typeof str === 'string' && str.trim().length !== 0;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,QAAQ,KAA+C;AACrE,SAAO,OAAO,QAAQ,YAAY,IAAI,KAAK,EAAE,WAAW;AAC1D;","names":[]}
|
package/dist/utils/string.d.cts
DELETED
package/dist/utils/string.d.ts
DELETED
package/dist/utils/string.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/string.ts"],"sourcesContent":["/**\n * Check whether the given String contains actual text.\n * */\nexport function hasText(str: string | null | undefined): str is string {\n return typeof str === 'string' && str.trim().length !== 0;\n}\n"],"mappings":";AAGO,SAAS,QAAQ,KAA+C;AACrE,SAAO,OAAO,QAAQ,YAAY,IAAI,KAAK,EAAE,WAAW;AAC1D;","names":[]}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils/token-bucket.ts
|
|
21
|
-
var token_bucket_exports = {};
|
|
22
|
-
__export(token_bucket_exports, {
|
|
23
|
-
TokenBucket: () => TokenBucket
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(token_bucket_exports);
|
|
26
|
-
var INTERVAL_MAP = {
|
|
27
|
-
second: 1e3,
|
|
28
|
-
minute: 60 * 1e3,
|
|
29
|
-
hour: 60 * 60 * 1e3,
|
|
30
|
-
day: 24 * 60 * 60 * 1e3
|
|
31
|
-
};
|
|
32
|
-
var TokenBucket = class {
|
|
33
|
-
rate;
|
|
34
|
-
capacity;
|
|
35
|
-
requested;
|
|
36
|
-
timer;
|
|
37
|
-
tokens;
|
|
38
|
-
constructor({ rate, capacity, requested, interval = "second" }) {
|
|
39
|
-
if (rate <= 0) throw new Error("rate must be greater than 0");
|
|
40
|
-
if (capacity <= 0) throw new Error("capacity must be greater than 0");
|
|
41
|
-
if (requested <= 0) throw new Error("requested must be greater than 0");
|
|
42
|
-
if (requested > capacity) throw new Error("requested must be less than or equal to capacity");
|
|
43
|
-
this.rate = rate;
|
|
44
|
-
this.capacity = capacity;
|
|
45
|
-
this.requested = requested;
|
|
46
|
-
this.tokens = capacity;
|
|
47
|
-
this.timer = setInterval(() => {
|
|
48
|
-
if (this.tokens < this.capacity) {
|
|
49
|
-
const tokens = this.tokens + this.rate;
|
|
50
|
-
this.tokens = Math.min(tokens, this.capacity);
|
|
51
|
-
}
|
|
52
|
-
}, INTERVAL_MAP[interval]);
|
|
53
|
-
}
|
|
54
|
-
wait(ms) {
|
|
55
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
56
|
-
}
|
|
57
|
-
async removeTokens() {
|
|
58
|
-
while (this.tokens < this.requested) {
|
|
59
|
-
const ms = Math.ceil(1e3 * (this.requested - this.tokens) / this.rate);
|
|
60
|
-
await this.wait(ms);
|
|
61
|
-
}
|
|
62
|
-
this.tokens -= this.requested;
|
|
63
|
-
return this.tokens;
|
|
64
|
-
}
|
|
65
|
-
destroy() {
|
|
66
|
-
clearInterval(this.timer);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
70
|
-
0 && (module.exports = {
|
|
71
|
-
TokenBucket
|
|
72
|
-
});
|
|
73
|
-
//# sourceMappingURL=token-bucket.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/token-bucket.ts"],"sourcesContent":["type Interval = 'second' | 'minute' | 'hour' | 'day';\n\nconst INTERVAL_MAP: Record<Interval, number> = {\n second: 1000,\n minute: 60 * 1000,\n hour: 60 * 60 * 1000,\n day: 24 * 60 * 60 * 1000,\n};\n\nexport interface TokenBucketOptions {\n rate: number;\n capacity: number;\n requested: number;\n interval?: Interval;\n}\n\nexport class TokenBucket {\n readonly rate: number;\n readonly capacity: number;\n readonly requested: number;\n private readonly timer: number | NodeJS.Timeout;\n private tokens: number;\n\n constructor({ rate, capacity, requested, interval = 'second' }: TokenBucketOptions) {\n if (rate <= 0) throw new Error('rate must be greater than 0');\n if (capacity <= 0) throw new Error('capacity must be greater than 0');\n if (requested <= 0) throw new Error('requested must be greater than 0');\n if (requested > capacity) throw new Error('requested must be less than or equal to capacity');\n\n this.rate = rate;\n this.capacity = capacity;\n this.requested = requested;\n this.tokens = capacity;\n this.timer = setInterval(() => {\n if (this.tokens < this.capacity) {\n const tokens = this.tokens + this.rate;\n this.tokens = Math.min(tokens, this.capacity);\n }\n }, INTERVAL_MAP[interval]);\n }\n\n private wait(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n async removeTokens(): Promise<number> {\n while (this.tokens < this.requested) {\n const ms = Math.ceil((1000 * (this.requested - this.tokens)) / this.rate);\n await this.wait(ms);\n }\n this.tokens -= this.requested;\n return this.tokens;\n }\n\n destroy() {\n clearInterval(this.timer);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAM,eAAyC;AAAA,EAC7C,QAAQ;AAAA,EACR,QAAQ,KAAK;AAAA,EACb,MAAM,KAAK,KAAK;AAAA,EAChB,KAAK,KAAK,KAAK,KAAK;AACtB;AASO,IAAM,cAAN,MAAkB;AAAA,EACd;AAAA,EACA;AAAA,EACA;AAAA,EACQ;AAAA,EACT;AAAA,EAER,YAAY,EAAE,MAAM,UAAU,WAAW,WAAW,SAAS,GAAuB;AAClF,QAAI,QAAQ,EAAG,OAAM,IAAI,MAAM,6BAA6B;AAC5D,QAAI,YAAY,EAAG,OAAM,IAAI,MAAM,iCAAiC;AACpE,QAAI,aAAa,EAAG,OAAM,IAAI,MAAM,kCAAkC;AACtE,QAAI,YAAY,SAAU,OAAM,IAAI,MAAM,kDAAkD;AAE5F,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,SAAK,YAAY;AACjB,SAAK,SAAS;AACd,SAAK,QAAQ,YAAY,MAAM;AAC7B,UAAI,KAAK,SAAS,KAAK,UAAU;AAC/B,cAAM,SAAS,KAAK,SAAS,KAAK;AAClC,aAAK,SAAS,KAAK,IAAI,QAAQ,KAAK,QAAQ;AAAA,MAC9C;AAAA,IACF,GAAG,aAAa,QAAQ,CAAC;AAAA,EAC3B;AAAA,EAEQ,KAAK,IAA2B;AACtC,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,EACzD;AAAA,EAEA,MAAM,eAAgC;AACpC,WAAO,KAAK,SAAS,KAAK,WAAW;AACnC,YAAM,KAAK,KAAK,KAAM,OAAQ,KAAK,YAAY,KAAK,UAAW,KAAK,IAAI;AACxE,YAAM,KAAK,KAAK,EAAE;AAAA,IACpB;AACA,SAAK,UAAU,KAAK;AACpB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,UAAU;AACR,kBAAc,KAAK,KAAK;AAAA,EAC1B;AACF;","names":[]}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
type Interval = 'second' | 'minute' | 'hour' | 'day';
|
|
2
|
-
interface TokenBucketOptions {
|
|
3
|
-
rate: number;
|
|
4
|
-
capacity: number;
|
|
5
|
-
requested: number;
|
|
6
|
-
interval?: Interval;
|
|
7
|
-
}
|
|
8
|
-
declare class TokenBucket {
|
|
9
|
-
readonly rate: number;
|
|
10
|
-
readonly capacity: number;
|
|
11
|
-
readonly requested: number;
|
|
12
|
-
private readonly timer;
|
|
13
|
-
private tokens;
|
|
14
|
-
constructor({ rate, capacity, requested, interval }: TokenBucketOptions);
|
|
15
|
-
private wait;
|
|
16
|
-
removeTokens(): Promise<number>;
|
|
17
|
-
destroy(): void;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export { TokenBucket, type TokenBucketOptions };
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
type Interval = 'second' | 'minute' | 'hour' | 'day';
|
|
2
|
-
interface TokenBucketOptions {
|
|
3
|
-
rate: number;
|
|
4
|
-
capacity: number;
|
|
5
|
-
requested: number;
|
|
6
|
-
interval?: Interval;
|
|
7
|
-
}
|
|
8
|
-
declare class TokenBucket {
|
|
9
|
-
readonly rate: number;
|
|
10
|
-
readonly capacity: number;
|
|
11
|
-
readonly requested: number;
|
|
12
|
-
private readonly timer;
|
|
13
|
-
private tokens;
|
|
14
|
-
constructor({ rate, capacity, requested, interval }: TokenBucketOptions);
|
|
15
|
-
private wait;
|
|
16
|
-
removeTokens(): Promise<number>;
|
|
17
|
-
destroy(): void;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export { TokenBucket, type TokenBucketOptions };
|