@cto.af/pcap-ng-parser

中文文档
3.0.2发布于 2026/03/09BSD-3ES Module构建来源已验证近 30 天下载 2758

Node.js Module for parsing .pcapng files and tcpdump output.

原始使用文档
文档目录5 节
  1. Via .pcapng File for Node.js
  2. Via .pcapng file in Deno
  3. Via .pcapng file in Bun
  4. Via TCPDump in Node.JS
  5. Other Examples

Overview

@cto.af/pcap-ng-parser is a stream-based module to decode, print and analyze network traffic packets. With this module, you can read from an existing .pcap or .pcapng file or connect it to an active stream.

Implements:

Installation

This module is available through the npm registry.

$ npm install @cto.af/pcap-ng-parser

Usage

Note that the package on its own should work just fine in any modern JS runtime (including browsers). However, reading from a file requires some runtime-dependent code.

Via .pcapng File for Node.js

Here is a quick example of how to log out packets to the console from a valid .pcapng file named myfile.pcapng.

import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
import fs from 'node:fs/promises';
const parser = new PCAPNGParser();
const file = await fs.open('examples/res/myfile.pcapng');

parser
  .on('data', parsedPacket => {
    console.log(parsedPacket);
  })
  .on('interface', interfaceInfo => {
    console.log(interfaceInfo);
  });
file.readableWebStream().pipeTo(parser);

In the example above, we create a new ReadableStream from our file and pipe to the instance parser which will cause various events to fire.

Via .pcapng file in Deno

import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
const pcapNgParser = new PCAPNGParser();

pcapNgParser
  .on('data', parsedPacket => {
    console.log(parsedPacket);
  });

const f = await Deno.open('examples/res/myfile.pcapng');
f.readable.pipeTo(pcapNgParser);

Via .pcapng file in Bun

import {PCAPNGParser} from './src/index.ts';
const pcapNgParser = new PCAPNGParser();

pcapNgParser
  .on('data', parsedPacket => {
    console.log(parsedPacket);
  })
  .on('interface', interfaceInfo => {
    console.log(interfaceInfo);
  });

const f = Bun.file('examples/res/myfile.pcapng');
f.stream().pipeTo(pcapNgParser);

Via TCPDump in Node.JS

You can also pipe from TCPDump using process.stdin for a command line interaction.

import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
import {Readable} from 'node:stream';
const pcapNgParser = new PCAPNGParser();

pcapNgParser
  .on('data', parsedPacket => {
    console.log(parsedPacket);
  })
  .on('interface', interfaceInfo => {
    console.log(interfaceInfo);
  });

Readable.toWeb(process.stdin).pipeTo(pcapNgParser);
$ sudo tcpdump -w - | node exampleAbove.js

Note that in order to utilize tcpdump you must be a superuser. Refer to tcpdump documentation for details.

Further note: If you specify an interface to listen on with "-i", tcpdump on some systems uses the old PCAP format, which PCAPNGParser will read just fine.

Other Examples

Additional examples can be found in the examples directory.

It allows you to read from a sample capture file (the default), a file (if you specify a file name on the command line), or stdin (if you specify "-" as the input file).

Class PCAPNGParser

PCAPNGParser is an extension of the stream.Transform class. The PCAPNGParser class fires the following events:

  • data: An Enhanced Packet or Simple block was parsed.
  • section: A Section Header block was parsed.
  • interface: An Interface block was parsed.
  • names: A Name Resolution block was parsed.
  • secrets: A Decryption Secrets block was parsed.
  • stats: An Interface Statistics block was parsed.
  • custom: A Custom block was parsed.
  • blockType: An unknown block type has been received.

See the full API documentation for the types of the event parameters.

Contribution

Refer to the the Contribution Guide for details on how to contribute.

License

This module is covered under the BSD-3 Open Software License. Review the License Documention for more information.

Provenance

This code was forked from https://github.com/CollinearGroup/pcap-ng-parser due to lack of maintenance. To simplify matters, copyright remains with the original authors, including all changes made in this repository. If the original authors contact me (easiest would be to file an issue here), all of this can be changed in any way that suits them.

The following things have been added:

  • Fixed critical RangeError bug
  • Made 'end' and 'finish' events fire correctly by removing broken _finish code. Note that you have to add an event for 'data' to get these events or 'close' to fire, as with all Node Transform streams.
  • Converted to ES6 (BREAKING)
  • Added TypeScript types
  • Brought dependencies up to date
  • Added linting in @cto.af house style (to make maintenance easier)
  • Added test coverage
  • Renamed 'master' branch to 'main'
  • Added Github Actions for testing and publishing
  • Fixed broken links in readme and code
  • Fixed issues with padding for options
  • Now reads all options for supported block types, giving them names when known
  • Fixed issues with reading unexpected sizes in input stream
  • Added support for AbortSignals
  • Added support for multiple Section Header blocks in a stream, including changes of endian-ness.
  • Added support for Simple Packet blocks.
  • Added support for Name Resolution blocks.
  • Added support for Interface Statistics blocks.
  • Added initial Support for Decryption Secrets blocks.
  • Added support for Custom blocks.
  • Added Enhanced Packet flag decoding.
  • Added converting timestamps to JS Dates. Does not handle timezones yet. (BREAKING)
  • Added generate API documentation.
  • Added implementation of old .pcap format, with auto-detection.
  • Added decoding of linkType names.
  • Switched to web streams and Uint8Array, for portability. (BREAKING)

Tests codecov

版本与发布记录

@cto.af/pcap-ng-parser 共发布 7 个版本,首次发布于 2025/10/24;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.2latest2026/03/098 个月前
3.0.12025/12/1510 个月前
3.0.02025/12/1510 个月前
2.1.02025/11/031 年前
2.0.32025/10/241 年前
2.0.22025/10/241 年前

关于 @cto.af/pcap-ng-parser 的常见问题

@cto.af/pcap-ng-parser 是什么?

Node.js Module for parsing .pcapng files and tcpdump output.

如何安装 @cto.af/pcap-ng-parser?

在项目目录运行 npm install @cto.af/pcap-ng-parser,或使用 pnpm add / yarn add / bun add。

@cto.af/pcap-ng-parser 支持 TypeScript 吗?

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

@cto.af/pcap-ng-parser 支持 ESM 还是 CommonJS?

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

@cto.af/pcap-ng-parser 需要什么版本的 Node.js?

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

@cto.af/pcap-ng-parser 可以商用吗?

@cto.af/pcap-ng-parser 使用 BSD-3 许可。声明的许可为 BSD-3,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@cto.af/pcap-ng-parser 的下载量有多少?

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

@cto.af/pcap-ng-parser 还在维护吗?

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