@based/utils 1.0.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.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +360 -0
  3. package/dist/src/base64.d.ts +2 -0
  4. package/dist/src/base64.js +74 -0
  5. package/dist/src/base64.js.map +1 -0
  6. package/dist/src/deepCopy.d.ts +4 -0
  7. package/dist/src/deepCopy.js +21 -0
  8. package/dist/src/deepCopy.js.map +1 -0
  9. package/dist/src/deepEqual.d.ts +2 -0
  10. package/dist/src/deepEqual.js +88 -0
  11. package/dist/src/deepEqual.js.map +1 -0
  12. package/dist/src/deepMerge.d.ts +2 -0
  13. package/dist/src/deepMerge.js +131 -0
  14. package/dist/src/deepMerge.js.map +1 -0
  15. package/dist/src/deflate.d.ts +1 -0
  16. package/dist/src/deflate.js +20 -0
  17. package/dist/src/deflate.js.map +1 -0
  18. package/dist/src/encoder/decode.d.ts +3 -0
  19. package/dist/src/encoder/decode.js +79 -0
  20. package/dist/src/encoder/decode.js.map +1 -0
  21. package/dist/src/encoder/encode.d.ts +3 -0
  22. package/dist/src/encoder/encode.js +103 -0
  23. package/dist/src/encoder/encode.js.map +1 -0
  24. package/dist/src/encoder/index.d.ts +10 -0
  25. package/dist/src/encoder/index.js +55 -0
  26. package/dist/src/encoder/index.js.map +1 -0
  27. package/dist/src/fastPrng.d.ts +1 -0
  28. package/dist/src/fastPrng.js +7 -0
  29. package/dist/src/fastPrng.js.map +1 -0
  30. package/dist/src/getType.d.ts +2 -0
  31. package/dist/src/getType.js +4 -0
  32. package/dist/src/getType.js.map +1 -0
  33. package/dist/src/index.d.ts +22 -0
  34. package/dist/src/index.js +23 -0
  35. package/dist/src/index.js.map +1 -0
  36. package/dist/src/isEmptyObject.d.ts +3 -0
  37. package/dist/src/isEmptyObject.js +7 -0
  38. package/dist/src/isEmptyObject.js.map +1 -0
  39. package/dist/src/isObject.d.ts +1 -0
  40. package/dist/src/isObject.js +4 -0
  41. package/dist/src/isObject.js.map +1 -0
  42. package/dist/src/isWinterTimeInEurope.d.ts +1 -0
  43. package/dist/src/isWinterTimeInEurope.js +25 -0
  44. package/dist/src/isWinterTimeInEurope.js.map +1 -0
  45. package/dist/src/longTimeOut.d.ts +1 -0
  46. package/dist/src/longTimeOut.js +20 -0
  47. package/dist/src/longTimeOut.js.map +1 -0
  48. package/dist/src/padding.d.ts +2 -0
  49. package/dist/src/padding.js +15 -0
  50. package/dist/src/padding.js.map +1 -0
  51. package/dist/src/path.d.ts +3 -0
  52. package/dist/src/path.js +41 -0
  53. package/dist/src/path.js.map +1 -0
  54. package/dist/src/query.d.ts +11 -0
  55. package/dist/src/query.js +142 -0
  56. package/dist/src/query.js.map +1 -0
  57. package/dist/src/queued.d.ts +113 -0
  58. package/dist/src/queued.js +126 -0
  59. package/dist/src/queued.js.map +1 -0
  60. package/dist/src/randomString.d.ts +12 -0
  61. package/dist/src/randomString.js +36 -0
  62. package/dist/src/randomString.js.map +1 -0
  63. package/dist/src/readStream.d.ts +5 -0
  64. package/dist/src/readStream.js +62 -0
  65. package/dist/src/readStream.js.map +1 -0
  66. package/dist/src/retry.d.ts +11 -0
  67. package/dist/src/retry.js +26 -0
  68. package/dist/src/retry.js.map +1 -0
  69. package/dist/src/timestamp.d.ts +1 -0
  70. package/dist/src/timestamp.js +71 -0
  71. package/dist/src/timestamp.js.map +1 -0
  72. package/dist/src/uint8.d.ts +31 -0
  73. package/dist/src/uint8.js +305 -0
  74. package/dist/src/uint8.js.map +1 -0
  75. package/dist/src/wait.d.ts +2 -0
  76. package/dist/src/wait.js +4 -0
  77. package/dist/src/wait.js.map +1 -0
  78. package/package.json +39 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"padding.js","sourceRoot":"","sources":["../../src/padding.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,GAAW,EAAE,GAAW,EAAE,IAAY,EAAU,EAAE;IACxE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAA;IACpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACjC,GAAG,GAAG,IAAI,GAAG,GAAG,CAAA;IAClB,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,GAAW,EAAE,IAAY,EAAU,EAAE;IACzE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAA;IACpB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACjC,GAAG,GAAG,GAAG,GAAG,IAAI,CAAA;IAClB,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export type Path = (number | string)[];
2
+ export declare function setByPath(target: any, path: Path, value: any): any;
3
+ export declare const getByPath: (target: any, path: Path) => any;
@@ -0,0 +1,41 @@
1
+ export function setByPath(target, path, value) {
2
+ if (typeof target !== 'object') {
3
+ return target;
4
+ }
5
+ let d = target;
6
+ for (let i = 0; i < path.length; i++) {
7
+ const seg = path[i];
8
+ if (i === path.length - 1) {
9
+ d[seg] = value;
10
+ break;
11
+ }
12
+ if (d[seg] === undefined) {
13
+ if (typeof path[i + 1] === 'number') {
14
+ d[seg] = [];
15
+ }
16
+ else {
17
+ d[seg] = {};
18
+ }
19
+ }
20
+ d = d[seg];
21
+ }
22
+ return target;
23
+ }
24
+ export const getByPath = (target, path) => {
25
+ if (typeof target !== 'object') {
26
+ return;
27
+ }
28
+ let d = target;
29
+ for (let i = 0; i < path.length; i++) {
30
+ const seg = path[i];
31
+ if (d?.[seg] !== undefined) {
32
+ d = d[seg];
33
+ }
34
+ else {
35
+ d = undefined;
36
+ break;
37
+ }
38
+ }
39
+ return d;
40
+ };
41
+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rC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;QACnB,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE,CAAC;YAC3B,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;QACZ,CAAC;aAAM,CAAC;YACN,CAAC,GAAG,SAAS,CAAA;YACb,MAAK;QACP,CAAC;IACH,CAAC;IACD,OAAO,CAAC,CAAA;AACV,CAAC,CAAA"}
@@ -0,0 +1,11 @@
1
+ type QueryValue = string | number | boolean | null;
2
+ type QueryParams = {
3
+ [key: string]: QueryValue | QueryValue[] | {
4
+ [key: string]: any;
5
+ };
6
+ };
7
+ export declare const parseQuery: (query: string) => QueryParams | void;
8
+ export declare const serializeQuery: (q: QueryValue | QueryValue[] | {
9
+ [key: string]: any;
10
+ }, deep?: boolean) => string;
11
+ export {};
@@ -0,0 +1,142 @@
1
+ const parseQueryValue = (q) => {
2
+ if (q.includes(',')) {
3
+ q = q.split(',');
4
+ for (let i = 0; i < q.length; i++) {
5
+ q[i] = parseQueryValue(q[i]);
6
+ }
7
+ return q;
8
+ }
9
+ if (q === 'null') {
10
+ return null;
11
+ }
12
+ else if (q === 'true' || q === '') {
13
+ return true;
14
+ }
15
+ else if (q === 'false') {
16
+ return false;
17
+ }
18
+ else if (!isNaN(q)) {
19
+ return Number(q);
20
+ }
21
+ return q;
22
+ };
23
+ export const parseQuery = (query) => {
24
+ if (query) {
25
+ try {
26
+ const r = {};
27
+ let inMem = '';
28
+ let inMemVal = '';
29
+ let parseVal = false;
30
+ let isJson = 0;
31
+ const len = query.length;
32
+ // len larger then 100 return?
33
+ for (let i = 0; i < len; i++) {
34
+ const q = query[i];
35
+ if (q === '&' &&
36
+ (!isJson ||
37
+ (isJson === 1 && query[i - 1] === '}') ||
38
+ (isJson === 2 && query[i - 1] === ']'))) {
39
+ if (!parseVal) {
40
+ r[inMem] = true;
41
+ }
42
+ else if (isJson) {
43
+ try {
44
+ r[inMem] = JSON.parse(inMemVal);
45
+ }
46
+ catch (err) {
47
+ r[inMem] = inMemVal;
48
+ }
49
+ }
50
+ else {
51
+ r[inMem] = parseQueryValue(inMemVal);
52
+ }
53
+ // is json
54
+ isJson = 0;
55
+ parseVal = false;
56
+ inMem = '';
57
+ inMemVal = '';
58
+ }
59
+ else if (q === '=' && !isJson) {
60
+ parseVal = true;
61
+ }
62
+ else {
63
+ if (parseVal) {
64
+ // if empty
65
+ if (inMemVal === '') {
66
+ if (q === '{') {
67
+ isJson = 1;
68
+ }
69
+ else if (q === '[') {
70
+ isJson = 2;
71
+ }
72
+ }
73
+ inMemVal += q;
74
+ }
75
+ else {
76
+ inMem += q;
77
+ }
78
+ }
79
+ }
80
+ if (inMem) {
81
+ if (isJson) {
82
+ try {
83
+ r[inMem] = JSON.parse(inMemVal);
84
+ }
85
+ catch (err) {
86
+ r[inMem] = inMemVal;
87
+ }
88
+ }
89
+ else {
90
+ r[inMem] = parseQueryValue(inMemVal);
91
+ }
92
+ }
93
+ return r;
94
+ }
95
+ catch (_e) { }
96
+ }
97
+ };
98
+ const topLevelObject = (q) => {
99
+ const args = [];
100
+ for (const key in q) {
101
+ const value = q[key];
102
+ if (value === true) {
103
+ args.push(`${key}`);
104
+ }
105
+ else {
106
+ args.push(`${key}=${serializeQuery(value, true)}`);
107
+ }
108
+ }
109
+ return args.join('&');
110
+ };
111
+ export const serializeQuery = (q, deep = false) => {
112
+ if (typeof q === 'string') {
113
+ return q;
114
+ }
115
+ if (typeof q === 'boolean') {
116
+ return !q ? 'false' : 'true';
117
+ }
118
+ if (typeof q === 'number') {
119
+ return String(q);
120
+ }
121
+ if (q === null) {
122
+ return 'null';
123
+ }
124
+ if (Array.isArray(q)) {
125
+ return q
126
+ .map((v) => {
127
+ if (typeof v === 'object' && v !== null) {
128
+ return JSON.stringify(v);
129
+ }
130
+ return serializeQuery(v, true);
131
+ })
132
+ .join(',');
133
+ }
134
+ if (typeof q === 'object') {
135
+ if (deep) {
136
+ return JSON.stringify(q);
137
+ }
138
+ return topLevelObject(q);
139
+ }
140
+ return '';
141
+ };
142
+ //# sourceMappingURL=query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/query.ts"],"names":[],"mappings":"AAMA,MAAM,eAAe,GAAG,CAAC,CAAM,EAA6B,EAAE;IAC5D,IAAI,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACpB,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAClC,CAAC,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAC9B,CAAC;QACD,OAAO,CAAC,CAAA;IACV,CAAC;IACD,IAAI,CAAC,KAAK,MAAM,EAAE,CAAC;QACjB,OAAO,IAAI,CAAA;IACb,CAAC;SAAM,IAAI,CAAC,KAAK,MAAM,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;QACpC,OAAO,IAAI,CAAA;IACb,CAAC;SAAM,IAAI,CAAC,KAAK,OAAO,EAAE,CAAC;QACzB,OAAO,KAAK,CAAA;IACd,CAAC;SAAM,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;QACrB,OAAO,MAAM,CAAC,CAAC,CAAC,CAAA;IAClB,CAAC;IACD,OAAO,CAAC,CAAA;AACV,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAa,EAAsB,EAAE;IAC9D,IAAI,KAAK,EAAE,CAAC;QACV,IAAI,CAAC;YACH,MAAM,CAAC,GAAgB,EAAE,CAAA;YACzB,IAAI,KAAK,GAAG,EAAE,CAAA;YACd,IAAI,QAAQ,GAAG,EAAE,CAAA;YACjB,IAAI,QAAQ,GAAG,KAAK,CAAA;YACpB,IA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0,0 +1,113 @@
1
+ type Retry = {
2
+ max?: number;
3
+ minTime?: number;
4
+ maxTime?: number;
5
+ logError?: (err: Error, args: any[], retries: number) => void;
6
+ shouldRetry?: (error: any) => boolean;
7
+ };
8
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a?: A, b?: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>, opts?: {
9
+ concurrency?: number;
10
+ dedup?: (...args: any[]) => number | string;
11
+ retry?: Retry;
12
+ }): (a?: A, b?: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>;
13
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b?: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>, opts?: {
14
+ concurrency?: number;
15
+ dedup?: (...args: any[]) => number | string;
16
+ retry?: Retry;
17
+ }): (a: A, b?: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>;
18
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>, opts?: {
19
+ concurrency?: number;
20
+ dedup?: (...args: any[]) => number | string;
21
+ retry?: Retry;
22
+ }): (a: A, b: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>;
23
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c: C, d?: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>, opts?: {
24
+ concurrency?: number;
25
+ dedup?: (...args: any[]) => number | string;
26
+ retry?: Retry;
27
+ }): (a: A, b: B, c: C, d?: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>;
28
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c: C, d: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>, opts?: {
29
+ concurrency?: number;
30
+ dedup?: (...args: any[]) => number | string;
31
+ retry?: Retry;
32
+ }): (a: A, b: B, c: C, d: D, e?: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>;
33
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>, opts?: {
34
+ concurrency?: number;
35
+ dedup?: (...args: any[]) => number | string;
36
+ retry?: Retry;
37
+ }): (a: A, b: B, c: C, d: D, e: E, f?: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>;
38
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>, opts?: {
39
+ concurrency?: number;
40
+ dedup?: (...args: any[]) => number | string;
41
+ retry?: Retry;
42
+ }): (a: A, b: B, c: C, d: D, e: E, f: F, g?: G, h?: H, i?: I, j?: J) => Promise<K>;
43
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h?: H, i?: I, j?: J) => Promise<K>, opts?: {
44
+ concurrency?: number;
45
+ dedup?: (...args: any[]) => number | string;
46
+ retry?: Retry;
47
+ }): (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h?: H, i?: I, j?: J) => Promise<K>;
48
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i?: I, j?: J) => Promise<K>, opts?: {
49
+ concurrency?: number;
50
+ dedup?: (...args: any[]) => number | string;
51
+ retry?: Retry;
52
+ }): (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i?: I, j?: J) => Promise<K>;
53
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j?: J) => Promise<K>, opts?: {
54
+ concurrency?: number;
55
+ dedup?: (...args: any[]) => number | string;
56
+ retry?: Retry;
57
+ }): (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j?: J) => Promise<K>;
58
+ declare function queued<K>(promiseFn: () => Promise<K>, opts?: {
59
+ concurrency?: number;
60
+ dedup?: (...args: any[]) => number | string;
61
+ retry?: Retry;
62
+ }): () => Promise<K>;
63
+ declare function queued<A, K>(promiseFn: (a: A) => Promise<K>, opts?: {
64
+ concurrency?: number;
65
+ dedup?: (...args: any[]) => number | string;
66
+ retry?: Retry;
67
+ }): (a: A) => Promise<K>;
68
+ declare function queued<A, B, K>(promiseFn: (a: A, b: B) => Promise<K>, opts?: {
69
+ concurrency?: number;
70
+ dedup?: (...args: any[]) => number | string;
71
+ retry?: Retry;
72
+ }): (a: A, b: B) => Promise<K>;
73
+ declare function queued<A, B, C, K>(promiseFn: (a: A, b: B, c: C) => Promise<K>, opts?: {
74
+ concurrency?: number;
75
+ dedup?: (...args: any[]) => number | string;
76
+ retry?: Retry;
77
+ }): (a: A, b: B, c: C) => Promise<K>;
78
+ declare function queued<A, B, C, D, K>(promiseFn: (a: A, b: B, c: C, d: D) => Promise<K>, opts?: {
79
+ concurrency?: number;
80
+ dedup?: (...args: any[]) => number | string;
81
+ retry?: Retry;
82
+ }): (a: A, b: B, c: C, d: D) => Promise<K>;
83
+ declare function queued<A, B, C, D, E, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E) => Promise<K>, opts?: {
84
+ concurrency?: number;
85
+ dedup?: (...args: any[]) => number | string;
86
+ retry?: Retry;
87
+ }): (a: A, b: B, c: C, d: D, e: E) => Promise<K>;
88
+ declare function queued<A, B, C, D, E, F, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F) => Promise<K>, opts?: {
89
+ concurrency?: number;
90
+ dedup?: (...args: any[]) => number | string;
91
+ retry?: Retry;
92
+ }): (a: A, b: B, c: C, d: D, e: E, f: F) => Promise<K>;
93
+ declare function queued<A, B, C, D, E, F, G, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => Promise<K>, opts?: {
94
+ concurrency?: number;
95
+ dedup?: (...args: any[]) => number | string;
96
+ retry?: Retry;
97
+ }): (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => Promise<K>;
98
+ declare function queued<A, B, C, D, E, F, G, H, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) => Promise<K>, opts?: {
99
+ concurrency?: number;
100
+ dedup?: (...args: any[]) => number | string;
101
+ retry?: Retry;
102
+ }): (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) => Promise<K>;
103
+ declare function queued<A, B, C, D, E, F, G, H, I, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) => Promise<K>, opts?: {
104
+ concurrency?: number;
105
+ dedup?: (...args: any[]) => number | string;
106
+ retry?: Retry;
107
+ }): (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) => Promise<K>;
108
+ declare function queued<A, B, C, D, E, F, G, H, I, J, K>(promiseFn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) => Promise<K>, opts?: {
109
+ concurrency?: number;
110
+ dedup?: (...args: any[]) => number | string;
111
+ retry?: Retry;
112
+ }): (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) => Promise<K>;
113
+ export default queued;
@@ -0,0 +1,126 @@
1
+ import { hash, hashObjectIgnoreKeyOrder } from '@based/hash';
2
+ const isInstanceOfClass = (obj) => {
3
+ if (obj === null || typeof obj !== 'object')
4
+ return false;
5
+ let proto = Object.getPrototypeOf(obj);
6
+ while (proto) {
7
+ if (proto.constructor && proto.constructor !== Object) {
8
+ return true;
9
+ }
10
+ proto = Object.getPrototypeOf(proto);
11
+ }
12
+ return false;
13
+ };
14
+ const randomId = () => (~~(Math.random() * 99999)).toString(16);
15
+ function retryPromiseFn(fn, retry) {
16
+ let retries = 0;
17
+ const retryIt = (...args) => new Promise((resolve, reject) => {
18
+ fn(...args)
19
+ .then((r) => resolve(r))
20
+ .catch((err) => {
21
+ retries++;
22
+ if (retry.logError) {
23
+ retry.logError(err, args, retries);
24
+ }
25
+ if (typeof retry?.shouldRetry === 'function' &&
26
+ !retry.shouldRetry(err)) {
27
+ return reject(err);
28
+ }
29
+ if (!retry.max || retries < retry.max) {
30
+ setTimeout(() => {
31
+ resolve(retryIt(...args));
32
+ }, Math.min(retries * (retry.minTime ?? 1e3), retry.maxTime ?? Infinity));
33
+ }
34
+ else {
35
+ reject(err);
36
+ }
37
+ });
38
+ });
39
+ return retryIt;
40
+ }
41
+ const defaultDedup = (...args) => {
42
+ let x = '';
43
+ for (const arg of args) {
44
+ if (arg !== undefined) {
45
+ if (typeof arg === 'object') {
46
+ if (isInstanceOfClass(arg)) {
47
+ x += randomId();
48
+ }
49
+ else {
50
+ x += hashObjectIgnoreKeyOrder(arg);
51
+ }
52
+ }
53
+ else {
54
+ x += hash(arg);
55
+ }
56
+ }
57
+ }
58
+ if (!x) {
59
+ // random id
60
+ return randomId();
61
+ }
62
+ return x;
63
+ };
64
+ function queued(promiseFn, opts = {}) {
65
+ if (opts.retry) {
66
+ promiseFn = retryPromiseFn(promiseFn, opts.retry);
67
+ }
68
+ // default options
69
+ if (!opts.dedup) {
70
+ opts.dedup = defaultDedup;
71
+ }
72
+ if (!opts.concurrency) {
73
+ opts.concurrency = 1;
74
+ }
75
+ const listeners = {};
76
+ const keysInProgress = new Set();
77
+ const drain = () => {
78
+ for (const key in listeners) {
79
+ if (keysInProgress.size === opts.concurrency) {
80
+ break;
81
+ }
82
+ if (!keysInProgress.has(key)) {
83
+ const l = listeners[key];
84
+ keysInProgress.add(key);
85
+ promiseFn(...l.args)
86
+ .then((v) => {
87
+ delete listeners[key];
88
+ keysInProgress.delete(key);
89
+ l.listeners.forEach(([resolve]) => {
90
+ resolve(v);
91
+ });
92
+ drain();
93
+ })
94
+ .catch((err) => {
95
+ delete listeners[key];
96
+ keysInProgress.delete(key);
97
+ l.listeners.forEach(([, reject]) => {
98
+ reject(err);
99
+ });
100
+ drain();
101
+ });
102
+ if (keysInProgress.size === opts.concurrency) {
103
+ break;
104
+ }
105
+ }
106
+ }
107
+ };
108
+ return (...args) => {
109
+ return new Promise((resolve, reject) => {
110
+ if ('dedup' in opts) {
111
+ const id = opts.dedup(...args);
112
+ if (!listeners[id]) {
113
+ listeners[id] = { args, listeners: [[resolve, reject]] };
114
+ }
115
+ else {
116
+ listeners[id].listeners.push([resolve, reject]);
117
+ }
118
+ }
119
+ if (keysInProgress.size < (opts.concurrency ?? 1)) {
120
+ drain();
121
+ }
122
+ });
123
+ };
124
+ }
125
+ export default queued;
126
+ //# sourceMappingURL=queued.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queued.js","sourceRoot":"","sources":["../../src/queued.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AAY5D,MAAM,iBAAiB,GAAG,CAAC,GAAQ,EAAE,EAAE;IACrC,IAAI,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAEzD,IAAI,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAAA;IACtC,OAAO,KAAK,EAAE,CAAC;QACb,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,WAAW,KAAK,MAAM,EAAE,CAAC;YACtD,OAAO,IAAI,CAAA;QACb,CAAC;QACD,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAA;IACtC,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,QAAQ,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE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K,CAAC,GAAG,IAAI,CAAC,CAAA;gBAC9B,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,CAAC;oBACnB,SAAS,CAAC,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,EAAE,CAAA;gBAC1D,CAAC;qBAAM,CAAC;oBACN,SAAS,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAA;gBACjD,CAAC;YACH,CAAC;YACD,IAAI,cAAc,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;gBAClD,KAAK,EAAE,CAAA;YACT,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC;AAED,eAAe,MAAM,CAAA"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Create string of random character of specified length
3
+ * @param length length of required string
4
+ * @param opts By default, all chars are allowed. It's possible to disable certain chars using these options.
5
+ * Options are `specials` `lowerCase` `upperCase` `numbers`
6
+ */
7
+ export default function randomString(length: number, opts?: {
8
+ noSpecials?: boolean;
9
+ noLowerCase?: boolean;
10
+ noUpperCase?: boolean;
11
+ noNumbers?: boolean;
12
+ }): string;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Create string of random character of specified length
3
+ * @param length length of required string
4
+ * @param opts By default, all chars are allowed. It's possible to disable certain chars using these options.
5
+ * Options are `specials` `lowerCase` `upperCase` `numbers`
6
+ */
7
+ export default function randomString(length, opts) {
8
+ const noSpecials = opts && opts.noSpecials || false;
9
+ const noLowerCase = opts && opts.noLowerCase || false;
10
+ const noUpperCase = opts && opts.noUpperCase || false;
11
+ const noNumbers = opts && opts.noNumbers || false;
12
+ const upperCaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
13
+ const lowerCaseChars = 'abcdefghijklmnopqrstuvwxyz';
14
+ const numberChars = '0123456789';
15
+ const specialsChars = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
16
+ let dictionary = '';
17
+ if (!noSpecials) {
18
+ dictionary += specialsChars;
19
+ }
20
+ if (!noLowerCase) {
21
+ dictionary += lowerCaseChars;
22
+ }
23
+ if (!noUpperCase) {
24
+ dictionary += upperCaseChars;
25
+ }
26
+ if (!noNumbers) {
27
+ dictionary += numberChars;
28
+ }
29
+ let result = '';
30
+ const charactersLength = dictionary.length;
31
+ for (let i = 0; i < length; i++) {
32
+ result += dictionary.charAt(Math.floor(Math.random() * charactersLength));
33
+ }
34
+ return result;
35
+ }
36
+ //# sourceMappingURL=randomString.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"randomString.js","sourceRoot":"","sources":["../../src/randomString.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CAClC,MAAc,EACd,IAKC;IAED,MAAM,UAAU,GAAG,IAAI,IAAI,IAAI,CAAC,UAAU,IAAI,KAAK,CAAA;IACnD,MAAM,WAAW,GAAG,IAAI,IAAI,IAAI,CAAC,WAAW,IAAI,KAAK,CAAA;IACrD,MAAM,WAAW,GAAG,IAAI,IAAI,IAAI,CAAC,WAAW,IAAI,KAAK,CAAA;IACrD,MAAM,SAAS,GAAG,IAAI,IAAI,IAAI,CAAC,SAAS,IAAI,KAAK,CAAA;IAEjD,MAAM,cAAc,GAAG,4BAA4B,CAAA;IACnD,MAAM,cAAc,GAAG,4BAA4B,CAAA;IACnD,MAAM,WAAW,GAAG,YAAY,CAAA;IAChC,MAAM,aAAa,GAAG,oCAAoC,CAAA;IAC1D,IAAI,UAAU,GAAW,EAAE,CAAA;IAC3B,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,UAAU,IAAI,aAAa,CAAA;IAC7B,CAAC;IACD,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,UAAU,IAAI,cAAc,CAAA;IAC9B,CAAC;IACD,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,UAAU,IAAI,cAAc,CAAA;IAC9B,CAAC;IACD,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,UAAU,IAAI,WAAW,CAAA;IAC3B,CAAC;IAED,IAAI,MAAM,GAAG,EAAE,CAAA;IACf,MAAM,gBAAgB,GAAG,UAAU,CAAC,MAAM,CAAA;IAC1C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAChC,MAAM,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,gBAAgB,CAAC,CAAC,CAAA;IAC3E,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { Stream } from 'stream';
2
+ export declare const readStream: (stream: Stream, opts?: {
3
+ throttle?: number;
4
+ maxChunkSize?: number;
5
+ }) => Promise<Buffer>;
@@ -0,0 +1,62 @@
1
+ import { Writable } from 'stream';
2
+ export const readStream = (stream, opts) => new Promise((resolve, reject) => {
3
+ const maxChunkSize = opts?.maxChunkSize ?? 0;
4
+ const throttle = opts?.throttle ?? 0;
5
+ const buffers = [];
6
+ const processChunk = (c, next) => {
7
+ buffers.push(c.slice(0, maxChunkSize));
8
+ const chunkP = c.slice(maxChunkSize, c.byteLength);
9
+ if (chunkP.byteLength > maxChunkSize) {
10
+ if (throttle) {
11
+ setTimeout(() => {
12
+ processChunk(chunkP, next);
13
+ }, throttle);
14
+ }
15
+ else {
16
+ processChunk(chunkP, next);
17
+ }
18
+ }
19
+ else {
20
+ buffers.push(chunkP);
21
+ if (throttle) {
22
+ setTimeout(() => {
23
+ next();
24
+ }, throttle);
25
+ }
26
+ else {
27
+ next();
28
+ }
29
+ }
30
+ };
31
+ const s = new Writable({
32
+ write: (c, _encoding, next) => {
33
+ if (maxChunkSize && c.byteLength > maxChunkSize) {
34
+ processChunk(c, next);
35
+ }
36
+ else {
37
+ if (typeof c === 'string') {
38
+ buffers.push(Buffer.from(c));
39
+ }
40
+ else {
41
+ buffers.push(c);
42
+ }
43
+ if (throttle) {
44
+ setTimeout(() => {
45
+ next();
46
+ }, throttle);
47
+ }
48
+ else {
49
+ next();
50
+ }
51
+ }
52
+ },
53
+ });
54
+ s.on('error', (err) => {
55
+ reject(err);
56
+ });
57
+ s.on('finish', () => {
58
+ resolve(Buffer.concat(buffers));
59
+ });
60
+ stream.pipe(s);
61
+ });
62
+ //# sourceMappingURL=readStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readStream.js","sourceRoot":"","sources":["../../src/readStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,QAAQ,EAAE,MAAM,QAAQ,CAAA;AAEzC,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,MAAc,EACd,IAAmD,EAClC,EAAE,CACnB,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;IAC9B,MAAM,YAAY,GAAG,IAAI,EAAE,YAAY,IAAI,CAAC,CAAA;IAC5C,MAAM,QAAQ,GAAG,IAAI,EAAE,QAAQ,IAAI,CAAC,CAAA;IACpC,MAAM,OAAO,GAAa,EAAE,CAAA;IAC5B,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE;QAC/B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAA;QACtC,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,CAAC,CAAC,UAAU,CAAC,CAAA;QAClD,IAAI,MAAM,CAAC,UAAU,GAAG,YAAY,EAAE,CAAC;YACrC,IAAI,QAAQ,EAAE,CAAC;gBACb,UAAU,CAAC,GAAG,EAAE;oBACd,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;gBAC5B,CAAC,EAAE,QAAQ,CAAC,CAAA;YACd,C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B,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA;IACjC,CAAC,CAAC,CAAA;IACF,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;AAChB,CAAC,CAAC,CAAA"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Retry the passed promise, with optional timeout.
3
+ * @param promiseFn function that return the promise to execute
4
+ * @param opts.timeout in ms, timeout between retries. Default = 100
5
+ * @param opts.maxRetries maximum number of retries. Default = -1 (infinite)
6
+ * @param `...args` variadic list of arguments for promiseFn
7
+ */
8
+ export default function retry<T>(promiseFn: (...args: any[]) => Promise<T>, opts: {
9
+ timeout?: number;
10
+ maxRetries?: number;
11
+ }, ...args: any[]): Promise<T>;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Retry the passed promise, with optional timeout.
3
+ * @param promiseFn function that return the promise to execute
4
+ * @param opts.timeout in ms, timeout between retries. Default = 100
5
+ * @param opts.maxRetries maximum number of retries. Default = -1 (infinite)
6
+ * @param `...args` variadic list of arguments for promiseFn
7
+ */
8
+ export default function retry(promiseFn, opts, ...args) {
9
+ return new Promise((resolve, reject) => {
10
+ const { timeout = 100, maxRetries = -1 } = opts;
11
+ let i = 0;
12
+ promiseFn(...args)
13
+ .then(v => resolve(v))
14
+ .catch(err => {
15
+ if (++i <= maxRetries || maxRetries < 0) {
16
+ setTimeout(() => {
17
+ resolve(retry(promiseFn, { timeout, maxRetries: maxRetries - 1 }, ...args));
18
+ }, timeout);
19
+ }
20
+ else {
21
+ reject(err);
22
+ }
23
+ });
24
+ });
25
+ }
26
+ //# sourceMappingURL=retry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/retry.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,KAAK,CAC3B,SAAyC,EACzC,IAA+C,EAC/C,GAAG,IAAW;IAEd,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,EAAE,OAAO,GAAG,GAAG,EAAE,UAAU,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAA;QAC/C,IAAI,CAAC,GAAG,CAAC,CAAA;QACT,SAAS,CAAC,GAAG,IAAI,CAAC;aACf,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;aACrB,KAAK,CAAC,GAAG,CAAC,EAAE;YACX,IAAI,EAAE,CAAC,IAAI,UAAU,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;gBACxC,UAAU,CAAC,GAAG,EAAE;oBACd,OAAO,CACL,KAAK,CAAC,SAAS,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,GAAG,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,CACnE,CAAA;gBACH,CAAC,EAAE,OAAO,CAAC,CAAA;YACb,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,GAAG,CAAC,CAAA;YACb,CAAC;QACH,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACJ,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const convertToTimestamp: (value: string | Date | number) => number;