Socket
Socket
Sign inDemoInstall

cached-request

Package Overview
Dependencies
3
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.1.1

13

lib/cached-request.js

@@ -111,2 +111,6 @@ /*

CachedRequest.prototype.parseHeaders = function (headers) {
return JSON.parse(headers);
};
CachedRequest.prototype.cachedRequest = function () {

@@ -190,3 +194,10 @@ var self = this

headersReader.on("end", function () {
response.headers = JSON.parse(response.headers);
try {
response.headers = self.parseHeaders(response.headers);
} catch (e) {
self.handleError(e);
headersReader.close();
return makeRequest();
}
//Notify the response comes from the cache.

@@ -193,0 +204,0 @@ response.headers["x-from-cache"] = 1;

2

package.json
{
"name": "cached-request",
"version": "1.1.0",
"version": "1.1.1",
"description": "Node.js module to perform HTTP requests with caching support",

@@ -5,0 +5,0 @@ "author": "Daniel López <danypype@gmail.com>",

@@ -8,3 +8,3 @@ #cached-request

##How it works
This tool was made to work with the popular [request](https://github.com/request/request) module, which simplifies the HTTP requests in Node.js. Therefore, this must be consireded a wrapper around **request**.
This tool was made to work with the popular [request](https://github.com/request/request) module, which simplifies the HTTP requests in Node.js. Therefore, this must be considered a wrapper around **request**.

@@ -11,0 +11,0 @@ First, you instantiate a **cachedRequest** instance by passing a **request** function, which is going to act as the requester for the uncached requests - you still need to `$npm install request` independently. - Then, you can use **cachedRequest** to perform your HTTP requests.

@@ -42,4 +42,9 @@ var CachedRequest = require("../")

this.cachedRequest.setCacheDirectory(cacheDir);
nock.cleanAll();
});
afterEach(function (done) {
temp.cleanup(done);
})
describe("caching", function () {

@@ -175,2 +180,39 @@ it("makes the request when the response isn't cached", function (done) {

});
describe('when cannot parse the cached response headers', function () {
after(function () {
if (this._parseHeaders) {
this.cachedRequest.set('parseHeaders', this._parseHeaders);
}
});
it("makes the request", function (done) {
var self = this;
var options = {uri: "http://ping.com/", ttl: 5000};
mock("GET", 2, function () {
return new MockedResponseStream({}, "pong");
}, {foo: 'bar'});
this.cachedRequest(options, function (error, response, body) {
if (error) return done(error);
expect(response.statusCode).to.equal(200);
expect(response.headers["x-from-cache"]).to.not.exist;
expect(body).to.equal("pong");
self._parseHeaders = self.cachedRequest.get('parseHeaders');
self.cachedRequest.set('parseHeaders', function () {
throw new Error('Cannot parse headers');
});
self.cachedRequest(options, function (error, response, body) {
if (error) return done(error);
expect(response.statusCode).to.equal(200);
expect(response.headers["x-from-cache"]).to.not.exist;
expect(body).to.equal("pong");
done();
});
});
});
});
});

@@ -310,6 +352,2 @@

});
after(function () {
temp.cleanupSync();
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc