Skip to content

Commit 8e2b6b0

Browse files
authored
Merge pull request from GHSA-f6gv-hh8j-q8vq
* fix(trie-router): don't remain with the values of named param * denoify * fix: don't share `params` * denoify
1 parent af9e485 commit 8e2b6b0

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

deno_dist/router/trie-router/node.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import { splitPath, splitRoutingPath, getPattern } from '../../utils/url.ts'
55

66
type HandlerSet<T> = {
77
handler: T
8-
params: Record<string, string>
98
possibleKeys: string[]
109
score: number
1110
name: string // For debug
1211
}
1312

13+
type HandlerParamsSet<T> = HandlerSet<T> & {
14+
params: Record<string, string>
15+
}
16+
1417
export class Node<T> {
1518
methods: Record<string, HandlerSet<T>>[]
1619

@@ -26,7 +29,7 @@ export class Node<T> {
2629
this.name = ''
2730
if (method && handler) {
2831
const m: Record<string, HandlerSet<T>> = {}
29-
m[method] = { handler, params: {}, possibleKeys: [], score: 0, name: this.name }
32+
m[method] = { handler, possibleKeys: [], score: 0, name: this.name }
3033
this.methods = [m]
3134
}
3235
this.patterns = []
@@ -74,7 +77,6 @@ export class Node<T> {
7477

7578
const handlerSet: HandlerSet<T> = {
7679
handler,
77-
params: {},
7880
possibleKeys,
7981
name: this.name,
8082
score: this.order,
@@ -87,12 +89,17 @@ export class Node<T> {
8789
}
8890

8991
// getHandlerSets
90-
private gHSets(node: Node<T>, method: string, params: Record<string, string>): HandlerSet<T>[] {
91-
const handlerSets: HandlerSet<T>[] = []
92+
private gHSets(
93+
node: Node<T>,
94+
method: string,
95+
params: Record<string, string>
96+
): HandlerParamsSet<T>[] {
97+
const handlerSets: HandlerParamsSet<T>[] = []
9298
for (let i = 0, len = node.methods.length; i < len; i++) {
9399
const m = node.methods[i]
94-
const handlerSet = m[method] || m[METHOD_NAME_ALL]
100+
const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>
95101
if (handlerSet !== undefined) {
102+
handlerSet.params = {}
96103
handlerSet.possibleKeys.map((key) => {
97104
handlerSet.params[key] = params[key]
98105
})
@@ -103,7 +110,7 @@ export class Node<T> {
103110
}
104111

105112
search(method: string, path: string): [[T, Params][]] {
106-
const handlerSets: HandlerSet<T>[] = []
113+
const handlerSets: HandlerParamsSet<T>[] = []
107114

108115
const params: Record<string, string> = {}
109116
this.params = {}
@@ -126,11 +133,9 @@ export class Node<T> {
126133
if (isLast === true) {
127134
// '/hello/*' => match '/hello'
128135
if (nextNode.children['*']) {
129-
handlerSets.push(
130-
...this.gHSets(nextNode.children['*'], method, { ...params, ...node.params })
131-
)
136+
handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params))
132137
}
133-
handlerSets.push(...this.gHSets(nextNode, method, { ...params, ...node.params }))
138+
handlerSets.push(...this.gHSets(nextNode, method, node.params))
134139
} else {
135140
tempNodes.push(nextNode)
136141
}
@@ -144,7 +149,7 @@ export class Node<T> {
144149
if (pattern === '*') {
145150
const astNode = node.children['*']
146151
if (astNode) {
147-
handlerSets.push(...this.gHSets(astNode, method, { ...params, ...node.params }))
152+
handlerSets.push(...this.gHSets(astNode, method, node.params))
148153
tempNodes.push(astNode)
149154
}
150155
continue

src/router/trie-router/node.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ describe('Name path', () => {
8989
expect(res[0][0]).toEqual('get events')
9090
expect(res[0][1]['location']).toBe('yokohama')
9191
})
92+
93+
it('Should not return a previous param value', () => {
94+
const node = new Node()
95+
node.insert('delete', '/resource/:id', 'resource')
96+
const [resA] = node.search('delete', '/resource/a')
97+
const [resB] = node.search('delete', '/resource/b')
98+
expect(resA).not.toBeNull()
99+
expect(resA.length).toBe(1)
100+
expect(resA[0][0]).toEqual('resource')
101+
expect(resA[0][1]).toEqual({ id: 'a' })
102+
expect(resB).not.toBeNull()
103+
expect(resB.length).toBe(1)
104+
expect(resB[0][0]).toEqual('resource')
105+
expect(resB[0][1]).toEqual({ id: 'b' })
106+
})
92107
})
93108

94109
describe('Name path - Multiple route', () => {

src/router/trie-router/node.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import { splitPath, splitRoutingPath, getPattern } from '../../utils/url'
55

66
type HandlerSet<T> = {
77
handler: T
8-
params: Record<string, string>
98
possibleKeys: string[]
109
score: number
1110
name: string // For debug
1211
}
1312

13+
type HandlerParamsSet<T> = HandlerSet<T> & {
14+
params: Record<string, string>
15+
}
16+
1417
export class Node<T> {
1518
methods: Record<string, HandlerSet<T>>[]
1619

@@ -26,7 +29,7 @@ export class Node<T> {
2629
this.name = ''
2730
if (method && handler) {
2831
const m: Record<string, HandlerSet<T>> = {}
29-
m[method] = { handler, params: {}, possibleKeys: [], score: 0, name: this.name }
32+
m[method] = { handler, possibleKeys: [], score: 0, name: this.name }
3033
this.methods = [m]
3134
}
3235
this.patterns = []
@@ -74,7 +77,6 @@ export class Node<T> {
7477

7578
const handlerSet: HandlerSet<T> = {
7679
handler,
77-
params: {},
7880
possibleKeys,
7981
name: this.name,
8082
score: this.order,
@@ -87,12 +89,17 @@ export class Node<T> {
8789
}
8890

8991
// getHandlerSets
90-
private gHSets(node: Node<T>, method: string, params: Record<string, string>): HandlerSet<T>[] {
91-
const handlerSets: HandlerSet<T>[] = []
92+
private gHSets(
93+
node: Node<T>,
94+
method: string,
95+
params: Record<string, string>
96+
): HandlerParamsSet<T>[] {
97+
const handlerSets: HandlerParamsSet<T>[] = []
9298
for (let i = 0, len = node.methods.length; i < len; i++) {
9399
const m = node.methods[i]
94-
const handlerSet = m[method] || m[METHOD_NAME_ALL]
100+
const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>
95101
if (handlerSet !== undefined) {
102+
handlerSet.params = {}
96103
handlerSet.possibleKeys.map((key) => {
97104
handlerSet.params[key] = params[key]
98105
})
@@ -103,7 +110,7 @@ export class Node<T> {
103110
}
104111

105112
search(method: string, path: string): [[T, Params][]] {
106-
const handlerSets: HandlerSet<T>[] = []
113+
const handlerSets: HandlerParamsSet<T>[] = []
107114

108115
const params: Record<string, string> = {}
109116
this.params = {}
@@ -126,11 +133,9 @@ export class Node<T> {
126133
if (isLast === true) {
127134
// '/hello/*' => match '/hello'
128135
if (nextNode.children['*']) {
129-
handlerSets.push(
130-
...this.gHSets(nextNode.children['*'], method, { ...params, ...node.params })
131-
)
136+
handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params))
132137
}
133-
handlerSets.push(...this.gHSets(nextNode, method, { ...params, ...node.params }))
138+
handlerSets.push(...this.gHSets(nextNode, method, node.params))
134139
} else {
135140
tempNodes.push(nextNode)
136141
}
@@ -144,7 +149,7 @@ export class Node<T> {
144149
if (pattern === '*') {
145150
const astNode = node.children['*']
146151
if (astNode) {
147-
handlerSets.push(...this.gHSets(astNode, method, { ...params, ...node.params }))
152+
handlerSets.push(...this.gHSets(astNode, method, node.params))
148153
tempNodes.push(astNode)
149154
}
150155
continue

0 commit comments

Comments
 (0)