Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

util.ts

28 lines1.1 KB

1/**
2 * Reject a resource/script path that escapes its skill root. Pure string check
3 * (edge-safe, no `node:path`): no absolute paths, no `..` segments, no
4 * backslashes. Enforced here so both the resource tool and `skillDirectory`
5 * share one guard and the conformance suite can pin it.
6 */
7export function assertSafeResourcePath(path: string): void {
8 const normalized = path.replace(/\\/g, '/')
9 const bad =
10 normalized.startsWith('/') ||
11 /^[a-zA-Z]:/.test(normalized) ||
12 normalized.split('/').some((seg) => seg === '..' || seg === '~')
13 if (bad) {
14 throw new Error(`unsafe resource path: "${path}"`)
15 }
17​
18/** Small, edge-safe (no `node:crypto`) stable string hash for `revision()`. */
19export function stableHash(input: string): string {
20 // ponytail: FNV-1a; collisions don't matter here — revision only needs to
21 // change when content changes, not be cryptographically unique.
22 let h = 0x811c9dc5
23 for (let i = 0; i < input.length; i++) {
24 h ^= input.charCodeAt(i)
25 h = Math.imul(h, 0x01000193)
26 }
27 return (h >>> 0).toString(16).padStart(8, '0')