monoidentity 0.12.4 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/+client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "./+common.js";
2
- export { getLoginRecognized, getVerification, getStorage, getScopedFS } from "./storage.js";
2
+ export { getLoginRecognized, getVerification, getStorage, getScopedFS, completeSync, } from "./storage.js";
3
3
  export { retrieveVerification } from "./verification-client.js";
4
4
  export { default as rawAttest } from "./verification/attest-remote.js";
5
5
  export { trackReady } from "./trackready.js";
package/dist/+client.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "./+common.js";
2
- export { getLoginRecognized, getVerification, getStorage, getScopedFS } from "./storage.js";
2
+ export { getLoginRecognized, getVerification, getStorage, getScopedFS, completeSync, } from "./storage.js";
3
3
  export { retrieveVerification } from "./verification-client.js";
4
4
  export { default as rawAttest } from "./verification/attest-remote.js";
5
5
  export { trackReady } from "./trackready.js";
@@ -1,8 +1,10 @@
1
1
  import { AwsClient } from "aws4fetch";
2
2
  import { storageClient, STORAGE_EVENT } from "./storageclient.svelte.js";
3
+ import { addToSync } from "../../src/lib/storage.js";
3
4
  const CLOUD_CACHE_KEY = "monoidentity-x/cloud-cache";
4
5
  const isJunk = (key) => key.includes(".obsidian/") || key.includes(".cache/");
5
6
  const keepAnyway = (key) => key.includes(".cache/");
7
+ let unmount;
6
8
  const loadFromCloud = async (base, client) => {
7
9
  const listResp = await client.fetch(base);
8
10
  if (!listResp.ok)
@@ -59,14 +61,44 @@ const syncFromCloud = async (bucket, client) => {
59
61
  }
60
62
  };
61
63
  export const backupCloud = async (bucket) => {
64
+ unmount?.();
62
65
  const client = new AwsClient({
63
66
  accessKeyId: bucket.accessKeyId,
64
67
  secretAccessKey: bucket.secretAccessKey,
65
68
  });
66
69
  await syncFromCloud(bucket, client);
67
- setInterval(() => syncFromCloud(bucket, client), 15 * 60 * 1000);
70
+ const syncIntervalId = setInterval(() => syncFromCloud(bucket, client), 15 * 60 * 1000);
68
71
  // Continuous sync: mirror local changes to cloud
69
- addEventListener(STORAGE_EVENT, async (event) => {
72
+ const write = async (key, value) => {
73
+ const url = `${bucket.base}/${key}`;
74
+ if (value != undefined) {
75
+ // PUT content (unconditional to start; you can add If-Match/If-None-Match for safety)
76
+ const r = await client.fetch(url, {
77
+ method: "PUT",
78
+ headers: { "content-type": "application/octet-stream" },
79
+ body: value,
80
+ });
81
+ if (!r.ok)
82
+ throw new Error(`PUT ${key} failed: ${r.status}`);
83
+ // Update cache
84
+ const etag = r.headers.get("etag")?.replaceAll('"', "");
85
+ if (etag) {
86
+ const cache = JSON.parse(localStorage[CLOUD_CACHE_KEY] || "{}");
87
+ cache[key] = { etag, content: value };
88
+ localStorage[CLOUD_CACHE_KEY] = JSON.stringify(cache);
89
+ }
90
+ }
91
+ else {
92
+ // DELETE key
93
+ const r = await client.fetch(url, { method: "DELETE" });
94
+ if (!r.ok && r.status != 404)
95
+ throw new Error(`DELETE ${key} failed: ${r.status}`);
96
+ const cache = JSON.parse(localStorage[CLOUD_CACHE_KEY] || "{}");
97
+ delete cache[key];
98
+ localStorage[CLOUD_CACHE_KEY] = JSON.stringify(cache);
99
+ }
100
+ };
101
+ const listener = (event) => {
70
102
  let key = event.detail.key;
71
103
  if (!key.startsWith("monoidentity/"))
72
104
  return;
@@ -74,37 +106,19 @@ export const backupCloud = async (bucket) => {
74
106
  if (isJunk(key))
75
107
  return;
76
108
  console.debug("[monoidentity cloud] saving", key);
77
- const url = `${bucket.base}/${key}`;
78
- try {
79
- if (event.detail.value != undefined) {
80
- // PUT content (unconditional to start; you can add If-Match/If-None-Match for safety)
81
- const r = await client.fetch(url, {
82
- method: "PUT",
83
- headers: { "content-type": "application/octet-stream" },
84
- body: event.detail.value,
85
- });
86
- if (!r.ok)
87
- throw new Error(`PUT ${key} failed: ${r.status}`);
88
- // Update cache
89
- const etag = r.headers.get("etag")?.replaceAll('"', "");
90
- if (etag) {
91
- const cache = JSON.parse(localStorage[CLOUD_CACHE_KEY] || "{}");
92
- cache[key] = { etag, content: event.detail.value };
93
- localStorage[CLOUD_CACHE_KEY] = JSON.stringify(cache);
94
- }
95
- }
96
- else {
97
- // DELETE key
98
- const r = await client.fetch(url, { method: "DELETE" });
99
- if (!r.ok && r.status != 404)
100
- throw new Error(`DELETE ${key} failed: ${r.status}`);
101
- const cache = JSON.parse(localStorage[CLOUD_CACHE_KEY] || "{}");
102
- delete cache[key];
103
- localStorage[CLOUD_CACHE_KEY] = JSON.stringify(cache);
104
- }
105
- }
106
- catch (err) {
107
- console.warn("[monoidentity cloud] sync failed", key, err);
108
- }
109
- });
109
+ const promise = write(key, event.detail.value).catch((err) => {
110
+ console.warn("[monoidentity cloud] save failed", key, err);
111
+ });
112
+ addToSync(promise);
113
+ };
114
+ addEventListener(STORAGE_EVENT, listener);
115
+ unmount = () => {
116
+ clearInterval(syncIntervalId);
117
+ removeEventListener(STORAGE_EVENT, listener);
118
+ };
110
119
  };
120
+ if (import.meta.hot) {
121
+ import.meta.hot.dispose(() => {
122
+ unmount?.();
123
+ });
124
+ }
@@ -1,6 +1,7 @@
1
1
  import { createStore, get, set } from "idb-keyval";
2
2
  import { STORAGE_EVENT, storageClient } from "./storageclient.svelte.js";
3
3
  import { canBackup } from "../utils-transport.js";
4
+ let unmount;
4
5
  const saveToDir = (dir) => {
5
6
  let dirCache = {};
6
7
  const getDirCached = async (route) => {
@@ -16,7 +17,7 @@ const saveToDir = (dir) => {
16
17
  }
17
18
  return parent;
18
19
  };
19
- addEventListener(STORAGE_EVENT, async (event) => {
20
+ const listener = async (event) => {
20
21
  let key = event.detail.key;
21
22
  if (!key.startsWith("monoidentity/"))
22
23
  return;
@@ -35,19 +36,24 @@ const saveToDir = (dir) => {
35
36
  else {
36
37
  await parent.removeEntry(name);
37
38
  }
38
- });
39
+ };
40
+ addEventListener(STORAGE_EVENT, listener);
41
+ return () => {
42
+ removeEventListener(STORAGE_EVENT, listener);
43
+ };
39
44
  };
40
45
  export const backupLocally = async (requestBackup) => {
41
46
  if (!canBackup)
42
47
  return;
43
48
  if (localStorage["monoidentity-x/backup"] == "off")
44
49
  return;
50
+ unmount?.();
45
51
  const handles = createStore("monoidentity-x", "handles");
46
52
  if (localStorage["monoidentity-x/backup"] == "on") {
47
53
  const dir = await get("backup", handles);
48
54
  if (!dir)
49
55
  throw new Error("No backup handle found");
50
- saveToDir(dir);
56
+ unmount = saveToDir(dir);
51
57
  }
52
58
  else {
53
59
  localStorage["monoidentity-x/backup"] = "off";
@@ -79,7 +85,12 @@ export const backupLocally = async (requestBackup) => {
79
85
  location.reload();
80
86
  return;
81
87
  }
82
- saveToDir(dir);
88
+ unmount = saveToDir(dir);
83
89
  });
84
90
  }
85
91
  };
92
+ if (import.meta.hot) {
93
+ import.meta.hot.dispose(() => {
94
+ unmount?.();
95
+ });
96
+ }
package/dist/storage.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export declare const conf: (a: string) => void;
2
+ export declare const addToSync: (p: Promise<void>) => void;
3
+ export declare const completeSync: () => Promise<void>;
2
4
  export declare const getLoginRecognized: () => {
3
5
  email: string;
4
6
  password: string;
package/dist/storage.js CHANGED
@@ -6,9 +6,14 @@ import { verify } from "@tsndr/cloudflare-worker-jwt";
6
6
  import publicKey from "./verification/public-key.js";
7
7
  import { storageClient } from "./storage/storageclient.svelte.js";
8
8
  let app = "unknown";
9
+ let syncPromise = Promise.resolve();
9
10
  export const conf = (a) => {
10
11
  app = a;
11
12
  };
13
+ export const addToSync = (p) => {
14
+ syncPromise = syncPromise.then(() => p);
15
+ };
16
+ export const completeSync = () => syncPromise;
12
17
  const LOGIN_RECOGNIZED_PATH = ".core/login.encjson";
13
18
  export const getLoginRecognized = () => {
14
19
  const client = storageClient();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monoidentity",
3
- "version": "0.12.4",
3
+ "version": "0.13.0",
4
4
  "repository": "KTibow/monoidentity",
5
5
  "author": {
6
6
  "name": "KTibow"