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

multipart-fetch

v0.1.1

Published

Separate the parts of a multipart stream

Downloads

22

Readme

Multipart Fetch

Split your Fetch Response into a Multipart Response.

Motivation

Multipart parsing libraries typically chunk each part body when delivering it to a consumer. Not only does this force consumers to unnecessarily wait while the part is being delivered to completion, this approach is essentially useless when a message nests multipart bodies. Only when an outer part is completely delivered will the consumer get access to the parts of the nested multipart message.

Multipart Fetch solves these problems by streaming the parts of the multipart response as soon as it receives data from the HTTP stream. It splits up a Fetch Response into part Responses each of which can be serially consumed just like a Response.

Installation

Browser

You can use Multipart Fetch directly in the browser as shown below:

<script type="module">
  import multipartFetch from "https://path/to/multipart-fetch/dist/browser.js";
</script>

CDN

Replace the dummy import path with a link to the bundle provided by your favourite CDN. Find the links/link formats at:

Local

Alternatively you can download the package from npm and use it locally. If you have npm installed, an easy way to do this is:

npm pack multipart-fetch

Unpack the downloaded .tgz file and point the import path to dist/browser.min.js.

JavaScript Runtimes

Install Multipart Fetch using your favorite package manager:

<npm|pnpm|yarn|bun> add multipart-fetch

You can now import Multipart Fetch in your project, as usual:

import multipartFetch from "multipart-fetch";

On Deno, you can link to the bundle directly from source, just like in the browser, or export it from deps.ts.

Usage

  1. fetch() a resource with multipart media-type:

    // Request a resource for multipart media
    let response;
    try {
      response = await fetch("https://example.org/multipart", {
        headers: ["Accept", "multipart/*"],
      });
    } catch (e) {
      // Handle any network errors
    }
  2. Use multipartFetch() to part the response:

    let multipartResponse;
    try {
      multipartResponse = multipartFetch(response);
    } catch (e) {
      // Response was not multipart
      // use the `response` object as usual
    }
  3. Now you can iterate over the parts of the multipartResponse:

    for await (const partResponse of multipartResponse) {
      // Consume just like a fetch response, say,
      console.log(partResponse.json());
    }

    or, if you are using the asyncIterator protocol:

    const partResponse = await multipartResponse.parts();
    let { value, done } = await partResponse.next();
    while (!done) {
      console.log(await value.body());
      ({ value, done } = await partResponse.next());
    }

IMPORTANT: Make sure you read out each part before moving to the next part. This is because the iterator is pulling data from a singular HTTP stream.

Copyright and License

Copyright © 2024, Rahul Gupta and Multipart Fetch contributors.

The source code in this repository is released under the Mozilla Public License v2.0.