Skip to content

Commit dc668ec

Browse files
c-wdelucis
andauthored
feat(server): Expose SocketIO transport & convert to TS (#658)
* Enable passing through options to socket.io * Expose SocketIO in server package * Remove test-only arguments from public interface Co-authored-by: Chris Swithinbank <[email protected]>
1 parent 814621b commit dc668ec

8 files changed

Lines changed: 190 additions & 142 deletions

File tree

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"@types/koa-router": "^7.4.0",
8888
"@types/koa__cors": "^3.0.1",
8989
"@types/shortid": "0.0.29",
90+
"@types/socket.io": "^2.1.4",
9091
"ajv": "^6.6.2",
9192
"babel-core": "^7.0.0-bridge.0",
9293
"babel-eslint": "^8.0.0",

packages/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
import { Server } from '../src/server';
1010
import { FlatFile } from '../src/server/db';
11+
import { SocketIO } from '../src/server/transport/socketio';
1112

12-
export { Server, FlatFile };
13+
export { Server, FlatFile, SocketIO };

src/master/master.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const stripCredentialsFromAction = (action: CredentialedActionShape.Any) => {
112112
return { ...action, payload };
113113
};
114114

115-
type AuthFn = (
115+
export type AuthFn = (
116116
actionCredentials: string,
117117
playerMetadata: Server.PlayerMetadata
118118
) => boolean | Promise<boolean>;

src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function Server({
9999
typeof authenticateCredentials === 'function'
100100
? authenticateCredentials
101101
: true;
102-
transport = SocketIO({
102+
transport = new SocketIO({
103103
auth,
104104
https,
105105
});

src/server/transport/socketio.js

Lines changed: 0 additions & 131 deletions
This file was deleted.
Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@
99
import { TransportAPI, SocketIO } from './socketio';
1010
import { ProcessGameConfig } from '../../core/game';
1111

12+
class SocketIOTestAdapter extends SocketIO {
13+
constructor({ clientInfo = new Map(), roomInfo = new Map(), ...args } = {}) {
14+
super(args);
15+
this.clientInfo = clientInfo;
16+
this.roomInfo = roomInfo;
17+
}
18+
}
19+
1220
jest.mock('../../master/master', () => {
1321
class Master {
22+
onUpdate: jest.Mock<any, any>;
23+
onSync: jest.Mock<any, any>;
24+
1425
constructor() {
1526
this.onUpdate = jest.fn();
1627
this.onSync = jest.fn();
@@ -22,6 +33,11 @@ jest.mock('../../master/master', () => {
2233

2334
jest.mock('koa-socket-2', () => {
2435
class MockSocket {
36+
id: string;
37+
callbacks: {};
38+
emit: jest.Mock<any, any>;
39+
broadcast: { emit: jest.Mock<any, any> };
40+
2541
constructor() {
2642
this.id = 'id';
2743
this.callbacks = {};
@@ -49,15 +65,20 @@ jest.mock('koa-socket-2', () => {
4965
}
5066

5167
class MockIO {
68+
socket: MockSocket;
69+
5270
constructor() {
5371
this.socket = new MockSocket();
5472
}
73+
5574
attach(app) {
5675
app.io = app._io = this;
5776
}
77+
5878
of() {
5979
return this;
6080
}
81+
6182
on(type, callback) {
6283
callback(this.socket);
6384
}
@@ -67,15 +88,15 @@ jest.mock('koa-socket-2', () => {
6788
});
6889

6990
describe('basic', () => {
70-
const app = { context: {} };
91+
const app: any = { context: {} };
7192
const games = [ProcessGameConfig({ seed: 0 })];
7293
let clientInfo;
7394
let roomInfo;
7495

7596
beforeEach(() => {
7697
clientInfo = new Map();
7798
roomInfo = new Map();
78-
const transport = SocketIO({ clientInfo, roomInfo });
99+
const transport = new SocketIOTestAdapter({ clientInfo, roomInfo });
79100
transport.init(app, games);
80101
});
81102

@@ -89,11 +110,11 @@ describe('TransportAPI', () => {
89110
let api;
90111

91112
beforeAll(() => {
92-
const app = { context: {} };
113+
const app: any = { context: {} };
93114
const games = [ProcessGameConfig({ seed: 0 })];
94115
const clientInfo = new Map();
95116
const roomInfo = new Map();
96-
const transport = SocketIO({ clientInfo, roomInfo });
117+
const transport = new SocketIOTestAdapter({ clientInfo, roomInfo });
97118
transport.init(app, games);
98119
io = app.context.io;
99120
api = TransportAPI('gameID', io.socket, clientInfo, roomInfo);
@@ -132,9 +153,9 @@ describe('TransportAPI', () => {
132153
});
133154

134155
describe('sync / update', () => {
135-
const app = { context: {} };
156+
const app: any = { context: {} };
136157
const games = [ProcessGameConfig({ seed: 0 })];
137-
const transport = SocketIO();
158+
const transport = new SocketIOTestAdapter();
138159
transport.init(app, games);
139160
const io = app.context.io;
140161

@@ -148,7 +169,7 @@ describe('sync / update', () => {
148169
});
149170

150171
describe('connect / disconnect', () => {
151-
const app = { context: {} };
172+
const app: any = { context: {} };
152173
const games = [ProcessGameConfig({ seed: 0 })];
153174
let clientInfo;
154175
let roomInfo;
@@ -165,7 +186,7 @@ describe('connect / disconnect', () => {
165186
beforeAll(() => {
166187
clientInfo = new Map();
167188
roomInfo = new Map();
168-
const transport = SocketIO({ clientInfo, roomInfo });
189+
const transport = new SocketIOTestAdapter({ clientInfo, roomInfo });
169190
transport.init(app, games);
170191
io = app.context.io;
171192
});

0 commit comments

Comments
 (0)