Skip to content

Commit

Permalink
feat: response compression #174
Browse files Browse the repository at this point in the history
  • Loading branch information
David Longworth committed May 30, 2017
1 parent 6b879b6 commit 46929be
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 195 deletions.
4 changes: 2 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ var conf = convict({
}
},
headers: {
useGzipCompression: {
doc: "If true, uses gzip compression and adds a 'Content-Encoding:gzip' header to the response.",
useCompression: {
doc: "If true, uses br or gzip compression where available and adds a 'Content-Encoding: [br|gzip]' header to the response.",
format: Boolean,
default: true
},
Expand Down
21 changes: 4 additions & 17 deletions dadi/lib/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ var log = require('@dadi/logger')
var Datasource = require(path.join(__dirname, '/../datasource'))
var Event = require(path.join(__dirname, '/../event'))
var View = require(path.join(__dirname, '/../view'))

var Send = require(path.join(__dirname, '/../view/send'))
var servePublic = require(path.join(__dirname, '/../view/public'))

// helpers
var sendBackHTML = help.sendBackHTML
var sendBackJSON = help.sendBackJSON

/**
*
*/
Expand Down Expand Up @@ -174,15 +170,12 @@ Controller.prototype.process = function process (req, res, next) {
help.timer.start(req.method.toLowerCase())

var done

var statusCode = res.statusCode || 200

var data = this.buildInitialViewData(req)

var view = new View(req.url, this.page, data.json)

if (data.json) {
done = sendBackJSON(statusCode, res, next)
done = Send.json(statusCode, res, next)
} else {
if (config.get('server.protocol') === 'https' && res.push) {
servePublic.process(
Expand All @@ -194,13 +187,7 @@ Controller.prototype.process = function process (req, res, next) {
)
}

done = sendBackHTML(
req.method,
statusCode,
this.page.contentType,
res,
next
)
done = Send.html(res, req, next, statusCode, this.page.contentType)
}

this.loadData(req, res, data, (err, data, dsResponse) => {
Expand All @@ -218,7 +205,7 @@ Controller.prototype.process = function process (req, res, next) {
// not just the data, send the whole response back
if (dsResponse) {
if (dsResponse.statusCode === 202) {
done = sendBackJSON(dsResponse.statusCode, res, next)
done = Send.json(dsResponse.statusCode, res, next)
return done(null, data)
}
}
Expand Down
80 changes: 0 additions & 80 deletions dadi/lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,71 +102,6 @@ module.exports.isApiAvailable = function (done) {
request.end()
}

// helper that sends json response
module.exports.sendBackJSON = function (successCode, res, next) {
return function (err, results) {
if (err) return next(err)

var resBody = JSON.stringify(results, null, 2)

res.setHeader('Server', config.get('server.name'))

res.statusCode = successCode
res.setHeader('Content-Type', 'application/json')
res.setHeader('content-length', Buffer.byteLength(resBody))
res.end(resBody)
}
}

module.exports.sendBackJSONP = function (callbackName, res, next) {
return function (err, results) {
if (err) console.log(err)

// callback MUST be made up of letters only
if (!callbackName.match(/^[a-zA-Z]+$/)) return res.send(400)

res.statusCode = 200

var resBody = JSON.stringify(results)
resBody = callbackName + '(' + resBody + ');'
res.setHeader('Content-Type', 'text/javascript')
res.setHeader('content-length', resBody.length)
res.end(resBody)
}
}

// helper that sends html response
module.exports.sendBackHTML = function (
method,
successCode,
contentType,
res,
next
) {
return function (err, results) {
if (err) {
console.log(err)
return next(err)
}

var resBody = results

res.statusCode = successCode
res.setHeader('Server', config.get('server.name'))
res.setHeader('Content-Type', contentType)
res.setHeader('Content-Length', Buffer.byteLength(resBody))

self.addHeaders(res)

if (method.toLowerCase() === 'head') {
res.setHeader('Connection', 'close')
return res.end('')
} else {
return res.end(resBody)
}
}
}

/**
* Checks for valid client credentials in the request body
* @param {req} req - the HTTP request
Expand Down Expand Up @@ -209,21 +144,6 @@ module.exports.validateRequestMethod = function (req, res, allowedMethod) {
return true
}

/**
* Adds headers defined in the configuration file to the response
* @param {res} res - the HTTP response
*/
module.exports.addHeaders = function (res) {
var headers = config.get('headers')

_.each(headers.cors, function (value, header) {
res.setHeader(header, value)
if (header === 'Access-Control-Allow-Origin' && value !== '*') {
res.setHeader('Vary', 'Origin')
}
})
}

// function to wrap try - catch for JSON.parse to mitigate pref losses
module.exports.parseQuery = function (queryStr) {
var ret
Expand Down
14 changes: 4 additions & 10 deletions dadi/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1])
var _ = require('underscore')
var bodyParser = require('body-parser')
var colors = require("colors") // eslint-disable-line
var compress = require('compression')
var debug = require('debug')('web:server')
var dust = require('./dust')
var enableDestroy = require('server-destroy')
Expand Down Expand Up @@ -38,6 +37,7 @@ var cache = require(path.join(__dirname, '/cache'))
var Controller = require(path.join(__dirname, '/controller'))
var forceDomain = require(path.join(__dirname, '/controller/forceDomain'))
var help = require(path.join(__dirname, '/help'))
var Send = require(path.join(__dirname, '/view/send'))
var Middleware = require(path.join(__dirname, '/middleware'))
var servePublic = require(path.join(__dirname, '/view/public'))
var monitor = require(path.join(__dirname, '/monitor'))
Expand Down Expand Up @@ -154,9 +154,6 @@ Server.prototype.start = function (done) {
app.use(apiMiddleware.setUpRequest())
app.use(apiMiddleware.transportSecurity())

// add gzip compression
if (config.get('headers.useGzipCompression')) app.use(compress())

// init main public path for static files
if (options.publicPath) {
app.use(servePublic.middleware(options.publicPath))
Expand Down Expand Up @@ -407,7 +404,7 @@ Server.prototype.loadApi = function (options, reload, callback) {
help.validateRequestCredentials(req, res)
) {
return help.clearCache(req, err => {
help.sendBackJSON(200, res, next)(err, {
Send.json(200, res, next)(err, {
result: 'success',
message: 'Succeed to clear'
})
Expand Down Expand Up @@ -635,7 +632,7 @@ Server.prototype.addComponent = function (options, reload) {
if (next) {
return next()
} else {
return help.sendBackJSON(404, res, next)(
return Send.json(404, res, next)(
null,
require(options.filepath)
)
Expand All @@ -646,10 +643,7 @@ Server.prototype.addComponent = function (options, reload) {
if (next) {
return next()
} else {
return help.sendBackJSON(404, res, next)(
null,
require(options.filepath)
)
return Send.json(404, res, next)(null, require(options.filepath))
}
}
})
Expand Down
Loading

0 comments on commit 46929be

Please sign in to comment.