From d7f4acb70253bd7d957d734a5d661c638d1ffad4 Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Thu, 21 Dec 2023 20:50:35 -0700 Subject: [PATCH] make more adjustments to try and get logging to work --- README.md | 4 +-- package.json | 2 +- src/App.vue | 4 +-- src/components/TopMessage.vue | 2 +- src/db/index.ts | 14 ++++++++-- src/db/tables/contacts.ts | 2 +- src/db/tables/logs.ts | 7 +++++ sw_scripts/additional-scripts.js | 45 ++++++++++++++++-------------- sw_scripts/safari-notifications.js | 13 +++++---- 9 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 src/db/tables/logs.ts diff --git a/README.md b/README.md index 861a86d..34a169a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ If you are deploying in a subdirectory, add it to `publicPath` in vue.config.js, * Tag wth the new version: `git tag 0.1.0`. -* If production, change src/constants/app.ts DEFAULT_*_SERVER to be PROD. +* If production, change src/constants/app.ts DEFAULT_*_SERVER to be PROD and package.json to not be _Test. * `npm run build` @@ -40,7 +40,7 @@ If you are deploying in a subdirectory, add it to `publicPath` in vue.config.js, * `rsync -azvu -e "ssh -i ~/.ssh/..." dist ubuntu@endorser.ch:time-safari` -* Revert src/constants/app.ts, increment version, add "-beta", `npm install`, and commit. +* Revert src/constants/app.ts and/or package.json, edit package.json to increment version & add "-beta", `npm install`, and commit. diff --git a/package.json b/package.json index a24cb46..1d35ad2 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "TimeSafari", + "name": "TimeSafari_Test", "version": "0.1.8-beta", "private": true, "scripts": { diff --git a/src/App.vue b/src/App.vue index 05bbe0a..b69bbd0 100644 --- a/src/App.vue +++ b/src/App.vue @@ -462,9 +462,7 @@ export default class App extends Vue { "An error occurred setting notification permissions:", error, ); - alert( - "Some error occurred setting notification permissions. See logs.", - ); + alert("Some error occurred setting notification permissions."); }); } diff --git a/src/components/TopMessage.vue b/src/components/TopMessage.vue index 8433741..bccc2fe 100644 --- a/src/components/TopMessage.vue +++ b/src/components/TopMessage.vue @@ -50,7 +50,7 @@ export default class TopMessage extends Vue { title: "Error Detecting Server", text: JSON.stringify(err), }, - 10000, + -1, ); } } diff --git a/src/db/index.ts b/src/db/index.ts index 37d6cf5..644f813 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,7 +1,8 @@ import BaseDexie, { Table } from "dexie"; import { encrypted, Encryption } from "@pvermeer/dexie-encrypted-addon"; import { Account, AccountsSchema } from "./tables/accounts"; -import { Contact, ContactsSchema } from "./tables/contacts"; +import { Contact, ContactSchema } from "./tables/contacts"; +import { Log, LogSchema } from "@/db/tables/logs"; import { MASTER_SETTINGS_KEY, Settings, @@ -13,6 +14,7 @@ import { AppString } from "@/constants/app"; type SensitiveTables = { accounts: Table }; type NonsensitiveTables = { contacts: Table; + logs: Table; settings: Table; }; @@ -26,7 +28,11 @@ export const accountsDB = new BaseDexie("TimeSafariAccounts") as SensitiveDexie; const SensitiveSchemas = { ...AccountsSchema }; export const db = new BaseDexie("TimeSafari") as NonsensitiveDexie; -const NonsensitiveSchemas = { ...ContactsSchema, ...SettingsSchema }; +const NonsensitiveSchemas = { + ...ContactSchema, + ...LogSchema, + ...SettingsSchema, +}; // Manage the encryption key. If not present in localStorage, create and store it. const secret = @@ -38,7 +44,9 @@ encrypted(accountsDB, { secretKey: secret }); // Define the schema for our databases accountsDB.version(1).stores(SensitiveSchemas); -db.version(1).stores(NonsensitiveSchemas); +// v1 was contacts & settings +// v2 added logs +db.version(2).stores(NonsensitiveSchemas); // Event handler to initialize the non-sensitive database with default settings db.on("populate", () => { diff --git a/src/db/tables/contacts.ts b/src/db/tables/contacts.ts index b684505..8ffd2fb 100644 --- a/src/db/tables/contacts.ts +++ b/src/db/tables/contacts.ts @@ -6,6 +6,6 @@ export interface Contact { registered?: boolean; } -export const ContactsSchema = { +export const ContactSchema = { contacts: "&did, name, publicKeyBase64, registered, seesMe", }; diff --git a/src/db/tables/logs.ts b/src/db/tables/logs.ts new file mode 100644 index 0000000..2dd99f7 --- /dev/null +++ b/src/db/tables/logs.ts @@ -0,0 +1,7 @@ +export interface Log { + message: string; +} + +export const LogSchema = { + logs: "message", +}; diff --git a/sw_scripts/additional-scripts.js b/sw_scripts/additional-scripts.js index 5ecb1ed..5defa97 100644 --- a/sw_scripts/additional-scripts.js +++ b/sw_scripts/additional-scripts.js @@ -4,7 +4,19 @@ importScripts( "https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js", ); +self.addEventListener("install", (event) => { + console.log("Service worker scripts importing...", event); + importScripts( + "safari-notifications.js", + "nacl.js", + "noble-curves.js", + "noble-hashes.js", + ); + console.log("Service worker scripts imported."); +}); + function logDbOrConsole(self, message, arg1, arg2) { + console.log(`${new Date().toISOString()} ${message}`, arg1, arg2); let fullMessage = `${new Date().toISOString()} ${message}`; if (arg1) { fullMessage += `: ${JSON.stringify(arg1)}`; @@ -12,24 +24,15 @@ function logDbOrConsole(self, message, arg1, arg2) { if (arg2) { fullMessage += ` -- ${JSON.stringify(arg2)}`; } - const logged = self.logMessage(fullMessage); - if (!logged) { - console.log(`$new Date().toISOString()} ${message}`, arg1, arg2); - } + // const logged = + self.logMessage(fullMessage); + // if (logged || !logged) { + // console.log(`${new Date().toISOString()} ${message}`, arg1, arg2); + // } } -self.addEventListener("install", (event) => { - logDbOrConsole(self, "Installing service worker:", event); - importScripts( - "safari-notifications.js", - "nacl.js", - "noble-curves.js", - "noble-hashes.js", - ); -}); - self.addEventListener("push", function (event) { - logDbOrConsole(self, "Received push event:", event); + logDbOrConsole(this, "Received push event:", event); event.waitUntil( (async () => { try { @@ -58,28 +61,28 @@ self.addEventListener("push", function (event) { }); self.addEventListener("message", (event) => { - logDbOrConsole(self, "Service worker message:", event); + logDbOrConsole(this, "Service worker message:", event); if (event.data && event.data.type === "SEND_LOCAL_DATA") { self.secret = event.data.data; event.ports[0].postMessage({ success: true }); } - logDbOrConsole(self, "Service worker posted message."); + logDbOrConsole(this, "Service worker posted message."); }); self.addEventListener("activate", (event) => { - logDbOrConsole(self, "Service worker activating...", event); + logDbOrConsole(this, "Service worker activating...", event); // see https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim // and https://web.dev/articles/service-worker-lifecycle#clientsclaim event.waitUntil(clients.claim()); - logDbOrConsole(self, "Service worker activated."); + logDbOrConsole(this, "Service worker activated."); }); self.addEventListener("fetch", (event) => { - logDbOrConsole(self, "Got fetch event:", event); + logDbOrConsole(this, "Got fetch event:", event); }); self.addEventListener("error", (event) => { - logDbOrConsole(self, "Error in Service Worker:", event); + logDbOrConsole(this, "Error in Service Worker:", event); console.error("Full Error:", event); console.error("Message:", event.message); console.error("File:", event.filename); diff --git a/sw_scripts/safari-notifications.js b/sw_scripts/safari-notifications.js index 16969ad..e2f6fe0 100644 --- a/sw_scripts/safari-notifications.js +++ b/sw_scripts/safari-notifications.js @@ -407,16 +407,17 @@ async function setMostRecentNotified(id) { async function logMessage(message) { try { const db = await openIndexedDB("TimeSafari"); - const transaction = db.transaction("worker_log", "readwrite"); - const store = transaction.objectStore("worker_log"); + const transaction = db.transaction("logs", "readwrite"); + const store = transaction.objectStore("logs"); // will only keep one day's worth of logs - let data = await getRecord(store, new Date().toDateString()); + const todayKey = new Date().toDateString(); + let data = await getRecord(store, todayKey); if (!data) { await store.clear(); // clear out anything older than today } data = data || ""; data += `\n${message}`; - await updateRecord(store, data); + await updateRecord(store, { message: data }, todayKey); transaction.oncomplete = () => db.close(); return true; } catch (error) { @@ -442,9 +443,9 @@ function getRecord(store, key) { } // Note that this assumes there is only one record in the store. -function updateRecord(store, data) { +function updateRecord(store, data, key) { return new Promise((resolve, reject) => { - const request = store.put(data); + const request = key ? store.put(data, key) : store.put(data); request.onsuccess = () => resolve(request.result); request.onerror = () => reject(request.error); });