Skip to content

Commit

Permalink
feat: as a dev I want the cache layer to have access to updating the …
Browse files Browse the repository at this point in the history
…UA data in the transform fn
  • Loading branch information
demetriusj committed Apr 19, 2015
1 parent 2c10988 commit 461fff9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
97 changes: 59 additions & 38 deletions src/components/user-agent-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,54 @@ var pellet = require('pellet')
, UAParser = new _UAParser();

if(pellet.options.includeUserAgentInfo) {

function getUA(ua) {
var detect = new mobileDetect(ua);
ua = UAParser.setUA(ua).getResult();

if(!ua.device.type) {
if(detect.tablet()) {
ua.device.type = 'tablet';
ua.device.vendor = detect.tablet().replace(/Tablet/i,'');
} else if(detect.mobile()) {
ua.device.type = 'mobile';
ua.device.vendor = detect.mobile().replace(/Mobile/i,'');
} else if(detect.is('console')) {
ua.device.type = 'console';
} else if(detect.is('tv')) {
ua.device.type = 'tv';
}
}

if(detect.is('wpdesktop')) {
ua.device.type = 'web';
} else if(detect.phone()) {
ua.device.type = 'phone';
} else if(!ua.device.type) {
ua.device.type = 'web';
}

if(detect.is('bot')) {
ua.type = 'bot';
if(detect.is('mobilebot')) {
ua.type = 'mobilebot';
ua.device.type = 'mobile';
}
} else {
ua.type = 'device';
}

delete ua.ua;

return ua;
}

pellet.middlewareStack.push({
priority: 7,
fn: function(req, res, next) {
var ua = req.headers['user-agent'];
var detect;

if(ua) {
detect = new mobileDetect(ua);
ua = UAParser.setUA(ua).getResult();

if(!ua.device.type) {
if(detect.tablet()) {
ua.device.type = 'tablet';
ua.device.vendor = detect.tablet().replace(/Tablet/i,'');
} else if(detect.mobile()) {
ua.device.type = 'mobile';
ua.device.vendor = detect.mobile().replace(/Mobile/i,'');
} else if(detect.is('console')) {
ua.device.type = 'console';
} else if(detect.is('tv')) {
ua.device.type = 'tv';
}
}

if(detect.is('wpdesktop')) {
ua.device.type = 'web';
} else if(detect.phone()) {
ua.device.type = 'phone';
} else if(!ua.device.type) {
ua.device.type = 'web';
}

if(detect.is('bot')) {
ua.type = 'bot';
if(detect.is('mobilebot')) {
ua.type = 'mobilebot';
ua.device.type = 'mobile';
}
} else {
ua.type = 'device';
}

delete ua.ua;
ua = getUA(ua);
}

if(!req.requestContext) {
Expand All @@ -57,4 +62,20 @@ if(pellet.options.includeUserAgentInfo) {
next();
}
});

if(process.env.SERVER_ENV) {
pellet.patchUACacheLayer = function (pipeline, ctx, head, metaData, next) {

var ua = pipeline.http.request.headers['user-agent'];
if(ua) {
ctx.requestContext.userAgentDetails = getUA(ua);
}

next(null, ctx, head);
}
} else {
pellet.patchUACacheLayer = function (pipeline, ctx, head, metaData, next) {
next(null, ctx, head);
}
}
}
2 changes: 1 addition & 1 deletion src/isomorphic/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ pipeline.prototype.serveFromCache = function(dirtyRead, transformCtxFn, next) {
}

if(transformCtxFn) {
transformCtxFn((data && data.ctx && JSON.parse(data.ctx)), data.head, metaData, function(err, ctx, head) {
transformCtxFn(_this, (data && data.ctx && JSON.parse(data.ctx)), data.head, metaData, function(err, ctx, head) {
console.debug('Cache layer: use dirty read', dirtyRead && ((Date.now() - metaData.lastModified) <= dirtyRead), 'ttl:', dirtyRead, 'elapse:', (Date.now() - metaData.lastModified));

if(dirtyRead && ((Date.now() - metaData.lastModified) <= dirtyRead)) {
Expand Down

0 comments on commit 461fff9

Please sign in to comment.