市场夺宝奇兵
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.1 KiB

  1. # @sec-ant/readable-stream
  2. [![npm version](https://img.shields.io/npm/v/@sec-ant/readable-stream?cacheSeconds=300)](https://www.npmjs.com/package/@sec-ant/readable-stream/v/latest) [![npm downloads](https://img.shields.io/npm/dm/@sec-ant/readable-stream?cacheSeconds=300)](https://www.npmjs.com/package/@sec-ant/readable-stream/v/latest) [![](https://img.shields.io/jsdelivr/npm/hm/@sec-ant/readable-stream?cacheSeconds=300&color=ff5627)](https://www.jsdelivr.com/package/npm/@sec-ant/readable-stream) [![bundlephobia minzipped](https://img.shields.io/bundlephobia/minzip/@sec-ant/readable-stream?cacheSeconds=300)](https://bundlephobia.com/package/@sec-ant/readable-stream@latest) [![npm license](https://img.shields.io/npm/l/@sec-ant/readable-stream?cacheSeconds=300)](https://www.npmjs.com/package/@sec-ant/readable-stream/v/latest)
  3. A tiny, zero-dependency yet spec-compliant asynchronous iterator polyfill/ponyfill for [`ReadableStream`](https://developer.mozilla.org/docs/Web/API/ReadableStream)s.
  4. ## Features
  5. ### Asynchronously iterate a `ReadableStream`
  6. With this package, you can consume a `ReadableStream` as an `AsyncIterable`.
  7. - spec: https://streams.spec.whatwg.org/#rs-asynciterator
  8. - tests: https://github.com/Sec-ant/readable-stream/blob/main/tests/asyncIterator.spec.ts (copied from [wpt](https://github.com/web-platform-tests/wpt/blob/309231a7f3e900d04914bc4963b016efd9989a00/streams/readable-streams/async-iterator.any.js))
  9. ### Convert an `AsyncIterable` or an `Iterable` into a `ReadableStream`
  10. With this package, you can construct a `ReadableStream` from an `AsyncIterable` or an `Iterable`.
  11. - spec: https://streams.spec.whatwg.org/#rs-from
  12. - tests: https://github.com/Sec-ant/readable-stream/blob/main/tests/fromAnyIterable.spec.ts (copied from [wpt](https://github.com/web-platform-tests/wpt/blob/309231a7f3e900d04914bc4963b016efd9989a00/streams/readable-streams/from.any.js))
  13. This package passes all the aforementioned tests.
  14. ## Install
  15. ```bash
  16. npm i @sec-ant/readable-stream
  17. ```
  18. ## Usage
  19. ### Ponyfill
  20. This package can be imported as a _ponyfill_ to avoid side effects:
  21. #### `asyncIterator`
  22. Path:
  23. ```
  24. @sec-ant/readable-stream/ponyfill/asyncIterator
  25. ```
  26. Example:
  27. ```ts
  28. import {
  29. asyncIterator,
  30. type ReadableStreamIteratorOptions,
  31. } from "@sec-ant/readable-stream/ponyfill/asyncIterator";
  32. const readableStream = (await fetch("https://www.example.org/")).body;
  33. let total = 0;
  34. for await (const chunk of asyncIterator.call(readableStream)) {
  35. total += chunk.length;
  36. }
  37. console.log(total);
  38. ```
  39. Check https://streams.spec.whatwg.org/#rs-class-definition and https://streams.spec.whatwg.org/#rs-asynciterator for further explanation on `ReadableStreamIteratorOptions`.
  40. #### `fromAnyIterable`
  41. Path:
  42. ```
  43. @sec-ant/readable-stream/ponyfill/fromAnyIterable
  44. ```
  45. Example:
  46. ```ts
  47. import { fromAnyIterable } from "@sec-ant/readable-stream/ponyfill/fromAnyIterable";
  48. const readableStream = fromAnyIterable(["a", "b"]);
  49. ```
  50. #### All-in-One
  51. Path:
  52. ```
  53. @sec-ant/readable-stream/ponyfill
  54. ```
  55. Example:
  56. ```ts
  57. import {
  58. fromAnyIterable,
  59. asyncIterator,
  60. type ReadableStreamIteratorOptions,
  61. } from "@sec-ant/readable-stream/ponyfill";
  62. ```
  63. ### Polyfill
  64. This package can be imported as a drop-in _polyfill_ with side effects.
  65. #### `ReadableStream.prototype[Symbol.asyncIterator]` and `ReadableStream.prototype.values`
  66. Path:
  67. ```
  68. @sec-ant/readable-stream/polyfill/asyncIterator
  69. ```
  70. Example:
  71. ```ts
  72. import "@sec-ant/readable-stream/polyfill/asyncIterator";
  73. const readableStream = (await fetch("https://www.example.org/")).body;
  74. let total = 0;
  75. for await (const chunk of readableStream) {
  76. total += chunk.length;
  77. }
  78. console.log(total);
  79. ```
  80. #### `ReadableStream.from`
  81. Path:
  82. ```
  83. @sec-ant/readable-stream/polyfill/fromAnyIterable
  84. ```
  85. Example:
  86. ```js
  87. import "@sec-ant/readable-stream/polyfill/fromAnyIterable";
  88. const readableStream = ReadableStream.from(["a", "b"]);
  89. ```
  90. Note that `ReadableStream.from` is not typed because [declared vars cannot be overridden](https://github.com/microsoft/TypeScript/issues/36146).
  91. #### All-in-One
  92. Path:
  93. ```
  94. @sec-ant/readable-stream/polyfill
  95. ```
  96. Example:
  97. ```ts
  98. import "@sec-ant/readable-stream/polyfill";
  99. ```
  100. ### Ponyfill + Polyfill
  101. #### `asyncIterator`
  102. Path:
  103. ```
  104. @sec-ant/readable-stream/asyncIterator
  105. ```
  106. Example:
  107. ```ts
  108. import {
  109. asyncIterator,
  110. type ReadableStreamIteratorOptions,
  111. } from "@sec-ant/readable-stream/asyncIterator";
  112. // also with side effects
  113. ```
  114. #### `fromAnyIterable`
  115. Path:
  116. ```
  117. @sec-ant/readable-stream/fromAnyIterable
  118. ```
  119. Example:
  120. ```ts
  121. import { fromAnyIterable } from "@sec-ant/readable-stream/fromAnyIterable";
  122. // also with side effects
  123. ```
  124. #### All-in-One
  125. Path:
  126. ```
  127. @sec-ant/readable-stream
  128. ```
  129. Example:
  130. ```ts
  131. import {
  132. fromAnyIterable,
  133. asyncIterator,
  134. type ReadableStreamIteratorOptions,
  135. } from "@sec-ant/readable-stream";
  136. // also with side effects
  137. ```
  138. ### Types
  139. You can also use this package to augment the `ReadableStream` type for async iteration if the runtime already supports it but the type system does not.
  140. Path:
  141. ```
  142. @sec-ant/readable-stream/async-iterator
  143. ```
  144. Example:
  145. ```ts
  146. /// <reference types="@sec-ant/readable-stream/async-iterator" />
  147. ```
  148. ## License
  149. MIT