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
45 changes: 45 additions & 0 deletions common/src/desktop-cloud/run-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CloudRunReader,
CloudStreamHttpError,
CLOUD_STREAM_ROTATE_MIN_AGE_MS,
CLOUD_STREAM_MAX_FRAME_BYTES,
} from './run-stream'

const page = {
Expand Down Expand Up @@ -32,6 +33,50 @@ test('SSE preserves split UTF-8 and bigint cursors and ignores keepalives', asyn
items.push(item)
expect(items).toEqual([page])
})
test('SSE enforces the frame byte limit for complete and unfinished UTF-8 frames', async () => {
const frame = `data: ${JSON.stringify({
...page,
events: [
{
...page.events[0],
payload: 'é'.repeat(CLOUD_STREAM_MAX_FRAME_BYTES / 2),
},
],
})}`
expect(frame.length).toBeLessThan(CLOUD_STREAM_MAX_FRAME_BYTES)
for (const ending of ['\n\n', '']) {
const bytes = new TextEncoder().encode(frame + ending)
let cancelled = false
const body = new ReadableStream<Uint8Array>(
{
start(controller) {
const split = Math.floor(bytes.length / 2)
controller.enqueue(bytes.subarray(0, split))
controller.enqueue(bytes.subarray(split))
},
pull(controller) {
controller.close()
},
cancel() {
cancelled = true
},
},
{ highWaterMark: 0 },
)
const iterator = readCloudRunStream(
new Response(body, { headers: { 'content-type': 'text/event-stream' } }),
)
try {
await expect(
iterator.next().then((result) => (result.done ? 'ended' : 'accepted')),
).rejects.toThrow('Cloud stream frame too large')
expect(cancelled).toBe(true)
} finally {
await iterator.return(undefined)
}
}
})

test('transport failure falls back once, then waits before probing SSE again', async () => {
let opens = 0,
polls = 0
Expand Down
5 changes: 3 additions & 2 deletions common/src/desktop-cloud/run-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function* readCloudRunStream(
throw new Error('Cloud stream unavailable')
const reader = response.body.getReader()
const decoder = new TextDecoder()
const encoder = new TextEncoder()
let buffer = ''
try {
while (true) {
Expand All @@ -42,7 +43,7 @@ export async function* readCloudRunStream(
while ((boundary = buffer.indexOf('\n\n')) !== -1) {
const frame = buffer.slice(0, boundary)
buffer = buffer.slice(boundary + 2)
if (frame.length > CLOUD_STREAM_MAX_FRAME_BYTES)
if (encoder.encode(frame).byteLength > CLOUD_STREAM_MAX_FRAME_BYTES)
throw new Error('Cloud stream frame too large')
const data = frame
.split('\n')
Expand Down Expand Up @@ -70,7 +71,7 @@ export async function* readCloudRunStream(
}
yield item as CloudStreamPage
}
if (buffer.length > CLOUD_STREAM_MAX_FRAME_BYTES)
if (encoder.encode(buffer).byteLength > CLOUD_STREAM_MAX_FRAME_BYTES)
throw new Error('Cloud stream frame too large')
}
} finally {
Expand Down