@apeleghq/multipart-parser

中文文档
1.0.21发布于 2025/10/13ISCTypeScriptESM + CJS构建来源已验证近 30 天下载 8155

TypeScript streaming parser for MIME multipart messages

原始使用文档
文档目录7 节
  1. Features
  2. 🚀 Installation
  3. 🎬 Usage
  4. 📚 API
  5. Example
  6. 🤝 Contributing
  7. 📄 License

Multipart Message Parser

Reliability Rating Vulnerabilities Bugs Security Rating Maintainability Rating NPM Downloads

This is a library for parsing MIME multipart messages, such as those used in HTTP requests and email messages, written in TypeScript. It provides an easy-to-use async generator that returns the parsed headers and body of each part in a multipart message. Nested multipart messages are supported.

Features

  • Parses multipart messages according to the specification
  • Supports nested multipart messages
  • Lightweight and fast
  • Written in TypeScript, but can be used with plain JavaScript as well
  • Well-tested

🚀 Installation

You can install the library using either npm or yarn:

npm install @apeleghq/multipart-parser
yarn add @apeleghq/multipart-parser

🎬 Usage

The library exports the function parseMultipartMessage, which returns an async generator that yields objects with the headers and body (as a Uint8Array) of each part in the multipart message.

📚 API

parseMultipartMessage(stream: ReadableStream, boundary: string): AsyncGenerator

This function takes a ReadableStream and a boundary string as arguments, and returns an asynchronous generator that yields objects with the following properties:

  • headers: a Headers object containing the headers of the current part
  • body: a Uint8Array containing the body of the current part, or null if the part is empty.
  • parts: a nested iterator of the same structure for any parts within the current part, if the part's Content-Type header indicates that it is a multipart message.

boundaryRegex: RegExp

A regular expression that can be used to validate a boundary string.

boundaryMatchRegex: RegExp

A regular expression that can be used to extract a boundary string from a Content-Type header.

encodeMultipartMessage(boundary: string, msg: AsyncIterable<TDecodedMultipartMessage>): ReadableStream<ArrayBuffer>

This function takes a boundary string and an array of messages as arguments and returns a ReadableStream that can be read to obtain a multipart message.

TDecodedMultipartMessage is defined as an object with the following fields:

  • headers: a Headers object containing the headers of the current part
  • body (optional): The body of the current part, or null if the part is empty. It can be any of the following types: ArrayBuffer, Blob, ReadableStream or any typed array, such as Uint8Array.
  • parts (optional): An async or sync iterable of one element or more of the same type (TDecodedMultipartMessage), for nested messages. If both body and parts are specified, body takes precedence.

Example

import { parseMultipartMessage } from '@apeleghq/multipart-message-parser';

const decoder = new TextDecoder();

const stream = ... // a ReadableStream containing the multipart message
const boundary = 'my-boundary'; // the boundary of the multipart message

for await (const part of parseMultipartMessage(stream, boundary)) {
  console.log(part.headers.get('content-type'));
  console.log(decoder.decode(part.body));
}

🤝 Contributing

We welcome contributions to this project! Feel free to submit a pull request or open an issue if you find a bug or have a feature request.

📄 License

This library is licensed under the ISC License, see LICENSE for more information.

版本与发布记录

@apeleghq/multipart-parser 共发布 7 个版本,首次发布于 2024/09/12;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.21latest2025/10/131.1 年前
1.0.202025/10/131.1 年前
1.0.192025/10/111.1 年前
1.0.182025/05/211.5 年前
1.0.172025/05/211.5 年前
1.0.162025/03/241.6 年前

关于 @apeleghq/multipart-parser 的常见问题

@apeleghq/multipart-parser 是什么?

TypeScript streaming parser for MIME multipart messages

如何安装 @apeleghq/multipart-parser?

在项目目录运行 npm install @apeleghq/multipart-parser,或使用 pnpm add / yarn add / bun add。

@apeleghq/multipart-parser 支持 TypeScript 吗?

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

@apeleghq/multipart-parser 支持 ESM 还是 CommonJS?

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

@apeleghq/multipart-parser 需要什么版本的 Node.js?

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

@apeleghq/multipart-parser 可以商用吗?

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

@apeleghq/multipart-parser 的下载量有多少?

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

@apeleghq/multipart-parser 还在维护吗?

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