Skip to content
Source
Sign inSign up

browserProfile.ts

59 lines2.2 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/** Control characters could collide with client cache-key delimiters and select another partition. */
8export const BrowserProfileId = TrimmedNonEmptyString.check(
9 Schema.isMaxLength(64),
10 Schema.isPattern(/^[^\p{Cc}]+$/u),
11);
12export type BrowserProfileId = typeof BrowserProfileId.Type;
13​
14export const BrowserProfileName = TrimmedNonEmptyString.check(
15 Schema.isMaxLength(BROWSER_PROFILE_NAME_MAX_LENGTH),
16);
17​
18export const BrowserProfileKind = Schema.Literals(["persistent", "incognito"]);
19export type BrowserProfileKind = typeof BrowserProfileKind.Type;
20​
21export const BrowserProfile = Schema.Struct({
22 id: BrowserProfileId,
23 name: BrowserProfileName,
24 kind: BrowserProfileKind,
25});
26export type BrowserProfile = typeof BrowserProfile.Type;
27​
28export const DEFAULT_BROWSER_PROFILE_ID: BrowserProfileId = "default";
29export const INCOGNITO_BROWSER_PROFILE_ID: BrowserProfileId = "incognito";
30​
31export const BUILT_IN_BROWSER_PROFILES: ReadonlyArray<BrowserProfile> = [
32 { id: DEFAULT_BROWSER_PROFILE_ID, name: "Default", kind: "persistent" },
33 { id: INCOGNITO_BROWSER_PROFILE_ID, name: "Incognito", kind: "incognito" },
34];
35​
36export function isBuiltInBrowserProfileId(id: string): boolean {
37 return BUILT_IN_BROWSER_PROFILES.some((profile) => profile.id === id);
39​
40/** Deduplicates ids sharing a partition; only the built-in incognito id is actually ephemeral. */
41export function resolveBrowserProfiles(
42 userProfiles: ReadonlyArray<BrowserProfile>,
43): ReadonlyArray<BrowserProfile> {
44 const seen = new Set(BUILT_IN_BROWSER_PROFILES.map((profile) => profile.id));
45 const resolved = [...BUILT_IN_BROWSER_PROFILES];
46 for (const profile of userProfiles) {
47 if (seen.has(profile.id)) continue;
48 seen.add(profile.id);
49 resolved.push(profile.kind === "persistent" ? profile : { ...profile, kind: "persistent" });
50 }
51 return resolved;
53​
54export function findBrowserProfile(
55 profiles: ReadonlyArray<BrowserProfile>,
56 id: string | undefined,
57): BrowserProfile | undefined {
58 return id === undefined ? undefined : profiles.find((profile) => profile.id === id);