-
Notifications
You must be signed in to change notification settings - Fork 20
fix: more accurate streaming statuses #3593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
astandrik
wants to merge
3
commits into
main
Choose a base branch
from
astandrik.3592
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import {ReadableStream as WebReadableStream} from 'stream/web'; | ||
| import {TextDecoder as NodeTextDecoder, TextEncoder as NodeTextEncoder} from 'util'; | ||
|
|
||
| import type {MultipartPart} from '@mjackson/multipart-parser'; | ||
|
|
||
| import {readPartText} from '../streamingPartReader'; | ||
|
|
||
| // jsdom does not provide Web Streams / Encoding APIs; polyfill for this test file. | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| if (typeof globalThis.ReadableStream === 'undefined') { | ||
| (globalThis as any).ReadableStream = WebReadableStream; | ||
| } | ||
| if (typeof globalThis.TextEncoder === 'undefined') { | ||
| (globalThis as any).TextEncoder = NodeTextEncoder; | ||
| } | ||
| if (typeof globalThis.TextDecoder === 'undefined') { | ||
| (globalThis as any).TextDecoder = NodeTextDecoder; | ||
| } | ||
| /* eslint-enable @typescript-eslint/no-explicit-any */ | ||
|
|
||
| function createFakePart(bodyChunks: Uint8Array[], contentLength: number | null): MultipartPart { | ||
| const body = new ReadableStream<Uint8Array>({ | ||
| start(controller) { | ||
| for (const chunk of bodyChunks) { | ||
| controller.enqueue(chunk); | ||
| } | ||
| controller.close(); | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| headers: {contentLength}, | ||
| body, | ||
| text: () => { | ||
| const reader = body.getReader(); | ||
| const chunks: Uint8Array[] = []; | ||
| const pump = (): Promise<string> => | ||
| reader.read().then(({done, value}) => { | ||
| if (done) { | ||
| const total = chunks.reduce((s, c) => s + c.byteLength, 0); | ||
| const merged = new Uint8Array(total); | ||
| let off = 0; | ||
| for (const c of chunks) { | ||
| merged.set(c, off); | ||
| off += c.byteLength; | ||
| } | ||
| return new TextDecoder().decode(merged); | ||
| } | ||
| chunks.push(value); | ||
| return pump(); | ||
| }); | ||
| return pump(); | ||
| }, | ||
| } as unknown as MultipartPart; | ||
| } | ||
|
|
||
| function toBytes(str: string): Uint8Array { | ||
| return Buffer.from(str); | ||
| } | ||
|
|
||
| function splitBytes(data: Uint8Array, ...splitPoints: number[]): Uint8Array[] { | ||
| const chunks: Uint8Array[] = []; | ||
| let prev = 0; | ||
| for (const point of splitPoints) { | ||
| chunks.push(data.subarray(prev, point)); | ||
| prev = point; | ||
| } | ||
| chunks.push(data.subarray(prev)); | ||
| return chunks; | ||
| } | ||
|
|
||
| describe('readPartText', () => { | ||
| test('reads body delivered as a single chunk', async () => { | ||
| const json = '{"event":"SessionCreated"}'; | ||
| const bytes = toBytes(json); | ||
| const part = createFakePart([bytes], bytes.byteLength); | ||
|
|
||
| const result = await readPartText(part); | ||
| expect(result).toBe(json); | ||
| }); | ||
|
|
||
| test('accumulates body split across multiple small chunks', async () => { | ||
| const json = '{"meta":{"event":"SessionCreated","node_id":1,"query_id":"q1"}}'; | ||
| const bytes = toBytes(json); | ||
| const chunks = splitBytes(bytes, 5, 20, 40); | ||
|
|
||
| expect(chunks.length).toBe(4); | ||
| expect(chunks.reduce((sum, c) => sum + c.byteLength, 0)).toBe(bytes.byteLength); | ||
|
|
||
| const part = createFakePart(chunks, bytes.byteLength); | ||
| const result = await readPartText(part); | ||
| expect(result).toBe(json); | ||
| }); | ||
|
|
||
| test('accumulates body delivered one byte at a time', async () => { | ||
| const json = '{"x":1}'; | ||
| const bytes = toBytes(json); | ||
| const chunks = Array.from(bytes).map((b) => new Uint8Array([b])); | ||
|
|
||
| const part = createFakePart(chunks, bytes.byteLength); | ||
| const result = await readPartText(part); | ||
| expect(result).toBe(json); | ||
| }); | ||
|
|
||
| test('falls back to part.text() when Content-Length is absent', async () => { | ||
| const json = '{"fallback":true}'; | ||
| const bytes = toBytes(json); | ||
| const part = createFakePart([bytes], null); | ||
|
|
||
| const result = await readPartText(part); | ||
| expect(result).toBe(json); | ||
| }); | ||
|
|
||
| test('clamps chunk that exceeds remaining buffer capacity', async () => { | ||
| const json = '{"ok":true}'; | ||
| const bytes = toBytes(json); | ||
| const oversized = new Uint8Array(bytes.byteLength + 20); | ||
| oversized.set(bytes, 0); | ||
|
|
||
| const part = createFakePart([oversized], bytes.byteLength); | ||
| const result = await readPartText(part); | ||
| expect(result).toBe(json); | ||
| }); | ||
|
|
||
| test('handles async delivery with delays between chunks', async () => { | ||
| const json = '{"delayed":"chunks"}'; | ||
| const bytes = toBytes(json); | ||
| const mid = Math.floor(bytes.byteLength / 2); | ||
|
|
||
| const body = new ReadableStream<Uint8Array>({ | ||
| async start(controller) { | ||
| controller.enqueue(bytes.subarray(0, mid)); | ||
| await new Promise((r) => setTimeout(r, 50)); | ||
| controller.enqueue(bytes.subarray(mid)); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
|
|
||
| const part = { | ||
| headers: {contentLength: bytes.byteLength}, | ||
| body, | ||
| text: () => Promise.resolve(json), | ||
| } as unknown as MultipartPart; | ||
|
|
||
| const result = await readPartText(part); | ||
| expect(result).toBe(json); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type {MultipartPart} from '@mjackson/multipart-parser'; | ||
|
|
||
| export async function readPartText(part: MultipartPart): Promise<string> { | ||
| const contentLength = part.headers.contentLength; | ||
| if (contentLength === null || contentLength <= 0) { | ||
| return part.text(); | ||
| } | ||
|
|
||
| const reader = part.body.getReader(); | ||
| try { | ||
| const buffer = new Uint8Array(contentLength); | ||
| let offset = 0; | ||
|
|
||
| while (offset < contentLength) { | ||
| const {done, value} = await reader.read(); | ||
| if (done) { | ||
| break; | ||
| } | ||
| const remaining = contentLength - offset; | ||
| const slice = value.byteLength <= remaining ? value : value.subarray(0, remaining); | ||
| buffer.set(slice, offset); | ||
| offset += slice.byteLength; | ||
| } | ||
|
|
||
| return new TextDecoder().decode(buffer.subarray(0, offset)); | ||
astandrik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } finally { | ||
| reader.releaseLock(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.