Skip to content

Commit de33751

Browse files
committed
SP-725 Adds in memory scan
1 parent 9f0874d commit de33751

File tree

5 files changed

+109
-11
lines changed

5 files changed

+109
-11
lines changed

Diff for: CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
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.
44

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

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

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scanoss",
3-
"version": "0.13.2",
3+
"version": "0.14.0",
44
"description": "The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",

Diff for: src/sdk/scanner/Scanner.ts

+54-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { Dispatcher } from './Dispatcher/Dispatcher';
99
import { DispatchableItem } from './Dispatcher/DispatchableItem';
1010
import { DispatcherResponse } from './Dispatcher/DispatcherResponse';
1111
import { ScannerCfg } from './ScannerCfg';
12-
import { ScannerEvents, ScannerInput, ScannerResults } from './ScannerTypes';
12+
import {
13+
ContentScannerInput, ScannerComponent,
14+
ScannerEvents,
15+
ScannerInput,
16+
ScannerResults
17+
} from './ScannerTypes';
1318

1419
import { WfpProvider } from './WfpProvider/WfpProvider';
1520
import { FingerprintPackage } from './WfpProvider/FingerprintPackage';
@@ -18,11 +23,15 @@ import { WfpSplitter } from './WfpProvider/WfpSplitter/WfpSplitter';
1823

1924
import sortPaths from 'sort-paths';
2025
import { v4 as uuidv4 } from 'uuid';
26+
import path from 'path';
2127

2228
let finishPromiseResolve;
2329
let finishPromiseReject;
2430

2531
export class Scanner extends EventEmitter {
32+
33+
private readonly SCAN_FOLDER_NAME = 'scanner';
34+
2635
private scannerCfg: ScannerCfg;
2736

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

79+
private getScanFolderId(){
80+
return `${this.SCAN_FOLDER_NAME}-${this.getScannerId()}`;
81+
}
82+
7083
public init() {
7184
this.scanFinished = false;
7285
this.processingNewData = false;
@@ -89,7 +102,7 @@ export class Scanner extends EventEmitter {
89102
this.setDispatcherListeners();
90103

91104
if (this.workDirectory === undefined)
92-
this.setWorkDirectory(`${os.tmpdir()}/scanner-${this.getScannerId()}`);
105+
this.setWorkDirectory(`${os.tmpdir()}/${this.getScanFolderId()}`);
93106
}
94107

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

121-
public scan(scannerInput: Array<ScannerInput>): Promise<string> {
134+
public async scan(scannerInput: Array<ScannerInput>):Promise<string> {
135+
122136
this.init();
123137
this.createOutputFiles();
124138
this.scannerInput = scannerInput;
@@ -149,6 +163,43 @@ export class Scanner extends EventEmitter {
149163
return this.finishPromise;
150164
}
151165

166+
/**
167+
* Scans the provided content.
168+
*
169+
* @param {ContentScannerInput} contentScannerInput - The input containing content and file name.
170+
* @param {string} contentScannerInput.content - The content to be scanned.
171+
* @param {string} contentScannerInput.key - Unique key to be referenced on scan result .
172+
* @returns {Promise<ScannerComponent | null>} - The scan result as a `ScannerComponent` or `null` if no content is provided.
173+
*
174+
* @throws {Error} - Throws an error if there is an issue during the scan.
175+
*
176+
* */
177+
public async scanContents(contentScannerInput: ContentScannerInput):Promise<ScannerComponent | null> {
178+
if (!contentScannerInput.content) {
179+
this.reportLog('[ SCANNER ]: No input provided', 'warning');
180+
return null;
181+
}
182+
const workingDir = `${os.tmpdir()}/${this.getScanFolderId()}`;
183+
this.setWorkDirectory(workingDir);
184+
this.workDirectory = workingDir;
185+
186+
await fs.promises.writeFile(`${workingDir}/${contentScannerInput.key}`, contentScannerInput.content, 'utf-8');
187+
188+
const rootPath = path.resolve(`${workingDir}/${contentScannerInput.key}`);
189+
190+
// Build the input for a common scan
191+
const scannerInput: ScannerInput = {
192+
folderRoot: workingDir,
193+
fileList: [rootPath],
194+
};
195+
const input = {...contentScannerInput, ...scannerInput};
196+
197+
// Perform a common scan
198+
const resultPath = await this.scan([input]);
199+
200+
return JSON.parse(await fs.promises.readFile(resultPath, 'utf-8')) as ScannerComponent;
201+
}
202+
152203
public getScannerId() {
153204
return this.scannerId;
154205
}

Diff for: src/sdk/scanner/ScannerTypes.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,26 @@ export enum SbomMode {
4242
SBOM_IDENTIFY = 'identify'
4343
}
4444

45-
export interface ScannerInput {
46-
fileList: Array<string>;
47-
folderRoot?: string;
45+
export interface BaseScannerInput {
46+
wfpPath?: string;
47+
sbom?: string;
48+
sbomMode?: SbomMode;
4849
engineFlags?: number;
4950
winnowing?: {
5051
mode: WinnowingMode,
5152
}
52-
wfpPath?: string;
53-
sbom?: string;
54-
sbomMode?: SbomMode;
53+
}
54+
55+
export interface ScannerInput extends BaseScannerInput {
56+
fileList: Array<string>;
57+
folderRoot?: string;
5558
};
5659

60+
export interface ContentScannerInput extends BaseScannerInput{
61+
content: string,
62+
key: string,
63+
}
64+
5765
/********************** Scanner results types **********************/
5866

5967
export type ScannerResults = Record<string , ScannerComponent[]>;

Diff for: tests/sdk/Scanner/Scanner.spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as assert from 'assert';
2+
import {
3+
ContentScannerInput,
4+
Scanner,
5+
ScannerInput
6+
} from '../../../build/main';
7+
import fs from 'fs';
8+
import os from 'os';
9+
import path from 'path';
10+
import { expect } from 'chai';
11+
12+
describe('Suit test for scanner', () => {
13+
14+
it('Scan in memory', async function () {
15+
this.timeout(5000);
16+
const fileContent = await fs.promises.readFile(path.join(__dirname, '/WfpProvider/WfpCalculator/samples/file1.c'), 'utf-8');
17+
const scanner = new Scanner();
18+
const scannerInput: ContentScannerInput = {
19+
content: fileContent,
20+
key: "file1.c"
21+
};
22+
const results = await scanner.scanContents(scannerInput);
23+
assert.notEqual(Object.values(results).length, null);
24+
expect(results).to.be.an('object');
25+
});
26+
27+
it('Scan in memory empty content', async function () {
28+
const scanner = new Scanner();
29+
const scannerInput: ContentScannerInput = {
30+
content: "",
31+
key: "file1.c"
32+
};
33+
const results = await scanner.scanContents(scannerInput);
34+
assert.equal(results, null);
35+
});
36+
37+
});

0 commit comments

Comments
 (0)