|
| 1 | +/** |
| 2 | + * This file contains a partial and minimal re-declaration of the |
| 3 | + * DOM ReadableStream types (copied as-is), to be able to disambiguate them from |
| 4 | + * the ones in the Node types, which are also declared globally, |
| 5 | + * and can thus cause issues (interface override/merging) when |
| 6 | + * a TS codebase relies on both (even indirectly, e.g. if @types/node is |
| 7 | + * brought in by a dependency via triple-slash /// directive). |
| 8 | + * |
| 9 | + * Hopefully TS ships some native way of uniquely referencing the DOM |
| 10 | + * interface, see: https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/2120 . |
| 11 | + */ |
| 12 | + |
| 13 | +// it'd be good to set <reference no-default-lib="true"/> (via /// directive) here to ensure the type definitions here |
| 14 | +// do not implicitly reference others, but it currently disables including libs anywhere |
| 15 | +// (see https://github.com/microsoft/TypeScript/issues/59067) |
| 16 | +/// <reference lib="dom" /> |
| 17 | +/// <reference lib="es2018.asynciterable" /> |
| 18 | + |
| 19 | +export namespace DOM { |
| 20 | + /*! ***************************************************************************** |
| 21 | + Copyright (c) Microsoft Corporation. All rights reserved. |
| 22 | + Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 23 | + this file except in compliance with the License. You may obtain a copy of the |
| 24 | + License at http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | +
|
| 26 | + THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 27 | + KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED |
| 28 | + WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 29 | + MERCHANTABLITY OR NON-INFRINGEMENT. |
| 30 | +
|
| 31 | + See the Apache Version 2.0 License for specific language governing permissions |
| 32 | + and limitations under the License. |
| 33 | + ***************************************************************************** */ |
| 34 | + |
| 35 | + interface WritableStream<W = any> { |
| 36 | + /** |
| 37 | + * The **`locked`** read-only property of the WritableStream interface returns a boolean indicating whether the `WritableStream` is locked to a writer. |
| 38 | + * |
| 39 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/locked) |
| 40 | + */ |
| 41 | + readonly locked: boolean; |
| 42 | + /** |
| 43 | + * The **`abort()`** method of the WritableStream interface aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded. |
| 44 | + * |
| 45 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/abort) |
| 46 | + */ |
| 47 | + abort(reason?: any): Promise<void>; |
| 48 | + /** |
| 49 | + * The **`close()`** method of the WritableStream interface closes the associated stream. |
| 50 | + * |
| 51 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/close) |
| 52 | + */ |
| 53 | + close(): Promise<void>; |
| 54 | + /** |
| 55 | + * The **`getWriter()`** method of the WritableStream interface returns a new instance of WritableStreamDefaultWriter and locks the stream to that instance. |
| 56 | + * |
| 57 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/getWriter) |
| 58 | + */ |
| 59 | + getWriter(): WritableStreamDefaultWriter<W>; |
| 60 | + } |
| 61 | + /** |
| 62 | + * The `ReadableStream` interface of the Streams API represents a readable stream of byte data. |
| 63 | + * |
| 64 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream) |
| 65 | + */ |
| 66 | + export interface ReadableStream<R = any> { |
| 67 | + /** |
| 68 | + * The **`locked`** read-only property of the ReadableStream interface returns whether or not the readable stream is locked to a reader. |
| 69 | + * |
| 70 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/locked) |
| 71 | + */ |
| 72 | + readonly locked: boolean; |
| 73 | + /** |
| 74 | + * The **`cancel()`** method of the ReadableStream interface returns a Promise that resolves when the stream is canceled. |
| 75 | + * |
| 76 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/cancel) |
| 77 | + */ |
| 78 | + cancel(reason?: any): Promise<void>; |
| 79 | + /** |
| 80 | + * The **`getReader()`** method of the ReadableStream interface creates a reader and locks the stream to it. |
| 81 | + * |
| 82 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/getReader) |
| 83 | + */ |
| 84 | + getReader(options: { mode: "byob" }): ReadableStreamBYOBReader; |
| 85 | + getReader(): ReadableStreamDefaultReader<R>; |
| 86 | + getReader(options?: globalThis.ReadableStreamGetReaderOptions): globalThis.ReadableStreamReader<R>; |
| 87 | + /** |
| 88 | + * The **`pipeThrough()`** method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair. |
| 89 | + * |
| 90 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/pipeThrough) |
| 91 | + */ |
| 92 | + pipeThrough<T>( |
| 93 | + transform: ReadableWritablePair<T, R>, options?: globalThis.StreamPipeOptions |
| 94 | + ): ReadableStream<T>; |
| 95 | + /** |
| 96 | + * The **`pipeTo()`** method of the ReadableStream interface pipes the current `ReadableStream` to a given WritableStream and returns a Promise that fulfills when the piping process completes successfully, or rejects if any errors were encountered. |
| 97 | + * |
| 98 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/pipeTo) |
| 99 | + */ |
| 100 | + pipeTo(destination: WritableStream<R>, options?: globalThis.StreamPipeOptions): Promise<void>; |
| 101 | + /** |
| 102 | + * The **`tee()`** method of the two-element array containing the two resulting branches as new ReadableStream instances. |
| 103 | + * |
| 104 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/tee) |
| 105 | + */ |
| 106 | + tee(): [ReadableStream<R>, ReadableStream<R>]; |
| 107 | + |
| 108 | + // The AsyncIterator methods are declared as optional `?` here for informational purposes, |
| 109 | + // since the default DOM ReadableStream does not implement the AsyncIterator interface: |
| 110 | + // instead, the `dom.asyncinterable` lib needs to be explicitly included. |
| 111 | + // This is because the AsyncIterator browser support is still not widespread enough (missing Safari support). |
| 112 | + // To use the asyncIterable interface when available, users can manually cast a DOM.ReadableStream to |
| 113 | + // ReadableStream or PonyfilledReadableStream . |
| 114 | + [Symbol.asyncIterator]?(options?: globalThis.ReadableStreamIteratorOptions): ReadableStreamAsyncIterator<R>; |
| 115 | + values?(options?: globalThis.ReadableStreamIteratorOptions): ReadableStreamAsyncIterator<R>; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * The `ReadableStreamBYOBReader` interface of the Streams API defines a reader for a ReadableStream that supports zero-copy reading from an underlying byte source. |
| 120 | + * |
| 121 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader) |
| 122 | + */ |
| 123 | + export interface ReadableStreamBYOBReader extends globalThis.ReadableStreamGenericReader { |
| 124 | + /** |
| 125 | + * The **`read()`** method of the ReadableStreamBYOBReader interface is used to read data into a view on a user-supplied buffer from an associated readable byte stream. |
| 126 | + * |
| 127 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader/read) |
| 128 | + */ |
| 129 | + read<T extends ArrayBufferView>(view: T): Promise<globalThis.ReadableStreamReadResult<T>>; |
| 130 | + /** |
| 131 | + * The **`releaseLock()`** method of the ReadableStreamBYOBReader interface releases the reader's lock on the stream. |
| 132 | + * |
| 133 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader/releaseLock) |
| 134 | + */ |
| 135 | + releaseLock(): void; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * The **`ReadableStreamDefaultReader`** interface of the Streams API represents a default reader that can be used to read stream data supplied from a network (such as a fetch request). |
| 140 | + * |
| 141 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader) |
| 142 | + */ |
| 143 | + export interface ReadableStreamDefaultReader<R = any> extends globalThis.ReadableStreamGenericReader { |
| 144 | + /** |
| 145 | + * The **`read()`** method of the ReadableStreamDefaultReader interface returns a Promise providing access to the next chunk in the stream's internal queue. |
| 146 | + * |
| 147 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader/read) |
| 148 | + */ |
| 149 | + read(): Promise<globalThis.ReadableStreamReadResult<R>>; |
| 150 | + /** |
| 151 | + * The **`releaseLock()`** method of the ReadableStreamDefaultReader interface releases the reader's lock on the stream. |
| 152 | + * |
| 153 | + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader/releaseLock) |
| 154 | + */ |
| 155 | + releaseLock(): void; |
| 156 | + } |
| 157 | + |
| 158 | + interface ReadableWritablePair<R = any, W = any> { |
| 159 | + readable: ReadableStream<R>; |
| 160 | + /** |
| 161 | + * Provides a convenient, chainable way of piping this readable stream through a transform stream (or any other { writable, readable } pair). It simply pipes the stream into the writable side of the supplied pair, and returns the readable side for further use. |
| 162 | + * |
| 163 | + * Piping a stream will lock it for the duration of the pipe, preventing any other consumer from acquiring a reader. |
| 164 | + */ |
| 165 | + writable: WritableStream<W>; |
| 166 | + } |
| 167 | + |
| 168 | + interface ReadableStreamAsyncIterator<R> extends AsyncIterableIterator<R> { |
| 169 | + next(): Promise<IteratorResult<R, undefined>>; |
| 170 | + return?(value?: any): Promise<IteratorResult<R>>; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +export type DomReadableStream<R> = DOM.ReadableStream<R>; |
| 175 | + |
| 176 | + |
0 commit comments