/
routePart.js
150 lines (129 loc) · 3.86 KB
/
routePart.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
/*
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2018 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const
querystring = require('querystring'),
RouteHandler = require('./routeHandler'),
{ has } = require('../../util/safeObject');
/**
* Defines a new route part
*
* @class RoutePart
*/
class RoutePart {
constructor() {
this.subparts = {};
this.placeholders = null;
this.handler = null;
}
/**
* Checks if an url part already exists
*
* @param {string} part
* @returns {boolean}
*/
exists(part) {
return this.subparts[part] !== undefined && this.subparts[part].handler !== null;
}
/**
* Gets the next element of an URL part, creating a new
* tree leaf if necessary
*
* @param {string} part
* @returns {RoutePart}
*/
getNext(part) {
if (!has(this.subparts, part)) {
this.subparts[part] = new RoutePart();
}
return this.subparts[part];
}
/**
* Returns a RouteHandler instance corresponding to the provided URL
* Returns null if no handler was found
*
* @param {HttpMessage} message
* @return {RouteHandler} registered function handler
*/
getHandler(message) {
// we need a fake URL base because the WHATWG API currently doesn't handle
// relative URLs, and the http.IncomingMessage class doesn't provide an
// easy way (and an inexpensive one) of getting an absolute URL
// See: https://github.com/nodejs/node/issues/12682
const parsed = new URL(
message.url,
message.url[0] === '/' ? 'http://fake' : null);
// limit calls to "parsed.pathname", it's a getter joining an array
let pathname = parsed.pathname;
if (pathname[pathname.length - 1] === '/') {
pathname = pathname.slice(0, -1);
}
const qs = {};
parsed.searchParams.forEach((v, k) => (qs[k] = v));
const routeHandler = new RouteHandler(pathname, qs, message);
return getHandlerPart(this, pathname.split('/'), routeHandler);
}
}
/**
* Populate the routeHandler argument with parametric values, if any
*
* @param {RoutePart} routePart - tree leaf to scan
* @param {Array<string>} parts
* @param {RouteHandler} routeHandler - registered function handler
* @param {Array<string>} placeholders - sorted array to populate the list of parametric values
* @return {RouteHandler} registered function handler
*/
function getHandlerPart (routePart, parts, routeHandler, placeholders = []) {
let part;
do {
part = parts.shift();
} while (parts.length > 0 && part.length === 0);
if (part === undefined) {
routeHandler.handler = routePart.handler;
if (routePart.placeholders !== null) {
for (const i of Object.keys(routePart.placeholders)) {
routeHandler.addArgument(routePart.placeholders[i], placeholders[i]);
}
}
return routeHandler;
}
part = querystring.unescape(part);
if (has(routePart.subparts, part)) {
return getHandlerPart(
routePart.subparts[part],
parts,
routeHandler,
placeholders);
}
if (routePart.subparts['*']) {
placeholders.push(part);
return getHandlerPart(
routePart.subparts['*'],
parts,
routeHandler,
placeholders);
}
return routeHandler;
}
/**
* @type {RoutePart}
*/
module.exports = RoutePart;