UNPKG

@softr/sdk

Version:

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

85 lines (83 loc) • 3.41 kB
import { softAssert } from "./assertions.js"; import { softrConfig } from "./softr-config.js"; import { getRequestHeader, getResponseHeaders, setResponseHeader } from "@tanstack/react-start/server"; //#region src/cookies.ts function parseCookieHeader(header) { const cookies = {}; if (!header) return cookies; for (const pair of header.split(";")) { const separator = pair.indexOf("="); if (separator === -1) continue; const name = pair.slice(0, separator).trim(); if (name.length === 0) continue; cookies[name] = decode(pair.slice(separator + 1).trim()); } return cookies; } function decode(value) { try { return decodeURIComponent(value); } catch { return value; } } function readRequestCookies() { return parseCookieHeader(getRequestHeader("cookie")); } function serializeCookie(name, value, { maxAge, secure, crossSite = false }) { if (name.startsWith("__Host-") && !secure) throw new Error(`Refusing to write ${name} without Secure: a __Host- cookie is only valid when Secure, Path=/ and free of Domain, and a browser silently discards one that is not.`); const parts = [ `${name}=${encodeURIComponent(value)}`, "Path=/", "HttpOnly" ]; if (secure) parts.push("Secure"); parts.push(...crossSite && secure ? ["SameSite=None"] : sameSiteAttributes(secure), `Max-Age=${maxAge}`); return parts.join("; "); } function sameSiteAttributes(secure) { return partitions(secure) ? ["SameSite=None", "Partitioned"] : ["SameSite=Lax"]; } function partitions(secure) { return secure && (process.env.NODE_ENV !== "production" || (softrConfig().allowEmbedding ?? []).length > 0); } function expired(name, partitioned) { return partitioned ? `${name}=; Path=/; HttpOnly; Secure; SameSite=None; Partitioned; Max-Age=0` : `${name}=; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=0`; } /** * The deletions for one cookie. A browser keeps a partitioned and a plain * cookie of one name apart, and sends both. Which one the app wrote can change * between sign-in and sign-out, when `allowEmbedding` changes. So over https, * delete both. The one left behind would keep the visitor signed in. */ function expireCookie(name, secure) { if (!secure) return [serializeCookie(name, "", { maxAge: 0, secure })]; return [expired(name, false), expired(name, true)]; } /** * The deletion of the copy the other mode would write. Sent with a new cookie, * so a copy from before `allowEmbedding` changed can't be read in its place. */ function expireOtherCopy(name, secure) { return secure ? [expired(name, !partitions(secure))] : []; } const USER_TIMEZONE_COOKIE = "user-timezone"; function readUserTimezone() { return readRequestCookies()[USER_TIMEZONE_COOKIE] ?? null; } /** What browsers keep of one cookie's name and value. They drop a larger one silently. */ const COOKIE_MAX_BYTES = 4096; function writeCookies(cookies) { const written = cookies.filter((cookie) => { const size = cookie.split(";")[0].length; softAssert(size <= COOKIE_MAX_BYTES, () => `The ${cookie.slice(0, cookie.indexOf("="))} cookie is ${size} bytes, over the ${COOKIE_MAX_BYTES} a browser keeps. It was not written.`); return size <= COOKIE_MAX_BYTES; }); if (written.length === 0) return; setResponseHeader("Set-Cookie", [...getResponseHeaders().getSetCookie(), ...written]); } //#endregion export { expireCookie, expireOtherCopy, readRequestCookies, readUserTimezone, serializeCookie, writeCookies };