@gera2ld/common 0.0.1 → 0.0.3

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.
Files changed (2) hide show
  1. package/dist/index.js +487 -542
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -1,546 +1,491 @@
1
- import { BigNumber as g } from "bignumber.js";
2
- import { delay as b } from "es-toolkit";
3
- const y = "=", w = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4
- let S, x, T;
5
- function P() {
6
- return S ||= B(w), S;
7
- }
8
- function k() {
9
- return x ||= w.slice(0, -2) + "-_", x;
10
- }
11
- function q() {
12
- return T ||= B(k()), T;
13
- }
14
- function B(n) {
15
- const e = new Array(255);
16
- for (let t = 0; t < n.length; t += 1)
17
- e[n.charCodeAt(t)] = t;
18
- return e;
19
- }
20
- function N(n, e) {
21
- const t = e[n];
22
- if (t == null) throw new Error("Unable to parse base64 string.");
23
- return t;
24
- }
25
- function E(n, e, t) {
26
- let r = "", i = 0;
27
- for (; i < n.length; ) {
28
- const s = n[i++], o = n[i++], a = n[i++];
29
- if (r += e[s >> 2], r += e[(s & 3) << 4 | (o || 0) >> 4], o == null ? t && (r += y) : r += e[(o & 15) << 2 | (a || 0) >> 6], a == null) {
30
- t && (r += y);
31
- break;
32
- }
33
- r += e[a & 63];
34
- }
35
- return r;
36
- }
37
- function C(n, e) {
38
- let t = n.length;
39
- for (; t > 0 && n[t - 1] === "="; ) t -= 1;
40
- const r = Math.ceil(t / 4), i = new Uint8Array(3 * r);
41
- let s = 0, o = 0, a = 0;
42
- for (o = 0; o < r; o += 1) {
43
- let f = 0;
44
- for (a = 0; a < 4; a += 1) {
45
- s = o * 4 + a;
46
- const c = n[s];
47
- if (!c || c === y) break;
48
- f |= N(n.charCodeAt(s), e) << (3 - a) * 6;
49
- }
50
- i[o * 3] = f >> 16, i[o * 3 + 1] = f >> 8 & 255, i[o * 3 + 2] = f & 255;
51
- }
52
- if (s < t - 1 || a === 1) throw new Error("Invalid base64 string");
53
- let u = 3 * r;
54
- for (; a < 4; a += 1) u -= 1;
55
- return i.subarray(0, u);
56
- }
57
- function H(n) {
58
- return E(n, w, !0);
59
- }
60
- function _(n) {
61
- return C(n, P());
62
- }
63
- function K(n) {
64
- return E(n, k(), !1);
65
- }
66
- function V(n) {
67
- return C(n, q());
68
- }
69
- function F(n) {
70
- return new TextEncoder().encode(n);
71
- }
72
- function Y(n) {
73
- return new TextDecoder().decode(n);
74
- }
75
- function W(n, e) {
76
- return typeof e == "bigint" ? `${e}n` : e;
77
- }
78
- const R = {
79
- truncateThreshold: 300,
80
- headLength: 100,
81
- tailLength: 100
1
+ import { BigNumber as e } from "bignumber.js";
2
+ import { delay as t } from "es-toolkit";
3
+ //#region src/base64.ts
4
+ var n = "=", r = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", i, a, o;
5
+ function s() {
6
+ return i ||= u(r), i;
7
+ }
8
+ function c() {
9
+ return a ||= r.slice(0, -2) + "-_", a;
10
+ }
11
+ function l() {
12
+ return o ||= u(c()), o;
13
+ }
14
+ function u(e) {
15
+ let t = Array(255);
16
+ for (let n = 0; n < e.length; n += 1) t[e.charCodeAt(n)] = n;
17
+ return t;
18
+ }
19
+ function d(e, t) {
20
+ let n = t[e];
21
+ if (n == null) throw Error("Unable to parse base64 string.");
22
+ return n;
23
+ }
24
+ function f(e, t, r) {
25
+ let i = "", a = 0;
26
+ for (; a < e.length;) {
27
+ let o = e[a++], s = e[a++], c = e[a++];
28
+ if (i += t[o >> 2], i += t[(o & 3) << 4 | (s || 0) >> 4], s == null ? r && (i += n) : i += t[(s & 15) << 2 | (c || 0) >> 6], c == null) {
29
+ r && (i += n);
30
+ break;
31
+ }
32
+ i += t[c & 63];
33
+ }
34
+ return i;
35
+ }
36
+ function p(e, t) {
37
+ let r = e.length;
38
+ for (; r > 0 && e[r - 1] === "=";) --r;
39
+ let i = Math.ceil(r / 4), a = new Uint8Array(3 * i), o = 0, s = 0, c = 0;
40
+ for (s = 0; s < i; s += 1) {
41
+ let r = 0;
42
+ for (c = 0; c < 4; c += 1) {
43
+ o = s * 4 + c;
44
+ let i = e[o];
45
+ if (!i || i === n) break;
46
+ r |= d(e.charCodeAt(o), t) << (3 - c) * 6;
47
+ }
48
+ a[s * 3] = r >> 16, a[s * 3 + 1] = r >> 8 & 255, a[s * 3 + 2] = r & 255;
49
+ }
50
+ if (o < r - 1 || c === 1) throw Error("Invalid base64 string");
51
+ let l = 3 * i;
52
+ for (; c < 4; c += 1) --l;
53
+ return a.subarray(0, l);
54
+ }
55
+ function m(e) {
56
+ return f(e, r, !0);
57
+ }
58
+ function h(e) {
59
+ return p(e, s());
60
+ }
61
+ function g(e) {
62
+ return f(e, c(), !1);
63
+ }
64
+ function _(e) {
65
+ return p(e, l());
66
+ }
67
+ function v(e) {
68
+ return new TextEncoder().encode(e);
69
+ }
70
+ function y(e) {
71
+ return new TextDecoder().decode(e);
72
+ }
73
+ //#endregion
74
+ //#region src/util.ts
75
+ function b(e, t) {
76
+ return typeof t == "bigint" ? `${t}n` : t;
77
+ }
78
+ var x = {
79
+ truncateThreshold: 300,
80
+ headLength: 100,
81
+ tailLength: 100
82
82
  };
83
- function D(n, e) {
84
- const { truncateThreshold: t, headLength: r, tailLength: i } = {
85
- ...R,
86
- ...e
87
- }, s = Array.from(n);
88
- if (s.length > t) {
89
- const o = Math.min(r, s.length), a = Math.min(i, s.length - o), u = s.slice(0, o).join(""), f = s.slice(-a).join("");
90
- return s.length - o - a > 0 ? u + "..." + f : n;
91
- }
92
- return n;
93
- }
94
- function v(n, e) {
95
- return JSON.stringify(n, (t, r) => (typeof r == "bigint" && (r = `${r}n`), typeof r == "string" ? D(r, e) : r));
96
- }
97
- function $(n, ...e) {
98
- let t = n.replace(/%./g, (r) => {
99
- const i = r[1], s = i.toUpperCase() === i ? { truncateThreshold: 1 / 0 } : void 0;
100
- if ("df".includes(i)) return String(Number(e.shift()));
101
- if ("jJ".includes(i)) return v(e.shift(), s);
102
- if ("sSoO".includes(i)) {
103
- const o = e.shift();
104
- return o instanceof Error ? `${o}
105
- ${o.stack}` : o && typeof o == "object" ? v(o, s) : D(String(o), s);
106
- }
107
- return r;
108
- });
109
- return e.length && (t += " " + e.join(" ")), t;
110
- }
111
- function m() {
112
- const n = {
113
- status: "pending"
114
- };
115
- return n.promise = new Promise((e, t) => {
116
- n.resolve = e, n.reject = t;
117
- }), n.promise.then(
118
- () => {
119
- n.status = "resolved";
120
- },
121
- () => {
122
- n.status = "rejected";
123
- }
124
- ), n;
125
- }
126
- function I(n, e) {
127
- return function(...r) {
128
- return e.call(this, n, ...r);
129
- };
130
- }
131
- function X(n) {
132
- return Array.from(n).map((e) => e.toString(16).padStart(2, "0")).join("");
133
- }
134
- function ee(n) {
135
- return new Promise((e, t) => {
136
- const r = document.createElement("script");
137
- r.src = n, r.onload = e, r.onerror = t, document.head.append(r), r.remove();
138
- });
139
- }
140
- const M = new Uint8Array([13, 10]);
141
- class te {
142
- buffer = new Uint8Array(0);
143
- requests = [];
144
- isClosed = !1;
145
- read(e = 8192) {
146
- const t = m();
147
- return this.requests.push({
148
- deferred: t,
149
- handle: () => {
150
- const r = Math.min(e, this.buffer.length), i = this.buffer.subarray(0, r);
151
- return this.buffer = this.buffer.subarray(r), t.resolve(i), !0;
152
- }
153
- }), this.processBuffer(), t.promise;
154
- }
155
- readExact(e) {
156
- const t = m();
157
- return this.requests.push({
158
- deferred: t,
159
- handle: () => {
160
- if (e > this.buffer.length) return !1;
161
- const r = this.buffer.subarray(0, e);
162
- return this.buffer = this.buffer.subarray(e), t.resolve(r), !0;
163
- }
164
- }), this.processBuffer(), t.promise;
165
- }
166
- readUntil(e = M) {
167
- typeof e == "string" && (e = F(e));
168
- const t = m();
169
- return this.requests.push({
170
- deferred: t,
171
- handle: () => {
172
- const r = z(this.buffer, e);
173
- if (r < 0) return !1;
174
- const i = r + e.length, s = this.buffer.subarray(0, i);
175
- return this.buffer = this.buffer.subarray(i), t.resolve(s), !0;
176
- }
177
- }), this.processBuffer(), t.promise;
178
- }
179
- feed(e) {
180
- if (this.isClosed) throw new Error("Buffer is closed");
181
- this.buffer = O(this.buffer, e), this.processBuffer();
182
- }
183
- close() {
184
- this.isClosed = !0, this.processBuffer();
185
- }
186
- processBuffer() {
187
- for (; this.requests.length; ) {
188
- const e = this.requests[0];
189
- if (!e.handle())
190
- if (this.isClosed)
191
- e.deferred.reject(new Error("Buffer is closed"));
192
- else break;
193
- this.requests.shift();
194
- }
195
- }
196
- }
197
- function O(n, e) {
198
- const t = new Uint8Array(n.length + e.length);
199
- return t.set(n, 0), t.set(e, n.length), t;
200
- }
201
- function z(n, e) {
202
- if (!e.length) return 0;
203
- for (let t = 0; t <= n.length - e.length; t += 1) {
204
- let r;
205
- for (r = 0; r < e.length && n[t + r] === e[r]; r += 1)
206
- ;
207
- if (r === e.length) return t;
208
- }
209
- return -1;
210
- }
211
- function re(n, e) {
212
- const t = [], r = /* @__PURE__ */ new Set();
213
- async function i() {
214
- const u = m();
215
- return t.push(u), o(), await u.promise, u;
216
- }
217
- function s(u) {
218
- r.delete(u), o();
219
- }
220
- function o() {
221
- for (; t.length && r.size < e; ) {
222
- const u = t.shift();
223
- r.add(u), u.resolve();
224
- }
225
- }
226
- async function a(...u) {
227
- const f = await i();
228
- try {
229
- return await n(...u);
230
- } finally {
231
- s(f);
232
- }
233
- }
234
- return a;
235
- }
236
- class ne {
237
- constructor(e = 0) {
238
- this.maxSize = e;
239
- }
240
- data = [];
241
- getQueue = /* @__PURE__ */ new Set();
242
- putQueue = /* @__PURE__ */ new Set();
243
- get size() {
244
- return this.data.length;
245
- }
246
- defer(e, t = 0) {
247
- const r = m();
248
- return e.add(r), r.promise.finally(() => {
249
- e.delete(r);
250
- }), t && setTimeout(r.reject, t), r.promise;
251
- }
252
- resolve(e) {
253
- const t = e.values().next().value;
254
- t && (t.resolve(), e.delete(t));
255
- }
256
- async get(e = 0) {
257
- this.data.length || await this.defer(this.getQueue, e);
258
- const t = this.data.shift();
259
- return this.resolve(this.putQueue), t;
260
- }
261
- async put(e, t = 0) {
262
- this.maxSize && this.data.length >= this.maxSize && await this.defer(this.putQueue, t), this.data.push(e), this.resolve(this.getQueue);
263
- }
264
- }
265
- class U {
266
- static defaultOptions = {};
267
- output;
268
- options;
269
- constructor(e) {
270
- this.options = {
271
- ...U.defaultOptions,
272
- ...typeof e == "string" ? { prefix: e } : e
273
- };
274
- }
275
- getPrefix() {
276
- let { prefix: e } = this.options;
277
- return typeof e == "function" && (e = e()), e || "";
278
- }
279
- collect() {
280
- return this.output = [], () => {
281
- const { output: e } = this;
282
- if (this.output = void 0, !e) throw new Error("Logs already collected");
283
- return e;
284
- };
285
- }
286
- log(e, t, ...r) {
287
- const i = $(t, ...r);
288
- this.output && this.output.push(i), console[e](this.getPrefix(), i);
289
- }
290
- info(e, ...t) {
291
- return this.log("info", e, ...t);
292
- }
293
- error(e, ...t) {
294
- return this.log("error", e, ...t);
295
- }
296
- wrap(e, t) {
297
- const r = this;
298
- return I(e, function(s, ...o) {
299
- return t.call(this, r, s, ...o);
300
- });
301
- }
302
- }
303
- function ie(n, e) {
304
- return BigInt(
305
- g(n).multipliedBy(g(10).exponentiatedBy(e)).toFixed(0)
306
- );
307
- }
308
- function se(n, e) {
309
- return g(n).dividedBy(g(10).exponentiatedBy(e)).toNumber();
310
- }
311
- const Q = {
312
- maximumFractionDigits: 4,
313
- exponentialThresholdLarge: 1e6,
314
- exponentialThresholdSmall: 1e-6,
315
- minimumSignificantDigits: 2,
316
- maximumSignificantDigits: 8,
317
- quantizationStep: 0,
318
- keepTrailingZeros: !1
83
+ function S(e, t) {
84
+ let { truncateThreshold: n, headLength: r, tailLength: i } = {
85
+ ...x,
86
+ ...t
87
+ }, a = Array.from(e);
88
+ if (a.length > n) {
89
+ let t = Math.min(r, a.length), n = Math.min(i, a.length - t), o = a.slice(0, t).join(""), s = a.slice(-n).join("");
90
+ return a.length - t - n > 0 ? o + "..." + s : e;
91
+ }
92
+ return e;
93
+ }
94
+ function C(e, t) {
95
+ return JSON.stringify(e, (e, n) => (typeof n == "bigint" && (n = `${n}n`), typeof n == "string" ? S(n, t) : n));
96
+ }
97
+ function w(e, ...t) {
98
+ let n = e.replace(/%./g, (e) => {
99
+ let n = e[1], r = n.toUpperCase() === n ? { truncateThreshold: Infinity } : void 0;
100
+ if ("df".includes(n)) return String(Number(t.shift()));
101
+ if ("jJ".includes(n)) return C(t.shift(), r);
102
+ if ("sSoO".includes(n)) {
103
+ let e = t.shift();
104
+ return e instanceof Error ? `${e}\n${e.stack}` : e && typeof e == "object" ? C(e, r) : S(String(e), r);
105
+ }
106
+ return e;
107
+ });
108
+ return t.length && (n += " " + t.join(" ")), n;
109
+ }
110
+ function T() {
111
+ let e = { status: "pending" };
112
+ return e.promise = new Promise((t, n) => {
113
+ e.resolve = t, e.reject = n;
114
+ }), e.promise.then(() => {
115
+ e.status = "resolved";
116
+ }, () => {
117
+ e.status = "rejected";
118
+ }), e;
119
+ }
120
+ function E(e, t) {
121
+ return function(...n) {
122
+ return t.call(this, e, ...n);
123
+ };
124
+ }
125
+ function D(e) {
126
+ return Array.from(e).map((e) => e.toString(16).padStart(2, "0")).join("");
127
+ }
128
+ function O(e) {
129
+ return new Promise((t, n) => {
130
+ let r = document.createElement("script");
131
+ r.src = e, r.onload = t, r.onerror = n, document.head.append(r), r.remove();
132
+ });
133
+ }
134
+ //#endregion
135
+ //#region src/buffer.ts
136
+ var k = new Uint8Array([13, 10]), A = class {
137
+ buffer = new Uint8Array();
138
+ requests = [];
139
+ isClosed = !1;
140
+ read(e = 8192) {
141
+ let t = T();
142
+ return this.requests.push({
143
+ deferred: t,
144
+ handle: () => {
145
+ let n = Math.min(e, this.buffer.length), r = this.buffer.subarray(0, n);
146
+ return this.buffer = this.buffer.subarray(n), t.resolve(r), !0;
147
+ }
148
+ }), this.processBuffer(), t.promise;
149
+ }
150
+ readExact(e) {
151
+ let t = T();
152
+ return this.requests.push({
153
+ deferred: t,
154
+ handle: () => {
155
+ if (e > this.buffer.length) return !1;
156
+ let n = this.buffer.subarray(0, e);
157
+ return this.buffer = this.buffer.subarray(e), t.resolve(n), !0;
158
+ }
159
+ }), this.processBuffer(), t.promise;
160
+ }
161
+ readUntil(e = k) {
162
+ typeof e == "string" && (e = v(e));
163
+ let t = T();
164
+ return this.requests.push({
165
+ deferred: t,
166
+ handle: () => {
167
+ let n = M(this.buffer, e);
168
+ if (n < 0) return !1;
169
+ let r = n + e.length, i = this.buffer.subarray(0, r);
170
+ return this.buffer = this.buffer.subarray(r), t.resolve(i), !0;
171
+ }
172
+ }), this.processBuffer(), t.promise;
173
+ }
174
+ feed(e) {
175
+ if (this.isClosed) throw Error("Buffer is closed");
176
+ this.buffer = j(this.buffer, e), this.processBuffer();
177
+ }
178
+ close() {
179
+ this.isClosed = !0, this.processBuffer();
180
+ }
181
+ processBuffer() {
182
+ for (; this.requests.length;) {
183
+ let e = this.requests[0];
184
+ if (!e.handle()) if (this.isClosed) e.deferred.reject(/* @__PURE__ */ Error("Buffer is closed"));
185
+ else break;
186
+ this.requests.shift();
187
+ }
188
+ }
319
189
  };
320
- function j(n, e) {
321
- if (n == null || n === "")
322
- return "";
323
- let t = +n;
324
- if (Number.isNaN(t))
325
- return "";
326
- const r = {
327
- ...Q,
328
- ...e
329
- };
330
- r.quantizationStep > 0 && (t = Math.round(t / r.quantizationStep) * r.quantizationStep);
331
- const i = Math.abs(t), s = r.maximumSignificantDigits;
332
- if (t !== 0 && (i >= r.exponentialThresholdLarge || i < r.exponentialThresholdSmall)) {
333
- let c;
334
- return r.keepTrailingZeros ? c = t.toExponential(Math.max(0, s - 1)) : (c = t.toPrecision(s), c = (+c).toExponential()), c;
335
- }
336
- const o = {
337
- useGrouping: !1,
338
- notation: "standard"
339
- };
340
- e?.maximumFractionDigits !== void 0 ? o.maximumFractionDigits = r.maximumFractionDigits : (o.minimumSignificantDigits = r.minimumSignificantDigits, o.maximumSignificantDigits = s);
341
- let f = new Intl.NumberFormat("en-US", o).format(t);
342
- return !r.keepTrailingZeros && f.includes(".") && (f = f.replace(/\.?0+$/, "")), f;
343
- }
344
- function oe(n, e) {
345
- return j(n * 100, e) + "%";
346
- }
347
- const L = {
348
- interval: 2e3,
349
- maxRetry: 6,
350
- maxInterval: 0
190
+ function j(e, t) {
191
+ let n = new Uint8Array(e.length + t.length);
192
+ return n.set(e, 0), n.set(t, e.length), n;
193
+ }
194
+ function M(e, t) {
195
+ if (!t.length) return 0;
196
+ for (let n = 0; n <= e.length - t.length; n += 1) {
197
+ let r;
198
+ for (r = 0; r < t.length && e[n + r] === t[r]; r += 1);
199
+ if (r === t.length) return n;
200
+ }
201
+ return -1;
202
+ }
203
+ //#endregion
204
+ //#region src/concurrency.ts
205
+ function N(e, t) {
206
+ let n = [], r = /* @__PURE__ */ new Set();
207
+ async function i() {
208
+ let e = T();
209
+ return n.push(e), o(), await e.promise, e;
210
+ }
211
+ function a(e) {
212
+ r.delete(e), o();
213
+ }
214
+ function o() {
215
+ for (; n.length && r.size < t;) {
216
+ let e = n.shift();
217
+ r.add(e), e.resolve();
218
+ }
219
+ }
220
+ async function s(...t) {
221
+ let n = await i();
222
+ try {
223
+ return await e(...t);
224
+ } finally {
225
+ a(n);
226
+ }
227
+ }
228
+ return s;
229
+ }
230
+ var P = class {
231
+ data = [];
232
+ getQueue = /* @__PURE__ */ new Set();
233
+ putQueue = /* @__PURE__ */ new Set();
234
+ constructor(e = 0) {
235
+ this.maxSize = e;
236
+ }
237
+ get size() {
238
+ return this.data.length;
239
+ }
240
+ defer(e, t = 0) {
241
+ let n = T();
242
+ return e.add(n), n.promise.finally(() => {
243
+ e.delete(n);
244
+ }), t && setTimeout(n.reject, t), n.promise;
245
+ }
246
+ resolve(e) {
247
+ let t = e.values().next().value;
248
+ t && (t.resolve(), e.delete(t));
249
+ }
250
+ async get(e = 0) {
251
+ this.data.length || await this.defer(this.getQueue, e);
252
+ let t = this.data.shift();
253
+ return this.resolve(this.putQueue), t;
254
+ }
255
+ async put(e, t = 0) {
256
+ this.maxSize && this.data.length >= this.maxSize && await this.defer(this.putQueue, t), this.data.push(e), this.resolve(this.getQueue);
257
+ }
258
+ }, F = class e {
259
+ static defaultOptions = {};
260
+ output;
261
+ options;
262
+ constructor(t) {
263
+ this.options = {
264
+ ...e.defaultOptions,
265
+ ...typeof t == "string" ? { prefix: t } : t
266
+ };
267
+ }
268
+ getPrefix() {
269
+ let { prefix: e } = this.options;
270
+ return typeof e == "function" && (e = e()), e || "";
271
+ }
272
+ collect() {
273
+ return this.output = [], () => {
274
+ let { output: e } = this;
275
+ if (this.output = void 0, !e) throw Error("Logs already collected");
276
+ return e;
277
+ };
278
+ }
279
+ log(e, t, ...n) {
280
+ let r = w(t, ...n);
281
+ this.output && this.output.push(r), console[e](this.getPrefix(), r);
282
+ }
283
+ info(e, ...t) {
284
+ return this.log("info", e, ...t);
285
+ }
286
+ error(e, ...t) {
287
+ return this.log("error", e, ...t);
288
+ }
289
+ wrap(e, t) {
290
+ let n = this;
291
+ return E(e, function(e, ...r) {
292
+ return t.call(this, n, e, ...r);
293
+ });
294
+ }
351
295
  };
352
- function ae(n) {
353
- const e = {
354
- ...L,
355
- ...n
356
- };
357
- let { interval: t, maxInterval: r } = e;
358
- r && r < t && (r = t);
359
- let i = Promise.resolve();
360
- return function(o) {
361
- const a = i.then(async () => {
362
- let u = "Unknown error";
363
- for (let f = 0; f < e.maxRetry; f += 1)
364
- try {
365
- const c = await o();
366
- return t = Math.max(e.interval, t / 2), c;
367
- } catch (c) {
368
- if (u = c, e.isTooFrequent?.(c))
369
- t *= 2, r && (t = Math.min(r, t)), e.onScheduleRetry?.(t), await b(t);
370
- else
371
- throw c;
372
- }
373
- throw u;
374
- });
375
- return i = a.catch(() => {
376
- }).then(() => b(t)), a;
377
- };
378
- }
379
- async function ue(n, e) {
380
- const t = {
381
- ...L,
382
- ...e
383
- };
384
- let { interval: r, maxInterval: i } = t;
385
- i && i < r && (i = r);
386
- let s;
387
- for (let o = 0; o < t.maxRetry && (s = await n(), !s.ok); o += 1)
388
- s.interval ? r = s.interval : r *= 2, i && (r = Math.min(i, r)), await b(r);
389
- return s;
390
- }
391
- class p extends Error {
392
- constructor(e, t, r, i) {
393
- super(e, { cause: i }), this.request = t, this.response = r;
394
- }
395
- }
396
- function J(n) {
397
- const e = new URLSearchParams();
398
- return Array.isArray(n) || n instanceof URLSearchParams ? new URLSearchParams(n).forEach((t, r) => {
399
- e.append(r, t);
400
- }) : n && Object.entries(n).forEach(([t, r]) => {
401
- if (Array.isArray(r)) {
402
- const i = t.endsWith("[]") ? t : `${t}[]`;
403
- r.forEach((s) => {
404
- e.append(i, s);
405
- });
406
- } else
407
- e.append(t, r);
408
- }), e;
409
- }
410
- function fe(n, e) {
411
- const { json: t, timeout: r = 1e4, searchParams: i, ...s } = e || {}, o = new AbortController(), a = r ? setTimeout(() => o.abort(), r) : null, u = new Headers(s.headers);
412
- t != null && (s.body = JSON.stringify(t), u.set("content-type", "application/json"));
413
- const f = new URL(n);
414
- i && (f.search = J(i).toString());
415
- async function c(l) {
416
- let d;
417
- try {
418
- d = await fetch(f, {
419
- ...s,
420
- headers: u,
421
- signal: o.signal
422
- });
423
- const h = await l(d);
424
- if (!d.ok)
425
- throw new p(
426
- d.statusText,
427
- {
428
- url: f.toString(),
429
- method: s.method || "GET",
430
- options: e
431
- },
432
- d,
433
- h
434
- );
435
- return h;
436
- } catch (h) {
437
- throw h instanceof p ? h : new p(
438
- h?.message || `${h}`,
439
- {
440
- url: f.toString(),
441
- method: s.method || "GET",
442
- options: e
443
- },
444
- d,
445
- h
446
- );
447
- } finally {
448
- a && clearTimeout(a);
449
- }
450
- }
451
- return {
452
- async arrayBuffer() {
453
- return c((l) => l.arrayBuffer());
454
- },
455
- async blob() {
456
- return c((l) => l.blob());
457
- },
458
- async json() {
459
- return u.has("accept") || u.set("accept", "application/json"), c((l) => l.json());
460
- },
461
- async text() {
462
- return c((l) => l.text());
463
- },
464
- abort() {
465
- o.abort();
466
- }
467
- };
468
- }
469
- const A = [
470
- ["ms", 1],
471
- ["s", 1e3],
472
- ["m", 60],
473
- ["h", 60],
474
- ["d", 24],
475
- ["mo", 30],
476
- ["y", 365 / 30]
477
- ];
478
- function ce(n, e) {
479
- let r = `${n}`.match(/[a-zA-Z]+$/)?.[0] || "", i = r ? +`${n}`.slice(0, -r.length) : +n;
480
- if (r) {
481
- for (const [s, o] of e?.intervalLevels || A)
482
- if (i *= o, s === r) {
483
- r = "";
484
- break;
485
- }
486
- }
487
- if (r || isNaN(i))
488
- throw new Error(`Invalid interval: ${n}`);
489
- return i;
490
- }
491
- function le(n, e) {
492
- const t = Math.sign(n);
493
- n = Math.abs(n);
494
- let r = "ms";
495
- for (const [i, s] of e?.intervalLevels || A) {
496
- if (n < s) break;
497
- n /= s, r = i;
498
- }
499
- return `${t < 0 ? "-" : ""}${j(n, e) || ""}${r}`;
500
- }
501
- function he(n) {
502
- return [n.getFullYear(), n.getMonth() + 1, n.getDate()].map((e) => `${e}`.padStart(2, "0")).join("-");
503
- }
504
- function de(n) {
505
- return new Intl.DateTimeFormat("en-GB", {
506
- dateStyle: "short",
507
- timeStyle: "medium"
508
- }).format(new Date(n));
509
- }
510
- export {
511
- te as BufferReader,
512
- M as CrLf,
513
- U as Logger,
514
- ne as Queue,
515
- p as SimpleRequestError,
516
- _ as b64decode,
517
- H as b64encode,
518
- V as b64urlDecode,
519
- K as b64urlEncode,
520
- J as buildSearchParams,
521
- X as bytesToHex,
522
- O as concatBuffer,
523
- Y as decodeText,
524
- A as defaultIntervalLevels,
525
- m as defer,
526
- se as deflateNumber,
527
- F as encodeText,
528
- he as formatDate,
529
- le as formatInterval,
530
- j as formatNumber,
531
- oe as formatPercentage,
532
- $ as formatString,
533
- ie as inflateNumber,
534
- re as limitConcurrency,
535
- ee as loadJS,
536
- ce as normalizeInterval,
537
- ae as rateLimiter,
538
- W as replacerBigInt,
539
- de as reprDate,
540
- v as reprJson,
541
- z as searchBuffer,
542
- fe as simpleRequest,
543
- D as truncateString,
544
- ue as waitUntil,
545
- I as wrapFunction
296
+ //#endregion
297
+ //#region src/number.ts
298
+ function I(t, n) {
299
+ return BigInt(e(t).multipliedBy(e(10).exponentiatedBy(n)).toFixed(0));
300
+ }
301
+ function L(t, n) {
302
+ return e(t).dividedBy(e(10).exponentiatedBy(n)).toNumber();
303
+ }
304
+ var R = {
305
+ maximumFractionDigits: 4,
306
+ exponentialThresholdLarge: 1e6,
307
+ exponentialThresholdSmall: 1e-6,
308
+ minimumSignificantDigits: 2,
309
+ maximumSignificantDigits: 8,
310
+ quantizationStep: 0,
311
+ keepTrailingZeros: !1
312
+ };
313
+ function z(e, t) {
314
+ if (e == null || e === "") return "";
315
+ let n = +e;
316
+ if (Number.isNaN(n)) return "";
317
+ let r = {
318
+ ...R,
319
+ ...t
320
+ };
321
+ r.quantizationStep > 0 && (n = Math.round(n / r.quantizationStep) * r.quantizationStep);
322
+ let i = Math.abs(n), a = r.maximumSignificantDigits;
323
+ if (n !== 0 && (i >= r.exponentialThresholdLarge || i < r.exponentialThresholdSmall)) {
324
+ let e;
325
+ return r.keepTrailingZeros ? e = n.toExponential(Math.max(0, a - 1)) : (e = n.toPrecision(a), e = (+e).toExponential()), e;
326
+ }
327
+ let o = {
328
+ useGrouping: !1,
329
+ notation: "standard"
330
+ };
331
+ t?.maximumFractionDigits === void 0 ? (o.minimumSignificantDigits = r.minimumSignificantDigits, o.maximumSignificantDigits = a) : o.maximumFractionDigits = r.maximumFractionDigits;
332
+ let s = new Intl.NumberFormat("en-US", o).format(n);
333
+ return !r.keepTrailingZeros && s.includes(".") && (s = s.replace(/\.?0+$/, "")), s;
334
+ }
335
+ function B(e, t) {
336
+ return z(e * 100, t) + "%";
337
+ }
338
+ //#endregion
339
+ //#region src/rate-limiter.ts
340
+ var V = {
341
+ interval: 2e3,
342
+ maxRetry: 6,
343
+ maxInterval: 0
546
344
  };
345
+ function H(e) {
346
+ let n = {
347
+ ...V,
348
+ ...e
349
+ }, { interval: r, maxInterval: i } = n;
350
+ i && i < r && (i = r);
351
+ let a = Promise.resolve();
352
+ return function(e) {
353
+ let o = a.then(async () => {
354
+ let a = "Unknown error";
355
+ for (let o = 0; o < n.maxRetry; o += 1) try {
356
+ let t = await e();
357
+ return r = Math.max(n.interval, r / 2), t;
358
+ } catch (e) {
359
+ if (a = e, n.isTooFrequent?.(e)) r *= 2, i && (r = Math.min(i, r)), n.onScheduleRetry?.(r), await t(r);
360
+ else throw e;
361
+ }
362
+ throw a;
363
+ });
364
+ return a = o.catch(() => {}).then(() => t(r)), o;
365
+ };
366
+ }
367
+ async function U(e, n) {
368
+ let r = {
369
+ ...V,
370
+ ...n
371
+ }, { interval: i, maxInterval: a } = r;
372
+ a && a < i && (a = i);
373
+ let o;
374
+ for (let n = 0; n < r.maxRetry && (o = await e(), !o.ok); n += 1) o.interval ? i = o.interval : i *= 2, a && (i = Math.min(a, i)), await t(i);
375
+ return o;
376
+ }
377
+ //#endregion
378
+ //#region src/request.ts
379
+ var W = class extends Error {
380
+ constructor(e, t, n, r) {
381
+ super(e, { cause: r }), this.request = t, this.response = n;
382
+ }
383
+ };
384
+ function G(e) {
385
+ let t = new URLSearchParams();
386
+ return Array.isArray(e) || e instanceof URLSearchParams ? new URLSearchParams(e).forEach((e, n) => {
387
+ t.append(n, e);
388
+ }) : e && Object.entries(e).forEach(([e, n]) => {
389
+ if (Array.isArray(n)) {
390
+ let r = e.endsWith("[]") ? e : `${e}[]`;
391
+ n.forEach((e) => {
392
+ t.append(r, e);
393
+ });
394
+ } else t.append(e, n);
395
+ }), t;
396
+ }
397
+ function K(e, t) {
398
+ let { json: n, timeout: r = 1e4, searchParams: i, ...a } = t || {}, o = new AbortController(), s = r ? setTimeout(() => o.abort(), r) : null, c = new Headers(a.headers);
399
+ n != null && (a.body = JSON.stringify(n), c.set("content-type", "application/json"));
400
+ let l = new URL(e);
401
+ i && (l.search = G(i).toString());
402
+ async function u(e) {
403
+ let n;
404
+ try {
405
+ n = await fetch(l, {
406
+ ...a,
407
+ headers: c,
408
+ signal: o.signal
409
+ });
410
+ let r = await e(n);
411
+ if (!n.ok) throw new W(n.statusText, {
412
+ url: l.toString(),
413
+ method: a.method || "GET",
414
+ options: t
415
+ }, n, r);
416
+ return r;
417
+ } catch (e) {
418
+ throw e instanceof W ? e : new W(e?.message || `${e}`, {
419
+ url: l.toString(),
420
+ method: a.method || "GET",
421
+ options: t
422
+ }, n, e);
423
+ } finally {
424
+ s && clearTimeout(s);
425
+ }
426
+ }
427
+ return {
428
+ async arrayBuffer() {
429
+ return u((e) => e.arrayBuffer());
430
+ },
431
+ async blob() {
432
+ return u((e) => e.blob());
433
+ },
434
+ async json() {
435
+ return c.has("accept") || c.set("accept", "application/json"), u((e) => e.json());
436
+ },
437
+ async text() {
438
+ return u((e) => e.text());
439
+ },
440
+ abort() {
441
+ o.abort();
442
+ }
443
+ };
444
+ }
445
+ //#endregion
446
+ //#region src/time.ts
447
+ var q = [
448
+ ["ms", 1],
449
+ ["s", 1e3],
450
+ ["m", 60],
451
+ ["h", 60],
452
+ ["d", 24],
453
+ ["mo", 30],
454
+ ["y", 365 / 30]
455
+ ];
456
+ function J(e, t) {
457
+ let n = `${e}`.match(/[a-zA-Z]+$/)?.[0] || "", r = n ? +`${e}`.slice(0, -n.length) : +e;
458
+ if (n) {
459
+ for (let [e, i] of t?.intervalLevels || q) if (r *= i, e === n) {
460
+ n = "";
461
+ break;
462
+ }
463
+ }
464
+ if (n || isNaN(r)) throw Error(`Invalid interval: ${e}`);
465
+ return r;
466
+ }
467
+ function Y(e, t) {
468
+ let n = Math.sign(e);
469
+ e = Math.abs(e);
470
+ let r = "ms";
471
+ for (let [n, i] of t?.intervalLevels || q) {
472
+ if (e < i) break;
473
+ e /= i, r = n;
474
+ }
475
+ return `${n < 0 ? "-" : ""}${z(e, t) || ""}${r}`;
476
+ }
477
+ function X(e) {
478
+ return [
479
+ e.getFullYear(),
480
+ e.getMonth() + 1,
481
+ e.getDate()
482
+ ].map((e) => `${e}`.padStart(2, "0")).join("-");
483
+ }
484
+ function Z(e) {
485
+ return new Intl.DateTimeFormat("en-GB", {
486
+ dateStyle: "short",
487
+ timeStyle: "medium"
488
+ }).format(new Date(e));
489
+ }
490
+ //#endregion
491
+ export { A as BufferReader, k as CrLf, F as Logger, P as Queue, W as SimpleRequestError, h as b64decode, m as b64encode, _ as b64urlDecode, g as b64urlEncode, G as buildSearchParams, D as bytesToHex, j as concatBuffer, y as decodeText, q as defaultIntervalLevels, T as defer, L as deflateNumber, v as encodeText, X as formatDate, Y as formatInterval, z as formatNumber, B as formatPercentage, w as formatString, I as inflateNumber, N as limitConcurrency, O as loadJS, J as normalizeInterval, H as rateLimiter, b as replacerBigInt, Z as reprDate, C as reprJson, M as searchBuffer, K as simpleRequest, S as truncateString, U as waitUntil, E as wrapFunction };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gera2ld/common",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -16,8 +16,8 @@
16
16
  "registry": "https://registry.npmjs.org/"
17
17
  },
18
18
  "dependencies": {
19
- "bignumber.js": "^9.3.1",
20
- "es-toolkit": "^1.41.0"
19
+ "bignumber.js": "^11.0.0",
20
+ "es-toolkit": "^1.45.1"
21
21
  },
22
22
  "scripts": {
23
23
  "clean": "del-cli dist tsconfig.tsbuildinfo",