-
Notifications
You must be signed in to change notification settings - Fork 1
feat: js local resolver #32
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
Open
andreas-karlsson
wants to merge
7
commits into
main
Choose a base branch
from
local-js-resolver
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
7 commits
Select commit
Hold shift + click to select a range
e369992
feat: js local resolver
andreas-karlsson 496a36d
fix: missing return types
andreas-karlsson 6a870cb
fix: remove buggy wee-alloc
andreas-karlsson 1a3e176
fix: js use after free bug
andreas-karlsson 52763c2
fix: rust-analyzer protoc problem
andreas-karlsson 0e96b2d
fix: proto:gen cleanup
andreas-karlsson 5ea7a1a
feat: retries and some cleanup
andreas-karlsson 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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
|
||
[*.{js,json,yml}] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 |
This file contains hidden or 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,4 @@ | ||
/.yarn/** linguist-vendored | ||
/.yarn/releases/* binary | ||
/.yarn/plugins/**/* binary | ||
/.pnp.* binary linguist-generated |
This file contains hidden or 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,10 @@ | ||
.yarn/* | ||
!.yarn/patches | ||
!.yarn/plugins | ||
!.yarn/releases | ||
!.yarn/sdks | ||
!.yarn/versions | ||
node_modules/ | ||
src/proto/ | ||
dist/ | ||
.env.test |
This file contains hidden or 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 @@ | ||
nodeLinker: node-modules |
This file contains hidden or 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 @@ | ||
# js | ||
This file contains hidden or 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,54 @@ | ||
{ | ||
"name": "@spotify-confidence/openfeature-server-provider-local", | ||
"version": "0.0.0", | ||
"private": true, | ||
"description": "Spotify Condfidence Open Feature provider", | ||
"type": "module", | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "./dist/index.node.js", | ||
"module": "./dist/index.browser.js", | ||
"types": "./dist/index.node.d.ts", | ||
"exports": { | ||
".": { | ||
"node": { | ||
"types": "./dist/index.node.d.ts", | ||
"default": "./dist/index.node.js" | ||
}, | ||
"browser": { | ||
"types": "./dist/index.browser.d.ts", | ||
"default": "./dist/index.browser.js" | ||
}, | ||
"default": { | ||
"types": "./dist/index.node.d.ts", | ||
"default": "./dist/index.node.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"scripts": { | ||
"build": "tsdown", | ||
"dev": "tsdown --watch", | ||
"test": "vitest", | ||
"proto:gen": "rm -rf src/proto && mkdir -p src/proto && protoc --plugin=node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt useOptionals=messages --ts_proto_opt esModuleInterop=true --ts_proto_out src/proto -Iproto api.proto messages.proto" | ||
}, | ||
"dependencies": { | ||
"loglevel": "^1.9.2" | ||
}, | ||
"devDependencies": { | ||
"@openfeature/core": "^1.9.0", | ||
"@openfeature/server-sdk": "^1.19.0", | ||
"@types/node": "^24.0.1", | ||
"@vitest/coverage-v8": "^3.2.4", | ||
"dotenv": "^17.2.2", | ||
"rolldown": "1.0.0-beta.38", | ||
"ts-proto": "^2.7.3", | ||
"tsdown": "latest", | ||
"vitest": "^3.2.4" | ||
}, | ||
"peerDependencies": { | ||
"@openfeature/server-sdk": "^1.19.0" | ||
}, | ||
"packageManager": "yarn@4.6.0" | ||
} |
This file contains hidden or 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,92 @@ | ||
syntax = "proto3"; | ||
|
||
import "google/protobuf/struct.proto"; | ||
import "google/protobuf/timestamp.proto"; | ||
|
||
message ResolveFlagsRequest { | ||
// If non-empty, the specific flags are resolved, otherwise all flags | ||
// available to the client will be resolved. | ||
repeated string flags = 1; | ||
|
||
// An object that contains data used in the flag resolve. For example, | ||
// the targeting key e.g. the id of the randomization unit, other attributes | ||
// like country or version that are used for targeting. | ||
google.protobuf.Struct evaluation_context = 2; | ||
|
||
// Credentials for the client. It is used to identify the client and find | ||
// the flags that are available to it. | ||
string client_secret = 3; | ||
|
||
// Determines whether the flags should be applied directly as part of the | ||
// resolve, or delayed until `ApplyFlag` is called. A flag is typically | ||
// applied when it is used, if this occurs much later than the resolve, then | ||
// `apply` should likely be set to false. | ||
bool apply = 4; | ||
|
||
// Information about the SDK used to initiate the request. | ||
// Sdk sdk = 5; | ||
} | ||
|
||
message ResolveFlagsResponse { | ||
// The list of all flags that could be resolved. Note: if any flag was | ||
// archived it will not be included in this list. | ||
repeated ResolvedFlag resolved_flags = 1; | ||
|
||
// An opaque token that is used when `apply` is set to false in `ResolveFlags`. | ||
// When `apply` is set to false, the token must be passed to `ApplyFlags`. | ||
bytes resolve_token = 2; | ||
|
||
// Unique identifier for this particular resolve request. | ||
string resolve_id = 3; | ||
} | ||
|
||
|
||
message ResolvedFlag { | ||
// The id of the flag that as resolved. | ||
string flag = 1; | ||
|
||
// The id of the resolved variant has the format `flags/abc/variants/xyz`. | ||
string variant = 2; | ||
|
||
// The value corresponding to the variant. It will always be a json object, | ||
// for example `{ "color": "red", "size": 12 }`. | ||
google.protobuf.Struct value = 3; | ||
|
||
// The schema of the value that was returned. For example: | ||
// ``` | ||
// { | ||
// "schema": { | ||
// "color": { "stringSchema": {} }, | ||
// "size": { "intSchema": {} } | ||
// } | ||
// } | ||
// ``` | ||
// types.v1.FlagSchema.StructFlagSchema flag_schema = 4; | ||
|
||
// The reason to why the flag could be resolved or not. | ||
ResolveReason reason = 5; | ||
} | ||
|
||
|
||
enum ResolveReason { | ||
// Unspecified enum. | ||
RESOLVE_REASON_UNSPECIFIED = 0; | ||
// The flag was successfully resolved because one rule matched. | ||
RESOLVE_REASON_MATCH = 1; | ||
// The flag could not be resolved because no rule matched. | ||
RESOLVE_REASON_NO_SEGMENT_MATCH = 2; | ||
// The flag could not be resolved because the matching rule had no variant | ||
// that could be assigned. | ||
RESOLVE_REASON_NO_TREATMENT_MATCH = 3 [deprecated = true]; | ||
// The flag could not be resolved because it was archived. | ||
RESOLVE_REASON_FLAG_ARCHIVED = 4; | ||
// The flag could not be resolved because the targeting key field was invalid | ||
RESOLVE_REASON_TARGETING_KEY_ERROR = 5; | ||
// Unknown error occurred during the resolve | ||
RESOLVE_REASON_ERROR = 6; | ||
} | ||
|
||
message SetResolverStateRequest { | ||
bytes state = 1; | ||
string account_id = 2; | ||
} |
This file contains hidden or 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,14 @@ | ||
syntax = "proto3"; | ||
|
||
message Void {} | ||
|
||
message Request { | ||
bytes data = 1; | ||
} | ||
|
||
message Response { | ||
oneof result { | ||
bytes data = 1; | ||
string error = 2; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
openfeature-provider/js/src/ConfidenceServerProviderLocal.e2e.test.ts
This file contains hidden or 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,100 @@ | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
import { OpenFeature } from '@openfeature/server-sdk'; | ||
import { ConfidenceServerProviderLocal } from './ConfidenceServerProviderLocal'; | ||
import { readFileSync } from 'node:fs'; | ||
import { WasmResolver } from './WasmResolver'; | ||
// import log from 'loglevel'; | ||
// log.setLevel("debug"); | ||
|
||
const { | ||
CONFIDENCE_API_CLIENT_ID, | ||
CONFIDENCE_API_CLIENT_SECRET, | ||
} = requireEnv('CONFIDENCE_API_CLIENT_ID', 'CONFIDENCE_API_CLIENT_SECRET'); | ||
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. just noting that these need to be added to github secrets once we want to run this in GH workflows. |
||
|
||
const moduleBytes = readFileSync(__dirname + '/../../../wasm/confidence_resolver.wasm'); | ||
const module = new WebAssembly.Module(moduleBytes); | ||
const resolver = await WasmResolver.load(module); | ||
const confidenceProvider = new ConfidenceServerProviderLocal(resolver, { | ||
flagClientSecret: 'RxDVTrXvc6op1XxiQ4OaR31dKbJ39aYV', | ||
apiClientId: CONFIDENCE_API_CLIENT_ID, | ||
apiClientSecret: CONFIDENCE_API_CLIENT_SECRET | ||
}); | ||
|
||
describe('ConfidenceServerProvider E2E tests', () => { | ||
beforeAll( async () => { | ||
|
||
await OpenFeature.setProviderAndWait(confidenceProvider); | ||
OpenFeature.setContext({ | ||
targetingKey: 'test-a', // control | ||
}); | ||
}); | ||
|
||
afterAll(() => OpenFeature.close()) | ||
|
||
it('should resolve a boolean e2e', async () => { | ||
const client = OpenFeature.getClient(); | ||
|
||
expect(await client.getBooleanValue('web-sdk-e2e-flag.bool', true)).toBeFalsy(); | ||
}); | ||
|
||
it('should resolve an int', async () => { | ||
const client = OpenFeature.getClient(); | ||
|
||
expect(await client.getNumberValue('web-sdk-e2e-flag.int', 10)).toEqual(3); | ||
}); | ||
|
||
it('should resolve a double', async () => { | ||
const client = OpenFeature.getClient(); | ||
|
||
expect(await client.getNumberValue('web-sdk-e2e-flag.double', 10)).toEqual(3.5); | ||
}); | ||
|
||
it('should resolve a string', async () => { | ||
const client = OpenFeature.getClient(); | ||
|
||
expect(await client.getStringValue('web-sdk-e2e-flag.str', 'default')).toEqual('control'); | ||
}); | ||
|
||
it('should resolve a struct', async () => { | ||
const client = OpenFeature.getClient(); | ||
const expectedObject = { | ||
int: 4, | ||
str: 'obj control', | ||
bool: false, | ||
double: 3.6, | ||
['obj-obj']: {}, | ||
}; | ||
|
||
expect(await client.getObjectValue('web-sdk-e2e-flag.obj', {})).toEqual(expectedObject); | ||
}); | ||
|
||
it('should resolve a sub value from a struct', async () => { | ||
const client = OpenFeature.getClient(); | ||
|
||
expect(await client.getBooleanValue('web-sdk-e2e-flag.obj.bool', true)).toBeFalsy(); | ||
}); | ||
|
||
it('should resolve a sub value from a struct with details with resolve token for client side apply call', async () => { | ||
const client = OpenFeature.getClient(); | ||
const expectedObject = { | ||
flagKey: 'web-sdk-e2e-flag.obj.double', | ||
reason: 'MATCH', | ||
variant: 'flags/web-sdk-e2e-flag/variants/control', | ||
flagMetadata: {}, | ||
value: 3.6, | ||
}; | ||
|
||
expect(await client.getNumberDetails('web-sdk-e2e-flag.obj.double', 1)).toEqual(expectedObject); | ||
}); | ||
}); | ||
|
||
function requireEnv<const N extends string[]>(...names:N): Record<N[number],string> { | ||
return names.reduce((acc, name) => { | ||
const value = process.env[name]; | ||
if(!value) throw new Error(`Missing environment variable ${name}`) | ||
return { | ||
...acc, | ||
[name]: value | ||
}; | ||
}, {}) as Record<N[number],string>; | ||
} |
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.
Ask AI to fix!