-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTTP server fails if body is a String object #1269
Comments
I'm not sure this is a bug. The API accepts a string or an object. If it is an object, it needs to be a buffer/view. What would you propose to change here? |
The docs state "If the body is a String or ArrayBuffer, it is the complete response." I am literally returning a |
NB: in light of #1271 maybe the best is to either not support strings or to consistently perform a TextEncoder transformation right away. |
I understand it is subtle, but there is a difference between a string and a boxed string (string object). When they behave the same, it is generally because the implementation cals toString() on the argument. That's not an option here because an object is interpreted as a buffer. I don't see a strong need for a code change. The caller can easily pass a string instead of a string object. The ECMA-419 socket and network protocol implementations (e.g. http) don't accept strings, only buffers. That avoids the problem. |
I guess it comes down to overall developer experience. If the docs had been accurate it would have saved me a bunch of time and aggravation. If the code had lived up to the docs, ditto. I know the difference between String and string but it's not something the vast majority of JS APIs slap me in the face with. |
I think this might be a simple solution. Around line 678, change: this.body = response.body; ...to... this.body = (response.body instanceof String) ? response.body.valueOf() : response.body; |
This API accepts a string or an object. In that case, the distinction matters. I'm sorry you don't like it, but it is what it is here. |
I can confirm that the code change fixes the issue for me. |
Thanks. I'll commit that. |
Closing (fixed). |
Moddable SDK version: 4.3
Target device: esp32
Description
When a "prepareResponse" callback of HTTP server returns a body consisting of a String object (as opposed to a string) the server throws an exception causing the request to be aborted. The reason for this is a check against
typeof this.body
:https://github.com/Moddable-OpenSource/moddable/blob/public/modules/network/http/http.js#L687
Steps to Reproduce
{status: 200, body: new String('foo')}
Note that this is not the only instance of this erroneous check in this module...
The text was updated successfully, but these errors were encountered: