Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import type { DomainRoutingConfig } from "./routing.js";
|
||||
|
||||
export type SessionCookieSameSite = "lax";
|
||||
|
||||
export type SessionCookiePolicy = {
|
||||
name?: string;
|
||||
domain?: string;
|
||||
sameSite: SessionCookieSameSite;
|
||||
secure: boolean;
|
||||
httpOnly: boolean;
|
||||
path: "/";
|
||||
maxAge?: number;
|
||||
};
|
||||
|
||||
export type ResolveSessionCookiePolicyOptions = {
|
||||
nodeEnv?: string;
|
||||
routing: Pick<DomainRoutingConfig, "appBaseDomain" | "protocol">;
|
||||
name?: string;
|
||||
maxAge?: number;
|
||||
};
|
||||
|
||||
export type ResolveApiSessionCookiePolicyOptions = Omit<ResolveSessionCookiePolicyOptions, "name">;
|
||||
|
||||
const API_SESSION_COOKIE_NAME = "RJ_api_session";
|
||||
|
||||
export function resolveSessionCookiePolicy({ nodeEnv, routing, name, maxAge }: ResolveSessionCookiePolicyOptions): SessionCookiePolicy {
|
||||
return {
|
||||
...(name ? { name } : {}),
|
||||
...(maxAge === undefined ? {} : { maxAge }),
|
||||
...resolveCookieDomain(routing.appBaseDomain),
|
||||
sameSite: "lax",
|
||||
secure: nodeEnv === "production" || routing.protocol === "https",
|
||||
httpOnly: true,
|
||||
path: "/",
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveApiSessionCookiePolicy(options: ResolveApiSessionCookiePolicyOptions): SessionCookiePolicy {
|
||||
return resolveSessionCookiePolicy({
|
||||
...options,
|
||||
name: API_SESSION_COOKIE_NAME,
|
||||
});
|
||||
}
|
||||
|
||||
function resolveCookieDomain(appBaseDomain: string): Pick<SessionCookiePolicy, "domain"> {
|
||||
const hostname = stripPort(appBaseDomain.trim().toLowerCase());
|
||||
|
||||
if (!hostname || isLocalCookieHost(hostname)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return { domain: hostname };
|
||||
}
|
||||
|
||||
function stripPort(host: string) {
|
||||
if (host.startsWith("[")) {
|
||||
return host.slice(1, host.indexOf("]"));
|
||||
}
|
||||
|
||||
const firstColon = host.indexOf(":");
|
||||
return firstColon === -1 ? host : host.slice(0, firstColon);
|
||||
}
|
||||
|
||||
function isLocalCookieHost(hostname: string) {
|
||||
return hostname === "localhost" || hostname.endsWith(".localhost") || isIpv4Address(hostname) || hostname === "::1";
|
||||
}
|
||||
|
||||
function isIpv4Address(hostname: string) {
|
||||
return /^\d{1,3}(?:\.\d{1,3}){3}$/.test(hostname);
|
||||
}
|
||||
Reference in new issue
Block a user