@unikvs/cookies
How to use @unikvs/cookies, which stores strings and bytes in HTTP cookies.
Overview
@unikvs/cookies is a storage backed by HTTP cookies.
This storage converts strings and bytes into a cookie-storable form. Bytes are Base64-encoded, and keys and values are URL-encoded before storing.
nameisCookies.isOpenis alwaystrue.open()checks cookie store availability.- All operations are synchronous.
deletethrowsKeyNotFoundErrorfor a missing key.- Stream reading and writing are not supported.
Install with:
npm install @unikvs/cookiespnpm add @unikvs/cookiesyarn add @unikvs/cookiesbun add @unikvs/cookiesnub add @unikvs/cookiesaube add @unikvs/cookiesUsage
Cookies implements IStorage. Pass it to UniKvs with persist().
import { Cookies } from "@unikvs/cookies";
Cookies
Storage backed by cookies.
Constructor arguments:
cookieAttributes?CookieAttributes
Attributes attached to every cookie. Specify path, domain, secure, sameSite, and maxAge.
CookieAttributessizeLimitBytes?number
Maximum size (bytes) applied to both the raw value size and the serialized size.
number4096store?CookieStoreLike
Store used to read and write cookies. Uses document.cookie when omitted.
CookieStoreLikeThe following example stores a value and reads it back.
import { Cookies } from "@unikvs/cookies";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{
foo: unikvs.Value<string>;
}>()
.persist(new Cookies())
.build();
await kvs.open();
await kvs.set("foo", "hello");
const data = await kvs.get("foo");
console.log(data); // "hello"
await kvs.close();
Data types
| Operation | Supported data type |
| ––––––– | ––––––––––––– | ———— |
| write/read | Strings and bytes (string | Uint8Array) |
- Stores strings and bytes. Strings are stored with an
s:prefix, bytes as Base64 with ab:prefix. - Values whose raw size (UTF-8 bytes for strings, byte length for bytes) exceeds
sizeLimitBytescannot be stored. - Even when the raw size is within the limit, a value cannot be stored when the size of the Set-Cookie string, including the URL-encoded name, value, and attributes, exceeds
sizeLimitBytes. writethrows aTypeErrorwhen the key is empty.- Inject a
CookieStoreLikeasstorein environments withoutdocument.cookie. - Stream reading and writing are not supported.
Errors
This package throws the following errors.
CookieTooLargeError
keystring
The key being written.
stringactualnumber
The size (bytes) that exceeded the limit.
numberlimitnumber
The applied sizeLimitBytes.
numbermeasurement"raw" | "serialized"
Whether actual is the raw size or the serialized size. Defaults to "raw" when omitted from the constructor.
"raw" | "serialized"Meaning
write throws it when the raw size (UTF-8 bytes for strings, byte length for bytes) exceeds sizeLimitBytes, or when the size of the serialized Set-Cookie string exceeds sizeLimitBytes. The serialized size includes the URL-encoded name, the URL-encoded value, and the attributes specified in cookieAttributes. Use meta.measurement to tell which size exceeded the limit.
How to fix
Make the value smaller or raise sizeLimitBytes. When only the serialized size exceeds the limit, shorten the key or value, or reduce the attributes. Browsers also impose their own cookie limits, so store large values in another storage.
CookieNotAvailableError
Meaning
Thrown when document.cookie is unavailable and no store was injected.
How to fix
Pass an implementation satisfying CookieStoreLike as store.
KeyNotFoundError
Meaning
read and delete throw it when the specified key does not exist. A shared error from @unikvs/core.
How to fix
Check with exists before reading or deleting.
Example
The following example catches KeyNotFoundError.
import { KeyNotFoundError } from "@unikvs/cookies";
try {
const data = await kvs.get("missing-key");
} catch (ex) {
if (ex instanceof KeyNotFoundError) {
console.log("Key not found");
} else {
throw ex;
}
}