@kodiak-finance/orderly-net 2.7.4
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/README.md +1 -0
- package/dist/index.d.mts +108 -0
- package/dist/index.d.ts +108 -0
- package/dist/index.js +759 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +717 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/version.ts
|
|
42
|
+
if (typeof window !== "undefined") {
|
|
43
|
+
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
|
|
44
|
+
window.__ORDERLY_VERSION__["@kodiak-finance/orderly-net"] = "2.7.4";
|
|
45
|
+
}
|
|
46
|
+
var version_default = "2.7.4";
|
|
47
|
+
|
|
48
|
+
// src/errors/apiError.ts
|
|
49
|
+
var ApiError = class extends Error {
|
|
50
|
+
constructor(message, code) {
|
|
51
|
+
super(message);
|
|
52
|
+
this.code = code;
|
|
53
|
+
this.name = "ApiError";
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/fetch/index.ts
|
|
58
|
+
function request(url, options) {
|
|
59
|
+
return __async(this, null, function* () {
|
|
60
|
+
if (!url.startsWith("http")) {
|
|
61
|
+
throw new Error("url must start with http(s)");
|
|
62
|
+
}
|
|
63
|
+
const response = yield fetch(url, __spreadProps(__spreadValues({}, options), {
|
|
64
|
+
// mode: "cors",
|
|
65
|
+
// credentials: "include",
|
|
66
|
+
headers: _createHeaders(options.headers, options.method)
|
|
67
|
+
}));
|
|
68
|
+
if (response.ok) {
|
|
69
|
+
const res = yield response.json();
|
|
70
|
+
return res;
|
|
71
|
+
} else {
|
|
72
|
+
try {
|
|
73
|
+
const errorMsg = yield response.json();
|
|
74
|
+
if (response.status === 400) {
|
|
75
|
+
throw new ApiError(
|
|
76
|
+
errorMsg.message || errorMsg.code || response.statusText,
|
|
77
|
+
errorMsg.code
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
throw new Error(errorMsg.message || errorMsg.code || response.statusText);
|
|
81
|
+
} catch (e) {
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function _createHeaders(headers = {}, method) {
|
|
88
|
+
const _headers = new Headers(headers);
|
|
89
|
+
if (!_headers.has("Content-Type")) {
|
|
90
|
+
if (method !== "DELETE") {
|
|
91
|
+
_headers.append("Content-Type", "application/json;charset=utf-8");
|
|
92
|
+
} else {
|
|
93
|
+
_headers.append("Content-Type", "application/x-www-form-urlencoded");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return _headers;
|
|
97
|
+
}
|
|
98
|
+
function get(url, options, formatter) {
|
|
99
|
+
return __async(this, null, function* () {
|
|
100
|
+
const res = yield request(url, __spreadValues({
|
|
101
|
+
method: "GET"
|
|
102
|
+
}, options));
|
|
103
|
+
if (res.success) {
|
|
104
|
+
if (typeof formatter === "function") {
|
|
105
|
+
return formatter(res.data);
|
|
106
|
+
}
|
|
107
|
+
if (Array.isArray(res.data["rows"])) {
|
|
108
|
+
return res.data["rows"];
|
|
109
|
+
}
|
|
110
|
+
return res.data;
|
|
111
|
+
}
|
|
112
|
+
throw new Error(res.message);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function post(url, data, options) {
|
|
116
|
+
return __async(this, null, function* () {
|
|
117
|
+
const res = yield request(url, __spreadValues({
|
|
118
|
+
method: "POST",
|
|
119
|
+
body: JSON.stringify(data)
|
|
120
|
+
}, options));
|
|
121
|
+
return res;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function put(url, data, options) {
|
|
125
|
+
return __async(this, null, function* () {
|
|
126
|
+
const res = yield request(url, __spreadValues({
|
|
127
|
+
method: "PUT",
|
|
128
|
+
body: JSON.stringify(data)
|
|
129
|
+
}, options));
|
|
130
|
+
return res;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function del(url, options) {
|
|
134
|
+
return __async(this, null, function* () {
|
|
135
|
+
const res = yield request(url, __spreadValues({
|
|
136
|
+
method: "DELETE"
|
|
137
|
+
}, options));
|
|
138
|
+
return res;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
function mutate(url, init) {
|
|
142
|
+
return __async(this, null, function* () {
|
|
143
|
+
const res = yield request(url, init);
|
|
144
|
+
return res;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/constants.ts
|
|
149
|
+
var __ORDERLY_API_URL_KEY__ = "__ORDERLY_API_URL__";
|
|
150
|
+
|
|
151
|
+
// src/ws/ws.ts
|
|
152
|
+
import EventEmitter from "eventemitter3";
|
|
153
|
+
|
|
154
|
+
// src/ws/handler/baseHandler.ts
|
|
155
|
+
var BaseHandler = class {
|
|
156
|
+
handle(message, webSocket) {
|
|
157
|
+
throw new Error("Method not implemented.");
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// src/ws/handler/ping.ts
|
|
162
|
+
var PingHandler = class extends BaseHandler {
|
|
163
|
+
handle(_, webSocket) {
|
|
164
|
+
webSocket.send(JSON.stringify({ event: "pong", ts: Date.now() }));
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// src/ws/handler/handler.ts
|
|
169
|
+
var messageHandlers = /* @__PURE__ */ new Map([
|
|
170
|
+
["ping", new PingHandler()]
|
|
171
|
+
]);
|
|
172
|
+
|
|
173
|
+
// src/ws/ws.ts
|
|
174
|
+
var WebSocketEvent = /* @__PURE__ */ ((WebSocketEvent2) => {
|
|
175
|
+
WebSocketEvent2["OPEN"] = "open";
|
|
176
|
+
WebSocketEvent2["CLOSE"] = "close";
|
|
177
|
+
WebSocketEvent2["ERROR"] = "error";
|
|
178
|
+
WebSocketEvent2["MESSAGE"] = "message";
|
|
179
|
+
WebSocketEvent2["CONNECTING"] = "connecting";
|
|
180
|
+
WebSocketEvent2["RECONNECTING"] = "reconnecting";
|
|
181
|
+
return WebSocketEvent2;
|
|
182
|
+
})(WebSocketEvent || {});
|
|
183
|
+
var defaultMessageFormatter = (message) => message.data;
|
|
184
|
+
var COMMON_ID = "OqdphuyCtYWxwzhxyLLjOWNdFP7sQt8RPWzmb5xY";
|
|
185
|
+
var TIME_OUT = 1e3 * 60 * 2;
|
|
186
|
+
var CONNECT_LIMIT = 5;
|
|
187
|
+
var WS = class extends EventEmitter {
|
|
188
|
+
constructor(options) {
|
|
189
|
+
super();
|
|
190
|
+
this.options = options;
|
|
191
|
+
this._eventContainer = /* @__PURE__ */ new Map();
|
|
192
|
+
this.publicIsReconnecting = false;
|
|
193
|
+
this.privateIsReconnecting = false;
|
|
194
|
+
this.reconnectInterval = 1e3;
|
|
195
|
+
this.authenticated = false;
|
|
196
|
+
this._pendingPrivateSubscribe = [];
|
|
197
|
+
this._pendingPublicSubscribe = [];
|
|
198
|
+
// all message handlers
|
|
199
|
+
this._eventHandlers = /* @__PURE__ */ new Map();
|
|
200
|
+
this._eventPrivateHandlers = /* @__PURE__ */ new Map();
|
|
201
|
+
this._publicRetryCount = 0;
|
|
202
|
+
this._privateRetryCount = 0;
|
|
203
|
+
this.send = (message) => {
|
|
204
|
+
var _a, _b;
|
|
205
|
+
if (typeof message !== "string") {
|
|
206
|
+
message = JSON.stringify(message);
|
|
207
|
+
}
|
|
208
|
+
if (typeof message === "undefined")
|
|
209
|
+
return;
|
|
210
|
+
if (((_a = this._publicSocket) == null ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
211
|
+
(_b = this._publicSocket) == null ? void 0 : _b.send(message);
|
|
212
|
+
} else {
|
|
213
|
+
console.warn("WebSocket connection is not open. Cannot send message.");
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
this.createPublicSC(options);
|
|
217
|
+
if (!!options.accountId) {
|
|
218
|
+
this.createPrivateSC(options);
|
|
219
|
+
}
|
|
220
|
+
this.bindEvents();
|
|
221
|
+
}
|
|
222
|
+
bindEvents() {
|
|
223
|
+
var _a, _b;
|
|
224
|
+
if (typeof document !== "undefined") {
|
|
225
|
+
(_a = document.addEventListener) == null ? void 0 : _a.call(
|
|
226
|
+
document,
|
|
227
|
+
"visibilitychange",
|
|
228
|
+
this.onVisibilityChange.bind(this)
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
if (typeof window !== "undefined") {
|
|
232
|
+
(_b = window.addEventListener) == null ? void 0 : _b.call(
|
|
233
|
+
window,
|
|
234
|
+
"online",
|
|
235
|
+
this.onNetworkStatusChange.bind(this)
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
onVisibilityChange() {
|
|
240
|
+
if (document.visibilityState === "visible") {
|
|
241
|
+
this.checkSocketStatus();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
onNetworkStatusChange() {
|
|
245
|
+
if (navigator.onLine) {
|
|
246
|
+
this.checkSocketStatus();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Determine the current connection status,
|
|
251
|
+
* 1. If it is disconnected, reconnect
|
|
252
|
+
* 2. If no message is received for too long, disconnect and reconnect actively
|
|
253
|
+
* 3. When returning from the background and the network status changes, the following process is followed
|
|
254
|
+
*/
|
|
255
|
+
checkSocketStatus() {
|
|
256
|
+
var _a, _b, _c, _d;
|
|
257
|
+
const now = Date.now();
|
|
258
|
+
if (document.visibilityState !== "visible")
|
|
259
|
+
return;
|
|
260
|
+
if (!navigator.onLine)
|
|
261
|
+
return;
|
|
262
|
+
if (!this.publicIsReconnecting) {
|
|
263
|
+
if (((_a = this._publicSocket) == null ? void 0 : _a.readyState) === WebSocket.CLOSED) {
|
|
264
|
+
this.reconnectPublic();
|
|
265
|
+
} else {
|
|
266
|
+
if (now - this._publicHeartbeatTime > TIME_OUT) {
|
|
267
|
+
(_b = this._publicSocket) == null ? void 0 : _b.close(3888);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (!this.privateIsReconnecting) {
|
|
272
|
+
if (((_c = this.privateSocket) == null ? void 0 : _c.readyState) === WebSocket.CLOSED) {
|
|
273
|
+
this.reconnectPrivate();
|
|
274
|
+
} else {
|
|
275
|
+
if (this._privateHeartbeatTime && now - this._privateHeartbeatTime > TIME_OUT) {
|
|
276
|
+
(_d = this.privateSocket) == null ? void 0 : _d.close(3888);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
openPrivate(accountId) {
|
|
282
|
+
var _a;
|
|
283
|
+
if (((_a = this.privateSocket) == null ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
this.createPrivateSC(__spreadProps(__spreadValues({}, this.options), {
|
|
287
|
+
accountId
|
|
288
|
+
}));
|
|
289
|
+
}
|
|
290
|
+
closePrivate(code, reason) {
|
|
291
|
+
var _a, _b;
|
|
292
|
+
if (((_a = this.privateSocket) == null ? void 0 : _a.readyState) !== WebSocket.OPEN) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
this.authenticated = false;
|
|
296
|
+
this._pendingPrivateSubscribe = [];
|
|
297
|
+
this._eventPrivateHandlers.clear();
|
|
298
|
+
(_b = this.privateSocket) == null ? void 0 : _b.close(code, reason);
|
|
299
|
+
}
|
|
300
|
+
createPublicSC(options) {
|
|
301
|
+
if (!options.publicUrl) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (this._publicSocket && this._publicSocket.readyState === WebSocket.OPEN) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
this._publicSocket = new WebSocket(
|
|
308
|
+
`${options.publicUrl}/ws/stream/${COMMON_ID}`
|
|
309
|
+
);
|
|
310
|
+
this._publicSocket.onopen = this.onOpen.bind(this);
|
|
311
|
+
this._publicSocket.addEventListener(
|
|
312
|
+
"message",
|
|
313
|
+
this.onPublicMessage.bind(this)
|
|
314
|
+
);
|
|
315
|
+
this._publicSocket.addEventListener("close", this.onPublicClose.bind(this));
|
|
316
|
+
this._publicSocket.addEventListener("error", this.onPublicError.bind(this));
|
|
317
|
+
}
|
|
318
|
+
createPrivateSC(options) {
|
|
319
|
+
if (this.privateSocket && this.privateSocket.readyState === WebSocket.OPEN) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
this.options = options;
|
|
323
|
+
this.privateSocket = new WebSocket(
|
|
324
|
+
`${this.options.privateUrl}/v2/ws/private/stream/${options.accountId}`
|
|
325
|
+
);
|
|
326
|
+
this.privateSocket.onopen = this.onPrivateOpen.bind(this);
|
|
327
|
+
this.privateSocket.onmessage = this.onPrivateMessage.bind(this);
|
|
328
|
+
this.privateSocket.onclose = this.onPrivateClose.bind(this);
|
|
329
|
+
this.privateSocket.onerror = this.onPrivateError.bind(this);
|
|
330
|
+
}
|
|
331
|
+
onOpen(event) {
|
|
332
|
+
if (this._pendingPublicSubscribe.length > 0) {
|
|
333
|
+
this._pendingPublicSubscribe.forEach(([params, cb, isOnce]) => {
|
|
334
|
+
this.subscribe(params, cb, isOnce);
|
|
335
|
+
});
|
|
336
|
+
this._pendingPublicSubscribe = [];
|
|
337
|
+
}
|
|
338
|
+
this.publicIsReconnecting = false;
|
|
339
|
+
this.emit("status:change", {
|
|
340
|
+
type: "open" /* OPEN */,
|
|
341
|
+
isPrivate: false,
|
|
342
|
+
isReconnect: this._publicRetryCount > 0
|
|
343
|
+
});
|
|
344
|
+
this._publicRetryCount = 0;
|
|
345
|
+
}
|
|
346
|
+
onPrivateOpen(event) {
|
|
347
|
+
this.authenticate(this.options.accountId).then(() => {
|
|
348
|
+
this.privateIsReconnecting = false;
|
|
349
|
+
this.emit("status:change", {
|
|
350
|
+
type: "open" /* OPEN */,
|
|
351
|
+
isPrivate: true,
|
|
352
|
+
isReconnect: this._privateRetryCount > 0
|
|
353
|
+
});
|
|
354
|
+
this._privateRetryCount = 0;
|
|
355
|
+
}).catch((e) => {
|
|
356
|
+
console.log("ws authenticate failed", e);
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
onMessage(event, socket, handlerMap) {
|
|
360
|
+
try {
|
|
361
|
+
const message = JSON.parse(event.data);
|
|
362
|
+
const commoneHandler = messageHandlers.get(message.event);
|
|
363
|
+
if (message.event === "auth" && message.success) {
|
|
364
|
+
this.authenticated = true;
|
|
365
|
+
this.handlePendingPrivateTopic();
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (commoneHandler) {
|
|
369
|
+
commoneHandler.handle(message, socket);
|
|
370
|
+
} else {
|
|
371
|
+
const topicKey = this.getTopicKeyFromMessage(message);
|
|
372
|
+
const eventhandler = handlerMap.get(topicKey);
|
|
373
|
+
if (eventhandler == null ? void 0 : eventhandler.callback) {
|
|
374
|
+
eventhandler.callback.forEach((cb) => {
|
|
375
|
+
const data = cb.formatter ? cb.formatter(message) : defaultMessageFormatter(message);
|
|
376
|
+
if (data) {
|
|
377
|
+
cb.onMessage(data);
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
this._eventContainer.forEach((_, key) => {
|
|
382
|
+
const reg = new RegExp(key);
|
|
383
|
+
if (reg.test(topicKey)) {
|
|
384
|
+
this.emit(key, message);
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
} catch (e) {
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
onPublicMessage(event) {
|
|
392
|
+
this.onMessage(event, this._publicSocket, this._eventHandlers);
|
|
393
|
+
this._publicHeartbeatTime = Date.now();
|
|
394
|
+
}
|
|
395
|
+
onPrivateMessage(event) {
|
|
396
|
+
this.onMessage(event, this.privateSocket, this._eventPrivateHandlers);
|
|
397
|
+
this._privateHeartbeatTime = Date.now();
|
|
398
|
+
}
|
|
399
|
+
handlePendingPrivateTopic() {
|
|
400
|
+
if (this._pendingPrivateSubscribe.length > 0) {
|
|
401
|
+
this._pendingPrivateSubscribe.forEach(([params, cb]) => {
|
|
402
|
+
this.privateSubscribe(params, cb);
|
|
403
|
+
});
|
|
404
|
+
this._pendingPrivateSubscribe = [];
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
onPublicClose(event) {
|
|
408
|
+
this._eventHandlers.forEach((value, key) => {
|
|
409
|
+
value.callback.forEach((cb) => {
|
|
410
|
+
this._pendingPublicSubscribe.push([value.params, cb, value.isOnce]);
|
|
411
|
+
});
|
|
412
|
+
this._eventHandlers.delete(key);
|
|
413
|
+
});
|
|
414
|
+
this.emit("status:change", {
|
|
415
|
+
type: "close" /* CLOSE */,
|
|
416
|
+
isPrivate: false
|
|
417
|
+
});
|
|
418
|
+
setTimeout(() => this.checkSocketStatus(), 0);
|
|
419
|
+
}
|
|
420
|
+
onPrivateClose(event) {
|
|
421
|
+
if (event.code === 3887)
|
|
422
|
+
return;
|
|
423
|
+
if (this.privateIsReconnecting)
|
|
424
|
+
return;
|
|
425
|
+
this._eventPrivateHandlers.forEach((value, key) => {
|
|
426
|
+
value.callback.forEach((cb) => {
|
|
427
|
+
this._pendingPrivateSubscribe.push([value.params, cb, value.isOnce]);
|
|
428
|
+
});
|
|
429
|
+
this._eventPrivateHandlers.delete(key);
|
|
430
|
+
});
|
|
431
|
+
this.authenticated = false;
|
|
432
|
+
this.emit("status:change", {
|
|
433
|
+
type: "close" /* CLOSE */,
|
|
434
|
+
isPrivate: true,
|
|
435
|
+
event
|
|
436
|
+
});
|
|
437
|
+
setTimeout(() => this.checkSocketStatus(), 0);
|
|
438
|
+
}
|
|
439
|
+
onPublicError(event) {
|
|
440
|
+
var _a, _b;
|
|
441
|
+
console.error("public WebSocket error:", event);
|
|
442
|
+
this.publicIsReconnecting = false;
|
|
443
|
+
if (((_a = this._publicSocket) == null ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
444
|
+
(_b = this._publicSocket) == null ? void 0 : _b.close(3888);
|
|
445
|
+
} else {
|
|
446
|
+
if (this._publicRetryCount > CONNECT_LIMIT)
|
|
447
|
+
return;
|
|
448
|
+
setTimeout(() => {
|
|
449
|
+
this.reconnectPublic();
|
|
450
|
+
}, this._publicRetryCount * 1e3);
|
|
451
|
+
}
|
|
452
|
+
this.errorBoardscast(event, this._eventHandlers);
|
|
453
|
+
this.emit("status:change", {
|
|
454
|
+
type: "error" /* ERROR */,
|
|
455
|
+
isPrivate: false
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
onPrivateError(event) {
|
|
459
|
+
var _a;
|
|
460
|
+
console.error("Private WebSocket error:", event);
|
|
461
|
+
this.privateIsReconnecting = false;
|
|
462
|
+
if (((_a = this.privateSocket) == null ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
463
|
+
this.privateSocket.close(3888);
|
|
464
|
+
} else {
|
|
465
|
+
if (this._privateRetryCount > CONNECT_LIMIT)
|
|
466
|
+
return;
|
|
467
|
+
setTimeout(() => {
|
|
468
|
+
this.reconnectPrivate();
|
|
469
|
+
}, this._privateRetryCount * 1e3);
|
|
470
|
+
}
|
|
471
|
+
this.errorBoardscast(event, this._eventPrivateHandlers);
|
|
472
|
+
this.emit("status:change", { type: "error" /* ERROR */, isPrivate: true });
|
|
473
|
+
}
|
|
474
|
+
errorBoardscast(error, eventHandlers) {
|
|
475
|
+
eventHandlers.forEach((value) => {
|
|
476
|
+
value.callback.forEach((cb) => {
|
|
477
|
+
var _a;
|
|
478
|
+
(_a = cb.onError) == null ? void 0 : _a.call(cb, error);
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
close() {
|
|
483
|
+
var _a, _b;
|
|
484
|
+
(_a = this._publicSocket) == null ? void 0 : _a.close();
|
|
485
|
+
(_b = this.privateSocket) == null ? void 0 : _b.close();
|
|
486
|
+
}
|
|
487
|
+
set accountId(accountId) {
|
|
488
|
+
}
|
|
489
|
+
authenticate(accountId) {
|
|
490
|
+
return __async(this, null, function* () {
|
|
491
|
+
var _a, _b;
|
|
492
|
+
if (this.authenticated)
|
|
493
|
+
return;
|
|
494
|
+
if (!this.privateSocket) {
|
|
495
|
+
console.error("private ws not connected");
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
if (this.privateSocket.readyState !== WebSocket.OPEN) {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const message = yield (_b = (_a = this.options).onSigntureRequest) == null ? void 0 : _b.call(_a, accountId);
|
|
502
|
+
this.privateSocket.send(
|
|
503
|
+
JSON.stringify({
|
|
504
|
+
id: "auth",
|
|
505
|
+
event: "auth",
|
|
506
|
+
params: {
|
|
507
|
+
orderly_key: message.publicKey,
|
|
508
|
+
sign: message.signature,
|
|
509
|
+
timestamp: message.timestamp
|
|
510
|
+
}
|
|
511
|
+
})
|
|
512
|
+
);
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
privateSubscribe(params, callback) {
|
|
516
|
+
var _a;
|
|
517
|
+
const [subscribeMessage, onUnsubscribe] = this.generateMessage(
|
|
518
|
+
params,
|
|
519
|
+
callback.onUnsubscribe
|
|
520
|
+
);
|
|
521
|
+
if (((_a = this.privateSocket) == null ? void 0 : _a.readyState) !== WebSocket.OPEN) {
|
|
522
|
+
this._pendingPrivateSubscribe.push([params, callback]);
|
|
523
|
+
return () => {
|
|
524
|
+
this.unsubscribePrivate(subscribeMessage);
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
const topic = subscribeMessage.topic || subscribeMessage.event;
|
|
528
|
+
const handler = this._eventPrivateHandlers.get(topic);
|
|
529
|
+
const callbacks = __spreadProps(__spreadValues({}, callback), {
|
|
530
|
+
onUnsubscribe
|
|
531
|
+
});
|
|
532
|
+
if (!handler) {
|
|
533
|
+
this._eventPrivateHandlers.set(topic, {
|
|
534
|
+
params,
|
|
535
|
+
callback: [callbacks]
|
|
536
|
+
});
|
|
537
|
+
this.privateSocket.send(JSON.stringify(subscribeMessage));
|
|
538
|
+
} else {
|
|
539
|
+
handler.callback.push(callbacks);
|
|
540
|
+
}
|
|
541
|
+
return () => {
|
|
542
|
+
this.unsubscribePrivate(subscribeMessage);
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
subscribe(params, callback, once, id) {
|
|
546
|
+
if (!this._publicSocket) {
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
const [subscribeMessage, onUnsubscribe] = this.generateMessage(
|
|
550
|
+
params,
|
|
551
|
+
callback.onUnsubscribe,
|
|
552
|
+
callback.onMessage
|
|
553
|
+
);
|
|
554
|
+
if (this._publicSocket.readyState !== WebSocket.OPEN) {
|
|
555
|
+
this._pendingPublicSubscribe.push([params, callback, once, id]);
|
|
556
|
+
if (!once) {
|
|
557
|
+
return () => {
|
|
558
|
+
this.unsubscribePublic(subscribeMessage);
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const topic = this.getTopicKeyFromParams(subscribeMessage);
|
|
564
|
+
const handler = this._eventHandlers.get(topic);
|
|
565
|
+
const callbacks = __spreadProps(__spreadValues({}, callback), {
|
|
566
|
+
onUnsubscribe
|
|
567
|
+
});
|
|
568
|
+
if (!handler) {
|
|
569
|
+
this._eventHandlers.set(topic, {
|
|
570
|
+
params,
|
|
571
|
+
isOnce: once,
|
|
572
|
+
callback: [callbacks]
|
|
573
|
+
});
|
|
574
|
+
this._publicSocket.send(JSON.stringify(subscribeMessage));
|
|
575
|
+
} else {
|
|
576
|
+
if (once) {
|
|
577
|
+
handler.callback = [callbacks];
|
|
578
|
+
this._publicSocket.send(JSON.stringify(subscribeMessage));
|
|
579
|
+
} else {
|
|
580
|
+
handler.callback.push(callbacks);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (!once) {
|
|
584
|
+
return () => {
|
|
585
|
+
this.unsubscribePublic(subscribeMessage);
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
getTopicKeyFromParams(params) {
|
|
590
|
+
let topic;
|
|
591
|
+
if (params.topic) {
|
|
592
|
+
topic = params.topic;
|
|
593
|
+
} else {
|
|
594
|
+
const eventName = params.event;
|
|
595
|
+
topic = params.event;
|
|
596
|
+
if (params.id) {
|
|
597
|
+
topic += `_${params.id}`;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return topic;
|
|
601
|
+
}
|
|
602
|
+
getTopicKeyFromMessage(message) {
|
|
603
|
+
let topic;
|
|
604
|
+
if (message.topic) {
|
|
605
|
+
topic = message.topic;
|
|
606
|
+
} else {
|
|
607
|
+
if (message.event) {
|
|
608
|
+
topic = `${message.event}`;
|
|
609
|
+
if (message.id) {
|
|
610
|
+
topic += `_${message.id}`;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return topic;
|
|
615
|
+
}
|
|
616
|
+
// sendPublicMessage(){
|
|
617
|
+
// if(this.publicSocket.readyState !== )
|
|
618
|
+
// }
|
|
619
|
+
onceSubscribe(params, callback) {
|
|
620
|
+
this.subscribe(params, callback, true);
|
|
621
|
+
}
|
|
622
|
+
unsubscribe(parmas, webSocket, handlerMap) {
|
|
623
|
+
const topic = parmas.topic || parmas.event;
|
|
624
|
+
const handler = handlerMap.get(topic);
|
|
625
|
+
if (!!handler && Array.isArray(handler == null ? void 0 : handler.callback)) {
|
|
626
|
+
if (handler.callback.length === 1) {
|
|
627
|
+
const unsubscribeMessage = handler.callback[0].onUnsubscribe(topic);
|
|
628
|
+
webSocket.send(JSON.stringify(unsubscribeMessage));
|
|
629
|
+
handlerMap.delete(topic);
|
|
630
|
+
} else {
|
|
631
|
+
const index = handler.callback.findIndex(
|
|
632
|
+
(cb) => cb.onMessage === parmas.onMessage
|
|
633
|
+
);
|
|
634
|
+
if (index === -1)
|
|
635
|
+
return;
|
|
636
|
+
handler.callback.splice(index, 1);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
unsubscribePrivate(parmas) {
|
|
641
|
+
this.unsubscribe(parmas, this.privateSocket, this._eventPrivateHandlers);
|
|
642
|
+
}
|
|
643
|
+
unsubscribePublic(parmas) {
|
|
644
|
+
this.unsubscribe(parmas, this._publicSocket, this._eventHandlers);
|
|
645
|
+
}
|
|
646
|
+
generateMessage(params, onUnsubscribe, onMessage) {
|
|
647
|
+
let subscribeMessage;
|
|
648
|
+
if (typeof params === "string") {
|
|
649
|
+
subscribeMessage = { event: "subscribe", topic: params };
|
|
650
|
+
} else {
|
|
651
|
+
subscribeMessage = params;
|
|
652
|
+
}
|
|
653
|
+
if (typeof onUnsubscribe !== "function") {
|
|
654
|
+
if (typeof params === "string") {
|
|
655
|
+
onUnsubscribe = () => ({ event: "unsubscribe", topic: params });
|
|
656
|
+
} else {
|
|
657
|
+
onUnsubscribe = () => ({ event: "unsubscribe", topic: params.topic });
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return [__spreadProps(__spreadValues({}, subscribeMessage), { onMessage }), onUnsubscribe];
|
|
661
|
+
}
|
|
662
|
+
reconnectPublic() {
|
|
663
|
+
if (this.publicIsReconnecting)
|
|
664
|
+
return;
|
|
665
|
+
this.publicIsReconnecting = true;
|
|
666
|
+
if (typeof window === "undefined")
|
|
667
|
+
return;
|
|
668
|
+
window.setTimeout(() => {
|
|
669
|
+
this._publicRetryCount++;
|
|
670
|
+
this.createPublicSC(this.options);
|
|
671
|
+
this.emit("status:change", {
|
|
672
|
+
type: "reconnecting" /* RECONNECTING */,
|
|
673
|
+
isPrivate: false,
|
|
674
|
+
count: this._publicRetryCount
|
|
675
|
+
});
|
|
676
|
+
}, this.reconnectInterval);
|
|
677
|
+
}
|
|
678
|
+
reconnectPrivate() {
|
|
679
|
+
if (!this.options.accountId)
|
|
680
|
+
return;
|
|
681
|
+
if (this.privateIsReconnecting)
|
|
682
|
+
return;
|
|
683
|
+
this.privateIsReconnecting = true;
|
|
684
|
+
if (typeof window === "undefined")
|
|
685
|
+
return;
|
|
686
|
+
window.setTimeout(() => {
|
|
687
|
+
this._privateRetryCount++;
|
|
688
|
+
this.createPrivateSC(this.options);
|
|
689
|
+
this.emit("status:change", {
|
|
690
|
+
type: "reconnecting" /* RECONNECTING */,
|
|
691
|
+
isPrivate: true,
|
|
692
|
+
count: this._privateRetryCount
|
|
693
|
+
});
|
|
694
|
+
}, this.reconnectInterval);
|
|
695
|
+
}
|
|
696
|
+
// get publicSocket(): WebSocket {
|
|
697
|
+
// return this._publicSocket;
|
|
698
|
+
// }
|
|
699
|
+
get client() {
|
|
700
|
+
return {
|
|
701
|
+
public: this._publicSocket,
|
|
702
|
+
private: this.privateSocket
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
export {
|
|
707
|
+
WS,
|
|
708
|
+
WebSocketEvent,
|
|
709
|
+
__ORDERLY_API_URL_KEY__,
|
|
710
|
+
del,
|
|
711
|
+
get,
|
|
712
|
+
mutate,
|
|
713
|
+
post,
|
|
714
|
+
put,
|
|
715
|
+
version_default as version
|
|
716
|
+
};
|
|
717
|
+
//# sourceMappingURL=index.mjs.map
|