@krokodilushka/mavlink-parser-browserify 0.1.0

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.
@@ -0,0 +1,18 @@
1
+ /// <reference types="node" />
2
+ type Serializer = ((value: any, buffer: Buffer, offset: number) => void) | ((value: any, buffer: Buffer, offset: number, maxLen: number) => void);
3
+ /**
4
+ * A dictionary containing functions that serialize a certain value based on the field type
5
+ */
6
+ export declare const SERIALIZERS: {
7
+ [x: string]: Serializer;
8
+ };
9
+ type Deserializer = ((buffer: Buffer, offset: number) => any) | ((buffer: Buffer, offset: number, length: number) => any);
10
+ type Deserializers = {
11
+ [key: string]: Deserializer;
12
+ };
13
+ /**
14
+ * A dictionary containing functions that deserialize a certain value based on the field type
15
+ */
16
+ export declare const DESERIALIZERS: Deserializers;
17
+ export {};
18
+ //# sourceMappingURL=serialization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialization.d.ts","sourceRoot":"","sources":["../../lib/serialization.ts"],"names":[],"mappings":";AAaA,KAAK,UAAU,GACd,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC,GACtD,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC,CAAA;AAiFvE;;GAEG;AACH,eAAO,MAAM,WAAW;;CAIvB,CAAA;AAED,KAAK,YAAY,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,GAAG,CAAC,CAAA;AACzH,KAAK,aAAa,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAA;CAAE,CAAA;AAqFpD;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,aAI3B,CAAA"}
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DESERIALIZERS = exports.SERIALIZERS = void 0;
4
+ const SPECIAL_TYPES_SERIALIZERS = {
5
+ 'uint8_t_mavlink_version': (value, buffer, offset) => buffer.writeUInt8(value, offset),
6
+ };
7
+ const SINGULAR_TYPES_SERIALIZERS = {
8
+ 'char': (value, buffer, offset) => buffer.writeUInt8(value, offset),
9
+ 'int8_t': (value, buffer, offset) => buffer.writeInt8(value, offset),
10
+ 'uint8_t': (value, buffer, offset) => buffer.writeUInt8(value, offset),
11
+ 'int16_t': (value, buffer, offset) => buffer.writeInt16LE(value, offset),
12
+ 'uint16_t': (value, buffer, offset) => buffer.writeUInt16LE(value, offset),
13
+ 'int32_t': (value, buffer, offset) => buffer.writeInt32LE(value, offset),
14
+ 'uint32_t': (value, buffer, offset) => buffer.writeUInt32LE(value, offset),
15
+ 'int64_t': (value, buffer, offset) => buffer.writeBigInt64LE(value, offset),
16
+ 'uint64_t': (value, buffer, offset) => buffer.writeBigUInt64LE(value, offset),
17
+ 'float': (value, buffer, offset) => buffer.writeFloatLE(value, offset),
18
+ 'double': (value, buffer, offset) => buffer.writeDoubleLE(value, offset),
19
+ };
20
+ const ARRAY_TYPES_SERIALIZERS = {
21
+ 'char[]': (value, buffer, offset, maxLen) => {
22
+ for (let i = 0; i < value.length && i < maxLen; i++) {
23
+ const code = value.charCodeAt(i);
24
+ buffer.writeUInt8(code, offset + i);
25
+ }
26
+ },
27
+ 'int8_t[]': (value, buffer, offset, maxLen) => {
28
+ for (let i = 0; i < value.length && i < maxLen; i++) {
29
+ buffer.writeInt8(value[i], offset + i);
30
+ }
31
+ },
32
+ 'uint8_t[]': (value, buffer, offset, maxLen) => {
33
+ for (let i = 0; i < value.length && i < maxLen; i++) {
34
+ buffer.writeUInt8(value[i], offset + i);
35
+ }
36
+ },
37
+ 'int16_t[]': (value, buffer, offset, maxLen) => {
38
+ for (let i = 0; i < value.length && i < maxLen; i++) {
39
+ buffer.writeInt16LE(value[i], offset + i * 2);
40
+ }
41
+ },
42
+ 'uint16_t[]': (value, buffer, offset, maxLen) => {
43
+ for (let i = 0; i < value.length && i < maxLen; i++) {
44
+ buffer.writeUInt16LE(value[i], offset + i * 2);
45
+ }
46
+ },
47
+ 'int32_t[]': (value, buffer, offset, maxLen) => {
48
+ for (let i = 0; i < value.length && i < maxLen; i++) {
49
+ buffer.writeInt32LE(value[i], offset + i * 4);
50
+ }
51
+ },
52
+ 'uint32_t[]': (value, buffer, offset, maxLen) => {
53
+ for (let i = 0; i < value.length && i < maxLen; i++) {
54
+ buffer.writeUInt32LE(value[i], offset + i * 4);
55
+ }
56
+ },
57
+ 'int64_t[]': (value, buffer, offset, maxLen) => {
58
+ for (let i = 0; i < value.length && i < maxLen; i++) {
59
+ buffer.writeBigInt64LE(value[i], offset + i * 8);
60
+ }
61
+ },
62
+ 'uint64_t[]': (value, buffer, offset, maxLen) => {
63
+ for (let i = 0; i < value.length && i < maxLen; i++) {
64
+ buffer.writeBigUInt64LE(value[i], offset + i * 8);
65
+ }
66
+ },
67
+ 'float[]': (value, buffer, offset, maxLen) => {
68
+ for (let i = 0; i < value.length && i < maxLen; i++) {
69
+ buffer.writeFloatLE(value[i], offset + i * 4);
70
+ }
71
+ },
72
+ 'double[]': (value, buffer, offset, maxLen) => {
73
+ for (let i = 0; i < value.length && i < maxLen; i++) {
74
+ buffer.writeDoubleLE(value[i], offset + i * 8);
75
+ }
76
+ },
77
+ };
78
+ /**
79
+ * A dictionary containing functions that serialize a certain value based on the field type
80
+ */
81
+ exports.SERIALIZERS = {
82
+ ...SPECIAL_TYPES_SERIALIZERS,
83
+ ...SINGULAR_TYPES_SERIALIZERS,
84
+ ...ARRAY_TYPES_SERIALIZERS,
85
+ };
86
+ const SPECIAL_DESERIALIZERS = {
87
+ 'uint8_t_mavlink_version': (buffer, offset) => buffer.readUInt8(offset),
88
+ };
89
+ const SINGULAR_TYPES_DESERIALIZERS = {
90
+ 'char': (buffer, offset) => String.fromCharCode(buffer.readUInt8(offset)),
91
+ 'int8_t': (buffer, offset) => buffer.readInt8(offset),
92
+ 'uint8_t': (buffer, offset) => buffer.readUInt8(offset),
93
+ 'int16_t': (buffer, offset) => buffer.readInt16LE(offset),
94
+ 'uint16_t': (buffer, offset) => buffer.readUInt16LE(offset),
95
+ 'int32_t': (buffer, offset) => buffer.readInt32LE(offset),
96
+ 'uint32_t': (buffer, offset) => buffer.readUInt32LE(offset),
97
+ 'int64_t': (buffer, offset) => buffer.readBigInt64LE(offset),
98
+ 'uint64_t': (buffer, offset) => buffer.readBigUInt64LE(offset),
99
+ 'float': (buffer, offset) => buffer.readFloatLE(offset),
100
+ 'double': (buffer, offset) => buffer.readDoubleLE(offset),
101
+ };
102
+ const ARRAY_TYPES_DESERIALIZERS = {
103
+ 'char[]': (buffer, offset, length) => {
104
+ let result = '';
105
+ for (let i = 0; i < length; i++) {
106
+ const charCode = buffer.readUInt8(offset + i);
107
+ if (charCode !== 0) {
108
+ result += String.fromCharCode(charCode);
109
+ }
110
+ else {
111
+ break;
112
+ }
113
+ }
114
+ return result;
115
+ },
116
+ 'int8_t[]': (buffer, offset, length) => {
117
+ const result = new Array(length);
118
+ for (let i = 0; i < length; i++)
119
+ result[i] = buffer.readInt8(offset + i);
120
+ return result;
121
+ },
122
+ 'uint8_t[]': (buffer, offset, length) => {
123
+ const result = new Array(length);
124
+ for (let i = 0; i < length; i++)
125
+ result[i] = buffer.readUInt8(offset + i);
126
+ return result;
127
+ },
128
+ 'int16_t[]': (buffer, offset, length) => {
129
+ const result = new Array(length);
130
+ for (let i = 0; i < length; i++)
131
+ result[i] = buffer.readInt16LE(offset + i * 2);
132
+ return result;
133
+ },
134
+ 'uint16_t[]': (buffer, offset, length) => {
135
+ const result = new Array(length);
136
+ for (let i = 0; i < length; i++)
137
+ result[i] = buffer.readUInt16LE(offset + i * 2);
138
+ return result;
139
+ },
140
+ 'int32_t[]': (buffer, offset, length) => {
141
+ const result = new Array(length);
142
+ for (let i = 0; i < length; i++)
143
+ result[i] = buffer.readInt32LE(offset + i * 4);
144
+ return result;
145
+ },
146
+ 'uint32_t[]': (buffer, offset, length) => {
147
+ const result = new Array(length);
148
+ for (let i = 0; i < length; i++)
149
+ result[i] = buffer.readUInt32LE(offset + i * 4);
150
+ return result;
151
+ },
152
+ 'int64_t[]': (buffer, offset, length) => {
153
+ const result = new Array(length);
154
+ for (let i = 0; i < length; i++)
155
+ result[i] = buffer.readBigInt64LE(offset + i * 8);
156
+ return result;
157
+ },
158
+ 'uint64_t[]': (buffer, offset, length) => {
159
+ const result = new Array(length);
160
+ for (let i = 0; i < length; i++)
161
+ result[i] = buffer.readBigUInt64LE(offset + i * 8);
162
+ return result;
163
+ },
164
+ 'float[]': (buffer, offset, length) => {
165
+ const result = new Array(length);
166
+ for (let i = 0; i < length; i++)
167
+ result[i] = buffer.readFloatLE(offset + i * 4);
168
+ return result;
169
+ },
170
+ 'double[]': (buffer, offset, length) => {
171
+ const result = new Array(length);
172
+ for (let i = 0; i < length; i++)
173
+ result[i] = buffer.readDoubleLE(offset + i * 8);
174
+ return result;
175
+ },
176
+ };
177
+ /**
178
+ * A dictionary containing functions that deserialize a certain value based on the field type
179
+ */
180
+ exports.DESERIALIZERS = {
181
+ ...SPECIAL_DESERIALIZERS,
182
+ ...SINGULAR_TYPES_DESERIALIZERS,
183
+ ...ARRAY_TYPES_DESERIALIZERS,
184
+ };
@@ -0,0 +1,36 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Convert a number to hexadecimal representation with a minumum
4
+ * number of characters and optional prefix (0x by default)
5
+ *
6
+ * @param n value to convert
7
+ * @param len length of the converted string (without prefix)
8
+ * @param prefix prefix to prepend the generated string with
9
+ */
10
+ export declare function hex(n: number, len?: number, prefix?: string): string;
11
+ /**
12
+ * Dump a buffer in a readable form
13
+ *
14
+ * @param buffer buffer to dump
15
+ * @param lineWidth width of the line, in bytes of buffer
16
+ */
17
+ export declare function dump(buffer: Buffer, lineWidth?: number): void;
18
+ /**
19
+ * Sleep for a given number of miliseconds
20
+ *
21
+ * @param {number} ms of miliseconds to sleep
22
+ */
23
+ export declare function sleep(ms: number): Promise<unknown>;
24
+ /**
25
+ * Execute a callback every <code>interval</code>ms and if it will not return
26
+ * a truthy value in the <code>timeout<code>ms then throw a Timeout exception.
27
+ * This is a very useful utility that will allow you to specify how often
28
+ * a particular expression should be evaluated and how long will it take to end
29
+ * the execution without success. Great for time-sensitive operations.
30
+ *
31
+ * @param cb callback to call every <code>interval</code>ms. Waiting stops if the callback returns a truthy value.
32
+ * @param timeout number of miliseconds that need to pass before the Timeout exception is thrown
33
+ * @param interval number of miliseconds before re-running the callback
34
+ */
35
+ export declare function waitFor<T>(cb: () => T, timeout?: number, interval?: number): Promise<T>;
36
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../lib/utils.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,GAAE,MAAU,EAAE,MAAM,SAAO,UAE5D;AAED;;;;;GAKG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,SAAK,QAclD;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,oBAE/B;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,OAAO,SAAQ,EAAE,QAAQ,SAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAoBzF"}
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.waitFor = exports.sleep = exports.dump = exports.hex = void 0;
4
+ /**
5
+ * Convert a number to hexadecimal representation with a minumum
6
+ * number of characters and optional prefix (0x by default)
7
+ *
8
+ * @param n value to convert
9
+ * @param len length of the converted string (without prefix)
10
+ * @param prefix prefix to prepend the generated string with
11
+ */
12
+ function hex(n, len = 2, prefix = '0x') {
13
+ return `${prefix}${n.toString(16).padStart(len, '0')}`;
14
+ }
15
+ exports.hex = hex;
16
+ /**
17
+ * Dump a buffer in a readable form
18
+ *
19
+ * @param buffer buffer to dump
20
+ * @param lineWidth width of the line, in bytes of buffer
21
+ */
22
+ function dump(buffer, lineWidth = 16) {
23
+ const line = [];
24
+ let address = 0;
25
+ for (let i = 0; i < buffer.length; i++) {
26
+ line.push(hex(buffer[i], 2, '0x'));
27
+ if (line.length === lineWidth) {
28
+ console.log(hex(address, 4), '|', line.join(' '));
29
+ address += lineWidth;
30
+ line.length = 0;
31
+ }
32
+ }
33
+ if (line.length > 0) {
34
+ console.log(hex(address, 4), '|', line.join(' '));
35
+ }
36
+ }
37
+ exports.dump = dump;
38
+ /**
39
+ * Sleep for a given number of miliseconds
40
+ *
41
+ * @param {number} ms of miliseconds to sleep
42
+ */
43
+ function sleep(ms) {
44
+ return new Promise(resolve => setTimeout(resolve, ms));
45
+ }
46
+ exports.sleep = sleep;
47
+ /**
48
+ * Execute a callback every <code>interval</code>ms and if it will not return
49
+ * a truthy value in the <code>timeout<code>ms then throw a Timeout exception.
50
+ * This is a very useful utility that will allow you to specify how often
51
+ * a particular expression should be evaluated and how long will it take to end
52
+ * the execution without success. Great for time-sensitive operations.
53
+ *
54
+ * @param cb callback to call every <code>interval</code>ms. Waiting stops if the callback returns a truthy value.
55
+ * @param timeout number of miliseconds that need to pass before the Timeout exception is thrown
56
+ * @param interval number of miliseconds before re-running the callback
57
+ */
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) {
67
+ cleanup();
68
+ resolve(result);
69
+ }
70
+ });
71
+ const cleanup = () => {
72
+ clearTimeout(timeoutTimer);
73
+ clearTimeout(intervalTimer);
74
+ };
75
+ });
76
+ }
77
+ exports.waitFor = waitFor;
Binary file
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@krokodilushka/mavlink-parser-browserify",
3
+ "version": "0.1.0",
4
+ "author": "Guillaume de Boyer <gdeboyer@gmail.com>",
5
+ "license": "LGPL",
6
+ "description": "MavLink definitions and parsing library for the browser",
7
+ "keywords": [
8
+ "mavlink"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/geopan/mavlink-parser-broserify"
13
+ },
14
+ "bugs": {
15
+ "email": "gdeboyer@gmail.com",
16
+ "url": "https://github.com/geopan/mavlink-parser-broserify/issues"
17
+ },
18
+ "funding": {
19
+ "type": "patreon",
20
+ "url": "https://www.patreon.com/padcom"
21
+ },
22
+ "main": "dist/index.js",
23
+ "types": "dist/index.d.ts",
24
+ "dependencies": {
25
+ "mavlink-mappings": "^1.0.13-20230115-3"
26
+ },
27
+ "scripts": {
28
+ "clean": "rm -rf dist lib/*.js",
29
+ "build": "tsc",
30
+ "test": "jest",
31
+ "dev": "jest --watch",
32
+ "sanity-check": "./sanity-check.cjs && ./sanity-check.mjs",
33
+ "prepublishOnly": "npm run clean && npm install && npm test && npm run build && npm run sanity-check"
34
+ },
35
+ "devDependencies": {
36
+ "@types/jest": "^27.4.1",
37
+ "@types/node": "^15.14.9",
38
+ "@types/xml2js": "^0.4.8",
39
+ "@types/yargs": "^17.0.8",
40
+ "jest": "^27.5.1",
41
+ "minimize-js": "^1.3.0",
42
+ "serialport": "^10.0.0",
43
+ "ts-jest": "^27.1.4",
44
+ "ts-node": "^10.6.0",
45
+ "typescript": "^4.7.4",
46
+ "wget-improved": "^3.2.1",
47
+ "xml2js": "^0.4.23",
48
+ "yargs": "^17.3.1"
49
+ }
50
+ }
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { join } = require('node:path')
4
+ const { createReadStream } = require('node:fs')
5
+ const { createMavLinkStream } = require('.')
6
+ const { minimal, common, ardupilotmega } = require('.')
7
+
8
+ const REGISTRY = {
9
+ ...minimal.REGISTRY,
10
+ ...common.REGISTRY,
11
+ ...ardupilotmega.REGISTRY,
12
+ }
13
+
14
+ const port = createReadStream(join(__dirname, 'examples/GH-5.bin'))
15
+ const parser = createMavLinkStream(port)
16
+ parser.on('data', packet => {
17
+ const clazz = REGISTRY[packet.header.msgid]
18
+ if (clazz) {
19
+ const data = packet.protocol.data(packet.payload, clazz)
20
+ console.log('Received packet:', data)
21
+ } else {
22
+ console.log(packet.debug())
23
+ }
24
+ })
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { join } from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
5
+ import { createReadStream } from 'node:fs'
6
+ import { createMavLinkStream } from './dist/index.js'
7
+ import { minimal, common, ardupilotmega } from './dist/index.js'
8
+
9
+ const REGISTRY = {
10
+ ...minimal.REGISTRY,
11
+ ...common.REGISTRY,
12
+ ...ardupilotmega.REGISTRY,
13
+ }
14
+
15
+ const __dirname = fileURLToPath(new URL('.', import.meta.url))
16
+ const port = createReadStream(join(__dirname, 'examples/GH-5.bin'))
17
+ const parser = createMavLinkStream(port)
18
+ parser.on('data', packet => {
19
+ const clazz = REGISTRY[packet.header.msgid]
20
+ if (clazz) {
21
+ const data = packet.protocol.data(packet.payload, clazz)
22
+ console.log('Received packet:', data)
23
+ }
24
+ })