Compare commits
32 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 |
@@ -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"
|
||||
|
||||
22147
package-lock.json
generated
22147
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
@@ -1,32 +1,33 @@
|
||||
{
|
||||
"name": "@apiglobal/typedsocket",
|
||||
"version": "1.0.14",
|
||||
"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.10",
|
||||
"@types/node": "^14.14.22",
|
||||
"@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.56",
|
||||
"@apiglobal/typedrequest": "^2.0.3",
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||
"@pushrocks/isohash": "^1.0.2",
|
||||
"@pushrocks/smartexpress": "^3.0.100",
|
||||
"@pushrocks/smartsocket": "^1.2.7",
|
||||
"@pushrocks/smartstring": "^3.0.24"
|
||||
"@pushrocks/isohash": "^2.0.0",
|
||||
"@pushrocks/smartsocket": "^2.0.6",
|
||||
"@pushrocks/smartstring": "^4.0.2"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
|
||||
import * as typedsocket from '../ts/index';
|
||||
import * as typedsocket from '../ts/index.js';
|
||||
|
||||
tap.test('should create a client', async () => {
|
||||
console.log('Browser test') // TODO: implement browser tests
|
||||
|
||||
20
test/test.ts
20
test/test.ts
@@ -2,8 +2,7 @@ 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';
|
||||
import * as typedsocket from '../ts/index.js';
|
||||
|
||||
interface IRequest_Client_Server extends typedrequestInterfaces.implementsTR<
|
||||
typedrequestInterfaces.ITypedRequest,
|
||||
@@ -34,6 +33,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 21 seconds for reconnect');
|
||||
await tools.delayFor(21000);
|
||||
});
|
||||
|
||||
tap.test('should process messages from both sides', async () => {
|
||||
@@ -49,13 +58,10 @@ tap.test('should process messages from both sides', async () => {
|
||||
console.log(response2);
|
||||
})
|
||||
|
||||
tap.test('should run without problems for a little bit', async tools => {
|
||||
await tools.delayFor(5000);
|
||||
})
|
||||
|
||||
tap.test('should disconnect', async () => {
|
||||
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,10 +1,9 @@
|
||||
import * as plugins from './typedsocket.plugins';
|
||||
import type * as smartexpress from '@pushrocks/smartexpress';
|
||||
import * as plugins from './typedsocket.plugins.js';
|
||||
|
||||
const publicRoleName = 'publicRoleName';
|
||||
const publicRolePass = 'publicRolePass';
|
||||
|
||||
export type TTypedSocketSide = 'server' | 'client';
|
||||
export type TTypedSocketSide = 'server' | 'client';
|
||||
|
||||
export class TypedSocket {
|
||||
// STATIC
|
||||
@@ -12,25 +11,21 @@ export class TypedSocket {
|
||||
* creates a typedsocket server
|
||||
* note: this will fail in browser environments as server libs are not bundled.
|
||||
*/
|
||||
public static async createServer(
|
||||
public static async createServer (
|
||||
typedrouterArg: plugins.typedrequest.TypedRouter,
|
||||
smartexpressServerArg?: smartexpress.Server
|
||||
smartexpressServerArg?: any
|
||||
): Promise<TypedSocket> {
|
||||
const smartsocketServer = new plugins.smartsocket.Smartsocket({
|
||||
alias: 'typedsocketServer',
|
||||
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);
|
||||
},
|
||||
@@ -39,7 +34,7 @@ export class TypedSocket {
|
||||
const typedsocket = new TypedSocket(
|
||||
'server',
|
||||
typedrouterArg,
|
||||
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||
async <T extends plugins.typedrequestInterfaces.ITypedRequest> (
|
||||
dataArg: T,
|
||||
targetConnectionArg?: plugins.smartsocket.SocketConnection
|
||||
): Promise<T> => {
|
||||
@@ -48,9 +43,9 @@ export class TypedSocket {
|
||||
console.log(
|
||||
'Since no targetConnection was supplied and there is only one active one present, choosing that one automatically'
|
||||
);
|
||||
targetConnectionArg = smartsocketServer.socketConnections.getArray()[0];
|
||||
targetConnectionArg = smartsocketServer.socketConnections.getArray()[ 0 ];
|
||||
} else {
|
||||
throw new Error('you need to specify the wanted targetConnection');
|
||||
throw new Error('you need to specify the wanted targetConnection. Currently no target is selectable automatically.');
|
||||
}
|
||||
}
|
||||
const response: T = await smartsocketServer.clientCall(
|
||||
@@ -67,17 +62,15 @@ export class TypedSocket {
|
||||
return typedsocket;
|
||||
}
|
||||
|
||||
public static async createClient(
|
||||
public static async createClient (
|
||||
typedrouterArg: plugins.typedrequest.TypedRouter,
|
||||
serverUrlArg: string,
|
||||
aliasArg = 'clientArg'
|
||||
): Promise<TypedSocket> {
|
||||
const domain = new plugins.smartstring.Domain(serverUrlArg);
|
||||
|
||||
const socketOptions = {
|
||||
const socketOptions: plugins.smartsocket.ISmartsocketClientOptions = {
|
||||
alias: aliasArg,
|
||||
role: publicRoleName,
|
||||
password: publicRolePass,
|
||||
port: domain.port || 3000,
|
||||
url: `${domain.nodeParsedUrl.protocol}//${domain.nodeParsedUrl.hostname}`,
|
||||
autoReconnect: true,
|
||||
@@ -88,7 +81,6 @@ export class TypedSocket {
|
||||
smartsocketClient.addSocketFunction(
|
||||
new plugins.smartsocket.SocketFunction({
|
||||
funcName: 'processMessage',
|
||||
allowedRoles: [],
|
||||
funcDef: async (dataArg, socketConnectionArg) => {
|
||||
return typedrouterArg.routeAndAddResponse(dataArg);
|
||||
},
|
||||
@@ -97,7 +89,7 @@ export class TypedSocket {
|
||||
const typedsocket = new TypedSocket(
|
||||
'client',
|
||||
typedrouterArg,
|
||||
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(dataArg: T): Promise<T> => {
|
||||
async <T extends plugins.typedrequestInterfaces.ITypedRequest> (dataArg: T): Promise<T> => {
|
||||
const response: T = (smartsocketClient.serverCall('processMessage', dataArg) as any) as T;
|
||||
return response;
|
||||
},
|
||||
@@ -130,7 +122,7 @@ export class TypedSocket {
|
||||
this.socketServerOrClient = socketServerOrClientArg;
|
||||
}
|
||||
|
||||
public addTag(keyArg: string, payloadArg: any) {
|
||||
public addTag (keyArg: string, payloadArg: any) {
|
||||
if (this.side === 'client' && this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient) {
|
||||
this.socketServerOrClient.socketConnection.addTag({
|
||||
id: keyArg,
|
||||
@@ -141,8 +133,8 @@ export class TypedSocket {
|
||||
}
|
||||
}
|
||||
|
||||
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||
methodName: T['method'],
|
||||
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>(
|
||||
@@ -157,7 +149,12 @@ export class TypedSocket {
|
||||
return typedrequest;
|
||||
}
|
||||
|
||||
public async findAllTargetConnections(
|
||||
/**
|
||||
* 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) {
|
||||
@@ -173,31 +170,36 @@ export class TypedSocket {
|
||||
}
|
||||
}
|
||||
|
||||
public async findTargetConnection(
|
||||
/**
|
||||
* 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 = this.findAllTargetConnections(asyncFindFuncArg);
|
||||
return allMatching[0];
|
||||
const allMatching = await this.findAllTargetConnections(asyncFindFuncArg);
|
||||
return allMatching[ 0 ];
|
||||
}
|
||||
|
||||
public async findAllTargetConnectionsByTag(keyArg: string, payloadArg?: any) {
|
||||
public async findAllTargetConnectionsByTag (keyArg: string, payloadArg?: any) {
|
||||
return this.findAllTargetConnections(async socketConnectionArg => {
|
||||
let result: boolean;
|
||||
if (!payloadArg) {
|
||||
result = !!socketConnectionArg.getTagById('keyArg')
|
||||
result = !!(await socketConnectionArg.getTagById(keyArg));
|
||||
} else {
|
||||
result = !!socketConnectionArg.getTagById('keyArg') === payloadArg;
|
||||
result = !!((await socketConnectionArg.getTagById(keyArg)).payload === payloadArg);
|
||||
}
|
||||
return result;
|
||||
})
|
||||
}
|
||||
|
||||
public async findTargetConnectionByTag(keyArg: string, payloadArg?: any) {
|
||||
const allResults = this.findAllTargetConnectionsByTag(keyArg, payloadArg)
|
||||
return allResults[0];
|
||||
public async findTargetConnectionByTag (keyArg: string, payloadArg?: any) {
|
||||
const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg)
|
||||
return allResults[ 0 ];
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
public async stop () {
|
||||
await this.socketServerOrClient.stop()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user