# WritableStreamDefaultWriter

The `WritableStreamDefaultWriter` interface is part of the Streams API of Azion Runtime. A call to [`WritableStream.getWriter()`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/getWriter) returns one and locks the stream to it, so no other writer can write to the underlying destination until the lock is released. For more information, refer to [WritableStreamDefaultWriter](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter) on MDN Web Docs.

---

## Constructor

The [`WritableStreamDefaultWriter()`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/WritableStreamDefaultWriter) constructor creates a writer for the stream you pass to it:

```javascript
new WritableStreamDefaultWriter(stream)
```

The `stream` parameter is the `WritableStream` the writer writes to. Calling `stream.getWriter()` returns an object of the same type.

---

## Properties

| Property                                                                                                  | Type    | Description                                                                                                                                                             |
| --------------------------------------------------------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`closed`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/closed)           | Promise | Fulfills when the stream closes, and rejects when the stream errors or the lock of the writer is released. Use it to run code when the stream ends.                     |
| [`desiredSize`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/desiredSize) | Number  | Size still needed to fill the internal queue of the stream. A stream created with `new CountQueuingStrategy({ highWaterMark: 2 })` reports `2` before the first write.  |
| [`ready`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/ready)             | Promise | Resolves when the desired size of the internal queue changes from zero or less to a positive value. That change signals that the stream no longer applies backpressure. |

---

## Methods

| Method                                                                                                             | Description                                                                                                                                                                                                                                                                          |
| ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [`writer.abort()`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/abort)             | Aborts the stream. The producer can no longer write to it, the stream moves to an error state at once, and queued writes are discarded.                                                                                                                                              |
| [`writer.close()`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/close)             | Closes the writable stream of the writer. Returns a promise.                                                                                                                                                                                                                         |
| [`writer.releaseLock()`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/releaseLock) | Releases the lock of the writer on its stream, and the writer stops being active. After the call, `locked` on the stream is `false`. If the stream is errored when the lock is released, the writer reports the same error from then on; otherwise, it reports the stream as closed. |
| [`writer.write(chunk)`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/write)        | Writes `chunk` to the stream and its underlying sink. Returns a promise that resolves or rejects to report whether the write succeeded.                                                                                                                                              |

---

## Example

This handler creates a `WritableStream` whose sink collects every chunk, gets a writer, writes two chunks, and closes the writer. It returns the state of the stream and the writer before the writes, with the chunks the sink received:

```javascript
export default {
  async fetch(request, env, ctx) {
    const got = [];
    const stream = new WritableStream(
      {
        write(chunk) {
          got.push(chunk);
        },
      },
      new CountQueuingStrategy({ highWaterMark: 2 }),
    );
    const writer = stream.getWriter();
    const info = {
      locked: stream.locked,
      ctor: writer.constructor.name,
      desiredSize: writer.desiredSize,
    };
    await writer.ready;
    await writer.write('one');
    await writer.write('two');
    await writer.close();
    await writer.closed;
    return Response.json({ ...info, got });
  },
};
```

The function returns these values. The stream is locked to the writer, and the sink receives both chunks in order:

```json
{
 "locked": true,
 "ctor": "WritableStreamDefaultWriter",
 "desiredSize": 2,
 "got": [
  "one",
  "two"
 ]
}
```

---

## Related resources

- [WritableStream](/en/documentation/devtools/runtime/api-reference/writable-stream.md): The stream that a writer locks and writes to, and its own methods.
- [ReadableStream](/en/documentation/devtools/runtime/api-reference/readable-stream.md): How to pipe a readable stream into a writable stream with `pipeTo()`.
- [TransformStream](/en/documentation/devtools/runtime/api-reference/transform-stream.md): A pair of a writable and a readable stream that changes chunks as they pass.
- [Web APIs](/en/documentation/devtools/runtime/api-reference/javascript.md): The other Web APIs that Azion Runtime supports.
