peer-by-ip 1.0.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 +22 -0
- package/README.md +317 -0
- package/dist/bin/peerjs.js +3 -0
- package/dist/bin/peerjs.js.map +1 -0
- package/dist/index.cjs +856 -0
- package/dist/index.cjs.map +1 -0
- package/dist/module.mjs +850 -0
- package/dist/module.mjs.map +1 -0
- package/dist/peer.d.ts +66 -0
- package/dist/peer.d.ts.map +1 -0
- package/package.json +110 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,856 @@
|
|
|
1
|
+
var $cl6Iw$express = require("express");
|
|
2
|
+
var $cl6Iw$nodehttp = require("node:http");
|
|
3
|
+
var $cl6Iw$nodehttps = require("node:https");
|
|
4
|
+
var $cl6Iw$nodepath = require("node:path");
|
|
5
|
+
var $cl6Iw$nodecrypto = require("node:crypto");
|
|
6
|
+
var $cl6Iw$nodeevents = require("node:events");
|
|
7
|
+
var $cl6Iw$ws = require("ws");
|
|
8
|
+
var $cl6Iw$cors = require("cors");
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
function $parcel$interopDefault(a) {
|
|
12
|
+
return a && a.__esModule ? a.default : a;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function $parcel$export(e, n, v, s) {
|
|
16
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
$parcel$export(module.exports, "ExpressPeerServer", () => $123f4982a51872b4$export$8c57434a18c696c9);
|
|
20
|
+
$parcel$export(module.exports, "PeerServer", () => $123f4982a51872b4$export$f99d31af51f48b1e);
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
const $629a620fdd8149f9$var$defaultConfig = {
|
|
25
|
+
host: "::",
|
|
26
|
+
port: 9000,
|
|
27
|
+
expire_timeout: 5000,
|
|
28
|
+
alive_timeout: 90000,
|
|
29
|
+
key: "peerjs",
|
|
30
|
+
path: "/",
|
|
31
|
+
concurrent_limit: 5000,
|
|
32
|
+
allow_discovery: false,
|
|
33
|
+
proxied: false,
|
|
34
|
+
cleanup_out_msgs: 1000,
|
|
35
|
+
corsOptions: {
|
|
36
|
+
origin: true
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var $629a620fdd8149f9$export$2e2bcd8739ae039 = $629a620fdd8149f9$var$defaultConfig;
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class $884a4c78775bc4af$export$eb4c623330d4cbcc {
|
|
44
|
+
getLastReadAt() {
|
|
45
|
+
return this.lastReadAt;
|
|
46
|
+
}
|
|
47
|
+
addMessage(message) {
|
|
48
|
+
this.messages.push(message);
|
|
49
|
+
}
|
|
50
|
+
readMessage() {
|
|
51
|
+
if (this.messages.length > 0) {
|
|
52
|
+
this.lastReadAt = new Date().getTime();
|
|
53
|
+
return this.messages.shift();
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
getMessages() {
|
|
58
|
+
return this.messages;
|
|
59
|
+
}
|
|
60
|
+
constructor(){
|
|
61
|
+
this.lastReadAt = new Date().getTime();
|
|
62
|
+
this.messages = [];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class $7a434f2b8c14dece$export$3ee29d34e33d9116 {
|
|
69
|
+
getClientsIds() {
|
|
70
|
+
return [
|
|
71
|
+
...this.clients.keys()
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
getClientById(clientId) {
|
|
75
|
+
return this.clients.get(clientId);
|
|
76
|
+
}
|
|
77
|
+
getClientsIdsWithQueue() {
|
|
78
|
+
return [
|
|
79
|
+
...this.messageQueues.keys()
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
setClient(client, id) {
|
|
83
|
+
this.clients.set(id, client);
|
|
84
|
+
// Add to IP-based grouping
|
|
85
|
+
const ip = client.getIpAddress();
|
|
86
|
+
if (ip) {
|
|
87
|
+
let ipGroup = this.clientsByIp.get(ip);
|
|
88
|
+
if (!ipGroup) {
|
|
89
|
+
ipGroup = new Map();
|
|
90
|
+
this.clientsByIp.set(ip, ipGroup);
|
|
91
|
+
}
|
|
92
|
+
ipGroup.set(id, client);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
removeClientById(id) {
|
|
96
|
+
const client = this.getClientById(id);
|
|
97
|
+
if (!client) return false;
|
|
98
|
+
// Remove from IP-based grouping
|
|
99
|
+
const ip = client.getIpAddress();
|
|
100
|
+
if (ip) {
|
|
101
|
+
const ipGroup = this.clientsByIp.get(ip);
|
|
102
|
+
if (ipGroup) {
|
|
103
|
+
ipGroup.delete(id);
|
|
104
|
+
if (ipGroup.size === 0) this.clientsByIp.delete(ip);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
this.clients.delete(id);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
getMessageQueueById(id) {
|
|
111
|
+
return this.messageQueues.get(id);
|
|
112
|
+
}
|
|
113
|
+
addMessageToQueue(id, message) {
|
|
114
|
+
if (!this.getMessageQueueById(id)) this.messageQueues.set(id, new (0, $884a4c78775bc4af$export$eb4c623330d4cbcc)());
|
|
115
|
+
this.getMessageQueueById(id)?.addMessage(message);
|
|
116
|
+
}
|
|
117
|
+
clearMessageQueue(id) {
|
|
118
|
+
this.messageQueues.delete(id);
|
|
119
|
+
}
|
|
120
|
+
generateClientId(generateClientId) {
|
|
121
|
+
const generateId = generateClientId ? generateClientId : (0, $cl6Iw$nodecrypto.randomUUID);
|
|
122
|
+
let clientId = generateId();
|
|
123
|
+
while(this.getClientById(clientId))clientId = generateId();
|
|
124
|
+
return clientId;
|
|
125
|
+
}
|
|
126
|
+
getClientsByIp(ipAddress) {
|
|
127
|
+
return this.clientsByIp.get(ipAddress);
|
|
128
|
+
}
|
|
129
|
+
getAllIpGroups() {
|
|
130
|
+
return this.clientsByIp;
|
|
131
|
+
}
|
|
132
|
+
constructor(){
|
|
133
|
+
this.clients = new Map();
|
|
134
|
+
this.messageQueues = new Map();
|
|
135
|
+
this.clientsByIp = new Map();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
const $41c352e9bd485a56$var$DEFAULT_CHECK_INTERVAL = 300;
|
|
141
|
+
class $41c352e9bd485a56$export$6fa53df6b5b88df7 {
|
|
142
|
+
constructor({ realm: realm, config: config, checkInterval: checkInterval = $41c352e9bd485a56$var$DEFAULT_CHECK_INTERVAL, onClose: onClose }){
|
|
143
|
+
this.timeoutId = null;
|
|
144
|
+
this.realm = realm;
|
|
145
|
+
this.config = config;
|
|
146
|
+
this.onClose = onClose;
|
|
147
|
+
this.checkInterval = checkInterval;
|
|
148
|
+
}
|
|
149
|
+
start() {
|
|
150
|
+
if (this.timeoutId) clearTimeout(this.timeoutId);
|
|
151
|
+
this.timeoutId = setTimeout(()=>{
|
|
152
|
+
this.checkConnections();
|
|
153
|
+
this.timeoutId = null;
|
|
154
|
+
this.start();
|
|
155
|
+
}, this.checkInterval);
|
|
156
|
+
}
|
|
157
|
+
stop() {
|
|
158
|
+
if (this.timeoutId) {
|
|
159
|
+
clearTimeout(this.timeoutId);
|
|
160
|
+
this.timeoutId = null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
checkConnections() {
|
|
164
|
+
const clientsIds = this.realm.getClientsIds();
|
|
165
|
+
const now = new Date().getTime();
|
|
166
|
+
const { alive_timeout: aliveTimeout } = this.config;
|
|
167
|
+
for (const clientId of clientsIds){
|
|
168
|
+
const client = this.realm.getClientById(clientId);
|
|
169
|
+
if (!client) continue;
|
|
170
|
+
const timeSinceLastPing = now - client.getLastPing();
|
|
171
|
+
if (timeSinceLastPing < aliveTimeout) continue;
|
|
172
|
+
try {
|
|
173
|
+
client.getSocket()?.close();
|
|
174
|
+
} finally{
|
|
175
|
+
this.realm.clearMessageQueue(clientId);
|
|
176
|
+
this.realm.removeClientById(clientId);
|
|
177
|
+
client.setSocket(null);
|
|
178
|
+
this.onClose?.(client);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
var $115ef0ff92fe1a17$export$b8e9cd941e8016ac = /*#__PURE__*/ function(Errors) {
|
|
186
|
+
Errors["INVALID_KEY"] = "Invalid key provided";
|
|
187
|
+
Errors["INVALID_TOKEN"] = "Invalid token provided";
|
|
188
|
+
Errors["INVALID_WS_PARAMETERS"] = "No id, token, or key supplied to websocket server";
|
|
189
|
+
Errors["CONNECTION_LIMIT_EXCEED"] = "Server has reached its concurrent user limit";
|
|
190
|
+
return Errors;
|
|
191
|
+
}({});
|
|
192
|
+
var $115ef0ff92fe1a17$export$80edbf15fa61a4db = /*#__PURE__*/ function(MessageType) {
|
|
193
|
+
MessageType["OPEN"] = "OPEN";
|
|
194
|
+
MessageType["LEAVE"] = "LEAVE";
|
|
195
|
+
MessageType["CANDIDATE"] = "CANDIDATE";
|
|
196
|
+
MessageType["OFFER"] = "OFFER";
|
|
197
|
+
MessageType["ANSWER"] = "ANSWER";
|
|
198
|
+
MessageType["EXPIRE"] = "EXPIRE";
|
|
199
|
+
MessageType["HEARTBEAT"] = "HEARTBEAT";
|
|
200
|
+
MessageType["ID_TAKEN"] = "ID-TAKEN";
|
|
201
|
+
MessageType["ERROR"] = "ERROR";
|
|
202
|
+
MessageType["PEER_JOINED_IP_GROUP"] = "PEER-JOINED-IP-GROUP";
|
|
203
|
+
MessageType["PEER_LEFT_IP_GROUP"] = "PEER-LEFT-IP-GROUP";
|
|
204
|
+
return MessageType;
|
|
205
|
+
}({});
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class $0684e92015b7a087$export$a13b411d0e88b1af {
|
|
209
|
+
constructor({ realm: realm, config: config, messageHandler: messageHandler }){
|
|
210
|
+
this.timeoutId = null;
|
|
211
|
+
this.realm = realm;
|
|
212
|
+
this.config = config;
|
|
213
|
+
this.messageHandler = messageHandler;
|
|
214
|
+
}
|
|
215
|
+
startMessagesExpiration() {
|
|
216
|
+
if (this.timeoutId) clearTimeout(this.timeoutId);
|
|
217
|
+
// Clean up outstanding messages
|
|
218
|
+
this.timeoutId = setTimeout(()=>{
|
|
219
|
+
this.pruneOutstanding();
|
|
220
|
+
this.timeoutId = null;
|
|
221
|
+
this.startMessagesExpiration();
|
|
222
|
+
}, this.config.cleanup_out_msgs);
|
|
223
|
+
}
|
|
224
|
+
stopMessagesExpiration() {
|
|
225
|
+
if (this.timeoutId) {
|
|
226
|
+
clearTimeout(this.timeoutId);
|
|
227
|
+
this.timeoutId = null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
pruneOutstanding() {
|
|
231
|
+
const destinationClientsIds = this.realm.getClientsIdsWithQueue();
|
|
232
|
+
const now = new Date().getTime();
|
|
233
|
+
const maxDiff = this.config.expire_timeout;
|
|
234
|
+
const seen = {};
|
|
235
|
+
for (const destinationClientId of destinationClientsIds){
|
|
236
|
+
const messageQueue = this.realm.getMessageQueueById(destinationClientId);
|
|
237
|
+
if (!messageQueue) continue;
|
|
238
|
+
const lastReadDiff = now - messageQueue.getLastReadAt();
|
|
239
|
+
if (lastReadDiff < maxDiff) continue;
|
|
240
|
+
const messages = messageQueue.getMessages();
|
|
241
|
+
for (const message of messages){
|
|
242
|
+
const seenKey = `${message.src}_${message.dst}`;
|
|
243
|
+
if (!seen[seenKey]) {
|
|
244
|
+
this.messageHandler.handle(undefined, {
|
|
245
|
+
type: (0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).EXPIRE,
|
|
246
|
+
src: message.dst,
|
|
247
|
+
dst: message.src
|
|
248
|
+
});
|
|
249
|
+
seen[seenKey] = true;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
this.realm.clearMessageQueue(destinationClientId);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class $af5fd0bf18c3ac6d$export$1f2bb630327ac4b6 {
|
|
261
|
+
constructor({ id: id, token: token }){
|
|
262
|
+
this.socket = null;
|
|
263
|
+
this.lastPing = new Date().getTime();
|
|
264
|
+
this.ipAddress = "";
|
|
265
|
+
this.alias = "";
|
|
266
|
+
this.id = id;
|
|
267
|
+
this.token = token;
|
|
268
|
+
}
|
|
269
|
+
getId() {
|
|
270
|
+
return this.id;
|
|
271
|
+
}
|
|
272
|
+
getToken() {
|
|
273
|
+
return this.token;
|
|
274
|
+
}
|
|
275
|
+
getSocket() {
|
|
276
|
+
return this.socket;
|
|
277
|
+
}
|
|
278
|
+
setSocket(socket) {
|
|
279
|
+
this.socket = socket;
|
|
280
|
+
}
|
|
281
|
+
getLastPing() {
|
|
282
|
+
return this.lastPing;
|
|
283
|
+
}
|
|
284
|
+
setLastPing(lastPing) {
|
|
285
|
+
this.lastPing = lastPing;
|
|
286
|
+
}
|
|
287
|
+
getIpAddress() {
|
|
288
|
+
return this.ipAddress;
|
|
289
|
+
}
|
|
290
|
+
setIpAddress(ip) {
|
|
291
|
+
this.ipAddress = ip;
|
|
292
|
+
}
|
|
293
|
+
getAlias() {
|
|
294
|
+
return this.alias;
|
|
295
|
+
}
|
|
296
|
+
setAlias(alias) {
|
|
297
|
+
this.alias = alias;
|
|
298
|
+
}
|
|
299
|
+
send(data) {
|
|
300
|
+
this.socket?.send(JSON.stringify(data));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
const $30473f63e8bdcc00$var$adjectives = [
|
|
307
|
+
"Bright",
|
|
308
|
+
"Great",
|
|
309
|
+
"Smart",
|
|
310
|
+
"Swift",
|
|
311
|
+
"Bold",
|
|
312
|
+
"Calm",
|
|
313
|
+
"Cool",
|
|
314
|
+
"Warm",
|
|
315
|
+
"Happy",
|
|
316
|
+
"Lucky",
|
|
317
|
+
"Noble",
|
|
318
|
+
"Proud",
|
|
319
|
+
"Quick",
|
|
320
|
+
"Witty",
|
|
321
|
+
"Brave",
|
|
322
|
+
"Cheerful",
|
|
323
|
+
"Daring",
|
|
324
|
+
"Elegant",
|
|
325
|
+
"Fancy",
|
|
326
|
+
"Gentle",
|
|
327
|
+
"Jolly",
|
|
328
|
+
"Keen",
|
|
329
|
+
"Lively",
|
|
330
|
+
"Merry",
|
|
331
|
+
"Polite",
|
|
332
|
+
"Shiny",
|
|
333
|
+
"Stellar",
|
|
334
|
+
"Talented",
|
|
335
|
+
"Vibrant",
|
|
336
|
+
"Wise",
|
|
337
|
+
// Funny additions
|
|
338
|
+
"Silly",
|
|
339
|
+
"Funky",
|
|
340
|
+
"Zany",
|
|
341
|
+
"Goofy",
|
|
342
|
+
"Wobbly",
|
|
343
|
+
"Glittery",
|
|
344
|
+
"Bouncy",
|
|
345
|
+
"Pickled",
|
|
346
|
+
"Sneaky",
|
|
347
|
+
"Nerdy",
|
|
348
|
+
"Fluffy",
|
|
349
|
+
"Spicy",
|
|
350
|
+
"Crispy",
|
|
351
|
+
"Snazzy",
|
|
352
|
+
"Quirky",
|
|
353
|
+
"Cheeky",
|
|
354
|
+
"Giggly",
|
|
355
|
+
"Breezy",
|
|
356
|
+
"Slippery"
|
|
357
|
+
];
|
|
358
|
+
const $30473f63e8bdcc00$var$nouns = [
|
|
359
|
+
"Banana",
|
|
360
|
+
"Cherry",
|
|
361
|
+
"Apple",
|
|
362
|
+
"Orange",
|
|
363
|
+
"Grape",
|
|
364
|
+
"Mango",
|
|
365
|
+
"Peach",
|
|
366
|
+
"Lemon",
|
|
367
|
+
"Melon",
|
|
368
|
+
"Berry",
|
|
369
|
+
"Pear",
|
|
370
|
+
"Plum",
|
|
371
|
+
"Kiwi",
|
|
372
|
+
"Lime",
|
|
373
|
+
"Papaya",
|
|
374
|
+
"Apricot",
|
|
375
|
+
"Fig",
|
|
376
|
+
"Coconut",
|
|
377
|
+
"Avocado",
|
|
378
|
+
"Dragon",
|
|
379
|
+
"Phoenix",
|
|
380
|
+
"Tiger",
|
|
381
|
+
"Eagle",
|
|
382
|
+
"Lion",
|
|
383
|
+
"Wolf",
|
|
384
|
+
"Bear",
|
|
385
|
+
"Hawk",
|
|
386
|
+
"Falcon",
|
|
387
|
+
"Panda",
|
|
388
|
+
// Funny additions
|
|
389
|
+
"Noodle",
|
|
390
|
+
"Unicorn",
|
|
391
|
+
"Tofu",
|
|
392
|
+
"Pickle",
|
|
393
|
+
"Marshmallow",
|
|
394
|
+
"Wombat",
|
|
395
|
+
"RubberDuck",
|
|
396
|
+
"Taco",
|
|
397
|
+
"Waffle",
|
|
398
|
+
"Bubble",
|
|
399
|
+
"Squid",
|
|
400
|
+
"Hippo",
|
|
401
|
+
"Muffin",
|
|
402
|
+
"Nacho",
|
|
403
|
+
"Ginger",
|
|
404
|
+
"Squirrel",
|
|
405
|
+
"Penguin",
|
|
406
|
+
"Turtle",
|
|
407
|
+
"Otter",
|
|
408
|
+
"Dumpling"
|
|
409
|
+
];
|
|
410
|
+
function $30473f63e8bdcc00$export$65b9416602f2db62() {
|
|
411
|
+
const adjective = $30473f63e8bdcc00$var$adjectives[Math.floor(Math.random() * $30473f63e8bdcc00$var$adjectives.length)];
|
|
412
|
+
const noun = $30473f63e8bdcc00$var$nouns[Math.floor(Math.random() * $30473f63e8bdcc00$var$nouns.length)];
|
|
413
|
+
return `${adjective} ${noun}`;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
const $e9ffe6de55430dbc$var$WS_PATH = "peerjs";
|
|
418
|
+
class $e9ffe6de55430dbc$export$f47674b57e51ee3b extends (0, $cl6Iw$nodeevents.EventEmitter) {
|
|
419
|
+
constructor({ server: server, realm: realm, config: config }){
|
|
420
|
+
super();
|
|
421
|
+
this.setMaxListeners(0);
|
|
422
|
+
this.realm = realm;
|
|
423
|
+
this.config = config;
|
|
424
|
+
const path = this.config.path;
|
|
425
|
+
this.path = `${path}${path.endsWith("/") ? "" : "/"}${$e9ffe6de55430dbc$var$WS_PATH}`;
|
|
426
|
+
const options = {
|
|
427
|
+
path: this.path,
|
|
428
|
+
server: server
|
|
429
|
+
};
|
|
430
|
+
this.socketServer = config.createWebSocketServer ? config.createWebSocketServer(options) : new (0, $cl6Iw$ws.WebSocketServer)(options);
|
|
431
|
+
this.socketServer.on("connection", (socket, req)=>{
|
|
432
|
+
this._onSocketConnection(socket, req);
|
|
433
|
+
});
|
|
434
|
+
this.socketServer.on("error", (error)=>{
|
|
435
|
+
this._onSocketError(error);
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
_onSocketConnection(socket, req) {
|
|
439
|
+
// An unhandled socket error might crash the server. Handle it first.
|
|
440
|
+
socket.on("error", (error)=>{
|
|
441
|
+
this._onSocketError(error);
|
|
442
|
+
});
|
|
443
|
+
// Extract IP address (handle proxied requests)
|
|
444
|
+
const forwardedFor = req.headers["x-forwarded-for"];
|
|
445
|
+
let ip = req.socket.remoteAddress ?? "unknown";
|
|
446
|
+
if (forwardedFor) {
|
|
447
|
+
const forwardedIp = Array.isArray(forwardedFor) ? forwardedFor[0] ?? "" : forwardedFor;
|
|
448
|
+
const firstIp = forwardedIp.split(",")[0];
|
|
449
|
+
if (firstIp) ip = firstIp.trim();
|
|
450
|
+
}
|
|
451
|
+
// We are only interested in the query, the base url is therefore not relevant
|
|
452
|
+
const { searchParams: searchParams } = new URL(req.url ?? "", "https://peerjs");
|
|
453
|
+
const { id: id, token: token, key: key } = Object.fromEntries(searchParams.entries());
|
|
454
|
+
if (!id || !token || !key) {
|
|
455
|
+
this._sendErrorAndClose(socket, (0, $115ef0ff92fe1a17$export$b8e9cd941e8016ac).INVALID_WS_PARAMETERS);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (key !== this.config.key) {
|
|
459
|
+
this._sendErrorAndClose(socket, (0, $115ef0ff92fe1a17$export$b8e9cd941e8016ac).INVALID_KEY);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const client = this.realm.getClientById(id);
|
|
463
|
+
if (client) {
|
|
464
|
+
if (token !== client.getToken()) {
|
|
465
|
+
// ID-taken, invalid token
|
|
466
|
+
socket.send(JSON.stringify({
|
|
467
|
+
type: (0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).ID_TAKEN,
|
|
468
|
+
payload: {
|
|
469
|
+
msg: "ID is taken"
|
|
470
|
+
}
|
|
471
|
+
}));
|
|
472
|
+
socket.close();
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
this._configureWS(socket, client);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
this._registerClient({
|
|
479
|
+
socket: socket,
|
|
480
|
+
id: id,
|
|
481
|
+
token: token,
|
|
482
|
+
ip: ip
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
_onSocketError(error) {
|
|
486
|
+
// handle error
|
|
487
|
+
this.emit("error", error);
|
|
488
|
+
}
|
|
489
|
+
_registerClient({ socket: socket, id: id, token: token, ip: ip }) {
|
|
490
|
+
// Check concurrent limit
|
|
491
|
+
const clientsCount = this.realm.getClientsIds().length;
|
|
492
|
+
if (clientsCount >= this.config.concurrent_limit) {
|
|
493
|
+
this._sendErrorAndClose(socket, (0, $115ef0ff92fe1a17$export$b8e9cd941e8016ac).CONNECTION_LIMIT_EXCEED);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const newClient = new (0, $af5fd0bf18c3ac6d$export$1f2bb630327ac4b6)({
|
|
497
|
+
id: id,
|
|
498
|
+
token: token
|
|
499
|
+
});
|
|
500
|
+
newClient.setIpAddress(ip);
|
|
501
|
+
newClient.setAlias((0, $30473f63e8bdcc00$export$65b9416602f2db62)());
|
|
502
|
+
this.realm.setClient(newClient, id);
|
|
503
|
+
socket.send(JSON.stringify({
|
|
504
|
+
type: (0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).OPEN
|
|
505
|
+
}));
|
|
506
|
+
this._configureWS(socket, newClient);
|
|
507
|
+
// Notify other clients in the same IP group about the new peer
|
|
508
|
+
this._notifyIpGroup(ip, newClient.getId(), {
|
|
509
|
+
type: (0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).PEER_JOINED_IP_GROUP,
|
|
510
|
+
payload: {
|
|
511
|
+
id: newClient.getId(),
|
|
512
|
+
alias: newClient.getAlias(),
|
|
513
|
+
ip: ip
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
_configureWS(socket, client) {
|
|
518
|
+
client.setSocket(socket);
|
|
519
|
+
// Cleanup after a socket closes.
|
|
520
|
+
socket.on("close", ()=>{
|
|
521
|
+
if (client.getSocket() === socket) {
|
|
522
|
+
const clientIp = client.getIpAddress();
|
|
523
|
+
const clientId = client.getId();
|
|
524
|
+
const clientAlias = client.getAlias();
|
|
525
|
+
this.realm.removeClientById(clientId);
|
|
526
|
+
// Notify other clients in the same IP group about the peer leaving
|
|
527
|
+
if (clientIp) this._notifyIpGroup(clientIp, clientId, {
|
|
528
|
+
type: (0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).PEER_LEFT_IP_GROUP,
|
|
529
|
+
payload: {
|
|
530
|
+
id: clientId,
|
|
531
|
+
alias: clientAlias,
|
|
532
|
+
ip: clientIp
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
this.emit("close", client);
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
// Handle messages from peers.
|
|
539
|
+
socket.on("message", (data)=>{
|
|
540
|
+
try {
|
|
541
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
542
|
+
const message = JSON.parse(data.toString());
|
|
543
|
+
message.src = client.getId();
|
|
544
|
+
this.emit("message", client, message);
|
|
545
|
+
} catch (e) {
|
|
546
|
+
this.emit("error", e);
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
this.emit("connection", client);
|
|
550
|
+
}
|
|
551
|
+
_sendErrorAndClose(socket, msg) {
|
|
552
|
+
socket.send(JSON.stringify({
|
|
553
|
+
type: (0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).ERROR,
|
|
554
|
+
payload: {
|
|
555
|
+
msg: msg
|
|
556
|
+
}
|
|
557
|
+
}));
|
|
558
|
+
socket.close();
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Notify all clients in the same IP group about a peer event
|
|
562
|
+
* @param ip - The IP address of the group
|
|
563
|
+
* @param excludeClientId - Client ID to exclude from notification (usually the one triggering the event)
|
|
564
|
+
* @param message - The message to send
|
|
565
|
+
*/ _notifyIpGroup(ip, excludeClientId, message) {
|
|
566
|
+
const ipGroup = this.realm.getClientsByIp(ip);
|
|
567
|
+
if (!ipGroup) return;
|
|
568
|
+
for (const [clientId, client] of ipGroup){
|
|
569
|
+
// Don't notify the client that triggered the event
|
|
570
|
+
if (clientId === excludeClientId) continue;
|
|
571
|
+
const socket = client.getSocket();
|
|
572
|
+
if (socket && socket.readyState === 1) // WebSocket.OPEN = 1
|
|
573
|
+
try {
|
|
574
|
+
socket.send(JSON.stringify(message));
|
|
575
|
+
} catch (e) {
|
|
576
|
+
// Ignore send errors for now
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
const $07b58d06a24ebcce$export$65302b915833a46d = (client)=>{
|
|
585
|
+
if (client) {
|
|
586
|
+
const nowTime = new Date().getTime();
|
|
587
|
+
client.setLastPing(nowTime);
|
|
588
|
+
}
|
|
589
|
+
return true;
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
const $ceefb4d5c5574ccb$export$809c011ea942310 = ({ realm: realm })=>{
|
|
595
|
+
const handle = (client, message)=>{
|
|
596
|
+
const type = message.type;
|
|
597
|
+
const srcId = message.src;
|
|
598
|
+
const dstId = message.dst;
|
|
599
|
+
const destinationClient = realm.getClientById(dstId);
|
|
600
|
+
// User is connected!
|
|
601
|
+
if (destinationClient) {
|
|
602
|
+
const socket = destinationClient.getSocket();
|
|
603
|
+
try {
|
|
604
|
+
if (socket) {
|
|
605
|
+
const data = JSON.stringify(message);
|
|
606
|
+
socket.send(data);
|
|
607
|
+
} else // Neither socket no res available. Peer dead?
|
|
608
|
+
throw new Error("Peer dead");
|
|
609
|
+
} catch (e) {
|
|
610
|
+
// This happens when a peer disconnects without closing connections and
|
|
611
|
+
// the associated WebSocket has not closed.
|
|
612
|
+
// Tell other side to stop trying.
|
|
613
|
+
if (socket) socket.close();
|
|
614
|
+
else realm.removeClientById(destinationClient.getId());
|
|
615
|
+
handle(client, {
|
|
616
|
+
type: (0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).LEAVE,
|
|
617
|
+
src: dstId,
|
|
618
|
+
dst: srcId
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
} else {
|
|
622
|
+
// Wait for this client to connect/reconnect (XHR) for important
|
|
623
|
+
// messages.
|
|
624
|
+
const ignoredTypes = [
|
|
625
|
+
(0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).LEAVE,
|
|
626
|
+
(0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).EXPIRE
|
|
627
|
+
];
|
|
628
|
+
if (!ignoredTypes.includes(type) && dstId) realm.addMessageToQueue(dstId, message);
|
|
629
|
+
else if (type === (0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).LEAVE && !dstId) realm.removeClientById(srcId);
|
|
630
|
+
}
|
|
631
|
+
return true;
|
|
632
|
+
};
|
|
633
|
+
return handle;
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
class $89578eeb272510da$export$cfe4a96645b0bbcf {
|
|
640
|
+
registerHandler(messageType, handler) {
|
|
641
|
+
if (this.handlers.has(messageType)) return;
|
|
642
|
+
this.handlers.set(messageType, handler);
|
|
643
|
+
}
|
|
644
|
+
handle(client, message) {
|
|
645
|
+
const { type: type } = message;
|
|
646
|
+
const handler = this.handlers.get(type);
|
|
647
|
+
if (!handler) return false;
|
|
648
|
+
return handler(client, message);
|
|
649
|
+
}
|
|
650
|
+
constructor(){
|
|
651
|
+
this.handlers = new Map();
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
class $379224fea64e1898$export$3deceafe0aaeaa95 {
|
|
657
|
+
constructor(realm, handlersRegistry = new (0, $89578eeb272510da$export$cfe4a96645b0bbcf)()){
|
|
658
|
+
this.handlersRegistry = handlersRegistry;
|
|
659
|
+
const transmissionHandler = (0, $ceefb4d5c5574ccb$export$809c011ea942310)({
|
|
660
|
+
realm: realm
|
|
661
|
+
});
|
|
662
|
+
const heartbeatHandler = (0, $07b58d06a24ebcce$export$65302b915833a46d);
|
|
663
|
+
const handleTransmission = (client, { type: type, src: src, dst: dst, payload: payload })=>{
|
|
664
|
+
return transmissionHandler(client, {
|
|
665
|
+
type: type,
|
|
666
|
+
src: src,
|
|
667
|
+
dst: dst,
|
|
668
|
+
payload: payload
|
|
669
|
+
});
|
|
670
|
+
};
|
|
671
|
+
const handleHeartbeat = (client, message)=>heartbeatHandler(client, message);
|
|
672
|
+
this.handlersRegistry.registerHandler((0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).HEARTBEAT, handleHeartbeat);
|
|
673
|
+
this.handlersRegistry.registerHandler((0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).OFFER, handleTransmission);
|
|
674
|
+
this.handlersRegistry.registerHandler((0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).ANSWER, handleTransmission);
|
|
675
|
+
this.handlersRegistry.registerHandler((0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).CANDIDATE, handleTransmission);
|
|
676
|
+
this.handlersRegistry.registerHandler((0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).LEAVE, handleTransmission);
|
|
677
|
+
this.handlersRegistry.registerHandler((0, $115ef0ff92fe1a17$export$80edbf15fa61a4db).EXPIRE, handleTransmission);
|
|
678
|
+
}
|
|
679
|
+
handle(client, message) {
|
|
680
|
+
return this.handlersRegistry.handle(client, message);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
var $9d461d6237a76c70$exports = {};
|
|
688
|
+
$9d461d6237a76c70$exports = JSON.parse("{\"name\":\"PeerJS Server\",\"description\":\"A server side element to broker connections between PeerJS clients.\",\"website\":\"https://peerjs.com/\"}");
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
var $20377580afc1f4e2$export$2e2bcd8739ae039 = ({ config: config, realm: realm })=>{
|
|
693
|
+
const app = (0, ($parcel$interopDefault($cl6Iw$express))).Router();
|
|
694
|
+
// Retrieve guaranteed random ID.
|
|
695
|
+
app.get("/id", (_, res)=>{
|
|
696
|
+
res.contentType("html");
|
|
697
|
+
res.send(realm.generateClientId(config.generateClientId));
|
|
698
|
+
});
|
|
699
|
+
// Get a list of all peers for a key, enabled by the `allowDiscovery` flag.
|
|
700
|
+
app.get("/peers", (_, res)=>{
|
|
701
|
+
if (config.allow_discovery) {
|
|
702
|
+
const clientsIds = realm.getClientsIds();
|
|
703
|
+
return res.send(clientsIds);
|
|
704
|
+
}
|
|
705
|
+
return res.sendStatus(401);
|
|
706
|
+
});
|
|
707
|
+
// Get all peers from the same IP as the requesting client
|
|
708
|
+
app.get("/by-ip", (req, res)=>{
|
|
709
|
+
if (config.allow_discovery) {
|
|
710
|
+
// Extract client's IP address from request
|
|
711
|
+
const forwardedFor = req.headers["x-forwarded-for"];
|
|
712
|
+
let clientIp = req.socket.remoteAddress ?? "unknown";
|
|
713
|
+
if (forwardedFor) {
|
|
714
|
+
const forwardedIp = Array.isArray(forwardedFor) ? forwardedFor[0] ?? "" : forwardedFor;
|
|
715
|
+
const firstIp = forwardedIp.split(",")[0];
|
|
716
|
+
if (firstIp) clientIp = firstIp.trim();
|
|
717
|
+
}
|
|
718
|
+
const ipGroup = realm.getClientsByIp(clientIp);
|
|
719
|
+
if (!ipGroup) return res.send([]);
|
|
720
|
+
const peers = Array.from(ipGroup.values()).map((client)=>({
|
|
721
|
+
id: client.getId(),
|
|
722
|
+
alias: client.getAlias()
|
|
723
|
+
}));
|
|
724
|
+
return res.send(peers);
|
|
725
|
+
}
|
|
726
|
+
return res.sendStatus(401);
|
|
727
|
+
});
|
|
728
|
+
// Get all IP groups (admin/debugging endpoint)
|
|
729
|
+
app.get("/groups", (_, res)=>{
|
|
730
|
+
if (config.allow_discovery) {
|
|
731
|
+
const result = {};
|
|
732
|
+
const ipGroups = Array.from(realm.getAllIpGroups().entries());
|
|
733
|
+
for (const [ip, clients] of ipGroups)result[ip] = Array.from(clients.values()).map((client)=>({
|
|
734
|
+
id: client.getId(),
|
|
735
|
+
alias: client.getAlias()
|
|
736
|
+
}));
|
|
737
|
+
return res.send(result);
|
|
738
|
+
}
|
|
739
|
+
return res.sendStatus(401);
|
|
740
|
+
});
|
|
741
|
+
return app;
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
const $6519c7a56ba95525$export$bf71da7aebe9ddc1 = ({ config: config, realm: realm, corsOptions: corsOptions })=>{
|
|
746
|
+
const app = (0, ($parcel$interopDefault($cl6Iw$express))).Router();
|
|
747
|
+
app.use((0, ($parcel$interopDefault($cl6Iw$cors)))(corsOptions));
|
|
748
|
+
app.get("/", (_, res)=>{
|
|
749
|
+
res.send((0, (/*@__PURE__*/$parcel$interopDefault($9d461d6237a76c70$exports))));
|
|
750
|
+
});
|
|
751
|
+
app.use("/:key", (0, $20377580afc1f4e2$export$2e2bcd8739ae039)({
|
|
752
|
+
config: config,
|
|
753
|
+
realm: realm
|
|
754
|
+
}));
|
|
755
|
+
return app;
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
const $516a786dc008ff17$export$99152e8d49ca4e7d = ({ app: app, server: server, options: options })=>{
|
|
760
|
+
const config = options;
|
|
761
|
+
const realm = new (0, $7a434f2b8c14dece$export$3ee29d34e33d9116)();
|
|
762
|
+
const messageHandler = new (0, $379224fea64e1898$export$3deceafe0aaeaa95)(realm);
|
|
763
|
+
const api = (0, $6519c7a56ba95525$export$bf71da7aebe9ddc1)({
|
|
764
|
+
config: config,
|
|
765
|
+
realm: realm,
|
|
766
|
+
corsOptions: options.corsOptions
|
|
767
|
+
});
|
|
768
|
+
const messagesExpire = new (0, $0684e92015b7a087$export$a13b411d0e88b1af)({
|
|
769
|
+
realm: realm,
|
|
770
|
+
config: config,
|
|
771
|
+
messageHandler: messageHandler
|
|
772
|
+
});
|
|
773
|
+
const checkBrokenConnections = new (0, $41c352e9bd485a56$export$6fa53df6b5b88df7)({
|
|
774
|
+
realm: realm,
|
|
775
|
+
config: config,
|
|
776
|
+
onClose: (client)=>{
|
|
777
|
+
app.emit("disconnect", client);
|
|
778
|
+
}
|
|
779
|
+
});
|
|
780
|
+
app.use(options.path, api);
|
|
781
|
+
//use mountpath for WS server
|
|
782
|
+
const customConfig = {
|
|
783
|
+
...config,
|
|
784
|
+
path: (0, ($parcel$interopDefault($cl6Iw$nodepath))).posix.join(app.path(), options.path, "/")
|
|
785
|
+
};
|
|
786
|
+
const wss = new (0, $e9ffe6de55430dbc$export$f47674b57e51ee3b)({
|
|
787
|
+
server: server,
|
|
788
|
+
realm: realm,
|
|
789
|
+
config: customConfig
|
|
790
|
+
});
|
|
791
|
+
wss.on("connection", (client)=>{
|
|
792
|
+
const messageQueue = realm.getMessageQueueById(client.getId());
|
|
793
|
+
if (messageQueue) {
|
|
794
|
+
let message;
|
|
795
|
+
while(message = messageQueue.readMessage())messageHandler.handle(client, message);
|
|
796
|
+
realm.clearMessageQueue(client.getId());
|
|
797
|
+
}
|
|
798
|
+
app.emit("connection", client);
|
|
799
|
+
});
|
|
800
|
+
wss.on("message", (client, message)=>{
|
|
801
|
+
app.emit("message", client, message);
|
|
802
|
+
messageHandler.handle(client, message);
|
|
803
|
+
});
|
|
804
|
+
wss.on("close", (client)=>{
|
|
805
|
+
app.emit("disconnect", client);
|
|
806
|
+
});
|
|
807
|
+
wss.on("error", (error)=>{
|
|
808
|
+
app.emit("error", error);
|
|
809
|
+
});
|
|
810
|
+
messagesExpire.startMessagesExpiration();
|
|
811
|
+
checkBrokenConnections.start();
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
function $123f4982a51872b4$export$8c57434a18c696c9(server, options) {
|
|
816
|
+
const app = (0, ($parcel$interopDefault($cl6Iw$express)))();
|
|
817
|
+
const newOptions = {
|
|
818
|
+
...(0, $629a620fdd8149f9$export$2e2bcd8739ae039),
|
|
819
|
+
...options
|
|
820
|
+
};
|
|
821
|
+
if (newOptions.proxied) app.set("trust proxy", newOptions.proxied === "false" ? false : !!newOptions.proxied);
|
|
822
|
+
app.on("mount", ()=>{
|
|
823
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
824
|
+
if (!server) throw new Error("Server is not passed to constructor - can't start PeerServer");
|
|
825
|
+
(0, $516a786dc008ff17$export$99152e8d49ca4e7d)({
|
|
826
|
+
app: app,
|
|
827
|
+
server: server,
|
|
828
|
+
options: newOptions
|
|
829
|
+
});
|
|
830
|
+
});
|
|
831
|
+
return app;
|
|
832
|
+
}
|
|
833
|
+
function $123f4982a51872b4$export$f99d31af51f48b1e(options = {}, callback) {
|
|
834
|
+
const app = (0, ($parcel$interopDefault($cl6Iw$express)))();
|
|
835
|
+
let newOptions = {
|
|
836
|
+
...(0, $629a620fdd8149f9$export$2e2bcd8739ae039),
|
|
837
|
+
...options
|
|
838
|
+
};
|
|
839
|
+
const port = newOptions.port;
|
|
840
|
+
const host = newOptions.host;
|
|
841
|
+
let server;
|
|
842
|
+
const { ssl: ssl, ...restOptions } = newOptions;
|
|
843
|
+
if (ssl && Object.keys(ssl).length) {
|
|
844
|
+
server = (0, ($parcel$interopDefault($cl6Iw$nodehttps))).createServer(ssl, app);
|
|
845
|
+
newOptions = restOptions;
|
|
846
|
+
} else server = (0, ($parcel$interopDefault($cl6Iw$nodehttp))).createServer(app);
|
|
847
|
+
// Increase max listeners to prevent warning when multiple services attach listeners
|
|
848
|
+
server.setMaxListeners(0);
|
|
849
|
+
const peerjs = $123f4982a51872b4$export$8c57434a18c696c9(server, newOptions);
|
|
850
|
+
app.use(peerjs);
|
|
851
|
+
server.listen(port, host, ()=>callback?.(server));
|
|
852
|
+
return peerjs;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
//# sourceMappingURL=index.cjs.map
|