-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnap.js
243 lines (198 loc) · 4.64 KB
/
nap.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var rhumb = require('@websdk/rhumb')
var nap = { environment: {} }
nap.web = newWeb
nap.is = is
nap.into = into
nap.negotiate = {
selector : bySelector
, method : dispatcher(uses(checkMethod, setMethod), errorsWith(405))
, accept : dispatcher(uses(checkAcceptType, setContentType), errorsWith(415))
}
nap.responses = {
ok : ok
, error : error
}
function noop(){}
function into(node) {
return function(err, res) {
if(res.statusCode != 200) return
if(res.headers.contentType && res.headers.contentType != "application/x.nap.view") return
if(!isFn(res.body)) return
if(!node) return
// TODO: This breaks d3; presumably because there's a discrepancy in domino CustomEvent vs. jsdom CustomEvent
// node.dispatchEvent && node.dispatchEvent(new CustomEvent("update"))
res.body(node)
}
}
function is(n, s) {
return n.matches(s)
}
function isFn(inst){
return typeof inst === "function"
}
function isStr(inst){
return typeof inst === "string"
}
function toArray(args) {
return Array.prototype.slice.call(args)
}
function ok(data) {
return {
body : data
, statusCode : 200
, headers : {}
}
}
function error(code) {
return {
statusCode : code
, headers : {}
}
}
function notFound(req, res) {
res(null, error(404))
}
function dispatcher(wants, error) {
return function(map) {
var args = []
Object.keys(map).forEach(function(key) {
args.push(wants(key, map[key]))
})
args.push(error)
return dispatch.apply(null, args)
}
}
function uses(comparator, respond) {
return function(key, fn) {
return function(req, res) {
if(comparator(req, key)) {
fn.call(null, req, respond(key, res))
return true
}
}
}
}
function dispatch() {
var fns = toArray(arguments)
return function() {
var args = toArray(arguments)
return fns.some(function(fn) {
return fn.apply(null, args)
})
}
}
function checkMethod(req, method) {
return req.method == method
}
function setMethod(type, res) {
return function(err, data) {
data.method = type
res(err, data)
}
}
function checkAcceptType(req, type) {
return req.headers.accept == type
}
function setContentType(type, res) {
return function(err, data) {
data.headers.contentType = type
res(err, data)
}
}
function errorsWith(code) {
return function(req, res) {
res(null, error(code))
return true
}
}
function bySelector(){
var options = toArray(arguments)
.reduce(
function(curr, next){
if(isStr(next)) curr.push({ selector : next })
else curr[curr.length - 1].fn = next
return curr
}
, []
)
return function(node, cb){
var called = false
, cb = cb || noop
called = options.some(function(option){
if(is(node, option.selector)){
option.fn.call(null, node)
cb(null, option.selector)
return true
}
})
if(!called) cb("No matching selector")
}
}
function wrap(fn, stack) {
return stack.reduce(middleware, fn)
}
function middleware(next, middle) {
return function(req, res) {
middle.call(null, req, res, next)
}
}
function newWeb(){
var web = {}
, resources = {}
, routes = rhumb.create()
, middleware = []
web.resource = function(name, ptn, handler){
if(arguments.length == 1) return resources[name]
if(arguments.length == 2) {
handler = ptn
ptn = name
}
handler = wrap(handler, middleware)
resources[name] = {
name : name
, ptn : ptn
, handler : handler
}
routes.add(ptn, function(params){
return {
fn : handler
, params : params
}
})
return web
}
web.req = function(path, cb){
var req = isStr(path) ? {uri: path} : path
, cb = cb || noop
req.web = web
req.method || (req.method = "get")
req.method == "get" && (delete req["body"])
req.headers || (req.headers = {})
req.headers.accept || (req.headers.accept = "application/x.nap.view")
var match = routes.match(req.uri) || { fn : wrap(notFound, middleware) }
req.params = match.params
match.fn.call(null, req, cb)
return web
}
web.use = function() {
if(!arguments.length) return web
middleware = toArray(arguments).reverse().concat(middleware)
return web
}
web.uri = function(ptn, params){
var meta = resources[ptn]
if(meta) ptn = meta.ptn
var parts = rhumb._parse(ptn)
return parts.reduce(
function(uri, part){
if(part.type == "var"){
return [uri , params[part.input]].join("/")
}
return [uri , part.input].join("/")
}
, ""
)
}
return web
}
module.exports = nap