Skip to content
Myr

fix(reg-exp-router): Register static paths first for duplicate checking. (#1146)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(reg-exp-router): Register static paths first for duplicate checking. * chore: denoify

authored by Taku Amano · committed by GitHub · parent c09dbc0 · Browse files · 3 files+57 added−45 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+57 added−45 removed

  • tests:+27 added−15 removed
  • other:+30 added−30 removed

3 files+57 added−45 removed

modified:deno_dist/router/reg-exp-router/router.ts+15 added-15 removed
27 unmodified lines
28}2930function buildMatcherFromPreprocessedRoutes<T>(routes: [string, T[]][]): Matcher<T> {31 const trie = new Trie()32 const handlers: HandlerData<T>[] = []32 const handlerData: HandlerData<T>[] = []33 if (routes.length === 0) {34 return nullMatcher35 }3637 routes = routes.sort(([a], [b]) => a.length - b.length)37 const routesWithStaticPathFlag = routes38 .map((route) => [!/\*|\/:/.test(route[0]), ...route] as [boolean, string, T[]])39 .sort(([isStaticA, pathA], [isStaticB, pathB]) =>40 isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length41 )4243 const staticMap: StaticMap<T> = {}40 for (let i = 0, j = -1, len = routes.length; i < len; i++) {41 const path = routes[i][0]42 let pathErrorCheckOnly = false43 if (!/\*|\/:/.test(path)) {44 pathErrorCheckOnly = true45 staticMap[routes[i][0]] = { handlers: routes[i][1], params: emptyParam }44 for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {45 const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i]46 if (pathErrorCheckOnly) {47 staticMap[path] = { handlers, params: emptyParam }48 } else {49 j++50 }51
7 unmodified lines
59 if (pathErrorCheckOnly) {60 continue61 }6261 handlers[j] =62 paramMap.length === 063 ? [{ handlers: routes[i][1], params: emptyParam }, null]64 : [routes[i][1], paramMap]63 handlerData[j] =64 paramMap.length === 0 ? [{ handlers, params: emptyParam }, null] : [handlers, paramMap]65 }6667 const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp()68 for (let i = 0, len = handlers.length; i < len; i++) {69 const paramMap = handlers[i][1]68 for (let i = 0, len = handlerData.length; i < len; i++) {69 const paramMap = handlerData[i][1]70 if (paramMap) {71 for (let j = 0, len = paramMap.length; j < len; j++) {72 paramMap[j][1] = paramReplacementMap[paramMap[j][1]]73 }
2 unmodified lines
7677 const handlerMap: HandlerData<T>[] = []78 // using `in` because indexReplacementMap is a sparse array79 for (const i in indexReplacementMap) {80 handlerMap[i] = handlers[indexReplacementMap[i]]80 handlerMap[i] = handlerData[indexReplacementMap[i]]81 }8283 return [regexp, handlerMap, staticMap] as Matcher<T>84}
175 unmodified lines
modified:src/router/reg-exp-router/router.test.ts+27 added-15 removed
220 unmodified lines
221 router.match('GET', '/')222 }).toThrowError(UnsupportedPathError)223 })224225 describe('static and dynamic', () => {225 it('static and dynamic', () => {226 const router = new RegExpRouter<string>()227 router.add('GET', '/reg-exp/router', 'foo')228 router.add('GET', '/reg-exp/:id', 'bar')226 it('static first', () => {227 const router = new RegExpRouter<string>()228 router.add('GET', '/reg-exp/router', 'foo')229 router.add('GET', '/reg-exp/:id', 'bar')230230 expect(() => {231 router.match('GET', '/')232 }).toThrowError(UnsupportedPathError)233 })231 expect(() => {232 router.match('GET', '/')233 }).toThrowError(UnsupportedPathError)234 })235235 it('dynamic and static', () => {236 const router = new RegExpRouter<string>()237 router.add('GET', '/reg-exp/:id', 'bar')238 router.add('GET', '/reg-exp/router', 'foo')236 it('long label', () => {237 const router = new RegExpRouter<string>()238 router.add('GET', '/reg-exp/router', 'foo')239 router.add('GET', '/reg-exp/:service', 'bar')240240 expect(() => {241 router.match('GET', '/')242 }).toThrowError(UnsupportedPathError)241 expect(() => {242 router.match('GET', '/')243 }).toThrowError(UnsupportedPathError)244 })245246 it('dynamic first', () => {247 const router = new RegExpRouter<string>()248 router.add('GET', '/reg-exp/:id', 'bar')249 router.add('GET', '/reg-exp/router', 'foo')250251 expect(() => {252 router.match('GET', '/')253 }).toThrowError(UnsupportedPathError)254 })255 })256257 it('different regular expression', () => {258 const router = new RegExpRouter<string>()
247 unmodified lines
modified:src/router/reg-exp-router/router.ts+15 added-15 removed
27 unmodified lines
28}2930function buildMatcherFromPreprocessedRoutes<T>(routes: [string, T[]][]): Matcher<T> {31 const trie = new Trie()32 const handlers: HandlerData<T>[] = []32 const handlerData: HandlerData<T>[] = []33 if (routes.length === 0) {34 return nullMatcher35 }3637 routes = routes.sort(([a], [b]) => a.length - b.length)37 const routesWithStaticPathFlag = routes38 .map((route) => [!/\*|\/:/.test(route[0]), ...route] as [boolean, string, T[]])39 .sort(([isStaticA, pathA], [isStaticB, pathB]) =>40 isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length41 )4243 const staticMap: StaticMap<T> = {}40 for (let i = 0, j = -1, len = routes.length; i < len; i++) {41 const path = routes[i][0]42 let pathErrorCheckOnly = false43 if (!/\*|\/:/.test(path)) {44 pathErrorCheckOnly = true45 staticMap[routes[i][0]] = { handlers: routes[i][1], params: emptyParam }44 for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {45 const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i]46 if (pathErrorCheckOnly) {47 staticMap[path] = { handlers, params: emptyParam }48 } else {49 j++50 }51
7 unmodified lines
59 if (pathErrorCheckOnly) {60 continue61 }6261 handlers[j] =62 paramMap.length === 063 ? [{ handlers: routes[i][1], params: emptyParam }, null]64 : [routes[i][1], paramMap]63 handlerData[j] =64 paramMap.length === 0 ? [{ handlers, params: emptyParam }, null] : [handlers, paramMap]65 }6667 const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp()68 for (let i = 0, len = handlers.length; i < len; i++) {69 const paramMap = handlers[i][1]68 for (let i = 0, len = handlerData.length; i < len; i++) {69 const paramMap = handlerData[i][1]70 if (paramMap) {71 for (let j = 0, len = paramMap.length; j < len; j++) {72 paramMap[j][1] = paramReplacementMap[paramMap[j][1]]73 }
2 unmodified lines
7677 const handlerMap: HandlerData<T>[] = []78 // using `in` because indexReplacementMap is a sparse array79 for (const i in indexReplacementMap) {80 handlerMap[i] = handlers[indexReplacementMap[i]]80 handlerMap[i] = handlerData[indexReplacementMap[i]]81 }8283 return [regexp, handlerMap, staticMap] as Matcher<T>84}
175 unmodified lines