UNPKG

1.79 kBJavaScriptView Raw
1'use strict';
2
3
4var Transform = require('stream').Transform;
5var streamParser = require('stream-parser');
6var inherits = require('util').inherits;
7
8
9function ParserStream() {
10 Transform.call(this, { readableObjectMode: true });
11}
12
13inherits(ParserStream, Transform);
14streamParser(ParserStream.prototype);
15
16
17exports.ParserStream = ParserStream;
18
19
20exports.sliceEq = function (src, start, dest) {
21 for (var i = start, j = 0; j < dest.length;) {
22 if (src[i++] !== dest[j++]) return false;
23 }
24 return true;
25};
26
27exports.str2arr = function (str, format) {
28 var arr = [], i = 0;
29
30 if (format && format === 'hex') {
31 while (i < str.length) {
32 arr.push(parseInt(str.slice(i, i + 2), 16));
33 i += 2;
34 }
35 } else {
36 for (; i < str.length; i++) {
37 /* eslint-disable no-bitwise */
38 arr.push(str.charCodeAt(i) & 0xFF);
39 }
40 }
41
42 return arr;
43};
44
45exports.readUInt16LE = function (data, offset) {
46 return data[offset] | (data[offset + 1] << 8);
47};
48
49exports.readUInt16BE = function (data, offset) {
50 return data[offset + 1] | (data[offset] << 8);
51};
52
53exports.readUInt32LE = function (data, offset) {
54 return data[offset] |
55 (data[offset + 1] << 8) |
56 (data[offset + 2] << 16) |
57 (data[offset + 3] * 0x1000000);
58};
59
60exports.readUInt32BE = function (data, offset) {
61 return data[offset + 3] |
62 (data[offset + 2] << 8) |
63 (data[offset + 1] << 16) |
64 (data[offset] * 0x1000000);
65};
66
67
68function ProbeError(message, code, statusCode) {
69 Error.call(this);
70 Error.captureStackTrace(this, this.constructor);
71
72 this.name = this.constructor.name;
73
74 this.message = message;
75 if (code) this.code = code;
76 if (statusCode) this.statusCode = statusCode;
77}
78
79// Inherit from Error
80require('inherits')(ProbeError, Error);
81
82
83exports.ProbeError = ProbeError;