repomix
Version:
A tool to pack repository contents to single file for AI consumption
82 lines (81 loc) • 3.14 kB
JavaScript
import pc from 'picocolors';
import { logger } from '../../shared/logger.js';
import { getProcessConcurrency as defaultGetProcessConcurrency, initTaskRunner, } from '../../shared/processConcurrency.js';
const BATCH_SIZE = 50;
export const runSecurityCheck = async (rawFiles, progressCallback = () => { }, gitDiffResult, gitLogResult, deps = {
initTaskRunner,
getProcessConcurrency: defaultGetProcessConcurrency,
}) => {
const gitDiffItems = [];
const gitLogItems = [];
if (gitDiffResult) {
if (gitDiffResult.workTreeDiffContent) {
gitDiffItems.push({
filePath: 'Working tree changes',
content: gitDiffResult.workTreeDiffContent,
type: 'gitDiff',
});
}
if (gitDiffResult.stagedDiffContent) {
gitDiffItems.push({
filePath: 'Staged changes',
content: gitDiffResult.stagedDiffContent,
type: 'gitDiff',
});
}
}
if (gitLogResult) {
if (gitLogResult.logContent) {
gitLogItems.push({
filePath: 'Git log history',
content: gitLogResult.logContent,
type: 'gitLog',
});
}
}
const fileItems = rawFiles.map((file) => ({
filePath: file.path,
content: file.content,
type: 'file',
}));
const allItems = [...fileItems, ...gitDiffItems, ...gitLogItems];
const totalItems = allItems.length;
if (totalItems === 0) {
return [];
}
const maxSecurityWorkers = Math.min(2, deps.getProcessConcurrency());
const taskRunner = deps.initTaskRunner({
numOfTasks: totalItems,
workerType: 'securityCheck',
runtime: 'worker_threads',
maxWorkerThreads: maxSecurityWorkers,
});
const batches = [];
for (let i = 0; i < allItems.length; i += BATCH_SIZE) {
batches.push(allItems.slice(i, i + BATCH_SIZE));
}
try {
logger.trace(`Starting security check for ${totalItems} files/content in ${batches.length} batches`);
const startTime = process.hrtime.bigint();
let completedItems = 0;
const batchResults = await Promise.all(batches.map(async (batch) => {
const results = await taskRunner.run({ items: batch });
completedItems += batch.length;
const lastItem = batch[batch.length - 1];
progressCallback(`Running security check... (${completedItems}/${totalItems}) ${pc.dim(lastItem.filePath)}`);
logger.trace(`Running security check... (${completedItems}/${totalItems}) ${lastItem.filePath}`);
return results;
}));
const endTime = process.hrtime.bigint();
const duration = Number(endTime - startTime) / 1e6;
logger.trace(`Security check completed in ${duration.toFixed(2)}ms`);
return batchResults.flat().filter((result) => result !== null);
}
catch (error) {
logger.error('Error during security check:', error);
throw error;
}
finally {
await taskRunner.cleanup();
}
};