Skip to content

Commit

Permalink
fix(language-server): made xml parser async, updates language server,…
Browse files Browse the repository at this point in the history
… adds notification handler
  • Loading branch information
invincibleJai authored and Ravindra Singh Ratnawat committed Oct 26, 2018
1 parent 2ace23b commit a738db9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2,201 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
"dependencies": {
"request": "^2.79.0",
"stream-json": "0.4.2",
"vscode-languageserver": "^2.2.0",
"vscode-languageserver": "^4.1.3",
"winston": "2.3.1",
"xml2object": "0.1.2"
},
"devDependencies": {
"@types/node": "^6.0.52",
"@krux/condition-jenkins": "1.0.1",
"semantic-release": "8.2.0",
"typescript": "^2.1.4"
Expand Down
26 changes: 17 additions & 9 deletions src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,21 @@ class NaivePomXmlSaxParser {
// the XML document doesn't have to be well-formed, that's fine
parser.error = null;
});
parser.on("end", function () {
// the XML document doesn't have to be well-formed, that's fine
// parser.error = null;
this.dependencies = deps;
});
return parser
}

parse(): Array<IDependency> {
try {
this.stream.pipe(this.parser.saxStream);
} catch (e) {
console.error(e.message)
}
return this.dependencies
async parse() {
return new Promise(resolve => {
this.stream.pipe(this.parser.saxStream).on('end', (data) => {
resolve(this.dependencies);
});
});

}
}

Expand All @@ -204,8 +209,11 @@ class PomXmlDependencyCollector {

async collect(file: Stream): Promise<Array<IDependency>> {
let parser = new NaivePomXmlSaxParser(file);
let dependencies: Array<IDependency> = parser.parse();
return dependencies;
let dependencies;
await parser.parse().then(data => {
dependencies = data;
});
return dependencies || [];
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,17 @@ files.on(EventStream.Diagnostics, "^package\\.json$", (uri, name, contents) => {
/* Convert from readable stream into string */
let stream = stream_from_string(contents);
let collector = new DependencyCollector(null);
connection.sendNotification('caNotification', 'Analysing dependencies for any security vulnerability');

collector.collect(stream).then((deps) => {
let diagnostics = [];
/* Aggregate asynchronous requests and send the diagnostics at once */
let aggregator = new Aggregator(deps, () => {
if(diagnostics.length > 0) {
connection.sendNotification('caNotification', `Out of ${deps.length}, ${diagnostics.length} dependencies have vulnerabilities`);
} else {
connection.sendNotification('caNotification', `Out of ${deps.length}, No dependencies have vulnerabilities`);
}
connection.sendDiagnostics({uri: uri, diagnostics: diagnostics});
});
const regexVersion = new RegExp(/^(\d+\.)?(\d+\.)?(\d+)$/);
Expand All @@ -272,11 +278,17 @@ files.on(EventStream.Diagnostics, "^pom\\.xml$", (uri, name, contents) => {
/* Convert from readable stream into string */
let stream = stream_from_string(contents);
let collector = new PomXmlDependencyCollector();
connection.sendNotification('caNotification', 'Analysing dependencies for any security vulnerability');

collector.collect(stream).then((deps) => {
let diagnostics = [];
/* Aggregate asynchronous requests and send the diagnostics at once */
let aggregator = new Aggregator(deps, () => {
if(diagnostics.length > 0) {
connection.sendNotification('caNotification', `Out of ${deps.length}, ${diagnostics.length} dependencies have vulnerabilities`);
} else {
connection.sendNotification('caNotification', `Out of ${deps.length}, No dependencies have vulnerabilities`);
}
connection.sendDiagnostics({uri: uri, diagnostics: diagnostics});
});
const regexVersion = /^((\d+|[A-Za-z]+)[-.])*((\d+|[A-Za-z]+))$/
Expand Down
Loading

0 comments on commit a738db9

Please sign in to comment.