Skip to content

Commit

Permalink
✨ Return Socket.io server / namespace on attach
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumolari committed Jan 22, 2021
1 parent fd028df commit 5e7c451
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('IO', () => {
it('should add socket.io to Koa app', () => {
const app = new Koa();
const socket = new IO();
socket.attach(app);
expect(socket.attach(app)).toEqual(expect.any(SocketIOServer));

expect(((app as unknown) as EnhancedKoa).io).toEqual(expect.any(IO));
expect(((app as unknown) as EnhancedKoa).server).toBeTruthy();
Expand All @@ -148,7 +148,7 @@ describe('IO', () => {
const app = new Koa();
const socket = new IO();
((app as unknown) as EnhancedKoa).server = createServer();
socket.attach(app);
expect(socket.attach(app)).toEqual(expect.any(SocketIOServer));

expect(((app as unknown) as EnhancedKoa).io).toEqual(expect.any(IO));

Expand All @@ -170,8 +170,8 @@ describe('IO', () => {
const socket = new IO();
const chat = new IO('chat');

socket.attach(app);
chat.attach(app);
expect(socket.attach(app)).toEqual(expect.any(SocketIOServer));
expect(chat.attach(app)).toHaveProperty('constructor.name', 'Namespace');

expect(((app as unknown) as { chat: unknown }).chat).toEqual(chat);
});
Expand All @@ -180,7 +180,7 @@ describe('IO', () => {
const app = new Koa();
const chat = new IO('chat');

chat.attach(app);
expect(chat.attach(app)).toHaveProperty('constructor.name', 'Namespace');

expect(((app as unknown) as EnhancedKoa)._io).toEqual(expect.any(SocketIOServer));
expect(((app as unknown) as { chat: unknown }).chat).toEqual(chat);
Expand All @@ -194,14 +194,14 @@ describe('IO', () => {
const io = new SocketIOServer(server);
((app as unknown) as EnhancedKoa)._io = io;

chat.attach(app);
expect(chat.attach(app)).toHaveProperty('constructor.name', 'Namespace');
});

it('should allow attaching a namespace should be done via an options object', () => {
const app = new Koa();
const chat = new IO({ namespace: 'chat' });

chat.attach(app);
expect(chat.attach(app)).toHaveProperty('constructor.name', 'Namespace');

expect(((app as unknown) as { chat: unknown }).chat).toEqual(chat);
});
Expand All @@ -211,7 +211,7 @@ describe('IO', () => {
const app = new Koa();
const chat = new IO({ namespace: 'chat', hidden: true });

chat.attach(app);
expect(chat.attach(app)).toHaveProperty('constructor.name', 'Namespace');

const server = ((app as unknown) as EnhancedKoa).server?.listen();
const address = server.address() as AddressInfo;
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('IO', () => {
const app = new Koa();
const socket = new IO();

socket.attach(app);
expect(socket.attach(app)).toEqual(expect.any(SocketIOServer));

const spy = jest.fn();
((app as unknown) as EnhancedKoa).server.listen = spy;
Expand Down
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class IO<
app: Koa<StateT, ContextT>,
https = false,
opts: Parameters<typeof createHttpsServer>[0] = {},
): void {
): SocketIOServer | Namespace {
const enhancedApp = (app as unknown) as EnhancedKoaInstance<StateT, ContextT>;

this.createServerIfNeeded(app, https, opts);
Expand All @@ -197,17 +197,15 @@ export class IO<
throw new Error('Socket failed to initialise::Instance may already exist');
}

this.attachNamespace(enhancedApp, this.opts.namespace);
return;
return this.attachNamespace(enhancedApp, this.opts.namespace);
}

this.ensureDefaultNamespaceIsNotHidden();

enhancedApp._io = new SocketIOServer(enhancedApp.server, this.opts.ioOptions);

if (this.opts.namespace) {
this.attachNamespace(enhancedApp, this.opts.namespace);
return;
return this.attachNamespace(enhancedApp, this.opts.namespace);
}

// Local aliases / passthrough socket.io functionality
Expand All @@ -219,30 +217,35 @@ export class IO<
// If there is no namespace then connect using the default
this.socket = enhancedApp._io;
this.socket.on('connection', this.onConnection);

return enhancedApp._io;
}

/**
* Attaches the namespace to the server
* @param app the koa app to use
* @param id namespace identifier
*/
attachNamespace(app: EnhancedKoaInstance<StateT, ContextT>, id: string): void {
attachNamespace(app: EnhancedKoaInstance<StateT, ContextT>, id: string): Namespace {
if (!app._io) {
throw new Error('Namespaces can only be attached once a socketIO instance has been attached');
}

this.socket = app._io.of(id);
const namespace = app._io.of(id);
this.socket = namespace;
this.socket.on('connection', this.onConnection);

if (this.opts.hidden) {
return;
return namespace;
}

if (app[id]) {
throw new Error('Namespace ' + id + ' already attached to koa instance');
}

app[id] = this;

return namespace;
}

/**
Expand Down

0 comments on commit 5e7c451

Please sign in to comment.