bluzi / bluzi/node-jsonstore

Incorrect Content-Length header

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

The Content-Length header is calculated incorrectly causing the express server to return a 400 Bad Request response which crashes the node-jsonstore when it tries to JSON.parse that response. For me I've been able to use the node-jsonstore post method sometimes, but it seams when I try to save some text using editor.js it will not save to the jsonstore database. My guess is that some characters included in that output take more than 1 byte to store and therefore the string.length value given the the Content-Length header is no longer correct and makes express return a 400. A fix for this would be to replace `stringifiedData.length` with `Buffer.byteLength(stringifiedData)` [(Buffer.byteLength)](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_bytelength_string_encoding). This would need to be done on both the post and put methods.
Here is a StackOverflow answer that suggest using Buffer.byteLength instead:
[What is the correct method for calculating the Content-length header in node.js](https://stackoverflow.com/questions/17922748/what-is-the-correct-method-for-calculating-the-content-length-header-in-node-js)
Here is the line of code where the change needs to happen. The change aswell needs to be done on line 84 [index.js (line 53)](https://github.com/bluzi/node-jsonstore/blob/master/index.js#L53).

I tried copying the index.js file over to my project and importing that instead of 'node-jsonstore'. In that file I commented out the lines that set these headers and my code started working and the stuff saved correctly. I do though think it's better to set the header to a correct value instead of not setting it at all, so using Buffer.byteLength would be better.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.