hjespers / node-red-contrib-nest

Node-Red module for Nest thermostats and smoke detectors
20 stars 15 forks source link

Large JSON responses can't be processed #2

Closed jfishbein closed 8 years ago

jfishbein commented 9 years ago

Using the query node when more than one of a device type exists in the nest account leads to a empty message being passed. Tested with the smoke_co_alarms request on my account with two Nest Protects registered.

The cause is the use of the .on("data") handler on the request call. Since large JSON responses will get broken into multiple data chunks, when the first chunk is passed to the JSON parser, it throws an exception (since it's only a portion of a valid JSON response) which is caught by the exception handler and turned into an empty message.

My guess is that the .on("data") handler was used instead of the requests library response callback to handle the streaming case, but a better method is needed to make sure that large non-streaming requests can be handled. Suggested fix would be to move the non-streaming requests to use the request callback, and streaming requests should attempt to parse the response as JSON, and if that fails, add the response to a buffer so the remainder of the response can arrive and be processed.

hjespers commented 8 years ago

I separated out how the streaming and non-streaming data is handled and published the new release as version 0.1.7. This should fix the problem of truncated large responses.