@freesignal/socketio 0.1.2 → 0.1.4
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/LICENSE +674 -674
- package/dist/base.d.ts +4 -0
- package/dist/base.js +6 -0
- package/dist/client.js +4 -4
- package/dist/run.d.ts +1 -0
- package/dist/run.js +4 -0
- package/dist/server.js +7 -6
- package/package.json +45 -43
- package/dist/index.d.ts +0 -84
- package/dist/index.js +0 -163
package/dist/base.d.ts
CHANGED
|
@@ -2,6 +2,10 @@ import { Database, LocalStorage, Crypto, KeyExchangeDataBundle } from "@freesign
|
|
|
2
2
|
import { Datagram, PrivateIdentityKey, UserId } from "@freesignal/protocol";
|
|
3
3
|
import { ExportedKeySession } from "@freesignal/protocol/double-ratchet";
|
|
4
4
|
import { BootstrapRequest, FreeSignalNode } from "@freesignal/protocol/node";
|
|
5
|
+
export declare enum TransportEvent {
|
|
6
|
+
MESSAGE = "transport:message",
|
|
7
|
+
HANDSHAKE = "transport:handshake"
|
|
8
|
+
}
|
|
5
9
|
export declare class FreeSignalSocketio extends FreeSignalNode {
|
|
6
10
|
protected readonly outbox: LocalStorage<string, Uint8Array[]>;
|
|
7
11
|
constructor(storage: Database<{
|
package/dist/base.js
CHANGED
|
@@ -9,6 +9,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { Datagram, UserId } from "@freesignal/protocol";
|
|
11
11
|
import { FreeSignalNode } from "@freesignal/protocol/node";
|
|
12
|
+
const TransportEventPrefix = "transport:";
|
|
13
|
+
export var TransportEvent;
|
|
14
|
+
(function (TransportEvent) {
|
|
15
|
+
TransportEvent["MESSAGE"] = "transport:message";
|
|
16
|
+
TransportEvent["HANDSHAKE"] = "transport:handshake";
|
|
17
|
+
})(TransportEvent || (TransportEvent = {}));
|
|
12
18
|
export class FreeSignalSocketio extends FreeSignalNode {
|
|
13
19
|
constructor(storage, privateIdentityKey) {
|
|
14
20
|
super(storage, privateIdentityKey);
|
package/dist/client.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { UserId } from "@freesignal/protocol";
|
|
11
11
|
import { io } from "socket.io-client";
|
|
12
|
-
import { FreeSignalSocketio } from "./base.js";
|
|
12
|
+
import { FreeSignalSocketio, TransportEvent } from "./base.js";
|
|
13
13
|
export class FreeSignalClient extends FreeSignalSocketio {
|
|
14
14
|
constructor(storage, privateIdentityKey) {
|
|
15
15
|
super(storage, privateIdentityKey);
|
|
@@ -25,7 +25,7 @@ export class FreeSignalClient extends FreeSignalSocketio {
|
|
|
25
25
|
else if (!this.socket || !this.socket.connected)
|
|
26
26
|
this.addToOutbox(receiverId, datagram);
|
|
27
27
|
else
|
|
28
|
-
this.socket.
|
|
28
|
+
this.socket.emit(TransportEvent.MESSAGE, datagram.toBytes());
|
|
29
29
|
};
|
|
30
30
|
this.emitter.on('send', this.sendHandler);
|
|
31
31
|
}
|
|
@@ -44,13 +44,13 @@ export class FreeSignalClient extends FreeSignalSocketio {
|
|
|
44
44
|
userId: this.userId.toString()
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
|
-
this.socket.on(
|
|
47
|
+
this.socket.on(TransportEvent.HANDSHAKE, (userId) => __awaiter(this, void 0, void 0, function* () {
|
|
48
48
|
this._serverId = userId;
|
|
49
49
|
yield this.waitHandshaked(userId);
|
|
50
50
|
yield this.flushOutbox();
|
|
51
51
|
resolve();
|
|
52
52
|
}));
|
|
53
|
-
this.socket.on(
|
|
53
|
+
this.socket.on(TransportEvent.MESSAGE, (data) => { this.open(data); });
|
|
54
54
|
this.socket.on('disconnect', () => {
|
|
55
55
|
this.onClose();
|
|
56
56
|
this.socket = undefined;
|
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/run.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AsyncMap } from "@freesignal/protocol";
|
|
2
|
+
import { FreeSignalServer } from "./server.js";
|
|
3
|
+
const server = new FreeSignalServer({ keyExchange: new AsyncMap(), sessions: new AsyncMap(), users: new AsyncMap(), bundles: new AsyncMap(), bootstraps: new AsyncMap(), outbox: new AsyncMap() }).listen();
|
|
4
|
+
console.log("Freesignal Server started!");
|
package/dist/server.js
CHANGED
|
@@ -26,7 +26,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
26
26
|
});
|
|
27
27
|
};
|
|
28
28
|
import { Server } from 'socket.io';
|
|
29
|
-
import { FreeSignalSocketio } from "./base.js";
|
|
29
|
+
import { FreeSignalSocketio, TransportEvent } from "./base.js";
|
|
30
30
|
export class FreeSignalServer extends FreeSignalSocketio {
|
|
31
31
|
constructor(storage, privateIdentityKey) {
|
|
32
32
|
super(storage, privateIdentityKey);
|
|
@@ -44,7 +44,7 @@ export class FreeSignalServer extends FreeSignalSocketio {
|
|
|
44
44
|
if (!socket.connected)
|
|
45
45
|
this.addToOutbox(receiverId, datagram);
|
|
46
46
|
else
|
|
47
|
-
socket.
|
|
47
|
+
socket.emit(TransportEvent.MESSAGE, datagram.toBytes());
|
|
48
48
|
};
|
|
49
49
|
this.emitter.on('send', this.sendHandler);
|
|
50
50
|
this.emitter.on('bootstrap', ({ request }) => request === null || request === void 0 ? void 0 : request.accept());
|
|
@@ -53,10 +53,11 @@ export class FreeSignalServer extends FreeSignalSocketio {
|
|
|
53
53
|
server !== null && server !== void 0 ? server : (server = 12437);
|
|
54
54
|
this.wss = new Server(server, {
|
|
55
55
|
cors: {
|
|
56
|
-
origin: (origin, callback) => {
|
|
56
|
+
/*origin: (origin, callback) => {
|
|
57
57
|
// origin può essere undefined (curl, mobile app, server)
|
|
58
58
|
callback(null, true);
|
|
59
|
-
}
|
|
59
|
+
},*/
|
|
60
|
+
origin: '*',
|
|
60
61
|
methods: ["GET", "POST"]
|
|
61
62
|
}
|
|
62
63
|
});
|
|
@@ -68,9 +69,9 @@ export class FreeSignalServer extends FreeSignalSocketio {
|
|
|
68
69
|
}
|
|
69
70
|
console.debug("Client connected: ", userId);
|
|
70
71
|
this.connections.set(userId, socket);
|
|
71
|
-
socket.on(
|
|
72
|
+
socket.on(TransportEvent.MESSAGE, (data) => this.open(data));
|
|
72
73
|
socket.on('disconnect', () => this.connections.delete(userId));
|
|
73
|
-
socket.emit(
|
|
74
|
+
socket.emit(TransportEvent.HANDSHAKE, this.userId.toString());
|
|
74
75
|
if (!(yield this.sessions.has(userId)))
|
|
75
76
|
this.sendBootstrap(userId);
|
|
76
77
|
else
|
package/package.json
CHANGED
|
@@ -1,43 +1,45 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@freesignal/socketio",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Socket.io trasnport package for FreeSignal protocol",
|
|
5
|
-
"homepage": "https://github.com/christianbraghette/freesignal-socketio#readme",
|
|
6
|
-
"bugs": {
|
|
7
|
-
"url": "https://github.com/christianbraghette/freesignal-socketio/issues"
|
|
8
|
-
},
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/christianbraghette/freesignal-socketio.git"
|
|
12
|
-
},
|
|
13
|
-
"license": "GPL-3.0-or-later",
|
|
14
|
-
"author": "Christian Braghette",
|
|
15
|
-
"type": "module",
|
|
16
|
-
"exports": {
|
|
17
|
-
"./server": {
|
|
18
|
-
"import": "./dist/server.js",
|
|
19
|
-
"require": "./dist/server.js",
|
|
20
|
-
"types": "./dist/server.d.ts"
|
|
21
|
-
},
|
|
22
|
-
"./client": {
|
|
23
|
-
"import": "./dist/client.js",
|
|
24
|
-
"require": "./dist/client.js",
|
|
25
|
-
"types": "./dist/client.d.ts"
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
"scripts": {
|
|
29
|
-
"pretest": "tsc",
|
|
30
|
-
"test": "node ./dist/test.js",
|
|
31
|
-
"prepare": "tsc"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"@freesignal/
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@freesignal/socketio",
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "Socket.io trasnport package for FreeSignal protocol",
|
|
5
|
+
"homepage": "https://github.com/christianbraghette/freesignal-socketio#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/christianbraghette/freesignal-socketio/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/christianbraghette/freesignal-socketio.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "GPL-3.0-or-later",
|
|
14
|
+
"author": "Christian Braghette",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
"./server": {
|
|
18
|
+
"import": "./dist/server.js",
|
|
19
|
+
"require": "./dist/server.js",
|
|
20
|
+
"types": "./dist/server.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./client": {
|
|
23
|
+
"import": "./dist/client.js",
|
|
24
|
+
"require": "./dist/client.js",
|
|
25
|
+
"types": "./dist/client.d.ts"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"pretest": "tsc",
|
|
30
|
+
"test": "node ./dist/test.js",
|
|
31
|
+
"prepare": "tsc",
|
|
32
|
+
"prestart": "tsc",
|
|
33
|
+
"start": "node ./dist/run"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^22.15.21"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@freesignal/interfaces": "^0.3.1",
|
|
40
|
+
"@freesignal/protocol": "^0.8.0",
|
|
41
|
+
"@freesignal/utils": "^1.5.1",
|
|
42
|
+
"socket.io": "^4.8.1",
|
|
43
|
+
"socket.io-client": "^4.8.1"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* FreeSignal Protocol
|
|
3
|
-
*
|
|
4
|
-
* Copyright (C) 2025 Christian Braghette
|
|
5
|
-
*
|
|
6
|
-
* This program is free software: you can redistribute it and/or modify
|
|
7
|
-
* it under the terms of the GNU General Public License as published by
|
|
8
|
-
* the Free Software Foundation, either version 3 of the License, or
|
|
9
|
-
* (at your option) any later version.
|
|
10
|
-
*
|
|
11
|
-
* This program is distributed in the hope that it will be useful,
|
|
12
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
-
* GNU General Public License for more details.
|
|
15
|
-
*
|
|
16
|
-
* You should have received a copy of the GNU General Public License
|
|
17
|
-
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
18
|
-
*/
|
|
19
|
-
import { Database, LocalStorage, Crypto, KeyExchangeDataBundle } from "@freesignal/interfaces";
|
|
20
|
-
import { Datagram, PrivateIdentityKey, UserId } from "@freesignal/protocol";
|
|
21
|
-
import { ExportedKeySession, KeySession } from "@freesignal/protocol/double-ratchet";
|
|
22
|
-
import { BootstrapRequest, FreeSignalNode } from "@freesignal/protocol/node";
|
|
23
|
-
import { EventCallback } from "easyemitter.ts";
|
|
24
|
-
import { type Server as HttpServer } from "http";
|
|
25
|
-
declare class FreeSignalSocketio extends FreeSignalNode {
|
|
26
|
-
protected readonly outbox: LocalStorage<string, Uint8Array[]>;
|
|
27
|
-
constructor(storage: Database<{
|
|
28
|
-
sessions: LocalStorage<string, ExportedKeySession>;
|
|
29
|
-
keyExchange: LocalStorage<string, Crypto.KeyPair>;
|
|
30
|
-
users: LocalStorage<string, string>;
|
|
31
|
-
bundles: LocalStorage<string, KeyExchangeDataBundle>;
|
|
32
|
-
bootstraps: LocalStorage<string, BootstrapRequest>;
|
|
33
|
-
outbox: LocalStorage<string, Uint8Array[]>;
|
|
34
|
-
}>, privateIdentityKey?: PrivateIdentityKey);
|
|
35
|
-
protected addToOutbox(userId: string | UserId, datagram: Datagram | Uint8Array): Promise<void>;
|
|
36
|
-
protected flushOutbox(): Promise<void>;
|
|
37
|
-
onClose: () => void;
|
|
38
|
-
onError: (err: Event | Error) => void;
|
|
39
|
-
}
|
|
40
|
-
export declare class FreeSignalClient extends FreeSignalSocketio {
|
|
41
|
-
private _serverId?;
|
|
42
|
-
private socket?;
|
|
43
|
-
constructor(storage: Database<{
|
|
44
|
-
sessions: LocalStorage<string, ExportedKeySession>;
|
|
45
|
-
keyExchange: LocalStorage<string, Crypto.KeyPair>;
|
|
46
|
-
users: LocalStorage<string, string>;
|
|
47
|
-
bundles: LocalStorage<string, KeyExchangeDataBundle>;
|
|
48
|
-
bootstraps: LocalStorage<string, BootstrapRequest>;
|
|
49
|
-
outbox: LocalStorage<string, Uint8Array[]>;
|
|
50
|
-
}>, privateIdentityKey?: PrivateIdentityKey);
|
|
51
|
-
get connected(): boolean;
|
|
52
|
-
get serverId(): UserId | undefined;
|
|
53
|
-
protected sendHandler: EventCallback<{
|
|
54
|
-
session?: KeySession;
|
|
55
|
-
payload?: Uint8Array;
|
|
56
|
-
datagram?: Datagram;
|
|
57
|
-
request?: BootstrapRequest;
|
|
58
|
-
userId?: UserId;
|
|
59
|
-
}, typeof this.emitter>;
|
|
60
|
-
connect(url: string | URL): Promise<void>;
|
|
61
|
-
close(): void;
|
|
62
|
-
}
|
|
63
|
-
export declare class FreeSignalServer extends FreeSignalSocketio {
|
|
64
|
-
private wss?;
|
|
65
|
-
private readonly connections;
|
|
66
|
-
constructor(storage: Database<{
|
|
67
|
-
sessions: LocalStorage<string, ExportedKeySession>;
|
|
68
|
-
keyExchange: LocalStorage<string, Crypto.KeyPair>;
|
|
69
|
-
users: LocalStorage<string, string>;
|
|
70
|
-
bundles: LocalStorage<string, KeyExchangeDataBundle>;
|
|
71
|
-
bootstraps: LocalStorage<string, BootstrapRequest>;
|
|
72
|
-
outbox: LocalStorage<string, Uint8Array[]>;
|
|
73
|
-
}>, privateIdentityKey?: PrivateIdentityKey);
|
|
74
|
-
protected sendHandler: EventCallback<{
|
|
75
|
-
session?: KeySession;
|
|
76
|
-
datagram?: Datagram;
|
|
77
|
-
userId?: UserId;
|
|
78
|
-
}, typeof this.emitter>;
|
|
79
|
-
listen(): this;
|
|
80
|
-
listen(port: number): this;
|
|
81
|
-
listen(server: HttpServer): this;
|
|
82
|
-
close(): void;
|
|
83
|
-
}
|
|
84
|
-
export {};
|
package/dist/index.js
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* FreeSignal Protocol
|
|
3
|
-
*
|
|
4
|
-
* Copyright (C) 2025 Christian Braghette
|
|
5
|
-
*
|
|
6
|
-
* This program is free software: you can redistribute it and/or modify
|
|
7
|
-
* it under the terms of the GNU General Public License as published by
|
|
8
|
-
* the Free Software Foundation, either version 3 of the License, or
|
|
9
|
-
* (at your option) any later version.
|
|
10
|
-
*
|
|
11
|
-
* This program is distributed in the hope that it will be useful,
|
|
12
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
-
* GNU General Public License for more details.
|
|
15
|
-
*
|
|
16
|
-
* You should have received a copy of the GNU General Public License
|
|
17
|
-
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
18
|
-
*/
|
|
19
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
20
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
21
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
22
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
23
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
24
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
25
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
import { Datagram, UserId } from "@freesignal/protocol";
|
|
29
|
-
import { FreeSignalNode } from "@freesignal/protocol/node";
|
|
30
|
-
import { Server } from 'socket.io';
|
|
31
|
-
import { io } from "socket.io-client";
|
|
32
|
-
class FreeSignalSocketio extends FreeSignalNode {
|
|
33
|
-
constructor(storage, privateIdentityKey) {
|
|
34
|
-
super(storage, privateIdentityKey);
|
|
35
|
-
this.onClose = () => { };
|
|
36
|
-
this.onError = () => { };
|
|
37
|
-
this.outbox = storage.outbox;
|
|
38
|
-
}
|
|
39
|
-
addToOutbox(userId, datagram) {
|
|
40
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
const array = yield this.outbox.get(userId.toString());
|
|
42
|
-
this.outbox.set(userId.toString(), [...(array !== null && array !== void 0 ? array : []), Datagram.from(datagram).toBytes()]);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
flushOutbox() {
|
|
46
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
-
var _a;
|
|
48
|
-
for (const [userId, array] of Array.from((_a = yield this.outbox.entries()) !== null && _a !== void 0 ? _a : [])) {
|
|
49
|
-
yield this.outbox.delete(userId);
|
|
50
|
-
for (const data of array) {
|
|
51
|
-
this.emitter.emit('send', { userId: UserId.from(userId), datagram: Datagram.from(data) });
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
export class FreeSignalClient extends FreeSignalSocketio {
|
|
58
|
-
constructor(storage, privateIdentityKey) {
|
|
59
|
-
super(storage, privateIdentityKey);
|
|
60
|
-
this.sendHandler = (eventData) => {
|
|
61
|
-
const { session, datagram, userId } = eventData;
|
|
62
|
-
if (!userId && !session)
|
|
63
|
-
throw new Error("UserId missing");
|
|
64
|
-
const receiverId = (userId === null || userId === void 0 ? void 0 : userId.toString()) || (session === null || session === void 0 ? void 0 : session.userId.toString());
|
|
65
|
-
if (!datagram)
|
|
66
|
-
throw new Error("Datagram missing");
|
|
67
|
-
if (!!this._serverId && receiverId !== this._serverId)
|
|
68
|
-
this.sendRelay(this._serverId, receiverId, datagram);
|
|
69
|
-
else if (!this.socket || !this.socket.connected)
|
|
70
|
-
this.addToOutbox(receiverId, datagram);
|
|
71
|
-
else
|
|
72
|
-
this.socket.send(datagram.toBytes());
|
|
73
|
-
};
|
|
74
|
-
this.emitter.on('send', this.sendHandler);
|
|
75
|
-
}
|
|
76
|
-
get connected() {
|
|
77
|
-
var _a, _b;
|
|
78
|
-
return (_b = (_a = this.socket) === null || _a === void 0 ? void 0 : _a.connected) !== null && _b !== void 0 ? _b : false;
|
|
79
|
-
}
|
|
80
|
-
get serverId() {
|
|
81
|
-
return this._serverId ? UserId.from(this._serverId) : undefined;
|
|
82
|
-
}
|
|
83
|
-
connect(url) {
|
|
84
|
-
return new Promise((resolve, reject) => {
|
|
85
|
-
try {
|
|
86
|
-
this.socket = io(url.toString(), {
|
|
87
|
-
auth: {
|
|
88
|
-
userId: this.userId.toString()
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
this.socket.on('handshake', (userId) => __awaiter(this, void 0, void 0, function* () {
|
|
92
|
-
this._serverId = userId;
|
|
93
|
-
yield this.waitHandshaked(userId);
|
|
94
|
-
yield this.flushOutbox();
|
|
95
|
-
resolve();
|
|
96
|
-
}));
|
|
97
|
-
this.socket.on('message', (data) => __awaiter(this, void 0, void 0, function* () { this.open(data); }));
|
|
98
|
-
this.socket.on('disconnect', () => {
|
|
99
|
-
this.onClose();
|
|
100
|
-
this.socket = undefined;
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
reject(error);
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
close() {
|
|
109
|
-
if (!this.socket)
|
|
110
|
-
throw new Error("Socket not connected");
|
|
111
|
-
this.socket.close();
|
|
112
|
-
this.onClose();
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
export class FreeSignalServer extends FreeSignalSocketio {
|
|
116
|
-
constructor(storage, privateIdentityKey) {
|
|
117
|
-
super(storage, privateIdentityKey);
|
|
118
|
-
this.connections = new Map();
|
|
119
|
-
this.sendHandler = (eventData) => {
|
|
120
|
-
const { session, datagram, userId } = eventData;
|
|
121
|
-
if (!userId && !session)
|
|
122
|
-
throw new Error("UserId missing");
|
|
123
|
-
const receiverId = (userId === null || userId === void 0 ? void 0 : userId.toString()) || (session === null || session === void 0 ? void 0 : session.userId.toString());
|
|
124
|
-
const socket = this.connections.get(receiverId);
|
|
125
|
-
if (!socket)
|
|
126
|
-
throw new Error("Socket not found for user: " + userId);
|
|
127
|
-
if (!datagram)
|
|
128
|
-
throw new Error("Datagram missing");
|
|
129
|
-
if (!socket.connected)
|
|
130
|
-
this.addToOutbox(receiverId, datagram);
|
|
131
|
-
else
|
|
132
|
-
socket.send(datagram.toBytes());
|
|
133
|
-
};
|
|
134
|
-
this.emitter.on('send', this.sendHandler);
|
|
135
|
-
this.emitter.on('bootstrap', ({ request }) => request === null || request === void 0 ? void 0 : request.accept());
|
|
136
|
-
}
|
|
137
|
-
listen(server) {
|
|
138
|
-
server !== null && server !== void 0 ? server : (server = 12437);
|
|
139
|
-
this.wss = new Server(server);
|
|
140
|
-
this.wss.on('connection', (socket) => __awaiter(this, void 0, void 0, function* () {
|
|
141
|
-
const userId = socket.handshake.auth.userId;
|
|
142
|
-
if (!userId) {
|
|
143
|
-
socket.disconnect();
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
console.debug("Client connected: ", userId);
|
|
147
|
-
this.connections.set(userId, socket);
|
|
148
|
-
socket.on('message', (data) => this.open(data));
|
|
149
|
-
socket.on('disconnect', () => this.connections.delete(userId));
|
|
150
|
-
socket.emit('handshake', this.userId.toString());
|
|
151
|
-
if (!(yield this.sessions.has(userId)))
|
|
152
|
-
this.sendBootstrap(userId);
|
|
153
|
-
else
|
|
154
|
-
this.sendHandshake(userId);
|
|
155
|
-
}));
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
close() {
|
|
159
|
-
var _a;
|
|
160
|
-
if ((_a = this.wss) === null || _a === void 0 ? void 0 : _a.close((err) => err ? this.onError(err) : undefined))
|
|
161
|
-
this.onClose();
|
|
162
|
-
}
|
|
163
|
-
}
|