node-web-streams-adapter
Use the new Web Streams API built on top of Node Streams
Installation
npm install node-web-streams-adapter
Usage
Automatically assigns ReadableStream
and WritableStream
to global
, window
or self
.
var readable ={// initialize the source we're reading fromthismyDataSource =}async {// read (next) chunk from the source and add it to thevar chunk = await thismyDataSourcecontroller}{// safely close our source if the stream gets cancelledthismyDataSource}highWaterMark: 1024var writeable =// TODO, work in progressreadable
To learn more about the upcoming Web Streams API standard, read the spec.
Caveats
Node's Readable
streams pushes (to the stream) and dispatches (to consumer) data synchronously. E.g. Immediately after adding new chunk to the stream with readable.push(chunk)
, the stream sends to to consumer, empties the inner buffer and calls _read
again even if the previous call is not done yet.
Simply put:
Readable{// get the chunkvar chunk =// add it to the stream's internal bufferthis// WARNING: AT THIS POINT, STREAM HAS HANDED THE CHUNK TO CONSUMER AND CALLED THIS _read METHOD AGAIN,// BEFORE WE COULD CLOSE THE STREAM.if wasTheLastChunk// close the streamthis}
With Web Streams API (and this adapter) this code could be sligthly rewritten
{// get the chunkvar chunk =// add it to the stream's internal buffercontroller// Native ReadableStream's asynchrony let's us finish all we do in this pull() before calling the next one.// So we can safely close the stream. However, since this adapter is based on node. The same problem occurs.if wasTheLastChunk// close the streamcontroller}
but because this adapter is based on Node's Readable
, the same problem occurs. To avoid any problems, the method should be rewritten to close the stream as the first thing.
{// Always ask if the resource has no more data to read from and end this stream if so.if wasTheLastChunk// close the streamcontroller// Only then we can read the next available chunkvar chunk =// add it to the stream's internal buffercontroller}
Work in progress
So far only the ReadableStream
's basic implementation's done.