HTTP ETag header field parser
npm install @httpland/etag-parser






HTTP ETag header field parser.
Compliant with
RFC 9110, 8.8.3. ETag.
Parses string into ETag.
``ts
import { parseETag } from "https://deno.land/x/etag_parser@$VERSION/parse.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
assertEquals(parseETag(W/"123456789"), { tag: "123456789", weak: true });"123456789"
assertEquals(parseETag(), { tag: "123456789", weak: false });`
Throws SyntaxError if the input is invalid.
`ts
import { parseETag } from "https://deno.land/x/etag_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => parseETag("
`
Serialize ETag into string.
`ts
import { stringifyETag } from "https://deno.land/x/etag_parser@$VERSION/stringify.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
assertEquals(stringifyETag({ weak: true, tag: "123456789" }), W/"123456789");"123456789"
assertEquals(stringifyETag({ weak: false, tag: "123456789" }), );`
Throws TypeError if ETag contains invalid value.
`ts
import { stringifyETag } from "https://deno.land/x/etag_parser@$VERSION/stringify.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => stringifyETag({ tag: "aあ亜", weak: true }));
`
ETag is a structured object for ETag header.
| Name | Type | Description |
| ---- | --------- | ------------------------------------------------------------------------------------------- |
| tag | string | Representation of . |boolean
| weak | | Whether this is weak validator or not. |
Utility functions are provided.
Weak comparison. Two ETag are equivalent if ETag.tag matchETag.weak
character-by-character, regardless of either or both being tagged as.
Compliant with
RFC 9110, 8.8.3.2. Comparison.
`ts
import { compareWeak } from "https://deno.land/x/etag_parser@$VERSION/validate.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";
assert(
compareWeak(
{ weak: true, tag: "123456789" },
{ weak: false, tag: "123456789" },
),
);
`
Strong comparison. Two ETag are equivalent if both are StrongETag andETag.tag match character-by-character.
Compliant with
RFC 9110, 8.8.3.2. Comparison.
`ts
import { compareWeak } from "https://deno.land/x/etag_parser@$VERSION/validate.ts";
import { assert } from "https://deno.land/std/testing/asserts.ts";
assert(
compareWeak(
{ weak: false, tag: "123456789" },
{ weak: false, tag: "123456789" },
),
);
`
Whether the ETag is WeakETag or not.
`ts
import { isWeakETag } from "https://deno.land/x/etag_parser@$VERSION/validate.ts";
import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";
assert(isWeakETag({ weak: true, tag: "123456789" }));
assertFalse(isWeakETag({ weak: false, tag: "123456789" }));
`
Whether the ETag is StrongETag or not.
`ts
import { isStrongETag } from "https://deno.land/x/etag_parser@$VERSION/validate.ts";
import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";
assert(isStrongETag({ weak: false, tag: "123456789" }));
assertFalse(isStrongETag({ weak: true, tag: "123456789" }));
``
All APIs can be found in the
deno doc.
Copyright © 2023-present httpland.
Released under the MIT license