Skip to content
jshttpPublic

About

HTTP request freshness testing

Topics

Resources

Code of conduct

Security policy

Stars

163 stars

Watchers

10 watching

Forks

Repository files navigation

fresh

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

HTTP response freshness and request precondition testing.

Installation

$ npm install fresh

API

import { allowsRange, fresh, preconditionFailed } from "fresh";

Check freshness of the response using a lowercase-keyed request headers object and a response object with a getHeader(name) method. The method uses lowercase header names. A header value of null or undefined is treated as missing on both the request and response.

fresh(reqHeaders, res)

fresh() returns true when the request validators allow a 304 Not Modified for the selected representation. It returns false when the request has no validators or validation fails. The caller decides whether the request method and response status permit a 304.

The FreshHeaders type includes if-none-match and if-modified-since. fresh() reads the response’s etag or last-modified header as needed.

preconditionFailed(reqHeaders, res)

Check the GET and HEAD validators that can lead to 412 Precondition Failed. Returns true if a precondition fails and false otherwise.

If-Match uses strong ETag comparison and takes precedence over If-Unmodified-Since. A selected representation satisfies If-Match: * even without an ETag.

The PreconditionHeaders type includes if-match and if-unmodified-since. preconditionFailed() reads the response’s etag or last-modified header as needed.

allowsRange(reqHeaders, res, options?)

For a GET with Range, call allowsRange() after checking for 412 and 304. It returns true if If-Range is absent or matches the selected representation. If it returns true, process Range as usual. If it returns false, ignore Range and send the full representation.

An ETag must match exactly and be strong. Characters inside the quotes are compared as supplied. To allow an HTTP-date match, pass { lastModifiedIsStrong: true } when the caller knows the response’s Last-Modified is a strong validator. The date must match exactly.

The RangeHeaders type accepts if-range. allowsRange() checks If-Range and reads the response’s etag or last-modified header as needed.

Example

API usage

const reqHeaders = { "if-none-match": '"foo"' };
const headers = { etag: '"bar"' };
const res = {
  getHeader(name) {
    return headers[name];
  },
};

fresh(reqHeaders, res);
// => false

headers.etag = '"foo"';
fresh(reqHeaders, res);
// => true

Using with Node.js http server

import { createServer } from "node:http";
import { fresh, preconditionFailed } from "fresh";

const server = createServer(function (req, res) {
  if (req.method !== "GET" && req.method !== "HEAD") {
    res.writeHead(405, { Allow: "GET, HEAD" });
    res.end();
    return;
  }

  res.statusCode = 200;
  res.setHeader("ETag", '"hello-world-v1"');

  const headers = req.headersDistinct;

  if (preconditionFailed(headers, res)) {
    res.statusCode = 412;
    res.end();
    return;
  }

  if (fresh(headers, res)) {
    res.statusCode = 304;
    res.end();
    return;
  }

  res.end("hello, world!");
});

server.listen(3000);

Using with Fetch

const reqHeaders = {
  "if-none-match": request.headers.get("if-none-match"),
  "if-modified-since": request.headers.get("if-modified-since"),
};

const isFresh = fresh(reqHeaders, {
  getHeader(name) {
    return response.headers.get(name);
  },
});

License

MIT

About

HTTP request freshness testing

Topics

Resources

Code of conduct

Security policy

Stars

163 stars

Watchers

10 watching

Forks

Releases

Packages

Used by

Contributors

Languages