@@ -15,6 +15,22 @@ import {
1515 logResponse
1616} from './SensitiveLogger' ;
1717
18+ const Layer = require ( 'express/lib/router/layer' ) ;
19+
20+ function validateParameter ( key , value ) {
21+ if ( key == 'className' ) {
22+ if ( value . match ( / _ ? [ A - Z a - z ] [ A - Z a - z _ 0 - 9 ] * / ) ) {
23+ return value ;
24+ }
25+ } else if ( key == 'objectId' ) {
26+ if ( value . match ( / [ A - Z a - z 0 - 9 ] + / ) ) {
27+ return value ;
28+ }
29+ } else {
30+ return value ;
31+ }
32+ }
33+
1834export default class PromiseRouter {
1935 // Each entry should be an object with:
2036 // path: the path to route, in express format
@@ -69,7 +85,8 @@ export default class PromiseRouter {
6985 this . routes . push ( {
7086 path : path ,
7187 method : method ,
72- handler : handler
88+ handler : handler ,
89+ layer : new Layer ( path , null , handler )
7390 } ) ;
7491 } ;
7592
@@ -82,36 +99,16 @@ export default class PromiseRouter {
8299 if ( route . method != method ) {
83100 continue ;
84101 }
85- // NOTE: we can only route the specific wildcards :className and
86- // :objectId, and in that order.
87- // This is pretty hacky but I don't want to rebuild the entire
88- // express route matcher. Maybe there's a way to reuse its logic.
89- var pattern = '^' + route . path + '$' ;
90-
91- pattern = pattern . replace ( ':className' ,
92- '(_?[A-Za-z][A-Za-z_0-9]*)' ) ;
93- pattern = pattern . replace ( ':objectId' ,
94- '([A-Za-z0-9]+)' ) ;
95- pattern = pattern . replace ( ':functionName' ,
96- '([A-Za-z0-9_]+)' ) ;
97-
98- var re = new RegExp ( pattern ) ;
99- var m = path . match ( re ) ;
100- if ( ! m ) {
101- continue ;
102- }
103- var params = { } ;
104- if ( m [ 1 ] ) {
105- params . className = m [ 1 ] ;
106- }
107- if ( m [ 2 ] ) {
108- params . objectId = m [ 2 ] ;
109- }
110- if ( route . path . indexOf ( ':functionName' ) > 0 ) {
111- params . functionName = m [ 1 ] ;
112- }
113102
114- return { params : params , handler : route . handler } ;
103+ let layer = route . layer || new Layer ( route . path , null , route . handler ) ;
104+ let match = layer . match ( path ) ;
105+ if ( match ) {
106+ let params = layer . params ;
107+ Object . keys ( params ) . forEach ( ( key ) => {
108+ params [ key ] = validateParameter ( key , params [ key ] ) ;
109+ } ) ;
110+ return { params : params , handler : route . handler } ;
111+ }
115112 }
116113 } ;
117114
0 commit comments