Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
27 changes: 27 additions & 0 deletions .github/workflows/sdk-server-edge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: shared/sdk-server

on:
push:
branches: [main]
paths-ignore:
- '**.md' #Do not need to run CI for markdown changes.
pull_request:
branches: [main]
paths-ignore:
- '**.md'

jobs:
build-test-sdk-server:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: 'https://registry.npmjs.org'
- id: shared
name: Shared CI Steps
uses: ./actions/ci
with:
workspace_name: '@launchdarkly/js-server-sdk-common-edge'
workspace_path: packages/shared/sdk-server-edge
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"workspaces": [
"packages/shared/common",
"packages/shared/sdk-server",
"packages/shared/sdk-server-edge",
"packages/sdk/server-node",
"packages/sdk/cloudflare"
],
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/cloudflare/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@launchdarkly/cloudflare-server-sdk",
"version": "0.0.1",
"description": "LaunchDarkly Server-Side SDK for Cloudflare workers",
"description": "Cloudflare LaunchDarkly SDK",
"packageManager": "[email protected]",
"keywords": [
"launchdarkly",
Expand All @@ -11,22 +11,22 @@
],
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"type": "module",
"files": [
"/dist"
],
"scripts": {
"doc": "../../../scripts/build-doc.sh .",
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 useful for local dev.

"build": "rimraf dist && yarn tsc",
"tsw": "yarn tsc --watch",
"start": "rimraf dist && yarn tsw",
"lint": "eslint . --ext .ts",
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
"test": "NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest --ci --runInBand --coverage",
"check": "yarn prettier && yarn lint && yarn tsc && yarn test"
"check": "yarn prettier && yarn lint && yarn build && yarn test && yarn doc"
},
"dependencies": {
"@cloudflare/workers-types": "^4.20230321.0",
"@launchdarkly/js-server-sdk-common": "0.2.0",
"@launchdarkly/js-server-sdk-common-edge": "0.0.1",
"crypto-js": "^4.1.1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AsyncStoreFacade, LDFeatureStore } from '@launchdarkly/js-server-sdk-common';
import { AsyncStoreFacade, LDFeatureStore } from '@launchdarkly/js-server-sdk-common-edge';
import createFeatureStore from './createFeatureStore';

import mockKV from '../utils/mockKV';
import * as testData from '../utils/testData.json';
import mockKV from './utils/mockKV';
import * as testData from './utils/testData.json';

describe('createFeatureStore', () => {
const sdkKey = 'sdkKey';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { KVNamespace } from '@cloudflare/workers-types';
import type {
import {
DataKind,
LDLogger,
LDFeatureStore,
LDFeatureStoreDataStorage,
LDFeatureStoreItem,
LDFeatureStoreKindData,
} from '@launchdarkly/js-server-sdk-common';
import noop from '../utils/noop';
noop,
} from '@launchdarkly/js-server-sdk-common-edge';

const createFeatureStore = (kvNamespace: KVNamespace, sdkKey: string, logger: LDLogger) => {
const key = `LD-Env-${sdkKey}`;
Expand Down
23 changes: 0 additions & 23 deletions packages/sdk/cloudflare/src/createLDClient/CloudflareImpl.ts

This file was deleted.

This file was deleted.

67 changes: 0 additions & 67 deletions packages/sdk/cloudflare/src/createLDClient/createOptions.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/sdk/cloudflare/src/createLDClient/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Info, PlatformData, SdkData } from '@launchdarkly/js-server-sdk-common';
import type { Info, PlatformData, SdkData } from '@launchdarkly/js-server-sdk-common-edge';

import { name, version } from '../../package.json';
import { name, version } from '../package.json';

export default class CloudflareInfo implements Info {
class CloudflarePlatformInfo implements Info {
platformData(): PlatformData {
return {
name: 'Cloudflare worker',
Expand All @@ -16,3 +16,7 @@ export default class CloudflareInfo implements Info {
};
}
}

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

export default createPlatformInfo;
11 changes: 8 additions & 3 deletions packages/sdk/cloudflare/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { KVNamespace } from '@cloudflare/workers-types';
import { LDClient } from '@launchdarkly/js-server-sdk-common-edge';
import { Miniflare } from 'miniflare';
import { init, LDClient } from './index';
import { init } from './index';
import * as allFlagsSegments from './utils/testData.json';

const mf = new Miniflare({
Expand All @@ -15,17 +16,21 @@ const context = { kind: 'user', key: 'test-user-key-1' };
const namespace = 'LD_KV';
const rootEnvKey = `LD-Env-${sdkKey}`;

describe('worker', () => {
describe('init', () => {
let kv: KVNamespace;
let ldClient: LDClient;

beforeAll(async () => {
kv = (await mf.getKVNamespace(namespace)) as unknown as KVNamespace;
await kv.put(rootEnvKey, JSON.stringify(allFlagsSegments));
ldClient = init(kv, sdkKey);
ldClient = init(sdkKey, kv);
await ldClient.waitForInitialization();
});

afterAll(() => {
ldClient.close();
});

test('variation', async () => {
const flagDetail = await ldClient.variation(flagKey, context, false);
expect(flagDetail).toBeTruthy();
Expand Down
93 changes: 22 additions & 71 deletions packages/sdk/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This is the API reference for the LaunchDarkly Server-Side SDK for Cloudflare.
* This is the API reference for the Cloudflare LaunchDarkly SDK.
*
* In typical usage, you will call {@link init} once at startup time to obtain an instance of
* {@link LDClient}, which provides access to all of the SDK's functionality.
Expand All @@ -9,39 +9,18 @@
* @packageDocumentation
*/
import type { KVNamespace } from '@cloudflare/workers-types';
import type {
LDClient as LDClientCommon,
LDFlagsState,
LDFlagsStateOptions,
LDOptions as LDOptionsCommon,
LDContext,
LDEvaluationDetail,
LDFlagValue,
} from '@launchdarkly/js-server-sdk-common';
import createLDClient from './createLDClient';
import {
BasicLogger,
init as initEdge,
LDClient,
LDOptions,
} from '@launchdarkly/js-server-sdk-common-edge';
import createFeatureStore from './createFeatureStore';
import createPlatformInfo from './createPlatformInfo';

export * from '@launchdarkly/js-server-sdk-common';
export * from '@launchdarkly/js-server-sdk-common-edge';

/**
* The Cloudflare SDK only supports these functions:
* - waitForInitialization
* - variation
* - variationDetail
* - allFlagsState
*/
export type LDClient = Pick<
Omit<LDClientCommon, 'waitForInitialization'>,
'variation' | 'variationDetail' | 'allFlagsState'
> & {
waitForInitialization: () => Promise<LDClient>;
};

/**
* The Cloudflare SDK only supports these options:
* - logger
* - featureStore
*/
export type LDOptions = Pick<LDOptionsCommon, 'logger' | 'featureStore'>;
export type { LDClient };

/**
* Creates an instance of the Cloudflare LaunchDarkly client.
Expand All @@ -55,48 +34,20 @@ export type LDOptions = Pick<LDOptionsCommon, 'logger' | 'featureStore'>;
* this.
*
* @param kvNamespace
* The Cloudflare KV configured with LaunchDarkly.
* The Cloudflare KV configured with LaunchDarkly.
* @param sdkKey
* The client side SDK key. This is only used to query the kvNamespace above,
* The client side SDK key. This is only used to query the kvNamespace above,
* not to connect with LD servers.
* @param options
* Optional configuration settings. The only supported options for the Cloudflare SDK
* are 'logger' and 'featureStore'.
* The only supported option is 'logger'
* @return
* The new {@link LDClient} instance.
* The new {@link LDClient} instance.
*/
export const init = (
kvNamespace: KVNamespace,
sdkKey: string,
options: LDOptions = {}
): LDClient => {
const client = createLDClient(kvNamespace, sdkKey, options);
return {
variation(
key: string,
context: LDContext,
defaultValue: LDFlagValue,
callback?: (err: any, res: LDFlagValue) => void
): Promise<LDFlagValue> {
return client.variation(key, context, defaultValue, callback);
},
variationDetail(
key: string,
context: LDContext,
defaultValue: LDFlagValue,
callback?: (err: any, res: LDEvaluationDetail) => void
): Promise<LDEvaluationDetail> {
return client.variationDetail(key, context, defaultValue, callback);
},
allFlagsState(
context: LDContext,
o?: LDFlagsStateOptions,
callback?: (err: Error | null, res: LDFlagsState | null) => void
): Promise<LDFlagsState> {
return client.allFlagsState(context, o, callback);
},
waitForInitialization(): Promise<LDClient> {
return client.waitForInitialization();
},
};
export const init = (sdkKey: string, kvNamespace: KVNamespace, options: LDOptions = {}) => {
const logger = options.logger ?? BasicLogger.get();
return initEdge(sdkKey, createPlatformInfo(), {
featureStore: createFeatureStore(kvNamespace, sdkKey, logger),
logger,
...options,
});
};
12 changes: 0 additions & 12 deletions packages/sdk/cloudflare/src/platform/index.ts

This file was deleted.

Loading