Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
use independent thunks
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 3, 2016
1 parent 1a17e86 commit 3336f5f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
Expand Down
16 changes: 6 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// **License:** MIT

var path = require('path')
var thunk = require('thunks')()
var methods = require('methods')
var Trie = require('route-trie')

Expand Down Expand Up @@ -46,10 +47,7 @@ Router.prototype.toThunk = function () {
Router.prototype.route = function (context) {
var state = this._routerState

// back-compat
if (!context.thunk) context.thunk = arguments[1]

return context.thunk(function (done) {
return thunk.call(context, function (done) {
var normalPath = path.normalize(this.path).replace(/\\/g, '/')
var method = this.method

Expand All @@ -59,7 +57,7 @@ Router.prototype.route = function (context) {

var matched = state.trie.match(normalPath)
if (!matched) {
if (state.otherwise) return this.thunk(state.otherwise.call(this))(done)
if (state.otherwise) return thunk.call(this, state.otherwise.call(this))(done)
this.throw(501, '"' + this.path + '" is not implemented.')
}

Expand All @@ -78,13 +76,13 @@ Router.prototype.route = function (context) {
// If no route handler is returned
// it's a 405 error
if (!handler) {
if (state.otherwise) return this.thunk(state.otherwise.call(this))(done)
if (state.otherwise) return thunk.call(this, state.otherwise.call(this))(done)
this.set('Allow', matched.node.allowMethods)
this.throw(405, this.method + ' is not allowed in "' + this.path + '".')
}

this.params = this.request.params = matched.params
return this.thunk(handler.call(this))(done)
return thunk.call(this, handler.call(this))(done)
})
}

Expand All @@ -97,14 +95,12 @@ function Route (router, pattern) {
})
}

methods.map(function (method) {
methods.forEach(function (method) {
Router.prototype[method] = function (pattern, handler) {
defineHandler(this._routerState.trie.define(pattern), method, handler)
return this
}
})

methods.map(function (method) {
Route.prototype[method] = function (handler) {
defineHandler(this._node, method, handler)
return this
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"authors": [
"Yan Qing <[email protected]>"
],
"version": "1.3.3",
"version": "1.4.0",
"main": "index.js",
"repository": {
"type": "git",
"url": "[email protected]:toajs/toa-router.git"
},
"engines": {
"node": ">=0.10.0"
"node": ">=0.12.0"
},
"homepage": "https://github.com/toajs/toa-router",
"keywords": [
Expand All @@ -24,13 +24,14 @@
],
"dependencies": {
"methods": "^1.1.2",
"route-trie": "^1.2.6"
"route-trie": "^1.2.6",
"thunks": "^4.4.2"
},
"devDependencies": {
"standard": "^7.1.0",
"supertest": "^1.2.0",
"tman": "^0.9.6",
"toa": "^1.4.3"
"standard": "^7.1.2",
"supertest": "^2.0.0",
"tman": "^1.0.4",
"toa": "^1.8.2"
},
"scripts": {
"test": "standard && tman"
Expand Down
30 changes: 15 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var assert = require('assert')
var request = require('supertest')
var Toa = require('toa')
var toa = require('toa')
var tman = require('tman')
var Router = require('..')

Expand All @@ -14,7 +14,7 @@ tman.suite('toa-router', function () {
this.body = 'OK'
})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -30,7 +30,7 @@ tman.suite('toa-router', function () {
this.body = '/' + this.params.type + '/' + this.params.id
})

var app = Toa(function () {
var app = toa(function () {
return router
})

Expand All @@ -50,7 +50,7 @@ tman.suite('toa-router', function () {
this.body = 'POST /' + this.params.type + '/' + this.params.id
})

var app = Toa()
var app = toa()
app.use(router.toThunk())

return request(app.listen())
Expand All @@ -65,7 +65,7 @@ tman.suite('toa-router', function () {
this.status = 200
})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -80,7 +80,7 @@ tman.suite('toa-router', function () {
this.status = 200
})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -97,7 +97,7 @@ tman.suite('toa-router', function () {
.put('/:type/:id', function () {})
.del('/:type/:id', function () {})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -113,7 +113,7 @@ tman.suite('toa-router', function () {
var router = new Router()
router.get('/', function () {})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -128,7 +128,7 @@ tman.suite('toa-router', function () {
.get('/:type/:id', function () {})
.post('/:type/:id', function () {})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -145,7 +145,7 @@ tman.suite('toa-router', function () {
this.body = 'otherwise'
})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -163,7 +163,7 @@ tman.suite('toa-router', function () {
this.body = 'otherwise'
})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -181,7 +181,7 @@ tman.suite('toa-router', function () {
.put(function () {})
.del(function () {})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -201,7 +201,7 @@ tman.suite('toa-router', function () {
.put(function () {})
.del(function () {})

var app = Toa(function () {
var app = toa(function () {
return router.route(this)
})

Expand All @@ -225,7 +225,7 @@ tman.suite('toa-router', function () {
this.body = 'api'
})

var app = Toa(function () {
var app = toa(function () {
return this.thunk.all(router2.route(this), router1.route(this))
})

Expand All @@ -250,7 +250,7 @@ tman.suite('toa-router', function () {
}
})

var app = Toa(function () {
var app = toa(function () {
return this.thunk.all(router2.route(this), router1.route(this))
})

Expand Down

0 comments on commit 3336f5f

Please sign in to comment.