UNPKG

@patternplate/client

Version:

Universal javascript client application for patternplate

405 lines (345 loc) 11.7 kB
Function.prototype.$asyncbind = function $asyncbind(self, catcher) { "use strict"; if (!Function.prototype.$asyncbind) { Object.defineProperty(Function.prototype, "$asyncbind", { value: $asyncbind, enumerable: false, configurable: true, writable: true }); } if (!$asyncbind.trampoline) { $asyncbind.trampoline = function trampoline(t, x, s, e, u) { return function b(q) { while (q) { if (q.then) { q = q.then(b, e); return u ? undefined : q; } try { if (q.pop) { if (q.length) return q.pop() ? x.call(t) : q; q = s; } else q = q.call(t); } catch (r) { return e(r); } } }; }; } if (!$asyncbind.LazyThenable) { $asyncbind.LazyThenable = function () { function isThenable(obj) { return obj && obj instanceof Object && typeof obj.then === "function"; } function resolution(p, r, how) { try { var x = how ? how(r) : r; if (p === x) return p.reject(new TypeError("Promise resolution loop")); if (isThenable(x)) { x.then(function (y) { resolution(p, y); }, function (e) { p.reject(e); }); } else { p.resolve(x); } } catch (ex) { p.reject(ex); } } function Chained() {} ; Chained.prototype = { resolve: _unchained, reject: _unchained, then: thenChain }; function _unchained(v) {} function thenChain(res, rej) { this.resolve = res; this.reject = rej; } function then(res, rej) { var chain = new Chained(); try { this._resolver(function (value) { return isThenable(value) ? value.then(res, rej) : resolution(chain, value, res); }, function (ex) { resolution(chain, ex, rej); }); } catch (ex) { resolution(chain, ex, rej); } return chain; } function Thenable(resolver) { this._resolver = resolver; this.then = then; } ; Thenable.resolve = function (v) { return Thenable.isThenable(v) ? v : { then: function then(resolve) { return resolve(v); } }; }; Thenable.isThenable = isThenable; return Thenable; }(); $asyncbind.EagerThenable = $asyncbind.Thenable = ($asyncbind.EagerThenableFactory = function (tick) { tick = tick || typeof process === "object" && process.nextTick || typeof setImmediate === "function" && setImmediate || function (f) { setTimeout(f, 0); }; var soon = function () { var fq = [], fqStart = 0, bufferSize = 1024; function callQueue() { while (fq.length - fqStart) { try { fq[fqStart](); } catch (ex) {} fq[fqStart++] = undefined; if (fqStart === bufferSize) { fq.splice(0, bufferSize); fqStart = 0; } } } return function (fn) { fq.push(fn); if (fq.length - fqStart === 1) tick(callQueue); }; }(); function Zousan(func) { if (func) { var me = this; func(function (arg) { me.resolve(arg); }, function (arg) { me.reject(arg); }); } } Zousan.prototype = { resolve: function resolve(value) { if (this.state !== undefined) return; if (value === this) return this.reject(new TypeError("Attempt to resolve promise with self")); var me = this; if (value && (typeof value === "function" || typeof value === "object")) { try { var first = 0; var then = value.then; if (typeof then === "function") { then.call(value, function (ra) { if (!first++) { me.resolve(ra); } }, function (rr) { if (!first++) { me.reject(rr); } }); return; } } catch (e) { if (!first) this.reject(e); return; } } this.state = STATE_FULFILLED; this.v = value; if (me.c) soon(function () { for (var n = 0, l = me.c.length; n < l; n++) STATE_FULFILLED(me.c[n], value); }); }, reject: function reject(reason) { if (this.state !== undefined) return; this.state = STATE_REJECTED; this.v = reason; var clients = this.c; if (clients) soon(function () { for (var n = 0, l = clients.length; n < l; n++) STATE_REJECTED(clients[n], reason); }); }, then: function then(onF, onR) { var p = new Zousan(); var client = { y: onF, n: onR, p: p }; if (this.state === undefined) { if (this.c) this.c.push(client);else this.c = [client]; } else { var s = this.state, a = this.v; soon(function () { s(client, a); }); } return p; } }; function STATE_FULFILLED(c, arg) { if (typeof c.y === "function") { try { var yret = c.y.call(undefined, arg); c.p.resolve(yret); } catch (err) { c.p.reject(err); } } else c.p.resolve(arg); } function STATE_REJECTED(c, reason) { if (typeof c.n === "function") { try { var yret = c.n.call(undefined, reason); c.p.resolve(yret); } catch (err) { c.p.reject(err); } } else c.p.reject(reason); } Zousan.resolve = function (val) { if (val && val instanceof Zousan) return val; var z = new Zousan(); z.resolve(val); return z; }; Zousan.reject = function (err) { if (err && err instanceof Zousan) return err; var z = new Zousan(); z.reject(err); return z; }; Zousan.version = "2.3.3-nodent"; return Zousan; })(); } var resolver = this; switch (catcher) { case true: return new $asyncbind.Thenable(boundThen); case 0: return new $asyncbind.LazyThenable(boundThen); case undefined: boundThen.then = boundThen; return boundThen; default: return function () { try { return resolver.apply(self, arguments); } catch (ex) { return catcher(ex); } }; } function boundThen() { return resolver.apply(self, arguments); } }; function _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; } function _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"); } } const path = require("path"); const _require = require("@patternplate/api"), api = _require.api; const _require2 = require("@patternplate/load-docs"), loadDocsTree = _require2.loadDocsTree; const _require3 = require("@patternplate/load-meta"), loadMeta = _require3.loadMeta; const express = require("express"); const serve = require("serve-static"); const fetch = require("isomorphic-fetch"); const cors = require("cors"); const render = require("./render"); module.exports = client; function client(options) { return new Promise(function ($return, $error) { var apiRoute, mainRoute, apiStatic, app, appStatic; return api({ cwd: options.cwd, config: options.config, server: options.server }).then(function ($await_5) { apiRoute = $await_5; return main({ cwd: options.cwd, config: options.config }).then(function ($await_6) { mainRoute = $await_6; apiStatic = path.join(options.cwd, "static"); app = express().use("/api/static", cors(), serve(apiStatic)).use("/api/", apiRoute.middleware).get("/pattern/*", mainRoute).get("/doc/*", mainRoute).get("/", mainRoute); appStatic = path.join(__dirname, "static"); app.use("/static", serve(appStatic)); app.subscribe = apiRoute.subscribe; app.unsubscribe = apiRoute.unsubscribe; return $return(app); }.$asyncbind(this, $error), $error); }.$asyncbind(this, $error), $error); }.$asyncbind(this)); } function main(options) { return new Promise(function ($return, $error) { return $return(function mainRoute(req, res, next) { return new Promise(function ($return, $error) { var _options$config, _options$config$entry, entry, cover, base, response, _ref, _ref2, docs, patterns, tree, rendering; var $Try_3_Post = function () { return $return(); }.$asyncbind(this, $error); var $Try_3_Catch = function (err) { next(err); return $Try_3_Post(); }.$asyncbind(this, $error); try { _options$config = options.config, _options$config$entry = _options$config.entry, entry = _options$config$entry === void 0 ? [] : _options$config$entry, cover = _options$config.cover; base = options.base || "/"; if (req.path === base && typeof cover === "string") { return fetch(`${req.protocol}://${req.get("host")}/api/cover.html?base=${base}`, { credentials: "include" }).then(function ($await_7) { response = $await_7; return response.text().then(function ($await_8) { return $return(res.send($await_8)); }.$asyncbind(this, $Try_3_Catch), $Try_3_Catch); }.$asyncbind(this, $Try_3_Catch), $Try_3_Catch); } return Promise.all([loadDocsTree({ cwd: options.cwd, docs: options.config.docs, readme: options.config.readme }), loadMeta({ cwd: options.cwd, entry })]).then(function ($await_9) { _ref = $await_9, _ref2 = _slicedToArray(_ref, 2), docs = _ref2[0], patterns = _ref2[1].patterns; tree = { id: "root", children: patterns }; return render(req.url, { schema: { meta: tree, docs }, config: options.config, base }).then(function ($await_10) { rendering = $await_10; res.status(rendering.status); res.send(rendering.contents); return $Try_3_Post(); }.$asyncbind(this, $Try_3_Catch), $Try_3_Catch); }.$asyncbind(this, $Try_3_Catch), $Try_3_Catch); } catch (err) { $Try_3_Catch(err) } }.$asyncbind(this)); }); }.$asyncbind(this)); } //# sourceMappingURL=server.js.map