@pezkuwi/rpc-provider 16.5.19 → 16.5.21

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 (210) hide show
  1. package/package.json +9 -9
  2. package/build/LICENSE +0 -201
  3. package/build/README.md +0 -68
  4. package/build/bizinikiwi-connect/Health.js +0 -259
  5. package/build/bizinikiwi-connect/index.js +0 -319
  6. package/build/bizinikiwi-connect/types.js +0 -1
  7. package/build/bundle.js +0 -5
  8. package/build/cjs/bizinikiwi-connect/Health.d.ts +0 -7
  9. package/build/cjs/bizinikiwi-connect/Health.js +0 -264
  10. package/build/cjs/bizinikiwi-connect/index.d.ts +0 -22
  11. package/build/cjs/bizinikiwi-connect/index.js +0 -323
  12. package/build/cjs/bizinikiwi-connect/types.d.ts +0 -12
  13. package/build/cjs/bizinikiwi-connect/types.js +0 -2
  14. package/build/cjs/bundle.d.ts +0 -5
  15. package/build/cjs/bundle.js +0 -14
  16. package/build/cjs/coder/error.js +0 -53
  17. package/build/cjs/coder/index.js +0 -63
  18. package/build/cjs/defaults.js +0 -8
  19. package/build/cjs/http/index.js +0 -196
  20. package/build/cjs/http/types.js +0 -2
  21. package/build/cjs/index.js +0 -5
  22. package/build/cjs/lru.js +0 -150
  23. package/build/cjs/mock/index.js +0 -196
  24. package/build/cjs/mock/mockHttp.js +0 -17
  25. package/build/cjs/mock/mockWs.js +0 -47
  26. package/build/cjs/mock/types.js +0 -2
  27. package/build/cjs/packageInfo.js +0 -4
  28. package/build/cjs/types.js +0 -2
  29. package/build/cjs/ws/errors.js +0 -41
  30. package/build/cjs/ws/index.js +0 -529
  31. package/build/coder/error.d.ts +0 -29
  32. package/build/coder/error.js +0 -50
  33. package/build/coder/index.d.ts +0 -8
  34. package/build/coder/index.js +0 -58
  35. package/build/defaults.d.ts +0 -5
  36. package/build/defaults.js +0 -6
  37. package/build/http/index.d.ts +0 -81
  38. package/build/http/index.js +0 -191
  39. package/build/http/types.d.ts +0 -7
  40. package/build/http/types.js +0 -1
  41. package/build/index.d.ts +0 -2
  42. package/build/index.js +0 -2
  43. package/build/lru.d.ts +0 -15
  44. package/build/lru.js +0 -146
  45. package/build/mock/index.d.ts +0 -35
  46. package/build/mock/index.js +0 -191
  47. package/build/mock/mockHttp.d.ts +0 -9
  48. package/build/mock/mockHttp.js +0 -12
  49. package/build/mock/mockWs.d.ts +0 -26
  50. package/build/mock/mockWs.js +0 -43
  51. package/build/mock/types.d.ts +0 -23
  52. package/build/mock/types.js +0 -1
  53. package/build/package.json +0 -344
  54. package/build/packageDetect.d.ts +0 -1
  55. package/build/packageDetect.js +0 -4
  56. package/build/packageInfo.d.ts +0 -6
  57. package/build/packageInfo.js +0 -1
  58. package/build/types.d.ts +0 -85
  59. package/build/types.js +0 -1
  60. package/build/ws/errors.d.ts +0 -1
  61. package/build/ws/errors.js +0 -38
  62. package/build/ws/index.d.ts +0 -121
  63. package/build/ws/index.js +0 -524
  64. package/build-deno/README.md +0 -66
  65. package/build-deno/bizinikiwi-connect/Health.ts +0 -323
  66. package/build-deno/bizinikiwi-connect/index.ts +0 -417
  67. package/build-deno/bizinikiwi-connect/types.ts +0 -14
  68. package/build-deno/bundle.ts +0 -6
  69. package/build-deno/coder/error.ts +0 -64
  70. package/build-deno/coder/index.ts +0 -86
  71. package/build-deno/defaults.ts +0 -8
  72. package/build-deno/http/index.ts +0 -236
  73. package/build-deno/http/types.ts +0 -9
  74. package/build-deno/index.ts +0 -4
  75. package/build-deno/lru.ts +0 -189
  76. package/build-deno/mock/index.ts +0 -257
  77. package/build-deno/mock/mockHttp.ts +0 -33
  78. package/build-deno/mock/mockWs.ts +0 -87
  79. package/build-deno/mock/types.ts +0 -34
  80. package/build-deno/mod.ts +0 -2
  81. package/build-deno/packageDetect.ts +0 -8
  82. package/build-deno/packageInfo.ts +0 -3
  83. package/build-deno/types.ts +0 -99
  84. package/build-deno/ws/errors.ts +0 -38
  85. package/build-deno/ws/index.ts +0 -650
  86. package/build-tsc-cjs/packageDetect.js +0 -6
  87. package/src/bizinikiwi-connect/Health.ts +0 -325
  88. package/src/bizinikiwi-connect/index.spec.ts +0 -675
  89. package/src/bizinikiwi-connect/index.ts +0 -427
  90. package/src/bizinikiwi-connect/types.ts +0 -16
  91. package/src/bundle.ts +0 -8
  92. package/src/coder/decodeResponse.spec.ts +0 -70
  93. package/src/coder/encodeJson.spec.ts +0 -20
  94. package/src/coder/encodeObject.spec.ts +0 -25
  95. package/src/coder/error.spec.ts +0 -111
  96. package/src/coder/error.ts +0 -66
  97. package/src/coder/index.ts +0 -88
  98. package/src/defaults.ts +0 -10
  99. package/src/http/index.spec.ts +0 -72
  100. package/src/http/index.ts +0 -238
  101. package/src/http/send.spec.ts +0 -61
  102. package/src/http/types.ts +0 -11
  103. package/src/index.ts +0 -6
  104. package/src/lru.spec.ts +0 -74
  105. package/src/lru.ts +0 -197
  106. package/src/mock/index.ts +0 -259
  107. package/src/mock/mockHttp.ts +0 -35
  108. package/src/mock/mockWs.ts +0 -92
  109. package/src/mock/on.spec.ts +0 -43
  110. package/src/mock/send.spec.ts +0 -38
  111. package/src/mock/subscribe.spec.ts +0 -81
  112. package/src/mock/types.ts +0 -36
  113. package/src/mock/unsubscribe.spec.ts +0 -57
  114. package/src/mod.ts +0 -4
  115. package/src/packageDetect.ts +0 -12
  116. package/src/packageInfo.ts +0 -6
  117. package/src/types.ts +0 -101
  118. package/src/ws/connect.spec.ts +0 -167
  119. package/src/ws/errors.ts +0 -41
  120. package/src/ws/index.spec.ts +0 -97
  121. package/src/ws/index.ts +0 -652
  122. package/src/ws/send.spec.ts +0 -126
  123. package/src/ws/state.spec.ts +0 -20
  124. package/src/ws/subscribe.spec.ts +0 -68
  125. package/src/ws/unsubscribe.spec.ts +0 -100
  126. package/tsconfig.build.json +0 -17
  127. package/tsconfig.build.tsbuildinfo +0 -1
  128. package/tsconfig.spec.json +0 -18
  129. package/tsconfig.spec.tsbuildinfo +0 -1
  130. /package/{build-tsc/bizinikiwi-connect → bizinikiwi-connect}/Health.d.ts +0 -0
  131. /package/{build-tsc-esm/bizinikiwi-connect → bizinikiwi-connect}/Health.js +0 -0
  132. /package/{build-tsc/bizinikiwi-connect → bizinikiwi-connect}/index.d.ts +0 -0
  133. /package/{build-tsc-esm/bizinikiwi-connect → bizinikiwi-connect}/index.js +0 -0
  134. /package/{build-tsc/bizinikiwi-connect → bizinikiwi-connect}/types.d.ts +0 -0
  135. /package/{build-tsc-esm/bizinikiwi-connect → bizinikiwi-connect}/types.js +0 -0
  136. /package/{build-tsc/bundle.d.ts → bundle.d.ts} +0 -0
  137. /package/{build-tsc-esm/bundle.js → bundle.js} +0 -0
  138. /package/{build → cjs}/bizinikiwi-connect/Health.d.ts +0 -0
  139. /package/{build-tsc-cjs → cjs}/bizinikiwi-connect/Health.js +0 -0
  140. /package/{build → cjs}/bizinikiwi-connect/index.d.ts +0 -0
  141. /package/{build-tsc-cjs → cjs}/bizinikiwi-connect/index.js +0 -0
  142. /package/{build → cjs}/bizinikiwi-connect/types.d.ts +0 -0
  143. /package/{build-tsc-cjs → cjs}/bizinikiwi-connect/types.js +0 -0
  144. /package/{build → cjs}/bundle.d.ts +0 -0
  145. /package/{build-tsc-cjs → cjs}/bundle.js +0 -0
  146. /package/{build-tsc → cjs}/coder/error.d.ts +0 -0
  147. /package/{build-tsc-cjs → cjs}/coder/error.js +0 -0
  148. /package/{build-tsc → cjs}/coder/index.d.ts +0 -0
  149. /package/{build-tsc-cjs → cjs}/coder/index.js +0 -0
  150. /package/{build-tsc → cjs}/defaults.d.ts +0 -0
  151. /package/{build-tsc-cjs → cjs}/defaults.js +0 -0
  152. /package/{build-tsc → cjs}/http/index.d.ts +0 -0
  153. /package/{build-tsc-cjs → cjs}/http/index.js +0 -0
  154. /package/{build-tsc → cjs}/http/types.d.ts +0 -0
  155. /package/{build-tsc-cjs → cjs}/http/types.js +0 -0
  156. /package/{build-tsc → cjs}/index.d.ts +0 -0
  157. /package/{build-tsc-cjs → cjs}/index.js +0 -0
  158. /package/{build-tsc → cjs}/lru.d.ts +0 -0
  159. /package/{build-tsc-cjs → cjs}/lru.js +0 -0
  160. /package/{build-tsc → cjs}/mock/index.d.ts +0 -0
  161. /package/{build-tsc-cjs → cjs}/mock/index.js +0 -0
  162. /package/{build-tsc → cjs}/mock/mockHttp.d.ts +0 -0
  163. /package/{build-tsc-cjs → cjs}/mock/mockHttp.js +0 -0
  164. /package/{build-tsc → cjs}/mock/mockWs.d.ts +0 -0
  165. /package/{build-tsc-cjs → cjs}/mock/mockWs.js +0 -0
  166. /package/{build-tsc → cjs}/mock/types.d.ts +0 -0
  167. /package/{build-tsc-cjs → cjs}/mock/types.js +0 -0
  168. /package/{build/cjs → cjs}/package.json +0 -0
  169. /package/{build-tsc → cjs}/packageDetect.d.ts +0 -0
  170. /package/{build/cjs → cjs}/packageDetect.js +0 -0
  171. /package/{build-tsc → cjs}/packageInfo.d.ts +0 -0
  172. /package/{build-tsc-cjs → cjs}/packageInfo.js +0 -0
  173. /package/{build-tsc → cjs}/types.d.ts +0 -0
  174. /package/{build-tsc-cjs → cjs}/types.js +0 -0
  175. /package/{build-tsc → cjs}/ws/errors.d.ts +0 -0
  176. /package/{build-tsc-cjs → cjs}/ws/errors.js +0 -0
  177. /package/{build-tsc → cjs}/ws/index.d.ts +0 -0
  178. /package/{build-tsc-cjs → cjs}/ws/index.js +0 -0
  179. /package/{build/cjs/coder → coder}/error.d.ts +0 -0
  180. /package/{build-tsc-esm/coder → coder}/error.js +0 -0
  181. /package/{build/cjs/coder → coder}/index.d.ts +0 -0
  182. /package/{build-tsc-esm/coder → coder}/index.js +0 -0
  183. /package/{build/cjs/defaults.d.ts → defaults.d.ts} +0 -0
  184. /package/{build-tsc-esm/defaults.js → defaults.js} +0 -0
  185. /package/{build/cjs/http → http}/index.d.ts +0 -0
  186. /package/{build-tsc-esm/http → http}/index.js +0 -0
  187. /package/{build/cjs/http → http}/types.d.ts +0 -0
  188. /package/{build-tsc-esm/http → http}/types.js +0 -0
  189. /package/{build/cjs/index.d.ts → index.d.ts} +0 -0
  190. /package/{build-tsc-esm/index.js → index.js} +0 -0
  191. /package/{build/cjs/lru.d.ts → lru.d.ts} +0 -0
  192. /package/{build-tsc-esm/lru.js → lru.js} +0 -0
  193. /package/{build/cjs/mock → mock}/index.d.ts +0 -0
  194. /package/{build-tsc-esm/mock → mock}/index.js +0 -0
  195. /package/{build/cjs/mock → mock}/mockHttp.d.ts +0 -0
  196. /package/{build-tsc-esm/mock → mock}/mockHttp.js +0 -0
  197. /package/{build/cjs/mock → mock}/mockWs.d.ts +0 -0
  198. /package/{build-tsc-esm/mock → mock}/mockWs.js +0 -0
  199. /package/{build/cjs/mock → mock}/types.d.ts +0 -0
  200. /package/{build-tsc-esm/mock → mock}/types.js +0 -0
  201. /package/{build/cjs/packageDetect.d.ts → packageDetect.d.ts} +0 -0
  202. /package/{build-tsc-esm/packageDetect.js → packageDetect.js} +0 -0
  203. /package/{build/cjs/packageInfo.d.ts → packageInfo.d.ts} +0 -0
  204. /package/{build-tsc-esm/packageInfo.js → packageInfo.js} +0 -0
  205. /package/{build/cjs/types.d.ts → types.d.ts} +0 -0
  206. /package/{build-tsc-esm/types.js → types.js} +0 -0
  207. /package/{build/cjs/ws → ws}/errors.d.ts +0 -0
  208. /package/{build-tsc-esm/ws → ws}/errors.js +0 -0
  209. /package/{build/cjs/ws → ws}/index.d.ts +0 -0
  210. /package/{build-tsc-esm/ws → ws}/index.js +0 -0
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +0,0 @@
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/build/cjs/lru.js DELETED
@@ -1,150 +0,0 @@
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;
@@ -1,196 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MockProvider = void 0;
4
- const tslib_1 = require("tslib");
5
- const eventemitter3_1 = require("eventemitter3");
6
- const testing_1 = require("@pezkuwi/keyring/testing");
7
- const types_1 = require("@pezkuwi/types");
8
- const jsonrpc_1 = tslib_1.__importDefault(require("@pezkuwi/types/interfaces/jsonrpc"));
9
- const Header_004_json_1 = tslib_1.__importDefault(require("@pezkuwi/types-support/json/Header.004.json"));
10
- const SignedBlock_004_immortal_json_1 = tslib_1.__importDefault(require("@pezkuwi/types-support/json/SignedBlock.004.immortal.json"));
11
- const static_bizinikiwi_1 = tslib_1.__importDefault(require("@pezkuwi/types-support/metadata/static-bizinikiwi"));
12
- const util_1 = require("@pezkuwi/util");
13
- const util_crypto_1 = require("@pezkuwi/util-crypto");
14
- const INTERVAL = 1000;
15
- const SUBSCRIPTIONS = Array.prototype.concat.apply([], Object.values(jsonrpc_1.default).map((section) => Object
16
- .values(section)
17
- .filter(({ isSubscription }) => isSubscription)
18
- .map(({ jsonrpc }) => jsonrpc)
19
- .concat('chain_subscribeNewHead')));
20
- const keyring = (0, testing_1.createTestKeyring)({ type: 'ed25519' });
21
- const l = (0, util_1.logger)('api-mock');
22
- /**
23
- * A mock provider mainly used for testing.
24
- * @return {ProviderInterface} The mock provider
25
- * @internal
26
- */
27
- class MockProvider {
28
- db = {};
29
- emitter = new eventemitter3_1.EventEmitter();
30
- intervalId;
31
- isUpdating = true;
32
- registry;
33
- prevNumber = new util_1.BN(-1);
34
- requests = {
35
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
36
- chain_getBlock: () => this.registry.createType('SignedBlock', SignedBlock_004_immortal_json_1.default.result).toJSON(),
37
- chain_getBlockHash: () => '0x1234000000000000000000000000000000000000000000000000000000000000',
38
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
39
- chain_getFinalizedHead: () => this.registry.createType('Header', Header_004_json_1.default.result).hash,
40
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
41
- chain_getHeader: () => this.registry.createType('Header', Header_004_json_1.default.result).toJSON(),
42
- rpc_methods: () => this.registry.createType('RpcMethods').toJSON(),
43
- state_getKeys: () => [],
44
- state_getKeysPaged: () => [],
45
- state_getMetadata: () => static_bizinikiwi_1.default,
46
- state_getRuntimeVersion: () => this.registry.createType('RuntimeVersion').toHex(),
47
- state_getStorage: (storage, [key]) => (0, util_1.u8aToHex)(storage[key]),
48
- system_chain: () => 'mockChain',
49
- system_health: () => ({}),
50
- system_name: () => 'mockClient',
51
- system_properties: () => ({ ss58Format: 42 }),
52
- system_upgradedToTripleRefCount: () => this.registry.createType('bool', true),
53
- system_version: () => '9.8.7',
54
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return, sort-keys
55
- dev_echo: (_, params) => params
56
- };
57
- subscriptions = SUBSCRIPTIONS.reduce((subs, name) => {
58
- subs[name] = {
59
- callbacks: {},
60
- lastValue: null
61
- };
62
- return subs;
63
- }, {});
64
- subscriptionId = 0;
65
- subscriptionMap = {};
66
- constructor(registry) {
67
- this.registry = registry;
68
- this.init();
69
- }
70
- get hasSubscriptions() {
71
- return !!true;
72
- }
73
- clone() {
74
- throw new Error('Unimplemented');
75
- }
76
- async connect() {
77
- // noop
78
- }
79
- // eslint-disable-next-line @typescript-eslint/require-await
80
- async disconnect() {
81
- if (this.intervalId) {
82
- clearInterval(this.intervalId);
83
- this.intervalId = null;
84
- }
85
- }
86
- get isClonable() {
87
- return !!false;
88
- }
89
- get isConnected() {
90
- return !!true;
91
- }
92
- on(type, sub) {
93
- this.emitter.on(type, sub);
94
- return () => {
95
- this.emitter.removeListener(type, sub);
96
- };
97
- }
98
- // eslint-disable-next-line @typescript-eslint/require-await
99
- async send(method, params) {
100
- l.debug(() => ['send', method, params]);
101
- if (!this.requests[method]) {
102
- throw new Error(`provider.send: Invalid method '${method}'`);
103
- }
104
- return this.requests[method](this.db, params);
105
- }
106
- // eslint-disable-next-line @typescript-eslint/require-await
107
- async subscribe(_type, method, ...params) {
108
- l.debug(() => ['subscribe', method, params]);
109
- if (!this.subscriptions[method]) {
110
- throw new Error(`provider.subscribe: Invalid method '${method}'`);
111
- }
112
- const callback = params.pop();
113
- const id = ++this.subscriptionId;
114
- this.subscriptions[method].callbacks[id] = callback;
115
- this.subscriptionMap[id] = method;
116
- if (this.subscriptions[method].lastValue !== null) {
117
- callback(null, this.subscriptions[method].lastValue);
118
- }
119
- return id;
120
- }
121
- // eslint-disable-next-line @typescript-eslint/require-await
122
- async unsubscribe(_type, _method, id) {
123
- const sub = this.subscriptionMap[id];
124
- l.debug(() => ['unsubscribe', id, sub]);
125
- if (!sub) {
126
- throw new Error(`Unable to find subscription for ${id}`);
127
- }
128
- delete this.subscriptionMap[id];
129
- delete this.subscriptions[sub].callbacks[id];
130
- return true;
131
- }
132
- init() {
133
- const emitEvents = ['connected', 'disconnected'];
134
- let emitIndex = 0;
135
- let newHead = this.makeBlockHeader();
136
- let counter = -1;
137
- const metadata = new types_1.Metadata(this.registry, static_bizinikiwi_1.default);
138
- this.registry.setMetadata(metadata);
139
- const query = (0, types_1.decorateStorage)(this.registry, metadata.asLatest, metadata.version);
140
- // Do something every 1 seconds
141
- this.intervalId = setInterval(() => {
142
- if (!this.isUpdating) {
143
- return;
144
- }
145
- // create a new header (next block)
146
- newHead = this.makeBlockHeader();
147
- // increment the balances and nonce for each account
148
- keyring.getPairs().forEach(({ publicKey }, index) => {
149
- this.setStateBn(query['system']['account'](publicKey), newHead.number.toBn().addn(index));
150
- });
151
- // set the timestamp for the current block
152
- this.setStateBn(query['timestamp']['now'](), Math.floor(Date.now() / 1000));
153
- this.updateSubs('chain_subscribeNewHead', newHead);
154
- // We emit connected/disconnected at intervals
155
- if (++counter % 2 === 1) {
156
- if (++emitIndex === emitEvents.length) {
157
- emitIndex = 0;
158
- }
159
- this.emitter.emit(emitEvents[emitIndex]);
160
- }
161
- }, INTERVAL);
162
- }
163
- makeBlockHeader() {
164
- const blockNumber = this.prevNumber.addn(1);
165
- const header = this.registry.createType('Header', {
166
- digest: {
167
- logs: []
168
- },
169
- extrinsicsRoot: (0, util_crypto_1.randomAsU8a)(),
170
- number: blockNumber,
171
- parentHash: blockNumber.isZero()
172
- ? new Uint8Array(32)
173
- : (0, util_1.bnToU8a)(this.prevNumber, { bitLength: 256, isLe: false }),
174
- stateRoot: (0, util_1.bnToU8a)(blockNumber, { bitLength: 256, isLe: false })
175
- });
176
- this.prevNumber = blockNumber;
177
- return header;
178
- }
179
- setStateBn(key, value) {
180
- this.db[(0, util_1.u8aToHex)(key)] = (0, util_1.bnToU8a)(value, { bitLength: 64, isLe: true });
181
- }
182
- updateSubs(method, value) {
183
- this.subscriptions[method].lastValue = value;
184
- Object
185
- .values(this.subscriptions[method].callbacks)
186
- .forEach((cb) => {
187
- try {
188
- cb(null, value.toJSON());
189
- }
190
- catch (error) {
191
- l.error(`Error on '${method}' subscription`, error);
192
- }
193
- });
194
- }
195
- }
196
- exports.MockProvider = MockProvider;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TEST_HTTP_URL = void 0;
4
- exports.mockHttp = mockHttp;
5
- const tslib_1 = require("tslib");
6
- const nock_1 = tslib_1.__importDefault(require("nock"));
7
- exports.TEST_HTTP_URL = 'http://localhost:9944';
8
- function mockHttp(requests) {
9
- nock_1.default.cleanAll();
10
- return requests.reduce((scope, request) => scope
11
- .post('/')
12
- .reply(request.code || 200, (_uri, body) => {
13
- scope.body = scope.body || {};
14
- scope.body[request.method] = body;
15
- return Object.assign({ id: body.id, jsonrpc: '2.0' }, request.reply || {});
16
- }), (0, nock_1.default)(exports.TEST_HTTP_URL));
17
- }
@@ -1,47 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TEST_WS_URL = void 0;
4
- exports.mockWs = mockWs;
5
- const mock_socket_1 = require("mock-socket");
6
- const util_1 = require("@pezkuwi/util");
7
- global.WebSocket = mock_socket_1.WebSocket;
8
- exports.TEST_WS_URL = 'ws://localhost:9955';
9
- function createError({ error: { code, message }, id }) {
10
- return {
11
- error: {
12
- code,
13
- message
14
- },
15
- id,
16
- jsonrpc: '2.0'
17
- };
18
- }
19
- function createReply({ id, reply: { result } }) {
20
- return {
21
- id,
22
- jsonrpc: '2.0',
23
- result
24
- };
25
- }
26
- function mockWs(requests, wsUrl = exports.TEST_WS_URL) {
27
- const server = new mock_socket_1.Server(wsUrl);
28
- let requestCount = 0;
29
- const scope = {
30
- body: {},
31
- done: () => new Promise((resolve) => server.stop(resolve)),
32
- requests: 0,
33
- server
34
- };
35
- server.on('connection', (socket) => {
36
- socket.on('message', (body) => {
37
- const request = requests[requestCount];
38
- const response = request.error
39
- ? createError(request)
40
- : createReply(request);
41
- scope.body[request.method] = body;
42
- requestCount++;
43
- socket.send((0, util_1.stringify)(response));
44
- });
45
- });
46
- return scope;
47
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.packageInfo = void 0;
4
- exports.packageInfo = { name: '@pezkuwi/rpc-provider', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '16.5.18' };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getWSErrorString = getWSErrorString;
4
- const known = {
5
- 1000: 'Normal Closure',
6
- 1001: 'Going Away',
7
- 1002: 'Protocol Error',
8
- 1003: 'Unsupported Data',
9
- 1004: '(For future)',
10
- 1005: 'No Status Received',
11
- 1006: 'Abnormal Closure',
12
- 1007: 'Invalid frame payload data',
13
- 1008: 'Policy Violation',
14
- 1009: 'Message too big',
15
- 1010: 'Missing Extension',
16
- 1011: 'Internal Error',
17
- 1012: 'Service Restart',
18
- 1013: 'Try Again Later',
19
- 1014: 'Bad Gateway',
20
- 1015: 'TLS Handshake'
21
- };
22
- function getWSErrorString(code) {
23
- if (code >= 0 && code <= 999) {
24
- return '(Unused)';
25
- }
26
- else if (code >= 1016) {
27
- if (code <= 1999) {
28
- return '(For WebSocket standard)';
29
- }
30
- else if (code <= 2999) {
31
- return '(For WebSocket extensions)';
32
- }
33
- else if (code <= 3999) {
34
- return '(For libraries and frameworks)';
35
- }
36
- else if (code <= 4999) {
37
- return '(For applications)';
38
- }
39
- }
40
- return known[code] || '(Unknown)';
41
- }