Skip to content

Commit

Permalink
chore: remove node6 support and upgrade codebase (#1851)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
reconbot committed May 2, 2019
1 parent f7587ed commit d4f15c0
Show file tree
Hide file tree
Showing 47 changed files with 453 additions and 501 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 2018,
"ecmaFeatures": {
"jsx": true
}
Expand Down
2 changes: 1 addition & 1 deletion .generators/lib/{{dashCase name}}.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Transform = require('stream').Transform
const { Transform } = require('stream')
const debug = require('debug')('serialport/{{dashCase name}}')

/**
Expand Down
2 changes: 1 addition & 1 deletion .generators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"debug": "^4.1.1"
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.6.0"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 0 additions & 1 deletion docs/api-binding-abstract.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
75 changes: 32 additions & 43 deletions packages/binding-abstract/binding-abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
}
Expand All @@ -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()
}

/**
Expand All @@ -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')
}
Expand All @@ -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()
}

/**
Expand All @@ -114,28 +110,27 @@ 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()
}

/**
* Changes connection settings on an open port. Only `baudRate` is supported.
* @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')
}
Expand All @@ -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()
}

/**
Expand All @@ -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()
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/binding-abstract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"debug": "^4.1.1"
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.6.0"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit d4f15c0

Please sign in to comment.