@ucptools/validator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/CLAUDE.md +109 -0
  2. package/CONTRIBUTING.md +113 -0
  3. package/LICENSE +21 -0
  4. package/README.md +203 -0
  5. package/api/analyze-feed.js +140 -0
  6. package/api/badge.js +185 -0
  7. package/api/benchmark.js +177 -0
  8. package/api/directory-stats.ts +29 -0
  9. package/api/directory.ts +73 -0
  10. package/api/generate-compliance.js +143 -0
  11. package/api/generate-schema.js +457 -0
  12. package/api/generate.js +132 -0
  13. package/api/security-scan.js +133 -0
  14. package/api/simulate.js +187 -0
  15. package/api/tsconfig.json +10 -0
  16. package/api/validate.js +1351 -0
  17. package/apify-actor/.actor/actor.json +68 -0
  18. package/apify-actor/.actor/input_schema.json +32 -0
  19. package/apify-actor/APIFY-STORE-LISTING.md +412 -0
  20. package/apify-actor/Dockerfile +8 -0
  21. package/apify-actor/README.md +166 -0
  22. package/apify-actor/main.ts +111 -0
  23. package/apify-actor/package.json +17 -0
  24. package/apify-actor/src/main.js +199 -0
  25. package/docs/BRAND-IDENTITY.md +238 -0
  26. package/docs/BRAND-STYLE-GUIDE.md +356 -0
  27. package/drizzle/0000_black_king_cobra.sql +39 -0
  28. package/drizzle/meta/0000_snapshot.json +309 -0
  29. package/drizzle/meta/_journal.json +13 -0
  30. package/drizzle.config.ts +10 -0
  31. package/examples/full-profile.json +70 -0
  32. package/examples/minimal-profile.json +23 -0
  33. package/package.json +69 -0
  34. package/public/.well-known/ucp +25 -0
  35. package/public/android-chrome-192x192.png +0 -0
  36. package/public/android-chrome-512x512.png +0 -0
  37. package/public/apple-touch-icon.png +0 -0
  38. package/public/brand.css +321 -0
  39. package/public/directory.html +701 -0
  40. package/public/favicon-16x16.png +0 -0
  41. package/public/favicon-32x32.png +0 -0
  42. package/public/favicon.ico +0 -0
  43. package/public/guides/bigcommerce.html +743 -0
  44. package/public/guides/fastucp.html +838 -0
  45. package/public/guides/magento.html +779 -0
  46. package/public/guides/shopify.html +726 -0
  47. package/public/guides/squarespace.html +749 -0
  48. package/public/guides/wix.html +747 -0
  49. package/public/guides/woocommerce.html +733 -0
  50. package/public/index.html +3835 -0
  51. package/public/learn.html +396 -0
  52. package/public/logo.jpeg +0 -0
  53. package/public/og-image-icon.png +0 -0
  54. package/public/og-image.png +0 -0
  55. package/public/robots.txt +6 -0
  56. package/public/site.webmanifest +31 -0
  57. package/public/sitemap.xml +69 -0
  58. package/public/social/linkedin-banner-1128x191.png +0 -0
  59. package/public/social/temp.PNG +0 -0
  60. package/public/social/x-header-1500x500.png +0 -0
  61. package/public/verify.html +410 -0
  62. package/scripts/generate-favicons.js +44 -0
  63. package/scripts/generate-ico.js +23 -0
  64. package/scripts/generate-og-image.js +45 -0
  65. package/scripts/reset-db.ts +77 -0
  66. package/scripts/seed-db.ts +71 -0
  67. package/scripts/setup-benchmark-db.js +70 -0
  68. package/src/api/server.ts +266 -0
  69. package/src/cli/index.ts +302 -0
  70. package/src/compliance/compliance-generator.ts +452 -0
  71. package/src/compliance/index.ts +28 -0
  72. package/src/compliance/templates.ts +338 -0
  73. package/src/compliance/types.ts +170 -0
  74. package/src/db/index.ts +28 -0
  75. package/src/db/schema.ts +84 -0
  76. package/src/feed-analyzer/feed-analyzer.ts +726 -0
  77. package/src/feed-analyzer/index.ts +34 -0
  78. package/src/feed-analyzer/types.ts +354 -0
  79. package/src/generator/index.ts +7 -0
  80. package/src/generator/key-generator.ts +124 -0
  81. package/src/generator/profile-builder.ts +402 -0
  82. package/src/hosting/artifacts-generator.ts +679 -0
  83. package/src/hosting/index.ts +6 -0
  84. package/src/index.ts +105 -0
  85. package/src/security/index.ts +15 -0
  86. package/src/security/security-scanner.ts +604 -0
  87. package/src/security/types.ts +55 -0
  88. package/src/services/directory.ts +434 -0
  89. package/src/simulator/agent-simulator.ts +941 -0
  90. package/src/simulator/index.ts +7 -0
  91. package/src/simulator/types.ts +170 -0
  92. package/src/types/generator.ts +140 -0
  93. package/src/types/index.ts +7 -0
  94. package/src/types/ucp-profile.ts +140 -0
  95. package/src/types/validation.ts +89 -0
  96. package/src/validator/index.ts +194 -0
  97. package/src/validator/network-validator.ts +417 -0
  98. package/src/validator/rules-validator.ts +297 -0
  99. package/src/validator/sdk-validator.ts +330 -0
  100. package/src/validator/structural-validator.ts +476 -0
  101. package/tests/fixtures/non-compliant-profile.json +25 -0
  102. package/tests/fixtures/official-sample-profile.json +75 -0
  103. package/tests/integration/benchmark.test.ts +207 -0
  104. package/tests/integration/database.test.ts +163 -0
  105. package/tests/integration/directory-api.test.ts +268 -0
  106. package/tests/integration/simulate-api.test.ts +230 -0
  107. package/tests/integration/validate-api.test.ts +269 -0
  108. package/tests/setup.ts +15 -0
  109. package/tests/unit/agent-simulator.test.ts +575 -0
  110. package/tests/unit/compliance-generator.test.ts +374 -0
  111. package/tests/unit/directory-service.test.ts +272 -0
  112. package/tests/unit/feed-analyzer.test.ts +517 -0
  113. package/tests/unit/lint-suggestions.test.ts +423 -0
  114. package/tests/unit/official-samples.test.ts +211 -0
  115. package/tests/unit/pdf-report.test.ts +390 -0
  116. package/tests/unit/sdk-validator.test.ts +531 -0
  117. package/tests/unit/security-scanner.test.ts +410 -0
  118. package/tests/unit/validation.test.ts +390 -0
  119. package/tsconfig.json +20 -0
  120. package/vercel.json +34 -0
  121. package/vitest.config.ts +22 -0
@@ -0,0 +1,194 @@
1
+ /**
2
+ * UCP Profile Validator
3
+ * Main entry point combining structural, rules, and network validation
4
+ */
5
+
6
+ import type { UcpProfile } from '../types/ucp-profile.js';
7
+ import type {
8
+ ValidationReport,
9
+ ValidationIssue,
10
+ ValidationMode,
11
+ ValidationOptions,
12
+ } from '../types/validation.js';
13
+ import { validateStructure } from './structural-validator.js';
14
+ import { validateRules } from './rules-validator.js';
15
+ import { validateNetwork, validateRemoteProfile, clearSchemaCache } from './network-validator.js';
16
+ import { safeValidateWithSdk, getSdkVersion, isSdkCompliant } from './sdk-validator.js';
17
+ import type { NetworkValidationOptions } from './network-validator.js';
18
+
19
+ export { validateStructure } from './structural-validator.js';
20
+ export { validateRules } from './rules-validator.js';
21
+ export { validateNetwork, validateRemoteProfile, clearSchemaCache } from './network-validator.js';
22
+ export {
23
+ safeValidateWithSdk,
24
+ validateWithSdk,
25
+ getSdkVersion,
26
+ isSdkCompliant,
27
+ validateServiceWithSdk,
28
+ validateCapabilityWithSdk,
29
+ validateSigningKeysWithSdk,
30
+ } from './sdk-validator.js';
31
+
32
+ /**
33
+ * Validate a UCP profile (local JSON)
34
+ */
35
+ export async function validateProfile(
36
+ profile: unknown,
37
+ options: ValidationOptions = {}
38
+ ): Promise<ValidationReport> {
39
+ const mode = options.mode || 'full';
40
+ const issues: ValidationIssue[] = [];
41
+
42
+ // Phase 1: Structural validation (always run)
43
+ if (mode === 'structural' || mode === 'rules' || mode === 'full') {
44
+ const structuralIssues = validateStructure(profile);
45
+ issues.push(...structuralIssues);
46
+
47
+ // If structural validation has errors, don't proceed with rules/network
48
+ const hasStructuralErrors = structuralIssues.some(i => i.severity === 'error');
49
+ if (hasStructuralErrors && mode !== 'structural') {
50
+ return buildReport(issues, mode, undefined, profile);
51
+ }
52
+ }
53
+
54
+ // At this point, profile structure is valid
55
+ const ucpProfile = profile as UcpProfile;
56
+
57
+ // Phase 2: UCP rules validation
58
+ if (mode === 'rules' || mode === 'full') {
59
+ const rulesIssues = validateRules(ucpProfile);
60
+ issues.push(...rulesIssues);
61
+ }
62
+
63
+ // Phase 3: Network validation (optional)
64
+ if (mode === 'network' || mode === 'full') {
65
+ if (!options.skipNetworkChecks) {
66
+ const networkOptions: NetworkValidationOptions = {
67
+ timeoutMs: options.timeoutMs,
68
+ cacheTtlMs: options.cacheTtlMs,
69
+ };
70
+ const networkIssues = await validateNetwork(ucpProfile, networkOptions);
71
+ issues.push(...networkIssues);
72
+ }
73
+ }
74
+
75
+ return buildReport(issues, mode, undefined, ucpProfile);
76
+ }
77
+
78
+ /**
79
+ * Validate a remote UCP profile (fetches from domain)
80
+ */
81
+ export async function validateRemote(
82
+ domain: string,
83
+ options: ValidationOptions = {}
84
+ ): Promise<ValidationReport> {
85
+ const issues: ValidationIssue[] = [];
86
+
87
+ // Fetch remote profile
88
+ const { profile, profileUrl: foundProfileUrl, issues: fetchIssues } = await validateRemoteProfile(domain, {
89
+ timeoutMs: options.timeoutMs,
90
+ cacheTtlMs: options.cacheTtlMs,
91
+ });
92
+ issues.push(...fetchIssues);
93
+
94
+ const profileUrl = foundProfileUrl || `https://${domain}/.well-known/ucp`;
95
+
96
+ if (!profile) {
97
+ return buildReport(issues, 'network', profileUrl, undefined);
98
+ }
99
+
100
+ // Run full validation on fetched profile
101
+ const validationResult = await validateProfile(profile, options);
102
+ issues.push(...validationResult.issues);
103
+
104
+ return buildReport(issues, options.mode || 'full', profileUrl, profile);
105
+ }
106
+
107
+ /**
108
+ * Build validation report
109
+ */
110
+ function buildReport(
111
+ issues: ValidationIssue[],
112
+ mode: ValidationMode,
113
+ profileUrl?: string,
114
+ profile?: unknown
115
+ ): ValidationReport {
116
+ // Determine if validation passed (no errors)
117
+ const hasErrors = issues.some(i => i.severity === 'error');
118
+
119
+ // Extract UCP version if available
120
+ let ucpVersion: string | undefined;
121
+ if (profile && typeof profile === 'object') {
122
+ const p = profile as Record<string, unknown>;
123
+ if (p.ucp && typeof p.ucp === 'object') {
124
+ const ucp = p.ucp as Record<string, unknown>;
125
+ if (typeof ucp.version === 'string') {
126
+ ucpVersion = ucp.version;
127
+ }
128
+ }
129
+ }
130
+
131
+ // Run SDK validation for compliance check
132
+ const sdkCompliant = profile ? isSdkCompliant(profile) : false;
133
+
134
+ return {
135
+ ok: !hasErrors,
136
+ profile_url: profileUrl,
137
+ ucp_version: ucpVersion,
138
+ issues,
139
+ validated_at: new Date().toISOString(),
140
+ validation_mode: mode,
141
+ sdk_validation: {
142
+ validated: true,
143
+ sdk_version: getSdkVersion(),
144
+ compliant: sdkCompliant,
145
+ },
146
+ };
147
+ }
148
+
149
+ /**
150
+ * Quick validation (structural + rules only, no network)
151
+ */
152
+ export function validateQuick(profile: unknown): ValidationReport {
153
+ const issues: ValidationIssue[] = [];
154
+
155
+ // Structural validation
156
+ const structuralIssues = validateStructure(profile);
157
+ issues.push(...structuralIssues);
158
+
159
+ // If structural is OK, run rules validation
160
+ const hasStructuralErrors = structuralIssues.some(i => i.severity === 'error');
161
+ if (!hasStructuralErrors) {
162
+ const rulesIssues = validateRules(profile as UcpProfile);
163
+ issues.push(...rulesIssues);
164
+ }
165
+
166
+ return buildReport(issues, 'rules', undefined, profile);
167
+ }
168
+
169
+ /**
170
+ * Parse and validate JSON string
171
+ */
172
+ export async function validateJsonString(
173
+ json: string,
174
+ options: ValidationOptions = {}
175
+ ): Promise<ValidationReport> {
176
+ try {
177
+ const profile = JSON.parse(json);
178
+ return validateProfile(profile, options);
179
+ } catch (error) {
180
+ const message = error instanceof Error ? error.message : 'Invalid JSON';
181
+ return {
182
+ ok: false,
183
+ issues: [{
184
+ severity: 'error',
185
+ code: 'UCP_MISSING_ROOT' as const,
186
+ path: '$',
187
+ message: `Failed to parse JSON: ${message}`,
188
+ hint: 'Ensure the input is valid JSON',
189
+ }],
190
+ validated_at: new Date().toISOString(),
191
+ validation_mode: options.mode || 'full',
192
+ };
193
+ }
194
+ }
@@ -0,0 +1,417 @@
1
+ /**
2
+ * Network Validator
3
+ * Validates UCP profile with network checks (fetches remote schemas)
4
+ */
5
+
6
+ import type { UcpProfile } from '../types/ucp-profile.js';
7
+ import type { ValidationIssue, FetchResult, SchemaCacheEntry } from '../types/validation.js';
8
+ import { ValidationErrorCodes } from '../types/validation.js';
9
+
10
+ // Simple in-memory cache for schema fetches
11
+ const schemaCache = new Map<string, SchemaCacheEntry>();
12
+ const DEFAULT_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
13
+ const DEFAULT_TIMEOUT_MS = 10000; // 10 seconds
14
+
15
+ export interface NetworkValidationOptions {
16
+ timeoutMs?: number;
17
+ cacheTtlMs?: number;
18
+ skipSchemaFetch?: boolean;
19
+ }
20
+
21
+ /**
22
+ * Validate UCP profile with network checks
23
+ */
24
+ export async function validateNetwork(
25
+ profile: UcpProfile,
26
+ options: NetworkValidationOptions = {}
27
+ ): Promise<ValidationIssue[]> {
28
+ const issues: ValidationIssue[] = [];
29
+ const timeoutMs = options.timeoutMs || DEFAULT_TIMEOUT_MS;
30
+ const cacheTtlMs = options.cacheTtlMs || DEFAULT_CACHE_TTL_MS;
31
+
32
+ if (options.skipSchemaFetch) {
33
+ return issues;
34
+ }
35
+
36
+ const capabilities = profile.ucp.capabilities || [];
37
+
38
+ // Validate each capability's schema
39
+ for (let i = 0; i < capabilities.length; i++) {
40
+ const cap = capabilities[i];
41
+ const path = `$.ucp.capabilities[${i}]`;
42
+
43
+ if (cap.schema) {
44
+ const schemaIssues = await validateCapabilitySchema(
45
+ cap.schema,
46
+ cap.name,
47
+ cap.version,
48
+ path,
49
+ timeoutMs,
50
+ cacheTtlMs
51
+ );
52
+ issues.push(...schemaIssues);
53
+ }
54
+ }
55
+
56
+ return issues;
57
+ }
58
+
59
+ /**
60
+ * Validate remote profile fetch
61
+ */
62
+ export async function validateRemoteProfile(
63
+ domain: string,
64
+ options: NetworkValidationOptions = {}
65
+ ): Promise<{ profile: UcpProfile | null; profileUrl?: string; issues: ValidationIssue[] }> {
66
+ const issues: ValidationIssue[] = [];
67
+ const timeoutMs = options.timeoutMs || DEFAULT_TIMEOUT_MS;
68
+
69
+ // Try both /.well-known/ucp and /.well-known/ucp.json
70
+ const urls = [
71
+ `https://${domain}/.well-known/ucp`,
72
+ `https://${domain}/.well-known/ucp.json`,
73
+ ];
74
+
75
+ for (const profileUrl of urls) {
76
+ const result = await fetchProfileWithTimeout(profileUrl, timeoutMs);
77
+
78
+ if (!result.success) {
79
+ // Try next URL
80
+ continue;
81
+ }
82
+
83
+ // Verify it's an object with ucp field
84
+ if (!result.data || typeof result.data !== 'object') {
85
+ continue;
86
+ }
87
+
88
+ const profileData = result.data as Record<string, unknown>;
89
+ if (!profileData.ucp) {
90
+ continue;
91
+ }
92
+
93
+ return { profile: result.data as UcpProfile, profileUrl, issues };
94
+ }
95
+
96
+ // All URLs failed
97
+ issues.push({
98
+ severity: 'error',
99
+ code: ValidationErrorCodes.PROFILE_FETCH_FAILED,
100
+ path: '$.well-known/ucp',
101
+ message: 'No UCP profile found at /.well-known/ucp or /.well-known/ucp.json',
102
+ hint: 'Check that the profile is accessible and returns valid JSON',
103
+ });
104
+ return { profile: null, issues };
105
+ }
106
+
107
+ /**
108
+ * Validate a capability's schema (fetch and check self-describing)
109
+ */
110
+ async function validateCapabilitySchema(
111
+ schemaUrl: string,
112
+ expectedName: string,
113
+ expectedVersion: string,
114
+ basePath: string,
115
+ timeoutMs: number,
116
+ cacheTtlMs: number
117
+ ): Promise<ValidationIssue[]> {
118
+ const issues: ValidationIssue[] = [];
119
+
120
+ // Check cache first
121
+ const cached = getCachedSchema(schemaUrl, cacheTtlMs);
122
+ let schemaData: Record<string, unknown>;
123
+
124
+ if (cached) {
125
+ schemaData = cached;
126
+ } else {
127
+ // Fetch schema
128
+ const result = await fetchWithTimeout<Record<string, unknown>>(schemaUrl, timeoutMs);
129
+
130
+ if (!result.success) {
131
+ issues.push({
132
+ severity: 'warn',
133
+ code: ValidationErrorCodes.SCHEMA_FETCH_FAILED,
134
+ path: `${basePath}.schema`,
135
+ message: `Failed to fetch schema from ${schemaUrl}`,
136
+ hint: result.error || 'Schema URL may be incorrect or temporarily unavailable',
137
+ });
138
+ return issues;
139
+ }
140
+
141
+ if (!result.data) {
142
+ issues.push({
143
+ severity: 'warn',
144
+ code: ValidationErrorCodes.SCHEMA_FETCH_FAILED,
145
+ path: `${basePath}.schema`,
146
+ message: `Schema response is empty`,
147
+ });
148
+ return issues;
149
+ }
150
+
151
+ schemaData = result.data;
152
+
153
+ // Cache the schema
154
+ cacheSchema(schemaUrl, schemaData, result.etag, cacheTtlMs);
155
+ }
156
+
157
+ // Check if schema is self-describing
158
+ const selfDescribingIssues = validateSelfDescribingSchema(
159
+ schemaData,
160
+ expectedName,
161
+ expectedVersion,
162
+ basePath
163
+ );
164
+ issues.push(...selfDescribingIssues);
165
+
166
+ return issues;
167
+ }
168
+
169
+ /**
170
+ * Validate schema is self-describing (contains name and version matching capability)
171
+ */
172
+ function validateSelfDescribingSchema(
173
+ schema: Record<string, unknown>,
174
+ expectedName: string,
175
+ expectedVersion: string,
176
+ basePath: string
177
+ ): ValidationIssue[] {
178
+ const issues: ValidationIssue[] = [];
179
+
180
+ // Check for $id or name field
181
+ const schemaName = (schema.$id as string) || (schema.name as string);
182
+ const schemaVersion = schema.version as string;
183
+
184
+ if (!schemaName && !schema.$id) {
185
+ issues.push({
186
+ severity: 'info',
187
+ code: ValidationErrorCodes.SCHEMA_NOT_SELF_DESCRIBING,
188
+ path: `${basePath}.schema`,
189
+ message: 'Schema does not contain self-describing $id or name field',
190
+ hint: 'Consider adding $id field to schema for better discoverability',
191
+ });
192
+ }
193
+
194
+ // If schema has a name, check it contains the capability name
195
+ if (schemaName) {
196
+ // Extract capability name from schema $id if it's a URL
197
+ const nameFromId = extractNameFromSchemaId(schemaName);
198
+ if (nameFromId && !expectedName.includes(nameFromId) && !nameFromId.includes(expectedName.split('.').pop() || '')) {
199
+ issues.push({
200
+ severity: 'warn',
201
+ code: ValidationErrorCodes.SCHEMA_NAME_MISMATCH,
202
+ path: `${basePath}.schema`,
203
+ message: `Schema name "${nameFromId}" may not match capability "${expectedName}"`,
204
+ });
205
+ }
206
+ }
207
+
208
+ // Check version if present
209
+ if (schemaVersion && schemaVersion !== expectedVersion) {
210
+ issues.push({
211
+ severity: 'info',
212
+ code: ValidationErrorCodes.SCHEMA_VERSION_MISMATCH,
213
+ path: `${basePath}.schema`,
214
+ message: `Schema version "${schemaVersion}" differs from capability version "${expectedVersion}"`,
215
+ hint: 'Ensure schema and capability versions are aligned',
216
+ });
217
+ }
218
+
219
+ return issues;
220
+ }
221
+
222
+ /**
223
+ * Extract capability name from schema $id URL
224
+ */
225
+ function extractNameFromSchemaId(schemaId: string): string | null {
226
+ try {
227
+ const url = new URL(schemaId);
228
+ // Extract last path segment without .json extension
229
+ const pathParts = url.pathname.split('/').filter(Boolean);
230
+ const lastPart = pathParts[pathParts.length - 1] || '';
231
+ return lastPart.replace('.json', '');
232
+ } catch {
233
+ // Not a URL, return as-is
234
+ return schemaId;
235
+ }
236
+ }
237
+
238
+ /**
239
+ * Fetch profile URL with timeout, checking for HTML responses
240
+ */
241
+ async function fetchProfileWithTimeout(
242
+ url: string,
243
+ timeoutMs: number
244
+ ): Promise<FetchResult<unknown>> {
245
+ const controller = new AbortController();
246
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
247
+
248
+ try {
249
+ const response = await fetch(url, {
250
+ signal: controller.signal,
251
+ headers: {
252
+ 'Accept': 'application/json',
253
+ 'User-Agent': 'UCP-Profile-Validator/1.0',
254
+ },
255
+ });
256
+
257
+ clearTimeout(timeoutId);
258
+
259
+ if (!response.ok) {
260
+ return {
261
+ success: false,
262
+ error: `HTTP ${response.status}: ${response.statusText}`,
263
+ statusCode: response.status,
264
+ };
265
+ }
266
+
267
+ const text = await response.text();
268
+
269
+ // Check if response looks like JSON (not HTML)
270
+ if (text.trim().startsWith('<')) {
271
+ return {
272
+ success: false,
273
+ error: 'Response is HTML, not JSON',
274
+ };
275
+ }
276
+
277
+ const data = JSON.parse(text);
278
+ const etag = response.headers.get('etag') || undefined;
279
+
280
+ return {
281
+ success: true,
282
+ data,
283
+ statusCode: response.status,
284
+ etag,
285
+ };
286
+ } catch (error) {
287
+ clearTimeout(timeoutId);
288
+
289
+ if (error instanceof Error) {
290
+ if (error.name === 'AbortError') {
291
+ return {
292
+ success: false,
293
+ error: `Request timed out after ${timeoutMs}ms`,
294
+ };
295
+ }
296
+ return {
297
+ success: false,
298
+ error: error.message,
299
+ };
300
+ }
301
+
302
+ return {
303
+ success: false,
304
+ error: 'Unknown error occurred',
305
+ };
306
+ }
307
+ }
308
+
309
+ /**
310
+ * Fetch URL with timeout
311
+ */
312
+ async function fetchWithTimeout<T>(
313
+ url: string,
314
+ timeoutMs: number
315
+ ): Promise<FetchResult<T>> {
316
+ const controller = new AbortController();
317
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
318
+
319
+ try {
320
+ const response = await fetch(url, {
321
+ signal: controller.signal,
322
+ headers: {
323
+ 'Accept': 'application/json',
324
+ 'User-Agent': 'UCP-Profile-Validator/1.0',
325
+ },
326
+ });
327
+
328
+ clearTimeout(timeoutId);
329
+
330
+ if (!response.ok) {
331
+ return {
332
+ success: false,
333
+ error: `HTTP ${response.status}: ${response.statusText}`,
334
+ statusCode: response.status,
335
+ };
336
+ }
337
+
338
+ const data = await response.json() as T;
339
+ const etag = response.headers.get('etag') || undefined;
340
+
341
+ return {
342
+ success: true,
343
+ data,
344
+ statusCode: response.status,
345
+ etag,
346
+ };
347
+ } catch (error) {
348
+ clearTimeout(timeoutId);
349
+
350
+ if (error instanceof Error) {
351
+ if (error.name === 'AbortError') {
352
+ return {
353
+ success: false,
354
+ error: `Request timed out after ${timeoutMs}ms`,
355
+ };
356
+ }
357
+ return {
358
+ success: false,
359
+ error: error.message,
360
+ };
361
+ }
362
+
363
+ return {
364
+ success: false,
365
+ error: 'Unknown error occurred',
366
+ };
367
+ }
368
+ }
369
+
370
+ /**
371
+ * Get cached schema if valid
372
+ */
373
+ function getCachedSchema(
374
+ url: string,
375
+ cacheTtlMs: number
376
+ ): Record<string, unknown> | null {
377
+ const cached = schemaCache.get(url);
378
+ if (!cached) {
379
+ return null;
380
+ }
381
+
382
+ const now = new Date().toISOString();
383
+ if (cached.expiresAt < now) {
384
+ schemaCache.delete(url);
385
+ return null;
386
+ }
387
+
388
+ return cached.body;
389
+ }
390
+
391
+ /**
392
+ * Cache a schema
393
+ */
394
+ function cacheSchema(
395
+ url: string,
396
+ body: Record<string, unknown>,
397
+ etag: string | undefined,
398
+ cacheTtlMs: number
399
+ ): void {
400
+ const now = new Date();
401
+ const expiresAt = new Date(now.getTime() + cacheTtlMs);
402
+
403
+ schemaCache.set(url, {
404
+ url,
405
+ etag,
406
+ fetchedAt: now.toISOString(),
407
+ body,
408
+ expiresAt: expiresAt.toISOString(),
409
+ });
410
+ }
411
+
412
+ /**
413
+ * Clear the schema cache
414
+ */
415
+ export function clearSchemaCache(): void {
416
+ schemaCache.clear();
417
+ }