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

pretend-cdn

v0.1.2

Published

Middleware that pretends to go through a CDN like Akamai and delays requests based on cache presence

Downloads

10

Readme

pretend-cdn

Put theory into practice! Sort of. Pretend-cdn helps you with experiencing your CDN cache setup in an easy-to-configure way.

Experience slow hits that go through to your main server, see how blazingly fast your CDN responses are, and notice how file updates on your server are not immediately reflected on CDN. All on your local setup.

Pretend-cdn works by setting a url-based configuration to determine how many CDN servings a response should get. You're not bound to time and expiry headers, but instead use request count (or cache hits) to imitate cache expiry.

Example use case

You provide translations to your frontend app using a request on /i18n.json. The contents of the file may change at any given time, as your copy writer works round the clock and is never satisfied.

A request to your server and rendering the file takes up to 2 seconds per hit, people are not happy. Setting cache expiry headers improves page load times but still requires every visitor to fetch the source from the server once when opening the homepage -- the page that needs to load fastest. You decide to cache the file on CDN.

Your theory is that a 5 minute cache would be ideal, the copy writer will experience a short delay but most requests can hit the CDN instead of the server. You configure Pretend-cdn to serve 5 cached requests for every request forwarded to the main server:

{
	"paths": {
		"^GET:/i18n.json$": "cdn"
	}
}

When you first open the file in your browser you notice it's slow and takes about two seconds for it to be served. It has fetched the file from the server. But then you refresh, and again, and again -- it's fast, coming from CDN.

You make a change in your translations, and refresh. Nothing chaned, the response is fast, but your change is not shown. The old version is still being served from the CDN. Another two refreshes, still nothing new. Once more, it's slow again, the 5 'minutes' have expired, a new version is being fetched from the server, and your changes? They're there. Blazingly fast, with another 5 refreshes to spare.

Installation

npm install pretend-cdn

Usage

var $express = require('express'); 

var $pretendCdn = require('pretend-cdn');

// create server
var app = $express();

// hook up pretend-cdn as middleware
var options = {};
app.use($pretendCdn(options));

// continue serving as usual
app.use($express.static('.'));

app.listen(8000);

Options (configuration)

{
	// show output in console?
	log: false, // default: true
	delays: {
		// delay before returning cached request from CDN
		cdn: 30, // default: 100
		
		// delay before returning uncached request from source
		source: 2000 // default: 2000
	},
	
	// create a profile if you have a specific cache setting
	profiles: {
		
		// custom profile, caches 300 requests on url
		custom: 300,
		
		// default profiles
		// (request count could be seen as minutes of cache validity):
		exclude: 0, // don't cache, ever
		permanent: -1, // cache permanently after first fetch
		cdn: 5, // cache 5 requests after first fetch
		short: 30, // cache 30 requests after first fetch
		long: 1440 // cache 1440 requests after first fetch
		
	},
	
	// key : value
	// key is a regular expression and value the desired profile
	// value is the key of the profiles object
	// key is matched against: req.method + ':' + req.path (i.e. GET:/home)
	paths: {
		'^GET:/home': 'exclude', // use 'exclude' profile for urls starting with /home
		'^GET:.*?cdn$': 'cdn', // use 'cdn' profile for urls ending on '?cdn'
		'^GET:/custom': 'custom' // use 'custom' profile
	}
	
}

Profile strategy

Some predefined profiles have been added to help with caching strategies:

  • exclude - source hits only
    don't cache, is private or always needs fresh version.
  • permanent - 1 source hit, followed by solely CDN hits
    cache indefinitely, assumed to never change on server.
  • cdn - 1 source hit before serving 5 CDN hits
    short caching on CDN for reduced latency and server hits.
  • short - 1 source hit before serving 30 CDN hits
    short caching, i.e. refresh every 30 minutes
  • long - 1 source hit before serving 1440 CDN hits
    long caching, i.e. refresh daily

Potential Roadmap

  • add cache headers to response for browser caching?
  • read cache headers in original response?