Skip to content

Commit 02e0eee

Browse files
authored
feat: Add Fastly Edge SDK (#723)
**Requirements** - [x] I have added test coverage for new or changed functionality - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [x] I have validated my changes against all supported platform versions **Describe the solution you've provided** This PR adds a new Fastly Compute SDK. This SDK must be used in conjunction with our upcoming Fastly KV integration. The SDK is essentially the same as the other edge integrations (Akamai, Cloudflare, Vercel), with the following changes: - `node:events` is not compatible with Fastly's runtime. As a result, we cannot use `@launchdarkly/sdk-server-edge`. Instead, I copied the contents of `@launchdarkly/sdk-server-edge` into the fastly package and replaced `createCallbacks.ts` with an empty implementation. - Enabled `sendEvents` by default. - Added the `eventsUri` option to allow for sending events to a custom endpoint. - Added a new optional `eventsBackendName` option. A [Fastly Backend](https://developer.fastly.com/reference/api/services/backend/) configured to `https://events.launchdarkly.com` is required for sending events. The default value is `launchdarkly`. This option is passed to [Fastly's customized fetch()](https://js-compute-reference-docs.edgecompute.app/docs/globals/fetch#explicit-backends). I added an example app that demonstrates using the SDK to evaluate a feature flag edge to control the static image served. I published an beta version to npm [here](https://www.npmjs.com/package/@launchdarkly/fastly-server-sdk?activeTab=readme).
1 parent 465e60c commit 02e0eee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1927
-0
lines changed

.github/workflows/fastly.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: sdk/fastly
2+
3+
on:
4+
push:
5+
branches: [main, 'feat/**']
6+
paths-ignore:
7+
- '**.md' #Do not need to run CI for markdown changes.
8+
pull_request:
9+
branches: [main, 'feat/**']
10+
paths-ignore:
11+
- '**.md'
12+
13+
jobs:
14+
build-test-fastly:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-node@v4
19+
- id: shared
20+
name: Shared CI Steps
21+
uses: ./actions/ci
22+
with:
23+
workspace_name: '@launchdarkly/fastly-server-sdk'
24+
workspace_path: packages/sdk/fastly

.github/workflows/manual-publish-docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ on:
1212
- packages/shared/sdk-server-edge
1313
- packages/shared/akamai-edgeworker-sdk
1414
- packages/sdk/cloudflare
15+
- packages/sdk/fastly
1516
- packages/sdk/server-node
1617
- packages/sdk/vercel
1718
- packages/sdk/akamai-base

.github/workflows/manual-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ on:
2222
- packages/shared/sdk-server-edge
2323
- packages/shared/akamai-edgeworker-sdk
2424
- packages/sdk/cloudflare
25+
- packages/sdk/fastly
2526
- packages/sdk/react-native
2627
- packages/sdk/server-node
2728
- packages/sdk/react-universal

.github/workflows/release-please.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
package-sdk-server-edge-released: ${{ steps.release.outputs['packages/shared/sdk-server-edge--release_created'] }}
1515
package-akamai-edgeworker-sdk-released: ${{ steps.release.outputs['packages/shared/akamai-edgeworker-sdk--release_created'] }}
1616
package-cloudflare-released: ${{ steps.release.outputs['packages/sdk/cloudflare--release_created'] }}
17+
package-fastly-released: ${{ steps.release.outputs['packages/sdk/fastly--release_created'] }}
1718
package-react-native-released: ${{ steps.release.outputs['packages/sdk/react-native--release_created'] }}
1819
package-server-node-released: ${{ steps.release.outputs['packages/sdk/server-node--release_created'] }}
1920
package-vercel-released: ${{ steps.release.outputs['packages/sdk/vercel--release_created'] }}
@@ -153,6 +154,26 @@ jobs:
153154
workspace_path: packages/sdk/cloudflare
154155
aws_assume_role: ${{ vars.AWS_ROLE_ARN }}
155156

157+
release-fastly:
158+
runs-on: ubuntu-latest
159+
needs: ['release-please', 'release-sdk-server']
160+
permissions:
161+
id-token: write
162+
contents: write
163+
if: ${{ always() && !failure() && !cancelled() && needs.release-please.outputs.package-fastly-released == 'true'}}
164+
steps:
165+
- uses: actions/checkout@v4
166+
- uses: actions/setup-node@v4
167+
with:
168+
node-version: 20.x
169+
registry-url: 'https://registry.npmjs.org'
170+
- id: release-fastly
171+
name: Full release of packages/sdk/fastly
172+
uses: ./actions/full-release
173+
with:
174+
workspace_path: packages/sdk/fastly
175+
aws_assume_role: ${{ vars.AWS_ROLE_ARN }}
176+
156177
release-react-native:
157178
runs-on: ubuntu-latest
158179
needs: ['release-please', 'release-sdk-client']

.release-please-manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"packages/shared/sdk-server": "2.11.1",
44
"packages/sdk/server-node": "9.7.4",
55
"packages/sdk/cloudflare": "2.6.5",
6+
"packages/sdk/fastly": "0.0.1",
67
"packages/shared/sdk-server-edge": "2.5.4",
78
"packages/sdk/vercel": "1.3.23",
89
"packages/sdk/akamai-base": "3.0.0",

.sdk_metadata.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
"tag-prefix": "cloudflare-server-sdk-"
2929
}
3030
},
31+
"fastly": {
32+
"name": "Fastly SDK",
33+
"type": "edge",
34+
"path": "packages/sdk/fastly",
35+
"languages": ["JavaScript", "TypeScript"],
36+
"releases": {
37+
"tag-prefix": "fastly-server-sdk-"
38+
}
39+
},
3140
"react-native": {
3241
"name": "React Native SDK",
3342
"type": "client-side",

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"packages/sdk/server-node",
1010
"packages/sdk/cloudflare",
1111
"packages/sdk/cloudflare/example",
12+
"packages/sdk/fastly",
13+
"packages/sdk/fastly/example",
1214
"packages/sdk/react-native",
1315
"packages/sdk/react-native/example",
1416
"packages/sdk/react-universal",

packages/sdk/fastly/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# LaunchDarkly SDK for Fastly
2+
3+
The LaunchDarkly SDK for Fastly is designed for use in [Fastly Compute Platform](https://www.fastly.com/documentation/guides/compute/). It follows the server-side LaunchDarkly model for multi-user contexts. It is not intended for use in desktop and embedded systems applications.
4+
5+
## Install
6+
7+
```shell
8+
# npm
9+
npm i @launchdarkly/fastly-server-sdk
10+
11+
# yarn
12+
yarn add @launchdarkly/fastly-server-sdk
13+
```
14+
15+
## Usage notes
16+
17+
- The SDK must be initialized and used when processing requests, not during build-time initialization.
18+
- The SDK caches all KV data during initialization to reduce the number of backend requests needed to fetch KV data. This means changes to feature flags or segments will not be picked up during the lifecycle of a single request instance.
19+
- Events should flushed using the [`waitUntil()` method](https://js-compute-reference-docs.edgecompute.app/docs/globals/FetchEvent/prototype/waitUntil).
20+
21+
## Quickstart
22+
23+
See the full [example app](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/fastly/example).
24+
25+
## Developing this SDK
26+
27+
```shell
28+
# at js-core repo root
29+
yarn && yarn build && cd packages/sdk/fastly
30+
31+
# run tests
32+
yarn test
33+
```
34+
35+
## Verifying SDK build provenance with the SLSA framework
36+
37+
LaunchDarkly uses the [SLSA framework](https://slsa.dev/spec/v1.0/about) (Supply-chain Levels for Software Artifacts) to help developers make their supply chain more secure by ensuring the authenticity and build integrity of our published SDK packages. To learn more, see the [provenance guide](PROVENANCE.md).
38+
39+
## About LaunchDarkly
40+
41+
- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
42+
- Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
43+
- Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
44+
- Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
45+
- Grant access to certain features based on user attributes, like payment plan (eg: users on the 'gold' plan get access to more features than users in the 'silver' plan).
46+
- Disable parts of your application to facilitate maintenance, without taking everything offline.
47+
- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Read [our documentation](https://docs.launchdarkly.com/sdk) for a complete list.
48+
- Explore LaunchDarkly
49+
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
50+
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
51+
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
52+
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates
53+
54+
[sdk-fastly-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/fastly.yml/badge.svg
55+
[sdk-fastly-ci]: https://github.com/launchdarkly/js-core/actions/workflows/fastly.yml
56+
[sdk-fastly-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/fastly-server-sdk.svg?style=flat-square
57+
[sdk-fastly-npm-link]: https://www.npmjs.com/package/@launchdarkly/fastly-server-sdk
58+
[sdk-fastly-ghp-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8
59+
[sdk-fastly-ghp-link]: https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/
60+
[sdk-fastly-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/fastly-server-sdk.svg?style=flat-square
61+
[sdk-fastly-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/fastly-server-sdk.svg?style=flat-square
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const KVStore = jest.fn().mockImplementation(() => ({
2+
get: jest.fn(),
3+
put: jest.fn(),
4+
delete: jest.fn(),
5+
getMulti: jest.fn(),
6+
putMulti: jest.fn(),
7+
deleteMulti: jest.fn(),
8+
}));
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { AsyncStoreFacade, LDFeatureStore } from '@launchdarkly/js-server-sdk-common';
2+
3+
import { EdgeFeatureStore } from '../../src/api/EdgeFeatureStore';
4+
import mockEdgeProvider from '../utils/mockEdgeProvider';
5+
import * as testData from './testData.json';
6+
7+
describe('EdgeFeatureStore', () => {
8+
const clientSideId = 'client-side-id';
9+
const kvKey = `LD-Env-${clientSideId}`;
10+
const mockLogger = {
11+
error: jest.fn(),
12+
warn: jest.fn(),
13+
info: jest.fn(),
14+
debug: jest.fn(),
15+
};
16+
const mockGet = mockEdgeProvider.get as jest.Mock;
17+
let featureStore: LDFeatureStore;
18+
let asyncFeatureStore: AsyncStoreFacade;
19+
20+
beforeEach(() => {
21+
mockGet.mockImplementation(() => Promise.resolve(JSON.stringify(testData)));
22+
featureStore = new EdgeFeatureStore(
23+
mockEdgeProvider,
24+
clientSideId,
25+
'MockEdgeProvider',
26+
mockLogger,
27+
);
28+
asyncFeatureStore = new AsyncStoreFacade(featureStore);
29+
});
30+
31+
afterEach(() => {
32+
jest.resetAllMocks();
33+
});
34+
35+
describe('get', () => {
36+
it('can retrieve valid flag', async () => {
37+
const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'testFlag1');
38+
39+
expect(mockGet).toHaveBeenCalledWith(kvKey);
40+
expect(flag).toMatchObject(testData.flags.testFlag1);
41+
});
42+
43+
it('returns undefined for invalid flag key', async () => {
44+
const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'invalid');
45+
46+
expect(flag).toBeUndefined();
47+
});
48+
49+
it('can retrieve valid segment', async () => {
50+
const segment = await asyncFeatureStore.get({ namespace: 'segments' }, 'testSegment1');
51+
52+
expect(mockGet).toHaveBeenCalledWith(kvKey);
53+
expect(segment).toMatchObject(testData.segments.testSegment1);
54+
});
55+
56+
it('returns undefined for invalid segment key', async () => {
57+
const segment = await asyncFeatureStore.get({ namespace: 'segments' }, 'invalid');
58+
59+
expect(segment).toBeUndefined();
60+
});
61+
62+
it('returns null for invalid kv key', async () => {
63+
mockGet.mockImplementation(() => Promise.resolve(null));
64+
const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'testFlag1');
65+
66+
expect(flag).toBeNull();
67+
});
68+
});
69+
70+
describe('all', () => {
71+
it('can retrieve all flags', async () => {
72+
const flags = await asyncFeatureStore.all({ namespace: 'features' });
73+
74+
expect(mockGet).toHaveBeenCalledWith(kvKey);
75+
expect(flags).toMatchObject(testData.flags);
76+
});
77+
78+
it('can retrieve all segments', async () => {
79+
const segment = await asyncFeatureStore.all({ namespace: 'segments' });
80+
81+
expect(mockGet).toHaveBeenCalledWith(kvKey);
82+
expect(segment).toMatchObject(testData.segments);
83+
});
84+
85+
it('returns empty object for invalid DataKind', async () => {
86+
const flag = await asyncFeatureStore.all({ namespace: 'InvalidDataKind' });
87+
88+
expect(flag).toEqual({});
89+
});
90+
91+
it('returns empty object for invalid kv key', async () => {
92+
mockGet.mockImplementation(() => Promise.resolve(null));
93+
const segment = await asyncFeatureStore.all({ namespace: 'segments' });
94+
95+
expect(segment).toEqual({});
96+
});
97+
});
98+
99+
describe('initialized', () => {
100+
it('returns true when initialized', async () => {
101+
const isInitialized = await asyncFeatureStore.initialized();
102+
103+
expect(mockGet).toHaveBeenCalledWith(kvKey);
104+
expect(isInitialized).toBeTruthy();
105+
});
106+
107+
it('returns false when not initialized', async () => {
108+
mockGet.mockImplementation(() => Promise.resolve(null));
109+
const isInitialized = await asyncFeatureStore.initialized();
110+
111+
expect(mockGet).toHaveBeenCalledWith(kvKey);
112+
expect(isInitialized).toBeFalsy();
113+
});
114+
});
115+
116+
describe('init & getDescription', () => {
117+
it('can initialize', (done) => {
118+
const cb = jest.fn(() => {
119+
done();
120+
});
121+
featureStore.init(testData, cb);
122+
});
123+
124+
it('can retrieve description', async () => {
125+
const description = featureStore.getDescription?.();
126+
127+
expect(description).toEqual('MockEdgeProvider');
128+
});
129+
});
130+
});

0 commit comments

Comments
 (0)