Write store info as part of binary serialization.

This commit is contained in:
Mario Zechner 2023-06-16 20:38:39 +02:00
parent 222720365c
commit d63d42d623
2 changed files with 18 additions and 1 deletions

View File

@ -150,6 +150,13 @@ function sortItems(items) {
function compressBinary(items) {
const buffer = [];
buffer.push(STORE_KEYS.length);
for (const key of STORE_KEYS) {
const nameBuffer = Buffer.from(key, "utf8");
const nameLengthBuffer = Buffer.allocUnsafe(2);
nameLengthBuffer.writeUInt16LE(nameBuffer.length, 0);
buffer.push(...nameLengthBuffer, ...nameBuffer);
}
for (const item of items) {
// Serialize 'bio', 'isWeighted', and 'unit' into a single byte

View File

@ -10,6 +10,16 @@ function decompressBinary(buffer) {
const baseDate = new Date("2000-01-01");
const textDecoder = new TextDecoder("utf-8");
const numStores = view.getUint8(offset++);
const stores = [];
for (let i = 0; i < numStores; i++) {
const nameLength = view.getUint16(offset, true);
offset += 2;
const nameBuffer = new Uint8Array(buffer, offset, nameLength);
stores.push(textDecoder.decode(nameBuffer));
offset += nameLength;
}
while (offset < buffer.byteLength) {
const obj = {};
@ -28,7 +38,7 @@ function decompressBinary(buffer) {
offset += 4;
// Deserialize 'store' as a byte
obj.store = STORE_KEYS[view.getUint8(offset++)];
obj.store = stores[view.getUint8(offset++)];
// Deserialize 'name' as UTF-8 with 2 bytes encoding the string length
const nameLength = view.getUint16(offset, true);