js-sdk-contrib/libs/providers/flagd
Todd Baert f2efa55941
feat: support no default variant (#1354)
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
2025-07-16 09:17:07 -04:00
..
schemas@2852d7772e chore(deps): update libs/providers/flagd/schemas digest to 2852d77 (#1299) 2025-06-05 05:07:23 +00:00
spec@a3678719bf chore(deps): update libs/providers/flagd/spec digest to a367871 (#1326) 2025-07-04 11:16:50 -04:00
src feat: support no default variant (#1354) 2025-07-16 09:17:07 -04:00
.babelrc chore: migrate to nx 16 (#366) 2023-05-15 14:28:50 -04:00
.eslintrc.json chore: update nx packages (#1147) 2025-01-10 11:36:17 -05:00
CHANGELOG.md chore(main): release flagd-provider 0.13.3 (#1191) 2025-02-07 16:20:41 -05:00
README.md feat: support proxy routing via gRPC default_authority (#1202) 2025-02-07 09:40:41 -05:00
babel.config.json feat: flagd-web provider (#142) 2022-11-21 14:58:23 -05:00
jest.config.ts feat: add offline mode, fix in-process connection edge cases (#708) 2024-01-08 13:20:32 -05:00
package.json chore: add 1.13.0 as a valid grpc-js peer version (#1234) 2025-03-17 15:27:03 -04:00
project.json chore: update nx packages (#1147) 2025-01-10 11:36:17 -05:00
tsconfig.json feat: flagd in-process provider (#633) 2023-11-14 11:31:01 -05:00
tsconfig.lib.json chore: various gherkin improvements for e2e tests (#1008) 2024-11-06 13:49:22 -05:00
tsconfig.spec.json chore: various gherkin improvements for e2e tests (#1008) 2024-11-06 13:49:22 -05:00

README.md

Server-Side flagd Provider

This provider is designed to use flagd's evaluation protocol, or locally evaluate flags defined in a flagd flag definition. This repository and package provides the client code for interacting with it via the OpenFeature server-side JavaScript SDK.

Installation

npm

npm install @openfeature/flagd-provider

yarn

yarn add @openfeature/server-sdk @grpc/grpc-js @openfeature/flagd-core

[!NOTE] yarn requires manual installation of peer dependencies

Configurations and Usage

The FlagdProvider supports multiple configuration options and has the ability to resolve flags remotely over RPC or in-process. Options can be defined in the constructor or as environment variables. Constructor options having the highest precedence.

Available Configuration Options

Option name Environment variable name Type Default Supported values
host FLAGD_HOST string localhost
port FLAGD_PORT number resolver specific defaults
tls FLAGD_TLS boolean false
socketPath FLAGD_SOCKET_PATH string -
resolverType FLAGD_RESOLVER string rpc rpc, in-process
offlineFlagSourcePath FLAGD_OFFLINE_FLAG_SOURCE_PATH string -
selector FLAGD_SOURCE_SELECTOR string -
cache FLAGD_CACHE string lru lru, disabled
maxCacheSize FLAGD_MAX_CACHE_SIZE int 1000
defaultAuthority FLAGD_DEFAULT_AUTHORITY string - rpc, in-process

Resolver type-specific Defaults

Option name Environment variable name in-process rpc default
port FLAGD_PORT 8015 8013 8013

Below are examples of usage patterns.

Remote flag resolving over RPC

This is the default mode of operation of the provider. In this mode, FlagdProvider communicates with flagd via the gRPC protocol. Flag evaluations take place remotely on the connected flagd instance.

  OpenFeature.setProvider(new FlagdProvider())

In the above example, the provider expects flagd to be available at localhost:8013 (default host and port).

Alternatively, you can use socket paths to connect to flagd.

  OpenFeature.setProvider(new FlagdProvider({
      socketPath: "/tmp/flagd.socks",
  }))

In-process resolver

This mode performs flag evaluations locally (in-process). Flag configurations for evaluation are obtained via gRPC protocol using sync protobuf schema service definition.

  OpenFeature.setProvider(new FlagdProvider({
      resolverType: 'in-process',
  }))

In the above example, the provider expects a flag sync service implementation to be available at localhost:8015 (default host and port).

In-process resolver can also work in an offline mode. To enable this mode, you should provide a valid flag configuration file with the option offlineFlagSourcePath.

  OpenFeature.setProvider(new FlagdProvider({
      resolverType: 'in-process',
      offlineFlagSourcePath: './flags.json',
  }))

Offline mode uses fs.watchFile and polls every 5 seconds for changes to the file. This mode is useful for local development, test cases, and for offline applications.

Default Authority usage (optional)

This is useful for complex routing or service-discovery use cases that involve a proxy (e.g., Envoy). Please refer to this GitHub issue for more information.

  OpenFeature.setProvider(new FlagdProvider({
      resolverType: 'in-process',
      defaultAuthority: 'b-target-api.service',
  }))

Supported Events

The flagd provider emits PROVIDER_READY, PROVIDER_ERROR and PROVIDER_CONFIGURATION_CHANGED events.

SDK event Originating action in flagd
PROVIDER_READY The streaming connection with flagd has been established.
PROVIDER_ERROR The streaming connection with flagd has been broken.
PROVIDER_CONFIGURATION_CHANGED A flag configuration (default value, targeting rule, etc) in flagd has changed.

For general information on events, see the official documentation.

Flag Metadata

Flag metadata is a set of key-value pairs that can be associated with a flag. The values come from the flag definition in flagd.

Building

Run nx package providers-flagd to build the library.

NOTE: Buf must be installed to build locally.

Running Unit Tests

Run nx test providers-flagd to execute the unit tests via Jest.