|
| 1 | +#!/usr/bin/env node |
| 2 | +import { readFileSync, writeFileSync } from 'fs'; |
| 3 | +import { join, dirname } from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | + |
| 6 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 7 | + |
| 8 | +// Read the original types.ts file |
| 9 | +const typesPath = join(__dirname, '../src/types.ts'); |
| 10 | +const strictTypesPath = join(__dirname, '../src/strictTypes.ts'); |
| 11 | + |
| 12 | +let content = readFileSync(typesPath, 'utf-8'); |
| 13 | + |
| 14 | +// Add header comment |
| 15 | +const header = `/** |
| 16 | + * Types remove unknown |
| 17 | + * properties to maintaining compatibility with protocol extensions. |
| 18 | + * |
| 19 | + * - Protocol compatoble: Unknown fields from extended implementations are removed, not rejected |
| 20 | + * - Forward compatibility: Works with servers/clients that have additional fields |
| 21 | + * |
| 22 | + * @generated by scripts/generateStrictTypes.ts |
| 23 | + */ |
| 24 | +
|
| 25 | +`; |
| 26 | + |
| 27 | +// Replace all .passthrough() with .strip() |
| 28 | +content = content.replace(/\.passthrough\(\)/g, '.strip()'); |
| 29 | + |
| 30 | +// Special handling for experimental and capabilities that should remain open |
| 31 | +// These are explicitly designed to be extensible |
| 32 | +const patternsToKeepOpen = [ |
| 33 | + // Keep experimental fields open as they're meant for extensions |
| 34 | + /experimental: z\.optional\(z\.object\(\{\}\)\.strip\(\)\)/g, |
| 35 | + // Keep _meta fields open as they're meant for arbitrary metadata |
| 36 | + /_meta: z\.optional\(z\.object\(\{\}\)\.strip\(\)\)/g, |
| 37 | + // Keep JSON Schema properties open as they can have arbitrary fields |
| 38 | + /properties: z\.optional\(z\.object\(\{\}\)\.strip\(\)\)/g, |
| 39 | +]; |
| 40 | + |
| 41 | +// Revert strip back to passthrough for these special cases |
| 42 | +patternsToKeepOpen.forEach(pattern => { |
| 43 | + content = content.replace(pattern, (match) => |
| 44 | + match.replace('.strip()', '.passthrough()') |
| 45 | + ); |
| 46 | +}); |
| 47 | + |
| 48 | +// Add a comment explaining the difference |
| 49 | +const explanation = ` |
| 50 | +/** |
| 51 | + * Note: The following fields remain open (using .passthrough()): |
| 52 | + * - experimental: Designed for protocol extensions |
| 53 | + * - _meta: Designed for arbitrary metadata |
| 54 | + * - properties: JSON Schema properties that can have arbitrary fields |
| 55 | + * |
| 56 | + * All other objects use .strip() to remove unknown properties while |
| 57 | + * maintaining compatibility with extended protocols. |
| 58 | + */ |
| 59 | +`; |
| 60 | + |
| 61 | +// Insert the explanation after the imports |
| 62 | +const importEndIndex = content.lastIndexOf('import'); |
| 63 | +const importEndLineIndex = content.indexOf('\n', importEndIndex); |
| 64 | +content = content.slice(0, importEndLineIndex + 1) + explanation + content.slice(importEndLineIndex + 1); |
| 65 | + |
| 66 | +// Write the strict types file |
| 67 | +writeFileSync(strictTypesPath, header + content); |
| 68 | + |
| 69 | +console.log('Generated strictTypes.ts successfully!'); |
0 commit comments