From 824ccfea2183d91d49cd08ef2b52a801382a6cd9 Mon Sep 17 00:00:00 2001 From: Rob Moran Date: Sun, 21 May 2017 23:21:19 +0100 Subject: [PATCH] Switched to using eslint --- .eslintrc | 18 +++ circle.yml | 2 +- dist/dfu.js | 4 +- dist/secure-dfu.js | 46 +++---- examples/backup.html | 265 ++++++++++++++++++++++++++++++++++++ examples/dfu_node.js | 4 +- examples/secure_dfu_node.js | 9 +- gulpfile.js | 18 ++- package.json | 5 +- 9 files changed, 330 insertions(+), 41 deletions(-) create mode 100644 .eslintrc create mode 100644 examples/backup.html diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..10e71a0 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,18 @@ +{ + "env": { + "browser": true, + "node": true, + "es6": true + }, + "globals": { + "define": true + }, + rules: { + "semi": ["error"], + "indent": ["error", 4, { "SwitchCase": 1 }], + "no-irregular-whitespace": ["error"], + "linebreak-style": ["warn", "unix"], + "no-undef": ["warn"], + "no-unused-vars": ["warn"] + } +} diff --git a/circle.yml b/circle.yml index 21310fa..1de7916 100644 --- a/circle.yml +++ b/circle.yml @@ -8,4 +8,4 @@ dependencies: test: override: - - gulp + - npm run gulp diff --git a/dist/dfu.js b/dist/dfu.js index 5913c99..843ed7c 100644 --- a/dist/dfu.js +++ b/dist/dfu.js @@ -230,7 +230,7 @@ log("connected to device"); currentServer = gattServer; // This delay is needed because BlueZ needs time to update it's cache. - return new Promise(function(resolve, reject) { + return new Promise(function(resolve) { setTimeout(resolve, 2000); }); }) @@ -257,7 +257,7 @@ versionChar = characteristic; complete(); }) - .catch(function(error) { + .catch(function() { log("info: no version characteristic found"); complete(); }); diff --git a/dist/secure-dfu.js b/dist/secure-dfu.js index 646978f..6e0e34f 100644 --- a/dist/secure-dfu.js +++ b/dist/secure-dfu.js @@ -102,13 +102,13 @@ } function createListenerFn(eventTypes) { - return function(type, callback, capture) { + return function(type, callback) { if (eventTypes.indexOf(type) < 0) return; if (!this.events[type]) this.events[type] = []; this.events[type].push(callback); }; } - function removeEventListener(type, callback, capture) { + function removeEventListener(type, callback) { if (!this.events[type]) return; let i = this.events[type].indexOf(callback); if (i >= 0) this.events[type].splice(i, 1); @@ -136,7 +136,7 @@ totalBytes: 0, currentBytes: bytes }); - } + }; secureDfu.prototype.requestDevice = function(hiddenDfu, filters) { if (!hiddenDfu && !filters) { @@ -194,14 +194,14 @@ }) .then(() => { this.log("sent dfu mode"); - return new Promise((resolve, reject) => { - device.addEventListener("gattserverdisconnected", event => { + return new Promise((resolve) => { + device.addEventListener("gattserverdisconnected", () => { resolve(); }); }); }); }); - } + }; secureDfu.prototype.update = function(device, init, firmware) { if (!device) throw new Error("Device not specified"); @@ -219,17 +219,17 @@ }) .then(() => { this.log("complete, disconnecting..."); - return new Promise((resolve, reject) => { - device.addEventListener("gattserverdisconnected", event => { + return new Promise((resolve) => { + device.addEventListener("gattserverdisconnected", () => { this.log("disconnected"); resolve(); }); - }); + }); }); - } + }; secureDfu.prototype.connect = function(device) { - device.addEventListener("gattserverdisconnected", event => { + device.addEventListener("gattserverdisconnected", () => { this.controlChar = null; this.packetChar = null; }); @@ -271,7 +271,7 @@ .then(server => { this.log("connected to gatt server"); return server.getPrimaryService(SERVICE_UUID) - .catch(error => { + .catch(() => { throw new Error("Unable to find DFU service"); }); }) @@ -279,7 +279,7 @@ this.log("found DFU service"); return service.getCharacteristics(); }); - } + }; secureDfu.prototype.handleNotification = function(event) { let view = event.target.value; @@ -330,19 +330,19 @@ characteristic.writeValue(value); }); - } + }; secureDfu.prototype.sendControl = function(operation, buffer) { return this.sendOperation(this.controlChar, operation, buffer); - } + }; secureDfu.prototype.transferInit = function(buffer) { return this.transfer(buffer, "init", OPERATIONS.SELECT_COMMAND, OPERATIONS.CREATE_COMMAND); - } + }; secureDfu.prototype.transferFirmware = function(buffer) { return this.transfer(buffer, "firmware", OPERATIONS.SELECT_DATA, OPERATIONS.CREATE_DATA); - } + }; secureDfu.prototype.transfer = function(buffer, type, selectType, createType) { return this.sendControl(selectType) @@ -364,12 +364,12 @@ totalBytes: buffer.byteLength, currentBytes: bytes }); - } + }; this.progress(0); return this.transferObject(buffer, createType, maxSize, offset); }); - } + }; secureDfu.prototype.transferObject = function(buffer, createType, maxSize, offset) { let start = offset - offset % maxSize; @@ -379,7 +379,7 @@ view.setUint32(0, end - start, LITTLE_ENDIAN); return this.sendControl(createType, view.buffer) - .then(response => { + .then(() => { let data = buffer.slice(start, end); return this.transferData(data, start); }) @@ -406,7 +406,7 @@ this.log("transfer complete"); } }); - } + }; secureDfu.prototype.transferData = function(data, offset, start) { start = start || 0; @@ -421,7 +421,7 @@ return this.transferData(data, offset, end); } }); - } + }; secureDfu.prototype.checkCrc = function(buffer, crc) { if (!this.crc32) { @@ -430,7 +430,7 @@ } return crc === this.crc32(new Uint8Array(buffer)); - } + }; secureDfu.prototype.addEventListener = createListenerFn([ "log", "progress" ]); secureDfu.prototype.removeEventListener = removeEventListener; diff --git a/examples/backup.html b/examples/backup.html new file mode 100644 index 0000000..5d0c619 --- /dev/null +++ b/examples/backup.html @@ -0,0 +1,265 @@ + + + + Web Bluetooth Secure DFU + + + + + + + +

Web Bluetooth Secure Device Firmware Update

+
+ + + + + + + +
+ +
+ +
+
+
+
+
+ + + + + + + + diff --git a/examples/dfu_node.js b/examples/dfu_node.js index 426d3df..202cbe0 100644 --- a/examples/dfu_node.js +++ b/examples/dfu_node.js @@ -30,10 +30,10 @@ switch(deviceType) { dfu.findDevice({ services: [0x180D] }) .then(device => { fileName = fileMask.replace("{0}", device.name === "Hi_Rob" ? "bye" : "hi"); - log("found device: " + device.name); + log("found device: " + device.name); log("using file name: " + fileName); - return dfu.writeMode(device); + return dfu.writeMode(device); }) .then(() => dfu.findDevice({ name: "DfuTarg" })) .then(device => { diff --git a/examples/secure_dfu_node.js b/examples/secure_dfu_node.js index 7122ca0..9556546 100644 --- a/examples/secure_dfu_node.js +++ b/examples/secure_dfu_node.js @@ -17,7 +17,7 @@ function logError(error) { } function getFileName() { - return new Promise((resolve, reject) => { + return new Promise((resolve) => { if (process.argv[2]) { return resolve(process.argv[2]); } @@ -55,7 +55,7 @@ function downloadFile(url) { } function loadFile(fileName) { - return new Promise((resolve, reject) => { + return new Promise((resolve) => { var file = fs.readFileSync(fileName); resolve(new Uint8Array(file).buffer); }); @@ -97,6 +97,7 @@ function updateFirmware(dfu, package, manifest, device, type) { } function update() { + var device = null; var dfu = null; var package = null; var manifest = null; @@ -144,7 +145,7 @@ function update() { console.log("DFU mode set"); return bluetooth.requestDevice({ filters: [{ services: [secureDfu.SERVICE_UUID] }], - deviceFound: device => { + deviceFound: () => { // Select first device found with correct service return true; } @@ -153,7 +154,7 @@ function update() { .then(selectedDevice => { device = selectedDevice; - for (type of ["softdevice", "bootloader", "softdevice_bootloader"]) { + for (var type of ["softdevice", "bootloader", "softdevice_bootloader"]) { if (manifest[type]) { return updateFirmware(dfu, package, manifest[type], device, type); } diff --git a/gulpfile.js b/gulpfile.js index bc5530c..ef5942d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,10 +1,14 @@ -var gulp = require('gulp'); -var jshint = require('gulp-jshint'); +var gulp = require("gulp"); +var eslint = require("gulp-eslint"); -gulp.task('lint', function() { - return gulp.src(['dist/*.js']) - .pipe(jshint()) - .pipe(jshint.reporter('default')); +gulp.task("lint", function() { + return gulp.src([ + "dist/*.js", + "examples/*.js" + ]) + .pipe(eslint(".eslintrc")) + .pipe(eslint.format()) + .pipe(eslint.failOnError()); }); -gulp.task('default', ['lint']); +gulp.task("default", ["lint"]); diff --git a/package.json b/package.json index 31a42af..8052605 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "web-bluetooth" ], "scripts": { - "example": "node examples/secure_dfu_node.js" + "example": "node examples/secure_dfu_node.js", + "gulp": "gulp" }, "dependencies": { "bleat": "^0.1.3" @@ -29,7 +30,7 @@ "devDependencies": { "crc-32": "^1.0.2", "gulp": "^3.9.1", - "gulp-jshint": "^1.9.0", + "gulp-eslint": "^3.0.1", "jszip": "^3.1.3", "progress": "^2.0.0" }