aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/readablestream
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/readablestream
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/readablestream')
-rw-r--r--files/zh-cn/web/api/readablestream/getreader/index.html103
-rw-r--r--files/zh-cn/web/api/readablestream/index.html114
-rw-r--r--files/zh-cn/web/api/readablestream/readablestream/index.html117
3 files changed, 334 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/readablestream/getreader/index.html b/files/zh-cn/web/api/readablestream/getreader/index.html
new file mode 100644
index 0000000000..bddbcf0bae
--- /dev/null
+++ b/files/zh-cn/web/api/readablestream/getreader/index.html
@@ -0,0 +1,103 @@
+---
+title: ReadableStream.getReader()
+slug: Web/API/ReadableStream/getReader
+tags:
+ - API
+ - getReader
+ - 参考
+ - 可读取流
+ - 方法
+ - 流
+translation_of: Web/API/ReadableStream/getReader
+---
+<div>{{APIRef("Streams")}}</div>
+
+<p class="summary">使用{{domxref("ReadableStream")}}接口的<strong><code>getReader()</code></strong> 方法创建一个<code>reader</code>,并将流锁定。只有当前<code>reader</code>将流释放后,其他<code>reader</code>才能使用。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox notranslate">var reader = readableStreamInstance.getReader(<em>{mode}</em>);</pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt>{mode}- 可选参数</dt>
+ <dd>具有 <code>mode</code>参数的对象,值为  {{domxref("DOMString")}}类型 ,用来指定要创建的阅读器的类型。其值可以是:
+ <ul>
+ <li><code>byob</code>, 结果为 {{domxref("ReadableStreamBYOBReader")}} 类型,可读取可读字节流。</li>
+ <li><code>undefined</code> (未定类型 — 默认值), 返回{{domxref("ReadableStreamDefaultReader")}} ,可以从流中返回单个块。</li>
+ </ul>
+ </dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p>{{domxref("ReadableStreamDefaultReader")}} 类型或{{domxref("ReadableStreamBYOBReader")}} 类型 实例, 取决于 <code>mode</code> 值.</p>
+
+<h3 id="错误">错误</h3>
+
+<dl>
+ <dt>RangeError——范围错误</dt>
+ <dd>提供的 mode值 既不是 <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">byob</span></font>也不是  <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">undefined</span></font></dd>
+ <dt>TypeError——类型错误</dt>
+ <dd>尝试创建阅读器的流不是 {{domxref("ReadableStream")}}类型</dd>
+</dl>
+
+<h2 id="例子">例子</h2>
+
+<p>下面是个简单的例子, 在读取<code>ReadableStream</code>前,使用 <code>getReader()</code>创建一个{{domxref("ReadableStreamDefaultReader")}} 。(查看全部代码 <a href="https://mdn.github.io/dom-examples/streams/simple-random-stream/">Simple random stream example</a> ). 按顺序读取每个块,并传递给UI, 当整个流被读取完毕后, 从递归方法中退出,并在UI的另一部分输出整个流。</p>
+
+<pre class="brush: js notranslate">function fetchStream() {
+ const reader = stream.getReader();
+ let charsReceived = 0;
+
+ // read() 返回了一个promise
+ // 当数据被接收时resolve
+ reader.read().then(function processText({ done, value }) {
+ // Result对象包含了两个属性:
+ // done - 当stream传完所有数据时则变成true
+ // value - 数据片段。当done不为true时永远为undefined
+ if (done) {
+ console.log("Stream complete");
+ para.textContent = value;
+ return;
+ }
+
+ // value for fetch streams is a Uint8Array
+ charsReceived += value.length;
+ const chunk = value;
+ let listItem = document.createElement('li');
+ listItem.textContent = 'Received ' + charsReceived + ' characters so far. Current chunk = ' + chunk;
+ list2.appendChild(listItem);
+
+ result += chunk;
+
+ // 再次调用这个函数以读取更多数据
+ return reader.read().then(processText);
+ });
+}</pre>
+
+<h2 id="说明">说明</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status(状态)</th>
+ <th scope="col">Comment(备注)</th>
+ </tr>
+ <tr>
+ <td>{{SpecName("Streams","#rs-get-reader","getReader()")}}</td>
+ <td>{{Spec2('Streams')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容">浏览器兼容</h2>
+
+<div class="hidden">
+<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
+</div>
+
+<p>{{Compat("api.ReadableStream.getReader")}}</p>
diff --git a/files/zh-cn/web/api/readablestream/index.html b/files/zh-cn/web/api/readablestream/index.html
new file mode 100644
index 0000000000..445fc134d1
--- /dev/null
+++ b/files/zh-cn/web/api/readablestream/index.html
@@ -0,0 +1,114 @@
+---
+title: ReadableStream
+slug: Web/API/ReadableStream
+tags:
+ - API
+ - Fetch
+ - ReadableStream
+ - 引用
+ - 接口
+translation_of: Web/API/ReadableStream
+---
+<p>{{APIRef("Fetch")}}</p>
+
+<p><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Streams_API">流操作API</a> 中的<code>ReadableStream</code><span> 接口呈现了一个可读取的二进制流操作。</span><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API">Fetch API</a><span> 通过 {{domxref("Response")}} 的属性 {{domxref("Body.body", "body")}} 提供了一个具体的 </span><code>ReadableStream</code><span> 对象。</span></p>
+
+<h2 id="构造函数">构造函数</h2>
+
+<dl>
+ <dt>{{domxref("ReadableStream.ReadableStream", "ReadableStream()")}}</dt>
+ <dd>创建并从给定的 Handler 返回一个可读流对象。</dd>
+</dl>
+
+<h2 id="属性">属性</h2>
+
+<dl>
+ <dt>{{domxref("ReadableStream.locked")}} {{readonlyInline}}</dt>
+ <dd>locked 返回这个可读流是否被一个<a href="https://streams.spec.whatwg.org/#locked-to-a-reader" id="ref-for-locked-to-a-reader②">读取器锁定</a>。</dd>
+</dl>
+
+<h2 id="方法">方法</h2>
+
+<dl>
+ <dt>{{domxref("ReadableStream.cancel()")}}</dt>
+ <dd>取消读取流,读取方发出一个信号,表示对这束流失去兴趣。可以传入 reason 参数表示取消原因,这个原因将传回给调用方。</dd>
+ <dt>{{domxref("ReadableStream.getIterator()")}}</dt>
+ <dd>创建一个异步的 ReadableStream 迭代器并将流锁定于其上。一旦流被锁定,其他读取器将不能读取它,直到它被释放。</dd>
+ <dt>{{domxref("ReadableStream.getReader()")}}</dt>
+ <dd>创建一个读取器并将流锁定于其上。一旦流被锁定,其他读取器将不能读取它,直到它被释放。</dd>
+</dl>
+
+<dl>
+ <dt>{{domxref("ReadableStream.pipeThrough()")}}</dt>
+ <dd>提供将当前流管道输出到一个 transform 流或 writable/readable 流对的链式方法。</dd>
+</dl>
+
+<dl>
+ <dt>{{domxref("ReadableStream.pipeTo()")}}</dt>
+ <dd>将当前 ReadableStream 管道输出到给定的 {{domxref("WritableStream")}},并返回一个 promise,输出过程成功时返回 fulfilled,在发生错误时返回 rejected。</dd>
+ <dt>{{domxref("ReadableStream.tee()")}}</dt>
+ <dd><code>tee</code> 方法(tee本意是将高尔夫球放置在球座上)<a href="https://streams.spec.whatwg.org/#tee-a-readable-stream" id="ref-for-tee-a-readable-stream②">tees</a> 了可读流,返回包含两个{{domxref("ReadableStream")}} 实例分支的数组,每个元素接收了相同的传输数据。</dd>
+ <dt>{{domxref("ReadableStream[@@asyncIterator]()")}}</dt>
+ <dd><code>getIterator</code> 方法的别名。</dd>
+</dl>
+
+<h2 id="示例">示例</h2>
+
+<p>下面的例子,创建了一个智能的 {{domxref("Response")}} 来流式化从别的资源处取得的HTML 片段。</p>
+
+<p>它演示了 {{domxref("ReadableStream")}} 与 {{domxref("Uint8Array")}} 的协同用法。</p>
+
+<pre class="brush: js notranslate">fetch("https://www.example.org/").then((response) =&gt; {
+ const reader = response.body.getReader();
+ const stream = new ReadableStream({
+ start(controller) {
+ // 下面的函数处理每个数据块
+ function push() {
+ // "done"是一个布尔型,"value"是一个Unit8Array
+ reader.read().then(({ done, value }) =&gt; {
+ // 判断是否还有可读的数据?
+ if (done) {
+ // 告诉浏览器已经结束数据发送
+ controller.close();
+ return;
+ }
+
+ // 取得数据并将它通过controller发送给浏览器
+ controller.enqueue(value);
+ push();
+ });
+ };
+
+ push();
+ }
+ });
+
+ return new Response(stream, { headers: { "Content-Type": "text/html" } });
+});</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('Streams','#rs-class','ReadableStream')}}</td>
+ <td>{{Spec2('Streams')}}</td>
+ <td>初始定义</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>{{Compat("api.ReadableStream")}}</p>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li><a href="https://whatwg-stream-visualizer.glitch.me/">WHATWG Stream Visualiser</a>, for a basic visualisation of readable, writable, and transform streams.</li>
+</ul>
diff --git a/files/zh-cn/web/api/readablestream/readablestream/index.html b/files/zh-cn/web/api/readablestream/readablestream/index.html
new file mode 100644
index 0000000000..14341ef8c2
--- /dev/null
+++ b/files/zh-cn/web/api/readablestream/readablestream/index.html
@@ -0,0 +1,117 @@
+---
+title: ReadableStream.ReadableStream()
+slug: Web/API/ReadableStream/ReadableStream
+translation_of: Web/API/ReadableStream/ReadableStream
+---
+<div>{{draft}}{{SeeCompatTable}}{{APIRef("Streams")}}</div>
+
+<p class="summary"><strong><code>ReadableStream()</code></strong> 构造器创建并返回包含处理函数的可读流实例.</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">var readableStream = new ReadableStream(<em>underlyingSource</em>[, queueingStrategy]);</pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt>underlyingSource</dt>
+ <dd>一个包含定义了构造流行为方法和属性的对象.<code>underlyingSource</code> 包括:
+ <dl>
+ <dt>start(controller)</dt>
+ <dd>这是一个当对象被构造时立刻调用的方法.此方法的内容由开发人员定义,并应着眼于访问流,并执行其他任何必需的设置流功能.如果这个过程是异步完成的,它可以返回一个promise,表明成功或失败.传递给这个方法的<code>controller</code>是一个{{domxref("ReadableStreamDefaultController")}}或{{domxref("ReadableByteStreamController")}},具体取决于<code>type</code>属性的值。开发人员可以使用此方法在设立期间控制流.</dd>
+ <dt>pull(controller) {{optional_inline}}</dt>
+ <dd>这个方法,也是由开发人员定义的,当流的内部队列不满时,会重复调用这个方法,直到队列补满。如果<code>pull()</code>返回一个promise,那么它将不会再被调用,直到promise完成;如果promise失败,该流将会出现错误.传递给此方法的<code>controller</code>参数是{{domxref("ReadableStreamDefaultController")}}或{{domxref("ReadableByteStreamController")}},具体取决于<code>type</code>属性的值。由于更多的块被获取,这个方法可以被开发人员用来控制流.</dd>
+ <dt>cancel(reason) {{optional_inline}}</dt>
+ <dd>如果应用程序表示该流将被取消(例如,调用了{{domxref("ReadableStream.cancel()")}},则将调用此方法,该方法也由开发人员定义。 该方法应该做任何必要的事情来释放对流的访问。 如果这个过程是异步的,它可以返回一个promise,表明成功或失败.原因参数包含一个{{domxref("DOMString")}},它描述了流被取消的原因.</dd>
+ <dt>type {{optional_inline}}</dt>
+ <dd>该属性控制正在处理的可读类型的流。如果它包含一个设置为<code>bytes</code>的值,则传递的控制器对象将是一个{{domxref("ReadableByteStreamController")}},能够处理BYOB(带您自己的缓冲区)/字节流。如果未包含,则传递的控制器将为{{domxref("ReadableStreamDefaultController")}}。</dd>
+ <dt>autoAllocateChunkSize {{optional_inline}}</dt>
+ <dd>对于字节流,开发人员可以使用正整数值设置<code>autoAllocateChunkSize</code> 以打开流的自动分配功能。启用此功能后,流实现将自动分配一个具有给定整数大小的{{domxref("ArrayBuffer")}},并调用底层源代码,就好像消费者正在使用BYOB阅读器一样。</dd>
+ </dl>
+ </dd>
+ <dt>queueingStrategy {{optional_inline}}</dt>
+ <dd>一个可选择定义流的排队策略的对象。这需要两个参数:
+ <dl>
+ <dt>highWaterMark</dt>
+ <dd>非负整数 - 这定义了在应用背压之前可以包含在内部队列中的块的总数。</dd>
+ <dt>size(chunk)</dt>
+ <dd>包含参数<code>chunk</code> 的方法 - 这表示每个块使用的大小(以字节为单位).</dd>
+ </dl>
+
+ <div class="note">
+ <p><strong>Note</strong>: You could define your own custom <code>queueingStrategy</code>, or use an instance of {{domxref("ByteLengthQueueingStrategy")}} or {{domxref("CountQueueingStrategy")}} for this object value. If no <code>queueingStrategy</code> is supplied, the default used is the same as a <code>CountQueuingStrategy</code> with a high water mark of 1.</p>
+ </div>
+ </dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p>{{domxref("ReadableStream")}}对象的实例.</p>
+
+<h3 id="错误">错误</h3>
+
+<dl>
+ <dt>RangeError</dt>
+ <dd>提供的值既不是<code>bytes</code>也不是<code>undefined</code>.</dd>
+</dl>
+
+<h2 id="例子">例子</h2>
+
+<p>In the following simple example, a custom <code>ReadableStream</code> is created using a constructor (see our <a href="https://mdn.github.io/dom-examples/streams/simple-random-stream/">Simple random stream example</a> for the full code). The <code>start()</code> function generates a random string of text every second and enqueues it into the stream. A <code>cancel()</code> fuction is also provided to stop the generation if {{domxref("ReadableStream.cancel()")}} is called for any reason.</p>
+
+<p>When a button is pressed, the generation is stopped, the stream is closed using {{domxref("ReadableStreamDefaultController.close()")}}, and another function is run, which reads the data back out of the stream.</p>
+
+<pre class="brush: js">const stream = new ReadableStream({
+ start(controller) {
+ interval = setInterval(() =&gt; {
+ let string = randomChars();
+
+ // Add the string to the stream
+ controller.enqueue(string);
+
+ // show it on the screen
+ let listItem = document.createElement('li');
+ listItem.textContent = string;
+ list1.appendChild(listItem);
+ }, 1000);
+
+ button.addEventListener('click', function() {
+ clearInterval(interval);
+ fetchStream();
+ controller.close();
+ })
+ },
+ pull(controller) {
+ // We don't really need a pull in this example
+ },
+ cancel() {
+ // This is called if the reader cancels,
+ // so we should stop generating strings
+ clearInterval(interval);
+ }
+});</pre>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName("Streams","#rs-constructor","ReadableStream()")}}</td>
+ <td>{{Spec2('Streams')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<div class="hidden">
+<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
+</div>
+
+<p>{{Compat("api.ReadableStream.ReadableStream")}}</p>