Skip to content

Commit

Permalink
SP-725 Adds in memory scan
Browse files Browse the repository at this point in the history
  • Loading branch information
agustingroh committed May 23, 2024
1 parent 9f0874d commit 0c47ebc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.13.2](https://github.com/scanoss/scanoss.js/compare/v0.13.0...v0.13.1) (2024-05-20)
### [0.14.0](https://github.com/scanoss/scanoss.js/compare/v0.13.2...v0.14.0) (2024-05-23)

### [0.13.2](https://github.com/scanoss/scanoss.js/compare/v0.13.1...v0.13.2) (2024-05-20)

### [0.13.1](https://github.com/scanoss/scanoss.js/compare/v0.13.0...v0.13.1) (2024-05-15)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scanoss",
"version": "0.13.2",
"version": "0.14.0",
"description": "The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
57 changes: 54 additions & 3 deletions src/sdk/scanner/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { Dispatcher } from './Dispatcher/Dispatcher';
import { DispatchableItem } from './Dispatcher/DispatchableItem';
import { DispatcherResponse } from './Dispatcher/DispatcherResponse';
import { ScannerCfg } from './ScannerCfg';
import { ScannerEvents, ScannerInput, ScannerResults } from './ScannerTypes';
import {
ContentScannerInput, ScannerComponent,
ScannerEvents,
ScannerInput,
ScannerResults
} from './ScannerTypes';

import { WfpProvider } from './WfpProvider/WfpProvider';
import { FingerprintPackage } from './WfpProvider/FingerprintPackage';
Expand All @@ -18,11 +23,15 @@ import { WfpSplitter } from './WfpProvider/WfpSplitter/WfpSplitter';

import sortPaths from 'sort-paths';
import { v4 as uuidv4 } from 'uuid';
import path from 'path';

let finishPromiseResolve;
let finishPromiseReject;

export class Scanner extends EventEmitter {

private readonly SCAN_FOLDER_NAME = 'scanner';

private scannerCfg: ScannerCfg;

private workDirectory: string;
Expand Down Expand Up @@ -67,6 +76,10 @@ export class Scanner extends EventEmitter {
this.scannerId = new Date().getTime().toString();
}

private getScanFolderId(){
return `${this.SCAN_FOLDER_NAME}-${this.getScannerId()}`;
}

public init() {
this.scanFinished = false;
this.processingNewData = false;
Expand All @@ -89,7 +102,7 @@ export class Scanner extends EventEmitter {
this.setDispatcherListeners();

if (this.workDirectory === undefined)
this.setWorkDirectory(`${os.tmpdir()}/scanner-${this.getScannerId()}`);
this.setWorkDirectory(`${os.tmpdir()}/${this.getScanFolderId()}`);
}

public setWorkDirectory(workDirectory: string) {
Expand Down Expand Up @@ -118,7 +131,8 @@ export class Scanner extends EventEmitter {
if (fs.existsSync(this.wfpFilePath)) fs.unlinkSync(this.wfpFilePath);
}

public scan(scannerInput: Array<ScannerInput>): Promise<string> {
public scan(scannerInput: Array<ScannerInput>):Promise<string> {

this.init();
this.createOutputFiles();
this.scannerInput = scannerInput;
Expand Down Expand Up @@ -149,6 +163,43 @@ export class Scanner extends EventEmitter {
return this.finishPromise;
}

/**
* Scans the provided content.
*
* @param {ContentScannerInput} contentScannerInput - The input containing content and file name.
* @param {string} contentScannerInput.content - The content to be scanned.
* @param {string} contentScannerInput.key - Unique key to be referenced on scan result .
* @returns {Promise<ScannerComponent | null>} - The scan result as a `ScannerComponent` or `null` if no content is provided.
*
* @throws {Error} - Throws an error if there is an issue during the scan.
*
* */
public async scanContent(contentScannerInput: ContentScannerInput):Promise<ScannerComponent | null> {
if (!contentScannerInput.content) {
this.reportLog('[ SCANNER ]: No input provided', 'warning');
return null;
}
const workingDir = `${os.tmpdir()}/${this.getScanFolderId()}`;
this.setWorkDirectory(workingDir);
this.workDirectory = workingDir;

await fs.promises.writeFile(`${workingDir}/${contentScannerInput.key}`, contentScannerInput.content, 'utf-8');

const rootPath = path.resolve(`${workingDir}/${contentScannerInput.key}`);

// Build the input for a common scan
const scannerInput: ScannerInput = {
folderRoot: workingDir,
fileList: [rootPath],
};
const input = {...contentScannerInput, ...scannerInput};

// Perform a common scan
const resultPath = await this.scan([input]);

return JSON.parse(await fs.promises.readFile(resultPath, 'utf-8')) as ScannerComponent;
}

public getScannerId() {
return this.scannerId;
}
Expand Down
26 changes: 20 additions & 6 deletions src/sdk/scanner/ScannerTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,32 @@ export enum SbomMode {
SBOM_IDENTIFY = 'identify'
}

export interface ScannerInput {
fileList: Array<string>;
folderRoot?: string;
export enum ScanType {
FILE = 'FILE',
CONTENT = 'CONTENT'
}

export interface BaseScannerInput {
wfpPath?: string;
sbom?: string;
sbomMode?: SbomMode;
engineFlags?: number;
winnowing?: {
mode: WinnowingMode,
}
wfpPath?: string;
sbom?: string;
sbomMode?: SbomMode;
scanType?: ScanType,
}

export interface ScannerInput extends BaseScannerInput {
fileList: Array<string>;
folderRoot?: string;
};

export interface ContentScannerInput extends BaseScannerInput{
content: string,
key: string,
}

/********************** Scanner results types **********************/

export type ScannerResults = Record<string , ScannerComponent[]>;
Expand Down
37 changes: 37 additions & 0 deletions tests/sdk/Scanner/Scanner.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as assert from 'assert';
import {
ContentScannerInput,
Scanner,
ScannerInput
} from '../../../build/main';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { expect } from 'chai';

describe('Suit test for scanner', () => {

it('Scan in memory', async function () {
this.timeout(5000);
const fileContent = await fs.promises.readFile(path.join(__dirname, '/WfpProvider/WfpCalculator/samples/file1.c'), 'utf-8');
const scanner = new Scanner();
const scannerInput: ContentScannerInput = {
content: fileContent,
key: "file1.c"
};
const results = await scanner.scanContent(scannerInput);
assert.notEqual(Object.values(results).length, null);
expect(results).to.be.an('object');
});

it('Scan in memory empty content', async function () {
const scanner = new Scanner();
const scannerInput: ContentScannerInput = {
content: "",
key: "file1.c"
};
const results = await scanner.scanContent(scannerInput);
assert.equal(results, null);
});

});

0 comments on commit 0c47ebc

Please sign in to comment.