@rc-ex/ws 1.2.1 → 1.2.2
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/lib/exceptions/ClosedException.js.map +1 -1
- package/lib/exceptions/ClosedException.ts +7 -0
- package/lib/exceptions/ConnectionException.js.map +1 -1
- package/lib/exceptions/ConnectionException.ts +17 -0
- package/lib/exceptions/TimeoutException.js.map +1 -1
- package/lib/exceptions/TimeoutException.ts +7 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/index.ts +418 -0
- package/lib/rest.d.ts +1 -1
- package/lib/rest.js +1 -1
- package/lib/rest.js.map +1 -1
- package/lib/rest.ts +71 -0
- package/lib/subscription.d.ts +2 -2
- package/lib/subscription.js.map +1 -1
- package/lib/subscription.ts +131 -0
- package/lib/types.d.ts +1 -1
- package/lib/types.js.map +1 -1
- package/lib/types.ts +85 -0
- package/lib/utils.js.map +1 -1
- package/lib/utils.ts +82 -0
- package/package.json +5 -5
- package/src/exceptions/ClosedException.d.ts +4 -0
- package/src/exceptions/ClosedException.js +9 -0
- package/src/exceptions/ClosedException.js.map +1 -0
- package/src/exceptions/ConnectionException.d.ts +7 -0
- package/src/exceptions/ConnectionException.js +16 -0
- package/src/exceptions/ConnectionException.js.map +1 -0
- package/src/exceptions/TimeoutException.d.ts +4 -0
- package/src/exceptions/TimeoutException.js +9 -0
- package/src/exceptions/TimeoutException.js.map +1 -0
- package/src/index.d.ts +44 -0
- package/src/index.js +329 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +4 -4
- package/src/rest.d.ts +3 -0
- package/src/rest.js +55 -0
- package/src/rest.js.map +1 -0
- package/src/rest.ts +2 -2
- package/src/subscription.d.ts +20 -0
- package/src/subscription.js +91 -0
- package/src/subscription.js.map +1 -0
- package/src/subscription.ts +3 -3
- package/src/types.d.ts +61 -0
- package/src/types.js +3 -0
- package/src/types.js.map +1 -0
- package/src/types.ts +1 -1
- package/src/utils.d.ts +8 -0
- package/src/utils.js +72 -0
- package/src/utils.js.map +1 -0
- package/tsconfig.json +0 -3
package/src/index.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Events = void 0;
|
|
7
|
+
const SdkExtension_1 = __importDefault(require("@rc-ex/core/src/SdkExtension"));
|
|
8
|
+
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
9
|
+
const hyperid_1 = __importDefault(require("hyperid"));
|
|
10
|
+
const events_1 = require("events");
|
|
11
|
+
const wait_for_async_1 = __importDefault(require("wait-for-async"));
|
|
12
|
+
const RestException_1 = __importDefault(require("@rc-ex/core/src/RestException"));
|
|
13
|
+
const rest_1 = require("./rest");
|
|
14
|
+
const subscription_1 = __importDefault(require("./subscription"));
|
|
15
|
+
const ConnectionException_1 = __importDefault(require("./exceptions/ConnectionException"));
|
|
16
|
+
const utils_1 = __importDefault(require("./utils"));
|
|
17
|
+
const CONNECTING = 0;
|
|
18
|
+
const OPEN = 1;
|
|
19
|
+
const uuid = (0, hyperid_1.default)();
|
|
20
|
+
var Events;
|
|
21
|
+
(function (Events) {
|
|
22
|
+
Events["autoRecoverSuccess"] = "autoRecoverSuccess";
|
|
23
|
+
Events["autoRecoverFailed"] = "autoRecoverFailed";
|
|
24
|
+
Events["autoRecoverError"] = "autoRecoverError";
|
|
25
|
+
Events["newWebSocketObject"] = "newWebSocketObject";
|
|
26
|
+
Events["newWsc"] = "newWsc";
|
|
27
|
+
Events["connectionReady"] = "connectionReady";
|
|
28
|
+
})(Events || (exports.Events = Events = {}));
|
|
29
|
+
class WebSocketExtension extends SdkExtension_1.default {
|
|
30
|
+
constructor(options = {}) {
|
|
31
|
+
var _a, _b, _c, _d, _e;
|
|
32
|
+
var _f, _g, _h, _j, _k;
|
|
33
|
+
super();
|
|
34
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
35
|
+
this.wsTokenExpiresAt = 0;
|
|
36
|
+
this.request = rest_1.request; // request method was moved to another file to keep this file short
|
|
37
|
+
this.options = options;
|
|
38
|
+
(_a = (_f = this.options).restOverWebSocket) !== null && _a !== void 0 ? _a : (_f.restOverWebSocket = false);
|
|
39
|
+
(_b = (_g = this.options).debugMode) !== null && _b !== void 0 ? _b : (_g.debugMode = false);
|
|
40
|
+
(_c = (_h = this.options).autoRecover) !== null && _c !== void 0 ? _c : (_h.autoRecover = {
|
|
41
|
+
enabled: true,
|
|
42
|
+
});
|
|
43
|
+
(_d = (_j = this.options.autoRecover).checkInterval) !== null && _d !== void 0 ? _d : (_j.checkInterval = (retriesAttempted) => {
|
|
44
|
+
const interval = 2000 + 2000 * retriesAttempted;
|
|
45
|
+
return Math.min(8000, interval);
|
|
46
|
+
});
|
|
47
|
+
(_e = (_k = this.options.autoRecover).pingServerInterval) !== null && _e !== void 0 ? _e : (_k.pingServerInterval = 60000);
|
|
48
|
+
}
|
|
49
|
+
disable() {
|
|
50
|
+
super.disable();
|
|
51
|
+
if (this.subscription) {
|
|
52
|
+
this.subscription.enabled = false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async install(rc) {
|
|
56
|
+
this.rc = rc;
|
|
57
|
+
if (this.options.restOverWebSocket) {
|
|
58
|
+
const request = rc.request.bind(rc);
|
|
59
|
+
rc.request = async (method, endpoint, content, queryParams, config) => {
|
|
60
|
+
var _a, _b, _c;
|
|
61
|
+
if (!this.enabled || !this.options.restOverWebSocket) {
|
|
62
|
+
return request(method, endpoint, content, queryParams, config);
|
|
63
|
+
}
|
|
64
|
+
if (
|
|
65
|
+
// the following cannot be done with WebSocket
|
|
66
|
+
((_c = (_b = (_a = config === null || config === void 0 ? void 0 : config.headers) === null || _a === void 0 ? void 0 : _a.getContentType) === null || _b === void 0 ? void 0 : _b.toString()) === null || _c === void 0 ? void 0 : _c.includes("multipart/form-data")) ||
|
|
67
|
+
(config === null || config === void 0 ? void 0 : config.responseType) === "arraybuffer" ||
|
|
68
|
+
endpoint.startsWith("/restapi/oauth/") // token, revoke, wstoken
|
|
69
|
+
) {
|
|
70
|
+
return request(method, endpoint, content, queryParams, config);
|
|
71
|
+
}
|
|
72
|
+
return this.request(method, endpoint, content, queryParams, config);
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// should recover if this.options.wscToken
|
|
76
|
+
let connectMethod = this.connect.bind(this);
|
|
77
|
+
if (this.options.wscToken) {
|
|
78
|
+
this.wsc = {
|
|
79
|
+
token: this.options.wscToken,
|
|
80
|
+
sequence: 0,
|
|
81
|
+
};
|
|
82
|
+
connectMethod = this.recover.bind(this);
|
|
83
|
+
}
|
|
84
|
+
if (!this.options.autoRecover.enabled) {
|
|
85
|
+
await connectMethod();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
// code after is for auto recover
|
|
89
|
+
try {
|
|
90
|
+
await connectMethod();
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
if (e instanceof RestException_1.default) {
|
|
94
|
+
throw e; // such as InsufficientPermissions
|
|
95
|
+
}
|
|
96
|
+
if (this.options.debugMode) {
|
|
97
|
+
console.debug("Initial connect failed:", e);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
let retriesAttempted = 0;
|
|
101
|
+
let checking = false;
|
|
102
|
+
const check = async () => {
|
|
103
|
+
var _a, _b, _c;
|
|
104
|
+
if (!this.enabled) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (((_a = this.options.autoRecover) === null || _a === void 0 ? void 0 : _a.enabled) !== true) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (checking) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
checking = true;
|
|
114
|
+
if (((_b = this.ws) === null || _b === void 0 ? void 0 : _b.readyState) !== OPEN && ((_c = this.ws) === null || _c === void 0 ? void 0 : _c.readyState) !== CONNECTING) {
|
|
115
|
+
clearInterval(this.intervalHandle);
|
|
116
|
+
try {
|
|
117
|
+
await this.recover();
|
|
118
|
+
retriesAttempted = 0;
|
|
119
|
+
if (this.options.debugMode) {
|
|
120
|
+
console.debug(`Auto recover done, recoveryState: ${this.connectionDetails.recoveryState}`);
|
|
121
|
+
}
|
|
122
|
+
this.eventEmitter.emit(this.connectionDetails.recoveryState === "Successful"
|
|
123
|
+
? Events.autoRecoverSuccess
|
|
124
|
+
: Events.autoRecoverFailed, this.ws);
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
if (e instanceof RestException_1.default) {
|
|
128
|
+
throw e; // such as InsufficientPermissions
|
|
129
|
+
}
|
|
130
|
+
retriesAttempted += 1;
|
|
131
|
+
if (this.options.debugMode) {
|
|
132
|
+
console.debug("Auto recover error:", e);
|
|
133
|
+
}
|
|
134
|
+
this.eventEmitter.emit(Events.autoRecoverError, e);
|
|
135
|
+
}
|
|
136
|
+
this.intervalHandle = setInterval(check, this.options.autoRecover.checkInterval(retriesAttempted));
|
|
137
|
+
}
|
|
138
|
+
checking = false;
|
|
139
|
+
};
|
|
140
|
+
this.intervalHandle = setInterval(check, this.options.autoRecover.checkInterval(retriesAttempted));
|
|
141
|
+
// browser only code start
|
|
142
|
+
if (typeof window !== "undefined" && window.addEventListener) {
|
|
143
|
+
window.addEventListener("offline", () => {
|
|
144
|
+
var _a;
|
|
145
|
+
if (this.pingServerHandle) {
|
|
146
|
+
clearTimeout(this.pingServerHandle);
|
|
147
|
+
}
|
|
148
|
+
(_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
|
|
149
|
+
});
|
|
150
|
+
window.addEventListener("online", () => {
|
|
151
|
+
check();
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
// browser only code end
|
|
155
|
+
}
|
|
156
|
+
async recover() {
|
|
157
|
+
if (this._recoverPromise) {
|
|
158
|
+
return this._recoverPromise;
|
|
159
|
+
}
|
|
160
|
+
this._recoverPromise = this._recover();
|
|
161
|
+
try {
|
|
162
|
+
await this._recoverPromise;
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
this._recoverPromise = undefined;
|
|
166
|
+
}
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
async _recover() {
|
|
170
|
+
var _a, _b;
|
|
171
|
+
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === OPEN || ((_b = this.ws) === null || _b === void 0 ? void 0 : _b.readyState) === CONNECTING) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (!this.wsc || !this.wsc.token) {
|
|
175
|
+
await this.connect(false); // connect to WSG but do not recover
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (this.recoverTimestamp === undefined) {
|
|
179
|
+
this.recoverTimestamp = Date.now();
|
|
180
|
+
}
|
|
181
|
+
if (this.connectionDetails !== undefined &&
|
|
182
|
+
Date.now() - this.recoverTimestamp >
|
|
183
|
+
this.connectionDetails.recoveryTimeout * 1000) {
|
|
184
|
+
if (this.options.debugMode) {
|
|
185
|
+
console.debug("connect to WSG but do not recover");
|
|
186
|
+
}
|
|
187
|
+
await this.connect(false); // connect to WSG but do not recover
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
if (this.options.debugMode) {
|
|
191
|
+
console.debug("connect to WSG and recover");
|
|
192
|
+
}
|
|
193
|
+
await this.connect(true); // connect to WSG and recover
|
|
194
|
+
}
|
|
195
|
+
this.recoverTimestamp = undefined;
|
|
196
|
+
this.enable();
|
|
197
|
+
}
|
|
198
|
+
async pingServer() {
|
|
199
|
+
var _a, _b;
|
|
200
|
+
if (((_a = this.options.autoRecover) === null || _a === void 0 ? void 0 : _a.enabled) !== true) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (((_b = this.ws) === null || _b === void 0 ? void 0 : _b.readyState) !== OPEN) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
await this.ws.send(JSON.stringify([
|
|
208
|
+
{
|
|
209
|
+
type: "Heartbeat",
|
|
210
|
+
messageId: uuid(),
|
|
211
|
+
},
|
|
212
|
+
]));
|
|
213
|
+
}
|
|
214
|
+
catch (e) {
|
|
215
|
+
this.ws.close(); // Explicitly mark WS as closed
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async connect(recoverSession) {
|
|
219
|
+
if (this._connectPromise) {
|
|
220
|
+
return this._connectPromise;
|
|
221
|
+
}
|
|
222
|
+
this._connectPromise = this._connect(recoverSession);
|
|
223
|
+
try {
|
|
224
|
+
await this._connectPromise;
|
|
225
|
+
}
|
|
226
|
+
finally {
|
|
227
|
+
this._connectPromise = undefined;
|
|
228
|
+
}
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
async _connect(recoverSession = false) {
|
|
232
|
+
var _a;
|
|
233
|
+
if (!this.wsToken || Date.now() > this.wsTokenExpiresAt) {
|
|
234
|
+
const r = await this.rc.post("/restapi/oauth/wstoken");
|
|
235
|
+
this.wsToken = r.data;
|
|
236
|
+
// `expires_in` default value is 600 seconds. That's why we `* 0.8`
|
|
237
|
+
this.wsTokenExpiresAt = Date.now() + this.wsToken.expires_in * 0.8 * 1000;
|
|
238
|
+
}
|
|
239
|
+
let wsUri = `${this.wsToken.uri}?access_token=${this.wsToken.ws_access_token}`;
|
|
240
|
+
if (recoverSession && this.wsc) {
|
|
241
|
+
wsUri += `&wsc=${this.wsc.token}`;
|
|
242
|
+
}
|
|
243
|
+
this.ws = new isomorphic_ws_1.default(wsUri);
|
|
244
|
+
this.eventEmitter.emit(Events.newWebSocketObject, this.ws);
|
|
245
|
+
// override send method to wait for connecting
|
|
246
|
+
const send = this.ws.send.bind(this.ws);
|
|
247
|
+
this.ws.send = async (s) => {
|
|
248
|
+
if (this.ws.readyState === CONNECTING) {
|
|
249
|
+
await (0, wait_for_async_1.default)({
|
|
250
|
+
interval: 100,
|
|
251
|
+
condition: () => this.ws.readyState !== CONNECTING,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
await send(s);
|
|
255
|
+
};
|
|
256
|
+
if ((_a = this.options.autoRecover) === null || _a === void 0 ? void 0 : _a.enabled) {
|
|
257
|
+
this.ws.addEventListener("message", () => {
|
|
258
|
+
if (this.pingServerHandle) {
|
|
259
|
+
clearTimeout(this.pingServerHandle);
|
|
260
|
+
}
|
|
261
|
+
this.pingServerHandle = setTimeout(() => this.pingServer(), this.options.autoRecover.pingServerInterval);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
// debug mode to print all WebSocket traffic
|
|
265
|
+
if (this.options.debugMode) {
|
|
266
|
+
utils_1.default.debugWebSocket(this.ws);
|
|
267
|
+
}
|
|
268
|
+
// listen for new wsc data
|
|
269
|
+
this.ws.addEventListener("message", (mEvent) => {
|
|
270
|
+
const event = mEvent;
|
|
271
|
+
const [meta, body] = utils_1.default.splitWsgData(event.data);
|
|
272
|
+
if (meta.wsc &&
|
|
273
|
+
(!this.wsc ||
|
|
274
|
+
(meta.type === "ConnectionDetails" && body.recoveryState) ||
|
|
275
|
+
this.wsc.sequence < meta.wsc.sequence)) {
|
|
276
|
+
this.wsc = meta.wsc;
|
|
277
|
+
this.eventEmitter.emit(Events.newWsc, this.wsc);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
// get initial ConnectionDetails data
|
|
281
|
+
const [meta, body, event] = await utils_1.default.waitForWebSocketMessage(this.ws, (meta) => meta.type === "ConnectionDetails" || meta.type === "Error");
|
|
282
|
+
if (meta.type === "Error") {
|
|
283
|
+
throw new ConnectionException_1.default(event);
|
|
284
|
+
}
|
|
285
|
+
this.connectionDetails = body;
|
|
286
|
+
// fired when ws connection is ready for creating subscription
|
|
287
|
+
this.eventEmitter.emit(Events.connectionReady, this.ws);
|
|
288
|
+
// recover the subscription, if it exists and enabled
|
|
289
|
+
if (this.subscription && this.subscription.enabled) {
|
|
290
|
+
// because we have a new ws object
|
|
291
|
+
this.subscription.setupWsEventListener();
|
|
292
|
+
if (!recoverSession || this.connectionDetails.recoveryState === "Failed") {
|
|
293
|
+
// create new subscription if don't recover existing one
|
|
294
|
+
await this.subscription.subscribe();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// keepInterval means we do not clear the interval
|
|
299
|
+
async revoke(keepInterval = false) {
|
|
300
|
+
var _a, _b;
|
|
301
|
+
await ((_a = this.subscription) === null || _a === void 0 ? void 0 : _a.revoke());
|
|
302
|
+
this.subscription = undefined;
|
|
303
|
+
if (!keepInterval && this.intervalHandle) {
|
|
304
|
+
clearInterval(this.intervalHandle);
|
|
305
|
+
}
|
|
306
|
+
if (this.pingServerHandle) {
|
|
307
|
+
clearTimeout(this.pingServerHandle);
|
|
308
|
+
}
|
|
309
|
+
(_b = this.ws) === null || _b === void 0 ? void 0 : _b.close();
|
|
310
|
+
this.wsc = undefined;
|
|
311
|
+
this.wsToken = undefined;
|
|
312
|
+
this.wsTokenExpiresAt = 0;
|
|
313
|
+
this.disable();
|
|
314
|
+
}
|
|
315
|
+
async subscribe(eventFilters, callback, cache = undefined) {
|
|
316
|
+
const subscription = new subscription_1.default(this, eventFilters, callback);
|
|
317
|
+
if (cache === undefined || cache === null) {
|
|
318
|
+
await subscription.subscribe();
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
subscription.subscriptionInfo = cache;
|
|
322
|
+
await subscription.refresh();
|
|
323
|
+
}
|
|
324
|
+
this.subscription = subscription;
|
|
325
|
+
return subscription;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
exports.default = WebSocketExtension;
|
|
329
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;AAMA,gFAAwD;AA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AAI,KAAK,KAAK,IAAI,EAAE,CAAC;YAC1C,MAAM,YAAY,CAAC,SAAS,EAAE,CAAC;QACjC,CAAC;aAAM,CAAC;YACN,YAAY,CAAC,gBAAgB,GAAG,KAAK,CAAC;YACtC,MAAM,YAAY,CAAC,OAAO,EAAE,CAAC;QAC/B,CAAC;QACD,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,OAAO,YAAY,CAAC;IACtB,CAAC;CACF;AAED,kBAAe,kBAAkB,CAAC"}
|
package/src/index.ts
CHANGED
|
@@ -3,15 +3,15 @@ import type {
|
|
|
3
3
|
RestMethod,
|
|
4
4
|
RestRequestConfig,
|
|
5
5
|
RestResponse,
|
|
6
|
-
} from "@rc-ex/core/
|
|
7
|
-
import SdkExtension from "@rc-ex/core/
|
|
6
|
+
} from "@rc-ex/core/src/types";
|
|
7
|
+
import SdkExtension from "@rc-ex/core/src/SdkExtension";
|
|
8
8
|
import type { MessageEvent } from "isomorphic-ws";
|
|
9
9
|
import WS from "isomorphic-ws";
|
|
10
10
|
import hyperid from "hyperid";
|
|
11
11
|
import { EventEmitter } from "events";
|
|
12
12
|
import waitFor from "wait-for-async";
|
|
13
|
-
import RestException from "@rc-ex/core/
|
|
14
|
-
import type SubscriptionInfo from "@rc-ex/core/
|
|
13
|
+
import RestException from "@rc-ex/core/src/RestException";
|
|
14
|
+
import type SubscriptionInfo from "@rc-ex/core/src/definitions/SubscriptionInfo";
|
|
15
15
|
|
|
16
16
|
import { request } from "./rest";
|
|
17
17
|
import type {
|
package/src/rest.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { RestMethod, RestRequestConfig, RestResponse } from "@rc-ex/core/src/types";
|
|
2
|
+
import type { WebSocketExtensionInterface } from "./types";
|
|
3
|
+
export declare function request<T>(this: WebSocketExtensionInterface, method: RestMethod, endpoint: string, content?: {}, queryParams?: {}, config?: RestRequestConfig): Promise<RestResponse<T>>;
|
package/src/rest.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.request = request;
|
|
7
|
+
const RestException_1 = __importDefault(require("@rc-ex/core/src/RestException"));
|
|
8
|
+
const hyperid_1 = __importDefault(require("hyperid"));
|
|
9
|
+
const http_status_codes_1 = require("http-status-codes");
|
|
10
|
+
const utils_1 = __importDefault(require("./utils"));
|
|
11
|
+
const version = "0.16";
|
|
12
|
+
const uuid = (0, hyperid_1.default)();
|
|
13
|
+
async function request(method, endpoint, content, queryParams, config) {
|
|
14
|
+
var _a;
|
|
15
|
+
const newConfig = {
|
|
16
|
+
method,
|
|
17
|
+
baseURL: (_a = this.wsToken) === null || _a === void 0 ? void 0 : _a.uri,
|
|
18
|
+
url: endpoint,
|
|
19
|
+
data: content,
|
|
20
|
+
params: queryParams,
|
|
21
|
+
...config,
|
|
22
|
+
};
|
|
23
|
+
newConfig.headers = {
|
|
24
|
+
...newConfig.headers,
|
|
25
|
+
"X-User-Agent": `${this.rc.rest.appName}/${this.rc.rest.appVersion} ringcentral-extensible/ws/${version}`,
|
|
26
|
+
};
|
|
27
|
+
const messageId = uuid();
|
|
28
|
+
const requestBody = [
|
|
29
|
+
{
|
|
30
|
+
type: "ClientRequest",
|
|
31
|
+
messageId,
|
|
32
|
+
method: newConfig.method,
|
|
33
|
+
path: newConfig.url,
|
|
34
|
+
headers: newConfig.headers,
|
|
35
|
+
query: newConfig.params,
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
if (newConfig.data) {
|
|
39
|
+
requestBody.push(newConfig.data);
|
|
40
|
+
}
|
|
41
|
+
await this.ws.send(JSON.stringify(requestBody));
|
|
42
|
+
const [meta, body] = await utils_1.default.waitForWebSocketMessage(this.ws, (_meta) => _meta.messageId === messageId);
|
|
43
|
+
const response = {
|
|
44
|
+
data: body,
|
|
45
|
+
status: meta.status,
|
|
46
|
+
statusText: (0, http_status_codes_1.getReasonPhrase)(meta.status),
|
|
47
|
+
headers: meta.headers,
|
|
48
|
+
config: newConfig,
|
|
49
|
+
};
|
|
50
|
+
if (meta.type === "ClientRequest" && meta.status >= 200 && meta.status < 300) {
|
|
51
|
+
return response;
|
|
52
|
+
}
|
|
53
|
+
throw new RestException_1.default(response);
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=rest.js.map
|
package/src/rest.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rest.js","sourceRoot":"","sources":["rest.ts"],"names":[],"mappings":";;;;;AAgBA,0BAsDC;AAjED,kFAA0D;AAC1D,sDAA8B;AAC9B,yDAAoD;AAEpD,oDAA4B;AAG5B,MAAM,OAAO,GAAG,MAAM,CAAC;AAEvB,MAAM,IAAI,GAAG,IAAA,iBAAO,GAAE,CAAC;AAEhB,KAAK,UAAU,OAAO,CAE3B,MAAkB,EAClB,QAAgB,EAChB,OAAY,EACZ,WAAgB,EAChB,MAA0B;;IAE1B,MAAM,SAAS,GAAsB;QACnC,MAAM;QACN,OAAO,EAAE,MAAA,IAAI,CAAC,OAAO,0CAAE,GAAG;QAC1B,GAAG,EAAE,QAAQ;QACb,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,WAAW;QACnB,GAAG,MAAM;KACV,CAAC;IACF,SAAS,CAAC,OAAO,GAAG;QAClB,GAAG,SAAS,CAAC,OAAO;QACpB,cAAc,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC,IAAK,CAAC,OAAO,IACtC,IAAI,CAAC,EAAE,CAAC,IAAK,CAAC,UAChB,8BAA8B,OAAO,EAAE;KACjC,CAAC;IACT,MAAM,SAAS,GAAG,IAAI,EAAE,CAAC;IACzB,MAAM,WAAW,GAAG;QAClB;YACE,IAAI,EAAE,eAAe;YACrB,SAAS;YACT,MAAM,EAAE,SAAS,CAAC,MAAM;YACxB,IAAI,EAAE,SAAS,CAAC,GAAG;YACnB,OAAO,EAAE,SAAS,CAAC,OAAO;YAC1B,KAAK,EAAE,SAAS,CAAC,MAAM;SACxB;KACF,CAAC;IACF,IAAI,SAAS,CAAC,IAAI,EAAE,CAAC;QACnB,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IACD,MAAM,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC;IAChD,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,MAAM,eAAK,CAAC,uBAAuB,CACtD,IAAI,CAAC,EAAE,EACP,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,KAAK,SAAS,CACzC,CAAC;IACF,MAAM,QAAQ,GAAiB;QAC7B,IAAI,EAAE,IAAS;QACf,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,UAAU,EAAE,IAAA,mCAAe,EAAC,IAAI,CAAC,MAAM,CAAC;QACxC,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,MAAM,EAAE,SAAgB;KACzB,CAAC;IACF,IACE,IAAI,CAAC,IAAI,KAAK,eAAe,IAAI,IAAI,CAAC,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,MAAM,GAAG,GAAG,EACxE,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,MAAM,IAAI,uBAAa,CAAC,QAAQ,CAAC,CAAC;AACpC,CAAC"}
|
package/src/rest.ts
CHANGED
|
@@ -2,8 +2,8 @@ import type {
|
|
|
2
2
|
RestMethod,
|
|
3
3
|
RestRequestConfig,
|
|
4
4
|
RestResponse,
|
|
5
|
-
} from "@rc-ex/core/
|
|
6
|
-
import RestException from "@rc-ex/core/
|
|
5
|
+
} from "@rc-ex/core/src/types";
|
|
6
|
+
import RestException from "@rc-ex/core/src/RestException";
|
|
7
7
|
import hyperid from "hyperid";
|
|
8
8
|
import { getReasonPhrase } from "http-status-codes";
|
|
9
9
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type CreateSubscriptionRequest from "@rc-ex/core/src/definitions/CreateSubscriptionRequest";
|
|
2
|
+
import type SubscriptionInfo from "@rc-ex/core/src/definitions/SubscriptionInfo";
|
|
3
|
+
import type { MessageEvent } from "ws";
|
|
4
|
+
import type { WebSocketExtensionInterface } from "./types";
|
|
5
|
+
declare class Subscription {
|
|
6
|
+
subscriptionInfo?: SubscriptionInfo;
|
|
7
|
+
wse: WebSocketExtensionInterface;
|
|
8
|
+
eventFilters: string[];
|
|
9
|
+
eventListener: (event: MessageEvent) => void;
|
|
10
|
+
timeout?: NodeJS.Timeout;
|
|
11
|
+
enabled: boolean;
|
|
12
|
+
constructor(wse: WebSocketExtensionInterface, eventFilters: string[], callback: (event: {}) => void);
|
|
13
|
+
setupWsEventListener(): void;
|
|
14
|
+
get requestBody(): CreateSubscriptionRequest;
|
|
15
|
+
subscribe(): Promise<void>;
|
|
16
|
+
refresh(): Promise<void>;
|
|
17
|
+
revoke(): Promise<void>;
|
|
18
|
+
remove(): void;
|
|
19
|
+
}
|
|
20
|
+
export default Subscription;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const utils_1 = __importDefault(require("./utils"));
|
|
7
|
+
class Subscription {
|
|
8
|
+
constructor(wse, eventFilters, callback) {
|
|
9
|
+
this.enabled = true;
|
|
10
|
+
this.wse = wse;
|
|
11
|
+
this.eventFilters = eventFilters;
|
|
12
|
+
this.eventListener = (mEvent) => {
|
|
13
|
+
const event = mEvent;
|
|
14
|
+
const [meta, body] = utils_1.default
|
|
15
|
+
.splitWsgData(event.data);
|
|
16
|
+
if (this.enabled && meta.type === "ServerNotification" &&
|
|
17
|
+
body.subscriptionId === this.subscriptionInfo.id) {
|
|
18
|
+
callback(body);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
this.setupWsEventListener();
|
|
22
|
+
}
|
|
23
|
+
setupWsEventListener() {
|
|
24
|
+
this.wse.ws.addEventListener("message", this.eventListener);
|
|
25
|
+
}
|
|
26
|
+
get requestBody() {
|
|
27
|
+
return {
|
|
28
|
+
deliveryMode: { transportType: "WebSocket" }, // because WebSocket is not in spec
|
|
29
|
+
eventFilters: this.eventFilters,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
async subscribe() {
|
|
33
|
+
this.subscriptionInfo = (await this.wse.request("POST", "/restapi/v1.0/subscription", this.requestBody)).data;
|
|
34
|
+
}
|
|
35
|
+
async refresh() {
|
|
36
|
+
if (!this.subscriptionInfo) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
this.subscriptionInfo = (await this.wse.request("PUT", `/restapi/v1.0/subscription/${this.subscriptionInfo.id}`, this.requestBody)).data;
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
const re = e;
|
|
44
|
+
if (re.response && re.response.status === 404) {
|
|
45
|
+
// subscription expired
|
|
46
|
+
await this.subscribe();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async revoke() {
|
|
51
|
+
if (!this.subscriptionInfo) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
await this.wse.request("DELETE", `/restapi/v1.0/subscription/${this.subscriptionInfo.id}`);
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
const re = e;
|
|
59
|
+
if (re.response && re.response.status === 404) {
|
|
60
|
+
// ignore
|
|
61
|
+
if (this.wse.options.debugMode) {
|
|
62
|
+
console.debug(`Subscription ${this.subscriptionInfo.id} doesn't exist on server side`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else if (re.response && re.response.status === 401) {
|
|
66
|
+
// ignore
|
|
67
|
+
if (this.wse.options.debugMode) {
|
|
68
|
+
console.debug("Token invalid when trying to revoke subscription");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
this.remove();
|
|
76
|
+
}
|
|
77
|
+
remove() {
|
|
78
|
+
if (this.timeout) {
|
|
79
|
+
global.clearTimeout(this.timeout);
|
|
80
|
+
this.timeout = undefined;
|
|
81
|
+
}
|
|
82
|
+
this.enabled = false;
|
|
83
|
+
this.subscriptionInfo = undefined;
|
|
84
|
+
if (this.wse.ws) {
|
|
85
|
+
this.wse.ws.removeEventListener("message", this.eventListener);
|
|
86
|
+
}
|
|
87
|
+
this.wse.subscription = undefined;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.default = Subscription;
|
|
91
|
+
//# sourceMappingURL=subscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["subscription.ts"],"names":[],"mappings":";;;;;AAMA,oDAA4B;AAE5B,MAAM,YAAY;IAahB,YACE,GAAgC,EAChC,YAAsB,EACtB,QAA6B;QALxB,YAAO,GAAG,IAAI,CAAC;QAOpB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,aAAa,GAAG,CAAC,MAAoB,EAAE,EAAE;YAC5C,MAAM,KAAK,GAAG,MAAkB,CAAC;YACjC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,GAA0C,eAAK;iBAC9D,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YAC5B,IACE,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,IAAI,KAAK,oBAAoB;gBAClD,IAAI,CAAC,cAAc,KAAK,IAAI,CAAC,gBAAiB,CAAC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AAC,EAAE,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QACjE,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,YAAY,GAAG,SAAS,CAAC;IACpC,CAAC;CACF;AAED,kBAAe,YAAY,CAAC"}
|
package/src/subscription.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type CreateSubscriptionRequest from "@rc-ex/core/
|
|
2
|
-
import type SubscriptionInfo from "@rc-ex/core/
|
|
3
|
-
import type { RestResponse } from "@rc-ex/core/
|
|
1
|
+
import type CreateSubscriptionRequest from "@rc-ex/core/src/definitions/CreateSubscriptionRequest";
|
|
2
|
+
import type SubscriptionInfo from "@rc-ex/core/src/definitions/SubscriptionInfo";
|
|
3
|
+
import type { RestResponse } from "@rc-ex/core/src/types";
|
|
4
4
|
import type { MessageEvent } from "ws";
|
|
5
5
|
|
|
6
6
|
import type { WebSocketExtensionInterface, WsgEvent, WsgMeta } from "./types";
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type RingCentral from "@rc-ex/core";
|
|
2
|
+
import type { RestMethod, RestRequestConfig, RestResponse } from "@rc-ex/core/src/types";
|
|
3
|
+
import type WS from "isomorphic-ws";
|
|
4
|
+
export interface WsToken {
|
|
5
|
+
uri: string;
|
|
6
|
+
ws_access_token: string;
|
|
7
|
+
expires_in: number;
|
|
8
|
+
}
|
|
9
|
+
export type CheckInterval = (retriesAttempted: number) => number;
|
|
10
|
+
export interface WebSocketOptions {
|
|
11
|
+
restOverWebSocket?: boolean;
|
|
12
|
+
debugMode?: boolean;
|
|
13
|
+
autoRecover?: {
|
|
14
|
+
enabled: boolean;
|
|
15
|
+
checkInterval?: CheckInterval;
|
|
16
|
+
pingServerInterval?: number;
|
|
17
|
+
};
|
|
18
|
+
wscToken?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface WsgEvent {
|
|
21
|
+
data: string;
|
|
22
|
+
}
|
|
23
|
+
export interface Wsc {
|
|
24
|
+
token: string;
|
|
25
|
+
sequence: number;
|
|
26
|
+
}
|
|
27
|
+
export interface WsgMeta {
|
|
28
|
+
type: "ClientRequest" | "ServerNotification" | "Error" | "ConnectionDetails" | "Heartbeat";
|
|
29
|
+
messageId: string;
|
|
30
|
+
status: number;
|
|
31
|
+
headers: {
|
|
32
|
+
[key: string]: string;
|
|
33
|
+
};
|
|
34
|
+
wsc?: Wsc;
|
|
35
|
+
}
|
|
36
|
+
export interface WsgError {
|
|
37
|
+
errorCode: string;
|
|
38
|
+
message: string;
|
|
39
|
+
}
|
|
40
|
+
export interface ConnectionDetails {
|
|
41
|
+
creationTime: string;
|
|
42
|
+
maxConnectionsPerSession: number;
|
|
43
|
+
recoveryBufferSize: number;
|
|
44
|
+
recoveryTimeout: number;
|
|
45
|
+
idleTimeout: number;
|
|
46
|
+
absoluteTimeout: number;
|
|
47
|
+
maxActiveRequests: number;
|
|
48
|
+
recoveryState?: "Successful" | "Failed";
|
|
49
|
+
recoveryErrorCode?: string;
|
|
50
|
+
}
|
|
51
|
+
export interface WebSocketExtensionInterface {
|
|
52
|
+
options: WebSocketOptions;
|
|
53
|
+
subscription?: SubscriptionInterface;
|
|
54
|
+
ws: WS;
|
|
55
|
+
wsToken?: WsToken;
|
|
56
|
+
rc: RingCentral;
|
|
57
|
+
request: <T>(method: RestMethod, endpoint: string, content?: {}, queryParams?: {}, config?: RestRequestConfig) => Promise<RestResponse<T>>;
|
|
58
|
+
}
|
|
59
|
+
export interface SubscriptionInterface {
|
|
60
|
+
eventFilters: string[];
|
|
61
|
+
}
|
package/src/types.js
ADDED
package/src/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":""}
|