add helper class to parse meshcore path info

This commit is contained in:
liamcottle 2026-03-07 11:46:42 +13:00
parent f59b0f7ce4
commit 5fa39ceb37
3 changed files with 60 additions and 0 deletions

11
src/hex_util.js Normal file
View file

@ -0,0 +1,11 @@
class HexUtil {
static bytesToHex(bytes) {
return Array.from(bytes).map((byte) => {
return byte.toString(16).padStart(2, "0");
}).join("");
}
}
export default HexUtil;

View file

@ -9,6 +9,7 @@ import Advert from "./advert.js";
import Packet from "./packet.js";
import BufferUtils from "./buffer_utils.js";
import CayenneLpp from "./cayenne_lpp.js";
import MeshCorePath from "./meshore_path.js";
export {
Connection,
@ -22,4 +23,5 @@ export {
Packet,
BufferUtils,
CayenneLpp,
MeshCorePath,
};

47
src/meshore_path.js Normal file
View file

@ -0,0 +1,47 @@
import BufferReader from "./buffer_reader.js";
import HexUtil from "./hex_util.js";
import Packet from "./packet.js";
class MeshCorePath {
constructor(pathHashSize, pathHashCount, pathItems) {
this.pathHashSize = pathHashSize;
this.pathHashCount = pathHashCount;
this.pathItems = pathItems;
}
static fromPathAndLength(path, pathLen) {
// make sure path is valid
if(pathLen === 0xFF){
return null;
}
const pathHashSize = Packet.extractPathHashSize(pathLen);
const pathHashCount = Packet.extractPathHashCount(pathLen);
const pathByteLength = pathHashCount * pathHashSize;
const pathBytes = path.subarray(0, pathByteLength);
if(pathBytes.length < pathByteLength){
return null;
}
// convert path to comma delimited hex string
const pathItems = [];
const pathBuffer = new BufferReader(pathBytes);
for(var i = 0; i < pathHashCount; i++){
pathItems.push(pathBuffer.readBytes(pathHashSize));
}
return new MeshCorePath(pathHashSize, pathHashCount, pathItems);
}
toHexPathString() {
return this.pathItems.map((pathItem) => {
return HexUtil.bytesToHex(pathItem);
}).join(",");
}
}
export default MeshCorePath;