node-mavlink 1.0.10-beta.1 → 1.0.12
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/dist/lib/serialization.js +1 -1
- package/jest.config.js +5 -0
- package/lib/serialization.test.ts +210 -0
- package/lib/serialization.ts +1 -1
- package/package.json +8 -3
@@ -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/jest.config.js
ADDED
@@ -0,0 +1,210 @@
|
|
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('float', () => {
|
166
|
+
it('will serialize float', () => {
|
167
|
+
const b = Buffer.from([0, -1, -2, -3])
|
168
|
+
SERIALIZERS['float'](2.5, b, 0)
|
169
|
+
expect(b).toStrictEqual(Buffer.from([0, 0, 32, 64]))
|
170
|
+
})
|
171
|
+
it('will deserialize float', () => {
|
172
|
+
const b = Buffer.from([0, 0, 32, 192])
|
173
|
+
const result = DESERIALIZERS['float'](b, 0)
|
174
|
+
expect(result).toBe(-2.5)
|
175
|
+
})
|
176
|
+
it('will serialize array of floats', () => {
|
177
|
+
const b = Buffer.from(new Array(12))
|
178
|
+
SERIALIZERS['float[]']([ 1, 2, 4 ], b, 0, 3)
|
179
|
+
expect(b).toStrictEqual(Buffer.from([0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 128, 64]))
|
180
|
+
})
|
181
|
+
it('will deserialize array of floats', () => {
|
182
|
+
const b = Buffer.from([0, 0, 192, 63, 0, 0, 32, 192, 0, 0, 128, 64])
|
183
|
+
const result = DESERIALIZERS['float[]'](b, 0, 3)
|
184
|
+
expect(result).toStrictEqual([1.5, -2.5, 4])
|
185
|
+
})
|
186
|
+
})
|
187
|
+
|
188
|
+
describe('double', () => {
|
189
|
+
it('will serialize double', () => {
|
190
|
+
const b = Buffer.from([0, -1, -2, -3, -4, -5, -6, -7])
|
191
|
+
SERIALIZERS['double'](2.34, b, 0)
|
192
|
+
expect(b).toStrictEqual(Buffer.from([184, 30, 133, 235, 81, 184, 2, 64]))
|
193
|
+
})
|
194
|
+
it('will deserialize double', () => {
|
195
|
+
const b = Buffer.from([184, 30, 133, 235, 81, 184, 2, 64])
|
196
|
+
const result = DESERIALIZERS['double'](b, 0)
|
197
|
+
expect(result).toBe(2.34)
|
198
|
+
})
|
199
|
+
it('will serialize array of doubles', () => {
|
200
|
+
const b = Buffer.from(new Array(24))
|
201
|
+
SERIALIZERS['double[]']([ 1.5, -2.5, 4 ], b, 0, 3)
|
202
|
+
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]))
|
203
|
+
})
|
204
|
+
it('will deserialize array of doubles', () => {
|
205
|
+
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])
|
206
|
+
const result = DESERIALIZERS['double[]'](b, 0, 3)
|
207
|
+
expect(result).toStrictEqual([1.5, -2.5, 4])
|
208
|
+
})
|
209
|
+
})
|
210
|
+
})
|
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
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "node-mavlink",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.12",
|
4
4
|
"author": "Matthias Hryniszak <padcom@gmail.com>",
|
5
5
|
"license": "LGPL",
|
6
6
|
"description": "MavLink definitions and parsing library",
|
@@ -26,15 +26,20 @@
|
|
26
26
|
},
|
27
27
|
"scripts": {
|
28
28
|
"build": "tsc",
|
29
|
-
"test": "
|
30
|
-
"
|
29
|
+
"test": "jest",
|
30
|
+
"dev": "jest --watch",
|
31
|
+
"test:e2e": "cd tests && ./main.ts e2e --input data.mavlink",
|
32
|
+
"prepublishOnly": "rm -rf dist && npm install && npm test && npm run test:e2e && npm run build"
|
31
33
|
},
|
32
34
|
"devDependencies": {
|
35
|
+
"@types/jest": "^27.4.1",
|
33
36
|
"@types/node": "^15.14.9",
|
34
37
|
"@types/serialport": "^8.0.1",
|
35
38
|
"@types/xml2js": "^0.4.8",
|
36
39
|
"@types/yargs": "^17.0.8",
|
40
|
+
"jest": "^27.5.1",
|
37
41
|
"serialport": "^9.0.7",
|
42
|
+
"ts-jest": "^27.1.4",
|
38
43
|
"ts-node": "^9.1.1",
|
39
44
|
"typescript": "^4.4.3",
|
40
45
|
"wget-improved": "^3.2.1",
|