这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
The Streams API allows JavaScript to programmatically access streams of data received over the network and process them as desired by the developer.
Streaming involves breaking a resource that you want to receive over a network down into small chunks, then processing it bit by bit. This is something browsers do anyway when receiving assets to be shown on webpages — videos buffer and more is gradually available to play, and sometimes you'll see images display gradually as more is loaded.
But this has never been available to JavaScript before. Previously, if we wanted to process a resource of some kind (be it a video, or a text file, etc.), we'd have to download the entire file, wait for it to be deserialized into a suitable format, then process the whole lot after it is fully received.
With Streams being available to JavaScript, this all changes — you can now start processing raw data with JavaScript bit by bit as soon as it is available on the client-side, without needing to generate a buffer, string, or blob.
There are more advantages too — you can detect when streams start or end, chain streams together, handle errors and cancel streams as required, and react to the speed of the stream is being read at.
The basic usage of Streams hinges around making responses available as streams. For example, the response
Body
returned by a successful
fetch request
can be exposed as a
ReadableStream
, and you can then read it using a reader created with
ReadableStream.getReader()
, cancel it with
ReadableStream.cancel()
,等。
More complicated uses involve creating your own stream using the
ReadableStream()
constructor, for example to process data inside a
service worker
.
You can also write data to streams using
WritableStream
.
注意 : You can find a lot more details about the theory and practice of streams in our articles — Streams API concepts , Using readable streams ,和 Using writable streams .
ReadableStream
ReadableStream()
构造函数)。
ReadableStreamDefaultReader
Represents a default reader that can be used to read stream data supplied from a network (e.g. a fetch request).
ReadableStreamDefaultController
ReadableStream
's state and internal queue. Default controllers are for streams that are not byte streams.
WritableStream
Provides a standard abstraction for writing streaming data to a destination, known as a sink. This object comes with built-in backpressure and queuing.
WritableStreamDefaultWriter
Represents a default writable stream writer that can be used to write chunks of data to a writable stream.
WritableStreamDefaultController
WritableStream
's state. When constructing a
WritableStream
, the underlying sink is given a corresponding
WritableStreamDefaultController
instance to manipulate.
ByteLengthQueuingStrategy
Provides a built-in byte length queuing strategy that can be used when constructing streams.
CountQueuingStrategy
Provides a built-in chunk counting queuing strategy that can be used when constructing streams.
Request
Request
object is constructed, you can pass it a
ReadableStream
在
body
property of its
RequestInit
dictionary. This
Request
could then be passed to a
WindowOrWorkerGlobalScope.fetch()
to commence fetching the stream.
Body
Body
returned by a successful
fetch request
is exposed by default as a
ReadableStream
, and can have a reader attached to it, etc.
重要 : these are not implemented anywhere as yet, and questions have been raised as to whether the spec details are in a finished enough state for them to be implemented. This may change over time.
ReadableStreamBYOBReader
ReadableStream()
构造函数)。
ReadableByteStreamController
ReadableStream
's state and internal queue. Byte stream controllers are for byte streams.
ReadableStreamBYOBRequest
ReadableByteStreamController
.
We have created a directory of examples to go along with the Streams API documentation — see mdn/dom-examples/streams . The examples are as follows:
pipeThrough()
can be used to transform a ReadableStream into a stream of other data types by transforming a data of a PNG file into a stream of PNG chunks.
Examples from other developers:
| 规范 | 状态 | 注释 |
|---|---|---|
| 流 | 实时标准 | 初始定义。 |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
ReadableStream
|
Chrome 43 | Edge 14 |
Firefox
65
|
IE No | Opera 30 | Safari 10.1 | WebView Android 43 | Chrome Android 43 |
Firefox Android
65
|
Opera Android 30 | Safari iOS 10.3 | Samsung Internet Android 4.0 |
ReadableStream()
构造函数
|
Chrome 43 | Edge 79 |
Firefox
65
|
IE No | Opera 30 | Safari 10.1 | WebView Android 43 | Chrome Android 43 |
Firefox Android
65
|
Opera Android 30 | Safari iOS 10.3 | Samsung Internet Android 4.0 |
cancel
|
Chrome 43 | Edge 14 |
Firefox
65
|
IE No | Opera 30 | Safari 10.1 | WebView Android 43 | Chrome Android 43 |
Firefox Android
65
|
Opera Android 30 | Safari iOS 10.3 | Samsung Internet Android 4.0 |
getReader
|
Chrome 43 | Edge 14 |
Firefox
65
|
IE No | Opera 30 | Safari 10.1 | WebView Android 43 | Chrome Android 43 |
Firefox Android
65
|
Opera Android 30 | Safari iOS 10.3 | Samsung Internet Android 4.0 |
locked
|
Chrome 43 | Edge 14 |
Firefox
65
|
IE No | Opera 30 | Safari 10.1 | WebView Android 43 | Chrome Android 43 |
Firefox Android
65
|
Opera Android 30 | Safari iOS 10.3 | Samsung Internet Android 4.0 |
pipeThrough
|
Chrome 59 | Edge 79 | Firefox No | IE No | Opera 46 | Safari 10.1 | WebView Android 59 | Chrome Android 59 | Firefox Android No | Opera Android 43 | Safari iOS 10.3 | Samsung Internet Android 7.0 |
pipeTo
|
Chrome 59 | Edge 79 | Firefox No | IE No | Opera 46 | Safari 10.1 | WebView Android 59 | Chrome Android 59 | Firefox Android No | Opera Android 43 | Safari iOS 10.3 | Samsung Internet Android 7.0 |
tee
|
Chrome 43 | Edge 79 |
Firefox
65
|
IE No | Opera 30 | Safari 10.1 | WebView Android 43 | Chrome Android 43 |
Firefox Android
65
|
Opera Android 30 | Safari iOS 10.3 | Samsung Internet Android 4.0 |
完整支持
不支持
实验。期望将来行为有所改变。
用户必须明确启用此特征。
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
WritableStream
|
Chrome 59 | Edge 16 | Firefox No | IE No | Opera 47 | Safari ? | WebView Android 59 | Chrome Android 59 | Firefox Android No | Opera Android 44 | Safari iOS ? | Samsung Internet Android 7.0 |
WritableStream()
构造函数
|
Chrome 59 | Edge 16 | Firefox No | IE No | Opera 47 | Safari ? | WebView Android 59 | Chrome Android 59 | Firefox Android No | Opera Android 44 | Safari iOS ? | Samsung Internet Android 7.0 |
abort
|
Chrome 59 | Edge 16 | Firefox No | IE No | Opera 47 | Safari ? | WebView Android 59 | Chrome Android 59 | Firefox Android No | Opera Android 44 | Safari iOS ? | Samsung Internet Android 7.0 |
getWriter
|
Chrome 59 | Edge 16 | Firefox No | IE No | Opera 47 | Safari ? | WebView Android 59 | Chrome Android 59 | Firefox Android No | Opera Android 44 | Safari iOS ? | Samsung Internet Android 7.0 |
locked
|
Chrome 59 | Edge 16 | Firefox No | IE No | Opera 47 | Safari ? | WebView Android 59 | Chrome Android 59 | Firefox Android No | Opera Android 44 | Safari iOS ? | Samsung Internet Android 7.0 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。