Buffer upgrades (#1168)

* upgrade to non deprecated buffer methods
* Don't lint on node 4 as it's now broken

I really wish packages would keep things working on LTS platforms.
This commit is contained in:
Francis Gulotta 2017-05-14 12:58:56 -04:00 • committed by GitHub
parent b619a8c572
commit 5f53a45cae
23 changed files with 154 additions and 133 deletions

View file

@ -308,7 +308,7 @@ You can write to the serial port by sending a string or buffer to the write meth
```js
port.write('Hi Mom!');
port.write(new Buffer('Hi Mom!'));
port.write(Buffer.from('Hi Mom!'));
```
Enjoy and do cool things with this code.

View file

@ -113,8 +113,12 @@ install:
script:
# linting no longer works on node 0.10
- npm run lint
# linting no longer works on node 4
- >
if [[ $TRAVIS_NODE_VERSION != "4" ]]; then
npm run lint
fi
- npm run docs:diff
- >
if [[ -z $TRAVIS_ELECTRON_VERSION ]]; then

View file

@ -356,7 +356,7 @@ You can write to the serial port by sending a string or buffer to the write meth
```js
port.write('Hi Mom!');
port.write(new Buffer('Hi Mom!'));
port.write(Buffer.from('Hi Mom!'));
```
Enjoy and do cool things with this code.
@ -710,7 +710,7 @@ To use the Delimiter parser you must specify, you must provide a delimiter as a
var SerialPort = require('serialport');
var Delimiter = SerialPort.parsers.Delimiter;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(new Delimiter({delimiter: new Buffer('EOL')}));
var parser = port.pipe(new Delimiter({delimiter: Buffer.from('EOL')}));
parser.on('data', console.log);
```

View file

@ -100,7 +100,11 @@ build_script:
- npm install --build-from-source --msvs_version=2013
test_script:
- ps: npm run lint
- ps: >
if ($env:nodejs_version -ne "4") {
npm run lint
}
true;
# If we run npm test in powershell it'll have the wrong encoding
# so we have to do it like this

View file

@ -1,11 +1,12 @@
#!/usr/bin/env node
'use strict';
const args = require('commander');
const Buffer = require('safe-buffer').Buffer;
const SerialPort = require('../');
const version = require('../package.json').version;
const args = require('commander');
const readyData = new Buffer('READY');
const readyData = Buffer.from('READY');
args
.version(version)

View file

@ -1,6 +1,6 @@
/* eslint-disable require-path-exists/exists */
'use strict';
const Buffer = require('safe-buffer').Buffer;
const SerialPort = require('serialport');
const port = new SerialPort('/dev/cu.Cubelet-RGB');
@ -8,7 +8,7 @@ port.on('open', () => {
console.log('port opened');
});
const largeMessage = new Buffer(1024 * 10).fill('!');
const largeMessage = Buffer.from(1024 * 10).fill('!');
port.write(largeMessage, () => {
console.log('Write callback returned');

View file

@ -1,4 +1,5 @@
'use strict';
const Buffer = require('safe-buffer').Buffer;
const BaseBinding = require('./base');
let ports = {};
@ -20,11 +21,11 @@ class MockBindings extends BaseBinding {
static createPort(path, opt) {
opt = Object.assign({
echo: true,
readyData: new Buffer('READY')
readyData: Buffer.from('READY')
}, opt);
ports[path] = {
data: new Buffer(0),
data: Buffer.alloc(0),
lastWrite: null,
echo: opt.echo,
readyData: opt.readyData,
@ -109,7 +110,7 @@ class MockBindings extends BaseBinding {
.then(() => {
delete port.openOpt;
// reset data on close
port.data = new Buffer(0);
port.data = Buffer.alloc(0);
delete this.port;
this.isOpen = false;
@ -136,7 +137,7 @@ class MockBindings extends BaseBinding {
write(buffer) {
return super.write(buffer)
.then(() => {
const data = this.port.lastWrite = new Buffer(buffer); // copy
const data = this.port.lastWrite = Buffer.from(buffer); // copy
if (this.port.echo) {
process.nextTick(() => this.emitData(data));
}

View file

@ -1,4 +1,5 @@
'use strict';
const Buffer = require('safe-buffer').Buffer;
const inherits = require('util').inherits;
const Transform = require('stream').Transform;
@ -19,7 +20,7 @@ function ByteLengthParser(options) {
}
this.length = options.length;
this.buffer = new Buffer(0);
this.buffer = Buffer.alloc(0);
}
inherits(ByteLengthParser, Transform);
@ -37,7 +38,7 @@ ByteLengthParser.prototype._transform = function(chunk, encoding, cb) {
ByteLengthParser.prototype._flush = function(cb) {
this.push(this.buffer);
this.buffer = new Buffer(0);
this.buffer = Buffer.alloc(0);
cb();
};

View file

@ -1,4 +1,5 @@
'use strict';
const Buffer = require('safe-buffer').Buffer;
const inherits = require('util').inherits;
const Transform = require('stream').Transform;
@ -18,8 +19,8 @@ function DelimiterParser(options) {
throw new TypeError('"delimiter" has a 0 or undefined length');
}
this.delimiter = new Buffer(options.delimiter);
this.buffer = new Buffer(0);
this.delimiter = Buffer.from(options.delimiter);
this.buffer = Buffer.alloc(0);
}
inherits(DelimiterParser, Transform);
@ -37,7 +38,7 @@ DelimiterParser.prototype._transform = function(chunk, encoding, cb) {
DelimiterParser.prototype._flush = function(cb) {
this.push(this.buffer);
this.buffer = new Buffer(0);
this.buffer = Buffer.alloc(0);
cb();
};

View file

@ -38,7 +38,7 @@ To use the Delimiter parser you must specify, you must provide a delimiter as a
var SerialPort = require('serialport');
var Delimiter = SerialPort.parsers.Delimiter;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(new Delimiter({delimiter: new Buffer('EOL')}));
var parser = port.pipe(new Delimiter({delimiter: Buffer.from('EOL')}));
parser.on('data', console.log);
```

View file

@ -1,6 +1,8 @@
'use strict';
const DelimiterParser = require('./delimiter');
const inherits = require('util').inherits;
const Buffer = require('safe-buffer').Buffer;
const DelimiterParser = require('./delimiter');
function ReadlineParser(options) {
if (!(this instanceof ReadlineParser)) {
@ -10,13 +12,13 @@ function ReadlineParser(options) {
options = options || {};
if (options.delimiter === undefined) {
options.delimiter = new Buffer('\n', 'utf8');
options.delimiter = Buffer.from('\n', 'utf8');
}
DelimiterParser.call(this, options);
const encoding = options.encoding || 'utf8';
this.delimiter = new Buffer(options.delimiter, encoding);
this.delimiter = Buffer.from(options.delimiter, encoding);
this.setEncoding(encoding);
}

View file

@ -1,4 +1,6 @@
'use strict';
const Buffer = require('safe-buffer').Buffer;
const inherits = require('util').inherits;
const Transform = require('stream').Transform;
@ -26,7 +28,7 @@ function RegexParser(options) {
this.setEncoding(encoding);
this.delimiter = options.delimiter;
this.buffer = new Buffer(0);
this.buffer = Buffer.alloc(0);
}
inherits(RegexParser, Transform);
@ -42,13 +44,13 @@ RegexParser.prototype._transform = function(chunk, encoding, cb) {
this.push(part);
});
this.buffer = new Buffer(data);
this.buffer = Buffer.from(data);
cb();
};
RegexParser.prototype._flush = function(cb) {
this.push(this.buffer);
this.buffer = new Buffer(0);
this.buffer = Buffer.alloc(0);
cb();
};

View file

@ -5,6 +5,7 @@
* @copyright Chris Williams <chris@iterativedesigns.com>
*/
const Buffer = require('safe-buffer').Buffer;
const stream = require('stream');
const util = require('util');
@ -41,13 +42,7 @@ const defaultSetFlags = Object.freeze({
});
function allocNewReadPool(poolSize) {
let pool;
// performs better on node 6+
if (Buffer.allocUnsafe) {
pool = Buffer.allocUnsafe(poolSize);
} else {
pool = new Buffer(poolSize);
}
const pool = Buffer.allocUnsafe(poolSize);
pool.used = 0;
return pool;
}
@ -294,7 +289,7 @@ Even though serialport is a stream, when writing it can accept arrays of bytes i
const superWrite = SerialPort.prototype.write;
SerialPort.prototype.write = function(data, encoding, callback) {
if (Array.isArray(data)) {
data = new Buffer(data);
data = Buffer.from(data);
}
return superWrite.call(this, data, encoding, callback);
};

View file

@ -57,13 +57,16 @@
"commander": "^2.9.0",
"debug": "^2.6.6",
"nan": "^2.6.2",
"node-pre-gyp": "^0.6.32"
"node-pre-gyp": "^0.6.32",
"safe-buffer": "^5.0.1"
},
"devDependencies": {
"chai": "^3.5.0",
"chai-subset": "^1.2.2",
"eslint": "^3.19.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-require-path-exists": "^1.1.7",
"eslint-plugin-standard": "^3.0.1",

View file

@ -3,6 +3,7 @@
// `npm run stress` to run these tests
const Buffer = require('safe-buffer').Buffer;
const assert = require('chai').assert;
const util = require('util');
const SerialPort = require('../../');
@ -34,7 +35,7 @@ describe('the stress', () => {
}
it("doesn't leak memory", (done) => {
const data = new Buffer(1024);
const data = Buffer.alloc(1024);
const hd = new memwatch.HeapDiff();
const port = new SerialPort(testPort, {}, false);
port.on('close', done);

View file

@ -2,6 +2,7 @@
/* eslint-disable no-new */
const assert = require('chai').assert;
const Buffer = require('safe-buffer').Buffer;
let platform;
switch (process.platform) {
@ -55,7 +56,7 @@ function disconnect(err) {
// The echo firmware should respond with this data when it's
// ready to echo. This allows for remote device bootup.
// the default firmware is called arduinoEcho.ino
const readyData = new Buffer('READY');
const readyData = Buffer.from('READY');
// Test our mock binding and the binding for the platform we're running on
bindingsToTest.forEach((bindingName) => {
@ -354,7 +355,7 @@ function testBinding(bindingName, Binding, testPort, disabledFeatures) {
disconnect
});
let noZalgo = false;
binding.write(new Buffer([])).catch((err) => {
binding.write(Buffer.from([])).catch((err) => {
assert.instanceOf(err, Error);
assert(noZalgo);
done();
@ -390,13 +391,13 @@ function testBinding(bindingName, Binding, testPort, disabledFeatures) {
afterEach(() => binding.close());
it('resolves after a small write', () => {
const data = new Buffer('simple write of 24 bytes');
const data = Buffer.from('simple write of 24 bytes');
return binding.write(data);
});
it('resolves after a large write', function() {
this.timeout(20000);
const data = new Buffer(1024 * 5);
const data = Buffer.alloc(1024 * 5);
return binding.write(data);
});
});
@ -520,7 +521,7 @@ function testBinding(bindingName, Binding, testPort, disabledFeatures) {
describe('#read', () => {
it('errors asynchronously when not open', (done) => {
const binding = new Binding({ disconnect });
const buffer = new Buffer(5);
const buffer = Buffer.alloc(5);
let noZalgo = false;
binding.read(buffer, 0, buffer.length).catch((err) => {
assert.instanceOf(err, Error);
@ -537,7 +538,7 @@ function testBinding(bindingName, Binding, testPort, disabledFeatures) {
let binding, buffer;
beforeEach(() => {
buffer = new Buffer(readyData.length);
buffer = Buffer.alloc(readyData.length);
binding = new Binding({ disconnect });
return binding.open(testPort, defaultOpenOptions);
});

View file

@ -1,4 +1,5 @@
'use strict';
const Buffer = require('safe-buffer').Buffer;
const crypto = require('crypto');
const assert = require('chai').assert;
const SerialPort = require('../');
@ -14,7 +15,7 @@ switch (process.platform) {
throw new Error(`Unknown platform "${process.platform}"`);
}
const readyData = new Buffer('READY');
const readyData = Buffer.from('READY');
// test everything on our mock biding and natively
const defaultBinding = SerialPort.Binding;
@ -152,7 +153,7 @@ function integrationTest(platform, testPort, binding) {
it('5k test', function(done) {
this.timeout(20000);
// 5k of random ascii
const output = new Buffer(crypto.randomBytes(5120).toString('ascii'));
const output = Buffer.from(crypto.randomBytes(5120).toString('ascii'));
const expectedInput = Buffer.concat([readyData, output]);
const port = new SerialPort(testPort);
@ -161,7 +162,7 @@ function integrationTest(platform, testPort, binding) {
port.write(output);
});
let input = new Buffer(0);
let input = Buffer.alloc(0);
port.on('data', (data) => {
input = Buffer.concat([input, data]);
if (input.length >= expectedInput.length) {

View file

@ -1,5 +1,6 @@
'use strict';
const Buffer = require('safe-buffer').Buffer;
const SerialPort = require('../../');
const port = process.env.TEST_PORT;
@ -13,7 +14,7 @@ if (!port) {
// SerialPort.Binding = Binding;
// debugger;
const writeData = new Buffer(50000).fill(1);
const writeData = Buffer.alloc(50000, 1);
const serialPort = new SerialPort(port, {
// baudRate: 115200

View file

@ -2,8 +2,8 @@
/* eslint-disable no-new */
const assert = require('chai').assert;
const Buffer = require('safe-buffer').Buffer;
const sinon = require('sinon');
const ByteLengthParser = require('../lib/parsers/byte-length');
describe('ByteLengthParser', () => {
@ -14,15 +14,15 @@ describe('ByteLengthParser', () => {
});
it('emits data events every 8 bytes', () => {
const data = new Buffer('Robots are so freaking cool!');
const data = Buffer.from('Robots are so freaking cool!');
const spy = sinon.spy();
const parser = new ByteLengthParser({ length: 8 });
parser.on('data', spy);
parser.write(data);
assert.equal(spy.callCount, 3);
assert.deepEqual(spy.getCall(0).args[0], new Buffer('Robots a'));
assert.deepEqual(spy.getCall(1).args[0], new Buffer('re so fr'));
assert.deepEqual(spy.getCall(2).args[0], new Buffer('eaking c'));
assert.deepEqual(spy.getCall(0).args[0], Buffer.from('Robots a'));
assert.deepEqual(spy.getCall(1).args[0], Buffer.from('re so fr'));
assert.deepEqual(spy.getCall(2).args[0], Buffer.from('eaking c'));
});
it('throws when not provided with a length', () => {
@ -51,20 +51,20 @@ describe('ByteLengthParser', () => {
const parser = new ByteLengthParser({ length: 4 });
const spy = sinon.spy();
parser.on('data', spy);
parser.write(new Buffer('ab'));
parser.write(new Buffer('cd'));
parser.write(Buffer.from('ab'));
parser.write(Buffer.from('cd'));
assert.equal(spy.callCount, 1);
assert.deepEqual(spy.getCall(0).args[0], new Buffer('abcd'));
assert.deepEqual(spy.getCall(0).args[0], Buffer.from('abcd'));
});
it('flushes remaining data when the stream ends', () => {
const parser = new ByteLengthParser({ length: 4 });
const spy = sinon.spy();
parser.on('data', spy);
parser.write(new Buffer('12'));
parser.write(Buffer.from('12'));
assert.equal(spy.callCount, 0);
parser.end();
assert.equal(spy.callCount, 1);
assert.deepEqual(spy.getCall(0).args[0], new Buffer('12'));
assert.deepEqual(spy.getCall(0).args[0], Buffer.from('12'));
});
});

View file

@ -1,6 +1,7 @@
'use strict';
/* eslint-disable no-new */
const Buffer = require('safe-buffer').Buffer;
const assert = require('chai').assert;
const sinon = require('sinon');
@ -9,34 +10,34 @@ const DelimiterParser = require('../lib/parsers/delimiter');
describe('DelimiterParser', () => {
it('works without new', () => {
// eslint-disable-next-line new-cap
const parser = DelimiterParser({ delimiter: new Buffer([0]) });
const parser = DelimiterParser({ delimiter: Buffer.from([0]) });
assert.instanceOf(parser, DelimiterParser);
});
it('transforms data to strings split on a delimiter', () => {
const spy = sinon.spy();
const parser = new DelimiterParser({
delimiter: new Buffer('\n')
delimiter: Buffer.from('\n')
});
parser.on('data', spy);
parser.write(new Buffer('I love robots\nEach '));
parser.write(new Buffer('and Every One\n'));
parser.write(new Buffer('even you!'));
parser.write(Buffer.from('I love robots\nEach '));
parser.write(Buffer.from('and Every One\n'));
parser.write(Buffer.from('even you!'));
assert.deepEqual(spy.getCall(0).args[0], new Buffer('I love robots'));
assert.deepEqual(spy.getCall(1).args[0], new Buffer('Each and Every One'));
assert.deepEqual(spy.getCall(0).args[0], Buffer.from('I love robots'));
assert.deepEqual(spy.getCall(1).args[0], Buffer.from('Each and Every One'));
assert(spy.calledTwice);
});
it('flushes remaining data when the stream ends', () => {
const parser = DelimiterParser({ delimiter: new Buffer([0]) });
const parser = DelimiterParser({ delimiter: Buffer.from([0]) });
const spy = sinon.spy();
parser.on('data', spy);
parser.write(new Buffer([1]));
parser.write(Buffer.from([1]));
assert.equal(spy.callCount, 0);
parser.end();
assert.equal(spy.callCount, 1);
assert.deepEqual(spy.getCall(0).args[0], new Buffer([1]));
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1]));
});
it('throws when not provided with a delimiter', () => {
@ -48,7 +49,7 @@ describe('DelimiterParser', () => {
it('throws when called with a 0 length delimiter', () => {
assert.throws(() => {
new DelimiterParser({
delimiter: new Buffer(0)
delimiter: Buffer.from(0)
});
});
@ -70,7 +71,7 @@ describe('DelimiterParser', () => {
});
it('allows setting of the delimiter with a buffer', () => {
new DelimiterParser({ delimiter: new Buffer([1]) });
new DelimiterParser({ delimiter: Buffer.from([1]) });
});
it('allows setting of the delimiter with an array of bytes', () => {
@ -78,19 +79,19 @@ describe('DelimiterParser', () => {
});
it('emits data events every time it meets 00x 00x', () => {
const data = new Buffer('This could be\0\0binary data\0\0sent from a Moteino\0\0');
const data = Buffer.from('This could be\0\0binary data\0\0sent from a Moteino\0\0');
const parser = new DelimiterParser({ delimiter: [0, 0] });
const spy = sinon.spy();
parser.on('data', spy);
parser.write(data);
assert.equal(spy.callCount, 3);
assert.deepEqual(spy.getCall(0).args[0], new Buffer('This could be'));
assert.deepEqual(spy.getCall(1).args[0], new Buffer('binary data'));
assert.deepEqual(spy.getCall(2).args[0], new Buffer('sent from a Moteino'));
assert.deepEqual(spy.getCall(0).args[0], Buffer.from('This could be'));
assert.deepEqual(spy.getCall(1).args[0], Buffer.from('binary data'));
assert.deepEqual(spy.getCall(2).args[0], Buffer.from('sent from a Moteino'));
});
it('accepts single byte delimiter', () => {
const data = new Buffer('This could be\0binary data\0sent from a Moteino\0');
const data = Buffer.from('This could be\0binary data\0sent from a Moteino\0');
const parser = new DelimiterParser({ delimiter: [0] });
const spy = sinon.spy();
parser.on('data', spy);
@ -99,8 +100,8 @@ describe('DelimiterParser', () => {
});
it('Works when buffer starts with delimiter', () => {
const data = new Buffer('\0Hello\0World\0');
const parser = new DelimiterParser({ delimiter: new Buffer([0]) });
const data = Buffer.from('\0Hello\0World\0');
const parser = new DelimiterParser({ delimiter: Buffer.from([0]) });
const spy = sinon.spy();
parser.on('data', spy);
parser.write(data);
@ -108,7 +109,7 @@ describe('DelimiterParser', () => {
});
it('should only emit if delimiters are strictly in row', () => {
const data = new Buffer('\0Hello\u0001World\0\0\u0001');
const data = Buffer.from('\0Hello\u0001World\0\0\u0001');
const parser = new DelimiterParser({ delimiter: [0, 1] });
const spy = sinon.spy();
parser.on('data', spy);
@ -120,11 +121,11 @@ describe('DelimiterParser', () => {
const parser = new DelimiterParser({ delimiter: [0, 0] });
const spy = sinon.spy();
parser.on('data', spy);
parser.write(new Buffer('This could be\0\0binary '));
parser.write(new Buffer('data\0\0sent from a Moteino\0\0'));
parser.write(Buffer.from('This could be\0\0binary '));
parser.write(Buffer.from('data\0\0sent from a Moteino\0\0'));
assert.equal(spy.callCount, 3);
assert.deepEqual(spy.getCall(0).args[0], new Buffer('This could be'));
assert.deepEqual(spy.getCall(1).args[0], new Buffer('binary data'));
assert.deepEqual(spy.getCall(2).args[0], new Buffer('sent from a Moteino'));
assert.deepEqual(spy.getCall(0).args[0], Buffer.from('This could be'));
assert.deepEqual(spy.getCall(1).args[0], Buffer.from('binary data'));
assert.deepEqual(spy.getCall(2).args[0], Buffer.from('sent from a Moteino'));
});
});

View file

@ -1,6 +1,7 @@
'use strict';
/* eslint-disable no-new */
const Buffer = require('safe-buffer').Buffer;
const assert = require('chai').assert;
const sinon = require('sinon');
@ -17,9 +18,9 @@ describe('ReadlineParser', () => {
const spy = sinon.spy();
const parser = new ReadlineParser();
parser.on('data', spy);
parser.write(new Buffer('I love robots\nEach '));
parser.write(new Buffer('and Every One\n'));
parser.write(new Buffer('even you!'));
parser.write(Buffer.from('I love robots\nEach '));
parser.write(Buffer.from('and Every One\n'));
parser.write(Buffer.from('even you!'));
assert(spy.calledWith('I love robots'));
assert(spy.calledWith('Each and Every One'));
assert(spy.calledTwice);
@ -32,16 +33,16 @@ describe('ReadlineParser', () => {
const spy = sinon.spy();
const parser = new ReadlineParser({ delimiter: 'a' });
parser.on('data', spy);
parser.write(new Buffer('how are youa'));
parser.write(Buffer.from('how are youa'));
assert(spy.calledWith('how '));
assert(spy.calledWith('re you'));
});
it('allows setting of the delimiter with a buffer', () => {
const spy = sinon.spy();
const parser = new ReadlineParser({ delimiter: new Buffer('a') });
const parser = new ReadlineParser({ delimiter: Buffer.from('a') });
parser.on('data', spy);
parser.write(new Buffer('how are youa'));
parser.write(Buffer.from('how are youa'));
assert(spy.calledWith('how '));
assert(spy.calledWith('re you'));
});
@ -50,7 +51,7 @@ describe('ReadlineParser', () => {
const spy = sinon.spy();
const parser = new ReadlineParser({ delimiter: [97] });
parser.on('data', spy);
parser.write(new Buffer('how are youa'));
parser.write(Buffer.from('how are youa'));
assert(spy.calledWith('how '));
assert(spy.calledWith('re you'));
});
@ -61,7 +62,7 @@ describe('ReadlineParser', () => {
encoding: 'hex'
});
parser.on('data', spy);
parser.write(new Buffer('a\nb\n'));
parser.write(Buffer.from('a\nb\n'));
assert.equal(spy.getCall(0).args[0], '61');
assert.equal(spy.getCall(1).args[0], '62');
});
@ -73,7 +74,7 @@ describe('ReadlineParser', () => {
encoding: 'hex'
});
parser.on('data', spy);
parser.write(new Buffer([0, 255, 1, 255]));
parser.write(Buffer.from([0, 255, 1, 255]));
assert.equal(spy.getCall(0).args[0], '00');
assert.equal(spy.getCall(1).args[0], '01');
});
@ -81,7 +82,7 @@ describe('ReadlineParser', () => {
it('throws when called with a 0 length delimiter', () => {
assert.throws(() => {
new ReadlineParser({
delimiter: new Buffer(0)
delimiter: Buffer.from(0)
});
});
@ -103,7 +104,7 @@ describe('ReadlineParser', () => {
});
it('allows setting of the delimiter with a buffer', () => {
new ReadlineParser({ delimiter: new Buffer([1]) });
new ReadlineParser({ delimiter: Buffer.from([1]) });
});
it('allows setting of the delimiter with an array of bytes', () => {
@ -114,7 +115,7 @@ describe('ReadlineParser', () => {
const spy = sinon.spy();
const parser = new ReadlineParser({ delimiter: 'a' });
parser.on('data', spy);
parser.write(new Buffer('aFa'));
parser.write(Buffer.from('aFa'));
assert(spy.calledOnce);
assert(spy.calledWith('F'));
});

View file

@ -2,6 +2,7 @@
/* eslint-disable no-new */
const assert = require('chai').assert;
const Buffer = require('safe-buffer').Buffer;
const sinon = require('sinon');
const RegexParser = require('../lib/parsers/regex');
@ -9,7 +10,7 @@ const RegexParser = require('../lib/parsers/regex');
describe('RegexParser', () => {
it('works without new', () => {
// eslint-disable-next-line new-cap
const parser = RegexParser({ delimiter: new Buffer([0]) });
const parser = RegexParser({ delimiter: Buffer.from([0]) });
assert.instanceOf(parser, RegexParser);
});
@ -19,9 +20,9 @@ describe('RegexParser', () => {
delimiter: new RegExp(/\r\n|\n\r|\n|\r/)
});
parser.on('data', spy);
parser.write(new Buffer('I love robots\n\rEach '));
parser.write(new Buffer('and Every One\r'));
parser.write(new Buffer('even you!'));
parser.write(Buffer.from('I love robots\n\rEach '));
parser.write(Buffer.from('and Every One\r'));
parser.write(Buffer.from('even you!'));
assert.deepEqual(spy.getCall(0).args[0], 'I love robots');
assert.deepEqual(spy.getCall(1).args[0], 'Each and Every One');
@ -32,11 +33,11 @@ describe('RegexParser', () => {
const parser = RegexParser({ delimiter: /\n/ });
const spy = sinon.spy();
parser.on('data', spy);
parser.write(new Buffer([1]));
parser.write(Buffer.from([1]));
assert.equal(spy.callCount, 0);
parser.end();
assert.equal(spy.callCount, 1);
assert.deepEqual(spy.getCall(0).args[0], new Buffer([1]).toString());
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([1]).toString());
});
it('throws when not provided with a delimiter', () => {
@ -48,7 +49,7 @@ describe('RegexParser', () => {
it('throws when called with a 0 length delimiter', () => {
assert.throws(() => {
new RegexParser({
delimiter: new Buffer(0)
delimiter: Buffer.from(0)
});
});
@ -75,7 +76,7 @@ describe('RegexParser', () => {
});
it('allows setting of the delimiter with a buffer', () => {
const parser = new RegexParser({ delimiter: new Buffer('a|b') });
const parser = new RegexParser({ delimiter: Buffer.from('a|b') });
const spy = sinon.spy();
parser.on('data', spy);
parser.write('bhow are youa');
@ -90,13 +91,13 @@ describe('RegexParser', () => {
encoding: 'hex'
});
parser.on('data', spy);
parser.write(new Buffer('a\rb\r'));
parser.write(Buffer.from('a\rb\r'));
assert.equal(spy.getCall(0).args[0], '61');
assert.equal(spy.getCall(1).args[0], '62');
});
it('Works when buffer starts with regex delimiter', () => {
const data = new Buffer('\rHello\rWorld\r');
const data = Buffer.from('\rHello\rWorld\r');
const parser = new RegexParser({ delimiter: /\r/ });
const spy = sinon.spy();
parser.on('data', spy);
@ -105,7 +106,7 @@ describe('RegexParser', () => {
});
it('should match unicode in buffer string', () => {
const data = new Buffer('\u000aHello\u000aWorld\u000d\u000a!');
const data = Buffer.from('\u000aHello\u000aWorld\u000d\u000a!');
const parser = new RegexParser({ delimiter: /\r\n|\n/ });
const spy = sinon.spy();
parser.on('data', spy);
@ -117,8 +118,8 @@ describe('RegexParser', () => {
const parser = new RegexParser({ delimiter: /\r\n|\n/ });
const spy = sinon.spy();
parser.on('data', spy);
parser.write(new Buffer('This could be\na poem '));
parser.write(new Buffer('or prose\r\nsent from a robot\r\n'));
parser.write(Buffer.from('This could be\na poem '));
parser.write(Buffer.from('or prose\r\nsent from a robot\r\n'));
assert.equal(spy.callCount, 3);
assert.deepEqual(spy.getCall(0).args[0], 'This could be');
assert.deepEqual(spy.getCall(1).args[0], 'a poem or prose');
@ -129,7 +130,7 @@ describe('RegexParser', () => {
const spy = sinon.spy();
const parser = new RegexParser({ delimiter: /a|b/ });
parser.on('data', spy);
parser.write(new Buffer('abaFab'));
parser.write(Buffer.from('abaFab'));
assert(spy.calledOnce);
assert(spy.calledWith('F'));
});

View file

@ -1,7 +1,7 @@
'use strict';
const sinon = require('sinon');
const Buffer = require('safe-buffer').Buffer;
const chai = require('chai');
const sinon = require('sinon');
chai.use(require('chai-subset'));
const assert = chai.assert;
@ -232,7 +232,7 @@ describe('SerialPort', () => {
it('returns false when the port is opening', (done) => {
const port = new SerialPort('/dev/exists', { autoOpen: false });
sandbox.stub(port.binding, 'open', () => {
sandbox.stub(port.binding, 'open').callsFake(() => {
assert.isTrue(port.opening);
assert.isFalse(port.isOpen);
done();
@ -249,7 +249,7 @@ describe('SerialPort', () => {
it('returns false when the port is closing', (done) => {
const port = new SerialPort('/dev/exists', {}, function() {
sandbox.stub(this.binding, 'close', () => {
sandbox.stub(this.binding, 'close').callsFake(() => {
assert.isFalse(port.isOpen);
done();
return Promise.resolve();
@ -297,7 +297,7 @@ describe('SerialPort', () => {
stopBits: 1
};
const port = new SerialPort('/dev/exists', { autoOpen: false });
sandbox.stub(port.binding, 'open', (path, opt) => {
sandbox.stub(port.binding, 'open').callsFake((path, opt) => {
assert.equal(path, '/dev/exists');
assert.containSubset(opt, defaultOptions);
done();
@ -314,7 +314,7 @@ describe('SerialPort', () => {
});
it('emits data after being reopened', (done) => {
const data = new Buffer('Howdy!');
const data = Buffer.from('Howdy!');
const port = new SerialPort('/dev/exists', () => {
port.close(() => {
port.open(() => {
@ -351,7 +351,7 @@ describe('SerialPort', () => {
it('allows opening after an open error', (done) => {
const port = new SerialPort('/dev/exists', { autoOpen: false });
const stub = sandbox.stub(port.binding, 'open', () => {
const stub = sandbox.stub(port.binding, 'open').callsFake(() => {
return Promise.reject(new Error('Haha no'));
});
port.open((err) => {
@ -364,7 +364,7 @@ describe('SerialPort', () => {
describe('#write', () => {
it('writes to the bindings layer', (done) => {
const data = new Buffer('Crazy!');
const data = Buffer.from('Crazy!');
const port = new SerialPort('/dev/exists');
port.on('open', () => {
port.write(data, () => {
@ -380,7 +380,7 @@ describe('SerialPort', () => {
const data = 'Crazy!';
port.write(data, () => {
const lastWrite = port.binding.port.lastWrite;
assert.deepEqual(new Buffer(data), lastWrite);
assert.deepEqual(Buffer.from(data), lastWrite);
done();
});
});
@ -392,7 +392,7 @@ describe('SerialPort', () => {
const data = 'COFFEE';
port.write(data, 'hex', () => {
const lastWrite = port.binding.port.lastWrite;
assert.deepEqual(new Buffer(data, 'hex'), lastWrite);
assert.deepEqual(Buffer.from(data, 'hex'), lastWrite);
done();
});
});
@ -404,7 +404,7 @@ describe('SerialPort', () => {
const data = [0, 32, 44, 88];
port.write(data, () => {
const lastWrite = port.binding.port.lastWrite;
assert.deepEqual(new Buffer(data), lastWrite);
assert.deepEqual(Buffer.from(data), lastWrite);
done();
});
});
@ -422,9 +422,9 @@ describe('SerialPort', () => {
port.open(() => {
port.cork();
port.write('abc');
port.write(new Buffer('123'), () => {
port.write(Buffer.from('123'), () => {
assert.equal(spy.callCount, 1);
assert.deepEqual(port.binding.port.lastWrite, new Buffer('abc123'));
assert.deepEqual(port.binding.port.lastWrite, Buffer.from('abc123'));
done();
});
port.uncork();
@ -503,7 +503,7 @@ describe('SerialPort', () => {
it('handles errors in callback', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'close', () => {
sinon.stub(port.binding, 'close').callsFake(() => {
return Promise.reject(new Error('like tears in the rain'));
});
port.on('open', () => {
@ -516,7 +516,7 @@ describe('SerialPort', () => {
it('handles errors in event', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'close', () => {
sinon.stub(port.binding, 'close').callsFake(() => {
return Promise.reject(new Error('attack ships on fire off the shoulder of Orion'));
});
port.on('open', () => {
@ -571,7 +571,7 @@ describe('SerialPort', () => {
it('handles errors in callback', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'update', () => {
sinon.stub(port.binding, 'update').callsFake(() => {
return Promise.reject(new Error('like tears in the rain'));
});
port.on('open', () => {
@ -584,7 +584,7 @@ describe('SerialPort', () => {
it('handles errors in event', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'update', () => {
sinon.stub(port.binding, 'update').callsFake(() => {
return Promise.reject(new Error('attack ships on fire off the shoulder of Orion'));
});
port.on('open', () => {
@ -681,7 +681,7 @@ describe('SerialPort', () => {
it('handles errors in callback', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'set', () => {
sinon.stub(port.binding, 'set').callsFake(() => {
return Promise.reject(new Error('like tears in the rain'));
});
port.on('open', () => {
@ -694,7 +694,7 @@ describe('SerialPort', () => {
it('handles errors in event', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'set', () => {
sinon.stub(port.binding, 'set').callsFake(() => {
return Promise.reject(new Error('attack ships on fire off the shoulder of Orion'));
});
port.on('open', () => {
@ -730,7 +730,7 @@ describe('SerialPort', () => {
it('handles errors in callback', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'flush', () => {
sinon.stub(port.binding, 'flush').callsFake(() => {
return Promise.reject(new Error('like tears in the rain'));
});
port.on('open', () => {
@ -743,7 +743,7 @@ describe('SerialPort', () => {
it('handles errors in event', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'flush', () => {
sinon.stub(port.binding, 'flush').callsFake(() => {
return Promise.reject(new Error('attack ships on fire off the shoulder of Orion'));
});
port.on('open', () => {
@ -779,7 +779,7 @@ describe('SerialPort', () => {
it('handles errors in callback', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'drain', () => {
sinon.stub(port.binding, 'drain').callsFake(() => {
return Promise.reject(new Error('like tears in the rain'));
});
port.on('open', () => {
@ -792,7 +792,7 @@ describe('SerialPort', () => {
it('handles errors in event', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'drain', () => {
sinon.stub(port.binding, 'drain').callsFake(() => {
return Promise.reject(new Error('attack ships on fire off the shoulder of Orion'));
});
port.on('open', () => {
@ -832,7 +832,7 @@ describe('SerialPort', () => {
it('handles errors in callback', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'get', () => {
sinon.stub(port.binding, 'get').callsFake(() => {
return Promise.reject(new Error('like tears in the rain'));
});
port.on('open', () => {
@ -845,7 +845,7 @@ describe('SerialPort', () => {
it('handles errors in event', (done) => {
const port = new SerialPort('/dev/exists');
sinon.stub(port.binding, 'get', () => {
sinon.stub(port.binding, 'get').callsFake(() => {
return Promise.reject(new Error('attack ships on fire off the shoulder of Orion'));
});
port.on('open', () => {
@ -861,7 +861,7 @@ describe('SerialPort', () => {
describe('reading data', () => {
it('emits data events by default', (done) => {
const testData = new Buffer('I am a really short string');
const testData = Buffer.from('I am a really short string');
const port = new SerialPort('/dev/exists', () => {
port.once('data', (recvData) => {
assert.deepEqual(recvData, testData);