simple-boot-http-ssr
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -8,4 +8,7 @@ import { RequestResponse } from 'simple-boot-http-server/models/RequestResponse'; | ||
import { SimFrontOption } from 'simple-boot-front/option/SimFrontOption'; | ||
import { SimpleBootFront } from 'simple-boot-front/SimpleBootFront'; | ||
import * as JSDOM from 'jsdom'; | ||
export declare type FactoryAndParams = { | ||
frontDistPath: string; | ||
frontDistIndexFileName?: string; | ||
factorySimFrontOption: (window: any) => SimFrontOption; | ||
@@ -22,3 +25,3 @@ factory: SimpleBootHttpSSRFactory; | ||
export declare class SSRFilter implements Filter { | ||
private factory; | ||
factory: FactoryAndParams; | ||
otherInstanceSim?: Map<ConstructorType<any>, any> | undefined; | ||
@@ -28,3 +31,8 @@ private simpleBootFrontPool; | ||
private indexHTML; | ||
private welcomUrl; | ||
constructor(factory: FactoryAndParams, otherInstanceSim?: Map<ConstructorType<any>, any> | undefined); | ||
onInit(app: SimpleBootHttpServer): Promise<void>; | ||
makeJsdom(): Promise<JSDOM.JSDOM>; | ||
makeFront(jsdom: JSDOM.JSDOM): Promise<SimpleBootFront>; | ||
enqueueFrontApp(simpleBootFront: SimpleBootFront): void; | ||
pushQueue(): Promise<void>; | ||
@@ -31,0 +39,0 @@ before(rr: RequestResponse): Promise<boolean>; |
@@ -45,5 +45,5 @@ "use strict"; | ||
var AsyncBlockingQueue_1 = require("simple-boot-core/queues/AsyncBlockingQueue"); | ||
var RandomUtils_1 = require("simple-boot-core/utils/random/RandomUtils"); | ||
var SSRFilter = (function () { | ||
function SSRFilter(factory, otherInstanceSim) { | ||
var _this = this; | ||
this.factory = factory; | ||
@@ -53,3 +53,8 @@ this.otherInstanceSim = otherInstanceSim; | ||
this.simpleBootFrontQueue = new AsyncBlockingQueue_1.AsyncBlockingQueue(); | ||
(function () { return __awaiter(_this, void 0, void 0, function () { | ||
this.welcomUrl = 'http://localhost'; | ||
factory.frontDistIndexFileName = factory.frontDistIndexFileName || 'index.html'; | ||
this.indexHTML = JsdomInitializer_1.JsdomInitializer.loadFile(this.factory.frontDistPath, factory.frontDistIndexFileName); | ||
} | ||
SSRFilter.prototype.onInit = function (app) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var i; | ||
@@ -73,19 +78,28 @@ return __generator(this, function (_a) { | ||
}); | ||
}); })().then(function (it) { | ||
}); | ||
this.indexHTML = JsdomInitializer_1.JsdomInitializer.loadFile(this.factory.frontDistPath, 'index.html'); | ||
} | ||
SSRFilter.prototype.pushQueue = function () { | ||
}; | ||
SSRFilter.prototype.makeJsdom = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var idx, jsdom, window_1, option, simpleBootFront; | ||
var jsdom; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, new JsdomInitializer_1.JsdomInitializer(this.factory.frontDistPath, this.factory.frontDistIndexFileName || 'index.html', { url: this.welcomUrl }).run()]; | ||
case 1: | ||
jsdom = _a.sent(); | ||
return [2, jsdom]; | ||
} | ||
}); | ||
}); | ||
}; | ||
SSRFilter.prototype.makeFront = function (jsdom) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var name, window, option, simpleBootFront; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!(this.simpleBootFrontPool.length < this.factory.poolOption.max)) return [3, 2]; | ||
idx = this.simpleBootFrontPool.length; | ||
jsdom = new JsdomInitializer_1.JsdomInitializer(this.factory.frontDistPath, { url: "http://localhost" }).run(); | ||
window_1 = jsdom.window; | ||
window_1.ssrUse = false; | ||
option = this.factory.factorySimFrontOption(window_1); | ||
option.name = "SSRFilter-pool-".concat(idx + 1); | ||
name = RandomUtils_1.RandomUtils.uuid(); | ||
window = jsdom.window; | ||
window.ssrUse = false; | ||
option = this.factory.factorySimFrontOption(window); | ||
option.name = name; | ||
return [4, this.factory.factory.create(option, this.factory.using, this.factory.domExcludes)]; | ||
@@ -95,6 +109,4 @@ case 1: | ||
simpleBootFront.run(this.otherInstanceSim); | ||
this.simpleBootFrontPool.push(simpleBootFront); | ||
this.simpleBootFrontQueue.enqueue(simpleBootFront); | ||
_a.label = 2; | ||
case 2: return [2]; | ||
simpleBootFront.jsdom = jsdom; | ||
return [2, simpleBootFront]; | ||
} | ||
@@ -104,34 +116,60 @@ }); | ||
}; | ||
SSRFilter.prototype.enqueueFrontApp = function (simpleBootFront) { | ||
this.simpleBootFrontPool.push(simpleBootFront); | ||
this.simpleBootFrontQueue.enqueue(simpleBootFront); | ||
}; | ||
SSRFilter.prototype.pushQueue = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (!(this.simpleBootFrontPool.length < this.factory.poolOption.max)) return [3, 3]; | ||
_a = this.enqueueFrontApp; | ||
_b = this.makeFront; | ||
return [4, this.makeJsdom()]; | ||
case 1: return [4, _b.apply(this, [_c.sent()])]; | ||
case 2: | ||
_a.apply(this, [_c.sent()]); | ||
_c.label = 3; | ||
case 3: return [2]; | ||
} | ||
}); | ||
}); | ||
}; | ||
SSRFilter.prototype.before = function (rr) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var simpleBootFront, data, html, serverSideData, data_1; | ||
var now, simpleBootFront, html, serverSideData, data; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (!(rr.reqHasAcceptHeader(Mimes_1.Mimes.TextHtml) || rr.reqHasAcceptHeader(Mimes_1.Mimes.All))) return [3, 9]; | ||
if ((_b = (_a = this.factory).ssrExcludeFilter) === null || _b === void 0 ? void 0 : _b.call(_a, rr)) { | ||
this.writeOkHtmlAndEnd({ rr: rr }, this.indexHTML); | ||
return [2, false]; | ||
} | ||
if (!this.simpleBootFrontQueue.isEmpty()) return [3, 2]; | ||
return [4, this.pushQueue()]; | ||
now = Date.now(); | ||
if (!(rr.reqHasAcceptHeader(Mimes_1.Mimes.TextHtml) || rr.reqHasAcceptHeader(Mimes_1.Mimes.All))) return [3, 11]; | ||
if (!((_b = (_a = this.factory).ssrExcludeFilter) === null || _b === void 0 ? void 0 : _b.call(_a, rr))) return [3, 2]; | ||
return [4, this.writeOkHtmlAndEnd({ rr: rr }, this.indexHTML)]; | ||
case 1: | ||
_c.sent(); | ||
_c.label = 2; | ||
case 2: return [4, this.simpleBootFrontQueue.dequeue()]; | ||
return [2, false]; | ||
case 2: | ||
if (!this.simpleBootFrontQueue.isEmpty()) return [3, 4]; | ||
return [4, this.pushQueue()]; | ||
case 3: | ||
_c.sent(); | ||
_c.label = 4; | ||
case 4: return [4, this.simpleBootFrontQueue.dequeue()]; | ||
case 5: | ||
simpleBootFront = _c.sent(); | ||
_c.label = 4; | ||
case 4: | ||
_c.trys.push([4, , 7, 8]); | ||
_c.label = 6; | ||
case 6: | ||
_c.trys.push([6, , 9, 10]); | ||
simpleBootFront.option.window.ssrUse = true; | ||
delete simpleBootFront.option.window.server_side_data; | ||
return [4, simpleBootFront.goRouting(rr.reqUrl)]; | ||
case 5: | ||
data = _c.sent(); | ||
case 7: | ||
_c.sent(); | ||
html = simpleBootFront.option.window.document.documentElement.outerHTML; | ||
serverSideData = simpleBootFront.option.window.server_side_data; | ||
if (serverSideData) { | ||
data_1 = Object.entries(serverSideData).map(function (_a) { | ||
data = Object.entries(serverSideData).map(function (_a) { | ||
var k = _a[0], v = _a[1]; | ||
@@ -145,11 +183,11 @@ if (typeof v === 'string') { | ||
}).join(';'); | ||
if (data_1) { | ||
html = html.replace('</head>', "<script> window.server_side_data={}; ".concat(data_1, "; </script></head>")); | ||
if (data) { | ||
html = html.replace('</head>', "<script> window.server_side_data={}; ".concat(data, "; </script></head>")); | ||
} | ||
} | ||
return [4, this.writeOkHtmlAndEnd({ rr: rr }, html)]; | ||
case 6: | ||
case 8: | ||
_c.sent(); | ||
return [3, 8]; | ||
case 7: | ||
return [3, 10]; | ||
case 9: | ||
simpleBootFront.option.window.ssrUse = false; | ||
@@ -159,4 +197,4 @@ delete simpleBootFront.option.window.server_side_data; | ||
return [7]; | ||
case 8: return [2, false]; | ||
case 9: return [2, true]; | ||
case 10: return [2, false]; | ||
case 11: return [2, true]; | ||
} | ||
@@ -163,0 +201,0 @@ }); |
import * as JSDOM from 'jsdom'; | ||
export declare class JsdomInitializer { | ||
private frontDistPath; | ||
private frontDistIndexFileName; | ||
private reconfigureSettings?; | ||
constructor(frontDistPath: string, reconfigureSettings?: JSDOM.ReconfigureSettings | undefined); | ||
constructor(frontDistPath: string, frontDistIndexFileName: string, reconfigureSettings?: JSDOM.ReconfigureSettings | undefined); | ||
static loadFile(distPath: string, filePath: string): string; | ||
run(): JSDOM.JSDOM; | ||
run(): Promise<JSDOM.JSDOM>; | ||
} |
@@ -21,2 +21,38 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -31,4 +67,5 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var JsdomInitializer = (function () { | ||
function JsdomInitializer(frontDistPath, reconfigureSettings) { | ||
function JsdomInitializer(frontDistPath, frontDistIndexFileName, reconfigureSettings) { | ||
this.frontDistPath = frontDistPath; | ||
this.frontDistIndexFileName = frontDistIndexFileName; | ||
this.reconfigureSettings = reconfigureSettings; | ||
@@ -40,26 +77,36 @@ } | ||
JsdomInitializer.prototype.run = function () { | ||
var indexHTML = JsdomInitializer.loadFile(this.frontDistPath, 'index.html'); | ||
var jsdom = new JSDOM.JSDOM(indexHTML); | ||
if (this.reconfigureSettings) { | ||
jsdom.reconfigure(this.reconfigureSettings); | ||
} | ||
global.document = jsdom.window.document; | ||
global.window = jsdom.window; | ||
var dummyResponse = { ok: false, json: function () { return Promise.resolve({}); } }; | ||
global.fetch = function () { | ||
var data = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
data[_i] = arguments[_i]; | ||
} | ||
return Promise.resolve(dummyResponse); | ||
}; | ||
global.history = jsdom.window.history; | ||
global.Event = jsdom.window.Event; | ||
global.IntersectionObserver = jsdom.window.IntersectionObserver; | ||
global.navigator = jsdom.window.navigator; | ||
global.NodeFilter = jsdom.window.NodeFilter; | ||
global.Node = jsdom.window.Node; | ||
global.HTMLElement = jsdom.window.HTMLElement; | ||
global.Element = jsdom.window.Element; | ||
return jsdom; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var pathStr, jsdom, dummyResponse; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
pathStr = path_1.default.join(this.frontDistPath, this.frontDistIndexFileName); | ||
return [4, JSDOM.JSDOM.fromFile(pathStr)]; | ||
case 1: | ||
jsdom = _a.sent(); | ||
if (this.reconfigureSettings) { | ||
jsdom.reconfigure(this.reconfigureSettings); | ||
} | ||
global.document = jsdom.window.document; | ||
global.window = jsdom.window; | ||
dummyResponse = { ok: false, json: function () { return Promise.resolve({}); } }; | ||
global.fetch = function () { | ||
var data = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
data[_i] = arguments[_i]; | ||
} | ||
return Promise.resolve(dummyResponse); | ||
}; | ||
global.history = jsdom.window.history; | ||
global.Event = jsdom.window.Event; | ||
global.IntersectionObserver = jsdom.window.IntersectionObserver; | ||
global.navigator = jsdom.window.navigator; | ||
global.NodeFilter = jsdom.window.NodeFilter; | ||
global.Node = jsdom.window.Node; | ||
global.HTMLElement = jsdom.window.HTMLElement; | ||
global.Element = jsdom.window.Element; | ||
return [2, jsdom]; | ||
} | ||
}); | ||
}); | ||
}; | ||
@@ -66,0 +113,0 @@ return JsdomInitializer; |
{ | ||
"name": "simple-boot-http-ssr", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"main": "SimpleBootHttpSSRServer.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
44688
18
752