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

browser-range-response

v1.0.1

Published

## Why?

Downloads

4

Readme

browser-range-response

Why?

While experimenting with CacheStorage in service workers, I discovered that if we use cache.add() / cache.put() on a full HTTP Response, that response is returned by cache.match() even if the request it is matching is for partial content.

This seems to cause problems for the browser - I was using it with an <audio> tag, which uses partial content requests extensively when seeking back and forth. Despite the fact that the entire response was available, I could no longer set currentTime and have the file seek immediately.

What does this do?

This library manually constructs partial responses from full ones, given any request. If the request does not contains a Range header it will simply pass through the response untouched. But if it does, it uses Response.body.getReader() to grab a ReadableStreamReader, and listen for the data as it passes through - discarding the data it doesn't need, and piping the data it does need through a new stream.

How do I use it?

First off, install it:

npm install browser-range-response

The most common usage for this is in response to a service worker fetch event. Simply add it to the end of the promise chain you've used to identify responses, like so:

import checkForRangeRequest from 'browser-range-response';

self.addEventListener('fetch', function(e) {
    e.respondWith(
        caches.match(e.request)
        .then(function (res) {
            return checkForRangeRequest(e.request, res);
        })
        .then(function(res) {
            if (res) {
                return res;
            } else {

                // No cached version, go to remote
                return fetch(e.request);
            }
        })
    )
})

While this will work fine with remote responses (i.e. those from fetch()) I don't recommend using it that way, as you might end up making multiple full requests when you don't mean to. The code outlined above only uses the check for cached responses.

Testing

To test the library, clone this repo, run npm install, then npm test. This will set up a server, and a Browserify watch script to recompile the code whenever you make a change. Go to http://localhost:4001 in your browser to run the Mocha tests there.