File size: 6,482 Bytes
dc06026 c202a37 dc06026 ec788f0 dc06026 c202a37 dc06026 c202a37 dc06026 c202a37 dc06026 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { dirname } from 'path';
export interface AccountLink {
githubUserId: string;
githubLogin: string;
huggingfaceUsername: string;
huggingfaceAccessToken?: string;
linkedAt: string;
lastUpdated: string;
}
export interface AccountLinksData {
links: AccountLink[];
metadata: {
version: string;
createdAt: string;
lastModified: string;
};
}
export class AccountLinkingService {
private filePath: string;
constructor() {
this.filePath = process.env.ACCOUNT_LINKS_FILE || '/tmp/account-links.json';
this.ensureFileExists();
}
private ensureFileExists(): void {
const dir = dirname(this.filePath);
// Create directory if it doesn't exist
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
// Create file if it doesn't exist
if (!existsSync(this.filePath)) {
const initialData: AccountLinksData = {
links: [],
metadata: {
version: '1.0.0',
createdAt: new Date().toISOString(),
lastModified: new Date().toISOString(),
},
};
this.writeData(initialData);
}
}
private readData(): AccountLinksData {
try {
const content = readFileSync(this.filePath, 'utf-8');
return JSON.parse(content);
} catch (error) {
console.error('Error reading account links file:', error);
// Return default structure if file is corrupted
return {
links: [],
metadata: {
version: '1.0.0',
createdAt: new Date().toISOString(),
lastModified: new Date().toISOString(),
},
};
}
}
private writeData(data: AccountLinksData): void {
try {
data.metadata.lastModified = new Date().toISOString();
writeFileSync(this.filePath, JSON.stringify(data, null, 2), 'utf-8');
} catch (error) {
console.error('Error writing account links file:', error);
throw new Error('Failed to save account links');
}
}
/**
* Create a new account link
*/
createLink(githubUserId: string, githubLogin: string, huggingfaceUsername: string, huggingfaceAccessToken?: string): AccountLink {
const data = this.readData();
const now = new Date().toISOString();
// Check for existing links
const existingGithubLink = data.links.find(link => link.githubUserId === githubUserId);
const existingHfLink = data.links.find(link => link.huggingfaceUsername === huggingfaceUsername);
if (existingGithubLink) {
throw new Error(`GitHub user ${githubLogin} is already linked to HuggingFace user ${existingGithubLink.huggingfaceUsername}`);
}
if (existingHfLink) {
throw new Error(`HuggingFace user ${huggingfaceUsername} is already linked to GitHub user ${existingHfLink.githubLogin}`);
}
const newLink: AccountLink = {
githubUserId,
githubLogin,
huggingfaceUsername,
huggingfaceAccessToken,
linkedAt: now,
lastUpdated: now,
};
data.links.push(newLink);
this.writeData(data);
console.log(`β
Created account link: ${githubLogin} β ${huggingfaceUsername}`);
return newLink;
}
/**
* Find account link by GitHub user
*/
findByGitHubUser(githubUserId: string): AccountLink | null {
const data = this.readData();
return data.links.find(link => link.githubUserId === githubUserId) || null;
}
/**
* Find account link by HuggingFace user
*/
findByHuggingFaceUser(huggingfaceUsername: string): AccountLink | null {
const data = this.readData();
return data.links.find(link => link.huggingfaceUsername === huggingfaceUsername) || null;
}
/**
* Update an existing account link
*/
updateLink(githubUserId: string, updates: Partial<Omit<AccountLink, 'githubUserId' | 'linkedAt'>>): AccountLink {
const data = this.readData();
const linkIndex = data.links.findIndex(link => link.githubUserId === githubUserId);
if (linkIndex === -1) {
throw new Error(`No account link found for GitHub user ID: ${githubUserId}`);
}
const existingLink = data.links[linkIndex];
const updatedLink: AccountLink = {
...existingLink,
...updates,
lastUpdated: new Date().toISOString(),
};
data.links[linkIndex] = updatedLink;
this.writeData(data);
console.log(`β
Updated account link for GitHub user: ${existingLink.githubLogin}`);
return updatedLink;
}
/**
* Update HuggingFace access token for an existing account link
*/
updateAccessToken(githubUserId: string, accessToken: string): AccountLink {
return this.updateLink(githubUserId, { huggingfaceAccessToken: accessToken });
}
/**
* Remove an account link
*/
removeLink(githubUserId: string): boolean {
const data = this.readData();
const initialLength = data.links.length;
data.links = data.links.filter(link => link.githubUserId !== githubUserId);
if (data.links.length < initialLength) {
this.writeData(data);
console.log(`β
Removed account link for GitHub user ID: ${githubUserId}`);
return true;
}
return false;
}
/**
* Get all account links
*/
getAllLinks(): AccountLink[] {
const data = this.readData();
return data.links;
}
/**
* Get linking statistics
*/
getStats(): { totalLinks: number; lastModified: string } {
const data = this.readData();
return {
totalLinks: data.links.length,
lastModified: data.metadata.lastModified,
};
}
/**
* Check if accounts can be linked (no conflicts)
*/
canLink(githubUserId: string, huggingfaceUsername: string): { canLink: boolean; reason?: string } {
const data = this.readData();
const existingGithubLink = data.links.find(link => link.githubUserId === githubUserId);
if (existingGithubLink) {
return {
canLink: false,
reason: `GitHub account is already linked to HuggingFace user: ${existingGithubLink.huggingfaceUsername}`,
};
}
const existingHfLink = data.links.find(link => link.huggingfaceUsername === huggingfaceUsername);
if (existingHfLink) {
return {
canLink: false,
reason: `HuggingFace account is already linked to GitHub user: ${existingHfLink.githubLogin}`,
};
}
return { canLink: true };
}
}
// Singleton instance
export const accountLinkingService = new AccountLinkingService();
|