Skip to content

Commit 30866f3

Browse files
authored
feat: JSR support for Cloudflare SDK. (#415)
1 parent e138b33 commit 30866f3

File tree

11 files changed

+58
-18
lines changed

11 files changed

+58
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ yarn-error.log
2020
.DS_Store
2121
.vscode
2222
dump.rdb
23+
.wrangler

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ coverage
1010
.vscode
1111
**/*/CHANGELOG.md
1212
packages/sdk/akamai-edgekv/src/edgekv/edgekv.js
13+
packages/sdk/cloudflare/src/createPlatformInfo.ts

.yarnrc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ nodeLinker: node-modules
22

33
npmPublishAccess: public
44

5+
npmScopes:
6+
jsr:
7+
npmRegistryServer: 'https://npm.jsr.io'
8+
59
plugins:
610
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
711
spec: '@yarnpkg/plugin-workspace-tools'

packages/sdk/cloudflare/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ For more information, see the [complete reference guide for this SDK](https://do
1515
## Install
1616

1717
```shell
18+
# npm
1819
npm i @launchdarkly/cloudflare-server-sdk
20+
21+
# yarn
22+
yarn add @launchdarkly/cloudflare-server-sdk
23+
24+
# jsr
25+
npx jsr add @launchdarkly/cloudflare-server-sdk
1926
```
2027

2128
Then turn on the Node.js compatibility flag in your `wrangler.toml`. This allows the SDK to use `node:events`:
@@ -24,6 +31,10 @@ Then turn on the Node.js compatibility flag in your `wrangler.toml`. This allows
2431
compatibility_flags = [ "nodejs_compat" ]
2532
```
2633

34+
## Additional JSR setup
35+
36+
If you want to install this package as a JSR package, you will need to use [`esbuild` version >= 19.7](https://github.com/evanw/esbuild/releases/tag/v0.19.7) to enable support for `import attributes`.
37+
2738
## Quickstart
2839

2940
Initialize the ldClient with your client side sdk key and the [Cloudflare KV namespace](https://developers.cloudflare.com/workers/runtime-apis/kv#kv-bindings):
@@ -33,12 +44,12 @@ import { init as initLD } from '@launchdarkly/cloudflare-server-sdk';
3344

3445
export default {
3546
async fetch(request: Request, env: Bindings): Promise<Response> {
36-
const sdkKey = 'test-sdk-key';
47+
const clientSideID = 'test-client-side-id';
3748
const flagKey = 'testFlag1';
3849
const context = { kind: 'user', key: 'test-user-key-1' };
3950

4051
// init the ldClient, wait and finally evaluate
41-
const client = initLD(sdkKey, env.LD_KV);
52+
const client = initLD(clientSideID, env.LD_KV);
4253
await client.waitForInitialization();
4354
const flagValue = await client.variation(flagKey, context, false);
4455

packages/sdk/cloudflare/example/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,26 @@ yarn && yarn build
2525
kv_namespaces = [{ binding = "LD_KV", id = "YOUR_KV_ID", preview_id = "YOUR_PREVIEW_KV_ID" }]
2626
```
2727

28-
3. Insert test data to the preview environment:
28+
3. Insert test data to the preview environment. You must use your own clientSideID and prefix it with `LD-Env-`. In the example below, the clientSideID is `test-client-side-id`. Internally, the Cloudflare SDK uses this `LD-Env-` namespace to distinguish LaunchDarkly data from others.
2929

3030
```shell
31-
# The Cloudflare SDK automatically adds the "LD-Env-" prefix to your sdk key
32-
npx wrangler kv:key put --binding=LD_KV "LD-Env-test-sdk-key" --path ./src/testData.json --preview
31+
npx wrangler kv:key put --binding=LD_KV "LD-Env-test-client-side-id" --path ./src/testData.json --preview
3332
```
3433

3534
4. View that test data to ensure it's present:
3635

3736
```shell
38-
npx wrangler kv:key get --binding=LD_KV "LD-Env-test-sdk-key" --preview
37+
npx wrangler kv:key get --binding=LD_KV "LD-Env-test-client-side-id" --preview
3938
```
4039

41-
5. Finally:
40+
5. Edit [index.ts](https://github.com/launchdarkly/js-core/blob/main/packages/sdk/cloudflare/example/src/index.ts#L6) to use your clientSideID and a valid flag key from the test data you just inserted.
41+
42+
```ts
43+
const clientSideID = 'test-client-side-id';
44+
const flagKey = 'test-boolean-flag';
45+
```
46+
47+
6. Finally:
4248

4349
```shell
4450
yarn start

packages/sdk/cloudflare/example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"devDependencies": {
1111
"@cloudflare/workers-types": "^4.20230321.0",
1212
"@types/jest": "^29.5.5",
13-
"esbuild": "^0.14.41",
13+
"esbuild": "^0.20.2",
1414
"jest": "^29.7.0",
1515
"jest-environment-miniflare": "^2.5.0",
1616
"miniflare": "^2.5.0",
17-
"prettier": "^2.6.2",
17+
"prettier": "^3.2.5",
1818
"ts-jest": "^28.0.3",
1919
"typescript": "5.1.6",
2020
"wrangler": "2.20.2"

packages/sdk/cloudflare/example/src/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('test', () => {
1616
};
1717
env = getMiniflareBindings();
1818
const { LD_KV } = env;
19-
await LD_KV.put('LD-Env-test-sdk-key', JSON.stringify(testData));
19+
await LD_KV.put('LD-Env-test-client-side-id', JSON.stringify(testData));
2020
});
2121

2222
afterEach(() => {

packages/sdk/cloudflare/jsr.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@launchdarkly/cloudflare-server-sdk",
3+
"version": "0.0.3",
4+
"exports": "./src/index.ts",
5+
"publish": {
6+
"include": ["LICENSE", "README.md", "package.json", "jsr.json", "src/**/*.ts"],
7+
"exclude": ["src/**/*.test.ts"]
8+
}
9+
}

packages/sdk/cloudflare/src/createPlatformInfo.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { Info, PlatformData, SdkData } from '@launchdarkly/js-server-sdk-common-edge';
22

3-
import { name, version } from '../package.json';
3+
// @ts-ignore
4+
// eslint-disable-next-line prettier/prettier
5+
import * as packageJson from '../package.json' assert { type: "json" }
6+
7+
const { name, version } = packageJson
48

59
class CloudflarePlatformInfo implements Info {
610
platformData(): PlatformData {
@@ -18,6 +22,6 @@ class CloudflarePlatformInfo implements Info {
1822
}
1923
}
2024

21-
const createPlatformInfo = () => new CloudflarePlatformInfo();
25+
const createPlatformInfo = (): CloudflarePlatformInfo => new CloudflarePlatformInfo()
2226

2327
export default createPlatformInfo;

packages/sdk/cloudflare/src/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ const mf = new Miniflare({
1212
kvNamespaces: ['TEST_NAMESPACE'],
1313
});
1414

15-
const sdkKey = 'test-sdk-key';
15+
const clientSideID = 'test-client-side-id';
1616
const flagKey1 = 'testFlag1';
1717
const flagKey2 = 'testFlag2';
1818
const flagKey3 = 'testFlag3';
1919
const context: LDContext = { kind: 'user', key: 'test-user-key-1' };
2020
const namespace = 'LD_KV';
21-
const rootEnvKey = `LD-Env-${sdkKey}`;
21+
const rootEnvKey = `LD-Env-${clientSideID}`;
2222

2323
describe('init', () => {
2424
let kv: KVNamespace;
@@ -27,7 +27,7 @@ describe('init', () => {
2727
beforeAll(async () => {
2828
kv = (await mf.getKVNamespace(namespace)) as unknown as KVNamespace;
2929
await kv.put(rootEnvKey, JSON.stringify(allFlagsSegments));
30-
ldClient = init(sdkKey, kv);
30+
ldClient = init(clientSideID, kv);
3131
await ldClient.waitForInitialization();
3232
});
3333

0 commit comments

Comments
 (0)