secret-scan-cli
Version: 
A tool to scan codebases for potential secrets and sensitive information
41 lines (35 loc) • 1.17 kB
JavaScript
// src/worker.js
import { parentPort, workerData } from 'worker_threads';
import fs from 'fs';
import chalk from 'chalk';
import { patterns } from './config.js';
// Function to scan a file for secrets
function scanFile(filePath) {
  try {
    const content = fs.readFileSync(filePath, 'utf8');
    const lines = content.split('\n');
    let foundSecrets = [];
    for (const [type, regex] of Object.entries(patterns)) {
      const matches = [];
      const lineNumbers = [];
      
      lines.forEach((line, index) => {
        const lineMatches = line.match(regex);
        if (lineMatches) {
          matches.push(...lineMatches);
          lineNumbers.push(index + 1);
        }
      });
      if (matches.length > 0) {
        foundSecrets.push({ type, matches, lineNumbers });
      }
    }
    return foundSecrets;
  } catch (error) {
    console.error(chalk.yellow(`Warning: Could not read file ${filePath} (skipping): ${error.message}`));
    return [];
  }
}
// Worker thread logic
const files = workerData;
const results = files.map((file) => ({ file, secrets: scanFile(file) }));
parentPort.postMessage(results);