-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathdarwin.js
110 lines (94 loc) · 2.57 KB
/
darwin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const { promisify } = require('util')
const binding = require('bindings')('bindings.node')
const AbstractBinding = require('@serialport/binding-abstract')
const Poller = require('./poller')
const unixRead = require('./unix-read')
const unixWrite = require('./unix-write')
const { wrapWithHiddenComName } = require('./legacy')
const defaultBindingOptions = Object.freeze({
vmin: 1,
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 wrapWithHiddenComName(asyncList())
}
constructor(opt = {}) {
super(opt)
this.bindingOptions = { ...defaultBindingOptions, ...opt.bindingOptions }
this.fd = null
this.writeOperation = null
}
get isOpen() {
return this.fd !== null
}
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)
}
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)
}
async read(buffer, offset, length) {
await super.read(buffer, offset, length)
return unixRead({ binding: this, buffer, offset, length })
}
async write(buffer) {
this.writeOperation = super.write(buffer).then(async () => {
if (buffer.length === 0) {
return
}
await unixWrite({ binding: this, buffer })
this.writeOperation = null
})
return this.writeOperation
}
async update(options) {
await super.update(options)
return asyncUpdate(this.fd, options)
}
async set(options) {
await super.set(options)
return asyncSet(this.fd, options)
}
async get() {
await super.get()
return asyncGet(this.fd)
}
async getBaudRate() {
await super.get()
return asyncGetBaudRate(this.fd)
}
async drain() {
await super.drain()
await this.writeOperation
return asyncDrain(this.fd)
}
async flush() {
await super.flush()
return asyncFlush(this.fd)
}
}
module.exports = DarwinBinding