text-stream-search
Version:
Searches for occurrences of a given search term in Node.js text streams
46 lines • 1.81 kB
JavaScript
import { SearchList } from "./search-list.js";
import { RegexSearch } from "./searches/regex-search.js";
import { StringSearch } from "./searches/string-search.js";
import { TextAccumulator } from "./text-accumulator.js";
/**
* TextStreamSearch searches ReadableStreams for text and regexes.
*/
export default class TextStreamSearch {
constructor(stream) {
this.streamText = new TextAccumulator();
this.searchList = new SearchList();
stream.on("data", this.onStreamData.bind(this));
}
/** Fulltext returns the complete content captured from this stream so far. */
fullText() {
return this.streamText.toString();
}
/**
* WaitForRegex returns a promise that resolves with the matching text
* when the given RegExp shows up in observed stream.
* If a timeout is given, aborts after the given duration.
*/
waitForRegex(regex, timeout) {
return new Promise((resolve, reject) => {
this.searchList.push(new RegexSearch(regex, resolve, reject, this.streamText, timeout));
this.searchList.scan();
});
}
/**
* WaitForText returns a promise that resolves with the matching text
* when the given text shows up in the observed stream.
* If a timeout is given, aborts after the given duration.
*/
waitForText(text, timeout) {
return new Promise((resolve, reject) => {
this.searchList.push(new StringSearch(text, resolve, reject, this.streamText, timeout));
this.searchList.scan();
});
}
/** OnStreamData is called when new text arrives on the input stream. */
onStreamData(data) {
this.streamText.push(data.toString());
this.searchList.scan();
}
}
//# sourceMappingURL=text-stream-search.js.map