mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
This is a crazy diff that's unreadable. Prettier has gained a lot of traction I personally like it and a lot of code looks like this these days. It also supports linting and formatting that eslint doesn't on its own. That alone helps keep the code more standard. This PR is equivalent of ripping off a bandaid. - Add prittier - `npm run format`
55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
const Transform = require('stream').Transform
|
|
|
|
/**
|
|
* Emit data every number of bytes
|
|
* @extends Transform
|
|
* @param {Object} options parser options object
|
|
* @param {Number} options.length the number of bytes on each data event
|
|
* @summary A transform stream that emits data as a buffer after a specific number of bytes are received. Runs in O(n) time.
|
|
* @example
|
|
const SerialPort = require('serialport')
|
|
const ByteLength = require('@serialport/parser-byte-length')
|
|
const port = new SerialPort('/dev/tty-usbserial1')
|
|
const parser = port.pipe(new ByteLength({length: 8}))
|
|
parser.on('data', console.log) // will have 8 bytes per data event
|
|
*/
|
|
class ByteLengthParser extends Transform {
|
|
constructor(options = {}) {
|
|
super(options)
|
|
|
|
if (typeof options.length !== 'number') {
|
|
throw new TypeError('"length" is not a number')
|
|
}
|
|
|
|
if (options.length < 1) {
|
|
throw new TypeError('"length" is not greater than 0')
|
|
}
|
|
|
|
this.length = options.length
|
|
this.position = 0
|
|
this.buffer = Buffer.alloc(this.length)
|
|
}
|
|
|
|
_transform(chunk, encoding, cb) {
|
|
let cursor = 0
|
|
while (cursor < chunk.length) {
|
|
this.buffer[this.position] = chunk[cursor]
|
|
cursor++
|
|
this.position++
|
|
if (this.position === this.length) {
|
|
this.push(this.buffer)
|
|
this.buffer = Buffer.alloc(this.length)
|
|
this.position = 0
|
|
}
|
|
}
|
|
cb()
|
|
}
|
|
|
|
_flush(cb) {
|
|
this.push(this.buffer.slice(0, this.position))
|
|
this.buffer = Buffer.alloc(this.length)
|
|
cb()
|
|
}
|
|
}
|
|
|
|
module.exports = ByteLengthParser
|