Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Response from Stream in Microsoft Edge

Does someone know of a way to create a new Response from a ReadableStream in Microsoft Edge?

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)) 

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)) 

Does anyone know a way to create a new Response from a Readable Stream in Edge?

PS: I am using Microsoft Edge 42.17115.1.0 (latest developer preview, since I am testing Service Workers)

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.

like image 694
Erik Avatar asked Mar 13 '18 08:03

Erik


People also ask

How do I stream with Microsoft Edge?

Launch Microsoft Edge and head to a site like YouTube for video, Facebook for photos, or Pandora for music. Once you have the video, audio, or pictures set up, click the More ellipsis button (…) in the upper-right corner of the browser. Then select Cast media to device from the menu.

What is a response stream?

Response streams have been available in all modern browsers for a while now. They allow you to access parts of a response as they arrive from the server: const response = await fetch(url); const reader = response.

Does Microsoft Edge support fetch?

Starting in EdgeHTML 14, which ships with Windows 10 Anniversary Update, Microsoft Edge supports the Fetch API.

How do I view HTTP headers in edge?

Display HTTP headersClick the URL of the request, under the Name column of the Requests table. Click the Headers tab.


1 Answers

Try this:

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"; 
like image 116
Eugene Mihaylin Avatar answered Sep 28 '22 18:09

Eugene Mihaylin