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