Compare commits

...

9 Commits

Author SHA1 Message Date
912b9bad51 1.0.7 2021-01-21 14:26:49 +00:00
6e4505256c fix(core): update 2021-01-21 14:26:48 +00:00
bd81f731ad 1.0.6 2020-12-26 18:52:39 +00:00
50506f4cfa fix(core): update 2020-12-26 18:52:39 +00:00
83450b8fd2 1.0.5 2020-12-26 18:49:44 +00:00
4a02ebea0c 1.0.4 2020-12-26 18:45:17 +00:00
ab2b068ac4 fix(core): update 2020-12-26 18:45:17 +00:00
f18be49423 1.0.3 2020-12-26 18:14:19 +00:00
09cd4c3feb fix(core): implement basic functionality 2020-12-26 18:14:19 +00:00
7 changed files with 469 additions and 498 deletions

726
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "@apiglobal/typedsocket",
"version": "1.0.2",
"version": "1.0.7",
"private": false,
"description": "a typedrequest extension supporting websockets",
"main": "dist_ts/index.js",
@@ -16,13 +16,15 @@
"@gitzone/tsbundle": "^1.0.78",
"@gitzone/tstest": "^1.0.44",
"@pushrocks/tapbundle": "^3.2.9",
"@types/node": "^14.11.2",
"@types/node": "^14.14.22",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0"
},
"dependencies": {
"@apiglobal/typedrequest": "^1.0.55",
"@pushrocks/smartsocket": "^1.1.68"
"@apiglobal/typedrequest": "^1.0.56",
"@apiglobal/typedrequest-interfaces": "^1.0.15",
"@pushrocks/isohash": "^1.0.2",
"@pushrocks/smartsocket": "^1.2.0"
},
"browserslist": [
"last 1 chrome versions"

View File

@@ -25,7 +25,11 @@ Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20W
## Usage
Use TypeScript for best in class intellisense
Use TypeScript for best in class intellisense.
TypedSocket is the typedrequest you love, just in a bidirectional way.
See the following examples from our test for reference:
## Contribution

View File

@@ -0,0 +1,9 @@
import { tap, expect } from '@pushrocks/tapbundle';
import * as typedsocket from '../ts/index';
tap.test('should create a client', async () => {
console.log('Browser test') // TODO: implement browser tests
});
tap.start();

View File

@@ -1,10 +1,61 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as typedrequest from '@apiglobal/typedrequest';
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
import * as typedsocket from '../ts/index';
import { request } from 'http';
let testTypedSocket: typedsocket.TypedSocket;
interface IRequest_Client_Server extends typedrequestInterfaces.implementsTR<
typedrequestInterfaces.ITypedRequest,
IRequest_Client_Server
> {
method: 'sayhi';
request: {
greeting: string;
};
response: {
answer: string
}
}
tap.test('first test', async () => {
testTypedSocket = new typedsocket.TypedSocket();
let testTypedSocketServer: typedsocket.TypedSocket;
let testTypedSocketClient: typedsocket.TypedSocket;
const testTypedRouter = new typedrequest.TypedRouter();
tap.test('should add some handlers', async () => {
testTypedRouter.addTypedHandler<IRequest_Client_Server>(new typedrequest.TypedHandler('sayhi', async requestData => {
return {
answer: `ok, got it : ${requestData.greeting}`
}
}));
})
tap.test('should create Server and Client', async (tools) => {
testTypedSocketServer = await typedsocket.TypedSocket.createServer(testTypedRouter);
testTypedSocketClient = await typedsocket.TypedSocket.createClient(testTypedRouter, 'http://localhost');
});
tap.test('should process messages from both sides', async () => {
const myServerSideTypedRequest = testTypedSocketServer.createTypedRequest<IRequest_Client_Server>('sayhi');
const myClientSideTypedRequest = testTypedSocketClient.createTypedRequest<IRequest_Client_Server>('sayhi');
const response = await myClientSideTypedRequest.fire({
greeting: 'that is a greeting from the client'
});
console.log(response);
const response2 = await myServerSideTypedRequest.fire({
greeting: 'that is a greeting from the server'
});
console.log(response2);
})
tap.test('should run without problems for a little bit', async tools => {
await tools.delayFor(5000);
})
tap.test('should disconnect', async () => {
await testTypedSocketClient.stop();
await testTypedSocketServer.stop();
})
tap.start();

View File

@@ -1,9 +1,154 @@
import * as plugins from './typedsocket.plugins';
const publicRoleName = 'publicRoleName';
const publicRolePass = 'publicRolePass';
export class TypedSocket {
public static async createServer () {}
// STATIC
/**
* creates a typedsocket server
* note: this will fail in browser environments as server libs are not bundled.
*/
public static async createServer(
typedrouterArg: plugins.typedrequest.TypedRouter,
smartexpressServerArg?: any
): Promise<TypedSocket> {
const smartsocketServer = new plugins.smartsocket.Smartsocket({
port: 3000,
});
if (smartexpressServerArg) {
smartsocketServer.setExternalServer('smartexpress', smartexpressServerArg);
}
const publicRole = new plugins.smartsocket.SocketRole({
name: publicRoleName,
passwordHash: await plugins.isohash.sha256FromString(publicRolePass),
});
smartsocketServer.addSocketRoles([publicRole]);
smartsocketServer.socketFunctions.add(
new plugins.smartsocket.SocketFunction({
funcName: 'processMessage',
allowedRoles: [publicRole],
funcDef: async (dataArg, socketConnectionArg) => {
return typedrouterArg.routeAndAddResponse(dataArg);
},
})
);
const typedsocket = new TypedSocket(
typedrouterArg,
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(
dataArg: T,
targetConnectionArg?: plugins.smartsocket.SocketConnection
): Promise<T> => {
if (!targetConnectionArg) {
if ((smartsocketServer.socketConnections.getArray().length = 1)) {
console.log(
'Since no targetConnection was supplied and there is only one active one present, choosing that one automatically'
);
targetConnectionArg = smartsocketServer.socketConnections.getArray()[0];
} else {
throw new Error('you need to specify the wanted targetConnection');
}
}
const response: T = await smartsocketServer.clientCall(
'processMessage',
dataArg,
targetConnectionArg
) as any;
return response;
},
smartsocketServer
);
public static createCleint() {}
await smartsocketServer.start();
constructor() {}
}
return typedsocket;
}
public static async createClient(
typedrouterArg: plugins.typedrequest.TypedRouter,
serverUrlArg: string,
aliasArg = 'clientArg'
): Promise<TypedSocket> {
const smartsocketClient = new plugins.smartsocket.SmartsocketClient({
alias: aliasArg,
role: publicRoleName,
password: publicRolePass,
port: 3000,
url: serverUrlArg,
autoReconnect: true,
});
smartsocketClient.addSocketFunction(
new plugins.smartsocket.SocketFunction({
funcName: 'processMessage',
allowedRoles: [],
funcDef: async (dataArg, socketConnectionArg) => {
return typedrouterArg.routeAndAddResponse(dataArg);
},
})
);
const typedsocket = new TypedSocket(
typedrouterArg,
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(dataArg: T): Promise<T> => {
const response: T = (smartsocketClient.serverCall('processMessage', dataArg) as any) as T;
return response;
},
smartsocketClient
);
await smartsocketClient.connect();
return typedsocket;
}
// INSTANCE
public typedrouter: plugins.typedrequest.TypedRouter;
private postMethod: plugins.typedrequest.IPostMethod &
((
typedRequestPostObject: plugins.typedrequestInterfaces.ITypedRequest,
socketConnectionArg?: plugins.smartsocket.SocketConnection
) => Promise<plugins.typedrequestInterfaces.ITypedRequest>);
private socketServerOrClient: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient;
constructor(
typedrouterArg: plugins.typedrequest.TypedRouter,
postMethodArg: plugins.typedrequest.IPostMethod,
socketServerOrClientArg: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient
) {
this.typedrouter = typedrouterArg;
this.postMethod = postMethodArg;
this.socketServerOrClient = socketServerOrClientArg;
}
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
methodName: T['method'],
targetConnection?: plugins.smartsocket.SocketConnection
): plugins.typedrequest.TypedRequest<T> {
const typedrequest = new plugins.typedrequest.TypedRequest<T>(
new plugins.typedrequest.TypedTarget({
postMethod: async (requestDataArg) => {
const result = await this.postMethod(requestDataArg, targetConnection);
return result;
},
}),
methodName
);
return typedrequest;
}
public async findTargetConnection(
findFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => boolean
) {
if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) {
for (const socketConnection of this.socketServerOrClient.socketConnections.getArray()) {
if (findFuncArg(socketConnection)) {
return socketConnection;
}
}
} else {
console.warn('this method >>findTargetConnection<< is only available from the server');
}
}
public async stop() {
await this.socketServerOrClient.stop()
}
}

View File

@@ -1,13 +1,17 @@
// @apiglobal scope
import * as typedrequest from '@apiglobal/typedrequest';
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
export {
typedrequest
typedrequest,
typedrequestInterfaces,
}
// @pushrocks scope
import * as isohash from '@pushrocks/isohash';
import * as smartsocket from '@pushrocks/smartsocket';
export {
isohash,
smartsocket
}