-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use streaming when creating log symbols file. #2858
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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,125 @@ | ||
import { Readable } from "stream"; | ||
import { StringDecoder } from "string_decoder"; | ||
|
||
/** | ||
* Buffer to hold state used when splitting a text stream into lines. | ||
*/ | ||
export class SplitBuffer { | ||
private readonly decoder = new StringDecoder("utf8"); | ||
private readonly maxSeparatorLength: number; | ||
private buffer = ""; | ||
private searchIndex = 0; | ||
private ended = false; | ||
|
||
constructor(private readonly separators: readonly string[]) { | ||
this.maxSeparatorLength = separators | ||
.map((s) => s.length) | ||
.reduce((a, b) => Math.max(a, b), 0); | ||
} | ||
|
||
/** | ||
* Append new text data to the buffer. | ||
* @param chunk The chunk of data to append. | ||
*/ | ||
public addChunk(chunk: Buffer): void { | ||
this.buffer += this.decoder.write(chunk); | ||
} | ||
|
||
/** | ||
* Signal that the end of the input stream has been reached. | ||
*/ | ||
public end(): void { | ||
this.buffer += this.decoder.end(); | ||
this.ended = true; | ||
} | ||
|
||
/** | ||
* A version of startsWith that isn't overriden by a broken version of ms-python. | ||
* | ||
* The definition comes from | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith | ||
* which is CC0/public domain | ||
* | ||
* See https://github.com/github/vscode-codeql/issues/802 for more context as to why we need it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the upstream issues have been fixed. Maybe we can remove this workaround. (Not needed for this PR.) |
||
*/ | ||
private static startsWith( | ||
s: string, | ||
searchString: string, | ||
position: number, | ||
): boolean { | ||
const pos = position > 0 ? position | 0 : 0; | ||
return s.substring(pos, pos + searchString.length) === searchString; | ||
} | ||
|
||
/** | ||
* Extract the next full line from the buffer, if one is available. | ||
* @returns The text of the next available full line (without the separator), or `undefined` if no | ||
* line is available. | ||
*/ | ||
public getNextLine(): string | undefined { | ||
// If we haven't received all of the input yet, don't search too close to the end of the buffer, | ||
// or we could match a separator that's split across two chunks. For example, we could see "\r" | ||
// at the end of the buffer and match that, even though we were about to receive a "\n" right | ||
// after it. | ||
const maxSearchIndex = this.ended | ||
? this.buffer.length - 1 | ||
: this.buffer.length - this.maxSeparatorLength; | ||
while (this.searchIndex <= maxSearchIndex) { | ||
for (const separator of this.separators) { | ||
if (SplitBuffer.startsWith(this.buffer, separator, this.searchIndex)) { | ||
const line = this.buffer.slice(0, this.searchIndex); | ||
this.buffer = this.buffer.slice(this.searchIndex + separator.length); | ||
this.searchIndex = 0; | ||
return line; | ||
} | ||
} | ||
this.searchIndex++; | ||
} | ||
|
||
if (this.ended && this.buffer.length > 0) { | ||
// If we still have some text left in the buffer, return it as the last line. | ||
const line = this.buffer; | ||
this.buffer = ""; | ||
this.searchIndex = 0; | ||
return line; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Splits a text stream into lines based on a list of valid line separators. | ||
* @param stream The text stream to split. This stream will be fully consumed. | ||
* @param separators The list of strings that act as line separators. | ||
* @returns A sequence of lines (not including separators). | ||
*/ | ||
export async function* splitStreamAtSeparators( | ||
stream: Readable, | ||
separators: string[], | ||
): AsyncGenerator<string, void, unknown> { | ||
const buffer = new SplitBuffer(separators); | ||
for await (const chunk of stream) { | ||
buffer.addChunk(chunk); | ||
let line: string | undefined; | ||
do { | ||
line = buffer.getNextLine(); | ||
if (line !== undefined) { | ||
yield line; | ||
} | ||
} while (line !== undefined); | ||
} | ||
buffer.end(); | ||
let line: string | undefined; | ||
do { | ||
line = buffer.getNextLine(); | ||
if (line !== undefined) { | ||
yield line; | ||
} | ||
} while (line !== undefined); | ||
} | ||
|
||
/** | ||
* Standard line endings for splitting human-readable text. | ||
*/ | ||
export const LINE_ENDINGS = ["\r\n", "\r", "\n"]; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there anything in this file that has changed when you extracted it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, other than the casing of
LINE_ENDINGS
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and the bug I discovered when you made me write a unit test:)