(key: string)
| 24 | const MAX_KEY_LENGTH = 128 |
| 25 | |
| 26 | export function validateKey(key: string): void { |
| 27 | if (!key) { |
| 28 | throw new Error('Empty key') |
| 29 | } |
| 30 | if (key.length > MAX_KEY_LENGTH) { |
| 31 | throw new Error(`Key too long (max ${MAX_KEY_LENGTH})`) |
| 32 | } |
| 33 | if (!KEY_REGEX.test(key)) { |
| 34 | throw new Error(`Invalid key chars: ${JSON.stringify(key)}`) |
| 35 | } |
| 36 | if (key.startsWith('.')) { |
| 37 | throw new Error('Leading dot forbidden') |
| 38 | } |
| 39 | // M6 fix: match the basename (pre-dot component) so e.g. NUL.txt and |
| 40 | // CON.foo are also rejected. On Windows these still alias to the device |
| 41 | // file regardless of extension and would silently lose data. |
| 42 | const basenameComponent = key.includes('.') ? key.split('.')[0]! : key |
| 43 | if (WINDOWS_RESERVED_BASENAME.test(basenameComponent)) { |
| 44 | throw new Error(`Windows reserved name: ${key}`) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** Returns true iff key would pass validateKey (no throw). Useful for guards. */ |
| 49 | export function isValidKey(key: string): boolean { |
no outgoing calls
no test coverage detected