UNPKG

@micahjonas/switchboard

Version:

Switchboard, a small fetch api router useful for cloudflare workers. Based on Trouter.

3 lines (2 loc) 3.32 kB
import n from"trouter";function t(n,e){return(t=Object.setPrototypeOf||function(n,t){return n.__proto__=t,n})(n,e)}function r(n,t,e){if(!n.s){if(e instanceof i){if(!e.s)return void(e.o=r.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(r.bind(null,n,t),r.bind(null,n,2));n.s=t,n.v=e;var o=n.o;o&&o(n)}}var o=function(n){var o,s;function h(t){return void 0===t&&(t={}),n.call(this,t)||this}s=n,(o=h).prototype=Object.create(s.prototype),o.prototype.constructor=o,t(o,s);var a=h.prototype;return a.handleRequest=function(n){try{var t,o,s,h,a=new URL(n.url),l=this.find(n.method,a.pathname),v=l.handlers,d=l.params;return Promise.resolve(f(function(){function a(n){return t?n:new Response(JSON.stringify(e,null," "),{status:400,statusText:"No response generated"})}var l,p=!0,y=!1,b=c(function(){return f(function(){return o=function(n){var t;if("undefined"!=typeof Symbol){if(Symbol.asyncIterator&&null!=(t=n[Symbol.asyncIterator]))return t.call(n);if(Symbol.iterator&&null!=(t=n[Symbol.iterator]))return t.call(n)}throw new TypeError("Object is not async iterable")}(v),function(n,t,e){for(var o;;){var f=n();if(u(f)&&(f=f.v),!f)return c;if(f.then){o=0;break}var c=e();if(c&&c.then){if(!u(c)){o=1;break}c=c.s}if(t){var s=t();if(s&&s.then&&!u(s)){o=2;break}}}var h=new i,a=r.bind(null,h,2);return(0===o?f.then(v):1===o?c.then(l):s.then(d)).then(void 0,a),h;function l(o){c=o;do{if(t&&(s=t())&&s.then&&!u(s))return void s.then(d).then(void 0,a);if(!(f=n())||u(f)&&!f.v)return void r(h,1,c);if(f.then)return void f.then(v).then(void 0,a);u(c=e())&&(c=c.v)}while(!c||!c.then);c.then(l).then(void 0,a)}function v(n){n?(c=e())&&c.then?c.then(l).then(void 0,a):l(c):r(h,1,c)}function d(){(f=n())?f.then?f.then(v).then(void 0,a):v(f):r(h,1,c)}}(function(){function n(n){function e(n){return!t&&(h=n,!p)}return p=s.done,s=n,t?e(!t&&s.value):Promise.resolve(!t&&s.value).then(e)}return t?!!n(!t&&o.next()):!!Promise.resolve(!t&&o.next()).then(n)},function(){return!!(p=!0)},function(){return Promise.resolve(h(n,d)).then(function(n){if(n&&n instanceof Response)return t=1,n})})},function(n){y=!0,l=n})},function(n,e){function r(r){if(t)return r;if(n)throw e;return e}var i=c(function(){var n=function(){if(!p&&null!=o.return)return Promise.resolve(o.return()).then(function(){})}();if(n&&n.then)return n.then(function(){})},function(n,t){if(y)throw l;if(n)throw t;return t});return i&&i.then?i.then(r):r(i)});return b&&b.then?b.then(a):a(b)},function(n){return new Response(JSON.stringify(n,null," "),{status:500,statusText:"Worker failed"})}))}catch(n){return Promise.reject(n)}},a.sendJsonResponse=function(n){return new Response(JSON.stringify(n),{headers:{"content-type":"text/html;charset=UTF-8"}})},h}(n),i=function(){function n(){}return n.prototype.then=function(t,e){var o=new n,i=this.s;if(i){var u=1&i?t:e;if(u){try{r(o,1,u(this.v))}catch(n){r(o,2,n)}return o}return this}return this.o=function(n){try{var i=n.v;1&n.s?r(o,1,t?t(i):i):e?r(o,1,e(i)):r(o,2,i)}catch(n){r(o,2,n)}},o},n}();function u(n){return n instanceof i&&1&n.s}function f(n,t){try{var e=n()}catch(n){return t(n)}return e&&e.then?e.then(void 0,t):e}function c(n,t){try{var e=n()}catch(n){return t(!0,n)}return e&&e.then?e.then(t.bind(null,!1),t.bind(null,!0)):t(!1,e)}export default o; //# sourceMappingURL=switchboard.module.js.map