Skip to content

Commit b9ee2b6

Browse files
committed
refactor: moving deriveNamespace back to ElectronIPC
1 parent 71568cf commit b9ee2b6

13 files changed

Lines changed: 47 additions & 56 deletions

File tree

packages/sdk/electron/__tests__/ElectronClient.ipcMain.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import type {
88
LDIdentifyOptions,
99
} from '@launchdarkly/js-client-sdk-common';
1010

11-
import { deriveNamespace } from '../src/deriveNamespace';
1211
import { ElectronClient } from '../src/ElectronClient';
13-
import { getIPCChannelName } from '../src/ElectronIPC';
12+
import { deriveNamespace, getIPCChannelName } from '../src/ElectronIPC';
1413
import ElectronCrypto from '../src/platform/ElectronCrypto';
1514
import ElectronEncoding from '../src/platform/ElectronEncoding';
1615
import ElectronInfo from '../src/platform/ElectronInfo';

packages/sdk/electron/__tests__/ElectronIPC.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { deriveNamespace } from '../src/deriveNamespace';
2-
import { getIPCChannelName } from '../src/ElectronIPC';
1+
import { deriveNamespace, getIPCChannelName } from '../src/ElectronIPC';
32

43
it('derives namespace from credential alone', () => {
54
expect(deriveNamespace('mob-abc-123')).toBe('mob-abc-123');

packages/sdk/electron/__tests__/bridge/LDClientBridge.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ipcRenderer } from 'electron';
22

33
import '../../src/bridge';
44
import type { LDClientBridge } from '../../src/bridge/LDClientBridge';
5-
import { deriveNamespace } from '../../src/deriveNamespace';
5+
import { deriveNamespace } from '../../src/ElectronIPC';
66
import type { LDContext } from '../../src/index';
77

88
const clientSideId = 'client-side-id';

packages/sdk/electron/__tests__/renderer/ElectronRendererClient.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { LDContext } from '@launchdarkly/js-client-sdk-common';
22

33
import type { LDClientBridge } from '../../src/bridge/LDClientBridge';
4-
import { deriveNamespace } from '../../src/deriveNamespace';
4+
import { deriveNamespace } from '../../src/ElectronIPC';
55
import { ElectronRendererClient } from '../../src/renderer/ElectronRendererClient';
66

77
const ldClientBridge: LDClientBridge = {

packages/sdk/electron/contract-tests/entity/src/ClientEntity.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// eslint-disable-next-line import/no-extraneous-dependencies
22
import { app } from 'electron';
3-
import { createHash } from 'node:crypto';
43
import fs from 'node:fs';
54
import path from 'node:path';
65

@@ -28,7 +27,7 @@ export const malformedCommand = new Error('command was malformed');
2827
const isSet = (x?: unknown) => x !== null && x !== undefined;
2928
const maybeTime = (seconds?: number) => (isSet(seconds) ? seconds / 1000 : undefined);
3029

31-
function makeSdkConfig(options: SDKConfigParams, tag: string, namespace?: string) {
30+
function makeSdkConfig(options: SDKConfigParams, tag: string) {
3231
if (!options.clientSide) {
3332
throw new Error('configuration did not include clientSide options');
3433
}
@@ -121,7 +120,6 @@ function makeSdkConfig(options: SDKConfigParams, tag: string, namespace?: string
121120
// }
122121

123122
cf.enableIPC = false;
124-
cf.namespace = namespace;
125123

126124
// TODO: we might need this
127125
// cf.fetchGoals = false;
@@ -259,7 +257,7 @@ export class ClientEntity {
259257
}
260258
}
261259

262-
export async function createEntity(options: CreateInstanceParams, clientNamespace?: string) {
260+
export async function createEntity(options: CreateInstanceParams) {
263261
const logger = makeLogger(options.tag);
264262

265263
const clientSideId = options.configuration.credential || 'unknown-env-id';
@@ -272,7 +270,7 @@ export async function createEntity(options: CreateInstanceParams, clientNamespac
272270
options.configuration.startWaitTimeMs !== undefined
273271
? options.configuration.startWaitTimeMs
274272
: 5000;
275-
const sdkConfig = makeSdkConfig(options.configuration, options.tag, clientNamespace);
273+
const sdkConfig = makeSdkConfig(options.configuration, options.tag);
276274
const initialContext =
277275
options.configuration.clientSide?.initialUser ||
278276
options.configuration.clientSide?.initialContext ||

packages/sdk/electron/contract-tests/entity/src/ClientFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class ClientFactory {
1010
const id = this._clientCounter.toString();
1111
this._clientCounter += 1;
1212

13-
const client = await createEntity(options, id);
13+
const client = await createEntity(options);
1414
this._clients[id] = client;
1515

1616
return id;

packages/sdk/electron/src/ElectronClient.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import {
2626
readFlagsFromBootstrap,
2727
} from '@launchdarkly/js-client-sdk-common';
2828

29-
import { deriveNamespace } from './deriveNamespace';
3029
import ElectronDataManager from './ElectronDataManager';
3130
import {
3231
AllAsyncChannels,
3332
AllSyncChannels,
33+
deriveNamespace,
3434
getIPCChannelName,
3535
IpcEventCallback,
3636
IpcEventSubscription,
@@ -97,6 +97,7 @@ export class ElectronClient extends LDClientImpl {
9797
};
9898

9999
const platform = new ElectronPlatform(logger, options);
100+
const derivedNs = deriveNamespace(credential, validatedElectronOptions.namespace);
100101
const endpoints = useClientSideId ? browserFdv1Endpoints(credential) : mobileFdv1Endpoints();
101102

102103
super(
@@ -236,127 +237,127 @@ export class ElectronClient extends LDClientImpl {
236237
return dataManager.getConnectionMode() === 'offline';
237238
}
238239

239-
private _openIPCChannels(credential: string): void {
240-
this._ipcNamespace = credential;
240+
private _openIPCChannels(namespace: string): void {
241+
this._ipcNamespace = namespace;
241242
this._ipcEventSubscriptions = new Map<LDEmitterEventName, IpcEventSubscription>();
242243
this._ipcCallbackIdToEventName = new Map<string, LDEmitterEventName>();
243244
this._ipcSubscriptionQueue = [];
244245

245246
ipcMain.on(
246-
getIPCChannelName(credential, 'addEventHandler'),
247+
getIPCChannelName(namespace, 'addEventHandler'),
247248
(event: IpcMainEvent, messageData: IpcEventCallback) => {
248249
this._ipcSubscriptionQueue!.push({ type: 'add', event, messageData });
249250
this._processSubscriptionQueue();
250251
},
251252
);
252253

253254
ipcMain.on(
254-
getIPCChannelName(credential, 'removeEventHandler'),
255+
getIPCChannelName(namespace, 'removeEventHandler'),
255256
(event: IpcMainEvent, callbackId: string) => {
256257
this._ipcSubscriptionQueue!.push({ type: 'remove', event, callbackId });
257258
this._processSubscriptionQueue();
258259
},
259260
);
260261

261262
ipcMain.handle(
262-
getIPCChannelName(credential, 'waitForInitialization'),
263+
getIPCChannelName(namespace, 'waitForInitialization'),
263264
(_event, options?: LDWaitForInitializationOptions): Promise<LDWaitForInitializationResult> =>
264265
this.waitForInitialization(options),
265266
);
266267

267-
ipcMain.on(getIPCChannelName(credential, 'allFlags'), (event) => {
268+
ipcMain.on(getIPCChannelName(namespace, 'allFlags'), (event) => {
268269
// eslint-disable-next-line no-param-reassign
269270
event.returnValue = this.allFlags();
270271
});
271272

272-
ipcMain.on(getIPCChannelName(credential, 'boolVariation'), (event, key, defaultValue) => {
273+
ipcMain.on(getIPCChannelName(namespace, 'boolVariation'), (event, key, defaultValue) => {
273274
// eslint-disable-next-line no-param-reassign
274275
event.returnValue = this.boolVariation(key, defaultValue);
275276
});
276277

277-
ipcMain.on(getIPCChannelName(credential, 'boolVariationDetail'), (event, key, defaultValue) => {
278+
ipcMain.on(getIPCChannelName(namespace, 'boolVariationDetail'), (event, key, defaultValue) => {
278279
// eslint-disable-next-line no-param-reassign
279280
event.returnValue = this.boolVariationDetail(key, defaultValue);
280281
});
281282

282-
ipcMain.handle(getIPCChannelName(credential, 'flush'), (_event) => this.flush());
283+
ipcMain.handle(getIPCChannelName(namespace, 'flush'), (_event) => this.flush());
283284

284-
ipcMain.on(getIPCChannelName(credential, 'getContext'), (event) => {
285+
ipcMain.on(getIPCChannelName(namespace, 'getContext'), (event) => {
285286
// eslint-disable-next-line no-param-reassign
286287
event.returnValue = this.getContext();
287288
});
288289

289-
ipcMain.handle(getIPCChannelName(credential, 'identify'), (_event, context, identifyOptions) =>
290+
ipcMain.handle(getIPCChannelName(namespace, 'identify'), (_event, context, identifyOptions) =>
290291
this.identifyResult(context, identifyOptions),
291292
);
292293

293-
ipcMain.on(getIPCChannelName(credential, 'log'), (_event, level: string, message: string) => {
294+
ipcMain.on(getIPCChannelName(namespace, 'log'), (_event, level: string, message: string) => {
294295
if (VALID_LOG_LEVELS.has(level)) {
295296
this.logger[level as keyof LDLogger](message);
296297
}
297298
});
298299

299-
ipcMain.on(getIPCChannelName(credential, 'jsonVariation'), (event, key, defaultValue) => {
300+
ipcMain.on(getIPCChannelName(namespace, 'jsonVariation'), (event, key, defaultValue) => {
300301
// eslint-disable-next-line no-param-reassign
301302
event.returnValue = this.jsonVariation(key, defaultValue);
302303
});
303304

304-
ipcMain.on(getIPCChannelName(credential, 'jsonVariationDetail'), (event, key, defaultValue) => {
305+
ipcMain.on(getIPCChannelName(namespace, 'jsonVariationDetail'), (event, key, defaultValue) => {
305306
// eslint-disable-next-line no-param-reassign
306307
event.returnValue = this.jsonVariationDetail(key, defaultValue);
307308
});
308309

309-
ipcMain.on(getIPCChannelName(credential, 'numberVariation'), (event, key, defaultValue) => {
310+
ipcMain.on(getIPCChannelName(namespace, 'numberVariation'), (event, key, defaultValue) => {
310311
// eslint-disable-next-line no-param-reassign
311312
event.returnValue = this.numberVariation(key, defaultValue);
312313
});
313314

314315
ipcMain.on(
315-
getIPCChannelName(credential, 'numberVariationDetail'),
316+
getIPCChannelName(namespace, 'numberVariationDetail'),
316317
(event, key, defaultValue) => {
317318
// eslint-disable-next-line no-param-reassign
318319
event.returnValue = this.numberVariationDetail(key, defaultValue);
319320
},
320321
);
321322

322-
ipcMain.on(getIPCChannelName(credential, 'stringVariation'), (event, key, defaultValue) => {
323+
ipcMain.on(getIPCChannelName(namespace, 'stringVariation'), (event, key, defaultValue) => {
323324
// eslint-disable-next-line no-param-reassign
324325
event.returnValue = this.stringVariation(key, defaultValue);
325326
});
326327

327328
ipcMain.on(
328-
getIPCChannelName(credential, 'stringVariationDetail'),
329+
getIPCChannelName(namespace, 'stringVariationDetail'),
329330
(event, key, defaultValue) => {
330331
// eslint-disable-next-line no-param-reassign
331332
event.returnValue = this.stringVariationDetail(key, defaultValue);
332333
},
333334
);
334335

335-
ipcMain.on(getIPCChannelName(credential, 'track'), (event, key, data, metricValue) => {
336+
ipcMain.on(getIPCChannelName(namespace, 'track'), (event, key, data, metricValue) => {
336337
// eslint-disable-next-line no-param-reassign
337338
event.returnValue = this.track(key, data, metricValue);
338339
});
339340

340-
ipcMain.on(getIPCChannelName(credential, 'variation'), (event, key, defaultValue) => {
341+
ipcMain.on(getIPCChannelName(namespace, 'variation'), (event, key, defaultValue) => {
341342
// eslint-disable-next-line no-param-reassign
342343
event.returnValue = this.variation(key, defaultValue);
343344
});
344345

345-
ipcMain.on(getIPCChannelName(credential, 'variationDetail'), (event, key, defaultValue) => {
346+
ipcMain.on(getIPCChannelName(namespace, 'variationDetail'), (event, key, defaultValue) => {
346347
// eslint-disable-next-line no-param-reassign
347348
event.returnValue = this.variationDetail(key, defaultValue);
348349
});
349350

350-
ipcMain.handle(getIPCChannelName(credential, 'setConnectionMode'), (_event, mode) =>
351+
ipcMain.handle(getIPCChannelName(namespace, 'setConnectionMode'), (_event, mode) =>
351352
this.setConnectionMode(mode),
352353
);
353354

354-
ipcMain.on(getIPCChannelName(credential, 'getConnectionMode'), (event) => {
355+
ipcMain.on(getIPCChannelName(namespace, 'getConnectionMode'), (event) => {
355356
// eslint-disable-next-line no-param-reassign
356357
event.returnValue = this.getConnectionMode();
357358
});
358359

359-
ipcMain.on(getIPCChannelName(credential, 'isOffline'), (event) => {
360+
ipcMain.on(getIPCChannelName(namespace, 'isOffline'), (event) => {
360361
// eslint-disable-next-line no-param-reassign
361362
event.returnValue = this.isOffline();
362363
});

packages/sdk/electron/src/ElectronIPC.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { MessagePortMain } from 'electron';
2+
13
import { LDEmitterEventName } from '@launchdarkly/js-client-sdk-common';
24

35
/**
@@ -70,7 +72,7 @@ export type IPCChannel = IPCSyncChannel | IPCAsyncChannel;
7072
*/
7173
export interface IpcEventSubscription {
7274
broadcastCallback: (...args: any[]) => void;
73-
ports: Map<string, Electron.MessagePortMain>;
75+
ports: Map<string, MessagePortMain>;
7476
}
7577

7678
export interface IpcEventCallback {
@@ -84,3 +86,10 @@ export interface IpcEventCallback {
8486
export function getIPCChannelName(namespace: string, channel: IPCChannel): string {
8587
return `ld:${namespace}:${channel}`;
8688
}
89+
90+
/**
91+
* Derives an IPC namespace from a credential and an optional user-provided namespace.
92+
*/
93+
export function deriveNamespace(credential: string, customNamespace?: string): string {
94+
return customNamespace ? `${customNamespace}_${credential}` : credential;
95+
}

packages/sdk/electron/src/ElectronOptions.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,8 @@ export interface ElectronOptions extends LDOptionsBase {
120120
useClientSideId?: boolean;
121121

122122
/**
123-
* An optional namespace to isolate this client's storage and IPC channels
123+
* An optional namespace to isolate this client's IPC channels
124124
* from other clients using the same credential in the same process.
125-
*
126-
* @remarks
127-
* Useful when running multiple client instances (e.g., multiple environments)
128-
* in the same Electron app. When omitted, isolation is based solely on the credential.
129125
*/
130126
namespace?: string;
131127
}

packages/sdk/electron/src/deriveNamespace.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)