@rc-ex/ws 1.2.1 → 1.3.0

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 (56) hide show
  1. package/lib/esm/exceptions/ClosedException.d.ts +4 -0
  2. package/lib/esm/exceptions/ClosedException.js +7 -0
  3. package/lib/esm/exceptions/ClosedException.js.map +1 -0
  4. package/lib/esm/exceptions/ConnectionException.d.ts +7 -0
  5. package/lib/esm/exceptions/ConnectionException.js +13 -0
  6. package/lib/esm/exceptions/ConnectionException.js.map +1 -0
  7. package/lib/esm/exceptions/TimeoutException.d.ts +4 -0
  8. package/lib/esm/exceptions/TimeoutException.js +7 -0
  9. package/lib/esm/exceptions/TimeoutException.js.map +1 -0
  10. package/lib/esm/index.d.ts +44 -0
  11. package/lib/{index.js → esm/index.js} +62 -63
  12. package/lib/esm/index.js.map +1 -0
  13. package/lib/esm/rest.d.ts +3 -0
  14. package/lib/{rest.js → esm/rest.js} +10 -17
  15. package/lib/esm/rest.js.map +1 -0
  16. package/lib/esm/subscription.d.ts +20 -0
  17. package/lib/{subscription.js → esm/subscription.js} +9 -9
  18. package/lib/esm/subscription.js.map +1 -0
  19. package/lib/esm/types.d.ts +61 -0
  20. package/lib/esm/types.js +2 -0
  21. package/lib/esm/types.js.map +1 -0
  22. package/lib/esm/utils.d.ts +8 -0
  23. package/lib/{utils.js → esm/utils.js} +5 -10
  24. package/lib/esm/utils.js.map +1 -0
  25. package/lib/exceptions/ClosedException.cjs +26 -0
  26. package/lib/exceptions/ClosedException.js.map +1 -1
  27. package/lib/exceptions/ConnectionException.cjs +35 -0
  28. package/lib/exceptions/ConnectionException.d.ts +1 -1
  29. package/lib/exceptions/ConnectionException.js.map +1 -1
  30. package/lib/exceptions/TimeoutException.cjs +26 -0
  31. package/lib/exceptions/TimeoutException.js.map +1 -1
  32. package/lib/index.cjs +515 -0
  33. package/lib/index.d.ts +5 -5
  34. package/lib/index.js.map +1 -1
  35. package/lib/rest.cjs +104 -0
  36. package/lib/rest.d.ts +2 -2
  37. package/lib/rest.js.map +1 -1
  38. package/lib/subscription.cjs +178 -0
  39. package/lib/subscription.d.ts +3 -3
  40. package/lib/subscription.js.map +1 -1
  41. package/lib/types.d.ts +1 -1
  42. package/lib/utils.cjs +116 -0
  43. package/lib/utils.d.ts +1 -1
  44. package/lib/utils.js.map +1 -1
  45. package/package.json +10 -5
  46. package/src/exceptions/ConnectionException.ts +2 -2
  47. package/src/index.ts +15 -12
  48. package/src/rest.ts +4 -4
  49. package/src/subscription.ts +9 -5
  50. package/src/types.ts +1 -1
  51. package/src/utils.ts +3 -3
  52. package/tsconfig.json +0 -3
  53. package/lib/exceptions/ClosedException.js +0 -9
  54. package/lib/exceptions/ConnectionException.js +0 -16
  55. package/lib/exceptions/TimeoutException.js +0 -9
  56. /package/lib/{types.js → types.cjs} +0 -0
package/lib/rest.cjs ADDED
@@ -0,0 +1,104 @@
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
package/lib/rest.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import type { RestMethod, RestRequestConfig, RestResponse } from "@rc-ex/core/lib/types";
2
- import type { WebSocketExtensionInterface } from "./types";
1
+ import type { RestMethod, RestRequestConfig, RestResponse } from "@rc-ex/core/lib/esm/types.js";
2
+ import type { WebSocketExtensionInterface } from "./types.js";
3
3
  export declare function request<T>(this: WebSocketExtensionInterface, method: RestMethod, endpoint: string, content?: {}, queryParams?: {}, config?: RestRequestConfig): Promise<RestResponse<T>>;
package/lib/rest.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"rest.js","sourceRoot":"","sources":["../src/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;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AAC;wBACxC,OAAO,EAAE,IAAI,CAAC,OAAO;wBACrB,MAAM,EAAE,SAAgB;qBACzB,CAAC;oBACF,IACE,IAAI,CAAC,IAAI,KAAK,eAAe,IAAI,IAAI,CAAC,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,MAAM,GAAG,GAAG,EACxE,CAAC;wBACD,sBAAO,QAAQ,EAAC;oBAClB,CAAC;oBACD,MAAM,IAAI,0BAAa,CAAC,QAAQ,CAAC,CAAC;;;;CACnC"}
@@ -0,0 +1,178 @@
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
@@ -1,7 +1,7 @@
1
- import type CreateSubscriptionRequest from "@rc-ex/core/lib/definitions/CreateSubscriptionRequest";
2
- import type SubscriptionInfo from "@rc-ex/core/lib/definitions/SubscriptionInfo";
1
+ import type CreateSubscriptionRequest from "@rc-ex/core/lib/esm/definitions/CreateSubscriptionRequest.js";
2
+ import type SubscriptionInfo from "@rc-ex/core/lib/esm/definitions/SubscriptionInfo.js";
3
3
  import type { MessageEvent } from "ws";
4
- import type { WebSocketExtensionInterface } from "./types";
4
+ import type { WebSocketExtensionInterface } from "./types.js";
5
5
  declare class Subscription {
6
6
  subscriptionInfo?: SubscriptionInfo;
7
7
  wse: WebSocketExtensionInterface;
@@ -1 +1 @@
1
- {"version":3,"file":"subscription.js","sourceRoot":"","sources":["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AAS,CAAC;QAC3B,CAAC;QACD,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,gBAAgB,GAAG,SAAS,CAAC;QAClC,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;YAChB,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;QACjE,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,YAAY,GAAG,SAAS,CAAC;IACpC,CAAC;IACH,mBAAC;AAAD,CAAC,AAxHD,IAwHC;AAED,kBAAe,YAAY,CAAC"}
package/lib/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type RingCentral from "@rc-ex/core";
2
- import type { RestMethod, RestRequestConfig, RestResponse } from "@rc-ex/core/lib/types";
2
+ import type { RestMethod, RestRequestConfig, RestResponse } from "@rc-ex/core/lib/esm/types.js";
3
3
  import type WS from "isomorphic-ws";
4
4
  export interface WsToken {
5
5
  uri: string;
package/lib/utils.cjs ADDED
@@ -0,0 +1,116 @@
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
package/lib/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type WS from "isomorphic-ws";
2
- import type { WsgEvent, WsgMeta } from "./types";
2
+ import type { WsgEvent, WsgMeta } from "./types.js";
3
3
  declare class Utils {
4
4
  static splitWsgData(wsgData: string): [WsgMeta, any];
5
5
  static debugWebSocket(_ws: WS): void;
package/lib/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;AAIA,mFAA2D;AAC3D,qFAA6D;AAE7D,MAAM,KAAK;IACF,MAAM,CAAC,YAAY,CAAC,OAAe;QACxC,IAAI,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;YACpC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;YAC7C,OAAO;gBACL,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;gBACvC,OAAO,CAAC,SAAS,CAAC,KAAK,GAAG,CAAC,EAAE,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;aACjD,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAEM,MAAM,CAAC,cAAc,CAAC,GAAO;QAClC,MAAM,EAAE,GAAG,GAAG,CAAC;QACf,MAAM,IAAI,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9B,EAAE,CAAC,IAAI,GAAG,KAAK,EAAE,GAAW,EAAE,EAAE;YAC9B,MAAM,IAAI,CAAC,GAAG,CAAC,CAAC;YAChB,OAA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EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;gBAC/B,CAAC;YACH,CAAC,CAAC;YACF,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC1C,CAAC,CAAC,CAAC;IACL,CAAC;IACH,YAAC;AAAD,CAAC,AAxED,IAwEC;AAED,kBAAe,KAAK,CAAC"}
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@rc-ex/ws",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
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",
7
7
  "license": "MIT",
8
- "main": "lib/index.js",
9
- "types": "lib/index.d.ts",
8
+ "type": "module",
9
+ "types": "./lib/esm/index.d.ts",
10
+ "main": "./lib/index.cjs",
11
+ "module": "./lib/esm/index.js",
10
12
  "repository": {
11
13
  "type": "git",
12
14
  "url": "git+https://github.com/ringcentral/ringcentral-extensible.git"
@@ -26,7 +28,10 @@
26
28
  "ws": "^8.18.0"
27
29
  },
28
30
  "devDependencies": {
29
- "@rc-ex/core": "^1.5.1"
31
+ "@rc-ex/core": "^1.6.0"
30
32
  },
31
- "gitHead": "1a8a5becc832b92af73f1ad1f58b87f38fee3650"
33
+ "scripts": {
34
+ "rename-to-cjs": "find lib -type f -name '*.js' -exec sh -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\;"
35
+ },
36
+ "gitHead": "d480a5508918b03aa59b6f09a1b270437c535159"
32
37
  }
@@ -1,5 +1,5 @@
1
- import type { WsgError, WsgEvent, WsgMeta } from "../types";
2
- import Utils from "../utils";
1
+ import type { WsgError, WsgEvent, WsgMeta } from "../types.js";
2
+ import Utils from "../utils.js";
3
3
 
4
4
  class ConnectionException extends Error {
5
5
  public wsgEvent: WsgEvent;
package/src/index.ts CHANGED
@@ -3,17 +3,17 @@ import type {
3
3
  RestMethod,
4
4
  RestRequestConfig,
5
5
  RestResponse,
6
- } from "@rc-ex/core/lib/types";
7
- import SdkExtension from "@rc-ex/core/lib/SdkExtension";
6
+ } from "@rc-ex/core/lib/esm/types.js";
7
+ import SdkExtension from "@rc-ex/core/lib/esm/SdkExtension.js";
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/lib/RestException";
14
- import type SubscriptionInfo from "@rc-ex/core/lib/definitions/SubscriptionInfo";
13
+ import RestException from "@rc-ex/core/lib/esm/RestException.js";
14
+ import type SubscriptionInfo from "@rc-ex/core/lib/esm/definitions/SubscriptionInfo.js";
15
15
 
16
- import { request } from "./rest";
16
+ import { request } from "./rest.js";
17
17
  import type {
18
18
  ConnectionDetails,
19
19
  WebSocketExtensionInterface,
@@ -21,10 +21,10 @@ import type {
21
21
  Wsc,
22
22
  WsgEvent,
23
23
  WsToken,
24
- } from "./types";
25
- import Subscription from "./subscription";
26
- import ConnectionException from "./exceptions/ConnectionException";
27
- import Utils from "./utils";
24
+ } from "./types.js";
25
+ import Subscription from "./subscription.js";
26
+ import ConnectionException from "./exceptions/ConnectionException.js";
27
+ import Utils from "./utils.js";
28
28
 
29
29
  const CONNECTING = 0;
30
30
  const OPEN = 1;
@@ -200,14 +200,17 @@ class WebSocketExtension extends SdkExtension {
200
200
  );
201
201
 
202
202
  // browser only code start
203
- if (typeof window !== "undefined" && window.addEventListener) {
204
- window.addEventListener("offline", () => {
203
+ if (
204
+ typeof globalThis.window !== "undefined" &&
205
+ globalThis.window.addEventListener
206
+ ) {
207
+ globalThis.window.addEventListener("offline", () => {
205
208
  if (this.pingServerHandle) {
206
209
  clearTimeout(this.pingServerHandle);
207
210
  }
208
211
  this.ws?.close();
209
212
  });
210
- window.addEventListener("online", () => {
213
+ globalThis.window.addEventListener("online", () => {
211
214
  check();
212
215
  });
213
216
  }
package/src/rest.ts CHANGED
@@ -2,13 +2,13 @@ import type {
2
2
  RestMethod,
3
3
  RestRequestConfig,
4
4
  RestResponse,
5
- } from "@rc-ex/core/lib/types";
6
- import RestException from "@rc-ex/core/lib/RestException";
5
+ } from "@rc-ex/core/lib/esm/types.js";
6
+ import RestException from "@rc-ex/core/lib/esm/RestException.js";
7
7
  import hyperid from "hyperid";
8
8
  import { getReasonPhrase } from "http-status-codes";
9
9
 
10
- import Utils from "./utils";
11
- import type { WebSocketExtensionInterface } from "./types";
10
+ import Utils from "./utils.js";
11
+ import type { WebSocketExtensionInterface } from "./types.js";
12
12
 
13
13
  const version = "0.16";
14
14
 
@@ -1,10 +1,14 @@
1
- import type CreateSubscriptionRequest from "@rc-ex/core/lib/definitions/CreateSubscriptionRequest";
2
- import type SubscriptionInfo from "@rc-ex/core/lib/definitions/SubscriptionInfo";
3
- import type { RestResponse } from "@rc-ex/core/lib/types";
1
+ import type CreateSubscriptionRequest from "@rc-ex/core/lib/esm/definitions/CreateSubscriptionRequest.js";
2
+ import type SubscriptionInfo from "@rc-ex/core/lib/esm/definitions/SubscriptionInfo.js";
3
+ import type { RestResponse } from "@rc-ex/core/lib/esm/types.js";
4
4
  import type { MessageEvent } from "ws";
5
5
 
6
- import type { WebSocketExtensionInterface, WsgEvent, WsgMeta } from "./types";
7
- import Utils from "./utils";
6
+ import type {
7
+ WebSocketExtensionInterface,
8
+ WsgEvent,
9
+ WsgMeta,
10
+ } from "./types.js";
11
+ import Utils from "./utils.js";
8
12
 
9
13
  class Subscription {
10
14
  public subscriptionInfo?: SubscriptionInfo;
package/src/types.ts CHANGED
@@ -3,7 +3,7 @@ import type {
3
3
  RestMethod,
4
4
  RestRequestConfig,
5
5
  RestResponse,
6
- } from "@rc-ex/core/lib/types";
6
+ } from "@rc-ex/core/lib/esm/types.js";
7
7
  import type WS from "isomorphic-ws";
8
8
 
9
9
  export interface WsToken {