/
buildPattern.js
141 lines (124 loc) · 3.16 KB
/
buildPattern.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import parseURL from './parseURL'
import { parse as parseQuery } from 'querystring'
export default function buildPattern(schema) {
const components = parseURL(schema)
const matchProtocol = protocol(components.protocol)
const matchHostname = hostname(components.hostname)
const matchPort = port(components.port)
const matchQuery = query(components.search && parseQuery(components.search.slice(1)))
const matchMethod = method(components.hash && components.hash.slice(1))
const matchPathname = pathname(components.pathname)
return req => (
true
&& matchMethod(req.method)
&& matchPort(req.port || (req.secure ? 443 : 80))
&& matchProtocol(req.protocol)
&& matchHostname(req.hostname)
&& matchQuery(req.query)
// pathname must be the last one so we can return path params directly
&& matchPathname(req.pathname)
)
}
function protocol(schema) {
if (schema == null) {
return () => true
}
const accepts = schema.toUpperCase().split('|')
return target => accepts.includes(target.replace(/:$/, '').toUpperCase())
}
/**
* Matching hostname
*
* Supports DNS wildcard
*/
function hostname(schema) {
if (schema == null) {
return () => true
}
const domain = schema.replace(/^\*(\.\*)?/, '')
const strict = !schema.startsWith('*.*.')
const wildcard = schema.startsWith('*.')
const length = schema.split('.').length
return target => {
if (!target.endsWith(domain)) {
return false
}
if (strict && target.split('.').length !== length) {
return false
}
return wildcard || target === schema
}
}
function port(schema) {
if (schema == null) {
return () => true
}
const accepts = schema.split('|')
return target => accepts.includes(target)
}
/**
* Matching pathname with params
*
* eg. /pathname/with/:shortParam/::longParam
*
* :shortParam matches any characters except "/"
* ::longParam matches any characters
*/
function pathname(schema) {
if (schema == null) {
return () => ({})
}
const params = []
const source = schema.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&').replace(/::?\w+/g, param => {
if (param.startsWith('::')) {
params.push(param.slice(2))
return '(.+)'
}
params.push(param.slice(1))
return '([^/]+)'
})
const paramLength = params.length
const regex = new RegExp(`^${source}$`)
return target => {
const match = target.match(regex)
if (!match) {
return null
}
const result = {}
for (let i = 0; i < paramLength; i++) {
result[params[i]] = match[i + 1]
}
return result
}
}
/**
* Matching query
*
* eg. foo&bar=baz
*
* "foo" must exists, and "bar" must equals to "baz"
*/
function query(schema) {
if (schema == null) {
return () => true
}
const keys = Object.keys(schema)
return target => {
for (const key of keys) {
if (target[key] == null) {
return false
}
if (schema[key] && (target[key] !== schema[key])) {
return false
}
}
return true
}
}
function method(schema) {
if (schema == null) {
return () => true
}
const accepts = schema.toUpperCase().split('|')
return target => accepts.includes(target.toUpperCase())
}