|
1 | 1 | /** |
2 | 2 | * Shared utility functions |
3 | 3 | */ |
| 4 | +import { createRequire } from "node:module"; |
| 5 | +import { existsSync } from "node:fs"; |
| 6 | +import { dirname, join } from "node:path"; |
| 7 | +import { fileURLToPath } from "node:url"; |
4 | 8 |
|
5 | | -import { VERSION } from "../generated/version.js"; |
| 9 | +// Load VERSION with fallback for resilience |
| 10 | +// In production/bundled environments, the generated file is always present |
| 11 | +// In development edge cases (direct tsx without prebuild), falls back to "unknown" |
| 12 | +let VERSION = "unknown"; |
| 13 | +try { |
| 14 | + // Use createRequire for synchronous import that can be caught |
| 15 | + const require = createRequire(import.meta.url); |
| 16 | + const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 17 | + |
| 18 | + // Try .js first (compiled), then .ts (source/dev) |
| 19 | + const jsPath = join(__dirname, "../generated/version.js"); |
| 20 | + const tsPath = join(__dirname, "../generated/version.ts"); |
| 21 | + |
| 22 | + if (existsSync(jsPath)) { |
| 23 | + const versionModule = require("../generated/version.js"); |
| 24 | + VERSION = versionModule.VERSION ?? "unknown"; |
| 25 | + } else if (existsSync(tsPath)) { |
| 26 | + // In development/test with tsx, load the .ts file |
| 27 | + const versionModule = require("../generated/version.ts"); |
| 28 | + VERSION = versionModule.VERSION ?? "unknown"; |
| 29 | + } |
| 30 | +} catch { |
| 31 | + // Generated file doesn't exist or failed to load - use fallback |
| 32 | + // User-Agent will still identify the product: augment.ctxc.cli/unknown |
| 33 | +} |
6 | 34 |
|
7 | 35 | /** |
8 | 36 | * Sanitize a key for use in filenames/paths. |
@@ -73,10 +101,10 @@ export interface MCPClientInfo { |
73 | 101 |
|
74 | 102 | /** |
75 | 103 | * Sanitize a string for use in User-Agent per RFC 9110. |
76 | | - * Only allows: a-z A-Z 0-9 \! # $ % & ' * + . ^ _ ` | ~ - |
| 104 | + * Only allows: a-z A-Z 0-9 \! # $ % & ' * + . ^ _ \` | ~ - |
77 | 105 | */ |
78 | 106 | function sanitizeUserAgentToken(s: string, maxLen: number): string { |
79 | | - return s.replace(/[^a-zA-Z0-9\!#$%&'*+.^_`|~-]/g, "-").slice(0, maxLen); |
| 107 | + return s.replace(/[^a-zA-Z0-9\!#$%&'*+.^_\`|~-]/g, "-").slice(0, maxLen); |
80 | 108 | } |
81 | 109 |
|
82 | 110 | /** |
|
0 commit comments