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

@mjackson/form-data-parser

v0.5.1

Published

A request.formData() wrapper with streaming file upload handling

Downloads

8,356

Readme

form-data-parser

form-data-parser is a wrapper around request.formData() that provides streaming support for handling file uploads. This is useful in server contexts where large files should be streamed to disk or some cloud storage service like AWS S3 or Cloudflare R2 instead of being buffered in memory.

Features

  • Drop-in replacement for request.formData() with support for streaming file uploads
  • Built on the standard web Streams API and File API
  • Does not buffer any content, minimal memory usage
  • Automatically falls back to native request.formData() implementation for non-multipart/form-data requests

The Problem

The web fetch API's built-in request.formData() method is not a great fit for server environments because it doesn't provide a way to stream file uploads. This means that when you call request.formData() in a server environment on a request that was submitted by a <form enctype="multipart/form-data">, any file uploads contained in the request are buffered in memory. For small files this may not be an issue, but it's a total non-starter for large files that exceed the server's memory capacity.

form-data-parser fixes this issue by providing an API to handle streaming file data.

Installation

Install from npm:

npm install @mjackson/form-data-parser

Usage

import { LocalFileStorage } from '@mjackson/file-storage/local';
import { type FileUpload, parseFormData } from '@mjackson/form-data-parser';

const fileStorage = new LocalFileStorage('/uploads/user-avatars');

async function uploadHandler(fileUpload: FileUpload) {
  // Is this file upload from the <input type="file" name="user-avatar"> field?
  if (fileUpload.fieldName === 'user-avatar') {
    let storageKey = `user-${user.id}-avatar`;

    // FileUpload objects are not meant to stick around for very long (they are
    // streaming data from the request.body!) so we should store them as soon as
    // possible.
    await fileStorage.set(storageKey, fileUpload);

    // Return a File for the FormData object. This is a LazyFile that knows how
    // to access the file's content if needed (using e.g. file.stream()) but
    // waits until it is requested to actually read anything.
    return fileStorage.get(storageKey);
  }

  // Ignore any files we don't recognize the name of...
}

async function requestHandler(request: Request) {
  let formData = await parseFormData(request, uploadHandler);

  let file = formData.get('user-avatar'); // File
  file.name; // "my-avatar.jpg" (name of the file on the user's computer)
  file.size; // number
  file.type; // "image/jpeg"
}

Related Packages

  • file-storage - A simple key/value interface for storing FileUpload objects you get from the parser
  • multipart-parser - The parser used internally for parsing multipart/form-data HTTP messages

License

See LICENSE