From dbf9f71454c8faa3fcff67614a893db9994bad8f Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 1 Jun 2018 12:02:04 +0200 Subject: [PATCH] lint; server code lint --- .eslintrc.json | 2 + app/main/callbacks.js | 81 ++++++------ app/main/electron-app.js | 23 ++-- app/main/electron-window.js | 36 +++--- app/main/ipc/client.js | 12 +- app/main/ipc/server.js | 2 +- app/main/main.js | 2 +- app/main/midi.js | 92 ++++++++------ app/main/osc/index.js | 247 ++++++++++++++++++------------------ app/main/osc/tcp.js | 21 ++- app/main/osc/udp.js | 20 +-- app/main/server.js | 26 ++-- app/main/settings.js | 141 ++++++++++---------- app/main/theme.js | 4 +- 14 files changed, 357 insertions(+), 352 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index d2898798..5a9fe439 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -12,7 +12,9 @@ } }, "rules": { + "no-console": "off", "no-undef": "off", + "no-useless-escape": "warn", "no-empty": [ "error", { diff --git a/app/main/callbacks.js b/app/main/callbacks.js index 84402716..42cb42c0 100644 --- a/app/main/callbacks.js +++ b/app/main/callbacks.js @@ -1,9 +1,8 @@ -var vm = require('vm'), - path = require('path'), +var path = require('path'), fs = require('fs'), settings = require('./settings'), osc = require('./osc'), - {ipc, clients} = require('./server'), + {ipc} = require('./server'), theme = require('./theme'), chokidar = require('chokidar') @@ -67,7 +66,7 @@ module.exports = { sessionOpen: function(data,clientId) { - var file = data.file || (function(){try {return fs.readFileSync(data.path,'utf8')} catch(err) {return false}})(), + var file = data.file || (function(){try {return fs.readFileSync(data.path,'utf8')} catch(err) {return false}})(), error = file === false && data.path ? 'Session file "' + data.path + '" not found.' : false, session @@ -128,7 +127,7 @@ module.exports = { } else { - ipc.send('error', `Invalid session file (${error})`) + ipc.send('error', `Invalid session file (${error})`) } }, @@ -155,7 +154,7 @@ module.exports = { data[k] = shortdata[k] } - data.args = data.preArgs ? data.preArgs.concat(value) : [value] + data.args = data.preArgs ? data.preArgs.concat(value) : [value] if (!data.noSync) ipc.send('receiveOsc', data, null, clientId) @@ -164,69 +163,69 @@ module.exports = { sendOsc: function(shortdata, clientId) { - if (!(widgetHashTable[clientId] && widgetHashTable[clientId][shortdata.h])) return + if (!(widgetHashTable[clientId] && widgetHashTable[clientId][shortdata.h])) return - var value = shortdata.v, - data = widgetHashTable[clientId][shortdata.h] + var value = shortdata.v, + data = widgetHashTable[clientId][shortdata.h] - data = JSON.parse(JSON.stringify(data)) - for (var k in shortdata) { - data[k] = shortdata[k] - } + data = JSON.parse(JSON.stringify(data)) + for (var k in shortdata) { + data[k] = shortdata[k] + } - data.args = data.preArgs ? data.preArgs.concat(value) : [value] + data.args = data.preArgs ? data.preArgs.concat(value) : [value] - if (data.target) { + if (data.target) { - var targets = [] + var targets = [] - if (data.target.indexOf(null) === -1 && settings.read('targets') && !shortdata.target) Array.prototype.push.apply(targets, settings.read('targets')) - if (data.target) Array.prototype.push.apply(targets, data.target) + if (data.target.indexOf(null) === -1 && settings.read('targets') && !shortdata.target) Array.prototype.push.apply(targets, settings.read('targets')) + if (data.target) Array.prototype.push.apply(targets, data.target) - for (var i in targets) { + for (var i in targets) { - if (targets[i] === 'self') { - ipc.send('receiveOsc',data,clientId) - continue - } else if (targets[i] === null) { - continue - } + if (targets[i] === 'self') { + ipc.send('receiveOsc',data,clientId) + continue + } else if (targets[i] === null) { + continue + } - var host = targets[i].split(':')[0], - port = targets[i].split(':')[1] + var host = targets[i].split(':')[0], + port = targets[i].split(':')[1] - if (port) { + if (port) { - if (data.split) { - - for (var j in data.split) { - osc.send(host,port,data.split[j],data.args[j],data.precision) - } - - } else { - - osc.send(host,port,data.address,data.args,data.precision) + if (data.split) { + for (var j in data.split) { + osc.send(host,port,data.split[j],data.args[j],data.precision) } + } else { + + osc.send(host,port,data.address,data.args,data.precision) + } } } - if (!data.noSync) ipc.send('receiveOsc', data, null, clientId) + } + + if (!data.noSync) ipc.send('receiveOsc', data, null, clientId) }, addWidget(data, clientId) { if (!widgetHashTable[clientId]) { - widgetHashTable[clientId] = {} + widgetHashTable[clientId] = {} } if (!widgetHashTable[clientId][data.hash]) { - widgetHashTable[clientId][data.hash] = {} + widgetHashTable[clientId][data.hash] = {} } var cache = widgetHashTable[clientId][data.hash], @@ -263,7 +262,7 @@ module.exports = { settings.write('presetPath',file) if (file.indexOf('.preset')==-1){file+='.preset'} - fs.writeFile(file,data, function (err, data) { + fs.writeFile(file,data, function(err, data) { if (err) throw err console.log('The current state was saved in '+file) }) diff --git a/app/main/electron-app.js b/app/main/electron-app.js index 757df71e..62040e62 100644 --- a/app/main/electron-app.js +++ b/app/main/electron-app.js @@ -1,27 +1,26 @@ -var {app, Menu, shell, dialog} = require('electron'), +var {app, Menu, shell} = require('electron'), settings = require('./settings'), - path = require('path'), infos = require('../package.json') app.commandLine.appendSwitch('--touch-events') -if (settings.read('noVsync') || (!settings.cli && settings.read('argv')['disable-vsync'])) { +if (settings.read('noVsync') || (!settings.cli && settings.read('argv')['disable-vsync'])) { app.commandLine.appendSwitch('--disable-gpu-vsync') } -if (settings.read('noGpu') || (!settings.cli && settings.read('argv')['disable-gpu'])) { +if (settings.read('noGpu') || (!settings.cli && settings.read('argv')['disable-gpu'])) { app.disableHardwareAcceleration() } var template = [{ label: 'Edit', submenu: [ - {role: 'undo', accelerator: "CmdOrCtrl+Z"}, - {role: 'redo', accelerator: "Shift+CmdOrCtrl+Z"}, + {role: 'undo', accelerator: 'CmdOrCtrl+Z'}, + {role: 'redo', accelerator: 'Shift+CmdOrCtrl+Z'}, {type: 'separator'}, - {role: 'cut', accelerator: "CmdOrCtrl+X"}, - {role: 'copy', accelerator: "CmdOrCtrl+C"}, - {role: 'paste', accelerator: "CmdOrCtrl+V"} + {role: 'cut', accelerator: 'CmdOrCtrl+X'}, + {role: 'copy', accelerator: 'CmdOrCtrl+C'}, + {role: 'paste', accelerator: 'CmdOrCtrl+V'} ] }] @@ -50,7 +49,7 @@ if (process.platform === 'darwin') { { label: 'Quit', accelerator: 'Command+Q', - click: () => app.quit() + click: ()=>app.quit() } ] }) @@ -79,14 +78,14 @@ if (process.platform === 'darwin') { submenu: [ { label: 'Documentation', - click: () => shell.openExternal(infos.homepage) + click: ()=>shell.openExternal(infos.homepage) }, { type: 'separator' }, { label: 'Report an Issue', - click: () => shell.openExternal(infos.repository.url + '/issues') + click: ()=>shell.openExternal(infos.repository.url + '/issues') } ] } diff --git a/app/main/electron-window.js b/app/main/electron-window.js index 9db29978..8aa8af41 100644 --- a/app/main/electron-window.js +++ b/app/main/electron-window.js @@ -1,5 +1,5 @@ var path = require('path'), - {BrowserWindow, Menu, dialog} = require('electron'), + {BrowserWindow, dialog} = require('electron'), shortcut = require('electron-localshortcut'), settings = require('./settings'), theme = require('./theme') @@ -10,8 +10,8 @@ module.exports = function(options={}) { var window window = new BrowserWindow({ - width: options.width || 800, - height: options.height || 600, + width: options.width || 800, + height: options.height || 600, title: options.title || settings.read('appName'), icon: path.resolve(__dirname + '/../browser/logo.png'), backgroundColor: options.color || theme.backgroundColor, @@ -24,24 +24,24 @@ module.exports = function(options={}) { } }) - window.webContents.on('will-prevent-unload', (event) => { - var choice = dialog.showMessageBox(window, { - type: 'question', - buttons: ['Yes', 'No'], - title: 'Are you sure ?', - message: 'Unsaved data will be lost. Are you sure you want to quit?', - defaultId: 0, - cancelId: 1 - }) - if (choice === 0) { - event.preventDefault() - } + window.webContents.on('will-prevent-unload', (event)=>{ + var choice = dialog.showMessageBox(window, { + type: 'question', + buttons: ['Yes', 'No'], + title: 'Are you sure ?', + message: 'Unsaved data will be lost. Are you sure you want to quit?', + defaultId: 0, + cancelId: 1 + }) + if (choice === 0) { + event.preventDefault() + } }) - window.webContents.on('will-navigate', (event) => event.preventDefault()) - window.webContents.on('new-window', (event) => event.preventDefault()) + window.webContents.on('will-navigate', (event)=>event.preventDefault()) + window.webContents.on('new-window', (event)=>event.preventDefault()) - if (options.fullscreen) { + if (options.fullscreen) { window.webContents.on('dom-ready', ()=>{ window.webContents.executeJavaScript(` window.ELECTRON_FULLSCREEN = true diff --git a/app/main/ipc/client.js b/app/main/ipc/client.js index aa08546d..5bb8adb3 100644 --- a/app/main/ipc/client.js +++ b/app/main/ipc/client.js @@ -6,7 +6,7 @@ var hearbeatInterval = 25000, class Socket extends EventEmitter { - constructor(ws, id) { + constructor(ws, id) { super() @@ -21,7 +21,7 @@ class Socket extends EventEmitter { this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) }) this.on('ping', ()=>{ - this.socket.send(`["pong"]`) + this.socket.send('["pong"]') }) this.dieTimeout = undefined @@ -30,7 +30,7 @@ class Socket extends EventEmitter { } - open(ws) { + open(ws) { this.socket = ws @@ -46,7 +46,7 @@ class Socket extends EventEmitter { this.hearbeat = setInterval(()=>{ if (!this.connected()) return - this.socket.send(`["ping"]`) + this.socket.send('["ping"]') this.hearbeatTimeout = setTimeout(()=>{ if (this.connected()) this.socket.close() }, hearbeatTimeout) @@ -68,7 +68,7 @@ class Socket extends EventEmitter { } - connected() { + connected() { return this.socket.readyState == this.socket.OPEN @@ -80,7 +80,7 @@ class Socket extends EventEmitter { var packet = JSON.parse(message) - if (Array.isArray(packet) && typeof packet[0] == "string") { + if (Array.isArray(packet) && typeof packet[0] == 'string') { this.emit(packet[0], packet[1]) diff --git a/app/main/ipc/server.js b/app/main/ipc/server.js index 1fee1bea..cb2ec945 100644 --- a/app/main/ipc/server.js +++ b/app/main/ipc/server.js @@ -13,7 +13,7 @@ class Ipc extends EventEmitter { this.server = new WebSocketServer({server: server}) - this.server.on('connection', (socket, req) => { + this.server.on('connection', (socket, req)=>{ var id = req.url.split('/').pop() diff --git a/app/main/main.js b/app/main/main.js index a9f33752..f7f76358 100644 --- a/app/main/main.js +++ b/app/main/main.js @@ -70,7 +70,7 @@ if (settings.cli) { launcher app.on('ready',function(){ - launcher = require('./electron-window')({address:address, shortcuts:dev, width:680, height:(100 + 8*3 + 29 * Object.keys(settings.options).filter(x => settings.options[x].launcher !== false).length), node:true, color:'#253040'}) + launcher = require('./electron-window')({address:address, shortcuts:dev, width:680, height:(100 + 8*3 + 29 * Object.keys(settings.options).filter(x=>settings.options[x].launcher !== false).length), node:true, color:'#253040'}) }) if (process.log) { diff --git a/app/main/midi.js b/app/main/midi.js index 0386b3dc..d30fbf50 100644 --- a/app/main/midi.js +++ b/app/main/midi.js @@ -1,54 +1,64 @@ var PythonShell = require('python-shell'), - path = require('path'), settings = require('./settings') -var py = new PythonShell('midi.py', {scriptPath:__dirname, mode:'text', args: [ - settings.read('debug') ? 'debug' : '', - ...settings.read('midi') - ] -}) +class Midi { + constructor() { + + this.py = new PythonShell('midi.py', { + scriptPath:__dirname, + mode:'text', + args: [ + settings.read('debug') ? 'debug' : '', + ...settings.read('midi') + ] + }) -oscToMidi = (data)=>{ - var args = [] - for (i in data.args) { - args.push(data.args[i].value) } - py.send(JSON.stringify([data.port, data.address, ...args])) -} -stop = ()=>{ - py.childProcess.kill() -} + send(data) { -init = (receiveOsc)=>{ - py.on('message', function (message) { - // console.log(message) - var name, data - try { - [name, data] = JSON.parse(message) - } catch (err) { - // console.log(err) + var args = [] + for (i in data.args) { + args.push(data.args[i].value) } - if (name == 'log') { - if (data.indexOf('ERROR') === 0) { - console.error(data) - } else { - console.log(data) + + this.py.send(JSON.stringify([data.port, data.address, ...args])) + + } + + stop() { + + this.py.childProcess.kill() + + } + + init(receiveOsc) { + this.py.on('message', function(message) { + // console.log(message) + var name, data + try { + [name, data] = JSON.parse(message) + } catch (err) { + // console.log(err) } - } else if (name == 'osc') { - receiveOsc(data) - } else if (name == 'error') { - console.error('ERROR: MIDI: ' + data) - stop() - } - }) + if (name == 'log') { + if (data.indexOf('ERROR') === 0) { + console.error(data) + } else { + console.log(data) + } + } else if (name == 'osc') { + receiveOsc(data) + } else if (name == 'error') { + console.error('ERROR: MIDI: ' + data) + this.stop() + } + }) + } + } +var midi = new Midi() - -module.exports = { - send: oscToMidi, - init: init, - stop: stop -} +module.exports = midi diff --git a/app/main/osc/index.js b/app/main/osc/index.js index 73622628..44ccdfd7 100644 --- a/app/main/osc/index.js +++ b/app/main/osc/index.js @@ -2,99 +2,96 @@ // (electron-rebuild breaks it for node-only mode) require('vmodule')('serialport', {}, {global: true}) -var osc = require('osc'), - ipc = require('../server').ipc, - settings = require('../settings'), - zeroconf = require('../zeroconf'), - oscInPort = settings.read('oscInPort') || settings.read('httpPort'), - tcpInPort = settings.read('tcpInPort'), - debug = settings.read('debug'), - vm = require('vm'), - fs = require('fs'), +var ipc = require('../server').ipc, + settings = require('../settings'), + tcpInPort = settings.read('tcpInPort'), + debug = settings.read('debug'), + vm = require('vm'), + fs = require('fs'), midi = settings.read('midi') ? require('../midi') : false, - oscUDPServer = require('./udp'), - oscTCPServer = require('./tcp') + oscUDPServer = require('./udp'), + oscTCPServer = require('./tcp') class OscServer { constructor(){ - this.customModule = (()=>{ + this.customModule = (()=>{ - if (!settings.read('customModule')) return false + if (!settings.read('customModule')) return false - var file = (()=>{ - try { - return fs.readFileSync(settings.read('customModule'),'utf8') - } catch(err) { - console.log('CustomModule Error: File not found: ' + settings.read('customModule')) - return false - } - })(), - mod, - context = { - console, - sendOsc: this.sendOsc.bind(this), - receiveOsc: this.receiveOsc.bind(this), - setTimeout, - clearTimeout, - setInterval, - clearInterval, - settings - } + var file = (()=>{ + try { + return fs.readFileSync(settings.read('customModule'),'utf8') + } catch(err) { + console.log('CustomModule Error: File not found: ' + settings.read('customModule')) + return false + } + })(), + mod, + context = { + console, + sendOsc: this.sendOsc.bind(this), + receiveOsc: this.receiveOsc.bind(this), + setTimeout, + clearTimeout, + setInterval, + clearInterval, + settings + } - try { - mod = vm.runInContext(file, vm.createContext(context)) - } catch(err) { - console.error(err) - return false - } + try { + mod = vm.runInContext(file, vm.createContext(context)) + } catch(err) { + console.error(err) + return false + } - return mod + return mod - })() + })() } parseType(type){ - var t = type[0].match(/[bhtdscrmifTFNI]/) + var t = type[0].match(/[bhtdscrmifTFNI]/) - if (t!==null) { - return t[0] - } else { - return 's' - } + if (t!==null) { + return t[0] + } else { + return 's' + } } parseArg(arg, precision){ - if (arg==null) return null - switch (typeof arg) { - case 'number': - var typeTagMatch = typeof precision == 'string' ? precision.match(/[0-9]+([a-zA-Z]{1})/) : null, - typeTag = typeTagMatch === null ? precision == 0 ? 'i' : 'f' : typeTagMatch[1], - precisionValue = parseFloat(precision) || 0 + if (arg==null) return null + switch (typeof arg) { + case 'number': + var typeTagMatch = typeof precision == 'string' ? precision.match(/[0-9]+([a-zA-Z]{1})/) : null, + typeTag = typeTagMatch === null ? precision == 0 ? 'i' : 'f' : typeTagMatch[1], + precisionValue = parseFloat(precision) || 0 - return {type: typeTag, value: parseFloat(arg.toFixed(precisionValue))} - case 'boolean': - return {type: arg ? 'T' : 'F', value: arg} - case 'object': - if (arg.type) { - return {type: parseType(arg.type), value: arg.value} - } else { - return {type: 's', value:JSON.stringify(arg)} - } + return {type: typeTag, value: parseFloat(arg.toFixed(precisionValue))} + case 'boolean': + return {type: arg ? 'T' : 'F', value: arg} + case 'object': + if (arg.type) { + return {type: parseType(arg.type), value: arg.value} + } else { + return {type: 's', value:JSON.stringify(arg)} + } case 'string': return {type: 's', value:arg} - default: - return {type: 's', value:JSON.stringify(arg)} - } + default: + return {type: 's', value:JSON.stringify(arg)} + } } sendOsc(data){ - if (!data) return + if (!data) return if (data.host == 'midi') { @@ -102,24 +99,24 @@ class OscServer { } else { - if (typeof data.address !== 'string' || data.address[0] !== '/') { - console.error('OSC error: malformed address: "' + data.address + "") - return - } + if (typeof data.address !== 'string' || data.address[0] !== '/') { + console.error('OSC error: malformed address: "' + data.address + '') + return + } - oscUDPServer.send({ - address: data.address, - args: data.args - }, data.host, data.port) + oscUDPServer.send({ + address: data.address, + args: data.args + }, data.host, data.port) - if (tcpInPort && oscTCPServer.clients[data.host] && oscTCPServer.clients[data.host][data.port]) { - oscTCPServer.clients[data.host][data.port].send({ - address: data.address, - args: data.args - }) - } + if (tcpInPort && oscTCPServer.clients[data.host] && oscTCPServer.clients[data.host][data.port]) { + oscTCPServer.clients[data.host][data.port].send({ + address: data.address, + args: data.args + }) + } - if (debug) console.log('OSC sent: ',{address:data.address, args: data.args}, 'To : ' + data.host + ':' + data.port) + if (debug) console.log('OSC sent: ',{address:data.address, args: data.args}, 'To : ' + data.host + ':' + data.port) } @@ -128,51 +125,51 @@ class OscServer { receiveOsc(data, info){ - if (!data) return + if (!data) return for (i in data.args) { data.args[i] = data.args[i].value } - if (data.args.length==1) data.args = data.args[0] + if (data.args.length==1) data.args = data.args[0] - ipc.send('receiveOsc',data) + ipc.send('receiveOsc',data) - if (debug) console.log('OSC received: ', {address:data.address, args: data.args}, 'From : ' + data.host + ':' + data.port) + if (debug) console.log('OSC received: ', {address:data.address, args: data.args}, 'From : ' + data.host + ':' + data.port) } oscInFilter(data){ - if (this.customModule.oscInFilter) { - return this.customModule.oscInFilter(data) - } else { - return data - } + if (this.customModule.oscInFilter) { + return this.customModule.oscInFilter(data) + } else { + return data + } } oscOutFilter(data){ - if (this.customModule.oscOutFilter) { - return this.customModule.oscOutFilter(data) - } else { - return data - } + if (this.customModule.oscOutFilter) { + return this.customModule.oscOutFilter(data) + } else { + return data + } } oscInHandler(msg, timetag, info){ - var data = this.oscInFilter({address: msg.address, args: msg.args, host: info.address, port: info.port}) + var data = this.oscInFilter({address: msg.address, args: msg.args, host: info.address, port: info.port}) if (!data) return - this.receiveOsc(data) + this.receiveOsc(data) } oscInHandlerWrapper(msg, timetag, info) { - var delay = timetag? Math.max(0,timetag.native - Date.now()) : 0 - if (delay) { - setTimeout(()=>{ - this.oscInHandler(msg, timetag, info) - }, delay) - } else { - this.oscInHandler(msg, timetag, info) - } + var delay = timetag? Math.max(0,timetag.native - Date.now()) : 0 + if (delay) { + setTimeout(()=>{ + this.oscInHandler(msg, timetag, info) + }, delay) + } else { + this.oscInHandler(msg, timetag, info) + } } @@ -181,7 +178,7 @@ class OscServer { if (midi) midi.init((data)=>{ - var data = this.oscInFilter({address: data.address, args: data.args, host: data.host, port: data.port}) + data = this.oscInFilter({address: data.address, args: data.args, host: data.host, port: data.port}) if (!data) return @@ -189,17 +186,17 @@ class OscServer { }) - oscUDPServer.on('message', this.oscInHandlerWrapper.bind(this)) - oscUDPServer.open() + oscUDPServer.on('message', this.oscInHandlerWrapper.bind(this)) + oscUDPServer.open() - if (settings.read('tcpInPort')) { - oscTCPServer.on('message', this.oscInHandlerWrapper.bind(this)) - oscTCPServer.open() - } + if (settings.read('tcpInPort')) { + oscTCPServer.on('message', this.oscInHandlerWrapper.bind(this)) + oscTCPServer.open() + } - if (this.customModule.init) { - this.customModule.init() - } + if (this.customModule.init) { + this.customModule.init() + } } @@ -212,22 +209,22 @@ oscServer.init() module.exports = { - send: function(host,port,address,args,precision) { + send: function(host,port,address,args,precision) { - var message = [] + var message = [] if (!Array.isArray(args)) args = [args] - if (typeof args=='object' && args!==null) { - for (var i in args) { - var arg = oscServer.parseArg(args[i],precision) - if (arg!=null) message.push(arg) - } - } + if (typeof args=='object' && args!==null) { + for (var i in args) { + var arg = oscServer.parseArg(args[i],precision) + if (arg!=null) message.push(arg) + } + } - var data = oscServer.oscOutFilter({address:address, args: message, host: host, port: port}) + var data = oscServer.oscOutFilter({address:address, args: message, host: host, port: port}) - oscServer.sendOsc(data) + oscServer.sendOsc(data) - }, + }, midi:midi } diff --git a/app/main/osc/tcp.js b/app/main/osc/tcp.js index 80a2306b..d9d37609 100644 --- a/app/main/osc/tcp.js +++ b/app/main/osc/tcp.js @@ -1,7 +1,6 @@ var osc = require('osc'), - EventEmitter = require('events').EventEmitter + EventEmitter = require('events').EventEmitter, settings = require('../settings'), - debug = settings.read('debug'), zeroconf = require('../zeroconf'), tcpInPort = settings.read('tcpInPort'), tcpTargets = settings.read('tcpTargets'), @@ -9,7 +8,7 @@ var osc = require('osc'), class OscTCPClient extends EventEmitter { - constructor(options) { + constructor(options) { super() @@ -47,7 +46,7 @@ class OscTCPClient extends EventEmitter { console.log(`TCP: connection established (${this.remoteAddress}:${this.remotePort})`) }) - if (!this.options.socket) { + if (!this.options.socket) { this.port.open() @@ -67,7 +66,7 @@ class OscTCPClient extends EventEmitter { } - send(msg) { + send(msg) { this.port.send(msg) @@ -79,7 +78,7 @@ class OscTCPClient extends EventEmitter { class OscTCPServer extends EventEmitter { - constructor() { + constructor() { super() @@ -100,7 +99,7 @@ class OscTCPServer extends EventEmitter { addClient(client) { - this.clients[client.remoteAddress] = this.clients[client.remoteAddress] || {} + this.clients[client.remoteAddress] = this.clients[client.remoteAddress] || {} this.clients[client.remoteAddress][client.remotePort] = client client.on('message', (msg, timetag, info)=>{ @@ -130,10 +129,10 @@ class OscTCPServer extends EventEmitter { } zeroconf.publish({ - name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''), - protocol: 'tcp', - type: 'osc', - port: tcpInPort + name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''), + protocol: 'tcp', + type: 'osc', + port: tcpInPort }).on('error', (e)=>{ console.error(`Error: Zeroconf: ${e.message}`) }) diff --git a/app/main/osc/udp.js b/app/main/osc/udp.js index 08278e9a..28e496a2 100644 --- a/app/main/osc/udp.js +++ b/app/main/osc/udp.js @@ -1,13 +1,13 @@ var osc = require('osc'), - settings = require('../settings'), - zeroconf = require('../zeroconf'), - oscInPort = settings.read('oscInPort') || settings.read('httpPort') + settings = require('../settings'), + zeroconf = require('../zeroconf'), + oscInPort = settings.read('oscInPort') || settings.read('httpPort') var oscUDPServer = new osc.UDPPort({ - localAddress: "0.0.0.0", + localAddress: '0.0.0.0', localPort: oscInPort, - metadata: true, - broadcast: true + metadata: true, + broadcast: true }) oscUDPServer.on('error', function(error) { @@ -15,10 +15,10 @@ oscUDPServer.on('error', function(error) { }) zeroconf.publish({ - name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''), - protocol: 'udp', - type: 'osc', - port: oscInPort + name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''), + protocol: 'udp', + type: 'osc', + port: oscInPort }).on('error', (e)=>{ console.error(`Error: Zeroconf: ${e.message}`) }) diff --git a/app/main/server.js b/app/main/server.js index 55c0a3bb..ad4a612e 100644 --- a/app/main/server.js +++ b/app/main/server.js @@ -3,12 +3,12 @@ var express = require('express')(), http = require('http'), server = http.createServer(express), Ipc = require('./ipc/server') - ipc = new Ipc(server), - settings = require('./settings'), - theme = require('./theme').init(), - zeroconf = require('./zeroconf'), - appAddresses = settings.read('appAddresses'), - clients = {} +ipc = new Ipc(server), +settings = require('./settings'), +theme = require('./theme').init(), +zeroconf = require('./zeroconf'), +appAddresses = settings.read('appAddresses'), +clients = {} express.get('/', function(req, res){ res.sendFile(path.resolve(__dirname + '/../browser/index.html')) @@ -16,8 +16,8 @@ express.get('/', function(req, res){ express.get('*', function(req, res){ if (req.path.indexOf('theme.css') != -1) { - res.set('Content-Type', 'text/css'); - if (settings.read('theme')) { + res.set('Content-Type', 'text/css') + if (settings.read('theme')) { res.send(new Buffer(theme.get())) } else { res.send('') @@ -32,9 +32,9 @@ express.get('*', function(req, res){ server.listen(settings.read('httpPort')) zeroconf.publish({ - name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''), - type: 'http', - port: settings.read('httpPort') + name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''), + type: 'http', + port: settings.read('httpPort') }).on('error', (e)=>{ console.error(`Error: Zeroconf: ${e.message}`) }) @@ -62,7 +62,7 @@ var bindCallbacks = function(callbacks) { console.log('App available at ' + appAddresses.join(' & ')) module.exports = { - ipc:ipc, - bindCallbacks:bindCallbacks, + ipc:ipc, + bindCallbacks:bindCallbacks, clients:clients } diff --git a/app/main/settings.js b/app/main/settings.js index 1ba22165..dd4bbf2c 100644 --- a/app/main/settings.js +++ b/app/main/settings.js @@ -8,82 +8,82 @@ var baseDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME' var options = { 's':{alias:'send',type:'array',describe:'default targets for all widgets (ip:port pairs)', - check: (s)=>{ - return s.some(item => !item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ? - 'Targets must be ip:port pairs & port must be >= 1024' : true - } + check: (s)=>{ + return s.some(item=>!item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ? + 'Targets must be ip:port pairs & port must be >= 1024' : true + } }, 'sync':{type:'array',describe:false,launcher:false,hidden:true, - check: (s)=>{ - if (s) console.error('Warning: --sync is deprecated, use --send instead.') - return s.some(item => !item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ? - 'Targets must be ip:port pairs & port must be >= 1024' : true - } + check: (s)=>{ + if (s) console.error('Warning: --sync is deprecated, use --send instead.') + return s.some(item=>!item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ? + 'Targets must be ip:port pairs & port must be >= 1024' : true + } }, 'l':{alias:'load',type:'string',file:{name:'OSC Session (.json)', extensions: ['json', 'js']},describe:'session file to load'}, 'b':{alias:'blank',type:'boolean',describe:'load a blank session and start the editor'}, 'c':{alias:'custom-module',type:'string',file:{name:'OSC Custom module (.js)', extensions: ['js']},describe:'custom module file to load'}, 'p':{alias:'port',type:'number',describe:'http port of the server (default to 8080)', - check: (p)=>{ - return (!isNaN(p) && p > 1023 && parseInt(p)===p) ? + check: (p)=>{ + return (!isNaN(p) && p > 1023 && parseInt(p)===p) ? true : 'Port must be an integer >= 1024' - } - }, - 'o':{alias:'osc-port',type:'number',describe:'osc input port (default to --port)', - check: (o)=>{ - return (!isNaN(o) && o > 1023 && parseInt(o)===o) ? - true : 'Port must be an integer >= 1024' - } - }, - 'tcp-port':{type:'number', describe:'TCP server input port', - check: (t, argv)=>{ - var h = argv.p || 8080 - if (t===h) return 'TCP input port must different from --port' - return (!isNaN(t) && t > 1023 && parseInt(t)===t) ? + } + }, + 'o':{alias:'osc-port',type:'number',describe:'osc input port (default to --port)', + check: (o)=>{ + return (!isNaN(o) && o > 1023 && parseInt(o)===o) ? true : 'Port must be an integer >= 1024' - } - }, - 'tcp-targets':{type:'array',describe:'TCP servers to connect to (ip:port pairs), does not susbtitute for --send', - check: (s, argv)=>{ - if (!argv['tcp-port']) return '--tcp-port must be set' - return s.some(item => !item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ? - 'Targets must be ip:port pairs & port must be >= 1024' : true - } - }, + } + }, + 'tcp-port':{type:'number', describe:'TCP server input port', + check: (t, argv)=>{ + var h = argv.p || 8080 + if (t===h) return 'TCP input port must different from --port' + return (!isNaN(t) && t > 1023 && parseInt(t)===t) ? + true : 'Port must be an integer >= 1024' + } + }, + 'tcp-targets':{type:'array',describe:'TCP servers to connect to (ip:port pairs), does not susbtitute for --send', + check: (s, argv)=>{ + if (!argv['tcp-port']) return '--tcp-port must be set' + return s.some(item=>!item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ? + 'Targets must be ip:port pairs & port must be >= 1024' : true + } + }, 'm':{alias:'midi',type:'array',describe:'midi router settings'}, 'd':{alias:'debug',type:'boolean',describe:'log received osc messages in the console'}, 'n':{alias:'no-gui',type:'boolean',describe:'disable default gui', - check: (n,argv)=>{ - return (!n || !argv.g) ? + check: (n,argv)=>{ + return (!n || !argv.g) ? true : 'no-gui and gui-only can\'s be enabled simultaneously' - } - }, + } + }, 'g':{alias:'gui-only',type:'string',describe:'app server\'s url. If true, local port (--port) is used', - check: (g,argv)=>{ - return (!g || !argv.n) ? + check: (g,argv)=>{ + return (!g || !argv.n) ? true : 'no-gui and gui-only can\'s be enabled simultaneously' - } + } }, 't':{alias:'theme',type:'array',describe:'theme name or path (mutliple values allowed)'}, 'e':{alias:'examples',type:'boolean',describe:'list examples instead of recent sessions', - check: (e,argv)=>{ - return (!e || !argv.l) ? + check: (e,argv)=>{ + return (!e || !argv.l) ? true : 'examples can\'t be listed if --load is set' - } + } }, 'url-options':{type:'array',describe:'url options (opt=value pairs)', check: (u, argv)=>{ - return (!u || !argv.n) ? - true : 'url options can\'t be passed in no-gui mode' + return (!u || !argv.n) ? + true : 'url options can\'t be passed in no-gui mode' } }, 'disable-vsync':{type:'boolean',describe:'disable gui\'s vertical synchronization', restart: true}, 'disable-gpu':{type:'boolean',describe:'disable hardware acceleration', restart: true}, 'read-only':{type:'boolean',describe:'disable session editing and session history changes', - check: (r, argv)=>{ - return (!r || !argv.b) ? + check: (r, argv)=>{ + return (!r || !argv.b) ? true : 'blank session can\'t be started in read-only mode' - } + } }, 'instance-name':{type:'string',describe:'used to differenciate multiple instances in a zeroconf network'}, 'fullscreen':{type:'boolean', describe:'launch the default client gui in fullscreen mode (F11 to exit)'} @@ -91,22 +91,22 @@ var options = { var argv = require('yargs') - .help('help').usage(`\nUsage:\n $0 [options]`).alias('h', 'help') - .options(options) - .check((argv)=>{ - var err = [] - for (key in options) { - if (options[key].check && argv[key] != undefined) { - var c = options[key].check(argv[key],argv) - if (c!==true) { - err.push(`-${key}: ${c}`) - } + .help('help').usage('\nUsage:\n $0 [options]').alias('h', 'help') + .options(options) + .check((argv)=>{ + var err = [] + for (key in options) { + if (options[key].check && argv[key] != undefined) { + var c = options[key].check(argv[key],argv) + if (c!==true) { + err.push(`-${key}: ${c}`) } } - return err.length ? err.join('\n') : true - }) - .strict() - .version().alias('v','version') + } + return err.length ? err.join('\n') : true + }) + .strict() + .version().alias('v','version') argv = argv.argv @@ -127,11 +127,10 @@ var makeDefaultConfig = function(argv){ sessionPath: process.cwd(), recentSessions: [], appName: 'Open Stage Control', - instanceName: argv['instance-name'] || false, - targets: argv.s || argv.sync || false, + instanceName: argv['instance-name'] || false, + targets: argv.s || argv.sync || false, oscInPort: argv.o || 0, httpPort: argv.p || 8080, - httpPort: argv.p || 8080, tcpInPort: argv['tcp-port'] || false, tcpTargets: argv['tcp-targets'] || [], debug: argv.d || false, @@ -142,14 +141,14 @@ var makeDefaultConfig = function(argv){ noGui: argv.n || false, guiOnly: typeof argv.g == 'string' ? argv.g.length ? argv.g : true : false, urlOptions: argv['url-options'] ? '?' + argv['url-options'].join('&') : '', - noVsync: argv['disable-vsync'] || false, - noGpu: argv['disable-gpu'] || false, - readOnly: argv['read-only'] || false, + noVsync: argv['disable-vsync'] || false, + noGpu: argv['disable-gpu'] || false, + readOnly: argv['read-only'] || false, midi: argv.m, appAddresses:function(){ var appAddresses = [] - Object.keys(ifaces).forEach(function (ifname) { + Object.keys(ifaces).forEach(function(ifname) { for (i=0;i