@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
35 lines (34 loc) • 1.12 kB
JavaScript
import { open } from "node:fs/promises";
//#region src/utils/gcode.utils.ts
const gcodeScanningChunkSize = 64 * 1024;
/**
* Read local GCode metadata or thumbnails
* @param filePath
* @param numberOfLines
*/
async function readLastLinesLocal(filePath, numberOfLines) {
const file = await open(filePath, "r");
try {
const buffer = Buffer.alloc(gcodeScanningChunkSize);
const fileSize = await file.stat();
let lines = [];
let position = fileSize.size;
let iterationsLeft = 100;
while (lines.length <= numberOfLines && position > 0) {
iterationsLeft--;
if (iterationsLeft <= 0) throw new Error("Too many iterations reached, 'readLastLines' aborted");
const bytesToRead = Math.min(gcodeScanningChunkSize, position);
position -= bytesToRead;
await file.read(buffer, 0, bytesToRead, position);
lines = buffer.toString("utf-8", 0, bytesToRead).split("\n").concat(lines);
await file.close();
}
return lines.slice(-numberOfLines);
} catch (e) {
await file.close();
throw e;
}
}
//#endregion
export { gcodeScanningChunkSize, readLastLinesLocal };
//# sourceMappingURL=gcode.utils.js.map