@openfin/cloud-interop-core-api 0.0.1-alpha.e6793f0 → 0.0.1-alpha.e6a5658

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/package.json CHANGED
@@ -1,231 +1,18 @@
1
1
  {
2
- "name": "@openfin/cloud-interop-core-api",
3
- "version": "0.0.1-alpha.e6793f0",
4
- "type": "module",
5
- "description": "",
6
- "files": [
7
- "./dist/*"
8
- ],
9
- "main": "./dist/index.cjs",
10
- "browser": "./dist/index.mjs",
11
- "types": "./dist/index.d.ts",
12
- "scripts": {
13
- "build": "rollup -c",
14
- "build:watch": "rollup -c --watch",
15
- "typecheck": "tsc --noEmit",
16
- "test": "jest --coverage",
17
- "lint": "eslint . --max-warnings 0",
18
- "lint:fix": "eslint . --fix --max-warnings 0"
19
- },
20
- "author": "",
21
- "license": "SEE LICENSE IN LICENSE.md",
22
- "devDependencies": {
23
- "@rollup/plugin-commonjs": "^28.0.1",
24
- "@rollup/plugin-inject": "^5.0.5",
25
- "@rollup/plugin-node-resolve": "^15.2.3",
26
- "@rollup/plugin-typescript": "^11.1.6",
27
- "@types/jest": "^29.5.14",
28
- "@types/node": "^20.10.0",
29
- "@typescript-eslint/eslint-plugin": "^7.5.0",
30
- "@typescript-eslint/parser": "^7.15.0",
31
- "eslint": "^8.57.0",
32
- "eslint-config-prettier": "^9.1.0",
33
- "eslint-plugin-check-file": "^2.8.0",
34
- "eslint-plugin-prettier": "^5.2.1",
35
- "eslint-plugin-simple-import-sort": "^12.1.1",
36
- "eslint-plugin-unicorn": "^55.0.0",
37
- "eslint-plugin-unused-imports": "^4.1.4",
38
- "jest": "^29.7.0",
39
- "prettier": "^3.3.3",
40
- "rollup": "^4.9.6",
41
- "ts-jest": "^29.2.5",
42
- "typescript": "^5.6.3"
43
- },
44
- "dependencies": {
45
- "axios": "^1.6.2",
46
- "mqtt": "^5.3.1"
47
- },
48
- "eslintConfig": {
49
- "env": {
50
- "browser": true,
51
- "node": true
52
- },
53
- "parser": "@typescript-eslint/parser",
54
- "parserOptions": {
55
- "ecmaVersion": "latest",
56
- "project": true,
57
- "sourceType": "module"
58
- },
59
- "extends": [
60
- "eslint:recommended",
61
- "plugin:@typescript-eslint/recommended",
62
- "plugin:@typescript-eslint/strict",
63
- "plugin:unicorn/recommended",
64
- "plugin:prettier/recommended"
65
- ],
66
- "plugins": [
67
- "prettier",
68
- "check-file",
69
- "simple-import-sort",
70
- "unused-imports"
71
- ],
72
- "rules": {
73
- "unused-imports/no-unused-imports": "warn",
74
- "unicorn/prevent-abbreviations": [
75
- "error",
76
- {
77
- "replacements": {
78
- "props": false,
79
- "prop": false,
80
- "ref": false,
81
- "args": false,
82
- "arg": false,
83
- "src": false,
84
- "dev": false,
85
- "str": false,
86
- "req": false,
87
- "res": false
88
- }
89
- }
90
- ],
91
- "@typescript-eslint/no-non-null-assertion": "error",
92
- "unicorn/no-nested-ternary": "off",
93
- "unicorn/no-array-for-each": "off",
94
- "unicorn/no-useless-undefined": "off",
95
- "unicorn/no-null": "off",
96
- "eqeqeq": [
97
- "error",
98
- "always"
99
- ],
100
- "no-alert": "error",
101
- "no-eval": "error",
102
- "prettier/prettier": "warn",
103
- "simple-import-sort/imports": [
104
- "error",
105
- {
106
- "groups": [
107
- [
108
- "^react$"
109
- ],
110
- [
111
- "^react"
112
- ],
113
- [
114
- "^next"
115
- ],
116
- [
117
- "^zod"
118
- ],
119
- [
120
- "^@radix-ui/"
121
- ],
122
- [
123
- "^[^.]"
124
- ],
125
- [
126
- "@/components/ui/.*"
127
- ],
128
- [
129
- "@/components/.*"
130
- ],
131
- [
132
- "@/config/.*"
133
- ],
134
- [
135
- "@/lib/.*"
136
- ],
137
- [
138
- "^\\.\\.(?!/?$)",
139
- "^\\.\\./?$"
140
- ],
141
- [
142
- "^\\./(?=.*/)(?!/?$)",
143
- "^\\.(?!/?$)",
144
- "^\\./?$"
145
- ],
146
- [
147
- "^.+\\.s?css$"
148
- ]
149
- ]
150
- }
151
- ],
152
- "check-file/no-index": "off",
153
- "check-file/filename-naming-convention": [
154
- "error",
155
- {
156
- "**/*.{jsx,tsx}": "KEBAB_CASE",
157
- "**/*.{js,ts}": "KEBAB_CASE"
158
- },
159
- {
160
- "ignoreMiddleExtensions": true
161
- }
162
- ],
163
- "check-file/filename-blocklist": [
164
- "error",
165
- {
166
- "**/*.spec.js": "*.test.js",
167
- "**/*.spec.jsx": "*.test.jsx",
168
- "**/*.spec.ts": "*.test.ts",
169
- "**/*.spec.tsx": "*.test.tsx"
170
- }
171
- ],
172
- "no-restricted-syntax": [
173
- "error",
174
- {
175
- "selector": "TSEnumDeclaration",
176
- "message": "Prefer string unions to enums."
177
- }
178
- ],
179
- "curly": [
180
- "error",
181
- "multi-line"
182
- ],
183
- "@typescript-eslint/consistent-type-definitions": [
184
- "error",
185
- "type"
186
- ],
187
- "@typescript-eslint/no-unused-vars": [
188
- "warn",
189
- {
190
- "argsIgnorePattern": "^_",
191
- "varsIgnorePattern": "^_"
192
- }
193
- ],
194
- "@typescript-eslint/no-explicit-any": "warn"
195
- },
196
- "ignorePatterns": [
197
- "node_modules",
198
- "out",
199
- "build",
200
- "dist",
201
- "coverage",
202
- "tests",
203
- "rollup.config.mjs",
204
- "examples"
205
- ]
206
- },
207
- "jest": {
208
- "collectCoverage": true,
209
- "collectCoverageFrom": [
210
- "src/**/*.ts"
211
- ],
212
- "coverageReporters": [
213
- "lcov",
214
- "text-summary"
215
- ],
216
- "preset": "ts-jest",
217
- "restoreMocks": true,
218
- "setupFiles": [],
219
- "testMatch": [
220
- "**/tests/*.test.ts"
221
- ],
222
- "testTimeout": 100000
223
- },
224
- "prettier": {
225
- "printWidth": 160,
226
- "semi": true,
227
- "singleQuote": true,
228
- "tabWidth": 4,
229
- "trailingComma": "all"
230
- }
2
+ "name": "@openfin/cloud-interop-core-api",
3
+ "version": "0.0.1-alpha.e6a5658",
4
+ "type": "module",
5
+ "description": "",
6
+ "main": "./index.cjs",
7
+ "browser": "./index.mjs",
8
+ "types": "./bundle.d.ts",
9
+ "author": "",
10
+ "license": "SEE LICENSE IN LICENSE.md",
11
+ "optionalDependencies": {
12
+ "@rollup/rollup-linux-x64-gnu": "*"
13
+ },
14
+ "dependencies": {
15
+ "mqtt": "^5.3.1",
16
+ "zod": "^3.24.2"
17
+ }
231
18
  }
package/dist/api.d.ts DELETED
@@ -1,70 +0,0 @@
1
- import mqtt from 'mqtt';
2
- import { CloudInteropSettings, ConnectParameters as ConnectParameters, ContextEvent } from './interfaces';
3
- type CreateSessionResponse = {
4
- sessionId: string;
5
- sessionRootTopic: string;
6
- url: string;
7
- token: string;
8
- orgId: string;
9
- sub: string;
10
- platformId: string;
11
- sourceId: string;
12
- };
13
- type EventMap = {
14
- connected: () => void;
15
- disconnected: () => void;
16
- context: (event: ContextEvent) => void;
17
- reconnecting: (attemptNo: number) => void;
18
- error: (error: Error) => void;
19
- };
20
- /**
21
- * Represents a single connection to a Cloud Interop service
22
- *
23
- * @export
24
- * @class CloudInteropAPI
25
- * @implements {Client}
26
- */
27
- export declare class CloudInteropAPI {
28
- #private;
29
- private cloudInteropSettings;
30
- private _sessionDetails?;
31
- private _mqttClient?;
32
- private reconnectRetryLimit;
33
- private logger;
34
- private reconnectRetries;
35
- private connectionParams?;
36
- private eventListeners;
37
- constructor(cloudInteropSettings: CloudInteropSettings);
38
- get sessionDetails(): CreateSessionResponse | undefined;
39
- get mqttClient(): mqtt.MqttClient | undefined;
40
- /**
41
- * Connects and creates a session on the Cloud Interop service
42
- *
43
- * @param {ConnectParameters} parameters - The parameters to use to connect
44
- * @return {*} {Promise<void>}
45
- * @memberof CloudInteropAPI
46
- * @throws {CloudInteropAPIError} - If an error occurs during connection
47
- * @throws {AuthorizationError} - If the connection is unauthorized
48
- */
49
- connect(parameters: ConnectParameters): Promise<void>;
50
- /**
51
- * Disconnects from the Cloud Interop service
52
- *
53
- * @return {*} {Promise<void>}
54
- * @memberof CloudInteropAPI
55
- * @throws {CloudInteropAPIError} - If an error occurs during disconnection
56
- */
57
- disconnect(): Promise<void>;
58
- /**
59
- * Publishes a new context for the given context group to the other connected sessions
60
- *
61
- * @param {string} contextGroup - The context group to publish to
62
- * @param {object} context - The context to publish
63
- * @return {*} {Promise<void>}
64
- * @memberof CloudInteropAPI
65
- */
66
- setContext(contextGroup: string, context: object): Promise<void>;
67
- addEventListener<K extends keyof EventMap>(type: K, callback: EventMap[K]): void;
68
- removeEventListener<K extends keyof EventMap>(type: K, callback: EventMap[K]): void;
69
- }
70
- export {};
@@ -1,7 +0,0 @@
1
- export declare class CloudInteropAPIError extends Error {
2
- code: string;
3
- constructor(message?: string, code?: string, cause?: unknown);
4
- }
5
- export declare class AuthorizationError extends CloudInteropAPIError {
6
- constructor(message?: string, code?: string);
7
- }
package/dist/index.cjs DELETED
@@ -1,255 +0,0 @@
1
- 'use strict';
2
-
3
- var axios = require('axios');
4
- var mqtt = require('mqtt');
5
-
6
- class CloudInteropAPIError extends Error {
7
- code;
8
- constructor(message = 'An unexpected error has occurred', code = 'UNEXPECTED_ERROR', cause) {
9
- super(message, { cause: cause });
10
- this.name = this.constructor.name;
11
- this.code = code;
12
- }
13
- }
14
- class AuthorizationError extends CloudInteropAPIError {
15
- constructor(message = 'Not authorized', code = 'ERR_UNAUTHORIZED') {
16
- super(message, code, undefined);
17
- }
18
- }
19
-
20
- /**
21
- * Represents a single connection to a Cloud Interop service
22
- *
23
- * @export
24
- * @class CloudInteropAPI
25
- * @implements {Client}
26
- */
27
- class CloudInteropAPI {
28
- cloudInteropSettings;
29
- _sessionDetails;
30
- _mqttClient;
31
- reconnectRetryLimit = 30;
32
- logger = (level, message) => {
33
- console[level](message);
34
- };
35
- reconnectRetries = 0;
36
- connectionParams;
37
- eventListeners = new Map();
38
- constructor(cloudInteropSettings) {
39
- this.cloudInteropSettings = cloudInteropSettings;
40
- }
41
- get sessionDetails() {
42
- return this._sessionDetails;
43
- }
44
- get mqttClient() {
45
- return this._mqttClient;
46
- }
47
- /**
48
- * Connects and creates a session on the Cloud Interop service
49
- *
50
- * @param {ConnectParameters} parameters - The parameters to use to connect
51
- * @return {*} {Promise<void>}
52
- * @memberof CloudInteropAPI
53
- * @throws {CloudInteropAPIError} - If an error occurs during connection
54
- * @throws {AuthorizationError} - If the connection is unauthorized
55
- */
56
- async connect(parameters) {
57
- this.#validateConnectParams(parameters);
58
- this.connectionParams = parameters;
59
- this.reconnectRetryLimit = parameters.reconnectRetryLimit || this.reconnectRetryLimit;
60
- this.logger = parameters.logger || this.logger;
61
- const { sourceId, platformId } = this.connectionParams;
62
- try {
63
- const createSessionResponse = await axios.post(`${this.cloudInteropSettings.url}/api/sessions`, {
64
- sourceId,
65
- platformId,
66
- }, {
67
- headers: this.#getRequestHeaders(),
68
- });
69
- if (createSessionResponse.status !== 201) {
70
- throw new CloudInteropAPIError(`Failed to connect to the Cloud Interop service: ${this.cloudInteropSettings.url}`, 'ERR_CONNECT', createSessionResponse.status);
71
- }
72
- this._sessionDetails = createSessionResponse.data;
73
- const sessionRootTopic = this._sessionDetails.sessionRootTopic;
74
- const clientOptions = {
75
- clientId: this._sessionDetails.sessionId,
76
- clean: true,
77
- protocolVersion: 5,
78
- // The "will" message will be published on an unexpected disconnection
79
- // The server can then tidy up. So it needs every for this client to do that, the session details is perfect
80
- will: {
81
- topic: 'interop/lastwill',
82
- payload: Buffer.from(JSON.stringify(this._sessionDetails)),
83
- qos: 0,
84
- retain: false,
85
- },
86
- username: this._sessionDetails.token,
87
- };
88
- this._mqttClient = await mqtt.connectAsync(this._sessionDetails.url, clientOptions);
89
- this.logger('log', `Cloud Interop successfully connected to ${this.cloudInteropSettings.url}`);
90
- this._mqttClient.on('error', (error) => {
91
- this.logger('error', `Cloud Interop Infrastructure Error: ${error}`);
92
- this.#emitEvent('error', error);
93
- this.disconnect();
94
- });
95
- this._mqttClient.on('reconnect', () => {
96
- this.logger('debug', `Cloud Interop attempting reconnection...`);
97
- // Default reconnectPeriod = 30 seconds
98
- // Attempt reconnection 30 times before ending session
99
- this.reconnectRetries += 1;
100
- if (this.reconnectRetries === this.reconnectRetryLimit) {
101
- this.logger('warn', `Cloud Interop reached max reconnection attempts...`);
102
- this.disconnect();
103
- }
104
- this.#emitEvent('reconnecting', this.reconnectRetries);
105
- });
106
- // Does not fire on initial connection, only successful reconnection attempts
107
- this._mqttClient.on('connect', () => {
108
- this.logger('debug', `Cloud Interop successfully reconnected`);
109
- this.reconnectRetries = 0;
110
- this.#emitEvent('connected');
111
- });
112
- this._mqttClient.on('message', (topic, message) => {
113
- if (!this._sessionDetails) {
114
- this.logger('warn', 'Received message when session not connected');
115
- return;
116
- }
117
- this.#handleCommand(topic, message, this._sessionDetails);
118
- });
119
- // Subscribe to all context groups
120
- this._mqttClient.subscribe(`${sessionRootTopic}/context-groups/#`);
121
- // Listen out for global commands
122
- this._mqttClient.subscribe(`${sessionRootTopic}/commands`);
123
- }
124
- catch (error) {
125
- if (axios.isAxiosError(error)) {
126
- if (error.response?.status === 401 || error.response?.status === 403) {
127
- throw new AuthorizationError();
128
- }
129
- throw new CloudInteropAPIError();
130
- }
131
- throw error;
132
- }
133
- }
134
- /**
135
- * Disconnects from the Cloud Interop service
136
- *
137
- * @return {*} {Promise<void>}
138
- * @memberof CloudInteropAPI
139
- * @throws {CloudInteropAPIError} - If an error occurs during disconnection
140
- */
141
- async disconnect() {
142
- if (!this._sessionDetails) {
143
- return;
144
- }
145
- try {
146
- const disconnectResponse = await axios.delete(`${this.cloudInteropSettings.url}/api/sessions/${this._sessionDetails.sessionId}`, {
147
- headers: this.#getRequestHeaders(),
148
- });
149
- if (disconnectResponse.status !== 200) {
150
- throw new CloudInteropAPIError('Error during disconnection', 'ERR_DISCONNECT', disconnectResponse.status);
151
- }
152
- }
153
- catch {
154
- throw new CloudInteropAPIError('Error during disconnection', 'ERR_DISCONNECT');
155
- }
156
- finally {
157
- this._mqttClient?.removeAllListeners();
158
- this._mqttClient?.end(true);
159
- this._sessionDetails = undefined;
160
- this._mqttClient = undefined;
161
- this.reconnectRetries = 0;
162
- this.#emitEvent('disconnected');
163
- }
164
- }
165
- /**
166
- * Publishes a new context for the given context group to the other connected sessions
167
- *
168
- * @param {string} contextGroup - The context group to publish to
169
- * @param {object} context - The context to publish
170
- * @return {*} {Promise<void>}
171
- * @memberof CloudInteropAPI
172
- */
173
- async setContext(contextGroup, context) {
174
- if (!this._sessionDetails || !this.connectionParams) {
175
- throw new Error('Session not connected');
176
- }
177
- const { sourceId } = this.connectionParams;
178
- const payload = {
179
- sourceId,
180
- context,
181
- };
182
- await axios.post(`${this.cloudInteropSettings.url}/api/context-groups/${this._sessionDetails.sessionId}/${contextGroup}`, payload, {
183
- headers: this.#getRequestHeaders(),
184
- });
185
- }
186
- addEventListener(type, callback) {
187
- const listeners = this.eventListeners.get(type) || [];
188
- listeners.push(callback);
189
- this.eventListeners.set(type, listeners);
190
- }
191
- removeEventListener(type, callback) {
192
- const listeners = this.eventListeners.get(type) || [];
193
- const index = listeners.indexOf(callback);
194
- if (index !== -1) {
195
- listeners.splice(index, 1);
196
- }
197
- this.eventListeners.set(type, listeners);
198
- }
199
- #handleCommand(topic, message, sessionDetails) {
200
- if (message.length === 0 || !sessionDetails) {
201
- // Ignore clean up messages
202
- return;
203
- }
204
- const messageEnvelope = JSON.parse(message.toString());
205
- if (topic.startsWith(`${sessionDetails.sessionRootTopic}/context-groups/`)) {
206
- if (messageEnvelope.source.sessionId === sessionDetails.sessionId) {
207
- return;
208
- }
209
- const { channelName: contextGroup, payload: context, source } = messageEnvelope;
210
- this.#emitEvent('context', { contextGroup, context, source });
211
- }
212
- }
213
- #emitEvent(type, ...args) {
214
- const listeners = this.eventListeners.get(type) || [];
215
- listeners.forEach((listener) => listener(...args));
216
- }
217
- #validateConnectParams = (parameters) => {
218
- if (!parameters) {
219
- throw new Error('Connect parameters must be provided');
220
- }
221
- if (parameters.authenticationType === 'jwt' &&
222
- (!parameters.jwtAuthenticationParameters?.jwtRequestCallback || !parameters.jwtAuthenticationParameters?.authenticationId)) {
223
- throw new Error('jwtAuthenticationParameters must be provided when using jwt authentication');
224
- }
225
- if (parameters.authenticationType === 'basic' &&
226
- (!parameters.basicAuthenticationParameters?.username || !parameters.basicAuthenticationParameters?.password)) {
227
- throw new Error('basicAuthenticationParameters must be provided when using basic authentication');
228
- }
229
- };
230
- #getRequestHeaders = () => {
231
- if (!this.connectionParams) {
232
- throw new Error('Connect parameters must be provided');
233
- }
234
- const headers = new axios.AxiosHeaders();
235
- headers['Content-Type'] = 'application/json';
236
- if (this.connectionParams.authenticationType === 'jwt' && this.connectionParams.jwtAuthenticationParameters) {
237
- const tokenResult = this.connectionParams.jwtAuthenticationParameters.jwtRequestCallback();
238
- if (!tokenResult) {
239
- throw new Error('jwtRequestCallback must return a token');
240
- }
241
- headers['x-of-auth-id'] = this.connectionParams.jwtAuthenticationParameters.authenticationId;
242
- headers['Authorization'] =
243
- typeof tokenResult === 'string' ? `Bearer ${tokenResult}` : `Bearer ${Buffer.from(JSON.stringify(tokenResult)).toString('base64')}`;
244
- }
245
- if (this.connectionParams.authenticationType === 'basic' && this.connectionParams.basicAuthenticationParameters) {
246
- const { username, password } = this.connectionParams.basicAuthenticationParameters;
247
- headers['Authorization'] = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
248
- }
249
- return headers;
250
- };
251
- }
252
-
253
- exports.AuthorizationError = AuthorizationError;
254
- exports.CloudInteropAPI = CloudInteropAPI;
255
- exports.CloudInteropAPIError = CloudInteropAPIError;
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './api';
2
- export * from './errors/api.error';
3
- export * from './interfaces';