@shware/analytics 2.16.1 → 2.17.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/feedback/index.cjs +2 -2
- package/dist/feedback/index.cjs.map +1 -1
- package/dist/feedback/index.mjs +1 -1
- package/dist/feedback/index.mjs.map +1 -1
- package/dist/hooks/use-click-id-persistence.cjs +3 -3
- package/dist/hooks/use-click-id-persistence.cjs.map +1 -1
- package/dist/hooks/use-click-id-persistence.mjs +1 -1
- package/dist/hooks/use-click-id-persistence.mjs.map +1 -1
- package/dist/hooks/use-web-session-analytics.cjs +18 -11
- package/dist/hooks/use-web-session-analytics.cjs.map +1 -1
- package/dist/hooks/use-web-session-analytics.mjs +18 -11
- package/dist/hooks/use-web-session-analytics.mjs.map +1 -1
- package/dist/link/index.cjs +3 -3
- package/dist/link/index.cjs.map +1 -1
- package/dist/link/index.mjs +1 -1
- package/dist/link/index.mjs.map +1 -1
- package/dist/server/linkedin-conversions-api.cjs +2 -2
- package/dist/server/linkedin-conversions-api.cjs.map +1 -1
- package/dist/server/linkedin-conversions-api.mjs +1 -1
- package/dist/server/linkedin-conversions-api.mjs.map +1 -1
- package/dist/server/reddit-conversions-api.cjs +2 -2
- package/dist/server/reddit-conversions-api.cjs.map +1 -1
- package/dist/server/reddit-conversions-api.mjs +1 -1
- package/dist/server/reddit-conversions-api.mjs.map +1 -1
- package/dist/setup/session.cjs +2 -0
- package/dist/setup/session.cjs.map +1 -1
- package/dist/setup/session.d.cts +2 -1
- package/dist/setup/session.d.ts +2 -1
- package/dist/setup/session.mjs +1 -0
- package/dist/setup/session.mjs.map +1 -1
- package/dist/track/index.cjs +3 -4
- package/dist/track/index.cjs.map +1 -1
- package/dist/track/index.mjs +1 -2
- package/dist/track/index.mjs.map +1 -1
- package/dist/visitor/index.cjs +4 -4
- package/dist/visitor/index.cjs.map +1 -1
- package/dist/visitor/index.mjs +1 -1
- package/dist/visitor/index.mjs.map +1 -1
- package/dist/web/index.cjs +3 -3
- package/dist/web/index.cjs.map +1 -1
- package/dist/web/index.mjs +1 -1
- package/dist/web/index.mjs.map +1 -1
- package/package.json +3 -2
- 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/storage.cjs +0 -56
- package/dist/utils/storage.cjs.map +0 -1
- package/dist/utils/storage.d.cts +0 -10
- package/dist/utils/storage.d.ts +0 -10
- package/dist/utils/storage.mjs +0 -31
- package/dist/utils/storage.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/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,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jD,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/storage.cjs
DELETED
|
@@ -1,56 +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/storage.ts
|
|
21
|
-
var storage_exports = {};
|
|
22
|
-
__export(storage_exports, {
|
|
23
|
-
expiringStorage: () => expiringStorage
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(storage_exports);
|
|
26
|
-
var expiringStorage = {
|
|
27
|
-
setItem: (key, value, ttlInMs) => {
|
|
28
|
-
const now = Date.now();
|
|
29
|
-
const item = { value, expiresAt: now + ttlInMs };
|
|
30
|
-
try {
|
|
31
|
-
localStorage.setItem(key, JSON.stringify(item));
|
|
32
|
-
} catch (error) {
|
|
33
|
-
console.error("Failed to set item with expiry:", error);
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
getItem: (key) => {
|
|
37
|
-
const itemStr = localStorage.getItem(key);
|
|
38
|
-
if (!itemStr) return null;
|
|
39
|
-
try {
|
|
40
|
-
const item = JSON.parse(itemStr);
|
|
41
|
-
const now = Date.now();
|
|
42
|
-
if (now > item.expiresAt) {
|
|
43
|
-
localStorage.removeItem(key);
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
return item.value;
|
|
47
|
-
} catch {
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
53
|
-
0 && (module.exports = {
|
|
54
|
-
expiringStorage
|
|
55
|
-
});
|
|
56
|
-
//# sourceMappingURL=storage.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/storage.ts"],"sourcesContent":["export type LocalStorageItemWithExpiry<T = unknown> = { value: T; expiresAt: number };\n\nexport const expiringStorage = {\n setItem: <T = unknown>(key: string, value: T, ttlInMs: number) => {\n const now = Date.now();\n const item: LocalStorageItemWithExpiry<T> = { value, expiresAt: now + ttlInMs };\n try {\n localStorage.setItem(key, JSON.stringify(item));\n } catch (error) {\n console.error('Failed to set item with expiry:', error);\n }\n },\n getItem: <T = unknown>(key: string): T | null => {\n const itemStr = localStorage.getItem(key);\n if (!itemStr) return null;\n\n try {\n const item: LocalStorageItemWithExpiry<T> = JSON.parse(itemStr);\n const now = Date.now();\n if (now > item.expiresAt) {\n localStorage.removeItem(key);\n return null;\n }\n return item.value;\n } catch {\n return null;\n }\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,IAAM,kBAAkB;AAAA,EAC7B,SAAS,CAAc,KAAa,OAAU,YAAoB;AAChE,UAAM,MAAM,KAAK,IAAI;AACrB,UAAM,OAAsC,EAAE,OAAO,WAAW,MAAM,QAAQ;AAC9E,QAAI;AACF,mBAAa,QAAQ,KAAK,KAAK,UAAU,IAAI,CAAC;AAAA,IAChD,SAAS,OAAO;AACd,cAAQ,MAAM,mCAAmC,KAAK;AAAA,IACxD;AAAA,EACF;AAAA,EACA,SAAS,CAAc,QAA0B;AAC/C,UAAM,UAAU,aAAa,QAAQ,GAAG;AACxC,QAAI,CAAC,QAAS,QAAO;AAErB,QAAI;AACF,YAAM,OAAsC,KAAK,MAAM,OAAO;AAC9D,YAAM,MAAM,KAAK,IAAI;AACrB,UAAI,MAAM,KAAK,WAAW;AACxB,qBAAa,WAAW,GAAG;AAC3B,eAAO;AAAA,MACT;AACA,aAAO,KAAK;AAAA,IACd,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
|
package/dist/utils/storage.d.cts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
type LocalStorageItemWithExpiry<T = unknown> = {
|
|
2
|
-
value: T;
|
|
3
|
-
expiresAt: number;
|
|
4
|
-
};
|
|
5
|
-
declare const expiringStorage: {
|
|
6
|
-
setItem: <T = unknown>(key: string, value: T, ttlInMs: number) => void;
|
|
7
|
-
getItem: <T = unknown>(key: string) => T | null;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export { type LocalStorageItemWithExpiry, expiringStorage };
|
package/dist/utils/storage.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
type LocalStorageItemWithExpiry<T = unknown> = {
|
|
2
|
-
value: T;
|
|
3
|
-
expiresAt: number;
|
|
4
|
-
};
|
|
5
|
-
declare const expiringStorage: {
|
|
6
|
-
setItem: <T = unknown>(key: string, value: T, ttlInMs: number) => void;
|
|
7
|
-
getItem: <T = unknown>(key: string) => T | null;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export { type LocalStorageItemWithExpiry, expiringStorage };
|
package/dist/utils/storage.mjs
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// src/utils/storage.ts
|
|
2
|
-
var expiringStorage = {
|
|
3
|
-
setItem: (key, value, ttlInMs) => {
|
|
4
|
-
const now = Date.now();
|
|
5
|
-
const item = { value, expiresAt: now + ttlInMs };
|
|
6
|
-
try {
|
|
7
|
-
localStorage.setItem(key, JSON.stringify(item));
|
|
8
|
-
} catch (error) {
|
|
9
|
-
console.error("Failed to set item with expiry:", error);
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
getItem: (key) => {
|
|
13
|
-
const itemStr = localStorage.getItem(key);
|
|
14
|
-
if (!itemStr) return null;
|
|
15
|
-
try {
|
|
16
|
-
const item = JSON.parse(itemStr);
|
|
17
|
-
const now = Date.now();
|
|
18
|
-
if (now > item.expiresAt) {
|
|
19
|
-
localStorage.removeItem(key);
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
return item.value;
|
|
23
|
-
} catch {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
export {
|
|
29
|
-
expiringStorage
|
|
30
|
-
};
|
|
31
|
-
//# sourceMappingURL=storage.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/storage.ts"],"sourcesContent":["export type LocalStorageItemWithExpiry<T = unknown> = { value: T; expiresAt: number };\n\nexport const expiringStorage = {\n setItem: <T = unknown>(key: string, value: T, ttlInMs: number) => {\n const now = Date.now();\n const item: LocalStorageItemWithExpiry<T> = { value, expiresAt: now + ttlInMs };\n try {\n localStorage.setItem(key, JSON.stringify(item));\n } catch (error) {\n console.error('Failed to set item with expiry:', error);\n }\n },\n getItem: <T = unknown>(key: string): T | null => {\n const itemStr = localStorage.getItem(key);\n if (!itemStr) return null;\n\n try {\n const item: LocalStorageItemWithExpiry<T> = JSON.parse(itemStr);\n const now = Date.now();\n if (now > item.expiresAt) {\n localStorage.removeItem(key);\n return null;\n }\n return item.value;\n } catch {\n return null;\n }\n },\n};\n"],"mappings":";AAEO,IAAM,kBAAkB;AAAA,EAC7B,SAAS,CAAc,KAAa,OAAU,YAAoB;AAChE,UAAM,MAAM,KAAK,IAAI;AACrB,UAAM,OAAsC,EAAE,OAAO,WAAW,MAAM,QAAQ;AAC9E,QAAI;AACF,mBAAa,QAAQ,KAAK,KAAK,UAAU,IAAI,CAAC;AAAA,IAChD,SAAS,OAAO;AACd,cAAQ,MAAM,mCAAmC,KAAK;AAAA,IACxD;AAAA,EACF;AAAA,EACA,SAAS,CAAc,QAA0B;AAC/C,UAAM,UAAU,aAAa,QAAQ,GAAG;AACxC,QAAI,CAAC,QAAS,QAAO;AAErB,QAAI;AACF,YAAM,OAAsC,KAAK,MAAM,OAAO;AAC9D,YAAM,MAAM,KAAK,IAAI;AACrB,UAAI,MAAM,KAAK,WAAW;AACxB,qBAAa,WAAW,GAAG;AAC3B,eAAO;AAAA,MACT;AACA,aAAO,KAAK;AAAA,IACd,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AACF;","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);\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":[]}
|
|
@@ -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 };
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
// src/utils/token-bucket.ts
|
|
2
|
-
var INTERVAL_MAP = {
|
|
3
|
-
second: 1e3,
|
|
4
|
-
minute: 60 * 1e3,
|
|
5
|
-
hour: 60 * 60 * 1e3,
|
|
6
|
-
day: 24 * 60 * 60 * 1e3
|
|
7
|
-
};
|
|
8
|
-
var TokenBucket = class {
|
|
9
|
-
rate;
|
|
10
|
-
capacity;
|
|
11
|
-
requested;
|
|
12
|
-
timer;
|
|
13
|
-
tokens;
|
|
14
|
-
constructor({ rate, capacity, requested, interval = "second" }) {
|
|
15
|
-
if (rate <= 0) throw new Error("rate must be greater than 0");
|
|
16
|
-
if (capacity <= 0) throw new Error("capacity must be greater than 0");
|
|
17
|
-
if (requested <= 0) throw new Error("requested must be greater than 0");
|
|
18
|
-
if (requested > capacity) throw new Error("requested must be less than or equal to capacity");
|
|
19
|
-
this.rate = rate;
|
|
20
|
-
this.capacity = capacity;
|
|
21
|
-
this.requested = requested;
|
|
22
|
-
this.tokens = capacity;
|
|
23
|
-
this.timer = setInterval(() => {
|
|
24
|
-
if (this.tokens < this.capacity) {
|
|
25
|
-
const tokens = this.tokens + this.rate;
|
|
26
|
-
this.tokens = Math.min(tokens, this.capacity);
|
|
27
|
-
}
|
|
28
|
-
}, INTERVAL_MAP[interval]);
|
|
29
|
-
}
|
|
30
|
-
wait(ms) {
|
|
31
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
32
|
-
}
|
|
33
|
-
async removeTokens() {
|
|
34
|
-
while (this.tokens < this.requested) {
|
|
35
|
-
const ms = Math.ceil(1e3 * (this.requested - this.tokens) / this.rate);
|
|
36
|
-
await this.wait(ms);
|
|
37
|
-
}
|
|
38
|
-
this.tokens -= this.requested;
|
|
39
|
-
return this.tokens;
|
|
40
|
-
}
|
|
41
|
-
destroy() {
|
|
42
|
-
clearInterval(this.timer);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
export {
|
|
46
|
-
TokenBucket
|
|
47
|
-
};
|
|
48
|
-
//# sourceMappingURL=token-bucket.mjs.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":";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,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