@serialport/parser-slip-encoder
Advanced tools
Comparing version 1.0.5 to 2.0.1
{ | ||
"name": "@serialport/parser-slip-encoder", | ||
"main": "slip-encoder.js", | ||
"version": "1.0.5", | ||
"dependencies": { | ||
"safe-buffer": "^5.1.1" | ||
"version": "2.0.1", | ||
"engines": { | ||
"node": ">=6.0.0" | ||
}, | ||
@@ -14,4 +14,5 @@ "publishConfig": { | ||
"type": "git", | ||
"url": "git://github.com/node-serialport/parsers.git" | ||
} | ||
"url": "git://github.com/node-serialport/node-serialport.git" | ||
}, | ||
"gitHead": "40a06fb71d659940ed5058316b594b9da9957c2f" | ||
} |
@@ -1,1 +0,1 @@ | ||
See our api docs https://node-serialport.github.io/parsers/SlipEncoderParser.html | ||
See our api docs https://node-serialport.github.io/node-serialport/SlipEncoderParser.html |
@@ -1,9 +0,7 @@ | ||
'use strict' | ||
const Buffer = require('safe-buffer').Buffer | ||
const Transform = require('stream').Transform | ||
const END = 0xC0; | ||
const ESC = 0xDB; | ||
const ESC_END = 0xDC; | ||
const ESC_ESC = 0xDD; | ||
const END = 0xc0 | ||
const ESC = 0xdb | ||
const ESC_END = 0xdc | ||
const ESC_ESC = 0xdd | ||
@@ -30,48 +28,47 @@ /** | ||
class SlipEncoderParser extends Transform { | ||
constructor (options) { | ||
options = options || {} | ||
super(options) | ||
constructor(options = {}) { | ||
super(options) | ||
if (options.bluetoothQuirk) { | ||
this._bluetoothQuirk = true; | ||
} | ||
} | ||
if (options.bluetoothQuirk) { | ||
this._bluetoothQuirk = true | ||
} | ||
} | ||
_transform (chunk, encoding, cb) { | ||
const chunkLength = chunk.length; | ||
_transform(chunk, encoding, cb) { | ||
const chunkLength = chunk.length | ||
if (this._bluetoothQuirk && chunkLength === 0) { | ||
// Edge case: push no data. Bluetooth-quirky SLIP parsers don't like | ||
// lots of 0xC0s together. | ||
return cb(); | ||
} | ||
if (this._bluetoothQuirk && chunkLength === 0) { | ||
// Edge case: push no data. Bluetooth-quirky SLIP parsers don't like | ||
// lots of 0xC0s together. | ||
return cb() | ||
} | ||
// Allocate memory for the worst-case scenario: all bytes are escaped, | ||
// plus start and end separators. | ||
let encoded = Buffer.alloc((chunkLength * 2) + 2); | ||
let j = 0; | ||
// Allocate memory for the worst-case scenario: all bytes are escaped, | ||
// plus start and end separators. | ||
const encoded = Buffer.alloc(chunkLength * 2 + 2) | ||
let j = 0 | ||
if (this._bluetoothQuirk) { | ||
encoded[j++] = END; | ||
} | ||
if (this._bluetoothQuirk) { | ||
encoded[j++] = END | ||
} | ||
for (let i=0; i<chunkLength; i++) { | ||
let byte = chunk[i]; | ||
if (byte === END) { | ||
encoded[j++] = ESC; | ||
byte = ESC_END; | ||
} else if (byte === ESC) { | ||
encoded[j++] = ESC; | ||
byte = ESC_ESC; | ||
} | ||
for (let i = 0; i < chunkLength; i++) { | ||
let byte = chunk[i] | ||
if (byte === END) { | ||
encoded[j++] = ESC | ||
byte = ESC_END | ||
} else if (byte === ESC) { | ||
encoded[j++] = ESC | ||
byte = ESC_ESC | ||
} | ||
encoded[j++] = byte; | ||
} | ||
encoded[j++] = byte | ||
} | ||
encoded[j++] = END; | ||
encoded[j++] = END | ||
cb(null, encoded.slice(0, j)); | ||
} | ||
cb(null, encoded.slice(0, j)) | ||
} | ||
} | ||
module.exports = SlipEncoderParser |
@@ -1,5 +0,3 @@ | ||
'use strict' | ||
/* eslint-disable no-new */ | ||
const Buffer = require('safe-buffer').Buffer | ||
const sinon = require('sinon') | ||
@@ -10,105 +8,109 @@ | ||
describe('SlipEncoderParser', () => { | ||
it('Adds one delimiter to one-byte messages', () => { | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder() | ||
encoder.on('data', spy) | ||
it ('Adds one delimiter to one-byte messages', ()=>{ | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder(); | ||
encoder.on('data', spy); | ||
encoder.write(Buffer.from([0x01])) | ||
encoder.write(Buffer.from([0x80])) | ||
encoder.write(Buffer.from([0xff])) | ||
encoder.write(Buffer.from([0xa5])) | ||
encoder.write(Buffer.from([0x01])); | ||
encoder.write(Buffer.from([0x80])); | ||
encoder.write(Buffer.from([0xFF])); | ||
encoder.write(Buffer.from([0xA5])); | ||
assert.equal(spy.callCount, 4) | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0x01, 0xc0])) | ||
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([0x80, 0xc0])) | ||
assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0xff, 0xc0])) | ||
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([0xa5, 0xc0])) | ||
}) | ||
assert.equal(spy.callCount, 4); | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0x01, 0xC0])); | ||
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([0x80, 0xC0])); | ||
assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0xFF, 0xC0])); | ||
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([0xA5, 0xC0])); | ||
}); | ||
it('Adds two delimiters to one-byte messages with the bluetooth quirk', () => { | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder({ bluetoothQuirk: true }) | ||
encoder.on('data', spy) | ||
it ('Adds two delimiters to one-byte messages with the bluetooth quirk', ()=>{ | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder({ bluetoothQuirk: true }); | ||
encoder.on('data', spy); | ||
encoder.write(Buffer.from([0x01])) | ||
encoder.write(Buffer.from([0x80])) | ||
encoder.write(Buffer.from([0xff])) | ||
encoder.write(Buffer.from([0xa5])) | ||
encoder.write(Buffer.from([0x01])); | ||
encoder.write(Buffer.from([0x80])); | ||
encoder.write(Buffer.from([0xFF])); | ||
encoder.write(Buffer.from([0xA5])); | ||
assert.equal(spy.callCount, 4) | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0xc0, 0x01, 0xc0])) | ||
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([0xc0, 0x80, 0xc0])) | ||
assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0xc0, 0xff, 0xc0])) | ||
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([0xc0, 0xa5, 0xc0])) | ||
}) | ||
assert.equal(spy.callCount, 4); | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0xC0, 0x01, 0xC0])); | ||
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([0xC0, 0x80, 0xC0])); | ||
assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0xC0, 0xFF, 0xC0])); | ||
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([0xC0, 0xA5, 0xC0])); | ||
}); | ||
it('Adds one delimiter to zero-byte messages', () => { | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder() | ||
encoder.on('data', spy) | ||
it ('Adds one delimiter to zero-byte messages', ()=>{ | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder(); | ||
encoder.on('data', spy); | ||
encoder.write(Buffer.from([])) | ||
encoder.write(Buffer.from([])); | ||
assert.equal(spy.callCount, 1) | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0xc0])) | ||
}) | ||
assert.equal(spy.callCount, 1); | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0xC0])); | ||
}); | ||
it('Does nothing with zero-byte messages with the bluetooth quirk', () => { | ||
const spy = sinon.spy() | ||
it ('Does nothing with zero-byte messages with the bluetooth quirk', ()=>{ | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder({ bluetoothQuirk: true }) | ||
const encoder = new SlipEncoder({ bluetoothQuirk: true }); | ||
encoder.on('data', spy) | ||
encoder.on('data', spy); | ||
encoder.write(Buffer.from([])) | ||
encoder.write(Buffer.from([])) | ||
encoder.write(Buffer.from([])) | ||
encoder.write(Buffer.from([])) | ||
encoder.write(Buffer.from([])); | ||
encoder.write(Buffer.from([])); | ||
encoder.write(Buffer.from([])); | ||
encoder.write(Buffer.from([])); | ||
assert.equal(spy.callCount, 0) | ||
}) | ||
assert.equal(spy.callCount, 0); | ||
}); | ||
it('Escapes characters', () => { | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder() | ||
encoder.on('data', spy) | ||
encoder.write(Buffer.from([0x01])) | ||
encoder.write(Buffer.from([0xc0])) | ||
encoder.write(Buffer.from([0xdb])) | ||
encoder.write(Buffer.from([0xdc])) | ||
encoder.write(Buffer.from([0xdd])) | ||
encoder.write(Buffer.from([0xff])) | ||
it ('Escapes characters', ()=>{ | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder(); | ||
encoder.on('data', spy); | ||
assert.equal(spy.callCount, 6) | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0x01, 0xc0])) | ||
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([0xdb, 0xdc, 0xc0])) | ||
assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0xdb, 0xdd, 0xc0])) | ||
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([0xdc, 0xc0])) | ||
assert.deepEqual(spy.getCall(4).args[0], Buffer.from([0xdd, 0xc0])) | ||
assert.deepEqual(spy.getCall(5).args[0], Buffer.from([0xff, 0xc0])) | ||
}) | ||
encoder.write(Buffer.from([0x01])); | ||
encoder.write(Buffer.from([0xC0])); | ||
encoder.write(Buffer.from([0xDB])); | ||
encoder.write(Buffer.from([0xDC])); | ||
encoder.write(Buffer.from([0xDD])); | ||
encoder.write(Buffer.from([0xFF])); | ||
it('Escapes characters with the bluetooth quirk', () => { | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder({ bluetoothQuirk: true }) | ||
encoder.on('data', spy) | ||
assert.equal(spy.callCount, 6); | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0x01, 0xC0])); | ||
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([0xDB, 0xDC, 0xC0])); | ||
assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0xDB, 0xDD, 0xC0])); | ||
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([0xDC, 0xC0])); | ||
assert.deepEqual(spy.getCall(4).args[0], Buffer.from([0xDD, 0xC0])); | ||
assert.deepEqual(spy.getCall(5).args[0], Buffer.from([0xFF, 0xC0])); | ||
}); | ||
encoder.write(Buffer.from([0x01])) | ||
encoder.write(Buffer.from([0xc0])) | ||
encoder.write(Buffer.from([0xdb])) | ||
encoder.write(Buffer.from([0xdc])) | ||
encoder.write(Buffer.from([0xdd])) | ||
encoder.write(Buffer.from([0xff])) | ||
it ('Escapes characters with the bluetooth quirk', ()=>{ | ||
const spy = sinon.spy() | ||
const encoder = new SlipEncoder({ bluetoothQuirk: true }); | ||
encoder.on('data', spy); | ||
encoder.write(Buffer.from([0x01])); | ||
encoder.write(Buffer.from([0xC0])); | ||
encoder.write(Buffer.from([0xDB])); | ||
encoder.write(Buffer.from([0xDC])); | ||
encoder.write(Buffer.from([0xDD])); | ||
encoder.write(Buffer.from([0xFF])); | ||
assert.equal(spy.callCount, 6); | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0xC0, 0x01, 0xC0])); | ||
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([0xC0, 0xDB, 0xDC, 0xC0])); | ||
assert.deepEqual(spy.getCall(2).args[0], Buffer.from([0xC0, 0xDB, 0xDD, 0xC0])); | ||
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([0xC0, 0xDC, 0xC0])); | ||
assert.deepEqual(spy.getCall(4).args[0], Buffer.from([0xC0, 0xDD, 0xC0])); | ||
assert.deepEqual(spy.getCall(5).args[0], Buffer.from([0xC0, 0xFF, 0xC0])); | ||
}); | ||
}); | ||
assert.equal(spy.callCount, 6) | ||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([0xc0, 0x01, 0xc0])) | ||
assert.deepEqual( | ||
spy.getCall(1).args[0], | ||
Buffer.from([0xc0, 0xdb, 0xdc, 0xc0]) | ||
) | ||
assert.deepEqual( | ||
spy.getCall(2).args[0], | ||
Buffer.from([0xc0, 0xdb, 0xdd, 0xc0]) | ||
) | ||
assert.deepEqual(spy.getCall(3).args[0], Buffer.from([0xc0, 0xdc, 0xc0])) | ||
assert.deepEqual(spy.getCall(4).args[0], Buffer.from([0xc0, 0xdd, 0xc0])) | ||
assert.deepEqual(spy.getCall(5).args[0], Buffer.from([0xc0, 0xff, 0xc0])) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8261
0
6
154
1
- Removedsafe-buffer@^5.1.1
- Removedsafe-buffer@5.2.1(transitive)