From a180ec159e02f41175f6ef015eef175dab3ed368 Mon Sep 17 00:00:00 2001 From: Claas Date: Sun, 7 Dec 2025 00:38:32 +0100 Subject: [PATCH] Format file size for readability --- app/src/service-worker/serviceWorker.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/src/service-worker/serviceWorker.ts b/app/src/service-worker/serviceWorker.ts index 2dff2cd..302debd 100644 --- a/app/src/service-worker/serviceWorker.ts +++ b/app/src/service-worker/serviceWorker.ts @@ -44,7 +44,9 @@ async function getConfiguration(): Promise { const configuration = /** @type {Schema["configuration"]} */ cursor?.value; return configuration; } - +/** + * Convenience function to make get a file from a directory more ergonomic without failing if the file does not exist. + */ async function getFile( directory: FileSystemDirectoryHandle, fileName: string @@ -52,13 +54,16 @@ async function getFile( try { return await directory.getFileHandle(fileName); } catch (error) { - // Finding out if the file does not exist is a bit unergonomic if (!(error instanceof DOMException) || error.name !== "NotFoundError") throw error; } } const FILE_NAME = "client.meal"; +const fileSizeFormatter = new Intl.NumberFormat(undefined, { + unit: "megabyte", +}); + async function persistClient(client: Uint8Array) { // Persist client state const directory = await navigator.storage.getDirectory(); @@ -77,7 +82,11 @@ async function persistClient(client: Uint8Array) { writeStream.close(); } - console.debug("[Service worker]: Stored client with length", client.length); + console.debug( + `[Service worker]: Stored client with length ${fileSizeFormatter.format( + client.length + )}` + ); return client; } -- 2.51.2