Skip to content
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

Cached/uncached response body inconsistency #22

Closed
maxceem opened this issue Jan 30, 2017 · 4 comments · Fixed by #32
Closed

Cached/uncached response body inconsistency #22

maxceem opened this issue Jan 30, 2017 · 4 comments · Fixed by #32
Labels

Comments

@maxceem
Copy link

maxceem commented Jan 30, 2017

I like your module because it looks very simple and does exactly what I want. Though I found out one inconsistency, maybe I missed something.

When I perform the first request which is not cached yet, I get a plain text in the body property of the response object in the callback function.

body: 'Text\n'

Though when I perform the second request for the same url which is already cached, I get a Buffer object at the body property.

body: <Buffer 54 65 78 74 0a>

And same for the body argument of the callback function.

I perform the request I passing only the one parameter url:

cachedRequest({
    url: 'http://localhost',
  }, (err, response, body) => {
  console.log(err, response, body);
});
@elhoyos
Copy link
Contributor

elhoyos commented Jul 3, 2017

Hi @maxceem. What cached-request version are you using to get two different response representations?

I just tried the following with 1.1.2 and got a string representation printed on every request:

const request = require('request')
const cr = require('cached-request')(request)
const crDirectory = '/tmp/cache'

cr({url: 'https://www.google.com'}, (err, response, body) => {console.log(body instanceof String)})

@maxceem
Copy link
Author

maxceem commented Jul 3, 2017

Unfortunately I don't remember what was the version, as because of this issue I decided not to use this module that time. But I'm sure it was version which was current in npm repository on Jan 30 2017.
I've just tried to reproduce the issue with the synthetic example using current version but cannot.

@elhoyos
Copy link
Contributor

elhoyos commented Jul 3, 2017

Here's an example reproducing the situation:

cr({url: 'https://www.google.com', ttl: 10000}, (err, response, body) => {console.log(body instanceof Buffer)}) // false
cr({url: 'https://www.google.com', ttl: 10000}, (err, response, body) => {console.log(body instanceof Buffer)}) // true

Response type should be consistent here. Thanks @maxceem.

Any patch is welcome or I will fix it when I have a window of time for it.

@xamgore
Copy link

xamgore commented May 25, 2019

At the moment I do something like this:

cr(options, (error, response) => {
  if (response.body instanceof Buffer)
    response.body = response.body.toString()
  
})

Seems it can be moved directly into the cached request's code, just before passing data to the error-response callback.

@elhoyos elhoyos changed the title Cached/uncached response body inconsystency Cached/uncached response body inconsistency Jun 13, 2019
@elhoyos elhoyos added bug and removed enhancement labels Jun 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants