Skip to content

Commit

Permalink
Added processUrls config option and use that instead of always proces…
Browse files Browse the repository at this point in the history
…sing html.
  • Loading branch information
papandreou committed Oct 19, 2012
1 parent 386e842 commit a6bf896
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 60 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ icons and background images:
.use(express.logger())
.use(require('assetgraph-middleware')({
root: root,
processUrls: /\/$|\.html$/,
transform: function (assetGraph, cb) {
// This transform will be run on an assetgraph object that contains only the Html asset.
assetGraph
Expand Down
139 changes: 79 additions & 60 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var AssetGraph = require('assetgraph'),
transforms = AssetGraph.transforms,
passError = require('passerror'),
URL = require('url'),
_ = require('underscore'),
query = AssetGraph.query;

require('bufferjs');
Expand Down Expand Up @@ -34,8 +36,23 @@ module.exports = function (options) {
}
res.end(asset.rawSrc);
} else {
var syntheticIfNoneMatch = false;
if (req.accepts('html') || /\*\/\*/.test(req.headers.accept)) {
var syntheticIfNoneMatch = false,
doProcess = !!asset;

if (!doProcess) {
if (options.processUrls) {
if (_.isRegExp(options.processUrls)) {
doProcess = options.processUrls.test(req.url);
} else if (_.isArray(options.processUrls)) {
doProcess = options.processUrls.indexOf(req.url) !== -1;
} else {
// Assume string
doProcess = options.processUrls === req.url;
}
}
}

if (doProcess) {
delete req.headers['if-modified-since'];

if (asset && asset.maxAge && ((new Date().getTime() - asset.date.getTime()) / 1000 < asset.maxAge)) {
Expand Down Expand Up @@ -99,68 +116,70 @@ module.exports = function (options) {
return res.end();
}
} else {
var matchContentType = contentType && contentType.match(/^text\/html(?:;\s*charset=([a-z0-9\-]+))?$/i);
var assetGraph = new AssetGraph({root: options.root}),
now = new Date(),
cacheControl = res.getHeader('Cache-Control'),
expires = res.getHeader('Expires'),
maxAge = 0,
matchCacheControl = cacheControl && cacheControl.match(/\bmax-age=(\d+)/);
if (matchCacheControl) {
maxAge = parseInt(matchCacheControl[1], 10);
} else if (expires) {
expires = new Date(expires);
if (!isNaN(expires.getTime())) {
maxAge = Math.floor((expires.getTime() - now.getTime()) / 1000);
}
}
var date = new Date(res.getHeader('date'));
if (isNaN(date.getTime())) {
date = now;
}
var assetConfig = {
url: URL.resolve(assetGraph.root, req.url.substr(1)),
isInitial: true,
etag: etag,
date: date,
expires: expires,
cacheControl: cacheControl,
maxAge: maxAge
};
var matchContentType = contentType.match(/^\s*([\w\-\+\.]+\/[\w\-\+\.]+)(?:\s|;|$)/i);
if (matchContentType) {
// Not cached or outdated, optimize it, save it, then deliver to the client:
res.removeHeader('Content-Length');
if (options.debug) {
res.setHeader('X-AssetGraph-Middleware-Cache', 'miss');
assetConfig.contentType = matchContentType[1].toLowerCase();
var matchCharset = contentType.match(/;\s*charset\s*=\s*(['"]|)\s*([\w\-]+)\s*\1(?:\s|;|$)/i);
if (matchCharset) {
assetConfig.encoding = matchCharset[2].toLowerCase();
}
var encoding = matchContentType[1] || 'iso-8859-1',
chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
var now = new Date(),
cacheControl = res.getHeader('Cache-Control'),
expires = res.getHeader('Expires'),
maxAge = 0,
matchCacheControl = cacheControl && cacheControl.match(/\bmax-age=(\d+)/);
if (matchCacheControl) {
maxAge = parseInt(matchCacheControl[1], 10);
} else if (expires) {
expires = new Date(expires);
if (!isNaN(expires.getTime())) {
maxAge = Math.floor((expires.getTime() - now.getTime()) / 1000);
}
}
var date = new Date(res.getHeader('date'));
if (isNaN(date.getTime())) {
date = now;
}

var assetGraph = new AssetGraph({root: options.root}),
htmlAsset = new AssetGraph.assets.Html({
rawSrc: Buffer.concat(chunks),
url: URL.resolve(assetGraph.root, req.url),
encoding: encoding,
isInitial: true,
etag: etag,
date: date,
expires: expires,
cacheControl: res.getHeader('Cache-Control'),
maxAge: maxAge
});
}

assetGraph
.loadAssets(htmlAsset)
.queue(options.transform)
.queue(function (assetGraph) {
assetGraph.findAssets({isInline: false}).forEach(function (asset) {
processedAssetByUrl['/' + asset.url.replace(assetGraph.root, '')] = asset;
});
processedAssetByUrl[req.url] = htmlAsset;
})
.run(function (err) {
if (err) {
return next(err);
}
res.end(htmlAsset.rawSrc);
});
});
} else {
res.unhijack();
res.removeHeader('Content-Length');
if (options.debug) {
res.setHeader('X-AssetGraph-Middleware-Cache', 'miss');
}
var chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
assetConfig.rawSrc = Buffer.concat(chunks);
var asset;
assetGraph
.loadAssets(assetConfig)
.queue(function () {
asset = assetGraph.findAssets({isInitial: true})[0];
})
.queue(options.transform)
.queue(function () {
assetGraph.findAssets({isInline: false}).forEach(function (asset) {
processedAssetByUrl['/' + asset.url.replace(assetGraph.root, '')] = asset;
});
if (asset) {
processedAssetByUrl[req.url] = asset;
}
})
.run(passError(next, function () {
res.end(asset.rawSrc);
}));
});
}
});
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"assetgraph": "=0.4.5",
"bufferjs": "=1.1.0",
"express-hijackresponse": "=0.0.7",
"passerror": "=0.0.1",
"underscore": "=1.2.4"
},
"devDependencies": {
Expand Down

0 comments on commit a6bf896

Please sign in to comment.