Skip to content

Commit 9e368d6

Browse files
authored
feat: register middlewares without namespace on all namespaces (#919)
1 parent 133dd02 commit 9e368d6

File tree

2 files changed

+83
-25
lines changed

2 files changed

+83
-25
lines changed

src/SocketControllers.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,24 @@ export class SocketControllers {
9292
});
9393

9494
const middlewaresWithoutNamespace = middlewares.filter(middleware => !middleware.metadata.namespace);
95-
const middlewaresWithNamespace = middlewares.filter(middleware => !!middleware.metadata.namespace);
9695

9796
for (const middleware of middlewaresWithoutNamespace) {
9897
this.registerMiddleware(this.io as unknown as Namespace, middleware);
9998
}
10099

101100
this.io.on('new_namespace', (namespace: Namespace) => {
102-
for (const middleware of middlewaresWithNamespace) {
101+
for (const middleware of middlewares) {
103102
const middlewareNamespaces = Array.isArray(middleware.metadata.namespace)
104103
? middleware.metadata.namespace
105104
: [middleware.metadata.namespace];
106105

107106
const shouldApply = middlewareNamespaces.some(nsp => {
108-
const nspRegexp = nsp instanceof RegExp ? nsp : pathToRegexp(nsp as string);
107+
// Register middlewares without namespace too
108+
if (nsp == null) {
109+
return true;
110+
}
111+
112+
const nspRegexp = nsp instanceof RegExp ? nsp : pathToRegexp(nsp);
109113
return nspRegexp.test(namespace.name);
110114
});
111115

test/functional/middlewares.spec.ts

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Middlewares', () => {
1717
let httpServer: HttpServer;
1818
let wsApp: Server;
1919
let wsClient: Socket;
20-
let testResult;
20+
let testResult: string[] = [];
2121
let socketControllers: SocketControllers;
2222

2323
beforeEach(done => {
@@ -33,7 +33,7 @@ describe('Middlewares', () => {
3333
});
3434

3535
afterEach(() => {
36-
testResult = undefined;
36+
testResult = [];
3737

3838
Container.reset();
3939
wsClient.close();
@@ -53,7 +53,7 @@ describe('Middlewares', () => {
5353
@Service()
5454
class GlobalMiddleware implements MiddlewareInterface {
5555
use(socket: any, next: (err?: any) => any): any {
56-
testResult = 'global middleware';
56+
testResult.push('global middleware');
5757
next();
5858
}
5959
}
@@ -76,7 +76,7 @@ describe('Middlewares', () => {
7676
wsClient = io(PATH_FOR_CLIENT, { reconnection: false, timeout: 5000, forceNew: true });
7777

7878
await waitForEvent(wsClient, 'connected');
79-
expect(testResult).toEqual('global middleware');
79+
expect(testResult).toEqual(['global middleware']);
8080
});
8181

8282
describe('string namespace', () => {
@@ -85,7 +85,16 @@ describe('Middlewares', () => {
8585
@Service()
8686
class StringNamespaceMiddleware implements MiddlewareInterface {
8787
use(socket: any, next: (err?: any) => any): any {
88-
testResult = 'string middleware';
88+
testResult.push('string middleware');
89+
next();
90+
}
91+
}
92+
93+
@Middleware()
94+
@Service()
95+
class MiddlewareWithoutNamespace implements MiddlewareInterface {
96+
use(socket: any, next: (err?: any) => any): any {
97+
testResult.push('middleware without namespace');
8998
next();
9099
}
91100
}
@@ -102,21 +111,30 @@ describe('Middlewares', () => {
102111
socketControllers = new SocketControllers({
103112
io: wsApp,
104113
container: Container,
105-
middlewares: [StringNamespaceMiddleware],
114+
middlewares: [StringNamespaceMiddleware, MiddlewareWithoutNamespace],
106115
controllers: [StringNamespaceController],
107116
});
108117
wsClient = io(PATH_FOR_CLIENT + '/string', { reconnection: false, timeout: 5000, forceNew: true });
109118

110119
await waitForEvent(wsClient, 'connected');
111-
expect(testResult).toEqual('string middleware');
120+
expect(testResult).toEqual(['string middleware', 'middleware without namespace']);
112121
});
113122

114123
it('incorrect namespace', async () => {
115124
@Middleware({ namespace: '/string' })
116125
@Service()
117126
class StringNamespaceMiddleware implements MiddlewareInterface {
118127
use(socket: any, next: (err?: any) => any): any {
119-
testResult = 'string middleware';
128+
testResult.push('string middleware');
129+
next();
130+
}
131+
}
132+
133+
@Middleware()
134+
@Service()
135+
class MiddlewareWithoutNamespace implements MiddlewareInterface {
136+
use(socket: any, next: (err?: any) => any): any {
137+
testResult.push('middleware without namespace');
120138
next();
121139
}
122140
}
@@ -133,13 +151,13 @@ describe('Middlewares', () => {
133151
socketControllers = new SocketControllers({
134152
io: wsApp,
135153
container: Container,
136-
middlewares: [StringNamespaceMiddleware],
154+
middlewares: [StringNamespaceMiddleware, MiddlewareWithoutNamespace],
137155
controllers: [String2NamespaceController],
138156
});
139157
wsClient = io(PATH_FOR_CLIENT + '/string2', { reconnection: false, timeout: 5000, forceNew: true });
140158

141159
await waitForEvent(wsClient, 'connected');
142-
expect(testResult).toEqual(undefined);
160+
expect(testResult).toEqual(['middleware without namespace']);
143161
});
144162
});
145163

@@ -149,7 +167,16 @@ describe('Middlewares', () => {
149167
@Service()
150168
class RegexpNamespaceMiddleware implements MiddlewareInterface {
151169
use(socket: any, next: (err?: any) => any): any {
152-
testResult = socket.nsp.name;
170+
testResult.push(socket.nsp.name as string);
171+
next();
172+
}
173+
}
174+
175+
@Middleware()
176+
@Service()
177+
class MiddlewareWithoutNamespace implements MiddlewareInterface {
178+
use(socket: any, next: (err?: any) => any): any {
179+
testResult.push('middleware without namespace');
153180
next();
154181
}
155182
}
@@ -166,21 +193,30 @@ describe('Middlewares', () => {
166193
socketControllers = new SocketControllers({
167194
io: wsApp,
168195
container: Container,
169-
middlewares: [RegexpNamespaceMiddleware],
196+
middlewares: [RegexpNamespaceMiddleware, MiddlewareWithoutNamespace],
170197
controllers: [RegexpNamespaceController],
171198
});
172199
wsClient = io(PATH_FOR_CLIENT + '/dynamic-1', { reconnection: false, timeout: 5000, forceNew: true });
173200

174201
await waitForEvent(wsClient, 'connected');
175-
expect(testResult).toEqual('/dynamic-1');
202+
expect(testResult).toEqual(['/dynamic-1', 'middleware without namespace']);
176203
});
177204

178205
it('incorrect namespace', async () => {
179206
@Middleware({ namespace: /^\/dynamic-\s+$/ })
180207
@Service()
181208
class RegexpNamespaceMiddleware implements MiddlewareInterface {
182209
use(socket: any, next: (err?: any) => any): any {
183-
testResult = socket.nsp.name;
210+
testResult.push(socket.nsp.name as string);
211+
next();
212+
}
213+
}
214+
215+
@Middleware()
216+
@Service()
217+
class MiddlewareWithoutNamespace implements MiddlewareInterface {
218+
use(socket: any, next: (err?: any) => any): any {
219+
testResult.push('middleware without namespace');
184220
next();
185221
}
186222
}
@@ -197,13 +233,13 @@ describe('Middlewares', () => {
197233
socketControllers = new SocketControllers({
198234
io: wsApp,
199235
container: Container,
200-
middlewares: [RegexpNamespaceMiddleware],
236+
middlewares: [RegexpNamespaceMiddleware, MiddlewareWithoutNamespace],
201237
controllers: [RegexpNamespaceController],
202238
});
203239
wsClient = io(PATH_FOR_CLIENT + '/dynamic-1', { reconnection: false, timeout: 5000, forceNew: true });
204240

205241
await waitForEvent(wsClient, 'connected');
206-
expect(testResult).toEqual(undefined);
242+
expect(testResult).toEqual(['middleware without namespace']);
207243
});
208244
});
209245

@@ -213,7 +249,16 @@ describe('Middlewares', () => {
213249
@Service()
214250
class RegexpArrayNamespaceMiddleware implements MiddlewareInterface {
215251
use(socket: any, next: (err?: any) => any): any {
216-
testResult = socket.nsp.name;
252+
testResult.push(socket.nsp.name as string);
253+
next();
254+
}
255+
}
256+
257+
@Middleware()
258+
@Service()
259+
class MiddlewareWithoutNamespace implements MiddlewareInterface {
260+
use(socket: any, next: (err?: any) => any): any {
261+
testResult.push('middleware without namespace');
217262
next();
218263
}
219264
}
@@ -230,21 +275,30 @@ describe('Middlewares', () => {
230275
socketControllers = new SocketControllers({
231276
io: wsApp,
232277
container: Container,
233-
middlewares: [RegexpArrayNamespaceMiddleware],
278+
middlewares: [RegexpArrayNamespaceMiddleware, MiddlewareWithoutNamespace],
234279
controllers: [RegexpNamespaceController],
235280
});
236281
wsClient = io(PATH_FOR_CLIENT + '/dynamic-1', { reconnection: false, timeout: 5000, forceNew: true });
237282

238283
await waitForEvent(wsClient, 'connected');
239-
expect(testResult).toEqual('/dynamic-1');
284+
expect(testResult).toEqual(['/dynamic-1', 'middleware without namespace']);
240285
});
241286

242287
it('incorrect namespace', async () => {
243288
@Middleware({ namespace: [/^\/dynamic-\s+$/] })
244289
@Service()
245290
class RegexpArrayNamespaceMiddleware implements MiddlewareInterface {
246291
use(socket: any, next: (err?: any) => any): any {
247-
testResult = socket.nsp.name;
292+
testResult.push(socket.nsp.name as string);
293+
next();
294+
}
295+
}
296+
297+
@Middleware()
298+
@Service()
299+
class MiddlewareWithoutNamespace implements MiddlewareInterface {
300+
use(socket: any, next: (err?: any) => any): any {
301+
testResult.push('middleware without namespace');
248302
next();
249303
}
250304
}
@@ -261,13 +315,13 @@ describe('Middlewares', () => {
261315
socketControllers = new SocketControllers({
262316
io: wsApp,
263317
container: Container,
264-
middlewares: [RegexpArrayNamespaceMiddleware],
318+
middlewares: [RegexpArrayNamespaceMiddleware, MiddlewareWithoutNamespace],
265319
controllers: [RegexpNamespaceController],
266320
});
267321
wsClient = io(PATH_FOR_CLIENT + '/dynamic-1', { reconnection: false, timeout: 5000, forceNew: true });
268322

269323
await waitForEvent(wsClient, 'connected');
270-
expect(testResult).toEqual(undefined);
324+
expect(testResult).toEqual(['middleware without namespace']);
271325
});
272326
});
273327
});

0 commit comments

Comments
 (0)