@tangentfeed/signaling-server 0.2.0
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 +21 -0
- package/README.md +9 -0
- package/bin/server.mjs +8 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +79 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sreeraj T A
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @tangentfeed/signaling-server
|
|
2
|
+
|
|
3
|
+
Blind WebRTC signaling relay for tangentfeed. Handles presence and relays opaque signaling blobs; never sees your data.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx @tangentfeed/signaling-server # listens on :8787
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Part of [tangentfeed](https://github.com/sreerajta/tangentfeed). MIT licensed.
|
package/bin/server.mjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Standalone runner: PORT=8787 node bin/server.mjs
|
|
3
|
+
// Uses tsx-free plain import of built output; for dev: npx tsx bin/dev.mts
|
|
4
|
+
import { createSignalingServer } from "../dist/index.js";
|
|
5
|
+
const port = Number(process.env.PORT ?? 8787);
|
|
6
|
+
const server = await createSignalingServer({ port });
|
|
7
|
+
console.log(`tangentfeed signaling server listening on ws://0.0.0.0:${server.port}`);
|
|
8
|
+
process.on("SIGINT", async () => { await server.close(); process.exit(0); });
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signaling server — the only piece of infrastructure in the whole system,
|
|
3
|
+
* kept honest by knowing nothing: it never sees ops, state, or plaintext.
|
|
4
|
+
* It does two jobs: presence (who's in a space's room) and blind relay of
|
|
5
|
+
* signaling blobs (SDP offers/answers, ICE candidates) between peers.
|
|
6
|
+
*
|
|
7
|
+
* Wire (JSON over WebSocket):
|
|
8
|
+
* client → server: { t:"join", space, device }
|
|
9
|
+
* { t:"signal", to, data } // data is opaque
|
|
10
|
+
* server → client: { t:"peers", devices: [...] } // reply to join
|
|
11
|
+
* { t:"peer-joined", device }
|
|
12
|
+
* { t:"peer-left", device }
|
|
13
|
+
* { t:"signal", from, data }
|
|
14
|
+
* { t:"error", message }
|
|
15
|
+
*/
|
|
16
|
+
interface SignalingServer {
|
|
17
|
+
port: number;
|
|
18
|
+
close(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
declare function createSignalingServer(opts?: {
|
|
21
|
+
port?: number;
|
|
22
|
+
}): Promise<SignalingServer>;
|
|
23
|
+
|
|
24
|
+
export { type SignalingServer, createSignalingServer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { WebSocketServer, WebSocket } from "ws";
|
|
3
|
+
function createSignalingServer(opts = {}) {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const wss = new WebSocketServer({ port: opts.port ?? 0 });
|
|
6
|
+
const rooms = /* @__PURE__ */ new Map();
|
|
7
|
+
wss.on("connection", (ws) => {
|
|
8
|
+
let me = null;
|
|
9
|
+
ws.on("message", (raw) => {
|
|
10
|
+
let msg;
|
|
11
|
+
try {
|
|
12
|
+
msg = JSON.parse(raw.toString());
|
|
13
|
+
} catch {
|
|
14
|
+
return send(ws, { t: "error", message: "invalid JSON" });
|
|
15
|
+
}
|
|
16
|
+
if (msg["t"] === "join") {
|
|
17
|
+
const space = str(msg["space"]);
|
|
18
|
+
const device = str(msg["device"]);
|
|
19
|
+
if (!space || !device) return send(ws, { t: "error", message: "join needs space and device" });
|
|
20
|
+
if (me) leave(me);
|
|
21
|
+
me = { ws, space, device };
|
|
22
|
+
const room = rooms.get(space) ?? /* @__PURE__ */ new Map();
|
|
23
|
+
const evicted = room.get(device);
|
|
24
|
+
if (evicted) evicted.ws.close(4e3, "replaced by new connection");
|
|
25
|
+
room.set(device, me);
|
|
26
|
+
rooms.set(space, room);
|
|
27
|
+
send(ws, { t: "peers", devices: [...room.keys()].filter((d) => d !== device) });
|
|
28
|
+
for (const c of room.values()) {
|
|
29
|
+
if (c.device !== device) send(c.ws, { t: "peer-joined", device });
|
|
30
|
+
}
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (msg["t"] === "signal") {
|
|
34
|
+
if (!me) return send(ws, { t: "error", message: "join first" });
|
|
35
|
+
const to = str(msg["to"]);
|
|
36
|
+
const target = rooms.get(me.space)?.get(to);
|
|
37
|
+
if (!target) return;
|
|
38
|
+
send(target.ws, { t: "signal", from: me.device, data: msg["data"] });
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
send(ws, { t: "error", message: `unknown message type` });
|
|
42
|
+
});
|
|
43
|
+
ws.on("close", () => {
|
|
44
|
+
if (me) leave(me);
|
|
45
|
+
me = null;
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
function leave(c) {
|
|
49
|
+
const room = rooms.get(c.space);
|
|
50
|
+
if (!room || room.get(c.device)?.ws !== c.ws) return;
|
|
51
|
+
room.delete(c.device);
|
|
52
|
+
if (room.size === 0) rooms.delete(c.space);
|
|
53
|
+
for (const other of room?.values() ?? []) {
|
|
54
|
+
send(other.ws, { t: "peer-left", device: c.device });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
wss.on("listening", () => {
|
|
58
|
+
const addr = wss.address();
|
|
59
|
+
const port = typeof addr === "object" && addr ? addr.port : opts.port ?? 0;
|
|
60
|
+
resolve({
|
|
61
|
+
port,
|
|
62
|
+
close: () => new Promise((res) => {
|
|
63
|
+
for (const c of wss.clients) c.terminate();
|
|
64
|
+
wss.close(() => res());
|
|
65
|
+
})
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
wss.on("error", reject);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function send(ws, msg) {
|
|
72
|
+
if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(msg));
|
|
73
|
+
}
|
|
74
|
+
function str(v) {
|
|
75
|
+
return typeof v === "string" ? v : "";
|
|
76
|
+
}
|
|
77
|
+
export {
|
|
78
|
+
createSignalingServer
|
|
79
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tangentfeed/signaling-server",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"tangentfeed-signaling": "bin/server.mjs"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "vitest run --passWithNoTests",
|
|
11
|
+
"start": "node bin/server.mjs",
|
|
12
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
13
|
+
"prepack": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"ws": "^8.18.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^20.0.0",
|
|
20
|
+
"@types/ws": "^8.5.0",
|
|
21
|
+
"tsx": "^4.19.0",
|
|
22
|
+
"typescript": "^5.5.0",
|
|
23
|
+
"vitest": "^2.0.0",
|
|
24
|
+
"esbuild": "^0.23.0",
|
|
25
|
+
"tsup": "^8.5.0"
|
|
26
|
+
},
|
|
27
|
+
"description": "Blind WebRTC signaling relay for tangentfeed",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/sreerajta/tangentfeed.git",
|
|
32
|
+
"directory": "packages/signaling-server"
|
|
33
|
+
},
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist",
|
|
43
|
+
"README.md",
|
|
44
|
+
"LICENSE"
|
|
45
|
+
],
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=20"
|
|
48
|
+
}
|
|
49
|
+
}
|