Skip to content
Draft
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
31 changes: 5 additions & 26 deletions packages/adapter-launchdarkly/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
type LDClient,
type LDContext,
} from '@launchdarkly/vercel-server-sdk';
import { createClient, type EdgeConfigClient } from '@vercel/edge-config';
import { AsyncLocalStorage } from 'async_hooks';
import { createClient } from '@vercel/edge-config';
import type { Adapter } from 'flags';
import { createCachedEdgeConfigClient as cacheEdgeConfigClient } from 'flags/utils';

export { getProviderData } from './provider';
export type { LDContext };
Expand Down Expand Up @@ -46,31 +46,10 @@ export function createLaunchDarklyAdapter({
edgeConfigConnectionString: string;
}): AdapterResponse {
const edgeConfigClient = createClient(edgeConfigConnectionString);

const store = new AsyncLocalStorage<WeakKey>();
const cache = new WeakMap<WeakKey, Promise<unknown>>();

const patchedEdgeConfigClient: EdgeConfigClient = {
...edgeConfigClient,
get: async <T>(key: string) => {
const h = store.getStore();
if (h) {
const cached = cache.get(h);
if (cached) {
return cached as Promise<T>;
}
}

const promise = edgeConfigClient.get<T>(key);
if (h) cache.set(h, promise);

return promise;
},
};
const { run, client } = cacheEdgeConfigClient(edgeConfigClient);

let initPromise: Promise<unknown> | null = null;

const ldClient = init(clientSideId, patchedEdgeConfigClient);
const ldClient = init(clientSideId, client);

function origin(key: string) {
return `https://app.launchdarkly.com/projects/${projectSlug}/flags/${key}/`;
Expand All @@ -87,7 +66,7 @@ export function createLaunchDarklyAdapter({
await initPromise;
}

return store.run(
return run(
headers,
() =>
ldClient.variation(
Expand Down
9 changes: 9 additions & 0 deletions packages/flags/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./utils": {
"import": "./dist/utils.js",
"require": "./dist/utils.cjs"
},
"./next": {
"import": "./dist/next.js",
"require": "./dist/next.cjs"
Expand All @@ -50,6 +54,10 @@
"dist/*.d.ts",
"dist/*.d.cts"
],
"utils": [
"dist/utils.d.ts",
"dist/utils.d.cts"
],
"next": [
"dist/next.d.ts",
"dist/next.d.cts"
Expand Down Expand Up @@ -79,6 +87,7 @@
},
"dependencies": {
"@edge-runtime/cookies": "^5.0.2",
"@vercel/edge-config": "^1.4.0",
"jose": "^5.2.1"
},
"devDependencies": {
Expand Down
59 changes: 59 additions & 0 deletions packages/flags/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/** This file is an entry point for flags/utils, so its exports are public */
import type { EdgeConfigClient } from '@vercel/edge-config';
import { AsyncLocalStorage } from 'async_hooks';

/**
* Returns a patched version of the EdgeConfigClient that cache reads
* for the duration of a request.
*
* Uses the headers as a cache key.
*/
export function createCachedEdgeConfigClient(
edgeConfigClient: EdgeConfigClient,
): {
/**
* A patched version of the Edge Config client, which only
* reads Edge Config once per request and caches the result
* for the duration of the request.
*/
client: EdgeConfigClient;
run: <R>(headers: HeadersInit, fn: () => R) => R;
} {
const store = new AsyncLocalStorage<WeakKey>();
const cache = new WeakMap<WeakKey, Promise<unknown>>();

const patchedEdgeConfigClient: EdgeConfigClient = {
...edgeConfigClient,
get: async <T>(key: string) => {
const h = store.getStore();
if (h) {
const cached = cache.get(h);
if (cached) {
return cached as Promise<T>;
}
}

const promise = edgeConfigClient.get<T>(key);
if (h) cache.set(h, promise);

return promise;
},
};

return {
client: patchedEdgeConfigClient,
// The "run" function puts the headers into the AsyncLocalStorage, which
// allows the patchedEdgeConfigClient to read the headers without otherwise
// having access to them.
//
// The patchedEdgeConfigClient then uses the headers as a cache key to
// deduplicate Edge Config reads for the duration of a request.
//
// Performance wise it would actually be fine to read Edge Config on every
// flag evaluation, but this would also cause a lot of unnecessary reads,
// and as Edge Config is charged per read, this would cause unnecessary charges.
//
// So this approach helps with performance and ensures a fair usage of Edge Config.
run: store.run.bind(store),
};
}
1 change: 1 addition & 0 deletions packages/flags/tsup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const defaultConfig = {
export default defineConfig({
entry: {
index: 'src/index.ts',
utils: 'src/utils.ts',
next: 'src/next/index.ts',
sveltekit: 'src/sveltekit/index.ts',
react: 'src/react/index.tsx',
Expand Down
32 changes: 18 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading