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

@lufrai/multipart

v0.0.47

Published

Multipart parser for fetch API

Downloads

232

Readme

@hattip/multipart

Multipart parser for Hattip. It can be used to parse multipart requests, especially multipart/form-data for handling file uploads.

The web standards offer Request.prototype.formData for parsing multipart/form-data requests, but it offers no way of enforcing size limits or controlling where the files are stored. Most implementations simply store the files in memory, which is not ideal for large files.

parseMultipartFormData

parseMultipartFormData parses a multipart request body into a MultipartFormData object. It takes a Request and an options object as arguments. The options object is required to have a handleFile property, which is called for each file field in the request. The handleFile function is passed an object with information about the file and process it as needed. The return value will be used as the value for the file field in the MultipartFormData object.

For example a file handler for Node.js module could look like this:

const formData = await parseMultipartFormData(request, {
  async handleFile(info) {
    const tempPath = path.join(TEMP_DIR, info.filename);

    try {
      // Note: you have to consume the body stream or it will be consumed when
      // you return from the handler. You can't save the stream for later use.

      await fs.promises.writeFile(
        tempPath,
        // Node accepts ReadableStream here but the typings don't allow it
        info.body as any,
      );

      return {
        name: info.name,
        filename: info.filename,
        contentType: info.contentType,
        tempPath,
      };
    } catch (error) {
      // Try to delete the partially written file
      await fs.promises.rm(tempPath).catch(() => {});

      // Rethrow the error
      throw error;
    }
  },

  // Other options (all optional) are related to size and number limits or are
  // callbacks for creating various types of errors. See the type definitions
  // for details.
});

The handler, instead of saving the file to disk, could stream it to S3, save it to a database, or cache it in memory, for example.

The returned MultipartFormData object is similar to the standard FormData object, except that it's read-only and file fields are represented by whatever was returned from the handleFile function instead of a standard File object. Note that this means you shouldn't return plain strings from handleFile as it would leave no way to distinguish between a file field and a text field.

parseMultipart

parseMultipart is a lower-level function that parses a multipart request body (a ReadableStream<Uint8Array>) into an async iterable of MultipartPart objects. MultipartPart objects have a headers and a body.