HttpStream is a Web Streams API compatible implementation that allows you to load bytes from a remote server on demand. Under the hood it retrieves the bytes via HTTP 206 Range requests.
- Web Streams API Compatible - Implements ReadableStream interface
- Range Request Support - Uses HTTP 206 partial content requests for efficient streaming
- Configurable Chunk Size - Control memory usage with customizable chunk sizes
- Error Handling - Proper HTTP error handling and stream error propagation
- Backward Compatible - Maintains original readAsync/getLength API
new HttpStream(url: string, options?: { chunkSize?: number })getReadableStream(): ReadableStream<Uint8Array>- Returns a standard ReadableStreamgetReader(): ReadableStreamDefaultReader<Uint8Array>- Returns a stream readerread(position?, length?): Promise<{value: Uint8Array | undefined, done: boolean}>- Read chunks or specific rangespipeTo(destination: WritableStream): Promise<void>- Pipe to a WritableStreampipeThrough(transform): ReadableStream- Transform the stream
readAsync(position: number, length: number): Promise<ArrayBuffer>- Read specific byte rangegetLength(): Promise<number>- Get total file size
- Host an HTTP server on the project root directory.
- Launch your browser and navigate to index.html, then open develop tools to see what is going on.
The demo will show you how to detect pixel size of two images under the public folder without downloading them completely, plus demonstrations of the new Stream API features.
const stream = new HttpStream('/path/to/file.jpg')
const header = await stream.readAsync(0, 30) // Read first 30 bytes
const fileSize = await stream.getLength()const stream = new HttpStream('/path/to/file.jpg', { chunkSize: 8192 })
const reader = stream.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) break
// Process chunk
console.log(`Received ${value.length} bytes`)
}const httpStream = new HttpStream('/path/to/file.jpg')
const writableStream = new WritableStream({
write(chunk) {
// Process each chunk
console.log(`Processing ${chunk.length} bytes`)
}
})
await httpStream.pipeTo(writableStream)