Skip to main content

Scanning with scanoss.json

To scan using a scanoss.json settings file, load and parse the file, pass it to ScannerCfg.SCANOSS_SETTINGS, and configure the Scanner with your API key and endpoint before calling scan():
// Import as ES6
import { Scanner, ScannerCfg, ScannerEvents } from "scanoss";
import { readFileSync } from "fs";

// Import as CommonJS
// const { Scanner, ScannerEvents } = require('scanoss');

const settings = JSON.parse(
  readFileSync("/yourProjectFolder/scanoss.json", "utf-8"),
);

const cfg = new ScannerCfg();
cfg.SCANOSS_SETTINGS = settings; // parsed object, not a file path

const scanner = new Scanner(cfg);
scanner.API_KEY = "your-api-key";
scanner.API_URL = "https://your-custom-api-url";

// Set the folder path where the module will save the scan results and fingerprints
// If is not specified, the module will create a folder on tmp
// directory using a timestamp as a name
scanner.setWorkDirectory("/yourProjectFolder/ScanResults/");

// Set the scanner log event handler
scanner.on(ScannerEvents.SCANNER_LOG, (logTxt) => console.log(logTxt));

// Set the scanner finish event handler
scanner.on(ScannerEvents.SCAN_DONE, (resultPath) => {
  console.log("Path to results: ", resultPath);
});

const scannerInput = {
  fileList: ["/yourProjectFolder/example1.c"],
};

// Launch the scanner
scanner.scan([scannerInput]);

Scanner Events Reference

The Scanner class (and Fingerprint, which shares the same event system) emits the following events, which can be subscribed to via .on():
EventDescription
SCAN_DONEScan completed; provides the path to the results file
SCANNER_LOGInternal log messages emitted by the scanner
DISPATCHER_NEW_DATANew results received from the server; not yet written to the output file
RESULTS_APPENDEDBuffered results written and appended to the output file
WINNOWING_STATUSProgress update during fingerprint generation
ERRORAn error occurred during scanning