From 179c11bf4ce7ab6441ccb86f54bcfc2dd1a10036 Mon Sep 17 00:00:00 2001 From: delvedor Date: Wed, 23 Aug 2017 20:07:35 +0200 Subject: [PATCH 1/6] Keep the context for the run callback --- middie.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/middie.js b/middie.js index 29c75f8..794db00 100644 --- a/middie.js +++ b/middie.js @@ -26,7 +26,7 @@ function middie (complete) { function run (req, res) { if (!middlewares.length) { - complete(null, req, res) + complete.call(this, null, req, res) return } @@ -34,6 +34,7 @@ function middie (complete) { holder.req = req holder.res = res holder.url = sanitizeUrl(req.url) + holder.context = this holder.done() } @@ -42,6 +43,7 @@ function middie (complete) { this.req = null this.res = null this.url = null + this.context = null this.i = 0 var that = this @@ -49,12 +51,14 @@ function middie (complete) { const req = that.req const res = that.res const url = that.url + const context = that.context const i = that.i++ if (err || middlewares.length === i) { - complete(err, req, res) + complete.call(context, err, req, res) that.req = null that.res = null + that.context = null that.i = 0 pool.release(that) } else { From e7389629665c90c21b2e571a649b8e22599855a9 Mon Sep 17 00:00:00 2001 From: delvedor Date: Wed, 23 Aug 2017 20:07:47 +0200 Subject: [PATCH 2/6] Updated test --- test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test.js b/test.js index 3291980..962b96b 100644 --- a/test.js +++ b/test.js @@ -205,3 +205,25 @@ test('Should strip the url to only match the pathname', t => { instance.run(req, res) }) + +test('should keep the context', t => { + t.plan(6) + + const instance = middie(function (err, a, b) { + t.error(err) + t.equal(a, req) + t.equal(b, res) + t.ok(this.key) + }) + const req = { + url: '/test' + } + const res = {} + + t.equal(instance.use(function (req, res, next) { + t.pass('function called') + next() + }), instance) + + instance.run.call({ key: true }, req, res) +}) From 62d06d5fccf26fad3b25ebca6191d9107c9d8c24 Mon Sep 17 00:00:00 2001 From: delvedor Date: Wed, 23 Aug 2017 20:07:57 +0200 Subject: [PATCH 3/6] Updated README.md --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 570f784..6cc80b0 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,26 @@ function _runMiddlewares (err, req, res) { // => routing function } ``` + +#### Keep the context +If you need it you can also keep the context of the calling function by calling `run` with `run.call(this, req, res)`, in this way you can avoid closures allocation. + +```js +http + .createServer(function handler (req, res) { + middie.run.call({ context: 'object' }, req, res) + }) + .listen(3000) + +function _runMiddlewares (err, req, res) { + if (err) { + console.log(err) + res.end(err) + return + } + console.log(this.context) +} +``` #### Restrict middleware execution to a certain path(s) From a5a5f8232991a23f6aafdc7ee2704679054afb98 Mon Sep 17 00:00:00 2001 From: delvedor Date: Thu, 24 Aug 2017 10:04:46 +0200 Subject: [PATCH 4/6] Pass the context as parameter --- middie.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/middie.js b/middie.js index 794db00..b112ac1 100644 --- a/middie.js +++ b/middie.js @@ -24,9 +24,9 @@ function middie (complete) { return this } - function run (req, res) { + function run (req, res, ctx) { if (!middlewares.length) { - complete.call(this, null, req, res) + complete(null, req, res, ctx) return } @@ -34,7 +34,7 @@ function middie (complete) { holder.req = req holder.res = res holder.url = sanitizeUrl(req.url) - holder.context = this + holder.context = ctx holder.done() } @@ -55,7 +55,7 @@ function middie (complete) { const i = that.i++ if (err || middlewares.length === i) { - complete.call(context, err, req, res) + complete(err, req, res, context) that.req = null that.res = null that.context = null From b13f6072dddd80e43ae32de93356ab15ac6818de Mon Sep 17 00:00:00 2001 From: delvedor Date: Thu, 24 Aug 2017 10:04:55 +0200 Subject: [PATCH 5/6] Updated test --- test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test.js b/test.js index 962b96b..ee730bf 100644 --- a/test.js +++ b/test.js @@ -209,11 +209,11 @@ test('Should strip the url to only match the pathname', t => { test('should keep the context', t => { t.plan(6) - const instance = middie(function (err, a, b) { + const instance = middie(function (err, a, b, ctx) { t.error(err) t.equal(a, req) t.equal(b, res) - t.ok(this.key) + t.ok(ctx.key) }) const req = { url: '/test' @@ -225,5 +225,5 @@ test('should keep the context', t => { next() }), instance) - instance.run.call({ key: true }, req, res) + instance.run(req, res, { key: true }) }) From 456c7390c0020a628693d3ddf244ce88eb81b641 Mon Sep 17 00:00:00 2001 From: delvedor Date: Thu, 24 Aug 2017 10:05:00 +0200 Subject: [PATCH 6/6] Updated README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6cc80b0..3bd63ca 100644 --- a/README.md +++ b/README.md @@ -44,22 +44,22 @@ function _runMiddlewares (err, req, res) { ``` #### Keep the context -If you need it you can also keep the context of the calling function by calling `run` with `run.call(this, req, res)`, in this way you can avoid closures allocation. +If you need it you can also keep the context of the calling function by calling `run` with `run(req, res, this)`, in this way you can avoid closures allocation. ```js http .createServer(function handler (req, res) { - middie.run.call({ context: 'object' }, req, res) + middie.run(req, res, { context: 'object' }) }) .listen(3000) -function _runMiddlewares (err, req, res) { +function _runMiddlewares (err, req, res, ctx) { if (err) { console.log(err) res.end(err) return } - console.log(this.context) + console.log(ctx) } ```