// Copyright (c) 2020 Jan Kaßel // Copyright (c) 2022 Gerben // // SPDX-License-Identifier: MIT import { randomBytes } from 'crypto'; import type { Level } from 'level'; export function generateKey(length = 16) { return randomBytes(length).toString('hex'); } export async function getPrefixedEntries(db: Level, prefix: string, shallow?: boolean) { const entries: [string, any][] = []; for await (const entry of db.iterator({ gt: prefix })) { if (!entry[0].startsWith(prefix)) break; if (shallow && entry[0].slice(prefix.length).includes('/')) continue; entries.push(entry); } return entries; } export function escapeHtml(s: string) { return s .replace('<', '<') .replace('>', '>') .replace('&', '&') .replace('"', '"') .replace("'", '''); }