forked from github/vscode-codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsarif-parser.ts
More file actions
51 lines (43 loc) · 1.71 KB
/
sarif-parser.ts
File metadata and controls
51 lines (43 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as Sarif from 'sarif';
import * as fs from 'fs-extra';
import Assembler = require('stream-json/Assembler');
import { getErrorMessage } from './pure/helpers-pure';
import Pick = require('stream-json/filters/Pick');
const DUMMY_TOOL: Sarif.Tool = { driver: { name: '' } };
export async function sarifParser(interpretedResultsPath: string): Promise<Sarif.Log> {
try {
// Parse the SARIF file into token streams, filtering out only the results array.
const pipeline = fs.createReadStream(interpretedResultsPath).pipe(Pick.withParser({ filter: 'runs.0.results' }));
// Creates JavaScript objects from the token stream
const asm = Assembler.connectTo(pipeline);
// Returns a constructed Log object with the results of an empty array if no results were found.
// If the parser fails for any reason, it will reject the promise.
return await new Promise((resolve, reject) => {
let alreadyDone = false;
pipeline.on('error', (error) => {
reject(error);
});
// If the parser pipeline completes before the assembler, we've reached end of file and have not found any results.
pipeline.on('end', () => {
if (!alreadyDone) {
reject(new Error('Invalid SARIF file: expecting at least one run with result.'));
}
});
asm.on('done', (asm) => {
const log: Sarif.Log = {
version: '2.1.0',
runs: [
{
tool: DUMMY_TOOL,
results: asm.current ?? []
}
]
};
resolve(log);
alreadyDone = true;
});
});
} catch (e) {
throw new Error(`Parsing output of interpretation failed: ${(e as any).stderr || getErrorMessage(e)}`);
}
}