-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
175 lines (147 loc) · 4.5 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
'use strict';
module.exports = HttpHash;
function HttpHash() {
if (!(this instanceof HttpHash)) {
return new HttpHash();
}
this._hash = new RouteNode();
}
HttpHash.prototype.get = get;
HttpHash.prototype.set = set;
function get(pathname) {
var pathSegments = pathname.split('/');
var hash = this._hash;
var splat = null;
var params = {};
var variablePaths;
for (var i = 0; i < pathSegments.length; i++) {
var segment = pathSegments[i];
if (!segment && !hash.isSplat) {
continue;
} else if (
segment === '__proto__' &&
hash.hasOwnProperty('proto')
) {
hash = hash.proto;
} else if (hash.staticPaths.hasOwnProperty(segment)) {
hash = hash.staticPaths[segment];
} else if ((variablePaths = hash.variablePaths)) {
if (variablePaths.isSplat) {
splat = pathSegments.slice(i).join('/');
hash = variablePaths;
break;
} else {
params[variablePaths.segment] = segment;
hash = variablePaths;
}
} else {
hash = null;
break;
}
}
// Match the empty splat
if (hash &&
hash.handler === null &&
hash.variablePaths &&
hash.variablePaths.isSplat
) {
splat = '';
hash = hash.variablePaths;
}
return new RouteResult(hash, params, splat);
}
function set(pathname, handler) {
var pathSegments = pathname.split('/');
var hash = this._hash;
var lastIndex = pathSegments.length - 1;
var splatIndex = pathname.indexOf('*');
var hasSplat = splatIndex >= 0;
if (hasSplat && splatIndex !== pathname.length - 1) {
throw SplatError(pathname);
}
for (var i = 0; i < pathSegments.length; i++) {
var segment = pathSegments[i];
if (!segment) {
continue;
}
if (hasSplat && i === lastIndex) {
hash = (
hash.variablePaths ||
(hash.variablePaths = new RouteNode(hash, segment, true))
);
if (!hash.isSplat) {
throw RouteConflictError(pathname, hash);
}
} else if (segment.indexOf(':') === 0) {
segment = segment.slice(1);
hash = (
hash.variablePaths ||
(hash.variablePaths = new RouteNode(hash, segment))
);
if (hash.segment !== segment || hash.isSplat) {
throw RouteConflictError(pathname, hash);
}
} else if (segment === '__proto__') {
hash = (
(
hash.hasOwnProperty('proto') &&
hash.proto
) ||
(hash.proto = new RouteNode(hash, segment))
);
} else {
hash = (
(
hash.staticPaths.hasOwnProperty(segment) &&
hash.staticPaths[segment]
) ||
(hash.staticPaths[segment] = new RouteNode(hash, segment))
);
}
}
if (hash.handler === null) {
hash.src = pathname;
hash.handler = handler;
} else {
throwRouteConflictError(pathname, hash);
}
}
function RouteNode(parent, segment, isSplat) {
this.parent = parent || null;
this.segment = segment || null;
this.handler = null;
this.staticPaths = {};
this.variablePaths = null;
this.isSplat = !!isSplat;
this.src = null;
}
function RouteResult(node, params, splat) {
this.handler = node && node.handler || null;
this.splat = splat;
this.params = params;
this.src = node && node.src || null;
}
function SplatError(pathname) {
var err = new Error('The splat * must be the last segment of the path');
err.pathname = pathname;
return err;
}
function RouteConflictError(pathname, hash) {
var conflictPath = hash.isSplat ? '' : '/';
while (hash && hash.parent) {
var prefix = (
!hash.isSplat &&
hash === hash.parent.variablePaths
) ? ':' : '';
conflictPath = '/' + prefix + hash.segment + conflictPath;
hash = hash.parent;
}
var err = new Error('Route conflict');
err.attemptedPath = pathname;
err.conflictPath = conflictPath;
return err;
}
// Break this out to prevent deoptimization of path.set
function throwRouteConflictError(pathname, hash) {
throw RouteConflictError(pathname, hash);
}