Browse Source

make more adjustments to try and get logging to work

starred-projects
Trent Larson 11 months ago
parent
commit
d7f4acb702
  1. 4
      README.md
  2. 2
      package.json
  3. 4
      src/App.vue
  4. 2
      src/components/TopMessage.vue
  5. 14
      src/db/index.ts
  6. 2
      src/db/tables/contacts.ts
  7. 7
      src/db/tables/logs.ts
  8. 45
      sw_scripts/additional-scripts.js
  9. 13
      sw_scripts/safari-notifications.js

4
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`. * 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` * `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` * `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.

2
package.json

@ -1,5 +1,5 @@
{ {
"name": "TimeSafari", "name": "TimeSafari_Test",
"version": "0.1.8-beta", "version": "0.1.8-beta",
"private": true, "private": true,
"scripts": { "scripts": {

4
src/App.vue

@ -462,9 +462,7 @@ export default class App extends Vue {
"An error occurred setting notification permissions:", "An error occurred setting notification permissions:",
error, error,
); );
alert( alert("Some error occurred setting notification permissions.");
"Some error occurred setting notification permissions. See logs.",
);
}); });
} }

2
src/components/TopMessage.vue

@ -50,7 +50,7 @@ export default class TopMessage extends Vue {
title: "Error Detecting Server", title: "Error Detecting Server",
text: JSON.stringify(err), text: JSON.stringify(err),
}, },
10000, -1,
); );
} }
} }

14
src/db/index.ts

@ -1,7 +1,8 @@
import BaseDexie, { Table } from "dexie"; import BaseDexie, { Table } from "dexie";
import { encrypted, Encryption } from "@pvermeer/dexie-encrypted-addon"; import { encrypted, Encryption } from "@pvermeer/dexie-encrypted-addon";
import { Account, AccountsSchema } from "./tables/accounts"; 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 { import {
MASTER_SETTINGS_KEY, MASTER_SETTINGS_KEY,
Settings, Settings,
@ -13,6 +14,7 @@ import { AppString } from "@/constants/app";
type SensitiveTables = { accounts: Table<Account> }; type SensitiveTables = { accounts: Table<Account> };
type NonsensitiveTables = { type NonsensitiveTables = {
contacts: Table<Contact>; contacts: Table<Contact>;
logs: Table<Log>;
settings: Table<Settings>; settings: Table<Settings>;
}; };
@ -26,7 +28,11 @@ export const accountsDB = new BaseDexie("TimeSafariAccounts") as SensitiveDexie;
const SensitiveSchemas = { ...AccountsSchema }; const SensitiveSchemas = { ...AccountsSchema };
export const db = new BaseDexie("TimeSafari") as NonsensitiveDexie; 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. // Manage the encryption key. If not present in localStorage, create and store it.
const secret = const secret =
@ -38,7 +44,9 @@ encrypted(accountsDB, { secretKey: secret });
// Define the schema for our databases // Define the schema for our databases
accountsDB.version(1).stores(SensitiveSchemas); 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 // Event handler to initialize the non-sensitive database with default settings
db.on("populate", () => { db.on("populate", () => {

2
src/db/tables/contacts.ts

@ -6,6 +6,6 @@ export interface Contact {
registered?: boolean; registered?: boolean;
} }
export const ContactsSchema = { export const ContactSchema = {
contacts: "&did, name, publicKeyBase64, registered, seesMe", contacts: "&did, name, publicKeyBase64, registered, seesMe",
}; };

7
src/db/tables/logs.ts

@ -0,0 +1,7 @@
export interface Log {
message: string;
}
export const LogSchema = {
logs: "message",
};

45
sw_scripts/additional-scripts.js

@ -4,7 +4,19 @@ importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js", "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) { function logDbOrConsole(self, message, arg1, arg2) {
console.log(`${new Date().toISOString()} ${message}`, arg1, arg2);
let fullMessage = `${new Date().toISOString()} ${message}`; let fullMessage = `${new Date().toISOString()} ${message}`;
if (arg1) { if (arg1) {
fullMessage += `: ${JSON.stringify(arg1)}`; fullMessage += `: ${JSON.stringify(arg1)}`;
@ -12,24 +24,15 @@ function logDbOrConsole(self, message, arg1, arg2) {
if (arg2) { if (arg2) {
fullMessage += ` -- ${JSON.stringify(arg2)}`; fullMessage += ` -- ${JSON.stringify(arg2)}`;
} }
const logged = self.logMessage(fullMessage); // const logged =
if (!logged) { self.logMessage(fullMessage);
console.log(`$new Date().toISOString()} ${message}`, arg1, arg2); // 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) { self.addEventListener("push", function (event) {
logDbOrConsole(self, "Received push event:", event); logDbOrConsole(this, "Received push event:", event);
event.waitUntil( event.waitUntil(
(async () => { (async () => {
try { try {
@ -58,28 +61,28 @@ self.addEventListener("push", function (event) {
}); });
self.addEventListener("message", (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") { if (event.data && event.data.type === "SEND_LOCAL_DATA") {
self.secret = event.data.data; self.secret = event.data.data;
event.ports[0].postMessage({ success: true }); event.ports[0].postMessage({ success: true });
} }
logDbOrConsole(self, "Service worker posted message."); logDbOrConsole(this, "Service worker posted message.");
}); });
self.addEventListener("activate", (event) => { 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 // see https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim
// and https://web.dev/articles/service-worker-lifecycle#clientsclaim // and https://web.dev/articles/service-worker-lifecycle#clientsclaim
event.waitUntil(clients.claim()); event.waitUntil(clients.claim());
logDbOrConsole(self, "Service worker activated."); logDbOrConsole(this, "Service worker activated.");
}); });
self.addEventListener("fetch", (event) => { self.addEventListener("fetch", (event) => {
logDbOrConsole(self, "Got fetch event:", event); logDbOrConsole(this, "Got fetch event:", event);
}); });
self.addEventListener("error", (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("Full Error:", event);
console.error("Message:", event.message); console.error("Message:", event.message);
console.error("File:", event.filename); console.error("File:", event.filename);

13
sw_scripts/safari-notifications.js

@ -407,16 +407,17 @@ async function setMostRecentNotified(id) {
async function logMessage(message) { async function logMessage(message) {
try { try {
const db = await openIndexedDB("TimeSafari"); const db = await openIndexedDB("TimeSafari");
const transaction = db.transaction("worker_log", "readwrite"); const transaction = db.transaction("logs", "readwrite");
const store = transaction.objectStore("worker_log"); const store = transaction.objectStore("logs");
// will only keep one day's worth of 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) { if (!data) {
await store.clear(); // clear out anything older than today await store.clear(); // clear out anything older than today
} }
data = data || ""; data = data || "";
data += `\n${message}`; data += `\n${message}`;
await updateRecord(store, data); await updateRecord(store, { message: data }, todayKey);
transaction.oncomplete = () => db.close(); transaction.oncomplete = () => db.close();
return true; return true;
} catch (error) { } catch (error) {
@ -442,9 +443,9 @@ function getRecord(store, key) {
} }
// Note that this assumes there is only one record in the store. // 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) => { 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.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error); request.onerror = () => reject(request.error);
}); });

Loading…
Cancel
Save