@sprucelabs/mercury-client 42.0.9 → 42.0.10
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/build/.spruce/errors/errors.types.d.ts +50 -50
- package/build/.spruce/errors/mercuryClient/connectionFailed.schema.js +5 -5
- package/build/.spruce/errors/mercuryClient/invalidEventSignature.schema.js +5 -5
- package/build/.spruce/errors/mercuryClient/invalidPayload.schema.js +3 -3
- package/build/.spruce/errors/mercuryClient/invalidProtocol.schema.js +3 -3
- package/build/.spruce/errors/mercuryClient/missingTestCacheDir.schema.js +1 -1
- package/build/.spruce/errors/mercuryClient/mustCreateEvent.schema.js +3 -3
- package/build/.spruce/errors/mercuryClient/mustHandleLocally.schema.js +3 -3
- package/build/.spruce/errors/mercuryClient/notConnected.schema.js +5 -5
- package/build/.spruce/errors/mercuryClient/timeout.schema.js +9 -9
- package/build/.spruce/errors/mercuryClient/unauthorizedAccess.schema.js +9 -9
- package/build/.spruce/errors/mercuryClient/unauthorizedTarget.schema.js +11 -11
- package/build/.spruce/errors/mercuryClient/unexpectedPayload.schema.js +3 -3
- package/build/.spruce/errors/mercuryClient/unknownError.schema.js +1 -1
- package/build/.spruce/errors/options.types.d.ts +2 -2
- package/build/.spruce/schemas/fields/fields.types.d.ts +1 -1
- package/build/clients/MercuryClientFactory.js +10 -11
- package/build/clients/MercurySocketIoClient.js +65 -63
- package/build/clients/MercuryTestClient.js +29 -24
- package/build/clients/MutableContractClient.js +5 -4
- package/build/errors/SpruceError.js +3 -2
- package/build/esm/.spruce/errors/errors.types.d.ts +50 -50
- package/build/esm/.spruce/errors/options.types.d.ts +2 -2
- package/build/tests/AbstractClientTest.js +5 -4
- package/build/tests/constants.js +10 -9
- package/package.json +14 -16
|
@@ -9,15 +9,10 @@ const constants_1 = require("../constants");
|
|
|
9
9
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
10
10
|
const MutableContractClient_1 = __importDefault(require("./MutableContractClient"));
|
|
11
11
|
class MercuryClientFactory {
|
|
12
|
-
static isTestMode = false;
|
|
13
|
-
static defaultContract;
|
|
14
|
-
static timeoutMs = 30000;
|
|
15
|
-
static totalClients = 0;
|
|
16
|
-
static clients = [];
|
|
17
|
-
static ClientClass;
|
|
18
12
|
static async Client(connectionOptions) {
|
|
13
|
+
var _a;
|
|
19
14
|
const { host: hostOption, contracts, reconnectDelayMs, allowSelfSignedCrt, emitTimeoutMs = this.timeoutMs, shouldReconnect, maxEmitRetries, connectionRetries, } = connectionOptions || {};
|
|
20
|
-
const host = hostOption
|
|
15
|
+
const host = hostOption !== null && hostOption !== void 0 ? hostOption : constants_1.DEFAULT_HOST;
|
|
21
16
|
if (host.substr(0, 4) !== 'http') {
|
|
22
17
|
throw new SpruceError_1.default({ code: 'INVALID_PROTOCOL', uri: host });
|
|
23
18
|
}
|
|
@@ -30,15 +25,15 @@ class MercuryClientFactory {
|
|
|
30
25
|
const eventContract = !contracts && this.defaultContract
|
|
31
26
|
? this.defaultContract
|
|
32
27
|
: //@ts-ignore
|
|
33
|
-
spruce_event_utils_1.eventContractUtil.unifyContracts(contracts
|
|
28
|
+
spruce_event_utils_1.eventContractUtil.unifyContracts(contracts !== null && contracts !== void 0 ? contracts : []);
|
|
34
29
|
let Client = MutableContractClient_1.default;
|
|
35
30
|
if (this.isTestMode) {
|
|
36
31
|
//TODO, make this something fitxures sets to make the test client available
|
|
37
32
|
Client = require('../clients/MercuryTestClient').default;
|
|
38
33
|
}
|
|
39
|
-
const client = new (MercuryClientFactory.ClientClass
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
const client = new ((_a = MercuryClientFactory.ClientClass) !== null && _a !== void 0 ? _a :
|
|
35
|
+
//@ts-ignore
|
|
36
|
+
Client)({
|
|
42
37
|
host,
|
|
43
38
|
reconnection: false,
|
|
44
39
|
reconnectDelayMs,
|
|
@@ -88,4 +83,8 @@ class MercuryClientFactory {
|
|
|
88
83
|
return this.clients;
|
|
89
84
|
}
|
|
90
85
|
}
|
|
86
|
+
MercuryClientFactory.isTestMode = false;
|
|
87
|
+
MercuryClientFactory.timeoutMs = 30000;
|
|
88
|
+
MercuryClientFactory.totalClients = 0;
|
|
89
|
+
MercuryClientFactory.clients = [];
|
|
91
90
|
exports.default = MercuryClientFactory;
|
|
@@ -11,66 +11,52 @@ const socket_io_client_1 = require("socket.io-client");
|
|
|
11
11
|
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
12
12
|
const socketIoEventUtil_utility_1 = __importDefault(require("../utilities/socketIoEventUtil.utility"));
|
|
13
13
|
class MercurySocketIoClient {
|
|
14
|
-
_eventContract;
|
|
15
14
|
get eventContract() {
|
|
16
15
|
return this._eventContract;
|
|
17
16
|
}
|
|
18
17
|
set eventContract(contract) {
|
|
19
18
|
this._eventContract = contract;
|
|
20
19
|
}
|
|
21
|
-
static io = socket_io_client_1.io;
|
|
22
|
-
host;
|
|
23
|
-
ioOptions;
|
|
24
|
-
socket;
|
|
25
|
-
proxyToken = null;
|
|
26
|
-
emitTimeoutMs;
|
|
27
|
-
reconnectDelayMs;
|
|
28
|
-
isReAuthing = false;
|
|
29
|
-
reconnectPromise = null;
|
|
30
|
-
lastAuthOptions;
|
|
31
|
-
shouldReconnect;
|
|
32
|
-
connectionRetriesRemaining = 5;
|
|
33
|
-
connectionRetries;
|
|
34
|
-
registeredListeners = [];
|
|
35
|
-
allowNextEventToBeAuthenticate = false;
|
|
36
|
-
auth;
|
|
37
|
-
shouldAutoRegisterListeners = true;
|
|
38
|
-
isManuallyDisconnected = false;
|
|
39
|
-
isReconnecting = false;
|
|
40
|
-
id;
|
|
41
|
-
skipWaitIfReconnecting = false;
|
|
42
|
-
maxEmitRetries;
|
|
43
|
-
authRawResults;
|
|
44
|
-
authPromise;
|
|
45
|
-
shouldRegisterProxyOnReconnect = false;
|
|
46
|
-
reconnectKey;
|
|
47
20
|
constructor(options) {
|
|
21
|
+
this.proxyToken = null;
|
|
22
|
+
this.isReAuthing = false;
|
|
23
|
+
this.reconnectPromise = null;
|
|
24
|
+
this.connectionRetriesRemaining = 5;
|
|
25
|
+
this.registeredListeners = [];
|
|
26
|
+
this.allowNextEventToBeAuthenticate = false;
|
|
27
|
+
this.shouldAutoRegisterListeners = true;
|
|
28
|
+
this.isManuallyDisconnected = false;
|
|
29
|
+
this.isReconnecting = false;
|
|
30
|
+
this.skipWaitIfReconnecting = false;
|
|
31
|
+
this.shouldRegisterProxyOnReconnect = false;
|
|
48
32
|
const { host, eventContract, emitTimeoutMs, reconnectDelayMs, shouldReconnect, maxEmitRetries = 5, connectionRetries, ...ioOptions } = options;
|
|
49
33
|
this.host = host;
|
|
50
34
|
this.ioOptions = { ...ioOptions, withCredentials: false };
|
|
51
35
|
this.eventContract = eventContract;
|
|
52
|
-
this.emitTimeoutMs = emitTimeoutMs
|
|
53
|
-
this.reconnectDelayMs = reconnectDelayMs
|
|
54
|
-
this.shouldReconnect = shouldReconnect
|
|
36
|
+
this.emitTimeoutMs = emitTimeoutMs !== null && emitTimeoutMs !== void 0 ? emitTimeoutMs : 30000;
|
|
37
|
+
this.reconnectDelayMs = reconnectDelayMs !== null && reconnectDelayMs !== void 0 ? reconnectDelayMs : 5000;
|
|
38
|
+
this.shouldReconnect = shouldReconnect !== null && shouldReconnect !== void 0 ? shouldReconnect : true;
|
|
55
39
|
this.id = new Date().getTime().toString();
|
|
56
40
|
this.maxEmitRetries = maxEmitRetries;
|
|
57
|
-
this.connectionRetriesRemaining = connectionRetries
|
|
58
|
-
this.connectionRetries = connectionRetries
|
|
41
|
+
this.connectionRetriesRemaining = connectionRetries !== null && connectionRetries !== void 0 ? connectionRetries : 5;
|
|
42
|
+
this.connectionRetries = connectionRetries !== null && connectionRetries !== void 0 ? connectionRetries : 5;
|
|
59
43
|
}
|
|
60
44
|
async connect() {
|
|
61
45
|
this.socket = MercurySocketIoClient.io(this.host, this.ioOptions);
|
|
62
46
|
this.emitStatusChange('connecting');
|
|
63
47
|
await new Promise((resolve, reject) => {
|
|
64
|
-
|
|
48
|
+
var _a, _b;
|
|
49
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.on('connect', () => {
|
|
50
|
+
var _a, _b;
|
|
65
51
|
this.connectionRetriesRemaining = this.connectionRetries;
|
|
66
52
|
//@ts-ignore
|
|
67
|
-
this.socket
|
|
53
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.removeAllListeners();
|
|
68
54
|
this.log(`Connection established!`);
|
|
69
55
|
if (!this.isReconnecting) {
|
|
70
56
|
this.emitStatusChange('connected');
|
|
71
57
|
}
|
|
72
58
|
if (this.shouldReconnect) {
|
|
73
|
-
this.socket
|
|
59
|
+
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.once('disconnect', async (opts) => {
|
|
74
60
|
this.log('Mercury disconnected, reason:', opts);
|
|
75
61
|
await this.attemptReconnectAfterDelay();
|
|
76
62
|
});
|
|
@@ -78,7 +64,7 @@ class MercurySocketIoClient {
|
|
|
78
64
|
this.attachConnectError();
|
|
79
65
|
resolve(undefined);
|
|
80
66
|
});
|
|
81
|
-
this.socket
|
|
67
|
+
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.on('timeout', () => {
|
|
82
68
|
reject(new SpruceError_1.default({
|
|
83
69
|
code: 'TIMEOUT',
|
|
84
70
|
eventName: 'connect',
|
|
@@ -98,24 +84,26 @@ class MercurySocketIoClient {
|
|
|
98
84
|
});
|
|
99
85
|
}
|
|
100
86
|
attachConnectError(reject, resolve) {
|
|
101
|
-
|
|
87
|
+
var _a;
|
|
88
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.on('connect_error', async (err) => {
|
|
89
|
+
var _a;
|
|
102
90
|
const error = this.mapSocketErrorToSpruceError(err);
|
|
103
91
|
//@ts-ignore
|
|
104
|
-
this.socket
|
|
92
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.removeAllListeners();
|
|
105
93
|
this.log('Failed to connect to Mercury', error.message);
|
|
106
94
|
this.log('Connection retries left', `${this.connectionRetriesRemaining}`);
|
|
107
95
|
if (this.connectionRetriesRemaining === 0) {
|
|
108
|
-
reject
|
|
96
|
+
reject === null || reject === void 0 ? void 0 : reject(error);
|
|
109
97
|
return;
|
|
110
98
|
}
|
|
111
99
|
try {
|
|
112
100
|
this.isReconnecting = false;
|
|
113
101
|
await this.attemptReconnectAfterDelay();
|
|
114
|
-
resolve
|
|
102
|
+
resolve === null || resolve === void 0 ? void 0 : resolve();
|
|
115
103
|
}
|
|
116
104
|
catch (err) {
|
|
117
105
|
//@ts-ignore
|
|
118
|
-
reject
|
|
106
|
+
reject === null || reject === void 0 ? void 0 : reject(err);
|
|
119
107
|
}
|
|
120
108
|
});
|
|
121
109
|
}
|
|
@@ -143,6 +131,7 @@ class MercurySocketIoClient {
|
|
|
143
131
|
return this.reconnectPromise;
|
|
144
132
|
}
|
|
145
133
|
async reconnect(resolve, reject, retriesLeft) {
|
|
134
|
+
var _a;
|
|
146
135
|
try {
|
|
147
136
|
this.connectionRetriesRemaining--;
|
|
148
137
|
const key = new Date().getTime();
|
|
@@ -180,7 +169,7 @@ class MercurySocketIoClient {
|
|
|
180
169
|
}
|
|
181
170
|
catch (err) {
|
|
182
171
|
;
|
|
183
|
-
(console.error
|
|
172
|
+
((_a = console.error) !== null && _a !== void 0 ? _a : console.log)(err.message);
|
|
184
173
|
this.isReconnecting = false;
|
|
185
174
|
this.skipWaitIfReconnecting = false;
|
|
186
175
|
retriesLeft = retriesLeft - 1;
|
|
@@ -210,7 +199,8 @@ class MercurySocketIoClient {
|
|
|
210
199
|
await all;
|
|
211
200
|
}
|
|
212
201
|
mapSocketErrorToSpruceError(err) {
|
|
213
|
-
|
|
202
|
+
var _a;
|
|
203
|
+
const originalError = new Error((_a = err.message) !== null && _a !== void 0 ? _a : err);
|
|
214
204
|
if (err.stack) {
|
|
215
205
|
originalError.stack = err.stack;
|
|
216
206
|
}
|
|
@@ -243,10 +233,10 @@ class MercurySocketIoClient {
|
|
|
243
233
|
if (isLocalEvent) {
|
|
244
234
|
const listeners = this.registeredListeners.filter((r) => r[0] === eventName);
|
|
245
235
|
for (const listener of listeners) {
|
|
246
|
-
const cb = listener
|
|
247
|
-
cb
|
|
236
|
+
const cb = listener === null || listener === void 0 ? void 0 : listener[1];
|
|
237
|
+
cb === null || cb === void 0 ? void 0 : cb({
|
|
248
238
|
//@ts-ignore
|
|
249
|
-
payload: targetAndPayload
|
|
239
|
+
payload: targetAndPayload === null || targetAndPayload === void 0 ? void 0 : targetAndPayload.payload,
|
|
250
240
|
});
|
|
251
241
|
}
|
|
252
242
|
return {
|
|
@@ -261,12 +251,13 @@ class MercurySocketIoClient {
|
|
|
261
251
|
async emitAndFlattenResponses(eventName, payload, cb) {
|
|
262
252
|
const results = await this.emit(eventName, payload, cb);
|
|
263
253
|
const { payloads, errors } = spruce_event_utils_1.eventResponseUtil.getAllResponsePayloadsAndErrors(results, SpruceError_1.default);
|
|
264
|
-
if (errors
|
|
254
|
+
if (errors === null || errors === void 0 ? void 0 : errors[0]) {
|
|
265
255
|
throw errors[0];
|
|
266
256
|
}
|
|
267
257
|
return payloads;
|
|
268
258
|
}
|
|
269
259
|
async _emit(retriesRemaining, eventName, payload, cb) {
|
|
260
|
+
var _a;
|
|
270
261
|
if (!this.skipWaitIfReconnecting) {
|
|
271
262
|
await this.waitIfReconnecting();
|
|
272
263
|
}
|
|
@@ -303,7 +294,7 @@ class MercurySocketIoClient {
|
|
|
303
294
|
}
|
|
304
295
|
};
|
|
305
296
|
if (cb) {
|
|
306
|
-
this.socket
|
|
297
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.on(responseEventName, singleResponseHandler);
|
|
307
298
|
}
|
|
308
299
|
const args = [];
|
|
309
300
|
if (payload || this.proxyToken) {
|
|
@@ -320,9 +311,11 @@ class MercurySocketIoClient {
|
|
|
320
311
|
args.push(p);
|
|
321
312
|
}
|
|
322
313
|
const results = await new Promise((resolve, reject) => {
|
|
314
|
+
var _a;
|
|
323
315
|
try {
|
|
324
316
|
const emitTimeout = setTimeout(async () => {
|
|
325
|
-
|
|
317
|
+
var _a;
|
|
318
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.off(responseEventName, singleResponseHandler);
|
|
326
319
|
if (retriesRemaining == 0) {
|
|
327
320
|
const err = new SpruceError_1.default({
|
|
328
321
|
code: 'TIMEOUT',
|
|
@@ -352,13 +345,14 @@ class MercurySocketIoClient {
|
|
|
352
345
|
}
|
|
353
346
|
}, this.emitTimeoutMs);
|
|
354
347
|
args.push((results) => {
|
|
348
|
+
var _a;
|
|
355
349
|
clearTimeout(emitTimeout);
|
|
356
350
|
this.handleConfirmPinResponse(eventName, results);
|
|
357
|
-
this.socket
|
|
351
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.off(responseEventName, singleResponseHandler);
|
|
358
352
|
resolve(results);
|
|
359
353
|
});
|
|
360
354
|
const ioName = socketIoEventUtil_utility_1.default.toSocketName(eventName);
|
|
361
|
-
this.socket
|
|
355
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit(ioName, ...args);
|
|
362
356
|
}
|
|
363
357
|
catch (err) {
|
|
364
358
|
reject(err);
|
|
@@ -371,7 +365,7 @@ class MercurySocketIoClient {
|
|
|
371
365
|
const signature = this.getEventSignatureByName(eventName);
|
|
372
366
|
if (signature.emitPayloadSchema) {
|
|
373
367
|
try {
|
|
374
|
-
(0, schema_1.validateSchemaValues)(signature.emitPayloadSchema, payload
|
|
368
|
+
(0, schema_1.validateSchemaValues)(signature.emitPayloadSchema, payload !== null && payload !== void 0 ? payload : {});
|
|
375
369
|
}
|
|
376
370
|
catch (err) {
|
|
377
371
|
throw new SpruceError_1.default({
|
|
@@ -389,8 +383,9 @@ class MercurySocketIoClient {
|
|
|
389
383
|
}
|
|
390
384
|
}
|
|
391
385
|
handleConfirmPinResponse(eventName, results) {
|
|
392
|
-
|
|
393
|
-
|
|
386
|
+
var _a, _b;
|
|
387
|
+
const payload = (_b = (_a = results === null || results === void 0 ? void 0 : results.responses) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.payload;
|
|
388
|
+
if (eventName.search('confirm-pin') === 0 && (payload === null || payload === void 0 ? void 0 : payload.person)) {
|
|
394
389
|
this.lastAuthOptions = { token: payload.token };
|
|
395
390
|
this.auth = {
|
|
396
391
|
person: payload.person,
|
|
@@ -407,6 +402,7 @@ class MercurySocketIoClient {
|
|
|
407
402
|
this.shouldAutoRegisterListeners = should;
|
|
408
403
|
}
|
|
409
404
|
async on(eventName, cb) {
|
|
405
|
+
var _a, _b, _c;
|
|
410
406
|
this.registeredListeners.push([eventName, cb]);
|
|
411
407
|
const isLocalEvent = this.isEventLocal(eventName);
|
|
412
408
|
if (isLocalEvent) {
|
|
@@ -418,11 +414,11 @@ class MercurySocketIoClient {
|
|
|
418
414
|
payload: { events: [{ eventName }] },
|
|
419
415
|
});
|
|
420
416
|
if (results.totalErrors > 0) {
|
|
421
|
-
const options = results.responses[0].errors
|
|
417
|
+
const options = (_b = (_a = results.responses[0].errors) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : 'UNKNOWN_ERROR';
|
|
422
418
|
throw error_1.default.parse(options, SpruceError_1.default);
|
|
423
419
|
}
|
|
424
420
|
}
|
|
425
|
-
this.socket
|
|
421
|
+
(_c = this.socket) === null || _c === void 0 ? void 0 : _c.on(eventName,
|
|
426
422
|
//@ts-ignore
|
|
427
423
|
async (targetAndPayload, ioCallback) => {
|
|
428
424
|
if (cb) {
|
|
@@ -455,11 +451,12 @@ class MercurySocketIoClient {
|
|
|
455
451
|
}
|
|
456
452
|
async off(eventName) {
|
|
457
453
|
return new Promise((resolve, reject) => {
|
|
454
|
+
var _a;
|
|
458
455
|
if (!this.socket || !this.auth || this.isEventLocal(eventName)) {
|
|
459
456
|
resolve(0);
|
|
460
457
|
return;
|
|
461
458
|
}
|
|
462
|
-
this.socket
|
|
459
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit('unregister-listeners::v2020_12_25', {
|
|
463
460
|
payload: {
|
|
464
461
|
fullyQualifiedEventNames: [eventName],
|
|
465
462
|
},
|
|
@@ -478,27 +475,30 @@ class MercurySocketIoClient {
|
|
|
478
475
|
return this.id;
|
|
479
476
|
}
|
|
480
477
|
async disconnect() {
|
|
478
|
+
var _a;
|
|
481
479
|
this.isManuallyDisconnected = true;
|
|
482
480
|
if (this.isSocketConnected()) {
|
|
483
481
|
//@ts-ignore
|
|
484
|
-
this.socket
|
|
482
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.removeAllListeners();
|
|
485
483
|
await new Promise((resolve) => {
|
|
486
|
-
|
|
484
|
+
var _a, _b;
|
|
485
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.once('disconnect', () => {
|
|
487
486
|
this.socket = undefined;
|
|
488
487
|
resolve(undefined);
|
|
489
488
|
});
|
|
490
|
-
this.socket
|
|
489
|
+
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.disconnect();
|
|
491
490
|
});
|
|
492
491
|
}
|
|
493
492
|
return;
|
|
494
493
|
}
|
|
495
494
|
async authenticate(options) {
|
|
495
|
+
var _a, _b;
|
|
496
496
|
const { skillId, apiKey, token } = options;
|
|
497
497
|
if (this.authPromise) {
|
|
498
498
|
await this.authPromise;
|
|
499
499
|
return {
|
|
500
|
-
skill: this.auth
|
|
501
|
-
person: this.auth
|
|
500
|
+
skill: (_a = this.auth) === null || _a === void 0 ? void 0 : _a.skill,
|
|
501
|
+
person: (_b = this.auth) === null || _b === void 0 ? void 0 : _b.person,
|
|
502
502
|
};
|
|
503
503
|
}
|
|
504
504
|
this.lastAuthOptions = options;
|
|
@@ -528,7 +528,8 @@ class MercurySocketIoClient {
|
|
|
528
528
|
return !this.isReAuthing && this.isSocketConnected();
|
|
529
529
|
}
|
|
530
530
|
isSocketConnected() {
|
|
531
|
-
|
|
531
|
+
var _a, _b;
|
|
532
|
+
return (_b = (_a = this.socket) === null || _a === void 0 ? void 0 : _a.connected) !== null && _b !== void 0 ? _b : false;
|
|
532
533
|
}
|
|
533
534
|
getProxyToken() {
|
|
534
535
|
return this.proxyToken;
|
|
@@ -548,5 +549,6 @@ class MercurySocketIoClient {
|
|
|
548
549
|
return false;
|
|
549
550
|
}
|
|
550
551
|
}
|
|
552
|
+
MercurySocketIoClient.io = socket_io_client_1.io;
|
|
551
553
|
exports.default = MercurySocketIoClient;
|
|
552
554
|
exports.authenticateFqen = 'authenticate::v2020_12_25';
|
|
@@ -49,15 +49,6 @@ class InternalEmitter extends mercury_event_emitter_1.AbstractEventEmitter {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
class MercuryTestClient extends MutableContractClient_1.default {
|
|
52
|
-
static emitter;
|
|
53
|
-
_isConnected = false;
|
|
54
|
-
isConnectedToApi = false;
|
|
55
|
-
connectPromise;
|
|
56
|
-
static shouldCheckPermissionsOnLocalEvents = false;
|
|
57
|
-
shouldHandleAuthenticateLocallyIfListenerSet = true;
|
|
58
|
-
static namespacesThatHaveToBeHandledLocally = [];
|
|
59
|
-
shouldWaitForDelayedConnectIfAuthing = true;
|
|
60
|
-
static shouldRequireLocalListeners = true;
|
|
61
52
|
get eventContract() {
|
|
62
53
|
return MercuryTestClient.emitter.getContract();
|
|
63
54
|
}
|
|
@@ -74,6 +65,10 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
74
65
|
constructor(options) {
|
|
75
66
|
const contract = options.eventContract;
|
|
76
67
|
super({ ...options, eventContract: contract });
|
|
68
|
+
this._isConnected = false;
|
|
69
|
+
this.isConnectedToApi = false;
|
|
70
|
+
this.shouldHandleAuthenticateLocallyIfListenerSet = true;
|
|
71
|
+
this.shouldWaitForDelayedConnectIfAuthing = true;
|
|
77
72
|
if (!MercuryTestClient.emitter) {
|
|
78
73
|
MercuryTestClient.getInternalEmitter(contract);
|
|
79
74
|
}
|
|
@@ -94,8 +89,9 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
94
89
|
return MercuryTestClient.emitter;
|
|
95
90
|
}
|
|
96
91
|
async off(eventName, cb) {
|
|
97
|
-
|
|
98
|
-
|
|
92
|
+
var _a, _b;
|
|
93
|
+
await ((_a = MercuryTestClient.emitter) === null || _a === void 0 ? void 0 : _a.off(eventName, cb));
|
|
94
|
+
if (((_b = MercuryTestClient.emitter) === null || _b === void 0 ? void 0 : _b.listenCount(eventName)) === 0) {
|
|
99
95
|
return super.off(eventName);
|
|
100
96
|
}
|
|
101
97
|
else {
|
|
@@ -111,14 +107,16 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
111
107
|
MercuryTestClient.emitter.mixinContract(contract);
|
|
112
108
|
}
|
|
113
109
|
doesHandleEvent(eventName) {
|
|
110
|
+
var _a;
|
|
114
111
|
return (super.doesHandleEvent(eventName) ||
|
|
115
|
-
MercuryTestClient.emitter
|
|
112
|
+
((_a = MercuryTestClient.emitter) === null || _a === void 0 ? void 0 : _a.doesHandleEvent(eventName)));
|
|
116
113
|
}
|
|
117
114
|
async on(...args) {
|
|
118
115
|
//@ts-ignore
|
|
119
116
|
return MercuryTestClient.emitter.on(...args);
|
|
120
117
|
}
|
|
121
118
|
async emit(...args) {
|
|
119
|
+
var _a, _b, _c, _d, _e;
|
|
122
120
|
const fqen = args[0];
|
|
123
121
|
try {
|
|
124
122
|
if (this.shouldHandleEventLocally(fqen)) {
|
|
@@ -136,16 +134,16 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
136
134
|
await this.connectIfNotConnected(fqen);
|
|
137
135
|
//@ts-ignore
|
|
138
136
|
const results = await super.emit(...args);
|
|
139
|
-
const firstError = results.responses
|
|
137
|
+
const firstError = (_c = (_b = (_a = results.responses) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.errors) === null || _c === void 0 ? void 0 : _c[0];
|
|
140
138
|
if (firstError &&
|
|
141
|
-
firstError.options
|
|
139
|
+
((_d = firstError.options) === null || _d === void 0 ? void 0 : _d.code) === 'INVALID_EVENT_NAME') {
|
|
142
140
|
firstError.message = `Event not found! Make sure you are booting your skill in your test with \`await this.bootSkill()\`. If you haven't, you'll need to create a listener with \`spruce create.listener\`.\n\nOriginal Error:\n\n${firstError.options.friendlyMessage}`;
|
|
143
141
|
}
|
|
144
142
|
return results;
|
|
145
143
|
}
|
|
146
144
|
}
|
|
147
145
|
catch (err) {
|
|
148
|
-
if (err.options
|
|
146
|
+
if (((_e = err.options) === null || _e === void 0 ? void 0 : _e.code) === 'INVALID_EVENT_NAME') {
|
|
149
147
|
err.message = `${err.message} Double check it's spelled correctly (types are passing) and that you've run \`spruce create.event\` to create the event.`;
|
|
150
148
|
}
|
|
151
149
|
throw err;
|
|
@@ -168,6 +166,7 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
168
166
|
return emitter.listenCount(fqen) > 0;
|
|
169
167
|
}
|
|
170
168
|
async handleEventLocally(args) {
|
|
169
|
+
var _a;
|
|
171
170
|
const emitter = MercuryTestClient.emitter;
|
|
172
171
|
const fqen = args[0];
|
|
173
172
|
const payload = args[1];
|
|
@@ -217,7 +216,7 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
217
216
|
code: 'UNAUTHORIZED_ACCESS',
|
|
218
217
|
fqen,
|
|
219
218
|
action: 'emit',
|
|
220
|
-
target: args[1]
|
|
219
|
+
target: (_a = args[1]) !== null && _a !== void 0 ? _a : {},
|
|
221
220
|
permissionContractId: sig.emitPermissionContract.id,
|
|
222
221
|
}),
|
|
223
222
|
],
|
|
@@ -231,7 +230,8 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
231
230
|
return (0, just_clone_1.default)(results);
|
|
232
231
|
}
|
|
233
232
|
assertValidEventSignature(sig, fqen) {
|
|
234
|
-
|
|
233
|
+
var _a, _b;
|
|
234
|
+
if (!sig.isGlobal && !((_b = (_a = sig.emitPayloadSchema) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.target)) {
|
|
235
235
|
throw new SpruceError_1.default({
|
|
236
236
|
code: 'INVALID_EVENT_SIGNATURE',
|
|
237
237
|
fqen,
|
|
@@ -240,15 +240,16 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
async optionallyCheckPermissions(args, permissionContractId, fqen) {
|
|
243
|
+
var _a;
|
|
243
244
|
if (!MercuryTestClient.shouldCheckPermissionsOnLocalEvents) {
|
|
244
245
|
return true;
|
|
245
246
|
}
|
|
246
|
-
let { target } = args[1]
|
|
247
|
+
let { target } = (_a = args[1]) !== null && _a !== void 0 ? _a : {};
|
|
247
248
|
const permTarget = {};
|
|
248
|
-
if (target
|
|
249
|
+
if (target === null || target === void 0 ? void 0 : target.organizationId) {
|
|
249
250
|
permTarget.organizationId = target.organizationId;
|
|
250
251
|
}
|
|
251
|
-
if (target
|
|
252
|
+
if (target === null || target === void 0 ? void 0 : target.locationId) {
|
|
252
253
|
throw new Error('checking permissions against a location is not supported. Add to mercury-workspace -> mercury-client');
|
|
253
254
|
}
|
|
254
255
|
const results = await this.emit('does-honor-permission-contract::v2020_12_25', {
|
|
@@ -265,13 +266,14 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
265
266
|
return doesHonor;
|
|
266
267
|
}
|
|
267
268
|
buildSource(args) {
|
|
269
|
+
var _a, _b, _c;
|
|
268
270
|
let source = {
|
|
269
|
-
...args[1]
|
|
271
|
+
...(_a = args[1]) === null || _a === void 0 ? void 0 : _a.source,
|
|
270
272
|
};
|
|
271
|
-
if (this.auth
|
|
273
|
+
if ((_b = this.auth) === null || _b === void 0 ? void 0 : _b.person) {
|
|
272
274
|
source.personId = this.auth.person.id;
|
|
273
275
|
}
|
|
274
|
-
if (this.auth
|
|
276
|
+
if ((_c = this.auth) === null || _c === void 0 ? void 0 : _c.skill) {
|
|
275
277
|
source.skillId = this.auth.skill.id;
|
|
276
278
|
}
|
|
277
279
|
if (args[0] !== 'authenticate::v2020_12_25' &&
|
|
@@ -343,10 +345,13 @@ class MercuryTestClient extends MutableContractClient_1.default {
|
|
|
343
345
|
return this.shouldRequireLocalListeners;
|
|
344
346
|
}
|
|
345
347
|
}
|
|
348
|
+
MercuryTestClient.shouldCheckPermissionsOnLocalEvents = false;
|
|
349
|
+
MercuryTestClient.namespacesThatHaveToBeHandledLocally = [];
|
|
350
|
+
MercuryTestClient.shouldRequireLocalListeners = true;
|
|
346
351
|
exports.default = MercuryTestClient;
|
|
347
352
|
function mixinConnectionEvents(contract) {
|
|
348
353
|
return spruce_event_utils_1.eventContractUtil.unifyContracts([
|
|
349
|
-
contract
|
|
354
|
+
contract !== null && contract !== void 0 ? contract : { eventSignatures: {} },
|
|
350
355
|
statusChangePayloadSchema_1.connectionStatusContract,
|
|
351
356
|
]);
|
|
352
357
|
}
|
|
@@ -6,10 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const spruce_event_utils_1 = require("@sprucelabs/spruce-event-utils");
|
|
7
7
|
const MercurySocketIoClient_1 = __importDefault(require("./MercurySocketIoClient"));
|
|
8
8
|
class MutableContractClient extends MercurySocketIoClient_1.default {
|
|
9
|
-
static inMemoryContract;
|
|
10
9
|
static mixinContract(contract) {
|
|
10
|
+
var _a;
|
|
11
11
|
this.inMemoryContract = spruce_event_utils_1.eventContractUtil.unifyContracts([
|
|
12
|
-
this.inMemoryContract
|
|
12
|
+
(_a = this.inMemoryContract) !== null && _a !== void 0 ? _a : { eventSignatures: {} },
|
|
13
13
|
contract,
|
|
14
14
|
]);
|
|
15
15
|
}
|
|
@@ -31,14 +31,15 @@ class MutableContractClient extends MercurySocketIoClient_1.default {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
getEventSignatureByName(eventName) {
|
|
34
|
+
var _a, _b;
|
|
34
35
|
try {
|
|
35
36
|
const sig = this.eventContract || MutableContractClient.inMemoryContract
|
|
36
|
-
? spruce_event_utils_1.eventContractUtil.getSignatureByName(this.eventContract
|
|
37
|
+
? spruce_event_utils_1.eventContractUtil.getSignatureByName((_a = this.eventContract) !== null && _a !== void 0 ? _a : { eventSignatures: {} }, eventName)
|
|
37
38
|
: {};
|
|
38
39
|
return sig;
|
|
39
40
|
}
|
|
40
41
|
catch (err) {
|
|
41
|
-
const inMemorySig = spruce_event_utils_1.eventContractUtil.getSignatureByName(MutableContractClient.inMemoryContract
|
|
42
|
+
const inMemorySig = spruce_event_utils_1.eventContractUtil.getSignatureByName((_b = MutableContractClient.inMemoryContract) !== null && _b !== void 0 ? _b : {
|
|
42
43
|
eventSignatures: {},
|
|
43
44
|
}, eventName);
|
|
44
45
|
return inMemorySig;
|
|
@@ -6,9 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const error_1 = __importDefault(require("@sprucelabs/error"));
|
|
7
7
|
class SpruceError extends error_1.default {
|
|
8
8
|
friendlyMessage() {
|
|
9
|
+
var _a;
|
|
9
10
|
const { options } = this;
|
|
10
11
|
let message;
|
|
11
|
-
switch (options
|
|
12
|
+
switch (options === null || options === void 0 ? void 0 : options.code) {
|
|
12
13
|
case 'INVALID_PROTOCOL':
|
|
13
14
|
message = `You must connect via the https protocol. The uri you used was ${options.uri}!`;
|
|
14
15
|
break;
|
|
@@ -64,7 +65,7 @@ class SpruceError extends error_1.default {
|
|
|
64
65
|
default:
|
|
65
66
|
message = super.friendlyMessage();
|
|
66
67
|
}
|
|
67
|
-
message = options.friendlyMessage
|
|
68
|
+
message = (_a = options.friendlyMessage) !== null && _a !== void 0 ? _a : message;
|
|
68
69
|
return message;
|
|
69
70
|
}
|
|
70
71
|
}
|