inferno-server
Version:
Provides methods to render HTML strings from Inferno elements
2 lines (1 loc) • 14.9 kB
JavaScript
;var e=require("inferno"),r=require("stream"),t="a runtime error occured! Use Inferno in development environment to find the error.",n=Array.isArray;function o(e){var r=typeof e;return"string"===r||"number"===r}function u(e){return void 0===e||null===e}function i(e){return null===e||!1===e||!0===e||void 0===e}function s(e){return"function"===typeof e}function a(e){return"string"===typeof e}function c(e){return"number"===typeof e}function l(e){return null===e}function f(e){return void 0===e}function h(e){throw e||(e=t),new Error("Inferno Error: "+e)}function d(){return d=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},d.apply(null,arguments)}var p=/["'&<>]/;function v(e){if(!p.test(e))return e;for(var r="",t="",n=0,o=0;o<e.length;++o){switch(e.charCodeAt(o)){case 34:t=""";break;case 39:t="'";break;case 38:t="&";break;case 60:t="<";break;case 62:t=">";break;default:continue}o>n&&(r+=e.slice(n,o)),r+=t,n=o+1}return r+e.slice(n,o)}var y=":A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD",g=y+"\\-.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040",m=new RegExp("^["+y+"]["+g+"]*$"),b={},T={};function x(e){if(void 0!==T[e])return!0;if(void 0!==b[e])return!1;if(m.test(e))return T[e]=!0,!0;return b[e]=!0,!1}var O=new Set(["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"]);function P(e,r,t){if(e.constructor.getDerivedStateFromProps)return d({},t,e.constructor.getDerivedStateFromProps(r,t));return t}function Q(r,t){var n=r.props||e.EMPTY_OBJ;return 32768&r.flags?r.type.render(n,r.ref,t):r.type(n,t)}function S(e){if(a(e))return v(e);var r="";for(var t in e){var n=e[t];o(n)&&(r+=t+":"+n+";")}return r}function k(e){var r=S(e);if(r)return' style="'+r+'"';return""}function F(){return F=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},F.apply(null,arguments)}function w(r,t,o){var f=r.flags,d=r.type,p=r.props||e.EMPTY_OBJ,y=r.children;if(0!==(14&f)){if(4&f){var g,m=new d(p,o),b=Boolean(d.getDerivedStateFromProps);if(m.$BS=!1,m.$SSR=!0,s(m.getChildContext)&&(g=m.getChildContext()),g=u(g)?o:F({},o,g),m.props===e.EMPTY_OBJ&&(m.props=p),m.context=o,!b&&s(m.componentWillMount)){m.$BR=!0,m.componentWillMount(),m.$BR=!1;var T=m.$PS;if(T){var S=m.state;if(null===S)m.state=T;else for(var _ in T)S[_]=T[_];m.$PSS=!1,m.$PS=null}}b&&(m.state=P(m,p,m.state));var j=m.render(p,m.state,m.context);if(i(j))return"\x3c!--!--\x3e";if(a(j))return v(j);if(c(j))return j+"";return w(j,r,g)}var N=Q(r,o);if(i(N))return"\x3c!--!--\x3e";if(a(N))return v(N);if(c(N))return N+"";return w(N,r,o)}if(0!==(481&f)){var C,B="<"+d,M=O.has(d),$=r.className;if(a($)?B+=' class="'+v($)+'"':c($)&&(B+=' class="'+$+'"'),!l(p)){for(var E in p){var D=p[E];switch(E){case"dangerouslySetInnerHTML":C=D.__html;break;case"style":u(p.style)||(B+=k(p.style));break;case"children":case"className":break;case"defaultValue":p.value||(B+=' value="'+(a(D)?v(D):D)+'"');break;case"defaultChecked":p.checked||!0!==D||(B+=' checked="'+D+'"');break;default:x(E)&&(a(D)?B+=" "+E+'="'+v(D)+'"':c(D)?B+=" "+E+'="'+D+'"':!0===D&&(B+=" "+E))}}"option"===d&&"undefined"!==typeof p.value&&p.value===t.props.value&&(B+=" selected")}if(M)B+=">";else{B+=">";var R=r.childFlags;if(2===R)B+=w(y,r,o);else if(12&R)for(var V=0,A=y.length;V<A;++V)B+=w(y[V],r,o);else 16===R?B+=""===y?" ":v(y):C&&(B+=C);M||(B+="</"+d+">")}if(String(d).match(/[\s\n/='"\0<>]/))throw B;return B}if(0!==(16&f))return""===y?" ":v(y);if(n(r)||0!==(8192&f)){var I=r.childFlags;if(2===I||n(r)&&0===r.length)return"\x3c!--!--\x3e";if(12&I||n(r)){for(var G=n(r)?r:y,W="",J=0,Y=G.length;J<Y;++J)W+=w(G[J],r,o);return W}}else h();return""}function _(e){return w(e,{},{})}function j(){return j=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},j.apply(null,arguments)}function N(e){var r=e.$PS;if(r){var t=e.state;e.state=null===t?r:j({},t,r),e.$PS=null}e.$BR=!1}function C(){return C=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},C.apply(null,arguments)}function B(e,r){e.prototype=Object.create(r.prototype),e.prototype.constructor=e,M(e,r)}function M(e,r){return M=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,r){return e.__proto__=r,e},M(e,r)}var $=function(r){function t(e){var t;return(t=r.call(this)||this).collector=[1/0],t.promises=[],t.pushQueue=t.pushQueue.bind(t),e&&t.renderVNodeToQueue(e,null,null),t}B(t,r);var o=t.prototype;return o._read=function(){setTimeout(this.pushQueue,0)},o.addToQueue=function(e,r){if(u(r))"string"===typeof e&&this.collector.length-1===0?this.push(e):"string"===typeof e&&"string"===typeof this.collector[this.collector.length-2]?this.collector[this.collector.length-2]+=e:this.collector.splice(-1,0,e);else{var t=this.promises[r].length-1;"string"===typeof this.promises[r][t]&&"string"===typeof e?this.promises[r][t]+=e:this.promises[r].push(e)}},o.pushQueue=function(){var e=this,r=this.collector[0];"string"===typeof r?(this.push(r),this.collector.shift()):r&&("object"===typeof r||s(r))&&s(r.then)?(r.then(function(r){var t;(t=e.collector).splice.apply(t,[0,1].concat(e.promises[r])),e.promises[r]=null,setTimeout(e.pushQueue,0)}),this.collector[0]=null):r===1/0&&this.emit("end")},o.renderVNodeToQueue=function(r,t,o){var d=this,p=r.flags,y=r.type,g=r.props||e.EMPTY_OBJ,m=r.children;if((14&p)>0)if(4&p){var b,T=new y(g,t),S=Boolean(y.getDerivedStateFromProps);if(T.$BS=!1,T.$SSR=!0,f(T.getChildContext)||(b=T.getChildContext()),u(b)||(t=C({},t,b)),T.props===e.EMPTY_OBJ&&(T.props=g),T.context=t,!S&&s(T.componentWillMount)&&(T.$BR=!0,T.componentWillMount(),N(T)),s(T.getInitialProps)){var F=T.getInitialProps(T.props,T.context);if(F){if(Promise.resolve(F)===F){var w=this.promises.push([])-1;return void this.addToQueue(F.then(function(e){"object"===typeof e&&(T.props=C({},T.props,e));var r=T.render(T.props,T.state,T.context);return i(r)?d.addToQueue("\x3c!--!--\x3e",w):a(r)?d.addToQueue(v(r),w):c(r)?d.addToQueue(r+"",w):d.renderVNodeToQueue(r,T.context,w),setTimeout(d.pushQueue,0),w}),o)}T.props=C({},T.props,F)}}S&&(T.state=P(T,g,T.state));var _=T.render(T.props,T.state,T.context);i(_)?this.addToQueue("\x3c!--!--\x3e",o):a(_)?this.addToQueue(v(_),o):c(_)?this.addToQueue(_+"",o):this.renderVNodeToQueue(_,t,o)}else{var j=Q(r,t);i(j)?this.addToQueue("\x3c!--!--\x3e",o):a(j)?this.addToQueue(v(j),o):c(j)?this.addToQueue(j+"",o):this.renderVNodeToQueue(j,t,o)}else if((481&p)>0){var B,M="<"+y,$=O.has(y),E=r.className;if(a(E)?M+=' class="'+v(E)+'"':c(E)&&(M+=' class="'+E+'"'),!l(g))for(var D in g){var R=g[D];switch(D){case"dangerouslySetInnerHTML":B=R.__html;break;case"style":u(g.style)||(M+=k(g.style));break;case"children":case"className":break;case"defaultValue":g.value||(M+=' value="'+(a(R)?v(R):R)+'"');break;case"defaultChecked":g.checked||!0!==R||(M+=' checked="'+R+'"');break;default:x(D)&&(a(R)?M+=" "+D+'="'+v(R)+'"':c(R)?M+=" "+D+'="'+R+'"':!0===R&&(M+=" "+D))}}if(M+=">",String(y).match(/[\s\n/='"\0<>]/))throw M;if($)this.addToQueue(M,o);else{var V=r.childFlags;if(2===V)return this.addToQueue(M,o),this.renderVNodeToQueue(m,t,o),void this.addToQueue("</"+y+">",o);if(16===V)return this.addToQueue(M,o),this.addToQueue(""===m?" ":v(m+""),o),void this.addToQueue("</"+y+">",o);if(12&V){this.addToQueue(M,o);for(var A=0,I=m.length;A<I;++A)this.renderVNodeToQueue(m[A],t,o);return void this.addToQueue("</"+y+">",o)}if(B)return void this.addToQueue(M+B+"</"+y+">",o);$||this.addToQueue(M+"</"+y+">",o)}}else if((16&p)>0)this.addToQueue(""===m?" ":v(m),o);else if(n(r)||0!==(8192&p)){var G=r.childFlags;if(2===G||n(r)&&0===r.length)this.addToQueue("\x3c!--!--\x3e",o);else if(12&G||n(r))for(var W=n(r)?r:r.children,J=0,Y=W.length;J<Y;++J)this.renderVNodeToQueue(W[J],t,o)}else h()},t}(r.Readable);function E(e){return new $(e)}function D(){return D=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},D.apply(null,arguments)}function R(){/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */var e,r,t="function"==typeof Symbol?Symbol:{},n=t.iterator||"@@iterator",o=t.toStringTag||"@@toStringTag";function u(t,n,o,u){var a=n&&n.prototype instanceof s?n:s,c=Object.create(a.prototype);return V(c,"_invoke",function(t,n,o){var u,s,a,c=0,l=o||[],f=!1,h={p:0,n:0,v:e,a:d,f:d.bind(e,4),d:function(r,t){return u=r,s=0,a=e,h.n=t,i}};function d(t,n){for(s=t,a=n,r=0;!f&&c&&!o&&r<l.length;r++){var o,u=l[r],d=h.p,p=u[2];t>3?(o=p===n)&&(a=u[(s=u[4])?5:(s=3,3)],u[4]=u[5]=e):u[0]<=d&&((o=t<2&&d<u[1])?(s=0,h.v=n,h.n=u[1]):d<p&&(o=t<3||u[0]>n||n>p)&&(u[4]=t,u[5]=n,h.n=p,s=0))}if(o||t>1)return i;throw f=!0,n}return function(o,l,p){if(c>1)throw TypeError("Generator is already running");for(f&&1===l&&d(l,p),s=l,a=p;(r=s<2?e:a)||!f;){u||(s?s<3?(s>1&&(h.n=-1),d(s,a)):h.n=a:h.v=a);try{if(c=2,u){if(s||(o="next"),r=u[o]){if(!(r=r.call(u,a)))throw TypeError("iterator result is not an object");if(!r.done)return r;a=r.value,s<2&&(s=0)}else 1===s&&(r=u.return)&&r.call(u),s<2&&(a=TypeError("The iterator does not provide a '"+o+"' method"),s=1);u=e}else if((r=(f=h.n<0)?a:t.call(n,h))!==i)break}catch(r){u=e,s=1,a=r}finally{c=1}}return{value:r,done:f}}}(t,o,u),!0),c}var i={};function s(){}function a(){}function c(){}r=Object.getPrototypeOf;var l=[][n]?r(r([][n]())):(V(r={},n,function(){return this}),r),f=c.prototype=s.prototype=Object.create(l);function h(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,c):(e.__proto__=c,V(e,o,"GeneratorFunction")),e.prototype=Object.create(f),e}return a.prototype=c,V(f,"constructor",c),V(c,"constructor",a),a.displayName="GeneratorFunction",V(c,o,"GeneratorFunction"),V(f),V(f,o,"Generator"),V(f,n,function(){return this}),V(f,"toString",function(){return"[object Generator]"}),(R=function(){return{w:u,m:h}})()}function V(e,r,t,n){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}V=function(e,r,t,n){function u(r,t){V(e,r,function(e){return this._invoke(r,t,e)})}r?o?o(e,r,{value:t,enumerable:!n,configurable:!n,writable:!n}):e[r]=t:(u("next",0),u("throw",1),u("return",2))},V(e,r,t,n)}function A(e,r,t,n,o,u,i){try{var s=e[u](i),a=s.value}catch(e){return void t(e)}s.done?r(a):Promise.resolve(a).then(n,o)}function I(e){return function(){var r=this,t=arguments;return new Promise(function(n,o){var u=e.apply(r,t);function i(e){A(u,n,o,i,s,"next",e)}function s(e){A(u,n,o,i,s,"throw",e)}i(void 0)})}}function G(e,r){e.prototype=Object.create(r.prototype),e.prototype.constructor=e,W(e,r)}function W(e,r){return W=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,r){return e.__proto__=r,e},W(e,r)}var J=Promise.resolve(),Y=function(e){function r(r){var t;return(t=e.call(this)||this).initNode=void 0,t.started=!1,t.initNode=r,t}G(r,e);var t=r.prototype;return t._read=function(){var e=this;if(this.started)return;this.started=!0,J.then(function(){return e.renderNode(e.initNode,null)}).then(function(){e.push(null)}).catch(function(r){e.emit("error",r)})},t.renderNode=function(e,r){var t=e.flags;if((14&t)>0)return this.renderComponent(e,r,4&t);if((481&t)>0)return this.renderElement(e,r);if(n(e)||0!==(8192&t))return this.renderArrayOrFragment(e,r);this.renderText(e)},t.renderArrayOrFragment=function(e,r){var t=this,o=e.childFlags;if(2===o||n(e)&&0===e.length)return this.push("\x3c!--!--\x3e");if(12&o||n(e))return(n(e)?e:e.children).reduce(function(){var e=I(R().m(function e(n,o){return R().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,n.then(I(R().m(function e(){return R().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,Promise.resolve(t.renderNode(o,r)).then(function(){return!!(16&o.flags)});case 1:return e.a(2,e.v)}},e)})));case 1:return e.a(2,e.v)}},e)}));return function(r,t){return e.apply(this,arguments)}}(),Promise.resolve(!1))},t.renderComponent=function(e,r,t){var n=this,o=e.type,l=e.props;if(!t){var f=Q(e,r);if(i(f))return this.push("\x3c!--!--\x3e");if(a(f))return this.push(v(f));if(c(f))return this.push(f+"");return this.renderNode(f,r)}var h,d=new o(l,r),p=Boolean(o.getDerivedStateFromProps);return d.$BS=!1,d.$SSR=!0,s(d.getChildContext)&&(h=d.getChildContext()),u(h)||(r=D({},r,h)),d.context=r,d.$BR=!0,Promise.resolve(!p&&(null==d.componentWillMount?void 0:d.componentWillMount())).then(function(){N(d),p&&(d.state=P(d,l,d.state));var e=d.render(d.props,d.state,d.context);if(i(e))return n.push("\x3c!--!--\x3e");if(a(e))return n.push(v(e));if(c(e))return n.push(e+"");return n.renderNode(e,r)})},t.renderChildren=function(e,r,t){var n=this;if(2===t)return this.renderNode(e,r);if(16===t)return this.push(""===e?" ":v(e+""));if(12&t)return e.reduce(function(){var e=I(R().m(function e(t,o){return R().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,t.then(I(R().m(function e(){return R().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,Promise.resolve(n.renderNode(o,r)).then(function(){return!!(16&o.flags)});case 1:return e.a(2,e.v)}},e)})));case 1:return e.a(2,e.v)}},e)}));return function(r,t){return e.apply(this,arguments)}}(),Promise.resolve(!1))},t.renderText=function(e){this.push(""===e.children?" ":v(e.children))},t.renderElement=function(e,r){var t,n=this,o=e.type,i=e.props,s="<"+o,f=O.has(o),h=e.className;if(a(h)?s+=' class="'+v(h)+'"':c(h)&&(s+=' class="'+h+'"'),!l(i))for(var d in i){var p=i[d];switch(d){case"dangerouslySetInnerHTML":t=p.__html;break;case"style":u(i.style)||(s+=k(i.style));break;case"children":case"className":break;case"defaultValue":i.value||(s+=' value="'+(a(p)?v(p):p)+'"');break;case"defaultChecked":i.checked||!0!==p||(s+=' checked="'+p+'"');break;default:if(x(d)){a(p)?s+=" "+d+'="'+v(p)+'"':c(p)?s+=" "+d+'="'+p+'"':!0===p&&(s+=" "+d);break}}}if(s+=">",this.push(s),String(o).match(/[\s\n/='"\0<>]/))throw s;if(f)return;if(t)return this.push(t),void this.push("</"+o+">");var y=e.childFlags;if(1===y)return void this.push("</"+o+">");return Promise.resolve(this.renderChildren(e.children,r,y)).then(function(){n.push("</"+o+">")})},r}(r.Readable);function q(e){return new Y(e)}exports.RenderQueueStream=$,exports.RenderStream=Y,exports.renderToStaticMarkup=_,exports.renderToString=_,exports.streamAsStaticMarkup=q,exports.streamAsString=q,exports.streamQueueAsStaticMarkup=E,exports.streamQueueAsString=E;