mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +08:00
feat(cron): expose run receipts and modularize execution
Return committed trigger IDs and add CLI wait and trigger-specific logs while preserving API, CLI, transaction, deduplication and unknown-result semantics. Move scheduling, lifecycle, settlement, queries and legacy adapters into back/cron; remove obsolete internal import paths and circular dependencies. Apply project formatting across the repository, cover CJS/MJS in formatting hooks, add prettier:check and require formatting checks in AGENTS.md. Validation: backend build; 1398 backend/frontend tests passed (17 platform skips); 327 CLI tests passed (6 platform skips); 670 files pass format checks.
This commit is contained in:
1 parent
77ece406ac
commit
c5458596c8
346 files changed
+32133
-11556
No files matched your search
@@ -9,37 +9,60 @@ function isGone(error: unknown) {
|
||||
return code === 'ENOENT' || code === 'ENOTDIR';
|
||||
}
|
||||
|
||||
async function directoryVersion(root: string, parent: string, blacklist: string[]) {
|
||||
async function directoryVersion(
|
||||
root: string,
|
||||
parent: string,
|
||||
blacklist: string[],
|
||||
) {
|
||||
const dir = parent ? resolveFileAccess(root, [parent], blacklist) : root;
|
||||
const denied = () => Object.assign(new Error('Invalid directory'), { status: 403 });
|
||||
const denied = () =>
|
||||
Object.assign(new Error('Invalid directory'), { status: 403 });
|
||||
if (!dir) throw denied();
|
||||
try {
|
||||
if (await fs.realpath(dir) !== path.join(root, parent)) throw denied();
|
||||
if ((await fs.realpath(dir)) !== path.join(root, parent)) throw denied();
|
||||
const stat = await fs.lstat(dir, { bigint: true });
|
||||
return stat.isDirectory() ? [stat.dev, stat.ino, stat.mtimeNs, stat.ctimeNs].join(':') : null;
|
||||
return stat.isDirectory()
|
||||
? [stat.dev, stat.ino, stat.mtimeNs, stat.ctimeNs].join(':')
|
||||
: null;
|
||||
} catch (error) {
|
||||
if (isGone(error)) return null;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function checkDirectoryVersions(root: string, blacklist: string[], versions: DirectoryVersion[]) {
|
||||
export async function checkDirectoryVersions(
|
||||
root: string,
|
||||
blacklist: string[],
|
||||
versions: DirectoryVersion[],
|
||||
) {
|
||||
const realRoot = await fs.realpath(root);
|
||||
const matches = await Promise.all(versions.map(async ([parent, expected]) => {
|
||||
try {
|
||||
return await directoryVersion(realRoot, parent, blacklist) === expected;
|
||||
} catch (error) {
|
||||
// A formerly accessible directory may now be a symlink outside the root.
|
||||
if ((error as { status?: number }).status === 403) return false;
|
||||
throw error;
|
||||
}
|
||||
}));
|
||||
const matches = await Promise.all(
|
||||
versions.map(async ([parent, expected]) => {
|
||||
try {
|
||||
return (
|
||||
(await directoryVersion(realRoot, parent, blacklist)) === expected
|
||||
);
|
||||
} catch (error) {
|
||||
// A formerly accessible directory may now be a symlink outside the root.
|
||||
if ((error as { status?: number }).status === 403) return false;
|
||||
throw error;
|
||||
}
|
||||
}),
|
||||
);
|
||||
return matches.every(Boolean);
|
||||
}
|
||||
|
||||
export async function readDirectoryPage(root: string, blacklist: string[], directory: string, after: string,
|
||||
search: string, foldersOnly: boolean, newestFirst: boolean, limit = 50) {
|
||||
const blocked = new Set(blacklist.map(name => name.toLowerCase()));
|
||||
export async function readDirectoryPage(
|
||||
root: string,
|
||||
blacklist: string[],
|
||||
directory: string,
|
||||
after: string,
|
||||
search: string,
|
||||
foldersOnly: boolean,
|
||||
newestFirst: boolean,
|
||||
limit = 50,
|
||||
) {
|
||||
const blocked = new Set(blacklist.map((name) => name.toLowerCase()));
|
||||
const realRoot = await fs.realpath(root);
|
||||
const versions: DirectoryVersion[] = [];
|
||||
async function entries(parent: string) {
|
||||
@@ -47,36 +70,63 @@ export async function readDirectoryPage(root: string, blacklist: string[], direc
|
||||
versions.push([parent, version]);
|
||||
if (version === null) return [];
|
||||
try {
|
||||
return (await fs.readdir(path.join(realRoot, parent), { withFileTypes: true })).filter(e =>
|
||||
!blocked.has(e.name.toLowerCase()) && (e.isDirectory() || (!foldersOnly && e.isFile())));
|
||||
return (
|
||||
await fs.readdir(path.join(realRoot, parent), { withFileTypes: true })
|
||||
).filter(
|
||||
(e) =>
|
||||
!blocked.has(e.name.toLowerCase()) &&
|
||||
(e.isDirectory() || (!foldersOnly && e.isFile())),
|
||||
);
|
||||
} catch (error) {
|
||||
if (isGone(error)) return [];
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
const compare = (a: string, b: string) => a < b ? -1 : a > b ? 1 : 0;
|
||||
const compare = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
|
||||
const rows = [];
|
||||
let next: string | null = null;
|
||||
async function row(parent: string, title: string, directory: boolean) {
|
||||
const key = path.join(parent, title);
|
||||
if (directory) return { title, parent, key, type: 'directory', isLeaf: false };
|
||||
if (directory)
|
||||
return { title, parent, key, type: 'directory', isLeaf: false };
|
||||
try {
|
||||
const stat = await fs.lstat(path.join(root, key));
|
||||
if (stat.isFile()) return { title, parent, key, type: 'file', isLeaf: true, size: stat.size };
|
||||
} catch (error) { if (!isGone(error)) throw error; }
|
||||
if (stat.isFile())
|
||||
return {
|
||||
title,
|
||||
parent,
|
||||
key,
|
||||
type: 'file',
|
||||
isLeaf: true,
|
||||
size: stat.size,
|
||||
};
|
||||
} catch (error) {
|
||||
if (!isGone(error)) throw error;
|
||||
}
|
||||
}
|
||||
if (!search) {
|
||||
const sorted = (await entries(directory)).sort((a,b) =>
|
||||
Number(b.isDirectory()) - Number(a.isDirectory()) ||
|
||||
compare(a.name, b.name) * (newestFirst && !a.isDirectory() ? -1 : 1));
|
||||
const selected = sorted.filter(e => {
|
||||
const sorted = (await entries(directory)).sort(
|
||||
(a, b) =>
|
||||
Number(b.isDirectory()) - Number(a.isDirectory()) ||
|
||||
compare(a.name, b.name) * (newestFirst && !a.isDirectory() ? -1 : 1),
|
||||
);
|
||||
const selected = sorted.filter((e) => {
|
||||
if (!after) return true;
|
||||
const group = e.isDirectory() ? '0' : '1';
|
||||
return group > after[0] || (group === after[0] &&
|
||||
compare(e.name, after.slice(1)) * (newestFirst && !e.isDirectory() ? -1 : 1) > 0);
|
||||
return (
|
||||
group > after[0] ||
|
||||
(group === after[0] &&
|
||||
compare(e.name, after.slice(1)) *
|
||||
(newestFirst && !e.isDirectory() ? -1 : 1) >
|
||||
0)
|
||||
);
|
||||
});
|
||||
const page = selected.slice(0, limit);
|
||||
const data = (await Promise.all(page.map(e => row(directory, e.name, e.isDirectory())))).filter(Boolean);
|
||||
const data = (
|
||||
await Promise.all(
|
||||
page.map((e) => row(directory, e.name, e.isDirectory())),
|
||||
)
|
||||
).filter(Boolean);
|
||||
if (selected.length > limit) {
|
||||
const last = page[page.length - 1];
|
||||
next = (last.isDirectory() ? '0' : '1') + last.name;
|
||||
@@ -84,16 +134,26 @@ export async function readDirectoryPage(root: string, blacklist: string[], direc
|
||||
return { data, next, versions };
|
||||
}
|
||||
// Resume a depth-first search at the last visited entry, including on pages with zero matches.
|
||||
async function* walk(parent: string, resume: string[]): AsyncGenerator<{ parent: string; name: string; dir: boolean }> {
|
||||
const items = (await entries(parent)).sort((a,b) => compare(a.name,b.name));
|
||||
async function* walk(
|
||||
parent: string,
|
||||
resume: string[],
|
||||
): AsyncGenerator<{ parent: string; name: string; dir: boolean }> {
|
||||
const items = (await entries(parent)).sort((a, b) =>
|
||||
compare(a.name, b.name),
|
||||
);
|
||||
for (const item of items) {
|
||||
if (resume.length && item.name < resume[0]) continue;
|
||||
const same = item.name === resume[0];
|
||||
if (!same) yield { parent, name: item.name, dir: item.isDirectory() };
|
||||
if (item.isDirectory()) {
|
||||
try { yield* walk(path.join(parent, item.name), same ? resume.slice(1) : []); }
|
||||
catch (error) {
|
||||
if (!isGone(error) && (error as { status?: number }).status !== 403) throw error;
|
||||
try {
|
||||
yield* walk(
|
||||
path.join(parent, item.name),
|
||||
same ? resume.slice(1) : [],
|
||||
);
|
||||
} catch (error) {
|
||||
if (!isGone(error) && (error as { status?: number }).status !== 403)
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,7 +165,10 @@ export async function readDirectoryPage(root: string, blacklist: string[], direc
|
||||
const item = await row(entry.parent, entry.name, entry.dir);
|
||||
if (item) rows.push(item);
|
||||
}
|
||||
if (++visited >= 512 || rows.length === limit) { next = key; break; }
|
||||
if (++visited >= 512 || rows.length === limit) {
|
||||
next = key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return { data: rows, next, versions };
|
||||
}
|
||||
Reference in new issue
Block a user