Skip to content

Commit

Permalink
feat: Log explanatory warning if origins option is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Jul 7, 2021
1 parent 2319f65 commit 4c00e03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import type { StorageAPI } from '../types';

const game = { seed: 0 };

const warn = jest.spyOn(console, 'warn').mockImplementation(() => {});
beforeEach(warn.mockReset);
afterAll(warn.mockRestore);

jest.mock('../core/logger', () => ({
info: () => {},
error: () => {},
Expand Down Expand Up @@ -80,6 +84,18 @@ describe('new', () => {
const server = Server({ games: [game], authenticateCredentials });
expect(server.db).not.toBeNull();
});

test('logs a warning if origins not set', () => {
Server({ games: [{}] });
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('Server `origins` option is not set.')
);
});

test('does not log a warning if origins set', () => {
Server({ games: [{}], origins: [] });
expect(warn).not.toHaveBeenCalled();
});
});

describe('run', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ export function Server({
if (transport === undefined) {
transport = new SocketIO({ https });
}
if (origins === undefined) {
console.warn(
'Server `origins` option is not set.\n' +
'Since boardgame.io@0.45, CORS is not enabled by default and you must ' +
'explicitly set the origins that are allowed to connect to the server.\n' +
'See https://boardgame.io/documentation/#/api/Server'
);
}
transport.init(app, games, origins);

const router = createRouter({ db, games, uuid, auth });
Expand Down

0 comments on commit 4c00e03

Please sign in to comment.