BBizKit

http.etag.parseETag

v0.1.0 latest

Parse a single ETag header value into a structured { weak, value } object per RFC 9110 §8.8.3.

httpetagcachingrfc9110header-parsing

Parse a single ETag header value into a structured { weak, value } object per RFC 9110 §8.8.3.

Signature

function parseETag(input: string): ETag | null

Type Definitions:

  • ETag — interface

Problem

ETag header values require distinguishing strong vs weak validators, extracting the opaque-tag content, and handling the case-sensitive W/ prefix. Manual regex extraction is fragile and commonly implemented incorrectly (e.g. allowing lowercase w/).

How It Works

Handles both strong ETags ("abc") and weak ETags (W/"abc"). The W/ prefix is case-sensitive per RFC 9110. Returns a frozen { weak, value } object.

Boundaries

  • Returns null for empty/whitespace input or malformed ETags.
  • Throws TypeError for non-string input.
  • Does not validate that the opaque-tag content is valid per the grammar.
  • w/"abc" (lowercase w) returns null — RFC 9110 requires uppercase W.

Replaces

Common boilerplate this function replaces:

const match = header.match(/^(W\/)?"(.*)"$/); if (match) { return { weak: !!match[1], value: match[2] }; }

Examples

http.etag.parseETag('"abc"');  // { weak: false, value: "abc" }
http.etag.parseETag('W/"abc"');  // { weak: true, value: "abc" }
http.etag.parseETag('w/"abc"');  // null
http.etag.parseETag('abc');  // null

Standards

Caveats

  • The W/ prefix is case-sensitive per RFC 9110 — lowercase w/ is rejected.
  • Any content between double quotes is accepted as the opaque-tag value.

FAQ

How to parse an ETag header in JavaScript?

http.etag.parseETag('"abc"') returns { weak: false, value: 'abc' }. Handles both strong and weak ETags per RFC 9110.

What is a weak ETag?

A weak ETag is prefixed with W/ (e.g. W/"abc"). It indicates the resource is semantically equivalent but not byte-for-byte identical.

Is the W/ prefix case-sensitive?

Yes. RFC 9110 requires uppercase W. parseETag('w/"abc"') returns null.

What happens with malformed ETag values?

Returns null for any input that doesn't match the ETag grammar (missing quotes, no W/ prefix, etc.).

Can parseETag handle ETags with special characters?

Yes. Any content between double quotes is accepted as the opaque-tag value.

Referenced Standards