@rc-ex/ws 1.3.14 → 1.3.16
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/cjs/exceptions/ClosedException.js +4 -21
- package/dist/cjs/exceptions/ConnectionException.js +10 -27
- package/dist/cjs/exceptions/TimeoutException.js +4 -21
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +301 -482
- package/dist/cjs/rest.js +46 -96
- package/dist/cjs/subscription.js +69 -151
- package/dist/cjs/types.d.ts +1 -1
- package/dist/cjs/utils.js +33 -77
- package/dist/esm/exceptions/ClosedException.js +1 -3
- package/dist/esm/exceptions/ConnectionException.js +3 -8
- package/dist/esm/exceptions/TimeoutException.js +1 -3
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +26 -32
- package/dist/esm/rest.js +9 -15
- package/dist/esm/subscription.js +3 -8
- package/dist/esm/types.d.ts +1 -1
- package/dist/esm/types.js +1 -2
- package/dist/esm/utils.js +5 -10
- package/package.json +6 -6
package/dist/cjs/rest.js
CHANGED
|
@@ -1,103 +1,53 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
4
|
};
|
|
52
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
6
|
exports.request = request;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
function request(method, endpoint, content, queryParams, config) {
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
});
|
|
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);
|
|
39
|
+
}
|
|
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);
|
|
103
53
|
}
|
package/dist/cjs/subscription.js
CHANGED
|
@@ -1,166 +1,85 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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) {
|
|
47
15
|
this.wse = wse;
|
|
48
16
|
this.eventFilters = eventFilters;
|
|
49
|
-
this.eventListener =
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
.splitWsgData(event.data)
|
|
53
|
-
if (
|
|
54
|
-
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) {
|
|
55
23
|
callback(body);
|
|
56
24
|
}
|
|
57
25
|
};
|
|
58
26
|
this.setupWsEventListener();
|
|
59
27
|
}
|
|
60
|
-
|
|
28
|
+
setupWsEventListener() {
|
|
61
29
|
this.wse.ws.addEventListener("message", this.eventListener);
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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*/];
|
|
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`);
|
|
117
68
|
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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*/];
|
|
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");
|
|
159
74
|
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
throw e;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
this.remove();
|
|
81
|
+
}
|
|
82
|
+
remove() {
|
|
164
83
|
if (this.timeout) {
|
|
165
84
|
global.clearTimeout(this.timeout);
|
|
166
85
|
this.timeout = undefined;
|
|
@@ -171,7 +90,6 @@ var Subscription = /** @class */ (function () {
|
|
|
171
90
|
this.wse.ws.removeEventListener("message", this.eventListener);
|
|
172
91
|
}
|
|
173
92
|
this.wse.subscription = undefined;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
}());
|
|
93
|
+
}
|
|
94
|
+
}
|
|
177
95
|
exports.default = Subscription;
|
package/dist/cjs/types.d.ts
CHANGED
package/dist/cjs/utils.js
CHANGED
|
@@ -1,105 +1,62 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
Utils.splitWsgData = function (wsgData) {
|
|
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) {
|
|
48
10
|
if (wsgData.includes(",--Boundary")) {
|
|
49
|
-
|
|
11
|
+
const index = wsgData.indexOf(",--Boundary");
|
|
50
12
|
return [
|
|
51
13
|
JSON.parse(wsgData.substring(1, index)),
|
|
52
14
|
wsgData.substring(index + 1, wsgData.length - 1),
|
|
53
15
|
];
|
|
54
16
|
}
|
|
55
17
|
return JSON.parse(wsgData);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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******"));
|
|
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
|
+
******`);
|
|
75
33
|
});
|
|
76
|
-
ws.addEventListener("open",
|
|
34
|
+
ws.addEventListener("open", (event) => {
|
|
77
35
|
console.debug("WebSocket open event:", event);
|
|
78
36
|
});
|
|
79
|
-
ws.addEventListener("error",
|
|
37
|
+
ws.addEventListener("error", (event) => {
|
|
80
38
|
console.debug("WebSocket error event:", event);
|
|
81
39
|
});
|
|
82
|
-
ws.addEventListener("close",
|
|
40
|
+
ws.addEventListener("close", (event) => {
|
|
83
41
|
console.debug("WebSocket close event:", event);
|
|
84
42
|
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
var checkHandle = setInterval(function () {
|
|
43
|
+
}
|
|
44
|
+
static waitForWebSocketMessage(ws, matchCondition, timeout = 60000) {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
const checkHandle = setInterval(() => {
|
|
90
47
|
if (ws.readyState === ws.CLOSED) {
|
|
91
48
|
clearInterval(checkHandle);
|
|
92
49
|
reject(new ClosedException_js_1.default());
|
|
93
50
|
}
|
|
94
51
|
}, 1000);
|
|
95
|
-
|
|
52
|
+
const timeoutHandle = setTimeout(() => {
|
|
96
53
|
ws.removeEventListener("message", handler);
|
|
97
54
|
clearInterval(checkHandle);
|
|
98
55
|
reject(new TimeoutException_js_1.default());
|
|
99
56
|
}, timeout);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
57
|
+
const handler = (mEvent) => {
|
|
58
|
+
const event = mEvent;
|
|
59
|
+
const [meta, body] = Utils.splitWsgData(event.data);
|
|
103
60
|
if (matchCondition(meta)) {
|
|
104
61
|
ws.removeEventListener("message", handler);
|
|
105
62
|
clearInterval(checkHandle);
|
|
@@ -109,7 +66,6 @@ var Utils = /** @class */ (function () {
|
|
|
109
66
|
};
|
|
110
67
|
ws.addEventListener("message", handler);
|
|
111
68
|
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
}());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
115
71
|
exports.default = Utils;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
class ClosedException extends Error {
|
|
4
2
|
constructor(message) {
|
|
5
3
|
super(message ?? "WebSocket has been closed");
|
|
6
4
|
}
|
|
7
5
|
}
|
|
8
|
-
|
|
6
|
+
export default ClosedException;
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const utils_js_1 = __importDefault(require("../utils.js"));
|
|
1
|
+
import Utils from "../utils.js";
|
|
7
2
|
class ConnectionException extends Error {
|
|
8
3
|
wsgEvent;
|
|
9
4
|
wsgError;
|
|
10
5
|
constructor(wsgEvent) {
|
|
11
|
-
const [, wsgError] =
|
|
6
|
+
const [, wsgError] = Utils.splitWsgData(wsgEvent.data);
|
|
12
7
|
super(JSON.stringify(wsgError, null, 2));
|
|
13
8
|
this.wsgEvent = wsgEvent;
|
|
14
9
|
this.wsgError = wsgError;
|
|
15
10
|
}
|
|
16
11
|
}
|
|
17
|
-
|
|
12
|
+
export default ConnectionException;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
class TimeoutException extends Error {
|
|
4
2
|
constructor(message) {
|
|
5
3
|
super(message ?? "Failed to receive expected WebSocket message in time.");
|
|
6
4
|
}
|
|
7
5
|
}
|
|
8
|
-
|
|
6
|
+
export default TimeoutException;
|
package/dist/esm/index.d.ts
CHANGED