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

@httpland/etag-middleware

v1.1.0

Published

HTTP ETag middleware

Downloads

187

Readme

etag-middleware

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

HTTP ETag middleware.

Compliant with RFC 9110, 8.8.3. ETag.

Middleware

For a definition of Universal HTTP middleware, see the http-middleware project.

Usage

From the response, calculate the ETag and add it to the ETag header.

import {
  etag,
  type Handler,
} from "https://deno.land/x/etag_middleware@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const middleware = etag();
declare const request: Request;
declare const handler: Handler;

const response = await middleware(request, handler);

assertEquals(
  response.headers.get("etag"),
  `W/"<hex:SHA-1:Content-Type,body>"`,
);

yield:

ETag: W/"<hex:SHA-1:Content-Type,body>"

The Default ETag is a hexadecimal representation of the SHA-1 digest of the response body and Content-Type.

This is a weak validator.

ETag computation

The ETag is computed from the body and the response header.

By default, it is a hash of the stream of body and specific headers.

This satisfies most of the requirements for a strong validator. However, middleware cannot guarantee that the following requirements will be met:

For example, if the origin server sends the same validator for a representation with a gzip content coding applied as it does for a representation with no content coding, then that validator is weak.

For this reason, the default is to compute as a weak validator.

ETag Strategy

ETag computation strategy.

| Name | Type | Default | Description | | ------- | ---------- | ------------------ | --------------------------------------------------------------------- | | strong | boolean | false | Whether the validator is strong or not. | | headers | string[] | ["content-type"] | Semantically significant header related with the representation data. | | digest | Digest | SHA-1 digest | Compute digest. |

Strong

The strong field specifies whether the validator is strong or not.

Middleware cannot guarantee that it is a strong validator.

This is because the body and Content-Encoding can be changed by other processes after the ETag computing.

In that case, the ETag must be recomputed.

If you can guarantee that this situation will not occur, you can change to strong validator.

import {
  etag,
  type Handler,
} from "https://deno.land/x/etag_middleware@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const middleware = etag({ strong: true });
declare const request: Request;
declare const handler: Handler;

const response = await middleware(request, handler);
assertEquals(response.headers.get("etag"), `"<hex:SHA-1:Content-Type,body>"`);

Headers

Additional metadata to uniquely identify representation data. Default is ["content-type"].

The strong validator requires uniqueness to include metadata such as Content-Type in addition to the body.

By adding a header, a hash value is computed from the stream of the body and the specified header.

import {
  etag,
  type Handler,
} from "https://deno.land/x/etag_middleware@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const middleware = etag({ headers: [] });
declare const request: Request;
declare const handler: Handler;

const response = await middleware(request, handler);
assertEquals(response.headers.get("etag"), `W/"<hex:SHA-256:body>"`);

Digest

Specifies digest function. Default is digestSha1.

function digestSha1(data: ArrayBuffer): Promise<ArrayBuffer> {
  return crypto.subtle.digest("SHA-1", data);
}

data is an ArrayBuffer that concatenates the headers specified in headers and body.

import {
  etag,
  type Handler,
} from "https://deno.land/x/etag_middleware@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const middleware = etag({
  digest: (data) => crypto.subtle.digest("SHA-256", data),
});
declare const request: Request;
declare const handler: Handler;

const response = await middleware(request, handler);
assertEquals(
  response.headers.get("etag"),
  `W/"<hex:SHA-256:Content-Type,body>"`,
);

Effects

Middleware will effect following:

  • HTTP Headers
    • ETag

Conditions

Middleware will execute only if the following conditions are met:

  • Response body is successful(2xx) status code
  • Response body exists
  • Response body is readable
  • ETag header does not exist in response

API

All APIs can be found in the deno doc.

License

Copyright © 2023-present httpland.

Released under the MIT license