@api.global/typedsocket 2.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist_ts/00_commitinfo_data.d.ts +8 -0
- package/dist_ts/00_commitinfo_data.js +9 -0
- package/dist_ts/index.d.ts +1 -0
- package/dist_ts/index.js +2 -0
- package/dist_ts/typedsocket.classes.typedsocket.d.ts +34 -0
- package/dist_ts/typedsocket.classes.typedsocket.js +150 -0
- package/dist_ts/typedsocket.plugins.d.ts +10 -0
- package/dist_ts/typedsocket.plugins.js +13 -0
- package/license +19 -0
- package/npmextra.json +18 -0
- package/package.json +50 -0
- package/readme.md +41 -0
- package/ts/00_commitinfo_data.ts +8 -0
- package/ts/index.ts +1 -0
- package/ts/typedsocket.classes.typedsocket.ts +233 -0
- package/ts/typedsocket.plugins.ts +15 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* autocreated commitinfo by @pushrocks/commitinfo
|
|
3
|
+
*/
|
|
4
|
+
export const commitinfo = {
|
|
5
|
+
name: '@api.global/typedsocket',
|
|
6
|
+
version: '2.0.28',
|
|
7
|
+
description: 'a typedrequest extension supporting websockets'
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSx5QkFBeUI7SUFDL0IsT0FBTyxFQUFFLFFBQVE7SUFDakIsV0FBVyxFQUFFLGdEQUFnRDtDQUM5RCxDQUFBIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './typedsocket.classes.typedsocket.js';
|
package/dist_ts/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as plugins from './typedsocket.plugins.js';
|
|
2
|
+
export type TTypedSocketSide = 'server' | 'client';
|
|
3
|
+
export declare class TypedSocket {
|
|
4
|
+
/**
|
|
5
|
+
* creates a typedsocket server
|
|
6
|
+
* note: this will fail in browser environments as server libs are not bundled.
|
|
7
|
+
*/
|
|
8
|
+
static createServer(typedrouterArg: plugins.typedrequest.TypedRouter, smartexpressServerArg?: any): Promise<TypedSocket>;
|
|
9
|
+
static createClient(typedrouterArg: plugins.typedrequest.TypedRouter, serverUrlArg: string, aliasArg?: string): Promise<TypedSocket>;
|
|
10
|
+
static useWindowLocationOriginUrl: () => string;
|
|
11
|
+
side: TTypedSocketSide;
|
|
12
|
+
typedrouter: plugins.typedrequest.TypedRouter;
|
|
13
|
+
get eventSubject(): plugins.smartrx.rxjs.Subject<plugins.smartsocket.TConnectionStatus>;
|
|
14
|
+
private postMethod;
|
|
15
|
+
private socketServerOrClient;
|
|
16
|
+
constructor(sideArg: TTypedSocketSide, typedrouterArg: plugins.typedrequest.TypedRouter, postMethodArg: plugins.typedrequest.IPostMethod, socketServerOrClientArg: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient);
|
|
17
|
+
addTag<T extends plugins.typedrequestInterfaces.ITag = any>(nameArg: T['name'], payloadArg: T['payload']): void;
|
|
18
|
+
createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(methodName: T['method'], targetConnection?: plugins.smartsocket.SocketConnection): plugins.typedrequest.TypedRequest<T>;
|
|
19
|
+
/**
|
|
20
|
+
* returns all matching target connection
|
|
21
|
+
* @param asyncFindFuncArg
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
findAllTargetConnections(asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>): Promise<plugins.smartsocket.SocketConnection[]>;
|
|
25
|
+
/**
|
|
26
|
+
* returns a single target connection by returning the first one of all matching ones
|
|
27
|
+
* @param asyncFindFuncArg
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
findTargetConnection(asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>): Promise<plugins.smartsocket.SocketConnection>;
|
|
31
|
+
findAllTargetConnectionsByTag<TTag extends plugins.typedrequestInterfaces.ITag = any>(keyArg: TTag['name'], payloadArg?: TTag['payload']): Promise<plugins.smartsocket.SocketConnection[]>;
|
|
32
|
+
findTargetConnectionByTag<TTag extends plugins.typedrequestInterfaces.ITag = any>(keyArg: TTag['name'], payloadArg?: TTag['payload']): Promise<plugins.smartsocket.SocketConnection>;
|
|
33
|
+
stop(): Promise<void>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import * as plugins from './typedsocket.plugins.js';
|
|
2
|
+
const publicRoleName = 'publicRoleName';
|
|
3
|
+
const publicRolePass = 'publicRolePass';
|
|
4
|
+
export class TypedSocket {
|
|
5
|
+
// STATIC
|
|
6
|
+
/**
|
|
7
|
+
* creates a typedsocket server
|
|
8
|
+
* note: this will fail in browser environments as server libs are not bundled.
|
|
9
|
+
*/
|
|
10
|
+
static async createServer(typedrouterArg, smartexpressServerArg) {
|
|
11
|
+
const smartsocketServer = new plugins.smartsocket.Smartsocket({
|
|
12
|
+
alias: 'typedsocketServer',
|
|
13
|
+
port: 3000,
|
|
14
|
+
});
|
|
15
|
+
if (smartexpressServerArg) {
|
|
16
|
+
smartsocketServer.setExternalServer('smartexpress', smartexpressServerArg);
|
|
17
|
+
}
|
|
18
|
+
smartsocketServer.socketFunctions.add(new plugins.smartsocket.SocketFunction({
|
|
19
|
+
funcName: 'processMessage',
|
|
20
|
+
funcDef: async (dataArg, socketConnectionArg) => {
|
|
21
|
+
return typedrouterArg.routeAndAddResponse(dataArg);
|
|
22
|
+
},
|
|
23
|
+
}));
|
|
24
|
+
const typedsocket = new TypedSocket('server', typedrouterArg, async (dataArg, targetConnectionArg) => {
|
|
25
|
+
if (!targetConnectionArg) {
|
|
26
|
+
if ((smartsocketServer.socketConnections.getArray().length = 1)) {
|
|
27
|
+
console.log('Since no targetConnection was supplied and there is only one active one present, choosing that one automatically');
|
|
28
|
+
targetConnectionArg = smartsocketServer.socketConnections.getArray()[0];
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
throw new Error('you need to specify the wanted targetConnection. Currently no target is selectable automatically.');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const response = (await smartsocketServer.clientCall('processMessage', dataArg, targetConnectionArg));
|
|
35
|
+
return response;
|
|
36
|
+
}, smartsocketServer);
|
|
37
|
+
await smartsocketServer.start();
|
|
38
|
+
return typedsocket;
|
|
39
|
+
}
|
|
40
|
+
static async createClient(typedrouterArg, serverUrlArg, aliasArg = 'clientArg') {
|
|
41
|
+
const domain = new plugins.smartstring.Domain(serverUrlArg);
|
|
42
|
+
const socketOptions = {
|
|
43
|
+
alias: aliasArg,
|
|
44
|
+
port: domain.port || 3000,
|
|
45
|
+
url: `${domain.nodeParsedUrl.protocol}//${domain.nodeParsedUrl.hostname}`,
|
|
46
|
+
autoReconnect: true,
|
|
47
|
+
};
|
|
48
|
+
console.log(`starting typedsocket with the following settings:`);
|
|
49
|
+
console.log(socketOptions);
|
|
50
|
+
const smartsocketClient = new plugins.smartsocket.SmartsocketClient(socketOptions);
|
|
51
|
+
smartsocketClient.addSocketFunction(new plugins.smartsocket.SocketFunction({
|
|
52
|
+
funcName: 'processMessage',
|
|
53
|
+
funcDef: async (dataArg, socketConnectionArg) => {
|
|
54
|
+
return typedrouterArg.routeAndAddResponse(dataArg);
|
|
55
|
+
},
|
|
56
|
+
}));
|
|
57
|
+
const typedsocket = new TypedSocket('client', typedrouterArg, async (dataArg) => {
|
|
58
|
+
const response = smartsocketClient.serverCall('processMessage', dataArg);
|
|
59
|
+
return response;
|
|
60
|
+
}, smartsocketClient);
|
|
61
|
+
console.log(`typedsocket triggering smartsocket to connect...`);
|
|
62
|
+
const before = Date.now();
|
|
63
|
+
await smartsocketClient.connect();
|
|
64
|
+
console.log(`typedsocket triggered smartsocket connected in ${Date.now() - before}ms!!!`);
|
|
65
|
+
return typedsocket;
|
|
66
|
+
}
|
|
67
|
+
static { this.useWindowLocationOriginUrl = () => {
|
|
68
|
+
const windowLocationResult = plugins.smarturl.Smarturl.createFromUrl(globalThis.location.origin).toString();
|
|
69
|
+
return windowLocationResult;
|
|
70
|
+
}; }
|
|
71
|
+
get eventSubject() {
|
|
72
|
+
return this.socketServerOrClient.eventSubject;
|
|
73
|
+
}
|
|
74
|
+
constructor(sideArg, typedrouterArg, postMethodArg, socketServerOrClientArg) {
|
|
75
|
+
this.side = sideArg;
|
|
76
|
+
this.typedrouter = typedrouterArg;
|
|
77
|
+
this.postMethod = postMethodArg;
|
|
78
|
+
this.socketServerOrClient = socketServerOrClientArg;
|
|
79
|
+
}
|
|
80
|
+
addTag(nameArg, payloadArg) {
|
|
81
|
+
if (this.side === 'client' &&
|
|
82
|
+
this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient) {
|
|
83
|
+
this.socketServerOrClient.socketConnection.addTag({
|
|
84
|
+
id: nameArg,
|
|
85
|
+
payload: payloadArg,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
throw new Error('tagging is only supported on clients');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
createTypedRequest(methodName, targetConnection) {
|
|
93
|
+
const typedrequest = new plugins.typedrequest.TypedRequest(new plugins.typedrequest.TypedTarget({
|
|
94
|
+
postMethod: async (requestDataArg) => {
|
|
95
|
+
const result = await this.postMethod(requestDataArg, targetConnection);
|
|
96
|
+
return result;
|
|
97
|
+
},
|
|
98
|
+
}), methodName);
|
|
99
|
+
return typedrequest;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* returns all matching target connection
|
|
103
|
+
* @param asyncFindFuncArg
|
|
104
|
+
* @returns
|
|
105
|
+
*/
|
|
106
|
+
async findAllTargetConnections(asyncFindFuncArg) {
|
|
107
|
+
if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) {
|
|
108
|
+
const matchingSockets = [];
|
|
109
|
+
for (const socketConnection of this.socketServerOrClient.socketConnections.getArray()) {
|
|
110
|
+
if (await asyncFindFuncArg(socketConnection)) {
|
|
111
|
+
matchingSockets.push(socketConnection);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return matchingSockets;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
throw new Error('this method >>findTargetConnection<< is only available from the server');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* returns a single target connection by returning the first one of all matching ones
|
|
122
|
+
* @param asyncFindFuncArg
|
|
123
|
+
* @returns
|
|
124
|
+
*/
|
|
125
|
+
async findTargetConnection(asyncFindFuncArg) {
|
|
126
|
+
const allMatching = await this.findAllTargetConnections(asyncFindFuncArg);
|
|
127
|
+
return allMatching[0];
|
|
128
|
+
}
|
|
129
|
+
async findAllTargetConnectionsByTag(keyArg, payloadArg) {
|
|
130
|
+
return this.findAllTargetConnections(async (socketConnectionArg) => {
|
|
131
|
+
let result;
|
|
132
|
+
if (!payloadArg) {
|
|
133
|
+
result = !!(await socketConnectionArg.getTagById(keyArg));
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
result = !!(plugins.smartjson.stringify((await socketConnectionArg.getTagById(keyArg))?.payload) ===
|
|
137
|
+
plugins.smartjson.stringify(payloadArg));
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
async findTargetConnectionByTag(keyArg, payloadArg) {
|
|
143
|
+
const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg);
|
|
144
|
+
return allResults[0];
|
|
145
|
+
}
|
|
146
|
+
async stop() {
|
|
147
|
+
await this.socketServerOrClient.stop();
|
|
148
|
+
}
|
|
149
|
+
}
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Q3hDLENBQUM7YUFDSDtZQUNELE9BQU8sTUFBTSxDQUFDO1FBQ2hCLENBQUMsQ0FBQyxDQUFDO0lBQ0wsQ0FBQztJQUVNLEtBQUssQ0FBQyx5QkFBeUIsQ0FDcEMsTUFBb0IsRUFDcEIsVUFBNEI7UUFFNUIsTUFBTSxVQUFVLEdBQUcsTUFBTSxJQUFJLENBQUMsNkJBQTZCLENBQUMsTUFBTSxFQUFFLFVBQVUsQ0FBQyxDQUFDO1FBQ2hGLE9BQU8sVUFBVSxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQ3ZCLENBQUM7SUFFTSxLQUFLLENBQUMsSUFBSTtRQUNmLE1BQU0sSUFBSSxDQUFDLG9CQUFvQixDQUFDLElBQUksRUFBRSxDQUFDO0lBQ3pDLENBQUMifQ==
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as typedrequest from '@api.global/typedrequest';
|
|
2
|
+
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
|
3
|
+
export { typedrequest, typedrequestInterfaces };
|
|
4
|
+
import * as isohash from '@push.rocks/isohash';
|
|
5
|
+
import * as smartjson from '@push.rocks/smartjson';
|
|
6
|
+
import * as smartrx from '@push.rocks/smartrx';
|
|
7
|
+
import * as smartsocket from '@push.rocks/smartsocket';
|
|
8
|
+
import * as smartstring from '@push.rocks/smartstring';
|
|
9
|
+
import * as smarturl from '@push.rocks/smarturl';
|
|
10
|
+
export { isohash, smartjson, smartrx, smartsocket, smartstring, smarturl };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @apiglobal scope
|
|
2
|
+
import * as typedrequest from '@api.global/typedrequest';
|
|
3
|
+
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
|
4
|
+
export { typedrequest, typedrequestInterfaces };
|
|
5
|
+
// @pushrocks scope
|
|
6
|
+
import * as isohash from '@push.rocks/isohash';
|
|
7
|
+
import * as smartjson from '@push.rocks/smartjson';
|
|
8
|
+
import * as smartrx from '@push.rocks/smartrx';
|
|
9
|
+
import * as smartsocket from '@push.rocks/smartsocket';
|
|
10
|
+
import * as smartstring from '@push.rocks/smartstring';
|
|
11
|
+
import * as smarturl from '@push.rocks/smarturl';
|
|
12
|
+
export { isohash, smartjson, smartrx, smartsocket, smartstring, smarturl };
|
|
13
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZWRzb2NrZXQucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3R5cGVkc29ja2V0LnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsbUJBQW1CO0FBQ25CLE9BQU8sS0FBSyxZQUFZLE1BQU0sMEJBQTBCLENBQUM7QUFDekQsT0FBTyxLQUFLLHNCQUFzQixNQUFNLHFDQUFxQyxDQUFDO0FBRTlFLE9BQU8sRUFBRSxZQUFZLEVBQUUsc0JBQXNCLEVBQUUsQ0FBQztBQUVoRCxtQkFBbUI7QUFDbkIsT0FBTyxLQUFLLE9BQU8sTUFBTSxxQkFBcUIsQ0FBQztBQUMvQyxPQUFPLEtBQUssU0FBUyxNQUFNLHVCQUF1QixDQUFDO0FBQ25ELE9BQU8sS0FBSyxPQUFPLE1BQU0scUJBQXFCLENBQUM7QUFDL0MsT0FBTyxLQUFLLFdBQVcsTUFBTSx5QkFBeUIsQ0FBQztBQUN2RCxPQUFPLEtBQUssV0FBVyxNQUFNLHlCQUF5QixDQUFDO0FBQ3ZELE9BQU8sS0FBSyxRQUFRLE1BQU0sc0JBQXNCLENBQUM7QUFFakQsT0FBTyxFQUFFLE9BQU8sRUFBRSxTQUFTLEVBQUUsT0FBTyxFQUFFLFdBQVcsRUFBRSxXQUFXLEVBQUUsUUFBUSxFQUFFLENBQUMifQ==
|
package/license
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2020 Lossless GmbH (hello@lossless.com)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/npmextra.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"gitzone": {
|
|
3
|
+
"projectType": "npm",
|
|
4
|
+
"module": {
|
|
5
|
+
"githost": "gitlab.com",
|
|
6
|
+
"gitscope": "api.global",
|
|
7
|
+
"gitrepo": "typedsocket",
|
|
8
|
+
"description": "a typedrequest extension supporting websockets",
|
|
9
|
+
"npmPackagename": "@api.global/typedsocket",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"projectDomain": "api.global"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"npmci": {
|
|
15
|
+
"npmGlobalTools": [],
|
|
16
|
+
"npmAccessLevel": "public"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@api.global/typedsocket",
|
|
3
|
+
"version": "2.0.28",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "a typedrequest extension supporting websockets",
|
|
6
|
+
"main": "dist_ts/index.js",
|
|
7
|
+
"typings": "dist_ts/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"author": "Lossless GmbH",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "(tstest test/ --web)",
|
|
13
|
+
"build": "(tsbuild --web --allowimplicitany)",
|
|
14
|
+
"buildDocs": "tsdoc"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@gitzone/tsbuild": "^2.1.66",
|
|
18
|
+
"@gitzone/tsbundle": "^2.0.8",
|
|
19
|
+
"@gitzone/tsrun": "^1.2.44",
|
|
20
|
+
"@gitzone/tstest": "^1.0.77",
|
|
21
|
+
"@push.rocks/smartenv": "^5.0.5",
|
|
22
|
+
"@push.rocks/tapbundle": "^5.0.12",
|
|
23
|
+
"@types/node": "^20.4.8"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@api.global/typedrequest": "^3.0.1",
|
|
27
|
+
"@api.global/typedrequest-interfaces": "^3.0.1",
|
|
28
|
+
"@push.rocks/isohash": "^2.0.0",
|
|
29
|
+
"@push.rocks/smartjson": "^5.0.5",
|
|
30
|
+
"@push.rocks/smartrx": "^3.0.6",
|
|
31
|
+
"@push.rocks/smartsocket": "^2.0.22",
|
|
32
|
+
"@push.rocks/smartstring": "^4.0.2",
|
|
33
|
+
"@push.rocks/smarturl": "^3.0.5"
|
|
34
|
+
},
|
|
35
|
+
"browserslist": [
|
|
36
|
+
"last 1 chrome versions"
|
|
37
|
+
],
|
|
38
|
+
"files": [
|
|
39
|
+
"ts/**/*",
|
|
40
|
+
"ts_web/**/*",
|
|
41
|
+
"dist/**/*",
|
|
42
|
+
"dist_*/**/*",
|
|
43
|
+
"dist_ts/**/*",
|
|
44
|
+
"dist_ts_web/**/*",
|
|
45
|
+
"assets/**/*",
|
|
46
|
+
"cli.js",
|
|
47
|
+
"npmextra.json",
|
|
48
|
+
"readme.md"
|
|
49
|
+
]
|
|
50
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @apiglobal/typedsocket
|
|
2
|
+
a typedrequest extension supporting websockets
|
|
3
|
+
|
|
4
|
+
## Availabililty and Links
|
|
5
|
+
* [npmjs.org (npm package)](https://www.npmjs.com/package/@apiglobal/typedsocket)
|
|
6
|
+
* [gitlab.com (source)](https://gitlab.com/apiglobal/typedsocket)
|
|
7
|
+
* [github.com (source mirror)](https://github.com/apiglobal/typedsocket)
|
|
8
|
+
* [docs (typedoc)](https://apiglobal.gitlab.io/typedsocket/)
|
|
9
|
+
|
|
10
|
+
## Status for master
|
|
11
|
+
|
|
12
|
+
Status Category | Status Badge
|
|
13
|
+
-- | --
|
|
14
|
+
GitLab Pipelines | [](https://lossless.cloud)
|
|
15
|
+
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
|
16
|
+
npm | [](https://lossless.cloud)
|
|
17
|
+
Snyk | [](https://lossless.cloud)
|
|
18
|
+
TypeScript Support | [](https://lossless.cloud)
|
|
19
|
+
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
20
|
+
Code Style | [](https://lossless.cloud)
|
|
21
|
+
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
|
22
|
+
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
|
23
|
+
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Use TypeScript for best in class intellisense.
|
|
28
|
+
|
|
29
|
+
TypedSocket is the typedrequest you love, just in a bidirectional way.
|
|
30
|
+
|
|
31
|
+
See the following examples from our test for reference:
|
|
32
|
+
|
|
33
|
+
## Contribution
|
|
34
|
+
|
|
35
|
+
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
|
36
|
+
|
|
37
|
+
For further information read the linked docs at the top of this readme.
|
|
38
|
+
|
|
39
|
+
## Legal
|
|
40
|
+
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
|
41
|
+
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
package/ts/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './typedsocket.classes.typedsocket.js';
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import * as plugins from './typedsocket.plugins.js';
|
|
2
|
+
|
|
3
|
+
const publicRoleName = 'publicRoleName';
|
|
4
|
+
const publicRolePass = 'publicRolePass';
|
|
5
|
+
|
|
6
|
+
export type TTypedSocketSide = 'server' | 'client';
|
|
7
|
+
|
|
8
|
+
export class TypedSocket {
|
|
9
|
+
// STATIC
|
|
10
|
+
/**
|
|
11
|
+
* creates a typedsocket server
|
|
12
|
+
* note: this will fail in browser environments as server libs are not bundled.
|
|
13
|
+
*/
|
|
14
|
+
public static async createServer(
|
|
15
|
+
typedrouterArg: plugins.typedrequest.TypedRouter,
|
|
16
|
+
smartexpressServerArg?: any
|
|
17
|
+
): Promise<TypedSocket> {
|
|
18
|
+
const smartsocketServer = new plugins.smartsocket.Smartsocket({
|
|
19
|
+
alias: 'typedsocketServer',
|
|
20
|
+
port: 3000,
|
|
21
|
+
});
|
|
22
|
+
if (smartexpressServerArg) {
|
|
23
|
+
smartsocketServer.setExternalServer('smartexpress', smartexpressServerArg);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
smartsocketServer.socketFunctions.add(
|
|
27
|
+
new plugins.smartsocket.SocketFunction({
|
|
28
|
+
funcName: 'processMessage',
|
|
29
|
+
funcDef: async (dataArg, socketConnectionArg) => {
|
|
30
|
+
return typedrouterArg.routeAndAddResponse(dataArg);
|
|
31
|
+
},
|
|
32
|
+
})
|
|
33
|
+
);
|
|
34
|
+
const typedsocket = new TypedSocket(
|
|
35
|
+
'server',
|
|
36
|
+
typedrouterArg,
|
|
37
|
+
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
|
38
|
+
dataArg: T,
|
|
39
|
+
targetConnectionArg?: plugins.smartsocket.SocketConnection
|
|
40
|
+
): Promise<T> => {
|
|
41
|
+
if (!targetConnectionArg) {
|
|
42
|
+
if ((smartsocketServer.socketConnections.getArray().length = 1)) {
|
|
43
|
+
console.log(
|
|
44
|
+
'Since no targetConnection was supplied and there is only one active one present, choosing that one automatically'
|
|
45
|
+
);
|
|
46
|
+
targetConnectionArg = smartsocketServer.socketConnections.getArray()[0];
|
|
47
|
+
} else {
|
|
48
|
+
throw new Error(
|
|
49
|
+
'you need to specify the wanted targetConnection. Currently no target is selectable automatically.'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const response: T = (await smartsocketServer.clientCall(
|
|
54
|
+
'processMessage',
|
|
55
|
+
dataArg,
|
|
56
|
+
targetConnectionArg
|
|
57
|
+
)) as any;
|
|
58
|
+
return response;
|
|
59
|
+
},
|
|
60
|
+
smartsocketServer
|
|
61
|
+
);
|
|
62
|
+
await smartsocketServer.start();
|
|
63
|
+
|
|
64
|
+
return typedsocket;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public static async createClient(
|
|
68
|
+
typedrouterArg: plugins.typedrequest.TypedRouter,
|
|
69
|
+
serverUrlArg: string,
|
|
70
|
+
aliasArg = 'clientArg'
|
|
71
|
+
): Promise<TypedSocket> {
|
|
72
|
+
const domain = new plugins.smartstring.Domain(serverUrlArg);
|
|
73
|
+
|
|
74
|
+
const socketOptions: plugins.smartsocket.ISmartsocketClientOptions = {
|
|
75
|
+
alias: aliasArg,
|
|
76
|
+
port: domain.port || 3000,
|
|
77
|
+
url: `${domain.nodeParsedUrl.protocol}//${domain.nodeParsedUrl.hostname}`,
|
|
78
|
+
autoReconnect: true,
|
|
79
|
+
};
|
|
80
|
+
console.log(`starting typedsocket with the following settings:`);
|
|
81
|
+
console.log(socketOptions);
|
|
82
|
+
const smartsocketClient = new plugins.smartsocket.SmartsocketClient(socketOptions);
|
|
83
|
+
smartsocketClient.addSocketFunction(
|
|
84
|
+
new plugins.smartsocket.SocketFunction({
|
|
85
|
+
funcName: 'processMessage',
|
|
86
|
+
funcDef: async (dataArg, socketConnectionArg) => {
|
|
87
|
+
return typedrouterArg.routeAndAddResponse(dataArg);
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
);
|
|
91
|
+
const typedsocket = new TypedSocket(
|
|
92
|
+
'client',
|
|
93
|
+
typedrouterArg,
|
|
94
|
+
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(dataArg: T): Promise<T> => {
|
|
95
|
+
const response: T = smartsocketClient.serverCall('processMessage', dataArg) as any as T;
|
|
96
|
+
return response;
|
|
97
|
+
},
|
|
98
|
+
smartsocketClient
|
|
99
|
+
);
|
|
100
|
+
console.log(`typedsocket triggering smartsocket to connect...`);
|
|
101
|
+
const before = Date.now();
|
|
102
|
+
await smartsocketClient.connect();
|
|
103
|
+
console.log(`typedsocket triggered smartsocket connected in ${Date.now() - before}ms!!!`)
|
|
104
|
+
|
|
105
|
+
return typedsocket;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public static useWindowLocationOriginUrl = () => {
|
|
109
|
+
const windowLocationResult = plugins.smarturl.Smarturl.createFromUrl(globalThis.location.origin).toString();
|
|
110
|
+
return windowLocationResult;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// INSTANCE
|
|
114
|
+
public side: TTypedSocketSide;
|
|
115
|
+
public typedrouter: plugins.typedrequest.TypedRouter;
|
|
116
|
+
public get eventSubject(): plugins.smartrx.rxjs.Subject<plugins.smartsocket.TConnectionStatus> {
|
|
117
|
+
return this.socketServerOrClient.eventSubject;
|
|
118
|
+
}
|
|
119
|
+
private postMethod: plugins.typedrequest.IPostMethod &
|
|
120
|
+
((
|
|
121
|
+
typedRequestPostObject: plugins.typedrequestInterfaces.ITypedRequest,
|
|
122
|
+
socketConnectionArg?: plugins.smartsocket.SocketConnection
|
|
123
|
+
) => Promise<plugins.typedrequestInterfaces.ITypedRequest>);
|
|
124
|
+
private socketServerOrClient:
|
|
125
|
+
| plugins.smartsocket.Smartsocket
|
|
126
|
+
| plugins.smartsocket.SmartsocketClient;
|
|
127
|
+
constructor(
|
|
128
|
+
sideArg: TTypedSocketSide,
|
|
129
|
+
typedrouterArg: plugins.typedrequest.TypedRouter,
|
|
130
|
+
postMethodArg: plugins.typedrequest.IPostMethod,
|
|
131
|
+
socketServerOrClientArg: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient
|
|
132
|
+
) {
|
|
133
|
+
this.side = sideArg;
|
|
134
|
+
this.typedrouter = typedrouterArg;
|
|
135
|
+
this.postMethod = postMethodArg;
|
|
136
|
+
this.socketServerOrClient = socketServerOrClientArg;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public addTag<T extends plugins.typedrequestInterfaces.ITag = any>(
|
|
140
|
+
nameArg: T['name'],
|
|
141
|
+
payloadArg: T['payload']
|
|
142
|
+
) {
|
|
143
|
+
if (
|
|
144
|
+
this.side === 'client' &&
|
|
145
|
+
this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient
|
|
146
|
+
) {
|
|
147
|
+
this.socketServerOrClient.socketConnection.addTag({
|
|
148
|
+
id: nameArg,
|
|
149
|
+
payload: payloadArg,
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
throw new Error('tagging is only supported on clients');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
|
157
|
+
methodName: T['method'],
|
|
158
|
+
targetConnection?: plugins.smartsocket.SocketConnection
|
|
159
|
+
): plugins.typedrequest.TypedRequest<T> {
|
|
160
|
+
const typedrequest = new plugins.typedrequest.TypedRequest<T>(
|
|
161
|
+
new plugins.typedrequest.TypedTarget({
|
|
162
|
+
postMethod: async (requestDataArg) => {
|
|
163
|
+
const result = await this.postMethod(requestDataArg, targetConnection);
|
|
164
|
+
return result;
|
|
165
|
+
},
|
|
166
|
+
}),
|
|
167
|
+
methodName
|
|
168
|
+
);
|
|
169
|
+
return typedrequest;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* returns all matching target connection
|
|
174
|
+
* @param asyncFindFuncArg
|
|
175
|
+
* @returns
|
|
176
|
+
*/
|
|
177
|
+
public async findAllTargetConnections(
|
|
178
|
+
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
|
|
179
|
+
) {
|
|
180
|
+
if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) {
|
|
181
|
+
const matchingSockets: plugins.smartsocket.SocketConnection[] = [];
|
|
182
|
+
for (const socketConnection of this.socketServerOrClient.socketConnections.getArray()) {
|
|
183
|
+
if (await asyncFindFuncArg(socketConnection)) {
|
|
184
|
+
matchingSockets.push(socketConnection);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return matchingSockets;
|
|
188
|
+
} else {
|
|
189
|
+
throw new Error('this method >>findTargetConnection<< is only available from the server');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* returns a single target connection by returning the first one of all matching ones
|
|
195
|
+
* @param asyncFindFuncArg
|
|
196
|
+
* @returns
|
|
197
|
+
*/
|
|
198
|
+
public async findTargetConnection(
|
|
199
|
+
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
|
|
200
|
+
) {
|
|
201
|
+
const allMatching = await this.findAllTargetConnections(asyncFindFuncArg);
|
|
202
|
+
return allMatching[0];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public async findAllTargetConnectionsByTag<
|
|
206
|
+
TTag extends plugins.typedrequestInterfaces.ITag = any
|
|
207
|
+
>(keyArg: TTag['name'], payloadArg?: TTag['payload']) {
|
|
208
|
+
return this.findAllTargetConnections(async (socketConnectionArg) => {
|
|
209
|
+
let result: boolean;
|
|
210
|
+
if (!payloadArg) {
|
|
211
|
+
result = !!(await socketConnectionArg.getTagById(keyArg));
|
|
212
|
+
} else {
|
|
213
|
+
result = !!(
|
|
214
|
+
plugins.smartjson.stringify((await socketConnectionArg.getTagById(keyArg))?.payload) ===
|
|
215
|
+
plugins.smartjson.stringify(payloadArg)
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
return result;
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
public async findTargetConnectionByTag<TTag extends plugins.typedrequestInterfaces.ITag = any>(
|
|
223
|
+
keyArg: TTag['name'],
|
|
224
|
+
payloadArg?: TTag['payload']
|
|
225
|
+
) {
|
|
226
|
+
const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg);
|
|
227
|
+
return allResults[0];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
public async stop() {
|
|
231
|
+
await this.socketServerOrClient.stop();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @apiglobal scope
|
|
2
|
+
import * as typedrequest from '@api.global/typedrequest';
|
|
3
|
+
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
|
4
|
+
|
|
5
|
+
export { typedrequest, typedrequestInterfaces };
|
|
6
|
+
|
|
7
|
+
// @pushrocks scope
|
|
8
|
+
import * as isohash from '@push.rocks/isohash';
|
|
9
|
+
import * as smartjson from '@push.rocks/smartjson';
|
|
10
|
+
import * as smartrx from '@push.rocks/smartrx';
|
|
11
|
+
import * as smartsocket from '@push.rocks/smartsocket';
|
|
12
|
+
import * as smartstring from '@push.rocks/smartstring';
|
|
13
|
+
import * as smarturl from '@push.rocks/smarturl';
|
|
14
|
+
|
|
15
|
+
export { isohash, smartjson, smartrx, smartsocket, smartstring, smarturl };
|