@rc-ex/ws 1.3.2 → 1.3.4

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 (88) hide show
  1. package/dist/cjs/exceptions/ClosedException.js +26 -0
  2. package/dist/cjs/exceptions/ClosedException.js.map +1 -0
  3. package/{lib → dist/cjs}/exceptions/ConnectionException.d.ts +1 -1
  4. package/dist/cjs/exceptions/ConnectionException.js +35 -0
  5. package/dist/cjs/exceptions/ConnectionException.js.map +1 -0
  6. package/dist/cjs/exceptions/TimeoutException.js +26 -0
  7. package/dist/cjs/exceptions/TimeoutException.js.map +1 -0
  8. package/{lib/esm → dist/cjs}/index.d.ts +6 -6
  9. package/dist/cjs/index.js +515 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/{lib/esm → dist/cjs}/rest.d.ts +2 -2
  12. package/dist/cjs/rest.js +104 -0
  13. package/dist/cjs/rest.js.map +1 -0
  14. package/{lib → dist/cjs}/subscription.d.ts +3 -3
  15. package/dist/cjs/subscription.js +178 -0
  16. package/dist/cjs/subscription.js.map +1 -0
  17. package/{lib → dist/cjs}/types.d.ts +2 -2
  18. package/{lib/esm → dist/cjs}/utils.d.ts +1 -1
  19. package/dist/cjs/utils.js +116 -0
  20. package/dist/cjs/utils.js.map +1 -0
  21. package/dist/esm/exceptions/ClosedException.js +24 -0
  22. package/dist/esm/exceptions/ClosedException.js.map +1 -0
  23. package/{lib → dist}/esm/exceptions/ConnectionException.d.ts +1 -1
  24. package/dist/esm/exceptions/ConnectionException.js +30 -0
  25. package/dist/esm/exceptions/ConnectionException.js.map +1 -0
  26. package/dist/esm/exceptions/TimeoutException.js +24 -0
  27. package/dist/esm/exceptions/TimeoutException.js.map +1 -0
  28. package/{lib → dist/esm}/index.d.ts +6 -6
  29. package/dist/esm/index.js +509 -0
  30. package/dist/esm/index.js.map +1 -0
  31. package/{lib → dist/esm}/rest.d.ts +2 -2
  32. package/dist/esm/rest.js +98 -0
  33. package/dist/esm/rest.js.map +1 -0
  34. package/{lib → dist}/esm/subscription.d.ts +3 -3
  35. package/dist/esm/subscription.js +173 -0
  36. package/dist/esm/subscription.js.map +1 -0
  37. package/{lib → dist}/esm/types.d.ts +2 -2
  38. package/dist/esm/types.js +2 -0
  39. package/dist/esm/types.js.map +1 -0
  40. package/{lib → dist/esm}/utils.d.ts +1 -1
  41. package/dist/esm/utils.js +111 -0
  42. package/dist/esm/utils.js.map +1 -0
  43. package/package.json +23 -9
  44. package/src/exceptions/ConnectionException.ts +2 -2
  45. package/src/index.ts +10 -10
  46. package/src/rest.ts +4 -4
  47. package/src/subscription.ts +5 -9
  48. package/src/types.ts +2 -2
  49. package/src/utils.ts +3 -3
  50. package/tsconfig.cjs.json +28 -0
  51. package/tsconfig.esm.json +28 -0
  52. package/lib/esm/exceptions/ClosedException.js +0 -9
  53. package/lib/esm/exceptions/ClosedException.js.map +0 -1
  54. package/lib/esm/exceptions/ConnectionException.js +0 -18
  55. package/lib/esm/exceptions/ConnectionException.js.map +0 -1
  56. package/lib/esm/exceptions/TimeoutException.js +0 -9
  57. package/lib/esm/exceptions/TimeoutException.js.map +0 -1
  58. package/lib/esm/index.js +0 -334
  59. package/lib/esm/index.js.map +0 -1
  60. package/lib/esm/rest.js +0 -54
  61. package/lib/esm/rest.js.map +0 -1
  62. package/lib/esm/subscription.js +0 -96
  63. package/lib/esm/subscription.js.map +0 -1
  64. package/lib/esm/utils.js +0 -72
  65. package/lib/esm/utils.js.map +0 -1
  66. package/lib/exceptions/ClosedException.js +0 -9
  67. package/lib/exceptions/ClosedException.js.map +0 -1
  68. package/lib/exceptions/ConnectionException.js +0 -18
  69. package/lib/exceptions/ConnectionException.js.map +0 -1
  70. package/lib/exceptions/TimeoutException.js +0 -9
  71. package/lib/exceptions/TimeoutException.js.map +0 -1
  72. package/lib/index.js +0 -334
  73. package/lib/index.js.map +0 -1
  74. package/lib/rest.js +0 -54
  75. package/lib/rest.js.map +0 -1
  76. package/lib/subscription.js +0 -96
  77. package/lib/subscription.js.map +0 -1
  78. package/lib/types.js +0 -3
  79. package/lib/types.js.map +0 -1
  80. package/lib/utils.js +0 -72
  81. package/lib/utils.js.map +0 -1
  82. package/tsconfig.json +0 -8
  83. /package/{lib/esm → dist/cjs}/exceptions/ClosedException.d.ts +0 -0
  84. /package/{lib/esm → dist/cjs}/exceptions/TimeoutException.d.ts +0 -0
  85. /package/{lib/esm → dist/cjs}/types.js +0 -0
  86. /package/{lib/esm → dist/cjs}/types.js.map +0 -0
  87. /package/{lib → dist/esm}/exceptions/ClosedException.d.ts +0 -0
  88. /package/{lib → dist/esm}/exceptions/TimeoutException.d.ts +0 -0
@@ -0,0 +1,173 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
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
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ 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);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
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 Utils from "./utils";
38
+ var Subscription = /** @class */ (function () {
39
+ function Subscription(wse, eventFilters, callback) {
40
+ var _this = this;
41
+ this.enabled = true;
42
+ this.wse = wse;
43
+ this.eventFilters = eventFilters;
44
+ this.eventListener = function (mEvent) {
45
+ var event = mEvent;
46
+ var _a = Utils
47
+ .splitWsgData(event.data), meta = _a[0], body = _a[1];
48
+ if (_this.enabled && meta.type === "ServerNotification" &&
49
+ body.subscriptionId === _this.subscriptionInfo.id) {
50
+ callback(body);
51
+ }
52
+ };
53
+ this.setupWsEventListener();
54
+ }
55
+ Subscription.prototype.setupWsEventListener = function () {
56
+ this.wse.ws.addEventListener("message", this.eventListener);
57
+ };
58
+ Object.defineProperty(Subscription.prototype, "requestBody", {
59
+ get: function () {
60
+ return {
61
+ deliveryMode: { transportType: "WebSocket" }, // because WebSocket is not in spec
62
+ eventFilters: this.eventFilters,
63
+ };
64
+ },
65
+ enumerable: false,
66
+ configurable: true
67
+ });
68
+ Subscription.prototype.subscribe = function () {
69
+ return __awaiter(this, void 0, void 0, function () {
70
+ var _a;
71
+ return __generator(this, function (_b) {
72
+ switch (_b.label) {
73
+ case 0:
74
+ _a = this;
75
+ return [4 /*yield*/, this.wse.request("POST", "/restapi/v1.0/subscription", this.requestBody)];
76
+ case 1:
77
+ _a.subscriptionInfo = (_b.sent()).data;
78
+ return [2 /*return*/];
79
+ }
80
+ });
81
+ });
82
+ };
83
+ Subscription.prototype.refresh = function () {
84
+ return __awaiter(this, void 0, void 0, function () {
85
+ var _a, e_1, re;
86
+ return __generator(this, function (_b) {
87
+ switch (_b.label) {
88
+ case 0:
89
+ if (!this.subscriptionInfo) {
90
+ return [2 /*return*/];
91
+ }
92
+ _b.label = 1;
93
+ case 1:
94
+ _b.trys.push([1, 3, , 6]);
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*/];
112
+ }
113
+ });
114
+ });
115
+ };
116
+ Subscription.prototype.revoke = function () {
117
+ return __awaiter(this, void 0, void 0, function () {
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*/];
154
+ }
155
+ });
156
+ });
157
+ };
158
+ Subscription.prototype.remove = function () {
159
+ if (this.timeout) {
160
+ global.clearTimeout(this.timeout);
161
+ this.timeout = undefined;
162
+ }
163
+ this.enabled = false;
164
+ this.subscriptionInfo = undefined;
165
+ if (this.wse.ws) {
166
+ this.wse.ws.removeEventListener("message", this.eventListener);
167
+ }
168
+ this.wse.subscription = undefined;
169
+ };
170
+ return Subscription;
171
+ }());
172
+ export default Subscription;
173
+ //# sourceMappingURL=subscription.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../src/subscription.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,OAAO,KAAK,MAAM,SAAS,CAAC;AAE5B;IAaE,sBACE,GAAgC,EAChC,YAAsB,EACtB,QAA6B;QAH/B,iBAmBC;QArBM,YAAO,GAAG,IAAI,CAAC;QAOpB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,aAAa,GAAG,UAAC,MAAoB;YACxC,IAAM,KAAK,GAAG,MAAkB,CAAC;YAC3B,IAAA,KAAsD,KAAK;iBAC9D,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,EADpB,IAAI,QAAA,EAAE,IA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AO,GAAG,SAAS,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,eAAe,YAAY,CAAC"}
@@ -1,5 +1,5 @@
1
- import type RingCentral from "@rc-ex/core";
2
- import type { RestMethod, RestRequestConfig, RestResponse } from "@rc-ex/core/lib/esm/types.js";
1
+ import type RingCentral from "@rc-ex/core/index";
2
+ import type { RestMethod, RestRequestConfig, RestResponse } from "@rc-ex/core/types";
3
3
  import type WS from "isomorphic-ws";
4
4
  export interface WsToken {
5
5
  uri: string;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
@@ -1,5 +1,5 @@
1
1
  import type WS from "isomorphic-ws";
2
- import type { WsgEvent, WsgMeta } from "./types.js";
2
+ import type { WsgEvent, WsgMeta } from "./types";
3
3
  declare class Utils {
4
4
  static splitWsgData(wsgData: string): [WsgMeta, any];
5
5
  static debugWebSocket(_ws: WS): void;
@@ -0,0 +1,111 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
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
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ 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);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
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";
38
+ import TimeoutException from "./exceptions/TimeoutException";
39
+ var Utils = /** @class */ (function () {
40
+ function Utils() {
41
+ }
42
+ Utils.splitWsgData = function (wsgData) {
43
+ if (wsgData.includes(",--Boundary")) {
44
+ var index = wsgData.indexOf(",--Boundary");
45
+ return [
46
+ JSON.parse(wsgData.substring(1, index)),
47
+ wsgData.substring(index + 1, wsgData.length - 1),
48
+ ];
49
+ }
50
+ return JSON.parse(wsgData);
51
+ };
52
+ Utils.debugWebSocket = function (_ws) {
53
+ var _this = this;
54
+ var ws = _ws;
55
+ var send = ws.send.bind(ws);
56
+ ws.send = function (str) { return __awaiter(_this, void 0, void 0, function () {
57
+ return __generator(this, function (_a) {
58
+ switch (_a.label) {
59
+ case 0: return [4 /*yield*/, send(str)];
60
+ case 1:
61
+ _a.sent();
62
+ console.debug("*** WebSocket outgoing message: ***\n".concat(JSON.stringify(JSON.parse(str), null, 2), "\n******"));
63
+ return [2 /*return*/];
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******"));
70
+ });
71
+ ws.addEventListener("open", function (event) {
72
+ console.debug("WebSocket open event:", event);
73
+ });
74
+ ws.addEventListener("error", function (event) {
75
+ console.debug("WebSocket error event:", event);
76
+ });
77
+ ws.addEventListener("close", function (event) {
78
+ console.debug("WebSocket close event:", event);
79
+ });
80
+ };
81
+ Utils.waitForWebSocketMessage = function (ws, matchCondition, timeout) {
82
+ if (timeout === void 0) { timeout = 60000; }
83
+ return new Promise(function (resolve, reject) {
84
+ var checkHandle = setInterval(function () {
85
+ if (ws.readyState === ws.CLOSED) {
86
+ clearInterval(checkHandle);
87
+ reject(new ClosedException());
88
+ }
89
+ }, 1000);
90
+ var timeoutHandle = setTimeout(function () {
91
+ ws.removeEventListener("message", handler);
92
+ clearInterval(checkHandle);
93
+ reject(new TimeoutException());
94
+ }, timeout);
95
+ var handler = function (mEvent) {
96
+ var event = mEvent;
97
+ var _a = Utils.splitWsgData(event.data), meta = _a[0], body = _a[1];
98
+ if (matchCondition(meta)) {
99
+ ws.removeEventListener("message", handler);
100
+ clearInterval(checkHandle);
101
+ clearTimeout(timeoutHandle);
102
+ resolve([meta, body, event]);
103
+ }
104
+ };
105
+ ws.addEventListener("message", handler);
106
+ });
107
+ };
108
+ return Utils;
109
+ }());
110
+ export default Utils;
111
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,OAAO,eAAe,MAAM,8BAA8B,CAAC;AAC3D,OAAO,gBAAgB,MAAM,+BAA+B,CAAC;AAE7D;IAAA;IAwEA,CAAC;IAvEe,kBAAY,GAA1B,UAA2B,OAAe;QACxC,IAAI,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;YACpC,IAAM,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;IAEa,oBAAc,GAA5B,UAA6B,GAAO;QAApC,iBA4BC;QA3BC,IAAM,EAAE,GAAG,GAAG,CAAC;QACf,IAAM,IAAI,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9B,EAAE,CAAC,IAAI,GAAG,UAAO,GAAW;;;4BAC1B,qBAAM,IAAI,CAAC,GAAG,CAAC,EAAA;;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ACH,CAAC,EAAE,IAAI,CAAC,CAAC;YACT,IAAM,aAAa,GAAG,UAAU,CAAC;gBAC/B,EAAE,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;gBAC3C,aAAa,CAAC,WAAW,CAAC,CAAC;gBAC3B,MAAM,CAAC,IAAI,gBAAgB,EAAE,CAAC,CAAC;YACjC,CAAC,EAAE,OAAO,CAAC,CAAC;YACZ,IAAM,OAAO,GAAG,UAAC,MAAoB;gBACnC,IAAM,KAAK,GAAG,MAAkB,CAAC;gBAC3B,IAAA,KAAe,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,EAA5C,IAAI,QAAA,EAAE,IAAI,QAAkC,CAAC;gBACpD,IAAI,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC;oBACzB,EAAE,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;oBAC3C,aAAa,CAAC,WAAW,CAAC,CAAC;oBAC3B,YAAY,CAAC,aAAa,CAAC,CAAC;oBAC5B,OAAO,CAAC,CAAC,IAAI,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,eAAe,KAAK,CAAC"}
package/package.json CHANGED
@@ -1,13 +1,30 @@
1
1
  {
2
2
  "name": "@rc-ex/ws",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
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
- "types": "./lib/esm/index.d.ts",
9
- "main": "./lib/index.js",
10
- "module": "./lib/esm/index.js",
8
+ "main": "dist/cjs/index.js",
9
+ "types": "dist/cjs/index.d.ts",
10
+ "module": "dist/esm/index.js",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/esm/index.js",
14
+ "require": "./dist/cjs/index.js",
15
+ "types": "./dist/esm/index.d.ts"
16
+ },
17
+ "./lib/*": {
18
+ "import": "./dist/esm/*.js",
19
+ "require": "./dist/cjs/*.js",
20
+ "types": "./dist/esm/*.d.ts"
21
+ },
22
+ "./*": {
23
+ "import": "./dist/esm/*.js",
24
+ "require": "./dist/cjs/*.js",
25
+ "types": "./dist/esm/*.d.ts"
26
+ }
27
+ },
11
28
  "repository": {
12
29
  "type": "git",
13
30
  "url": "git+https://github.com/ringcentral/ringcentral-extensible.git"
@@ -26,11 +43,8 @@
26
43
  "wait-for-async": "^0.7.10",
27
44
  "ws": "^8.18.0"
28
45
  },
29
- "devDependencies": {
30
- "@rc-ex/core": "^1.6.2"
31
- },
32
46
  "scripts": {
33
- "rename-to-cjs": "find lib -type f -name '*.js' -exec sh -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\;"
47
+ "build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json"
34
48
  },
35
- "gitHead": "3406b92ec7a43ce3aea9e2f77e9fc81a5a713fdd"
49
+ "gitHead": "4a3dfa3b7c78e8460c531bcdd82802c7fcf3eee3"
36
50
  }
@@ -1,5 +1,5 @@
1
- import type { WsgError, WsgEvent, WsgMeta } from "../types.js";
2
- import Utils from "../utils.js";
1
+ import type { WsgError, WsgEvent, WsgMeta } from "../types";
2
+ import Utils from "../utils";
3
3
 
4
4
  class ConnectionException extends Error {
5
5
  public wsgEvent: WsgEvent;
package/src/index.ts CHANGED
@@ -1,19 +1,19 @@
1
- import type RingCentral from "@rc-ex/core";
1
+ import type RingCentral from "@rc-ex/core/index";
2
2
  import type {
3
3
  RestMethod,
4
4
  RestRequestConfig,
5
5
  RestResponse,
6
- } from "@rc-ex/core/lib/esm/types.js";
7
- import SdkExtension from "@rc-ex/core/lib/esm/SdkExtension.js";
6
+ } from "@rc-ex/core/types";
7
+ import SdkExtension from "@rc-ex/core/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/lib/esm/RestException.js";
14
- import type SubscriptionInfo from "@rc-ex/core/lib/esm/definitions/SubscriptionInfo.js";
13
+ import RestException from "@rc-ex/core/RestException";
14
+ import type SubscriptionInfo from "@rc-ex/core/definitions/SubscriptionInfo";
15
15
 
16
- import { request } from "./rest.js";
16
+ import { request } from "./rest";
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.js";
25
- import Subscription from "./subscription.js";
26
- import ConnectionException from "./exceptions/ConnectionException.js";
27
- import Utils from "./utils.js";
24
+ } from "./types";
25
+ import Subscription from "./subscription";
26
+ import ConnectionException from "./exceptions/ConnectionException";
27
+ import Utils from "./utils";
28
28
 
29
29
  const CONNECTING = 0;
30
30
  const OPEN = 1;
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/esm/types.js";
6
- import RestException from "@rc-ex/core/lib/esm/RestException.js";
5
+ } from "@rc-ex/core/types";
6
+ import RestException from "@rc-ex/core/RestException";
7
7
  import hyperid from "hyperid";
8
8
  import { getReasonPhrase } from "http-status-codes";
9
9
 
10
- import Utils from "./utils.js";
11
- import type { WebSocketExtensionInterface } from "./types.js";
10
+ import Utils from "./utils";
11
+ import type { WebSocketExtensionInterface } from "./types";
12
12
 
13
13
  const version = "0.16";
14
14
 
@@ -1,14 +1,10 @@
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";
1
+ import type CreateSubscriptionRequest from "@rc-ex/core/definitions/CreateSubscriptionRequest";
2
+ import type SubscriptionInfo from "@rc-ex/core/definitions/SubscriptionInfo";
3
+ import type { RestResponse } from "@rc-ex/core/types";
4
4
  import type { MessageEvent } from "ws";
5
5
 
6
- import type {
7
- WebSocketExtensionInterface,
8
- WsgEvent,
9
- WsgMeta,
10
- } from "./types.js";
11
- import Utils from "./utils.js";
6
+ import type { WebSocketExtensionInterface, WsgEvent, WsgMeta } from "./types";
7
+ import Utils from "./utils";
12
8
 
13
9
  class Subscription {
14
10
  public subscriptionInfo?: SubscriptionInfo;
package/src/types.ts CHANGED
@@ -1,9 +1,9 @@
1
- import type RingCentral from "@rc-ex/core";
1
+ import type RingCentral from "@rc-ex/core/index";
2
2
  import type {
3
3
  RestMethod,
4
4
  RestRequestConfig,
5
5
  RestResponse,
6
- } from "@rc-ex/core/lib/esm/types.js";
6
+ } from "@rc-ex/core/types";
7
7
  import type WS from "isomorphic-ws";
8
8
 
9
9
  export interface WsToken {
package/src/utils.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { MessageEvent } from "isomorphic-ws";
2
2
  import type WS from "isomorphic-ws";
3
3
 
4
- import type { WsgEvent, WsgMeta } from "./types.js";
5
- import ClosedException from "./exceptions/ClosedException.js";
6
- import TimeoutException from "./exceptions/TimeoutException.js";
4
+ import type { WsgEvent, WsgMeta } from "./types";
5
+ import ClosedException from "./exceptions/ClosedException";
6
+ import TimeoutException from "./exceptions/TimeoutException";
7
7
 
8
8
  class Utils {
9
9
  public static splitWsgData(wsgData: string): [WsgMeta, any] {
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "esModuleInterop": true,
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./dist/cjs",
7
+ "skipLibCheck": true,
8
+ "sourceMap": true,
9
+ "paths": {
10
+ "@rc-ex/core/*": [
11
+ "../../core/dist/cjs/*"
12
+ ],
13
+ "isomorphic-ws": [
14
+ "../../../node_modules/isomorphic-ws/node.js"
15
+ ],
16
+ "wait-for-async": [
17
+ "../../../node_modules/wait-for-async/dist/cjs/index.js"
18
+ ],
19
+ "hyperid": [
20
+ "../../../node_modules/hyperid/hyperid.js"
21
+ ],
22
+ "http-status-codes": [
23
+ "../../../node_modules/http-status-codes/build/cjs/index.js"
24
+ ]
25
+ }
26
+ },
27
+ "include": ["src/**/*.ts"]
28
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "esModuleInterop": true,
4
+ "module": "esnext",
5
+ "declaration": true,
6
+ "outDir": "./dist/esm",
7
+ "skipLibCheck": true,
8
+ "sourceMap": true,
9
+ "paths": {
10
+ "@rc-ex/core/*": [
11
+ "../../core/dist/esm/*"
12
+ ],
13
+ "isomorphic-ws": [
14
+ "../../../node_modules/isomorphic-ws/node.js"
15
+ ],
16
+ "wait-for-async": [
17
+ "../../../node_modules/wait-for-async/dist/esm/index.js"
18
+ ],
19
+ "hyperid": [
20
+ "../../../node_modules/hyperid/hyperid.js"
21
+ ],
22
+ "http-status-codes": [
23
+ "../../../node_modules/http-status-codes/build/es/index.js"
24
+ ]
25
+ }
26
+ },
27
+ "include": ["src/**/*.ts"]
28
+ }
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class ClosedException extends Error {
4
- constructor(message) {
5
- super(message ?? "WebSocket has been closed");
6
- }
7
- }
8
- exports.default = ClosedException;
9
- //# sourceMappingURL=ClosedException.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ClosedException.js","sourceRoot":"","sources":["../../../src/exceptions/ClosedException.ts"],"names":[],"mappings":";;AAAA,MAAM,eAAgB,SAAQ,KAAK;IACjC,YAAmB,OAAgB;QACjC,KAAK,CAAC,OAAO,IAAI,2BAA2B,CAAC,CAAC;IAChD,CAAC;CACF;AAED,kBAAe,eAAe,CAAC"}
@@ -1,18 +0,0 @@
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_js_1 = __importDefault(require("../utils.js"));
7
- class ConnectionException extends Error {
8
- wsgEvent;
9
- wsgError;
10
- constructor(wsgEvent) {
11
- const [, wsgError] = utils_js_1.default.splitWsgData(wsgEvent.data);
12
- super(JSON.stringify(wsgError, null, 2));
13
- this.wsgEvent = wsgEvent;
14
- this.wsgError = wsgError;
15
- }
16
- }
17
- exports.default = ConnectionException;
18
- //# sourceMappingURL=ConnectionException.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ConnectionException.js","sourceRoot":"","sources":["../../../src/exceptions/ConnectionException.ts"],"names":[],"mappings":";;;;;AACA,2DAAgC;AAEhC,MAAM,mBAAoB,SAAQ,KAAK;IAC9B,QAAQ,CAAW;IAEnB,QAAQ,CAAW;IAE1B,YAAmB,QAAkB;QACnC,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAwB,kBAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC5E,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QACzC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAED,kBAAe,mBAAmB,CAAC"}
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class TimeoutException extends Error {
4
- constructor(message) {
5
- super(message ?? "Failed to receive expected WebSocket message in time.");
6
- }
7
- }
8
- exports.default = TimeoutException;
9
- //# sourceMappingURL=TimeoutException.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"TimeoutException.js","sourceRoot":"","sources":["../../../src/exceptions/TimeoutException.ts"],"names":[],"mappings":";;AAAA,MAAM,gBAAiB,SAAQ,KAAK;IAClC,YAAmB,OAAgB;QACjC,KAAK,CAAC,OAAO,IAAI,uDAAuD,CAAC,CAAC;IAC5E,CAAC;CACF;AAED,kBAAe,gBAAgB,CAAC"}