@freesignal/socketio 0.1.1 → 0.1.3
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/run.d.ts +1 -0
- package/dist/run.js +4 -0
- package/dist/server.js +10 -1
- package/package.json +45 -43
- package/dist/index.d.ts +0 -84
- package/dist/index.js +0 -163
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
|
@@ -51,7 +51,16 @@ export class FreeSignalServer extends FreeSignalSocketio {
|
|
|
51
51
|
}
|
|
52
52
|
listen(server) {
|
|
53
53
|
server !== null && server !== void 0 ? server : (server = 12437);
|
|
54
|
-
this.wss = new Server(server
|
|
54
|
+
this.wss = new Server(server, {
|
|
55
|
+
cors: {
|
|
56
|
+
/*origin: (origin, callback) => {
|
|
57
|
+
// origin può essere undefined (curl, mobile app, server)
|
|
58
|
+
callback(null, true);
|
|
59
|
+
},*/
|
|
60
|
+
origin: '*',
|
|
61
|
+
methods: ["GET", "POST"]
|
|
62
|
+
}
|
|
63
|
+
});
|
|
55
64
|
this.wss.on('connection', (socket) => __awaiter(this, void 0, void 0, function* () {
|
|
56
65
|
const userId = socket.handshake.auth.userId;
|
|
57
66
|
if (!userId) {
|
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.3",
|
|
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
|
-
}
|