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

spdy-referrer-push

v0.0.2

Published

SPDY server push based on referrer headers

Downloads

10

Readme

Referrer-based SPDY server push for ExpressJS Build Status

ExpressJS middleware for SPDY server push based on referrer headers.

Usage

Install the module:

$ npm install spdy-referrer-push

Import the module:

var spdyPush = require('spdy-referrer-push');

Add as middleware to your already SPDY-enabled Express server:

app.use(spdyPush.referrer());

If using the express.static or other resource serving middleware, the spdy-referrer-push middleware must appear before those other middleware in the stack.

That's it!

Examples

Minimal server with SSL/TLS support:

$ npm install spdy-referrer-push spdy express
var spdyPush = require('spdy-referrer-push')
  , spdy = require('spdy')
  , express = require('express')
  , path = require('path')
  , fs = require('fs');

var app = express();
app.use(spdyPush.referrer());
var options = {
  key: fs.readFileSync(path.join(__dirname, '../test/keys/spdy-key.pem')),
  cert: fs.readFileSync(path.join(__dirname, '../test/keys/spdy-cert.pem')),
};
var port = 8443;
spdy.createServer(options, app).listen(port);

Minimal SPDY server without SSL/TLS support (keep in mind that most browsers don't support SPDY without SSL/TLS by default):

var spdyPush = require('spdy-referrer-push')
  , spdy = require('spdy')
  , express = require('express')
  , fs = require('fs');

var app = express();
app.use(spdyPush.referrer());
var options = {
  plain: true,
  ssl: false
};
var port = 8080;
spdy.createServer(options, app).listen(port);

Motivation

Server push is one of the most interesting features of SPDY and the upcoming HTTP 2.0.

Briefly, it allows a server to send additional resources in response to a client (web browser) request unsolicited (e.g. CSS, JavaScript, and images). This makes techniques such as inlining and spriting redundant while allowing for fine-grained control of resource caching.

The excellent node-spdy module provides the plumbing necessary for SPDY support in NodeJS (and Connect/Express, including server push. However, hardcoding which resources to push in the server implementation isn't ideal as this must be kept in sync with references in HTML and CSS files.

The Jetty servlet container for Java has a good solution for this problem, called the ReferrerPushStrategy, where the HTTP Referer request header is used to track what resource requests (say /index.html) trigger subsequent requests for additional resources. This is recorded and used to determine what additional resources to push the next time someone requests the same main resource (say /index.html again).

This module is a re-implementation of that algorithm in JavaScript as Express middleware.

Implementation Notes

The middleware retrieves resources to be pushed by creating an "internal" request/response pair and calling Connect's app.handle function. This function kicks off request handling using the server's middleware stack.

This approach avoids the overhead of a new socket connection and a TLS handshake (for resources served via https) while still supporting dynamic behaviour in the handling of resource requests. For instance, if your CSS resources are transpiled from LESS files on the fly, these CSS resources can still be pushed correctly to the client.

The "internal" request and response objects support the full Express request and response APIs but don't yet support all the optional arguments of the NodeJS Stream API. This is work in progress (see the To Do section).

When To Use

This module provides a simple way to test the impact that SPDY with server push can have on improving latency on your site. Combine it with tc (on Unix systems) or Apple's Network Link Conditioner, to test your site under various latency and bandwidth constraints. You will see the biggest benefit in high latency situations

For serious large scale use I would recommend serving static resources from a web server like Apache httpd or nginx instead of serving them from your NodeJS server. mod_spdy is available for Apache httpd 2.2 or later and supports server push when running as a reverse proxy in front of the NodeJS application via the X-Associated-Content response header. That will take you back to the problem of keeping the list of resources in sync between the NodeJS server and your HTML/CSS though.

To Do

  • ~~Setup Travis CI build.~~
  • The "internal" request and response objects should support the full Stream API, including all optional arguments.
  • Compare referrer header based on scheme + hostname + port rather than just hostname + port.
  • Support for the trust proxy Express application setting.
  • Improved configurability to match the Jetty API.
  • General code cleanup
  • Allow for use with plain Connect (without Express).

Contributors