@rc-ex/ws 1.3.15 → 1.3.17
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/dist/esm/exceptions/ClosedException.js +1 -3
- package/dist/esm/exceptions/ConnectionException.js +3 -8
- package/dist/esm/exceptions/TimeoutException.js +1 -3
- package/dist/esm/index.js +26 -32
- package/dist/esm/rest.js +9 -15
- package/dist/esm/subscription.js +3 -8
- package/dist/esm/types.js +1 -2
- package/dist/esm/utils.js +5 -10
- package/package.json +5 -5
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
class ClosedException extends Error {
|
|
4
2
|
constructor(message) {
|
|
5
3
|
super(message ?? "WebSocket has been closed");
|
|
6
4
|
}
|
|
7
5
|
}
|
|
8
|
-
|
|
6
|
+
export default ClosedException;
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
|
|
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_js_1 = __importDefault(require("../utils.js"));
|
|
1
|
+
import Utils from "../utils.js";
|
|
7
2
|
class ConnectionException extends Error {
|
|
8
3
|
wsgEvent;
|
|
9
4
|
wsgError;
|
|
10
5
|
constructor(wsgEvent) {
|
|
11
|
-
const [, wsgError] =
|
|
6
|
+
const [, wsgError] = Utils.splitWsgData(wsgEvent.data);
|
|
12
7
|
super(JSON.stringify(wsgError, null, 2));
|
|
13
8
|
this.wsgEvent = wsgEvent;
|
|
14
9
|
this.wsgError = wsgError;
|
|
15
10
|
}
|
|
16
11
|
}
|
|
17
|
-
|
|
12
|
+
export default ConnectionException;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
class TimeoutException extends Error {
|
|
4
2
|
constructor(message) {
|
|
5
3
|
super(message ?? "Failed to receive expected WebSocket message in time.");
|
|
6
4
|
}
|
|
7
5
|
}
|
|
8
|
-
|
|
6
|
+
export default TimeoutException;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,23 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const wait_for_async_1 = __importDefault(require("wait-for-async"));
|
|
12
|
-
const RestException_1 = __importDefault(require("@rc-ex/core/RestException"));
|
|
13
|
-
const rest_js_1 = require("./rest.js");
|
|
14
|
-
const subscription_js_1 = __importDefault(require("./subscription.js"));
|
|
15
|
-
const ConnectionException_js_1 = __importDefault(require("./exceptions/ConnectionException.js"));
|
|
16
|
-
const utils_js_1 = __importDefault(require("./utils.js"));
|
|
1
|
+
import SdkExtension from "@rc-ex/core/SdkExtension";
|
|
2
|
+
import WS from "isomorphic-ws";
|
|
3
|
+
import hyperid from "hyperid";
|
|
4
|
+
import { EventEmitter } from "events";
|
|
5
|
+
import waitFor from "wait-for-async";
|
|
6
|
+
import RestException from "@rc-ex/core/RestException";
|
|
7
|
+
import { request } from "./rest.js";
|
|
8
|
+
import Subscription from "./subscription.js";
|
|
9
|
+
import ConnectionException from "./exceptions/ConnectionException.js";
|
|
10
|
+
import Utils from "./utils.js";
|
|
17
11
|
const CONNECTING = 0;
|
|
18
12
|
const OPEN = 1;
|
|
19
|
-
const uuid = (
|
|
20
|
-
var Events;
|
|
13
|
+
const uuid = hyperid();
|
|
14
|
+
export var Events;
|
|
21
15
|
(function (Events) {
|
|
22
16
|
Events["autoRecoverSuccess"] = "autoRecoverSuccess";
|
|
23
17
|
Events["autoRecoverFailed"] = "autoRecoverFailed";
|
|
@@ -25,9 +19,9 @@ var Events;
|
|
|
25
19
|
Events["newWebSocketObject"] = "newWebSocketObject";
|
|
26
20
|
Events["newWsc"] = "newWsc";
|
|
27
21
|
Events["connectionReady"] = "connectionReady";
|
|
28
|
-
})(Events || (
|
|
29
|
-
class WebSocketExtension extends
|
|
30
|
-
eventEmitter = new
|
|
22
|
+
})(Events || (Events = {}));
|
|
23
|
+
class WebSocketExtension extends SdkExtension {
|
|
24
|
+
eventEmitter = new EventEmitter();
|
|
31
25
|
options;
|
|
32
26
|
rc;
|
|
33
27
|
wsToken;
|
|
@@ -42,7 +36,7 @@ class WebSocketExtension extends SdkExtension_1.default {
|
|
|
42
36
|
pingServerHandle;
|
|
43
37
|
_recoverPromise;
|
|
44
38
|
_connectPromise;
|
|
45
|
-
request =
|
|
39
|
+
request = request; // request method was moved to another file to keep this file short
|
|
46
40
|
constructor(options = {}) {
|
|
47
41
|
super();
|
|
48
42
|
this.options = options;
|
|
@@ -100,7 +94,7 @@ class WebSocketExtension extends SdkExtension_1.default {
|
|
|
100
94
|
await connectMethod();
|
|
101
95
|
}
|
|
102
96
|
catch (e) {
|
|
103
|
-
if (e instanceof
|
|
97
|
+
if (e instanceof RestException) {
|
|
104
98
|
throw e; // such as InsufficientPermissions
|
|
105
99
|
}
|
|
106
100
|
if (this.options.debugMode) {
|
|
@@ -133,7 +127,7 @@ class WebSocketExtension extends SdkExtension_1.default {
|
|
|
133
127
|
: Events.autoRecoverFailed, this.ws);
|
|
134
128
|
}
|
|
135
129
|
catch (e) {
|
|
136
|
-
if (e instanceof
|
|
130
|
+
if (e instanceof RestException) {
|
|
137
131
|
throw e; // such as InsufficientPermissions
|
|
138
132
|
}
|
|
139
133
|
retriesAttempted += 1;
|
|
@@ -246,13 +240,13 @@ class WebSocketExtension extends SdkExtension_1.default {
|
|
|
246
240
|
if (recoverSession && this.wsc) {
|
|
247
241
|
wsUri += `&wsc=${this.wsc.token}`;
|
|
248
242
|
}
|
|
249
|
-
this.ws = new
|
|
243
|
+
this.ws = new WS(wsUri);
|
|
250
244
|
this.eventEmitter.emit(Events.newWebSocketObject, this.ws);
|
|
251
245
|
// override send method to wait for connecting
|
|
252
246
|
const send = this.ws.send.bind(this.ws);
|
|
253
247
|
this.ws.send = async (s) => {
|
|
254
248
|
if (this.ws.readyState === CONNECTING) {
|
|
255
|
-
await (
|
|
249
|
+
await waitFor({
|
|
256
250
|
interval: 100,
|
|
257
251
|
condition: () => this.ws.readyState !== CONNECTING,
|
|
258
252
|
});
|
|
@@ -269,12 +263,12 @@ class WebSocketExtension extends SdkExtension_1.default {
|
|
|
269
263
|
}
|
|
270
264
|
// debug mode to print all WebSocket traffic
|
|
271
265
|
if (this.options.debugMode) {
|
|
272
|
-
|
|
266
|
+
Utils.debugWebSocket(this.ws);
|
|
273
267
|
}
|
|
274
268
|
// listen for new wsc data
|
|
275
269
|
this.ws.addEventListener("message", (mEvent) => {
|
|
276
270
|
const event = mEvent;
|
|
277
|
-
const [meta, body] =
|
|
271
|
+
const [meta, body] = Utils.splitWsgData(event.data);
|
|
278
272
|
if (meta.wsc &&
|
|
279
273
|
(!this.wsc ||
|
|
280
274
|
(meta.type === "ConnectionDetails" && body.recoveryState) ||
|
|
@@ -284,9 +278,9 @@ class WebSocketExtension extends SdkExtension_1.default {
|
|
|
284
278
|
}
|
|
285
279
|
});
|
|
286
280
|
// get initial ConnectionDetails data
|
|
287
|
-
const [meta, body, event] = await
|
|
281
|
+
const [meta, body, event] = await Utils.waitForWebSocketMessage(this.ws, (meta) => meta.type === "ConnectionDetails" || meta.type === "Error");
|
|
288
282
|
if (meta.type === "Error") {
|
|
289
|
-
throw new
|
|
283
|
+
throw new ConnectionException(event);
|
|
290
284
|
}
|
|
291
285
|
this.connectionDetails = body;
|
|
292
286
|
// fired when ws connection is ready for creating subscription
|
|
@@ -318,7 +312,7 @@ class WebSocketExtension extends SdkExtension_1.default {
|
|
|
318
312
|
this.disable();
|
|
319
313
|
}
|
|
320
314
|
async subscribe(eventFilters, callback, cache = undefined) {
|
|
321
|
-
const subscription = new
|
|
315
|
+
const subscription = new Subscription(this, eventFilters, callback);
|
|
322
316
|
if (cache === undefined || cache === null) {
|
|
323
317
|
await subscription.subscribe();
|
|
324
318
|
}
|
|
@@ -330,4 +324,4 @@ class WebSocketExtension extends SdkExtension_1.default {
|
|
|
330
324
|
return subscription;
|
|
331
325
|
}
|
|
332
326
|
}
|
|
333
|
-
|
|
327
|
+
export default WebSocketExtension;
|
package/dist/esm/rest.js
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.request = request;
|
|
7
|
-
const RestException_1 = __importDefault(require("@rc-ex/core/RestException"));
|
|
8
|
-
const hyperid_1 = __importDefault(require("hyperid"));
|
|
9
|
-
const http_status_codes_1 = require("http-status-codes");
|
|
10
|
-
const utils_js_1 = __importDefault(require("./utils.js"));
|
|
1
|
+
import RestException from "@rc-ex/core/RestException";
|
|
2
|
+
import hyperid from "hyperid";
|
|
3
|
+
import { getReasonPhrase } from "http-status-codes";
|
|
4
|
+
import Utils from "./utils.js";
|
|
11
5
|
const version = "0.16";
|
|
12
|
-
const uuid = (
|
|
13
|
-
async function request(method, endpoint, content, queryParams, config) {
|
|
6
|
+
const uuid = hyperid();
|
|
7
|
+
export async function request(method, endpoint, content, queryParams, config) {
|
|
14
8
|
const newConfig = {
|
|
15
9
|
method,
|
|
16
10
|
baseURL: this.wsToken?.uri,
|
|
@@ -38,16 +32,16 @@ async function request(method, endpoint, content, queryParams, config) {
|
|
|
38
32
|
requestBody.push(newConfig.data);
|
|
39
33
|
}
|
|
40
34
|
await this.ws.send(JSON.stringify(requestBody));
|
|
41
|
-
const [meta, body] = await
|
|
35
|
+
const [meta, body] = await Utils.waitForWebSocketMessage(this.ws, (_meta) => _meta.messageId === messageId);
|
|
42
36
|
const response = {
|
|
43
37
|
data: body,
|
|
44
38
|
status: meta.status,
|
|
45
|
-
statusText:
|
|
39
|
+
statusText: getReasonPhrase(meta.status),
|
|
46
40
|
headers: meta.headers,
|
|
47
41
|
config: newConfig,
|
|
48
42
|
};
|
|
49
43
|
if (meta.type === "ClientRequest" && meta.status >= 200 && meta.status < 300) {
|
|
50
44
|
return response;
|
|
51
45
|
}
|
|
52
|
-
throw new
|
|
46
|
+
throw new RestException(response);
|
|
53
47
|
}
|
package/dist/esm/subscription.js
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
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_js_1 = __importDefault(require("./utils.js"));
|
|
1
|
+
import Utils from "./utils.js";
|
|
7
2
|
class Subscription {
|
|
8
3
|
subscriptionInfo;
|
|
9
4
|
wse;
|
|
@@ -16,7 +11,7 @@ class Subscription {
|
|
|
16
11
|
this.eventFilters = eventFilters;
|
|
17
12
|
this.eventListener = (mEvent) => {
|
|
18
13
|
const event = mEvent;
|
|
19
|
-
const [meta, body] =
|
|
14
|
+
const [meta, body] = Utils
|
|
20
15
|
.splitWsgData(event.data);
|
|
21
16
|
if (this.enabled && meta.type === "ServerNotification" &&
|
|
22
17
|
body.subscriptionId === this.subscriptionInfo.id) {
|
|
@@ -92,4 +87,4 @@ class Subscription {
|
|
|
92
87
|
this.wse.subscription = undefined;
|
|
93
88
|
}
|
|
94
89
|
}
|
|
95
|
-
|
|
90
|
+
export default Subscription;
|
package/dist/esm/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/esm/utils.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const ClosedException_js_1 = __importDefault(require("./exceptions/ClosedException.js"));
|
|
7
|
-
const TimeoutException_js_1 = __importDefault(require("./exceptions/TimeoutException.js"));
|
|
1
|
+
import ClosedException from "./exceptions/ClosedException.js";
|
|
2
|
+
import TimeoutException from "./exceptions/TimeoutException.js";
|
|
8
3
|
class Utils {
|
|
9
4
|
static splitWsgData(wsgData) {
|
|
10
5
|
if (wsgData.includes(",--Boundary")) {
|
|
@@ -46,13 +41,13 @@ ${JSON.stringify(JSON.parse(event.data), null, 2)}
|
|
|
46
41
|
const checkHandle = setInterval(() => {
|
|
47
42
|
if (ws.readyState === ws.CLOSED) {
|
|
48
43
|
clearInterval(checkHandle);
|
|
49
|
-
reject(new
|
|
44
|
+
reject(new ClosedException());
|
|
50
45
|
}
|
|
51
46
|
}, 1000);
|
|
52
47
|
const timeoutHandle = setTimeout(() => {
|
|
53
48
|
ws.removeEventListener("message", handler);
|
|
54
49
|
clearInterval(checkHandle);
|
|
55
|
-
reject(new
|
|
50
|
+
reject(new TimeoutException());
|
|
56
51
|
}, timeout);
|
|
57
52
|
const handler = (mEvent) => {
|
|
58
53
|
const event = mEvent;
|
|
@@ -68,4 +63,4 @@ ${JSON.stringify(JSON.parse(event.data), null, 2)}
|
|
|
68
63
|
});
|
|
69
64
|
}
|
|
70
65
|
}
|
|
71
|
-
|
|
66
|
+
export default Utils;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rc-ex/ws",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.17",
|
|
4
4
|
"description": "WebSocket extension for ringcentral-extensible project",
|
|
5
5
|
"author": "Tyler Liu <tyler.liu@ringcentral.com>",
|
|
6
6
|
"homepage": "https://github.com/ringcentral/ringcentral-extensible/tree/master/packages/extensions/ws",
|
|
@@ -39,15 +39,15 @@
|
|
|
39
39
|
"access": "public"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@types/ws": "^8.
|
|
42
|
+
"@types/ws": "^8.18.0",
|
|
43
43
|
"http-status-codes": "^2.3.0",
|
|
44
44
|
"hyperid": "^3.3.0",
|
|
45
45
|
"isomorphic-ws": "^5.0.0",
|
|
46
|
-
"wait-for-async": "^0.7.
|
|
47
|
-
"ws": "^8.18.
|
|
46
|
+
"wait-for-async": "^0.7.13",
|
|
47
|
+
"ws": "^8.18.1"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "45a55ad7b1ed4a4d9d64a179f97952f20942863a"
|
|
53
53
|
}
|