From d4f15c0a09750bdb42e5631ffc412c28b9f09174 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Wed, 1 May 2019 21:19:29 -0400 Subject: [PATCH] chore: remove node6 support and upgrade codebase (#1851) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - use async functions - use Object.entries/Object.values - use Object spreading BREAKING CHANGE: bindings now use async functions so they’ll never throw, only reject --- .eslintrc | 2 +- .generators/lib/{{dashCase name}}.js | 2 +- .generators/package.json | 2 +- .travis.yml | 4 - appveyor.yml | 2 - docs/api-binding-abstract.md | 1 - package.json | 2 +- packages/binding-abstract/binding-abstract.js | 75 +++--- packages/binding-abstract/package.json | 2 +- packages/binding-mock/binding-mock.js | 220 ++++++++---------- packages/binding-mock/package.json | 2 +- packages/bindings/lib/bindings-test.js | 12 +- .../lib/bindings-win32-sn-parser-test.js | 6 +- packages/bindings/lib/darwin.js | 111 +++++---- packages/bindings/lib/linux.js | 109 +++++---- packages/bindings/lib/poller.js | 4 +- packages/bindings/lib/unix-read.js | 74 +++--- packages/bindings/lib/unix-write.js | 83 ++++--- packages/bindings/lib/util.js | 20 -- packages/bindings/lib/win32-sn-parser.js | 3 +- packages/bindings/lib/win32.js | 135 ++++++----- packages/bindings/package.json | 2 +- packages/list/package.json | 2 +- packages/parser-byte-length/byte-length.js | 2 +- packages/parser-byte-length/package.json | 2 +- packages/parser-cctalk/cctalk.js | 2 +- packages/parser-cctalk/package.json | 2 +- packages/parser-delimiter/delimiter.js | 2 +- packages/parser-delimiter/package.json | 2 +- .../inter-byte-timeout.js | 4 +- .../parser-inter-byte-timeout/package.json | 2 +- packages/parser-readline/package.json | 2 +- packages/parser-readline/readline.js | 12 +- packages/parser-ready/package.json | 2 +- packages/parser-ready/ready.js | 2 +- packages/parser-regex/package.json | 2 +- packages/parser-regex/regex.js | 12 +- packages/parser-slip-encoder/package.json | 2 +- packages/parser-slip-encoder/slip-encoder.js | 2 +- packages/repl/package.json | 2 +- packages/serialport/package.json | 2 +- .../serialport/test-manual/many-writes.js | 6 +- packages/stream/package.json | 2 +- packages/stream/stream.js | 6 +- packages/stream/stream.test.js | 3 +- packages/terminal/package.json | 2 +- test/initializers/test-config.js | 2 +- 47 files changed, 453 insertions(+), 501 deletions(-) delete mode 100644 packages/bindings/lib/util.js diff --git a/.eslintrc b/.eslintrc index 5e411934e..86a7c6526 100644 --- a/.eslintrc +++ b/.eslintrc @@ -19,7 +19,7 @@ "es6": true }, "parserOptions": { - "ecmaVersion": 6, + "ecmaVersion": 2018, "ecmaFeatures": { "jsx": true } diff --git a/.generators/lib/{{dashCase name}}.js b/.generators/lib/{{dashCase name}}.js index 9d5fe5607..cc6ba2c6d 100644 --- a/.generators/lib/{{dashCase name}}.js +++ b/.generators/lib/{{dashCase name}}.js @@ -1,4 +1,4 @@ -const Transform = require('stream').Transform +const { Transform } = require('stream') const debug = require('debug')('serialport/{{dashCase name}}') /** diff --git a/.generators/package.json b/.generators/package.json index 7acbe0e9f..96956b075 100644 --- a/.generators/package.json +++ b/.generators/package.json @@ -6,7 +6,7 @@ "debug": "^4.1.1" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/.travis.yml b/.travis.yml index b599b061b..d4cccc98f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,12 +19,8 @@ env: global: - secure: "L+AGMJc5NAsuym+xzB4FWj0c2rCobosixkoxLBhDBVkLiYsMtfS9y1w8Xz0pbWKJnJAH9tfwHluu5aX2qYk2HbreSyNzy8hbPW+9RbSyAQexeiZG4mLuDEz0xvlpCCQBsS1OfMypQk0/JvL4oA9B/xasrpkeVuPI7dwAz2WcFms=" matrix: - - TRAVIS_NODE_VERSION="6" - - TRAVIS_NODE_VERSION="6" ARCH="x86" - BINARY_BUILDER="true" TRAVIS_NODE_VERSION="8" - BINARY_BUILDER="true" TRAVIS_NODE_VERSION="8" ARCH="x86" - - TRAVIS_NODE_VERSION="9" - - TRAVIS_NODE_VERSION="9" ARCH="x86" - TRAVIS_NODE_VERSION="10" - TRAVIS_NODE_VERSION="12" matrix: diff --git a/appveyor.yml b/appveyor.yml index b02ec1621..956251db7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,10 +6,8 @@ environment: secure: iDcAJCYgJK4tffyzEHbMVR8DatJcIN8eY0h29p9JQkl43TcEcm6Z6JLOBpGDk1MJ matrix: - - nodejs_version: "6" - nodejs_version: "8" binary_builder: "true" - - nodejs_version: "9" - nodejs_version: "10" - nodejs_version: "12" diff --git a/docs/api-binding-abstract.md b/docs/api-binding-abstract.md index ea834e579..1edfbddd7 100644 --- a/docs/api-binding-abstract.md +++ b/docs/api-binding-abstract.md @@ -85,7 +85,6 @@ The in progress writes must error when the port is closed with an error object t /** * Drain waits until all output data is transmitted to the serial port. An in progress write should be completed before this returns. * @returns {Promise} Resolves once the drain operation finishes. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. */ drain(): Promise } diff --git a/package.json b/package.json index e272de382..3b5782f80 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "0.0.0", "description": "Node.js packages to access serial ports, process data from them and speak many protocols", "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "license": "MIT", "scripts": { diff --git a/packages/binding-abstract/binding-abstract.js b/packages/binding-abstract/binding-abstract.js index daead82c1..b53deaff6 100644 --- a/packages/binding-abstract/binding-abstract.js +++ b/packages/binding-abstract/binding-abstract.js @@ -25,9 +25,8 @@ class AbstractBinding { * Retrieves a list of available serial ports with metadata. The `comName` must be guaranteed, and all other fields should be undefined if unavailable. The `comName` is either the path or an identifier (eg `COM1`) used to open the serialport. * @returns {Promise} resolves to an array of port [info objects](#module_serialport--SerialPort.list). */ - static list() { + static async list() { debug('list') - return Promise.resolve() } constructor(opt) { @@ -41,9 +40,9 @@ class AbstractBinding { * @param {string} path the path or com port to open * @param {openOptions} options openOptions for the serialport * @returns {Promise} Resolves after the port is opened and configured. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - open(path, options) { + async open(path, options) { if (!path) { throw new TypeError('"path" is not a valid port') } @@ -54,22 +53,20 @@ class AbstractBinding { debug('open') if (this.isOpen) { - return Promise.reject(new Error('Already open')) + throw new Error('Already open') } - return Promise.resolve() } /** * Closes an open connection * @returns {Promise} Resolves once the connection is closed. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - close() { + async close() { debug('close') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } /** @@ -81,9 +78,9 @@ The in progress reads must error when the port is closed with an error object th * @param {integer} offset The offset in the buffer to start writing at. * @param {integer} length Specifies the maximum number of bytes to read. * @returns {Promise} Resolves with the number of bytes read after a read operation. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - read(buffer, offset, length) { + async read(buffer, offset, length) { if (!Buffer.isBuffer(buffer)) { throw new TypeError('"buffer" is not a Buffer') } @@ -98,13 +95,12 @@ The in progress reads must error when the port is closed with an error object th debug('read') if (buffer.length < offset + length) { - return Promise.reject(new Error('buffer is too small')) + throw new Error('buffer is too small') } if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } /** @@ -114,18 +110,17 @@ The in progress writes must error when the port is closed with an error object t * @param {buffer} buffer - Accepts a [`Buffer`](http://nodejs.org/api/buffer.html) object. * @returns {Promise} Resolves after the data is passed to the operating system for writing. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - write(buffer) { + async write(buffer) { if (!Buffer.isBuffer(buffer)) { throw new TypeError('"buffer" is not a Buffer') } debug('write', buffer.length, 'bytes') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } /** @@ -133,9 +128,9 @@ The in progress writes must error when the port is closed with an error object t * @param {object=} options Only supports `baudRate`. * @param {number=} [options.baudRate] If provided a baud rate that the bindings do not support, it should reject. * @returns {Promise} Resolves once the port's baud rate changes. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - update(options) { + async update(options) { if (typeof options !== 'object') { throw TypeError('"options" is not an object') } @@ -146,9 +141,8 @@ The in progress writes must error when the port is closed with an error object t debug('update') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } /** @@ -160,70 +154,65 @@ The in progress writes must error when the port is closed with an error object t * @param {Boolean} [options.dtr=true] flag for dtr * @param {Boolean} [options.rts=true] flag for rts * @returns {Promise} Resolves once the port's flags are set. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - set(options) { + async set(options) { if (typeof options !== 'object') { throw new TypeError('"options" is not an object') } debug('set') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } /** * Get the control flags (CTS, DSR, DCD) on the open port. * @returns {Promise} Resolves with the retrieved flags. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - get() { + async get() { debug('get') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } /** * Get the OS reported baud rate for the open port. * Used mostly for debugging custom baud rates. * @returns {Promise} Resolves with the current baud rate. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - getBaudRate() { + async getBaudRate() { debug('getbaudRate') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } /** * Flush (discard) data received but not read, and written but not transmitted. * @returns {Promise} Resolves once the flush operation finishes. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - flush() { + async flush() { debug('flush') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } /** * Drain waits until all output data is transmitted to the serial port. An in progress write should be completed before this returns. * @returns {Promise} Resolves once the drain operation finishes. - * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + * @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected. */ - drain() { + async drain() { debug('drain') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return Promise.resolve() } } diff --git a/packages/binding-abstract/package.json b/packages/binding-abstract/package.json index b9c1bfffe..46d0b7a1f 100644 --- a/packages/binding-abstract/package.json +++ b/packages/binding-abstract/package.json @@ -9,7 +9,7 @@ "debug": "^4.1.1" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/binding-mock/binding-mock.js b/packages/binding-mock/binding-mock.js index f5e949974..4d18548a6 100644 --- a/packages/binding-mock/binding-mock.js +++ b/packages/binding-mock/binding-mock.js @@ -30,17 +30,15 @@ class MockBinding extends AbstractBinding { // Create a mock port static createPort(path, opt) { serialNumber++ - opt = Object.assign( - { - echo: false, - record: false, - readyData: Buffer.from('READY'), - manufacturer: 'The J5 Robotics Company', - vendorId: undefined, - productId: undefined, - }, - opt - ) + opt = { + echo: false, + record: false, + readyData: Buffer.from('READY'), + manufacturer: 'The J5 Robotics Company', + vendorId: undefined, + productId: undefined, + ...opt, + } ports[path] = { data: Buffer.alloc(0), @@ -60,11 +58,8 @@ class MockBinding extends AbstractBinding { debug(serialNumber, 'created port', JSON.stringify({ path, opt })) } - static list() { - const info = Object.keys(ports).map(path => { - return ports[path].info - }) - return Promise.resolve(info) + static async list() { + return Object.values(ports).map(port => port.info) } // Emit data on a mock port @@ -83,89 +78,82 @@ class MockBinding extends AbstractBinding { } } - open(path, opt) { + async open(path, opt) { debug(null, `opening path ${path}`) const port = (this.port = ports[path]) - return super - .open(path, opt) - .then(resolveNextTick) - .then(() => { - if (!port) { - return Promise.reject(new Error(`Port does not exist - please call MockBinding.createPort('${path}') first`)) - } - this.serialNumber = port.info.serialNumber + await super.open(path, opt) + await resolveNextTick() + if (!port) { + throw new Error(`Port does not exist - please call MockBinding.createPort('${path}') first`) + } + this.serialNumber = port.info.serialNumber - if (port.openOpt && port.openOpt.lock) { - return Promise.reject(new Error('Port is locked cannot open')) - } + if (port.openOpt && port.openOpt.lock) { + throw new Error('Port is locked cannot open') + } - if (this.isOpen) { - return Promise.reject(new Error('Open: binding is already open')) - } + if (this.isOpen) { + throw new Error('Open: binding is already open') + } - port.openOpt = Object.assign({}, opt) - this.isOpen = true - debug(this.serialNumber, 'port is open') - if (port.echo) { - process.nextTick(() => { - if (this.isOpen) { - debug(this.serialNumber, 'emitting ready data') - this.emitData(port.readyData) - } - }) + port.openOpt = { ...opt } + this.isOpen = true + debug(this.serialNumber, 'port is open') + if (port.echo) { + process.nextTick(() => { + if (this.isOpen) { + debug(this.serialNumber, 'emitting ready data') + this.emitData(port.readyData) } }) + } } - close() { + async close() { const port = this.port debug(this.serialNumber, 'closing port') if (!port) { - return Promise.reject(new Error('already closed')) - } - - return super.close().then(() => { - delete port.openOpt - // reset data on close - port.data = Buffer.alloc(0) - debug(this.serialNumber, 'port is closed') - delete this.port - delete this.serialNumber - this.isOpen = false - if (this.pendingRead) { - this.pendingRead(new Error('port is closed')) - } - }) + throw new Error('already closed') + } + + await super.close() + delete port.openOpt + // reset data on close + port.data = Buffer.alloc(0) + debug(this.serialNumber, 'port is closed') + delete this.port + delete this.serialNumber + this.isOpen = false + if (this.pendingRead) { + this.pendingRead(new Error('port is closed')) + } } - read(buffer, offset, length) { + async read(buffer, offset, length) { debug(this.serialNumber, 'reading', length, 'bytes') - return super - .read(buffer, offset, length) - .then(resolveNextTick) - .then(() => { - if (!this.isOpen) { - throw new Error('Read canceled') - } - if (this.port.data.length <= 0) { - return new Promise((resolve, reject) => { - this.pendingRead = err => { - if (err) { - return reject(err) - } - this.read(buffer, offset, length).then(resolve, reject) - } - }) + await super.read(buffer, offset, length) + await resolveNextTick() + if (!this.isOpen) { + throw new Error('Read canceled') + } + if (this.port.data.length <= 0) { + return new Promise((resolve, reject) => { + this.pendingRead = err => { + if (err) { + return reject(err) + } + this.read(buffer, offset, length).then(resolve, reject) } - const data = this.port.data.slice(0, length) - const readLength = data.copy(buffer, offset) - this.port.data = this.port.data.slice(length) - debug(this.serialNumber, 'read', readLength, 'bytes') - return readLength }) + } + const data = this.port.data.slice(0, length) + const readLength = data.copy(buffer, offset) + this.port.data = this.port.data.slice(length) + debug(this.serialNumber, 'read', readLength, 'bytes') + return readLength } - write(buffer) { + async write(buffer) { debug(this.serialNumber, 'writing') if (this.writeOperation) { throw new Error('Overlapping writes are not supported and should be queued by the serialport object') @@ -173,7 +161,7 @@ class MockBinding extends AbstractBinding { this.writeOperation = super .write(buffer) .then(resolveNextTick) - .then(() => { + .then(async () => { if (!this.isOpen) { throw new Error('Write canceled') } @@ -194,57 +182,45 @@ class MockBinding extends AbstractBinding { return this.writeOperation } - update(opt) { - return super - .update(opt) - .then(resolveNextTick) - .then(() => { - this.port.openOpt.baudRate = opt.baudRate - }) + async update(opt) { + await super.update(opt) + await resolveNextTick() + this.port.openOpt.baudRate = opt.baudRate } - set(opt) { - return super.set(opt).then(resolveNextTick) + async set(opt) { + await super.set(opt) + await resolveNextTick() } - get() { - return super - .get() - .then(resolveNextTick) - .then(() => { - return { - cts: true, - dsr: false, - dcd: false, - } - }) + async get() { + await super.get() + await resolveNextTick() + return { + cts: true, + dsr: false, + dcd: false, + } } - getBaudRate() { - return super - .getBaudRate() - .then(resolveNextTick) - .then(() => { - return { - baudRate: this.port.openOpt.baudRate, - } - }) + async getBaudRate() { + await super.getBaudRate() + await resolveNextTick() + return { + baudRate: this.port.openOpt.baudRate, + } } - flush() { - return super - .flush() - .then(resolveNextTick) - .then(() => { - this.port.data = Buffer.alloc(0) - }) + async flush() { + await super.flush() + await resolveNextTick() + this.port.data = Buffer.alloc(0) } - drain() { - return super - .drain() - .then(() => this.writeOperation) - .then(() => resolveNextTick()) + async drain() { + await super.drain() + await this.writeOperation + await resolveNextTick() } } diff --git a/packages/binding-mock/package.json b/packages/binding-mock/package.json index 7b188753d..491de02b4 100644 --- a/packages/binding-mock/package.json +++ b/packages/binding-mock/package.json @@ -10,7 +10,7 @@ "debug": "^4.1.1" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/bindings/lib/bindings-test.js b/packages/bindings/lib/bindings-test.js index 0affe02f3..38535ab04 100644 --- a/packages/bindings/lib/bindings-test.js +++ b/packages/bindings/lib/bindings-test.js @@ -179,9 +179,7 @@ function testBinding(bindingName, Binding, testPort) { } it('cannot open if already open', () => { - const options = Object.assign({}, defaultOpenOptions, { - lock: false, - }) + const options = { ...defaultOpenOptions, lock: false } return binding.open(testPort, options).then(() => { return binding.open(testPort, options).catch(err => { assert.instanceOf(err, Error) @@ -200,9 +198,7 @@ function testBinding(bindingName, Binding, testPort) { describe('arbitrary baud rates', () => { ;[25000, 1000000, 250000].forEach(testBaud => { describe(`${testBaud} baud`, () => { - const customRates = Object.assign({}, defaultOpenOptions, { - baudRate: testBaud, - }) + const customRates = { ...defaultOpenOptions, baudRate: testBaud } testFeature(`baudrate.${testBaud}`, `opens at ${testBaud} baud`, () => { return binding.open(testPort, customRates).then(() => { assert.equal(binding.isOpen, true) @@ -242,9 +238,7 @@ function testBinding(bindingName, Binding, testPort) { }) testFeature('open.unlock', 'can unlock the port', () => { - const noLock = Object.assign({}, defaultOpenOptions, { - lock: false, - }) + const noLock = { ...defaultOpenOptions, lock: false } const binding2 = new Binding({ disconnect }) return binding diff --git a/packages/bindings/lib/bindings-win32-sn-parser-test.js b/packages/bindings/lib/bindings-win32-sn-parser-test.js index 0e2818f27..17b4a05fe 100644 --- a/packages/bindings/lib/bindings-win32-sn-parser-test.js +++ b/packages/bindings/lib/bindings-win32-sn-parser-test.js @@ -28,10 +28,10 @@ const devices = { } describe('serialNumParser', () => { - Object.keys(devices).forEach(device => { + Object.entries(devices).forEach(([device, info]) => { it(`parses pnp id for ${device}`, () => { - const pnpId = devices[device].pnpId - const serialNumber = devices[device].serialNumber + const pnpId = info.pnpId + const serialNumber = info.serialNumber assert.equal(serialNumParser(pnpId), serialNumber) }) }) diff --git a/packages/bindings/lib/darwin.js b/packages/bindings/lib/darwin.js index 7bedeec2d..aa6d4626b 100644 --- a/packages/bindings/lib/darwin.js +++ b/packages/bindings/lib/darwin.js @@ -1,7 +1,7 @@ +const { promisify } = require('util') const binding = require('bindings')('bindings.node') const AbstractBinding = require('@serialport/binding-abstract') const Poller = require('./poller') -const promisify = require('./util').promisify const unixRead = require('./unix-read') const unixWrite = require('./unix-write') @@ -10,17 +10,27 @@ const defaultBindingOptions = Object.freeze({ vtime: 0, }) +const asyncList = promisify(binding.list) +const asyncOpen = promisify(binding.open) +const asyncClose = promisify(binding.close) +const asyncUpdate = promisify(binding.update) +const asyncSet = promisify(binding.set) +const asyncGet = promisify(binding.get) +const asyncGetBaudRate = promisify(binding.getBaudRate) +const asyncDrain = promisify(binding.drain) +const asyncFlush = promisify(binding.flush) + /** * The Darwin binding layer for OSX */ class DarwinBinding extends AbstractBinding { static list() { - return promisify(binding.list)() + return asyncList() } constructor(opt) { super(opt) - this.bindingOptions = Object.assign({}, defaultBindingOptions, opt.bindingOptions || {}) + this.bindingOptions = { ...defaultBindingOptions, ...opt.bindingOptions } this.fd = null this.writeOperation = null } @@ -29,73 +39,72 @@ class DarwinBinding extends AbstractBinding { return this.fd !== null } - open(path, options) { - return super - .open(path, options) - .then(() => { - this.openOptions = Object.assign({}, this.bindingOptions, options) - return promisify(binding.open)(path, this.openOptions) - }) - .then(fd => { - this.fd = fd - this.poller = new Poller(fd) - }) + async open(path, options) { + await super.open(path, options) + this.openOptions = { ...this.bindingOptions, ...options } + const fd = await asyncOpen(path, this.openOptions) + this.fd = fd + this.poller = new Poller(fd) } - close() { - return super.close().then(() => { - const fd = this.fd - this.poller.stop() - this.poller.destroy() - this.poller = null - this.openOptions = null - this.fd = null - return promisify(binding.close)(fd) - }) + async close() { + await super.close() + const fd = this.fd + this.poller.stop() + this.poller.destroy() + this.poller = null + this.openOptions = null + this.fd = null + return asyncClose(fd) } - read(buffer, offset, length) { - return super.read(buffer, offset, length).then(() => unixRead.call(this, buffer, offset, length)) + async read(buffer, offset, length) { + await super.read(buffer, offset, length) + return unixRead.call(this, buffer, offset, length) } - write(buffer) { - if (buffer.length > 0) { - this.writeOperation = super - .write(buffer) - .then(() => unixWrite.call(this, buffer)) - .then(() => { - this.writeOperation = null - }) - return this.writeOperation + async write(buffer) { + if (buffer.length === 0) { + return } - return Promise.resolve() + this.writeOperation = super + .write(buffer) + .then(() => unixWrite.call(this, buffer)) + .then(() => { + this.writeOperation = null + }) + return this.writeOperation } - update(options) { - return super.update(options).then(() => promisify(binding.update)(this.fd, options)) + async update(options) { + await super.update(options) + return asyncUpdate(this.fd, options) } - set(options) { - return super.set(options).then(() => promisify(binding.set)(this.fd, options)) + async set(options) { + await super.set(options) + return asyncSet(this.fd, options) } - get() { - return super.get().then(() => promisify(binding.get)(this.fd)) + async get() { + await super.get() + return asyncGet(this.fd) } - getBaudRate() { - return super.get().then(() => promisify(binding.getBaudRate)(this.fd)) + async getBaudRate() { + await super.get() + return asyncGetBaudRate(this.fd) } - drain() { - return super - .drain() - .then(() => Promise.resolve(this.writeOperation)) - .then(() => promisify(binding.drain)(this.fd)) + async drain() { + await super.drain() + await this.writeOperation + return asyncDrain(this.fd) } - flush() { - return super.flush().then(() => promisify(binding.flush)(this.fd)) + async flush() { + await super.flush() + return asyncFlush(this.fd) } } diff --git a/packages/bindings/lib/linux.js b/packages/bindings/lib/linux.js index d0b11e0aa..86b4d3d58 100644 --- a/packages/bindings/lib/linux.js +++ b/packages/bindings/lib/linux.js @@ -1,8 +1,8 @@ +const { promisify } = require('util') const binding = require('bindings')('bindings.node') const AbstractBinding = require('@serialport/binding-abstract') const linuxList = require('./linux-list') const Poller = require('./poller') -const promisify = require('./util').promisify const unixRead = require('./unix-read') const unixWrite = require('./unix-write') @@ -10,6 +10,16 @@ const defaultBindingOptions = Object.freeze({ vmin: 1, vtime: 0, }) + +const asyncOpen = promisify(binding.open) +const asyncClose = promisify(binding.close) +const asyncUpdate = promisify(binding.update) +const asyncSet = promisify(binding.set) +const asyncGet = promisify(binding.get) +const asyncGetBaudRate = promisify(binding.getBaudRate) +const asyncDrain = promisify(binding.drain) +const asyncFlush = promisify(binding.flush) + /** * The linux binding layer */ @@ -20,7 +30,7 @@ class LinuxBinding extends AbstractBinding { constructor(opt) { super(opt) - this.bindingOptions = Object.assign({}, defaultBindingOptions, opt.bindingOptions || {}) + this.bindingOptions = { ...defaultBindingOptions, ...opt.bindingOptions } this.fd = null this.writeOperation = null } @@ -29,73 +39,72 @@ class LinuxBinding extends AbstractBinding { return this.fd !== null } - open(path, options) { - return super - .open(path, options) - .then(() => { - this.openOptions = Object.assign({}, this.bindingOptions, options) - return promisify(binding.open)(path, this.openOptions) - }) - .then(fd => { - this.fd = fd - this.poller = new Poller(fd) - }) + async open(path, options) { + await super.open(path, options) + this.openOptions = { ...this.bindingOptions, ...options } + const fd = await asyncOpen(path, this.openOptions) + this.fd = fd + this.poller = new Poller(fd) } - close() { - return super.close().then(() => { - const fd = this.fd - this.poller.stop() - this.poller.destroy() - this.poller = null - this.openOptions = null - this.fd = null - return promisify(binding.close)(fd) - }) + async close() { + await super.close() + const fd = this.fd + this.poller.stop() + this.poller.destroy() + this.poller = null + this.openOptions = null + this.fd = null + return asyncClose(fd) } - read(buffer, offset, length) { - return super.read(buffer, offset, length).then(() => unixRead.call(this, buffer, offset, length)) + async read(buffer, offset, length) { + await super.read(buffer, offset, length) + return unixRead.call(this, buffer, offset, length) } - write(buffer) { - if (buffer.length > 0) { - this.writeOperation = super - .write(buffer) - .then(() => unixWrite.call(this, buffer)) - .then(() => { - this.writeOperation = null - }) - return this.writeOperation + async write(buffer) { + if (buffer.length === 0) { + return } - return Promise.resolve() + this.writeOperation = super + .write(buffer) + .then(() => unixWrite.call(this, buffer)) + .then(() => { + this.writeOperation = null + }) + return this.writeOperation } - update(options) { - return super.update(options).then(() => promisify(binding.update)(this.fd, options)) + async update(options) { + await super.update(options) + return asyncUpdate(this.fd, options) } - set(options) { - return super.set(options).then(() => promisify(binding.set)(this.fd, options)) + async set(options) { + await super.set(options) + return asyncSet(this.fd, options) } - get() { - return super.get().then(() => promisify(binding.get)(this.fd)) + async get() { + await super.get() + return asyncGet(this.fd) } - getBaudRate() { - return super.getBaudRate().then(() => promisify(binding.getBaudRate)(this.fd)) + async getBaudRate() { + await super.get() + return asyncGetBaudRate(this.fd) } - drain() { - return super - .drain() - .then(() => Promise.resolve(this.writeOperation)) - .then(() => promisify(binding.drain)(this.fd)) + async drain() { + await super.drain() + await this.writeOperation + return asyncDrain(this.fd) } - flush() { - return super.flush().then(() => promisify(binding.flush)(this.fd)) + async flush() { + await super.flush() + return asyncFlush(this.fd) } } diff --git a/packages/bindings/lib/poller.js b/packages/bindings/lib/poller.js index 0fac3e184..80e4e6e5f 100644 --- a/packages/bindings/lib/poller.js +++ b/packages/bindings/lib/poller.js @@ -45,7 +45,7 @@ class Poller extends EventEmitter { * @param {string} event ('readable'|'writable'|'disconnect') * @returns {Poller} returns itself */ - once(event) { + once(event, callback) { switch (event) { case 'readable': this.poll(EVENTS.UV_READABLE) @@ -57,7 +57,7 @@ class Poller extends EventEmitter { this.poll(EVENTS.UV_DISCONNECT) break } - return EventEmitter.prototype.once.apply(this, arguments) + return super.once(event, callback) } /** diff --git a/packages/bindings/lib/unix-read.js b/packages/bindings/lib/unix-read.js index 803aa3cc4..ccb8b5228 100644 --- a/packages/bindings/lib/unix-read.js +++ b/packages/bindings/lib/unix-read.js @@ -1,51 +1,51 @@ const fs = require('fs') const debug = require('debug') const logger = debug('serialport/bindings/unixRead') +const { promisify } = require('util') -module.exports = function unixRead(buffer, offset, length) { +const readAsync = promisify(fs.read) + +const readable = binding => { + return new Promise((resolve, reject) => { + binding.poller.once('readable', err => (err ? reject(err) : resolve())) + }) +} + +module.exports = async function unixRead(buffer, offset, length) { logger('Starting read') if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return new Promise((resolve, reject) => { - fs.read(this.fd, buffer, offset, length, null, (err, bytesRead) => { - if (err && (err.code === 'EAGAIN' || err.code === 'EWOULDBLOCK' || err.code === 'EINTR')) { - if (!this.isOpen) { - return reject(new Error('Port is not open')) - } - logger('waiting for readable because of code:', err.code) - this.poller.once('readable', err => { - if (err) { - return reject(err) - } - resolve(this.read(buffer, offset, length)) - }) - return - } - const disconnectError = - err && - (err.code === 'EBADF' || // Bad file number means we got closed - err.code === 'ENXIO' || // No such device or address probably usb disconnect - err.code === 'UNKNOWN' || - err.errno === -1) // generic error + try { + const bytesRead = await readAsync(this.fd, buffer, offset, length, null) + if (bytesRead === 0) { + return this.read(buffer, offset, length) + } - if (disconnectError) { - err.disconnect = true - logger('disconnecting', err) + logger('Finished read', bytesRead, 'bytes') + return bytesRead + } catch (err) { + if (err.code === 'EAGAIN' || err.code === 'EWOULDBLOCK' || err.code === 'EINTR') { + if (!this.isOpen) { + throw new Error('Port is not open') } + logger('waiting for readable because of code:', err.code) + await readable(this) + return this.read(buffer, offset, length) + } - if (err) { - return reject(err) - } + const disconnectError = + err.code === 'EBADF' || // Bad file number means we got closed + err.code === 'ENXIO' || // No such device or address probably usb disconnect + err.code === 'UNKNOWN' || + err.errno === -1 // generic error - if (bytesRead === 0) { - resolve(this.read(buffer, offset, length)) - return - } + if (disconnectError) { + err.disconnect = true + logger('disconnecting', err) + } - logger('Finished read', bytesRead, 'bytes') - resolve(bytesRead) - }) - }) + throw err + } } diff --git a/packages/bindings/lib/unix-write.js b/packages/bindings/lib/unix-write.js index 08d4a9f4c..41ed7a29c 100644 --- a/packages/bindings/lib/unix-write.js +++ b/packages/bindings/lib/unix-write.js @@ -1,58 +1,57 @@ const fs = require('fs') const debug = require('debug') const logger = debug('serialport/bindings/unixWrite') +const { promisify } = require('util') -module.exports = function unixWrite(buffer, offset) { +const writeAsync = promisify(fs.write) + +const writable = binding => { + return new Promise((resolve, reject) => { + binding.poller.once('writable', err => (err ? reject(err) : resolve())) + }) +} + +module.exports = async function unixWrite(buffer, offset) { offset = offset || 0 const bytesToWrite = buffer.length - offset logger('Starting write', buffer.length, 'bytes offset', offset, 'bytesToWrite', bytesToWrite) if (!this.isOpen) { - return Promise.reject(new Error('Port is not open')) + throw new Error('Port is not open') } - return new Promise((resolve, reject) => { - fs.write(this.fd, buffer, offset, bytesToWrite, (err, bytesWritten) => { - logger('write returned', err, bytesWritten) - if (err && (err.code === 'EAGAIN' || err.code === 'EWOULDBLOCK' || err.code === 'EINTR')) { - if (!this.isOpen) { - return reject(new Error('Port is not open')) - } - logger('waiting for writable because of code:', err.code) - this.poller.once('writable', err => { - if (err) { - return reject(err) - } - resolve(unixWrite.call(this, buffer, offset)) - }) - return + try { + const bytesWritten = await writeAsync(this.fd, buffer, offset, bytesToWrite) + logger('write returned: wrote', bytesWritten, 'bytes') + if (bytesWritten + offset < buffer.length) { + if (!this.isOpen) { + throw new Error('Port is not open') } + return resolve(unixWrite.call(this, buffer, bytesWritten + offset)) + } - const disconnectError = - err && - (err.code === 'EBADF' || // Bad file number means we got closed - err.code === 'ENXIO' || // No such device or address probably usb disconnect - err.code === 'UNKNOWN' || - err.errno === -1) // generic error - - if (disconnectError) { - err.disconnect = true - logger('disconnecting', err) + logger('Finished writing', bytesWritten + offset, 'bytes') + } catch (err) { + logger('write errored', err) + if (err.code === 'EAGAIN' || err.code === 'EWOULDBLOCK' || err.code === 'EINTR') { + if (!this.isOpen) { + throw new Error('Port is not open') } + logger('waiting for writable because of code:', err.code) + await writable(this) + return unixWrite.call(this, buffer, offset) + } - if (err) { - logger('error', err) - return reject(err) - } + const disconnectError = + err.code === 'EBADF' || // Bad file number means we got closed + err.code === 'ENXIO' || // No such device or address probably usb disconnect + err.code === 'UNKNOWN' || + err.errno === -1 // generic error - logger('wrote', bytesWritten, 'bytes') - if (bytesWritten + offset < buffer.length) { - if (!this.isOpen) { - return reject(new Error('Port is not open')) - } - return resolve(unixWrite.call(this, buffer, bytesWritten + offset)) - } + if (disconnectError) { + err.disconnect = true + logger('disconnecting', err) + } - logger('Finished writing', bytesWritten + offset, 'bytes') - resolve() - }) - }) + logger('error', err) + throw err + } } diff --git a/packages/bindings/lib/util.js b/packages/bindings/lib/util.js deleted file mode 100644 index 04bea6f40..000000000 --- a/packages/bindings/lib/util.js +++ /dev/null @@ -1,20 +0,0 @@ -function promisify(func) { - if (typeof func !== 'function') { - throw new Error('"func" must be a function') - } - return function(...args) { - return new Promise((resolve, reject) => { - args.push((err, data) => { - if (err) { - return reject(err) - } - resolve(data) - }) - func(...args) - }) - } -} - -module.exports = { - promisify, -} diff --git a/packages/bindings/lib/win32-sn-parser.js b/packages/bindings/lib/win32-sn-parser.js index cf8c12b1e..d26acb19f 100644 --- a/packages/bindings/lib/win32-sn-parser.js +++ b/packages/bindings/lib/win32-sn-parser.js @@ -4,8 +4,7 @@ module.exports = function(pnpId) { if (!pnpId) { return null } - for (let index = 0; index < PARSERS.length; index++) { - const parser = PARSERS[index] + for (const parser of PARSERS) { const sn = pnpId.match(parser) if (sn) { return sn[1] diff --git a/packages/bindings/lib/win32.js b/packages/bindings/lib/win32.js index f435b4d17..12a0c187c 100644 --- a/packages/bindings/lib/win32.js +++ b/packages/bindings/lib/win32.js @@ -1,30 +1,43 @@ const binding = require('bindings')('bindings.node') const AbstractBinding = require('@serialport/binding-abstract') -const promisify = require('./util').promisify +const { promisify } = require('util') const serialNumParser = require('./win32-sn-parser') +const asyncList = promisify(binding.list) +const asyncOpen = promisify(binding.open) +const asyncRead = promisify(binding.read) +const asyncWrite = promisify(binding.write) +const asyncUpdate = promisify(binding.update) +const asyncSet = promisify(binding.set) +const asyncGet = promisify(binding.get) +const asyncGetBaudRate = promisify(binding.getBaudRate) +const asyncDrain = promisify(binding.drain) +const asyncFlush = promisify(binding.flush) + /** * The Windows binding layer */ class WindowsBinding extends AbstractBinding { - static list() { - return promisify(binding.list)().then(ports => { - // Grab the serial number from the pnp id - ports.forEach(port => { - if (port.pnpId && !port.serialNumber) { - const serialNumber = serialNumParser(port.pnpId) - if (serialNumber) { - port.serialNumber = serialNumber + static async list() { + const ports = await asyncList() + // Grab the serial number from the pnp id + return ports.map(port => { + if (port.pnpId && !port.serialNumber) { + const serialNumber = serialNumParser(port.pnpId) + if (serialNumber) { + return { + ...port, + serialNumber, } } - }) - return ports + } + return port }) } constructor(opt) { super(opt) - this.bindingOptions = Object.assign({}, opt.bindingOptions || {}) + this.bindingOptions = { ...opt.bindingOptions } this.fd = null this.writeOperation = null } @@ -33,76 +46,72 @@ class WindowsBinding extends AbstractBinding { return this.fd !== null } - open(path, options) { - return super - .open(path, options) - .then(() => { - this.openOptions = Object.assign({}, this.bindingOptions, options) - return promisify(binding.open)(path, this.openOptions) - }) - .then(fd => { - this.fd = fd - }) + async open(path, options) { + await super.open(path, options) + this.openOptions = { ...this.bindingOptions, ...options } + const fd = await asyncOpen(path, this.openOptions) + this.fd = fd } - close() { - return super.close().then(() => { - const fd = this.fd - this.fd = null - return promisify(binding.close)(fd) - }) + async close() { + await super.close() + const fd = this.fd + this.fd = null + return asyncClose(fd) } - read(buffer, offset, length) { - return super - .read(buffer, offset, length) - .then(() => promisify(binding.read)(this.fd, buffer, offset, length)) - .catch(err => { - if (!this.isOpen) { - err.canceled = true - } - throw err - }) + async read(buffer, offset, length) { + await super.read(buffer, offset, length) + return asyncRead(this.fd, buffer, offset, length).catch(err => { + if (!this.isOpen) { + err.canceled = true + } + throw err + }) } - write(buffer) { - if (buffer.length > 0) { - this.writeOperation = super - .write(buffer) - .then(() => promisify(binding.write)(this.fd, buffer)) - .then(() => { - this.writeOperation = null - }) - return this.writeOperation + async write(buffer) { + if (buffer.length === 0) { + return } - return Promise.resolve() + this.writeOperation = super + .write(buffer) + .then(() => asyncWrite(this.fd, buffer)) + .then(() => { + this.writeOperation = null + }) + return this.writeOperation } - update(options) { - return super.update(options).then(() => promisify(binding.update)(this.fd, options)) + async update(options) { + await super.update(options) + return asyncUpdate(this.fd, options) } - set(options) { - return super.set(options).then(() => promisify(binding.set)(this.fd, options)) + async set(options) { + await super.set(options) + return asyncSet(this.fd, options) } - get() { - return super.get().then(() => promisify(binding.get)(this.fd)) + async get() { + await super.get() + return asyncGet(this.fd) } - getBaudRate() { - return super.get().then(() => promisify(binding.getBaudRate)(this.fd)) + async getBaudRate() { + await super.get() + return asyncGetBaudRate(this.fd) } - drain() { - return super - .drain() - .then(() => Promise.resolve(this.writeOperation)) - .then(() => promisify(binding.drain)(this.fd)) + async drain() { + await super.drain() + await this.writeOperation + return asyncDrain(this.fd) } - flush() { - return super.flush().then(() => promisify(binding.flush)(this.fd)) + async flush() { + await super.flush() + return asyncFlush(this.fd) } } diff --git a/packages/bindings/package.json b/packages/bindings/package.json index 460f6fcbe..80fde4478 100644 --- a/packages/bindings/package.json +++ b/packages/bindings/package.json @@ -17,7 +17,7 @@ "@serialport/binding-mock": "^2.0.5" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "scripts": { "install": "prebuild-install --tag-prefix @serialport/bindings@ || node-gyp rebuild", diff --git a/packages/list/package.json b/packages/list/package.json index 71fc663bf..78b9560d4 100644 --- a/packages/list/package.json +++ b/packages/list/package.json @@ -10,7 +10,7 @@ "commander": "^2.13.0" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-byte-length/byte-length.js b/packages/parser-byte-length/byte-length.js index 803bb4458..a54e1929d 100644 --- a/packages/parser-byte-length/byte-length.js +++ b/packages/parser-byte-length/byte-length.js @@ -1,4 +1,4 @@ -const Transform = require('stream').Transform +const { Transform } = require('stream') /** * Emit data every number of bytes diff --git a/packages/parser-byte-length/package.json b/packages/parser-byte-length/package.json index 95d485c08..68eaece06 100644 --- a/packages/parser-byte-length/package.json +++ b/packages/parser-byte-length/package.json @@ -3,7 +3,7 @@ "version": "2.0.2", "main": "byte-length.js", "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-cctalk/cctalk.js b/packages/parser-cctalk/cctalk.js index d3e1642fb..f9217a341 100644 --- a/packages/parser-cctalk/cctalk.js +++ b/packages/parser-cctalk/cctalk.js @@ -1,4 +1,4 @@ -const Transform = require('stream').Transform +const { Transform } = require('stream') /** * Parse the CCTalk protocol diff --git a/packages/parser-cctalk/package.json b/packages/parser-cctalk/package.json index c86a22405..532b523eb 100644 --- a/packages/parser-cctalk/package.json +++ b/packages/parser-cctalk/package.json @@ -3,7 +3,7 @@ "version": "2.0.2", "main": "cctalk.js", "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-delimiter/delimiter.js b/packages/parser-delimiter/delimiter.js index 275ea06c7..1d101485c 100644 --- a/packages/parser-delimiter/delimiter.js +++ b/packages/parser-delimiter/delimiter.js @@ -1,4 +1,4 @@ -const Transform = require('stream').Transform +const { Transform } = require('stream') /** * A transform stream that emits data each time a byte sequence is received. diff --git a/packages/parser-delimiter/package.json b/packages/parser-delimiter/package.json index 40b8257e0..459e5ffb5 100644 --- a/packages/parser-delimiter/package.json +++ b/packages/parser-delimiter/package.json @@ -3,7 +3,7 @@ "main": "delimiter.js", "version": "2.0.2", "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-inter-byte-timeout/inter-byte-timeout.js b/packages/parser-inter-byte-timeout/inter-byte-timeout.js index 919710f1a..0183fe8e5 100644 --- a/packages/parser-inter-byte-timeout/inter-byte-timeout.js +++ b/packages/parser-inter-byte-timeout/inter-byte-timeout.js @@ -1,4 +1,4 @@ -const Transform = require('stream').Transform +const { Transform } = require('stream') /** * Emits data if there is a pause between packets for the specified amount of time. @@ -18,7 +18,7 @@ parser.on('data', console.log) // will emit data if there is a pause between pac class InterByteTimeoutParser extends Transform { constructor(options) { super() - options = Object.assign({ maxBufferSize: 65536 }, options) + options = { maxBufferSize: 65536, ...options } if (!options.interval) { throw new TypeError('"interval" is required') } diff --git a/packages/parser-inter-byte-timeout/package.json b/packages/parser-inter-byte-timeout/package.json index d1207cbc9..72227d53d 100644 --- a/packages/parser-inter-byte-timeout/package.json +++ b/packages/parser-inter-byte-timeout/package.json @@ -3,7 +3,7 @@ "version": "1.1.0", "main": "inter-byte-timeout.js", "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-readline/package.json b/packages/parser-readline/package.json index 08ce8971a..eccabf84f 100644 --- a/packages/parser-readline/package.json +++ b/packages/parser-readline/package.json @@ -6,7 +6,7 @@ "@serialport/parser-delimiter": "^2.0.2" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-readline/readline.js b/packages/parser-readline/readline.js index 24189c388..795f60843 100644 --- a/packages/parser-readline/readline.js +++ b/packages/parser-readline/readline.js @@ -13,13 +13,11 @@ parser.on('data', console.log) */ class ReadLineParser extends DelimiterParser { constructor(options) { - const opts = Object.assign( - { - delimiter: Buffer.from('\n', 'utf8'), - encoding: 'utf8', - }, - options - ) + const opts = { + delimiter: Buffer.from('\n', 'utf8'), + encoding: 'utf8', + ...options, + } if (typeof opts.delimiter === 'string') { opts.delimiter = Buffer.from(opts.delimiter, opts.encoding) diff --git a/packages/parser-ready/package.json b/packages/parser-ready/package.json index 181112138..233264a3e 100644 --- a/packages/parser-ready/package.json +++ b/packages/parser-ready/package.json @@ -3,7 +3,7 @@ "main": "ready.js", "version": "2.0.2", "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-ready/ready.js b/packages/parser-ready/ready.js index 29b94d92e..2f7415856 100644 --- a/packages/parser-ready/ready.js +++ b/packages/parser-ready/ready.js @@ -1,4 +1,4 @@ -const Transform = require('stream').Transform +const { Transform } = require('stream') /** * A transform stream that waits for a sequence of "ready" bytes before emitting a ready event and emitting data events diff --git a/packages/parser-regex/package.json b/packages/parser-regex/package.json index 75485d31e..2e18639f6 100644 --- a/packages/parser-regex/package.json +++ b/packages/parser-regex/package.json @@ -3,7 +3,7 @@ "main": "regex.js", "version": "2.0.2", "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-regex/regex.js b/packages/parser-regex/regex.js index c1f9d70a7..ad7175e10 100644 --- a/packages/parser-regex/regex.js +++ b/packages/parser-regex/regex.js @@ -1,4 +1,4 @@ -const Transform = require('stream').Transform +const { Transform } = require('stream') /** * A transform stream that uses a regular expression to split the incoming text upon. @@ -14,12 +14,10 @@ parser.on('data', console.log) */ class RegexParser extends Transform { constructor(options) { - const opts = Object.assign( - { - encoding: 'utf8', - }, - options - ) + const opts = { + encoding: 'utf8', + ...options, + } if (opts.regex === undefined) { throw new TypeError('"options.regex" must be a regular expression pattern or object') diff --git a/packages/parser-slip-encoder/package.json b/packages/parser-slip-encoder/package.json index 7306b4889..28c2a12f8 100644 --- a/packages/parser-slip-encoder/package.json +++ b/packages/parser-slip-encoder/package.json @@ -3,7 +3,7 @@ "main": "slip-encoder.js", "version": "2.0.2", "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/parser-slip-encoder/slip-encoder.js b/packages/parser-slip-encoder/slip-encoder.js index 3c244245e..0127ec51a 100644 --- a/packages/parser-slip-encoder/slip-encoder.js +++ b/packages/parser-slip-encoder/slip-encoder.js @@ -1,4 +1,4 @@ -const Transform = require('stream').Transform +const { Transform } = require('stream') const END = 0xc0 const ESC = 0xdb diff --git a/packages/repl/package.json b/packages/repl/package.json index c0c92c4d5..db41c7294 100644 --- a/packages/repl/package.json +++ b/packages/repl/package.json @@ -10,7 +10,7 @@ "serialport": "^7.1.5" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/serialport/package.json b/packages/serialport/package.json index fa7aa9605..7352d8f91 100644 --- a/packages/serialport/package.json +++ b/packages/serialport/package.json @@ -52,7 +52,7 @@ "debug": "^4.1.1" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "license": "MIT", "scripts": { diff --git a/packages/serialport/test-manual/many-writes.js b/packages/serialport/test-manual/many-writes.js index 71d025a57..cf7b3ff31 100644 --- a/packages/serialport/test-manual/many-writes.js +++ b/packages/serialport/test-manual/many-writes.js @@ -63,10 +63,10 @@ function writeOneCommandAtATime(port) { }); } -function writeAndDrain(port) { - const command = commands.pop(); +async function writeAndDrain(port) { + const command = commands.pop() if (!command) { - return Promise.resolve(port); + port } const commandNumber = commandCount - commands.length; return new Promise((resolve, reject) => { diff --git a/packages/stream/package.json b/packages/stream/package.json index d8512a6c8..64cbb62c2 100644 --- a/packages/stream/package.json +++ b/packages/stream/package.json @@ -7,7 +7,7 @@ "debug": "^4.1.1" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/packages/stream/stream.js b/packages/stream/stream.js index 444ebd506..1b54ab273 100644 --- a/packages/stream/stream.js +++ b/packages/stream/stream.js @@ -99,7 +99,7 @@ function SerialPort(path, options, openCallback) { options = {} } - const settings = Object.assign({}, defaultSettings, options) + const settings = { ...defaultSettings, ...options } stream.Duplex.call(this, { highWaterMark: settings.highWaterMark, @@ -260,7 +260,7 @@ SerialPort.prototype.update = function(options, callback) { return this._asyncError(new Error('Port is not open'), callback) } - const settings = Object.assign({}, defaultSettings, options) + const settings = { ...defaultSettings, ...options } this.settings.baudRate = settings.baudRate debug('update', `baudRate: ${settings.baudRate}`) @@ -466,7 +466,7 @@ SerialPort.prototype.set = function(options, callback) { return this._asyncError(new Error('Port is not open'), callback) } - const settings = Object.assign({}, defaultSetFlags, options) + const settings = { ...defaultSetFlags, ...options } debug('#set', settings) this.binding.set(settings).then( () => { diff --git a/packages/stream/stream.test.js b/packages/stream/stream.test.js index 841e0f784..d77d08669 100644 --- a/packages/stream/stream.test.js +++ b/packages/stream/stream.test.js @@ -259,10 +259,9 @@ describe('SerialPort', () => { const port = new SerialPort('/dev/exists', {}, () => { port.close() }) - sandbox.stub(SerialPort.Binding.prototype, 'close').callsFake(() => { + sandbox.stub(SerialPort.Binding.prototype, 'close').callsFake(async () => { assert.isFalse(port.isOpen) done() - return Promise.resolve() }) }) diff --git a/packages/terminal/package.json b/packages/terminal/package.json index 33683bb32..ee5ab605d 100644 --- a/packages/terminal/package.json +++ b/packages/terminal/package.json @@ -11,7 +11,7 @@ "serialport": "^7.1.5" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.6.0" }, "publishConfig": { "access": "public" diff --git a/test/initializers/test-config.js b/test/initializers/test-config.js index bfeb598b3..5c46070a6 100644 --- a/test/initializers/test-config.js +++ b/test/initializers/test-config.js @@ -1,7 +1,7 @@ const testConfig = require('../../test-config.json') global.makeTestFeature = function makeTestFeature(envName) { - const config = Object.assign({}, testConfig.all, testConfig[envName]) + const config = { ...testConfig.all, ...testConfig[envName] } return function testFeature(feature, description, callback) { if (config[feature] === false) { return it(`Feature "${feature}" is disabled in "${envName}. "${description}"`)