The Moddable SDK provides a full implementation of WHATWG Streams as widely used on the Web platform. Streams are optimized for use on embedded devices. The implementation is deeply integrated with the XS JavaScript engine to minimize its code footprint and memory use, and to optimize performance. Developers can work with streams in exactly the same way as they do on the web because the implementation conforms to the relevant Web Platform Tests at a level comparable to web browsers.
Web Streams are a powerful and convenient tool to organize complex data flows. That power requires overhead. Consequently, streams may not be the right tool where the highest possible performance or smallest footprint are a priority. If you are already familiar with streams, it can be quick to do a test to evaluate whether the implementation meets your needs.
Streams are a fundamental building block for many Web platform APIs. The Moddable SDK implements several of these Web APIs.
-
WebSocketStream – An experiemntal stream-based WebSocket client that addresses shortcominngs in the standard
WebSocket. See the WebSocket Guide for more information.
-
fetch – Streams can be used for both the Request body and Response body to conveniently support bodies larger than available memory. As an added bonus, streams allows fetch() to support compressed response bodies. See the HTTP Guide for more information.
-
Web Serial – Access serial (UART) ports using readable and writable streams. Available on devices and the desktop simulator.
Of course, you can use streams in your own projects in many ways. The streams examples can give you ideas, secifically for embedded devices. Start with the readme. Then, check out the button stream and touch stream examples.
The stream classes are implemented across several modules.
Include the core streams manifest in your project's manifest.json:
$(MODDABLE)/modules/web/streams/all/manifest.json
Then, import the required constructors in your JavaScript source code:
import {
ReadableStream,
ReadableStreamDefaultReader,
ReadableStreamBYOBReader,
ReadableStreamDefaultController,
ReadableByteStreamController,
ReadableStreamBYOBRequest,
WritableStream,
WritableStreamDefaultWriter,
WritableStreamDefaultController,
TransformStream,
TransformStreamDefaultController,
ByteLengthQueuingStrategy,
CountQueuingStrategy
} from "web/streams";
To use the DecompressionStream, TextDecoderStream, and TextEncoderStream constructors, include their manifests in your project's manifest.json:
$(MODDABLE)/modules/web/streams/decompression/manifest.json
$(MODDABLE)/modules/web/streams/text/decoder/manifest.json
$(MODDABLE)/modules/web/streams/text/encoder/manifest.json
Then, import the required constructors in your JavaScript source code:
import DecompressionStream from "web/decompressionstream";
import TextDecoderStream from "web/textdecoderstream";
import TextEncoderStream from "web/textencoderstream";
If your project uses stream constructors, mcpack automatically includes the corresponding manifests and initializes the globals.
The stream constructors are: ReadableStream, ReadableStreamDefaultReader, ReadableStreamBYOBReader, ReadableStreamDefaultController, ReadableByteStreamController, ReadableStreamBYOBRequest, WritableStream, WritableStreamDefaultWriter, WritableStreamDefaultController, TransformStream, TransformStreamDefaultController, ByteLengthQueuingStrategy, CountQueuingStrategy, DecompressionStream, TextDecoderStream, and TextEncoderStream.
- Documentation
- Standards
- Examples
- Implementation
- TypeScript Declarations