From d63d42d623a36baa75b1647e29762f77271514d7 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Fri, 16 Jun 2023 20:38:39 +0200 Subject: [PATCH] Write store info as part of binary serialization. --- analysis.js | 7 +++++++ site/model/items.js | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/analysis.js b/analysis.js index 54a7b8e..0a28dde 100644 --- a/analysis.js +++ b/analysis.js @@ -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 diff --git a/site/model/items.js b/site/model/items.js index 06dc64c..232aca4 100644 --- a/site/model/items.js +++ b/site/model/items.js @@ -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);