I have a long log file that I need to read from the bottom, line by line, stopping when a certain condition is met, and then returning that line. Also this needs to work on Windows. The file will probably be pretty long, so I don’t want to read the entire file and then reverse it.
I’m currently using reverse-line-reader, however it hasn’t been updated since 2015 and the async functionality leaves much to be desired.
Every other package I’ve seen is either archived, requires me to know the number of lines I want ahead of time, or doesn’t process by lines.
Is there any way to do this in vanilla Node, or is there a package that I haven’t seen that does what I need?
Current code, if it’s at all helpful to explain what I need:
reverseLineReader.eachLine(this.#path, (raw: string, last: boolean) => {
if (raw) { // skip blank line at end of file
const line = JSON.parse(raw)
if (line.event === 'FSDJump') {
this.location = new System(line)
Log.write(`Current location set to ${this.location.name}.`)
this.emit('ENTERED_NEW_SYSTEM')
return false // stop reading
} else if (last) {
log('Unable to find last hyperspace jump. Searching for last known location.')
return false
}
}
})
Thanks!