From 5d59bf45137f92a6b9f9ffb2fedb01444067f891 Mon Sep 17 00:00:00 2001 From: Alexandre Trovato Date: Sun, 14 Feb 2021 16:53:44 +0100 Subject: [PATCH] Improve eslint --- .editorconfig | 3 +- .eslintrc.js | 11 +-- examples/cache-gatt-discovery.js | 10 +++ examples/echo.js | 10 +++ examples/peripheral-explorer.js | 38 ++++++++- examples/pizza/central.js | 42 +++++++--- examples/pizza/pizza.js | 14 ++-- index.js | 4 +- lib/hci-socket/bindings.js | 4 +- lib/hci-socket/gap.js | 20 ++--- lib/hci-socket/gatt.js | 2 +- lib/hci-socket/hci.js | 6 +- lib/noble.js | 2 +- package.json | 3 +- test.js | 8 +- with-bindings.js | 2 +- ws-slave.js | 128 +++++++++++++++---------------- 17 files changed, 198 insertions(+), 109 deletions(-) diff --git a/.editorconfig b/.editorconfig index dd171c51e..04fcb79a9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,5 +6,6 @@ indent_style = space end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true - [*.{diff,md}] +quote_type = single +[*.{diff,md}] trim_trailing_whitespace = false diff --git a/.eslintrc.js b/.eslintrc.js index 5cea036b2..6feb172fc 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,6 +1,6 @@ module.exports = { root: true, - extends: ["eslint:recommended", "semistandard"], + extends: ['eslint:recommended', 'semistandard'], parserOptions: { ecmaVersion: 2017 }, @@ -13,12 +13,13 @@ module.exports = { Promise: true }, rules: { - "no-unused-vars": [ - "error", + 'space-before-function-paren': ['error', 'always'], + 'no-unused-vars': [ + 'error', { - args: "none" + args: 'none' } ], - "semi": "error" + semi: 'error' } }; diff --git a/examples/cache-gatt-discovery.js b/examples/cache-gatt-discovery.js index 70b074377..3e883fa14 100644 --- a/examples/cache-gatt-discovery.js +++ b/examples/cache-gatt-discovery.js @@ -89,6 +89,11 @@ const findServices = function (noble, peripheral) { }); peripheral.discoverServices([], (error, services) => { + if (error) { + console.error(error); + return; + } + let sensorCharacteristic; servicesToRead = services.length; @@ -108,6 +113,11 @@ const findServices = function (noble, peripheral) { }); service.discoverCharacteristics([], function (error, characteristics) { + if (error) { + console.error(error); + return; + } + console.log(`SRV\t${service.uuid} characteristic decoded data: `); for (let j = 0; j < characteristics.length; j++) { const ch = characteristics[j]; diff --git a/examples/echo.js b/examples/echo.js index ba9ec160a..daa0d0ac2 100755 --- a/examples/echo.js +++ b/examples/echo.js @@ -30,6 +30,11 @@ noble.on('discover', peripheral => { function connectAndSetUp (peripheral) { peripheral.connect(error => { + if (error) { + console.error(error); + return; + } + console.log('Connected to', peripheral.id); // specify the services and characteristics to discover @@ -47,6 +52,11 @@ function connectAndSetUp (peripheral) { } function onServicesAndCharacteristicsDiscovered (error, services, characteristics) { + if (error) { + console.error(error); + return; + } + console.log('Discovered services and characteristics'); const echoCharacteristic = characteristics[0]; diff --git a/examples/peripheral-explorer.js b/examples/peripheral-explorer.js index c41f9dff4..e3e778d37 100644 --- a/examples/peripheral-explorer.js +++ b/examples/peripheral-explorer.js @@ -59,7 +59,17 @@ function explore (peripheral) { }); peripheral.connect(function (error) { + if (error) { + console.error(error); + return; + } + peripheral.discoverServices([], function (error, services) { + if (error) { + console.error(error); + return; + } + let serviceIndex = 0; async.whilst( @@ -76,6 +86,11 @@ function explore (peripheral) { console.log(serviceInfo); service.discoverCharacteristics([], function (error, characteristics) { + if (error) { + console.error(error); + return; + } + let characteristicIndex = 0; async.whilst( @@ -93,6 +108,11 @@ function explore (peripheral) { async.series([ function (callback) { characteristic.discoverDescriptors(function (error, descriptors) { + if (error) { + console.error(error); + return; + } + async.detect( descriptors, function (descriptor, callback) { @@ -105,6 +125,10 @@ function explore (peripheral) { function (userDescriptionDescriptor) { if (userDescriptionDescriptor) { userDescriptionDescriptor.readValue(function (error, data) { + if (error) { + console.error(error); + } + if (data) { characteristicInfo += ` (${data.toString()})`; } @@ -122,6 +146,10 @@ function explore (peripheral) { if (characteristic.properties.indexOf('read') !== -1) { characteristic.read(function (error, data) { + if (error) { + console.error(error); + } + if (data) { const string = data.toString('ascii'); @@ -141,13 +169,21 @@ function explore (peripheral) { ]); }, function (error) { + if (error) { + console.error(error); + } + serviceIndex++; callback(); } ); }); }, - function (err) { + function (error) { + if (error) { + console.error(error); + } + peripheral.disconnect(); } ); diff --git a/examples/pizza/central.js b/examples/pizza/central.js index 4b7664005..7c79e3f53 100644 --- a/examples/pizza/central.js +++ b/examples/pizza/central.js @@ -38,12 +38,21 @@ noble.on('discover', function (peripheral) { // // Once the peripheral has been discovered, then connect to it. // - peripheral.connect(function (err) { + peripheral.connect(function (error) { + if (error) { + console.error(error); + } + // // Once the peripheral has been connected, then discover the // services and characteristics of interest. // - peripheral.discoverServices([pizzaServiceUuid], function (err, services) { + peripheral.discoverServices([pizzaServiceUuid], function (error, services) { + if (error) { + console.error(error); + return; + } + services.forEach(function (service) { // // This must be the service we were looking for. @@ -53,7 +62,12 @@ noble.on('discover', function (peripheral) { // // So, discover its characteristics. // - service.discoverCharacteristics([], function (err, characteristics) { + service.discoverCharacteristics([], function (error, characteristics) { + if (error) { + console.error(error); + return; + } + characteristics.forEach(function (characteristic) { // // Loop through each characteristic and match them to the @@ -118,17 +132,27 @@ function bakePizza () { if (data.length === 1) { const result = data.readUInt8(0); console.log('The result is', - result === pizza.PizzaBakeResult.HALF_BAKED ? 'half baked.' - : result === pizza.PizzaBakeResult.BAKED ? 'baked.' - : result === pizza.PizzaBakeResult.CRISPY ? 'crispy.' - : result === pizza.PizzaBakeResult.BURNT ? 'burnt.' - : result === pizza.PizzaBakeResult.ON_FIRE ? 'on fire!' + result === pizza.PizzaBakeResult.HALF_BAKED + ? 'half baked.' + : result === pizza.PizzaBakeResult.BAKED + ? 'baked.' + : result === pizza.PizzaBakeResult.CRISPY + ? 'crispy.' + : result === pizza.PizzaBakeResult.BURNT + ? 'burnt.' + : result === pizza.PizzaBakeResult.ON_FIRE + ? 'on fire!' : 'unknown?'); } else { console.log('result length incorrect'); } }); - pizzaBakeCharacteristic.subscribe(function (err) { + pizzaBakeCharacteristic.subscribe(function (error) { + if (error) { + console.error(error); + return; + } + // // Bake at 450 degrees! // diff --git a/examples/pizza/pizza.js b/examples/pizza/pizza.js index 78e553f27..7b98b6b2d 100644 --- a/examples/pizza/pizza.js +++ b/examples/pizza/pizza.js @@ -41,11 +41,15 @@ Pizza.prototype.bake = function (temperature) { console.log('baking pizza at', temperature, 'degrees for', time, 'milliseconds'); setTimeout(function () { const result = - (temperature < 350) ? PizzaBakeResult.HALF_BAKED - : (temperature < 450) ? PizzaBakeResult.BAKED - : (temperature < 500) ? PizzaBakeResult.CRISPY - : (temperature < 600) ? PizzaBakeResult.BURNT - : PizzaBakeResult.ON_FIRE; + (temperature < 350) + ? PizzaBakeResult.HALF_BAKED + : (temperature < 450) + ? PizzaBakeResult.BAKED + : (temperature < 500) + ? PizzaBakeResult.CRISPY + : (temperature < 600) + ? PizzaBakeResult.BURNT + : PizzaBakeResult.ON_FIRE; self.emit('ready', result); }, time); }; diff --git a/index.js b/index.js index d7abbab90..76a2a0bdf 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -var Noble = require('./lib/noble'); -var bindings = require('./lib/resolve-bindings')(); +const Noble = require('./lib/noble'); +const bindings = require('./lib/resolve-bindings')(); module.exports = new Noble(bindings); diff --git a/lib/hci-socket/bindings.js b/lib/hci-socket/bindings.js index 3ee1f9340..463c5baf5 100644 --- a/lib/hci-socket/bindings.js +++ b/lib/hci-socket/bindings.js @@ -165,7 +165,7 @@ NobleBindings.prototype.onDiscover = function (status, address, addressType, con } if (hasScanServiceUuids) { - var uuid = address.split(':').join(''); + const uuid = address.split(':').join(''); this._addresses[uuid] = address; this._addresseTypes[uuid] = addressType; this._connectable[uuid] = connectable; @@ -276,7 +276,7 @@ NobleBindings.prototype.onEncryptChange = function (handle, encrypt) { }; NobleBindings.prototype.onMtu = function (address, mtu) { - var uuid = address.split(':').join('').toLowerCase(); + const uuid = address.split(':').join('').toLowerCase(); this.emit('onMtu', uuid, mtu); }; diff --git a/lib/hci-socket/gap.js b/lib/hci-socket/gap.js index 29d7537f0..6bcf11b95 100644 --- a/lib/hci-socket/gap.js +++ b/lib/hci-socket/gap.js @@ -97,14 +97,16 @@ Gap.prototype.onLeScanEnableSetCmd = function (enable, filterDuplicates) { Gap.prototype.onHciLeAdvertisingReport = function (status, type, address, addressType, eir, rssi) { const previouslyDiscovered = !!this._discoveries[address]; - const advertisement = previouslyDiscovered ? this._discoveries[address].advertisement : { - localName: undefined, - txPowerLevel: undefined, - manufacturerData: undefined, - serviceData: [], - serviceUuids: [], - solicitationServiceUuids: [] - }; + const advertisement = previouslyDiscovered + ? this._discoveries[address].advertisement + : { + localName: undefined, + txPowerLevel: undefined, + manufacturerData: undefined, + serviceData: [], + serviceUuids: [], + solicitationServiceUuids: [] + }; let discoveryCount = previouslyDiscovered ? this._discoveries[address].count : 0; let hasScanResponse = previouslyDiscovered ? this._discoveries[address].hasScanResponse : false; @@ -123,7 +125,7 @@ Gap.prototype.onHciLeAdvertisingReport = function (status, type, address, addres let i = 0; while ((i + 1) < eir.length) { - var length = eir.readUInt8(i); + const length = eir.readUInt8(i); if (length < 1) { debug(`invalid EIR data, length = ${length}`); diff --git a/lib/hci-socket/gatt.js b/lib/hci-socket/gatt.js index 0d4a7768f..535487a4f 100644 --- a/lib/hci-socket/gatt.js +++ b/lib/hci-socket/gatt.js @@ -453,7 +453,7 @@ Gatt.prototype.discoverCharacteristics = function (serviceUuid, characteristicUu if (opcode !== ATT_OP_READ_BY_TYPE_RESP || characteristics[characteristics.length - 1].valueHandle === service.endHandle) { const characteristicsDiscovered = []; for (i = 0; i < characteristics.length; i++) { - var properties = characteristics[i].properties; + const properties = characteristics[i].properties; const characteristic = { properties: [], diff --git a/lib/hci-socket/hci.js b/lib/hci-socket/hci.js index 9bbdff4f7..38a5d8c84 100644 --- a/lib/hci-socket/hci.js +++ b/lib/hci-socket/hci.js @@ -393,7 +393,7 @@ Hci.prototype.connUpdateLe = function (handle, minInterval, maxInterval, latency }; Hci.prototype.cancelConnect = function () { - var cmd = Buffer.alloc(4); + const cmd = Buffer.alloc(4); // header cmd.writeUInt8(HCI_COMMAND_PKT, 0); @@ -609,9 +609,9 @@ Hci.prototype.onSocketData = function (data) { handle = data.readUInt16LE(1) & 0x0fff; if (ACL_START === flags) { - var cid = data.readUInt16LE(7); + const cid = data.readUInt16LE(7); - var length = data.readUInt16LE(5); + const length = data.readUInt16LE(5); const pktData = data.slice(9); debug(`\t\tcid = ${cid}`); diff --git a/lib/noble.js b/lib/noble.js index a4c5ebeb4..072ec9a85 100644 --- a/lib/noble.js +++ b/lib/noble.js @@ -568,7 +568,7 @@ Noble.prototype.onHandleNotify = function (peripheralUuid, handle, data) { }; Noble.prototype.onMtu = function (peripheralUuid, mtu) { - var peripheral = this._peripherals[peripheralUuid]; + const peripheral = this._peripherals[peripheralUuid]; peripheral.mtu = mtu; }; diff --git a/package.json b/package.json index 334df3b80..7cc9b92b9 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,8 @@ "ws": "^7.4.2" }, "scripts": { - "lint": "eslint '**/*.js'", + "lint": "eslint \"**/*.js\"", + "lint-fix": "eslint \"**/*.js\" --fix", "test": "mocha -R spec test/*.js" }, "browser": { diff --git a/test.js b/test.js index b60e39e55..92787754c 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,4 @@ -var noble = require('./index'); +const noble = require('./index'); console.log('noble'); @@ -42,7 +42,7 @@ noble.on('discover', function (peripheral) { peripheral.on('servicesDiscover', function (services) { console.log('on -> peripheral services discovered ' + services); - var serviceIndex = 0; + const serviceIndex = 0; services[serviceIndex].on('includedServicesDiscover', function (includedServiceUuids) { console.log('on -> service included services discovered ' + includedServiceUuids); @@ -52,7 +52,7 @@ noble.on('discover', function (peripheral) { services[serviceIndex].on('characteristicsDiscover', function (characteristics) { console.log('on -> service characteristics discovered ' + characteristics); - var characteristicIndex = 0; + const characteristicIndex = 0; characteristics[characteristicIndex].on('read', function (data, isNotification) { console.log('on -> characteristic read ' + data + ' ' + isNotification); @@ -82,7 +82,7 @@ noble.on('discover', function (peripheral) { characteristics[characteristicIndex].on('descriptorsDiscover', function (descriptors) { console.log('on -> descriptors discover ' + descriptors); - var descriptorIndex = 0; + const descriptorIndex = 0; descriptors[descriptorIndex].on('valueRead', function (data) { console.log('on -> descriptor value read ' + data); diff --git a/with-bindings.js b/with-bindings.js index 87725cf53..317a8a1b2 100644 --- a/with-bindings.js +++ b/with-bindings.js @@ -1,4 +1,4 @@ -var Noble = require('./lib/noble'); +const Noble = require('./lib/noble'); module.exports = function (bindings) { return new Noble(bindings); diff --git a/ws-slave.js b/ws-slave.js index 14fb568b4..292b31c8c 100644 --- a/ws-slave.js +++ b/ws-slave.js @@ -1,14 +1,14 @@ /* jshint loopfunc: true */ -var WebSocket = require('ws'); +const WebSocket = require('ws'); -var noble = require('./index'); +const noble = require('./index'); -var serverMode = !process.argv[2]; -var port = 0xB1e; -var host = process.argv[2]; +const serverMode = !process.argv[2]; +const port = 0xB1e; +const host = process.argv[2]; -var ws; -var wss; +let ws; +let wss; if (serverMode) { console.log('noble - ws slave - server mode'); @@ -61,63 +61,63 @@ if (serverMode) { }); } -var peripherals = {}; +const peripherals = {}; // TODO: open/close ws on state change function sendEvent (event) { - var message = JSON.stringify(event); + const message = JSON.stringify(event); console.log('ws -> send: ' + message); - var clients = serverMode ? wss.clients : [ws]; + const clients = serverMode ? wss.clients : [ws]; - for (var i = 0; i < clients.length; i++) { + for (let i = 0; i < clients.length; i++) { clients[i].send(message); } } -var onMessage = function (message) { +const onMessage = function (message) { console.log('ws -> message: ' + message); - var command = JSON.parse(message); - - var action = command.action; - var peripheralUuid = command.peripheralUuid; - var serviceUuids = command.serviceUuids; - var serviceUuid = command.serviceUuid; - var characteristicUuids = command.characteristicUuids; - var characteristicUuid = command.characteristicUuid; - var data = command.data ? Buffer.from(command.data, 'hex') : null; - var withoutResponse = command.withoutResponse; - var broadcast = command.broadcast; - var notify = command.notify; - var descriptorUuid = command.descriptorUuid; - var handle; - - var peripheral = peripherals[peripheralUuid]; - var service = null; - var characteristic = null; - var descriptor = null; + const command = JSON.parse(message); + + const action = command.action; + const peripheralUuid = command.peripheralUuid; + const serviceUuids = command.serviceUuids; + const serviceUuid = command.serviceUuid; + const characteristicUuids = command.characteristicUuids; + const characteristicUuid = command.characteristicUuid; + const data = command.data ? Buffer.from(command.data, 'hex') : null; + const withoutResponse = command.withoutResponse; + const broadcast = command.broadcast; + const notify = command.notify; + const descriptorUuid = command.descriptorUuid; + let handle; + + const peripheral = peripherals[peripheralUuid]; + let service = null; + let characteristic = null; + let descriptor = null; if (peripheral && serviceUuid) { - var services = peripheral.services; + const services = peripheral.services; - for (var i in services) { + for (const i in services) { if (services[i].uuid === serviceUuid) { service = services[i]; if (characteristicUuid) { - var characteristics = service.characteristics; + const characteristics = service.characteristics; - for (var j in characteristics) { + for (const j in characteristics) { if (characteristics[j].uuid === characteristicUuid) { characteristic = characteristics[j]; if (descriptorUuid) { - var descriptors = characteristic.descriptors; + const descriptors = characteristic.descriptors; - for (var k in descriptors) { + for (const k in descriptors) { if (descriptors[k].uuid === descriptorUuid) { descriptor = descriptors[k]; break; @@ -186,9 +186,9 @@ noble.on('discover', function (peripheral) { peripheralUuid: this.uuid }); - for (var i in this.services) { - for (var j in this.services[i].characteristics) { - for (var k in this.services[i].characteristics[j].descriptors) { + for (const i in this.services) { + for (const j in this.services[i].characteristics) { + for (const k in this.services[i].characteristics[j].descriptors) { this.services[i].characteristics[j].descriptors[k].removeAllListeners(); } @@ -209,10 +209,10 @@ noble.on('discover', function (peripheral) { }); peripheral.on('servicesDiscover', function (services) { - var peripheral = this; - var serviceUuids = []; + const peripheral = this; + const serviceUuids = []; - var includedServicesDiscover = function (includedServiceUuids) { + const includedServicesDiscover = function (includedServiceUuids) { sendEvent({ type: 'includedServicesDiscover', peripheralUuid: peripheral.uuid, @@ -221,12 +221,12 @@ noble.on('discover', function (peripheral) { }); }; - var characteristicsDiscover = function (characteristics) { - var service = this; - var discoveredCharacteristics = []; + const characteristicsDiscover = function (characteristics) { + const service = this; + const discoveredCharacteristics = []; - var read = function (data, isNotification) { - var characteristic = this; + const read = function (data, isNotification) { + const characteristic = this; sendEvent({ type: 'read', @@ -238,8 +238,8 @@ noble.on('discover', function (peripheral) { }); }; - var write = function () { - var characteristic = this; + const write = function () { + const characteristic = this; sendEvent({ type: 'write', @@ -249,8 +249,8 @@ noble.on('discover', function (peripheral) { }); }; - var broadcast = function (state) { - var characteristic = this; + const broadcast = function (state) { + const characteristic = this; sendEvent({ type: 'broadcast', @@ -261,8 +261,8 @@ noble.on('discover', function (peripheral) { }); }; - var notify = function (state) { - var characteristic = this; + const notify = function (state) { + const characteristic = this; sendEvent({ type: 'notify', @@ -273,13 +273,13 @@ noble.on('discover', function (peripheral) { }); }; - var descriptorsDiscover = function (descriptors) { - var characteristic = this; + const descriptorsDiscover = function (descriptors) { + const characteristic = this; - var discoveredDescriptors = []; + const discoveredDescriptors = []; - var valueRead = function (data) { - var descriptor = this; + const valueRead = function (data) { + const descriptor = this; sendEvent({ type: 'valueRead', @@ -291,8 +291,8 @@ noble.on('discover', function (peripheral) { }); }; - var valueWrite = function (data) { - var descriptor = this; + const valueWrite = function (data) { + const descriptor = this; sendEvent({ type: 'valueWrite', @@ -303,7 +303,7 @@ noble.on('discover', function (peripheral) { }); }; - for (var k in descriptors) { + for (const k in descriptors) { descriptors[k].on('valueRead', valueRead); descriptors[k].on('valueWrite', valueWrite); @@ -320,7 +320,7 @@ noble.on('discover', function (peripheral) { }); }; - for (var j = 0; j < characteristics.length; j++) { + for (let j = 0; j < characteristics.length; j++) { characteristics[j].on('read', read); characteristics[j].on('write', write); @@ -345,7 +345,7 @@ noble.on('discover', function (peripheral) { }); }; - for (var i in services) { + for (const i in services) { services[i].on('includedServicesDiscover', includedServicesDiscover); services[i].on('characteristicsDiscover', characteristicsDiscover);