Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions dadi/lib/cache/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const crypto = require('crypto')
const etag = require('etag')
const lengthStream = require('length-stream')
const path = require('path')
const pathToRegexp = require('path-to-regexp')
Expand Down Expand Up @@ -95,12 +96,24 @@ Cache.prototype.init = function () {
return cache
.getMetadata(cacheKey)
.then(metadata => {
res.statusCode = 200
res.setHeader('X-Cache-Lookup', 'HIT')

let compressed = false

if (metadata && metadata.compression === 'gzip') {
compressed = true
if (metadata) {
if (metadata.compression === 'gzip') {
compressed = true
}

if (metadata.etag) {
res.setHeader('ETag', metadata.etag)

if (req.headers['if-none-match'] === metadata.etag) {
res.statusCode = 304
return res.end()
}
}
}

if (noCache) {
Expand Down Expand Up @@ -170,12 +183,15 @@ Cache.prototype.init = function () {
_end.apply(res, arguments)

// If response is not 200 don't cache.
if (res.statusCode !== 200) return
if (res.statusCode !== 200) {
return
}

// Cache the content.
cache.set(cacheKey, data, {
metadata: {
compression: !acceptEncoding ? 'none' : acceptEncoding
compression: !acceptEncoding ? 'none' : acceptEncoding,
etag: etag(data)
}
}).then(() => {

Expand Down
18 changes: 12 additions & 6 deletions dadi/lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const cache = require('./cache')
const config = require('./../../config')
const crypto = require('crypto')
const ERROR_CODES = require('./../../error-codes')
const etag = require('etag')
const formatError = require('@dadi/format-error')
const log = require('@dadi/logger')
const stackTrace = require('stack-trace')
Expand Down Expand Up @@ -82,12 +83,6 @@ module.exports.sendBackJSON = function (successCode, res, next) {

let resBody = body ? JSON.stringify(body) : null

// log response if it's already been sent
if (res.finished) {
log.info({res: res}, 'Response already sent. Attempting to send results: ' + resBody)
return
}

if (originalRequest && module.exports.shouldCompress(originalRequest)) {
res.setHeader('Content-Encoding', 'gzip')

Expand All @@ -105,6 +100,17 @@ module.exports.sendBackJSON = function (successCode, res, next) {

return Promise.resolve(resBody).then(resBody => {
res.setHeader('Content-Type', 'application/json')

if (resBody) {
let etagResult = etag(resBody)
res.setHeader('ETag', etagResult)

if (originalRequest && originalRequest.headers['if-none-match'] === etagResult) {
res.statusCode = 304
return res.end()
}
}

res.statusCode = statusCode
res.end(resBody)
})
Expand Down
6 changes: 3 additions & 3 deletions dadi/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var chokidar = require('chokidar')
var cluster = require('cluster')
var colors = require('colors') // eslint-disable-line
var debug = require('debug')('api:server')
var parsecomments = require('parse-comments')
var ParseComments = require('parse-comments')
var fs = require('fs')
var mkdirp = require('mkdirp')
var path = require('path')
Expand Down Expand Up @@ -632,7 +632,7 @@ Server.prototype.addEndpointResource = function (options) {

this.addComponent({
aclKey,
docs: parsecomments(content),
docs: new ParseComments().parse(content),
component,
filepath,
route
Expand Down Expand Up @@ -688,7 +688,7 @@ Server.prototype.addHook = function (options) {
var opts = {
route: 'hook:' + name,
component: filepath,
docs: parsecomments(content),
docs: new ParseComments().parse(content),
filepath: filepath
}

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"debug": "3.1.0",
"deep-clone": "^3.0.2",
"deepmerge": "^2.1.0",
"etag": "^1.8.1",
"fs-extra": "^3.0.1",
"imagesize": "^1.0.0",
"js-promise-queue": "^1.1.0",
Expand All @@ -52,7 +53,7 @@
"moment": "2.19.3",
"natural": "^0.6.1",
"object-path": "^0.11.4",
"parse-comments": "0.4.3",
"parse-comments": "^1.0.0",
"path-to-regexp": "~1.7.0",
"recovery": "^0.2.6",
"require-directory": "^2.1.1",
Expand All @@ -65,7 +66,7 @@
"vary": "^1.1.2"
},
"devDependencies": {
"@commitlint/cli": "~4.1.1",
"@commitlint/cli": "^7.5.2",
"@commitlint/config-angular": "~3.1.1",
"aws-sdk-mock": "1.6.1",
"coveralls": "^3.0.1",
Expand All @@ -83,7 +84,7 @@
"should": "4.0.4",
"sinon": "2.3.2",
"snazzy": "7.0.0",
"snyk": "^1.104.1",
"snyk": "^1.147.3",
"standard": "8.6.0",
"supertest": "^3.1.0",
"uuid": "^3.3.2"
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe('Application', function () {
docs.should.exist
docs.should.be.Array

docs[0].lead.should.eql('Adds two numbers together.')
docs[0].description.should.eql('Adds two numbers together.')

done()
})
Expand Down
27 changes: 27 additions & 0 deletions test/acceptance/rest-endpoints/collections-api/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -1229,5 +1229,32 @@ describe('Collections API – GET', function () {
})
})
})

it('should respond with 304 if etag matches If-None-Match header', function (done) {
help.createDoc(bearerToken, function (err, doc1) {
if (err) return done(err)
help.createDoc(bearerToken, function (err, doc2) {
if (err) return done(err)

var client = request(connectionString)
client
.get('/vtest/testdb/test-schema')
.set('Authorization', 'Bearer ' + bearerToken)
.expect(200)
.expect('content-type', 'application/json')
.end((err, res) => {
if (err) return done(err)

let etag = res.headers['etag']

client
.get('/vtest/testdb/test-schema')
.set('Authorization', 'Bearer ' + bearerToken)
.set('If-None-Match', etag)
.expect(304, done)
})
})
})
})
})
})
4 changes: 2 additions & 2 deletions test/unit/helpTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Help', function (done) {
it('should send an error with the formatted message corresponding to the API error code with the status code provided', done => {
let res = {
end: function end (resBody) {
this.setHeader.callCount.should.eql(2)
this.setHeader.callCount.should.eql(3)
this.setHeader.args[0][0].should.eql('Content-Length')
this.setHeader.args[0][1].should.be.Number
this.setHeader.args[1][0].should.eql('Content-Type')
Expand All @@ -68,7 +68,7 @@ describe('Help', function (done) {
it('should send an error with the formatted message corresponding to the API error code with the status code 500 if one is not provided', done => {
let res = {
end: function end (resBody) {
this.setHeader.callCount.should.eql(2)
this.setHeader.callCount.should.eql(3)
this.setHeader.args[0][0].should.eql('Content-Length')
this.setHeader.args[0][1].should.be.Number
this.setHeader.args[1][0].should.eql('Content-Type')
Expand Down