@orderly.network/net 1.0.6 → 1.0.8
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.
- package/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +118 -58
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +117 -58
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/fetch/index.ts +39 -5
- package/src/index.ts +1 -1
- package/src/ws/ws.ts +110 -61
package/src/ws/ws.ts
CHANGED
|
@@ -13,21 +13,30 @@ export type WSOptions = {
|
|
|
13
13
|
|
|
14
14
|
export type unsubscribe = () => void;
|
|
15
15
|
|
|
16
|
+
export type MessageParams = {
|
|
17
|
+
event: string;
|
|
18
|
+
topic: string;
|
|
19
|
+
|
|
20
|
+
params?: any;
|
|
21
|
+
// [key: string]: any;
|
|
22
|
+
};
|
|
23
|
+
|
|
16
24
|
type WSMessageHandler = {
|
|
17
25
|
onMessage: (message: any) => void;
|
|
18
26
|
onError?: (error: any) => void;
|
|
19
27
|
onClose?: (event: any) => void;
|
|
20
|
-
onUnsubscribe: (event: any) =>
|
|
28
|
+
onUnsubscribe: (event: any) => any;
|
|
21
29
|
formatter?: (message: any) => any;
|
|
22
30
|
};
|
|
23
31
|
|
|
24
32
|
type Topics = {
|
|
25
|
-
params:
|
|
33
|
+
params: MessageParams;
|
|
26
34
|
isPrivate?: boolean;
|
|
27
|
-
|
|
35
|
+
callback: WSMessageHandler[];
|
|
28
36
|
};
|
|
29
37
|
|
|
30
38
|
const defaultMessageFormatter = (message: any) => message.data;
|
|
39
|
+
const COMMON_ID = "OqdphuyCtYWxwzhxyLLjOWNdFP7sQt8RPWzmb5xY";
|
|
31
40
|
|
|
32
41
|
export class WS {
|
|
33
42
|
private publicSocket!: WebSocket;
|
|
@@ -36,9 +45,6 @@ export class WS {
|
|
|
36
45
|
private publicIsReconnecting: boolean = false;
|
|
37
46
|
private privateIsReconnecting: boolean = false;
|
|
38
47
|
|
|
39
|
-
private publicReconnectTimeout?: number;
|
|
40
|
-
private privateReconnectTimeout?: number;
|
|
41
|
-
|
|
42
48
|
private reconnectInterval: number = 1000;
|
|
43
49
|
|
|
44
50
|
private authenticated: boolean = false;
|
|
@@ -46,9 +52,6 @@ export class WS {
|
|
|
46
52
|
private _pendingPrivateSubscribe: any[][] = [];
|
|
47
53
|
private _pendingPublicSubscribe: any[][] = [];
|
|
48
54
|
|
|
49
|
-
private _subscriptionPublicTopics: Topics[] = [];
|
|
50
|
-
private _subscriptionPrivateTopics: Topics[] = [];
|
|
51
|
-
|
|
52
55
|
private _eventHandlers: Map<string, Topics> = new Map();
|
|
53
56
|
|
|
54
57
|
constructor(private readonly options: WSOptions) {
|
|
@@ -66,7 +69,7 @@ export class WS {
|
|
|
66
69
|
} else {
|
|
67
70
|
url = WS_URL[options.networkId || "testnet"].public;
|
|
68
71
|
}
|
|
69
|
-
this.publicSocket = new WebSocket(`${url}${
|
|
72
|
+
this.publicSocket = new WebSocket(`${url}${COMMON_ID}`);
|
|
70
73
|
this.publicSocket.onopen = this.onOpen.bind(this);
|
|
71
74
|
this.publicSocket.onmessage = this.onMessage.bind(this);
|
|
72
75
|
this.publicSocket.onclose = this.onClose.bind(this);
|
|
@@ -87,7 +90,7 @@ export class WS {
|
|
|
87
90
|
console.log(this._pendingPublicSubscribe);
|
|
88
91
|
if (this._pendingPublicSubscribe.length > 0) {
|
|
89
92
|
this._pendingPublicSubscribe.forEach(([params, cb]) => {
|
|
90
|
-
this.
|
|
93
|
+
this.subscribe(params, cb);
|
|
91
94
|
});
|
|
92
95
|
this._pendingPublicSubscribe = [];
|
|
93
96
|
}
|
|
@@ -100,7 +103,7 @@ export class WS {
|
|
|
100
103
|
|
|
101
104
|
if (this._pendingPrivateSubscribe.length > 0) {
|
|
102
105
|
this._pendingPrivateSubscribe.forEach(([params, cb]) => {
|
|
103
|
-
this.
|
|
106
|
+
this.subscribe(params, cb);
|
|
104
107
|
});
|
|
105
108
|
this._pendingPrivateSubscribe = [];
|
|
106
109
|
}
|
|
@@ -116,13 +119,18 @@ export class WS {
|
|
|
116
119
|
if (commoneHandler) {
|
|
117
120
|
commoneHandler.handle(message, this.send);
|
|
118
121
|
} else {
|
|
119
|
-
const eventhandler = this._eventHandlers.get(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
+
const eventhandler = this._eventHandlers.get(
|
|
123
|
+
message.topic || message.event
|
|
124
|
+
);
|
|
125
|
+
if (eventhandler?.callback) {
|
|
126
|
+
eventhandler.callback.forEach((cb) => {
|
|
122
127
|
const data = cb.formatter
|
|
123
128
|
? cb.formatter(message)
|
|
124
129
|
: defaultMessageFormatter(message);
|
|
125
|
-
|
|
130
|
+
|
|
131
|
+
if (data) {
|
|
132
|
+
cb.onMessage(data);
|
|
133
|
+
}
|
|
126
134
|
});
|
|
127
135
|
}
|
|
128
136
|
}
|
|
@@ -143,7 +151,7 @@ export class WS {
|
|
|
143
151
|
|
|
144
152
|
this._eventHandlers.forEach((value, key) => {
|
|
145
153
|
if (!value.isPrivate) {
|
|
146
|
-
this._pendingPublicSubscribe.push([value.params, value.
|
|
154
|
+
this._pendingPublicSubscribe.push([value.params, value.callback]);
|
|
147
155
|
this._eventHandlers.delete(key);
|
|
148
156
|
}
|
|
149
157
|
});
|
|
@@ -156,7 +164,7 @@ export class WS {
|
|
|
156
164
|
|
|
157
165
|
this._eventHandlers.forEach((value, key) => {
|
|
158
166
|
if (value.isPrivate) {
|
|
159
|
-
this._pendingPrivateSubscribe.push([value.params, value.
|
|
167
|
+
this._pendingPrivateSubscribe.push([value.params, value.callback]);
|
|
160
168
|
this._eventHandlers.delete(key);
|
|
161
169
|
}
|
|
162
170
|
});
|
|
@@ -180,20 +188,17 @@ export class WS {
|
|
|
180
188
|
this.privateSocket?.close();
|
|
181
189
|
}
|
|
182
190
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
publicKey: string;
|
|
187
|
-
signature: string;
|
|
188
|
-
timestamp: number;
|
|
189
|
-
}
|
|
190
|
-
) {
|
|
191
|
+
set accountId(accountId: string) {}
|
|
192
|
+
|
|
193
|
+
private async authenticate(accountId: string) {
|
|
191
194
|
if (this.authenticated) return;
|
|
192
195
|
if (!this.privateSocket) {
|
|
193
196
|
console.error("private ws not connected");
|
|
194
197
|
return;
|
|
195
198
|
}
|
|
196
199
|
|
|
200
|
+
const message = await this.options.onSigntureRequest?.(accountId);
|
|
201
|
+
|
|
197
202
|
console.log("push auth message:", message);
|
|
198
203
|
this.privateSocket.send(
|
|
199
204
|
JSON.stringify({
|
|
@@ -210,73 +215,117 @@ export class WS {
|
|
|
210
215
|
// this.authenticated = true;
|
|
211
216
|
}
|
|
212
217
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
subscription(params: any, cb: WSMessageHandler): unsubscribe {
|
|
216
|
-
const [subscribeMessage, unsubscribeMessage, filter, messageFormatter] =
|
|
217
|
-
this.generateMessage(params);
|
|
218
|
+
privateSubscribe(params: any, callback: WSMessageHandler) {}
|
|
218
219
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
220
|
+
subscribe(
|
|
221
|
+
params: any,
|
|
222
|
+
callback: WSMessageHandler | Omit<WSMessageHandler, "onUnsubscribe">,
|
|
223
|
+
once?: boolean
|
|
224
|
+
): unsubscribe | undefined {
|
|
225
|
+
console.log("👉", params, callback, this.publicSocket.readyState);
|
|
226
|
+
|
|
227
|
+
const [subscribeMessage, onUnsubscribe] = this.generateMessage(
|
|
228
|
+
params,
|
|
229
|
+
(callback as WSMessageHandler).onUnsubscribe
|
|
230
|
+
);
|
|
223
231
|
|
|
224
232
|
if (this.publicSocket.readyState !== WebSocket.OPEN) {
|
|
225
|
-
this._pendingPublicSubscribe.push([params,
|
|
226
|
-
|
|
233
|
+
this._pendingPublicSubscribe.push([params, callback]);
|
|
234
|
+
|
|
235
|
+
if (!once) {
|
|
236
|
+
return () => {
|
|
237
|
+
this.unsubscribe(subscribeMessage);
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
return;
|
|
227
241
|
}
|
|
228
242
|
|
|
229
|
-
const
|
|
243
|
+
const topic = subscribeMessage.topic || subscribeMessage.event;
|
|
244
|
+
|
|
245
|
+
const handler = this._eventHandlers.get(topic);
|
|
246
|
+
const callbacks = {
|
|
247
|
+
...callback,
|
|
248
|
+
onUnsubscribe,
|
|
249
|
+
};
|
|
230
250
|
|
|
231
251
|
if (!handler) {
|
|
232
|
-
this._eventHandlers.set(
|
|
252
|
+
this._eventHandlers.set(topic, {
|
|
233
253
|
params,
|
|
234
|
-
|
|
254
|
+
callback: [callbacks],
|
|
235
255
|
});
|
|
236
256
|
} else {
|
|
237
|
-
handler.
|
|
257
|
+
handler.callback.push(callbacks);
|
|
238
258
|
}
|
|
239
259
|
|
|
240
260
|
this.publicSocket.send(JSON.stringify(subscribeMessage));
|
|
241
261
|
// this._subscriptionPublicTopics.push({params, cb: [cb]});
|
|
242
262
|
|
|
243
|
-
|
|
263
|
+
if (!once) {
|
|
264
|
+
return () => {
|
|
265
|
+
this.unsubscribe(subscribeMessage);
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
onceSubscribe(
|
|
271
|
+
params: any,
|
|
272
|
+
callback: Omit<WSMessageHandler, "onUnsubscribe">
|
|
273
|
+
) {
|
|
274
|
+
this.subscribe(params, callback, true);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private unsubscribe(parmas: MessageParams) {
|
|
278
|
+
const topic = parmas.topic || parmas.event;
|
|
279
|
+
const handler = this._eventHandlers.get(topic);
|
|
280
|
+
console.log("🤜 unsubscribe", parmas, topic, handler);
|
|
281
|
+
|
|
282
|
+
if (!!handler && Array.isArray(handler?.callback)) {
|
|
283
|
+
if (handler!.callback.length === 1) {
|
|
284
|
+
const unsubscribeMessage = handler!.callback[0].onUnsubscribe(topic);
|
|
285
|
+
|
|
286
|
+
console.log("unsubscribeMessage", unsubscribeMessage);
|
|
287
|
+
this.publicSocket.send(JSON.stringify(unsubscribeMessage));
|
|
288
|
+
this._eventHandlers.delete(topic);
|
|
289
|
+
//post unsubscribe message
|
|
290
|
+
} else {
|
|
291
|
+
this._eventHandlers.set(topic, {
|
|
292
|
+
...handler,
|
|
293
|
+
callback: handler.callback.slice(0, -1),
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
244
297
|
}
|
|
245
298
|
|
|
246
299
|
private generateMessage(
|
|
247
300
|
params: any,
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
Record<string, any>,
|
|
252
|
-
Record<string, any>,
|
|
253
|
-
(message: any) => boolean,
|
|
254
|
-
(message: any) => any
|
|
255
|
-
] {
|
|
256
|
-
let subscribeMessage: Record<string, any>,
|
|
257
|
-
unsubscribeMessage: Record<string, any>;
|
|
258
|
-
let filter: (message: any) => boolean,
|
|
259
|
-
messageFormatter: (message: any) => any = (message: any) => message.data;
|
|
301
|
+
onUnsubscribe?: (event: string) => any
|
|
302
|
+
): [MessageParams, (event: string) => any] {
|
|
303
|
+
let subscribeMessage;
|
|
260
304
|
|
|
261
305
|
if (typeof params === "string") {
|
|
262
306
|
subscribeMessage = { event: "subscribe", topic: params };
|
|
263
|
-
unsubscribeMessage = { event: "unsubscribe", topic: params };
|
|
264
|
-
filter = (message: any) => message.topic === params;
|
|
265
307
|
} else {
|
|
266
308
|
subscribeMessage = params;
|
|
267
|
-
unsubscribeMessage =
|
|
268
|
-
typeof unsubscribe === "function" ? unsubscribe() : unsubscribe;
|
|
269
|
-
filter = messageFilter || ((message: any) => true);
|
|
270
309
|
}
|
|
271
310
|
|
|
272
|
-
|
|
311
|
+
if (typeof onUnsubscribe !== "function") {
|
|
312
|
+
if (typeof params === "string") {
|
|
313
|
+
console.log("👉", params);
|
|
314
|
+
|
|
315
|
+
onUnsubscribe = () => ({ event: "unsubscribe", topic: params });
|
|
316
|
+
} else {
|
|
317
|
+
onUnsubscribe = () => ({ event: "unsubscribe", topic: params.topic });
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return [subscribeMessage, onUnsubscribe];
|
|
273
322
|
}
|
|
274
323
|
|
|
275
324
|
private reconnectPublic() {
|
|
276
325
|
if (this.publicIsReconnecting) return;
|
|
277
326
|
this.publicIsReconnecting = true;
|
|
278
327
|
console.log(`Reconnecting in ${this.reconnectInterval / 1000} seconds...`);
|
|
279
|
-
|
|
328
|
+
window.setTimeout(() => {
|
|
280
329
|
console.log("Reconnecting...");
|
|
281
330
|
// this.publicIsReconnecting = false;
|
|
282
331
|
|