node-serialport-bindings-cpp/lib/bindings-darwin.js
Francis Gulotta 1643dd1c81 Remove Polyfills and upgrade node-pre-gyp
- Remove object.assign
- Remove buffer-indexof
- Remove lie
2016-12-12 22:26:47 -05:00

145 lines
3.1 KiB
JavaScript

'use strict';
var binding = require('bindings')('serialport.node');
var defaultBindingOptions = {
vmin: 1,
vtime: 0
};
function asyncError(cb, err) {
process.nextTick(function() {
cb(err);
});
}
function DarwinBinding(opt) {
if (typeof opt !== 'object') {
throw new TypeError('"options" is not an object');
}
if (typeof opt.disconnect !== 'function') {
throw new TypeError('"options.disconnect" is not a function');
}
this.disconnect = opt.disconnect;
this.bindingOptions = opt.bindingOptions || {};
this.fd = null;
};
DarwinBinding.list = binding.list;
DarwinBinding.prototype.read = require('./read-unix');
DarwinBinding.prototype.open = function(path, options, cb) {
if (!path) {
throw new TypeError('"path" is not a valid port');
}
if (typeof options !== 'object') {
throw new TypeError('"options" is not an object');
}
if (typeof cb !== 'function') {
throw new TypeError('"cb" is not a function');
}
if (this.isOpen) {
return asyncError(cb, new Error('Already open'));
}
options = Object.assign({}, options, defaultBindingOptions, this.bindingOptions);
binding.open(path, options, function afterOpen(err, fd) {
if (err) {
return cb(err);
}
this.fd = fd;
cb(null);
}.bind(this));
};
DarwinBinding.prototype.close = function(cb) {
if (typeof cb !== 'function') {
throw new TypeError('"cb" is not a function');
}
if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
if (this.readPoller) {
this.readPoller.stop();
this.readPoller = null;
}
binding.close(this.fd, function afterClose(err) {
if (err) {
return cb(err);
}
this.fd = null;
cb(null);
}.bind(this));
};
DarwinBinding.prototype.set = function(options, cb) {
if (typeof options !== 'object') {
throw new TypeError('"options" is not an object');
}
if (typeof cb !== 'function') {
throw new TypeError('"cb" is not a function');
}
if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
binding.set(this.fd, options, cb);
};
DarwinBinding.prototype.write = function(buffer, cb) {
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('"buffer" is not a Buffer');
}
if (typeof cb !== 'function') {
throw new TypeError('"cb" is not a function');
}
if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
binding.write(this.fd, buffer, cb);
};
var commonMethods = [
'drain',
'flush',
'update',
'get'
];
commonMethods.map(function(methodName) {
DarwinBinding.prototype[methodName] = function() {
var args = Array.prototype.slice.apply(arguments);
var cb = args[args.length - 1];
if (typeof cb !== 'function') {
throw new TypeError('"cb" is not a function');
}
if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
args.unshift(this.fd);
binding[methodName].apply(binding, args);
};
});
Object.defineProperty(DarwinBinding.prototype, 'isOpen', {
enumerable: true,
get: function() {
return this.fd !== null;
}
});
module.exports = DarwinBinding;