Skip to content

Commit

Permalink
lint: use standard style
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 31, 2016
1 parent 9e0176c commit 12565d1
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cache:
- node_modules
before_install:
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard istanbul"

# Update Node.js modules
- "test ! -d node_modules || npm prune"
Expand All @@ -24,5 +24,6 @@ script:
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var mainapp = connect()
// create app that will server user content from public/{username}/
var userapp = connect()

userapp.use(function(req, res, next){
userapp.use(function (req, res, next) {
var username = req.vhost[0] // username is the "*"

// pretend request was for /{username}/* for file serving
Expand Down
26 changes: 13 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ module.exports = vhost
* @private
*/

var asteriskRegExp = /\*/g
var asteriskReplace = '([^\.]+)'
var endAnchoredRegExp = /(?:^|[^\\])(?:\\\\)*\$$/
var escapeRegExp = /([.+?^=!:${}()|\[\]\/\\])/g
var escapeReplace = '\\$1'
var ASTERISK_REGEXP = /\*/g
var ASTERISK_REPLACE = '([^.]+)'
var END_ANCHORED_REGEXP = /(?:^|[^\\])(?:\\\\)*\$$/
var ESCAPE_REGEXP = /([.+?^=!:${}()|\[\]\/\\])/g
var ESCAPE_REPLACE = '\\$1'

/**
* Create a vhost middleware.
Expand All @@ -34,7 +34,7 @@ var escapeReplace = '\\$1'
* @public
*/

function vhost(hostname, handle) {
function vhost (hostname, handle) {
if (!hostname) {
throw new TypeError('argument hostname is required')
}
Expand All @@ -50,7 +50,7 @@ function vhost(hostname, handle) {
// create regular expression for hostname
var regexp = hostregexp(hostname)

return function vhost(req, res, next) {
return function vhost (req, res, next) {
var vhostdata = vhostof(req, regexp)

if (!vhostdata) {
Expand All @@ -73,7 +73,7 @@ function vhost(hostname, handle) {
* @private
*/

function hostnameof(req) {
function hostnameof (req) {
var host = req.headers.host

if (!host) {
Expand All @@ -98,7 +98,7 @@ function hostnameof(req) {
* @private
*/

function isregexp(val) {
function isregexp (val) {
return Object.prototype.toString.call(val) === '[object RegExp]'
}

Expand All @@ -109,9 +109,9 @@ function isregexp(val) {
* @private
*/

function hostregexp(val) {
function hostregexp (val) {
var source = !isregexp(val)
? String(val).replace(escapeRegExp, escapeReplace).replace(asteriskRegExp, asteriskReplace)
? String(val).replace(ESCAPE_REGEXP, ESCAPE_REPLACE).replace(ASTERISK_REGEXP, ASTERISK_REPLACE)
: val.source

// force leading anchor matching
Expand All @@ -120,7 +120,7 @@ function hostregexp(val) {
}

// force trailing anchor matching
if (!endAnchoredRegExp.test(source)) {
if (!END_ANCHORED_REGEXP.test(source)) {
source += '$'
}

Expand All @@ -136,7 +136,7 @@ function hostregexp(val) {
* @private
*/

function vhostof(req, regexp) {
function vhostof (req, regexp) {
var host = req.headers.host
var hostname = hostnameof(req)

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"license": "MIT",
"repository": "expressjs/vhost",
"devDependencies": {
"eslint": "2.11.1",
"eslint-config-standard": "5.3.1",
"eslint-plugin-promise": "1.3.1",
"eslint-plugin-standard": "1.3.2",
"istanbul": "0.4.3",
"mocha": "2.5.3",
"supertest": "1.1.0"
Expand All @@ -22,6 +26,7 @@
"node": ">= 0.8.0"
},
"scripts": {
"lint": "eslint **/*.js",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}
96 changes: 48 additions & 48 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ var http = require('http')
var request = require('supertest')
var vhost = require('..')

describe('vhost(hostname, server)', function(){
it('should route by Host', function(done){
describe('vhost(hostname, server)', function () {
it('should route by Host', function (done) {
var vhosts = []

vhosts.push(vhost('tobi.com', tobi))
vhosts.push(vhost('loki.com', loki))

var app = createServer(vhosts)

function tobi(req, res) { res.end('tobi') }
function loki(req, res) { res.end('loki') }
function tobi (req, res) { res.end('tobi') }
function loki (req, res) { res.end('loki') }

request(app)
.get('/')
.set('Host', 'tobi.com')
.expect(200, 'tobi', done)
})

it('should ignore port in Host', function(done){
it('should ignore port in Host', function (done) {
var app = createServer('tobi.com', function (req, res) {
res.end('tobi')
})
Expand All @@ -33,7 +33,7 @@ describe('vhost(hostname, server)', function(){
.expect(200, 'tobi', done)
})

it('should support IPv6 literal in Host', function(done){
it('should support IPv6 literal in Host', function (done) {
var app = createServer('[::1]', function (req, res) {
res.end('loopback')
})
Expand All @@ -44,73 +44,73 @@ describe('vhost(hostname, server)', function(){
.expect(200, 'loopback', done)
})

it('should 404 unless matched', function(done){
it('should 404 unless matched', function (done) {
var vhosts = []

vhosts.push(vhost('tobi.com', tobi))
vhosts.push(vhost('loki.com', loki))

var app = createServer(vhosts)

function tobi(req, res) { res.end('tobi') }
function loki(req, res) { res.end('loki') }
function tobi (req, res) { res.end('tobi') }
function loki (req, res) { res.end('loki') }

request(app.listen())
.get('/')
.set('Host', 'ferrets.com')
.expect(404, done)
})

it('should 404 without Host header', function(done){
it('should 404 without Host header', function (done) {
var vhosts = []

vhosts.push(vhost('tobi.com', tobi))
vhosts.push(vhost('loki.com', loki))

var app = createServer(vhosts)

function tobi(req, res) { res.end('tobi') }
function loki(req, res) { res.end('loki') }
function tobi (req, res) { res.end('tobi') }
function loki (req, res) { res.end('loki') }

request(app.listen())
.get('/')
.unset('Host')
.expect(404, done)
})

describe('arguments', function(){
describe('hostname', function(){
it('should be required', function(){
describe('arguments', function () {
describe('hostname', function () {
it('should be required', function () {
assert.throws(vhost.bind(), /hostname.*required/)
})

it('should accept string', function(){
assert.doesNotThrow(vhost.bind(null, 'loki.com', function(){}))
it('should accept string', function () {
assert.doesNotThrow(vhost.bind(null, 'loki.com', function () {}))
})

it('should accept RegExp', function(){
assert.doesNotThrow(vhost.bind(null, /loki\.com/, function(){}))
it('should accept RegExp', function () {
assert.doesNotThrow(vhost.bind(null, /loki\.com/, function () {}))
})
})

describe('handle', function(){
it('should be required', function(){
describe('handle', function () {
it('should be required', function () {
assert.throws(vhost.bind(null, 'loki.com'), /handle.*required/)
})

it('should accept function', function(){
assert.doesNotThrow(vhost.bind(null, 'loki.com', function(){}))
it('should accept function', function () {
assert.doesNotThrow(vhost.bind(null, 'loki.com', function () {}))
})

it('should reject plain object', function(){
it('should reject plain object', function () {
assert.throws(vhost.bind(null, 'loki.com', {}), /handle.*function/)
})
})
})

describe('with string hostname', function(){
it('should support wildcards', function(done){
var app = createServer('*.ferrets.com', function(req, res){
describe('with string hostname', function () {
it('should support wildcards', function (done) {
var app = createServer('*.ferrets.com', function (req, res) {
res.end('wildcard!')
})

Expand All @@ -120,8 +120,8 @@ describe('vhost(hostname, server)', function(){
.expect(200, 'wildcard!', done)
})

it('should restrict wildcards to single part', function(done){
var app = createServer('*.ferrets.com', function(req, res){
it('should restrict wildcards to single part', function (done) {
var app = createServer('*.ferrets.com', function (req, res) {
res.end('wildcard!')
})

Expand All @@ -131,8 +131,8 @@ describe('vhost(hostname, server)', function(){
.expect(404, done)
})

it('should treat dot as a dot', function(done){
var app = createServer('a.b.com', function(req, res){
it('should treat dot as a dot', function (done) {
var app = createServer('a.b.com', function (req, res) {
res.end('tobi')
})

Expand All @@ -142,8 +142,8 @@ describe('vhost(hostname, server)', function(){
.expect(404, done)
})

it('should match entire string', function(done){
var app = createServer('.com', function(req, res){
it('should match entire string', function (done) {
var app = createServer('.com', function (req, res) {
res.end('commercial')
})

Expand All @@ -153,10 +153,10 @@ describe('vhost(hostname, server)', function(){
.expect(404, done)
})

it('should populate req.vhost', function(done){
var app = createServer('user-*.*.com', function(req, res){
it('should populate req.vhost', function (done) {
var app = createServer('user-*.*.com', function (req, res) {
var keys = Object.keys(req.vhost).sort()
var arr = keys.map(function(k){ return [k, req.vhost[k]] })
var arr = keys.map(function (k) { return [k, req.vhost[k]] })
res.end(JSON.stringify(arr))
})

Expand All @@ -167,9 +167,9 @@ describe('vhost(hostname, server)', function(){
})
})

describe('with RegExp hostname', function(){
it('should match using RegExp', function(done){
var app = createServer(/[tl]o[bk]i\.com/, function(req, res){
describe('with RegExp hostname', function () {
it('should match using RegExp', function (done) {
var app = createServer(/[tl]o[bk]i\.com/, function (req, res) {
res.end('tobi')
})

Expand All @@ -179,27 +179,27 @@ describe('vhost(hostname, server)', function(){
.expect(200, 'tobi', done)
})

it('should match entire hostname', function(done){
it('should match entire hostname', function (done) {
var vhosts = []

vhosts.push(vhost(/\.tobi$/, tobi))
vhosts.push(vhost(/^loki\./, loki))

var app = createServer(vhosts)

function tobi(req, res) { res.end('tobi') }
function loki(req, res) { res.end('loki') }
function tobi (req, res) { res.end('tobi') }
function loki (req, res) { res.end('loki') }

request(app)
.get('/')
.set('Host', 'loki.tobi.com')
.expect(404, done)
})

it('should populate req.vhost', function(done){
var app = createServer(/user-(bob|joe)\.([^\.]+)\.com/, function(req, res){
it('should populate req.vhost', function (done) {
var app = createServer(/user-(bob|joe)\.([^\.]+)\.com/, function (req, res) {
var keys = Object.keys(req.vhost).sort()
var arr = keys.map(function(k){ return [k, req.vhost[k]] })
var arr = keys.map(function (k) { return [k, req.vhost[k]] })
res.end(JSON.stringify(arr))
})

Expand All @@ -211,15 +211,15 @@ describe('vhost(hostname, server)', function(){
})
})

function createServer(hostname, server) {
function createServer (hostname, server) {
var vhosts = !Array.isArray(hostname)
? [vhost(hostname, server)]
: hostname

return http.createServer(function onRequest(req, res) {
return http.createServer(function onRequest (req, res) {
var index = 0

function next(err) {
function next (err) {
var vhost = vhosts[index++]

if (!vhost || err) {
Expand Down

0 comments on commit 12565d1

Please sign in to comment.