@esm2cjs/http-timer

中文文档
5.0.1发布于 2022/08/19MITTypeScriptESM + CJS近 30 天下载 1.4万

Timings for HTTP requests. This is a fork of @szmarczak/http-timer, but with CommonJS support.

原始使用文档
文档目录5 节
  1. Installation
  2. Usage
  3. API
  4. timer(request)
  5. License

http-timer

Timings for HTTP requests

Build Status Coverage Status install size

Inspired by the request package.

Installation

NPM:

npm install @szmarczak/http-timer

Yarn:

yarn add @szmarczak/http-timer

Usage

Note:

  • The measured events resemble Node.js events, not the kernel ones.
  • Sending a chunk greater than highWaterMark will result in invalid upload and response timings. You can avoid this by splitting the payload into smaller chunks.
import https from 'https';
import timer from '@szmarczak/http-timer';

const request = https.get('https://httpbin.org/anything');
timer(request);

request.once('response', response => {
    response.resume();
    response.once('end', () => {
        console.log(response.timings); // You can use `request.timings` as well
    });
});

// {
//   start: 1572712180361,
//   socket: 1572712180362,
//   lookup: 1572712180415,
//   connect: 1572712180571,
//   upload: 1572712180884,
//   response: 1572712181037,
//   end: 1572712181039,
//   error: undefined,
//   abort: undefined,
//   phases: {
//     wait: 1,
//     dns: 53,
//     tcp: 156,
//     request: 313,
//     firstByte: 153,
//     download: 2,
//     total: 678
//   }
// }

API

timer(request)

Returns: Object

Note: The time is a number representing the milliseconds elapsed since the UNIX epoch.

  • start - Time when the request started.
  • socket - Time when a socket was assigned to the request.
  • lookup - Time when the DNS lookup finished.
  • connect - Time when the socket successfully connected.
  • secureConnect - Time when the socket securely connected.
  • upload - Time when the request finished uploading.
  • response - Time when the request fired response event.
  • end - Time when the response fired end event.
  • error - Time when the request fired error event.
  • abort - Time when the request fired abort event.
  • phases
    • wait - timings.socket - timings.start
    • dns - timings.lookup - timings.socket
    • tcp - timings.connect - timings.lookup
    • tls - timings.secureConnect - timings.connect
    • request - timings.upload - (timings.secureConnect || timings.connect)
    • firstByte - timings.response - timings.upload
    • download - timings.end - timings.response
    • total - (timings.end || timings.error || timings.abort) - timings.start

If something has not been measured yet, it will be undefined.

License

MIT

版本与发布记录

@esm2cjs/http-timer 共发布 1 个版本,首次发布于 2022/08/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.1latest2022/08/194 年前

关于 @esm2cjs/http-timer 的常见问题

@esm2cjs/http-timer 是什么?

Timings for HTTP requests. This is a fork of @szmarczak/http-timer, but with CommonJS support.

如何安装 @esm2cjs/http-timer?

在项目目录运行 npm install @esm2cjs/http-timer,或使用 pnpm add / yarn add / bun add。

@esm2cjs/http-timer 支持 TypeScript 吗?

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

@esm2cjs/http-timer 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@esm2cjs/http-timer 需要什么版本的 Node.js?

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

@esm2cjs/http-timer 可以商用吗?

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

@esm2cjs/http-timer 的下载量有多少?

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

@esm2cjs/http-timer 还在维护吗?

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