From 2839e0cfcf3dd645173af7a0148e83f520c43532 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 31 May 2023 20:27:06 +0200 Subject: [PATCH] attempt to log image size in bytes --- api/index.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/api/index.js b/api/index.js index 1f10df4b..2a498690 100644 --- a/api/index.js +++ b/api/index.js @@ -50,6 +50,25 @@ function handle_sse_client(request, response, next) { }); } +const getSizeInBytes = obj => { + let str = null; + if (typeof obj === 'string') { + // If obj is a string, then use it + str = obj; + } else { + // Else, make obj into a string + str = JSON.stringify(obj); + } + // Get the length of the Uint8Array + const bytes = new TextEncoder().encode(str).length; + return bytes; +}; + +const logSizeInBytes = (description, obj) => { + const bytes = getSizeInBytes(obj); + console.log(`${description} is approximately ${bytes} B`); +}; + var connect_to_api = function () { console.log("Connecting to API"); ws = new WebSocket("ws://10.100.0.40:9001/"); @@ -69,12 +88,7 @@ var connect_to_api = function () { } } catch (error) { console.log("could not parse as json, must be bytes"); - let image = new Image(); - image.src = URL.createObjectURL(message.data); - image.addEventListener("load", function () { - console.log("image loaded with width: " + image.width + " and height: " + image.height); - - }); + logSizeInBytes("image", message.data); } });