Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

browserProfile.ts

69 lines2.5 KB

1import * as Schema from "effect/Schema";
2import { TrimmedNonEmptyString } from "./baseSchemas.ts";
3​
4export const BROWSER_PROFILE_NAME_MAX_LENGTH = 48;
5export const BROWSER_PROFILE_MAX_COUNT = 24;
6​
7/**
8 * Control characters are rejected because ids are folded into delimiter-joined
9 * cache keys on the client; one carrying the delimiter would resolve to a
10 * different profile's partition.
11 */
12export const BrowserProfileId = TrimmedNonEmptyString.check(
13 Schema.isMaxLength(64),
14 Schema.isPattern(/^[^\p{Cc}]+$/u),
15);
16export type BrowserProfileId = typeof BrowserProfileId.Type;
17​
18export const BrowserProfileName = TrimmedNonEmptyString.check(
19 Schema.isMaxLength(BROWSER_PROFILE_NAME_MAX_LENGTH),
20);
21​
22export const BrowserProfileKind = Schema.Literals(["persistent", "incognito"]);
23export type BrowserProfileKind = typeof BrowserProfileKind.Type;
24​
25export const BrowserProfile = Schema.Struct({
26 id: BrowserProfileId,
27 name: BrowserProfileName,
28 kind: BrowserProfileKind,
29});
30export type BrowserProfile = typeof BrowserProfile.Type;
31​
32export const DEFAULT_BROWSER_PROFILE_ID: BrowserProfileId = "default";
33export const INCOGNITO_BROWSER_PROFILE_ID: BrowserProfileId = "incognito";
34​
35export const BUILT_IN_BROWSER_PROFILES: ReadonlyArray<BrowserProfile> = [
36 { id: DEFAULT_BROWSER_PROFILE_ID, name: "Default", kind: "persistent" },
37 { id: INCOGNITO_BROWSER_PROFILE_ID, name: "Incognito", kind: "incognito" },
38];
39​
40export function isBuiltInBrowserProfileId(id: string): boolean {
41 return BUILT_IN_BROWSER_PROFILES.some((profile) => profile.id === id);
43​
44/**
45 * - Repeated ids, which map to one partition and would otherwise appear as
46 * two isolated identities sharing every cookie.
47 * - `kind: "incognito"` on anything but the built-in, since persistence is
48 * keyed off that one id; such a profile is labelled ephemeral while its
49 * cookies survive restarts.
50 */
51export function resolveBrowserProfiles(
52 userProfiles: ReadonlyArray<BrowserProfile>,
53): ReadonlyArray<BrowserProfile> {
54 const seen = new Set(BUILT_IN_BROWSER_PROFILES.map((profile) => profile.id));
55 const resolved = [...BUILT_IN_BROWSER_PROFILES];
56 for (const profile of userProfiles) {
57 if (seen.has(profile.id)) continue;
58 seen.add(profile.id);
59 resolved.push(profile.kind === "persistent" ? profile : { ...profile, kind: "persistent" });
60 }
61 return resolved;
63​
64export function findBrowserProfile(
65 profiles: ReadonlyArray<BrowserProfile>,
66 id: string | undefined,
67): BrowserProfile | undefined {
68 return id === undefined ? undefined : profiles.find((profile) => profile.id === id);