forked from jsnbuchanan/crowd-funder-for-time-pwa
make more adjustments to try and get logging to work
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "TimeSafari",
|
||||
"name": "TimeSafari_Test",
|
||||
"version": "0.1.8-beta",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
|
||||
@@ -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.");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ export default class TopMessage extends Vue {
|
||||
title: "Error Detecting Server",
|
||||
text: JSON.stringify(err),
|
||||
},
|
||||
10000,
|
||||
-1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Account> };
|
||||
type NonsensitiveTables = {
|
||||
contacts: Table<Contact>;
|
||||
logs: Table<Log>;
|
||||
settings: Table<Settings>;
|
||||
};
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -6,6 +6,6 @@ export interface Contact {
|
||||
registered?: boolean;
|
||||
}
|
||||
|
||||
export const ContactsSchema = {
|
||||
export const ContactSchema = {
|
||||
contacts: "&did, name, publicKeyBase64, registered, seesMe",
|
||||
};
|
||||
|
||||
7
src/db/tables/logs.ts
Normal file
7
src/db/tables/logs.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export interface Log {
|
||||
message: string;
|
||||
}
|
||||
|
||||
export const LogSchema = {
|
||||
logs: "message",
|
||||
};
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user