Streaming and Response Handling
Streaming is a powerful feature for handling large data sets or files, enabling your application to process data incrementally without requiring everything to be loaded into memory at once. Dvori supports streaming in both modern browsers and Node.js (v18+), allowing for efficient data handling across environments.
Downloading a File from S3
This example demonstrates how to download a large file from S3 using Dvori, leveraging streams to handle the data efficiently.
Node.js Example
This example uses Node.js’s writable streams to write the streamed data directly to a file. The pipeTo
method connects the fetch response stream to the file stream, allowing data to be written as it’s received.
Browser Example
In modern browsers, while direct file system access is limited, you can still process or display streamed data using JavaScript.
In this browser example, the streamed data is processed chunk by chunk using the ReadableStream reader.
Connecting Multiple Stream Handlers
For advanced scenarios, you might want to connect multiple handlers to a single stream, such as transforming data on the fly or piping it through multiple processing steps.
Example: Transforming Stream Data
This example demonstrates using a TransformStream
to modify data chunks as they pass through the stream, showcasing the flexibility of stream handling in Dvori.
Conclusion
Dvori’s streaming support enables efficient, real-time data processing for applications, accommodating scenarios from downloading large files to real-time data transformation. Leveraging streams can significantly enhance your application’s performance and user experience, particularly when dealing with large datasets or files.