UNPKG

12.9 kBJavaScriptView Raw
1Function.prototype.$asyncbind = function $asyncbind(self, catcher) {
2 "use strict";
3
4 if (!Function.prototype.$asyncbind) {
5 Object.defineProperty(Function.prototype, "$asyncbind", {
6 value: $asyncbind,
7 enumerable: false,
8 configurable: true,
9 writable: true
10 });
11 }
12
13 if (!$asyncbind.trampoline) {
14 $asyncbind.trampoline = function trampoline(t, x, s, e, u) {
15 return function b(q) {
16 while (q) {
17 if (q.then) {
18 q = q.then(b, e);
19 return u ? undefined : q;
20 }
21
22 try {
23 if (q.pop) {
24 if (q.length) return q.pop() ? x.call(t) : q;
25 q = s;
26 } else q = q.call(t);
27 } catch (r) {
28 return e(r);
29 }
30 }
31 };
32 };
33 }
34
35 if (!$asyncbind.LazyThenable) {
36 $asyncbind.LazyThenable = function () {
37 function isThenable(obj) {
38 return obj && obj instanceof Object && typeof obj.then === "function";
39 }
40
41 function resolution(p, r, how) {
42 try {
43 var x = how ? how(r) : r;
44 if (p === x) return p.reject(new TypeError("Promise resolution loop"));
45
46 if (isThenable(x)) {
47 x.then(function (y) {
48 resolution(p, y);
49 }, function (e) {
50 p.reject(e);
51 });
52 } else {
53 p.resolve(x);
54 }
55 } catch (ex) {
56 p.reject(ex);
57 }
58 }
59
60 function Chained() {}
61
62 ;
63 Chained.prototype = {
64 resolve: _unchained,
65 reject: _unchained,
66 then: thenChain
67 };
68
69 function _unchained(v) {}
70
71 function thenChain(res, rej) {
72 this.resolve = res;
73 this.reject = rej;
74 }
75
76 function then(res, rej) {
77 var chain = new Chained();
78
79 try {
80 this._resolver(function (value) {
81 return isThenable(value) ? value.then(res, rej) : resolution(chain, value, res);
82 }, function (ex) {
83 resolution(chain, ex, rej);
84 });
85 } catch (ex) {
86 resolution(chain, ex, rej);
87 }
88
89 return chain;
90 }
91
92 function Thenable(resolver) {
93 this._resolver = resolver;
94 this.then = then;
95 }
96
97 ;
98
99 Thenable.resolve = function (v) {
100 return Thenable.isThenable(v) ? v : {
101 then: function then(resolve) {
102 return resolve(v);
103 }
104 };
105 };
106
107 Thenable.isThenable = isThenable;
108 return Thenable;
109 }();
110
111 $asyncbind.EagerThenable = $asyncbind.Thenable = ($asyncbind.EagerThenableFactory = function (tick) {
112 tick = tick || typeof process === "object" && process.nextTick || typeof setImmediate === "function" && setImmediate || function (f) {
113 setTimeout(f, 0);
114 };
115
116 var soon = function () {
117 var fq = [],
118 fqStart = 0,
119 bufferSize = 1024;
120
121 function callQueue() {
122 while (fq.length - fqStart) {
123 try {
124 fq[fqStart]();
125 } catch (ex) {}
126
127 fq[fqStart++] = undefined;
128
129 if (fqStart === bufferSize) {
130 fq.splice(0, bufferSize);
131 fqStart = 0;
132 }
133 }
134 }
135
136 return function (fn) {
137 fq.push(fn);
138 if (fq.length - fqStart === 1) tick(callQueue);
139 };
140 }();
141
142 function Zousan(func) {
143 if (func) {
144 var me = this;
145 func(function (arg) {
146 me.resolve(arg);
147 }, function (arg) {
148 me.reject(arg);
149 });
150 }
151 }
152
153 Zousan.prototype = {
154 resolve: function resolve(value) {
155 if (this.state !== undefined) return;
156 if (value === this) return this.reject(new TypeError("Attempt to resolve promise with self"));
157 var me = this;
158
159 if (value && (typeof value === "function" || typeof value === "object")) {
160 try {
161 var first = 0;
162 var then = value.then;
163
164 if (typeof then === "function") {
165 then.call(value, function (ra) {
166 if (!first++) {
167 me.resolve(ra);
168 }
169 }, function (rr) {
170 if (!first++) {
171 me.reject(rr);
172 }
173 });
174 return;
175 }
176 } catch (e) {
177 if (!first) this.reject(e);
178 return;
179 }
180 }
181
182 this.state = STATE_FULFILLED;
183 this.v = value;
184 if (me.c) soon(function () {
185 for (var n = 0, l = me.c.length; n < l; n++) STATE_FULFILLED(me.c[n], value);
186 });
187 },
188 reject: function reject(reason) {
189 if (this.state !== undefined) return;
190 this.state = STATE_REJECTED;
191 this.v = reason;
192 var clients = this.c;
193 if (clients) soon(function () {
194 for (var n = 0, l = clients.length; n < l; n++) STATE_REJECTED(clients[n], reason);
195 });
196 },
197 then: function then(onF, onR) {
198 var p = new Zousan();
199 var client = {
200 y: onF,
201 n: onR,
202 p: p
203 };
204
205 if (this.state === undefined) {
206 if (this.c) this.c.push(client);else this.c = [client];
207 } else {
208 var s = this.state,
209 a = this.v;
210 soon(function () {
211 s(client, a);
212 });
213 }
214
215 return p;
216 }
217 };
218
219 function STATE_FULFILLED(c, arg) {
220 if (typeof c.y === "function") {
221 try {
222 var yret = c.y.call(undefined, arg);
223 c.p.resolve(yret);
224 } catch (err) {
225 c.p.reject(err);
226 }
227 } else c.p.resolve(arg);
228 }
229
230 function STATE_REJECTED(c, reason) {
231 if (typeof c.n === "function") {
232 try {
233 var yret = c.n.call(undefined, reason);
234 c.p.resolve(yret);
235 } catch (err) {
236 c.p.reject(err);
237 }
238 } else c.p.reject(reason);
239 }
240
241 Zousan.resolve = function (val) {
242 if (val && val instanceof Zousan) return val;
243 var z = new Zousan();
244 z.resolve(val);
245 return z;
246 };
247
248 Zousan.reject = function (err) {
249 if (err && err instanceof Zousan) return err;
250 var z = new Zousan();
251 z.reject(err);
252 return z;
253 };
254
255 Zousan.version = "2.3.3-nodent";
256 return Zousan;
257 })();
258 }
259
260 var resolver = this;
261
262 switch (catcher) {
263 case true:
264 return new $asyncbind.Thenable(boundThen);
265
266 case 0:
267 return new $asyncbind.LazyThenable(boundThen);
268
269 case undefined:
270 boundThen.then = boundThen;
271 return boundThen;
272
273 default:
274 return function () {
275 try {
276 return resolver.apply(self, arguments);
277 } catch (ex) {
278 return catcher(ex);
279 }
280 };
281 }
282
283 function boundThen() {
284 return resolver.apply(self, arguments);
285 }
286};
287
288function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
289
290function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
291
292const path = require("path");
293
294const _require = require("@patternplate/load-meta"),
295 loadMeta = _require.loadMeta;
296
297const AggregateError = require("aggregate-error");
298
299const fromString = require("require-from-string");
300
301const sander = require("@marionebl/sander");
302
303const unindent = require("unindent");
304
305const stringHash = require("string-hash");
306
307module.exports = demo;
308const BUNDLE_PATH = "/patternplate.node.components.js";
309const RENDER_PATH = "/patternplate.node.render.js";
310
311function demo(options) {
312 return new Promise(function ($return, $error) {
313 return $return(function demoRoute(req, res) {
314 return new Promise(function ($return, $error) {
315 var _options$config$entry, entry, cwd, id, _ref, patterns, found, _ref2, fs, getModule, bundle, component, context, render, content, error;
316
317 var $Try_3_Post = function () {
318 return $return();
319 }.$asyncbind(this, $error);
320
321 var $Try_3_Catch = function (err) {
322 error = Array.isArray(err) ? new AggregateError(err) : err;
323 console.error(error);
324 res.status(500).send(error.message);
325 return $Try_3_Post();
326 }.$asyncbind(this, $error);
327
328 try {
329 _options$config$entry = options.config.entry, entry = _options$config$entry === void 0 ? [] : _options$config$entry;
330 cwd = options.cwd;
331 id = req.params[0];
332 return loadMeta({
333 cwd,
334 entry
335 }).then(function ($await_4) {
336 _ref = $await_4, patterns = _ref.patterns;
337 found = patterns.find(pattern => pattern.id === id);
338
339 if (!found) {
340 return $return(res.sendStatus(404));
341 }
342
343 return wait(options.queue).then(function ($await_5) {
344 _ref2 = $await_5, fs = _ref2.fs;
345 getModule = fromFs(fs);
346 bundle = getModule(BUNDLE_PATH);
347 component = getComponent(bundle, found);
348 context = getContext(found);
349 render = component.render || getModule(RENDER_PATH);
350 return Promise.resolve(render(component, context)).then(function ($await_6) {
351 content = $await_6;
352 res.send(html(content, found));
353 return $Try_3_Post();
354 }.$asyncbind(this, $Try_3_Catch), $Try_3_Catch);
355 }.$asyncbind(this, $Try_3_Catch), $Try_3_Catch);
356 }.$asyncbind(this, $Try_3_Catch), $Try_3_Catch);
357 } catch (err) {
358 $Try_3_Catch(err)
359 }
360 }.$asyncbind(this));
361 });
362 }.$asyncbind(this));
363} // Todo: Provide basic description
364
365
366function wait(observable) {
367 return new Promise((resolve, reject) => {
368 const _observable$queue = _slicedToArray(observable.queue, 1),
369 _observable$queue$ = _observable$queue[0],
370 message = _observable$queue$ === void 0 ? {} : _observable$queue$;
371
372 switch (message.type) {
373 case 'done':
374 return resolve(message.payload);
375
376 case 'error':
377 return reject(message.payload);
378 }
379
380 observable.subscribe(queue => {
381 const _queue = _slicedToArray(queue, 1),
382 message = _queue[0];
383
384 switch (message.type) {
385 case 'done':
386 return resolve(message.payload);
387
388 case 'error':
389 return reject(message.payload);
390 }
391 }, reject);
392 });
393}
394
395function getComponent(components, data) {
396 const fileId = data.artifact.split(path.sep).join('/');
397 const top = components[fileId];
398 const moduleId = data.source.split(path.sep).join('/');
399
400 if (top[moduleId]) {
401 return top[moduleId];
402 }
403
404 return top;
405}
406
407function getContext(pattern) {
408 return {
409 dirname: path.dirname(pattern.path)
410 };
411}
412
413function fromFs(fs) {
414 return filename => {
415 const componentBundleSource = String(fs.readFileSync(filename));
416 return getExports(componentBundleSource, filename);
417 };
418}
419
420const exportsCache = new Map();
421
422function getExports(source, {
423 filename
424}) {
425 const hash = stringHash(source);
426
427 if (!exportsCache.has(hash)) {
428 exportsCache.set(hash, fromString(source, filename));
429 }
430
431 return exportsCache.get(hash);
432}
433
434function html(content, payload) {
435 const data = encodeURIComponent(JSON.stringify(payload));
436 return unindent(`
437 <!doctype html>
438 <html lang="en">
439 <head>
440 <!-- content.head -->
441 ${content.head || ""}
442 <style>
443 /* content.css */
444 ${content.css || ""}
445 </style>
446 </head>
447 <body>
448 <textarea style="display: none;" data-patternplate-vault="data-patternplate-vault">${data}</textarea>
449 <!-- content.before -->
450 ${content.before || ""}
451 <!-- content.html -->
452 <div data-patternplate-mount="data-patternplate-mount">${content.html || ""}</div>
453 <!-- content.after -->
454 ${content.after || ""}
455 <!-- ../ -> /api/ -->
456 <script src="../patternplate.web.components.js"></script>
457 <script src="../patternplate.web.probe.js"></script>
458 <script src="../patternplate.web.mount.js"></script>
459 <script src="../patternplate.web.demo.js"></script>
460 </body>
461 </html>
462 `);
463}
464//# sourceMappingURL=demo.js.map
\No newline at end of file