/etag-parser

HTTP ETag header field parser

Primary LanguageTypeScriptMIT LicenseMIT

etag-parser

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

test NPM

HTTP ETag header field parser.

Compliant with RFC 9110, 8.8.3. ETag.

Deserialization

Parses string into ETag.

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 });
assertEquals(parseETag(`"123456789"`), { tag: "123456789", weak: false });

Throwing error

Throws SyntaxError if the input is invalid <entity-tag>.

import { parseETag } from "https://deno.land/x/etag_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() => parseETag("<invalid>"));

Serialization

Serialize ETag into string.

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"`);
assertEquals(stringifyETag({ weak: false, tag: "123456789" }), `"123456789"`);

Throwing error

Throws TypeError if ETag contains invalid value.

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

ETag is a structured object for ETag header.

Name Type Description
tag string Representation of <etagc>.
weak boolean Whether this is weak validator or not.

Utilities

Utility functions are provided.

compareWeak

Weak comparison. Two ETag are equivalent if ETag.tag match character-by-character, regardless of either or both being tagged as ETag.weak.

Compliant with RFC 9110, 8.8.3.2. Comparison.

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" },
  ),
);

compareStrong

Strong comparison. Two ETag are equivalent if both are StrongETag and ETag.tag match character-by-character.

Compliant with RFC 9110, 8.8.3.2. Comparison.

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" },
  ),
);

isWeakETag

Whether the ETag is WeakETag or not.

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" }));

isStrongETag

Whether the ETag is StrongETag or not.

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" }));

API

All APIs can be found in the deno doc.

License

Copyright © 2023-present httpland.

Released under the MIT license