Skip to content

Commit 45c5283

Browse files
committed
refactor: create a safe client id
1 parent e65dd80 commit 45c5283

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

app/examples/Basic/Basic.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Provider } from 'react-redux';
44
import { WorkflowManager, WorkflowManagerConfig } from '../../../dist';
55
import { Child } from './Child';
66
import { store } from './store';
7+
import { createClientId } from './utils';
78

89
WorkflowManagerConfig.setStore(store);
910

@@ -13,7 +14,7 @@ export const Basic: React.FC = () => {
1314
<WorkflowManager
1415
brokerUrl="ws://broker.mqttdashboard.com:8000/mqtt"
1516
options={{
16-
clientId: `clientId-${Math.random().toString(36).substring(2, 9)}`,
17+
clientId: createClientId(),
1718
keepalive: 60,
1819
}}
1920
>

app/examples/Basic/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const createClientId = () => {
2+
const array = new Uint8Array(20);
3+
4+
window.crypto.getRandomValues(array);
5+
6+
return Array.from(array, (dec) =>
7+
('0' + dec.toString(16)).substring(-2),
8+
).join('');
9+
};

src/WorkflowManagerConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class WorkflowManagerConfig implements WorkflowManagerConfigProps {
9595
const client = this.getMqttClient();
9696
const store = WorkflowManagerConfig._store;
9797

98-
const shouldSubscribe = shouldSubscribeOrUnsubscribe(topic, store, client);
98+
const shouldSubscribe = shouldSubscribeOrUnsubscribe(store, client);
9999

100100
if (!shouldSubscribe) return;
101101

@@ -126,7 +126,7 @@ class WorkflowManagerConfig implements WorkflowManagerConfigProps {
126126
const client = this.getMqttClient();
127127
const store = WorkflowManagerConfig._store;
128128

129-
const unsubscribe = shouldSubscribeOrUnsubscribe(topic, store, client);
129+
const unsubscribe = shouldSubscribeOrUnsubscribe(store, client);
130130

131131
if (!unsubscribe) return;
132132

src/utils/constants.util.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export const VALID_TOPIC_PATTERN =
2-
/^(([\\+#]{1}|[^\\+#]*)\/)?(([\\+#]{1}|[^\\+#]*)\/{1})*(([\\+#]{1}|[^\\+#]*))$/;
3-
41
export const ERROR_MESSAGES = {
52
NO_WRAPPER:
63
'No context found. Did you forget to wrap your app with WorkflowManager component?',

src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './constants.util';
2-
export * from './isValidTopic.util';
32
export * from './isValidJSON.util';
43
export * from './shouldSubscribeOrUnsubscribe.util';

0 commit comments

Comments
 (0)