@cryptotaxcalculator/axios-cached-dns-resolve

中文文档
4.0.0发布于 2024/05/23MITTypeScriptCommonJS近 30 天下载 4391

Fork of axios-cached-dns-resolve in CJS and with type safety. Caches dns resolutions made with async dns.resolve instead of default sync dns.lookup, refreshes in background

原始使用文档
文档目录6 节
  1. Objectives
  2. Requirements
  3. Getting started
  4. Configuration
  5. Statistics
  6. Express Statistics

@cryptotaxcalculator/axios-cached-dns-resolve

This library is a fork of the original axios-cached-dns-resolve package but adds/changes the following:

  • Adds type safety
  • Changes the module resolution to CJS (from ESM)
  • Changes the test runner from ava to jest
  • Adds support for Redis as a cache

For the complete list of changes, see the CHANGELOG.


Axios uses node.js dns.lookup to resolve host names. dns.lookup is synchronous and executes on limited libuv thread pool. Every axios request will resolve the dns name in kubernetes, openshift, and cloud environments that intentionally set TTL low or to 0 for quick dynamic updates. The dns resolvers can be overwhelmed with the load. There is/was a bug in DNS resolutions that manifests as very long dns.lookups in node.js.

From the kubernetes documentation

Even if apps and libraries did proper re-resolution, the load of every client re-resolving DNS over and over would be difficult to manage.

This library uses dns.resolve and can optionally cache resolutions and round-robin among addresses. The cache size is configurable. If caching is enabled, a background thread will periodically refresh resolutions with dns.resolve rather than every request. There is an idle TTL that evicts background refresh if an address is no longer being used. This lib proxies through the OS resolution mechanism which may provide further caching.

Objectives

  • Async requests - dns resolve vs lookup
  • Fast - local in-app memory cache lookup
  • Fresh - periodically (frequently) updated
  • Constant DNS load/latency vs random load/variable latency
  • Providing statistics and introspection

Requirements

Node 14+

Getting started

npm i -S @cryptotaxcalculator/axios-cached-dns-resolve

Usage

import { registerInterceptor } from "@cryptotaxcalculator/axios-cached-dns-resolve";

const axiosClient = axios.create(config);

registerInterceptor(axiosClient);

Use axiosClient as normal

Configuration

export const config = {
  disabled: process.env.AXIOS_DNS_CACHE_DISABLE === "true",
  dnsTtlMs: parseInt(process.env.AXIOS_DNS_CACHE_TTL_MS || "5000"), // when to refresh actively used dns entries (5 sec)
  cacheGraceExpireMultiplier: parseInt(
    process.env.AXIOS_DNS_CACHE_EXPIRE_MULTIPLIER || "2"
  ), // maximum grace to use entry beyond TTL
  dnsIdleTtlMs:
    parseInt(process.env.AXIOS_DNS_CACHE_IDLE_TTL_MS || "1000") * 60 * 60, // when to remove entry entirely if not being used (1 hour)
  backgroundScanMs: parseInt(
    process.env.AXIOS_DNS_CACHE_BACKGROUND_SCAN_MS || "2400"
  ), // how frequently to scan for expired TTL and refresh (2.4 sec)
  dnsCacheSize: parseInt(process.env.AXIOS_DNS_CACHE_SIZE || "100"), // maximum number of entries to keep in cache
  // pino logging options
  logging: {
    name: "axios-cache-dns-resolve",
    // enabled: true,
    level: process.env.AXIOS_DNS_CACHE_LOG_LEVEL || "info", // default 'info' others trace, debug, info, warn, error, and fatal
    // timestamp: true,
    prettyPrint: process.env.NODE_ENV === "DEBUG" || false,
    formatters: {
      level: (label: string) => {
        return { level: label };
      },
    },
  },
  redisConfig:
    process.env.AXIOS_DNS_CACHE_USE_REDIS === "true"
      ? {
          url: process.env.AXIOS_DNS_CACHE_REDIS_URL || "localhost:6379",
          password: process.env.AXIOS_DNS_CACHE_REDIS_PASSWORD,
          ttl: parseInt(process.env.AXIOS_DNS_CACHE_REDIS_TTL || "5"), // default 5 seconds
        }
      : undefined,
  cache: undefined as LRUCache<string, DnsEntry> | undefined,
} as Config;

config.lruCacheConfig = {
  max: config.dnsCacheSize,
  ttl: config.dnsTtlMs * config.cacheGraceExpireMultiplier, // grace for refresh
} as LRUCacheConfig;

Statistics

Statistics are available via

getStats()

{
  "dnsEntries": 4,
  "refreshed": 375679,
  "hits": 128689,
  "misses": 393,
  "idleExpired": 279,
  "errors": 0,
  "lastError": 0,
  "lastErrorTs": 0
}

AND

getDnsCacheEntries()

[
  {
    "host": "foo-service.domain.com",
    "ips": [
      "51.210.235.165",
      "181.73.135.40"
    ],
    "nextIdx": 1,
    "lastUsedTs": 1604151366910,
    "updatedTs": 1604152691039
  },
  ...
]

Express Statistics

import {
  getStats,
  getDnsCacheEntries,
} from "@cryptotaxcalculator/axios-cached-dns-resolve";

router.get("/axios-dns-cache-statistics", getAxiosDnsCacheStatistics);

function getAxiosDnsCacheStatistics(req, resp) {
  resp.json(getStats());
}

router.get("/axios-dns-cache-entries", getAxiosDnsCacheEntries);

function getAxiosDnsCacheEntries(req, resp) {
  resp.json(getDnsCacheEntries());
}

版本与发布记录

@cryptotaxcalculator/axios-cached-dns-resolve 共发布 6 个版本,首次发布于 2024/05/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.0latest2024/05/232 年前
3.4.02024/05/212 年前
3.3.32024/05/062 年前
3.3.22024/05/022 年前
3.3.12024/05/012 年前
3.3.02024/05/012 年前

关于 @cryptotaxcalculator/axios-cached-dns-resolve 的常见问题

@cryptotaxcalculator/axios-cached-dns-resolve 是什么?

Fork of axios-cached-dns-resolve in CJS and with type safety. Caches dns resolutions made with async dns.resolve instead of default sync dns.lookup, refreshes in background

如何安装 @cryptotaxcalculator/axios-cached-dns-resolve?

在项目目录运行 npm install @cryptotaxcalculator/axios-cached-dns-resolve,或使用 pnpm add / yarn add / bun add。

@cryptotaxcalculator/axios-cached-dns-resolve 支持 TypeScript 吗?

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

@cryptotaxcalculator/axios-cached-dns-resolve 支持 ESM 还是 CommonJS?

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

@cryptotaxcalculator/axios-cached-dns-resolve 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cryptotaxcalculator/axios-cached-dns-resolve 可以商用吗?

@cryptotaxcalculator/axios-cached-dns-resolve 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cryptotaxcalculator/axios-cached-dns-resolve 的下载量有多少?

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

@cryptotaxcalculator/axios-cached-dns-resolve 还在维护吗?

最新版本 4.0.0 发布于 2024/05/23(2 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/05/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。