@openfin/cloud-interop-core-api 0.0.1-alpha.5344cb7 → 0.0.1-alpha.53e67c3
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/bundle.d.ts +1 -1
- package/index.cjs +18 -11
- package/index.mjs +18 -11
- package/package.json +5 -8
package/bundle.d.ts
CHANGED
|
@@ -364,7 +364,7 @@ export declare type ConnectParameters = {
|
|
|
364
364
|
/**
|
|
365
365
|
* When JWT authentication is being used, this will be invoked just whenever a JWT token is required for a request
|
|
366
366
|
*/
|
|
367
|
-
jwtRequestCallback: () => string | object
|
|
367
|
+
jwtRequestCallback: () => string | object | Promise<string | object>;
|
|
368
368
|
/**
|
|
369
369
|
* The id of the service gateway JWT authentication definition to use
|
|
370
370
|
*
|
package/index.cjs
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var buffer = require('buffer');
|
|
4
|
+
require('fs');
|
|
5
|
+
require('path');
|
|
4
6
|
var mqtt = require('mqtt');
|
|
5
7
|
|
|
8
|
+
var d=n=>{let t=n.replaceAll("-","+").replaceAll("_","/");return t.padEnd(t.length+(4-t.length%4)%4,"=")};
|
|
9
|
+
|
|
6
10
|
class CloudInteropAPIError extends Error {
|
|
7
11
|
code;
|
|
8
12
|
constructor(message = 'An unexpected error has occurred', code = 'UNEXPECTED_ERROR', cause) {
|
|
@@ -49,11 +53,11 @@ class EventController {
|
|
|
49
53
|
const isErrorIntentResult = (result) => 'error' in result;
|
|
50
54
|
|
|
51
55
|
const APP_ID_DELIM = '::';
|
|
52
|
-
const getRequestHeaders = (connectionParameters) => {
|
|
56
|
+
const getRequestHeaders = async (connectionParameters) => {
|
|
53
57
|
const headers = {};
|
|
54
58
|
headers['Content-Type'] = 'application/json';
|
|
55
59
|
if (connectionParameters.authenticationType === 'jwt' && connectionParameters.jwtAuthenticationParameters) {
|
|
56
|
-
const tokenResult = connectionParameters.jwtAuthenticationParameters.jwtRequestCallback();
|
|
60
|
+
const tokenResult = await connectionParameters.jwtAuthenticationParameters.jwtRequestCallback();
|
|
57
61
|
if (!tokenResult) {
|
|
58
62
|
throw new Error('jwtRequestCallback must return a token');
|
|
59
63
|
}
|
|
@@ -158,7 +162,7 @@ class IntentController {
|
|
|
158
162
|
try {
|
|
159
163
|
const startResponse = await fetch(`${this.#url}/api/intents/${this.#sessionDetails.sessionId}`, {
|
|
160
164
|
method: 'POST',
|
|
161
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
165
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
162
166
|
body: JSON.stringify({ findOptions }),
|
|
163
167
|
});
|
|
164
168
|
if (!startResponse.ok) {
|
|
@@ -203,7 +207,7 @@ class IntentController {
|
|
|
203
207
|
}
|
|
204
208
|
await fetch(`${this.#url}/api/intents/${this.#sessionDetails.sessionId}/${this.#discovery.id}`, {
|
|
205
209
|
method: 'DELETE',
|
|
206
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
210
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
207
211
|
})
|
|
208
212
|
.then((deleteResponse) => {
|
|
209
213
|
if (!deleteResponse.ok) {
|
|
@@ -224,7 +228,7 @@ class IntentController {
|
|
|
224
228
|
}
|
|
225
229
|
const postResponse = await fetch(`${this.#url}/api/intents/${this.#sessionDetails.sessionId}/sessions/${targetSessionId}`, {
|
|
226
230
|
method: 'POST',
|
|
227
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
231
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
228
232
|
body: JSON.stringify({ raiseOptions }),
|
|
229
233
|
});
|
|
230
234
|
if (!postResponse.ok) {
|
|
@@ -238,7 +242,7 @@ class IntentController {
|
|
|
238
242
|
try {
|
|
239
243
|
const reportResponse = await fetch(`${this.#url}/api/intents/${this.#sessionDetails.sessionId}/${discoveryId}`, {
|
|
240
244
|
method: 'POST',
|
|
241
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
245
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
242
246
|
body: JSON.stringify({ intents }),
|
|
243
247
|
});
|
|
244
248
|
if (reportResponse.ok) {
|
|
@@ -262,7 +266,7 @@ class IntentController {
|
|
|
262
266
|
const { sessionId } = getSourceFromSession(this.#sessionDetails);
|
|
263
267
|
const resultResponse = await fetch(`${this.#url}/api/intents/${initiatingSessionId}/result/${sessionId}`, {
|
|
264
268
|
method: 'POST',
|
|
265
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
269
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
266
270
|
body: JSON.stringify({ result }),
|
|
267
271
|
});
|
|
268
272
|
if (!resultResponse.ok) {
|
|
@@ -346,6 +350,7 @@ class CloudInteropAPI {
|
|
|
346
350
|
#reconnectRetryLimit = 30;
|
|
347
351
|
#keepAliveIntervalSeconds = 30;
|
|
348
352
|
#logger = (level, message) => {
|
|
353
|
+
// eslint-disable-next-line security/detect-object-injection -- level is restricted to the LogLevel union
|
|
349
354
|
console[level](message);
|
|
350
355
|
};
|
|
351
356
|
#reconnectRetries = 0;
|
|
@@ -381,7 +386,7 @@ class CloudInteropAPI {
|
|
|
381
386
|
const { sourceId, platformId } = this.#connectionParams;
|
|
382
387
|
const createSessionResponse = await fetch(`${this.#cloudInteropSettings.url}/api/sessions`, {
|
|
383
388
|
method: 'POST',
|
|
384
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
389
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
385
390
|
body: JSON.stringify({ sourceId: sourceId.trim(), platformId }),
|
|
386
391
|
});
|
|
387
392
|
if (!createSessionResponse.ok) {
|
|
@@ -419,6 +424,7 @@ class CloudInteropAPI {
|
|
|
419
424
|
// TODO: Dynamic intent discovery
|
|
420
425
|
// search for any ongoing discoveries in DB and fire report-intents on self
|
|
421
426
|
this.#logger('log', `Cloud Interop successfully connected to ${this.#cloudInteropSettings.url}`);
|
|
427
|
+
// eslint-disable-next-line security-node/detect-unhandled-event-errors -- this callback handles MQTT error events
|
|
422
428
|
this.#mqttClient.on('error', async (error) => {
|
|
423
429
|
// We will receive errors for each failed reconnection attempt
|
|
424
430
|
// We don't want to disconnect on these else we will never reconnect
|
|
@@ -511,7 +517,7 @@ class CloudInteropAPI {
|
|
|
511
517
|
};
|
|
512
518
|
const postResponse = await fetch(`${this.#cloudInteropSettings.url}/api/context-groups/${this.#sessionDetails.sessionId}/${contextGroup}`, {
|
|
513
519
|
method: 'POST',
|
|
514
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
520
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
515
521
|
body: JSON.stringify(payload),
|
|
516
522
|
});
|
|
517
523
|
if (!postResponse.ok) {
|
|
@@ -571,7 +577,7 @@ class CloudInteropAPI {
|
|
|
571
577
|
}
|
|
572
578
|
const disconnectResponse = await fetch(`${this.#cloudInteropSettings.url}/api/sessions/${this.#sessionDetails.sessionId}`, {
|
|
573
579
|
method: 'DELETE',
|
|
574
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
580
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
575
581
|
});
|
|
576
582
|
if (disconnectResponse.status !== 200) {
|
|
577
583
|
throw new CloudInteropAPIError('Error during session tear down - unexpected status', 'ERR_DISCONNECT', new Error(disconnectResponse.statusText));
|
|
@@ -594,6 +600,7 @@ class CloudInteropAPI {
|
|
|
594
600
|
}
|
|
595
601
|
}
|
|
596
602
|
#handleMessage(topic, message, sessionDetails) {
|
|
603
|
+
this.#logger('debug', `Received message on topic: ${topic}`);
|
|
597
604
|
if (message.length === 0 || !sessionDetails) {
|
|
598
605
|
// Ignore clean up messages
|
|
599
606
|
return;
|
|
@@ -684,7 +691,7 @@ class CloudInteropAPI {
|
|
|
684
691
|
}
|
|
685
692
|
const payload = parts[1];
|
|
686
693
|
// Decode base64url encoded payload
|
|
687
|
-
const decodedBytes = buffer.Buffer.from(payload, '
|
|
694
|
+
const decodedBytes = buffer.Buffer.from(d(payload), 'base64');
|
|
688
695
|
const payloadJson = decodedBytes.toString('utf8');
|
|
689
696
|
// Parse JSON to get the exp claim
|
|
690
697
|
const claims = JSON.parse(payloadJson);
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
|
+
import 'fs';
|
|
3
|
+
import 'path';
|
|
2
4
|
import mqtt from 'mqtt';
|
|
3
5
|
|
|
6
|
+
var d=n=>{let t=n.replaceAll("-","+").replaceAll("_","/");return t.padEnd(t.length+(4-t.length%4)%4,"=")};
|
|
7
|
+
|
|
4
8
|
class CloudInteropAPIError extends Error {
|
|
5
9
|
code;
|
|
6
10
|
constructor(message = 'An unexpected error has occurred', code = 'UNEXPECTED_ERROR', cause) {
|
|
@@ -47,11 +51,11 @@ class EventController {
|
|
|
47
51
|
const isErrorIntentResult = (result) => 'error' in result;
|
|
48
52
|
|
|
49
53
|
const APP_ID_DELIM = '::';
|
|
50
|
-
const getRequestHeaders = (connectionParameters) => {
|
|
54
|
+
const getRequestHeaders = async (connectionParameters) => {
|
|
51
55
|
const headers = {};
|
|
52
56
|
headers['Content-Type'] = 'application/json';
|
|
53
57
|
if (connectionParameters.authenticationType === 'jwt' && connectionParameters.jwtAuthenticationParameters) {
|
|
54
|
-
const tokenResult = connectionParameters.jwtAuthenticationParameters.jwtRequestCallback();
|
|
58
|
+
const tokenResult = await connectionParameters.jwtAuthenticationParameters.jwtRequestCallback();
|
|
55
59
|
if (!tokenResult) {
|
|
56
60
|
throw new Error('jwtRequestCallback must return a token');
|
|
57
61
|
}
|
|
@@ -156,7 +160,7 @@ class IntentController {
|
|
|
156
160
|
try {
|
|
157
161
|
const startResponse = await fetch(`${this.#url}/api/intents/${this.#sessionDetails.sessionId}`, {
|
|
158
162
|
method: 'POST',
|
|
159
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
163
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
160
164
|
body: JSON.stringify({ findOptions }),
|
|
161
165
|
});
|
|
162
166
|
if (!startResponse.ok) {
|
|
@@ -201,7 +205,7 @@ class IntentController {
|
|
|
201
205
|
}
|
|
202
206
|
await fetch(`${this.#url}/api/intents/${this.#sessionDetails.sessionId}/${this.#discovery.id}`, {
|
|
203
207
|
method: 'DELETE',
|
|
204
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
208
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
205
209
|
})
|
|
206
210
|
.then((deleteResponse) => {
|
|
207
211
|
if (!deleteResponse.ok) {
|
|
@@ -222,7 +226,7 @@ class IntentController {
|
|
|
222
226
|
}
|
|
223
227
|
const postResponse = await fetch(`${this.#url}/api/intents/${this.#sessionDetails.sessionId}/sessions/${targetSessionId}`, {
|
|
224
228
|
method: 'POST',
|
|
225
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
229
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
226
230
|
body: JSON.stringify({ raiseOptions }),
|
|
227
231
|
});
|
|
228
232
|
if (!postResponse.ok) {
|
|
@@ -236,7 +240,7 @@ class IntentController {
|
|
|
236
240
|
try {
|
|
237
241
|
const reportResponse = await fetch(`${this.#url}/api/intents/${this.#sessionDetails.sessionId}/${discoveryId}`, {
|
|
238
242
|
method: 'POST',
|
|
239
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
243
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
240
244
|
body: JSON.stringify({ intents }),
|
|
241
245
|
});
|
|
242
246
|
if (reportResponse.ok) {
|
|
@@ -260,7 +264,7 @@ class IntentController {
|
|
|
260
264
|
const { sessionId } = getSourceFromSession(this.#sessionDetails);
|
|
261
265
|
const resultResponse = await fetch(`${this.#url}/api/intents/${initiatingSessionId}/result/${sessionId}`, {
|
|
262
266
|
method: 'POST',
|
|
263
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
267
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
264
268
|
body: JSON.stringify({ result }),
|
|
265
269
|
});
|
|
266
270
|
if (!resultResponse.ok) {
|
|
@@ -344,6 +348,7 @@ class CloudInteropAPI {
|
|
|
344
348
|
#reconnectRetryLimit = 30;
|
|
345
349
|
#keepAliveIntervalSeconds = 30;
|
|
346
350
|
#logger = (level, message) => {
|
|
351
|
+
// eslint-disable-next-line security/detect-object-injection -- level is restricted to the LogLevel union
|
|
347
352
|
console[level](message);
|
|
348
353
|
};
|
|
349
354
|
#reconnectRetries = 0;
|
|
@@ -379,7 +384,7 @@ class CloudInteropAPI {
|
|
|
379
384
|
const { sourceId, platformId } = this.#connectionParams;
|
|
380
385
|
const createSessionResponse = await fetch(`${this.#cloudInteropSettings.url}/api/sessions`, {
|
|
381
386
|
method: 'POST',
|
|
382
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
387
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
383
388
|
body: JSON.stringify({ sourceId: sourceId.trim(), platformId }),
|
|
384
389
|
});
|
|
385
390
|
if (!createSessionResponse.ok) {
|
|
@@ -417,6 +422,7 @@ class CloudInteropAPI {
|
|
|
417
422
|
// TODO: Dynamic intent discovery
|
|
418
423
|
// search for any ongoing discoveries in DB and fire report-intents on self
|
|
419
424
|
this.#logger('log', `Cloud Interop successfully connected to ${this.#cloudInteropSettings.url}`);
|
|
425
|
+
// eslint-disable-next-line security-node/detect-unhandled-event-errors -- this callback handles MQTT error events
|
|
420
426
|
this.#mqttClient.on('error', async (error) => {
|
|
421
427
|
// We will receive errors for each failed reconnection attempt
|
|
422
428
|
// We don't want to disconnect on these else we will never reconnect
|
|
@@ -509,7 +515,7 @@ class CloudInteropAPI {
|
|
|
509
515
|
};
|
|
510
516
|
const postResponse = await fetch(`${this.#cloudInteropSettings.url}/api/context-groups/${this.#sessionDetails.sessionId}/${contextGroup}`, {
|
|
511
517
|
method: 'POST',
|
|
512
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
518
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
513
519
|
body: JSON.stringify(payload),
|
|
514
520
|
});
|
|
515
521
|
if (!postResponse.ok) {
|
|
@@ -569,7 +575,7 @@ class CloudInteropAPI {
|
|
|
569
575
|
}
|
|
570
576
|
const disconnectResponse = await fetch(`${this.#cloudInteropSettings.url}/api/sessions/${this.#sessionDetails.sessionId}`, {
|
|
571
577
|
method: 'DELETE',
|
|
572
|
-
headers: getRequestHeaders(this.#connectionParams),
|
|
578
|
+
headers: await getRequestHeaders(this.#connectionParams),
|
|
573
579
|
});
|
|
574
580
|
if (disconnectResponse.status !== 200) {
|
|
575
581
|
throw new CloudInteropAPIError('Error during session tear down - unexpected status', 'ERR_DISCONNECT', new Error(disconnectResponse.statusText));
|
|
@@ -592,6 +598,7 @@ class CloudInteropAPI {
|
|
|
592
598
|
}
|
|
593
599
|
}
|
|
594
600
|
#handleMessage(topic, message, sessionDetails) {
|
|
601
|
+
this.#logger('debug', `Received message on topic: ${topic}`);
|
|
595
602
|
if (message.length === 0 || !sessionDetails) {
|
|
596
603
|
// Ignore clean up messages
|
|
597
604
|
return;
|
|
@@ -682,7 +689,7 @@ class CloudInteropAPI {
|
|
|
682
689
|
}
|
|
683
690
|
const payload = parts[1];
|
|
684
691
|
// Decode base64url encoded payload
|
|
685
|
-
const decodedBytes = Buffer.from(payload, '
|
|
692
|
+
const decodedBytes = Buffer.from(d(payload), 'base64');
|
|
686
693
|
const payloadJson = decodedBytes.toString('utf8');
|
|
687
694
|
// Parse JSON to get the exp claim
|
|
688
695
|
const claims = JSON.parse(payloadJson);
|
package/package.json
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfin/cloud-interop-core-api",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
4
|
-
"type": "module",
|
|
3
|
+
"version": "0.0.1-alpha.53e67c3",
|
|
5
4
|
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
6
|
"main": "./index.cjs",
|
|
7
7
|
"browser": "./index.mjs",
|
|
8
8
|
"types": "./bundle.d.ts",
|
|
9
|
-
"author": "",
|
|
9
|
+
"author": "support@here.io",
|
|
10
10
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"mqtt": "
|
|
13
|
-
"zod": "
|
|
14
|
-
},
|
|
15
|
-
"optionalDependencies": {
|
|
16
|
-
"@rollup/rollup-linux-x64-gnu": "*"
|
|
12
|
+
"mqtt": "catalog:",
|
|
13
|
+
"zod": "catalog:"
|
|
17
14
|
}
|
|
18
15
|
}
|