This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
nap.js
273 lines (272 loc) · 9.02 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
nap = function environment(nap_window) {
function noop() {}
function into(node) {
return function(err, res) {
200 == res.statusCode && (res.headers.contentType && "application/x.nap.view" != res.headers.contentType || isFn(res.body) && node && (node.dispatchEvent && node.dispatchEvent(new CustomEvent("update")),
res.body(node)));
};
}
function is(n, s) {
return matchesSelector.call(n, s);
}
function isFn(inst) {
return "function" == typeof inst;
}
function isStr(inst) {
return "string" == typeof inst;
}
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 = [];
return Object.keys(map).forEach(function(key) {
args.push(wants(key, map[key]));
}), args.push(error), dispatch.apply(null, args);
};
}
function uses(comparator, respond) {
return function(key, fn) {
return function(req, res) {
return comparator(req, key) ? (fn.call(null, req, respond(key, res)), !0) : void 0;
};
};
}
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) {
return res(null, error(code)), !0;
};
}
function bySelector() {
var options = toArray(arguments).reduce(function(curr, next) {
return isStr(next) ? curr.push({
selector: next
}) : curr[curr.length - 1].fn = next, curr;
}, []);
return function(node, cb) {
var called = !1, cb = cb || noop;
called = options.some(function(option) {
return is(node, option.selector) ? (option.fn.call(null, node), cb(null, option.selector),
!0) : void 0;
}), 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 = (nap_document.documentElement, {}), routes = rhumb.create(), middleware = [];
return web.resource = function(name, ptn, handler) {
return 1 == arguments.length ? resources[name] : (2 == arguments.length && (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
};
}), web);
}, web.req = function(path, cb) {
var req = isStr(path) ? {
uri: path
} : path, cb = cb || noop;
req.web = web, req.method || (req.method = "get"), "get" == req.method && 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)
};
return req.params = match.params, match.fn.call(null, req, cb), web;
}, web.use = function() {
return arguments.length ? (middleware = toArray(arguments).reverse().concat(middleware),
web) : web;
}, web.uri = function(ptn, params) {
var meta = resources[ptn];
meta && (ptn = meta.ptn);
var parts = rhumb._parse(ptn);
return parts.reduce(function(uri, part) {
return "var" == part.type ? [ uri, params[part.input] ].join("/") : [ uri, part.input ].join("/");
}, "");
}, web;
}
var nap = {
environment: environment
}, nap_window = nap_window || window, nap_document = nap_window.document;
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
};
var root = nap_document.documentElement, matchesSelector = root.matchesSelector || root.webkitMatchesSelector || root.mozMatchesSelector || root.msMatchesSelector || root.oMatchesSelector;
return nap;
}(), function(root) {
function findIn(parts, tree) {
var params = {}, find = function(remaining, node) {
var part = remaining.shift();
if (!part) return node.leaf || !1;
if (node.fixed && part in node.fixed) return find(remaining, node.fixed[part]);
if (node.partial) {
var tests = node.partial.tests, found = tests.some(function(partial) {
if (partial.ptn.test(part)) {
var match = part.match(partial.ptn);
return partial.vars.forEach(function(d, i) {
params[d] = match[i + 1];
}), node = partial, !0;
}
});
if (found) return find(remaining, node);
}
return node.var ? (params[node.var.name] = part, find(remaining, node.var)) : !1;
}, found = find(parts, tree, params);
return found ? {
fn: found,
params: params
} : !1;
}
function isArr(inst) {
return inst instanceof Array;
}
function create() {
function updateTree(parts, node, fn) {
var peek, part = parts.shift(), more = !!parts.length;
if (isArr(part)) return node.leaf = fn, void updateTree(part, node, fn);
if (part) {
if ("fixed" == part.type) node.fixed || (node.fixed = {}), peek = node.fixed[part.input] || (node.fixed[part.input] = {}); else if ("var" == part.type) {
if (node.var) throw new Error("Ambiguity");
peek = node.var = {}, peek.name = part.input;
} else if (part.type = "partial") {
if (node.partial && node.partial.names[part.name]) throw new Error("Ambiguity");
node.partial || (node.partial = {
names: {},
tests: []
}), peek = {}, peek.ptn = part.input, peek.vars = part.vars, node.partial.names[part.name] = peek,
node.partial.tests.push(peek);
}
more ? updateTree(parts, peek, fn) : peek.leaf = fn;
}
}
var router = {}, tree = {};
return router.add = function(ptn, callback) {
updateTree(parse(ptn), tree, callback);
}, router.match = function(path) {
var parts = path.split("/").filter(falsy), match = findIn(parts, tree);
return match ? match.fn.apply(match.fn, [ match.params ]) : void 0;
}, router;
}
function falsy(d) {
return !!d;
}
function parse(ptn) {
function parseVar(part) {
var match = part.match(variable);
return {
type: "var",
input: match[1]
};
}
function parseFixed(part) {
return {
type: "fixed",
input: part
};
}
function parsePartial(part) {
for (var match = part.match(partial), ptn = "", len = part.length, i = 0; len > i && match; ) i += match[0].length,
match[1] && (ptn += match[1]), ptn += "([\\w-]+)", match[3] && (ptn += match[3]),
match = part.substr(i).match(partial);
var vars = [], name = part.replace(/{([\w-]+)}/g, function(p, d) {
return vars.push(d), "{var}";
});
return {
type: "partial",
input: new RegExp(ptn),
name: name,
vars: vars
};
}
function parsePtn(ptn) {
return ptn.split("/").filter(falsy).map(function(d) {
return variable.test(d) ? parseVar(d) : partial.test(d) ? parsePartial(d) : parseFixed(d);
});
}
function parseOptional(ptn) {
for (var curr, out = "", list = [], i = 0, len = ptn.length, onePart = !0; onePart && len > i; ) {
switch (curr = ptn[i]) {
case ")":
case "(":
onePart = !1;
break;
default:
out += curr;
}
i++;
}
if (!onePart) {
var next = parseOptional(ptn.substr(i + 1));
next.length && list.push(next);
}
return parsePtn(out).concat(list);
}
var variable = /^{(\w+)}$/, partial = /([\w'-]+)?{([\w-]+)}([\w'-]+)?/;
return "/" == ptn.trim() ? [ {
type: "fixed",
input: ""
} ] : -1 == ptn.indexOf("(") ? parsePtn(ptn) : parseOptional(ptn);
}
var rhumb = create();
return rhumb.create = create, rhumb._parse = parse, rhumb._findInTree = findIn,
root.rhumb = rhumb, "function" == typeof define && define.amd && define("rhumb", [], function() {
return rhumb;
}), rhumb;
}(window);