Files
qinglong/test/helpers/load-security-module.cjs
T
whyour c5458596c8 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.
2026-11-22 12:24:29 +08:00

68 lines
2.1 KiB
JavaScript

const fs = require('node:fs');
const path = require('node:path');
const { createRequire } = require('node:module');
const ts = require('typescript');
function moduleFile(file, name) {
const target = path.resolve(path.dirname(file), name);
const resolved = [
target,
`${target}.ts`,
`${target}.js`,
`${target}.cjs`,
path.join(target, 'index.ts'),
path.join(target, 'index.js'),
].find(
(candidate) => fs.existsSync(candidate) && fs.statSync(candidate).isFile(),
);
return resolved;
}
module.exports = function loadSecurityModule(
file,
mocks = {},
cache = new Map(),
resolvedMocks,
) {
file = path.resolve(file);
// A dependency keeps its mock when a nested module imports it at another depth.
resolvedMocks ??= new Map(
Object.entries(mocks).flatMap(([name, value]) => {
const target = name.startsWith('.') ? moduleFile(file, name) : undefined;
return target ? [[target, value]] : [];
}),
);
if (cache.has(file)) return cache.get(file).exports;
const module = { exports: {} };
cache.set(file, module);
const localRequire = createRequire(file);
const requireModule = (name) => {
const resolved = name.startsWith('.') ? moduleFile(file, name) : undefined;
if (resolvedMocks.has(resolved)) return resolvedMocks.get(resolved);
if (Object.hasOwn(mocks, name)) return mocks[name];
if (name.startsWith('.')) {
const target = path.resolve(path.dirname(file), name);
if (fs.existsSync(`${target}.ts`)) {
return loadSecurityModule(`${target}.ts`, mocks, cache, resolvedMocks);
}
}
return localRequire(name);
};
const { outputText } = ts.transpileModule(fs.readFileSync(file, 'utf8'), {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2020,
experimentalDecorators: true,
esModuleInterop: true,
},
});
// Use the host realm: express-unless checks RegExp with instanceof.
new Function('require', 'module', 'exports', '__dirname', outputText)(
requireModule,
module,
module.exports,
path.dirname(file),
);
return module.exports;
};