@pezkuwi/rpc-provider 16.5.8 → 16.5.9

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 (212) hide show
  1. package/cjs/packageInfo.js +1 -1
  2. package/package.json +12 -12
  3. package/packageInfo.js +1 -1
  4. package/build/LICENSE +0 -201
  5. package/build/README.md +0 -68
  6. package/build/bizinikiwi-connect/Health.d.ts +0 -7
  7. package/build/bizinikiwi-connect/Health.js +0 -259
  8. package/build/bizinikiwi-connect/index.d.ts +0 -22
  9. package/build/bizinikiwi-connect/index.js +0 -319
  10. package/build/bizinikiwi-connect/types.d.ts +0 -12
  11. package/build/bizinikiwi-connect/types.js +0 -1
  12. package/build/bundle.d.ts +0 -5
  13. package/build/bundle.js +0 -5
  14. package/build/cjs/bizinikiwi-connect/Health.d.ts +0 -7
  15. package/build/cjs/bizinikiwi-connect/Health.js +0 -264
  16. package/build/cjs/bizinikiwi-connect/index.d.ts +0 -22
  17. package/build/cjs/bizinikiwi-connect/index.js +0 -323
  18. package/build/cjs/bizinikiwi-connect/types.d.ts +0 -12
  19. package/build/cjs/bizinikiwi-connect/types.js +0 -2
  20. package/build/cjs/bundle.d.ts +0 -5
  21. package/build/cjs/bundle.js +0 -14
  22. package/build/cjs/coder/error.d.ts +0 -29
  23. package/build/cjs/coder/error.js +0 -53
  24. package/build/cjs/coder/index.d.ts +0 -8
  25. package/build/cjs/coder/index.js +0 -63
  26. package/build/cjs/defaults.d.ts +0 -5
  27. package/build/cjs/defaults.js +0 -8
  28. package/build/cjs/http/index.d.ts +0 -81
  29. package/build/cjs/http/index.js +0 -196
  30. package/build/cjs/http/types.d.ts +0 -7
  31. package/build/cjs/http/types.js +0 -2
  32. package/build/cjs/index.d.ts +0 -2
  33. package/build/cjs/index.js +0 -5
  34. package/build/cjs/lru.d.ts +0 -15
  35. package/build/cjs/lru.js +0 -150
  36. package/build/cjs/mock/index.d.ts +0 -35
  37. package/build/cjs/mock/index.js +0 -196
  38. package/build/cjs/mock/mockHttp.d.ts +0 -9
  39. package/build/cjs/mock/mockHttp.js +0 -17
  40. package/build/cjs/mock/mockWs.d.ts +0 -26
  41. package/build/cjs/mock/mockWs.js +0 -47
  42. package/build/cjs/mock/types.d.ts +0 -23
  43. package/build/cjs/mock/types.js +0 -2
  44. package/build/cjs/package.json +0 -3
  45. package/build/cjs/packageDetect.d.ts +0 -1
  46. package/build/cjs/packageDetect.js +0 -6
  47. package/build/cjs/packageInfo.d.ts +0 -6
  48. package/build/cjs/packageInfo.js +0 -4
  49. package/build/cjs/types.d.ts +0 -85
  50. package/build/cjs/types.js +0 -2
  51. package/build/cjs/ws/errors.d.ts +0 -1
  52. package/build/cjs/ws/errors.js +0 -41
  53. package/build/cjs/ws/index.d.ts +0 -121
  54. package/build/cjs/ws/index.js +0 -529
  55. package/build/coder/error.d.ts +0 -29
  56. package/build/coder/error.js +0 -50
  57. package/build/coder/index.d.ts +0 -8
  58. package/build/coder/index.js +0 -58
  59. package/build/defaults.d.ts +0 -5
  60. package/build/defaults.js +0 -6
  61. package/build/http/index.d.ts +0 -81
  62. package/build/http/index.js +0 -191
  63. package/build/http/types.d.ts +0 -7
  64. package/build/http/types.js +0 -1
  65. package/build/index.d.ts +0 -2
  66. package/build/index.js +0 -2
  67. package/build/lru.d.ts +0 -15
  68. package/build/lru.js +0 -146
  69. package/build/mock/index.d.ts +0 -35
  70. package/build/mock/index.js +0 -191
  71. package/build/mock/mockHttp.d.ts +0 -9
  72. package/build/mock/mockHttp.js +0 -12
  73. package/build/mock/mockWs.d.ts +0 -26
  74. package/build/mock/mockWs.js +0 -43
  75. package/build/mock/types.d.ts +0 -23
  76. package/build/mock/types.js +0 -1
  77. package/build/package.json +0 -344
  78. package/build/packageDetect.d.ts +0 -1
  79. package/build/packageDetect.js +0 -4
  80. package/build/packageInfo.d.ts +0 -6
  81. package/build/packageInfo.js +0 -1
  82. package/build/types.d.ts +0 -85
  83. package/build/types.js +0 -1
  84. package/build/ws/errors.d.ts +0 -1
  85. package/build/ws/errors.js +0 -38
  86. package/build/ws/index.d.ts +0 -121
  87. package/build/ws/index.js +0 -524
  88. package/build-deno/README.md +0 -66
  89. package/build-deno/bizinikiwi-connect/Health.ts +0 -323
  90. package/build-deno/bizinikiwi-connect/index.ts +0 -417
  91. package/build-deno/bizinikiwi-connect/types.ts +0 -14
  92. package/build-deno/bundle.ts +0 -6
  93. package/build-deno/coder/error.ts +0 -64
  94. package/build-deno/coder/index.ts +0 -86
  95. package/build-deno/defaults.ts +0 -8
  96. package/build-deno/http/index.ts +0 -236
  97. package/build-deno/http/types.ts +0 -9
  98. package/build-deno/index.ts +0 -4
  99. package/build-deno/lru.ts +0 -189
  100. package/build-deno/mock/index.ts +0 -257
  101. package/build-deno/mock/mockHttp.ts +0 -33
  102. package/build-deno/mock/mockWs.ts +0 -87
  103. package/build-deno/mock/types.ts +0 -34
  104. package/build-deno/mod.ts +0 -2
  105. package/build-deno/packageDetect.ts +0 -8
  106. package/build-deno/packageInfo.ts +0 -3
  107. package/build-deno/types.ts +0 -99
  108. package/build-deno/ws/errors.ts +0 -38
  109. package/build-deno/ws/index.ts +0 -650
  110. package/build-tsc/bizinikiwi-connect/Health.d.ts +0 -7
  111. package/build-tsc/bizinikiwi-connect/index.d.ts +0 -22
  112. package/build-tsc/bizinikiwi-connect/types.d.ts +0 -12
  113. package/build-tsc/bundle.d.ts +0 -5
  114. package/build-tsc/coder/error.d.ts +0 -29
  115. package/build-tsc/coder/index.d.ts +0 -8
  116. package/build-tsc/defaults.d.ts +0 -5
  117. package/build-tsc/http/index.d.ts +0 -81
  118. package/build-tsc/http/types.d.ts +0 -7
  119. package/build-tsc/index.d.ts +0 -2
  120. package/build-tsc/lru.d.ts +0 -15
  121. package/build-tsc/mock/index.d.ts +0 -35
  122. package/build-tsc/mock/mockHttp.d.ts +0 -9
  123. package/build-tsc/mock/mockWs.d.ts +0 -26
  124. package/build-tsc/mock/types.d.ts +0 -23
  125. package/build-tsc/packageDetect.d.ts +0 -1
  126. package/build-tsc/packageInfo.d.ts +0 -6
  127. package/build-tsc/types.d.ts +0 -85
  128. package/build-tsc/ws/errors.d.ts +0 -1
  129. package/build-tsc/ws/index.d.ts +0 -121
  130. package/build-tsc-cjs/bizinikiwi-connect/Health.js +0 -264
  131. package/build-tsc-cjs/bizinikiwi-connect/index.js +0 -323
  132. package/build-tsc-cjs/bizinikiwi-connect/types.js +0 -2
  133. package/build-tsc-cjs/bundle.js +0 -14
  134. package/build-tsc-cjs/coder/error.js +0 -53
  135. package/build-tsc-cjs/coder/index.js +0 -63
  136. package/build-tsc-cjs/defaults.js +0 -8
  137. package/build-tsc-cjs/http/index.js +0 -196
  138. package/build-tsc-cjs/http/types.js +0 -2
  139. package/build-tsc-cjs/index.js +0 -5
  140. package/build-tsc-cjs/lru.js +0 -150
  141. package/build-tsc-cjs/mock/index.js +0 -196
  142. package/build-tsc-cjs/mock/mockHttp.js +0 -17
  143. package/build-tsc-cjs/mock/mockWs.js +0 -47
  144. package/build-tsc-cjs/mock/types.js +0 -2
  145. package/build-tsc-cjs/packageDetect.js +0 -6
  146. package/build-tsc-cjs/packageInfo.js +0 -4
  147. package/build-tsc-cjs/types.js +0 -2
  148. package/build-tsc-cjs/ws/errors.js +0 -41
  149. package/build-tsc-cjs/ws/index.js +0 -529
  150. package/build-tsc-esm/bizinikiwi-connect/Health.js +0 -259
  151. package/build-tsc-esm/bizinikiwi-connect/index.js +0 -319
  152. package/build-tsc-esm/bizinikiwi-connect/types.js +0 -1
  153. package/build-tsc-esm/bundle.js +0 -5
  154. package/build-tsc-esm/coder/error.js +0 -50
  155. package/build-tsc-esm/coder/index.js +0 -58
  156. package/build-tsc-esm/defaults.js +0 -6
  157. package/build-tsc-esm/http/index.js +0 -191
  158. package/build-tsc-esm/http/types.js +0 -1
  159. package/build-tsc-esm/index.js +0 -2
  160. package/build-tsc-esm/lru.js +0 -146
  161. package/build-tsc-esm/mock/index.js +0 -191
  162. package/build-tsc-esm/mock/mockHttp.js +0 -12
  163. package/build-tsc-esm/mock/mockWs.js +0 -43
  164. package/build-tsc-esm/mock/types.js +0 -1
  165. package/build-tsc-esm/packageDetect.js +0 -4
  166. package/build-tsc-esm/packageInfo.js +0 -1
  167. package/build-tsc-esm/types.js +0 -1
  168. package/build-tsc-esm/ws/errors.js +0 -38
  169. package/build-tsc-esm/ws/index.js +0 -524
  170. package/src/bizinikiwi-connect/Health.ts +0 -325
  171. package/src/bizinikiwi-connect/index.spec.ts +0 -671
  172. package/src/bizinikiwi-connect/index.ts +0 -427
  173. package/src/bizinikiwi-connect/types.ts +0 -16
  174. package/src/bundle.ts +0 -8
  175. package/src/coder/decodeResponse.spec.ts +0 -70
  176. package/src/coder/encodeJson.spec.ts +0 -20
  177. package/src/coder/encodeObject.spec.ts +0 -25
  178. package/src/coder/error.spec.ts +0 -111
  179. package/src/coder/error.ts +0 -66
  180. package/src/coder/index.ts +0 -88
  181. package/src/defaults.ts +0 -10
  182. package/src/http/index.spec.ts +0 -72
  183. package/src/http/index.ts +0 -238
  184. package/src/http/send.spec.ts +0 -61
  185. package/src/http/types.ts +0 -11
  186. package/src/index.ts +0 -6
  187. package/src/lru.spec.ts +0 -74
  188. package/src/lru.ts +0 -197
  189. package/src/mock/index.ts +0 -259
  190. package/src/mock/mockHttp.ts +0 -35
  191. package/src/mock/mockWs.ts +0 -92
  192. package/src/mock/on.spec.ts +0 -43
  193. package/src/mock/send.spec.ts +0 -38
  194. package/src/mock/subscribe.spec.ts +0 -81
  195. package/src/mock/types.ts +0 -36
  196. package/src/mock/unsubscribe.spec.ts +0 -57
  197. package/src/mod.ts +0 -4
  198. package/src/packageDetect.ts +0 -12
  199. package/src/packageInfo.ts +0 -6
  200. package/src/types.ts +0 -101
  201. package/src/ws/connect.spec.ts +0 -167
  202. package/src/ws/errors.ts +0 -41
  203. package/src/ws/index.spec.ts +0 -97
  204. package/src/ws/index.ts +0 -652
  205. package/src/ws/send.spec.ts +0 -126
  206. package/src/ws/state.spec.ts +0 -20
  207. package/src/ws/subscribe.spec.ts +0 -68
  208. package/src/ws/unsubscribe.spec.ts +0 -100
  209. package/tsconfig.build.json +0 -17
  210. package/tsconfig.build.tsbuildinfo +0 -1
  211. package/tsconfig.spec.json +0 -18
  212. package/tsconfig.spec.tsbuildinfo +0 -1
@@ -1,671 +0,0 @@
1
- // Copyright 2017-2025 @pezkuwi/rpc-provider authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- /// <reference types="@pezkuwi/dev-test/globals.d.ts" />
5
-
6
- import type * as Sc from '@bizinikiwi/connect';
7
- import type { HealthChecker, SmoldotHealth } from './types.js';
8
-
9
- import { stringify } from '@pezkuwi/util';
10
-
11
- import { ScProvider } from './index.js';
12
-
13
- interface MockChain extends Sc.Chain {
14
- _spec: () => string;
15
- _recevedRequests: () => string[];
16
- _isTerminated: () => boolean;
17
- _triggerCallback: (response: string | object) => void;
18
- _setTerminateInterceptor: (fn: () => void) => void;
19
- _setSendJsonRpcInterceptor: (fn: (rpc: string) => void) => void;
20
- _getLatestRequest: () => string;
21
- }
22
-
23
- interface MockedHealthChecker extends HealthChecker {
24
- _isActive: () => boolean;
25
- _triggerHealthUpdate: (update: SmoldotHealth) => void;
26
- }
27
-
28
- type MockSc = typeof Sc & {
29
- latestChain: () => MockChain;
30
- };
31
-
32
- enum WellKnownChain {
33
- pezkuwi = 'pezkuwi',
34
- ksmcc3 = 'ksmcc3',
35
- pezkuwichain_v2_2 = 'pezkuwichain_v2_2',
36
- dicle2 = 'dicle2'
37
- }
38
-
39
- const wait = (ms: number) =>
40
- new Promise((resolve) =>
41
- setTimeout(resolve, ms)
42
- );
43
-
44
- function healthCheckerMock (): MockedHealthChecker {
45
- let cb: (health: SmoldotHealth) => void = () => undefined;
46
- let sendJsonRpc: (request: string) => void = () => undefined;
47
- let isActive = false;
48
-
49
- return {
50
- _isActive: () => isActive,
51
- _triggerHealthUpdate: (update: SmoldotHealth) => {
52
- cb(update);
53
- },
54
- responsePassThrough: (response) => response,
55
- sendJsonRpc: (...args) => sendJsonRpc(...args),
56
- setSendJsonRpc: (cb) => {
57
- sendJsonRpc = cb;
58
- },
59
- start: (x) => {
60
- isActive = true;
61
- cb = x;
62
- },
63
- stop: () => {
64
- isActive = false;
65
- }
66
- };
67
- }
68
-
69
- function healthCheckerFactory () {
70
- const _healthCheckers: MockedHealthChecker[] = [];
71
-
72
- return {
73
- _healthCheckers,
74
- _latestHealthChecker: () => _healthCheckers.slice(-1)[0],
75
- healthChecker: () => {
76
- const result = healthCheckerMock();
77
-
78
- _healthCheckers.push(result);
79
-
80
- return result;
81
- }
82
- };
83
- }
84
-
85
- function getFakeChain (spec: string): MockChain {
86
- const _receivedRequests: string[] = [];
87
- let _isTerminated = false;
88
- const _pendingResponses: string[] = [];
89
- let _responseResolver: ((value: string) => void) | null = null;
90
-
91
- let terminateInterceptor = Function.prototype;
92
- let sendJsonRpcInterceptor = Function.prototype;
93
-
94
- const triggerCallback = (response: string | object) => {
95
- const res = typeof response === 'string' ? response : stringify(response);
96
-
97
- if (_responseResolver) {
98
- _responseResolver(res);
99
- _responseResolver = null;
100
- } else {
101
- _pendingResponses.push(res);
102
- }
103
- };
104
-
105
- const nextJsonRpcResponse = (): Promise<string> => {
106
- if (_pendingResponses.length > 0) {
107
- return Promise.resolve(_pendingResponses.shift()!);
108
- }
109
-
110
- return new Promise((resolve) => {
111
- _responseResolver = resolve;
112
- });
113
- };
114
-
115
- const jsonRpcResponses: AsyncIterableIterator<string> = {
116
- [Symbol.asyncIterator] () {
117
- return this;
118
- },
119
- async next () {
120
- if (_isTerminated) {
121
- return { done: true, value: undefined };
122
- }
123
-
124
- const value = await nextJsonRpcResponse();
125
-
126
- return { done: false, value };
127
- }
128
- };
129
-
130
- return {
131
- _getLatestRequest: () => _receivedRequests[_receivedRequests.length - 1],
132
- _isTerminated: () => _isTerminated,
133
- _recevedRequests: () => _receivedRequests,
134
- _setSendJsonRpcInterceptor: (fn) => {
135
- sendJsonRpcInterceptor = fn;
136
- },
137
- _setTerminateInterceptor: (fn) => {
138
- terminateInterceptor = fn;
139
- },
140
- _spec: () => spec,
141
- _triggerCallback: triggerCallback,
142
- addChain: (chainSpec) =>
143
- Promise.resolve(getFakeChain(chainSpec)),
144
- remove: () => {
145
- terminateInterceptor();
146
- _isTerminated = true;
147
- },
148
- sendJsonRpc: (rpc) => {
149
- sendJsonRpcInterceptor(rpc);
150
- _receivedRequests.push(rpc);
151
- },
152
- nextJsonRpcResponse,
153
- jsonRpcResponses
154
- };
155
- }
156
-
157
- function getFakeClient () {
158
- const chains: MockChain[] = [];
159
- let addChainInterceptor: Promise<void> = Promise.resolve();
160
- let addWellKnownChainInterceptor: Promise<void> = Promise.resolve();
161
-
162
- return {
163
- _chains: () => chains,
164
- _setAddChainInterceptor: (interceptor: Promise<void>) => {
165
- addChainInterceptor = interceptor;
166
- },
167
- _setAddWellKnownChainInterceptor: (interceptor: Promise<void>) => {
168
- addWellKnownChainInterceptor = interceptor;
169
- },
170
- addChain: (chainSpec: string): Promise<MockChain> =>
171
- addChainInterceptor.then(() => {
172
- const result = getFakeChain(chainSpec);
173
-
174
- chains.push(result);
175
-
176
- return result;
177
- }),
178
- addWellKnownChain: (
179
- wellKnownChain: string
180
- ): Promise<MockChain> =>
181
- addWellKnownChainInterceptor.then(() => {
182
- const result = getFakeChain(wellKnownChain);
183
-
184
- chains.push(result);
185
-
186
- return result;
187
- })
188
- };
189
- }
190
-
191
- function connectorFactory (): MockSc {
192
- const clients: ReturnType<typeof getFakeClient>[] = [];
193
- const latestClient = () => clients[clients.length - 1];
194
-
195
- return {
196
- WellKnownChain,
197
- _clients: () => clients,
198
- createScClient: () => {
199
- const result = getFakeClient();
200
-
201
- clients.push(result);
202
-
203
- return result;
204
- },
205
- latestChain: () =>
206
- latestClient()._chains()[latestClient()._chains().length - 1],
207
- latestClient
208
- } as unknown as MockSc;
209
- }
210
-
211
- function setChainSyncyingStatus (isSyncing: boolean): void {
212
- getCurrentHealthChecker()._triggerHealthUpdate({
213
- isSyncing,
214
- peers: 1,
215
- shouldHavePeers: true
216
- });
217
- }
218
-
219
- let mockSc: MockSc;
220
- let mockedHealthChecker: ReturnType<typeof healthCheckerFactory>;
221
- const getCurrentHealthChecker = () => mockedHealthChecker._latestHealthChecker();
222
-
223
- describe('ScProvider', () => {
224
- beforeAll(() => {
225
- mockSc = connectorFactory();
226
- mockedHealthChecker = healthCheckerFactory();
227
- });
228
-
229
- describe('on', () => {
230
- it('emits `connected` as soon as the chain is not syncing', async () => {
231
- const provider = new ScProvider(mockSc, '');
232
-
233
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
234
-
235
- const onConnected = jest.fn();
236
-
237
- provider.on('connected', onConnected);
238
-
239
- expect(onConnected).not.toHaveBeenCalled();
240
- setChainSyncyingStatus(false);
241
- expect(onConnected).toHaveBeenCalled();
242
- });
243
-
244
- it('stops receiving notifications after unsubscribing', async () => {
245
- const provider = new ScProvider(mockSc, '');
246
-
247
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
248
-
249
- const onConnected = jest.fn();
250
-
251
- provider.on('connected', onConnected)();
252
- expect(onConnected).not.toHaveBeenCalled();
253
-
254
- setChainSyncyingStatus(false);
255
- expect(onConnected).not.toHaveBeenCalled();
256
- });
257
-
258
- it('synchronously emits connected if the Provider is already `connected`', async () => {
259
- const provider = new ScProvider(mockSc, '');
260
-
261
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
262
-
263
- setChainSyncyingStatus(false);
264
-
265
- const onConnected = jest.fn();
266
-
267
- provider.on('connected', onConnected);
268
- expect(onConnected).toHaveBeenCalled();
269
- });
270
-
271
- it('emits `disconnected` once the chain goes back to syncing', async () => {
272
- const provider = new ScProvider(mockSc, '');
273
-
274
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
275
-
276
- setChainSyncyingStatus(false);
277
-
278
- const onConnected = jest.fn();
279
- const onDisconnected = jest.fn();
280
-
281
- provider.on('connected', onConnected);
282
- provider.on('disconnected', onDisconnected);
283
-
284
- expect(onConnected).toHaveBeenCalled();
285
- expect(onDisconnected).not.toHaveBeenCalled();
286
-
287
- onConnected.mockReset();
288
- setChainSyncyingStatus(true);
289
-
290
- expect(onConnected).not.toHaveBeenCalled();
291
- expect(onDisconnected).toHaveBeenCalled();
292
- });
293
- });
294
-
295
- describe('hasSubscriptions', () => {
296
- it('supports subscriptions', async () => {
297
- const provider = new ScProvider(mockSc, '');
298
-
299
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
300
-
301
- expect(provider.hasSubscriptions).toBe(true);
302
- });
303
- });
304
-
305
- describe('clone', () => {
306
- it('can not be clonned', async () => {
307
- const provider = new ScProvider(mockSc, '');
308
-
309
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
310
-
311
- expect(() => provider.clone()).toThrow();
312
- });
313
- });
314
-
315
- describe('connect', () => {
316
- it('does not create a new chain when trying to re-connect while the current chain is syncing', async () => {
317
- const provider = new ScProvider(mockSc, '');
318
-
319
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
320
- const chain = mockSc.latestChain();
321
-
322
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
323
- expect(chain).toBe(mockSc.latestChain());
324
- });
325
-
326
- it('throws when trying to connect on an already connected Provider', async () => {
327
- const provider = new ScProvider(mockSc, '');
328
-
329
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
330
-
331
- setChainSyncyingStatus(false);
332
-
333
- await expect(
334
- provider.connect(undefined, mockedHealthChecker.healthChecker)
335
- ).rejects.toThrow(/Already connected/);
336
- });
337
- });
338
-
339
- describe('disconnect', () => {
340
- it('removes the chain and cleans up', async () => {
341
- const provider = new ScProvider(mockSc, '');
342
-
343
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
344
- const chain = mockSc.latestChain();
345
-
346
- await provider.disconnect();
347
-
348
- expect(chain._isTerminated()).toBe(true);
349
- });
350
-
351
- // eslint-disable-next-line jest/expect-expect
352
- it('does not throw when disconnecting on an already disconnected Provider', async () => {
353
- const provider = new ScProvider(mockSc, '');
354
-
355
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
356
- await provider.disconnect();
357
- await provider.disconnect();
358
- });
359
- });
360
-
361
- describe('send', () => {
362
- it('throws when trying to send a request while the Provider is not connected', async () => {
363
- const provider = new ScProvider(mockSc, '');
364
-
365
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
366
-
367
- await expect(provider.send('', [])).rejects.toThrow();
368
- });
369
-
370
- it('receives responses to its requests', async () => {
371
- const provider = new ScProvider(mockSc, '');
372
-
373
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
374
- const chain = mockSc.latestChain();
375
-
376
- setChainSyncyingStatus(false);
377
-
378
- const responsePromise = provider.send<unknown>('getData', ['foo']);
379
-
380
- await wait(0);
381
- expect(chain._getLatestRequest()).toEqual(
382
- '{"id":1,"jsonrpc":"2.0","method":"getData","params":["foo"]}'
383
- );
384
-
385
- const result = { foo: 'foo' };
386
-
387
- chain._triggerCallback({
388
- id: 1,
389
- jsonrpc: '2.0',
390
- result
391
- });
392
-
393
- const response = await responsePromise;
394
-
395
- expect(response).toEqual(result);
396
- });
397
-
398
- it("rejects when the response can't be deserialized", async () => {
399
- const provider = new ScProvider(mockSc, '');
400
-
401
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
402
- const chain = mockSc.latestChain();
403
-
404
- setChainSyncyingStatus(false);
405
-
406
- setTimeout(() => {
407
- chain._triggerCallback({
408
- id: 1,
409
- jsonrpc: '2.0'
410
- });
411
- }, 0);
412
-
413
- await expect(provider.send('getData', ['foo'])).rejects.toThrow();
414
- });
415
-
416
- it('rejects when the smoldot chain has crashed', async () => {
417
- const provider = new ScProvider(mockSc, '');
418
-
419
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
420
- const chain = mockSc.latestChain();
421
-
422
- setChainSyncyingStatus(false);
423
- await wait(0);
424
-
425
- chain._setSendJsonRpcInterceptor(() => {
426
- throw new Error('boom!');
427
- });
428
-
429
- await expect(
430
- provider.send('getData', ['foo'])
431
- ).rejects.toThrow(/Disconnected/);
432
- expect(provider.isConnected).toBe(false);
433
- });
434
- });
435
-
436
- describe('subscribe', () => {
437
- it('subscribes and recives messages until it unsubscribes', async () => {
438
- const provider = new ScProvider(mockSc, '');
439
-
440
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
441
- const chain = mockSc.latestChain();
442
-
443
- setChainSyncyingStatus(false);
444
-
445
- const unsubscribeToken = 'unsubscribeToken';
446
-
447
- setTimeout(() => {
448
- chain._triggerCallback({
449
- id: 1,
450
- jsonrpc: '2.0',
451
- result: unsubscribeToken
452
- });
453
- }, 0);
454
-
455
- const cb = jest.fn();
456
- const token = await provider.subscribe(
457
- 'foo',
458
- 'chain_subscribeNewHeads',
459
- ['baz'],
460
- cb
461
- );
462
-
463
- expect(token).toBe(unsubscribeToken);
464
- expect(cb).not.toHaveBeenCalled();
465
-
466
- chain._triggerCallback({
467
- jsonrpc: '2.0',
468
- method: 'foo',
469
- params: {
470
- result: 1,
471
- subscription: token
472
- }
473
- });
474
- expect(cb).toHaveBeenCalledTimes(1);
475
- expect(cb).toHaveBeenLastCalledWith(null, 1);
476
-
477
- chain._triggerCallback({
478
- jsonrpc: '2.0',
479
- method: 'foo',
480
- params: {
481
- result: 2,
482
- subscription: token
483
- }
484
- });
485
- expect(cb).toHaveBeenCalledTimes(2);
486
- expect(cb).toHaveBeenLastCalledWith(null, 2);
487
-
488
- provider
489
- .unsubscribe('foo', 'chain_unsubscribeNewHeads', unsubscribeToken)
490
- .catch(console.error);
491
-
492
- chain._triggerCallback({
493
- jsonrpc: '2.0',
494
- method: 'foo',
495
- params: {
496
- result: 3,
497
- subscription: token
498
- }
499
- });
500
- expect(cb).toHaveBeenCalledTimes(2);
501
- expect(cb).toHaveBeenLastCalledWith(null, 2);
502
- });
503
-
504
- it('ignores subscription messages that were received before the subscription token', async () => {
505
- const provider = new ScProvider(mockSc, '');
506
-
507
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
508
- const chain = mockSc.latestChain();
509
-
510
- setChainSyncyingStatus(false);
511
-
512
- const unsubscribeToken = 'unsubscribeToken';
513
-
514
- chain._triggerCallback({
515
- jsonrpc: '2.0',
516
- method: 'foo',
517
- params: {
518
- result: 1,
519
- subscription: unsubscribeToken
520
- }
521
- });
522
- setTimeout(() => {
523
- chain._triggerCallback({
524
- id: 1,
525
- jsonrpc: '2.0',
526
- result: unsubscribeToken
527
- });
528
- }, 0);
529
-
530
- const cb = jest.fn();
531
- const token = await provider.subscribe(
532
- 'foo',
533
- 'chain_subscribeNewHeads',
534
- ['baz'],
535
- cb
536
- );
537
-
538
- expect(token).toBe(unsubscribeToken);
539
- expect(cb).not.toHaveBeenCalled();
540
- });
541
-
542
- it('emits the error when the message has an error', async () => {
543
- const provider = new ScProvider(mockSc, '');
544
-
545
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
546
- const chain = mockSc.latestChain();
547
-
548
- setChainSyncyingStatus(false);
549
- await wait(0);
550
-
551
- const unsubscribeToken = 'unsubscribeToken';
552
-
553
- setTimeout(() => {
554
- chain._triggerCallback({
555
- id: 1,
556
- jsonrpc: '2.0',
557
- result: unsubscribeToken
558
- });
559
- }, 0);
560
-
561
- const cb = jest.fn();
562
- const token = await provider.subscribe(
563
- 'foo',
564
- 'chain_subscribeNewHeads',
565
- ['baz'],
566
- cb
567
- );
568
-
569
- chain._triggerCallback({
570
- jsonrpc: '2.0',
571
- method: 'foo',
572
- params: {
573
- error: 'boom',
574
- subscription: unsubscribeToken
575
- }
576
- });
577
-
578
- expect(token).toBe(unsubscribeToken);
579
- expect(cb).toHaveBeenCalledTimes(1);
580
- expect(cb).toHaveBeenLastCalledWith(expect.any(Error), undefined);
581
- });
582
-
583
- it('errors when subscribing to an unsupported method', async () => {
584
- const provider = new ScProvider(mockSc, '');
585
-
586
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
587
-
588
- setChainSyncyingStatus(false);
589
-
590
- await wait(0);
591
- await expect(
592
- provider.subscribe('foo', 'bar', ['baz'], () => undefined)
593
- ).rejects.toThrow(/Unsupported subscribe method: bar/);
594
- });
595
- });
596
-
597
- describe('unsubscribe', () => {
598
- it('rejects when trying to unsubscribe from un unexisting subscription', async () => {
599
- const provider = new ScProvider(mockSc, '');
600
-
601
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
602
-
603
- setChainSyncyingStatus(false);
604
-
605
- await expect(
606
- provider.unsubscribe('', '', '')
607
- ).rejects.toThrow(/Unable to find active subscription/);
608
- });
609
- });
610
-
611
- it('cleans up the stale subscriptions once it reconnects', async () => {
612
- const provider = new ScProvider(mockSc, '');
613
-
614
- await provider.connect(undefined, mockedHealthChecker.healthChecker);
615
- const chain = mockSc.latestChain();
616
-
617
- // setting the syncing status of the chain to fals so that the Provider
618
- // gets `connected`
619
- setChainSyncyingStatus(false);
620
-
621
- // while connected we create a subscription
622
- const unsubscribeToken = 'unsubscribeToken';
623
-
624
- setTimeout(() => {
625
- chain._triggerCallback({
626
- id: 1,
627
- jsonrpc: '2.0',
628
- result: unsubscribeToken
629
- });
630
- }, 0);
631
-
632
- const cb = jest.fn();
633
- const token = await provider.subscribe(
634
- 'foo',
635
- 'chain_subscribeNewHeads',
636
- ['baz'],
637
- cb
638
- );
639
-
640
- // setting the syncing status of the chain to fals so that the Provider
641
- // gets `disconnected`
642
- setChainSyncyingStatus(true);
643
-
644
- // let's wait some time in order to ensure that the stale unsubscription
645
- // messages are not sent until the chain syncing status changes back to false
646
- await wait(200);
647
-
648
- // before we let the healthChecker know that the chain is no longer syncing,
649
- // let's make sure that the chain has received the correct request, and
650
- // most importantly that it has not received a request for unsubscribing
651
- // from the stale subscription, since that request should happen once the
652
- // chain is no longer syncing
653
- expect(chain._recevedRequests()).toEqual([
654
- '{"id":1,"jsonrpc":"2.0","method":"chain_subscribeNewHeads","params":["baz"]}'
655
- ]);
656
-
657
- // lets change the sync status back to false
658
- setChainSyncyingStatus(false);
659
-
660
- // let's wait one tick to ensure that the microtasks got processed
661
- await wait(0);
662
-
663
- // let's make sure that we have now sent the request for killing the
664
- // stale subscription
665
- expect(chain._recevedRequests()).toEqual([
666
- '{"id":1,"jsonrpc":"2.0","method":"chain_subscribeNewHeads","params":["baz"]}',
667
- `{"id":2,"jsonrpc":"2.0","method":"chain_unsubscribeNewHeads","params":["${token}"]}`,
668
- '{"id":3,"jsonrpc":"2.0","method":"chain_subscribeNewHeads","params":["baz"]}'
669
- ]);
670
- });
671
- });