Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 68def29c80 | |||
| 106eb12a7f | |||
| 5b868f1259 | |||
| 952835e46a | |||
| 5aa8048ce8 | |||
| 48112f97ce | |||
| 57a10080b5 | |||
| 7bdac2b89d | |||
| 6c49870dd6 | |||
| a2db05bfe3 | |||
| 69668731be | |||
| e586843d81 | |||
| fef1ae338a | |||
| 969b2d6686 | |||
| f4d820d37e | |||
| c08acd076e | |||
| c968de37d1 |
@@ -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
|
||||
|
||||
19890
package-lock.json
generated
19890
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiglobal/typedsocket",
|
||||
"version": "1.0.11",
|
||||
"version": "1.0.20",
|
||||
"private": false,
|
||||
"description": "a typedrequest extension supporting websockets",
|
||||
"main": "dist_ts/index.js",
|
||||
@@ -12,20 +12,19 @@
|
||||
"build": "(tsbuild --web)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsbundle": "^1.0.78",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@types/node": "^14.14.22",
|
||||
"@gitzone/tsbuild": "^2.1.28",
|
||||
"@gitzone/tsbundle": "^1.0.88",
|
||||
"@gitzone/tstest": "^1.0.59",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^16.11.3",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@apiglobal/typedrequest": "^1.0.56",
|
||||
"@apiglobal/typedrequest": "^1.0.58",
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||
"@pushrocks/isohash": "^1.0.2",
|
||||
"@pushrocks/smartexpress": "^3.0.100",
|
||||
"@pushrocks/smartsocket": "^1.2.1",
|
||||
"@pushrocks/smartsocket": "^1.2.8",
|
||||
"@pushrocks/smartstring": "^3.0.24"
|
||||
},
|
||||
"browserslist": [
|
||||
|
||||
10
test/test.ts
10
test/test.ts
@@ -34,6 +34,16 @@ tap.test('should add some handlers', async () => {
|
||||
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 60 seconds for reconnect');
|
||||
await tools.delayFor(60000);
|
||||
});
|
||||
|
||||
tap.test('should process messages from both sides', async () => {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import * as plugins from './typedsocket.plugins';
|
||||
import type * as smartexpress from '@pushrocks/smartexpress';
|
||||
|
||||
const publicRoleName = 'publicRoleName';
|
||||
const publicRolePass = 'publicRolePass';
|
||||
|
||||
export type TTypedSocketSide = 'server' | 'client';
|
||||
|
||||
export class TypedSocket {
|
||||
// STATIC
|
||||
/**
|
||||
@@ -12,7 +13,7 @@ export class TypedSocket {
|
||||
*/
|
||||
public static async createServer(
|
||||
typedrouterArg: plugins.typedrequest.TypedRouter,
|
||||
smartexpressServerArg?: smartexpress.Server
|
||||
smartexpressServerArg?: any
|
||||
): Promise<TypedSocket> {
|
||||
const smartsocketServer = new plugins.smartsocket.Smartsocket({
|
||||
port: 3000,
|
||||
@@ -35,6 +36,7 @@ export class TypedSocket {
|
||||
})
|
||||
);
|
||||
const typedsocket = new TypedSocket(
|
||||
'server',
|
||||
typedrouterArg,
|
||||
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||
dataArg: T,
|
||||
@@ -79,6 +81,7 @@ export class TypedSocket {
|
||||
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(
|
||||
@@ -91,6 +94,7 @@ export class TypedSocket {
|
||||
})
|
||||
);
|
||||
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;
|
||||
@@ -105,6 +109,7 @@ export class TypedSocket {
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public side: TTypedSocketSide;
|
||||
public typedrouter: plugins.typedrequest.TypedRouter;
|
||||
private postMethod: plugins.typedrequest.IPostMethod &
|
||||
((
|
||||
@@ -113,15 +118,28 @@ export class TypedSocket {
|
||||
) => 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
|
||||
@@ -138,20 +156,56 @@ export class TypedSocket {
|
||||
return typedrequest;
|
||||
}
|
||||
|
||||
public async findTargetConnection(
|
||||
findFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => boolean
|
||||
/**
|
||||
* 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 (findFuncArg(socketConnection)) {
|
||||
return socketConnection;
|
||||
if (await asyncFindFuncArg(socketConnection)) {
|
||||
matchingSockets.push(socketConnection);
|
||||
}
|
||||
}
|
||||
return matchingSockets;
|
||||
} else {
|
||||
console.warn('this method >>findTargetConnection<< is only available from the server');
|
||||
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 = !!socketConnectionArg.getTagById(keyArg);
|
||||
} else {
|
||||
result = !!socketConnectionArg.getTagById(keyArg) === 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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user