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
28 changes: 24 additions & 4 deletions packages/sdk/cloudflare/src/createFeatureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const createFeatureStore = (kvNamespace: KVNamespace, sdkKey: string, logger: LD
const store: LDFeatureStore = {
get(
kind: DataKind,
flagKey: string,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't just flags.

dataKey: string,
callback: (res: LDFeatureStoreItem | null) => void = noop
): void {
const kindKey = kind.namespace === 'features' ? 'flags' : kind.namespace;
logger.debug(`Requesting ${flagKey} from ${key}.${kindKey}`);
logger.debug(`Requesting ${dataKey} from ${key}.${kindKey}`);

kvNamespace
.get(key)
Expand All @@ -32,7 +32,17 @@ const createFeatureStore = (kvNamespace: KVNamespace, sdkKey: string, logger: LD
if (!item) {
throw new Error(`Error deserializing ${kindKey}`);
}
callback(item[kindKey][flagKey]);
switch (kind.namespace) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't super elegant, but lets us keep the types the same, and we can make it more elegant in the next update.

case 'features':
callback(item.flags[dataKey]);
break;
case 'segments':
callback(item.segments[dataKey]);
break;
default:
// Unsupported data kind.
callback(null);
}
})
.catch((err) => {
logger.error(err);
Expand All @@ -54,7 +64,17 @@ const createFeatureStore = (kvNamespace: KVNamespace, sdkKey: string, logger: LD
throw new Error(`Error deserializing ${kindKey}`);
}

callback(item[kindKey]);
switch (kind.namespace) {
case 'features':
callback(item.flags);
break;
case 'segments':
callback(item.segments);
break;
default:
// Unsupported data kind.
callback({});
}
})
.catch((err) => {
logger.error(err);
Expand Down
9 changes: 8 additions & 1 deletion packages/shared/sdk-server/src/store/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function reviver(this: any, key: string, value: any): any {
}

interface FlagsAndSegments {
[key: string]: { [name: string]: Flag } | { [name: string]: Segment };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlagsAndSegments isn't intended to be addressed by strings. In places where it isn't flags or segments it would generally be an error. So, this is the main thing I was concerned about.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe long term DataKind needs to be flags | segments instead of string. But this is out of scope and out of time for my ambitions now.

export interface DataKind {
  namespace: 'flags' | 'segments';
}

flags: { [name: string]: Flag };
segments: { [name: string]: Segment };
}
Expand Down Expand Up @@ -158,6 +157,14 @@ export function deserializeAll(data: string): AllData | undefined {
return parsed;
}

/**
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because this isn't a permanent solution, and isn't a function we want a customer to use.

* This function is intended for usage inside LaunchDarkly SDKs.
* This function should NOT be used by customer applications.
* This function may be changed or removed without a major version.
*
* @param data String data from launchdarkly.
* @returns The parsed and processed data.
*/
export function deserializePoll(data: string): FlagsAndSegments | undefined {
const parsed = tryParse(data) as FlagsAndSegments;

Expand Down