web-streams-shim 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -16
- package/ReadableStreamDefaultReader-constructor.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
## Web Streams Shim Library
|
|
2
2
|
|
|
3
|
-
This README provides
|
|
3
|
+
This README provides an overview of the features in this Web Streams Shim Library, which is designed to create parity between the streaming interfaces within modern runtimes.
|
|
4
4
|
|
|
5
5
|
This library provides essential polyfills and shims to ensure modern Web Streams functionality is available and compliant across environments where native support is missing or incomplete, particularly focusing on `ReadableStream`, `Request`, `Response`, and `Blob` objects.
|
|
6
6
|
|
|
@@ -10,26 +10,27 @@ This library provides essential polyfills and shims to ensure modern Web Streams
|
|
|
10
10
|
|
|
11
11
|
The library focuses on extending core browser APIs to meet the latest Web Stream and Fetch specifications.
|
|
12
12
|
|
|
13
|
-
### 1. ReadableStream Async Iteration
|
|
13
|
+
### 1. ReadableStream Async Iteration
|
|
14
14
|
|
|
15
|
-
The library adds **comprehensive support for modern JavaScript iteration
|
|
15
|
+
The library adds **comprehensive support for modern JavaScript iteration patterns** to `ReadableStream` and its readers.
|
|
16
16
|
|
|
17
17
|
| Target | Method/Property | Description |
|
|
18
18
|
| :--- | :--- | :--- |
|
|
19
|
-
| `ReadableStream.prototype` | `[Symbol.asyncIterator]` | Allows the stream to be directly iterable in `for-await-of` loops.
|
|
20
|
-
| `ReadableStream.prototype` | `values()` | An alias for `[Symbol.asyncIterator]` for explicit iteration. |
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
| `ReadableStreamDefaultReader.prototype` | `[Symbol.asyncDispose]` | **Supports the async disposal pattern (`await using`)**. It safely cleans up resources by calling the internal `terminate` function. |
|
|
19
|
+
| [`ReadableStream.prototype`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) | `[Symbol.asyncIterator]` | Allows the stream to be directly iterable in `for-await-of` loops. |
|
|
20
|
+
| [`ReadableStream.prototype`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) | `values()` | An alias for `[Symbol.asyncIterator]` for explicit iteration. |
|
|
21
|
+
|
|
22
|
+

|
|
23
|
+

|
|
25
24
|
|
|
26
25
|
### 2. Stream Construction Utility
|
|
27
26
|
|
|
28
27
|
The library adds the static method for creating streams from existing data sources.
|
|
29
28
|
|
|
30
|
-
| Target | Method | Description |
|
|
29
|
+
| Target | Method/Property | Description |
|
|
31
30
|
| :--- | :--- | :--- |
|
|
32
|
-
| `ReadableStream` | `from(obj)` | **Creates a new `ReadableStream` from any iterable or async iterable object**. It handles both synchronous and asynchronous iterators, including objects that yield `Promise`-like values. |
|
|
31
|
+
| [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) | [`from(obj)`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/from_static) | **Creates a new `ReadableStream` from any iterable or async iterable object**. It handles both synchronous and asynchronous iterators, including objects that yield `Promise`-like values. |
|
|
32
|
+
|
|
33
|
+

|
|
33
34
|
|
|
34
35
|
### 3. Fetch and Body Integration Shims
|
|
35
36
|
|
|
@@ -37,14 +38,32 @@ These shims ensure `Request` and `Response` objects (Records) consistently expos
|
|
|
37
38
|
|
|
38
39
|
| Target | Method/Property | Description |
|
|
39
40
|
| :--- | :--- | :--- |
|
|
40
|
-
| `Request.prototype` | `body` (Getter) | Polyfills the `body` property to return a **`ReadableStream` representation of the body content**. This is crucial for environments where `fetch` exists but streaming is absent. |
|
|
41
|
-
| `Response.prototype` | `body` (Getter) | Provides the body content as a `ReadableStream`. The implementation clones the original record, converts the body to a `Blob`, gets the blob's stream, and enqueues chunks via a controller. |
|
|
42
|
-
| `Request.prototype
|
|
41
|
+
| `Request.prototype` | [`body`](https://developer.mozilla.org/en-US/docs/Web/API/Request/body) (Getter) | Polyfills the `body` property to return a **`ReadableStream` representation of the body content**. This is crucial for environments where `fetch` exists but streaming is absent. |
|
|
42
|
+
| `Response.prototype` | [`body`](https://developer.mozilla.org/en-US/docs/Web/API/Response/body) (Getter) | Provides the body content as a `ReadableStream`. The implementation clones the original record, converts the body to a `Blob`, gets the blob's stream, and enqueues chunks via a controller. |
|
|
43
|
+
| [`Request.prototype`](https://developer.mozilla.org/en-US/docs/Web/API/Request/bytes), [`Response.prototype`](https://developer.mozilla.org/en-US/docs/Web/API/Response/bytes), [`Blob.prototype`](https://developer.mozilla.org/en-US/docs/Web/API/Blob/bytes) | `bytes()` | Adds the `bytes()` method, which **asynchronously returns the object's body/content as a `Uint8Array`**. It achieves this by calling the native `arrayBuffer()` and wrapping the result. |
|
|
44
|
+
|
|
45
|
+

|
|
46
|
+

|
|
47
|
+

|
|
48
|
+

|
|
49
|
+

|
|
43
50
|
|
|
44
51
|
### 4. Duplex Compliance Shim
|
|
45
52
|
|
|
46
53
|
To satisfy modern `fetch` specifications when streaming request bodies, the library ensures compliance for **half-duplex operations**.
|
|
47
54
|
|
|
48
55
|
* **Property Injection:** The `duplex: 'half'` property is added to the prototypes of `Request`, `Response`, `ReadableStream`, and `Blob`.
|
|
49
|
-
* **Constructor Wrapping:** The global `Request` and `Response` constructors are subclassed and **wrapped** to automatically apply
|
|
50
|
-
* **Fetch Wrapping:** The global `fetch` function is **wrapped** to automatically apply
|
|
56
|
+
* **Constructor Wrapping:** The global `Request` and `Response` constructors are subclassed and **wrapped** to automatically apply `duplex: 'half'` utility function to all arguments passed during instantiation.
|
|
57
|
+
* **Fetch Wrapping:** The global `fetch` function is **wrapped** to automatically apply `duplex: 'half'` to its arguments before execution, guaranteeing compliance when streams are used in options.
|
|
58
|
+
|
|
59
|
+

|
|
60
|
+
|
|
61
|
+
### 5. ReadableStreamDefaultReader Constructor Support
|
|
62
|
+
|
|
63
|
+
The library adds support for the `ReadableStreamDefaultReader` constructor.
|
|
64
|
+
|
|
65
|
+
| Target | Method/Property | Description |
|
|
66
|
+
| :--- | :--- | :--- |
|
|
67
|
+
| [`ReadableStreamDefaultReader`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader) | [`constructor(stream)`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream) | **Polyfills the `ReadableStreamDefaultReader` constructor** to accept a stream directly. In environments where the native constructor doesn't support this (like Bun), it delegates to `stream.getReader()` and properly sets up the prototype chain. This allows `new ReadableStreamDefaultReader(stream)` to work consistently across all runtimes. |
|
|
68
|
+
|
|
69
|
+

|
|
@@ -103,6 +103,7 @@
|
|
|
103
103
|
return $ReadableStreamDefaultReader(stream)
|
|
104
104
|
},$ReadableStreamDefaultReader.prototype)
|
|
105
105
|
},$ReadableStreamDefaultReader));
|
|
106
|
+
globalThis.ReadableStreamDefaultReader.prototype.constructor = globalThis.ReadableStreamDefaultReader
|
|
106
107
|
}
|
|
107
108
|
|
|
108
109
|
})();
|