63 lines
2.8 KiB
JavaScript
63 lines
2.8 KiB
JavaScript
"use strict";
|
|
|
|
const { ValidationError } = require("./jobValidation");
|
|
|
|
const STATES = Object.freeze(["queued", "processing", "done", "failed"]);
|
|
const PAGE_SIZES = Object.freeze([10, 25, 50]);
|
|
const MAX_PAGE = 100000;
|
|
const MAX_SEARCH_LENGTH = 100;
|
|
const QUERY_TIMEOUT_MS = 5000;
|
|
|
|
function parseStatusQuery(query = {}) {
|
|
if (!query || typeof query !== "object" || Array.isArray(query)) {
|
|
throw new ValidationError("Invalid queue filters.");
|
|
}
|
|
for (const key of ["state", "q", "page", "pageSize"]) {
|
|
if (query[key] !== undefined && typeof query[key] !== "string") {
|
|
throw new ValidationError("Each queue filter must have a single text value.");
|
|
}
|
|
}
|
|
const state = query.state === undefined ? "queued" : query.state;
|
|
if (!STATES.includes(state)) throw new ValidationError("Choose a supported queue state.");
|
|
const rawSearch = query.q ?? "";
|
|
if (rawSearch.length > MAX_SEARCH_LENGTH || /[\u0000-\u001f\u007f-\u009f\uD800-\uDFFF]/u.test(rawSearch)) {
|
|
throw new ValidationError(`Search must contain at most ${MAX_SEARCH_LENGTH} characters without control characters.`);
|
|
}
|
|
const q = rawSearch.trim();
|
|
const pageText = query.page ?? "1";
|
|
if (!/^[1-9]\d{0,5}$/.test(pageText) || Number(pageText) > MAX_PAGE) {
|
|
throw new ValidationError(`Page must be a whole number between 1 and ${MAX_PAGE}.`);
|
|
}
|
|
const sizeText = query.pageSize ?? "25";
|
|
if (!/^(10|25|50)$/.test(sizeText)) throw new ValidationError("Choose 10, 25, or 50 items per page.");
|
|
return { state, q, page: Number(pageText), pageSize: Number(sizeText) };
|
|
}
|
|
|
|
function stateQuery(state) {
|
|
if (state === "queued") return { collection: "queue", filter: { processing: { $ne: true } }, sort: { created_at: 1, _id: 1 } };
|
|
if (state === "processing") return { collection: "queue", filter: { processing: true }, sort: { created_at: 1, _id: 1 } };
|
|
if (state === "done") return { collection: "done", filter: {}, sort: { finished_at: -1, _id: -1 } };
|
|
if (state === "failed") return { collection: "failed", filter: {}, sort: { failed_at: -1, _id: -1 } };
|
|
throw new ValidationError("Choose a supported queue state.");
|
|
}
|
|
|
|
function statusFilter(state, q) {
|
|
const { filter } = stateQuery(state);
|
|
// Treat user input as literal text. No query keys or regular expressions are
|
|
// accepted from the caller; search is confined to public filenames.
|
|
if (q) filter.filename = { $regex: q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), $options: "i" };
|
|
return filter;
|
|
}
|
|
|
|
function publicGlobal(record) {
|
|
return {
|
|
captcha_blocked: record?.captcha_blocked === true,
|
|
pause_reason: ["authentication", "resolution"].includes(record?.pause_reason) ? record.pause_reason : null,
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
STATES, PAGE_SIZES, MAX_PAGE, MAX_SEARCH_LENGTH, QUERY_TIMEOUT_MS,
|
|
parseStatusQuery, stateQuery, statusFilter, publicGlobal,
|
|
};
|