1+ import { config } from "./config"
2+ import { hash } from "./hash"
3+ import * as url from "url"
4+
5+ export class RouteCheck {
6+ private _key : string ;
7+ private _expire : number ;
8+ private _url : string ;
9+ private _original : string ;
10+
11+ private _isCacheable : boolean = false ;
12+ constructor ( private req ) {
13+ this . _original = url . parse ( req . originalUrl ) . pathname ;
14+ if ( config . ignore_case ) {
15+ this . _original = this . _original . toLocaleLowerCase ( ) ;
16+ }
17+
18+ const mayCache = this . matchesRule ( ) ;
19+ if ( mayCache ) {
20+ if ( typeof config . isCacheable == "function" ) {
21+ this . _isCacheable = config . isCacheable ( this . req ) ;
22+ }
23+ else {
24+ this . _isCacheable = true ;
25+ }
26+ }
27+ }
28+
29+ private getNormalizedKey ( ) : string {
30+ const vars = config . variables ;
31+
32+ let baseUrl : string [ ] = [
33+ this . _original ,
34+ this . req . method
35+ ] ;
36+ let param : string [ ] = [ ] ;
37+
38+ for ( let o in vars ) {
39+ if ( o in this . req ) {
40+ const values = this . req [ o ] ;
41+ const type = vars [ o ] ;
42+ for ( let i = 0 ; i < type . length ; i ++ ) {
43+ let n : string ;
44+ let v : string ;
45+ if ( typeof type [ i ] == "object" ) {
46+ [ n , v ] = [ type [ i ] . value , values [ n ] ] ;
47+
48+ if ( typeof v == "undefined" ) {
49+ continue ;
50+ }
51+
52+ if ( type [ i ] . ignore_case ) {
53+ v = v . toLocaleLowerCase ( ) ;
54+ }
55+ }
56+ else {
57+ [ n , v ] = [ type [ i ] , values [ i ] ] ;
58+
59+ if ( typeof v == "undefined" ) {
60+ continue ;
61+ }
62+ }
63+ param . push ( n + "=" + v ) ;
64+ }
65+ }
66+ }
67+
68+ if ( param . length ) {
69+ baseUrl . push ( "?" ) ;
70+ }
71+ return baseUrl . join ( "_" ) + param . join ( "&" ) ;
72+ }
73+
74+ private hasMethod ( rule : any ) : boolean {
75+ if ( rule . methods && rule . methods != "*" ) {
76+ if ( ! ~ rule . methods . indexOf ( this . req . method ) ) {
77+ return false ;
78+ }
79+ }
80+ return true ;
81+ }
82+
83+ private matchesRule ( ) : boolean {
84+ let url = this . _original ;
85+ let has : boolean = false ;
86+
87+ let rule ;
88+ for ( rule of config . rules ) {
89+ if ( ! this . hasMethod ( rule ) )
90+ continue ;
91+
92+ if ( rule . regex && rule . regex . test ( url ) ) {
93+ has = true ;
94+ break ;
95+ }
96+ else if ( rule . route ) {
97+ if ( rule . route == url ) {
98+ has = true ;
99+ break ;
100+ }
101+ }
102+ }
103+ if ( has ) {
104+ this . useMatchedRule ( rule ) ;
105+ }
106+ return has ;
107+ }
108+
109+ private useMatchedRule ( rule ) {
110+ let group = rule . group ? rule . group + "_" : "" ;
111+ this . _url = this . getNormalizedKey ( ) ;
112+ this . _key = group + hash ( this . _url ) ;
113+ this . _expire = rule . expire ;
114+ }
115+
116+ get isCacheable ( ) : boolean {
117+ return this . _isCacheable ;
118+ }
119+
120+ get key ( ) : string {
121+ return this . _key ;
122+ }
123+
124+ get expire ( ) : number {
125+ return this . _expire ;
126+ }
127+
128+ get normalizedKey ( ) : string {
129+ return this . _url ;
130+ }
131+
132+ mergeHeaders ( original : any , newValues : any ) : any {
133+ let head = Object . assign ( { } , original ) ;
134+
135+ for ( let o in head ) {
136+ let n = o . toLocaleLowerCase ( ) ;
137+ if ( n in newValues )
138+ delete head [ o ] ;
139+ }
140+
141+ return Object . assign ( head , newValues ) ;
142+ }
143+ }
0 commit comments