-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Fastly Edge SDK #723
Open
ldhenry
wants to merge
35
commits into
main
Choose a base branch
from
hbarrow/REL-4756/create-fastly-sdk-in-js-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
00ecc9a
Start on Fastly SDK
ldhenry 809e1cd
polyfill node:events in fastly sdk instead of example
ldhenry 0914d82
Add tests and cleanup
ldhenry be7aa07
Fix test and revert change to sdk-server-edge
ldhenry 40f05ab
Add CI
ldhenry 77485da
Add crypto-js types
ldhenry aff265d
fix typedoc
ldhenry 7d47d12
Add fastly to release-please
ldhenry cfddcdc
Prep for alpha release
ldhenry 6079a15
Merge main
ldhenry f9b9561
Prep for 0.0.1 release
ldhenry 7fd2d96
Update homepage url
ldhenry ce436a2
merge main
ldhenry 3717d13
Add jsr.json and pin version in example
ldhenry e96311c
Change name in platformInfo
ldhenry 9c7c966
lowercase in .sdk_metadata
ldhenry 91819c9
Add eslint config
ldhenry 584eef5
Add remaining eslint configs
ldhenry 08cb672
Add more eslint helpers
ldhenry 53891b1
Add @trivago/prettier-plugin-sort-imports
ldhenry 1fb5431
Downgrade @typescript-eslint version
ldhenry 9c6d3dd
Add ts-jest
ldhenry ed6764e
Add @types/jest
ldhenry 4282427
Bump dependencies
ldhenry a422de4
Bump example version
ldhenry 6271e7c
Clean up LDClient implementation and add eventsUri option
ldhenry dff0e69
Bump @fastly/cli
ldhenry 7c1bc03
Use release-please to set version
ldhenry 2eef68d
Fix test
ldhenry b41b07b
Remove the need for ts-ignore
ldhenry dda0ce5
Add newline to tsconfig.json
ldhenry 389c52e
Swap photos with PD versions
ldhenry a73b761
Reformat photo credits
ldhenry 0111854
merge main
ldhenry 3a67b2b
Add link to free Fastly account
ldhenry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: sdk/fastly | ||
|
||
on: | ||
push: | ||
branches: [main, 'feat/**'] | ||
paths-ignore: | ||
- '**.md' #Do not need to run CI for markdown changes. | ||
pull_request: | ||
branches: [main, 'feat/**'] | ||
paths-ignore: | ||
- '**.md' | ||
|
||
jobs: | ||
build-test-fastly: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
- id: shared | ||
name: Shared CI Steps | ||
uses: ./actions/ci | ||
with: | ||
workspace_name: '@launchdarkly/fastly-server-sdk' | ||
workspace_path: packages/sdk/fastly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# LaunchDarkly SDK for Fastly | ||
|
||
The LaunchDarkly SDK for Fastly is designed primarily 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. | ||
|
||
# ⛔️⛔️⛔️⛔️ | ||
|
||
> [!CAUTION] | ||
> This library is an alpha version and should not be considered ready for production use while this message is visible. | ||
|
||
# ☝️☝️☝️☝️☝️☝️ | ||
|
||
## Install | ||
|
||
```shell | ||
# npm | ||
npm i @launchdarkly/fastly-server-sdk | ||
|
||
# yarn | ||
yarn add @launchdarkly/fastly-server-sdk | ||
``` | ||
|
||
## Quickstart | ||
|
||
See the full [example app](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/fastly/example). | ||
|
||
## Developing this SDK | ||
|
||
```shell | ||
# at js-core repo root | ||
yarn && yarn build && cd packages/sdk/fastly | ||
|
||
# run tests | ||
yarn test | ||
``` | ||
|
||
## Verifying SDK build provenance with the SLSA framework | ||
|
||
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). | ||
|
||
## About LaunchDarkly | ||
|
||
- 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: | ||
- 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. | ||
- 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?). | ||
- 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. | ||
- 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). | ||
- Disable parts of your application to facilitate maintenance, without taking everything offline. | ||
- 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. | ||
- Explore LaunchDarkly | ||
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information | ||
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides | ||
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation | ||
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates | ||
|
||
[sdk-fastly-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/fastly.yml/badge.svg | ||
[sdk-fastly-ci]: https://github.com/launchdarkly/js-core/actions/workflows/fastly.yml | ||
[sdk-fastly-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/fastly-server-sdk.svg?style=flat-square | ||
[sdk-fastly-npm-link]: https://www.npmjs.com/package/@launchdarkly/fastly-server-sdk | ||
[sdk-fastly-ghp-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8 | ||
[sdk-fastly-ghp-link]: https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/ | ||
[sdk-fastly-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/fastly-server-sdk.svg?style=flat-square | ||
[sdk-fastly-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/fastly-server-sdk.svg?style=flat-square |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const KVStore = jest.fn().mockImplementation(() => ({ | ||
get: jest.fn(), | ||
put: jest.fn(), | ||
delete: jest.fn(), | ||
getMulti: jest.fn(), | ||
putMulti: jest.fn(), | ||
deleteMulti: jest.fn(), | ||
})); |
125 changes: 125 additions & 0 deletions
125
packages/sdk/fastly/__tests__/api/EdgeFeatureStore.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { AsyncStoreFacade, LDFeatureStore } from '@launchdarkly/js-server-sdk-common'; | ||
|
||
import { EdgeFeatureStore } from '../../src/api/EdgeFeatureStore'; | ||
import mockEdgeProvider from '../../src/utils/mockEdgeProvider'; | ||
import * as testData from './testData.json'; | ||
|
||
describe('EdgeFeatureStore', () => { | ||
const sdkKey = 'sdkKey'; | ||
const kvKey = `LD-Env-${sdkKey}`; | ||
const mockLogger = { | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
info: jest.fn(), | ||
debug: jest.fn(), | ||
}; | ||
const mockGet = mockEdgeProvider.get as jest.Mock; | ||
let featureStore: LDFeatureStore; | ||
let asyncFeatureStore: AsyncStoreFacade; | ||
|
||
beforeEach(() => { | ||
mockGet.mockImplementation(() => Promise.resolve(JSON.stringify(testData))); | ||
featureStore = new EdgeFeatureStore(mockEdgeProvider, sdkKey, 'MockEdgeProvider', mockLogger); | ||
asyncFeatureStore = new AsyncStoreFacade(featureStore); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('get', () => { | ||
test('get flag', async () => { | ||
const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'testFlag1'); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(kvKey); | ||
expect(flag).toMatchObject(testData.flags.testFlag1); | ||
}); | ||
|
||
test('invalid flag key', async () => { | ||
const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'invalid'); | ||
|
||
expect(flag).toBeUndefined(); | ||
}); | ||
|
||
test('get segment', async () => { | ||
const segment = await asyncFeatureStore.get({ namespace: 'segments' }, 'testSegment1'); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(kvKey); | ||
expect(segment).toMatchObject(testData.segments.testSegment1); | ||
}); | ||
|
||
test('invalid segment key', async () => { | ||
const segment = await asyncFeatureStore.get({ namespace: 'segments' }, 'invalid'); | ||
|
||
expect(segment).toBeUndefined(); | ||
}); | ||
|
||
test('invalid kv key', async () => { | ||
mockGet.mockImplementation(() => Promise.resolve(null)); | ||
const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'testFlag1'); | ||
|
||
expect(flag).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('all', () => { | ||
test('all flags', async () => { | ||
const flags = await asyncFeatureStore.all({ namespace: 'features' }); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(kvKey); | ||
expect(flags).toMatchObject(testData.flags); | ||
}); | ||
|
||
test('all segments', async () => { | ||
const segment = await asyncFeatureStore.all({ namespace: 'segments' }); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(kvKey); | ||
expect(segment).toMatchObject(testData.segments); | ||
}); | ||
|
||
test('invalid DataKind', async () => { | ||
const flag = await asyncFeatureStore.all({ namespace: 'InvalidDataKind' }); | ||
|
||
expect(flag).toEqual({}); | ||
}); | ||
|
||
test('invalid kv key', async () => { | ||
mockGet.mockImplementation(() => Promise.resolve(null)); | ||
const segment = await asyncFeatureStore.all({ namespace: 'segments' }); | ||
|
||
expect(segment).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('initialized', () => { | ||
test('is initialized', async () => { | ||
const isInitialized = await asyncFeatureStore.initialized(); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(kvKey); | ||
expect(isInitialized).toBeTruthy(); | ||
}); | ||
|
||
test('not initialized', async () => { | ||
mockGet.mockImplementation(() => Promise.resolve(null)); | ||
const isInitialized = await asyncFeatureStore.initialized(); | ||
|
||
expect(mockGet).toHaveBeenCalledWith(kvKey); | ||
expect(isInitialized).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('init & getDescription', () => { | ||
test('init', (done) => { | ||
const cb = jest.fn(() => { | ||
done(); | ||
}); | ||
featureStore.init(testData, cb); | ||
}); | ||
|
||
test('getDescription', async () => { | ||
const description = featureStore.getDescription?.(); | ||
|
||
expect(description).toEqual('MockEdgeProvider'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { internal } from '@launchdarkly/js-server-sdk-common'; | ||
|
||
import LDClient from '../../src/api/LDClient'; | ||
import { createBasicPlatform } from '../createBasicPlatform'; | ||
|
||
jest.mock('@launchdarkly/js-sdk-common', () => { | ||
const actual = jest.requireActual('@launchdarkly/js-sdk-common'); | ||
return { | ||
...actual, | ||
...{ | ||
internal: { | ||
...actual.internal, | ||
DiagnosticsManager: jest.fn(), | ||
EventProcessor: jest.fn(), | ||
}, | ||
}, | ||
}; | ||
}); | ||
|
||
let mockEventProcessor = internal.EventProcessor as jest.Mock; | ||
beforeEach(() => { | ||
mockEventProcessor = internal.EventProcessor as jest.Mock; | ||
mockEventProcessor.mockClear(); | ||
}); | ||
|
||
describe('Edge LDClient', () => { | ||
it('uses clientSideID endpoints', async () => { | ||
const client = new LDClient('client-side-id', createBasicPlatform().info, { | ||
sendEvents: true, | ||
eventsBackendName: 'launchdarkly', | ||
}); | ||
await client.waitForInitialization({ timeout: 10 }); | ||
const passedConfig = mockEventProcessor.mock.calls[0][0]; | ||
|
||
expect(passedConfig).toMatchObject({ | ||
sendEvents: true, | ||
serviceEndpoints: { | ||
includeAuthorizationHeader: false, | ||
analyticsEventPath: '/events/bulk/client-side-id', | ||
diagnosticEventPath: '/events/diagnostic/client-side-id', | ||
events: 'https://events.launchdarkly.com', | ||
polling: 'https://sdk.launchdarkly.com', | ||
streaming: 'https://stream.launchdarkly.com', | ||
}, | ||
}); | ||
}); | ||
it('uses custom eventsUri when specified', async () => { | ||
const client = new LDClient('client-side-id', createBasicPlatform().info, { | ||
sendEvents: true, | ||
eventsBackendName: 'launchdarkly', | ||
eventsUri: 'https://custom-base-uri.launchdarkly.com', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the option to set a custom |
||
}); | ||
await client.waitForInitialization({ timeout: 10 }); | ||
const passedConfig = mockEventProcessor.mock.calls[0][0]; | ||
|
||
expect(passedConfig).toMatchObject({ | ||
sendEvents: true, | ||
serviceEndpoints: { | ||
includeAuthorizationHeader: false, | ||
analyticsEventPath: '/events/bulk/client-side-id', | ||
diagnosticEventPath: '/events/diagnostic/client-side-id', | ||
events: 'https://custom-base-uri.launchdarkly.com', | ||
polling: 'https://custom-base-uri.launchdarkly.com', | ||
streaming: 'https://stream.launchdarkly.com', | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { BasicLogger } from '@launchdarkly/js-server-sdk-common'; | ||
|
||
import createOptions, { defaultOptions } from '../../src/api/createOptions'; | ||
|
||
describe('createOptions', () => { | ||
test('default options', () => { | ||
expect(createOptions({})).toEqual(defaultOptions); | ||
}); | ||
|
||
test('override logger', () => { | ||
const logger = new BasicLogger({ name: 'test' }); | ||
expect(createOptions({ logger })).toEqual({ | ||
...defaultOptions, | ||
logger, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All tests in this directory were copied over from
@launchdarkly/sdk-server-edge