-
Notifications
You must be signed in to change notification settings - Fork 869
/
Copy pathcontext-matcher.ts
93 lines (78 loc) · 2.34 KB
/
context-matcher.ts
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
import type { Filter, Request } from './types';
import * as isGlob from 'is-glob';
import * as micromatch from 'micromatch';
import * as url from 'url';
import { ERRORS } from './errors';
export function match(context: Filter, uri: string, req: Request): boolean {
// single path
if (isStringPath(context as string)) {
return matchSingleStringPath(context as string, uri);
}
// single glob path
if (isGlobPath(context as string)) {
return matchSingleGlobPath(context as string[], uri);
}
// multi path
if (Array.isArray(context)) {
if (context.every(isStringPath)) {
return matchMultiPath(context, uri);
}
if (context.every(isGlobPath)) {
return matchMultiGlobPath(context as string[], uri);
}
throw new Error(ERRORS.ERR_CONTEXT_MATCHER_INVALID_ARRAY);
}
// custom matching
if (typeof context === 'function') {
const pathname = getUrlPathName(uri);
return context(pathname, req);
}
throw new Error(ERRORS.ERR_CONTEXT_MATCHER_GENERIC);
}
/**
* @param {String} context '/api'
* @param {String} uri 'http://example.org/api/b/c/d.html'
* @return {Boolean}
*/
function matchSingleStringPath(context: string, uri: string) {
const pathname = getUrlPathName(uri);
return pathname.indexOf(context) === 0;
}
function matchSingleGlobPath(pattern: string | string[], uri: string) {
const pathname = getUrlPathName(uri);
const matches = micromatch([pathname], pattern);
return matches && matches.length > 0;
}
function matchMultiGlobPath(patternList: string | string[], uri: string) {
return matchSingleGlobPath(patternList, uri);
}
/**
* @param {String} contextList ['/api', '/ajax']
* @param {String} uri 'http://example.org/api/b/c/d.html'
* @return {Boolean}
*/
function matchMultiPath(contextList: string[], uri: string) {
let isMultiPath = false;
for (const context of contextList) {
if (matchSingleStringPath(context, uri)) {
isMultiPath = true;
break;
}
}
return isMultiPath;
}
/**
* Parses URI and returns RFC 3986 path
*
* @param {String} uri from req.url
* @return {String} RFC 3986 path
*/
function getUrlPathName(uri: string) {
return uri && url.parse(uri).pathname;
}
function isStringPath(context: string) {
return typeof context === 'string' && !isGlob(context);
}
function isGlobPath(context: string) {
return isGlob(context);
}