Compare commits

...

14 Commits

Author SHA1 Message Date
d7fe947107 2.0.46 2023-03-31 15:10:43 +02:00
dd426a4ca4 fix(core): update 2023-03-31 15:10:42 +02:00
2a2d4dabe4 2.0.45 2023-03-31 13:18:23 +02:00
830682d382 fix(core): update 2023-03-31 13:18:23 +02:00
d160a92bee 2.0.44 2023-03-30 19:44:44 +02:00
cc421c3321 fix(core): update 2023-03-30 19:44:44 +02:00
92ecef30d3 2.0.43 2023-03-30 19:42:55 +02:00
de4aab6df0 fix(core): update 2023-03-30 19:42:55 +02:00
5624e9dc1f 2.0.42 2023-03-30 19:40:41 +02:00
c7e40e4cde fix(core): update 2023-03-30 19:40:41 +02:00
0704febfc0 2.0.41 2023-03-30 17:44:10 +02:00
b8b1a61ae5 fix(core): update 2023-03-30 17:44:10 +02:00
45155bbce0 2.0.40 2023-03-30 16:44:46 +02:00
35bcf717cb fix(core): update 2023-03-30 16:44:45 +02:00
6 changed files with 90 additions and 70 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@apiglobal/typedserver",
"version": "2.0.39",
"version": "2.0.46",
"description": "easy serving of static files",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@apiglobal/typedserver',
version: '2.0.39',
version: '2.0.46',
description: 'easy serving of static files'
}

View File

@ -10,3 +10,14 @@ export interface IReq_PushLatestServerChangeTime extends typedrequestInterfaces.
};
response: {}
}
export interface IReq_GetLatestServerChangeTime extends typedrequestInterfaces.implementsTR<
typedrequestInterfaces.ITypedRequest,
IReq_GetLatestServerChangeTime
> {
method: 'getLatestServerChangeTime',
request: {};
response: {
time: number;
}
}

View File

@ -61,7 +61,7 @@ export class TypedServer {
// instance
public options: IServerOptions;
public serverInstance: servertools.Server;
public server: servertools.Server;
public smartchokInstance: plugins.smartchok.Smartchok;
public serveDirHashSubject = new plugins.smartrx.rxjs.ReplaySubject<string>(1);
public serveHash: string = '000000';
@ -72,31 +72,20 @@ export class TypedServer {
public ended = false;
constructor(optionsArg: IServerOptions) {
const standardOptions: IServerOptions = {
injectReload: true,
port: 3000,
serveDir: process.cwd(),
watch: true,
injectReload: false,
serveDir: null,
watch: false,
cors: true,
};
this.options = {
...standardOptions,
...optionsArg,
};
}
/**
* inits and starts the server
*/
public async start() {
// set the smartexpress instance
this.serverInstance = new servertools.Server({
port: this.options.port,
forceSsl: false,
cors: true,
});
this.server = new servertools.Server(this.options);
// add routes to the smartexpress instance
this.serverInstance.addRoute(
this.server.addRoute(
'/typedserver/:request',
new servertools.Handler('ALL', async (req, res) => {
switch (req.params.request) {
@ -120,67 +109,81 @@ export class TypedServer {
}
})
);
}
this.serverInstance.addRoute(
'/*',
new servertools.HandlerStatic(this.options.serveDir, {
responseModifier: async (responseArg) => {
let fileString = responseArg.responseContent;
if (plugins.path.parse(responseArg.path).ext === '.html') {
const fileStringArray = fileString.split('<head>');
if (this.options.injectReload && fileStringArray.length === 2) {
fileStringArray[0] = `${fileStringArray[0]}<head>
<!-- injected by @apiglobal/typedserver start -->
<script async defer type="module" src="/typedserver/devtools"></script>
<script>
globalThis.typedserver = {
lastReload: '${this.lastReload}',
versionInfo: ${JSON.stringify({}, null, 2)},
}
</script>
<!-- injected by @apiglobal/typedserver stop -->
`;
fileString = fileStringArray.join('');
console.log('injected typedserver script.');
} else if (this.options.injectReload) {
console.log('Could not insert typedserver script');
/**
* inits and starts the server
*/
public async start() {
if(this.options.serveDir) {
this.server.addRoute(
'/*',
new servertools.HandlerStatic(this.options.serveDir, {
responseModifier: async (responseArg) => {
let fileString = responseArg.responseContent;
if (plugins.path.parse(responseArg.path).ext === '.html') {
const fileStringArray = fileString.split('<head>');
if (this.options.injectReload && fileStringArray.length === 2) {
fileStringArray[0] = `${fileStringArray[0]}<head>
<!-- injected by @apiglobal/typedserver start -->
<script async defer type="module" src="/typedserver/devtools"></script>
<script>
globalThis.typedserver = {
lastReload: '${this.lastReload}',
versionInfo: ${JSON.stringify({}, null, 2)},
}
</script>
<!-- injected by @apiglobal/typedserver stop -->
`;
fileString = fileStringArray.join('');
console.log('injected typedserver script.');
} else if (this.options.injectReload) {
console.log('Could not insert typedserver script');
}
}
}
const headers = responseArg.headers;
headers.appHash = this.serveHash;
headers['Cache-Control'] = 'no-cache, no-store, must-revalidate';
headers['Pragma'] = 'no-cache';
headers['Expires'] = '0';
return {
headers,
path: responseArg.path,
responseContent: fileString,
};
},
serveIndexHtmlDefault: true,
})
);
this.smartchokInstance = new plugins.smartchok.Smartchok([this.options.serveDir], {});
if (this.options.watch) {
const headers = responseArg.headers;
headers.appHash = this.serveHash;
headers['Cache-Control'] = 'no-cache, no-store, must-revalidate';
headers['Pragma'] = 'no-cache';
headers['Expires'] = '0';
return {
headers,
path: responseArg.path,
responseContent: fileString,
};
},
serveIndexHtmlDefault: true,
})
);
} else if (this.options.injectReload) {
throw new Error('You set to inject the reload script without a serve dir. This is not supported at the moment.')
}
if (this.options.watch && this.options.serveDir) {
this.smartchokInstance = new plugins.smartchok.Smartchok([this.options.serveDir], {});
await this.smartchokInstance.start();
(await this.smartchokInstance.getObservableFor('change')).subscribe(async () => {
await this.createServeDirHash();
this.reload();
});
await this.createServeDirHash();
}
await this.createServeDirHash();
// lets start the server
await this.serverInstance.start();
console.log('open url in browser');
await this.server.start();
this.typedsocket = await plugins.typedsocket.TypedSocket.createServer(
this.typedrouter,
this.serverInstance
this.server
);
// lets setup typedrouter
this.typedrouter.addTypedHandler<interfaces.IReq_GetLatestServerChangeTime>(new plugins.typedrequest.TypedHandler('getLatestServerChangeTime', async reqDataArg => {
return {
time: this.lastReload,
}
}))
// console.log('open url in browser');
// await plugins.smartopen.openUrl(`http://testing.git.zone:${this.options.port}`);
}
@ -203,9 +206,11 @@ export class TypedServer {
public async stop() {
this.ended = true;
await this.serverInstance.stop();
await this.server.stop();
await this.typedsocket.stop();
await this.smartchokInstance.stop();
if (this.smartchokInstance) {
await this.smartchokInstance.stop();
}
}
public async createServeDirHash() {

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@apiglobal/typedserver',
version: '2.0.39',
version: '2.0.46',
description: 'easy serving of static files'
}

View File

@ -98,14 +98,18 @@ export class ReloadChecker {
this.typedrouter,
plugins.typedsocket.TypedSocket.useWindowLocationOriginUrl()
);
this.typedsocket.eventSubject.subscribe(eventArg => {
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!')
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);
}
});