node-mavlink 1.0.10 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +3 -0
- package/coverage/coverage-final.json +1 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +101 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/serialization.ts.html +613 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +0 -0
- package/dist/lib/mavesp.js +13 -24
- package/dist/lib/mavlink.js +21 -34
- package/dist/lib/serialization.js +1 -1
- package/dist/lib/utils.js +15 -26
- package/jest.config.js +5 -0
- package/lib/serialization.test.ts +256 -0
- package/lib/serialization.ts +1 -1
- package/package.json +8 -3
- package/tsconfig.json +1 -1
@@ -0,0 +1,196 @@
|
|
1
|
+
/* eslint-disable */
|
2
|
+
var addSorting = (function() {
|
3
|
+
'use strict';
|
4
|
+
var cols,
|
5
|
+
currentSort = {
|
6
|
+
index: 0,
|
7
|
+
desc: false
|
8
|
+
};
|
9
|
+
|
10
|
+
// returns the summary table element
|
11
|
+
function getTable() {
|
12
|
+
return document.querySelector('.coverage-summary');
|
13
|
+
}
|
14
|
+
// returns the thead element of the summary table
|
15
|
+
function getTableHeader() {
|
16
|
+
return getTable().querySelector('thead tr');
|
17
|
+
}
|
18
|
+
// returns the tbody element of the summary table
|
19
|
+
function getTableBody() {
|
20
|
+
return getTable().querySelector('tbody');
|
21
|
+
}
|
22
|
+
// returns the th element for nth column
|
23
|
+
function getNthColumn(n) {
|
24
|
+
return getTableHeader().querySelectorAll('th')[n];
|
25
|
+
}
|
26
|
+
|
27
|
+
function onFilterInput() {
|
28
|
+
const searchValue = document.getElementById('fileSearch').value;
|
29
|
+
const rows = document.getElementsByTagName('tbody')[0].children;
|
30
|
+
for (let i = 0; i < rows.length; i++) {
|
31
|
+
const row = rows[i];
|
32
|
+
if (
|
33
|
+
row.textContent
|
34
|
+
.toLowerCase()
|
35
|
+
.includes(searchValue.toLowerCase())
|
36
|
+
) {
|
37
|
+
row.style.display = '';
|
38
|
+
} else {
|
39
|
+
row.style.display = 'none';
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
// loads the search box
|
45
|
+
function addSearchBox() {
|
46
|
+
var template = document.getElementById('filterTemplate');
|
47
|
+
var templateClone = template.content.cloneNode(true);
|
48
|
+
templateClone.getElementById('fileSearch').oninput = onFilterInput;
|
49
|
+
template.parentElement.appendChild(templateClone);
|
50
|
+
}
|
51
|
+
|
52
|
+
// loads all columns
|
53
|
+
function loadColumns() {
|
54
|
+
var colNodes = getTableHeader().querySelectorAll('th'),
|
55
|
+
colNode,
|
56
|
+
cols = [],
|
57
|
+
col,
|
58
|
+
i;
|
59
|
+
|
60
|
+
for (i = 0; i < colNodes.length; i += 1) {
|
61
|
+
colNode = colNodes[i];
|
62
|
+
col = {
|
63
|
+
key: colNode.getAttribute('data-col'),
|
64
|
+
sortable: !colNode.getAttribute('data-nosort'),
|
65
|
+
type: colNode.getAttribute('data-type') || 'string'
|
66
|
+
};
|
67
|
+
cols.push(col);
|
68
|
+
if (col.sortable) {
|
69
|
+
col.defaultDescSort = col.type === 'number';
|
70
|
+
colNode.innerHTML =
|
71
|
+
colNode.innerHTML + '<span class="sorter"></span>';
|
72
|
+
}
|
73
|
+
}
|
74
|
+
return cols;
|
75
|
+
}
|
76
|
+
// attaches a data attribute to every tr element with an object
|
77
|
+
// of data values keyed by column name
|
78
|
+
function loadRowData(tableRow) {
|
79
|
+
var tableCols = tableRow.querySelectorAll('td'),
|
80
|
+
colNode,
|
81
|
+
col,
|
82
|
+
data = {},
|
83
|
+
i,
|
84
|
+
val;
|
85
|
+
for (i = 0; i < tableCols.length; i += 1) {
|
86
|
+
colNode = tableCols[i];
|
87
|
+
col = cols[i];
|
88
|
+
val = colNode.getAttribute('data-value');
|
89
|
+
if (col.type === 'number') {
|
90
|
+
val = Number(val);
|
91
|
+
}
|
92
|
+
data[col.key] = val;
|
93
|
+
}
|
94
|
+
return data;
|
95
|
+
}
|
96
|
+
// loads all row data
|
97
|
+
function loadData() {
|
98
|
+
var rows = getTableBody().querySelectorAll('tr'),
|
99
|
+
i;
|
100
|
+
|
101
|
+
for (i = 0; i < rows.length; i += 1) {
|
102
|
+
rows[i].data = loadRowData(rows[i]);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
// sorts the table using the data for the ith column
|
106
|
+
function sortByIndex(index, desc) {
|
107
|
+
var key = cols[index].key,
|
108
|
+
sorter = function(a, b) {
|
109
|
+
a = a.data[key];
|
110
|
+
b = b.data[key];
|
111
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
112
|
+
},
|
113
|
+
finalSorter = sorter,
|
114
|
+
tableBody = document.querySelector('.coverage-summary tbody'),
|
115
|
+
rowNodes = tableBody.querySelectorAll('tr'),
|
116
|
+
rows = [],
|
117
|
+
i;
|
118
|
+
|
119
|
+
if (desc) {
|
120
|
+
finalSorter = function(a, b) {
|
121
|
+
return -1 * sorter(a, b);
|
122
|
+
};
|
123
|
+
}
|
124
|
+
|
125
|
+
for (i = 0; i < rowNodes.length; i += 1) {
|
126
|
+
rows.push(rowNodes[i]);
|
127
|
+
tableBody.removeChild(rowNodes[i]);
|
128
|
+
}
|
129
|
+
|
130
|
+
rows.sort(finalSorter);
|
131
|
+
|
132
|
+
for (i = 0; i < rows.length; i += 1) {
|
133
|
+
tableBody.appendChild(rows[i]);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
// removes sort indicators for current column being sorted
|
137
|
+
function removeSortIndicators() {
|
138
|
+
var col = getNthColumn(currentSort.index),
|
139
|
+
cls = col.className;
|
140
|
+
|
141
|
+
cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
|
142
|
+
col.className = cls;
|
143
|
+
}
|
144
|
+
// adds sort indicators for current column being sorted
|
145
|
+
function addSortIndicators() {
|
146
|
+
getNthColumn(currentSort.index).className += currentSort.desc
|
147
|
+
? ' sorted-desc'
|
148
|
+
: ' sorted';
|
149
|
+
}
|
150
|
+
// adds event listeners for all sorter widgets
|
151
|
+
function enableUI() {
|
152
|
+
var i,
|
153
|
+
el,
|
154
|
+
ithSorter = function ithSorter(i) {
|
155
|
+
var col = cols[i];
|
156
|
+
|
157
|
+
return function() {
|
158
|
+
var desc = col.defaultDescSort;
|
159
|
+
|
160
|
+
if (currentSort.index === i) {
|
161
|
+
desc = !currentSort.desc;
|
162
|
+
}
|
163
|
+
sortByIndex(i, desc);
|
164
|
+
removeSortIndicators();
|
165
|
+
currentSort.index = i;
|
166
|
+
currentSort.desc = desc;
|
167
|
+
addSortIndicators();
|
168
|
+
};
|
169
|
+
};
|
170
|
+
for (i = 0; i < cols.length; i += 1) {
|
171
|
+
if (cols[i].sortable) {
|
172
|
+
// add the click event handler on the th so users
|
173
|
+
// dont have to click on those tiny arrows
|
174
|
+
el = getNthColumn(i).querySelector('.sorter').parentElement;
|
175
|
+
if (el.addEventListener) {
|
176
|
+
el.addEventListener('click', ithSorter(i));
|
177
|
+
} else {
|
178
|
+
el.attachEvent('onclick', ithSorter(i));
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
}
|
183
|
+
// adds sorting functionality to the UI
|
184
|
+
return function() {
|
185
|
+
if (!getTable()) {
|
186
|
+
return;
|
187
|
+
}
|
188
|
+
cols = loadColumns();
|
189
|
+
loadData();
|
190
|
+
addSearchBox();
|
191
|
+
addSortIndicators();
|
192
|
+
enableUI();
|
193
|
+
};
|
194
|
+
})();
|
195
|
+
|
196
|
+
window.addEventListener('load', addSorting);
|
File without changes
|
package/dist/lib/mavesp.js
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.MavEsp8266 = void 0;
|
13
4
|
const events_1 = require("events");
|
@@ -41,21 +32,19 @@ class MavEsp8266 extends events_1.EventEmitter {
|
|
41
32
|
* @param receivePort port to receive messages on (default: 14550)
|
42
33
|
* @param sendPort port to send messages to (default: 14555)
|
43
34
|
*/
|
44
|
-
start(receivePort = 14550, sendPort = 14555) {
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
.catch(e => { reject(e); });
|
58
|
-
});
|
35
|
+
async start(receivePort = 14550, sendPort = 14555) {
|
36
|
+
this.sendPort = sendPort;
|
37
|
+
// Create a UDP socket
|
38
|
+
this.socket = (0, dgram_1.createSocket)({ type: 'udp4', reuseAddr: true });
|
39
|
+
this.socket.on('message', this.processIncommingUDPData);
|
40
|
+
// Start listening on the socket
|
41
|
+
return new Promise((resolve, reject) => {
|
42
|
+
this.socket.bind(receivePort, () => {
|
43
|
+
// Wait for the first package to be returned to read the ip address
|
44
|
+
// of the controller
|
45
|
+
(0, utils_1.waitFor)(() => this.ip !== '')
|
46
|
+
.then(() => { resolve(this.ip); })
|
47
|
+
.catch(e => { reject(e); });
|
59
48
|
});
|
60
49
|
});
|
61
50
|
}
|
package/dist/lib/mavlink.js
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.sendSigned = exports.send = exports.createMavLinkStream = exports.MavLinkPacketParser = exports.MavLinkPacketSplitter = exports.MavLinkPacket = exports.MavLinkPacketSignature = exports.MavLinkProtocolV2 = exports.MavLinkProtocolV1 = exports.MavLinkProtocol = exports.MavLinkPacketHeader = void 0;
|
13
4
|
const stream_1 = require("stream");
|
@@ -596,7 +587,7 @@ exports.MavLinkPacketSplitter = MavLinkPacketSplitter;
|
|
596
587
|
*/
|
597
588
|
class MavLinkPacketParser extends stream_1.Transform {
|
598
589
|
constructor(opts = {}) {
|
599
|
-
super(
|
590
|
+
super({ ...opts, objectMode: true });
|
600
591
|
this.log = logger_1.Logger.getLogger(this);
|
601
592
|
}
|
602
593
|
getProtocol(buffer) {
|
@@ -643,17 +634,15 @@ let seq = 0;
|
|
643
634
|
* @param protocol protocol to use (default: MavLinkProtocolV1)
|
644
635
|
* @returns number of bytes sent
|
645
636
|
*/
|
646
|
-
function send(stream, msg, protocol = new MavLinkProtocolV1()) {
|
647
|
-
return
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
resolve(buffer.length);
|
656
|
-
});
|
637
|
+
async function send(stream, msg, protocol = new MavLinkProtocolV1()) {
|
638
|
+
return new Promise((resolve, reject) => {
|
639
|
+
const buffer = protocol.serialize(msg, seq++);
|
640
|
+
seq &= 255;
|
641
|
+
stream.write(buffer, err => {
|
642
|
+
if (err)
|
643
|
+
reject(err);
|
644
|
+
else
|
645
|
+
resolve(buffer.length);
|
657
646
|
});
|
658
647
|
});
|
659
648
|
}
|
@@ -670,19 +659,17 @@ exports.send = send;
|
|
670
659
|
* @param timestamp optional timestamp for packet signing (default: Date.now())
|
671
660
|
* @returns number of bytes sent
|
672
661
|
*/
|
673
|
-
function sendSigned(stream, msg, key, linkId = 1, sysid = MavLinkProtocol.SYS_ID, compid = MavLinkProtocol.COMP_ID, timestamp = Date.now()) {
|
674
|
-
return
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
resolve(b2.length);
|
685
|
-
});
|
662
|
+
async function sendSigned(stream, msg, key, linkId = 1, sysid = MavLinkProtocol.SYS_ID, compid = MavLinkProtocol.COMP_ID, timestamp = Date.now()) {
|
663
|
+
return new Promise((resolve, reject) => {
|
664
|
+
const protocol = new MavLinkProtocolV2(sysid, compid, MavLinkProtocolV2.IFLAG_SIGNED);
|
665
|
+
const b1 = protocol.serialize(msg, seq++);
|
666
|
+
seq &= 255;
|
667
|
+
const b2 = protocol.sign(b1, linkId, key, timestamp);
|
668
|
+
stream.write(b2, err => {
|
669
|
+
if (err)
|
670
|
+
reject(err);
|
671
|
+
else
|
672
|
+
resolve(b2.length);
|
686
673
|
});
|
687
674
|
});
|
688
675
|
}
|
@@ -166,7 +166,7 @@ exports.DESERIALIZERS = {
|
|
166
166
|
'double[]': (buffer, offset, length) => {
|
167
167
|
const result = new Array(length);
|
168
168
|
for (let i = 0; i < length; i++)
|
169
|
-
result[i] = buffer.
|
169
|
+
result[i] = buffer.readDoubleLE(offset + i * 8);
|
170
170
|
return result;
|
171
171
|
},
|
172
172
|
};
|
package/dist/lib/utils.js
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.waitFor = exports.sleep = exports.dump = exports.hex = void 0;
|
13
4
|
/**
|
@@ -64,25 +55,23 @@ exports.sleep = sleep;
|
|
64
55
|
* @param timeout number of miliseconds that need to pass before the Timeout exception is thrown
|
65
56
|
* @param interval number of miliseconds before re-running the callback
|
66
57
|
*/
|
67
|
-
function waitFor(cb, timeout = 10000, interval = 100) {
|
68
|
-
return
|
69
|
-
|
70
|
-
|
58
|
+
async function waitFor(cb, timeout = 10000, interval = 100) {
|
59
|
+
return new Promise((resolve, reject) => {
|
60
|
+
const timeoutTimer = setTimeout(() => {
|
61
|
+
cleanup();
|
62
|
+
reject('Timeout');
|
63
|
+
}, timeout);
|
64
|
+
const intervalTimer = setInterval(() => {
|
65
|
+
const result = cb();
|
66
|
+
if (result) {
|
71
67
|
cleanup();
|
72
|
-
|
73
|
-
}
|
74
|
-
const intervalTimer = setInterval(() => {
|
75
|
-
const result = cb();
|
76
|
-
if (result) {
|
77
|
-
cleanup();
|
78
|
-
resolve(result);
|
79
|
-
}
|
80
|
-
});
|
81
|
-
const cleanup = () => {
|
82
|
-
clearTimeout(timeoutTimer);
|
83
|
-
clearTimeout(intervalTimer);
|
84
|
-
};
|
68
|
+
resolve(result);
|
69
|
+
}
|
85
70
|
});
|
71
|
+
const cleanup = () => {
|
72
|
+
clearTimeout(timeoutTimer);
|
73
|
+
clearTimeout(intervalTimer);
|
74
|
+
};
|
86
75
|
});
|
87
76
|
}
|
88
77
|
exports.waitFor = waitFor;
|
package/jest.config.js
ADDED
@@ -0,0 +1,256 @@
|
|
1
|
+
import { SERIALIZERS, DESERIALIZERS } from './serialization'
|
2
|
+
|
3
|
+
describe('serialization', () => {
|
4
|
+
describe('char', () => {
|
5
|
+
it('will serialize char', () => {
|
6
|
+
const b = Buffer.from([0])
|
7
|
+
SERIALIZERS['char'](42, b, 0)
|
8
|
+
expect(b).toStrictEqual(Buffer.from([42]))
|
9
|
+
})
|
10
|
+
it('will deserialize char', () => {
|
11
|
+
const b = Buffer.from([42])
|
12
|
+
const result = DESERIALIZERS['char'](b, 0)
|
13
|
+
expect(result).toBe('*')
|
14
|
+
})
|
15
|
+
it('will serialize char[]', () => {
|
16
|
+
const b = Buffer.from(new Array(3))
|
17
|
+
SERIALIZERS['char[]']('LOL', b, 0, 5)
|
18
|
+
expect(b).toStrictEqual(Buffer.from([76, 79, 76]))
|
19
|
+
})
|
20
|
+
it('will deserialize char[]', () => {
|
21
|
+
const b = Buffer.from([76, 79, 76, 0, 0, 0, 0, 0])
|
22
|
+
const result = DESERIALIZERS['char[]'](b, 0, 5)
|
23
|
+
expect(result).toBe('LOL')
|
24
|
+
})
|
25
|
+
})
|
26
|
+
|
27
|
+
describe('uint8_t', () => {
|
28
|
+
it('will serialize 8-bit unsigned int', () => {
|
29
|
+
const b = Buffer.from([0])
|
30
|
+
SERIALIZERS['uint8_t'](2, b, 0)
|
31
|
+
expect(b).toStrictEqual(Buffer.from([2]))
|
32
|
+
})
|
33
|
+
it('will deserialize 8-bit unsigned int', () => {
|
34
|
+
const b = Buffer.from([2])
|
35
|
+
const result = DESERIALIZERS['uint8_t'](b, 0)
|
36
|
+
expect(result).toBe(2)
|
37
|
+
})
|
38
|
+
it('will serialize array of 8-bit unsigned int', () => {
|
39
|
+
const b = Buffer.from(new Array(3))
|
40
|
+
SERIALIZERS['uint8_t[]']([ 1, 2, 4 ], b, 0, 3)
|
41
|
+
expect(b).toStrictEqual(Buffer.from([1, 2, 4]))
|
42
|
+
})
|
43
|
+
it('will deserialize array of 8-bit unsigned int', () => {
|
44
|
+
const b = Buffer.from([1, 2, 4])
|
45
|
+
const result = DESERIALIZERS['uint8_t[]'](b, 0, 3)
|
46
|
+
expect(result).toStrictEqual([1, 2, 4])
|
47
|
+
})
|
48
|
+
})
|
49
|
+
|
50
|
+
describe('int8_t', () => {
|
51
|
+
it('will serialize 8-bit signed int', () => {
|
52
|
+
const b = Buffer.from([0])
|
53
|
+
SERIALIZERS['int8_t'](-2, b, 0)
|
54
|
+
expect(b).toStrictEqual(Buffer.from([-2]))
|
55
|
+
})
|
56
|
+
it('will deserialize 8-bit signed int', () => {
|
57
|
+
const b = Buffer.from([-2])
|
58
|
+
const result = DESERIALIZERS['int8_t'](b, 0)
|
59
|
+
expect(result).toBe(-2)
|
60
|
+
})
|
61
|
+
it('will serialize array of 8-bit signed int', () => {
|
62
|
+
const b = Buffer.from(new Array(3))
|
63
|
+
SERIALIZERS['int8_t[]']([ 1, -2, -5 ], b, 0, 3)
|
64
|
+
expect(b).toStrictEqual(Buffer.from([1, -2, -5]))
|
65
|
+
})
|
66
|
+
it('will deserialize array of 8-bit signed int', () => {
|
67
|
+
const b = Buffer.from([255, 254, 252])
|
68
|
+
const result = DESERIALIZERS['int8_t[]'](b, 0, 3)
|
69
|
+
expect(result).toStrictEqual([-1, -2, -4])
|
70
|
+
})
|
71
|
+
})
|
72
|
+
|
73
|
+
describe('uint16_t', () => {
|
74
|
+
it('will serialize 16-bit unsigned int', () => {
|
75
|
+
const b = Buffer.from([0, 0])
|
76
|
+
SERIALIZERS['uint16_t'](2, b, 0)
|
77
|
+
expect(b).toStrictEqual(Buffer.from([2, 0]))
|
78
|
+
})
|
79
|
+
it('will deserialize 16-bit unsigned int', () => {
|
80
|
+
const b = Buffer.from([2, 0])
|
81
|
+
const result = DESERIALIZERS['uint16_t'](b, 0)
|
82
|
+
expect(result).toBe(2)
|
83
|
+
})
|
84
|
+
it('will serialize array of 16-bit unsigned int', () => {
|
85
|
+
const b = Buffer.from(new Array(6))
|
86
|
+
SERIALIZERS['uint16_t[]']([ 1, 2, 4 ], b, 0, 3)
|
87
|
+
expect(b).toStrictEqual(Buffer.from([1, 0, 2, 0, 4, 0]))
|
88
|
+
})
|
89
|
+
it('will deserialize array of 16-bit unsigned int', () => {
|
90
|
+
const b = Buffer.from([1, 0, 2, 0, 4, 0])
|
91
|
+
const result = DESERIALIZERS['uint16_t[]'](b, 0, 3)
|
92
|
+
expect(result).toStrictEqual([1, 2, 4])
|
93
|
+
})
|
94
|
+
})
|
95
|
+
|
96
|
+
describe('int16_t', () => {
|
97
|
+
it('will serialize 16-bit signed int', () => {
|
98
|
+
const b = Buffer.from([0, 0])
|
99
|
+
SERIALIZERS['int16_t'](-2, b, 0)
|
100
|
+
expect(b).toStrictEqual(Buffer.from([254, 255]))
|
101
|
+
})
|
102
|
+
it('will deserialize 16-bit signed int', () => {
|
103
|
+
const b = Buffer.from([254, 255])
|
104
|
+
const result = DESERIALIZERS['int16_t'](b, 0)
|
105
|
+
expect(result).toBe(-2)
|
106
|
+
})
|
107
|
+
it('will serialize array of 16-bit signed int', () => {
|
108
|
+
const b = Buffer.from(new Array(6))
|
109
|
+
SERIALIZERS['int16_t[]']([1, -2, -5], b, 0, 3)
|
110
|
+
expect(b).toStrictEqual(Buffer.from([1, 0, 254, 255, 251, 255]))
|
111
|
+
})
|
112
|
+
it('will deserialize array of 16-bit signed int', () => {
|
113
|
+
const b = Buffer.from([1, 0, 254, 255, 251, 255])
|
114
|
+
const result = DESERIALIZERS['int16_t[]'](b, 0, 3)
|
115
|
+
expect(result).toStrictEqual([1, -2, -5])
|
116
|
+
})
|
117
|
+
})
|
118
|
+
|
119
|
+
describe('uint32_t', () => {
|
120
|
+
it('will serialize 32-bit unsigned int', () => {
|
121
|
+
const b = Buffer.from([0, -1, -2, -3])
|
122
|
+
SERIALIZERS['uint32_t'](2, b, 0)
|
123
|
+
expect(b).toStrictEqual(Buffer.from([2, 0, 0, 0]))
|
124
|
+
})
|
125
|
+
it('will deserialize 32-bit unsigned int', () => {
|
126
|
+
const b = Buffer.from([2, 0, 0, 0])
|
127
|
+
const result = DESERIALIZERS['uint32_t'](b, 0)
|
128
|
+
expect(result).toBe(2)
|
129
|
+
})
|
130
|
+
it('will serialize array of 32-bit unsigned int', () => {
|
131
|
+
const b = Buffer.from(new Array(12))
|
132
|
+
SERIALIZERS['uint32_t[]']([ 1, 2, 4 ], b, 0, 3)
|
133
|
+
expect(b).toStrictEqual(Buffer.from([1, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0]))
|
134
|
+
})
|
135
|
+
it('will deserialize array of 32-bit unsigned int', () => {
|
136
|
+
const b = Buffer.from([1, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0])
|
137
|
+
const result = DESERIALIZERS['uint32_t[]'](b, 0, 3)
|
138
|
+
expect(result).toStrictEqual([1, 2, 5])
|
139
|
+
})
|
140
|
+
})
|
141
|
+
|
142
|
+
describe('int32_t', () => {
|
143
|
+
it('will serialize 32-bit signed int', () => {
|
144
|
+
const b = Buffer.from([0, 0, 0, 0])
|
145
|
+
SERIALIZERS['int32_t'](-2, b, 0)
|
146
|
+
expect(b).toStrictEqual(Buffer.from([254, 255, 255, 255]))
|
147
|
+
})
|
148
|
+
it('will deserialize 32-bit signed int', () => {
|
149
|
+
const b = Buffer.from([254, 255, 255, 255])
|
150
|
+
const result = DESERIALIZERS['int32_t'](b, 0)
|
151
|
+
expect(result).toBe(-2)
|
152
|
+
})
|
153
|
+
it('will serialize array of 32-bit signed int', () => {
|
154
|
+
const b = Buffer.from(new Array(12))
|
155
|
+
SERIALIZERS['int32_t[]']([ 1, -2, -5 ], b, 0, 3)
|
156
|
+
expect(b).toStrictEqual(Buffer.from([1, 0, 0, 0, 254, 255, 255, 255, 251, 255, 255, 255]))
|
157
|
+
})
|
158
|
+
it('will deserialize array of 32-bit signed int', () => {
|
159
|
+
const b = Buffer.from([1, 0, 0, 0, 254, 255, 255, 255, 250, 255, 255, 255])
|
160
|
+
const result = DESERIALIZERS['int32_t[]'](b, 0, 3)
|
161
|
+
expect(result).toStrictEqual([1, -2, -6])
|
162
|
+
})
|
163
|
+
})
|
164
|
+
|
165
|
+
describe('uint64_t', () => {
|
166
|
+
it('will serialize 64-bit unsigned int', () => {
|
167
|
+
const b = Buffer.from([0, -1, -2, -3, -4, -5, -6, -7])
|
168
|
+
SERIALIZERS['uint64_t'](2n, b, 0)
|
169
|
+
expect(b).toStrictEqual(Buffer.from([2, 0, 0, 0, 0, 0, 0, 0]))
|
170
|
+
})
|
171
|
+
it('will deserialize 64-bit unsigned int', () => {
|
172
|
+
const b = Buffer.from([2, 0, 0, 0, 0, 0, 0, 0])
|
173
|
+
const result = DESERIALIZERS['uint64_t'](b, 0)
|
174
|
+
expect(result).toBe(2n)
|
175
|
+
})
|
176
|
+
it('will serialize array of 64-bit unsigned int', () => {
|
177
|
+
const b = Buffer.from(new Array(24))
|
178
|
+
SERIALIZERS['uint64_t[]']([ 1n, 2n, 4n ], b, 0, 3)
|
179
|
+
expect(b).toStrictEqual(Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]))
|
180
|
+
})
|
181
|
+
it('will deserialize array of 64-bit unsigned int', () => {
|
182
|
+
const b = Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0])
|
183
|
+
const result = DESERIALIZERS['uint64_t[]'](b, 0, 3)
|
184
|
+
expect(result).toEqual([1n, 2n, 5n])
|
185
|
+
})
|
186
|
+
})
|
187
|
+
|
188
|
+
describe('int64_t', () => {
|
189
|
+
it('will serialize 64-bit signed int', () => {
|
190
|
+
const b = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0])
|
191
|
+
SERIALIZERS['int64_t'](-2n, b, 0)
|
192
|
+
expect(b).toStrictEqual(Buffer.from([254, 255, 255, 255, 255, 255, 255, 255]))
|
193
|
+
})
|
194
|
+
it('will deserialize 64-bit signed int', () => {
|
195
|
+
const b = Buffer.from([254, 255, 255, 255, 255, 255, 255, 255])
|
196
|
+
const result = DESERIALIZERS['int64_t'](b, 0)
|
197
|
+
expect(result).toBe(-2n)
|
198
|
+
})
|
199
|
+
it('will serialize array of 64-bit signed int', () => {
|
200
|
+
const b = Buffer.from(new Array(24))
|
201
|
+
SERIALIZERS['int64_t[]']([ 1n, -2n, -5n ], b, 0, 3)
|
202
|
+
expect(b).toStrictEqual(Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 255, 255, 255, 255, 251, 255, 255, 255, 255, 255, 255, 255]))
|
203
|
+
})
|
204
|
+
it('will deserialize array of 64-bit signed int', () => {
|
205
|
+
const b = Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 255, 255, 255, 255, 250, 255, 255, 255, 255, 255, 255, 255])
|
206
|
+
const result = DESERIALIZERS['int64_t[]'](b, 0, 3)
|
207
|
+
expect(result).toEqual([1n, -2n, -6n])
|
208
|
+
})
|
209
|
+
})
|
210
|
+
|
211
|
+
describe('float', () => {
|
212
|
+
it('will serialize float', () => {
|
213
|
+
const b = Buffer.from([0, -1, -2, -3])
|
214
|
+
SERIALIZERS['float'](2.5, b, 0)
|
215
|
+
expect(b).toStrictEqual(Buffer.from([0, 0, 32, 64]))
|
216
|
+
})
|
217
|
+
it('will deserialize float', () => {
|
218
|
+
const b = Buffer.from([0, 0, 32, 192])
|
219
|
+
const result = DESERIALIZERS['float'](b, 0)
|
220
|
+
expect(result).toBe(-2.5)
|
221
|
+
})
|
222
|
+
it('will serialize array of floats', () => {
|
223
|
+
const b = Buffer.from(new Array(12))
|
224
|
+
SERIALIZERS['float[]']([ 1, 2, 4 ], b, 0, 3)
|
225
|
+
expect(b).toStrictEqual(Buffer.from([0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 128, 64]))
|
226
|
+
})
|
227
|
+
it('will deserialize array of floats', () => {
|
228
|
+
const b = Buffer.from([0, 0, 192, 63, 0, 0, 32, 192, 0, 0, 128, 64])
|
229
|
+
const result = DESERIALIZERS['float[]'](b, 0, 3)
|
230
|
+
expect(result).toStrictEqual([1.5, -2.5, 4])
|
231
|
+
})
|
232
|
+
})
|
233
|
+
|
234
|
+
describe('double', () => {
|
235
|
+
it('will serialize double', () => {
|
236
|
+
const b = Buffer.from([0, -1, -2, -3, -4, -5, -6, -7])
|
237
|
+
SERIALIZERS['double'](2.34, b, 0)
|
238
|
+
expect(b).toStrictEqual(Buffer.from([184, 30, 133, 235, 81, 184, 2, 64]))
|
239
|
+
})
|
240
|
+
it('will deserialize double', () => {
|
241
|
+
const b = Buffer.from([184, 30, 133, 235, 81, 184, 2, 64])
|
242
|
+
const result = DESERIALIZERS['double'](b, 0)
|
243
|
+
expect(result).toBe(2.34)
|
244
|
+
})
|
245
|
+
it('will serialize array of doubles', () => {
|
246
|
+
const b = Buffer.from(new Array(24))
|
247
|
+
SERIALIZERS['double[]']([ 1.5, -2.5, 4 ], b, 0, 3)
|
248
|
+
expect(b).toStrictEqual(Buffer.from([0, 0, 0, 0, 0, 0, 248, 63, 0, 0, 0, 0, 0, 0, 4, 192, 0, 0, 0, 0, 0, 0, 16, 64]))
|
249
|
+
})
|
250
|
+
it('will deserialize array of doubles', () => {
|
251
|
+
const b = Buffer.from([0, 0, 0, 0, 0, 0, 248, 63, 0, 0, 0, 0, 0, 0, 4, 192, 0, 0, 0, 0, 0, 0, 16, 64])
|
252
|
+
const result = DESERIALIZERS['double[]'](b, 0, 3)
|
253
|
+
expect(result).toStrictEqual([1.5, -2.5, 4])
|
254
|
+
})
|
255
|
+
})
|
256
|
+
})
|
package/lib/serialization.ts
CHANGED
@@ -170,7 +170,7 @@ export const DESERIALIZERS = {
|
|
170
170
|
},
|
171
171
|
'double[]': (buffer: Buffer, offset: number, length: number) => {
|
172
172
|
const result = new Array<number>(length)
|
173
|
-
for (let i = 0; i < length; i++) result[i] = buffer.
|
173
|
+
for (let i = 0; i < length; i++) result[i] = buffer.readDoubleLE(offset + i * 8)
|
174
174
|
return result
|
175
175
|
},
|
176
176
|
}
|