Elffuss BraidHub

kikocis/continue public

main · 4.7 KiBRawPermalink
1type ProcessWithEnv = {
2 env?: Record<string, string | undefined>;
3};
4
5function getProcessEnv(): Record<string, string | undefined> | undefined {
6 if (
7 typeof process !== "undefined" &&
8 process &&
9 typeof process === "object"
10 ) {
11 return (process as ProcessWithEnv).env;
12 }
13
14 if (typeof globalThis !== "undefined") {
15 const maybeProcess = (globalThis as { process?: ProcessWithEnv }).process;
16 return maybeProcess?.env;
17 }
18
19 return undefined;
20}
21
22function getHomeDirectory(): string | undefined {
23 const env = getProcessEnv();
24
25 const fromHome = env?.HOME?.trim();
26 if (fromHome) {
27 return fromHome;
28 }
29
30 const fromUserProfile = env?.USERPROFILE?.trim();
31 if (fromUserProfile) {
32 return fromUserProfile;
33 }
34
35 return undefined;
36}
37
38function expandLeadingTilde(identifier: string): string {
39 const homeDirectory = getHomeDirectory();
40 if (!homeDirectory) {
41 return identifier;
42 }
43
44 // Only replace a leading ~ so relative paths like ../~file stay untouched
45 return homeDirectory + identifier.slice(1);
46}
47
48export interface PackageSlug {
49 ownerSlug: string;
50 packageSlug: string;
51}
52
53export interface FullSlug extends PackageSlug {
54 versionSlug: string;
55}
56
57// Identifier roughly equals URI, except that we want to provide shorthand for slugs and filepaths
58// Later it is possible to allow URIs as well since they can be uniquely parsed
59interface BasePackageIdentifier {
60 uriType: "file" | "slug";
61}
62
63interface FullSlugIdentifier extends BasePackageIdentifier {
64 uriType: "slug";
65 fullSlug: FullSlug;
66}
67
68interface FileIdentifier extends BasePackageIdentifier {
69 uriType: "file";
70 fileUri: string;
71}
72
73export type PackageIdentifier = FullSlugIdentifier | FileIdentifier;
74
75export function packageIdentifierToDisplayName(id: PackageIdentifier): string {
76 switch (id.uriType) {
77 case "file":
78 return id.fileUri;
79 case "slug":
80 return id.fullSlug.packageSlug;
81 }
82}
83
84export function encodePackageIdentifier(identifier: PackageIdentifier): string {
85 switch (identifier.uriType) {
86 case "slug":
87 return encodeFullSlug(identifier.fullSlug);
88 case "file":
89 // For file paths, just return the path directly without a prefix
90 return identifier.fileUri;
91 default:
92 throw new Error(`Unknown URI type: ${(identifier as any).uriType}`);
93 }
94}
95
96export function decodePackageIdentifier(identifier: string): PackageIdentifier {
97 // Shorthand: if it starts with . or /, then it's a path
98 if (identifier.startsWith(".") || identifier.startsWith("/")) {
99 return {
100 uriType: "file",
101 fileUri: identifier,
102 };
103 }
104 // Keep support for explicit file:// protocol
105 else if (identifier.startsWith("file://")) {
106 return {
107 uriType: "file",
108 fileUri: identifier.substring(7),
109 };
110 }
111 // support ~ by replacing with home directory
112 else if (identifier.startsWith("~")) {
113 return {
114 uriType: "file",
115 fileUri: expandLeadingTilde(identifier),
116 };
117 }
118 // Otherwise, it's a slug
119 return {
120 uriType: "slug",
121 fullSlug: decodeFullSlug(identifier),
122 };
123}
124
125export enum VirtualTags {
126 Latest = "latest",
127}
128
129export function encodePackageSlug(packageSlug: PackageSlug): string {
130 return `${packageSlug.ownerSlug}/${packageSlug.packageSlug}`;
131}
132
133export function decodePackageSlug(pkgSlug: string): PackageSlug {
134 const [ownerSlug, packageSlug] = pkgSlug.split("/");
135 return {
136 ownerSlug,
137 packageSlug,
138 };
139}
140
141export function encodeFullSlug(fullSlug: FullSlug): string {
142 return `${fullSlug.ownerSlug}/${fullSlug.packageSlug}@${fullSlug.versionSlug}`;
143}
144
145export function packageSlugsEqual(
146 pkgSlug1: PackageSlug,
147 pkgSlug2: PackageSlug,
148): boolean {
149 return (
150 pkgSlug1.ownerSlug === pkgSlug2.ownerSlug &&
151 pkgSlug1.packageSlug === pkgSlug2.packageSlug
152 );
153}
154
155export function decodeFullSlug(fullSlug: string): FullSlug {
156 const [ownerSlug, packageSlug, versionSlug] = fullSlug.split(/[/@]/);
157 return {
158 ownerSlug,
159 packageSlug,
160 versionSlug: versionSlug || VirtualTags.Latest,
161 };
162}
163
164/**
165 * FQSN = Fully Qualified Secret Name
166 */
167export interface FQSN {
168 packageSlugs: PackageSlug[];
169 secretName: string;
170}
171
172export function encodeFQSN(fqsn: FQSN): string {
173 const parts = [...fqsn.packageSlugs.map(encodePackageSlug), fqsn.secretName];
174 return parts.join("/");
175}
176
177export function decodeFQSN(fqsn: string): FQSN {
178 const parts = fqsn.split("/");
179 const secretName = parts.pop()!;
180 const packageSlugs: PackageSlug[] = [];
181
182 // Process parts two at a time to decode package slugs
183 for (let i = 0; i < parts.length; i += 2) {
184 if (i + 1 >= parts.length) {
185 throw new Error("Invalid FQSN format: package slug must have two parts");
186 }
187 packageSlugs.push({
188 ownerSlug: parts[i],
189 packageSlug: parts[i + 1],
190 });
191 }
192
193 return { packageSlugs, secretName };
194}