Skip to main content
Module

std/http/mod.ts

Deno standard library
Go to Latest
Deprecated

(will be removed after 0.210.0) Import from https://deno.land/std/crypto/unstable_cookie_map.ts instead.

Provides a iterable map interfaces for managing cookies server side.

import * as mod from "https://deno.land/std@0.207.0/http/mod.ts";

Examples

To access the keys in a request and have any set keys available for creating a response:

import {
  CookieMap,
  mergeHeaders
} from "https://deno.land/std@0.207.0/http/unstable_cookie_map.ts";

const request = new Request("https://localhost/", {
  headers: { "cookie": "foo=bar; bar=baz;"}
});

const cookies = new CookieMap(request, { secure: true });
console.log(cookies.get("foo")); // logs "bar"
cookies.set("session", "1234567", { secure: true });

const response = new Response("hello", {
  headers: mergeHeaders({
    "content-type": "text/plain",
  }, cookies),
});

To have automatic management of cryptographically signed cookies, you can use the SecureCookieMap instead of CookieMap. The biggest difference is that the methods operate async in order to be able to support async signing and validation of cookies:

import {
  SecureCookieMap,
  mergeHeaders,
  type KeyRing,
} from "https://deno.land/std@0.207.0/http/unstable_cookie_map.ts";

const request = new Request("https://localhost/", {
  headers: { "cookie": "foo=bar; bar=baz;"}
});

// The keys must implement the `KeyRing` interface.
declare const keys: KeyRing;

const cookies = new SecureCookieMap(request, { keys, secure: true });
console.log(await cookies.get("foo")); // logs "bar"
// the cookie will be automatically signed using the supplied key ring.
await cookies.set("session", "1234567");

const response = new Response("hello", {
  headers: mergeHeaders({
    "content-type": "text/plain",
  }, cookies),
});

In addition, if you have a Response or Headers for a response at construction of the cookies object, they can be passed and any set cookies will be added directly to those headers:

import { CookieMap } from "https://deno.land/std@0.207.0/http/unstable_cookie_map.ts";

const request = new Request("https://localhost/", {
  headers: { "cookie": "foo=bar; bar=baz;"}
});

const response = new Response("hello", {
  headers: { "content-type": "text/plain" },
});

const cookies = new CookieMap(request, { response });
console.log(cookies.get("foo")); // logs "bar"
cookies.set("session", "1234567");

Classes

c
Server
deprecated

Transforms server-sent message objects into strings for the client.

Variables

v
CookieMap
deprecated
v
HTTP_METHODS
deprecated
v
isErrorStatus
deprecated
v
isHttpMethod
deprecated
v
isRedirectStatus
deprecated
v
mergeHeaders
deprecated
v
SecureCookieMap
deprecated
v
ServerSentEvent
deprecated
v
Status
deprecated
v
STATUS_TEXT
deprecated

Functions

Returns an array of media types accepted by the request, in order of preference. If there are no media types supplied in the request, then any media type selector will be returned.

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.

Returns an array of languages accepted by the request, in order of preference. If there are no languages supplied in the request, then ["*"] is returned, imply any language is accepted.

Calculate an ETag for an entity. When the entity is a specific set of data it will be fingerprinted as a "strong" tag, otherwise if it is just file information, it will be calculated as a weak tag.

Set the cookie header with empty value in the headers to delete it

Parse cookies of a header

Parse set-cookies of a header

A helper function that takes the value from the If-Match header and a calculated etag for the target. By using strong comparison, return true if the values match, otherwise false.

A helper function that takes the value from the If-None-Match header and a calculated etag for the target entity and returns false if the etag for the entity matches the supplied value, otherwise true.

f
serve
deprecated
f
serveListener
deprecated
f
serveTls
deprecated

Set the cookie header properly in the headers

Interfaces

I
ConnInfo
deprecated

Just the part of Deno.FileInfo that is required to calculate an ETag, so partial or user generated file information can be passed.

I
ServeInit
deprecated
I
ServerInit
deprecated
I
ServeTlsInit
deprecated