Skip to content

Commit 0e6408e

Browse files
committed
WIP: v8
1 parent e14430a commit 0e6408e

File tree

7 files changed

+352
-505
lines changed

7 files changed

+352
-505
lines changed

index.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,9 @@ function processParams (params, layer, called, req, res, done) {
596596

597597
paramIndex = 0
598598
key = keys[i++]
599-
name = key.name
600-
paramVal = req.params[name]
601-
paramCallbacks = params[name]
602-
paramCalled = called[name]
599+
paramVal = req.params[key]
600+
paramCallbacks = params[key]
601+
paramCalled = called[key]
603602

604603
if (paramVal === undefined || !paramCallbacks) {
605604
return param()
@@ -609,13 +608,13 @@ function processParams (params, layer, called, req, res, done) {
609608
if (paramCalled && (paramCalled.match === paramVal ||
610609
(paramCalled.error && paramCalled.error !== 'route'))) {
611610
// restore value
612-
req.params[name] = paramCalled.value
611+
req.params[key] = paramCalled.value
613612

614613
// next param
615614
return param(paramCalled.error)
616615
}
617616

618-
called[name] = paramCalled = {
617+
called[key] = paramCalled = {
619618
error: null,
620619
match: paramVal,
621620
value: paramVal
@@ -629,7 +628,7 @@ function processParams (params, layer, called, req, res, done) {
629628
var fn = paramCallbacks[paramIndex++]
630629

631630
// store updated value
632-
paramCalled.value = req.params[key.name]
631+
paramCalled.value = req.params[key]
633632

634633
if (err) {
635634
// store error
@@ -641,7 +640,7 @@ function processParams (params, layer, called, req, res, done) {
641640
if (!fn) return param()
642641

643642
try {
644-
var ret = fn(req, res, paramCallback, paramVal, key.name)
643+
var ret = fn(req, res, paramCallback, paramVal, key)
645644
if (isPromise(ret)) {
646645
ret.then(null, function (error) {
647646
paramCallback(error || new Error('Rejected promise'))

lib/layer.js

+32-26
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ var pathRegexp = require('path-to-regexp')
2020
* @private
2121
*/
2222

23-
var hasOwnProperty = Object.prototype.hasOwnProperty
2423
var TRAILING_SLASH_REGEXP = /\/+$/
2524

2625
/**
@@ -41,10 +40,27 @@ function Layer (path, options, fn) {
4140
this.name = fn.name || '<anonymous>'
4241
this.params = undefined
4342
this.path = undefined
44-
this.regexp = pathRegexp((opts.strict ? path : loosen(path)), this.keys, opts)
45-
46-
// set fast path flags
47-
this.regexp._slash = path === '/' && opts.end === false
43+
this.slash = path === '/' && opts.end === false
44+
45+
var matchers = []
46+
47+
function addMatcher (_path) {
48+
var matcher = pathRegexp.match((opts.strict ? _path : loosen(_path)), {
49+
sensitive: opts.sensitive,
50+
end: opts.end,
51+
trailing: !opts.strict,
52+
decode: decodeParam
53+
})
54+
matchers.push(matcher)
55+
}
56+
if (Array.isArray(path)) {
57+
for (var p of path) {
58+
addMatcher(p)
59+
}
60+
} else {
61+
addMatcher(path)
62+
}
63+
this.matchers = matchers
4864
}
4965

5066
/**
@@ -123,17 +139,20 @@ Layer.prototype.handleRequest = function handleRequest (req, res, next) {
123139

124140
Layer.prototype.match = function match (path) {
125141
var match
126-
127142
if (path != null) {
128143
// fast path non-ending match for / (any path matches)
129-
if (this.regexp._slash) {
144+
if (this.slash) {
130145
this.params = {}
131146
this.path = ''
132147
return true
133148
}
134149

135-
// match the path
136-
match = this.regexp.exec(path)
150+
var i = 0;
151+
while (!match && i < this.matchers.length) {
152+
// match the path
153+
match = this.matchers[i](path)
154+
i++;
155+
}
137156
}
138157

139158
if (!match) {
@@ -143,22 +162,9 @@ Layer.prototype.match = function match (path) {
143162
}
144163

145164
// store values
146-
this.params = {}
147-
this.path = match[0]
148-
149-
// iterate matches
150-
var keys = this.keys
151-
var params = this.params
152-
153-
for (var i = 1; i < match.length; i++) {
154-
var key = keys[i - 1]
155-
var prop = key.name
156-
var val = decodeParam(match[i])
157-
158-
if (val !== undefined || !(hasOwnProperty.call(params, prop))) {
159-
params[prop] = val
160-
}
161-
}
165+
this.params = match.params
166+
this.path = match.path
167+
this.keys = Object.keys(match.params)
162168

163169
return true
164170
}
@@ -192,7 +198,7 @@ function decodeParam (val) {
192198
* Loosens the given path for path-to-regexp matching.
193199
*/
194200
function loosen (path) {
195-
if (path instanceof RegExp) {
201+
if (path instanceof RegExp || path === '/') {
196202
return path
197203
}
198204

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"finalhandler": "1.2.0",
3030
"mocha": "10.2.0",
3131
"nyc": "15.1.0",
32+
"run-series": "^1.1.9",
3233
"safe-buffer": "5.2.1",
3334
"supertest": "6.3.3"
3435
},
@@ -46,6 +47,7 @@
4647
"scripts": {
4748
"lint": "eslint .",
4849
"test": "mocha --reporter spec --bail --check-leaks test/",
50+
"test:debug": "mocha --reporter spec --bail --check-leaks test/ --inspect --inspect-brk",
4951
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
5052
"test-cov": "nyc --reporter=text npm test",
5153
"version": "node scripts/version-history.js && git add HISTORY.md"

test/param.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
var after = require('after')
3+
var series = require('run-series')
34
var Router = require('..')
45
var utils = require('./support/utils')
56

@@ -300,7 +301,6 @@ describe('Router', function () {
300301

301302
describe('next("route")', function () {
302303
it('should cause route with param to be skipped', function (done) {
303-
var cb = after(3, done)
304304
var router = new Router()
305305
var server = createServer(router)
306306

@@ -326,17 +326,23 @@ describe('Router', function () {
326326
res.end('cannot get a new user')
327327
})
328328

329-
request(server)
330-
.get('/user/2')
331-
.expect(200, 'get user 2', cb)
332-
333-
request(server)
334-
.get('/user/bob')
335-
.expect(404, cb)
336-
337-
request(server)
338-
.get('/user/new')
339-
.expect(400, 'cannot get a new user', cb)
329+
series([
330+
function (cb) {
331+
request(server)
332+
.get('/user/2')
333+
.expect(200, 'get user 2', cb)
334+
},
335+
function (cb) {
336+
request(server)
337+
.get('/user/bob')
338+
.expect(404, cb)
339+
},
340+
function (cb) {
341+
request(server)
342+
.get('/user/new')
343+
.expect(400, 'cannot get a new user', cb)
344+
}
345+
], done)
340346
})
341347

342348
it('should invoke fn if path value differs', function (done) {

test/req.params.js

-40
Original file line numberDiff line numberDiff line change
@@ -124,46 +124,6 @@ describe('req.params', function () {
124124
.expect('x-params-1', '{"foo":"buzz"}')
125125
.expect(200, '{"foo":"bar"}', done)
126126
})
127-
128-
describe('with numeric properties in req.params', function () {
129-
it('should merge numeric properties by offsetting', function (done) {
130-
var router = Router({ mergeParams: true })
131-
var server = createServer(function (req, res, next) {
132-
req.params = { 0: 'foo', 1: 'bar' }
133-
134-
router(req, res, function (err) {
135-
if (err) return next(err)
136-
sawParams(req, res)
137-
})
138-
})
139-
140-
router.get('/(.*)', hitParams(1))
141-
142-
request(server)
143-
.get('/buzz')
144-
.expect('x-params-1', '{"0":"foo","1":"bar","2":"buzz"}')
145-
.expect(200, '{"0":"foo","1":"bar"}', done)
146-
})
147-
148-
it('should merge with same numeric properties', function (done) {
149-
var router = Router({ mergeParams: true })
150-
var server = createServer(function (req, res, next) {
151-
req.params = { 0: 'foo' }
152-
153-
router(req, res, function (err) {
154-
if (err) return next(err)
155-
sawParams(req, res)
156-
})
157-
})
158-
159-
router.get('/(.*)', hitParams(1))
160-
161-
request(server)
162-
.get('/bar')
163-
.expect('x-params-1', '{"0":"foo","1":"bar"}')
164-
.expect(200, '{"0":"foo"}', done)
165-
})
166-
})
167127
})
168128
})
169129

0 commit comments

Comments
 (0)