Skip to content

Commit e241fd0

Browse files
Revert "[feat] Add support for dynamic namespaces (socketio#3187)"
This reverts commit c0c79f0.
1 parent ad0c052 commit e241fd0

File tree

4 files changed

+5
-166
lines changed

4 files changed

+5
-166
lines changed

docs/API.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
- [server.onconnection(socket)](#serveronconnectionsocket)
2121
- [server.of(nsp)](#serverofnsp)
2222
- [server.close([callback])](#serverclosecallback)
23-
- [server.useNamespaceValidator(fn)](#serverusenamespacevalidatorfn)
2423
- [Class: Namespace](#namespace)
2524
- [namespace.name](#namespacename)
2625
- [namespace.connected](#namespaceconnected)
@@ -322,22 +321,6 @@ server.listen(PORT); // PORT is free to use
322321
io = Server(server);
323322
```
324323

325-
#### server.useNamespaceValidator(fn)
326-
327-
- `fn` _(Function)_
328-
329-
Sets up server middleware to validate whether a new namespace should be created.
330-
331-
```js
332-
io.useNamespaceValidator((nsp, next) => {
333-
if (nsp === 'dynamic') {
334-
next(null, true);
335-
} else {
336-
next(new Error('Invalid namespace'));
337-
}
338-
});
339-
```
340-
341324
#### server.engine.generateId
342325

343326
Overwrites the default method to generate your custom socket id.

lib/client.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,17 @@ Client.prototype.setup = function(){
5656
* Connects a client to a namespace.
5757
*
5858
* @param {String} name namespace
59-
* @param {String} query the query parameters
6059
* @api private
6160
*/
6261

6362
Client.prototype.connect = function(name, query){
64-
if (this.server.nsps[name]) {
65-
debug('connecting to namespace %s', name);
66-
return this.doConnect(name, query);
63+
debug('connecting to namespace %s', name);
64+
var nsp = this.server.nsps[name];
65+
if (!nsp) {
66+
this.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'});
67+
return;
6768
}
6869

69-
this.server.checkNamespace(name, (allow) => {
70-
if (allow) {
71-
debug('creating namespace %s', name);
72-
this.doConnect(name, query);
73-
} else {
74-
debug('creation of namespace %s was denied', name);
75-
this.packet({ type: parser.ERROR, nsp: name, data: 'Invalid namespace' });
76-
}
77-
});
78-
};
79-
80-
/**
81-
* Connects a client to a namespace.
82-
*
83-
* @param {String} name namespace
84-
* @param {String} query the query parameters
85-
* @api private
86-
*/
87-
88-
Client.prototype.doConnect = function(name, query){
89-
var nsp = this.server.of(name);
90-
9170
if ('/' != name && !this.nsps['/']) {
9271
this.connectBuffer.push(name);
9372
return;

lib/index.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ function Server(srv, opts){
4646
}
4747
opts = opts || {};
4848
this.nsps = {};
49-
this.nspValidators = [];
5049
this.path(opts.path || '/socket.io');
5150
this.serveClient(false !== opts.serveClient);
5251
this.parser = opts.parser || parser;
@@ -160,53 +159,6 @@ Server.prototype.set = function(key, val){
160159
return this;
161160
};
162161

163-
/**
164-
* Sets up server middleware to validate incoming namespaces not already created on the server.
165-
*
166-
* @return {Server} self
167-
* @api public
168-
*/
169-
170-
Server.prototype.useNamespaceValidator = function(fn){
171-
this.nspValidators.push(fn);
172-
return this;
173-
};
174-
175-
/**
176-
* Executes the middleware for an incoming namespace not already created on the server.
177-
*
178-
* @param name of incomming namespace
179-
* @param {Function} last fn call in the middleware
180-
* @api private
181-
*/
182-
183-
Server.prototype.checkNamespace = function(name, fn){
184-
var fns = this.nspValidators.slice(0);
185-
if (!fns.length) return fn(false);
186-
187-
var namespaceAllowed = false; // Deny unknown namespaces by default
188-
189-
function run(i){
190-
fns[i](name, function(err, allow){
191-
// upon error, short-circuit
192-
if (err) return fn(false);
193-
194-
// if one piece of middleware explicitly denies namespace, short-circuit
195-
if (allow === false) return fn(false);
196-
197-
namespaceAllowed = namespaceAllowed || allow === true;
198-
199-
// if no middleware left, summon callback
200-
if (!fns[i + 1]) return fn(namespaceAllowed);
201-
202-
// go on to next
203-
run(i + 1);
204-
});
205-
}
206-
207-
run(0);
208-
};
209-
210162
/**
211163
* Sets the client serving path.
212164
*

test/socket.io.js

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -889,81 +889,6 @@ describe('socket.io', function(){
889889
});
890890
});
891891
});
892-
893-
describe('dynamic', function () {
894-
it('should allow connections to dynamic namespaces', function(done){
895-
var srv = http();
896-
var sio = io(srv);
897-
srv.listen(function(){
898-
var namespace = '/dynamic';
899-
var dynamic = client(srv, namespace);
900-
sio.useNamespaceValidator(function(nsp, next) {
901-
expect(nsp).to.be(namespace);
902-
next(null, true);
903-
});
904-
dynamic.on('error', function(err) {
905-
expect().fail();
906-
});
907-
dynamic.on('connect', function() {
908-
expect(sio.nsps[namespace]).to.be.a(Namespace);
909-
expect(Object.keys(sio.nsps[namespace].sockets).length).to.be(1);
910-
done();
911-
});
912-
});
913-
});
914-
915-
it('should not allow connections to dynamic namespaces if not supported', function(done){
916-
var srv = http();
917-
var sio = io(srv);
918-
srv.listen(function(){
919-
var namespace = '/dynamic';
920-
sio.useNamespaceValidator(function(nsp, next) {
921-
expect(nsp).to.be(namespace);
922-
next(null, false);
923-
});
924-
sio.on('connect', function(socket) {
925-
if (socket.nsp.name === namespace) {
926-
expect().fail();
927-
}
928-
});
929-
930-
var dynamic = client(srv,namespace);
931-
dynamic.on('connect', function(){
932-
expect().fail();
933-
});
934-
dynamic.on('error', function(err) {
935-
expect(err).to.be("Invalid namespace");
936-
done();
937-
});
938-
});
939-
});
940-
941-
it('should not allow connections to dynamic namespaces if there is an error', function(done){
942-
var srv = http();
943-
var sio = io(srv);
944-
srv.listen(function(){
945-
var namespace = '/dynamic';
946-
sio.useNamespaceValidator(function(nsp, next) {
947-
expect(nsp).to.be(namespace);
948-
next(new Error(), true);
949-
});
950-
sio.on('connect', function(socket) {
951-
if (socket.nsp.name === namespace) {
952-
expect().fail();
953-
}
954-
});
955-
956-
var dynamic = client(srv,namespace);
957-
dynamic.on('connect', function(){
958-
expect().fail();
959-
});
960-
dynamic.on('error', function(err) {
961-
expect(err).to.be("Invalid namespace");
962-
done();
963-
});
964-
});
965-
});
966-
});
967892
});
968893

969894
describe('socket', function(){

0 commit comments

Comments
 (0)