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

express-prefer-wait

v1.2.0

Published

Express middleware for handling HTTP 'Prefer: wait' header for static files

Downloads

5

Readme

Express Middleware for HTTP "Prefer: wait" Header

This module implements RFC 7240's "Prefer: wait", indicating to a server that it can wait until the header expires to finish processing a request. In the case of this server, it waits for a file to appear instead of immediately responding with a 404 or 304. This allows clients to request files that they know will exist soon (like MPEG-DASH segments during live streaming) and receive them immediately after they are created.

Warning: Due to limitations in Node.js's fs.watch, we can't tell when a writer finishes writing a file. If you use this module, you should atomically move files into the folder (using rename for example). In future versions I intend to fix this by using inotify directly and waiting for IN_WRITE_CLOSE.

Installing

npm install express-prefer-wait

Example

var express = require("express");
var expressPreferWait = require("express-prefer-wait");

var staticFolder = "static";
var port = 8080;

var app = express;
app.use(expressPreferWait(staticFolder));
app.use(express.static(staticFolder));
app.listen(port);

Note that the "Prefer: wait" middleware should generally come before any static file servers, since all the middleware does is hold onto the request until the requested file appears or the timeout expires.

Specifically, the API is:

expressPreferWait(rootDirectory, options = {etag = false, indexes = ["index.html"], maxTimeout = 60})

  • rootDirectory - The root directory to serve files from. Should be the same as your static file server's root directory.
  • etag - Set to true to wait until etag changes before sending existing file.
  • indexes - List of files to watch when a directory is requested.
  • maxTimeout - The maximum time to wait for a file to appear (in seconds). The actual timeout will be the minimum of maxTimeout and the client's "wait" preference.

Here's an example setting the maximum timeout to 10 seconds, turning on etag behavior, and watching for both "index.html" and "index.htm":

var indexes = ["index.html", "index.htm"];
app.use(expressTimeout(staticFolder, {etag: true, index: indexes, maxTimeout: 10}));
app.use(express.static(staticFolder, {index: indexes);

For etags, etag(fstat) is used in order to match send's behavior ("send" is the module Express's static middleware uses for etags).

See this repo for a more complete example (in CoffeeScript).