From 4b15ac4738d6b3a78d411563319fd2a45abbcc7c Mon Sep 17 00:00:00 2001
From: Marin Karamihalev <marin.karamihalev@iopsys.eu>
Date: Mon, 16 Oct 2023 12:06:02 +0200
Subject: [PATCH] added cleanSession option, defaults to false, set clientId to
 fromId

---
 package.json               |  2 +-
 src/configurations/mqtt.ts | 12 ++++++++++--
 src/types.ts               |  4 ++++
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/package.json b/package.json
index e3468a1..f9d690e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "usp-js",
-  "version": "0.4.7",
+  "version": "0.4.8",
   "description": "Library for easy usp(TR-369) communication using mqtt or ws.",
   "main": "node/index.js",
   "browser": "web/index.js",
diff --git a/src/configurations/mqtt.ts b/src/configurations/mqtt.ts
index 7015f2d..9fe463c 100644
--- a/src/configurations/mqtt.ts
+++ b/src/configurations/mqtt.ts
@@ -16,8 +16,15 @@ import { jsonRoots } from "./util";
 export const isURL = (opts: ConnectionOptions): opts is URLConnectionOptions =>
   "url" in opts;
 
-export const connectClient: ConnectClientFn = async (opts) =>
-  isURL(opts)
+const correctOptions = (opts: ConnectionOptions): ConnectionOptions => ({
+  ...opts,
+  clientId: opts.clientId || opts.fromId,
+  clean: opts.cleanSession ?? false,
+});
+
+export const connectClient: ConnectClientFn = async (initialOpts) => {
+  const opts = correctOptions(initialOpts);
+  return isURL(opts)
     ? (mqttAsync.connectAsync(
         opts.url,
         opts as any
@@ -31,6 +38,7 @@ export const connectClient: ConnectClientFn = async (opts) =>
           : {}),
       } as any) as unknown as ConnectionClient)
     : (mqttAsync.connectAsync(opts) as unknown as ConnectionClient);
+};
 
 export const loadProtobuf = async (version: USPVersion): Promise<Proto> => {
   const [rootRecordJson, rootMsgJson] = jsonRoots[version];
diff --git a/src/types.ts b/src/types.ts
index a1efb7a..73f68be 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -765,6 +765,10 @@ export interface MainConnectionOptions {
    */
   connectTimeout?: number;
   clientId?: string;
+  /**
+   * Defaults to false
+   */
+  cleanSession?: boolean;
   /**
    * Allows passing additional options to the underlying connection library
    */
-- 
GitLab