Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@serialport/binding-mock

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@serialport/binding-mock - npm Package Compare versions

Comparing version 2.0.5 to 3.0.0

legacy.js

219

binding-mock.js
const AbstractBinding = require('@serialport/binding-abstract')
const debug = require('debug')('serialport/binding-mock')
const { wrapWithHiddenComName } = require('./legacy')

@@ -33,13 +34,11 @@ let ports = {}

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,
}

@@ -52,3 +51,3 @@ ports[path] = {

info: {
comName: path,
path,
manufacturer: opt.manufacturer,

@@ -65,7 +64,4 @@ serialNumber,

static list() {
const info = Object.keys(ports).map(path => {
return ports[path].info
})
return Promise.resolve(info)
static async list() {
return wrapWithHiddenComName(Object.values(ports).map(port => port.info))
}

@@ -89,85 +85,78 @@

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) {
throw new Error('Open: binding is already open')
}
port.openOpt = { ...opt }
this.isOpen = true
debug(this.serialNumber, 'port is open')
if (port.echo) {
process.nextTick(() => {
if (this.isOpen) {
return Promise.reject(new Error('Open: binding is already open'))
debug(this.serialNumber, 'emitting ready data')
this.emitData(port.readyData)
}
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)
}
})
}
})
}
}
close() {
async close() {
const port = this.port
debug(this.serialNumber, 'closing port')
if (!port) {
return Promise.reject(new Error('already closed'))
throw 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'))
}
})
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')
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)
}
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')

@@ -180,3 +169,3 @@ if (this.writeOperation) {

.then(resolveNextTick)
.then(() => {
.then(async () => {
if (!this.isOpen) {

@@ -202,53 +191,41 @@ throw new Error('Write canceled')

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()
}

@@ -255,0 +232,0 @@ }

@@ -6,2 +6,18 @@ # Change Log

# [3.0.0](https://github.com/node-serialport/node-serialport/compare/@serialport/binding-mock@2.0.5...@serialport/binding-mock@3.0.0) (2019-05-16)
### chore
* remove node6 support and upgrade codebase ([#1851](https://github.com/node-serialport/node-serialport/issues/1851)) ([d4f15c0](https://github.com/node-serialport/node-serialport/commit/d4f15c0))
### BREAKING CHANGES
* bindings now use async functions so they’ll never throw, only reject
## [2.0.5](https://github.com/node-serialport/node-serialport/compare/@serialport/binding-mock@2.0.4...@serialport/binding-mock@2.0.5) (2019-04-27)

@@ -8,0 +24,0 @@

{
"name": "@serialport/binding-mock",
"version": "2.0.5",
"version": "3.0.0",
"main": "binding-mock.js",

@@ -9,7 +9,7 @@ "keywords": [

"dependencies": {
"@serialport/binding-abstract": "^2.0.5",
"@serialport/binding-abstract": "^3.0.0",
"debug": "^4.1.1"
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.6.0"
},

@@ -24,3 +24,3 @@ "publishConfig": {

},
"gitHead": "4c723cc89e454017a28396e934bf7a00946fe866"
"gitHead": "524a2729003a94c9575904448d878a151f4f3790"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc