Compare commits

...

10 Commits

Author SHA1 Message Date
344e213c41 1.0.31 2021-08-16 17:47:21 +02:00
0ae13c970e fix(core): update 2021-08-16 17:47:20 +02:00
0502e7bc99 1.0.30 2021-08-16 16:26:13 +02:00
c0a425e16d fix(core): update 2021-08-16 16:26:13 +02:00
90089357cf 1.0.29 2021-08-16 10:31:39 +02:00
e36aed4b2b fix(core): update 2021-08-16 10:31:38 +02:00
4d2aa7a0ee 1.0.28 2021-08-16 10:30:46 +02:00
759fa1ff43 fix(core): update 2021-08-16 10:30:46 +02:00
042caeeeb5 1.0.27 2021-05-19 15:21:48 +00:00
6ddfaffbff fix(core): update 2021-05-19 15:21:47 +00:00
5 changed files with 16416 additions and 90 deletions

16384
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartssr", "name": "@pushrocks/smartssr",
"version": "1.0.26", "version": "1.0.31",
"private": false, "private": false,
"description": "a smart server side renderer supporting shadow dom", "description": "a smart server side renderer supporting shadow dom",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -10,7 +10,7 @@
"scripts": { "scripts": {
"test": "(tstest test/ --web)", "test": "(tstest test/ --web)",
"build": "(tsbuild --web)", "build": "(tsbuild --web)",
"format": "(gitzone format)" "serve": "tsrun scripts/serve.ts"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.25",

View File

@ -9,12 +9,8 @@ tap.test('should create a valid smartssr instance', async () => {
}); });
}); });
tap.test('should start the smartssr instance', async () => {
await testSSRInstance.start();
});
tap.test('should render central.eu', async (tools) => { tap.test('should render central.eu', async (tools) => {
await testSSRInstance.renderPage('https://in.work'); await testSSRInstance.renderPage('https://lossless.com');
}); });
tap.skip.test('should render lossless.com', async () => { tap.skip.test('should render lossless.com', async () => {
@ -26,8 +22,4 @@ tap.skip.test('should render https://lossless.gmbh', async () => {
console.log(renderedPage); console.log(renderedPage);
}); });
tap.test('should stop the smartssr instance', async () => {
await testSSRInstance.stop();
});
tap.start(); tap.start();

View File

@ -11,7 +11,6 @@ export interface ISmartSSROptions {
* *
*/ */
export class SmartSSR { export class SmartSSR {
public browser: plugins.smartpuppeteer.IncognitoBrowser;
public options: ISmartSSROptions; public options: ISmartSSROptions;
constructor(optionsArg?: ISmartSSROptions) { constructor(optionsArg?: ISmartSSROptions) {
@ -23,50 +22,36 @@ export class SmartSSR {
}; };
} }
public async start() {
this.browser = new plugins.smartpuppeteer.IncognitoBrowser();
await this.browser.start();
}
public async stop() {
if (this.browser) {
await plugins.smartdelay.delayFor(3000);
await this.browser.stop();
this.browser = null;
} else {
console.log('browser was not in started mode');
}
}
public async renderPage(urlArg: string) { public async renderPage(urlArg: string) {
const overallTimeMeasurement = new plugins.smarttime.HrtMeasurement(); const overallTimeMeasurement = new plugins.smarttime.HrtMeasurement();
overallTimeMeasurement.start(); overallTimeMeasurement.start();
const resultDeferred = plugins.smartpromise.defer<string>(); const resultDeferred = plugins.smartpromise.defer<string>();
const context = await this.browser.getNewIncognitoContext(); const browser = new plugins.smartpuppeteer.IncognitoBrowser();
const renderTimeMeasurement = new plugins.smarttime.HrtMeasurement();
let renderedPageString: string;
let screenshotBuffer: Buffer;
await browser.start();
try {
const context = await browser.getNewIncognitoContext();
const page = await context.newPage(); const page = await context.newPage();
// lets protext against left open tabs // lets protect against left open tabs
plugins.smartdelay.delayFor(30000).then(() => { plugins.smartdelay.delayFor(30000).then(async () => {
if (!page.isClosed) { try {
page.close(); await browser.stop();
context.close(); } catch {}
throw new Error(`failed to render ${urlArg}`);
}
}); });
page.on('console', (msg) => { page.on('console', (msg) => {
console.log(`${urlArg}: ${msg.text()}`); console.log(`${urlArg}: ${msg.text()}`);
}); });
const renderTimeMeasurement = new plugins.smarttime.HrtMeasurement();
renderTimeMeasurement.start(); renderTimeMeasurement.start();
await page.goto(urlArg, { await page.goto(urlArg, {
waitUntil: 'networkidle2', waitUntil: 'networkidle2',
timeout: 30000, timeout: 30000,
}); });
let screenshotBuffer: Buffer;
if (this.options.debug) { if (this.options.debug) {
screenshotBuffer = await page.screenshot({ screenshotBuffer = await page.screenshot({
encoding: 'binary', encoding: 'binary',
@ -75,15 +60,17 @@ export class SmartSSR {
await page.$eval('body', serializeFunction); await page.$eval('body', serializeFunction);
const pageContent = await page.content(); const pageContent = await page.content();
const renderedPageString = pageContent; renderedPageString = pageContent;
resultDeferred.resolve(renderedPageString); resultDeferred.resolve(renderedPageString);
const result = await resultDeferred.promise;
renderTimeMeasurement.stop(); renderTimeMeasurement.stop();
// lets clean up async // lets clean up async
await page.close(); await page.close();
await context.close(); await context.close();
} catch (e) {
console.log(e);
}
await browser.stop();
overallTimeMeasurement.stop(); overallTimeMeasurement.stop();
console.log( console.log(
@ -103,6 +90,6 @@ export class SmartSSR {
fs.writeFileSync(plugins.path.join(paths.noGitDir, 'test.png'), screenshotBuffer); fs.writeFileSync(plugins.path.join(paths.noGitDir, 'test.png'), screenshotBuffer);
} }
return result; return resultDeferred.promise;
} }
} }

View File

@ -63,11 +63,16 @@ export function serializeFunction(rootNode) {
const noteForAppending: HTMLElement[] = []; const noteForAppending: HTMLElement[] = [];
// lets care about static css first // lets care about static css first
if ((nodeArg.constructor as any).styles?.cssText) { if (
(nodeArg.constructor as any).styles &&
(nodeArg.constructor as any).styles instanceof Array
) {
for (const objectArg of (nodeArg.constructor as any).styles) {
const styleTag = document.createElement('style'); const styleTag = document.createElement('style');
styleTag.textContent = prependCss(nodeUUID, (nodeArg.constructor as any).styles.cssText); styleTag.textContent = prependCss(nodeUUID, objectArg.cssText);
noteForAppending.push(styleTag); noteForAppending.push(styleTag);
} }
}
childNodes.forEach((childNode) => { childNodes.forEach((childNode) => {
if (childNode instanceof HTMLElement) { if (childNode instanceof HTMLElement) {