@balena/happy-eyeballs

中文文档
0.0.6发布于 2021/12/01ISCCommonJS近 30 天下载 4906

patches http(s).Agent to implement happy eyeballs 2 (rfc8305), a standard published by the IETF

原始使用文档
文档目录5 节
  1. @balena-io/happy-eyeballs
  2. Installation
  3. Quick Usage
  4. Explicit Usage
  5. Explanation

@balena-io/happy-eyeballs

This package patches NodeJS's http(s).Agent to implement the IETF standard, "Happy Eyeballs 2" (rfc8305).

It improves client performance and reliability by trying concurrently trying multiple ip addresses for a given host name. See Explanation for more detail.

Installation

npm i --save-dev @balena/happy-eyeballs
yarn add --dev @balena/happy-eyeballs

Quick Usage

To use this library, simply import

import '@balena/happy-eyeballs/eye-patch';

or

require('@balena/happy-eyeballs/eye-patch')

to the top of your .js/.ts entry file.

Note: this will replace http.Agent.prototype.createConnection and https.Agent.prototype.createConnection, but it tries to preserve existing functionality as much as possible.

Explicit Usage

If you want to be more explicit, you can explicitly patch the http(s).Agent:

import { patch } from '@balena/eye-patch';
import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';

patch(HttpAgent, {delay: 300});
patch(HttpsAgent, {delay: 300});

Although, this is exactly what the import '@balena/happy-eyeballs/eye-patch'; does anyway.

Note that delay refers to the delay before attempting the next addresses. This is the delay browsers use.

Later you can unpatch, which will undo the last patch:

unpatch([HttpsAgent, HttpAgent])

or reset to the original values of an agent:

reset([HttpsAgent, HttpAgent])

Also, note that the default is HttpAgent and HttpsAgent, so this has the same effect as

reset()

And that goes for patch and unpatch too.

You could also implement your own agent and replace the createConnection method:

import { createConnection } from '@balena/happy-eyeballs';
import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';

export class MyHttpAgent extends HttpAgent {
  createConnection = createConnection;
  delay = 250;
}

export class MyHttpsAgent extends HttpsAgent {
  createConnection = createConnection;
  delay = 250;
}

This does basically the same thing as the previous examples though.

Explanation

Essentially, the algorithm amounts to this:

  1. Receive a hostname
  2. Have we connected to this host before?
  • If yes:
    • Try the family (Ipv4 or IPv6) of last successful connection if we have connected to this host before
    • If connection is not successful within 300ms, proceed to next step
  • If no: proceed to next step.
  1. Did the DNS lookup return both IPv4 and IPv6 addresses?
  • If yes: try, both addresses concurrently until all have been tried
  • If no: just try the address of the existent family
  1. Proceed with each address in chain, trying both families concurrently until either: *. A connection is made *. All connection attempts time out *. All connection attempt fail
  2. If no connection was successful, return error of the first connection attempt or a "time out" error if all connections timed out

版本与发布记录

@balena/happy-eyeballs 共发布 19 个版本,首次发布于 2021/11/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.6latest2021/12/014 年前
0.0.6-package-build-3cc0eb6f13ac1fc755cf10a0fc940ade1b606e322021/11/304 年前
0.0.6-package-build-a554def501c1c94944101917c473d7b8e60a2fb62021/11/304 年前
0.0.6-package-build-0f1f94a5f02402fc8c81564104cad20efe254bbc2021/11/304 年前
0.0.6-package-build-cacfde8ffb2b7bbb10cdc25f3b3cda7db07ef5b22021/11/304 年前
0.0.6-package-build-74550418be6d7c21ba65150904f645504b4edbe12021/11/304 年前

关于 @balena/happy-eyeballs 的常见问题

@balena/happy-eyeballs 是什么?

patches http(s).Agent to implement happy eyeballs 2 (rfc8305), a standard published by the IETF

如何安装 @balena/happy-eyeballs?

在项目目录运行 npm install @balena/happy-eyeballs,或使用 pnpm add / yarn add / bun add。

@balena/happy-eyeballs 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/balena__happy-eyeballs 是否存在。

@balena/happy-eyeballs 支持 ESM 还是 CommonJS?

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

@balena/happy-eyeballs 可以商用吗?

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

@balena/happy-eyeballs 的下载量有多少?

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

@balena/happy-eyeballs 还在维护吗?

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