Skip to content

Commit

Permalink
more web-server test
Browse files Browse the repository at this point in the history
  • Loading branch information
rinick committed Mar 12, 2023
1 parent 9d29ebe commit 8a7b33c
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 40 deletions.
3 changes: 3 additions & 0 deletions bin/test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const packagesToTest = ['./src/core', './src/web-server', './src/node'];
}
routeTiclo(app, '/ticlo');

let globalClientBlock = Root.instance._globalRoot.createBlock('^local-client');
globalClientBlock._load({'#is': 'http:client', 'url': `http://127.0.0.1:${port}/ticlo/`});

app.get('/', (req, res) => {
res.end();
});
Expand Down
8 changes: 5 additions & 3 deletions src/core/block/BlockFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ export abstract class BaseFunction<T extends FunctionData = FunctionData> {
}

// cleanup when function is destroyed but data needs to be reused
cleanup(): void {
this._data.output(undefined);
}
cleanup(): void {}
destroy(): void {
this._data = undefined;
}
}

export abstract class PureFunction extends BaseFunction {
isPure = true;

cleanup(): void {
this._data.output(undefined);
}
}
export abstract class ImpureFunction extends BaseFunction {
isPure = false;
Expand Down
3 changes: 3 additions & 0 deletions src/core/block/BlockProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export class BlockProperty extends PropDispatcher<any> implements PropListener<a

_listenRaw(source: BlockBindingSource) {
if (this._bindingSource) {
if (source === this._bindingSource) {
return;
}
this._bindingSource.unlisten(this);
this._bindingPath = null;
}
Expand Down
16 changes: 10 additions & 6 deletions src/core/functions/http/Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const methodList: RouteMethod[] = ['GET', 'POST', 'PUT', 'DELETE'];
const responseTypeList = ['arraybuffer', 'blob', 'document', 'json', 'text'];

const httpClient: HttpClient = {
request(config: AxiosRequestConfig) {
async request(config: AxiosRequestConfig) {
const url = config.url;
if (typeof url === 'string' && (url.startsWith('https://') || url.startsWith('http://') || url.startsWith('//'))) {
if (typeof url === 'string' && (url.startsWith('https://') || url.startsWith('http://'))) {
return axios(config);
}
throw new Error('Invalid Url');
throw new Error(`Invalid Url: ${url}`);
},
};

Expand Down Expand Up @@ -62,9 +62,7 @@ export class FetchFunction extends ImpureFunction {
// don't throw
return;
}
this._data.output(undefined, 'status');
this._data.output(undefined, 'responseHeaders');
this._data.output(undefined);
this.cleanup();
throw error;
});
}
Expand All @@ -78,6 +76,12 @@ export class FetchFunction extends ImpureFunction {
return true;
}

cleanup(): void {
this._data.output(undefined, 'status');
this._data.output(undefined, 'responseHeaders');
this._data.output(undefined);
}

destroy() {
this.cancel();
super.destroy();
Expand Down
16 changes: 12 additions & 4 deletions src/core/functions/http/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {URL} from 'url';
import axios, {AxiosPromise, AxiosRequestConfig, AxiosRequestHeaders} from 'axios';
import axios, {AxiosPromise, AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse} from 'axios';
import {ImpureFunction} from '../../block/BlockFunction';
import {Functions} from '../../block/Functions';

export interface HttpClient {
request(config: AxiosRequestConfig): AxiosPromise;
request(config: AxiosRequestConfig): Promise<AxiosResponse>;
}

export const axiosClient: HttpClient = {
Expand All @@ -24,8 +24,16 @@ class HttpClientObject implements HttpClient {
this.optionalHeaders = optionalHeaders;
}

request(config: AxiosRequestConfig): AxiosPromise {
const url = new URL(config.url, this.baseUrl).href;
async request(config: AxiosRequestConfig) {
let url = config.url;
// don't allow parent path
if (url.startsWith('/')) {
url = url.replace(/\/+/, '');
}
url = new URL(url, this.baseUrl).href;
if (!url.startsWith(this.baseUrl)) {
throw new Error(`Invalid Url: ${url}`);
}
let headers = config.headers;
if (this.headers || this.optionalHeaders) {
headers = {...this.optionalHeaders, ...headers, ...this.headers};
Expand Down
19 changes: 8 additions & 11 deletions src/web-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ import {ServerFunction} from './ServerFunction';
* open a port for the http:express-server service
* @param app
* @param basePath
* @param globalBlockPrefix
* @param globalBlockName
*/
export function routeTiclo(app: Express.Application, basePath: string, globalBlockPrefix: string = 'local') {
let serverName = `^${globalBlockPrefix}-server`;
let globalServerBlock = Root.instance._globalRoot.createBlock(serverName);
globalServerBlock._load({'#is': 'web-server:express-server'});

let clientName = `^${globalBlockPrefix}-client`;
let globalClientBlock = Root.instance._globalRoot.createBlock(clientName);
globalClientBlock._load({'#is': 'http:client', 'url': 'http://127.0.0.1/ticlo/'});

let serverFunction: ServerFunction = globalServerBlock._function as ServerFunction;
export function routeTiclo(app: Express.Application, basePath: string, globalBlockName: string = '^local-server') {
if (!globalBlockName.startsWith('^')) {
globalBlockName = '^' + globalBlockName;
}
let globalServiceBlock = Root.instance._globalRoot.createBlock(globalBlockName);
globalServiceBlock._load({'#is': 'web-server:express-server'});
let serverFunction: ServerFunction = globalServiceBlock._function as ServerFunction;
app.all(`${basePath}/*`, (req: Request, res: Response) => {
serverFunction.requestHandler(basePath, req, res);
});
Expand Down
Loading

0 comments on commit 8a7b33c

Please sign in to comment.