From 2eb7f9cfb198cd083bf53adcc6984360123a89cb Mon Sep 17 00:00:00 2001 From: Eden Tyler-Moss Date: Fri, 31 Jan 2020 10:11:49 +0000 Subject: [PATCH] Generate ID function passed in options object. Updated tests. --- src/api/index.js | 4 ++-- src/api/v1/public/index.js | 6 ++++-- src/index.js | 12 ++++++------ src/models/realm.js | 14 +++++++------- test/models/realm.js | 1 + 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/api/index.js b/src/api/index.js index e1adce1..86f3a07 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -3,7 +3,7 @@ const cors = require('cors'); const bodyParser = require('body-parser'); const publicContent = require('../../app.json'); -module.exports = ({ config, realm, messageHandler, randomId }) => { +module.exports = ({ config, realm, messageHandler }) => { const authMiddleware = require('./middleware/auth')({ config, realm }); const app = express.Router(); @@ -16,7 +16,7 @@ module.exports = ({ config, realm, messageHandler, randomId }) => { res.send(publicContent); }); - app.use('/:key', require('./v1/public')({ config, realm, randomId })); + app.use('/:key', require('./v1/public')({ config, realm })); app.use('/:key/:id/:token', authMiddleware, jsonParser, require('./v1/calls')({ realm, messageHandler })); return app; diff --git a/src/api/v1/public/index.js b/src/api/v1/public/index.js index 78473e1..ba49de9 100644 --- a/src/api/v1/public/index.js +++ b/src/api/v1/public/index.js @@ -1,12 +1,14 @@ const express = require('express'); -module.exports = ({ config, realm, randomId }) => { +module.exports = ({ config, realm }) => { const app = express.Router(); // Retrieve guaranteed random ID. app.get('/id', (req, res) => { res.contentType = 'text/html'; - res.send(realm.generateClientId(randomId)); + res.send(realm.generateClientId( + config.genRandomId ? config.genRandomId : undefined + )); }); // Get a list of all peers for a key, enabled by the `allowDiscovery` flag. diff --git a/src/index.js b/src/index.js index 0ec5087..ea3d9e0 100644 --- a/src/index.js +++ b/src/index.js @@ -6,11 +6,11 @@ const defaultConfig = require('../config'); const WebSocketServer = require('./services/webSocketServer'); const Realm = require('./models/realm'); -const init = ({ app, server, options, randomId }) => { +const init = ({ app, server, options }) => { const config = options; const realm = new Realm(); const messageHandler = require('./messageHandler')({ realm }); - const api = require('./api')({ config, realm, messageHandler, randomId }); + const api = require('./api')({ config, realm, messageHandler }); const { startMessagesExpiration } = require('./services/messagesExpire')({ realm, config, messageHandler }); const checkBrokenConnections = require('./services/checkBrokenConnections')({ @@ -62,7 +62,7 @@ const init = ({ app, server, options, randomId }) => { checkBrokenConnections.start(); }; -function ExpressPeerServer(server, options, randomId) { +function ExpressPeerServer(server, options) { const app = express(); options = { @@ -80,13 +80,13 @@ function ExpressPeerServer(server, options, randomId) { 'can\'t start PeerServer'); } - init({ app, server, options, randomId }); + init({ app, server, options }); }); return app; } -function PeerServer(options = {}, callback, randomId) { +function PeerServer(options = {}, callback) { const app = express(); options = { @@ -114,7 +114,7 @@ function PeerServer(options = {}, callback, randomId) { server = http.createServer(app); } - const peerjs = ExpressPeerServer(server, options, randomId); + const peerjs = ExpressPeerServer(server, options); app.use(peerjs); if (callback) { diff --git a/src/models/realm.js b/src/models/realm.js index e9221ce..cd84ea3 100644 --- a/src/models/realm.js +++ b/src/models/realm.js @@ -42,19 +42,19 @@ class Realm { this._messageQueues.delete(id); } - generateClientId (_randomId) { - const originalRandomId = () => { + generateClientId (_genRandomId) { + const originalGenRandomId = () => { return (Math.random().toString(36) + '0000000000000000000').substr(2, 16); } - const randomId = typeof _randomId === 'function' ? - () => _randomId : - () => originalRandomId; + const genRandomId = _genRandomId && typeof _genRandomId === 'function' ? + () => _genRandomId : + () => originalGenRandomId; - let clientId = randomId(randomId)(); + let clientId = genRandomId(); while (this.getClientById(clientId)) { - clientId = randomId(); + clientId = genRandomId(); } return clientId; diff --git a/test/models/realm.js b/test/models/realm.js index 16e0bd8..69e06d4 100644 --- a/test/models/realm.js +++ b/test/models/realm.js @@ -7,6 +7,7 @@ describe('Realm', () => { it('should generate a 16-character ID', () => { const realm = new Realm(); expect(realm.generateClientId().length).to.eq(16); + expect(realm.generateClientId(() => 'abcd').to.eq('abcd'); }); });