()
| 472 | } |
| 473 | |
| 474 | function readAllEvents() { |
| 475 | try { |
| 476 | const p = eventsPath(); |
| 477 | if (!fs.existsSync(p)) return []; |
| 478 | const stat = fs.statSync(p); |
| 479 | const fullReadCap = _eventsFullReadMaxBytes(); |
| 480 | if (stat.size <= fullReadCap) { |
| 481 | const raw = fs.readFileSync(p, 'utf8'); |
| 482 | return raw.split('\n').map(l => l.trim()).filter(Boolean).map(l => { |
| 483 | try { return JSON.parse(l); } catch { return null; } |
| 484 | }).filter(Boolean); |
| 485 | } |
| 486 | // Large file: tail-read to avoid OOM. Drop the first line ONLY when the |
| 487 | // chunk does not cover the whole file (readPos > 0), because in that case |
| 488 | // it can be cut mid-JSON. When chunkSize === stat.size the read starts at |
| 489 | // 0 and the first line is the actual start-of-file -- discarding it would |
| 490 | // silently lose a complete event. Bugbot caught this on PR #31. |
| 491 | const chunkSize = Math.min(stat.size, _eventsTailReadBytes()); |
| 492 | const readPos = stat.size - chunkSize; |
| 493 | const fd = fs.openSync(p, 'r'); |
| 494 | try { |
| 495 | const buf = Buffer.alloc(chunkSize); |
| 496 | fs.readSync(fd, buf, 0, chunkSize, readPos); |
| 497 | const lines = buf.toString('utf8').split('\n').map(l => l.trim()).filter(Boolean); |
| 498 | const intact = readPos > 0 && lines.length > 1 ? lines.slice(1) : lines; |
| 499 | return intact.map(l => { |
| 500 | try { return JSON.parse(l); } catch { return null; } |
| 501 | }).filter(Boolean); |
| 502 | } finally { |
| 503 | fs.closeSync(fd); |
| 504 | } |
| 505 | } catch (e) { |
| 506 | console.warn('[AssetStore] Failed to read events.jsonl:', e && e.message || e); |
| 507 | return []; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | function appendEventJsonl(eventObj) { |
| 512 | const dir = getGepAssetsDir(); ensureDir(dir); |
no test coverage detected