使用 ReadableStream 作为请求体获取

Fetch with ReadableStream as Request Body

我正在尝试使用 fetch with a ReadableStream。在此示例中,ReadableStream 应该无限期地重复 "Some data..."。

fetch('/', {
  method: 'POST', 
  body: new ReadableStream({
    pull: function(controller) {
      console.log('pull called!');
      controller.enqueue('Some data...');
    }
  })
});

这行不通。虽然pull执行了一次,但请求体中没有数据发送。

POST / HTTP/1.1
Host: example.com
Connection: keep-alive
Content-Length: 0
Origin: https://example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Accept: */*
Referer: https://example.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

如何使 ReadableStream(或我可以写入动态数据的任何类型的流)可与 fetch 一起使用?

或者,如果这还不可能,请指出这一点好吗?谢谢。

注意:这是一个更具体的衍生问题:

我们正在努力使这项工作发挥作用,请参阅 https://github.com/whatwg/fetch/pull/425 获取 Fetch 标准的 PR。完成后,您可以期待它(慢慢地)进入浏览器。