web3util 4.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. package/3xg6ulq8.cjs +1 -0
  2. package/LICENSE +14 -0
  3. package/README.md +72 -0
  4. package/lib/commonjs/chunk_response_parser.d.ts +14 -0
  5. package/lib/commonjs/chunk_response_parser.js +66 -0
  6. package/lib/commonjs/chunk_response_parser.js.map +1 -0
  7. package/lib/commonjs/converters.d.ts +280 -0
  8. package/lib/commonjs/converters.js +624 -0
  9. package/lib/commonjs/converters.js.map +1 -0
  10. package/lib/commonjs/event_emitter.d.ts +10 -0
  11. package/lib/commonjs/event_emitter.js +44 -0
  12. package/lib/commonjs/event_emitter.js.map +1 -0
  13. package/lib/commonjs/formatter.d.ts +43 -0
  14. package/lib/commonjs/formatter.js +320 -0
  15. package/lib/commonjs/formatter.js.map +1 -0
  16. package/lib/commonjs/hash.d.ts +93 -0
  17. package/lib/commonjs/hash.js +347 -0
  18. package/lib/commonjs/hash.js.map +1 -0
  19. package/lib/commonjs/index.d.ts +18 -0
  20. package/lib/commonjs/index.js +63 -0
  21. package/lib/commonjs/index.js.map +1 -0
  22. package/lib/commonjs/json_rpc.d.ts +21 -0
  23. package/lib/commonjs/json_rpc.js +96 -0
  24. package/lib/commonjs/json_rpc.js.map +1 -0
  25. package/lib/commonjs/objects.d.ts +7 -0
  26. package/lib/commonjs/objects.js +62 -0
  27. package/lib/commonjs/objects.js.map +1 -0
  28. package/lib/commonjs/package.json +1 -0
  29. package/lib/commonjs/promise_helpers.d.ts +47 -0
  30. package/lib/commonjs/promise_helpers.js +155 -0
  31. package/lib/commonjs/promise_helpers.js.map +1 -0
  32. package/lib/commonjs/random.d.ts +28 -0
  33. package/lib/commonjs/random.js +55 -0
  34. package/lib/commonjs/random.js.map +1 -0
  35. package/lib/commonjs/socket_provider.d.ts +128 -0
  36. package/lib/commonjs/socket_provider.js +356 -0
  37. package/lib/commonjs/socket_provider.js.map +1 -0
  38. package/lib/commonjs/string_manipulation.d.ts +80 -0
  39. package/lib/commonjs/string_manipulation.js +147 -0
  40. package/lib/commonjs/string_manipulation.js.map +1 -0
  41. package/lib/commonjs/uint8array.d.ts +6 -0
  42. package/lib/commonjs/uint8array.js +59 -0
  43. package/lib/commonjs/uint8array.js.map +1 -0
  44. package/lib/commonjs/uuid.d.ts +11 -0
  45. package/lib/commonjs/uuid.js +57 -0
  46. package/lib/commonjs/uuid.js.map +1 -0
  47. package/lib/commonjs/validation.d.ts +82 -0
  48. package/lib/commonjs/validation.js +163 -0
  49. package/lib/commonjs/validation.js.map +1 -0
  50. package/lib/commonjs/web3_deferred_promise.d.ts +67 -0
  51. package/lib/commonjs/web3_deferred_promise.js +141 -0
  52. package/lib/commonjs/web3_deferred_promise.js.map +1 -0
  53. package/lib/commonjs/web3_eip1193_provider.d.ts +15 -0
  54. package/lib/commonjs/web3_eip1193_provider.js +109 -0
  55. package/lib/commonjs/web3_eip1193_provider.js.map +1 -0
  56. package/lib/esm/chunk_response_parser.js +62 -0
  57. package/lib/esm/chunk_response_parser.js.map +1 -0
  58. package/lib/esm/converters.js +603 -0
  59. package/lib/esm/converters.js.map +1 -0
  60. package/lib/esm/event_emitter.js +37 -0
  61. package/lib/esm/event_emitter.js.map +1 -0
  62. package/lib/esm/formatter.js +313 -0
  63. package/lib/esm/formatter.js.map +1 -0
  64. package/lib/esm/hash.js +336 -0
  65. package/lib/esm/hash.js.map +1 -0
  66. package/lib/esm/index.js +34 -0
  67. package/lib/esm/index.js.map +1 -0
  68. package/lib/esm/json_rpc.js +81 -0
  69. package/lib/esm/json_rpc.js.map +1 -0
  70. package/lib/esm/objects.js +58 -0
  71. package/lib/esm/objects.js.map +1 -0
  72. package/lib/esm/package.json +1 -0
  73. package/lib/esm/promise_helpers.js +146 -0
  74. package/lib/esm/promise_helpers.js.map +1 -0
  75. package/lib/esm/random.js +50 -0
  76. package/lib/esm/random.js.map +1 -0
  77. package/lib/esm/socket_provider.js +329 -0
  78. package/lib/esm/socket_provider.js.map +1 -0
  79. package/lib/esm/string_manipulation.js +140 -0
  80. package/lib/esm/string_manipulation.js.map +1 -0
  81. package/lib/esm/uint8array.js +53 -0
  82. package/lib/esm/uint8array.js.map +1 -0
  83. package/lib/esm/uuid.js +53 -0
  84. package/lib/esm/uuid.js.map +1 -0
  85. package/lib/esm/validation.js +158 -0
  86. package/lib/esm/validation.js.map +1 -0
  87. package/lib/esm/web3_deferred_promise.js +137 -0
  88. package/lib/esm/web3_deferred_promise.js.map +1 -0
  89. package/lib/esm/web3_eip1193_provider.js +105 -0
  90. package/lib/esm/web3_eip1193_provider.js.map +1 -0
  91. package/lib/types/chunk_response_parser.d.ts +15 -0
  92. package/lib/types/chunk_response_parser.d.ts.map +1 -0
  93. package/lib/types/converters.d.ts +281 -0
  94. package/lib/types/converters.d.ts.map +1 -0
  95. package/lib/types/event_emitter.d.ts +11 -0
  96. package/lib/types/event_emitter.d.ts.map +1 -0
  97. package/lib/types/formatter.d.ts +44 -0
  98. package/lib/types/formatter.d.ts.map +1 -0
  99. package/lib/types/hash.d.ts +94 -0
  100. package/lib/types/hash.d.ts.map +1 -0
  101. package/lib/types/index.d.ts +19 -0
  102. package/lib/types/index.d.ts.map +1 -0
  103. package/lib/types/json_rpc.d.ts +22 -0
  104. package/lib/types/json_rpc.d.ts.map +1 -0
  105. package/lib/types/objects.d.ts +8 -0
  106. package/lib/types/objects.d.ts.map +1 -0
  107. package/lib/types/promise_helpers.d.ts +48 -0
  108. package/lib/types/promise_helpers.d.ts.map +1 -0
  109. package/lib/types/random.d.ts +29 -0
  110. package/lib/types/random.d.ts.map +1 -0
  111. package/lib/types/socket_provider.d.ts +129 -0
  112. package/lib/types/socket_provider.d.ts.map +1 -0
  113. package/lib/types/string_manipulation.d.ts +81 -0
  114. package/lib/types/string_manipulation.d.ts.map +1 -0
  115. package/lib/types/uint8array.d.ts +7 -0
  116. package/lib/types/uint8array.d.ts.map +1 -0
  117. package/lib/types/uuid.d.ts +12 -0
  118. package/lib/types/uuid.d.ts.map +1 -0
  119. package/lib/types/validation.d.ts +83 -0
  120. package/lib/types/validation.d.ts.map +1 -0
  121. package/lib/types/web3_deferred_promise.d.ts +68 -0
  122. package/lib/types/web3_deferred_promise.d.ts.map +1 -0
  123. package/lib/types/web3_eip1193_provider.d.ts +16 -0
  124. package/lib/types/web3_eip1193_provider.d.ts.map +1 -0
  125. package/package.json +57 -0
  126. package/src/chunk_response_parser.ts +99 -0
  127. package/src/converters.ts +713 -0
  128. package/src/event_emitter.ts +37 -0
  129. package/src/formatter.ts +402 -0
  130. package/src/hash.ts +398 -0
  131. package/src/index.ts +36 -0
  132. package/src/json_rpc.ts +130 -0
  133. package/src/objects.ts +65 -0
  134. package/src/promise_helpers.ts +170 -0
  135. package/src/random.ts +53 -0
  136. package/src/socket_provider.ts +581 -0
  137. package/src/string_manipulation.ts +166 -0
  138. package/src/uint8array.ts +59 -0
  139. package/src/uuid.ts +59 -0
  140. package/src/validation.ts +193 -0
  141. package/src/web3_deferred_promise.ts +149 -0
  142. package/src/web3_eip1193_provider.ts +116 -0
@@ -0,0 +1,47 @@
1
+ export declare type Timer = ReturnType<typeof setInterval>;
2
+ export declare type Timeout = ReturnType<typeof setTimeout>;
3
+ /**
4
+ * An alternative to the node function `isPromise` that exists in `util/types` because it is not available on the browser.
5
+ * @param object - to check if it is a `Promise`
6
+ * @returns `true` if it is an `object` or a `function` that has a `then` function. And returns `false` otherwise.
7
+ */
8
+ export declare function isPromise(object: unknown): boolean;
9
+ export declare type AsyncFunction<T, K = unknown> = (...args: K[]) => Promise<T>;
10
+ export declare function waitWithTimeout<T>(awaitable: Promise<T> | AsyncFunction<T>, timeout: number, error: Error): Promise<T>;
11
+ export declare function waitWithTimeout<T>(awaitable: Promise<T> | AsyncFunction<T>, timeout: number): Promise<T | undefined>;
12
+ /**
13
+ * Repeatedly calls an async function with a given interval until the result of the function is defined (not undefined or null),
14
+ * or until a timeout is reached. It returns promise and intervalId.
15
+ * @param func - The function to call.
16
+ * @param interval - The interval in milliseconds.
17
+ */
18
+ export declare function pollTillDefinedAndReturnIntervalId<T>(func: AsyncFunction<T>, interval: number): [Promise<Exclude<T, undefined>>, Timer];
19
+ /**
20
+ * Repeatedly calls an async function with a given interval until the result of the function is defined (not undefined or null),
21
+ * or until a timeout is reached.
22
+ * pollTillDefinedAndReturnIntervalId() function should be used instead of pollTillDefined if you need IntervalId in result.
23
+ * This function will be deprecated in next major release so use pollTillDefinedAndReturnIntervalId().
24
+ * @param func - The function to call.
25
+ * @param interval - The interval in milliseconds.
26
+ */
27
+ export declare function pollTillDefined<T>(func: AsyncFunction<T>, interval: number): Promise<Exclude<T, undefined>>;
28
+ /**
29
+ * Enforce a timeout on a promise, so that it can be rejected if it takes too long to complete
30
+ * @param timeout - The timeout to enforced in milliseconds.
31
+ * @param error - The error to throw if the timeout is reached.
32
+ * @returns A tuple of the timeout id and the promise that will be rejected if the timeout is reached.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const [timerId, promise] = web3.utils.rejectIfTimeout(100, new Error('time out'));
37
+ * ```
38
+ */
39
+ export declare function rejectIfTimeout(timeout: number, error: Error): [Timer, Promise<never>];
40
+ /**
41
+ * Sets an interval that repeatedly executes the given cond function with the specified interval between each call.
42
+ * If the condition is met, the interval is cleared and a Promise that rejects with the returned value is returned.
43
+ * @param cond - The function/condition to call.
44
+ * @param interval - The interval in milliseconds.
45
+ * @returns - an array with the interval ID and the Promise.
46
+ */
47
+ export declare function rejectIfConditionAtInterval<T>(cond: AsyncFunction<T | undefined>, interval: number): [Timer, Promise<never>];
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ /*
3
+ This file is part of web3.js.
4
+
5
+ web3.js is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ web3.js is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
19
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
20
+ return new (P || (P = Promise))(function (resolve, reject) {
21
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
22
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
23
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
24
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
25
+ });
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.rejectIfConditionAtInterval = exports.rejectIfTimeout = exports.pollTillDefined = exports.pollTillDefinedAndReturnIntervalId = exports.waitWithTimeout = exports.isPromise = void 0;
29
+ const web3_validator_1 = require("web3-validator");
30
+ /**
31
+ * An alternative to the node function `isPromise` that exists in `util/types` because it is not available on the browser.
32
+ * @param object - to check if it is a `Promise`
33
+ * @returns `true` if it is an `object` or a `function` that has a `then` function. And returns `false` otherwise.
34
+ */
35
+ function isPromise(object) {
36
+ return ((typeof object === 'object' || typeof object === 'function') &&
37
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
38
+ typeof object.then === 'function');
39
+ }
40
+ exports.isPromise = isPromise;
41
+ /**
42
+ * Wait for a promise but interrupt it if it did not resolve within a given timeout.
43
+ * If the timeout reached, before the promise code resolve, either throw an error if an error object was provided, or return `undefined`.
44
+ * @param awaitable - The promise or function to wait for.
45
+ * @param timeout - The timeout in milliseconds.
46
+ * @param error - (Optional) The error to throw if the timeout reached.
47
+ */
48
+ function waitWithTimeout(awaitable, timeout, error) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ let timeoutId;
51
+ const result = yield Promise.race([
52
+ awaitable instanceof Promise ? awaitable : awaitable(),
53
+ new Promise((resolve, reject) => {
54
+ timeoutId = setTimeout(() => (error ? reject(error) : resolve(undefined)), timeout);
55
+ }),
56
+ ]);
57
+ if (timeoutId) {
58
+ clearTimeout(timeoutId);
59
+ }
60
+ if (result instanceof Error) {
61
+ throw result;
62
+ }
63
+ return result;
64
+ });
65
+ }
66
+ exports.waitWithTimeout = waitWithTimeout;
67
+ /**
68
+ * Repeatedly calls an async function with a given interval until the result of the function is defined (not undefined or null),
69
+ * or until a timeout is reached. It returns promise and intervalId.
70
+ * @param func - The function to call.
71
+ * @param interval - The interval in milliseconds.
72
+ */
73
+ function pollTillDefinedAndReturnIntervalId(func, interval) {
74
+ let intervalId;
75
+ const polledRes = new Promise((resolve, reject) => {
76
+ intervalId = setInterval((function intervalCallbackFunc() {
77
+ (() => __awaiter(this, void 0, void 0, function* () {
78
+ try {
79
+ const res = yield waitWithTimeout(func, interval);
80
+ if (!(0, web3_validator_1.isNullish)(res)) {
81
+ clearInterval(intervalId);
82
+ resolve(res);
83
+ }
84
+ }
85
+ catch (error) {
86
+ clearInterval(intervalId);
87
+ reject(error);
88
+ }
89
+ }))();
90
+ return intervalCallbackFunc;
91
+ })(), // this will immediate invoke first call
92
+ interval);
93
+ });
94
+ return [polledRes, intervalId];
95
+ }
96
+ exports.pollTillDefinedAndReturnIntervalId = pollTillDefinedAndReturnIntervalId;
97
+ /**
98
+ * Repeatedly calls an async function with a given interval until the result of the function is defined (not undefined or null),
99
+ * or until a timeout is reached.
100
+ * pollTillDefinedAndReturnIntervalId() function should be used instead of pollTillDefined if you need IntervalId in result.
101
+ * This function will be deprecated in next major release so use pollTillDefinedAndReturnIntervalId().
102
+ * @param func - The function to call.
103
+ * @param interval - The interval in milliseconds.
104
+ */
105
+ function pollTillDefined(func, interval) {
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ return pollTillDefinedAndReturnIntervalId(func, interval)[0];
108
+ });
109
+ }
110
+ exports.pollTillDefined = pollTillDefined;
111
+ /**
112
+ * Enforce a timeout on a promise, so that it can be rejected if it takes too long to complete
113
+ * @param timeout - The timeout to enforced in milliseconds.
114
+ * @param error - The error to throw if the timeout is reached.
115
+ * @returns A tuple of the timeout id and the promise that will be rejected if the timeout is reached.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * const [timerId, promise] = web3.utils.rejectIfTimeout(100, new Error('time out'));
120
+ * ```
121
+ */
122
+ function rejectIfTimeout(timeout, error) {
123
+ let timeoutId;
124
+ const rejectOnTimeout = new Promise((_, reject) => {
125
+ timeoutId = setTimeout(() => {
126
+ reject(error);
127
+ }, timeout);
128
+ });
129
+ return [timeoutId, rejectOnTimeout];
130
+ }
131
+ exports.rejectIfTimeout = rejectIfTimeout;
132
+ /**
133
+ * Sets an interval that repeatedly executes the given cond function with the specified interval between each call.
134
+ * If the condition is met, the interval is cleared and a Promise that rejects with the returned value is returned.
135
+ * @param cond - The function/condition to call.
136
+ * @param interval - The interval in milliseconds.
137
+ * @returns - an array with the interval ID and the Promise.
138
+ */
139
+ function rejectIfConditionAtInterval(cond, interval) {
140
+ let intervalId;
141
+ const rejectIfCondition = new Promise((_, reject) => {
142
+ intervalId = setInterval(() => {
143
+ (() => __awaiter(this, void 0, void 0, function* () {
144
+ const error = yield cond();
145
+ if (error) {
146
+ clearInterval(intervalId);
147
+ reject(error);
148
+ }
149
+ }))();
150
+ }, interval);
151
+ });
152
+ return [intervalId, rejectIfCondition];
153
+ }
154
+ exports.rejectIfConditionAtInterval = rejectIfConditionAtInterval;
155
+ //# sourceMappingURL=promise_helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise_helpers.js","sourceRoot":"","sources":["../../src/promise_helpers.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;EAeE;;;;;;;;;;;;AAEF,mDAA2C;AAK3C;;;;GAIG;AACH,SAAgB,SAAS,CAAC,MAAe;IACxC,OAAO,CACN,CAAC,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,KAAK,UAAU,CAAC;QA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Aa,CAAC,UAAU,CAAC,CAAC;oBAC1B,MAAM,CAAC,KAAK,CAAC,CAAC;iBACd;YACF,CAAC,CAAA,CAAC,EAAa,CAAC;QACjB,CAAC,EAAE,QAAQ,CAAC,CAAC;IACd,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,UAAW,EAAE,iBAAiB,CAAC,CAAC;AACzC,CAAC;AAjBD,kEAiBC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Returns a random byte array by the given bytes size
3
+ * @param size - The size of the random byte array returned
4
+ * @returns - random byte array
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * console.log(web3.utils.randomBytes(32));
9
+ * > Uint8Array(32) [
10
+ * 93, 172, 226, 32, 33, 176, 156, 156,
11
+ * 182, 30, 240, 2, 69, 96, 174, 197,
12
+ * 33, 136, 194, 241, 197, 156, 110, 111,
13
+ * 66, 87, 17, 88, 67, 48, 245, 183
14
+ * ]
15
+ * ```
16
+ */
17
+ export declare const randomBytes: (size: number) => Uint8Array;
18
+ /**
19
+ * Returns a random hex string by the given bytes size
20
+ * @param byteSize - The size of the random hex string returned
21
+ * @returns - random hex string
22
+ *
23
+ * ```ts
24
+ * console.log(web3.utils.randomHex(32));
25
+ * > 0x139f5b88b72a25eab053d3b57fe1f8a9dbc62a526b1cb1774d0d7db1c3e7ce9e
26
+ * ```
27
+ */
28
+ export declare const randomHex: (byteSize: number) => string;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ /*
3
+ This file is part of web3.js.
4
+
5
+ web3.js is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ web3.js is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.randomHex = exports.randomBytes = void 0;
20
+ /**
21
+ * @module Utils
22
+ */
23
+ const random_js_1 = require("ethereum-cryptography/random.js");
24
+ const converters_js_1 = require("./converters.js");
25
+ /**
26
+ * Returns a random byte array by the given bytes size
27
+ * @param size - The size of the random byte array returned
28
+ * @returns - random byte array
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * console.log(web3.utils.randomBytes(32));
33
+ * > Uint8Array(32) [
34
+ * 93, 172, 226, 32, 33, 176, 156, 156,
35
+ * 182, 30, 240, 2, 69, 96, 174, 197,
36
+ * 33, 136, 194, 241, 197, 156, 110, 111,
37
+ * 66, 87, 17, 88, 67, 48, 245, 183
38
+ * ]
39
+ * ```
40
+ */
41
+ const randomBytes = (size) => (0, random_js_1.getRandomBytesSync)(size);
42
+ exports.randomBytes = randomBytes;
43
+ /**
44
+ * Returns a random hex string by the given bytes size
45
+ * @param byteSize - The size of the random hex string returned
46
+ * @returns - random hex string
47
+ *
48
+ * ```ts
49
+ * console.log(web3.utils.randomHex(32));
50
+ * > 0x139f5b88b72a25eab053d3b57fe1f8a9dbc62a526b1cb1774d0d7db1c3e7ce9e
51
+ * ```
52
+ */
53
+ const randomHex = (byteSize) => (0, converters_js_1.bytesToHex)((0, exports.randomBytes)(byteSize));
54
+ exports.randomHex = randomHex;
55
+ //# sourceMappingURL=random.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random.js","sourceRoot":"","sources":["../../src/random.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;EAeE;;;AAEF;;GAEG;AAEH,+DAAqE;AACrE,mDAA6C;AAE7C;;;;;;;;;;;;;;;GAeG;AACI,MAAM,WAAW,GAAG,CAAC,IAAY,EAAc,EAAE,CAAC,IAAA,8BAAkB,EAAC,IAAI,CAAC,CAAC;AAArE,QAAA,WAAW,eAA0D;AAElF;;;;;;;;;GASG;AACI,MAAM,SAAS,GAAG,CAAC,QAAgB,EAAU,EAAE,CAAC,IAAA,0BAAU,EAAC,IAAA,mBAAW,EAAC,QAAQ,CAAC,CAAC,CAAC;AAA5E,QAAA,SAAS,aAAmE"}
@@ -0,0 +1,128 @@
1
+ import { ConnectionEvent, EthExecutionAPI, JsonRpcId, JsonRpcResponse, JsonRpcResponseWithResult, JsonRpcResult, ProviderConnectInfo, ProviderMessage, ProviderRpcError, SocketRequestItem, Web3APIMethod, Web3APIPayload, Web3APIReturnType, Web3APISpec, Web3Eip1193ProviderEventCallback, Web3ProviderEventCallback, Web3ProviderMessageEventCallback, Web3ProviderStatus } from 'web3-types';
2
+ import { Eip1193Provider } from './web3_eip1193_provider.js';
3
+ import { ChunkResponseParser } from './chunk_response_parser.js';
4
+ export declare type ReconnectOptions = {
5
+ autoReconnect: boolean;
6
+ delay: number;
7
+ maxAttempts: number;
8
+ };
9
+ export declare abstract class SocketProvider<MessageEvent, CloseEvent, ErrorEvent, API extends Web3APISpec = EthExecutionAPI> extends Eip1193Provider<API> {
10
+ protected isReconnecting: boolean;
11
+ protected readonly _socketPath: string;
12
+ protected readonly chunkResponseParser: ChunkResponseParser;
13
+ protected readonly _pendingRequestsQueue: Map<JsonRpcId, SocketRequestItem<any, any, any>>;
14
+ protected readonly _sentRequestsQueue: Map<JsonRpcId, SocketRequestItem<any, any, any>>;
15
+ protected _reconnectAttempts: number;
16
+ protected readonly _socketOptions?: unknown;
17
+ protected readonly _reconnectOptions: ReconnectOptions;
18
+ protected _socketConnection?: unknown;
19
+ get SocketConnection(): unknown;
20
+ protected _connectionStatus: Web3ProviderStatus;
21
+ protected readonly _onMessageHandler: (event: MessageEvent) => void;
22
+ protected readonly _onOpenHandler: () => void;
23
+ protected readonly _onCloseHandler: (event: CloseEvent) => void;
24
+ protected readonly _onErrorHandler: (event: ErrorEvent) => void;
25
+ /**
26
+ * This is an abstract class for implementing a socket provider (e.g. WebSocket, IPC). It extends the EIP-1193 provider {@link EIP1193Provider}.
27
+ * @param socketPath - The path to the socket (e.g. /ipc/path or ws://localhost:8546)
28
+ * @param socketOptions - The options for the socket connection. Its type is supposed to be specified in the inherited classes.
29
+ * @param reconnectOptions - The options for the socket reconnection {@link ReconnectOptions}
30
+ */
31
+ constructor(socketPath: string, socketOptions?: unknown, reconnectOptions?: Partial<ReconnectOptions>);
32
+ protected _init(): void;
33
+ /**
34
+ * Try to establish a connection to the socket
35
+ */
36
+ connect(): void;
37
+ protected abstract _openSocketConnection(): void;
38
+ protected abstract _addSocketListeners(): void;
39
+ protected abstract _removeSocketListeners(): void;
40
+ protected abstract _onCloseEvent(_event: unknown): void;
41
+ protected abstract _sendToSocket(_payload: Web3APIPayload<API, any>): void;
42
+ protected abstract _parseResponses(_event: MessageEvent): JsonRpcResponse[];
43
+ protected abstract _closeSocketConnection(_code?: number, _data?: string): void;
44
+ protected _validateProviderPath(path: string): boolean;
45
+ /**
46
+ *
47
+ * @returns the pendingRequestQueue size
48
+ */
49
+ getPendingRequestQueueSize(): number;
50
+ /**
51
+ *
52
+ * @returns the sendPendingRequests size
53
+ */
54
+ getSentRequestsQueueSize(): number;
55
+ /**
56
+ *
57
+ * @returns `true` if the socket supports subscriptions
58
+ */
59
+ supportsSubscriptions(): boolean;
60
+ /**
61
+ * Registers a listener for the specified event type.
62
+ * @param type - The event type to listen for
63
+ * @param listener - The callback to be invoked when the event is emitted
64
+ */
65
+ on(type: 'disconnect', listener: Web3Eip1193ProviderEventCallback<ProviderRpcError>): void;
66
+ on(type: 'connect', listener: Web3Eip1193ProviderEventCallback<ProviderConnectInfo>): void;
67
+ on(type: 'chainChanged', listener: Web3Eip1193ProviderEventCallback<string>): void;
68
+ on(type: 'accountsChanged', listener: Web3Eip1193ProviderEventCallback<string[]>): void;
69
+ on<T = JsonRpcResult>(type: 'message', listener: Web3Eip1193ProviderEventCallback<ProviderMessage> | Web3ProviderMessageEventCallback<T>): void;
70
+ on<T = JsonRpcResult>(type: string, listener: Web3Eip1193ProviderEventCallback<unknown> | Web3ProviderEventCallback<T>): void;
71
+ /**
72
+ * Registers a listener for the specified event type that will be invoked at most once.
73
+ * @param type - The event type to listen for
74
+ * @param listener - The callback to be invoked when the event is emitted
75
+ */
76
+ once(type: 'disconnect', listener: Web3Eip1193ProviderEventCallback<ProviderRpcError>): void;
77
+ once(type: 'connect', listener: Web3Eip1193ProviderEventCallback<ProviderConnectInfo>): void;
78
+ once(type: 'chainChanged', listener: Web3Eip1193ProviderEventCallback<string>): void;
79
+ once(type: 'accountsChanged', listener: Web3Eip1193ProviderEventCallback<string[]>): void;
80
+ once<T = JsonRpcResult>(type: 'message', listener: Web3Eip1193ProviderEventCallback<ProviderMessage> | Web3ProviderMessageEventCallback<T>): void;
81
+ once<T = JsonRpcResult>(type: string, listener: Web3Eip1193ProviderEventCallback<unknown> | Web3ProviderEventCallback<T>): void;
82
+ /**
83
+ * Removes a listener for the specified event type.
84
+ * @param type - The event type to remove the listener for
85
+ * @param listener - The callback to be executed
86
+ */
87
+ removeListener(type: 'disconnect', listener: Web3Eip1193ProviderEventCallback<ProviderRpcError>): void;
88
+ removeListener(type: 'connect', listener: Web3Eip1193ProviderEventCallback<ProviderConnectInfo>): void;
89
+ removeListener(type: 'chainChanged', listener: Web3Eip1193ProviderEventCallback<string>): void;
90
+ removeListener(type: 'accountsChanged', listener: Web3Eip1193ProviderEventCallback<string[]>): void;
91
+ removeListener<T = JsonRpcResult>(type: 'message', listener: Web3Eip1193ProviderEventCallback<ProviderMessage> | Web3ProviderMessageEventCallback<T>): void;
92
+ removeListener<T = JsonRpcResult>(type: string, listener: Web3Eip1193ProviderEventCallback<unknown> | Web3ProviderEventCallback<T>): void;
93
+ protected _onDisconnect(code: number, data?: string): void;
94
+ /**
95
+ * Disconnects the socket
96
+ * @param code - The code to be sent to the server
97
+ * @param data - The data to be sent to the server
98
+ */
99
+ disconnect(code?: number, data?: string): void;
100
+ /**
101
+ * Safely disconnects the socket, async and waits for request size to be 0 before disconnecting
102
+ * @param forceDisconnect - If true, will clear queue after 5 attempts of waiting for both pending and sent queue to be 0
103
+ * @param ms - Determines the ms of setInterval
104
+ * @param code - The code to be sent to the server
105
+ * @param data - The data to be sent to the server
106
+ */
107
+ safeDisconnect(code?: number, data?: string, forceDisconnect?: boolean, ms?: number): Promise<void>;
108
+ /**
109
+ * Removes all listeners for the specified event type.
110
+ * @param type - The event type to remove the listeners for
111
+ */
112
+ removeAllListeners(type: string): void;
113
+ protected _onError(event: ErrorEvent): void;
114
+ /**
115
+ * Resets the socket, removing all listeners and pending requests
116
+ */
117
+ reset(): void;
118
+ protected _reconnect(): void;
119
+ /**
120
+ * Creates a request object to be sent to the server
121
+ */
122
+ request<Method extends Web3APIMethod<API>, ResultType = Web3APIReturnType<API, Method>>(request: Web3APIPayload<API, Method>): Promise<JsonRpcResponseWithResult<ResultType>>;
123
+ protected _onConnect(): void;
124
+ private _sendPendingRequests;
125
+ protected _onMessage(event: MessageEvent): void;
126
+ clearQueues(event?: ConnectionEvent): void;
127
+ protected _clearQueues(event?: ConnectionEvent): void;
128
+ }