mirror of
https://github.com/whyour/qinglong.git
synced 2026-11-30 16:27:21 +08:00
* feat(cli): add unified Commander CLI for QingLong 2.x * fix(cli): publish via npm and address security review feedback * ci(cli): package npm artifacts and remove evaluation collateral * test(cli): use a fixed shell fixture for log retention * refactor(cli): separate remote npm client from panel tools * feat(cli): cover active panel OpenAPI resources * docs(cli): unify authentication and skill guidance * refactor(cli): isolate internal commands and generate Commander help * refactor(cli): organize remote and internal modules by responsibility * ci(cli): publish verified npm archives from master * fix(cli): publish under the whyour npm scope * ci: use npm trusted publishing for both packages * docs: introduce the published CLI on the project homepage * fix(cli): preserve server log truncation and correct login hints * fix(cli): accept dashboard record request bodies * fix(cli): preserve stdin for local task execution * fix(cli): resolve task executables after changing directory * fix(cli): preserve shell function tasks and sanitize test failures * fix(cli): preserve shell hook state and resolve workdir after hooks * fix(cli): preserve cleanup across shared shell task timeouts * fix(cli): isolate shell control descriptors and reap timed-out descendants
87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
import { constants } from 'node:os';
|
|
|
|
const storage = new AsyncLocalStorage<AbortSignal>();
|
|
// Match the six signals handled by the retained 2.x task.sh entrypoint.
|
|
export const commandSignals = [
|
|
'SIGINT',
|
|
'SIGTERM',
|
|
'SIGHUP',
|
|
'SIGQUIT',
|
|
'SIGALRM',
|
|
'SIGTSTP',
|
|
] as const;
|
|
|
|
export function commandSignal(
|
|
reason: unknown,
|
|
): (typeof commandSignals)[number] {
|
|
return commandSignals.find((name) => name === reason) ?? 'SIGTERM';
|
|
}
|
|
|
|
export const operationSignal = (): AbortSignal | undefined =>
|
|
storage.getStore();
|
|
|
|
export function interruptedCode(signal?: AbortSignal): number | undefined {
|
|
if (!signal?.aborted) return undefined;
|
|
const reason: unknown = signal.reason;
|
|
const name = commandSignal(reason);
|
|
return 128 + constants.signals[name];
|
|
}
|
|
|
|
export async function cancellableOperation<T>(
|
|
signal: AbortSignal | undefined,
|
|
operation: () => Promise<T>,
|
|
): Promise<T> {
|
|
if (!signal) return operation();
|
|
signal.throwIfAborted();
|
|
const result = await storage.run(signal, operation);
|
|
signal.throwIfAborted();
|
|
return result;
|
|
}
|
|
|
|
export async function withCommandCancellation(
|
|
operation: (signal: AbortSignal) => Promise<number>,
|
|
): Promise<number> {
|
|
const controller = new AbortController();
|
|
const listeners = commandSignals.map((name) => {
|
|
const listener = () => controller.abort(name);
|
|
process.on(name, listener);
|
|
return { name, listener };
|
|
});
|
|
try {
|
|
return await operation(controller.signal);
|
|
} finally {
|
|
for (const { name, listener } of listeners) process.off(name, listener);
|
|
}
|
|
}
|
|
|
|
// Recovery must finish even after the operation that triggered it was cancelled.
|
|
export function withoutCancellation<T>(
|
|
operation: () => Promise<T>,
|
|
): Promise<T> {
|
|
return storage.exit(operation);
|
|
}
|
|
|
|
export function requestCancellation(timeoutMs: number): {
|
|
signal: AbortSignal;
|
|
dispose: () => void;
|
|
} {
|
|
const parent = operationSignal();
|
|
const controller = new AbortController();
|
|
const abort = () => controller.abort(parent?.reason);
|
|
const timeout = setTimeout(
|
|
() => controller.abort(new Error('Request timed out.')),
|
|
timeoutMs,
|
|
);
|
|
timeout.unref();
|
|
parent?.addEventListener('abort', abort, { once: true });
|
|
if (parent?.aborted) abort();
|
|
return {
|
|
signal: controller.signal,
|
|
dispose: () => {
|
|
clearTimeout(timeout);
|
|
parent?.removeEventListener('abort', abort);
|
|
},
|
|
};
|
|
}
|