Socket
Socket
Sign inDemoInstall

cached-request

Package Overview
Dependencies
2
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.1.1

10

lib/cached-request.js

@@ -231,3 +231,11 @@ /*

responseWriter = fs.createWriteStream(self.cacheDirectory + key);
response.pipe(zlib.createGzip()).pipe(responseWriter);
var contentEncoding = response.headers['content-encoding'] || ''
contentEncoding = contentEncoding.trim().toLowerCase()
if (contentEncoding === 'gzip') {
response.pipe(responseWriter);
} else {
response.pipe(zlib.createGzip()).pipe(responseWriter);
}
}

@@ -234,0 +242,0 @@ });

8

package.json
{
"name": "cached-request",
"version": "0.1.0",
"version": "0.1.1",
"description": "Node.js module to perform HTTP requests with caching support",
"author": "Daniel López <danypype@gmail.com>",
"scripts": {
"test": "./node_modules/.bin/mocha"
"test": "mocha"
},

@@ -27,7 +27,7 @@ "main": "./lib/index.js",

"chai": "^1.10.0",
"execSync": "^1.0.2",
"mocha": "^2.1.0",
"nock": "^0.52.4",
"request": "^2.51.0"
"request": "^2.51.0",
"temp": "^0.8.1"
}
}
#cached-request
Node.js module to perform HTTP requests with caching support.
#Why?
##Why?
At [alltherooms](http://alltherooms.com/) we make lots of requests to external APIs, and caching is crucial to provide a good experience to our users. We also love streams! however, we had a hard time finding a good tool for caching HTTP responses and streaming them at the same time, so we wrote **cached-request**. We hope to help others, and feedback is always welcome. :)

@@ -72,2 +72,2 @@

##License (MIT)
##License (MIT)
var CachedRequest = require("../")
, request = require("request")
, nock = require("nock")
, sh = require("execSync")
, temp = require('temp').track()
, Readable = require("stream").Readable
, util = require("util");
, util = require("util")
, zlib = require("zlib");

@@ -21,5 +22,5 @@ util.inherits(MockedResponseStream, Readable);

describe("CachedRequest", function () {
var cacheDir = __dirname + "/cache";
var cacheDir;
function mock (method, times, response) {
function mock (method, times, response, headers) {
method = method.toLowerCase();

@@ -31,7 +32,6 @@ times = times || 1;

.times(times)
.reply(200, response);
.reply(200, response, headers);
};
before(function () {
sh.exec("mkdir " + cacheDir);
nock.disableNetConnect();

@@ -41,3 +41,3 @@ });

beforeEach(function () {
sh.exec("rm " + cacheDir + "/*");
cacheDir = temp.mkdirSync("cache");
this.cachedRequest = CachedRequest(request);

@@ -133,7 +133,50 @@ this.cachedRequest.setCacheDirectory(cacheDir);

});
it("handles gzip response", function (done) {
var self = this;
var responseBody = "";
for (var i = 0; i < 1000; i++) {
responseBody += "this is a long response body";
};
//Return gzip compressed response with valid content encoding header
mock("GET", 1, function () {
return new MockedResponseStream({}, responseBody).pipe(zlib.createGzip());
},
{
"Content-Encoding": "gzip"
});
var options = {url: "http://ping.com/", ttl: 5000};
var body = "";
//Make fresh request
this.cachedRequest(options)
.on("data", function (data) {
//Ignore first reply
})
.on("end", function () {
body = "";
//Make cached request
self.cachedRequest(options)
.on("response", function (response) {
expect(response.statusCode).to.equal(200);
expect(response.headers["x-from-cache"]).to.equal(1);
expect(response.headers["content-encoding"]).to.equal("gzip");
response.on("data", function (data) {
body += data;
})
.on("end", function () {
expect(body).to.equal(responseBody);
done();
});
});
});
});
});
after(function () {
sh.exec("rm -rf " + cacheDir);
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