From b8b1a61ae5c5f3a1ede68e719e8e80512d76fa08 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 30 Mar 2023 17:44:10 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/typedserver.classes.typedserver.ts | 120 +++++++++++++------------- ts_web/00_commitinfo_data.ts | 2 +- 3 files changed, 60 insertions(+), 64 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index b42de8f..fb4013a 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiglobal/typedserver', - version: '2.0.40', + version: '2.0.41', description: 'easy serving of static files' } diff --git a/ts/typedserver.classes.typedserver.ts b/ts/typedserver.classes.typedserver.ts index 72096b1..22a15eb 100644 --- a/ts/typedserver.classes.typedserver.ts +++ b/ts/typedserver.classes.typedserver.ts @@ -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(1); public serveHash: string = '000000'; @@ -82,21 +82,10 @@ export class TypedServer { ...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,74 @@ 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(''); - if (this.options.injectReload && fileStringArray.length === 2) { - fileStringArray[0] = `${fileStringArray[0]} - - - - - `; - 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(''); + if (this.options.injectReload && fileStringArray.length === 2) { + fileStringArray[0] = `${fileStringArray[0]} + + + + + `; + 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 ); + // console.log('open url in browser'); // await plugins.smartopen.openUrl(`http://testing.git.zone:${this.options.port}`); } @@ -203,7 +199,7 @@ 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(); } diff --git a/ts_web/00_commitinfo_data.ts b/ts_web/00_commitinfo_data.ts index b42de8f..fb4013a 100644 --- a/ts_web/00_commitinfo_data.ts +++ b/ts_web/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiglobal/typedserver', - version: '2.0.40', + version: '2.0.41', description: 'easy serving of static files' }