-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (132 loc) · 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
/* global encodeURIComponent, URL, URLSearchParams */
import { format } from 'url'
import { join } from 'path'
import { compile } from 'path-to-regexp'
import camelCase from 'camelcase'
const hasOwn = Object.prototype.hasOwn ? Object.prototype.hasOwn : Object.prototype.hasOwnProperty
const supportedExpressMethods = [
'checkout', 'copy', 'delete', 'get', 'head', 'lock', 'merge', 'mkactivity', 'mkcol', 'move', 'm-search', 'notify', 'options', 'patch', 'post', 'purge', 'put', 'report', 'search', 'subscribe', 'trace', 'unlock', 'unsubscribe'
]
// If no host is provided a relative URL is created, otherwise the host is used
const pathnameToUrl = (pathname, query = {}, { host = null } = {}) => {
const fakeHost = 'http://fakehost'
const resultUrl = new URL(pathname, host || fakeHost)
resultUrl.search = new URLSearchParams(query)
const cleansedUrl = format(resultUrl, { auth: false, fragment: false })
return host ? cleansedUrl : cleansedUrl.slice(fakeHost.length)
}
class Route {
constructor (path, { app, mountpath, registry } = {}) {
this.path = path
this.app = app
this.subModuleMountPath = mountpath
this.registry = registry
}
get mountpath () {
return this.app.mountpath || this.app.reversicalMountpath || this.subModuleMountPath
}
get routePath () {
const chainedMountPath = this.registry.chainedRoutePath(this.mountpath)
return join(chainedMountPath, this.path)
}
render (params = {}, query = {}, { host = null } = {}) {
try {
const toPath = compile(this.routePath, { encode: encodeURIComponent })
const pathname = toPath(params || {})
return pathnameToUrl(pathname, query || {}, { host })
} catch (e) {
if (e instanceof TypeError) {
console.log(`failed to expand "${this.routePath}" with params "${JSON.stringify(params)}"`, e.stack)
return
}
throw e
}
}
}
class RouteRegistry {
constructor ({ parentRegistry, mountpath } = {}) {
this.routes = {}
this.parentRegistry = parentRegistry
this.mountpath = mountpath
}
register (name, path, { app, mountpath } = {}) {
const route = new Route(path, { app, mountpath, registry: this })
if (!this.routes[name] && !hasOwn.call(this, name)) {
this.routes[name] = route
this[camelCase(name)] = this.routes[name].render.bind(this.routes[name])
} else if (this.routes[name].path !== path) {
throw new Error(`NamedRouting: registered resource for "${name}" is already registered with different path (${this.routes[name].path} conflicts with ${path}).`)
}
// route is already registered, do nothing
}
addSubmodule (name, registry) {
if (!this.routes[name] && !hasOwn.call(this, name)) {
this.routes[name] = name
this[camelCase(name)] = registry
}
}
chainedRoutePath (routeMountpath) {
const parentRegistries = []
const maxDepths = 10
let i = 0
let parentRouteRegistry = this.parentRegistry
while (parentRouteRegistry) {
if (i >= maxDepths) { break } else { i++ }
parentRegistries.unshift(parentRouteRegistry)
parentRouteRegistry = parentRouteRegistry.parentRegistry
}
const paths = []
.concat(parentRegistries.map(r => r.mountpath))
.filter(x => !!x)
paths.push(routeMountpath)
return paths.length > 0 ? join(...paths) : ''
}
}
// initialize on first import
export let routes = new RouteRegistry()
export const reset = () => {
routes = new RouteRegistry()
}
export class NamedRouter {
constructor (app, { mountpath = null, routeRegistry = routes } = {}) {
this.app = app
this.mountpath = mountpath
this.routes = routeRegistry
}
route (name, path) {
this.routes.register(name, path, { app: this.app, mountpath: this.mountpath })
return this.app.route(path)
}
all (name, path, handler) {
this.routes.register(name, path, { app: this.app, mountpath: this.mountpath })
return this.app.all(path, handler)
}
use (...args) {
if (args.length === 3) { // named router
const [subModuleName, mountpath, appWithNamedRouterContext] = args
const registry = new RouteRegistry({ parentRegistry: this.routes, mountpath })
this.routes.addSubmodule(subModuleName, registry)
const bindRouter = app => {
extendRouterWithMountpath(app, mountpath)
return new NamedRouter(app, { mountpath, routeRegistry: registry })
}
return this.app.use(mountpath, appWithNamedRouterContext(bindRouter))
} else if (args.length === 2) { // not named router
const [mountpath, appOrRouter] = args
extendRouterWithMountpath(appOrRouter, mountpath)
return this.app.use(mountpath, appOrRouter)
}
return this.app.use(...args)
}
}
for (const method of supportedExpressMethods) {
NamedRouter.prototype[method] = function (name, path, handler) {
this.routes.register(name, path, { app: this.app })
return this.app[method](path, handler)
}
}
function extendRouterWithMountpath (router, mountpath) {
if (router && !router.mountpath) {
router.reversicalMountpath = mountpath
}
}