-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
220 lines (170 loc) · 5.71 KB
/
index.js
File metadata and controls
220 lines (170 loc) · 5.71 KB
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
216
217
218
219
220
'use strict'
const parserName = 'esprima'
const parser = require(parserName)
let authRoutes = {}
module.exports = function parseRoutes(routerObj, cfg, routePath) {
if (!cfg || !cfg.verbs)
cfg = { verbs: { create: 'create', read: 'read', update: 'update', delete: 'delete', list: 'list', auth: 'auth' } }
// FIXME: cfg = configFromObj() // Currently it's an all or nothing.
cfg.verbs.auth = 'auth'
let parsedRoutes = []
for (let routerProp of Object.keys(routerObj)) {
const routeObj = routerObj[routerProp]
// Parse auth object
if (routerProp && routerProp == cfg.verbs.auth) {
const route = parseAuthRoute(routePath, routeObj, cfg)
if (!route)
continue
authRoutes[route.path] = route
// NOTE: Protocol might want to do some special processing, feedback requested.
parsedRoutes.push(route)
continue
}
// Recurse into route object if needed (for nested routes)
if (typeof routeObj == 'object' && !Array.isArray(routeObj)) {
let recursePath = routerProp
if (routePath)
recursePath = `${routePath}.${routerProp}`
parsedRoutes = parsedRoutes.concat(parseRoutes(routeObj, cfg, recursePath))
continue
}
// Parse route function
if (typeof routeObj == 'function') {
const route = parseRoute(routePath, routerObj, routerProp, routeObj, cfg)
if (route)
parsedRoutes.push(route)
}
}
return parsedRoutes
}
function parseRoute(routePath, routeObj, routeName, routeFunc, cfg) {
let route = {
path: routePath,
params: paramsForFunction(routeFunc),
type: routeName,
}
route.fn = routeFunction(route, routeFunc, cfg)
switch (route.type) {
case cfg.verbs.create: break
case cfg.verbs.read: break
case cfg.verbs.update: break
case cfg.verbs.delete: break
case cfg.verbs.list: break
default:
route.type = 'all'
// Handle route path names that are only a function
if (!routeObj.fn)
if (!route.path)
route.path = routeName
else
route.path += `.${routeName}`
}
if (!route.path)
route.path = 'root'
return route
}
function paramsForFunction(func) {
try {
const ast = parser.parse(`(\n ${func.toString()} \n)`)
const program = parserName == 'babylon' ? ast.program : ast
return program
.body[0]
.expression
.params
.map(paramsForNode)
.filter(param => param !== 'callback')
} catch (e) {
return []
}
}
function paramsForNode(node) {
const maybe = function (x) {
return x || {} // optionals support
}
if (node.right && node.right.type == 'Literal') {
// Destructure only if string begins with '...' otherwise, it's likely an intentional default.
if (typeof node.right.value == 'string' && node.right.value.startsWith('...'))
return node.right.value
}
return node.name || maybe(node.left).name || `...${maybe(node.argument).name}`
}
function getPropertyFromObject(propertyName, object) {
const parts = propertyName.split('.')
let property = object
for (let part of parts ) {
if (!property[part])
return null
property = property[part]
}
return property
}
function routeFunction(route, routeObj, cfg) {
let log = function() {}
if (cfg && cfg.logger)
log = cfg.logger
return function(context, paramsObj, callback) {
let params = []
for (let param of route.params) {
// Destructure only if string begins with '...' otherwise, it's likely an intentional default. Also, set the default value to null to keep Params order.
if (param.startsWith('...')) {
param = param.substring(3)
const paramValue = getPropertyFromObject(param, paramsObj)
if (!paramValue)
params.push(null)
else
params.push(paramValue)
} else {
const paramValue = getPropertyFromObject(param, paramsObj)
if (!paramValue)
params.push(undefined)
else
params.push(paramValue)
}
}
// No auth function available, so run route function
if (!authRoutes[route.path] || !authRoutes[route.path].fn) {
params.push(callback)
routeObj.apply(context, params)
log({ route: route.path, context: context, params: params })
return
}
// Filter specified but does not contain this route, skip auth function
if (authRoutes[route.path].filter && !authRoutes[route.path].filter.includes(routeObj.name)) {
params.push(callback)
routeObj.apply(context, params)
log({ route: route.path, context: context, params: params })
return
}
// Run auth function
authRoutes[route.path].fn.apply(context, [(err) => {
if (err)
return callback(err)
params.push(callback)
log({ route: route.path, context: context, params: params })
routeObj.apply(context, params)
}])
}
}
function parseAuthRoute(routePath, routeObj, cfg) {
if (typeof routeObj == 'function') {
const route = { path: routePath, fn: routeObj, type: cfg.verbs.auth }
return route
} else if (typeof routeObj == 'object' && !Array.isArray(routeObj)) {
if (!routeObj.filter || !Array.isArray(routeObj.filter) || !routeObj.fn) {
console.log('[WARN]: Improper Auth function structure')
return undefined
}
Object.defineProperty(routeObj.fn, 'name', { value: 'auth' })
const route = { path: routePath, fn: routeObj.fn, filter: [], type: cfg.verbs.auth }
for (let filterFn of routeObj.filter) {
if (typeof filterFn === 'function') {
route.filter.push(filterFn.name)
} else if (typeof filterFn === 'string') {
route.filter.push(filterFn)
}
}
return route
}
console.log('[WARN]: Improper Auth function structure')
return undefined
}