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

prep-fetch

v0.1.0

Published

Prepare your Fetch Response to receive Per Resource Events notifications

Downloads

103

Readme

PREP Fetch

Prep your Fetch Response to receive Per Resource Events notifications.

PREP Fetch is thin wrapper around Multipart Fetch that pre-processes the response to a PREP request giving you separate representation and notification streams.

Installation

Browser

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

<script type="module">
  import prepFetch from "https://path/to/prep-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 prep-fetch

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

JavaScript Runtimes

Install PREP Fetch using your favorite package manager:

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

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

import prepFetch from "prep-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. Request PREP notifications using Fetch and invoke PREP Fetch on the response:

    let response;
    try {
      response = await fetch("https://example.org/source/of/events", {
        headers: {
          "accept-events": '"prep"',
        },
      });
    } catch (error) {
      // Handle any network errors
    }
    
    const prepResponse = prepFetch(response);
  2. Read representation, say, as text:

    const representation = await prepResponse.getRepresentation();
    console.log(await representation.text());

    IMPORTANT: Read the representation completely before reading notifications as these are being delivered serially on the underlying HTTP response stream.

  3. Now get the notifications and iterate over it:

    const notifications = await prepResponse.getNotifications();
    for await (const notification of notifications) {
      // .message parses the Response body as a internet format message
      // mime-type: `message/rfc822`
      const message = await notification.message();
      // output the notification
      console.log(message.headers);
      // output any body
      console.log(await message.body());
    }

    or, if you are using the asyncIterator protocol:

    const notifications = await prepResponse.getNotifications();
    const notificationsIterator = notifications.notifications();
    let { value, done } = await notificationsIterator.next();
    while (!done) {
      const message = await notification.message();
      console.log(await message.body());
      ({ value, done } = await notificationsIterator.next());
    }

    IMPORTANT: Always read each message completely before proceeding to the next notification.

For a more detailed example, see the "should serve notifications" block in tests/integration/prep-fetch.test.js.

Copyright and License

Copyright © 2024, Rahul Gupta and PREP Fetch contributors.

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