Skip to content

Commit b77aae9

Browse files
authored
Add withLaunchDarkly helper function (#848)
* withLaunchDarkly * changeset * refactor
1 parent d101dfe commit b77aae9

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

.changeset/cold-taxes-hunt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/adaptive': minor
3+
---
4+
5+
Add `withLaunchDarkly` helper function

bun.lock

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,13 @@
568568
"@gitbook/tsconfig": "*",
569569
"@types/js-cookie": "^3.0.6",
570570
"esbuild": "^0.25.3",
571+
"launchdarkly-js-client-sdk": "^3.8.1",
571572
"typescript": "^5.6.2",
572573
},
573574
},
574575
"packages/api": {
575576
"name": "@gitbook/api",
576-
"version": "0.117.0",
577+
"version": "0.119.0",
577578
"dependencies": {
578579
"event-iterator": "^2.0.0",
579580
"eventsource-parser": "^3.0.0",
@@ -588,7 +589,7 @@
588589
},
589590
"packages/cli": {
590591
"name": "@gitbook/cli",
591-
"version": "0.23.0",
592+
"version": "0.24.0",
592593
"bin": {
593594
"gitbook": "./cli.js",
594595
},
@@ -1594,7 +1595,7 @@
15941595

15951596
"fast-content-type-parse": ["[email protected]", "", {}, "sha512-nGqtvLrj5w0naR6tDPfB4cUmYCqouzyQiz6C5y/LtcDllJdrcc6WaWW6iXyIIOErTa/XRybj28aasdn4LkVk6Q=="],
15961597

1597-
"fast-deep-equal": ["fast-deep-equal@3.1.3", "", {}, "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="],
1598+
"fast-deep-equal": ["fast-deep-equal@2.0.1", "", {}, "sha512-bCK/2Z4zLidyB4ReuIsvALH6w31YfAQDmXMqMx6FyfHqvBxtjC0eRumeSu4Bs3XtXwpyIywtSTrVT99BxY1f9w=="],
15981599

15991600
"fast-glob": ["[email protected]", "", { "dependencies": { "@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.walk": "^1.2.3", "glob-parent": "^5.1.2", "merge2": "^1.3.0", "micromatch": "^4.0.8" } }, "sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg=="],
16001601

@@ -1862,6 +1863,10 @@
18621863

18631864
"kleur": ["[email protected]", "", {}, "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w=="],
18641865

1866+
"launchdarkly-js-client-sdk": ["[email protected]", "", { "dependencies": { "escape-string-regexp": "^4.0.0", "launchdarkly-js-sdk-common": "5.7.1" } }, "sha512-Y05FXM8FAXAMbbJqeI+ffr6a4m2M/TBUccgI9ejWPSxQS+/b2t+FBWZzfmc7wXuOOYzgGkpHHfQ6bFDU9NKPWQ=="],
1867+
1868+
"launchdarkly-js-sdk-common": ["[email protected]", "", { "dependencies": { "base64-js": "^1.3.0", "fast-deep-equal": "^2.0.1", "uuid": "^8.0.0" } }, "sha512-RFFeoYVL764zarFpU16lDt1yHzUCt0rnYYKlX5LLtZ5Nhq+2fzE33xRolP/sjxAYVInD0o5z6jKTlDe8gtcDYg=="],
1869+
18651870
"lines-and-columns": ["[email protected]", "", {}, "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg=="],
18661871

18671872
"listr2": ["[email protected]", "", { "dependencies": { "cli-truncate": "^2.1.0", "colorette": "^2.0.16", "log-update": "^4.0.0", "p-map": "^4.0.0", "rfdc": "^1.3.0", "rxjs": "^7.5.5", "through": "^2.3.8", "wrap-ansi": "^7.0.0" }, "peerDependencies": { "enquirer": ">= 2.3.0 < 3" }, "optionalPeers": ["enquirer"] }, "sha512-juGHV1doQdpNT3GSTs9IUN43QJb7KHdF9uqg7Vufs/tG9VTzpFphqF4pm/ICdAABGQxsyNn9CiYA3StkI6jpwA=="],
@@ -2352,6 +2357,8 @@
23522357

23532358
"util-deprecate": ["[email protected]", "", {}, "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="],
23542359

2360+
"uuid": ["[email protected]", "", { "bin": { "uuid": "dist/bin/uuid" } }, "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="],
2361+
23552362
"validate-npm-package-name": ["[email protected]", "", {}, "sha512-OljLrQ9SQdOUqTaQxqL5dEfZWrXExyyWsozYlAWFawPVNuD83igl7uJD2RTkNMbniIYgt8l81eCJGIdQF7avLQ=="],
23562363

23572364
"wcwidth": ["[email protected]", "", { "dependencies": { "defaults": "^1.0.3" } }, "sha512-XHPEwS0q6TaxcvG85+8EYkbiCux2XtWG2mkc47Ng2A77BQu9+DqIOJldST4HgPkuea7dvKSj5VgX3P1d4rW8Tg=="],
@@ -2556,6 +2563,8 @@
25562563

25572564
"@whatwg-node/fetch/urlpattern-polyfill": ["[email protected]", "", {}, "sha512-H/A06tKD7sS1O1X2SshBVeA5FLycRpjqiBeqGKmBwBDBy28EnRjORxTNe269KSSr5un5qyWi1iL61wLxpd+ZOg=="],
25582565

2566+
"ajv/fast-deep-equal": ["[email protected]", "", {}, "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="],
2567+
25592568
"ansi-escapes/type-fest": ["[email protected]", "", {}, "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w=="],
25602569

25612570
"anymatch/picomatch": ["[email protected]", "", {}, "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="],

packages/adaptive/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@gitbook/tsconfig": "*",
1616
"typescript": "^5.6.2",
1717
"@bucketco/browser-sdk": "^3.1.2",
18+
"launchdarkly-js-client-sdk": "^3.8.1",
1819
"@types/js-cookie": "^3.0.6",
1920
"esbuild": "^0.25.3"
2021
},

packages/adaptive/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './bucket';
2+
export * from './launchdarkly';
23
export { writeGitBookVisitorCookie } from './utils';

packages/adaptive/src/launchdarkly.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { writeGitBookVisitorCookie } from './utils';
2+
import type { LDClient } from 'launchdarkly-js-client-sdk';
3+
4+
/**
5+
* Sets a client-side session cookie with the features enabled in the Launchdarkly client.
6+
*/
7+
export function withLaunchDarkly(client: LDClient): () => void {
8+
const handler = () => {
9+
const features: Record<string, boolean> = {};
10+
// Check features enabled in the client
11+
for (const [key, value] of Object.entries(client.allFlags())) {
12+
features[key] = value;
13+
}
14+
15+
writeGitBookVisitorCookie('launchdarkly', { launchdarkly: features });
16+
};
17+
18+
// Determine if we're in a browser environment
19+
const isBrowser = typeof window !== 'undefined';
20+
21+
if (!isBrowser) {
22+
console.warn('withLaunchDarkly was called in a non-browser environment');
23+
return () => {};
24+
}
25+
26+
// Always try to call handler immediately in case client is already initialized
27+
// This is safe even if the client isn't ready yet
28+
try {
29+
handler();
30+
} catch (error) {
31+
// Ignore errors if client isn't ready yet
32+
console.debug('LaunchDarkly client not ready yet, will wait for initialization');
33+
}
34+
35+
const cleanup: Array<() => void> = [];
36+
37+
const onReadyOrLoad = () => {
38+
client.on('initialized', handler);
39+
cleanup.push(() => {
40+
client.off('initialized', handler);
41+
});
42+
};
43+
44+
// Add event listener for when the DOM is ready
45+
if (document.readyState === 'interactive' || document.readyState === 'complete') {
46+
onReadyOrLoad();
47+
} else {
48+
document.addEventListener('DOMContentLoaded', onReadyOrLoad);
49+
cleanup.push(() => {
50+
document.removeEventListener('DOMContentLoaded', onReadyOrLoad);
51+
});
52+
}
53+
54+
// Add event listeners for feature updates
55+
client.on('change', handler);
56+
cleanup.push(() => {
57+
client.off('change', handler);
58+
});
59+
60+
// Return cleanup function
61+
return () => {
62+
cleanup.forEach((fn) => fn());
63+
};
64+
}

0 commit comments

Comments
 (0)