Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions src/gep/assetStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,54 @@ function getLastEventId() {
try {
const p = eventsPath();
if (!fs.existsSync(p)) return null;
const raw = fs.readFileSync(p, 'utf8');
const lines = raw.split('\n').map(l => l.trim()).filter(Boolean);
if (lines.length === 0) return null;
const last = JSON.parse(lines[lines.length - 1]);
return last && typeof last.id === 'string' ? last.id : null;
const stat = fs.statSync(p);
if (stat.size < 64 * 1024) {
// Small files: read entire file
const raw = fs.readFileSync(p, 'utf8');
const lines = raw.split('\n').map(l => l.trim()).filter(Boolean);
if (lines.length === 0) return null;
const last = JSON.parse(lines[lines.length - 1]);
return last && typeof last.id === 'string' ? last.id : null;
} else {
// Large files: read only last 4KB
const fd = fs.openSync(p, 'r');
const buf = Buffer.alloc(4096);
fs.readSync(fd, buf, 0, 4096, stat.size - 4096);
fs.closeSync(fd);
const raw = buf.toString('utf8');
const lines = raw.split('\n').map(l => l.trim()).filter(Boolean);
if (lines.length === 0) return null;
const last = JSON.parse(lines[lines.length - 1]);
return last && typeof last.id === 'string' ? last.id : null;
}
} catch { return null; }
}

function readAllEvents() {
try {
const p = eventsPath();
if (!fs.existsSync(p)) return [];
const raw = fs.readFileSync(p, 'utf8');
return raw.split('\n').map(l => l.trim()).filter(Boolean).map(l => {
try { return JSON.parse(l); } catch { return null; }
}).filter(Boolean);
const stat = fs.statSync(p);
const MAX_EVENTS_TO_READ = 10000;
const CHUNK_SIZE = 1024 * 1024; // 1MB chunks
if (stat.size < CHUNK_SIZE * MAX_EVENTS_TO_READ) {
// Small files: read entire file
const raw = fs.readFileSync(p, 'utf8');
return raw.split('\n').map(l => l.trim()).filter(Boolean).map(l => {
try { return JSON.parse(l); } catch { return null; }
}).filter(Boolean);
} else {
// Large files: read only last 10K events
const fd = fs.openSync(p, 'r');
const chunkSize = Math.min(stat.size, CHUNK_SIZE * MAX_EVENTS_TO_READ);
const buf = Buffer.alloc(chunkSize);
fs.readSync(fd, buf, 0, chunkSize, stat.size - chunkSize);
fs.closeSync(fd);
const raw = buf.toString('utf8');
return raw.split('\n').map(l => l.trim()).filter(Boolean).map(l => {
try { return JSON.parse(l); } catch { return null; }
}).filter(Boolean);
}
} catch { return []; }
}

Expand Down