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.1 to 0.1.2

26

lib/cached-request.js

@@ -21,3 +21,3 @@ /*

Response.prototype._transform = function (chunk, enconding, callback) {
this.push(chunk.toString());
this.push(chunk);
callback();

@@ -181,18 +181,24 @@ };

//Gunzip the response file
gunzip = zlib.createGunzip();
responseReader.pipe(gunzip);
var stream;
if (response.headers['content-encoding'] === 'gzip') {
stream = responseReader;
} else {
// Gunzip the response file
stream = zlib.createGunzip();
responseReader.pipe(stream);
}
//Read the response file
responseBody = "";
gunzip.on("data", function (data) {
data = data.toString();
var responseBody;
stream.on("data", function (data) {
//Write to the response
response.write(data);
//If a callback was provided, then buffer the response to send it later
if (callback) responseBody += data;
if (callback) {
responseBody = responseBody ? Buffer.concat([responseBody, data]) : data;
}
//Push data to the client's request
requestMiddleware.push(data);
});
gunzip.on("end", function () {
stream.on("end", function () {
//End response

@@ -207,3 +213,3 @@ response.end();

try {
responseBody = JSON.parse(responseBody);
responseBody = JSON.parse(responseBody.toString());
} catch (e) {

@@ -210,0 +216,0 @@ return callback(e);

{
"name": "cached-request",
"version": "0.1.1",
"version": "0.1.2",
"description": "Node.js module to perform HTTP requests with caching support",

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

@@ -89,2 +89,42 @@ var CachedRequest = require("../")

});
it("responds the same from the cache if gzipped", function (done) {
var self = this;
var responseBody = 'foo';
var options = {
url: "http://ping.com/",
ttl: 5000,
encoding: null // avoids messing with gzip responses so we can handle them
};
//Return gzip compressed response with valid content encoding header
mock("GET", 1, function () {
return new MockedResponseStream({}, responseBody).pipe(zlib.createGzip());
},
{
"Content-Encoding": "gzip"
});
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;
zlib.gunzip(body, function (error, buffer) {
if (error) return done(error);
expect(buffer.toString()).to.deep.equal(responseBody);
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.equal(1);
zlib.gunzip(body, function (error, buffer) {
if (error) done(error);
expect(buffer.toString()).to.deep.equal(responseBody);
done();
});
});
});
});
});
});

@@ -153,22 +193,32 @@

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();
});
.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");
var gunzip = zlib.createGunzip();
gunzip.on("data", function (data) {
body += data.toString();
});
gunzip.on("end", function () {
expect(body).to.equal(responseBody);
done();
});
gunzip.on('error', function (error) {
done(error);
});
response.pipe(gunzip);
});
});
});
});

@@ -175,0 +225,0 @@ });

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