Skip to content
This repository was archived by the owner on Jul 25, 2025. It is now read-only.

multipart-parser v0.10.0

Compare
Choose a tag to compare
@github-actions github-actions released this 13 Jun 17:33
· 43 commits to main since this release

This release represents a major refactoring and simplification of this library from a async/promise-based architecture to a generator that suspends the parser as parts are found.

This is a reversion to the generator-based interface used before v0.8 when I switched to a promise interface to get around deadlock issues with consuming part streams inside a yield suspension point. The deadlock occurred when trying to read part.body inside a yield, because the parser was suspended and wouldn't emit any more bytes to the stream while the consumer was waiting for the stream to complete.

With this release, I realized that instead of getting rid of the generator, which is actually a fantastic interface for a streaming parser, I should've gotten rid of the part.body stream instead and replaced it with a part.content property that contains all the content for that part. This gives us a better parser interface and also makes error handling simpler when e.g. the parser's maxFileSize is exceeded. This also makes the parser easier to use because you don't have to e.g. await part.text() anymore, and you have access to part.size up front.

  • BREAKING CHANGE: parseMultipart and parseMultipartRequest are now generators that yield MultipartPart objects as they are parsed
  • BREAKING CHANGE: parseMultipart no longer parses streams, use parseMultipartStream instead
  • BREAKING CHANGE: parser.parse() is removed
  • BREAKING CHANGE: part.body, part.bodyUsed are removed
  • BREAKING CHANGE: part.arrayBuffer, part.bytes, part.text are now sync getters instead of async methods
  • BREAKING CHANGE: Default maxFileSize is now 2MiB, same as PHP's default upload_max_filesize

New APIs:

  • parseMultipartStream(stream, options) is a generator that parses a stream of data
  • parser.write(chunk) and parser.finish() are low-level APIs for running the parser directly
  • part.content is a Uint8Array[] of all content in that part
  • part.isText is true if the part originates from a text field
  • part.size is the total size of the content in bytes

If you're upgrading, check the README for current usage recommendations. Here's a high-level taste of the before/after of this release.

import { parseMultipartRequest } from '@mjackson/multipart-parser';

// before
await parseMultipartRequest(request, async (part) => {
  let buffer = await part.arrayBuffer();
  // ...
});

// after
for await (let part of parseMultipartRequest(request)) {
  let buffer = part.arrayBuffer;
  // ...
}