-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
29 lines (20 loc) · 858 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { randomBytes } from "crypto";
const BASE_SAFE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!-_.'()";
const STRICT_ADDITIONS = "*/";
export type Options = Partial<{
replacement: string;
strict: boolean;
}>;
export function sanitize(key: string, options?: Options): string {
if (typeof key !== "string") throw new Error("key must be a string");
const replacement = options?.replacement ?? "";
const safeChars = new Set(BASE_SAFE_CHARS + (options?.strict ? STRICT_ADDITIONS : ""));
let sanitizedKey = "";
// Start from index 1 if key starts with '/'
let startIndex = key.startsWith("/") ? 1 : 0;
for (let i = startIndex; i < key.length; i++) {
const char = key[i] || "";
sanitizedKey += safeChars.has(char) ? char : replacement;
}
return sanitizedKey || randomBytes(10).toString("hex");
}