Skip to content

Instantly share code, notes, and snippets.

@remotesynth
Last active June 6, 2023 13:15
Show Gist options
  • Save remotesynth/c308dff55252bbc4e98500eff9d3433a to your computer and use it in GitHub Desktop.
Save remotesynth/c308dff55252bbc4e98500eff9d3433a to your computer and use it in GitHub Desktop.
Cloudflare Worker with LaunchDarkly integration
import { getAssetFromKV } from "@cloudflare/kv-asset-handler";
const { init } = require("launchdarkly-cloudflare-edge-sdk");
let ldClient;
class FlagsStateInjector {
async element(element) {
// fetch all flag values for client-side SDKs as evaluated for an anonymous user
// use a more appropriate user key if needed
const user = {
key: "anonymous",
kind: "user"
};
const allFlags = await ldClient.allFlagsState(user, {
clientSideOnly: true,
});
element.append(
`<script>window.ldFlags = ${JSON.stringify(allFlags)}</script>`,
{ html: true }
);
}
}
class H1ElementHandler {
async element(element) {
// replace the header text with the value of a string flag
const headerText = await getFlagValue("header-text");
element.setInnerContent(headerText);
}
}
const rewriter = new HTMLRewriter();
rewriter.on("h1", new H1ElementHandler());
rewriter.on("head", new FlagsStateInjector());
addEventListener("fetch", (event) => {
event.respondWith(handleEvent(event));
});
async function handleEvent(event) {
let options = {};
try {
if (!ldClient) {
ldClient = init(MY_KV, "MY_KEY");
await ldClient.waitForInitialization();
}
const page = await getAssetFromKV(event, options);
// allow headers to be altered
const response = new Response(page.body, page);
const customHeader = await getFlagValue("custom-response-headers");
customHeader.headers.forEach((header) => {
response.headers.set(header.name, header.value);
});
return rewriter.transform(response);
} catch (e) {
console.log(e);
return new Response(e.message || e.toString(), { status: 500 });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment