This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (113 loc) · 3.07 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
//@ts-check
const url = require("url");
const { parse: parseQueryString } = require("querystring");
const { send } = require("micro");
const pathMatch = require("path-match");
const subAppMatch = pathMatch({
sensitive: false,
strict: false,
end: false
});
const routeMatch = pathMatch({
sensitive: false,
strict: false,
end: true
});
module.exports = Router;
exports = Router;
exports.default = Router;
exports.Router = Router;
const setBasePathSymbol = Symbol("BasePathSetter");
function Router() {
let basePath = "";
const routes = [];
function handle(method, path, handler) {
const match = routeMatch(basePath + path);
routes.push({ method, path, handler, match });
}
function use(path, handler) {
if (handler[setBasePathSymbol]) handler[setBasePathSymbol](basePath + path);
const match = subAppMatch(basePath + path);
routes.push({ method: null, path, handler, match });
}
async function handler(req, res) {
for (let route of routes) {
if (route.method && route.method !== req.method) continue;
const path = url.parse(req.url, true).pathname;
const params = route.match(path);
if (!params) continue;
paramsMap.set(req, params);
basePathMap.set(req, basePath);
return route.handler(req, res);
}
return send(res, 404);
}
const methods = [
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"CONNECT",
"OPTIONS",
"TRACE",
"PATCH"
];
methods.forEach(method => {
handler[method.toLowerCase()] = (path, handler) =>
handle(method, path, handler);
});
function setBasePath(bp) {
basePath = bp;
for (let route of routes) {
if (route.handler[setBasePathSymbol]) {
const newPath = `${basePath}${route.path}`;
const match = subAppMatch(newPath);
route.path = basePath;
route.match = match;
route.handler[setBasePathSymbol](newPath);
} else {
const newPath = `${basePath}${route.path}`;
const match = routeMatch(newPath);
route.match = match;
}
}
}
handler.use = use;
handler[setBasePathSymbol] = setBasePath;
return handler;
}
function createHook(fn) {
const weakMap = new WeakMap();
return function(req) {
const cache = weakMap.get(req);
if (cache) return cache;
const value = fn(req);
if (value instanceof Promise) {
return value.then(realValue => {
weakMap.set(req, realValue);
return realValue;
});
} else {
weakMap.set(req, value);
return value;
}
};
}
exports.createHook = createHook;
const paramsMap = new WeakMap();
exports.useUrlParams = function useUrlParams(req) {
return paramsMap.get(req);
};
exports.useQueryString = createHook(function useQueryString(req) {
const queryString = url.parse(req.url, true).search;
const query = parseQueryString(
// although queryString should not be nullable, sometimes it comes back null
(queryString || "").slice(1)
);
return query;
});
const basePathMap = new WeakMap();
exports.useBasePath = function(req) {
return basePathMap.get(req);
};