npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@apeleghq/multipart-parser

v1.0.15

Published

TypeScript streaming parser for MIME multipart messages

Downloads

75

Readme

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.