@pezkuwi/rpc-provider 16.5.5

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 (64) hide show
  1. package/README.md +68 -0
  2. package/build/bundle.d.ts +5 -0
  3. package/build/coder/error.d.ts +29 -0
  4. package/build/coder/index.d.ts +8 -0
  5. package/build/defaults.d.ts +5 -0
  6. package/build/http/index.d.ts +81 -0
  7. package/build/http/types.d.ts +7 -0
  8. package/build/index.d.ts +2 -0
  9. package/build/lru.d.ts +15 -0
  10. package/build/mock/index.d.ts +35 -0
  11. package/build/mock/mockHttp.d.ts +9 -0
  12. package/build/mock/mockWs.d.ts +26 -0
  13. package/build/mock/types.d.ts +23 -0
  14. package/build/packageDetect.d.ts +1 -0
  15. package/build/packageInfo.d.ts +6 -0
  16. package/build/substrate-connect/Health.d.ts +7 -0
  17. package/build/substrate-connect/index.d.ts +22 -0
  18. package/build/substrate-connect/types.d.ts +12 -0
  19. package/build/types.d.ts +85 -0
  20. package/build/ws/errors.d.ts +1 -0
  21. package/build/ws/index.d.ts +121 -0
  22. package/package.json +43 -0
  23. package/src/bundle.ts +8 -0
  24. package/src/coder/decodeResponse.spec.ts +70 -0
  25. package/src/coder/encodeJson.spec.ts +20 -0
  26. package/src/coder/encodeObject.spec.ts +25 -0
  27. package/src/coder/error.spec.ts +111 -0
  28. package/src/coder/error.ts +66 -0
  29. package/src/coder/index.ts +88 -0
  30. package/src/defaults.ts +10 -0
  31. package/src/http/index.spec.ts +72 -0
  32. package/src/http/index.ts +238 -0
  33. package/src/http/send.spec.ts +61 -0
  34. package/src/http/types.ts +11 -0
  35. package/src/index.ts +6 -0
  36. package/src/lru.spec.ts +74 -0
  37. package/src/lru.ts +197 -0
  38. package/src/mock/index.ts +259 -0
  39. package/src/mock/mockHttp.ts +35 -0
  40. package/src/mock/mockWs.ts +92 -0
  41. package/src/mock/on.spec.ts +43 -0
  42. package/src/mock/send.spec.ts +38 -0
  43. package/src/mock/subscribe.spec.ts +81 -0
  44. package/src/mock/types.ts +36 -0
  45. package/src/mock/unsubscribe.spec.ts +57 -0
  46. package/src/mod.ts +4 -0
  47. package/src/packageDetect.ts +12 -0
  48. package/src/packageInfo.ts +6 -0
  49. package/src/substrate-connect/Health.ts +325 -0
  50. package/src/substrate-connect/index.spec.ts +638 -0
  51. package/src/substrate-connect/index.ts +415 -0
  52. package/src/substrate-connect/types.ts +16 -0
  53. package/src/types.ts +101 -0
  54. package/src/ws/connect.spec.ts +167 -0
  55. package/src/ws/errors.ts +41 -0
  56. package/src/ws/index.spec.ts +97 -0
  57. package/src/ws/index.ts +652 -0
  58. package/src/ws/send.spec.ts +126 -0
  59. package/src/ws/state.spec.ts +20 -0
  60. package/src/ws/subscribe.spec.ts +68 -0
  61. package/src/ws/unsubscribe.spec.ts +100 -0
  62. package/tsconfig.build.json +17 -0
  63. package/tsconfig.build.tsbuildinfo +1 -0
  64. package/tsconfig.spec.json +18 -0
@@ -0,0 +1,126 @@
1
+ // Copyright 2017-2025 @polkadot/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 { Request } from '../mock/mockWs.js';
7
+ import type { Global, Mock } from '../mock/types.js';
8
+
9
+ import { mockWs } from '../mock/mockWs.js';
10
+ import { WsProvider } from './index.js';
11
+
12
+ declare const global: Global;
13
+
14
+ const TEST_WS_URL = 'ws://localhost-send.spec.ts:9965';
15
+
16
+ let provider: WsProvider | null;
17
+ let mock: Mock;
18
+
19
+ function createMock (requests: Request[]): void {
20
+ mock = mockWs(requests, TEST_WS_URL);
21
+ }
22
+
23
+ function createWs (autoConnect = 1000): Promise<WsProvider> {
24
+ provider = new WsProvider(TEST_WS_URL, autoConnect);
25
+
26
+ return provider.isReady;
27
+ }
28
+
29
+ describe('send', (): void => {
30
+ let globalWs: typeof WebSocket;
31
+
32
+ beforeEach((): void => {
33
+ globalWs = global.WebSocket;
34
+ });
35
+
36
+ afterEach(async () => {
37
+ global.WebSocket = globalWs;
38
+
39
+ if (mock) {
40
+ await mock.done();
41
+ }
42
+
43
+ if (provider) {
44
+ await provider.disconnect();
45
+ provider = null;
46
+ }
47
+ });
48
+
49
+ it('handles internal errors', (): Promise<any> => {
50
+ createMock([{
51
+ id: 1,
52
+ method: 'test_body',
53
+ reply: {
54
+ result: 'ok'
55
+ }
56
+ }]);
57
+
58
+ return createWs().then((ws) =>
59
+ ws
60
+ .send('test_encoding', [{ error: 'send error' }])
61
+ .catch((error): void => {
62
+ // eslint-disable-next-line jest/no-conditional-expect
63
+ expect((error as Error).message).toEqual('send error');
64
+ })
65
+ );
66
+ });
67
+
68
+ it('passes the body through correctly', (): Promise<void> => {
69
+ createMock([{
70
+ id: 1,
71
+ method: 'test_body',
72
+ reply: {
73
+ result: 'ok'
74
+ }
75
+ }]);
76
+
77
+ return createWs().then((ws) =>
78
+ ws
79
+ .send('test_body', ['param'])
80
+ .then((): void => {
81
+ expect(
82
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
83
+ (mock.body as any).test_body
84
+ ).toEqual('{"id":1,"jsonrpc":"2.0","method":"test_body","params":["param"]}');
85
+ })
86
+ );
87
+ });
88
+
89
+ it('throws error when !response.ok', (): Promise<any> => {
90
+ createMock([{
91
+ error: {
92
+ code: 666,
93
+ message: 'error'
94
+ },
95
+ id: 1,
96
+ method: 'something'
97
+ }]);
98
+
99
+ return createWs().then((ws) =>
100
+ ws
101
+ .send('test_error', [])
102
+ .catch((error): void => {
103
+ // eslint-disable-next-line jest/no-conditional-expect
104
+ expect((error as Error).message).toMatch(/666: error/);
105
+ })
106
+ );
107
+ });
108
+
109
+ it('adds subscriptions', (): Promise<void> => {
110
+ createMock([{
111
+ id: 1,
112
+ method: 'test_sub',
113
+ reply: {
114
+ result: 1
115
+ }
116
+ }]);
117
+
118
+ return createWs().then((ws) =>
119
+ ws
120
+ .send('test_sub', [])
121
+ .then((id): void => {
122
+ expect(id).toEqual(1);
123
+ })
124
+ );
125
+ });
126
+ });
@@ -0,0 +1,20 @@
1
+ // Copyright 2017-2025 @polkadot/rpc-provider authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@pezkuwi/dev-test/globals.d.ts" />
5
+
6
+ import { WsProvider } from './index.js';
7
+
8
+ describe('state', (): void => {
9
+ it('requires an ws:// prefixed endpoint', (): void => {
10
+ expect(
11
+ () => new WsProvider('http://', 0)
12
+ ).toThrow(/with 'ws/);
13
+ });
14
+
15
+ it('allows wss:// endpoints', (): void => {
16
+ expect(
17
+ () => new WsProvider('wss://', 0)
18
+ ).not.toThrow();
19
+ });
20
+ });
@@ -0,0 +1,68 @@
1
+ // Copyright 2017-2025 @polkadot/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 { Request } from '../mock/mockWs.js';
7
+ import type { Global, Mock } from './../mock/types.js';
8
+
9
+ import { mockWs } from '../mock/mockWs.js';
10
+ import { WsProvider } from './index.js';
11
+
12
+ declare const global: Global;
13
+
14
+ const TEST_WS_URL = 'ws://localhost-subscribe.test.ts:9933';
15
+
16
+ let provider: WsProvider | null;
17
+ let mock: Mock;
18
+
19
+ function createMock (requests: Request[]): void {
20
+ mock = mockWs(requests, TEST_WS_URL);
21
+ }
22
+
23
+ function createWs (autoConnect = 1000): Promise<WsProvider> {
24
+ provider = new WsProvider(TEST_WS_URL, autoConnect);
25
+
26
+ return provider.isReady;
27
+ }
28
+
29
+ describe('subscribe', (): void => {
30
+ let globalWs: typeof WebSocket;
31
+
32
+ beforeEach((): void => {
33
+ globalWs = global.WebSocket;
34
+ });
35
+
36
+ afterEach(async () => {
37
+ global.WebSocket = globalWs;
38
+
39
+ if (mock) {
40
+ await mock.done();
41
+ }
42
+
43
+ if (provider) {
44
+ await provider.disconnect();
45
+ provider = null;
46
+ }
47
+ });
48
+
49
+ it('adds subscriptions', (): Promise<void> => {
50
+ createMock([{
51
+ id: 1,
52
+ method: 'test_sub',
53
+ reply: {
54
+ result: 1
55
+ }
56
+ }]);
57
+
58
+ return createWs().then((ws) =>
59
+ ws
60
+ .subscribe('type', 'test_sub', [], (cb): void => {
61
+ expect(cb).toEqual(expect.anything());
62
+ })
63
+ .then((id): void => {
64
+ expect(id).toEqual(1);
65
+ })
66
+ );
67
+ });
68
+ });
@@ -0,0 +1,100 @@
1
+ // Copyright 2017-2025 @polkadot/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 { Request } from '../mock/mockWs.js';
7
+ import type { Global, Mock } from './../mock/types.js';
8
+
9
+ import { mockWs } from '../mock/mockWs.js';
10
+ import { WsProvider } from './index.js';
11
+
12
+ declare const global: Global;
13
+
14
+ const TEST_WS_URL = 'ws://localhost-unsubscribe.test.ts:9933';
15
+
16
+ let provider: WsProvider | null;
17
+ let mock: Mock;
18
+
19
+ function createMock (requests: Request[]): void {
20
+ mock = mockWs(requests, TEST_WS_URL);
21
+ }
22
+
23
+ function createWs (autoConnect = 1000): Promise<WsProvider> {
24
+ provider = new WsProvider(TEST_WS_URL, autoConnect);
25
+
26
+ return provider.isReady;
27
+ }
28
+
29
+ describe('subscribe', (): void => {
30
+ let globalWs: typeof WebSocket;
31
+
32
+ beforeEach((): void => {
33
+ globalWs = global.WebSocket;
34
+ });
35
+
36
+ afterEach(async () => {
37
+ global.WebSocket = globalWs;
38
+
39
+ if (mock) {
40
+ await mock.done();
41
+ }
42
+
43
+ if (provider) {
44
+ await provider.disconnect();
45
+ provider = null;
46
+ }
47
+ });
48
+
49
+ it('removes subscriptions', async (): Promise<void> => {
50
+ createMock([
51
+ {
52
+ id: 1,
53
+ method: 'subscribe_test',
54
+ reply: {
55
+ result: 1
56
+ }
57
+ },
58
+ {
59
+ id: 2,
60
+ method: 'unsubscribe_test',
61
+ reply: {
62
+ result: true
63
+ }
64
+ }
65
+ ]);
66
+
67
+ await createWs().then((ws) =>
68
+ ws
69
+ .subscribe('test', 'subscribe_test', [], (cb): void => {
70
+ expect(cb).toEqual(expect.anything());
71
+ })
72
+ .then((id): Promise<boolean> => {
73
+ return ws.unsubscribe('test', 'subscribe_test', id);
74
+ })
75
+ );
76
+ });
77
+
78
+ it('fails when sub not found', (): Promise<void> => {
79
+ createMock([{
80
+ id: 1,
81
+ method: 'subscribe_test',
82
+ reply: {
83
+ result: 1
84
+ }
85
+ }]);
86
+
87
+ return createWs().then((ws) =>
88
+ ws
89
+ .subscribe('test', 'subscribe_test', [], (cb): void => {
90
+ expect(cb).toEqual(expect.anything());
91
+ })
92
+ .then((): Promise<boolean> => {
93
+ return ws.unsubscribe('test', 'subscribe_test', 111);
94
+ })
95
+ .then((result): void => {
96
+ expect(result).toBe(false);
97
+ })
98
+ );
99
+ });
100
+ });
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "baseUrl": "..",
5
+ "outDir": "./build",
6
+ "rootDir": "./src",
7
+ "resolveJsonModule": true
8
+ },
9
+ "exclude": [
10
+ "**/*.spec.ts",
11
+ "**/mod.ts"
12
+ ],
13
+ "references": [
14
+ { "path": "../types/tsconfig.build.json" },
15
+ { "path": "../types-support/tsconfig.build.json" }
16
+ ]
17
+ }