refactor: Replace deprecated Buffer.slice with Buffer.subarray

This commit is contained in:
Sören Beye 2022-10-30 08:42:17 +01:00
parent 460b8e01d6
commit 88db3be62b
6 changed files with 22 additions and 22 deletions

View File

@ -48,16 +48,16 @@ class Codec {
const header = Buffer.alloc(2 + 2 + 4 + 4 + 4 + 16);
rawPacket.copy(header, 0,0,32);
const encryptedPayload = rawPacket.slice(32);
const encryptedPayload = rawPacket.subarray(32);
const stamp = header.readUInt32BE(12);
const calculatedChecksum = crypto.createHash("md5")
.update(header.slice(0, 16))
.update(header.subarray(0, 16))
.update(this.token)
.update(encryptedPayload)
.digest();
const checksumFromHeader = header.slice(16);
const checksumFromHeader = header.subarray(16);
let token = null;
let msg = null;
@ -71,7 +71,7 @@ class Codec {
// Apparently most if not all(?) miio messages are stringified json terminated with a \0
if (decryptedPayload[decryptedPayload.length -1] === 0) {
decryptedPayload = decryptedPayload.slice(0, decryptedPayload.length -1);
decryptedPayload = decryptedPayload.subarray(0, decryptedPayload.length -1);
}
msg = JSON.parse(decryptedPayload.toString());
@ -90,7 +90,7 @@ class Codec {
});
} else {
// If we receive an empty packet with a wrong checksum, assume that we're instead being provided a new token.
token = Buffer.from(header.slice(16));
token = Buffer.from(header.subarray(16));
if (
token.toString("hex") !== "ffffffffffffffffffffffffffffffff" &&
@ -144,7 +144,7 @@ class Codec {
const calculatedChecksum = crypto.createHash("md5")
.update(header.slice(0, 16))
.update(header.subarray(0, 16))
.update(this.token)
.update(encryptedPayload)
.digest();

View File

@ -231,8 +231,8 @@ const PNG_WRAPPER = {
TEXT_CHUNK_METADATA: Buffer.from("ValetudoMap\0\0"),
IMAGE: fs.readFileSync(path.join(__dirname, "../../res/valetudo_home_assistant_mqtt_wrapper.png"))
};
PNG_WRAPPER.IMAGE_WITHOUT_END_CHUNK = PNG_WRAPPER.IMAGE.slice(0, PNG_WRAPPER.IMAGE.length - 12);
PNG_WRAPPER.IMAGE_WITHOUT_END_CHUNK = PNG_WRAPPER.IMAGE.subarray(0, PNG_WRAPPER.IMAGE.length - 12);
//The PNG IEND chunk is always the last chunk and consists of a 4-byte length, the 4-byte chunk type, 0-byte chunk data and a 4-byte crc
PNG_WRAPPER.END_CHUNK = PNG_WRAPPER.IMAGE.slice(PNG_WRAPPER.IMAGE.length - 12);
PNG_WRAPPER.END_CHUNK = PNG_WRAPPER.IMAGE.subarray(PNG_WRAPPER.IMAGE.length - 12);
module.exports = MapNodeMqttHandle;

View File

@ -29,7 +29,7 @@ class DreameMapParser {
type = MAP_DATA_TYPES.REGULAR;
}
const parsedHeader = DreameMapParser.PARSE_HEADER(buf.slice(0, HEADER_SIZE));
const parsedHeader = DreameMapParser.PARSE_HEADER(buf.subarray(0, HEADER_SIZE));
/**
@ -77,13 +77,13 @@ class DreameMapParser {
if (buf.length >= HEADER_SIZE + parsedHeader.width * parsedHeader.height) {
const imageData = buf.slice(HEADER_SIZE, HEADER_SIZE + parsedHeader.width * parsedHeader.height);
const imageData = buf.subarray(HEADER_SIZE, HEADER_SIZE + parsedHeader.width * parsedHeader.height);
const activeSegmentIds = [];
const segmentNames = {};
let additionalData = {};
try {
additionalData = JSON.parse(buf.slice(parsedHeader.width * parsedHeader.height + HEADER_SIZE).toString());
additionalData = JSON.parse(buf.subarray(parsedHeader.width * parsedHeader.height + HEADER_SIZE).toString());
} catch (e) {
Logger.warn("Error while parsing additional map data", e);
}
@ -230,7 +230,7 @@ class DreameMapParser {
/*
TODO RESEARCH
There can be an spoint object. No idea what that does
There can also be multiple tpoint points. No idea when or why that happens or what it does either
*/
@ -552,7 +552,7 @@ class DreameMapParser {
*
* https://tools.ietf.org/html/rfc4648#section-5
*
*
*
*
* @param {Buffer|string} data
* @returns {Promise<Buffer|null>}

View File

@ -32,7 +32,7 @@ class RoborockMapParser {
static PARSE(mapBuf){
if (mapBuf[0x00] === 0x72 && mapBuf[0x01] === 0x72) {// rr
const metaData = RoborockMapParser.PARSE_METADATA(mapBuf);
const blocks = RoborockMapParser.BUILD_BLOCK_INDEX(mapBuf.slice(0x14));
const blocks = RoborockMapParser.BUILD_BLOCK_INDEX(mapBuf.subarray(0x14));
const processedBlocks = RoborockMapParser.PROCESS_BLOCKS(blocks);
return RoborockMapParser.POST_PROCESS_BLOCKS(metaData, processedBlocks);
@ -67,7 +67,7 @@ class RoborockMapParser {
const blockMetadata = RoborockMapParser.PARSE_BLOCK_METADATA(buf);
block_index.push(blockMetadata);
buf = buf.slice(blockMetadata.header_length + blockMetadata.data_length);
buf = buf.subarray(blockMetadata.header_length + blockMetadata.data_length);
}
return block_index;
@ -83,7 +83,7 @@ class RoborockMapParser {
data_length: buf.readUInt32LE(0x04)
};
block_metadata.view = buf.slice(0, block_metadata.header_length + block_metadata.data_length);
block_metadata.view = buf.subarray(0, block_metadata.header_length + block_metadata.data_length);
return block_metadata;
}
@ -181,7 +181,7 @@ class RoborockMapParser {
case 28:
//Gen3 headers have additional segments header data, which increases its length by 4 bytes
//Everything else stays at the same relative offsets so we can just throw those additional bytes away
view = block.view.slice(4);
view = block.view.subarray(4);
mayContainSegments = true;
//Initializing all possible 31 segments here and throwing away the empty ones later improves performance

View File

@ -25,7 +25,7 @@ class ViomiMapParser {
* @returns {Buffer}
*/
take(n, label) {
const data = this.buf.slice(this.offset, this.offset + n);
const data = this.buf.subarray(this.offset, this.offset + n);
const different_length = n !== data.length ? " actual length: " + data.length : "";
this.log("take " + n + "@" + label + different_length, data);
this.offset += n;
@ -39,7 +39,7 @@ class ViomiMapParser {
* @returns {Buffer}
*/
peek(n) {
return this.buf.slice(this.offset, this.offset + n);
return this.buf.subarray(this.offset, this.offset + n);
}
/**
@ -512,7 +512,7 @@ class ViomiMapParser {
if (count >= 300) {
throw new Error("Unable to seek to end of room data");
}
} while (Buffer.compare(takenBytes, rooms2header.slice(0, 4)) && this.offset < this.buf.length);
} while (Buffer.compare(takenBytes, rooms2header.subarray(0, 4)) && this.offset < this.buf.length);
this.take(3, "rest of tag");
}

View File

@ -57,8 +57,8 @@ Object.values(binaries).forEach(async (b,i) => {
const baseSize = fs.readFileSync(b.base).length;
const built = fs.readFileSync(b.built);
const runtime = built.slice(0, baseSize);
const payload = built.slice(baseSize);
const runtime = built.subarray(0, baseSize);
const payload = built.subarray(baseSize);
// UPX will reject files without the executable bit on linux. Also, default mode is 666
fs.writeFileSync(b.out + "_runtime", runtime, {mode: 0o777});