chrome-har

中文文档
2.0.0发布于 2026/11/03MITES Module

Create HAR files from Chrome Debugging Protocol data.

原始使用文档
文档目录3 节
  1. Create a new bug report
  2. Stream messages
  3. Support for Response Bodies

Chrome-har

Unit tests

Create HAR files based on Chrome DevTools Protocol data.

We got tired of waiting for the +30K engineers at Google implementing a way to automate to get a HAR file https://issues.chromium.org/issues/40809195 so we implemented our own solution in the mean time.

Code originally extracted from Browsertime, initial implementation inspired by Chromedriver_har.

Create a new bug report

Make sure to generate a event trace log file that we can use to recreate your issue. If you use Browsertime you can enable the trace with --chrome.collectPerfLog:

$ browsertime --chrome.collectPerfLog -n 1 https://www.sitespeed.io

Then take the file named chromePerflog-1.json.gz and put it in a gist or make it availible to us in any way you want.

If you use sitespeed.io:

$ sitespeed.io --browsertime.chrome.collectPerfLog -n 1 https://www.sitespeed.io

Stream messages

harFromMessages takes an array (or any iterable) of messages. If the messages come from a stream, use harFromMessagesAsync. It takes an async iterable, handles each message when it arrives and returns a promise for the HAR. You do not need to keep all messages in memory.

import { harFromMessagesAsync } from 'chrome-har';

const har = await harFromMessagesAsync(messageStream);

Support for Response Bodies

If you use Chrome-har standalone (without Browsertime/sitespeed.io) you can use get the response bodies in HARs if they are set on the response object by the caller and if the includeTextFromResponseBody option is set to true.

For example:

const harEvents: Array<any> = [];

client.on('Network.requestIntercepted', async (params: any) => {
  // Get the response body
  const response = await client.send(
    'Network.getResponseBodyForInterception',
    { interceptionId: params.interceptionId },
  );

  // Set the body on the response object
  if (params.response != null) {
    params.response.body = response.body;
  } else {
    params.response = response;
  }

  // Continue the request
  await client.send(
    'Network.continueInterceptedRequest',
    { interceptionId: params.interceptionId },
  );

  harEvents.push({ method, params });
});

const har = harFromMessages(harEvents, {includeTextFromResponseBody: true});

版本与发布记录

chrome-har 共发布 46 个版本,首次发布于 2017/03/05;过去 12 个月发布了 5 个版本。

版本发布日期距今
2.0.0latest2026/11/03今天
1.3.12026/05/195 个月前
1.3.02026/05/065 个月前
1.2.12026/04/226 个月前
1.2.02026/04/226 个月前
1.1.12025/10/171 年前

关于 chrome-har 的常见问题

chrome-har 是什么?

从Chrome调试协议数据创建HAR文件。

如何安装 chrome-har?

在项目目录运行 npm install chrome-har,或使用 pnpm add / yarn add / bun add。

chrome-har 支持 TypeScript 吗?

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

chrome-har 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

chrome-har 需要什么版本的 Node.js?

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

chrome-har 可以商用吗?

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

chrome-har 还在维护吗?

最新版本 2.0.0 发布于 2026/11/03(今天);至今共发布 46 个版本,其中过去 12 个月发布了 5 个;首次发布于 2017/03/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。