We do not need full text available in order to match using regular expression, try https://www.npmjs.com/package/@yiminghe/regexp :
npm i @yiminghe/regexp
Then we can pull char from stream and match it by regular expression.
import * as regexp from "@yiminghe/regexp";
(async function () {
const fakeData = ["x", "a", "b", "c", "a", "a", "b"];
let index = 0;
let length = fakeData.length;
let promise;
let stream = [];
// push data to steam
setInterval(() => {
stream.push(fakeData[index++ % length]);
if (promise) {
const { resolve } = promise;
promise = null;
resolve();
}
}, 500);
const patternInstance = regexp.compile("a+b", { async: true });
const matcher = patternInstance.matcherAsync(() => {
// fetch data from stream
return new Promise((resolve) => {
function r() {
if (stream.length) {
resolve(stream);
stream = [];
}
}
if (stream.length) {
r();
} else {
// waiting for data
promise = {
resolve: r
};
}
});
});
while (true) {
const { match } = await matcher.match();
console.log("match: ", match);
}
})();
document.getElementById("root").innerHTML = "open console";