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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tars/config

v2.0.5

Published

TARS 框架中用于获取服务配置文件

Downloads

22

Readme

Tars-Config

Tars 框架中用于获取服务配置文件。

安装

npm install @tars/config

实例化

使用前需先实例化 var config = new TarsConfig(data) 对象

其中:

data: 为 tars 配置文件路径 或 已配置的 @tars/config-parser 实例。

如果服务通过 node-agent (或在Tars平台)运行,则无需传入 data

枚举

FORMAT

定义了配置文件的格式:

  • C: C++ 服务格式
  • JSON: JSON 格式
  • TEXT: 普通文本(自定义格式)

LOCATION

定义了配置文件存放的区域:

  • APP: 配置文件存放于业务集下
  • SERVER: 配置文件存放于服务下

接口

loadConfig([files ,]options)

获取配置文件内容。

files(String|Array) 可以为单一文件名也可是数组,如不填写则默认获取所有文件内容。

options(Object) 为可选项,接受如下参数:

  • format: 文件格式, 默认为 FORMAT.C
  • location: 存放区域, 默认为 LOCATION.SERVER

调用成功后返回(Promise.resolve)由如下对象组成的数组:

  • filename: 文件名
  • content: 文件解析后内容

如仅获取单一文件则返回文件解析后的内容

例子

获取 a.conf 文件内容:

config.loadConfig("a.conf").then(function(data) {
    console.log("content:", data);
}, function (err) {
    console.error("loadConfig err", err);
});

获取 a.conf 文件内容并以 json 进行解析:

config.loadConfig("a.conf", {format : config.FORMAT.JSON}).then(function(data) {
    console.log("content:", data);
}, function (err) {
    console.error("loadConfig err", err);
});

获取存在于业务集中的 a.conf 文件内容:

config.loadConfig("a.conf", {location : config.LOCATION.APP}).then(function(data) {
    console.log("content:", data);
}, function (err) {
    console.error("loadConfig err", err);
});

获取 a.confb.conf 文件内容:

config.loadConfig(["a.conf", "b.conf"]).then(function(data) {
    data.forEach(function(item) {
        console.log("filename:", item.filename);
        console.log("content:", item.content);
    });
}, function (err) {
    console.error("loadConfig err", err);
});

获取服务所有配置文件内容:

config.loadConfig().then(function(data) {
    data.forEach(function(item) {
        console.log("filename:", item.filename);
        console.log("content:", item.content);
    });
}, function (err) {
    console.error("loadConfig err", err);
});

loadList(options)

获取配置文件列表(所有配置文件名)。

options(Object) 为可选项,接受如下参数

  • location: 存放区域, 默认为 LOCATION.SERVER

调用成功后返回(Promise.resolve)由文件名组成的数组。

例子

获取服务的所有配置文件名:

config.loadList().then(function(filelist) {
    console.log("files:", filelist);
}, function(err) {
    console.log("loadList error", err);
});

loadServerConfig(options)

获取默认配置文件(文件名由 App.Server.conf 组成)。

options(Object) 为可选项,接受如下参数

  • format: 文件格式, 默认为 FORMAT.C

调用成功后返回(Promise.resolve)返回文件解析后的内容。

例子

获取服务默认配置文件:

config.loadServerConfig().then(function(data) {
    console.log("content:", data);
}, function(err) {
    console.log("loadServerConfig error", err);
});

事件

configPushed

由Tars平台向服务Push配置文件的时将触发此事件。

回调会给出Push下发的文件名。

例子

监听Push事件,并获取Push文件内容:

config.on("configPushed", function(filename) {
    console.log("config pushed", filename);
    config.loadConfig(filename).then(function(data) {
        console.log("content:", data);
    }, function(err) {
        console.error("loadConfig err", err);
    });
});