Skip to content

Commit c3ab277

Browse files
committed
fix: bound message state reads
1 parent b92a24e commit c3ab277

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/daemon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ async function createMessage(req, res) {
15341534
return;
15351535
}
15361536

1537-
const kind = body && body.kind === undefined ? 'info' : body && body.kind;
1537+
const kind = body && typeof body === 'object' && body.kind !== undefined ? body.kind : 'info';
15381538

15391539
if (!messageKinds.has(kind)) {
15401540
sendJson(res, 400, { error: 'kind must be info, question, or results.' });

src/store.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const paths = {
2121
mcpToken: path.join(root, 'state', 'mcp-token'),
2222
};
2323

24+
const maxMessageBytes = 16 * 1024;
25+
2426
const defaults = {
2527
dashboardPort: 5757,
2628
maxConcurrent: 2,
@@ -244,7 +246,28 @@ function readMessages(limit = 50) {
244246
const max = Number.isInteger(limit) && limit > 0 ? limit : 50;
245247

246248
try {
247-
const lines = fs.readFileSync(paths.messages, 'utf8').split(/\r?\n/);
249+
const size = fs.statSync(paths.messages).size;
250+
251+
if (!size) {
252+
return [];
253+
}
254+
255+
const length = Math.min(size, maxMessageBytes);
256+
const buffer = Buffer.alloc(length);
257+
const descriptor = fs.openSync(paths.messages, 'r');
258+
let bytesRead;
259+
260+
try {
261+
bytesRead = fs.readSync(descriptor, buffer, 0, length, size - length);
262+
} finally {
263+
fs.closeSync(descriptor);
264+
}
265+
266+
const lines = buffer.subarray(0, bytesRead).toString('utf8').split(/\r?\n/);
267+
268+
if (size > length) {
269+
lines.shift();
270+
}
248271

249272
if (lines[lines.length - 1] === '') {
250273
lines.pop();

0 commit comments

Comments
 (0)