Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
31b96cc
Create jsr.json
yusinto Mar 21, 2024
c39756b
fix: Added explicit return types.
yusinto Mar 22, 2024
ca2cf35
Merge branch 'main' into yus/sc-236785/investigate-publishing-to-jsr
yusinto Mar 22, 2024
05353a3
fix: Include jsr.json when publishing.
yusinto Mar 22, 2024
0303885
Merge branch 'main' into yus/sc-236785/investigate-publishing-to-jsr
yusinto Mar 26, 2024
cde1ba4
fix: Modify package.json import to resolve esbuild error. Update esbu…
yusinto Mar 28, 2024
2c305ca
chore: Added jsr registry to npmrc and yarnrc.
yusinto Mar 28, 2024
fba552a
Update .prettierrc
yusinto Mar 28, 2024
af96d82
Update .gitignore
yusinto Mar 28, 2024
8f06f60
Update jsr.json
yusinto Mar 28, 2024
5e014f8
Update package.json
yusinto Mar 28, 2024
4c20848
Merge branch 'main' into yus/sc-236785/investigate-publishing-to-jsr
yusinto Apr 1, 2024
e588616
fix: Prettier ignore import assert.
yusinto Apr 1, 2024
cc7f0dc
fix: ts-ignore for import assertion because ts-jest errors.
yusinto Apr 1, 2024
b416c41
chore: Corrected example sdk key. Updated example README.
yusinto Apr 1, 2024
117363c
chore: Added jsr instructions.
yusinto Apr 1, 2024
a7d7dae
chore: Reverted example cf sdk version.
yusinto Apr 1, 2024
4fdab2a
chore: Minor readme and version updates after testing.
yusinto Apr 1, 2024
55feca3
Delete .npmrc
yusinto Apr 1, 2024
9c9ca3c
chore: Remove trailingCommas setting because its redundant.
yusinto Apr 1, 2024
4b29910
Update package.json
yusinto Apr 1, 2024
f2cbc4f
chore: Replace all instances of sdkKey with clientSideID.
yusinto Apr 1, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ yarn-error.log
.DS_Store
.vscode
dump.rdb
.wrangler
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage
.vscode
**/*/CHANGELOG.md
packages/sdk/akamai-edgekv/src/edgekv/edgekv.js
packages/sdk/cloudflare/src/createPlatformInfo.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trivago/prettier-plugin-sort-imports doesn't like the new-ish import assertion syntax. See my comments here.

4 changes: 4 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ nodeLinker: node-modules

npmPublishAccess: public

npmScopes:
jsr:
npmRegistryServer: 'https://npm.jsr.io'

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: '@yarnpkg/plugin-workspace-tools'
Expand Down
15 changes: 13 additions & 2 deletions packages/sdk/cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ For more information, see the [complete reference guide for this SDK](https://do
## Install

```shell
# npm
npm i @launchdarkly/cloudflare-server-sdk

# yarn
yarn add @launchdarkly/cloudflare-server-sdk

# jsr
npx jsr add @launchdarkly/cloudflare-server-sdk
```

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

## Additional JSR setup

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`.

## Quickstart

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

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

// init the ldClient, wait and finally evaluate
const client = initLD(sdkKey, env.LD_KV);
const client = initLD(clientSideID, env.LD_KV);
await client.waitForInitialization();
const flagValue = await client.variation(flagKey, context, false);

Expand Down
16 changes: 11 additions & 5 deletions packages/sdk/cloudflare/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,26 @@ yarn && yarn build
kv_namespaces = [{ binding = "LD_KV", id = "YOUR_KV_ID", preview_id = "YOUR_PREVIEW_KV_ID" }]
```

3. Insert test data to the preview environment:
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.

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

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

```shell
npx wrangler kv:key get --binding=LD_KV "LD-Env-test-sdk-key" --preview
npx wrangler kv:key get --binding=LD_KV "LD-Env-test-client-side-id" --preview
```

5. Finally:
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.

```ts
const clientSideID = 'test-client-side-id';
const flagKey = 'test-boolean-flag';
```

6. Finally:

```shell
yarn start
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/cloudflare/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"devDependencies": {
"@cloudflare/workers-types": "^4.20230321.0",
"@types/jest": "^29.5.5",
"esbuild": "^0.14.41",
"esbuild": "^0.20.2",
"jest": "^29.7.0",
"jest-environment-miniflare": "^2.5.0",
"miniflare": "^2.5.0",
"prettier": "^2.6.2",
"prettier": "^3.2.5",
"ts-jest": "^28.0.3",
"typescript": "5.1.6",
"wrangler": "2.20.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/cloudflare/example/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('test', () => {
};
env = getMiniflareBindings();
const { LD_KV } = env;
await LD_KV.put('LD-Env-test-sdk-key', JSON.stringify(testData));
await LD_KV.put('LD-Env-test-client-side-id', JSON.stringify(testData));
});

afterEach(() => {
Expand Down
9 changes: 9 additions & 0 deletions packages/sdk/cloudflare/jsr.json
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the jsr equivalent of package.json. It's used during publishing. The version is hardcoded atm, but I'll submit a separate PR to implement publishing through release-please.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@launchdarkly/cloudflare-server-sdk",
"version": "0.0.3",
Copy link
Member

Choose a reason for hiding this comment

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

Release please will need to be able to update this. If it is fine for it to re-write the file each time, then you can use the JSON updater.

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be like this.

{
  "extra-files": [
    {
      "type": "json",
      "path": "jsr.json",
      "jsonpath": "$.version"
    }
  ]
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right. I was going to submit a separate PR for the RP work. Maybe i'll just do it together in this PR now that you mentioned it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will do this in a separate pr focussed on publishing in CI.

"exports": "./src/index.ts",
"publish": {
"include": ["LICENSE", "README.md", "package.json", "jsr.json", "src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
}
}
8 changes: 6 additions & 2 deletions packages/sdk/cloudflare/src/createPlatformInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { Info, PlatformData, SdkData } from '@launchdarkly/js-server-sdk-common-edge';

import { name, version } from '../package.json';
// @ts-ignore
// eslint-disable-next-line prettier/prettier
import * as packageJson from '../package.json' assert { type: "json" }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This import assertion is new-ish and is not yet fully supported by jest and prettier. I took a shortcut here and ignore this for now to save time. Hopefully in time we can just remove these ignores.


const { name, version } = packageJson

class CloudflarePlatformInfo implements Info {
platformData(): PlatformData {
Expand All @@ -18,6 +22,6 @@ class CloudflarePlatformInfo implements Info {
}
}

const createPlatformInfo = () => new CloudflarePlatformInfo();
const createPlatformInfo = (): CloudflarePlatformInfo => new CloudflarePlatformInfo()

export default createPlatformInfo;
6 changes: 3 additions & 3 deletions packages/sdk/cloudflare/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const mf = new Miniflare({
kvNamespaces: ['TEST_NAMESPACE'],
});

const sdkKey = 'test-sdk-key';
const clientSideID = 'test-client-side-id';
const flagKey1 = 'testFlag1';
const flagKey2 = 'testFlag2';
const flagKey3 = 'testFlag3';
const context: LDContext = { kind: 'user', key: 'test-user-key-1' };
const namespace = 'LD_KV';
const rootEnvKey = `LD-Env-${sdkKey}`;
const rootEnvKey = `LD-Env-${clientSideID}`;

describe('init', () => {
let kv: KVNamespace;
Expand All @@ -27,7 +27,7 @@ describe('init', () => {
beforeAll(async () => {
kv = (await mf.getKVNamespace(namespace)) as unknown as KVNamespace;
await kv.put(rootEnvKey, JSON.stringify(allFlagsSegments));
ldClient = init(sdkKey, kv);
ldClient = init(clientSideID, kv);
await ldClient.waitForInitialization();
});

Expand Down
10 changes: 7 additions & 3 deletions packages/sdk/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
BasicLogger,
EdgeFeatureStore,
init as initEdge,
LDClient,
LDOptions,
type LDClient,
type LDOptions,
} from '@launchdarkly/js-server-sdk-common-edge';

import createPlatformInfo from './createPlatformInfo';
Expand Down Expand Up @@ -45,7 +45,11 @@ export type { LDClient };
* @return
* The new {@link LDClient} instance.
*/
export const init = (clientSideID: string, kvNamespace: KVNamespace, options: LDOptions = {}) => {
export const init = (
clientSideID: string,
kvNamespace: KVNamespace,
options: LDOptions = {},
): LDClient => {
const logger = options.logger ?? BasicLogger.get();
return initEdge(clientSideID, createPlatformInfo(), {
featureStore: new EdgeFeatureStore(kvNamespace, clientSideID, 'Cloudflare', logger),
Expand Down