Skip to content

Commit

Permalink
WIP: v8
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Aug 25, 2024
1 parent e14430a commit 9b5ea96
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 457 deletions.
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ function matchLayer (layer, path) {
try {
return layer.match(path)
} catch (err) {
err.statusCode = 400;
return err
}
}
Expand Down Expand Up @@ -596,10 +597,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 +609,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 +629,7 @@ function processParams (params, layer, called, req, res, done) {
var 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 +641,7 @@ function processParams (params, layer, called, req, res, done) {
if (!fn) return param()

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

var hasOwnProperty = Object.prototype.hasOwnProperty
var TRAILING_SLASH_REGEXP = /\/+$/

/**
Expand All @@ -41,7 +40,12 @@ 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.regexp = pathRegexp.match((opts.strict ? path : loosen(path)), {
sensitive: opts.sensitive,
end: opts.end,
trailing: !opts.strict,
decode: decodeParam
})

// set fast path flags
this.regexp._slash = path === '/' && opts.end === false
Expand Down Expand Up @@ -123,7 +127,6 @@ Layer.prototype.handleRequest = function handleRequest (req, res, next) {

Layer.prototype.match = function match (path) {
var match

if (path != null) {
// fast path non-ending match for / (any path matches)
if (this.regexp._slash) {
Expand All @@ -133,7 +136,7 @@ Layer.prototype.match = function match (path) {
}

// match the path
match = this.regexp.exec(path)
match = this.regexp(path)
}

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

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

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

for (var i = 1; i < match.length; i++) {
var key = keys[i - 1]
var prop = key.name
var 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 +182,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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"finalhandler": "1.2.0",
"mocha": "10.2.0",
"nyc": "15.1.0",
"run-series": "^1.1.9",
"safe-buffer": "5.2.1",
"supertest": "6.3.3"
},
Expand All @@ -46,6 +47,7 @@
"scripts": {
"lint": "eslint .",
"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
30 changes: 18 additions & 12 deletions test/param.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

var after = require('after')
var series = require('run-series')
var Router = require('..')
var utils = require('./support/utils')

Expand Down Expand Up @@ -300,7 +301,6 @@ describe('Router', function () {

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

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

request(server)
.get('/user/2')
.expect(200, 'get user 2', cb)

request(server)
.get('/user/bob')
.expect(404, cb)

request(server)
.get('/user/new')
.expect(400, 'cannot get a new user', cb)
series([
function (cb) {
request(server)
.get('/user/2')
.expect(200, 'get user 2', cb)
},
function (cb) {
request(server)
.get('/user/bob')
.expect(404, cb)
},
function (cb) {
request(server)
.get('/user/new')
.expect(400, 'cannot get a new user', cb)
}
], done)
})

it('should invoke fn if path value differs', function (done) {
Expand Down
40 changes: 0 additions & 40 deletions test/req.params.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,46 +124,6 @@ describe('req.params', function () {
.expect('x-params-1', '{"foo":"buzz"}')
.expect(200, '{"foo":"bar"}', done)
})

describe('with numeric properties in req.params', function () {
it('should merge numeric properties by offsetting', function (done) {
var router = Router({ mergeParams: true })
var server = createServer(function (req, res, next) {
req.params = { 0: 'foo', 1: 'bar' }

router(req, res, function (err) {
if (err) return next(err)
sawParams(req, res)
})
})

router.get('/(.*)', hitParams(1))

request(server)
.get('/buzz')
.expect('x-params-1', '{"0":"foo","1":"bar","2":"buzz"}')
.expect(200, '{"0":"foo","1":"bar"}', done)
})

it('should merge with same numeric properties', function (done) {
var router = Router({ mergeParams: true })
var server = createServer(function (req, res, next) {
req.params = { 0: 'foo' }

router(req, res, function (err) {
if (err) return next(err)
sawParams(req, res)
})
})

router.get('/(.*)', hitParams(1))

request(server)
.get('/bar')
.expect('x-params-1', '{"0":"foo","1":"bar"}')
.expect(200, '{"0":"foo"}', done)
})
})
})
})

Expand Down
Loading

0 comments on commit 9b5ea96

Please sign in to comment.