express-negotiator
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -9,2 +9,3 @@ var fs = require('fs'), | ||
mime = require('mime'), | ||
bowser = require('bowser-papandreou'), | ||
// List taken from an older version of CLDR, so it's probably incomplete: | ||
@@ -59,6 +60,10 @@ isLocaleIdRegExp = /^(?:aa|af|ak|am|ar|as|asa|az|be|bem|bez|bg|bm|bn|bo|br|brx|bs|byn|ca|cch|cgg|chr|cs|cy|da|dav|de|dv|dz|ebu|ee|el|en|eo|es|et|eu|fa|ff|fi|fil|fo|fr|fur|ga|gaa|gez|gl|gsw|gu|guz|gv|ha|haw|he|hi|hr|hu|hy|ia|id|ig|ii|in|is|it|iu|iw|ja|jmc|ka|kab|kaj|kam|kcg|kde|kea|kfo|khq|ki|kk|kl|kln|km|kn|ko|kok|kpe|ksb|ksh|ku|kw|ky|lag|lg|ln|lo|lt|luo|luy|lv|mas|mer|mfe|mg|mi|mk|ml|mn|mo|mr|ms|mt|my|naq|nb|nd|nds|ne|nl|nn|no|nr|nso|ny|nyn|oc|om|or|pa|pl|ps|pt|rm|ro|rof|root|ru|rw|rwk|sa|saq|se|seh|ses|sg|sh|shi|si|sid|sk|sl|sn|so|sq|sr|ss|ssy|st|sv|sw|syr|ta|te|teo|tg|th|ti|tig|tl|tn|to|tr|trv|ts|tt|tzm|ug|uk|ur|uz|ve|vi|vun|wal|wo|xh|xog|yo|zh|zu)(?:_|$)/; | ||
// .html.en-US => {contentType: 'text/html', contentTypeFragments: ['text', 'html'], localeId: 'en_us'} | ||
function getVariantInfoFromExtensionString(extensionString) { | ||
// .html.mobile.en-US => {contentType: 'text/html', contentTypeFragments: ['text', 'html'], mobile: true, localeId: 'en_us'} | ||
function getVariantInfoFromExtensionString(extensionString, supportUserAgent) { | ||
var variantInfo = { | ||
allExtensionsMatched: true | ||
}; | ||
if (supportUserAgent) { | ||
variantInfo.userAgent = {}; | ||
} | ||
if (extensionString) { | ||
@@ -74,2 +79,4 @@ extensionString.split(/(?=\.)/).forEach(function (extension) { | ||
variantInfo.contentTypeFragments = contentType.split('/'); | ||
} else if (supportUserAgent && /^\.(?:mobile|ie|chrome|phantom|safari|ios|iphone|ipad|touchpad|android|opera|firefox|seamonkey)$/.test(extension)) { | ||
variantInfo.userAgent[extension.substr(1)] = true; | ||
} else { | ||
@@ -90,3 +97,11 @@ variantInfo.allExtensionsMatched = false; | ||
var fsWatcherByDir = {}; | ||
var getVariantInfosByBaseName = memoizeAsync(function (dir, cb) { | ||
if (options.watch && !(dir in fsWatcherByDir)) { | ||
fsWatcherByDir[dir] = fs.watch(dir, {}, function (eventName, fileName) { | ||
getVariantInfosByBaseName.purge(dir); | ||
}); | ||
} | ||
fs.readdir(dir, function (err, fileNames) { | ||
@@ -118,3 +133,3 @@ if (err) { | ||
if (extensionString) { | ||
_.extend(variantInfo, getVariantInfoFromExtensionString(extensionString)); | ||
_.extend(variantInfo, getVariantInfoFromExtensionString(extensionString, options.userAgent)); | ||
} | ||
@@ -160,2 +175,4 @@ (variantInfosByBaseName[baseName] = variantInfosByBaseName[baseName] || []).push(variantInfo); | ||
var getUserAgentInfo = memoizeSync(bowser); | ||
return function expressNegotiator(req, res, next) { | ||
@@ -218,3 +235,10 @@ var matchDirAndFileName = req.url.match(/^(\/(?:[\w\.\-~%!$&'\(\)*+,;=:@]+\/)*)([\w\-~%!$&'\(\)*+,;=:@]*)((?:\.[\w\-~%!$&'\(\)*+,;=:@]*)*)((?:\?.*)?)$/); | ||
} | ||
if (options.userAgent && req.headers['user-agent']) { | ||
var userAgentInfo = getUserAgentInfo(req.headers['user-agent']); | ||
Object.keys(userAgentInfo).forEach(function (key) { | ||
if (variantInfo.userAgent[key]) { | ||
contentTypeQuality += .01; | ||
} | ||
}); | ||
} | ||
// In case two acceptable entries have the same quality, prefer the text/html one. | ||
@@ -241,3 +265,3 @@ if (contentTypeQuality > 0 && | ||
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate'); | ||
res.setHeader('Vary', 'Cookie, Accept-Language, Accept'); | ||
res.setHeader('Vary', 'Cookie, Accept-Language, Accept' + (options.userAgent ? ', User-Agent': '')); | ||
@@ -244,0 +268,0 @@ var etag = ''; |
@@ -25,3 +25,3 @@ { | ||
], | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"engines": { | ||
@@ -31,2 +31,3 @@ "node": "*" | ||
"dependencies": { | ||
"bowser-papandreou": "=0.2.0-patch2", | ||
"memoizeasync": "=0.0.1", | ||
@@ -33,0 +34,0 @@ "memoizesync": "=0.0.1", |
@@ -5,9 +5,6 @@ var path = require('path'), | ||
express = require('express'), // So that http.IncomingMessage.prototype gets patched | ||
_ = require('underscore'), | ||
expressNegotiator = require('../lib/expressNegotiator')({ | ||
root: path.resolve(__dirname, 'root'), | ||
cookieName: 'locale' | ||
}); | ||
expressNegotiator = require('../lib/expressNegotiator'), | ||
_ = require('underscore'); | ||
function createTest(description, requestProperties, expectedRewrittenUrl) { | ||
function createTest(description, requestProperties, expectedRewrittenUrl, expressNegotiatorOptions) { | ||
var req = new http.IncomingMessage(); | ||
@@ -20,3 +17,6 @@ _.extend(req, requestProperties); | ||
it('should be rewritten to ' + expectedRewrittenUrl, function (done) { | ||
expressNegotiator(req, new http.OutgoingMessage(), function (err) { | ||
expressNegotiator(_.extend({ | ||
root: path.resolve(__dirname, 'root'), | ||
cookieName: 'locale' | ||
}, expressNegotiatorOptions))(req, new http.OutgoingMessage(), function (err) { | ||
expect(req.url).to.eql(expectedRewrittenUrl); | ||
@@ -76,2 +76,4 @@ done(); | ||
createTest('/thething with Accept: */html', {url: '/thething', headers: {accept: '*/html'}}, '/thething.html'); | ||
createTest('/ Accept: */html, a mobile user agent and userAgent:true', {url: '/', headers: {accept: '*/html', 'user-agent': 'Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5"'}}, '/index.mobile.en_US.html', {userAgent: true}); | ||
createTest('/ Accept: */html, a mobile user agent and userAgent:false', {url: '/', headers: {accept: '*/html', 'user-agent': 'Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5"'}}, '/index.en_US.html', {userAgent: false}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
29450
27
348
1
7