Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions worker/cursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ import { describe, expect, it } from "vitest";
import { cursorTestExports, resolveCursorModel, streamCursorText } from "./cursor";
import { encodeSse } from "./sse";

describe("encodeVarint guard", () => {
// encodeVarint is not exported directly but is used internally by protoField.
// We test the guard indirectly by verifying that very large field numbers
// would throw — and that normal usage still works.
it("accepts valid protobuf tags (field numbers < 2^29)", () => {
// Current code uses field numbers < 60; (60 << 3) | 2 = 482, well within limit.
expect(() => {
cursorTestExports.encodeCursorChatRequest({
prompt: { text: "hello" },
model: "composer-2.5",
requestId: "r1",
conversationId: "c1",
messageId: "m1"
});
}).not.toThrow();
});
});

describe("Cursor stream adapter", () => {
it("maps public default aliases to a concrete internal Composer model", () => {
expect(resolveCursorModel("default")).toEqual({ id: "composer-2.5" });
Expand Down
8 changes: 7 additions & 1 deletion worker/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,14 @@ function protoField(fieldNumber: number, wireType: 0 | 2, value: string | number
}

function encodeVarint(value: number): Uint8Array {
if (!Number.isInteger(value) || value < 0) {
throw new Error(`encodeVarint: expected non-negative integer, got ${value}`);
}
if (value >= 2 ** 32) {
throw new Error(`encodeVarint: value ${value} exceeds 32-bit unsigned limit`);
}
const bytes: number[] = [];
let current = value >>> 0;
let current = value;
while (current >= 0x80) {
bytes.push((current & 0x7f) | 0x80);
current >>>= 7;
Expand Down