@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,41 @@
1
+ // Copyright 2017-2025 @polkadot/rpc-provider authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // from https://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed-with-close-code-1006
5
+
6
+ const known: Record<number, string> = {
7
+ 1000: 'Normal Closure',
8
+ 1001: 'Going Away',
9
+ 1002: 'Protocol Error',
10
+ 1003: 'Unsupported Data',
11
+ 1004: '(For future)',
12
+ 1005: 'No Status Received',
13
+ 1006: 'Abnormal Closure',
14
+ 1007: 'Invalid frame payload data',
15
+ 1008: 'Policy Violation',
16
+ 1009: 'Message too big',
17
+ 1010: 'Missing Extension',
18
+ 1011: 'Internal Error',
19
+ 1012: 'Service Restart',
20
+ 1013: 'Try Again Later',
21
+ 1014: 'Bad Gateway',
22
+ 1015: 'TLS Handshake'
23
+ };
24
+
25
+ export function getWSErrorString (code: number): string {
26
+ if (code >= 0 && code <= 999) {
27
+ return '(Unused)';
28
+ } else if (code >= 1016) {
29
+ if (code <= 1999) {
30
+ return '(For WebSocket standard)';
31
+ } else if (code <= 2999) {
32
+ return '(For WebSocket extensions)';
33
+ } else if (code <= 3999) {
34
+ return '(For libraries and frameworks)';
35
+ } else if (code <= 4999) {
36
+ return '(For applications)';
37
+ }
38
+ }
39
+
40
+ return known[code] || '(Unknown)';
41
+ }
@@ -0,0 +1,97 @@
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 { Mock } from '../mock/types.js';
8
+
9
+ import { mockWs } from '../mock/mockWs.js';
10
+ import { WsProvider } from './index.js';
11
+
12
+ const TEST_WS_URL = 'ws://localhost-index.spec.ts:9977';
13
+
14
+ let provider: WsProvider | null;
15
+ let mock: Mock;
16
+
17
+ function createWs (requests: Request[], autoConnect = 1000, headers?: Record<string, string>, timeout?: number): WsProvider {
18
+ mock = mockWs(requests, TEST_WS_URL);
19
+ provider = new WsProvider(TEST_WS_URL, autoConnect, headers, timeout);
20
+
21
+ return provider;
22
+ }
23
+
24
+ describe('Ws', (): void => {
25
+ afterEach(async () => {
26
+ if (mock) {
27
+ await mock.done();
28
+ }
29
+
30
+ if (provider) {
31
+ await provider.disconnect();
32
+ provider = null;
33
+ }
34
+ });
35
+
36
+ it('returns the connected state', (): void => {
37
+ expect(
38
+ createWs([]).isConnected
39
+ ).toEqual(false);
40
+ });
41
+
42
+ // eslint-disable-next-line jest/expect-expect
43
+ it('allows you to initialize the provider with custom headers', () => {
44
+ createWs([], 100, { foo: 'bar' });
45
+ });
46
+
47
+ // eslint-disable-next-line jest/expect-expect
48
+ it('allows you to set custom timeout value for handlers', () => {
49
+ const CUSTOM_TIMEOUT_S = 90;
50
+ const CUSTOM_TIMEOUT_MS = CUSTOM_TIMEOUT_S * 1000;
51
+
52
+ createWs([], 100, { foo: 'bar' }, CUSTOM_TIMEOUT_MS);
53
+ });
54
+ });
55
+
56
+ describe('Endpoint Parsing', (): void => {
57
+ // eslint-disable-next-line jest/expect-expect
58
+ it('Succeeds when WsProvider endpoint is a valid string', () => {
59
+ /* eslint-disable no-new */
60
+ new WsProvider(TEST_WS_URL, 0);
61
+ });
62
+
63
+ it('should throw error on negative cache capacity or TTL', () => {
64
+ expect(() => new WsProvider(TEST_WS_URL, false, {}, undefined, -5, 30000)).toThrow(/'capacity' must be a non-negative integer/);
65
+ expect(() => new WsProvider(TEST_WS_URL, false, {}, undefined, 1024, -1000)).toThrow(/'ttl' must be between 0 and 1800000 ms or null to disable/);
66
+ });
67
+
68
+ it('Throws when WsProvider endpoint is an invalid string', () => {
69
+ expect(
70
+ () => new WsProvider('http://127.0.0.1:9955', 0)
71
+ ).toThrow(/^Endpoint should start with /);
72
+ });
73
+
74
+ // eslint-disable-next-line jest/expect-expect
75
+ it('Succeeds when WsProvider endpoint is a valid array', () => {
76
+ const endpoints: string[] = ['ws://127.0.0.1:9955', 'wss://testnet.io:9944', 'ws://mychain.com:9933'];
77
+
78
+ /* eslint-disable no-new */
79
+ new WsProvider(endpoints, 0);
80
+ });
81
+
82
+ it('Throws when WsProvider endpoint is an empty array', () => {
83
+ const endpoints: string[] = [];
84
+
85
+ expect(
86
+ () => new WsProvider(endpoints, 0)
87
+ ).toThrow('WsProvider requires at least one Endpoint');
88
+ });
89
+
90
+ it('Throws when WsProvider endpoint is an invalid array', () => {
91
+ const endpoints: string[] = ['ws://127.0.0.1:9955', 'http://bad.co:9944', 'ws://mychain.com:9933'];
92
+
93
+ expect(
94
+ () => new WsProvider(endpoints, 0)
95
+ ).toThrow(/^Endpoint should start with /);
96
+ });
97
+ });