|
| 1 | +import { publish } from '@/events/lib/hydro' |
| 2 | +import { hydroNames } from '@/events/lib/schema' |
| 3 | + |
| 4 | +/** |
| 5 | + * Handles search analytics and client_name validation for external requests |
| 6 | + * Returns null if the request should continue, or an error response object if validation failed |
| 7 | + */ |
| 8 | +export async function handleExternalSearchAnalytics( |
| 9 | + req: any, |
| 10 | + searchContext: string, |
| 11 | +): Promise<{ error: string; status: number } | null> { |
| 12 | + const host = req.headers['x-host'] || req.headers.host |
| 13 | + const normalizedHost = stripPort(host as string) |
| 14 | + |
| 15 | + // Skip analytics entirely for production and internal staging environments |
| 16 | + if ( |
| 17 | + normalizedHost === 'docs.github.com' || |
| 18 | + normalizedHost.endsWith('.github.net') || |
| 19 | + normalizedHost.endsWith('.githubapp.com') |
| 20 | + ) { |
| 21 | + return null |
| 22 | + } |
| 23 | + |
| 24 | + // For localhost, send analytics but auto-set client_name if not provided |
| 25 | + let client_name = req.query.client_name || req.body?.client_name |
| 26 | + if (normalizedHost === 'localhost' && !client_name) { |
| 27 | + client_name = 'localhost' |
| 28 | + } |
| 29 | + |
| 30 | + // For all other external requests, require explicit client_name |
| 31 | + if (!client_name) { |
| 32 | + return { |
| 33 | + status: 400, |
| 34 | + error: "Missing required parameter 'client_name' for external requests", |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Send search event with client identifier |
| 39 | + try { |
| 40 | + await publish({ |
| 41 | + schema: hydroNames.search, |
| 42 | + value: { |
| 43 | + type: 'search', |
| 44 | + version: '1.0.0', |
| 45 | + context: { |
| 46 | + event_id: crypto.randomUUID(), |
| 47 | + user: 'server-side', |
| 48 | + version: '1.0.0', |
| 49 | + created: new Date().toISOString(), |
| 50 | + hostname: normalizedHost, |
| 51 | + path: '', |
| 52 | + search: '', |
| 53 | + hash: '', |
| 54 | + path_language: 'en', |
| 55 | + path_version: '', |
| 56 | + path_product: '', |
| 57 | + path_article: '', |
| 58 | + }, |
| 59 | + search_query: 'REDACTED', |
| 60 | + search_context: searchContext, |
| 61 | + search_client: client_name as string, |
| 62 | + }, |
| 63 | + }) |
| 64 | + } catch (error) { |
| 65 | + // Don't fail the request if analytics fails |
| 66 | + console.error('Failed to send search analytics:', error) |
| 67 | + } |
| 68 | + |
| 69 | + return null |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Determines if a host should bypass client_name requirement for analytics |
| 74 | + * Returns true if the host is docs.github.com or ends with github.net or githubapp.com |
| 75 | + * (for production and internal staging environments) |
| 76 | + * Note: localhost is NOT included here as it should send analytics with auto-set client_name |
| 77 | + */ |
| 78 | +export function shouldBypassClientNameRequirement(host: string | undefined): boolean { |
| 79 | + if (!host) return false |
| 80 | + |
| 81 | + const normalizedHost = stripPort(host) |
| 82 | + return ( |
| 83 | + normalizedHost === 'docs.github.com' || |
| 84 | + normalizedHost.endsWith('.github.net') || |
| 85 | + normalizedHost.endsWith('.githubapp.com') |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Strips port number from host string |
| 91 | + */ |
| 92 | +function stripPort(host: string): string { |
| 93 | + const [hostname] = host.split(':') |
| 94 | + return hostname |
| 95 | +} |
0 commit comments