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

Change set status assert, use statuses when possible #1308

Merged
merged 1 commit into from Jan 28, 2019

Conversation

fl0w
Copy link
Contributor

@fl0w fl0w commented Jan 27, 2019

Reference, #1119 #1042

This is a different approach to #1119 for allowing custom statuses. Instead of adding an API in Koa on top of statuses, just don't disallow anything unless it's a 3 digit integer.

This is a result from the fact that #1119 got stale or never got any traction, that statuses package can be repopulated added to by its design, dead-horses comment about RFC and no real purpose to disallow custom statuses, and the fact that I personally dislike previous approaches.

@codecov
Copy link

codecov bot commented Jan 27, 2019

Codecov Report

Merging #1308 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #1308   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files           5       5           
  Lines         393     393           
======================================
  Hits          393     393
Impacted Files Coverage Δ
lib/response.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 72f325b...65c000a. Read the comment docs.

@fl0w
Copy link
Contributor Author

fl0w commented Jan 27, 2019

Forced pushed an update to further constrain expression.

lib/response.js Outdated
@@ -84,7 +84,7 @@ module.exports = {
if (this.headerSent) return;

assert('number' == typeof code, 'status code must be a number');
assert(statuses[code], `invalid status code: ${code}`);
assert(/^[0-9]{3}$/.test(code), `invalid status code: ${code}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

assert(Number.isIngeter(code) && code >= 100 && code <= 999, 'message')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I'll update it. I ran a quick bench just to check how bad RegExp is in comparison (even though it's pointless in a real world scenario as I can't imagine ctx.status being a bottleneck:

Valid RegExp x 37,061,865 ops/sec ±0.51% (88 runs sampled)
Valid expression x 728,862,990 ops/sec ±1.60% (82 runs sampled)

The only thing I'd change is to not check both for number and Number.isInteger. I'll move isInteger to the above assert and keep that message (less "breaking") and replace RegExp.

@fl0w
Copy link
Contributor Author

fl0w commented Jan 28, 2019

Updated, squashed and rebased.

@dead-horse dead-horse merged commit b7bfa71 into koajs:master Jan 28, 2019
@fl0w fl0w deleted the change-assert-status branch January 28, 2019 08:50
@dead-horse
Copy link
Member

koa@2.7.0 released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants