-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement hybrid mode for BYOB and default reading
- Loading branch information
Showing
9 changed files
with
185 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,77 @@ | ||
import type { ReadableStreamDefaultReader } from 'node:stream/web'; | ||
import { EndOfStreamError } from './EndOfStreamError.js'; | ||
import { AbstractStreamReader } from "./AbstractStreamReader.js"; | ||
|
||
export class WebStreamDefaultReader extends AbstractStreamReader { | ||
private buffer: Uint8Array | null = null; // Internal buffer to store excess data | ||
private bufferOffset = 0; // Current position in the buffer | ||
|
||
public constructor(private reader: ReadableStreamDefaultReader) { | ||
super(); | ||
} | ||
|
||
protected async readFromStream(buffer: Uint8Array, offset: number, length: number): Promise<number> { | ||
if (this.endOfStream) { | ||
throw new EndOfStreamError(); | ||
} | ||
|
||
let totalBytesRead = 0; | ||
|
||
// Serve from the internal buffer first | ||
if (this.buffer) { | ||
const remainingInBuffer = this.buffer.byteLength - this.bufferOffset; | ||
const toCopy = Math.min(remainingInBuffer, length); | ||
buffer.set(this.buffer.subarray(this.bufferOffset, this.bufferOffset + toCopy), offset); | ||
this.bufferOffset += toCopy; | ||
totalBytesRead += toCopy; | ||
length -= toCopy; | ||
offset += toCopy; | ||
|
||
// If the buffer is exhausted, clear it | ||
if (this.bufferOffset >= this.buffer.byteLength) { | ||
this.buffer = null; | ||
this.bufferOffset = 0; | ||
} | ||
} | ||
|
||
// Continue reading from the stream if more data is needed | ||
while (length > 0 && !this.endOfStream) { | ||
const result = await this.reader.read(); | ||
|
||
if (result.done) { | ||
this.endOfStream = true; | ||
break; | ||
} | ||
|
||
if (result.value) { | ||
const chunk = result.value; | ||
|
||
// If the chunk is larger than the requested length, store the excess | ||
if (chunk.byteLength > length) { | ||
buffer.set(chunk.subarray(0, length), offset); | ||
this.buffer = chunk; | ||
this.bufferOffset = length; // Keep track of the unconsumed part | ||
totalBytesRead += length; | ||
return totalBytesRead; | ||
} | ||
|
||
// Otherwise, consume the entire chunk | ||
buffer.set(chunk, offset); | ||
totalBytesRead += chunk.byteLength; | ||
length -= chunk.byteLength; | ||
offset += chunk.byteLength; | ||
} | ||
} | ||
|
||
if (totalBytesRead === 0 && this.endOfStream) { | ||
throw new EndOfStreamError(); | ||
} | ||
|
||
return totalBytesRead; | ||
} | ||
|
||
public async abort(): Promise<void> { | ||
await this.reader.cancel(); | ||
this.reader.releaseLock(); | ||
} | ||
} |
This file contains 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,13 @@ | ||
import type { ReadableStream as NodeReadableStream, ReadableStreamDefaultReader } from 'node:stream/web'; | ||
import { WebStreamByobReader } from './WebStreamByobReader.js'; | ||
import { WebStreamDefaultReader } from './WebStreamDefaultReader.js'; | ||
export type AnyWebByteStream = NodeReadableStream<Uint8Array> | ReadableStream<Uint8Array>; | ||
|
||
export function makeWebStreamReader(stream: AnyWebByteStream): WebStreamByobReader | WebStreamDefaultReader { | ||
const reader = stream.getReader({mode: "byob"}); | ||
if (reader instanceof ReadableStreamBYOBReader) { | ||
return new WebStreamByobReader(reader); | ||
} | ||
// Fall back on default reader | ||
return new WebStreamDefaultReader(reader as ReadableStreamDefaultReader); | ||
} |
This file contains 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 |
---|---|---|
@@ -1,4 +1,10 @@ | ||
|
||
import { AbstractStreamReader } from './AbstractStreamReader.js'; | ||
import type { WebStreamByobReader } from './WebStreamByobReader.js'; | ||
|
||
export { EndOfStreamError } from './EndOfStreamError.js'; | ||
export { StreamReader } from './StreamReader.js'; | ||
export { WebStreamReader, type AnyWebByteStream } from './WebStreamReader.js'; | ||
export { WebStreamByobReader } from './WebStreamByobReader.js'; | ||
export { WebStreamDefaultReader } from './WebStreamDefaultReader.js'; | ||
export type { IStreamReader } from './AbstractStreamReader.js'; | ||
export { type AnyWebByteStream, makeWebStreamReader } from './WebStreamReaderFactory.js' |
This file contains 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 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 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