Microsoft Edge中来自Stream的新响应 [英] New Response from Stream in Microsoft Edge
问题描述
有人知道如何创建新的回复来自Microsoft Edge中的 ReadableStream ?
Does someone know of a way to create a new Response from a ReadableStream in Microsoft Edge?
对于Chrome,这非常简单。您可以只取一个 ReadableStream
并将其作为第一个参数传递给 Response
的构造函数。这样,您可以创建一个新的响应
,并说明来自网络响应的另一个状态代码,而不复制响应:
For Chrome, this is quite simple. You can just take a ReadableStream
and pass it in the constructor of Response
as the first argument. This way you can, for example, create a new Response
with say another status code from a network response, without copying the response:
fetch('https://www.baqend.com/')
.then(res => new Response(res.body, { status: 222 }))
.then(it => it.text())
.then(it => console.log('Text prefix ' + it.substr(0,16)))
.catch(it => console.log('error: ' + it))
虽然这在Chrome中完美运行,但Edge不支持 ReadableStream
作为 Response
构造函数的输入。我在Edge中使用它的唯一方法是首先获得文本响应(有效地复制响应并阻止流):
While this works perfectly in Chrome, Edge does not support ReadableStream
as input for the Response
constructor. The only way I get it to work in Edge is when I get the text response first (effectively copying the response and blocking the stream):
fetch('https://www.baqend.com/')
.then(it => it.text())
.then(text => new Response(text, { status: 222 }))
.then(it => it.text())
.then(it => console.log('Text prefix ' + it.substr(0,16)))
.catch(it => console.log('error: ' + it))
有没有人知道从Edge中的可读流创建新响应的方法?
PS:我是使用Microsoft Edge 42.17115.1.0(最新的开发人员预览,因为我正在测试服务工作者)
PS: I am using Microsoft Edge 42.17115.1.0 (latest developer preview, since I am testing Service Workers)
PPS:第一个代码在firefox中不起作用,因为firefox不支持获取来自 Response.body
的 RedableStream
。 Edge确实公开 Response.body
但是。
PPS: The first code does not work in firefox either because firefox does not support getting a RedableStream
from Response.body
. Edge does expose Response.body
though.
推荐答案
试试这个:
https:// www。 npmjs.com/package/web-streams-polyfill
var streams = require("web-streams-polyfill");
var readable = new streams.ReadableStream;
// Or, in ES6
import { ReadableStream } from "web-streams-polyfill";
这篇关于Microsoft Edge中来自Stream的新响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!