Decode a chunked binary response using the Fetch API

Is there a way to properly handle binary chunked responses when using the Fetch API? I have implemented the code below which successfully reads the chunked response from the server. However, I am encountering issues with the data being encoded/decoded in a way that sometimes causes the getFloat32 function to fail. When testing the response with curl, everything seems to work fine, suggesting that I may need to adjust something in the fetch api to treat the chunks as binary. The content-type of the response is correctly set to "application/octet-stream".

const consume = responseReader => {
    return responseReader.read().then(result => {
        if (result.done) { return; }
        const dv = new DataView(result.value.buffer, 0, result.value.buffer.length);
        dv.getFloat32(i, true);  // <-- experiencing garbled results at times
        return consume(responseReader);
    });
}

fetch('/binary').then(response => {
    return consume(response.body.getReader());
})
.catch(console.error);

To test and reproduce the issue, use the following express server. Any client-side JS code that can interact with this server should suffice.

const express = require('express');
const app = express();

app.get('/binary', function (req, res) {
  res.header("Content-Type", "application/octet-stream");
  res.header('Content-Transfer-Encoding', 'binary');
  const repro = new Uint32Array([0x417055b8, 0x4177d16f, 0x4179e9da, 0x418660e1, 0x41770476, 0x4183e05e]);
  setInterval(function () {
    res.write(Buffer.from(repro.buffer), 'binary');
  }, 2000);
});

app.listen(3000, () => console.log('Listening on port 3000!'));

When using the provided node server, you may notice that -13614102509256704 gets logged to the console instead of the expected value of ~16.48. How can one retrieve the original binary float value that was written?

Answer №1

Upon your mention of the issue at hand

The getFloat32 function requires a byte offset, as clearly documented

However, there is another aspect to consider in your work. I will address it here.

It's worth noting that Fetch Streams are not natively supported by both FF and Chrome. I have modified my code to accommodate streams for both platforms.

const express = require('express');
const app = express();

app.get('/', function (req, res) {
    // Code block omitted for brevity
});

// Other route handlers also included in the code snippet

app.use(express.static('./node_modules/fetch-readablestream/dist/'))
app.use(express.static('./node_modules/web-streams-polyfill/dist/'))
app.listen(3000, () => console.log('Listening on port 3000!'));

With these changes, the functionality now extends to FF.

https://i.sstatic.net/Oqnwn.png

as well as Chrome

https://i.sstatic.net/9Pl5W.png

To implement this, make use of https://www.npmjs.com/package/fetch-readablestream

Additionally, I incorporated the polyfill for ReadableStream in FF.

https://www.npmjs.com/package/web-streams-polyfill

If needed, native support for the same can be enabled by adjusting FF profile preferences.

https://i.sstatic.net/qS8y0.png

This information is provided here in hopes of aiding you or others in similar situations in the future.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on implementing Piwik JavaScript code within app.js on Express

I am trying to implement Piwik tracking on my Express website using JavaScript code. I placed the code in my app.js file but encountered an error. Here is the JavaScript code snippet: <script type="text/javascript"> var _paq = _paq || []; ...

Access my account on the one.com control panel using the identical login credentials

I am curious if it's possible to implement a login page on my website for customers and then seamlessly redirect them to the control panel on one.com without requiring them to re-enter their username and password? Visit the action page on one.com her ...

The art of manipulating the position and rotation of A-Frame entities

Unfortunately, my knowledge of positioning and rotating entities in 3D space is limited. Therefore, I am interested in developing a function that simplifies the process by using more intuitive parameters like: createEntity(vertical, horizontal, distance) ...

Is it better to convert fields extracted from a JSON string to Date objects or moment.js instances when using Angular and moment.js together?

When working on editing a user profile, the API call returns the following data structure: export class User { username: string; email: string; creationTime: Date; birthDate: Date; } For validating and manipulating the birthDate val ...

How big should the placeholder image be for the image area?

How can I create a loading image to replace a .gif while it loads? I need a placeholder image of about 325x325 (same size as the gif) to keep content in place. I've attempted using background: url() without success and haven't explored JS/jQuery ...

Unable to access the property 'function' of an undefined value

I've been attempting to call a function from another function within my React application. However, I am encountering an error that reads: Error in login TypeError: Cannot read property 'loadDashboard' of undefined. Despite researching simil ...

Ensuring AngularJS ui-router/app waits for $http data to avoid Flash of Unstyled Content (FOUC)

My question or situation pertains to the states defined in my AngularJS application. Here is an example of how I have them structured: $stateProvider .state('myApp', { abstract: true, template: '& ...

Revolutionary custom binding with Bootstrap popover integration

Utilizing asp.net mvc with dynamic knockout columns, I am facing an issue with one of the column headers named "Status". The desired functionality includes a bootstrap popover that displays information when a user clicks a font-icon question mark. Here is ...

What is the best way to showcase Vue data of a selected element from a v-for loop in a

Here is an example of how my json data is structured. I have multiple elements being displayed in a v-for loop, and when one is clicked, I want to show specific information from that element in a modal. [{ "id": 1, "companyNa ...

Is there a way to make the delete button remove just one item from the local storage?

Is there a way to make the delete button on each item in a list remove only that specific item without deleting the others and also remove it from local storage? I have been trying to figure this out but haven't had any success so far. <div class ...

A guide on manipulating an input field to trigger md-datepicker upon clicking

What is the best way to convert an input text into a fire md-datepicker and achieve a similar result like this? ...

Appending a JSON object to an array does not result in the object being added to the

Can anyone help me with an issue I'm facing? I have a code snippet where I am trying to push a JSON Object into an array, but the array is not updating properly. It only shows the last pushed element. var myData = {}; var id = 0; $("a").on('cli ...

Displaying an error message: Uncaught ReferenceError - Undefined reference detected

Hi there, I am having some trouble with a JSON file and I would appreciate some help. The error message I am receiving is: Uncaught ReferenceError: marketlivedata is not defined <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquer ...

``I am having trouble getting the Bootstrap carousel to start

I am having trouble getting the Bootstrap Carousel to function as expected. Despite including all the necessary code, such as initializing the carousel in jQuery.ready, the images are stacking on top of each other with navigation arrows below them instea ...

I'm curious about how I can apply @media queries given that certain phones possess higher resolution than computers

There seems to be a common recommendation to utilize @media queries for adjusting CSS based on whether the user is on mobile or not. However, I'm a bit confused because my phone has a width of 1440p while my computer has 1920p. Should I consider apply ...

React causing issues when displaying PNG images on browser

Running into an issue with my React app where I am unable to render a PNG file from the "src" folder. The error message pops up on Google Chrome browser, showcasing the problem: https://i.stack.imgur.com/y8dJf.png Unfortunately, my project doesn't ha ...

Need to create a callback within a sequence of events?

Is it possible to create a callback chain like this? Widget.update(...).onUpdate(function(data){ console.log('updated'); }); Here is the current code snippet: var Gateway = {}; Gateway.put = function(url, data, callback) { $.ajax({ ...

Unable to retrieve data from PHP using AJAX request

My project consists of 3 interconnected files: index.php, functions.js, and compute.php In index.php, there is a div that triggers a function in functions.js called compute(), which sends an AJAX request to perform a task in compute.php The code in index ...

One-of-a-kind browser identification for users, no login required

I have an online boutique and I want to enable customers to make purchases without creating an account. To achieve this, I need a way to associate a selected list of items with a user. Initially, I considered using the IP address for this purpose but fou ...

Tips for managing and loading data into a dataGrid or table with the help of ReactJS and ReactHooks

Struggling to retrieve user input data from the form and display it in the table/Datagrid below, without success. Follow the process flow outlined below Once the user submits the form and clicks the send now button, the {handleSubmit} function is trigger ...