UNPKG

@nguniversal/express-engine

Version:

Express Engine for running Server Angular Apps

58 lines (56 loc) 6.82 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("fs"),require("@angular/compiler"),require("@angular/core"),require("@angular/platform-server"),require("@nguniversal/express-engine/tokens")):"function"==typeof define&&define.amd?define("@nguniversal/express-engine",["exports","fs","@angular/compiler","@angular/core","@angular/platform-server","@nguniversal/express-engine/tokens"],r):r(((e="undefined"!=typeof globalThis?globalThis:e||self).nguniversal=e.nguniversal||{},e.nguniversal.expressEngine={}),e.fs,e.ng.compiler,e.ng.core,e.ng.platformServer,e.nguniversal.expressEngine.tokens)}(this,(function(e,r,t,n,o,i){"use strict";var u=function(){function e(){}return e.prototype.get=function(e){return new Promise((function(t,n){r.readFile(e,(function(e,r){if(e)return n(e);t(r.toString())}))}))},e}(); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */function a(e,r,t,n){return new(t||(t=Promise))((function(o,i){function u(e){try{c(n.next(e))}catch(e){i(e)}}function a(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){e.done?o(e.value):function r(e){return e instanceof t?e:new t((function(r){r(e)}))}(e.value).then(u,a)}c((n=n.apply(e,r||[])).next())}))}function c(e,r){var t,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function c(i){if(t)throw new TypeError("Generator is already executing.");for(;u;)try{if(t=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=r.call(e,u)}catch(e){i=[6,e],n=0}finally{t=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}}function s(e,r){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var n,o,i=t.call(e),u=[];try{for(;(void 0===r||r-- >0)&&!(n=i.next()).done;)u.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(t=i.return)&&t.call(i)}finally{if(o)throw o.error}}return u}var l=function(){function e(e,r){void 0===r&&(r=[]),this.moduleOrFactory=e,this.providers=r,this.factoryCacheMap=new Map,this.templateCache={}}return e.prototype.getCompiler=function(){return o.platformDynamicServer().injector.get(n.CompilerFactory).createCompiler([{providers:[{provide:t.ResourceLoader,useClass:u,deps:[]}]}])},e.prototype.render=function(e){return a(this,void 0,void 0,(function(){var r,t,n;return c(this,(function(i){switch(i.label){case 0:return(r=e.document)?[3,2]:[4,this.getDocument(e.documentFilePath)];case 1:r=i.sent(),i.label=2;case 2:return t=function u(){for(var e=[],r=0;r<arguments.length;r++)e=e.concat(s(arguments[r]));return e}(e.providers||[],this.providers||[],[{provide:o.INITIAL_CONFIG,useValue:{document:r,url:e.url}}]),[4,this.getFactory(this.moduleOrFactory||e.bootstrap)];case 3:return n=i.sent(),[2,o.renderModuleFactory(n,{extraProviders:t})]}}))}))},e.prototype.getFactory=function(e){return a(this,void 0,void 0,(function(){var r,t;return c(this,(function(o){switch(o.label){case 0:return e instanceof n.NgModuleFactory?[2,e]:[3,1];case 1:return(r=this.factoryCacheMap.get(e))?[2,r]:[4,this.getCompiler().compileModuleAsync(e)];case 2:return t=o.sent(),this.factoryCacheMap.set(e,t),[2,t]}}))}))},e.prototype.getDocument=function(e){var t=this.templateCache[e]=this.templateCache[e]||r.readFileSync(e).toString();return Promise.resolve(t)},e}(),p={}; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ e.ngExpressEngine=function f(e){var t=new l(e.bootstrap,e.providers);return function(n,o,u){try{var a=Object.assign({},o);if(!e.bootstrap&&!a.bootstrap)throw new Error("You must pass in a NgModule or NgModuleFactory to be bootstrapped");var c=a.req,s=a.res||c.res;a.url=a.url||c.protocol+"://"+(c.get("host")||"")+c.originalUrl,a.document=a.document||function l(e){return p[e]=p[e]||r.readFileSync(e).toString()}(n),a.providers=a.providers||[],a.providers=a.providers.concat(function f(e,r){var t=[{provide:i.REQUEST,useValue:e}];return r&&t.push({provide:i.RESPONSE,useValue:r}),t}(c,s)),t.render(a).then((function(e){return u(null,e)})).catch(u)}catch(e){u(e)}}},Object.defineProperty(e,"__esModule",{value:!0})}));