|
| 1 | +/** |
| 2 | + * UMS v2.2 URI Scheme Implementation |
| 3 | + * Format: ums://{module-id}#{component-id}/{primitive-type} |
| 4 | + * @see UMS v2.2 Specification Section 4 |
| 5 | + */ |
| 6 | + |
| 7 | +import { PrimitiveType } from '../../types/index.js'; |
| 8 | +import { MODULE_ID_REGEX, COMPONENT_ID_REGEX } from '../../constants.js'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Parsed UMS URI components |
| 12 | + */ |
| 13 | +export interface ParsedURI { |
| 14 | + /** The full URI string */ |
| 15 | + uri: string; |
| 16 | + /** The module ID (authority segment) */ |
| 17 | + moduleId: string; |
| 18 | + /** The component ID (optional, from fragment) */ |
| 19 | + componentId?: string; |
| 20 | + /** The primitive type (optional, from fragment) */ |
| 21 | + primitiveType?: PrimitiveType; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * URI validation result |
| 26 | + */ |
| 27 | +export interface URIValidationResult { |
| 28 | + valid: boolean; |
| 29 | + error?: string; |
| 30 | +} |
| 31 | + |
| 32 | +/** UMS URI protocol prefix */ |
| 33 | +export const UMS_PROTOCOL = 'ums://'; |
| 34 | + |
| 35 | +/** Valid primitive types */ |
| 36 | +const VALID_PRIMITIVE_TYPES = new Set(Object.values(PrimitiveType)); |
| 37 | + |
| 38 | +/** |
| 39 | + * Parse a UMS URI into its components. |
| 40 | + * @param uri - The URI to parse |
| 41 | + * @returns Parsed URI components or null if invalid |
| 42 | + */ |
| 43 | +export function parseURI(uri: string): ParsedURI | null { |
| 44 | + if (!uri.startsWith(UMS_PROTOCOL)) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + const withoutProtocol = uri.slice(UMS_PROTOCOL.length); |
| 49 | + const [authority, fragment] = withoutProtocol.split('#'); |
| 50 | + |
| 51 | + if (!authority || !MODULE_ID_REGEX.test(authority)) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + const result: ParsedURI = { |
| 56 | + uri, |
| 57 | + moduleId: authority, |
| 58 | + }; |
| 59 | + |
| 60 | + if (fragment) { |
| 61 | + const [componentId, primitiveType] = fragment.split('/'); |
| 62 | + |
| 63 | + if (componentId && COMPONENT_ID_REGEX.test(componentId)) { |
| 64 | + result.componentId = componentId; |
| 65 | + } |
| 66 | + |
| 67 | + if ( |
| 68 | + primitiveType && |
| 69 | + VALID_PRIMITIVE_TYPES.has(primitiveType as PrimitiveType) |
| 70 | + ) { |
| 71 | + result.primitiveType = primitiveType as PrimitiveType; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return result; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Validate a UMS URI. |
| 80 | + * @param uri - The URI to validate |
| 81 | + * @returns Validation result |
| 82 | + */ |
| 83 | +export function validateURI(uri: string): URIValidationResult { |
| 84 | + if (!uri.startsWith(UMS_PROTOCOL)) { |
| 85 | + return { valid: false, error: `URI must start with "${UMS_PROTOCOL}"` }; |
| 86 | + } |
| 87 | + |
| 88 | + const parsed = parseURI(uri); |
| 89 | + if (!parsed) { |
| 90 | + return { valid: false, error: 'Invalid URI format' }; |
| 91 | + } |
| 92 | + |
| 93 | + return { valid: true }; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Build a UMS URI from components. |
| 98 | + * @param moduleId - The module ID |
| 99 | + * @param componentId - Optional component ID |
| 100 | + * @param primitiveType - Optional primitive type |
| 101 | + * @returns The constructed URI |
| 102 | + */ |
| 103 | +export function buildURI( |
| 104 | + moduleId: string, |
| 105 | + componentId?: string, |
| 106 | + primitiveType?: PrimitiveType |
| 107 | +): string { |
| 108 | + let uri = `${UMS_PROTOCOL}${moduleId}`; |
| 109 | + |
| 110 | + if (componentId || primitiveType) { |
| 111 | + uri += '#'; |
| 112 | + if (componentId) { |
| 113 | + uri += componentId; |
| 114 | + } |
| 115 | + if (primitiveType) { |
| 116 | + uri += `/${primitiveType}`; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return uri; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Check if a string is a valid UMS URI. |
| 125 | + * @param uri - The string to check |
| 126 | + * @returns True if valid UMS URI |
| 127 | + */ |
| 128 | +export function isValidURI(uri: string): boolean { |
| 129 | + return validateURI(uri).valid; |
| 130 | +} |
0 commit comments