Skip to content

Commit

Permalink
Use compression library directly instead of express.compress
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Feb 9, 2016
1 parent 61f6687 commit 55e1237
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/hooks/http/middleware/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = function(sails, app) {
return cookieParser && cookieParser(sessionSecret);
})(),

compress: IS_PRODUCTION && express.compress(),
compress: IS_PRODUCTION && require('compression')(),

// Use body parser, if enabled
bodyParser: (function() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"captains-log": "~0.11.11",
"colors": "1.1.2",
"commander": "2.9.0",
"compression": "^1.5.2",
"connect": "2.30.0",
"connect-flash": "0.1.1",
"consolidate": "0.12.1",
Expand Down
119 changes: 119 additions & 0 deletions test/integration/middleware.compression.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
var _ = require('lodash');
var request = require('request');
var Sails = require('../../lib').Sails;
var assert = require('assert');
describe('middleware :: ', function() {

describe('compression :: ', function() {

// Source text (must be > 1024 bytes to trigger compression)
var lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras fringilla mollis sapien sed consequat. Cras vestibulum iaculis rhoncus. Vestibulum et auctor dolor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc vitae risus sit amet massa lacinia luctus. Quisque auctor hendrerit fermentum. Nullam neque diam, condimentum a nisi eu, ornare porttitor nisl. Praesent porttitor augue turpis, eu consectetur ligula suscipit eget. Aliquam placerat turpis ut varius vestibulum. Pellentesque ligula velit, molestie vel purus sollicitudin, elementum rutrum odio. Ut ultricies convallis leo. Integer id nisl vel tellus laoreet iaculis sed et ipsum. In malesuada sem vitae porttitor sollicitudin. Maecenas sodales est eu augue auctor, in accumsan dolor lacinia. Proin at euismod nibh, eu congue velit. Vestibulum risus velit, vulputate in dui in, commodo sodales elit. Curabitur consectetur justo tincidunt odio imperdiet blandit. Etiam gravida eu ante commodo viverra. Ut sed dapibus purus, eu vulputate neque. Maecenas suscipit felis ac sapien iaculis tempor. Etiam quis vulputate turpis. Cras at nulla lectus. Vestibulum non magna sem. Aliquam tristique lacinia ligula, non interdum justo scelerisque vitae. Praesent molestie eu nibh vel volutpat. Pellentesque ut lacus a tortor lacinia condimentum. Quisque blandit facilisis nunc sed tempus. Praesent dapibus leo at enim mollis, tristique facilisis turpis aliquam. Vestibulum tempus felis ac arcu rhoncus, in efficitur elit sodales. Suspendisse eu odio odio. Vestibulum tempus elementum massa, et rutrum risus ultricies ut. Ut ac mattis nulla. Aenean tristique sollicitudin metus. Morbi massa purus, hendrerit non placerat non, imperdiet nec turpis. Nulla et ultrices metus. Nulla eget congue urna, ut rutrum enim. Aenean rutrum dui massa, non luctus urna dignissim vel. Morbi a suscipit ligula. Nunc laoreet nisi eleifend tortor volutpat finibus vel nec risus. Fusce maximus non sem vel mattis. Etiam iaculis, turpis at sollicitudin blandit, massa mi finibus nunc, nec auctor ex nisl sed.";


describe('In the production environment', function() {

// Lift a Sails instance in production mode
var app = Sails();
before(function (done){
app.lift({
globals: false,
port: 1535,
environment: 'production',
log: {level: 'silent'},
hooks: {session: false},
routes: {
'/test': function(req, res) {
res.send(lipsum);
}
}
}, done);
});

after(function(done) {
app.lower(done);
});

it('responses should be compressed', function(done) {
var rawLen = 0, res = '';
request(
{
method: 'GET',
uri: 'http://localhost:1535/test',
gzip: true
}
)
.on('data', function(data) {
// decompressed data as it is received
res += data;
})
.on('response', function(response) {
// unmodified http.IncomingMessage object
response.on('data', function(data) {
rawLen += data.length;
})
})
.on('end', function(err) {
assert.equal(res, lipsum);
assert(rawLen < lipsum.length, 'Expected length of raw response data (' + rawLen.toString() + ') to be < length of source data (' + lipsum.length.toString() + ').');
return done(err);
});

});

});

describe('In the development environment', function() {

// Lift a Sails instance in production mode
var app = Sails();
before(function (done){
app.lift({
globals: false,
port: 1535,
environment: 'development',
log: {level: 'silent'},
hooks: {session: false},
routes: {
'/test': function(req, res) {
res.send(lipsum);
}
}
}, done);
});

after(function(done) {
app.lower(done);
});

it('responses should not be compressed', function(done) {
var rawLen = 0, res = '';
request(
{
method: 'GET',
uri: 'http://localhost:1535/test',
gzip: true
}
)
.on('data', function(data) {
// decompressed data as it is received
res += data;
})
.on('response', function(response) {
// unmodified http.IncomingMessage object
response.on('data', function(data) {
rawLen += data.length;
})
})
.on('end', function(err) {
assert.equal(res, lipsum);
assert.equal(rawLen, lipsum.length, 'Expected length of raw response data (' + rawLen.toString() + ') to be equal to length of source data (' + lipsum.length.toString() + ').');
return done(err);
});

});

});

});

});

2 comments on commit 55e1237

@mikermcneil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dougwilson it starts

@dougwilson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noice

Please sign in to comment.