Compare commits
55 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a82f8ccc26 | |||
| d934f4b53b | |||
| 54b8a692ba | |||
| 2f39b5662d | |||
| 5b9e785159 | |||
| a5bca90eeb | |||
| 244016faed | |||
| 61719769a0 | |||
| 6e2855cf3c | |||
| 916052ef0b | |||
| 2f51e10fc4 | |||
| 6e2e2768e2 | |||
| 4c5abc89b8 | |||
| 5911d829f9 | |||
| eac735a691 | |||
| 4af7164a58 | |||
| 006073cbc8 | |||
| 7d8b41a5a6 | |||
| acca64fa31 | |||
| 1e570813a3 | |||
| 0199e8ef17 | |||
| 68def29c80 | |||
| 106eb12a7f | |||
| 5b868f1259 | |||
| 952835e46a | |||
| 5aa8048ce8 | |||
| 48112f97ce | |||
| 57a10080b5 | |||
| 7bdac2b89d | |||
| 6c49870dd6 | |||
| a2db05bfe3 | |||
| 69668731be | |||
| e586843d81 | |||
| fef1ae338a | |||
| 969b2d6686 | |||
| f4d820d37e | |||
| c08acd076e | |||
| c968de37d1 | |||
| 5b8f7514f4 | |||
| 53874a7772 | |||
| 479cf60239 | |||
| 825a264327 | |||
| 16305a4cf8 | |||
| e7176ebc79 | |||
| ad8ff504a9 | |||
| a23c5a0fba | |||
| 912b9bad51 | |||
| 6e4505256c | |||
| bd81f731ad | |||
| 50506f4cfa | |||
| 83450b8fd2 | |||
| 4a02ebea0c | |||
| ab2b068ac4 | |||
| f18be49423 | |||
| 09cd4c3feb |
@@ -36,6 +36,7 @@ auditProductionDependencies:
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "apiglobal",
|
||||
"gitrepo": "typedsocket",
|
||||
"shortDescription": "a typedrequest extension supporting websockets",
|
||||
"description": "a typedrequest extension supporting websockets",
|
||||
"npmPackagename": "@apiglobal/typedsocket",
|
||||
"license": "MIT",
|
||||
"projectDomain": "api.global"
|
||||
|
||||
22467
package-lock.json
generated
22467
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
23
package.json
23
package.json
@@ -1,28 +1,33 @@
|
||||
{
|
||||
"name": "@apiglobal/typedsocket",
|
||||
"version": "1.0.2",
|
||||
"version": "2.0.3",
|
||||
"private": false,
|
||||
"description": "a typedrequest extension supporting websockets",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)"
|
||||
"build": "(tsbuild --web --allowimplicitany)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsbundle": "^1.0.78",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@types/node": "^14.11.2",
|
||||
"@gitzone/tsbuild": "^2.1.61",
|
||||
"@gitzone/tsbundle": "^1.0.102",
|
||||
"@gitzone/tstest": "^1.0.70",
|
||||
"@pushrocks/smartenv": "^5.0.1",
|
||||
"@pushrocks/tapbundle": "^5.0.3",
|
||||
"@types/node": "^17.0.23",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@apiglobal/typedrequest": "^1.0.55",
|
||||
"@pushrocks/smartsocket": "^1.1.68"
|
||||
"@apiglobal/typedrequest": "^2.0.3",
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||
"@pushrocks/isohash": "^2.0.0",
|
||||
"@pushrocks/smartsocket": "^2.0.6",
|
||||
"@pushrocks/smartstring": "^4.0.2"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
|
||||
@@ -25,7 +25,11 @@ Platform support | [ => {
|
||||
console.log('Browser test') // TODO: implement browser tests
|
||||
});
|
||||
|
||||
tap.start();
|
||||
65
test/test.ts
65
test/test.ts
@@ -1,10 +1,67 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as typedsocket from '../ts/index';
|
||||
import * as typedrequest from '@apiglobal/typedrequest';
|
||||
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
|
||||
|
||||
let testTypedSocket: typedsocket.TypedSocket;
|
||||
import * as typedsocket from '../ts/index.js';
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testTypedSocket = new typedsocket.TypedSocket();
|
||||
interface IRequest_Client_Server extends typedrequestInterfaces.implementsTR<
|
||||
typedrequestInterfaces.ITypedRequest,
|
||||
IRequest_Client_Server
|
||||
> {
|
||||
method: 'sayhi';
|
||||
request: {
|
||||
greeting: string;
|
||||
};
|
||||
response: {
|
||||
answer: string
|
||||
}
|
||||
}
|
||||
|
||||
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:3000');
|
||||
console.log('test: waiting 5 seconds');
|
||||
await tools.delayFor(5000);
|
||||
await testTypedSocketServer.stop();
|
||||
|
||||
// lets create another server
|
||||
testTypedSocketServer = await typedsocket.TypedSocket.createServer(testTypedRouter);
|
||||
|
||||
// lets see if auto reconnect works
|
||||
console.log('test: waiting 21 seconds for reconnect');
|
||||
await tools.delayFor(21000);
|
||||
});
|
||||
|
||||
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 disconnect', async (tools) => {
|
||||
await testTypedSocketClient.stop();
|
||||
await testTypedSocketServer.stop();
|
||||
tools.delayFor(1000).then(() => process.exit(0));
|
||||
})
|
||||
|
||||
tap.start();
|
||||
|
||||
@@ -1 +1 @@
|
||||
export * from './typedsocket.classes.typedsocket';
|
||||
export * from './typedsocket.classes.typedsocket.js';
|
||||
|
||||
@@ -1,9 +1,205 @@
|
||||
import * as plugins from './typedsocket.plugins';
|
||||
import * as plugins from './typedsocket.plugins.js';
|
||||
|
||||
const publicRoleName = 'publicRoleName';
|
||||
const publicRolePass = 'publicRolePass';
|
||||
|
||||
export type TTypedSocketSide = 'server' | 'client';
|
||||
|
||||
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({
|
||||
alias: 'typedsocketServer',
|
||||
port: 3000,
|
||||
});
|
||||
if (smartexpressServerArg) {
|
||||
smartsocketServer.setExternalServer('smartexpress', smartexpressServerArg);
|
||||
}
|
||||
|
||||
public static createCleint() {}
|
||||
smartsocketServer.socketFunctions.add(
|
||||
new plugins.smartsocket.SocketFunction({
|
||||
funcName: 'processMessage',
|
||||
funcDef: async (dataArg, socketConnectionArg) => {
|
||||
return typedrouterArg.routeAndAddResponse(dataArg);
|
||||
},
|
||||
})
|
||||
);
|
||||
const typedsocket = new TypedSocket(
|
||||
'server',
|
||||
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. Currently no target is selectable automatically.');
|
||||
}
|
||||
}
|
||||
const response: T = await smartsocketServer.clientCall(
|
||||
'processMessage',
|
||||
dataArg,
|
||||
targetConnectionArg
|
||||
) as any;
|
||||
return response;
|
||||
},
|
||||
smartsocketServer
|
||||
);
|
||||
await smartsocketServer.start();
|
||||
|
||||
constructor() {}
|
||||
}
|
||||
return typedsocket;
|
||||
}
|
||||
|
||||
public static async createClient (
|
||||
typedrouterArg: plugins.typedrequest.TypedRouter,
|
||||
serverUrlArg: string,
|
||||
aliasArg = 'clientArg'
|
||||
): Promise<TypedSocket> {
|
||||
const domain = new plugins.smartstring.Domain(serverUrlArg);
|
||||
|
||||
const socketOptions: plugins.smartsocket.ISmartsocketClientOptions = {
|
||||
alias: aliasArg,
|
||||
port: domain.port || 3000,
|
||||
url: `${domain.nodeParsedUrl.protocol}//${domain.nodeParsedUrl.hostname}`,
|
||||
autoReconnect: true,
|
||||
}
|
||||
console.log(`starting typedsocket with the following settings:`)
|
||||
console.log(socketOptions);
|
||||
const smartsocketClient = new plugins.smartsocket.SmartsocketClient(socketOptions);
|
||||
smartsocketClient.addSocketFunction(
|
||||
new plugins.smartsocket.SocketFunction({
|
||||
funcName: 'processMessage',
|
||||
funcDef: async (dataArg, socketConnectionArg) => {
|
||||
return typedrouterArg.routeAndAddResponse(dataArg);
|
||||
},
|
||||
})
|
||||
);
|
||||
const typedsocket = new TypedSocket(
|
||||
'client',
|
||||
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 side: TTypedSocketSide;
|
||||
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(
|
||||
sideArg: TTypedSocketSide,
|
||||
typedrouterArg: plugins.typedrequest.TypedRouter,
|
||||
postMethodArg: plugins.typedrequest.IPostMethod,
|
||||
socketServerOrClientArg: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient
|
||||
) {
|
||||
this.side = sideArg;
|
||||
this.typedrouter = typedrouterArg;
|
||||
this.postMethod = postMethodArg;
|
||||
this.socketServerOrClient = socketServerOrClientArg;
|
||||
}
|
||||
|
||||
public addTag (keyArg: string, payloadArg: any) {
|
||||
if (this.side === 'client' && this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient) {
|
||||
this.socketServerOrClient.socketConnection.addTag({
|
||||
id: keyArg,
|
||||
payload: payloadArg
|
||||
})
|
||||
} else {
|
||||
throw new Error('tagging is only supported on clients');
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns all matching target connection
|
||||
* @param asyncFindFuncArg
|
||||
* @returns
|
||||
*/
|
||||
public async findAllTargetConnections (
|
||||
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
|
||||
) {
|
||||
if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) {
|
||||
const matchingSockets: plugins.smartsocket.SocketConnection[] = [];
|
||||
for (const socketConnection of this.socketServerOrClient.socketConnections.getArray()) {
|
||||
if (await asyncFindFuncArg(socketConnection)) {
|
||||
matchingSockets.push(socketConnection);
|
||||
}
|
||||
}
|
||||
return matchingSockets;
|
||||
} else {
|
||||
throw new Error('this method >>findTargetConnection<< is only available from the server');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a single target connection by returning the first one of all matching ones
|
||||
* @param asyncFindFuncArg
|
||||
* @returns
|
||||
*/
|
||||
public async findTargetConnection (
|
||||
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
|
||||
) {
|
||||
const allMatching = await this.findAllTargetConnections(asyncFindFuncArg);
|
||||
return allMatching[ 0 ];
|
||||
}
|
||||
|
||||
public async findAllTargetConnectionsByTag (keyArg: string, payloadArg?: any) {
|
||||
return this.findAllTargetConnections(async socketConnectionArg => {
|
||||
let result: boolean;
|
||||
if (!payloadArg) {
|
||||
result = !!(await socketConnectionArg.getTagById(keyArg));
|
||||
} else {
|
||||
result = !!((await socketConnectionArg.getTagById(keyArg)).payload === payloadArg);
|
||||
}
|
||||
return result;
|
||||
})
|
||||
}
|
||||
|
||||
public async findTargetConnectionByTag (keyArg: string, payloadArg?: any) {
|
||||
const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg)
|
||||
return allResults[ 0 ];
|
||||
}
|
||||
|
||||
public async stop () {
|
||||
await this.socketServerOrClient.stop()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
// @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';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
|
||||
export {
|
||||
smartsocket
|
||||
isohash,
|
||||
smartsocket,
|
||||
smartstring
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user