@bucketeer/openfeature-node-server-sdk

中文文档
0.0.3发布于 2026/08/07Apache-2.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 1万

This is the official Node.js server-side OpenFeature provider for accessing your feature flags with Bucketeer.

原始使用文档
文档目录13 节
  1. Installation
  2. Usage
  3. Initialize the provider
  4. Evaluation Context
  5. Evaluate a feature flag
  6. Express.js Example
  7. Complete Example
  8. Contributing
  9. Development
  10. Environment
  11. Commands
  12. Example Application
  13. License

Bucketeer - OpenFeature Node.js Server Provider

This is the official Node.js server-side OpenFeature provider for accessing your feature flags with Bucketeer.

Bucketeer is an open-source platform created by CyberAgent to help teams make better decisions, reduce deployment lead time and release risk through feature flags. Bucketeer offers advanced features like dark launches and staged rollouts that perform limited releases based on user attributes, devices, and other segments.

In conjunction with the OpenFeature Server SDK you will be able to evaluate your feature flags in your Node.js server applications.

[!WARNING] This is a beta version. Breaking changes may be introduced before general release.

For documentation related to flags management in Bucketeer, refer to the Bucketeer documentation website.

Installation

npm install @bucketeer/openfeature-node-server-sdk @openfeature/server-sdk @bucketeer/node-server-sdk @openfeature/core

Note: This package requires @openfeature/server-sdk and @bucketeer/node-server-sdk as peer dependencies.

@openfeature/server-sdk requires @openfeature/core as a peer dependency.

Usage

Initialize the provider

Bucketeer provider needs to be created and then set in the global OpenFeature instance.

import { OpenFeature } from '@openfeature/server-sdk';
import { BucketeerProvider, defineBKTConfig } from '@bucketeer/openfeature-node-server-sdk';

const config = defineBKTConfig({
  apiKey: 'BUCKETEER_API_KEY',
  apiEndpoint: 'BUCKETEER_API_ENDPOINT',
  featureTag: 'FEATURE_TAG',
  appVersion: '1.2.3',
});

// Initialize the provider
const provider = new BucketeerProvider(config);

// Set the provider and wait for initialization
await OpenFeature.setProviderAndWait(provider);

See our documentation for more SDK configuration options.

The evaluation context allows the client to specify contextual data that Bucketeer uses to evaluate the feature flags.

The targetingKey is the user ID (Unique ID) and cannot be empty.

Evaluation Context

In server-side applications, evaluation context is typically provided per request rather than set globally. This allows for user-specific flag evaluations based on request data.

const client = OpenFeature.getClient();

// Define evaluation context per request/user
const evaluationContext = {
  targetingKey: 'user-123', // Required: unique user identifier
  email: '[email protected]', // User attributes for targeting
  plan: 'premium',
  region: 'us-east-1',
  timestamp: new Date().toISOString(),
};

// Evaluate flags with context
const featureEnabled = await client.getBooleanValue('new-feature', false, evaluationContext);

For applications that need to share some common context across all evaluations, you can set client-level context:

const client = OpenFeature.getClient();

// Set common context at the client level (e.g., application version)
client.setContext({
  version: process.env.APP_VERSION,
  environment: process.env.NODE_ENV,
});

// Per-request context will be merged with client context
const requestContext = {
  targetingKey: req.user.id,
  email: req.user.email,
  plan: req.user.plan,
};

const result = await client.getBooleanValue('feature-flag', false, requestContext);

Evaluate a feature flag

After the provider is set and ready, you can evaluate feature flags using the OpenFeature client. Always provide evaluation context for each evaluation.

const client = OpenFeature.getClient();

// Define evaluation context (typically per request)
const context = {
  targetingKey: 'user-123', // Required
  email: '[email protected]',
  plan: 'premium',
};

// boolean flag
const booleanValue = await client.getBooleanValue('my-feature-flag', false, context);

// string flag
const stringValue = await client.getStringValue('my-feature-flag', 'default-value', context);

// number flag
const numberValue = await client.getNumberValue('my-feature-flag', 0, context);

// object flag
const objectValue = await client.getObjectValue('my-feature-flag', {}, context);

// Get detailed evaluation results
const booleanDetails = await client.getBooleanDetails('my-feature-flag', false, context);
console.log(booleanDetails.value, booleanDetails.reason, booleanDetails.variant);

Express.js Example

Here's how you might use this in an Express.js application:

import express from 'express';
import { OpenFeature } from '@openfeature/server-sdk';
import { BucketeerProvider, defineBKTConfig } from '@bucketeer/openfeature-node-server-sdk';

const app = express();
const client = OpenFeature.getClient();

app.get('/api/features', async (req, res) => {
  // Create evaluation context from request
  // In a real app, you'd get the user ID from your auth system
  const userId = (req.headers['x-user-id'] as string) || 'anonymous';
  const userEmail = req.headers['x-user-email'] as string;

  const context = {
    targetingKey: userId,
    email: userEmail,
    userAgent: req.get('User-Agent'),
    ipAddress: req.ip,
  };

  try {
    const features = {
      newDashboard: await client.getBooleanValue('new-dashboard', false, context),
      welcomeMessage: await client.getStringValue('welcome-message', 'Welcome!', context),
      maxItems: await client.getNumberValue('max-items-per-page', 10, context),
    };

    res.json(features);
  } catch (error) {
    console.error('Feature flag evaluation error:', error);
    res.status(500).json({ error: 'Failed to evaluate features' });
  }
});

// Alternative example with authentication middleware
interface AuthenticatedRequest extends express.Request {
  user?: {
    id: string;
    email: string;
    plan: string;
  };
}

app.get('/api/user-features', async (req: AuthenticatedRequest, res) => {
  // Assuming you have authentication middleware that sets req.user
  const context = {
    targetingKey: req.user?.id || 'anonymous',
    email: req.user?.email,
    plan: req.user?.plan || 'free',
    userAgent: req.get('User-Agent'),
    ipAddress: req.ip,
  };

  try {
    const features = {
      premiumFeature: await client.getBooleanValue('premium-feature', false, context),
      dashboardTheme: await client.getStringValue('dashboard-theme', 'default', context),
    };

    res.json(features);
  } catch (error) {
    console.error('Feature flag evaluation error:', error);
    res.status(500).json({ error: 'Failed to evaluate features' });
  }
});

Complete Example

Here's a complete example of how to use the Bucketeer OpenFeature Node.js server provider:

import { OpenFeature } from '@openfeature/server-sdk';
import { BucketeerProvider, defineBKTConfig } from '@bucketeer/openfeature-node-server-sdk';

async function main() {
  try {
    // Configure Bucketeer
    const config = defineBKTConfig({
      apiKey: process.env.BUCKETEER_API_KEY!,
      apiEndpoint: process.env.BUCKETEER_API_ENDPOINT!,
      featureTag: process.env.BUCKETEER_FEATURE_TAG!,
      appVersion: '1.0.0',
    });

    // Initialize provider with optional timeout
    const provider = new BucketeerProvider(config, {
      initializationTimeoutMs: 30000, // 30 seconds
    });

    // Set provider and wait for initialization
    await OpenFeature.setProviderAndWait(provider);

    // Get client
    const client = OpenFeature.getClient('my-app');

    // Simulate user request - define evaluation context per user/request
    const userContext = {
      targetingKey: 'user-123',
      email: '[email protected]',
      plan: 'premium',
      timestamp: new Date().toISOString(),
    };

    // Evaluate feature flags with context
    const isNewFeatureEnabled = await client.getBooleanValue('new-feature', false, userContext);
    const welcomeMessage = await client.getStringValue('welcome-message', 'Welcome!', userContext);
    const maxItems = await client.getNumberValue('max-items', 10, userContext);

    console.log(`New feature enabled: ${isNewFeatureEnabled}`);
    console.log(`Welcome message: ${welcomeMessage}`);
    console.log(`Max items: ${maxItems}`);

    // Example of different user context
    const anotherUserContext = {
      targetingKey: 'user-456',
      email: '[email protected]',
      plan: 'free',
    };

    const featureForAnotherUser = await client.getBooleanValue(
      'new-feature',
      false,
      anotherUserContext,
    );
    console.log(`Feature for another user: ${featureForAnotherUser}`);
  } catch (error) {
    console.error('Error:', error);
  } finally {
    // Clean up when your application shuts down
    await OpenFeature.close();
  }
}

main();

Contributing

We would ❤️ for you to contribute to Bucketeer and help improve it! Anyone can use and enjoy it!

Please follow our contribution guide here.

Development

Environment

  • Node.js
    • check ./.nvmrc for the required version
  • yarn
    • package manager

You need a .env file to provide API secrets for testing. Copy .env.example and rename it to .env, then update it with your Bucketeer credentials.

Commands

# Install dependencies
yarn install

# Build the project
yarn build

# Run tests
yarn test

# Run end-to-end tests
yarn test:e2e

# Lint code
yarn lint

# Format code
yarn prettier:fix

Example Application

Check the example/ directory for a complete Express.js application demonstrating how to use this SDK.

cd example
npm install
npm run build
npm run dev

License

Apache License 2.0, see LICENSE.

版本与发布记录

@bucketeer/openfeature-node-server-sdk 共发布 3 个版本,首次发布于 2025/11/21;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.3latest2026/08/073 个月前
0.0.22026/06/294 个月前
0.0.12025/11/2111 个月前

关于 @bucketeer/openfeature-node-server-sdk 的常见问题

@bucketeer/openfeature-node-server-sdk 是什么?

This is the official Node.js server-side OpenFeature provider for accessing your feature flags with Bucketeer.

如何安装 @bucketeer/openfeature-node-server-sdk?

在项目目录运行 npm install @bucketeer/openfeature-node-server-sdk,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@bucketeer/node-server-sdk(^0.4.5)、@openfeature/server-sdk(^1.19.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bucketeer/openfeature-node-server-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bucketeer/openfeature-node-server-sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bucketeer/openfeature-node-server-sdk 可以商用吗?

@bucketeer/openfeature-node-server-sdk 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bucketeer/openfeature-node-server-sdk 的下载量有多少?

根据 npm 官方下载统计,@bucketeer/openfeature-node-server-sdk 在 2026/10/09 – 2026/11/07 这 30 天内被下载 10,272 次,日均约 342 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bucketeer/openfeature-node-server-sdk 还在维护吗?

最新版本 0.0.3 发布于 2026/08/07(3 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/11/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。