Created
February 12, 2025 01:54
-
-
Save Shelob9/da9ce81990ba925f48c5820cc0bbf7e4 to your computer and use it in GitHub Desktop.
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
const fs = require('fs'); | |
function readFromEnd(filePath, chunkSize = 1024) { | |
const stats = fs.statSync(filePath); | |
let position = stats.size; | |
let buffer = ''; | |
const lines = []; | |
const stream = fs.createReadStream(filePath, { | |
highWaterMark: chunkSize, | |
start: Math.max(0, position - chunkSize), | |
end: position - 1 | |
}); | |
stream.on('data', (chunk) => { | |
buffer = chunk.toString() + buffer; | |
let newlineIndex; | |
while ((newlineIndex = buffer.lastIndexOf('\n')) !== -1) { | |
const line = buffer.slice(newlineIndex + 1); | |
lines.push(line); | |
buffer = buffer.slice(0, newlineIndex); | |
} | |
position -= chunkSize; | |
if (position > 0) { | |
stream.close(); | |
stream.open(filePath, { | |
highWaterMark: chunkSize, | |
start: Math.max(0, position - chunkSize), | |
end: position - 1 | |
}); | |
} else { | |
if (buffer.length > 0) { | |
lines.push(buffer); | |
} | |
console.log('Finished reading file:', lines); | |
} | |
}); | |
stream.on('end', () => { | |
if (buffer.length > 0) { | |
lines.push(buffer); | |
} | |
console.log('Finished reading file:', lines); | |
}); | |
stream.on('error', (err) => { | |
console.error('Error reading file:', err); | |
}); | |
} | |
// Example usage | |
readFromEnd('path/to/your/file.jsonld'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment