Add serialport@8 updates (#41576)

Per https://github.com/serialport/node-serialport/blob/master/UPGRADE_GUIDE.md:
* Changed comName to path
* Removed callback to SerialPort.list() (was already absent from BaseBinding.list())
This commit is contained in:
Doug Brunner
2020-01-17 11:36:47 -08:00
committed by Eli Barzilay
parent a20b572cf4
commit 51f2687641
6 changed files with 320 additions and 8 deletions

View File

@@ -1,8 +1,9 @@
// Type definitions for serialport 7.0
// Type definitions for serialport 8.0
// Project: https://github.com/node-serialport/node-serialport
// Definitions by: Jeremy Foster <https://github.com/codefoster>
// Andrew Pearson <https://github.com/apearson>
// Cameron Tacklind <https://github.com/cinderblock>
// Doug Brunner <https://github.com/doug-a-brunner>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
@@ -41,14 +42,13 @@ declare class SerialPort extends Stream.Duplex {
static Binding: SerialPort.BaseBinding;
static list(callback?: SerialPort.ListCallback): Promise<SerialPort.PortInfo[]>;
static list(): Promise<SerialPort.PortInfo[]>;
}
declare namespace SerialPort {
// Callbacks Type Defs
type ErrorCallback = (error?: Error | null) => void;
type ModemBitsCallback = (error: Error | null | undefined, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void;
type ListCallback = (error: Error | null | undefined, ports: any[]) => void;
// Options Type Defs
interface OpenOptions {
@@ -81,7 +81,7 @@ declare namespace SerialPort {
}
interface PortInfo {
comName: string;
path: string;
manufacturer?: string;
serialNumber?: string;
pnpId?: string;

View File

@@ -139,7 +139,3 @@ function test_list_ports_promise() {
.then((ports: SerialPort.PortInfo[]) => {})
.catch((err: Error) => {});
}
function test_list_ports_callback() {
const ports = SerialPort.list((error: Error | null | undefined, ports: any[]) => {});
}

137
types/serialport/v7/index.d.ts vendored Normal file
View File

@@ -0,0 +1,137 @@
// Type definitions for serialport 7.0
// Project: https://github.com/node-serialport/node-serialport
// Definitions by: Jeremy Foster <https://github.com/codefoster>
// Andrew Pearson <https://github.com/apearson>
// Cameron Tacklind <https://github.com/cinderblock>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import * as Stream from 'stream';
declare class SerialPort extends Stream.Duplex {
constructor(path: string, callback?: SerialPort.ErrorCallback);
constructor(path: string, options?: SerialPort.OpenOptions, callback?: SerialPort.ErrorCallback);
readonly baudRate: number;
readonly binding: SerialPort.BaseBinding;
readonly isOpen: boolean;
readonly path: string;
open(callback?: SerialPort.ErrorCallback): void;
update(options: SerialPort.UpdateOptions, callback?: SerialPort.ErrorCallback): void;
write(data: string| number[] | Buffer, callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean;
write(buffer: string| number[] | Buffer, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean;
read(size?: number): string | Buffer | null;
close(callback?: (error?: Error | null) => void): void;
set(options: SerialPort.SetOptions, callback?: SerialPort.ErrorCallback): void;
get(callback?: SerialPort.ModemBitsCallback): void;
flush(callback?: SerialPort.ErrorCallback): void;
drain(callback?: SerialPort.ErrorCallback): void;
pause(): this;
resume(): this;
on(event: string, callback: (data?: any) => void): this;
static Binding: SerialPort.BaseBinding;
static list(callback?: SerialPort.ListCallback): Promise<SerialPort.PortInfo[]>;
}
declare namespace SerialPort {
// Callbacks Type Defs
type ErrorCallback = (error?: Error | null) => void;
type ModemBitsCallback = (error: Error | null | undefined, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void;
type ListCallback = (error: Error | null | undefined, ports: any[]) => void;
// Options Type Defs
interface OpenOptions {
autoOpen?: boolean;
baudRate?: 115200|57600|38400|19200|9600|4800|2400|1800|1200|600|300|200|150|134|110|75|50|number;
dataBits?: 8|7|6|5;
highWaterMark?: number;
lock?: boolean;
stopBits?: 1|2;
parity?: 'none'|'even'|'mark'|'odd'|'space';
rtscts?: boolean;
xon?: boolean;
xoff?: boolean;
xany?: boolean;
binding?: BaseBinding;
bindingOptions?: {
vmin?: number;
vtime?: number;
};
}
interface UpdateOptions {
baudRate?: 115200|57600|38400|19200|9600|4800|2400|1800|1200|600|300|200|150|134|110|75|50|number;
}
interface SetOptions {
brk?: boolean;
cts?: boolean;
dsr?: boolean;
dtr?: boolean;
rts?: boolean;
}
interface PortInfo {
comName: string;
manufacturer?: string;
serialNumber?: string;
pnpId?: string;
locationId?: string;
productId?: string;
vendorId?: string;
}
namespace parsers {
class ByteLength extends Stream.Transform {
constructor(options: {length: number});
}
class CCTalk extends Stream.Transform {
constructor();
}
class Delimiter extends Stream.Transform {
constructor(options: {delimiter: string | Buffer | number[], includeDelimiter?: boolean});
}
class Readline extends Delimiter {
constructor(options: {delimiter: string | Buffer | number[], encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', includeDelimiter?: boolean});
}
class Ready extends Stream.Transform {
constructor(options: {delimiter: string | Buffer | number[]});
}
class Regex extends Stream.Transform {
constructor(options: {regex: RegExp});
}
}
// Binding Type Defs
type win32Binding = BaseBinding;
type darwinBinding = BaseBinding;
type linuxBinding = BaseBinding;
// Binding Type Def
class BaseBinding {
constructor(options: any);
open(path: string, options: OpenOptions): Promise<any>;
close(): Promise<any>;
read(data: Buffer, offset: number, length: number): Promise<any>;
write(data: Buffer): Promise<any>;
update(options?: UpdateOptions): Promise<any>;
set(options?: SetOptions): Promise<any>;
get(): Promise<any>;
flush(): Promise<any>;
drain(): Promise<any>;
static list(): Promise<PortInfo[]>;
}
}
export = SerialPort;

View File

@@ -0,0 +1,145 @@
// Tests taken from documentation samples.
import SerialPort = require('serialport');
function test_basic_connect() {
const port = new SerialPort('');
}
function test_connect_config() {
const port1 = new SerialPort('', {
}, error => {});
const port4 = new SerialPort('', {
autoOpen: false,
lock: false,
baudRate: 115200,
dataBits: 5,
stopBits: 2,
parity: 'odd',
rtscts: true,
xon: true,
xoff: true,
highWaterMark: 1024,
bindingOptions: {
vmin: 1,
vtime: 1
}
},
error => {
if (error !== null) {
console.error(error);
}
}
);
}
function test_open() {
const port = new SerialPort('');
port.open(() => {});
}
function test_update() {
const port = new SerialPort('');
port.update({baudRate: 57600});
}
function test_write() {
const port = new SerialPort('');
port.write('test', (error?: Error | null) => {});
port.write('test', 'utf8', (error?: Error | null) => {});
}
function test_read() {
const port = new SerialPort('');
const data = port.read(8);
}
function test_close() {
const port = new SerialPort('');
port.close((error?: Error | null) => {});
}
function test_set() {
const port = new SerialPort('');
port.set({}, (error?: Error | null) => {});
}
function test_get() {
const port = new SerialPort('');
port.get((error, status) => {});
}
function test_flush() {
const port = new SerialPort('');
port.flush((error?: Error | null) => {});
}
function test_drain() {
const port = new SerialPort('');
port.drain((error?: Error | null) => {});
}
function test_pause_resume() {
const port = new SerialPort('');
const pauseItem: SerialPort = port.pause();
const resumeItem: SerialPort = port.resume();
}
function test_on_events() {
const port = new SerialPort('');
const onItem: SerialPort = port.on('event', (data: any) => {});
}
function test_binding() {
const port = new SerialPort('');
const bindingItem: SerialPort.BaseBinding = SerialPort.Binding;
}
function test_parsers() {
const port = new SerialPort('');
const ByteLengthParser = new SerialPort.parsers.ByteLength({length: 8});
const CCTalkParser = new SerialPort.parsers.CCTalk();
const DelimiterParser = new SerialPort.parsers.Delimiter({ delimiter: Buffer.from('EOL'), includeDelimiter: true });
const ReadlineParser = new SerialPort.parsers.Readline({ delimiter: '\r\n', includeDelimiter: false });
const ReadyParser = new SerialPort.parsers.Ready({ delimiter: 'READY' });
const RegexParser = new SerialPort.parsers.Regex({regex: /.*/});
port.pipe(ByteLengthParser);
port.pipe(CCTalkParser);
port.pipe(DelimiterParser);
port.pipe(ReadlineParser);
port.pipe(ReadyParser);
port.pipe(RegexParser);
}
function test_properties() {
const port = new SerialPort('');
const baudRate: number = port.baudRate;
const binding: SerialPort.BaseBinding = port.binding;
const isOpen: boolean = port.isOpen;
const path: string = port.path;
}
function test_list_ports_promise() {
const ports = SerialPort
.list()
.then((ports: SerialPort.PortInfo[]) => {})
.catch((err: Error) => {});
}
function test_list_ports_callback() {
const ports = SerialPort.list((error: Error | null | undefined, ports: any[]) => {});
}

View File

@@ -0,0 +1,28 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../../",
"typeRoots": [
"../../"
],
"paths": {
"serialport": [
"serialport/v7"
]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"serialport-tests.ts"
]
}

View File

@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"strict-type-predicates": true
}
}