@rc-ex/ws 1.3.11 → 1.3.13
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 +7 -22
- package/dist/esm/exceptions/ConnectionException.js +16 -28
- package/dist/esm/exceptions/TimeoutException.js +7 -22
- package/dist/esm/index.js +309 -484
- package/dist/esm/rest.js +50 -94
- package/dist/esm/subscription.js +74 -151
- package/dist/esm/types.js +2 -1
- package/dist/esm/utils.js +40 -79
- package/package.json +2 -2
package/dist/esm/rest.js
CHANGED
|
@@ -1,97 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
4
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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"));
|
|
11
|
+
const version = "0.16";
|
|
12
|
+
const uuid = (0, hyperid_1.default)();
|
|
13
|
+
async function request(method, endpoint, content, queryParams, config) {
|
|
14
|
+
const newConfig = {
|
|
15
|
+
method,
|
|
16
|
+
baseURL: this.wsToken?.uri,
|
|
17
|
+
url: endpoint,
|
|
18
|
+
data: content,
|
|
19
|
+
params: queryParams,
|
|
20
|
+
...config,
|
|
21
|
+
};
|
|
22
|
+
newConfig.headers = {
|
|
23
|
+
...newConfig.headers,
|
|
24
|
+
"X-User-Agent": `${this.rc.rest.appName}/${this.rc.rest.appVersion} ringcentral-extensible/ws/${version}`,
|
|
25
|
+
};
|
|
26
|
+
const messageId = uuid();
|
|
27
|
+
const requestBody = [
|
|
28
|
+
{
|
|
29
|
+
type: "ClientRequest",
|
|
30
|
+
messageId,
|
|
31
|
+
method: newConfig.method,
|
|
32
|
+
path: newConfig.url,
|
|
33
|
+
headers: newConfig.headers,
|
|
34
|
+
query: newConfig.params,
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
if (newConfig.data) {
|
|
38
|
+
requestBody.push(newConfig.data);
|
|
46
39
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
case 0:
|
|
61
|
-
newConfig = __assign({ method: method, baseURL: (_b = this.wsToken) === null || _b === void 0 ? void 0 : _b.uri, url: endpoint, data: content, params: queryParams }, config);
|
|
62
|
-
newConfig.headers = __assign(__assign({}, newConfig.headers), { "X-User-Agent": "".concat(this.rc.rest.appName, "/").concat(this.rc.rest.appVersion, " ringcentral-extensible/ws/").concat(version) });
|
|
63
|
-
messageId = uuid();
|
|
64
|
-
requestBody = [
|
|
65
|
-
{
|
|
66
|
-
type: "ClientRequest",
|
|
67
|
-
messageId: messageId,
|
|
68
|
-
method: newConfig.method,
|
|
69
|
-
path: newConfig.url,
|
|
70
|
-
headers: newConfig.headers,
|
|
71
|
-
query: newConfig.params,
|
|
72
|
-
},
|
|
73
|
-
];
|
|
74
|
-
if (newConfig.data) {
|
|
75
|
-
requestBody.push(newConfig.data);
|
|
76
|
-
}
|
|
77
|
-
return [4 /*yield*/, this.ws.send(JSON.stringify(requestBody))];
|
|
78
|
-
case 1:
|
|
79
|
-
_c.sent();
|
|
80
|
-
return [4 /*yield*/, Utils.waitForWebSocketMessage(this.ws, function (_meta) { return _meta.messageId === messageId; })];
|
|
81
|
-
case 2:
|
|
82
|
-
_a = _c.sent(), meta = _a[0], body = _a[1];
|
|
83
|
-
response = {
|
|
84
|
-
data: body,
|
|
85
|
-
status: meta.status,
|
|
86
|
-
statusText: getReasonPhrase(meta.status),
|
|
87
|
-
headers: meta.headers,
|
|
88
|
-
config: newConfig,
|
|
89
|
-
};
|
|
90
|
-
if (meta.type === "ClientRequest" && meta.status >= 200 && meta.status < 300) {
|
|
91
|
-
return [2 /*return*/, response];
|
|
92
|
-
}
|
|
93
|
-
throw new RestException(response);
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
});
|
|
40
|
+
await this.ws.send(JSON.stringify(requestBody));
|
|
41
|
+
const [meta, body] = await utils_js_1.default.waitForWebSocketMessage(this.ws, (_meta) => _meta.messageId === messageId);
|
|
42
|
+
const response = {
|
|
43
|
+
data: body,
|
|
44
|
+
status: meta.status,
|
|
45
|
+
statusText: (0, http_status_codes_1.getReasonPhrase)(meta.status),
|
|
46
|
+
headers: meta.headers,
|
|
47
|
+
config: newConfig,
|
|
48
|
+
};
|
|
49
|
+
if (meta.type === "ClientRequest" && meta.status >= 200 && meta.status < 300) {
|
|
50
|
+
return response;
|
|
51
|
+
}
|
|
52
|
+
throw new RestException_1.default(response);
|
|
97
53
|
}
|
package/dist/esm/subscription.js
CHANGED
|
@@ -1,161 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
4
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
import Utils from "./utils.js";
|
|
38
|
-
var Subscription = /** @class */ (function () {
|
|
39
|
-
function Subscription(wse, eventFilters, callback) {
|
|
40
|
-
var _this = this;
|
|
41
|
-
this.enabled = true;
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const utils_js_1 = __importDefault(require("./utils.js"));
|
|
7
|
+
class Subscription {
|
|
8
|
+
subscriptionInfo;
|
|
9
|
+
wse;
|
|
10
|
+
eventFilters;
|
|
11
|
+
eventListener;
|
|
12
|
+
timeout;
|
|
13
|
+
enabled = true;
|
|
14
|
+
constructor(wse, eventFilters, callback) {
|
|
42
15
|
this.wse = wse;
|
|
43
16
|
this.eventFilters = eventFilters;
|
|
44
|
-
this.eventListener =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
.splitWsgData(event.data)
|
|
48
|
-
if (
|
|
49
|
-
body.subscriptionId ===
|
|
17
|
+
this.eventListener = (mEvent) => {
|
|
18
|
+
const event = mEvent;
|
|
19
|
+
const [meta, body] = utils_js_1.default
|
|
20
|
+
.splitWsgData(event.data);
|
|
21
|
+
if (this.enabled && meta.type === "ServerNotification" &&
|
|
22
|
+
body.subscriptionId === this.subscriptionInfo.id) {
|
|
50
23
|
callback(body);
|
|
51
24
|
}
|
|
52
25
|
};
|
|
53
26
|
this.setupWsEventListener();
|
|
54
27
|
}
|
|
55
|
-
|
|
28
|
+
setupWsEventListener() {
|
|
56
29
|
this.wse.ws.addEventListener("message", this.eventListener);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
_a = this;
|
|
96
|
-
return [4 /*yield*/, this.wse.request("PUT", "/restapi/v1.0/subscription/".concat(this.subscriptionInfo.id), this.requestBody)];
|
|
97
|
-
case 2:
|
|
98
|
-
_a.subscriptionInfo = (_b.sent()).data;
|
|
99
|
-
return [3 /*break*/, 6];
|
|
100
|
-
case 3:
|
|
101
|
-
e_1 = _b.sent();
|
|
102
|
-
re = e_1;
|
|
103
|
-
if (!(re.response && re.response.status === 404)) return [3 /*break*/, 5];
|
|
104
|
-
// subscription expired
|
|
105
|
-
return [4 /*yield*/, this.subscribe()];
|
|
106
|
-
case 4:
|
|
107
|
-
// subscription expired
|
|
108
|
-
_b.sent();
|
|
109
|
-
_b.label = 5;
|
|
110
|
-
case 5: return [3 /*break*/, 6];
|
|
111
|
-
case 6: return [2 /*return*/];
|
|
30
|
+
}
|
|
31
|
+
get requestBody() {
|
|
32
|
+
return {
|
|
33
|
+
deliveryMode: { transportType: "WebSocket" }, // because WebSocket is not in spec
|
|
34
|
+
eventFilters: this.eventFilters,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
async subscribe() {
|
|
38
|
+
this.subscriptionInfo = (await this.wse.request("POST", "/restapi/v1.0/subscription", this.requestBody)).data;
|
|
39
|
+
}
|
|
40
|
+
async refresh() {
|
|
41
|
+
if (!this.subscriptionInfo) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
this.subscriptionInfo = (await this.wse.request("PUT", `/restapi/v1.0/subscription/${this.subscriptionInfo.id}`, this.requestBody)).data;
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
const re = e;
|
|
49
|
+
if (re.response && re.response.status === 404) {
|
|
50
|
+
// subscription expired
|
|
51
|
+
await this.subscribe();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async revoke() {
|
|
56
|
+
if (!this.subscriptionInfo) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
await this.wse.request("DELETE", `/restapi/v1.0/subscription/${this.subscriptionInfo.id}`);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
const re = e;
|
|
64
|
+
if (re.response && re.response.status === 404) {
|
|
65
|
+
// ignore
|
|
66
|
+
if (this.wse.options.debugMode) {
|
|
67
|
+
console.debug(`Subscription ${this.subscriptionInfo.id} doesn't exist on server side`);
|
|
112
68
|
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
var e_2, re;
|
|
119
|
-
return __generator(this, function (_a) {
|
|
120
|
-
switch (_a.label) {
|
|
121
|
-
case 0:
|
|
122
|
-
if (!this.subscriptionInfo) {
|
|
123
|
-
return [2 /*return*/];
|
|
124
|
-
}
|
|
125
|
-
_a.label = 1;
|
|
126
|
-
case 1:
|
|
127
|
-
_a.trys.push([1, 3, , 4]);
|
|
128
|
-
return [4 /*yield*/, this.wse.request("DELETE", "/restapi/v1.0/subscription/".concat(this.subscriptionInfo.id))];
|
|
129
|
-
case 2:
|
|
130
|
-
_a.sent();
|
|
131
|
-
return [3 /*break*/, 4];
|
|
132
|
-
case 3:
|
|
133
|
-
e_2 = _a.sent();
|
|
134
|
-
re = e_2;
|
|
135
|
-
if (re.response && re.response.status === 404) {
|
|
136
|
-
// ignore
|
|
137
|
-
if (this.wse.options.debugMode) {
|
|
138
|
-
console.debug("Subscription ".concat(this.subscriptionInfo.id, " doesn't exist on server side"));
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else if (re.response && re.response.status === 401) {
|
|
142
|
-
// ignore
|
|
143
|
-
if (this.wse.options.debugMode) {
|
|
144
|
-
console.debug("Token invalid when trying to revoke subscription");
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
throw e_2;
|
|
149
|
-
}
|
|
150
|
-
return [3 /*break*/, 4];
|
|
151
|
-
case 4:
|
|
152
|
-
this.remove();
|
|
153
|
-
return [2 /*return*/];
|
|
69
|
+
}
|
|
70
|
+
else if (re.response && re.response.status === 401) {
|
|
71
|
+
// ignore
|
|
72
|
+
if (this.wse.options.debugMode) {
|
|
73
|
+
console.debug("Token invalid when trying to revoke subscription");
|
|
154
74
|
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
throw e;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
this.remove();
|
|
81
|
+
}
|
|
82
|
+
remove() {
|
|
159
83
|
if (this.timeout) {
|
|
160
84
|
global.clearTimeout(this.timeout);
|
|
161
85
|
this.timeout = undefined;
|
|
@@ -166,7 +90,6 @@ var Subscription = /** @class */ (function () {
|
|
|
166
90
|
this.wse.ws.removeEventListener("message", this.eventListener);
|
|
167
91
|
}
|
|
168
92
|
this.wse.subscription = undefined;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
export default Subscription;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.default = Subscription;
|
package/dist/esm/types.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/dist/esm/utils.js
CHANGED
|
@@ -1,100 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
4
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
import ClosedException from "./exceptions/ClosedException.js";
|
|
38
|
-
import TimeoutException from "./exceptions/TimeoutException.js";
|
|
39
|
-
var Utils = /** @class */ (function () {
|
|
40
|
-
function Utils() {
|
|
41
|
-
}
|
|
42
|
-
Utils.splitWsgData = function (wsgData) {
|
|
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"));
|
|
8
|
+
class Utils {
|
|
9
|
+
static splitWsgData(wsgData) {
|
|
43
10
|
if (wsgData.includes(",--Boundary")) {
|
|
44
|
-
|
|
11
|
+
const index = wsgData.indexOf(",--Boundary");
|
|
45
12
|
return [
|
|
46
13
|
JSON.parse(wsgData.substring(1, index)),
|
|
47
14
|
wsgData.substring(index + 1, wsgData.length - 1),
|
|
48
15
|
];
|
|
49
16
|
}
|
|
50
17
|
return JSON.parse(wsgData);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}); };
|
|
67
|
-
ws.addEventListener("message", function (mEvent) {
|
|
68
|
-
var event = mEvent;
|
|
69
|
-
console.debug("*** WebSocket incoming message: ***\n".concat(JSON.stringify(JSON.parse(event.data), null, 2), "\n******"));
|
|
18
|
+
}
|
|
19
|
+
static debugWebSocket(_ws) {
|
|
20
|
+
const ws = _ws;
|
|
21
|
+
const send = ws.send.bind(ws);
|
|
22
|
+
ws.send = async (str) => {
|
|
23
|
+
await send(str);
|
|
24
|
+
console.debug(`*** WebSocket outgoing message: ***
|
|
25
|
+
${JSON.stringify(JSON.parse(str), null, 2)}
|
|
26
|
+
******`);
|
|
27
|
+
};
|
|
28
|
+
ws.addEventListener("message", (mEvent) => {
|
|
29
|
+
const event = mEvent;
|
|
30
|
+
console.debug(`*** WebSocket incoming message: ***
|
|
31
|
+
${JSON.stringify(JSON.parse(event.data), null, 2)}
|
|
32
|
+
******`);
|
|
70
33
|
});
|
|
71
|
-
ws.addEventListener("open",
|
|
34
|
+
ws.addEventListener("open", (event) => {
|
|
72
35
|
console.debug("WebSocket open event:", event);
|
|
73
36
|
});
|
|
74
|
-
ws.addEventListener("error",
|
|
37
|
+
ws.addEventListener("error", (event) => {
|
|
75
38
|
console.debug("WebSocket error event:", event);
|
|
76
39
|
});
|
|
77
|
-
ws.addEventListener("close",
|
|
40
|
+
ws.addEventListener("close", (event) => {
|
|
78
41
|
console.debug("WebSocket close event:", event);
|
|
79
42
|
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
var checkHandle = setInterval(function () {
|
|
43
|
+
}
|
|
44
|
+
static waitForWebSocketMessage(ws, matchCondition, timeout = 60000) {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
const checkHandle = setInterval(() => {
|
|
85
47
|
if (ws.readyState === ws.CLOSED) {
|
|
86
48
|
clearInterval(checkHandle);
|
|
87
|
-
reject(new
|
|
49
|
+
reject(new ClosedException_js_1.default());
|
|
88
50
|
}
|
|
89
51
|
}, 1000);
|
|
90
|
-
|
|
52
|
+
const timeoutHandle = setTimeout(() => {
|
|
91
53
|
ws.removeEventListener("message", handler);
|
|
92
54
|
clearInterval(checkHandle);
|
|
93
|
-
reject(new
|
|
55
|
+
reject(new TimeoutException_js_1.default());
|
|
94
56
|
}, timeout);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
57
|
+
const handler = (mEvent) => {
|
|
58
|
+
const event = mEvent;
|
|
59
|
+
const [meta, body] = Utils.splitWsgData(event.data);
|
|
98
60
|
if (matchCondition(meta)) {
|
|
99
61
|
ws.removeEventListener("message", handler);
|
|
100
62
|
clearInterval(checkHandle);
|
|
@@ -104,7 +66,6 @@ var Utils = /** @class */ (function () {
|
|
|
104
66
|
};
|
|
105
67
|
ws.addEventListener("message", handler);
|
|
106
68
|
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
export default Utils;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.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.13",
|
|
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",
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "7d20cb5dee1f13d16284fdf08835d1c69262712e"
|
|
53
53
|
}
|