Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

path-to-regexp@^8.0.0 #117

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ function processParams (params, layer, called, req, res, done) {
}

let i = 0
let name
let paramIndex = 0
let key
let paramVal
Expand All @@ -596,10 +595,9 @@ function processParams (params, layer, called, req, res, done) {

paramIndex = 0
key = keys[i++]
name = key.name
paramVal = req.params[name]
paramCallbacks = params[name]
paramCalled = called[name]
paramVal = req.params[key]
paramCallbacks = params[key]
paramCalled = called[key]

if (paramVal === undefined || !paramCallbacks) {
return param()
Expand All @@ -609,13 +607,13 @@ function processParams (params, layer, called, req, res, done) {
if (paramCalled && (paramCalled.match === paramVal ||
(paramCalled.error && paramCalled.error !== 'route'))) {
// restore value
req.params[name] = paramCalled.value
req.params[key] = paramCalled.value

// next param
return param(paramCalled.error)
}

called[name] = paramCalled = {
called[key] = paramCalled = {
error: null,
match: paramVal,
value: paramVal
Expand All @@ -629,7 +627,7 @@ function processParams (params, layer, called, req, res, done) {
const fn = paramCallbacks[paramIndex++]

// store updated value
paramCalled.value = req.params[key.name]
paramCalled.value = req.params[key]

if (err) {
// store error
Expand All @@ -641,7 +639,7 @@ function processParams (params, layer, called, req, res, done) {
if (!fn) return param()

try {
const ret = fn(req, res, paramCallback, paramVal, key.name)
const ret = fn(req, res, paramCallback, paramVal, key)
if (isPromise(ret)) {
ret.then(null, function (error) {
paramCallback(error || new Error('Rejected promise'))
Expand Down
82 changes: 58 additions & 24 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const pathRegexp = require('path-to-regexp')
* @private
*/

const hasOwnProperty = Object.prototype.hasOwnProperty
const TRAILING_SLASH_REGEXP = /\/+$/
const MATCHING_GROUP_REGEXP = /\((?:\?<(.*?)>)?(?!\?)/g

/**
* Expose `Layer`.
Expand All @@ -41,10 +41,53 @@ function Layer (path, options, fn) {
this.name = fn.name || '<anonymous>'
this.params = undefined
this.path = undefined
this.regexp = pathRegexp((opts.strict ? path : loosen(path)), this.keys, opts)
this.slash = path === '/' && opts.end === false

function matcher (_path) {
if (_path instanceof RegExp) {
const keys = []
let name = 0
let m
// eslint-disable-next-line no-cond-assign
while (m = MATCHING_GROUP_REGEXP.exec(_path.source)) {
keys.push({
name: m[1] || name++,
offset: m.index
})
}

return function regexpMatcher (p) {
const match = _path.exec(p)
if (!match) {
return false
}

const params = {}
for (let i = 1; i < match.length; i++) {
const key = keys[i - 1]
const prop = key.name
const val = decodeParam(match[i])

if (val !== undefined) {
params[prop] = val
}
}

return {
params,
path: match[0]
}
}
}

// set fast path flags
this.regexp._slash = path === '/' && opts.end === false
return pathRegexp.match((opts.strict ? _path : loosen(_path)), {
sensitive: opts.sensitive,
end: opts.end,
trailing: !opts.strict,
decode: decodeParam
})
}
this.matchers = Array.isArray(path) ? path.map(matcher) : [matcher(path)]
wesleytodd marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -126,14 +169,18 @@ Layer.prototype.match = function match (path) {

if (path != null) {
// fast path non-ending match for / (any path matches)
if (this.regexp._slash) {
if (this.slash) {
this.params = {}
this.path = ''
return true
}

// match the path
match = this.regexp.exec(path)
let i = 0
while (!match && i < this.matchers.length) {
// match the path
match = this.matchers[i](path)
i++
}
}

if (!match) {
Expand All @@ -143,22 +190,9 @@ Layer.prototype.match = function match (path) {
}

// store values
this.params = {}
this.path = match[0]

// iterate matches
const keys = this.keys
const params = this.params

for (let i = 1; i < match.length; i++) {
const key = keys[i - 1]
const prop = key.name
const val = decodeParam(match[i])

if (val !== undefined || !(hasOwnProperty.call(params, prop))) {
params[prop] = val
}
}
this.params = match.params
this.path = match.path
this.keys = Object.keys(match.params)

return true
}
Expand Down Expand Up @@ -192,7 +226,7 @@ function decodeParam (val) {
* Loosens the given path for path-to-regexp matching.
*/
function loosen (path) {
if (path instanceof RegExp) {
if (path instanceof RegExp || path === '/') {
return path
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"is-promise": "4.0.0",
"methods": "~1.1.2",
"parseurl": "~1.3.3",
"path-to-regexp": "3.2.0",
"path-to-regexp": "^8.0.0",
"setprototypeof": "1.2.0",
"utils-merge": "1.0.1"
},
Expand Down Expand Up @@ -41,6 +41,7 @@
"scripts": {
"lint": "standard",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test:debug": "mocha --reporter spec --bail --check-leaks test/ --inspect --inspect-brk",
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
"test-cov": "nyc --reporter=text npm test",
"version": "node scripts/version-history.js && git add HISTORY.md"
Expand Down
4 changes: 2 additions & 2 deletions test/req.params.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('req.params', function () {
})
})

router.get('/(.*)', hitParams(1))
router.get(/\/([^/]*)/, hitParams(1))

request(server)
.get('/buzz')
Expand All @@ -156,7 +156,7 @@ describe('req.params', function () {
})
})

router.get('/(.*)', hitParams(1))
router.get(/\/([^/]*)/, hitParams(1))

request(server)
.get('/bar')
Expand Down
Loading
Loading