Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match start with path (*) #6

Merged
merged 5 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions middie.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const reusify = require('reusify')
const pathMatch = require('pathname-match')

function middie (complete) {
var functions = []
var urls = []
var hasMiddlewares = false
var middlewares = []
var pool = reusify(Holder)

return {
Expand All @@ -19,14 +17,16 @@ function middie (complete) {
f = url
url = null
}
hasMiddlewares = true
functions.push(f)
urls.push(url)
middlewares.push({
path: url,
fn: f,
wildcard: hasWildcard(url)
})
return this
}

function run (req, res) {
if (!hasMiddlewares) {
if (!middlewares.length) {
complete(null, req, res)
return
}
Expand All @@ -52,17 +52,21 @@ function middie (complete) {
const url = that.url
const i = that.i++

if (err || functions.length === i) {
if (err || middlewares.length === i) {
complete(err, req, res)
that.req = null
that.res = null
that.i = 0
pool.release(that)
} else {
if (!urls[i]) {
functions[i](req, res, that.done)
} else if (urls[i] && (urls[i] === url || urls[i].indexOf(url) > -1)) {
functions[i](req, res, that.done)
const middleware = middlewares[i]
const fn = middleware.fn
if (!middleware.path) {
fn(req, res, that.done)
} else if (middleware.wildcard && pathMatchWildcard(url, middleware.path)) {
fn(req, res, that.done)
} else if (middleware.path === url || (typeof middleware.path !== 'string' && middleware.path.indexOf(url) > -1)) {
fn(req, res, that.done)
} else {
that.done()
}
Expand All @@ -71,4 +75,22 @@ function middie (complete) {
}
}

function hasWildcard (url) {
return typeof url === 'string' && url.length > 2 && url.charCodeAt(url.length - 1) === 42 /* * */ && url.charCodeAt(url.length - 2) === 47 /* / */
}

function pathMatchWildcard (url, wildcardUrl) {
if (url.length < wildcardUrl.length) {
return false
}

for (var i = 0; i < wildcardUrl.length - 2; i++) {
if (url.charCodeAt(i) !== wildcardUrl.charCodeAt(i)) {
return false
}
}

return true
}

module.exports = middie
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,38 @@ test('run restricted by path', t => {
instance.run(req, res)
})

test('run restricted by path with * at the end', t => {
t.plan(8)

const instance = middie(function (err, a, b) {
t.error(err)
t.equal(a, req)
t.equal('/test/aaa/bbb', req.url)
t.equal(b, res)
})
const req = {
url: '/test/aaa/bbb'
}
const res = {}

t.equal(instance.use('/test/*', function (req, res, next) {
t.ok('function called')
next()
}), instance)

t.equal(instance.use('/test/aaa/bbb/*', function (req, res, next) {
t.fail('should not call this function')
next()
}), instance)

t.equal(instance.use('/test/aaa*', function (req, res, next) {
t.fail('should not call this function')
next()
}), instance)

instance.run(req, res)
})

test('run restricted by array path', t => {
t.plan(9)

Expand Down