|
| 1 | +import { Err, Ok, type Result } from "@/common/types/result"; |
| 2 | +import { validateWorkspaceName } from "@/common/utils/validation/workspaceValidation"; |
| 3 | +import { buildWorkspaceNameWithSuffix } from "@/common/utils/workspaceNaming"; |
| 4 | +import { findNextForkSuffix, generateForkNameWithSuffix } from "@/node/services/forkNameGenerator"; |
| 5 | + |
| 6 | +export type WorkspaceNameCollisionStrategy = |
| 7 | + | { type: "error" } |
| 8 | + | { type: "random-suffix"; maxAttempts: number } |
| 9 | + | { type: "numeric-suffix" }; |
| 10 | + |
| 11 | +export interface WorkspaceNameResolution { |
| 12 | + name: string; |
| 13 | + suffix?: number; |
| 14 | +} |
| 15 | + |
| 16 | +function generateRandomSuffix(): string { |
| 17 | + return Math.random().toString(36).substring(2, 6); |
| 18 | +} |
| 19 | + |
| 20 | +function validateResolvedName(name: string): Result<string> { |
| 21 | + const validation = validateWorkspaceName(name); |
| 22 | + if (!validation.valid) { |
| 23 | + return Err(validation.error ?? "Invalid workspace name"); |
| 24 | + } |
| 25 | + return Ok(name); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Resolve a workspace name using a collision strategy and shared validation rules. |
| 30 | + */ |
| 31 | +export function resolveWorkspaceName( |
| 32 | + requestedName: string, |
| 33 | + existingNames: Set<string>, |
| 34 | + strategy: WorkspaceNameCollisionStrategy |
| 35 | +): Result<WorkspaceNameResolution> { |
| 36 | + const validation = validateWorkspaceName(requestedName); |
| 37 | + if (!validation.valid) { |
| 38 | + return Err(validation.error ?? "Invalid workspace name"); |
| 39 | + } |
| 40 | + |
| 41 | + if (strategy.type === "numeric-suffix") { |
| 42 | + const suffix = findNextForkSuffix(requestedName, existingNames); |
| 43 | + const candidate = generateForkNameWithSuffix(requestedName, suffix); |
| 44 | + const resolved = validateResolvedName(candidate); |
| 45 | + if (!resolved.success) { |
| 46 | + return Err(resolved.error); |
| 47 | + } |
| 48 | + return Ok({ name: resolved.data, suffix }); |
| 49 | + } |
| 50 | + |
| 51 | + if (!existingNames.has(requestedName)) { |
| 52 | + return Ok({ name: requestedName }); |
| 53 | + } |
| 54 | + |
| 55 | + if (strategy.type === "error") { |
| 56 | + return Err(`Workspace with name "${requestedName}" already exists`); |
| 57 | + } |
| 58 | + |
| 59 | + const attemptedNames = new Set(existingNames); |
| 60 | + for (let attempt = 0; attempt < strategy.maxAttempts; attempt++) { |
| 61 | + const suffix = generateRandomSuffix(); |
| 62 | + const candidate = buildWorkspaceNameWithSuffix(requestedName, suffix); |
| 63 | + if (attemptedNames.has(candidate)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + attemptedNames.add(candidate); |
| 67 | + const resolved = validateResolvedName(candidate); |
| 68 | + if (!resolved.success) { |
| 69 | + return Err(resolved.error); |
| 70 | + } |
| 71 | + return Ok({ name: resolved.data }); |
| 72 | + } |
| 73 | + |
| 74 | + return Err(`Unable to resolve unique workspace name for "${requestedName}"`); |
| 75 | +} |
0 commit comments