Files
fristy 650807c1fb
CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
chore: restart the repository with a clean history
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
2026-11-10 11:31:13 +03:00

32 lines
1.3 KiB
JavaScript

const crypto = require("node:crypto");
const ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
const MIXED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const NAME_REGEX = /^[a-z0-9-]{3,32}$/;
const RESERVED = new Set(["frislit"]);
const SECRET_LENGTH = 26;
const randomFrom = (alphabet, length) => {
const bytes = crypto.randomBytes(length);
let out = "";
for (let i = 0; i < length; i++) out += alphabet[bytes[i] % alphabet.length];
return out;
};
const randomId = (length = 6) => randomFrom(ALPHABET, length);
const randomSecret = () => randomFrom(MIXED, SECRET_LENGTH);
const randomToken = (length = 32) => randomFrom(MIXED, length);
const isValidName = name => NAME_REGEX.test(name) && !RESERVED.has(name);
const isReserved = name => RESERVED.has(name);
const hashSecret = secret => crypto.createHash("sha256").update(secret).digest();
const hashToken = token => crypto.createHash("sha256").update(String(token)).digest("hex");
const splitShareCode = code => {
if (typeof code !== "string" || !/^[a-z0-9-]+[A-Za-z0-9]{26}$/.test(code)) return null;
return { id: code.slice(0, -SECRET_LENGTH), secret: code.slice(-SECRET_LENGTH) };
};
module.exports = { randomId, randomSecret, randomToken, isValidName, isReserved, hashSecret, hashToken, splitShareCode, NAME_REGEX };