-
-
Notifications
You must be signed in to change notification settings - Fork 103
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
Add promise support #25
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cc43534
Add promise support
calebmer f5a6145
Provide explicit bluebird version
calebmer 3e5be32
Remove pop/concat
calebmer 3a925d6
Remove onFullfiled callback
calebmer 3a21cd3
Add error for falsey values
calebmer 07a227f
Anonymous function naming
calebmer 08551f1
Fix spec inconsisten thisArg
calebmer 1a6f581
Fix spec inconsistent this
calebmer f97b229
Merge branch 'master' of https://github.com/calebmer/router
calebmer 1f312b3
Better tests
calebmer 79a4ee0
Add context verification tests
calebmer a7814d4
Rename test file
calebmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
var Router = require('..') | ||
var utils = require('./support/utils') | ||
var Promise = require('bluebird') | ||
|
||
var assert = utils.assert | ||
var createHitHandle = utils.createHitHandle | ||
var createErrorHitHandle = utils.createErrorHitHandle | ||
var shouldHitHandle = utils.shouldHitHandle | ||
var createServer = utils.createServer | ||
var request = utils.request | ||
|
||
describe('Promise', function () { | ||
it('rejecting will trigger error handlers', function (done) { | ||
var router = Router() | ||
var server = createServer(router) | ||
|
||
router.use(function (req, res) { | ||
return new Promise(function (resolve, reject) { | ||
reject(new Error('Happy error')) | ||
}) | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.expect(500, done) | ||
}) | ||
|
||
it('will be ignored if next is called', function (done) { | ||
var router = Router() | ||
var server = createServer(router) | ||
var timeoutCalled = false | ||
|
||
router.use(createHitHandle(1), function (req, res, next) { | ||
return new Promise(function (resolve, reject) { | ||
next() | ||
setTimeout(function () { | ||
timeoutCalled = true | ||
resolve() | ||
}, 5) | ||
}) | ||
}) | ||
|
||
router.use(createHitHandle(2), function (req, res) { | ||
assert(!timeoutCalled) | ||
res.end('Awesome!') | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.expect(shouldHitHandle(1)) | ||
.expect(shouldHitHandle(2)) | ||
.expect(200, done) | ||
}) | ||
|
||
it('can be used in error handlers', function (done) { | ||
var router = Router() | ||
var server = createServer(router) | ||
|
||
router.use(createHitHandle(1), function (req, res, next) { | ||
next(new Error('Happy error')) | ||
}) | ||
|
||
router.use(createErrorHitHandle(2), function (error, req, res, next) { | ||
return new Promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
next(error) | ||
resolve() | ||
}, 5) | ||
}) | ||
}) | ||
|
||
router.use(function () { | ||
done(new Error('This should never be reached')) | ||
}) | ||
|
||
router.use(createErrorHitHandle(3), function (error, req, res, next) { | ||
res.end('Awesome!') | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.expect(shouldHitHandle(1)) | ||
.expect(shouldHitHandle(2)) | ||
.expect(shouldHitHandle(3)) | ||
.expect(200, done) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// IMPORTANT: Do not include 'use strict' anywhere else in this file | ||
// @see https://github.com/pillarjs/router/pull/25#discussion_r40988513 | ||
|
||
var Router = require('..') | ||
var utils = require('./support/utils') | ||
|
||
var createServer = utils.createServer | ||
var request = utils.request | ||
|
||
describe('this', function () { | ||
describe('when middleware is sloppy', function () { | ||
it('should have global context', function (done) { | ||
var router = new Router() | ||
var server = createServer(router) | ||
|
||
router.get('/', function (req, res) { | ||
res.end(String(this === global)) | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.expect(200, 'true', done) | ||
}) | ||
}) | ||
|
||
describe('when middleware is strict', function () { | ||
it('should have null context', function (done) { | ||
'use strict' | ||
var router = new Router() | ||
var server = createServer(router) | ||
|
||
router.get('/', function (req, res) { | ||
res.end(String(this === global)) | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.expect(200, 'false', done) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prior to this change, the
handle
was called with the default context (global
in sloppy mode andnull
in strict mode). Now this is being changed to always provide a context ofundefined
. This will probably break some middleware. Was there a reason to change the context of the handle call? Can we searchnpm
to determine the swath of modules this change would effect?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dougwilson If I understand, this will still function the same as this is already under strict mode. Also,
call
andapply
actually work the same here (when you passundefined
in non-strict mode it would be global, but this file is strict).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some basic research and
undefined
appeared to be the most canonical thing to use. How would we fix this in the scenario where it might be useful to give some people theglobal
object?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi guys, what determines the context in this case is the strict mode of the
handle
function, which is determined by the calling code outside of this module; the use of strict in this source code does not affect the context thathandle
ultimately ends up with.You can see this displayed right here:
This right now is definitely a breaking point of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, changed
undefined
to(function () { return this })()
which works in theory. Here is my test:and output:
Although I'm getting a different result from you (as you can see), both are returning the global object implying the
'use strict';
instrict()
does nothing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @calebmer , your change does not fix it, because the
this
in your code is still executing from within the strict code in the router source code, which is not where thethis
varies at.Your test above is also very different from the test I gave, as the
this
is always being executed in sloppy mode, not in the caller's defined code. Please refer to my example for what I'm talking about :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, the
this
is defined by where the physicalthis
is located, it will not vary depending on the call stack, which is what the issue is. In your test, thethis
for both calls is physically located in the same spot, this why it always evaluates to the same thing. My example demonstrates that thethis
is going to be physically located in other people's code, not within the router source code. Your use of.apply
overrides this behavior, which is likely to result in unexpected behavior (Node.js code experienced this a lot when they tried to change the context forsetTimeout
execution).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so I created a test for this, so really, all that would matter if the following test passed (ensure nowhere else in the file with the tests there is a
'use strict'
):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the tests verbatim and they worked with both
(function () { return this })()
andundefined
interestingly enough. So I switched back toundefined
. Want to take a look?