From f089c4f4aca32f01df5a58d06ac2f16ea9204fee Mon Sep 17 00:00:00 2001 From: Billouboq Date: Mon, 15 May 2017 23:41:05 +0200 Subject: [PATCH 1/5] implement /* path --- middie.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/middie.js b/middie.js index 0907e7d..b9b51ac 100644 --- a/middie.js +++ b/middie.js @@ -2,6 +2,7 @@ const reusify = require('reusify') const pathMatch = require('pathname-match') +const endOfPathAsterixRegex = /\/\*$/ function middie (complete) { var functions = [] @@ -61,10 +62,16 @@ function middie (complete) { } 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) } else { - that.done() + if (urls[i] === url) { + functions[i](req, res, that.done) + } else if (typeof urls[i] !== 'string' && urls[i].indexOf(url) > -1) { + functions[i](req, res, that.done) + } else if (endOfPathAsterixRegex.test(urls[i]) && url.indexOf(urls[i].slice(0, -1)) > -1) { + functions[i](req, res, that.done) + } else { + that.done() + } } } } From 5cc704ee885eeab6e50eedaf7dd822433f36df75 Mon Sep 17 00:00:00 2001 From: Billouboq Date: Mon, 15 May 2017 23:41:31 +0200 Subject: [PATCH 2/5] add test --- test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test.js b/test.js index 637499d..3291980 100644 --- a/test.js +++ b/test.js @@ -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) From bfe69c41f8dc0d5da582caa2976dc99f09559675 Mon Sep 17 00:00:00 2001 From: Billouboq Date: Tue, 16 May 2017 23:51:00 +0200 Subject: [PATCH 3/5] store url in litteral and change wildcard check --- middie.js | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/middie.js b/middie.js index b9b51ac..4871716 100644 --- a/middie.js +++ b/middie.js @@ -2,10 +2,8 @@ const reusify = require('reusify') const pathMatch = require('pathname-match') -const endOfPathAsterixRegex = /\/\*$/ function middie (complete) { - var functions = [] var urls = [] var hasMiddlewares = false var pool = reusify(Holder) @@ -21,8 +19,11 @@ function middie (complete) { url = null } hasMiddlewares = true - functions.push(f) - urls.push(url) + urls.push({ + path: url, + fn: f, + wildcard: hasWildcard(url) + }) return this } @@ -53,29 +54,43 @@ function middie (complete) { const url = that.url const i = that.i++ - if (err || functions.length === i) { + if (err || urls.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) + if (!urls[i].path) { + urls[i].fn(req, res, that.done) + } else if (urls[i].wildcard && pathMatchWildcard(url, urls[i].path)) { + urls[i].fn(req, res, that.done) + } else if (urls[i].path === url || (typeof urls[i].path !== 'string' && urls[i].path.indexOf(url) > -1)) { + urls[i].fn(req, res, that.done) } else { - if (urls[i] === url) { - functions[i](req, res, that.done) - } else if (typeof urls[i] !== 'string' && urls[i].indexOf(url) > -1) { - functions[i](req, res, that.done) - } else if (endOfPathAsterixRegex.test(urls[i]) && url.indexOf(urls[i].slice(0, -1)) > -1) { - functions[i](req, res, that.done) - } else { - that.done() - } + that.done() } } } } } +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 From 1dab6cdce397c13f481cbf90d6bd73886f5b7a8e Mon Sep 17 00:00:00 2001 From: Billouboq Date: Wed, 17 May 2017 10:59:58 +0200 Subject: [PATCH 4/5] assign url function to a variable --- middie.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/middie.js b/middie.js index 4871716..b19263d 100644 --- a/middie.js +++ b/middie.js @@ -61,12 +61,13 @@ function middie (complete) { that.i = 0 pool.release(that) } else { + const fn = urls[i].fn if (!urls[i].path) { - urls[i].fn(req, res, that.done) + fn(req, res, that.done) } else if (urls[i].wildcard && pathMatchWildcard(url, urls[i].path)) { - urls[i].fn(req, res, that.done) + fn(req, res, that.done) } else if (urls[i].path === url || (typeof urls[i].path !== 'string' && urls[i].path.indexOf(url) > -1)) { - urls[i].fn(req, res, that.done) + fn(req, res, that.done) } else { that.done() } From 07d245ffc488008344e1c70e4e9653da0e55e43b Mon Sep 17 00:00:00 2001 From: Billouboq Date: Wed, 17 May 2017 11:24:59 +0200 Subject: [PATCH 5/5] change variables name and delete hasMiddleware --- middie.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/middie.js b/middie.js index b19263d..227c9f2 100644 --- a/middie.js +++ b/middie.js @@ -4,8 +4,7 @@ const reusify = require('reusify') const pathMatch = require('pathname-match') function middie (complete) { - var urls = [] - var hasMiddlewares = false + var middlewares = [] var pool = reusify(Holder) return { @@ -18,8 +17,7 @@ function middie (complete) { f = url url = null } - hasMiddlewares = true - urls.push({ + middlewares.push({ path: url, fn: f, wildcard: hasWildcard(url) @@ -28,7 +26,7 @@ function middie (complete) { } function run (req, res) { - if (!hasMiddlewares) { + if (!middlewares.length) { complete(null, req, res) return } @@ -54,19 +52,20 @@ function middie (complete) { const url = that.url const i = that.i++ - if (err || urls.length === i) { + if (err || middlewares.length === i) { complete(err, req, res) that.req = null that.res = null that.i = 0 pool.release(that) } else { - const fn = urls[i].fn - if (!urls[i].path) { + const middleware = middlewares[i] + const fn = middleware.fn + if (!middleware.path) { fn(req, res, that.done) - } else if (urls[i].wildcard && pathMatchWildcard(url, urls[i].path)) { + } else if (middleware.wildcard && pathMatchWildcard(url, middleware.path)) { fn(req, res, that.done) - } else if (urls[i].path === url || (typeof urls[i].path !== 'string' && urls[i].path.indexOf(url) > -1)) { + } else if (middleware.path === url || (typeof middleware.path !== 'string' && middleware.path.indexOf(url) > -1)) { fn(req, res, that.done) } else { that.done()