Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
2a2d4dabe4 | |||
830682d382 | |||
d160a92bee | |||
cc421c3321 | |||
92ecef30d3 | |||
de4aab6df0 | |||
5624e9dc1f | |||
c7e40e4cde | |||
0704febfc0 | |||
b8b1a61ae5 | |||
45155bbce0 | |||
35bcf717cb | |||
4d3be1064d | |||
8ee72f9380 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiglobal/typedserver",
|
||||
"version": "2.0.38",
|
||||
"version": "2.0.45",
|
||||
"description": "easy serving of static files",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
|
@ -82,12 +82,11 @@ tap.test('should add typedrequest and typedsocket', async () => {
|
||||
|
||||
tap.test('should start the server allright', async () => {
|
||||
await testServer.start(3000);
|
||||
console.log('Yay Test Start successfull!');
|
||||
});
|
||||
|
||||
// see if a demo request holds up
|
||||
tap.test('should issue a request', async (tools) => {
|
||||
const response = await smartrequest.postJson('http://localhost:3000/someroute', {
|
||||
const response = await smartrequest.postJson('http://127.0.0.1:3000/someroute', {
|
||||
headers: {
|
||||
'X-Forwarded-Proto': 'https',
|
||||
},
|
||||
@ -99,20 +98,20 @@ tap.test('should issue a request', async (tools) => {
|
||||
});
|
||||
|
||||
tap.test('should get a file from disk', async () => {
|
||||
const response = await fetch('http://localhost:3000/someroute/testresponse.js');
|
||||
const response = await fetch('http://127.0.0.1:3000/someroute/testresponse.js');
|
||||
console.log(response.status);
|
||||
console.log(response.headers);
|
||||
});
|
||||
|
||||
tap.test('should answer a preflight request', async () => {
|
||||
const response = await fetch('http://localhost:3000/some/randompath/', {
|
||||
const response = await fetch('http://127.0.0.1:3000/some/randompath/', {
|
||||
method: 'OPTIONS',
|
||||
});
|
||||
console.log(response.headers);
|
||||
});
|
||||
|
||||
tap.test('should exposer a sitemap', async () => {
|
||||
const response = await fetch('http://localhost:3000/sitemap');
|
||||
const response = await fetch('http://127.0.0.1:3000/sitemap');
|
||||
console.log(await response.text());
|
||||
});
|
||||
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiglobal/typedserver',
|
||||
version: '2.0.38',
|
||||
version: '2.0.45',
|
||||
description: 'easy serving of static files'
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiglobal/typedserver',
|
||||
version: '2.0.38',
|
||||
version: '2.0.45',
|
||||
description: 'easy serving of static files'
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
});
|
||||
|
Reference in New Issue
Block a user