mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add helper class to parse meshcore path info
This commit is contained in:
parent
f59b0f7ce4
commit
5fa39ceb37
3 changed files with 60 additions and 0 deletions
11
src/hex_util.js
Normal file
11
src/hex_util.js
Normal 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;
|
||||||
|
|
@ -9,6 +9,7 @@ import Advert from "./advert.js";
|
||||||
import Packet from "./packet.js";
|
import Packet from "./packet.js";
|
||||||
import BufferUtils from "./buffer_utils.js";
|
import BufferUtils from "./buffer_utils.js";
|
||||||
import CayenneLpp from "./cayenne_lpp.js";
|
import CayenneLpp from "./cayenne_lpp.js";
|
||||||
|
import MeshCorePath from "./meshore_path.js";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
Connection,
|
Connection,
|
||||||
|
|
@ -22,4 +23,5 @@ export {
|
||||||
Packet,
|
Packet,
|
||||||
BufferUtils,
|
BufferUtils,
|
||||||
CayenneLpp,
|
CayenneLpp,
|
||||||
|
MeshCorePath,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
47
src/meshore_path.js
Normal file
47
src/meshore_path.js
Normal 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;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue