fix(core): update
This commit is contained in:
8
ts_web_inject/00_commitinfo_data.ts
Normal file
8
ts_web_inject/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@api.global/typedserver',
|
||||
version: '3.0.29',
|
||||
description: 'A TypeScript-based project for easy serving of static files with support for live reloading, compression, and typed requests.'
|
||||
}
|
152
ts_web_inject/index.ts
Normal file
152
ts_web_inject/index.ts
Normal file
@ -0,0 +1,152 @@
|
||||
import * as plugins from './typedserver_web.plugins.js';
|
||||
import * as interfaces from '../ts_interfaces/index.js';
|
||||
import { logger } from './typedserver_web.logger.js';
|
||||
logger.log('info', `TypedServer-Devtools initialized!`);
|
||||
|
||||
import { TypedserverInfoscreen } from './typedserver_web.infoscreen.js';
|
||||
|
||||
export class ReloadChecker {
|
||||
public reloadJustified = false;
|
||||
public backendConnectionLost = false;
|
||||
public infoscreen = new TypedserverInfoscreen();
|
||||
public store = new plugins.webstore.WebStore({
|
||||
dbName: 'apiglobal__typedserver',
|
||||
storeName: 'apiglobal__typedserver',
|
||||
});
|
||||
public storeKey = 'lastServerChange';
|
||||
|
||||
public typedsocket: plugins.typedsocket.TypedSocket;
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
|
||||
constructor() {}
|
||||
|
||||
public async reload() {
|
||||
// this looks a bit hacky, but apparently is the safest way to really reload stuff
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* starts the reload checker
|
||||
*/
|
||||
public async performHttpRequest() {
|
||||
logger.log('info', 'performing http check...');
|
||||
(await this.store.get(this.storeKey))
|
||||
? null
|
||||
: await this.store.set(this.storeKey, globalThis.typedserver.lastReload);
|
||||
|
||||
let response: Response;
|
||||
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
plugins.smartdelay.delayFor(5000).then(() => {
|
||||
controller.abort();
|
||||
});
|
||||
response = await fetch('/typedserver/reloadcheck', {
|
||||
method: 'POST',
|
||||
signal: controller.signal,
|
||||
});
|
||||
} catch (err: any) {}
|
||||
|
||||
if (response?.status !== 200) {
|
||||
this.backendConnectionLost = true;
|
||||
logger.log('warn', `got a status ${response?.status}.`);
|
||||
this.infoscreen.setText(`backend connection lost... Status ${response?.status}`);
|
||||
}
|
||||
if (response?.status === 200 && this.backendConnectionLost) {
|
||||
this.backendConnectionLost = false;
|
||||
this.infoscreen.setSuccess('regained connection to backend...');
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public async checkReload(lastServerChange: number) {
|
||||
let reloadJustified = false;
|
||||
let storedLastServerChange = await this.store.get(this.storeKey);
|
||||
if (storedLastServerChange && storedLastServerChange !== lastServerChange) {
|
||||
reloadJustified = true;
|
||||
} else {
|
||||
}
|
||||
|
||||
if (reloadJustified) {
|
||||
this.store.set(this.storeKey, lastServerChange);
|
||||
const reloadText = `upgrading... ${
|
||||
globalThis.globalSw ? '(purging the sw cache first...)' : ''
|
||||
}`;
|
||||
this.infoscreen.setText(reloadText);
|
||||
if (globalThis.globalSw?.purgeCache) {
|
||||
await globalThis.globalSw.purgeCache();
|
||||
} else {
|
||||
console.log('globalThis.globalSw not found...');
|
||||
}
|
||||
this.infoscreen.setText(`cleaned caches`);
|
||||
await plugins.smartdelay.delayFor(200);
|
||||
this.reload();
|
||||
return;
|
||||
} else {
|
||||
if (this.infoscreen) {
|
||||
this.infoscreen.hide();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public async connectTypedsocket() {
|
||||
if (!this.typedsocket) {
|
||||
this.typedrouter.addTypedHandler<interfaces.IReq_PushLatestServerChangeTime>(
|
||||
new plugins.typedrequest.TypedHandler('pushLatestServerChangeTime', async (dataArg) => {
|
||||
this.checkReload(dataArg.time);
|
||||
return {};
|
||||
})
|
||||
);
|
||||
this.typedsocket = await plugins.typedsocket.TypedSocket.createClient(
|
||||
this.typedrouter,
|
||||
plugins.typedsocket.TypedSocket.useWindowLocationOriginUrl()
|
||||
);
|
||||
this.typedsocket.addTag('typedserver_frontend', {});
|
||||
this.typedsocket.eventSubject.subscribe(async (eventArg) => {
|
||||
console.log(`typedsocket event subscription: ${eventArg}`);
|
||||
if (
|
||||
eventArg === 'disconnected' ||
|
||||
eventArg === 'disconnecting' ||
|
||||
eventArg === 'timedOut'
|
||||
) {
|
||||
this.backendConnectionLost = true;
|
||||
this.infoscreen.setText(`typedsocket ${eventArg}!`);
|
||||
} else if (eventArg === 'connected' && this.backendConnectionLost) {
|
||||
this.backendConnectionLost = false;
|
||||
this.infoscreen.setSuccess('typedsocket connected!');
|
||||
// lets check if a reload is necessary
|
||||
const getLatestServerChangeTime =
|
||||
this.typedsocket.createTypedRequest<interfaces.IReq_GetLatestServerChangeTime>(
|
||||
'getLatestServerChangeTime'
|
||||
);
|
||||
const response = await getLatestServerChangeTime.fire({});
|
||||
this.checkReload(response.time);
|
||||
}
|
||||
});
|
||||
logger.log('success', `ReloadChecker connected through typedsocket!`);
|
||||
}
|
||||
}
|
||||
|
||||
public started = false;
|
||||
public async start() {
|
||||
this.started = true;
|
||||
logger.log('info', `starting ReloadChecker...`);
|
||||
while (this.started) {
|
||||
const response = await this.performHttpRequest();
|
||||
if (response.status === 200) {
|
||||
logger.log('info', `ReloadChecker reached backend!`);
|
||||
await this.checkReload(parseInt(await response.text()));
|
||||
await this.connectTypedsocket();
|
||||
}
|
||||
await plugins.smartdelay.delayFor(120000);
|
||||
}
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
this.started = false;
|
||||
}
|
||||
}
|
||||
|
||||
const reloadCheckInstance = new ReloadChecker();
|
||||
reloadCheckInstance.start();
|
119
ts_web_inject/typedserver_web.infoscreen.ts
Normal file
119
ts_web_inject/typedserver_web.infoscreen.ts
Normal file
@ -0,0 +1,119 @@
|
||||
import {LitElement, html, css} from 'lit';
|
||||
import { customElement, property } from 'lit/decorators.js';
|
||||
|
||||
import * as plugins from './typedserver_web.plugins.js';
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
'typedserver-infoscreen': TypedserverInfoscreen;
|
||||
}
|
||||
}
|
||||
|
||||
@customElement('typedserver-infoscreen')
|
||||
export class TypedserverInfoscreen extends LitElement {
|
||||
//INSTANCE
|
||||
|
||||
@property()
|
||||
private text = 'Hello';
|
||||
|
||||
@property()
|
||||
private success = false;
|
||||
|
||||
public static styles = [
|
||||
css`
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:host {
|
||||
|
||||
}
|
||||
|
||||
.mainbox {
|
||||
z-index: 1000;
|
||||
position: fixed;
|
||||
transition: all 0.3s;
|
||||
display: block;
|
||||
bottom: -50px;
|
||||
right: 0px;
|
||||
left: 0px;
|
||||
height : 50px;
|
||||
background: #222;
|
||||
color: #CCC;
|
||||
padding: 15px;
|
||||
border-top: 1px solid #e4002b;
|
||||
text-align: center;
|
||||
font-family: 'Roboto', sans-serif;
|
||||
color: #fff;
|
||||
background: #111;
|
||||
box-shadow: 0px -30px 30px rgba(0,0,0,1);
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
.mainbox {
|
||||
color: #333;
|
||||
background: #eeeeee;
|
||||
box-shadow: 0px 0px 5px rgba(0,0,0,0.3);
|
||||
}
|
||||
}
|
||||
|
||||
.show {
|
||||
bottom: 0px;
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.success {
|
||||
background: green;
|
||||
border-top: 1px solid green;
|
||||
}
|
||||
`
|
||||
];
|
||||
|
||||
public setText(textArg: string) {
|
||||
this.success = false;
|
||||
this.text = textArg;
|
||||
this.show();
|
||||
this.success = false;
|
||||
}
|
||||
|
||||
public setSuccess(textArg: string) {
|
||||
this.text = textArg;
|
||||
this.show();
|
||||
this.success = true;
|
||||
setTimeout(() => this.hide(), 1000);
|
||||
}
|
||||
|
||||
private appended = false;
|
||||
public async show () {
|
||||
document.body.append(this);
|
||||
this.appended = true;
|
||||
await plugins.smartdelay.delayFor(0);
|
||||
const mainbox = this.shadowRoot.querySelector('.mainbox');
|
||||
mainbox.classList.add('show');
|
||||
}
|
||||
|
||||
public async hide() {
|
||||
this.text = '';
|
||||
if (this.appended) {
|
||||
const mainbox = this.shadowRoot.querySelector('.mainbox');
|
||||
mainbox.classList.remove('show');
|
||||
}
|
||||
await plugins.smartdelay.delayFor(300);
|
||||
if (this.appended) {
|
||||
this.appended = false;
|
||||
document.body.removeChild(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public render () {
|
||||
return html`
|
||||
<div class="mainbox ${this.success ? 'success': ''}">${this.text}</div>
|
||||
`
|
||||
}
|
||||
|
||||
public async firstUpdated(_changedProperties: Map<string | number | symbol, unknown>): Promise<void> {
|
||||
super.firstUpdated(_changedProperties);
|
||||
}
|
||||
}
|
15
ts_web_inject/typedserver_web.logger.ts
Normal file
15
ts_web_inject/typedserver_web.logger.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import * as plugins from './typedserver_web.plugins.js';
|
||||
|
||||
export const logger = new plugins.smartlog.Smartlog({
|
||||
logContext: {
|
||||
company: 'Some Company',
|
||||
companyunit: 'Some CompanyUnit',
|
||||
containerName: 'Some Containername',
|
||||
environment: "local",
|
||||
runtime: 'node',
|
||||
zone: 'gitzone'
|
||||
},
|
||||
minimumLogLevel: 'silly'
|
||||
});
|
||||
|
||||
logger.addLogDestination(new plugins.smartlogDestinationDevtools.SmartlogDestinationDevtools());
|
21
ts_web_inject/typedserver_web.plugins.ts
Normal file
21
ts_web_inject/typedserver_web.plugins.ts
Normal file
@ -0,0 +1,21 @@
|
||||
// @apiglobal scope
|
||||
import * as typedrequest from '@api.global/typedrequest';
|
||||
import * as typedsocket from '@api.global/typedsocket';
|
||||
|
||||
export {
|
||||
typedrequest,
|
||||
typedsocket,
|
||||
}
|
||||
|
||||
// pushrocks scope
|
||||
import * as smartdelay from '@push.rocks/smartdelay';
|
||||
import * as smartlog from '@push.rocks/smartlog';
|
||||
import * as smartlogDestinationDevtools from '@push.rocks/smartlog-destination-devtools';
|
||||
import * as webstore from '@push.rocks/webstore';
|
||||
|
||||
export {
|
||||
smartdelay,
|
||||
smartlog,
|
||||
smartlogDestinationDevtools,
|
||||
webstore,
|
||||
};
|
Reference in New Issue
Block a user