-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Implement anonymous context processing (#350)
Apologies, I missed implementing this when moving the project to js-core.
- Loading branch information
Showing
11 changed files
with
302 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Internal use only. These functions should only be used as part of the initial validation of | ||
* the LDContext object. Thereafter, the Context object should be used. | ||
*/ | ||
import type { LDContext, LDMultiKindContext, LDSingleKindContext, LDUser } from '../../api'; | ||
import { TypeValidators } from '../../validators'; | ||
|
||
/** | ||
* Check if a context is a single kind context. | ||
* @param context | ||
* @returns true if the context is a single kind context. | ||
*/ | ||
export function isSingleKind(context: LDContext): context is LDSingleKindContext { | ||
if ('kind' in context) { | ||
return TypeValidators.String.is(context.kind) && context.kind !== 'multi'; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Check if a context is a multi-kind context. | ||
* @param context | ||
* @returns true if it is a multi-kind context. | ||
*/ | ||
export function isMultiKind(context: LDContext): context is LDMultiKindContext { | ||
if ('kind' in context) { | ||
return TypeValidators.String.is(context.kind) && context.kind === 'multi'; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Check if a context is a legacy user context. | ||
* @param context | ||
* @returns true if it is a legacy user context. | ||
*/ | ||
export function isLegacyUser(context: LDContext): context is LDUser { | ||
return !('kind' in context) || context.kind === null || context.kind === undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/shared/sdk-client/src/utils/calculateFlagChanges.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { fastDeepEqual } from '@launchdarkly/js-sdk-common'; | ||
|
||
import { Flags } from '../types'; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export default function calculateFlagChanges(flags: Flags, incomingFlags: Flags) { | ||
const changedKeys: string[] = []; | ||
|
||
// flag deleted or updated | ||
Object.entries(flags).forEach(([k, f]) => { | ||
const incoming = incomingFlags[k]; | ||
if (!incoming || !fastDeepEqual(f, incoming)) { | ||
changedKeys.push(k); | ||
} | ||
}); | ||
|
||
// flag added | ||
Object.keys(incomingFlags).forEach((k) => { | ||
if (!flags[k]) { | ||
changedKeys.push(k); | ||
} | ||
}); | ||
|
||
return changedKeys; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import type { | ||
LDContext, | ||
LDContextCommon, | ||
LDMultiKindContext, | ||
LDUser, | ||
} from '@launchdarkly/js-sdk-common'; | ||
import { basicPlatform } from '@launchdarkly/private-js-mocks'; | ||
|
||
import ensureKey, { addNamespace, getOrGenerateKey } from './ensureKey'; | ||
|
||
const { crypto, storage } = basicPlatform; | ||
describe('ensureKey', () => { | ||
beforeEach(() => { | ||
crypto.randomUUID.mockReturnValueOnce('random1').mockReturnValueOnce('random2'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('addNamespace', async () => { | ||
const nsKey = addNamespace('org'); | ||
expect(nsKey).toEqual('LaunchDarkly_AnonKeys_org'); | ||
}); | ||
|
||
test('getOrGenerateKey create new key', async () => { | ||
const key = await getOrGenerateKey('org', basicPlatform); | ||
|
||
expect(key).toEqual('random1'); | ||
expect(crypto.randomUUID).toHaveBeenCalled(); | ||
expect(storage.get).toHaveBeenCalledWith('LaunchDarkly_AnonKeys_org'); | ||
expect(storage.set).toHaveBeenCalledWith('LaunchDarkly_AnonKeys_org', 'random1'); | ||
}); | ||
|
||
test('getOrGenerateKey existing key', async () => { | ||
storage.get.mockImplementation((nsKind: string) => | ||
nsKind === 'LaunchDarkly_AnonKeys_org' ? 'random1' : undefined, | ||
); | ||
|
||
const key = await getOrGenerateKey('org', basicPlatform); | ||
|
||
expect(key).toEqual('random1'); | ||
expect(crypto.randomUUID).not.toHaveBeenCalled(); | ||
expect(storage.get).toHaveBeenCalledWith('LaunchDarkly_AnonKeys_org'); | ||
expect(storage.set).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('ensureKey should not override anonymous key if specified', async () => { | ||
const context: LDContext = { kind: 'org', anonymous: true, key: 'Testy Pizza' }; | ||
const c = await ensureKey(context, basicPlatform); | ||
|
||
expect(c.key).toEqual('Testy Pizza'); | ||
}); | ||
|
||
test('ensureKey non-anonymous single context should be unchanged', async () => { | ||
const context: LDContext = { kind: 'org', key: 'Testy Pizza' }; | ||
const c = await ensureKey(context, basicPlatform); | ||
|
||
expect(c.key).toEqual('Testy Pizza'); | ||
expect(c.anonymous).toBeFalsy(); | ||
}); | ||
|
||
test('ensureKey non-anonymous contexts in multi should be unchanged', async () => { | ||
const context: LDContext = { | ||
kind: 'multi', | ||
user: { key: 'userKey' }, | ||
org: { key: 'orgKey' }, | ||
}; | ||
|
||
const c = (await ensureKey(context, basicPlatform)) as LDMultiKindContext; | ||
|
||
expect((c.user as LDContextCommon).key).toEqual('userKey'); | ||
expect((c.org as LDContextCommon).key).toEqual('orgKey'); | ||
}); | ||
|
||
test('ensureKey should create key for single anonymous context', async () => { | ||
const context: LDContext = { kind: 'org', anonymous: true, key: '' }; | ||
const c = await ensureKey(context, basicPlatform); | ||
expect(c.key).toEqual('random1'); | ||
}); | ||
|
||
test('ensureKey should create key for an anonymous context in multi', async () => { | ||
const context: LDContext = { | ||
kind: 'multi', | ||
user: { anonymous: true, key: '' }, | ||
org: { key: 'orgKey' }, | ||
}; | ||
|
||
const c = (await ensureKey(context, basicPlatform)) as LDMultiKindContext; | ||
|
||
expect((c.user as LDContextCommon).key).toEqual('random1'); | ||
expect((c.org as LDContextCommon).key).toEqual('orgKey'); | ||
}); | ||
|
||
test('ensureKey should create key for all anonymous contexts in multi', async () => { | ||
const context: LDContext = { | ||
kind: 'multi', | ||
user: { anonymous: true, key: '' }, | ||
org: { anonymous: true, key: '' }, | ||
}; | ||
|
||
const c = (await ensureKey(context, basicPlatform)) as LDMultiKindContext; | ||
|
||
expect((c.user as LDContextCommon).key).toEqual('random1'); | ||
expect((c.org as LDContextCommon).key).toEqual('random2'); | ||
}); | ||
|
||
test('ensureKey should create key for anonymous legacy user', async () => { | ||
const context: LDUser = { | ||
anonymous: true, | ||
key: '', | ||
}; | ||
const c = await ensureKey(context, basicPlatform); | ||
expect(c.key).toEqual('random1'); | ||
}); | ||
}); |
Oops, something went wrong.