@pezkuwi/rpc-provider 16.5.5 → 16.5.6

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 (125) 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/{build/bundle.d.ts → bundle.d.ts} +1 -1
  7. package/{src/bundle.ts → bundle.js} +1 -4
  8. package/cjs/bizinikiwi-connect/Health.d.ts +7 -0
  9. package/cjs/bizinikiwi-connect/Health.js +264 -0
  10. package/cjs/bizinikiwi-connect/index.d.ts +22 -0
  11. package/cjs/bizinikiwi-connect/index.js +323 -0
  12. package/cjs/bizinikiwi-connect/types.d.ts +12 -0
  13. package/cjs/bizinikiwi-connect/types.js +2 -0
  14. package/cjs/bundle.d.ts +5 -0
  15. package/cjs/bundle.js +14 -0
  16. package/cjs/coder/error.js +53 -0
  17. package/cjs/coder/index.js +63 -0
  18. package/cjs/defaults.js +8 -0
  19. package/{build → cjs}/http/index.d.ts +1 -1
  20. package/cjs/http/index.js +196 -0
  21. package/cjs/http/types.js +2 -0
  22. package/cjs/index.js +5 -0
  23. package/cjs/lru.js +150 -0
  24. package/cjs/mock/index.js +196 -0
  25. package/cjs/mock/mockHttp.js +17 -0
  26. package/cjs/mock/mockWs.js +47 -0
  27. package/cjs/mock/types.js +2 -0
  28. package/cjs/package.json +3 -0
  29. package/cjs/packageDetect.d.ts +1 -0
  30. package/cjs/packageDetect.js +6 -0
  31. package/cjs/packageInfo.js +4 -0
  32. package/cjs/types.js +2 -0
  33. package/cjs/ws/errors.js +41 -0
  34. package/{build → cjs}/ws/index.d.ts +1 -1
  35. package/cjs/ws/index.js +529 -0
  36. package/coder/error.d.ts +29 -0
  37. package/coder/error.js +50 -0
  38. package/coder/index.d.ts +8 -0
  39. package/coder/index.js +58 -0
  40. package/defaults.d.ts +5 -0
  41. package/defaults.js +6 -0
  42. package/http/index.d.ts +81 -0
  43. package/http/index.js +191 -0
  44. package/http/types.d.ts +7 -0
  45. package/http/types.js +1 -0
  46. package/index.d.ts +2 -0
  47. package/index.js +2 -0
  48. package/lru.d.ts +15 -0
  49. package/lru.js +146 -0
  50. package/mock/index.d.ts +35 -0
  51. package/mock/index.js +191 -0
  52. package/mock/mockHttp.d.ts +9 -0
  53. package/mock/mockHttp.js +12 -0
  54. package/mock/mockWs.d.ts +26 -0
  55. package/mock/mockWs.js +43 -0
  56. package/mock/types.d.ts +23 -0
  57. package/mock/types.js +1 -0
  58. package/package.json +316 -15
  59. package/packageDetect.d.ts +1 -0
  60. package/packageDetect.js +4 -0
  61. package/packageInfo.d.ts +6 -0
  62. package/packageInfo.js +1 -0
  63. package/types.d.ts +85 -0
  64. package/types.js +1 -0
  65. package/ws/errors.d.ts +1 -0
  66. package/ws/errors.js +38 -0
  67. package/ws/index.d.ts +121 -0
  68. package/ws/index.js +524 -0
  69. package/src/coder/decodeResponse.spec.ts +0 -70
  70. package/src/coder/encodeJson.spec.ts +0 -20
  71. package/src/coder/encodeObject.spec.ts +0 -25
  72. package/src/coder/error.spec.ts +0 -111
  73. package/src/coder/error.ts +0 -66
  74. package/src/coder/index.ts +0 -88
  75. package/src/defaults.ts +0 -10
  76. package/src/http/index.spec.ts +0 -72
  77. package/src/http/index.ts +0 -238
  78. package/src/http/send.spec.ts +0 -61
  79. package/src/http/types.ts +0 -11
  80. package/src/index.ts +0 -6
  81. package/src/lru.spec.ts +0 -74
  82. package/src/lru.ts +0 -197
  83. package/src/mock/index.ts +0 -259
  84. package/src/mock/mockHttp.ts +0 -35
  85. package/src/mock/mockWs.ts +0 -92
  86. package/src/mock/on.spec.ts +0 -43
  87. package/src/mock/send.spec.ts +0 -38
  88. package/src/mock/subscribe.spec.ts +0 -81
  89. package/src/mock/types.ts +0 -36
  90. package/src/mock/unsubscribe.spec.ts +0 -57
  91. package/src/mod.ts +0 -4
  92. package/src/packageDetect.ts +0 -12
  93. package/src/packageInfo.ts +0 -6
  94. package/src/substrate-connect/Health.ts +0 -325
  95. package/src/substrate-connect/index.spec.ts +0 -638
  96. package/src/substrate-connect/index.ts +0 -415
  97. package/src/substrate-connect/types.ts +0 -16
  98. package/src/types.ts +0 -101
  99. package/src/ws/connect.spec.ts +0 -167
  100. package/src/ws/errors.ts +0 -41
  101. package/src/ws/index.spec.ts +0 -97
  102. package/src/ws/index.ts +0 -652
  103. package/src/ws/send.spec.ts +0 -126
  104. package/src/ws/state.spec.ts +0 -20
  105. package/src/ws/subscribe.spec.ts +0 -68
  106. package/src/ws/unsubscribe.spec.ts +0 -100
  107. package/tsconfig.build.json +0 -17
  108. package/tsconfig.build.tsbuildinfo +0 -1
  109. package/tsconfig.spec.json +0 -18
  110. /package/{build/substrate-connect → bizinikiwi-connect}/Health.d.ts +0 -0
  111. /package/{build/substrate-connect → bizinikiwi-connect}/types.d.ts +0 -0
  112. /package/{build/packageDetect.d.ts → bizinikiwi-connect/types.js} +0 -0
  113. /package/{build → cjs}/coder/error.d.ts +0 -0
  114. /package/{build → cjs}/coder/index.d.ts +0 -0
  115. /package/{build → cjs}/defaults.d.ts +0 -0
  116. /package/{build → cjs}/http/types.d.ts +0 -0
  117. /package/{build → cjs}/index.d.ts +0 -0
  118. /package/{build → cjs}/lru.d.ts +0 -0
  119. /package/{build → cjs}/mock/index.d.ts +0 -0
  120. /package/{build → cjs}/mock/mockHttp.d.ts +0 -0
  121. /package/{build → cjs}/mock/mockWs.d.ts +0 -0
  122. /package/{build → cjs}/mock/types.d.ts +0 -0
  123. /package/{build → cjs}/packageInfo.d.ts +0 -0
  124. /package/{build → cjs}/types.d.ts +0 -0
  125. /package/{build → cjs}/ws/errors.d.ts +0 -0
package/defaults.js ADDED
@@ -0,0 +1,6 @@
1
+ const HTTP_URL = 'http://127.0.0.1:9933';
2
+ const WS_URL = 'ws://127.0.0.1:9944';
3
+ export default {
4
+ HTTP_URL,
5
+ WS_URL
6
+ };
@@ -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
+ }
package/http/index.js ADDED
@@ -0,0 +1,191 @@
1
+ import { logger, noop, stringify } from '@pezkuwi/util';
2
+ import { fetch } from '@pezkuwi/x-fetch';
3
+ import { RpcCoder } from '../coder/index.js';
4
+ import defaults from '../defaults.js';
5
+ import { DEFAULT_CAPACITY, DEFAULT_TTL, LRUCache } from '../lru.js';
6
+ const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead';
7
+ const l = logger('api-http');
8
+ /**
9
+ * # @pezkuwi/rpc-provider
10
+ *
11
+ * @name HttpProvider
12
+ *
13
+ * @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]].
14
+ *
15
+ * @example
16
+ * <BR>
17
+ *
18
+ * ```javascript
19
+ * import Api from '@pezkuwi/api/promise';
20
+ * import { HttpProvider } from '@pezkuwi/rpc-provider';
21
+ *
22
+ * const provider = new HttpProvider('http://127.0.0.1:9933');
23
+ * const api = new Api(provider);
24
+ * ```
25
+ *
26
+ * @see [[WsProvider]]
27
+ */
28
+ export class HttpProvider {
29
+ #callCache;
30
+ #cacheCapacity;
31
+ #coder;
32
+ #endpoint;
33
+ #headers;
34
+ #stats;
35
+ #ttl;
36
+ /**
37
+ * @param {string} endpoint The endpoint url starting with http://
38
+ * @param {Record<string, string>} headers The headers provided to the underlying Http Endpoint
39
+ * @param {number} [cacheCapacity] Custom size of the HttpProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
40
+ * @param {number} [cacheTtl] Custom TTL of the HttpProvider LRUCache. Determines how long an object can live in the cache. Defaults to `DEFAULT_TTL` (30000)
41
+ */
42
+ constructor(endpoint = defaults.HTTP_URL, headers = {}, cacheCapacity, cacheTtl) {
43
+ if (!/^(https|http):\/\//.test(endpoint)) {
44
+ throw new Error(`Endpoint should start with 'http://' or 'https://', received '${endpoint}'`);
45
+ }
46
+ this.#coder = new RpcCoder();
47
+ this.#endpoint = endpoint;
48
+ this.#headers = headers;
49
+ this.#cacheCapacity = cacheCapacity === 0 ? 0 : cacheCapacity || DEFAULT_CAPACITY;
50
+ const ttl = cacheTtl === undefined ? DEFAULT_TTL : cacheTtl;
51
+ this.#callCache = new LRUCache(cacheCapacity === 0 ? 0 : cacheCapacity || DEFAULT_CAPACITY, ttl);
52
+ this.#ttl = cacheTtl;
53
+ this.#stats = {
54
+ active: { requests: 0, subscriptions: 0 },
55
+ total: { bytesRecv: 0, bytesSent: 0, cached: 0, errors: 0, requests: 0, subscriptions: 0, timeout: 0 }
56
+ };
57
+ }
58
+ /**
59
+ * @summary `true` when this provider supports subscriptions
60
+ */
61
+ get hasSubscriptions() {
62
+ return !!false;
63
+ }
64
+ /**
65
+ * @description Returns a clone of the object
66
+ */
67
+ clone() {
68
+ return new HttpProvider(this.#endpoint, this.#headers);
69
+ }
70
+ /**
71
+ * @description Manually connect from the connection
72
+ */
73
+ async connect() {
74
+ // noop
75
+ }
76
+ /**
77
+ * @description Manually disconnect from the connection
78
+ */
79
+ async disconnect() {
80
+ // noop
81
+ }
82
+ /**
83
+ * @description Returns the connection stats
84
+ */
85
+ get stats() {
86
+ return this.#stats;
87
+ }
88
+ /**
89
+ * @description Returns the connection stats
90
+ */
91
+ get ttl() {
92
+ return this.#ttl;
93
+ }
94
+ /**
95
+ * @summary `true` when this provider supports clone()
96
+ */
97
+ get isClonable() {
98
+ return !!true;
99
+ }
100
+ /**
101
+ * @summary Whether the node is connected or not.
102
+ * @return {boolean} true if connected
103
+ */
104
+ get isConnected() {
105
+ return !!true;
106
+ }
107
+ /**
108
+ * @summary Events are not supported with the HttpProvider, see [[WsProvider]].
109
+ * @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
110
+ */
111
+ on(_type, _sub) {
112
+ l.error('HTTP Provider does not have \'on\' emitters, use WebSockets instead');
113
+ return noop;
114
+ }
115
+ /**
116
+ * @summary Send HTTP POST Request with Body to configured HTTP Endpoint.
117
+ */
118
+ async send(method, params, isCacheable) {
119
+ this.#stats.total.requests++;
120
+ const [, body] = this.#coder.encodeJson(method, params);
121
+ if (this.#cacheCapacity === 0) {
122
+ return this.#send(body);
123
+ }
124
+ const cacheKey = isCacheable ? `${method}::${stringify(params)}` : '';
125
+ let resultPromise = isCacheable
126
+ ? this.#callCache.get(cacheKey)
127
+ : null;
128
+ if (!resultPromise) {
129
+ resultPromise = this.#send(body);
130
+ if (isCacheable) {
131
+ this.#callCache.set(cacheKey, resultPromise);
132
+ }
133
+ }
134
+ else {
135
+ this.#stats.total.cached++;
136
+ }
137
+ return resultPromise;
138
+ }
139
+ async #send(body) {
140
+ this.#stats.active.requests++;
141
+ this.#stats.total.bytesSent += body.length;
142
+ try {
143
+ const response = await fetch(this.#endpoint, {
144
+ body,
145
+ headers: {
146
+ Accept: 'application/json',
147
+ 'Content-Length': `${body.length}`,
148
+ 'Content-Type': 'application/json',
149
+ ...this.#headers
150
+ },
151
+ method: 'POST'
152
+ });
153
+ if (!response.ok) {
154
+ throw new Error(`[${response.status}]: ${response.statusText}`);
155
+ }
156
+ const result = await response.text();
157
+ this.#stats.total.bytesRecv += result.length;
158
+ const decoded = this.#coder.decodeResponse(JSON.parse(result));
159
+ this.#stats.active.requests--;
160
+ return decoded;
161
+ }
162
+ catch (e) {
163
+ this.#stats.active.requests--;
164
+ this.#stats.total.errors++;
165
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
166
+ const { method, params } = JSON.parse(body);
167
+ const rpcError = e;
168
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
169
+ const failedRequest = `\nFailed HTTP Request: ${JSON.stringify({ method, params })}`;
170
+ // Provide HTTP Request alongside the error
171
+ rpcError.message = `${rpcError.message}${failedRequest}`;
172
+ throw rpcError;
173
+ }
174
+ }
175
+ /**
176
+ * @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
177
+ */
178
+ // eslint-disable-next-line @typescript-eslint/require-await
179
+ async subscribe(_types, _method, _params, _cb) {
180
+ l.error(ERROR_SUBSCRIBE);
181
+ throw new Error(ERROR_SUBSCRIBE);
182
+ }
183
+ /**
184
+ * @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
185
+ */
186
+ // eslint-disable-next-line @typescript-eslint/require-await
187
+ async unsubscribe(_type, _method, _id) {
188
+ l.error(ERROR_SUBSCRIBE);
189
+ throw new Error(ERROR_SUBSCRIBE);
190
+ }
191
+ }
@@ -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
+ }
package/http/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import './packageDetect.js';
2
+ export * from './bundle.js';
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import './packageDetect.js';
2
+ export * from './bundle.js';
package/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/lru.js ADDED
@@ -0,0 +1,146 @@
1
+ export const DEFAULT_CAPACITY = 1024;
2
+ export const DEFAULT_TTL = 30000; // 30 seconds
3
+ const MAX_TTL = 1800_000; // 30 minutes
4
+ const DISABLED_TTL = 31_536_000_000;
5
+ class LRUNode {
6
+ key;
7
+ #expires;
8
+ #ttl;
9
+ createdAt;
10
+ next;
11
+ prev;
12
+ constructor(key, ttl) {
13
+ this.key = key;
14
+ this.#ttl = ttl;
15
+ this.#expires = Date.now() + ttl;
16
+ this.createdAt = Date.now();
17
+ this.next = this.prev = this;
18
+ }
19
+ refresh() {
20
+ this.#expires = Date.now() + this.#ttl;
21
+ }
22
+ get expiry() {
23
+ return this.#expires;
24
+ }
25
+ }
26
+ export class LRUCache {
27
+ capacity;
28
+ #data = new Map();
29
+ #refs = new Map();
30
+ #length = 0;
31
+ #head;
32
+ #tail;
33
+ #ttl;
34
+ constructor(capacity = DEFAULT_CAPACITY, ttl = DEFAULT_TTL) {
35
+ // Validate capacity
36
+ if (!Number.isInteger(capacity) || capacity < 0) {
37
+ throw new Error(`LRUCache initialization error: 'capacity' must be a non-negative integer. Received: ${capacity}`);
38
+ }
39
+ // Validate ttl
40
+ if (ttl !== null && (!Number.isFinite(ttl) || ttl < 0 || ttl > MAX_TTL)) {
41
+ throw new Error(`LRUCache initialization error: 'ttl' must be between 0 and ${MAX_TTL} ms or null to disable. Received: ${ttl}`);
42
+ }
43
+ this.capacity = capacity;
44
+ ttl ? this.#ttl = ttl : this.#ttl = DISABLED_TTL;
45
+ this.#head = this.#tail = new LRUNode('<empty>', this.#ttl);
46
+ }
47
+ get ttl() {
48
+ return this.#ttl;
49
+ }
50
+ get length() {
51
+ return this.#length;
52
+ }
53
+ get lengthData() {
54
+ return this.#data.size;
55
+ }
56
+ get lengthRefs() {
57
+ return this.#refs.size;
58
+ }
59
+ entries() {
60
+ const keys = this.keys();
61
+ const count = keys.length;
62
+ const entries = new Array(count);
63
+ for (let i = 0; i < count; i++) {
64
+ const key = keys[i];
65
+ entries[i] = [key, this.#data.get(key)];
66
+ }
67
+ return entries;
68
+ }
69
+ keys() {
70
+ const keys = [];
71
+ if (this.#length) {
72
+ let curr = this.#head;
73
+ while (curr !== this.#tail) {
74
+ keys.push(curr.key);
75
+ curr = curr.next;
76
+ }
77
+ keys.push(curr.key);
78
+ }
79
+ return keys;
80
+ }
81
+ get(key) {
82
+ const data = this.#data.get(key);
83
+ if (data) {
84
+ this.#toHead(key);
85
+ // Evict TTL once data is refreshed
86
+ this.#evictTTL();
87
+ return data;
88
+ }
89
+ this.#evictTTL();
90
+ return null;
91
+ }
92
+ set(key, value) {
93
+ if (this.#data.has(key)) {
94
+ this.#toHead(key);
95
+ }
96
+ else {
97
+ const node = new LRUNode(key, this.#ttl);
98
+ this.#refs.set(node.key, node);
99
+ if (this.length === 0) {
100
+ this.#head = this.#tail = node;
101
+ }
102
+ else {
103
+ this.#head.prev = node;
104
+ node.next = this.#head;
105
+ this.#head = node;
106
+ }
107
+ if (this.#length === this.capacity) {
108
+ this.#data.delete(this.#tail.key);
109
+ this.#refs.delete(this.#tail.key);
110
+ this.#tail = this.#tail.prev;
111
+ this.#tail.next = this.#head;
112
+ }
113
+ else {
114
+ this.#length += 1;
115
+ }
116
+ }
117
+ // Evict TTL once data is refreshed or added
118
+ this.#evictTTL();
119
+ this.#data.set(key, value);
120
+ }
121
+ #evictTTL() {
122
+ // Find last node to keep
123
+ // traverse map to find the expired nodes
124
+ while (this.#tail.expiry && this.#tail.expiry < Date.now() && this.#length > 0) {
125
+ this.#refs.delete(this.#tail.key);
126
+ this.#data.delete(this.#tail.key);
127
+ this.#length -= 1;
128
+ this.#tail = this.#tail.prev;
129
+ this.#tail.next = this.#head;
130
+ }
131
+ if (this.#length === 0) {
132
+ this.#head = this.#tail = new LRUNode('<empty>', this.#ttl);
133
+ }
134
+ }
135
+ #toHead(key) {
136
+ const ref = this.#refs.get(key);
137
+ if (ref && ref !== this.#head) {
138
+ ref.refresh();
139
+ ref.prev.next = ref.next;
140
+ ref.next.prev = ref.prev;
141
+ ref.next = this.#head;
142
+ this.#head.prev = ref;
143
+ this.#head = ref;
144
+ }
145
+ }
146
+ }
@@ -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
+ }
package/mock/index.js ADDED
@@ -0,0 +1,191 @@
1
+ import { EventEmitter } from 'eventemitter3';
2
+ import { createTestKeyring } from '@pezkuwi/keyring/testing';
3
+ import { decorateStorage, Metadata } from '@pezkuwi/types';
4
+ import jsonrpc from '@pezkuwi/types/interfaces/jsonrpc';
5
+ import rpcHeader from '@pezkuwi/types-support/json/Header.004.json' assert { type: 'json' };
6
+ import rpcSignedBlock from '@pezkuwi/types-support/json/SignedBlock.004.immortal.json' assert { type: 'json' };
7
+ import rpcMetadata from '@pezkuwi/types-support/metadata/static-bizinikiwi';
8
+ import { BN, bnToU8a, logger, u8aToHex } from '@pezkuwi/util';
9
+ import { randomAsU8a } from '@pezkuwi/util-crypto';
10
+ const INTERVAL = 1000;
11
+ const SUBSCRIPTIONS = Array.prototype.concat.apply([], Object.values(jsonrpc).map((section) => Object
12
+ .values(section)
13
+ .filter(({ isSubscription }) => isSubscription)
14
+ .map(({ jsonrpc }) => jsonrpc)
15
+ .concat('chain_subscribeNewHead')));
16
+ const keyring = createTestKeyring({ type: 'ed25519' });
17
+ const l = logger('api-mock');
18
+ /**
19
+ * A mock provider mainly used for testing.
20
+ * @return {ProviderInterface} The mock provider
21
+ * @internal
22
+ */
23
+ export class MockProvider {
24
+ db = {};
25
+ emitter = new EventEmitter();
26
+ intervalId;
27
+ isUpdating = true;
28
+ registry;
29
+ prevNumber = new BN(-1);
30
+ requests = {
31
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
32
+ chain_getBlock: () => this.registry.createType('SignedBlock', rpcSignedBlock.result).toJSON(),
33
+ chain_getBlockHash: () => '0x1234000000000000000000000000000000000000000000000000000000000000',
34
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
35
+ chain_getFinalizedHead: () => this.registry.createType('Header', rpcHeader.result).hash,
36
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
37
+ chain_getHeader: () => this.registry.createType('Header', rpcHeader.result).toJSON(),
38
+ rpc_methods: () => this.registry.createType('RpcMethods').toJSON(),
39
+ state_getKeys: () => [],
40
+ state_getKeysPaged: () => [],
41
+ state_getMetadata: () => rpcMetadata,
42
+ state_getRuntimeVersion: () => this.registry.createType('RuntimeVersion').toHex(),
43
+ state_getStorage: (storage, [key]) => u8aToHex(storage[key]),
44
+ system_chain: () => 'mockChain',
45
+ system_health: () => ({}),
46
+ system_name: () => 'mockClient',
47
+ system_properties: () => ({ ss58Format: 42 }),
48
+ system_upgradedToTripleRefCount: () => this.registry.createType('bool', true),
49
+ system_version: () => '9.8.7',
50
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return, sort-keys
51
+ dev_echo: (_, params) => params
52
+ };
53
+ subscriptions = SUBSCRIPTIONS.reduce((subs, name) => {
54
+ subs[name] = {
55
+ callbacks: {},
56
+ lastValue: null
57
+ };
58
+ return subs;
59
+ }, {});
60
+ subscriptionId = 0;
61
+ subscriptionMap = {};
62
+ constructor(registry) {
63
+ this.registry = registry;
64
+ this.init();
65
+ }
66
+ get hasSubscriptions() {
67
+ return !!true;
68
+ }
69
+ clone() {
70
+ throw new Error('Unimplemented');
71
+ }
72
+ async connect() {
73
+ // noop
74
+ }
75
+ // eslint-disable-next-line @typescript-eslint/require-await
76
+ async disconnect() {
77
+ if (this.intervalId) {
78
+ clearInterval(this.intervalId);
79
+ this.intervalId = null;
80
+ }
81
+ }
82
+ get isClonable() {
83
+ return !!false;
84
+ }
85
+ get isConnected() {
86
+ return !!true;
87
+ }
88
+ on(type, sub) {
89
+ this.emitter.on(type, sub);
90
+ return () => {
91
+ this.emitter.removeListener(type, sub);
92
+ };
93
+ }
94
+ // eslint-disable-next-line @typescript-eslint/require-await
95
+ async send(method, params) {
96
+ l.debug(() => ['send', method, params]);
97
+ if (!this.requests[method]) {
98
+ throw new Error(`provider.send: Invalid method '${method}'`);
99
+ }
100
+ return this.requests[method](this.db, params);
101
+ }
102
+ // eslint-disable-next-line @typescript-eslint/require-await
103
+ async subscribe(_type, method, ...params) {
104
+ l.debug(() => ['subscribe', method, params]);
105
+ if (!this.subscriptions[method]) {
106
+ throw new Error(`provider.subscribe: Invalid method '${method}'`);
107
+ }
108
+ const callback = params.pop();
109
+ const id = ++this.subscriptionId;
110
+ this.subscriptions[method].callbacks[id] = callback;
111
+ this.subscriptionMap[id] = method;
112
+ if (this.subscriptions[method].lastValue !== null) {
113
+ callback(null, this.subscriptions[method].lastValue);
114
+ }
115
+ return id;
116
+ }
117
+ // eslint-disable-next-line @typescript-eslint/require-await
118
+ async unsubscribe(_type, _method, id) {
119
+ const sub = this.subscriptionMap[id];
120
+ l.debug(() => ['unsubscribe', id, sub]);
121
+ if (!sub) {
122
+ throw new Error(`Unable to find subscription for ${id}`);
123
+ }
124
+ delete this.subscriptionMap[id];
125
+ delete this.subscriptions[sub].callbacks[id];
126
+ return true;
127
+ }
128
+ init() {
129
+ const emitEvents = ['connected', 'disconnected'];
130
+ let emitIndex = 0;
131
+ let newHead = this.makeBlockHeader();
132
+ let counter = -1;
133
+ const metadata = new Metadata(this.registry, rpcMetadata);
134
+ this.registry.setMetadata(metadata);
135
+ const query = decorateStorage(this.registry, metadata.asLatest, metadata.version);
136
+ // Do something every 1 seconds
137
+ this.intervalId = setInterval(() => {
138
+ if (!this.isUpdating) {
139
+ return;
140
+ }
141
+ // create a new header (next block)
142
+ newHead = this.makeBlockHeader();
143
+ // increment the balances and nonce for each account
144
+ keyring.getPairs().forEach(({ publicKey }, index) => {
145
+ this.setStateBn(query['system']['account'](publicKey), newHead.number.toBn().addn(index));
146
+ });
147
+ // set the timestamp for the current block
148
+ this.setStateBn(query['timestamp']['now'](), Math.floor(Date.now() / 1000));
149
+ this.updateSubs('chain_subscribeNewHead', newHead);
150
+ // We emit connected/disconnected at intervals
151
+ if (++counter % 2 === 1) {
152
+ if (++emitIndex === emitEvents.length) {
153
+ emitIndex = 0;
154
+ }
155
+ this.emitter.emit(emitEvents[emitIndex]);
156
+ }
157
+ }, INTERVAL);
158
+ }
159
+ makeBlockHeader() {
160
+ const blockNumber = this.prevNumber.addn(1);
161
+ const header = this.registry.createType('Header', {
162
+ digest: {
163
+ logs: []
164
+ },
165
+ extrinsicsRoot: randomAsU8a(),
166
+ number: blockNumber,
167
+ parentHash: blockNumber.isZero()
168
+ ? new Uint8Array(32)
169
+ : bnToU8a(this.prevNumber, { bitLength: 256, isLe: false }),
170
+ stateRoot: bnToU8a(blockNumber, { bitLength: 256, isLe: false })
171
+ });
172
+ this.prevNumber = blockNumber;
173
+ return header;
174
+ }
175
+ setStateBn(key, value) {
176
+ this.db[u8aToHex(key)] = bnToU8a(value, { bitLength: 64, isLe: true });
177
+ }
178
+ updateSubs(method, value) {
179
+ this.subscriptions[method].lastValue = value;
180
+ Object
181
+ .values(this.subscriptions[method].callbacks)
182
+ .forEach((cb) => {
183
+ try {
184
+ cb(null, value.toJSON());
185
+ }
186
+ catch (error) {
187
+ l.error(`Error on '${method}' subscription`, error);
188
+ }
189
+ });
190
+ }
191
+ }