Function acceptsEncodings

  • Returns an array of content encodings accepted by the request, in order of preference. If there are no encoding supplied in the request, then ["*"] is returned, implying any encoding is accepted.

    Parameters

    • request: Pick<Request, "headers">

      The request to get the acceptable content encodings for.

    Returns string[]

    An array of content encodings this request accepts.

    import { acceptsEncodings } from "@std/http/negotiation";
    import { assertEquals } from "@std/assert";

    const request = new Request("https://example.com/", {
    headers: { "accept-encoding": "deflate, gzip;q=1.0, *;q=0.5" },
    });

    assertEquals(acceptsEncodings(request), ["deflate", "gzip", "*"]);
  • For a given set of content encodings, return the best match accepted in the request. If no content encodings match, then the function returns undefined.

    NOTE: You should always supply identity as one of the encodings to ensure that there is a match when the Accept-Encoding header is part of the request.

    Parameters

    • request: Pick<Request, "headers">

      The request to get the acceptable content encodings for.

    • Rest...encodings: string[]

      An array of encodings to find the best matching one from.

    Returns string | undefined

    The best matching encoding, if any match.

    import { acceptsEncodings } from "@std/http/negotiation";
    import { assertEquals } from "@std/assert";

    const request = new Request("https://example.com/", {
    headers: { "accept-encoding": "deflate, gzip;q=1.0, *;q=0.5" },
    });

    assertEquals(acceptsEncodings(request, "gzip", "identity"), "gzip");