• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTTP request freshness testing

fresh

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

HTTP response freshness testing

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install fresh

API

var fresh = require('fresh')

fresh(reqHeaders, resHeaders)

Check freshness of the response using request and response headers.

When the response is still "fresh" in the client's cache true is returned, otherwise false is returned to indicate that the client cache is now stale and the full response should be sent.

When a client sends the Cache-Control: no-cache request header to indicate an end-to-end reload request, this module will return false to make handling these requests transparent.

Known Issues

This module is designed to only follow the HTTP specifications, not to work-around all kinda of client bugs (especially since this module typically does not receive enough information to understand what the client actually is).

There is a known issue that in certain versions of Safari, Safari will incorrectly make a request that allows this module to validate freshness of the resource even when Safari does not have a representation of the resource in the cache. The module jumanji can be used in an Express application to work-around this issue and also provides links to further reading on this Safari bug.

Example

API usage

var reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { etag: '"bar"' }
fresh(reqHeaders, resHeaders)
// => false

var reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { etag: '"foo"' }
fresh(reqHeaders, resHeaders)
// => true

Using with Node.js http server

var fresh = require('fresh')
var http = require('http')

var server = http.createServer(function (req, res) {
  // perform server logic
  // ... including adding ETag / Last-Modified response headers

  if (isFresh(req, res)) {
    // client has a fresh copy of resource
    res.statusCode = 304
    res.end()
    return
  }

  // send the resource
  res.statusCode = 200
  res.end('hello, world!')
})

function isFresh (req, res) {
  return fresh(req.headers, {
    etag: res.getHeader('ETag'),
    'last-modified': res.getHeader('Last-Modified')
  })
}

server.listen(3000)

License

MIT

More Repositories

1

http-errors

Create HTTP Errors
JavaScript
1,489
star
2

mime-types

The ultimate javascript content-type utility.
JavaScript
1,311
star
3

cookie

HTTP server cookie parsing and serialization
JavaScript
1,303
star
4

mime-db

Media Type Database
JavaScript
1,083
star
5

basic-auth

Generic basic auth Authorization header field parser
JavaScript
704
star
6

on-finished

Execute a callback when a request closes, finishes, or errors
JavaScript
389
star
7

negotiator

An HTTP content negotiator for Node.js
JavaScript
307
star
8

statuses

HTTP status utility
JavaScript
269
star
9

accepts

Higher-level content negotiation
JavaScript
252
star
10

etag

Create simple HTTP ETags
JavaScript
251
star
11

type-is

Infer the content-type of a request.
JavaScript
226
star
12

content-disposition

Create and parse HTTP Content-Disposition header
JavaScript
220
star
13

methods

HTTP verbs that node supports
JavaScript
177
star
14

on-headers

Execute a listener when a response is about to write headers.
JavaScript
154
star
15

http-assert

assert with status codes
JavaScript
151
star
16

proxy-addr

Determine address of proxied request
JavaScript
132
star
17

content-type

Create and parse HTTP Content-Type header
JavaScript
130
star
18

style-guide

jshttp style guide
JavaScript
108
star
19

compressible

Compressible Content-Type / mime checking.
JavaScript
98
star
20

range-parser

Range header field parser
JavaScript
90
star
21

vary

Manipulate the HTTP Vary header
JavaScript
61
star
22

forwarded

Parse HTTP X-Forwarded-For header
JavaScript
56
star
23

media-typer

Simple RFC 6838 media type parser and formatter
JavaScript
54
star
24

jshttp.github.io

HTML
47
star
25

spdy-push

SPDY Push helper - will by replaced by http-push
JavaScript
29
star
26

http-push

14
star
27

http-utils

Low-level HTTP parsing/formatting utilities
JavaScript
14
star
28

.github

1
star