@hapi/nes 14.0.1 → 14.0.2
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.md +0 -0
- package/README.md +0 -0
- package/lib/client.js +0 -0
- package/lib/index.js +0 -0
- package/lib/listener.js +70 -17
- package/lib/socket.js +6 -1
- package/package.json +8 -5
package/LICENSE.md
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
File without changes
|
package/lib/client.js
CHANGED
|
File without changes
|
package/lib/index.js
CHANGED
|
File without changes
|
package/lib/listener.js
CHANGED
|
@@ -441,26 +441,37 @@ internals.Sockets.prototype.add = function (socket) {
|
|
|
441
441
|
};
|
|
442
442
|
|
|
443
443
|
|
|
444
|
-
internals.Sockets.prototype.auth = function (socket) {
|
|
444
|
+
internals.Sockets.prototype.auth = function (socket, previous, credentials) {
|
|
445
|
+
|
|
446
|
+
// Called before the socket credentials are replaced
|
|
447
|
+
|
|
448
|
+
if (!this._listener._settings.auth.index ||
|
|
449
|
+
!this._items[socket.id]) { // Closed while authenticating
|
|
445
450
|
|
|
446
|
-
if (!this._listener._settings.auth.index) {
|
|
447
451
|
return;
|
|
448
452
|
}
|
|
449
453
|
|
|
450
|
-
|
|
451
|
-
|
|
454
|
+
const user = credentials.user;
|
|
455
|
+
if (user === previous) {
|
|
456
|
+
return; // Same user as before, index unchanged
|
|
452
457
|
}
|
|
453
458
|
|
|
454
|
-
|
|
455
|
-
|
|
459
|
+
if (user &&
|
|
460
|
+
this._listener._settings.auth.maxConnectionsPerUser &&
|
|
456
461
|
this._byUser[user] &&
|
|
457
462
|
this._byUser[user].length >= this._listener._settings.auth.maxConnectionsPerUser) {
|
|
458
463
|
|
|
459
464
|
throw Boom.serverUnavailable('Too many connections for the authenticated user');
|
|
460
465
|
}
|
|
461
466
|
|
|
462
|
-
|
|
463
|
-
|
|
467
|
+
if (previous) {
|
|
468
|
+
this._deindex(socket, previous);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (user) {
|
|
472
|
+
this._byUser[user] = this._byUser[user] ?? [];
|
|
473
|
+
this._byUser[user].push(socket);
|
|
474
|
+
}
|
|
464
475
|
};
|
|
465
476
|
|
|
466
477
|
|
|
@@ -471,12 +482,17 @@ internals.Sockets.prototype.remove = function (socket) {
|
|
|
471
482
|
if (socket.auth.credentials &&
|
|
472
483
|
socket.auth.credentials.user) {
|
|
473
484
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
485
|
+
this._deindex(socket, socket.auth.credentials.user);
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
internals.Sockets.prototype._deindex = function (socket, user) {
|
|
491
|
+
|
|
492
|
+
if (this._byUser[user]) {
|
|
493
|
+
this._byUser[user] = this._byUser[user].filter((item) => item !== socket);
|
|
494
|
+
if (!this._byUser[user].length) {
|
|
495
|
+
delete this._byUser[user];
|
|
480
496
|
}
|
|
481
497
|
}
|
|
482
498
|
};
|
|
@@ -582,15 +598,52 @@ internals.Subscribers.prototype._remove = async function (socket, path, params)
|
|
|
582
598
|
};
|
|
583
599
|
|
|
584
600
|
|
|
601
|
+
internals.Subscribers.prototype.reauth = function (socket, previous) {
|
|
602
|
+
|
|
603
|
+
// Called after the socket credentials are replaced
|
|
604
|
+
|
|
605
|
+
if (!this._settings.auth ||
|
|
606
|
+
!this._settings.auth.index) {
|
|
607
|
+
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const user = socket.auth.credentials.user;
|
|
612
|
+
if (user === previous) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
const item = this._items[socket.id];
|
|
617
|
+
if (!item) {
|
|
618
|
+
return; // Removed while the socket was closing
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (previous) {
|
|
622
|
+
this._deindex(item, previous);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
if (user) {
|
|
626
|
+
this._byUser[user] = this._byUser[user] ?? [];
|
|
627
|
+
this._byUser[user].push(item);
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
|
|
585
632
|
internals.Subscribers.prototype._cleanup = function (socket, item) {
|
|
586
633
|
|
|
587
634
|
delete this._items[socket.id];
|
|
588
635
|
|
|
589
636
|
if (socket.auth.credentials &&
|
|
590
|
-
socket.auth.credentials.user
|
|
591
|
-
|
|
637
|
+
socket.auth.credentials.user) {
|
|
638
|
+
|
|
639
|
+
this._deindex(item, socket.auth.credentials.user);
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
internals.Subscribers.prototype._deindex = function (item, user) {
|
|
592
645
|
|
|
593
|
-
|
|
646
|
+
if (this._byUser[user]) {
|
|
594
647
|
this._byUser[user] = this._byUser[user].filter((record) => record !== item);
|
|
595
648
|
if (!this._byUser[user].length) {
|
|
596
649
|
delete this._byUser[user];
|
package/lib/socket.js
CHANGED
|
@@ -598,6 +598,9 @@ internals.Socket.prototype._authByCookie = async function () {
|
|
|
598
598
|
|
|
599
599
|
internals.Socket.prototype._setCredentials = function (auth) {
|
|
600
600
|
|
|
601
|
+
const previous = this.auth.credentials ? this.auth.credentials.user : undefined;
|
|
602
|
+
this._listener._sockets.auth(this, previous, auth.credentials); // Throws before any state changes when the user limit is reached
|
|
603
|
+
|
|
601
604
|
this.auth.isAuthenticated = true;
|
|
602
605
|
this.auth.credentials = auth.credentials;
|
|
603
606
|
this.auth.artifacts = auth.artifacts;
|
|
@@ -605,7 +608,9 @@ internals.Socket.prototype._setCredentials = function (auth) {
|
|
|
605
608
|
|
|
606
609
|
this.auth._verified = Date.now();
|
|
607
610
|
|
|
608
|
-
|
|
611
|
+
for (const subscribers of new Set(Object.values(this._subscriptions))) { // Paths on the same route share one subscribers object
|
|
612
|
+
subscribers.reauth(this, previous);
|
|
613
|
+
}
|
|
609
614
|
};
|
|
610
615
|
|
|
611
616
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hapi/nes",
|
|
3
3
|
"description": "WebSocket adapter plugin for hapi routes",
|
|
4
|
-
"version": "14.0.
|
|
5
|
-
"repository":
|
|
4
|
+
"version": "14.0.2",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git://github.com/hapijs/nes"
|
|
8
|
+
},
|
|
6
9
|
"main": "lib/index.js",
|
|
7
10
|
"types": "lib/index.d.ts",
|
|
8
11
|
"engines": {
|
|
@@ -37,9 +40,9 @@
|
|
|
37
40
|
"joi": "^17.13.3",
|
|
38
41
|
"typescript": "^5.4.5"
|
|
39
42
|
},
|
|
43
|
+
"license": "BSD-3-Clause",
|
|
40
44
|
"scripts": {
|
|
41
45
|
"test": "lab -a @hapi/code -t 100 -L -m 10000 -Y",
|
|
42
46
|
"test-cov-html": "lab -a @hapi/code -r html -o coverage.html -m 10000"
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
}
|
|
47
|
+
}
|
|
48
|
+
}
|