@shware/http 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/error/parse.cjs +2 -2
- package/dist/error/parse.cjs.map +1 -1
- package/dist/error/parse.mjs +1 -1
- package/dist/error/parse.mjs.map +1 -1
- package/dist/hono/validator.d.cts +1 -1
- package/dist/hono/validator.d.ts +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 +3 -3
- package/dist/response.cjs.map +1 -1
- package/dist/response.mjs +1 -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
|
@@ -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,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":[]}
|