Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions packages/shared/common/__tests__/Context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,151 @@ describe('given a multi-kind context', () => {
expect(context?.kindsAndKeys).toEqual({ org: 'OrgKey', user: 'User%:/Key' });
});
});

describe('given a user context with private attributes', () => {
const input = Context.fromLDContext({
key: 'testKey',
name: 'testName',
custom: { cat: 'calico', dog: 'lab' },
anonymous: true,
privateAttributeNames: ['/a/b/c', 'cat', 'custom/dog'],
});

const expected = {
key: 'testKey',
kind: 'user',
name: 'testName',
cat: 'calico',
dog: 'lab',
anonymous: true,
_meta: {
privateAttributes: ['/a/b/c', 'cat', 'custom/dog'],
},
};

it('it can convert from LDContext to Context and back to LDContext', () => {
expect(Context.toLDContext(input)).toEqual(expected);
});
});

describe('given a user context without private attributes', () => {
const input = Context.fromLDContext({
key: 'testKey',
name: 'testName',
custom: { cat: 'calico', dog: 'lab' },
anonymous: true,
});

const expected = {
key: 'testKey',
kind: 'user',
name: 'testName',
cat: 'calico',
dog: 'lab',
anonymous: true,
};

it('it can convert from LDContext to Context and back to LDContext', () => {
expect(Context.toLDContext(input)).toEqual(expected);
});
});

describe('given a single context with private attributes', () => {
const input = Context.fromLDContext({
kind: 'org',
key: 'testKey',
name: 'testName',
cat: 'calico',
dog: 'lab',
anonymous: true,
_meta: {
privateAttributes: ['/a/b/c', 'cat', 'dog'],
},
});

const expected = {
kind: 'org',
key: 'testKey',
name: 'testName',
cat: 'calico',
dog: 'lab',
anonymous: true,
_meta: {
privateAttributes: ['/a/b/c', 'cat', 'dog'],
},
};

it('it can convert from LDContext to Context and back to LDContext', () => {
expect(Context.toLDContext(input)).toEqual(expected);
});
});

describe('given a single context without meta', () => {
const input = Context.fromLDContext({
kind: 'org',
key: 'testKey',
name: 'testName',
cat: 'calico',
dog: 'lab',
anonymous: true,
});

const expected = {
kind: 'org',
key: 'testKey',
name: 'testName',
cat: 'calico',
dog: 'lab',
anonymous: true,
};

it('it can convert from LDContext to Context and back to LDContext', () => {
expect(Context.toLDContext(input)).toEqual(expected);
});
});

describe('given a multi context', () => {
const input = Context.fromLDContext({
kind: 'multi',
org: {
key: 'testKey',
name: 'testName',
cat: 'calico',
dog: 'lab',
anonymous: true,
_meta: {
privateAttributes: ['/a/b/c', 'cat', 'custom/dog'],
},
},
customer: {
key: 'testKey',
name: 'testName',
bird: 'party parrot',
chicken: 'hen',
},
});

const expected = {
kind: 'multi',
org: {
key: 'testKey',
name: 'testName',
cat: 'calico',
dog: 'lab',
anonymous: true,
_meta: {
privateAttributes: ['/a/b/c', 'cat', 'custom/dog'],
},
},
customer: {
key: 'testKey',
name: 'testName',
bird: 'party parrot',
chicken: 'hen',
},
};

it('it can convert from LDContext to Context and back to LDContext', () => {
expect(Context.toLDContext(input)).toEqual(expected);
});
});
30 changes: 30 additions & 0 deletions packages/shared/common/src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ function legacyToSingleKind(user: LDUser): LDSingleKindContext {
if (user.country !== null && user.country !== undefined) {
singleKindContext.country = user.country;
}
if (user.privateAttributeNames !== null && user.privateAttributeNames !== undefined) {
singleKindContext._meta = {
privateAttributes: user.privateAttributeNames,
};
}

// We are not pulling private attributes over because we will serialize
// those from attribute references for events.
Expand Down Expand Up @@ -338,6 +343,31 @@ export default class Context {
return Context.contextForError('unknown', 'Context was not of a valid kind');
}

/**
* Creates a {@link LDContext} from a {@link Context}.
* @param context to be converted
* @returns an {@link LDContext} if input was valid, otherwise undefined
*/
public static toLDContext(context: Context): LDContext | undefined {
if (!context.valid) {
return undefined;
}

const contexts = context.getContexts();
if (!context.isMulti) {
return contexts[0][1];
}
const result: LDMultiKindContext = {
kind: 'multi',
};
contexts.forEach((kindAndContext) => {
const kind = kindAndContext[0];
const nestedContext = kindAndContext[1];
result[kind] = nestedContext;
});
return result;
}

/**
* Attempt to get a value for the given context kind using the given reference.
* @param reference The reference to the value to get.
Expand Down
22 changes: 22 additions & 0 deletions packages/shared/common/src/api/platform/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/**
* Interface for a data store that holds feature flag data and other SDK
* properties in a serialized form.
*
* This interface should be used for platform-specific integrations that store
* data somewhere other than in memory. Each data item is uniquely identified by
* a string typically constructed following a namespacing structure that
* is then encoded.
*
* Implementations may not throw exceptions.
*
* The SDK assumes that the persistence is only being used by a single instance
* of the SDK per SDK key (two different SDK instances, with 2 different SDK
* keys could use the same persistence instance).
*
* The SDK, with correct usage, will not have overlapping writes to the same
* key.
*
* This interface does not depend on the ability to list the contents of the
* store or namespaces. This is to maintain the simplicity of implementing a
* key-value store on many platforms.
*/
export interface Storage {
get: (key: string) => Promise<string | null>;
set: (key: string, value: string) => Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/mocks/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export let hasher: Hasher;
export const setupCrypto = () => {
let counter = 0;
hasher = {
update: jest.fn(),
update: jest.fn(() => hasher),
digest: jest.fn(() => '1234567890123456'),
};

Expand Down
2 changes: 0 additions & 2 deletions packages/shared/sdk-client/src/LDClientImpl.events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { InputCustomEvent, InputIdentifyEvent } from '@launchdarkly/js-sdk-common/dist/internal';
import {
basicPlatform,
hasher,
logger,
MockEventProcessor,
setupMockStreamingProcessor,
Expand Down Expand Up @@ -59,7 +58,6 @@ describe('sdk-client object', () => {
);
setupMockStreamingProcessor(false, defaultPutResponse);
basicPlatform.crypto.randomUUID.mockReturnValue('random1');
hasher.digest.mockReturnValue('digested1');

ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, basicPlatform, {
logger,
Expand Down
Loading