-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite parser for win-storage from js to ts and simplify it
Use wrapper for win-credstore-parser,because Transform isn't injectable. In previous implementation each entity was separated on lines but currently we get chunks with all lines for each entity. I have to extends Transform this way because of: microsoft/TypeScript#17032
- Loading branch information
Showing
7 changed files
with
95 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
"use strict"; | ||
|
||
const stream = require("readable-stream"); | ||
const Transform = stream.Transform; | ||
import {injectable} from "inversify"; | ||
|
||
@injectable() | ||
export class WinCredStoreParsingStreamWrapper { | ||
private readonly instance: WinCredStoreParsingStream; | ||
|
||
constructor() { | ||
this.instance = new WinCredStoreParsingStream(); | ||
} | ||
|
||
public get parser(): WinCredStoreParsingStream { | ||
return this.instance; | ||
} | ||
} | ||
|
||
export class WinCredStoreParsingStream extends (Transform as { new(): any; }) { | ||
|
||
private static parseInputFieldsRegExp: RegExp = /^([^:]+):\s(.*)$/m; | ||
|
||
constructor() { | ||
super(); | ||
Transform.call(this, { | ||
objectMode: true | ||
}); | ||
this.currentEntry = undefined; | ||
} | ||
|
||
public _transform(chunk, encoding, callback) { | ||
const lines = chunk.toString(); | ||
|
||
if (!lines) { | ||
callback(); | ||
} | ||
|
||
if (WinCredStoreParsingStream.isNewData(lines)) { | ||
this.ensureCurrentEntryInitialized(); | ||
lines.split("\n").forEach((line) => { | ||
const match = WinCredStoreParsingStream.parseInputFieldsRegExp.exec(line); | ||
if (match) { | ||
const key = WinCredStoreParsingStream.separateWordsToCamelCase(match[1]); | ||
const value = match[2]; | ||
this.currentEntry[key] = value; | ||
} | ||
}); | ||
} | ||
|
||
callback(); | ||
} | ||
|
||
private static isNewData(line: string): boolean { | ||
return line !== ""; | ||
} | ||
|
||
private ensureCurrentEntryInitialized() { | ||
if (!this.currentEntry) { | ||
this.currentEntry = {}; | ||
} | ||
} | ||
|
||
private static separateWordsToCamelCase(fieldName: string): string { | ||
const parts = fieldName.split(" "); | ||
parts[0] = parts[0].toLowerCase(); | ||
return parts.join(""); | ||
} | ||
|
||
public _flush(callback) { | ||
if (this.currentEntry) { | ||
this.push(this.currentEntry); | ||
this.currentEntry = undefined; | ||
} | ||
callback(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters