UNPKG

@fdm-monster/server

Version:

FDM Monster is a bulk OctoPrint manager to set up, configure and monitor 3D printers. Our aim is to provide extremely optimized websocket performance and reliability.

48 lines (47 loc) 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { gcodeScanningChunkSize: function() { return gcodeScanningChunkSize; }, readLastLinesLocal: function() { return readLastLinesLocal; } }); const _promises = require("node:fs/promises"); const gcodeScanningChunkSize = 64 * 1024; async function readLastLinesLocal(filePath, numberOfLines) { const file = await (0, _promises.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); const chunk = buffer.toString("utf-8", 0, bytesToRead); lines = chunk.split("\n").concat(lines); await file.close(); } return lines.slice(-numberOfLines); } catch (e) { await file.close(); throw e; } } //# sourceMappingURL=gcode.utils.js.map