Skip to content

Commit

Permalink
Merge pull request from GHSA-f6gv-hh8j-q8vq
Browse files Browse the repository at this point in the history
* fix(trie-router): don't remain with the values of named param

* denoify

* fix: don't share `params`

* denoify
  • Loading branch information
yusukebe authored Dec 14, 2023
1 parent af9e485 commit 8e2b6b0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
29 changes: 17 additions & 12 deletions deno_dist/router/trie-router/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { splitPath, splitRoutingPath, getPattern } from '../../utils/url.ts'

type HandlerSet<T> = {
handler: T
params: Record<string, string>
possibleKeys: string[]
score: number
name: string // For debug
}

type HandlerParamsSet<T> = HandlerSet<T> & {
params: Record<string, string>
}

export class Node<T> {
methods: Record<string, HandlerSet<T>>[]

Expand All @@ -26,7 +29,7 @@ export class Node<T> {
this.name = ''
if (method && handler) {
const m: Record<string, HandlerSet<T>> = {}
m[method] = { handler, params: {}, possibleKeys: [], score: 0, name: this.name }
m[method] = { handler, possibleKeys: [], score: 0, name: this.name }
this.methods = [m]
}
this.patterns = []
Expand Down Expand Up @@ -74,7 +77,6 @@ export class Node<T> {

const handlerSet: HandlerSet<T> = {
handler,
params: {},
possibleKeys,
name: this.name,
score: this.order,
Expand All @@ -87,12 +89,17 @@ export class Node<T> {
}

// getHandlerSets
private gHSets(node: Node<T>, method: string, params: Record<string, string>): HandlerSet<T>[] {
const handlerSets: HandlerSet<T>[] = []
private gHSets(
node: Node<T>,
method: string,
params: Record<string, string>
): HandlerParamsSet<T>[] {
const handlerSets: HandlerParamsSet<T>[] = []
for (let i = 0, len = node.methods.length; i < len; i++) {
const m = node.methods[i]
const handlerSet = m[method] || m[METHOD_NAME_ALL]
const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>
if (handlerSet !== undefined) {
handlerSet.params = {}
handlerSet.possibleKeys.map((key) => {
handlerSet.params[key] = params[key]
})
Expand All @@ -103,7 +110,7 @@ export class Node<T> {
}

search(method: string, path: string): [[T, Params][]] {
const handlerSets: HandlerSet<T>[] = []
const handlerSets: HandlerParamsSet<T>[] = []

const params: Record<string, string> = {}
this.params = {}
Expand All @@ -126,11 +133,9 @@ export class Node<T> {
if (isLast === true) {
// '/hello/*' => match '/hello'
if (nextNode.children['*']) {
handlerSets.push(
...this.gHSets(nextNode.children['*'], method, { ...params, ...node.params })
)
handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params))
}
handlerSets.push(...this.gHSets(nextNode, method, { ...params, ...node.params }))
handlerSets.push(...this.gHSets(nextNode, method, node.params))
} else {
tempNodes.push(nextNode)
}
Expand All @@ -144,7 +149,7 @@ export class Node<T> {
if (pattern === '*') {
const astNode = node.children['*']
if (astNode) {
handlerSets.push(...this.gHSets(astNode, method, { ...params, ...node.params }))
handlerSets.push(...this.gHSets(astNode, method, node.params))
tempNodes.push(astNode)
}
continue
Expand Down
15 changes: 15 additions & 0 deletions src/router/trie-router/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ describe('Name path', () => {
expect(res[0][0]).toEqual('get events')
expect(res[0][1]['location']).toBe('yokohama')
})

it('Should not return a previous param value', () => {
const node = new Node()
node.insert('delete', '/resource/:id', 'resource')
const [resA] = node.search('delete', '/resource/a')
const [resB] = node.search('delete', '/resource/b')
expect(resA).not.toBeNull()
expect(resA.length).toBe(1)
expect(resA[0][0]).toEqual('resource')
expect(resA[0][1]).toEqual({ id: 'a' })
expect(resB).not.toBeNull()
expect(resB.length).toBe(1)
expect(resB[0][0]).toEqual('resource')
expect(resB[0][1]).toEqual({ id: 'b' })
})
})

describe('Name path - Multiple route', () => {
Expand Down
29 changes: 17 additions & 12 deletions src/router/trie-router/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { splitPath, splitRoutingPath, getPattern } from '../../utils/url'

type HandlerSet<T> = {
handler: T
params: Record<string, string>
possibleKeys: string[]
score: number
name: string // For debug
}

type HandlerParamsSet<T> = HandlerSet<T> & {
params: Record<string, string>
}

export class Node<T> {
methods: Record<string, HandlerSet<T>>[]

Expand All @@ -26,7 +29,7 @@ export class Node<T> {
this.name = ''
if (method && handler) {
const m: Record<string, HandlerSet<T>> = {}
m[method] = { handler, params: {}, possibleKeys: [], score: 0, name: this.name }
m[method] = { handler, possibleKeys: [], score: 0, name: this.name }
this.methods = [m]
}
this.patterns = []
Expand Down Expand Up @@ -74,7 +77,6 @@ export class Node<T> {

const handlerSet: HandlerSet<T> = {
handler,
params: {},
possibleKeys,
name: this.name,
score: this.order,
Expand All @@ -87,12 +89,17 @@ export class Node<T> {
}

// getHandlerSets
private gHSets(node: Node<T>, method: string, params: Record<string, string>): HandlerSet<T>[] {
const handlerSets: HandlerSet<T>[] = []
private gHSets(
node: Node<T>,
method: string,
params: Record<string, string>
): HandlerParamsSet<T>[] {
const handlerSets: HandlerParamsSet<T>[] = []
for (let i = 0, len = node.methods.length; i < len; i++) {
const m = node.methods[i]
const handlerSet = m[method] || m[METHOD_NAME_ALL]
const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>
if (handlerSet !== undefined) {
handlerSet.params = {}
handlerSet.possibleKeys.map((key) => {
handlerSet.params[key] = params[key]
})
Expand All @@ -103,7 +110,7 @@ export class Node<T> {
}

search(method: string, path: string): [[T, Params][]] {
const handlerSets: HandlerSet<T>[] = []
const handlerSets: HandlerParamsSet<T>[] = []

const params: Record<string, string> = {}
this.params = {}
Expand All @@ -126,11 +133,9 @@ export class Node<T> {
if (isLast === true) {
// '/hello/*' => match '/hello'
if (nextNode.children['*']) {
handlerSets.push(
...this.gHSets(nextNode.children['*'], method, { ...params, ...node.params })
)
handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params))
}
handlerSets.push(...this.gHSets(nextNode, method, { ...params, ...node.params }))
handlerSets.push(...this.gHSets(nextNode, method, node.params))
} else {
tempNodes.push(nextNode)
}
Expand All @@ -144,7 +149,7 @@ export class Node<T> {
if (pattern === '*') {
const astNode = node.children['*']
if (astNode) {
handlerSets.push(...this.gHSets(astNode, method, { ...params, ...node.params }))
handlerSets.push(...this.gHSets(astNode, method, node.params))
tempNodes.push(astNode)
}
continue
Expand Down

0 comments on commit 8e2b6b0

Please sign in to comment.