@pezkuwi/rpc-provider 16.5.5 → 16.5.8

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 (277) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +10 -10
  3. package/bizinikiwi-connect/Health.js +259 -0
  4. package/{build/substrate-connect → bizinikiwi-connect}/index.d.ts +3 -3
  5. package/bizinikiwi-connect/index.js +319 -0
  6. package/bizinikiwi-connect/types.js +1 -0
  7. package/build/LICENSE +201 -0
  8. package/build/README.md +68 -0
  9. package/build/bizinikiwi-connect/Health.d.ts +7 -0
  10. package/build/bizinikiwi-connect/Health.js +259 -0
  11. package/build/bizinikiwi-connect/index.d.ts +22 -0
  12. package/build/bizinikiwi-connect/index.js +319 -0
  13. package/build/bizinikiwi-connect/types.d.ts +12 -0
  14. package/build/bizinikiwi-connect/types.js +1 -0
  15. package/build/bundle.d.ts +1 -1
  16. package/build/bundle.js +5 -0
  17. package/build/cjs/bizinikiwi-connect/Health.d.ts +7 -0
  18. package/build/cjs/bizinikiwi-connect/Health.js +264 -0
  19. package/build/cjs/bizinikiwi-connect/index.d.ts +22 -0
  20. package/build/cjs/bizinikiwi-connect/index.js +323 -0
  21. package/build/cjs/bizinikiwi-connect/types.d.ts +12 -0
  22. package/build/cjs/bizinikiwi-connect/types.js +2 -0
  23. package/build/cjs/bundle.d.ts +5 -0
  24. package/build/cjs/bundle.js +14 -0
  25. package/build/cjs/coder/error.d.ts +29 -0
  26. package/build/cjs/coder/error.js +53 -0
  27. package/build/cjs/coder/index.d.ts +8 -0
  28. package/build/cjs/coder/index.js +63 -0
  29. package/build/cjs/defaults.d.ts +5 -0
  30. package/build/cjs/defaults.js +8 -0
  31. package/build/cjs/http/index.d.ts +81 -0
  32. package/build/cjs/http/index.js +196 -0
  33. package/build/cjs/http/types.d.ts +7 -0
  34. package/build/cjs/http/types.js +2 -0
  35. package/build/cjs/index.d.ts +2 -0
  36. package/build/cjs/index.js +5 -0
  37. package/build/cjs/lru.d.ts +15 -0
  38. package/build/cjs/lru.js +150 -0
  39. package/build/cjs/mock/index.d.ts +35 -0
  40. package/build/cjs/mock/index.js +196 -0
  41. package/build/cjs/mock/mockHttp.d.ts +9 -0
  42. package/build/cjs/mock/mockHttp.js +17 -0
  43. package/build/cjs/mock/mockWs.d.ts +26 -0
  44. package/build/cjs/mock/mockWs.js +47 -0
  45. package/build/cjs/mock/types.d.ts +23 -0
  46. package/build/cjs/mock/types.js +2 -0
  47. package/build/cjs/package.json +3 -0
  48. package/build/cjs/packageDetect.d.ts +1 -0
  49. package/build/cjs/packageDetect.js +6 -0
  50. package/build/cjs/packageInfo.d.ts +6 -0
  51. package/build/cjs/packageInfo.js +4 -0
  52. package/build/cjs/types.d.ts +85 -0
  53. package/build/cjs/types.js +2 -0
  54. package/build/cjs/ws/errors.d.ts +1 -0
  55. package/build/cjs/ws/errors.js +41 -0
  56. package/build/cjs/ws/index.d.ts +121 -0
  57. package/build/cjs/ws/index.js +529 -0
  58. package/build/coder/error.js +50 -0
  59. package/build/coder/index.js +58 -0
  60. package/build/defaults.js +6 -0
  61. package/build/http/index.d.ts +1 -1
  62. package/build/http/index.js +191 -0
  63. package/build/http/types.js +1 -0
  64. package/build/index.js +2 -0
  65. package/build/lru.js +146 -0
  66. package/build/mock/index.js +191 -0
  67. package/build/mock/mockHttp.js +12 -0
  68. package/build/mock/mockWs.js +43 -0
  69. package/build/mock/types.js +1 -0
  70. package/build/package.json +344 -0
  71. package/build/packageDetect.js +4 -0
  72. package/build/packageInfo.js +1 -0
  73. package/build/types.js +1 -0
  74. package/build/ws/errors.js +38 -0
  75. package/build/ws/index.d.ts +1 -1
  76. package/build/ws/index.js +524 -0
  77. package/build-deno/README.md +66 -0
  78. package/build-deno/bizinikiwi-connect/Health.ts +323 -0
  79. package/build-deno/bizinikiwi-connect/index.ts +417 -0
  80. package/build-deno/bizinikiwi-connect/types.ts +14 -0
  81. package/build-deno/bundle.ts +6 -0
  82. package/build-deno/coder/error.ts +64 -0
  83. package/build-deno/coder/index.ts +86 -0
  84. package/build-deno/defaults.ts +8 -0
  85. package/build-deno/http/index.ts +236 -0
  86. package/build-deno/http/types.ts +9 -0
  87. package/build-deno/index.ts +4 -0
  88. package/build-deno/lru.ts +189 -0
  89. package/build-deno/mock/index.ts +257 -0
  90. package/build-deno/mock/mockHttp.ts +33 -0
  91. package/build-deno/mock/mockWs.ts +87 -0
  92. package/build-deno/mock/types.ts +34 -0
  93. package/build-deno/mod.ts +2 -0
  94. package/build-deno/packageDetect.ts +8 -0
  95. package/build-deno/packageInfo.ts +3 -0
  96. package/build-deno/types.ts +99 -0
  97. package/build-deno/ws/errors.ts +38 -0
  98. package/build-deno/ws/index.ts +650 -0
  99. package/build-tsc/bizinikiwi-connect/Health.d.ts +7 -0
  100. package/build-tsc/bizinikiwi-connect/index.d.ts +22 -0
  101. package/build-tsc/bizinikiwi-connect/types.d.ts +12 -0
  102. package/build-tsc/bundle.d.ts +5 -0
  103. package/build-tsc/coder/error.d.ts +29 -0
  104. package/build-tsc/coder/index.d.ts +8 -0
  105. package/build-tsc/defaults.d.ts +5 -0
  106. package/build-tsc/http/index.d.ts +81 -0
  107. package/build-tsc/http/types.d.ts +7 -0
  108. package/build-tsc/index.d.ts +2 -0
  109. package/build-tsc/lru.d.ts +15 -0
  110. package/build-tsc/mock/index.d.ts +35 -0
  111. package/build-tsc/mock/mockHttp.d.ts +9 -0
  112. package/build-tsc/mock/mockWs.d.ts +26 -0
  113. package/build-tsc/mock/types.d.ts +23 -0
  114. package/build-tsc/packageDetect.d.ts +1 -0
  115. package/build-tsc/packageInfo.d.ts +6 -0
  116. package/build-tsc/types.d.ts +85 -0
  117. package/build-tsc/ws/errors.d.ts +1 -0
  118. package/build-tsc/ws/index.d.ts +121 -0
  119. package/build-tsc-cjs/bizinikiwi-connect/Health.js +264 -0
  120. package/build-tsc-cjs/bizinikiwi-connect/index.js +323 -0
  121. package/build-tsc-cjs/bizinikiwi-connect/types.js +2 -0
  122. package/build-tsc-cjs/bundle.js +14 -0
  123. package/build-tsc-cjs/coder/error.js +53 -0
  124. package/build-tsc-cjs/coder/index.js +63 -0
  125. package/build-tsc-cjs/defaults.js +8 -0
  126. package/build-tsc-cjs/http/index.js +196 -0
  127. package/build-tsc-cjs/http/types.js +2 -0
  128. package/build-tsc-cjs/index.js +5 -0
  129. package/build-tsc-cjs/lru.js +150 -0
  130. package/build-tsc-cjs/mock/index.js +196 -0
  131. package/build-tsc-cjs/mock/mockHttp.js +17 -0
  132. package/build-tsc-cjs/mock/mockWs.js +47 -0
  133. package/build-tsc-cjs/mock/types.js +2 -0
  134. package/build-tsc-cjs/packageDetect.js +6 -0
  135. package/build-tsc-cjs/packageInfo.js +4 -0
  136. package/build-tsc-cjs/types.js +2 -0
  137. package/build-tsc-cjs/ws/errors.js +41 -0
  138. package/build-tsc-cjs/ws/index.js +529 -0
  139. package/build-tsc-esm/bizinikiwi-connect/Health.js +259 -0
  140. package/build-tsc-esm/bizinikiwi-connect/index.js +319 -0
  141. package/build-tsc-esm/bizinikiwi-connect/types.js +1 -0
  142. package/build-tsc-esm/bundle.js +5 -0
  143. package/build-tsc-esm/coder/error.js +50 -0
  144. package/build-tsc-esm/coder/index.js +58 -0
  145. package/build-tsc-esm/defaults.js +6 -0
  146. package/build-tsc-esm/http/index.js +191 -0
  147. package/build-tsc-esm/http/types.js +1 -0
  148. package/build-tsc-esm/index.js +2 -0
  149. package/build-tsc-esm/lru.js +146 -0
  150. package/build-tsc-esm/mock/index.js +191 -0
  151. package/build-tsc-esm/mock/mockHttp.js +12 -0
  152. package/build-tsc-esm/mock/mockWs.js +43 -0
  153. package/build-tsc-esm/mock/types.js +1 -0
  154. package/build-tsc-esm/packageDetect.js +4 -0
  155. package/build-tsc-esm/packageInfo.js +1 -0
  156. package/build-tsc-esm/types.js +1 -0
  157. package/build-tsc-esm/ws/errors.js +38 -0
  158. package/build-tsc-esm/ws/index.js +524 -0
  159. package/bundle.d.ts +5 -0
  160. package/bundle.js +5 -0
  161. package/cjs/bizinikiwi-connect/Health.d.ts +7 -0
  162. package/cjs/bizinikiwi-connect/Health.js +264 -0
  163. package/cjs/bizinikiwi-connect/index.d.ts +22 -0
  164. package/cjs/bizinikiwi-connect/index.js +323 -0
  165. package/cjs/bizinikiwi-connect/types.d.ts +12 -0
  166. package/cjs/bizinikiwi-connect/types.js +2 -0
  167. package/cjs/bundle.d.ts +5 -0
  168. package/cjs/bundle.js +14 -0
  169. package/cjs/coder/error.d.ts +29 -0
  170. package/cjs/coder/error.js +53 -0
  171. package/cjs/coder/index.d.ts +8 -0
  172. package/cjs/coder/index.js +63 -0
  173. package/cjs/defaults.d.ts +5 -0
  174. package/cjs/defaults.js +8 -0
  175. package/cjs/http/index.d.ts +81 -0
  176. package/cjs/http/index.js +196 -0
  177. package/cjs/http/types.d.ts +7 -0
  178. package/cjs/http/types.js +2 -0
  179. package/cjs/index.d.ts +2 -0
  180. package/cjs/index.js +5 -0
  181. package/cjs/lru.d.ts +15 -0
  182. package/cjs/lru.js +150 -0
  183. package/cjs/mock/index.d.ts +35 -0
  184. package/cjs/mock/index.js +196 -0
  185. package/cjs/mock/mockHttp.d.ts +9 -0
  186. package/cjs/mock/mockHttp.js +17 -0
  187. package/cjs/mock/mockWs.d.ts +26 -0
  188. package/cjs/mock/mockWs.js +47 -0
  189. package/cjs/mock/types.d.ts +23 -0
  190. package/cjs/mock/types.js +2 -0
  191. package/cjs/package.json +3 -0
  192. package/cjs/packageDetect.d.ts +1 -0
  193. package/cjs/packageDetect.js +6 -0
  194. package/cjs/packageInfo.d.ts +6 -0
  195. package/cjs/packageInfo.js +4 -0
  196. package/cjs/types.d.ts +85 -0
  197. package/cjs/types.js +2 -0
  198. package/cjs/ws/errors.d.ts +1 -0
  199. package/cjs/ws/errors.js +41 -0
  200. package/cjs/ws/index.d.ts +121 -0
  201. package/cjs/ws/index.js +529 -0
  202. package/coder/error.d.ts +29 -0
  203. package/coder/error.js +50 -0
  204. package/coder/index.d.ts +8 -0
  205. package/coder/index.js +58 -0
  206. package/defaults.d.ts +5 -0
  207. package/defaults.js +6 -0
  208. package/http/index.d.ts +81 -0
  209. package/http/index.js +191 -0
  210. package/http/types.d.ts +7 -0
  211. package/http/types.js +1 -0
  212. package/index.d.ts +2 -0
  213. package/index.js +2 -0
  214. package/lru.d.ts +15 -0
  215. package/lru.js +146 -0
  216. package/mock/index.d.ts +35 -0
  217. package/mock/index.js +191 -0
  218. package/mock/mockHttp.d.ts +9 -0
  219. package/mock/mockHttp.js +12 -0
  220. package/mock/mockWs.d.ts +26 -0
  221. package/mock/mockWs.js +43 -0
  222. package/mock/types.d.ts +23 -0
  223. package/mock/types.js +1 -0
  224. package/package.json +316 -15
  225. package/packageDetect.d.ts +1 -0
  226. package/packageDetect.js +4 -0
  227. package/packageInfo.d.ts +6 -0
  228. package/packageInfo.js +1 -0
  229. package/src/{substrate-connect → bizinikiwi-connect}/Health.ts +1 -1
  230. package/src/{substrate-connect → bizinikiwi-connect}/index.spec.ts +55 -22
  231. package/src/{substrate-connect → bizinikiwi-connect}/index.ts +27 -15
  232. package/src/{substrate-connect → bizinikiwi-connect}/types.ts +1 -1
  233. package/src/bundle.ts +2 -2
  234. package/src/coder/decodeResponse.spec.ts +1 -1
  235. package/src/coder/encodeJson.spec.ts +1 -1
  236. package/src/coder/encodeObject.spec.ts +1 -1
  237. package/src/coder/error.spec.ts +1 -1
  238. package/src/coder/error.ts +1 -1
  239. package/src/coder/index.ts +1 -1
  240. package/src/defaults.ts +1 -1
  241. package/src/http/index.spec.ts +1 -1
  242. package/src/http/index.ts +2 -2
  243. package/src/http/send.spec.ts +1 -1
  244. package/src/http/types.ts +1 -1
  245. package/src/index.ts +1 -1
  246. package/src/lru.spec.ts +1 -1
  247. package/src/lru.ts +2 -2
  248. package/src/mock/index.ts +2 -2
  249. package/src/mock/mockHttp.ts +1 -1
  250. package/src/mock/mockWs.ts +1 -1
  251. package/src/mock/on.spec.ts +1 -1
  252. package/src/mock/send.spec.ts +1 -1
  253. package/src/mock/subscribe.spec.ts +1 -1
  254. package/src/mock/types.ts +1 -1
  255. package/src/mock/unsubscribe.spec.ts +1 -1
  256. package/src/mod.ts +1 -1
  257. package/src/packageDetect.ts +2 -2
  258. package/src/packageInfo.ts +3 -3
  259. package/src/types.ts +1 -1
  260. package/src/ws/connect.spec.ts +1 -1
  261. package/src/ws/errors.ts +1 -1
  262. package/src/ws/index.spec.ts +1 -1
  263. package/src/ws/index.ts +2 -2
  264. package/src/ws/send.spec.ts +1 -1
  265. package/src/ws/state.spec.ts +1 -1
  266. package/src/ws/subscribe.spec.ts +1 -1
  267. package/src/ws/unsubscribe.spec.ts +1 -1
  268. package/tsconfig.build.tsbuildinfo +1 -1
  269. package/tsconfig.spec.tsbuildinfo +1 -0
  270. package/types.d.ts +85 -0
  271. package/types.js +1 -0
  272. package/ws/errors.d.ts +1 -0
  273. package/ws/errors.js +38 -0
  274. package/ws/index.d.ts +121 -0
  275. package/ws/index.js +524 -0
  276. /package/{build/substrate-connect → bizinikiwi-connect}/Health.d.ts +0 -0
  277. /package/{build/substrate-connect → bizinikiwi-connect}/types.d.ts +0 -0
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const util_1 = require("@pezkuwi/util");
4
+ const UNKNOWN = -99999;
5
+ function extend(that, name, value) {
6
+ Object.defineProperty(that, name, {
7
+ configurable: true,
8
+ enumerable: false,
9
+ value
10
+ });
11
+ }
12
+ /**
13
+ * @name RpcError
14
+ * @summary Extension to the basic JS Error.
15
+ * @description
16
+ * The built-in JavaScript Error class is extended by adding a code to allow for Error categorization. In addition to the normal `stack`, `message`, the numeric `code` and `data` (any types) parameters are available on the object.
17
+ * @example
18
+ * <BR>
19
+ *
20
+ * ```javascript
21
+ * const { RpcError } from '@pezkuwi/util');
22
+ *
23
+ * throw new RpcError('some message', RpcError.CODES.METHOD_NOT_FOUND); // => error.code = -32601
24
+ * ```
25
+ */
26
+ class RpcError extends Error {
27
+ code;
28
+ data;
29
+ message;
30
+ name;
31
+ stack;
32
+ constructor(message = '', code = UNKNOWN, data) {
33
+ super();
34
+ extend(this, 'message', String(message));
35
+ extend(this, 'name', this.constructor.name);
36
+ extend(this, 'data', data);
37
+ extend(this, 'code', code);
38
+ if ((0, util_1.isFunction)(Error.captureStackTrace)) {
39
+ Error.captureStackTrace(this, this.constructor);
40
+ }
41
+ else {
42
+ const { stack } = new Error(message);
43
+ stack && extend(this, 'stack', stack);
44
+ }
45
+ }
46
+ static CODES = {
47
+ ASSERT: -90009,
48
+ INVALID_JSONRPC: -99998,
49
+ METHOD_NOT_FOUND: -32601, // Rust client
50
+ UNKNOWN
51
+ };
52
+ }
53
+ exports.default = RpcError;
@@ -0,0 +1,8 @@
1
+ import type { JsonRpcRequest, JsonRpcResponse } from '../types.js';
2
+ /** @internal */
3
+ export declare class RpcCoder {
4
+ #private;
5
+ decodeResponse<T>(response?: JsonRpcResponse<T>): T;
6
+ encodeJson(method: string, params: unknown[]): [number, string];
7
+ encodeObject(method: string, params: unknown[]): [number, JsonRpcRequest];
8
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RpcCoder = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const util_1 = require("@pezkuwi/util");
6
+ const error_js_1 = tslib_1.__importDefault(require("./error.js"));
7
+ function formatErrorData(data) {
8
+ if ((0, util_1.isUndefined)(data)) {
9
+ return '';
10
+ }
11
+ const formatted = `: ${(0, util_1.isString)(data)
12
+ ? data.replace(/Error\("/g, '').replace(/\("/g, '(').replace(/"\)/g, ')').replace(/\(/g, ', ').replace(/\)/g, '')
13
+ : (0, util_1.stringify)(data)}`;
14
+ // We need some sort of cut-off here since these can be very large and
15
+ // very nested, pick a number and trim the result display to it
16
+ return formatted.length <= 256
17
+ ? formatted
18
+ : `${formatted.substring(0, 255)}…`;
19
+ }
20
+ function checkError(error) {
21
+ if (error) {
22
+ const { code, data, message } = error;
23
+ throw new error_js_1.default(`${code}: ${message}${formatErrorData(data)}`, code, data);
24
+ }
25
+ }
26
+ /** @internal */
27
+ class RpcCoder {
28
+ #id = 0;
29
+ decodeResponse(response) {
30
+ if (!response || response.jsonrpc !== '2.0') {
31
+ throw new Error('Invalid jsonrpc field in decoded object');
32
+ }
33
+ const isSubscription = !(0, util_1.isUndefined)(response.params) && !(0, util_1.isUndefined)(response.method);
34
+ if (!(0, util_1.isNumber)(response.id) &&
35
+ (!isSubscription || (!(0, util_1.isNumber)(response.params.subscription) &&
36
+ !(0, util_1.isString)(response.params.subscription)))) {
37
+ throw new Error('Invalid id field in decoded object');
38
+ }
39
+ checkError(response.error);
40
+ if (response.result === undefined && !isSubscription) {
41
+ throw new Error('No result found in jsonrpc response');
42
+ }
43
+ if (isSubscription) {
44
+ checkError(response.params.error);
45
+ return response.params.result;
46
+ }
47
+ return response.result;
48
+ }
49
+ encodeJson(method, params) {
50
+ const [id, data] = this.encodeObject(method, params);
51
+ return [id, (0, util_1.stringify)(data)];
52
+ }
53
+ encodeObject(method, params) {
54
+ const id = ++this.#id;
55
+ return [id, {
56
+ id,
57
+ jsonrpc: '2.0',
58
+ method,
59
+ params
60
+ }];
61
+ }
62
+ }
63
+ exports.RpcCoder = RpcCoder;
@@ -0,0 +1,5 @@
1
+ declare const _default: {
2
+ HTTP_URL: string;
3
+ WS_URL: string;
4
+ };
5
+ export default _default;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const HTTP_URL = 'http://127.0.0.1:9933';
4
+ const WS_URL = 'ws://127.0.0.1:9944';
5
+ exports.default = {
6
+ HTTP_URL,
7
+ WS_URL
8
+ };
@@ -0,0 +1,81 @@
1
+ import type { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted, ProviderStats } from '../types.js';
2
+ /**
3
+ * # @pezkuwi/rpc-provider
4
+ *
5
+ * @name HttpProvider
6
+ *
7
+ * @description The HTTP Provider allows sending requests using HTTP to a HTTP RPC server TCP port. It does not support subscriptions so you won't be able to listen to events such as new blocks or balance changes. It is usually preferable using the [[WsProvider]].
8
+ *
9
+ * @example
10
+ * <BR>
11
+ *
12
+ * ```javascript
13
+ * import Api from '@pezkuwi/api/promise';
14
+ * import { HttpProvider } from '@pezkuwi/rpc-provider';
15
+ *
16
+ * const provider = new HttpProvider('http://127.0.0.1:9933');
17
+ * const api = new Api(provider);
18
+ * ```
19
+ *
20
+ * @see [[WsProvider]]
21
+ */
22
+ export declare class HttpProvider implements ProviderInterface {
23
+ #private;
24
+ /**
25
+ * @param {string} endpoint The endpoint url starting with http://
26
+ * @param {Record<string, string>} headers The headers provided to the underlying Http Endpoint
27
+ * @param {number} [cacheCapacity] Custom size of the HttpProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
28
+ * @param {number} [cacheTtl] Custom TTL of the HttpProvider LRUCache. Determines how long an object can live in the cache. Defaults to `DEFAULT_TTL` (30000)
29
+ */
30
+ constructor(endpoint?: string, headers?: Record<string, string>, cacheCapacity?: number, cacheTtl?: number | null);
31
+ /**
32
+ * @summary `true` when this provider supports subscriptions
33
+ */
34
+ get hasSubscriptions(): boolean;
35
+ /**
36
+ * @description Returns a clone of the object
37
+ */
38
+ clone(): HttpProvider;
39
+ /**
40
+ * @description Manually connect from the connection
41
+ */
42
+ connect(): Promise<void>;
43
+ /**
44
+ * @description Manually disconnect from the connection
45
+ */
46
+ disconnect(): Promise<void>;
47
+ /**
48
+ * @description Returns the connection stats
49
+ */
50
+ get stats(): ProviderStats;
51
+ /**
52
+ * @description Returns the connection stats
53
+ */
54
+ get ttl(): number | null | undefined;
55
+ /**
56
+ * @summary `true` when this provider supports clone()
57
+ */
58
+ get isClonable(): boolean;
59
+ /**
60
+ * @summary Whether the node is connected or not.
61
+ * @return {boolean} true if connected
62
+ */
63
+ get isConnected(): boolean;
64
+ /**
65
+ * @summary Events are not supported with the HttpProvider, see [[WsProvider]].
66
+ * @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
67
+ */
68
+ on(_type: ProviderInterfaceEmitted, _sub: ProviderInterfaceEmitCb): () => void;
69
+ /**
70
+ * @summary Send HTTP POST Request with Body to configured HTTP Endpoint.
71
+ */
72
+ send<T>(method: string, params: unknown[], isCacheable?: boolean): Promise<T>;
73
+ /**
74
+ * @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
75
+ */
76
+ subscribe(_types: string, _method: string, _params: unknown[], _cb: ProviderInterfaceCallback): Promise<number>;
77
+ /**
78
+ * @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
79
+ */
80
+ unsubscribe(_type: string, _method: string, _id: number): Promise<boolean>;
81
+ }
@@ -0,0 +1,196 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpProvider = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const util_1 = require("@pezkuwi/util");
6
+ const x_fetch_1 = require("@pezkuwi/x-fetch");
7
+ const index_js_1 = require("../coder/index.js");
8
+ const defaults_js_1 = tslib_1.__importDefault(require("../defaults.js"));
9
+ const lru_js_1 = require("../lru.js");
10
+ const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead';
11
+ const l = (0, util_1.logger)('api-http');
12
+ /**
13
+ * # @pezkuwi/rpc-provider
14
+ *
15
+ * @name HttpProvider
16
+ *
17
+ * @description The HTTP Provider allows sending requests using HTTP to a HTTP RPC server TCP port. It does not support subscriptions so you won't be able to listen to events such as new blocks or balance changes. It is usually preferable using the [[WsProvider]].
18
+ *
19
+ * @example
20
+ * <BR>
21
+ *
22
+ * ```javascript
23
+ * import Api from '@pezkuwi/api/promise';
24
+ * import { HttpProvider } from '@pezkuwi/rpc-provider';
25
+ *
26
+ * const provider = new HttpProvider('http://127.0.0.1:9933');
27
+ * const api = new Api(provider);
28
+ * ```
29
+ *
30
+ * @see [[WsProvider]]
31
+ */
32
+ class HttpProvider {
33
+ #callCache;
34
+ #cacheCapacity;
35
+ #coder;
36
+ #endpoint;
37
+ #headers;
38
+ #stats;
39
+ #ttl;
40
+ /**
41
+ * @param {string} endpoint The endpoint url starting with http://
42
+ * @param {Record<string, string>} headers The headers provided to the underlying Http Endpoint
43
+ * @param {number} [cacheCapacity] Custom size of the HttpProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
44
+ * @param {number} [cacheTtl] Custom TTL of the HttpProvider LRUCache. Determines how long an object can live in the cache. Defaults to `DEFAULT_TTL` (30000)
45
+ */
46
+ constructor(endpoint = defaults_js_1.default.HTTP_URL, headers = {}, cacheCapacity, cacheTtl) {
47
+ if (!/^(https|http):\/\//.test(endpoint)) {
48
+ throw new Error(`Endpoint should start with 'http://' or 'https://', received '${endpoint}'`);
49
+ }
50
+ this.#coder = new index_js_1.RpcCoder();
51
+ this.#endpoint = endpoint;
52
+ this.#headers = headers;
53
+ this.#cacheCapacity = cacheCapacity === 0 ? 0 : cacheCapacity || lru_js_1.DEFAULT_CAPACITY;
54
+ const ttl = cacheTtl === undefined ? lru_js_1.DEFAULT_TTL : cacheTtl;
55
+ this.#callCache = new lru_js_1.LRUCache(cacheCapacity === 0 ? 0 : cacheCapacity || lru_js_1.DEFAULT_CAPACITY, ttl);
56
+ this.#ttl = cacheTtl;
57
+ this.#stats = {
58
+ active: { requests: 0, subscriptions: 0 },
59
+ total: { bytesRecv: 0, bytesSent: 0, cached: 0, errors: 0, requests: 0, subscriptions: 0, timeout: 0 }
60
+ };
61
+ }
62
+ /**
63
+ * @summary `true` when this provider supports subscriptions
64
+ */
65
+ get hasSubscriptions() {
66
+ return !!false;
67
+ }
68
+ /**
69
+ * @description Returns a clone of the object
70
+ */
71
+ clone() {
72
+ return new HttpProvider(this.#endpoint, this.#headers);
73
+ }
74
+ /**
75
+ * @description Manually connect from the connection
76
+ */
77
+ async connect() {
78
+ // noop
79
+ }
80
+ /**
81
+ * @description Manually disconnect from the connection
82
+ */
83
+ async disconnect() {
84
+ // noop
85
+ }
86
+ /**
87
+ * @description Returns the connection stats
88
+ */
89
+ get stats() {
90
+ return this.#stats;
91
+ }
92
+ /**
93
+ * @description Returns the connection stats
94
+ */
95
+ get ttl() {
96
+ return this.#ttl;
97
+ }
98
+ /**
99
+ * @summary `true` when this provider supports clone()
100
+ */
101
+ get isClonable() {
102
+ return !!true;
103
+ }
104
+ /**
105
+ * @summary Whether the node is connected or not.
106
+ * @return {boolean} true if connected
107
+ */
108
+ get isConnected() {
109
+ return !!true;
110
+ }
111
+ /**
112
+ * @summary Events are not supported with the HttpProvider, see [[WsProvider]].
113
+ * @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
114
+ */
115
+ on(_type, _sub) {
116
+ l.error('HTTP Provider does not have \'on\' emitters, use WebSockets instead');
117
+ return util_1.noop;
118
+ }
119
+ /**
120
+ * @summary Send HTTP POST Request with Body to configured HTTP Endpoint.
121
+ */
122
+ async send(method, params, isCacheable) {
123
+ this.#stats.total.requests++;
124
+ const [, body] = this.#coder.encodeJson(method, params);
125
+ if (this.#cacheCapacity === 0) {
126
+ return this.#send(body);
127
+ }
128
+ const cacheKey = isCacheable ? `${method}::${(0, util_1.stringify)(params)}` : '';
129
+ let resultPromise = isCacheable
130
+ ? this.#callCache.get(cacheKey)
131
+ : null;
132
+ if (!resultPromise) {
133
+ resultPromise = this.#send(body);
134
+ if (isCacheable) {
135
+ this.#callCache.set(cacheKey, resultPromise);
136
+ }
137
+ }
138
+ else {
139
+ this.#stats.total.cached++;
140
+ }
141
+ return resultPromise;
142
+ }
143
+ async #send(body) {
144
+ this.#stats.active.requests++;
145
+ this.#stats.total.bytesSent += body.length;
146
+ try {
147
+ const response = await (0, x_fetch_1.fetch)(this.#endpoint, {
148
+ body,
149
+ headers: {
150
+ Accept: 'application/json',
151
+ 'Content-Length': `${body.length}`,
152
+ 'Content-Type': 'application/json',
153
+ ...this.#headers
154
+ },
155
+ method: 'POST'
156
+ });
157
+ if (!response.ok) {
158
+ throw new Error(`[${response.status}]: ${response.statusText}`);
159
+ }
160
+ const result = await response.text();
161
+ this.#stats.total.bytesRecv += result.length;
162
+ const decoded = this.#coder.decodeResponse(JSON.parse(result));
163
+ this.#stats.active.requests--;
164
+ return decoded;
165
+ }
166
+ catch (e) {
167
+ this.#stats.active.requests--;
168
+ this.#stats.total.errors++;
169
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
170
+ const { method, params } = JSON.parse(body);
171
+ const rpcError = e;
172
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
173
+ const failedRequest = `\nFailed HTTP Request: ${JSON.stringify({ method, params })}`;
174
+ // Provide HTTP Request alongside the error
175
+ rpcError.message = `${rpcError.message}${failedRequest}`;
176
+ throw rpcError;
177
+ }
178
+ }
179
+ /**
180
+ * @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
181
+ */
182
+ // eslint-disable-next-line @typescript-eslint/require-await
183
+ async subscribe(_types, _method, _params, _cb) {
184
+ l.error(ERROR_SUBSCRIBE);
185
+ throw new Error(ERROR_SUBSCRIBE);
186
+ }
187
+ /**
188
+ * @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
189
+ */
190
+ // eslint-disable-next-line @typescript-eslint/require-await
191
+ async unsubscribe(_type, _method, _id) {
192
+ l.error(ERROR_SUBSCRIBE);
193
+ throw new Error(ERROR_SUBSCRIBE);
194
+ }
195
+ }
196
+ exports.HttpProvider = HttpProvider;
@@ -0,0 +1,7 @@
1
+ import type { Logger } from '@pezkuwi/util/types';
2
+ import type { RpcCoder } from '../coder/index.js';
3
+ export interface HttpState {
4
+ coder: RpcCoder;
5
+ endpoint: string;
6
+ l: Logger;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import './packageDetect.js';
2
+ export * from './bundle.js';
package/cjs/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ require("./packageDetect.js");
5
+ tslib_1.__exportStar(require("./bundle.js"), exports);
package/cjs/lru.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ export declare const DEFAULT_CAPACITY = 1024;
2
+ export declare const DEFAULT_TTL = 30000;
3
+ export declare class LRUCache {
4
+ #private;
5
+ readonly capacity: number;
6
+ constructor(capacity?: number, ttl?: number | null);
7
+ get ttl(): number | null;
8
+ get length(): number;
9
+ get lengthData(): number;
10
+ get lengthRefs(): number;
11
+ entries(): [string, unknown][];
12
+ keys(): string[];
13
+ get<T>(key: string): T | null;
14
+ set<T>(key: string, value: T): void;
15
+ }
package/cjs/lru.js ADDED
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LRUCache = exports.DEFAULT_TTL = exports.DEFAULT_CAPACITY = void 0;
4
+ exports.DEFAULT_CAPACITY = 1024;
5
+ exports.DEFAULT_TTL = 30000; // 30 seconds
6
+ const MAX_TTL = 1800_000; // 30 minutes
7
+ const DISABLED_TTL = 31_536_000_000;
8
+ class LRUNode {
9
+ key;
10
+ #expires;
11
+ #ttl;
12
+ createdAt;
13
+ next;
14
+ prev;
15
+ constructor(key, ttl) {
16
+ this.key = key;
17
+ this.#ttl = ttl;
18
+ this.#expires = Date.now() + ttl;
19
+ this.createdAt = Date.now();
20
+ this.next = this.prev = this;
21
+ }
22
+ refresh() {
23
+ this.#expires = Date.now() + this.#ttl;
24
+ }
25
+ get expiry() {
26
+ return this.#expires;
27
+ }
28
+ }
29
+ class LRUCache {
30
+ capacity;
31
+ #data = new Map();
32
+ #refs = new Map();
33
+ #length = 0;
34
+ #head;
35
+ #tail;
36
+ #ttl;
37
+ constructor(capacity = exports.DEFAULT_CAPACITY, ttl = exports.DEFAULT_TTL) {
38
+ // Validate capacity
39
+ if (!Number.isInteger(capacity) || capacity < 0) {
40
+ throw new Error(`LRUCache initialization error: 'capacity' must be a non-negative integer. Received: ${capacity}`);
41
+ }
42
+ // Validate ttl
43
+ if (ttl !== null && (!Number.isFinite(ttl) || ttl < 0 || ttl > MAX_TTL)) {
44
+ throw new Error(`LRUCache initialization error: 'ttl' must be between 0 and ${MAX_TTL} ms or null to disable. Received: ${ttl}`);
45
+ }
46
+ this.capacity = capacity;
47
+ ttl ? this.#ttl = ttl : this.#ttl = DISABLED_TTL;
48
+ this.#head = this.#tail = new LRUNode('<empty>', this.#ttl);
49
+ }
50
+ get ttl() {
51
+ return this.#ttl;
52
+ }
53
+ get length() {
54
+ return this.#length;
55
+ }
56
+ get lengthData() {
57
+ return this.#data.size;
58
+ }
59
+ get lengthRefs() {
60
+ return this.#refs.size;
61
+ }
62
+ entries() {
63
+ const keys = this.keys();
64
+ const count = keys.length;
65
+ const entries = new Array(count);
66
+ for (let i = 0; i < count; i++) {
67
+ const key = keys[i];
68
+ entries[i] = [key, this.#data.get(key)];
69
+ }
70
+ return entries;
71
+ }
72
+ keys() {
73
+ const keys = [];
74
+ if (this.#length) {
75
+ let curr = this.#head;
76
+ while (curr !== this.#tail) {
77
+ keys.push(curr.key);
78
+ curr = curr.next;
79
+ }
80
+ keys.push(curr.key);
81
+ }
82
+ return keys;
83
+ }
84
+ get(key) {
85
+ const data = this.#data.get(key);
86
+ if (data) {
87
+ this.#toHead(key);
88
+ // Evict TTL once data is refreshed
89
+ this.#evictTTL();
90
+ return data;
91
+ }
92
+ this.#evictTTL();
93
+ return null;
94
+ }
95
+ set(key, value) {
96
+ if (this.#data.has(key)) {
97
+ this.#toHead(key);
98
+ }
99
+ else {
100
+ const node = new LRUNode(key, this.#ttl);
101
+ this.#refs.set(node.key, node);
102
+ if (this.length === 0) {
103
+ this.#head = this.#tail = node;
104
+ }
105
+ else {
106
+ this.#head.prev = node;
107
+ node.next = this.#head;
108
+ this.#head = node;
109
+ }
110
+ if (this.#length === this.capacity) {
111
+ this.#data.delete(this.#tail.key);
112
+ this.#refs.delete(this.#tail.key);
113
+ this.#tail = this.#tail.prev;
114
+ this.#tail.next = this.#head;
115
+ }
116
+ else {
117
+ this.#length += 1;
118
+ }
119
+ }
120
+ // Evict TTL once data is refreshed or added
121
+ this.#evictTTL();
122
+ this.#data.set(key, value);
123
+ }
124
+ #evictTTL() {
125
+ // Find last node to keep
126
+ // traverse map to find the expired nodes
127
+ while (this.#tail.expiry && this.#tail.expiry < Date.now() && this.#length > 0) {
128
+ this.#refs.delete(this.#tail.key);
129
+ this.#data.delete(this.#tail.key);
130
+ this.#length -= 1;
131
+ this.#tail = this.#tail.prev;
132
+ this.#tail.next = this.#head;
133
+ }
134
+ if (this.#length === 0) {
135
+ this.#head = this.#tail = new LRUNode('<empty>', this.#ttl);
136
+ }
137
+ }
138
+ #toHead(key) {
139
+ const ref = this.#refs.get(key);
140
+ if (ref && ref !== this.#head) {
141
+ ref.refresh();
142
+ ref.prev.next = ref.next;
143
+ ref.next.prev = ref.prev;
144
+ ref.next = this.#head;
145
+ this.#head.prev = ref;
146
+ this.#head = ref;
147
+ }
148
+ }
149
+ }
150
+ exports.LRUCache = LRUCache;
@@ -0,0 +1,35 @@
1
+ import type { Registry } from '@pezkuwi/types/types';
2
+ import type { ProviderInterface, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '../types.js';
3
+ import type { MockStateSubscriptions } from './types.js';
4
+ /**
5
+ * A mock provider mainly used for testing.
6
+ * @return {ProviderInterface} The mock provider
7
+ * @internal
8
+ */
9
+ export declare class MockProvider implements ProviderInterface {
10
+ private db;
11
+ private emitter;
12
+ private intervalId?;
13
+ isUpdating: boolean;
14
+ private registry;
15
+ private prevNumber;
16
+ private requests;
17
+ subscriptions: MockStateSubscriptions;
18
+ private subscriptionId;
19
+ private subscriptionMap;
20
+ constructor(registry: Registry);
21
+ get hasSubscriptions(): boolean;
22
+ clone(): MockProvider;
23
+ connect(): Promise<void>;
24
+ disconnect(): Promise<void>;
25
+ get isClonable(): boolean;
26
+ get isConnected(): boolean;
27
+ on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
28
+ send<T = any>(method: string, params: unknown[]): Promise<T>;
29
+ subscribe(_type: string, method: string, ...params: unknown[]): Promise<number>;
30
+ unsubscribe(_type: string, _method: string, id: number): Promise<boolean>;
31
+ private init;
32
+ private makeBlockHeader;
33
+ private setStateBn;
34
+ private updateSubs;
35
+ }