@yellow-org/sdk 1.0.1-alpha.0

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 (72) hide show
  1. package/README.md +999 -0
  2. package/dist/abis/generated.d.ts +4300 -0
  3. package/dist/abis/generated.js +1 -0
  4. package/dist/app/index.d.ts +2 -0
  5. package/dist/app/index.js +2 -0
  6. package/dist/app/packing.d.ts +7 -0
  7. package/dist/app/packing.js +111 -0
  8. package/dist/app/types.d.ts +91 -0
  9. package/dist/app/types.js +42 -0
  10. package/dist/asset_store.d.ts +13 -0
  11. package/dist/asset_store.js +121 -0
  12. package/dist/blockchain/evm/channel_hub_abi.d.ts +4284 -0
  13. package/dist/blockchain/evm/channel_hub_abi.js +5475 -0
  14. package/dist/blockchain/evm/client.d.ts +46 -0
  15. package/dist/blockchain/evm/client.js +428 -0
  16. package/dist/blockchain/evm/erc20.d.ts +13 -0
  17. package/dist/blockchain/evm/erc20.js +54 -0
  18. package/dist/blockchain/evm/erc20_abi.d.ts +144 -0
  19. package/dist/blockchain/evm/erc20_abi.js +96 -0
  20. package/dist/blockchain/evm/index.d.ts +6 -0
  21. package/dist/blockchain/evm/index.js +6 -0
  22. package/dist/blockchain/evm/interface.d.ts +10 -0
  23. package/dist/blockchain/evm/interface.js +1 -0
  24. package/dist/blockchain/evm/types.d.ts +27 -0
  25. package/dist/blockchain/evm/types.js +1 -0
  26. package/dist/blockchain/evm/utils.d.ts +9 -0
  27. package/dist/blockchain/evm/utils.js +129 -0
  28. package/dist/blockchain/index.d.ts +1 -0
  29. package/dist/blockchain/index.js +1 -0
  30. package/dist/client.d.ts +99 -0
  31. package/dist/client.js +720 -0
  32. package/dist/config.d.ts +12 -0
  33. package/dist/config.js +28 -0
  34. package/dist/core/event.d.ts +29 -0
  35. package/dist/core/event.js +1 -0
  36. package/dist/core/index.d.ts +6 -0
  37. package/dist/core/index.js +6 -0
  38. package/dist/core/interface.d.ts +53 -0
  39. package/dist/core/interface.js +1 -0
  40. package/dist/core/state.d.ts +21 -0
  41. package/dist/core/state.js +267 -0
  42. package/dist/core/state_packer.d.ts +13 -0
  43. package/dist/core/state_packer.js +98 -0
  44. package/dist/core/types.d.ts +203 -0
  45. package/dist/core/types.js +220 -0
  46. package/dist/core/utils.d.ts +16 -0
  47. package/dist/core/utils.js +181 -0
  48. package/dist/index.d.ts +9 -0
  49. package/dist/index.js +9 -0
  50. package/dist/rpc/api.d.ts +181 -0
  51. package/dist/rpc/api.js +1 -0
  52. package/dist/rpc/client.d.ts +36 -0
  53. package/dist/rpc/client.js +102 -0
  54. package/dist/rpc/dialer.d.ts +30 -0
  55. package/dist/rpc/dialer.js +146 -0
  56. package/dist/rpc/error.d.ts +18 -0
  57. package/dist/rpc/error.js +27 -0
  58. package/dist/rpc/index.d.ts +7 -0
  59. package/dist/rpc/index.js +7 -0
  60. package/dist/rpc/message.d.ts +25 -0
  61. package/dist/rpc/message.js +102 -0
  62. package/dist/rpc/methods.d.ts +30 -0
  63. package/dist/rpc/methods.js +27 -0
  64. package/dist/rpc/types.d.ts +101 -0
  65. package/dist/rpc/types.js +1 -0
  66. package/dist/signers.d.ts +48 -0
  67. package/dist/signers.js +96 -0
  68. package/dist/utils/sign.d.ts +2 -0
  69. package/dist/utils/sign.js +8 -0
  70. package/dist/utils.d.ts +42 -0
  71. package/dist/utils.js +226 -0
  72. package/package.json +89 -0
@@ -0,0 +1,181 @@
1
+ import { Address } from 'viem';
2
+ import { ChannelV1, ChannelDefinitionV1, ChannelSessionKeyStateV1, StateV1, BalanceEntryV1, TransactionV1, PaginationParamsV1, PaginationMetadataV1, AssetV1, BlockchainInfoV1 } from './types';
3
+ import { AppDefinitionV1, AppStateUpdateV1, AppSessionInfoV1, AppAllocationV1, AppSessionKeyStateV1, SignedAppStateUpdateV1 } from '../app/types';
4
+ import { TransactionType } from '../core/types';
5
+ export interface ChannelsV1GetHomeChannelRequest {
6
+ wallet: Address;
7
+ asset: string;
8
+ }
9
+ export interface ChannelsV1GetHomeChannelResponse {
10
+ channel: ChannelV1;
11
+ }
12
+ export interface ChannelsV1GetEscrowChannelRequest {
13
+ escrow_channel_id: string;
14
+ }
15
+ export interface ChannelsV1GetEscrowChannelResponse {
16
+ channel: ChannelV1;
17
+ }
18
+ export interface ChannelsV1GetChannelsRequest {
19
+ wallet: Address;
20
+ status?: string;
21
+ asset?: string;
22
+ channel_type?: string;
23
+ pagination?: PaginationParamsV1;
24
+ }
25
+ export interface ChannelsV1GetChannelsResponse {
26
+ channels: ChannelV1[];
27
+ metadata: PaginationMetadataV1;
28
+ }
29
+ export interface ChannelsV1GetLatestStateRequest {
30
+ wallet: Address;
31
+ asset: string;
32
+ only_signed: boolean;
33
+ }
34
+ export interface ChannelsV1GetLatestStateResponse {
35
+ state: StateV1;
36
+ }
37
+ export interface ChannelsV1GetStatesRequest {
38
+ wallet: Address;
39
+ asset: string;
40
+ epoch?: bigint;
41
+ channel_id?: string;
42
+ only_signed: boolean;
43
+ pagination?: PaginationParamsV1;
44
+ }
45
+ export interface ChannelsV1GetStatesResponse {
46
+ states: StateV1[];
47
+ metadata: PaginationMetadataV1;
48
+ }
49
+ export interface ChannelsV1RequestCreationRequest {
50
+ state: StateV1;
51
+ channel_definition: ChannelDefinitionV1;
52
+ }
53
+ export interface ChannelsV1RequestCreationResponse {
54
+ signature: string;
55
+ }
56
+ export interface ChannelsV1SubmitStateRequest {
57
+ state: StateV1;
58
+ }
59
+ export interface ChannelsV1SubmitStateResponse {
60
+ signature: string;
61
+ }
62
+ export interface ChannelsV1HomeChannelCreatedEvent {
63
+ channel: ChannelV1;
64
+ initial_state: StateV1;
65
+ }
66
+ export interface ChannelsV1SubmitSessionKeyStateRequest {
67
+ state: ChannelSessionKeyStateV1;
68
+ }
69
+ export interface ChannelsV1SubmitSessionKeyStateResponse {
70
+ }
71
+ export interface ChannelsV1GetLastKeyStatesRequest {
72
+ user_address: string;
73
+ session_key?: string;
74
+ }
75
+ export interface ChannelsV1GetLastKeyStatesResponse {
76
+ states: ChannelSessionKeyStateV1[];
77
+ }
78
+ export interface AppSessionsV1SubmitDepositStateRequest {
79
+ app_state_update: AppStateUpdateV1;
80
+ quorum_sigs: string[];
81
+ user_state: StateV1;
82
+ }
83
+ export interface AppSessionsV1SubmitDepositStateResponse {
84
+ signature: string;
85
+ }
86
+ export interface AppSessionsV1SubmitAppStateRequest {
87
+ app_state_update: AppStateUpdateV1;
88
+ quorum_sigs: string[];
89
+ }
90
+ export interface AppSessionsV1SubmitAppStateResponse {
91
+ }
92
+ export interface AppSessionsV1RebalanceAppSessionsRequest {
93
+ signed_updates: SignedAppStateUpdateV1[];
94
+ }
95
+ export interface AppSessionsV1RebalanceAppSessionsResponse {
96
+ batch_id: string;
97
+ }
98
+ export interface AppSessionsV1GetAppDefinitionRequest {
99
+ app_session_id: string;
100
+ }
101
+ export interface AppSessionsV1GetAppDefinitionResponse {
102
+ definition: AppDefinitionV1;
103
+ }
104
+ export interface AppSessionsV1GetAppSessionsRequest {
105
+ app_session_id?: string;
106
+ participant?: Address;
107
+ status?: string;
108
+ pagination?: PaginationParamsV1;
109
+ }
110
+ export interface AppSessionsV1GetAppSessionsResponse {
111
+ app_sessions: AppSessionInfoV1[];
112
+ metadata: PaginationMetadataV1;
113
+ }
114
+ export interface AppSessionsV1CreateAppSessionRequest {
115
+ definition: AppDefinitionV1;
116
+ session_data: string;
117
+ quorum_sigs?: string[];
118
+ }
119
+ export interface AppSessionsV1CreateAppSessionResponse {
120
+ app_session_id: string;
121
+ version: string;
122
+ status: string;
123
+ }
124
+ export interface AppSessionsV1CloseAppSessionRequest {
125
+ app_session_id: string;
126
+ allocations: AppAllocationV1[];
127
+ session_data?: string;
128
+ }
129
+ export interface AppSessionsV1CloseAppSessionResponse {
130
+ app_session_id: string;
131
+ version: string;
132
+ status: string;
133
+ }
134
+ export interface AppSessionsV1SubmitSessionKeyStateRequest {
135
+ state: AppSessionKeyStateV1;
136
+ }
137
+ export interface AppSessionsV1SubmitSessionKeyStateResponse {
138
+ }
139
+ export interface AppSessionsV1GetLastKeyStatesRequest {
140
+ user_address: string;
141
+ session_key?: string;
142
+ }
143
+ export interface AppSessionsV1GetLastKeyStatesResponse {
144
+ states: AppSessionKeyStateV1[];
145
+ }
146
+ export interface UserV1GetBalancesRequest {
147
+ wallet: Address;
148
+ }
149
+ export interface UserV1GetBalancesResponse {
150
+ balances: BalanceEntryV1[];
151
+ }
152
+ export interface UserV1GetTransactionsRequest {
153
+ wallet: Address;
154
+ asset?: string;
155
+ tx_type?: TransactionType;
156
+ pagination?: PaginationParamsV1;
157
+ from_time?: bigint;
158
+ to_time?: bigint;
159
+ }
160
+ export interface UserV1GetTransactionsResponse {
161
+ transactions: TransactionV1[];
162
+ metadata: PaginationMetadataV1;
163
+ }
164
+ export interface NodeV1PingRequest {
165
+ }
166
+ export interface NodeV1PingResponse {
167
+ }
168
+ export interface NodeV1GetConfigRequest {
169
+ }
170
+ export interface NodeV1GetConfigResponse {
171
+ node_address: Address;
172
+ node_version: string;
173
+ supported_sig_validators: number[];
174
+ blockchains: BlockchainInfoV1[];
175
+ }
176
+ export interface NodeV1GetAssetsRequest {
177
+ blockchain_id?: bigint;
178
+ }
179
+ export interface NodeV1GetAssetsResponse {
180
+ assets: AssetV1[];
181
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ import { Dialer } from './dialer';
2
+ import { Message } from './message';
3
+ import * as API from './api';
4
+ export declare class RPCClient {
5
+ private dialer;
6
+ constructor(dialer: Dialer);
7
+ start(url: string, handleClosure: (err?: Error) => void): Promise<void>;
8
+ private call;
9
+ channelsV1GetHomeChannel(req: API.ChannelsV1GetHomeChannelRequest, signal?: AbortSignal): Promise<API.ChannelsV1GetHomeChannelResponse>;
10
+ channelsV1GetEscrowChannel(req: API.ChannelsV1GetEscrowChannelRequest, signal?: AbortSignal): Promise<API.ChannelsV1GetEscrowChannelResponse>;
11
+ channelsV1GetChannels(req: API.ChannelsV1GetChannelsRequest, signal?: AbortSignal): Promise<API.ChannelsV1GetChannelsResponse>;
12
+ channelsV1GetLatestState(req: API.ChannelsV1GetLatestStateRequest, signal?: AbortSignal): Promise<API.ChannelsV1GetLatestStateResponse>;
13
+ channelsV1GetStates(req: API.ChannelsV1GetStatesRequest, signal?: AbortSignal): Promise<API.ChannelsV1GetStatesResponse>;
14
+ channelsV1RequestCreation(req: API.ChannelsV1RequestCreationRequest, signal?: AbortSignal): Promise<API.ChannelsV1RequestCreationResponse>;
15
+ channelsV1SubmitState(req: API.ChannelsV1SubmitStateRequest, signal?: AbortSignal): Promise<API.ChannelsV1SubmitStateResponse>;
16
+ channelsV1SubmitSessionKeyState(req: API.ChannelsV1SubmitSessionKeyStateRequest, signal?: AbortSignal): Promise<API.ChannelsV1SubmitSessionKeyStateResponse>;
17
+ channelsV1GetLastKeyStates(req: API.ChannelsV1GetLastKeyStatesRequest, signal?: AbortSignal): Promise<API.ChannelsV1GetLastKeyStatesResponse>;
18
+ appSessionsV1SubmitDepositState(req: API.AppSessionsV1SubmitDepositStateRequest, signal?: AbortSignal): Promise<API.AppSessionsV1SubmitDepositStateResponse>;
19
+ appSessionsV1SubmitAppState(req: API.AppSessionsV1SubmitAppStateRequest, signal?: AbortSignal): Promise<API.AppSessionsV1SubmitAppStateResponse>;
20
+ appSessionsV1RebalanceAppSessions(req: API.AppSessionsV1RebalanceAppSessionsRequest, signal?: AbortSignal): Promise<API.AppSessionsV1RebalanceAppSessionsResponse>;
21
+ appSessionsV1GetAppDefinition(req: API.AppSessionsV1GetAppDefinitionRequest, signal?: AbortSignal): Promise<API.AppSessionsV1GetAppDefinitionResponse>;
22
+ appSessionsV1GetAppSessions(req: API.AppSessionsV1GetAppSessionsRequest, signal?: AbortSignal): Promise<API.AppSessionsV1GetAppSessionsResponse>;
23
+ appSessionsV1CreateAppSession(req: API.AppSessionsV1CreateAppSessionRequest, signal?: AbortSignal): Promise<API.AppSessionsV1CreateAppSessionResponse>;
24
+ appSessionsV1CloseAppSession(req: API.AppSessionsV1CloseAppSessionRequest, signal?: AbortSignal): Promise<API.AppSessionsV1CloseAppSessionResponse>;
25
+ appSessionsV1SubmitSessionKeyState(req: API.AppSessionsV1SubmitSessionKeyStateRequest, signal?: AbortSignal): Promise<API.AppSessionsV1SubmitSessionKeyStateResponse>;
26
+ appSessionsV1GetLastKeyStates(req: API.AppSessionsV1GetLastKeyStatesRequest, signal?: AbortSignal): Promise<API.AppSessionsV1GetLastKeyStatesResponse>;
27
+ userV1GetBalances(req: API.UserV1GetBalancesRequest, signal?: AbortSignal): Promise<API.UserV1GetBalancesResponse>;
28
+ userV1GetTransactions(req: API.UserV1GetTransactionsRequest, signal?: AbortSignal): Promise<API.UserV1GetTransactionsResponse>;
29
+ nodeV1Ping(signal?: AbortSignal): Promise<void>;
30
+ nodeV1GetConfig(signal?: AbortSignal): Promise<API.NodeV1GetConfigResponse>;
31
+ nodeV1GetAssets(req: API.NodeV1GetAssetsRequest, signal?: AbortSignal): Promise<API.NodeV1GetAssetsResponse>;
32
+ close(): Promise<void>;
33
+ isConnected(): boolean;
34
+ eventChannel(): AsyncIterable<Message>;
35
+ }
36
+ export declare function newRPCClient(dialer: Dialer): RPCClient;
@@ -0,0 +1,102 @@
1
+ import { newRequest, newPayload, messageError, translatePayload } from './message';
2
+ import * as Methods from './methods';
3
+ export class RPCClient {
4
+ constructor(dialer) {
5
+ this.dialer = dialer;
6
+ }
7
+ async start(url, handleClosure) {
8
+ return this.dialer.dial(url, handleClosure);
9
+ }
10
+ async call(method, req, signal) {
11
+ const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
12
+ const payload = newPayload(req);
13
+ const message = newRequest(requestId, method, payload);
14
+ const response = await this.dialer.call(message, signal);
15
+ const err = messageError(response);
16
+ if (err) {
17
+ throw new Error(`rpc returned error: ${err.message}`);
18
+ }
19
+ return translatePayload(response.payload);
20
+ }
21
+ async channelsV1GetHomeChannel(req, signal) {
22
+ return this.call(Methods.ChannelsV1GetHomeChannelMethod, req, signal);
23
+ }
24
+ async channelsV1GetEscrowChannel(req, signal) {
25
+ return this.call(Methods.ChannelsV1GetEscrowChannelMethod, req, signal);
26
+ }
27
+ async channelsV1GetChannels(req, signal) {
28
+ return this.call(Methods.ChannelsV1GetChannelsMethod, req, signal);
29
+ }
30
+ async channelsV1GetLatestState(req, signal) {
31
+ return this.call(Methods.ChannelsV1GetLatestStateMethod, req, signal);
32
+ }
33
+ async channelsV1GetStates(req, signal) {
34
+ return this.call(Methods.ChannelsV1GetStatesMethod, req, signal);
35
+ }
36
+ async channelsV1RequestCreation(req, signal) {
37
+ return this.call(Methods.ChannelsV1RequestCreationMethod, req, signal);
38
+ }
39
+ async channelsV1SubmitState(req, signal) {
40
+ return this.call(Methods.ChannelsV1SubmitStateMethod, req, signal);
41
+ }
42
+ async channelsV1SubmitSessionKeyState(req, signal) {
43
+ return this.call(Methods.ChannelsV1SubmitSessionKeyStateMethod, req, signal);
44
+ }
45
+ async channelsV1GetLastKeyStates(req, signal) {
46
+ return this.call(Methods.ChannelsV1GetLastKeyStatesMethod, req, signal);
47
+ }
48
+ async appSessionsV1SubmitDepositState(req, signal) {
49
+ return this.call(Methods.AppSessionsV1SubmitDepositStateMethod, req, signal);
50
+ }
51
+ async appSessionsV1SubmitAppState(req, signal) {
52
+ return this.call(Methods.AppSessionsV1SubmitAppStateMethod, req, signal);
53
+ }
54
+ async appSessionsV1RebalanceAppSessions(req, signal) {
55
+ return this.call(Methods.AppSessionsV1RebalanceAppSessionsMethod, req, signal);
56
+ }
57
+ async appSessionsV1GetAppDefinition(req, signal) {
58
+ return this.call(Methods.AppSessionsV1GetAppDefinitionMethod, req, signal);
59
+ }
60
+ async appSessionsV1GetAppSessions(req, signal) {
61
+ return this.call(Methods.AppSessionsV1GetAppSessionsMethod, req, signal);
62
+ }
63
+ async appSessionsV1CreateAppSession(req, signal) {
64
+ return this.call(Methods.AppSessionsV1CreateAppSessionMethod, req, signal);
65
+ }
66
+ async appSessionsV1CloseAppSession(req, signal) {
67
+ return this.call(Methods.AppSessionsV1CloseAppSessionMethod, req, signal);
68
+ }
69
+ async appSessionsV1SubmitSessionKeyState(req, signal) {
70
+ return this.call(Methods.AppSessionsV1SubmitSessionKeyStateMethod, req, signal);
71
+ }
72
+ async appSessionsV1GetLastKeyStates(req, signal) {
73
+ return this.call(Methods.AppSessionsV1GetLastKeyStatesMethod, req, signal);
74
+ }
75
+ async userV1GetBalances(req, signal) {
76
+ return this.call(Methods.UserV1GetBalancesMethod, req, signal);
77
+ }
78
+ async userV1GetTransactions(req, signal) {
79
+ return this.call(Methods.UserV1GetTransactionsMethod, req, signal);
80
+ }
81
+ async nodeV1Ping(signal) {
82
+ await this.call(Methods.NodeV1PingMethod, {}, signal);
83
+ }
84
+ async nodeV1GetConfig(signal) {
85
+ return this.call(Methods.NodeV1GetConfigMethod, {}, signal);
86
+ }
87
+ async nodeV1GetAssets(req, signal) {
88
+ return this.call(Methods.NodeV1GetAssetsMethod, req, signal);
89
+ }
90
+ async close() {
91
+ return this.dialer.close();
92
+ }
93
+ isConnected() {
94
+ return this.dialer.isConnected();
95
+ }
96
+ eventChannel() {
97
+ return this.dialer.eventChannel();
98
+ }
99
+ }
100
+ export function newRPCClient(dialer) {
101
+ return new RPCClient(dialer);
102
+ }
@@ -0,0 +1,30 @@
1
+ import { Message } from './message';
2
+ export interface Dialer {
3
+ dial(url: string, handleClosure: (err?: Error) => void): Promise<void>;
4
+ isConnected(): boolean;
5
+ call(req: Message, signal?: AbortSignal): Promise<Message>;
6
+ eventChannel(): AsyncIterable<Message>;
7
+ close(): Promise<void>;
8
+ }
9
+ export interface WebsocketDialerConfig {
10
+ handshakeTimeout: number;
11
+ eventChanSize: number;
12
+ }
13
+ export declare const DefaultWebsocketDialerConfig: WebsocketDialerConfig;
14
+ export declare class WebsocketDialer implements Dialer {
15
+ private config;
16
+ private ws;
17
+ private responseSinks;
18
+ private eventQueue;
19
+ private eventResolvers;
20
+ private closeHandler?;
21
+ constructor(config?: WebsocketDialerConfig);
22
+ dial(url: string, handleClosure: (err?: Error) => void): Promise<void>;
23
+ isConnected(): boolean;
24
+ call(req: Message, signal?: AbortSignal): Promise<Message>;
25
+ eventChannel(): AsyncIterable<Message>;
26
+ close(): Promise<void>;
27
+ private handleMessage;
28
+ private handleClose;
29
+ }
30
+ export declare function newWebsocketDialer(config?: WebsocketDialerConfig): WebsocketDialer;
@@ -0,0 +1,146 @@
1
+ import { marshalMessage, unmarshalMessage } from './message';
2
+ import { ErrAlreadyConnected, ErrNotConnected } from './error';
3
+ export const DefaultWebsocketDialerConfig = {
4
+ handshakeTimeout: 5000,
5
+ eventChanSize: 100,
6
+ };
7
+ export class WebsocketDialer {
8
+ constructor(config = DefaultWebsocketDialerConfig) {
9
+ this.ws = null;
10
+ this.responseSinks = new Map();
11
+ this.eventQueue = [];
12
+ this.eventResolvers = [];
13
+ this.config = config;
14
+ }
15
+ async dial(url, handleClosure) {
16
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
17
+ throw ErrAlreadyConnected;
18
+ }
19
+ this.closeHandler = handleClosure;
20
+ return new Promise((resolve, reject) => {
21
+ const ws = new WebSocket(url);
22
+ const timeout = setTimeout(() => {
23
+ ws.close();
24
+ reject(new Error('WebSocket handshake timeout'));
25
+ }, this.config.handshakeTimeout);
26
+ ws.onopen = () => {
27
+ clearTimeout(timeout);
28
+ this.ws = ws;
29
+ resolve();
30
+ };
31
+ ws.onerror = () => {
32
+ clearTimeout(timeout);
33
+ const error = new Error('WebSocket error');
34
+ if (this.ws === ws) {
35
+ this.handleClose(error);
36
+ }
37
+ reject(error);
38
+ };
39
+ ws.onclose = () => {
40
+ clearTimeout(timeout);
41
+ if (this.ws === ws) {
42
+ this.handleClose();
43
+ }
44
+ };
45
+ ws.onmessage = (event) => {
46
+ this.handleMessage(event.data);
47
+ };
48
+ });
49
+ }
50
+ isConnected() {
51
+ return this.ws !== null && this.ws.readyState === WebSocket.OPEN;
52
+ }
53
+ async call(req, signal) {
54
+ if (!this.isConnected()) {
55
+ throw ErrNotConnected;
56
+ }
57
+ return new Promise((resolve, reject) => {
58
+ if (signal?.aborted) {
59
+ reject(new Error('Request aborted'));
60
+ return;
61
+ }
62
+ const abortHandler = () => {
63
+ this.responseSinks.delete(req.requestId);
64
+ reject(new Error('Request aborted'));
65
+ };
66
+ signal?.addEventListener('abort', abortHandler, { once: true });
67
+ this.responseSinks.set(req.requestId, (msg) => {
68
+ signal?.removeEventListener('abort', abortHandler);
69
+ resolve(msg);
70
+ });
71
+ try {
72
+ const data = marshalMessage(req);
73
+ this.ws.send(data);
74
+ }
75
+ catch (error) {
76
+ this.responseSinks.delete(req.requestId);
77
+ signal?.removeEventListener('abort', abortHandler);
78
+ reject(error);
79
+ }
80
+ });
81
+ }
82
+ async *eventChannel() {
83
+ while (this.isConnected()) {
84
+ if (this.eventQueue.length > 0) {
85
+ const event = this.eventQueue.shift();
86
+ yield event;
87
+ }
88
+ else {
89
+ const event = await new Promise((resolve) => {
90
+ this.eventResolvers.push(resolve);
91
+ });
92
+ yield event;
93
+ }
94
+ }
95
+ }
96
+ async close() {
97
+ if (this.ws) {
98
+ this.ws.close();
99
+ this.ws = null;
100
+ }
101
+ }
102
+ handleMessage(data) {
103
+ try {
104
+ const msg = unmarshalMessage(data);
105
+ const sink = this.responseSinks.get(msg.requestId);
106
+ if (sink) {
107
+ this.responseSinks.delete(msg.requestId);
108
+ sink(msg);
109
+ }
110
+ else {
111
+ if (this.eventResolvers.length > 0) {
112
+ const resolver = this.eventResolvers.shift();
113
+ resolver(msg);
114
+ }
115
+ else {
116
+ this.eventQueue.push(msg);
117
+ if (this.eventQueue.length > this.config.eventChanSize) {
118
+ this.eventQueue.shift();
119
+ }
120
+ }
121
+ }
122
+ }
123
+ catch (error) {
124
+ console.error('Failed to parse WebSocket message:', error);
125
+ }
126
+ }
127
+ handleClose(error) {
128
+ this.ws = null;
129
+ for (const [id, sink] of this.responseSinks.entries()) {
130
+ sink({
131
+ type: 4,
132
+ requestId: id,
133
+ method: '',
134
+ payload: { error: 'Connection closed' },
135
+ timestamp: Date.now(),
136
+ });
137
+ }
138
+ this.responseSinks.clear();
139
+ if (this.closeHandler) {
140
+ this.closeHandler(error);
141
+ }
142
+ }
143
+ }
144
+ export function newWebsocketDialer(config = DefaultWebsocketDialerConfig) {
145
+ return new WebsocketDialer(config);
146
+ }
@@ -0,0 +1,18 @@
1
+ import { Payload } from './message';
2
+ export declare const ERROR_PARAM_KEY = "error";
3
+ export declare class RPCError extends Error {
4
+ constructor(message: string);
5
+ }
6
+ export declare const ErrAlreadyConnected: RPCError;
7
+ export declare const ErrNotConnected: RPCError;
8
+ export declare const ErrConnectionTimeout: RPCError;
9
+ export declare const ErrReadingMessage: RPCError;
10
+ export declare const ErrNilRequest: RPCError;
11
+ export declare const ErrInvalidRequestMethod: RPCError;
12
+ export declare const ErrMarshalingRequest: RPCError;
13
+ export declare const ErrSendingRequest: RPCError;
14
+ export declare const ErrNoResponse: RPCError;
15
+ export declare const ErrSendingPing: RPCError;
16
+ export declare const ErrDialingWebsocket: RPCError;
17
+ export declare function newErrorPayload(errMsg: string): Payload;
18
+ export declare function errorf(format: string, ...args: unknown[]): RPCError;
@@ -0,0 +1,27 @@
1
+ export const ERROR_PARAM_KEY = 'error';
2
+ export class RPCError extends Error {
3
+ constructor(message) {
4
+ super(message);
5
+ this.name = 'RPCError';
6
+ }
7
+ }
8
+ export const ErrAlreadyConnected = new RPCError('already connected');
9
+ export const ErrNotConnected = new RPCError('not connected');
10
+ export const ErrConnectionTimeout = new RPCError('connection timeout');
11
+ export const ErrReadingMessage = new RPCError('error reading message');
12
+ export const ErrNilRequest = new RPCError('nil request');
13
+ export const ErrInvalidRequestMethod = new RPCError('invalid request method');
14
+ export const ErrMarshalingRequest = new RPCError('error marshaling request');
15
+ export const ErrSendingRequest = new RPCError('error sending request');
16
+ export const ErrNoResponse = new RPCError('no response received');
17
+ export const ErrSendingPing = new RPCError('error sending ping');
18
+ export const ErrDialingWebsocket = new RPCError('error dialing websocket');
19
+ export function newErrorPayload(errMsg) {
20
+ return {
21
+ [ERROR_PARAM_KEY]: errMsg,
22
+ };
23
+ }
24
+ export function errorf(format, ...args) {
25
+ const message = format.replace(/%s/g, () => String(args.shift() || ''));
26
+ return new RPCError(message);
27
+ }
@@ -0,0 +1,7 @@
1
+ export * from './types';
2
+ export * from './api';
3
+ export * from './client';
4
+ export * from './methods';
5
+ export * from './message';
6
+ export * from './dialer';
7
+ export * from './error';
@@ -0,0 +1,7 @@
1
+ export * from './types';
2
+ export * from './api';
3
+ export * from './client';
4
+ export * from './methods';
5
+ export * from './message';
6
+ export * from './dialer';
7
+ export * from './error';
@@ -0,0 +1,25 @@
1
+ export declare enum MsgType {
2
+ Req = 1,
3
+ Resp = 2,
4
+ Event = 3,
5
+ RespErr = 4
6
+ }
7
+ export type Payload = Record<string, unknown>;
8
+ export interface Message {
9
+ type: MsgType;
10
+ requestId: number;
11
+ method: string;
12
+ payload: Payload;
13
+ timestamp: number;
14
+ }
15
+ export declare function newPayload(v: unknown): Payload;
16
+ export declare function translatePayload<T>(payload: Payload): T;
17
+ export declare function payloadError(payload: Payload): Error | null;
18
+ export declare function messageError(message: Message): Error | null;
19
+ export declare function newMessage(type: MsgType, requestId: number, method: string, payload?: Payload): Message;
20
+ export declare function newRequest(requestId: number, method: string, payload?: Payload): Message;
21
+ export declare function newResponse(requestId: number, method: string, payload?: Payload): Message;
22
+ export declare function newEvent(requestId: number, method: string, payload?: Payload): Message;
23
+ export declare function newErrorResponse(requestId: number, method: string, errMsg: string): Message;
24
+ export declare function marshalMessage(message: Message): string;
25
+ export declare function unmarshalMessage(data: string): Message;