fix(housekeeping): address people moderation review
This commit is contained in:
1 parent
29fe22297b
commit
3fa1119f5a
20 files changed
+2000
-777
No files matched your search
@@ -19,23 +19,51 @@ export interface TicketReplyDb {
|
||||
touchTicket(ticketId: bigint, updatedAt: Date): Promise<unknown>;
|
||||
}
|
||||
|
||||
const MAX_UNSIGNED_BIGINT = 18_446_744_073_709_551_615n;
|
||||
export const MAX_UNSIGNED_TICKET_ID = 18_446_744_073_709_551_615n;
|
||||
|
||||
function boundedDecimalPattern(maximum: string): RegExp {
|
||||
const alternatives = [`[1-9]\\d{0,${maximum.length - 2}}`];
|
||||
for (let index = 0; index < maximum.length; index += 1) {
|
||||
const maximumDigit = Number(maximum[index]);
|
||||
const minimumDigit = index === 0 ? 1 : 0;
|
||||
const upperDigit = maximumDigit - 1;
|
||||
if (upperDigit < minimumDigit) continue;
|
||||
const digit =
|
||||
upperDigit === minimumDigit
|
||||
? String(minimumDigit)
|
||||
: `[${minimumDigit}-${upperDigit}]`;
|
||||
alternatives.push(
|
||||
`${maximum.slice(0, index)}${digit}\\d{${maximum.length - index - 1}}`,
|
||||
);
|
||||
}
|
||||
alternatives.push(maximum);
|
||||
return new RegExp(`^(?:${alternatives.join("|")})$`, "u");
|
||||
}
|
||||
|
||||
export const CANONICAL_TICKET_ID_PATTERN = boundedDecimalPattern(
|
||||
MAX_UNSIGNED_TICKET_ID.toString(),
|
||||
);
|
||||
|
||||
// Canonicalize a SQL BIGINT identifier without passing through Number.
|
||||
export function canonicalTicketId(value: string | number | bigint): bigint {
|
||||
let parsed: bigint;
|
||||
try {
|
||||
if (
|
||||
typeof value === "number" &&
|
||||
(!Number.isSafeInteger(value) || value <= 0)
|
||||
) {
|
||||
if (typeof value === "bigint") {
|
||||
parsed = value;
|
||||
} else if (typeof value === "string") {
|
||||
if (!CANONICAL_TICKET_ID_PATTERN.test(value)) {
|
||||
throw new Error("noncanonical identifier");
|
||||
}
|
||||
parsed = BigInt(value);
|
||||
} else if (Number.isSafeInteger(value) && value > 0) {
|
||||
parsed = BigInt(value);
|
||||
} else {
|
||||
throw new Error("unsafe identifier");
|
||||
}
|
||||
parsed = BigInt(String(value));
|
||||
} catch {
|
||||
throw new Error("invalid ticket identifier");
|
||||
}
|
||||
if (parsed <= 0n || parsed > MAX_UNSIGNED_BIGINT) {
|
||||
if (parsed <= 0n || parsed > MAX_UNSIGNED_TICKET_ID) {
|
||||
throw new Error("invalid ticket identifier");
|
||||
}
|
||||
return parsed;
|
||||
|
||||
Reference in new issue
Block a user