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 CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Web Streams Shim Library
2
2
 
3
- This README provides a comprehensive overview of the features and architectural strategies used in this Web Streams Shim Library, which is designed to integrate modern streams functionality into legacy JavaScript environments.
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 and Disposal
13
+ ### 1. ReadableStream Async Iteration
14
14
 
15
- The library adds **comprehensive support for modern JavaScript iteration and disposal patterns** to `ReadableStream` and its readers.
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. It reuses a reader associated with the stream, managed via a `WeakMap`. |
20
- | `ReadableStream.prototype` | `values()` | An alias for `[Symbol.asyncIterator]` for explicit iteration. |
21
- | `ReadableStreamDefaultReader.prototype` | `next()` | **Delegates directly to the reader’s native `read()` method**, fulfilling the async iterator requirement. |
22
- | `ReadableStreamDefaultReader.prototype` | `return(reason)` | Handles early termination (e.g., `break` or `return` within iteration). It safely calls the internal **`terminate` function** to cancel the stream and release the lock. |
23
- | `ReadableStreamDefaultReader.prototype` | `throw(reason)` | Handles error injection into the iteration. It calls `terminate` to cancel the stream and release the lock, and logs the error. |
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
+ ![ReadableStream.asyncIterator](https://caniuse.smokestack.workers.dev/?feature=api.ReadableStream.@@asyncIterator)
23
+ ![ReadableStream.values](https://caniuse.smokestack.workers.dev/?feature=api.ReadableStream.values)
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
+ ![Symbol.asyncIterator](https://caniuse.smokestack.workers.dev/?feature=api.ReadableStream.from)
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`, `Response.prototype`, `Blob.prototype` | `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. |
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
+ ![Request.body](https://caniuse.smokestack.workers.dev/?feature=api.Request.body)
46
+ ![Response.body](https://caniuse.smokestack.workers.dev/?feature=api.Response.body)
47
+ ![Request.bytes](https://caniuse.smokestack.workers.dev/?feature=api.Request.bytes)
48
+ ![Response.bytes](https://caniuse.smokestack.workers.dev/?feature=api.Response.bytes)
49
+ ![Blob.bytes](https://caniuse.smokestack.workers.dev/?feature=api.Blob.bytes)
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 the `duplexHalf` utility function to all arguments passed during instantiation.
50
- * **Fetch Wrapping:** The global `fetch` function is **wrapped** to automatically apply the `duplexHalf` utility function to its arguments before execution, guaranteeing compliance when streams are used in options.
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
+ ![Request.duplex](https://caniuse.smokestack.workers.dev/?feature=api.Request.duplex)
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
+ ![ReadableStreamDefaultReader.constructor](https://caniuse.smokestack.workers.dev/?feature=api.ReadableStreamDefaultReader.constructor)
@@ -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
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-streams-shim",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "web-streams-core.js",
6
6
  "scripts": {