-
Notifications
You must be signed in to change notification settings - Fork 350
/
index.js
215 lines (173 loc) · 5.13 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import RouteRecognizer from 'route-recognizer';
import castArray from 'lodash-es/castArray';
import { HTTP_METHODS, URL, assert, timeout, buildUrl } from '@pollyjs/utils';
import Route from './route';
import Handler from './handler';
import Middleware from './middleware';
const HOST = Symbol();
const NAMESPACES = Symbol();
const REGISTRY = Symbol();
const MIDDLEWARE = Symbol();
const HANDLERS = Symbol();
const CHARS = {
SLASH: '/',
STAR: '*',
COLON: ':'
};
const { keys } = Object;
function parseUrl(url) {
const parsedUrl = new URL(url);
/*
Use the full origin (http://hostname:port) if the host exists. If there
is no host, URL.origin returns "null" (null as a string) so set host to '/'
*/
const host = parsedUrl.host ? parsedUrl.origin : CHARS.SLASH;
const path = parsedUrl.pathname || CHARS.SLASH;
return { host, path };
}
export default class Server {
constructor() {
this[HOST] = '';
this[REGISTRY] = {};
this[NAMESPACES] = [];
this[MIDDLEWARE] = [];
}
host(path, callback) {
const host = this[HOST];
assert(`[Server] A host cannot be specified within another host.`, !host);
this[HOST] = path;
callback(this);
this[HOST] = host;
}
namespace(path, callback) {
const namespaces = this[NAMESPACES];
this[NAMESPACES] = [...namespaces, path];
callback(this);
this[NAMESPACES] = namespaces;
}
timeout() {
return timeout(...arguments);
}
get() {
return this._register('GET', ...arguments);
}
put() {
return this._register('PUT', ...arguments);
}
post() {
return this._register('POST', ...arguments);
}
delete() {
return this._register('DELETE', ...arguments);
}
patch() {
return this._register('PATCH', ...arguments);
}
merge() {
return this._register('MERGE', ...arguments);
}
head() {
return this._register('HEAD', ...arguments);
}
options() {
return this._register('OPTIONS', ...arguments);
}
any() {
return this._registerMiddleware(...arguments);
}
lookup(method, url) {
return new Route(this._recognize(method, url), this._lookupMiddleware(url));
}
_lookupMiddleware(url) {
const { host, path } = parseUrl(url);
return this[MIDDLEWARE].map(m => m.match(host, path)).filter(Boolean);
}
_register(method, routes) {
const handler = new Handler();
castArray(routes).forEach(route => {
const { host, path } = parseUrl(this._buildUrl(route));
const registry = this._registryForHost(host);
const name = this._nameForPath(path);
const router = registry[method.toUpperCase()];
if (router[HANDLERS].has(name)) {
router[HANDLERS].get(name).push(handler);
} else {
router[HANDLERS].set(name, [handler]);
router.add([{ path, handler: router[HANDLERS].get(name) }]);
}
});
return handler;
}
_registerMiddleware(routes) {
const handler = new Handler();
const pathsByHost = {};
castArray(routes).forEach(route => {
/*
If the route is a '*' or '' and there is no host or namespace
specified, treat the middleware as global so it will match all routes.
*/
if (
(!route || route === CHARS.STAR) &&
!this[HOST] &&
this[NAMESPACES].length === 0
) {
this[MIDDLEWARE].push(new Middleware({ global: true, handler }));
} else {
const { host, path } = parseUrl(this._buildUrl(route));
pathsByHost[host] = pathsByHost[host] || [];
pathsByHost[host].push(path);
}
});
keys(pathsByHost).forEach(host => {
this[MIDDLEWARE].push(
new Middleware({ host, paths: pathsByHost[host], handler })
);
});
return handler;
}
_recognize(method, url) {
const { host, path } = parseUrl(url);
const registry = this._registryForHost(host);
return registry[method.toUpperCase()].recognize(path);
}
_buildUrl(path) {
return buildUrl(this[HOST], ...this[NAMESPACES], path);
}
/**
* Converts a url path into a name used to combine route handlers by
* normalizing dynamic and star segments
* @param {String} path
* @returns {String}
*/
_nameForPath(path = '') {
const name = path
.split(CHARS.SLASH)
.map(segment => {
switch (segment.charAt(0)) {
// If this is a dynamic segment (e.g. :id), then just return `:`
// since /path/:id is the same as /path/:uuid
case CHARS.COLON:
return CHARS.COLON;
// If this is a star segment (e.g. *path), then just return `*`
// since /path/*path is the same as /path/*all
case CHARS.STAR:
return CHARS.STAR;
default:
return segment;
}
})
.join(CHARS.SLASH);
// Remove trailing slash, if we result with an empty string, return a slash
return name.replace(/\/$/, '') || CHARS.SLASH;
}
_registryForHost(host) {
if (!this[REGISTRY][host]) {
this[REGISTRY][host] = HTTP_METHODS.reduce((acc, method) => {
acc[method] = new RouteRecognizer();
acc[method][HANDLERS] = new Map();
return acc;
}, {});
}
return this[REGISTRY][host];
}
}