Skip to main content
Version: 2.2.0

WritableStream()

The WritableStream() constructor creates a new WritableStream object instance.

Syntax

new WritableStream(underlyingSink)
new WritableStream(underlyingSink, queuingStrategy)

Parameters

  • underlyingSink optional

    • : An object containing methods and properties that define how the constructed stream instance will behave. underlyingSink can contain the following:

      • start(controller) optional
        • : This is a method, called immediately when the object is constructed. The contents of this method are defined by the developer, and should aim to get access to the underlying sink. If this process is to be done asynchronously, it can return a promise to signal success or failure. The controller parameter passed to this method is a WritableStreamDefaultController. This can be used by the developer to control the stream during set up.
      • write(chunk, controller) optional
        • : This method, also defined by the developer, will be called when a new chunk of data (specified in the chunk parameter) is ready to be written to the underlying sink. It can return a promise to signal success or failure of the write operation. The controller parameter passed to this method is a WritableStreamDefaultController that can be used by the developer to control the stream as more chunks are submitted for writing. This method will be called only after previous writes have succeeded, and never after the stream is closed or aborted (see below).
      • close(controller) optional
        • : This method, also defined by the developer, will be called if the app signals that it has finished writing chunks to the stream. The contents should do whatever is necessary to finalize writes to the underlying sink, and release access to it. If this process is asynchronous, it can return a promise to signal success or failure. This method will be called only after all queued-up writes have succeeded. The controller parameter passed to this method is a WritableStreamDefaultController, which can be used to control the stream at the end of writing.
      • abort(reason) optional
        • : This method, also defined by the developer, will be called if the app signals that it wishes to abruptly close the stream and put it in an errored state. It can clean up any held resources, much like close(), but abort() will be called even if writes are queued up — those chunks will be thrown away. If this process is asynchronous, it can return a promise to signal success or failure. The reason parameter contains a string describing why the stream was aborted.
  • queuingStrategy optional

    • : An object that optionally defines a queuing strategy for the stream. This takes two parameters:

      • highWaterMark
        • : A non-negative integer — this defines the total number of chunks that can be contained in the internal queue before backpressure is applied.
      • size(chunk)
        • : A method containing a parameter chunk — this indicates the size to use for each chunk, in bytes.

      Note: You could define your own custom queuingStrategy, or use an instance of ByteLengthQueuingStrategy or CountQueuingStrategy for this object value. If no queuingStrategy is supplied, the default used is the same as a CountQueuingStrategy with a high water mark of 1.

Return value

An instance of the WritableStream object.