Skip to content

Commit 12565d1

Browse files
committed
lint: use standard style
1 parent 9e0176c commit 12565d1

8 files changed

Lines changed: 79 additions & 63 deletions

File tree

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage
2+
node_modules

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "standard"
3+
}

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cache:
1515
- node_modules
1616
before_install:
1717
# Setup Node.js version-specific dependencies
18-
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
18+
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard istanbul"
1919

2020
# Update Node.js modules
2121
- "test ! -d node_modules || npm prune"
@@ -24,5 +24,6 @@ script:
2424
# Run test script, depending on istanbul install
2525
- "test ! -z $(npm -ps ls istanbul) || npm test"
2626
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
27+
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
2728
after_script:
2829
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ var mainapp = connect()
8989
// create app that will server user content from public/{username}/
9090
var userapp = connect()
9191

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

9595
// pretend request was for /{username}/* for file serving

index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ module.exports = vhost
1919
* @private
2020
*/
2121

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

2828
/**
2929
* Create a vhost middleware.
@@ -34,7 +34,7 @@ var escapeReplace = '\\$1'
3434
* @public
3535
*/
3636

37-
function vhost(hostname, handle) {
37+
function vhost (hostname, handle) {
3838
if (!hostname) {
3939
throw new TypeError('argument hostname is required')
4040
}
@@ -50,7 +50,7 @@ function vhost(hostname, handle) {
5050
// create regular expression for hostname
5151
var regexp = hostregexp(hostname)
5252

53-
return function vhost(req, res, next) {
53+
return function vhost (req, res, next) {
5454
var vhostdata = vhostof(req, regexp)
5555

5656
if (!vhostdata) {
@@ -73,7 +73,7 @@ function vhost(hostname, handle) {
7373
* @private
7474
*/
7575

76-
function hostnameof(req) {
76+
function hostnameof (req) {
7777
var host = req.headers.host
7878

7979
if (!host) {
@@ -98,7 +98,7 @@ function hostnameof(req) {
9898
* @private
9999
*/
100100

101-
function isregexp(val) {
101+
function isregexp (val) {
102102
return Object.prototype.toString.call(val) === '[object RegExp]'
103103
}
104104

@@ -109,9 +109,9 @@ function isregexp(val) {
109109
* @private
110110
*/
111111

112-
function hostregexp(val) {
112+
function hostregexp (val) {
113113
var source = !isregexp(val)
114-
? String(val).replace(escapeRegExp, escapeReplace).replace(asteriskRegExp, asteriskReplace)
114+
? String(val).replace(ESCAPE_REGEXP, ESCAPE_REPLACE).replace(ASTERISK_REGEXP, ASTERISK_REPLACE)
115115
: val.source
116116

117117
// force leading anchor matching
@@ -120,7 +120,7 @@ function hostregexp(val) {
120120
}
121121

122122
// force trailing anchor matching
123-
if (!endAnchoredRegExp.test(source)) {
123+
if (!END_ANCHORED_REGEXP.test(source)) {
124124
source += '$'
125125
}
126126

@@ -136,7 +136,7 @@ function hostregexp(val) {
136136
* @private
137137
*/
138138

139-
function vhostof(req, regexp) {
139+
function vhostof (req, regexp) {
140140
var host = req.headers.host
141141
var hostname = hostnameof(req)
142142

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"license": "MIT",
1010
"repository": "expressjs/vhost",
1111
"devDependencies": {
12+
"eslint": "2.11.1",
13+
"eslint-config-standard": "5.3.1",
14+
"eslint-plugin-promise": "1.3.1",
15+
"eslint-plugin-standard": "1.3.2",
1216
"istanbul": "0.4.3",
1317
"mocha": "2.5.3",
1418
"supertest": "1.1.0"
@@ -22,6 +26,7 @@
2226
"node": ">= 0.8.0"
2327
},
2428
"scripts": {
29+
"lint": "eslint **/*.js",
2530
"test": "mocha --reporter spec --bail --check-leaks test/",
2631
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
2732
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"

test/.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"env": {
3+
"mocha": true
4+
}
5+
}

test/test.js

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ var http = require('http')
44
var request = require('supertest')
55
var vhost = require('..')
66

7-
describe('vhost(hostname, server)', function(){
8-
it('should route by Host', function(done){
7+
describe('vhost(hostname, server)', function () {
8+
it('should route by Host', function (done) {
99
var vhosts = []
1010

1111
vhosts.push(vhost('tobi.com', tobi))
1212
vhosts.push(vhost('loki.com', loki))
1313

1414
var app = createServer(vhosts)
1515

16-
function tobi(req, res) { res.end('tobi') }
17-
function loki(req, res) { res.end('loki') }
16+
function tobi (req, res) { res.end('tobi') }
17+
function loki (req, res) { res.end('loki') }
1818

1919
request(app)
2020
.get('/')
2121
.set('Host', 'tobi.com')
2222
.expect(200, 'tobi', done)
2323
})
2424

25-
it('should ignore port in Host', function(done){
25+
it('should ignore port in Host', function (done) {
2626
var app = createServer('tobi.com', function (req, res) {
2727
res.end('tobi')
2828
})
@@ -33,7 +33,7 @@ describe('vhost(hostname, server)', function(){
3333
.expect(200, 'tobi', done)
3434
})
3535

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

47-
it('should 404 unless matched', function(done){
47+
it('should 404 unless matched', function (done) {
4848
var vhosts = []
4949

5050
vhosts.push(vhost('tobi.com', tobi))
5151
vhosts.push(vhost('loki.com', loki))
5252

5353
var app = createServer(vhosts)
5454

55-
function tobi(req, res) { res.end('tobi') }
56-
function loki(req, res) { res.end('loki') }
55+
function tobi (req, res) { res.end('tobi') }
56+
function loki (req, res) { res.end('loki') }
5757

5858
request(app.listen())
5959
.get('/')
6060
.set('Host', 'ferrets.com')
6161
.expect(404, done)
6262
})
6363

64-
it('should 404 without Host header', function(done){
64+
it('should 404 without Host header', function (done) {
6565
var vhosts = []
6666

6767
vhosts.push(vhost('tobi.com', tobi))
6868
vhosts.push(vhost('loki.com', loki))
6969

7070
var app = createServer(vhosts)
7171

72-
function tobi(req, res) { res.end('tobi') }
73-
function loki(req, res) { res.end('loki') }
72+
function tobi (req, res) { res.end('tobi') }
73+
function loki (req, res) { res.end('loki') }
7474

7575
request(app.listen())
7676
.get('/')
7777
.unset('Host')
7878
.expect(404, done)
7979
})
8080

81-
describe('arguments', function(){
82-
describe('hostname', function(){
83-
it('should be required', function(){
81+
describe('arguments', function () {
82+
describe('hostname', function () {
83+
it('should be required', function () {
8484
assert.throws(vhost.bind(), /hostname.*required/)
8585
})
8686

87-
it('should accept string', function(){
88-
assert.doesNotThrow(vhost.bind(null, 'loki.com', function(){}))
87+
it('should accept string', function () {
88+
assert.doesNotThrow(vhost.bind(null, 'loki.com', function () {}))
8989
})
9090

91-
it('should accept RegExp', function(){
92-
assert.doesNotThrow(vhost.bind(null, /loki\.com/, function(){}))
91+
it('should accept RegExp', function () {
92+
assert.doesNotThrow(vhost.bind(null, /loki\.com/, function () {}))
9393
})
9494
})
9595

96-
describe('handle', function(){
97-
it('should be required', function(){
96+
describe('handle', function () {
97+
it('should be required', function () {
9898
assert.throws(vhost.bind(null, 'loki.com'), /handle.*required/)
9999
})
100100

101-
it('should accept function', function(){
102-
assert.doesNotThrow(vhost.bind(null, 'loki.com', function(){}))
101+
it('should accept function', function () {
102+
assert.doesNotThrow(vhost.bind(null, 'loki.com', function () {}))
103103
})
104104

105-
it('should reject plain object', function(){
105+
it('should reject plain object', function () {
106106
assert.throws(vhost.bind(null, 'loki.com', {}), /handle.*function/)
107107
})
108108
})
109109
})
110110

111-
describe('with string hostname', function(){
112-
it('should support wildcards', function(done){
113-
var app = createServer('*.ferrets.com', function(req, res){
111+
describe('with string hostname', function () {
112+
it('should support wildcards', function (done) {
113+
var app = createServer('*.ferrets.com', function (req, res) {
114114
res.end('wildcard!')
115115
})
116116

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

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

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

134-
it('should treat dot as a dot', function(done){
135-
var app = createServer('a.b.com', function(req, res){
134+
it('should treat dot as a dot', function (done) {
135+
var app = createServer('a.b.com', function (req, res) {
136136
res.end('tobi')
137137
})
138138

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

145-
it('should match entire string', function(done){
146-
var app = createServer('.com', function(req, res){
145+
it('should match entire string', function (done) {
146+
var app = createServer('.com', function (req, res) {
147147
res.end('commercial')
148148
})
149149

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

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

@@ -167,9 +167,9 @@ describe('vhost(hostname, server)', function(){
167167
})
168168
})
169169

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

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

182-
it('should match entire hostname', function(done){
182+
it('should match entire hostname', function (done) {
183183
var vhosts = []
184184

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

188188
var app = createServer(vhosts)
189189

190-
function tobi(req, res) { res.end('tobi') }
191-
function loki(req, res) { res.end('loki') }
190+
function tobi (req, res) { res.end('tobi') }
191+
function loki (req, res) { res.end('loki') }
192192

193193
request(app)
194194
.get('/')
195195
.set('Host', 'loki.tobi.com')
196196
.expect(404, done)
197197
})
198198

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

@@ -211,15 +211,15 @@ describe('vhost(hostname, server)', function(){
211211
})
212212
})
213213

214-
function createServer(hostname, server) {
214+
function createServer (hostname, server) {
215215
var vhosts = !Array.isArray(hostname)
216216
? [vhost(hostname, server)]
217217
: hostname
218218

219-
return http.createServer(function onRequest(req, res) {
219+
return http.createServer(function onRequest (req, res) {
220220
var index = 0
221221

222-
function next(err) {
222+
function next (err) {
223223
var vhost = vhosts[index++]
224224

225225
if (!vhost || err) {

0 commit comments

Comments
 (0)