npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@datatester/node-sdk

v1.2.5

Published

datatester node sdk

Readme

Datatester 分流 Node.js SDK

使用及说明

import AbClient from "@datatester/node-sdk";
// const AbClient = require('@datatester/node-sdk').default

(async () => {
  // 在rangers应用管理中查询token信息
  const abClient = new AbClient("token");

  // 因为返回的是promise,业务使用上需要自己处理reject行为
  try {
    const conf = await abClient.activate(
      "key",
      "decisionId",
      "trackId",
      new Map(),
      ""
    );
    // 获取key版本配置
    const config = conf.val;
  } catch (e) {
    console.error(e);
  }
})();

初始化参数

| 名词 | 类型 | 说明 | 备注 | | ----------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | | token | string | app 级别的 token, 可以在 rangers 控制台应用列表中查询 | | metaHost | string | 获取 meta 数据服务 host, 默认为 AbClient.metaHost.cn | | trackHost | string | 上报实验曝光数据, 默认为 AbClient.trackHost.cn | | interval | number | 元数据更新间隔时间,单位秒,默认 60s | | metaType | string | 元数据类型, 可选值:'cdn', 'server', 'file' | 对文件读取一次,不会按照 interval 去更新数据 | | metaFilePath | string | 元数据文件本地路径,使用前确保应用对文件有可读权限 | metaType==='file'时候才生效 | | enableFile | boolean | 开启日志文件保存 | | dir | string | 日志写入的文件路径, 默认为当前应用启动的根路径, 确保应用对路径有写路径 | enableFile=true 情况下日志写入路径, | | maxSize | string | 文件最大值 | | level | string | 需要记录的日志等级 可选值:'console','info', 'warn', 'error' | | formatter | function | 对日志做自定义格式化输出 | 参数请参照 types/plugins/logger/TLog | | trackMaxNum | number | 一次上报的事件量个数,可选值 1-50,默认每 10 秒会自动上报一次,一次上报 50 条 | | UserAbInfoHandler | object | 用户若需缓存相关功能,请实现此对象,包含查询和保存两个方法 | { query: (decisionId: string) => Promise; createOrUpdate: (decisionId: string, flight2VersionStr: string) => Promise; } |

接口及参数

接口类型参照 types/type.d.ts

参数说明

  • 入参

| 参数 | 类型 | 说明 | | ------------ | ---------------- | --------------------------------------------------- | | interval | number | 元数据获取时间间隔 | | experimentId | string | 实验 id | | decisionId | string | 决策 ID, 分流口径, 和系统中设置的白名单数据行为一致 | | featureId | string | feature flag id | | trackId | string | 上报数据唯一标识 | | attributes | Map<string, any> | 分流的受众数据,和系统中设置的目标受众做比对 |

  • 返回值(TConfig)

| 参数 | 类型 | 说明 | | --------- | ------ | --------------------------------------------------- | | val | string | 元数据获取时间间隔 | | vid | string | 实验 id | | entity_id | string | 决策 ID, 分流口径, 和系统中设置的白名单数据行为一致 |

接口说明

| 接口 | 返回值 | 说明 | | --------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------- | ----------------------------------------------------------------------------------------------------- | | getExperimentVariantName(experimentId: string, decisionId: string, attributes: Map<string, any>, options?: TOptions) | Promise<string> | 命中分流结果名称 | | getExperimentVariantNameWithImpression(experimentId: string, decisionId: string, trackId: string, attributes: Map<string, any>, options?: TOptions) | Promise<string> | 命中分流结果名称并曝光实验信息 | | getExperimentConfigs(experimentId: string, decisionId: string, attributes: Map<string, any>, options?: TOptions) | Promise<TConfig> | 命中分流结果配置信息 | | getExperimentConfigsWithImpression(experimentId: string, decisionId: string, trackId: string, attributes: Map<string, any>, options?: TOptions) | Promise<TConfig> | 命中分流结果配置信息并曝光信息 | | getAllExperimentConfigs(decisionId: string, attributes: Map<string, any>, options?: TOptions) | Promise<TConfig> | 获取 app 下所有实验命中分流结果配置 | | verifyFeatureEnabled(featureId: string, decisionId: string, attributes: Map<string, any>, options?: TOptions) | Promise<boolean> | 检验 feature flag 是否命中 | | getFeatureConfigs(featureId: string, decisionId: string, attributes: Map<string, any>, options?: TOptions) | Promise<TConfig> | feature flag 分流结果配置 | | getFeatureConfigsWithImpression(featureId: string, decisionId: string, trackId: string, attributes: Map<string, any>, options?: TOptions) | Promise<TConfig> | feature flag 分流结果配置并曝光信息 | | getAllFeatureConfigs(decisionId: string, attributes: Map<string, any>, options?: TOptions) | Promise<TConfig> | 命中的所有 feature flag 版本信息 | | getEnabledFeatureIds(decisionId: string, attributes: Map<string, any>, options?: TOptions) | Promise<string[]> | 获取所有命中的 feature flag id | | activate(variantKey: string, decisionId: string, trackId: string, attributes: Map<string, any>, defaultValue: unkown, options?: TOptions) | Promise<{val: any}> | 根据 key 做分流,并上报实验信息; 返回逻辑:命中的实验信息 -> 命中的 feature flag 信息 -> 传入的默认值 | | close() | void | 主动关闭所有元数据服务的 schedule job | | setInterval(interval: number) | void | 动态调整获取元数据的间隔时间 |

不同场景使用

自定义获取元数据和实验信息曝光地址

  • SaaS 服务不同区域的配置
import AbClient from '@datatester/node-sdk'

// metaHost
// cn: AbClient.metaHost.cn
// sg: AbClient.metaHost.sg
// va: AbClient.metaHost.va

// trackHost
// cn: AbClient.trackHost.cn
// sg: AbClient.trackHost.sg
// va: AbClient.trackHost.va

const abSdk = new AbClient('token', {
    trackHost: AbClient.trackHost.sg
    metaHost: AbClient.metaHost.sg
})
  • 私有化服务
import AbClient from '@datatester/node-sdk'
const abSdk = new AbClient('token', {
    trackHost: '' // 可访问到的上报http接口的host
    metaHost: ''// 可访问到的获取元数据http接口的host
})
  • 监听内部日志

    sdk 对外暴露了一个 EventEmitter 的实现,可以对 sdk 内部一些日志进行监听

// meta获取成功
abSdk.on("meta.success", (data) => {});
// meta获取失败
abSdk.on("meta.failure", (error) => {});
// ...