monoidentity 0.13.1 → 0.14.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.
@@ -3,12 +3,23 @@
3
3
  import { trackReady } from "./trackready.js";
4
4
  import type { Intent } from "./utils-transport.js";
5
5
 
6
- let { app, intents, children }: { app: string; intents?: Intent[]; children: Snippet } = $props();
6
+ let {
7
+ app,
8
+ intents,
9
+ shouldBackup,
10
+ children,
11
+ }: {
12
+ app: string;
13
+ intents?: Intent[];
14
+ shouldBackup: (path: string) => boolean;
15
+ children: Snippet;
16
+ } = $props();
7
17
 
8
18
  let backup: (() => void) | undefined = $state();
9
19
  const ready = trackReady(
10
20
  app,
11
21
  intents || [],
22
+ shouldBackup,
12
23
  (startBackup) =>
13
24
  (backup = () => {
14
25
  startBackup();
@@ -3,6 +3,7 @@ import type { Intent } from "./utils-transport.js";
3
3
  type $$ComponentProps = {
4
4
  app: string;
5
5
  intents?: Intent[];
6
+ shouldBackup: (path: string) => boolean;
6
7
  children: Snippet;
7
8
  };
8
9
  declare const Monoidentity: import("svelte").Component<$$ComponentProps, {}, "">;
@@ -0,0 +1 @@
1
+ export declare const shouldPersist: (key: string) => boolean;
@@ -0,0 +1 @@
1
+ export const shouldPersist = (key) => key.includes(".cache/");
@@ -1,2 +1,2 @@
1
1
  import type { Bucket } from "../utils-bucket.js";
2
- export declare const backupCloud: (bucket: Bucket) => Promise<void>;
2
+ export declare const backupCloud: (shouldBackup: (path: string) => boolean, bucket: Bucket) => Promise<void>;
@@ -1,11 +1,10 @@
1
1
  import { AwsClient } from "aws4fetch";
2
2
  import { storageClient, STORAGE_EVENT } from "./storageclient.svelte.js";
3
3
  import { addToSync } from "../storage.js";
4
+ import { shouldPersist } from "./_should.js";
4
5
  const CLOUD_CACHE_KEY = "monoidentity-x/cloud-cache";
5
- const isJunk = (key) => key.includes(".obsidian/") || key.includes(".cache/");
6
- const keepAnyway = (key) => key.includes(".cache/");
7
6
  let unmount;
8
- const loadFromCloud = async (base, client) => {
7
+ const loadFromCloud = async (shouldBackup, base, client) => {
9
8
  const listResp = await client.fetch(base);
10
9
  if (!listResp.ok)
11
10
  throw new Error(`List bucket failed: ${listResp.status}`);
@@ -13,7 +12,7 @@ const loadFromCloud = async (base, client) => {
13
12
  const objects = [...listXml.matchAll(/<Key>(.*?)<\/Key>.*?<ETag>(.*?)<\/ETag>/gs)]
14
13
  .map((m) => m.slice(1).map((s) => s.replaceAll("&quot;", `"`).replaceAll("&apos;", `'`)))
15
14
  .map(([key, etag]) => ({ key, etag: etag.replaceAll(`"`, "") }))
16
- .filter(({ key }) => !isJunk(key));
15
+ .filter(({ key }) => shouldBackup(key));
17
16
  const prevCache = JSON.parse(localStorage[CLOUD_CACHE_KEY] || "{}");
18
17
  const nextCache = {};
19
18
  const model = {};
@@ -46,13 +45,13 @@ const loadFromCloud = async (base, client) => {
46
45
  localStorage[CLOUD_CACHE_KEY] = JSON.stringify(nextCache);
47
46
  return model;
48
47
  };
49
- const syncFromCloud = async (bucket, client) => {
50
- const remote = await loadFromCloud(bucket.base, client);
48
+ const syncFromCloud = async (shouldBackup, bucket, client) => {
49
+ const remote = await loadFromCloud(shouldBackup, bucket.base, client);
51
50
  const local = storageClient();
52
51
  for (const key of Object.keys(local)) {
53
52
  if (key in remote)
54
53
  continue;
55
- if (keepAnyway(key))
54
+ if (shouldPersist(key))
56
55
  continue;
57
56
  delete local[key];
58
57
  }
@@ -60,16 +59,22 @@ const syncFromCloud = async (bucket, client) => {
60
59
  local[key] = value;
61
60
  }
62
61
  };
63
- export const backupCloud = async (bucket) => {
62
+ export const backupCloud = async (shouldBackup, bucket) => {
64
63
  unmount?.();
65
64
  const client = new AwsClient({
66
65
  accessKeyId: bucket.accessKeyId,
67
66
  secretAccessKey: bucket.secretAccessKey,
68
67
  });
69
- await syncFromCloud(bucket, client);
70
- const syncIntervalId = setInterval(() => syncFromCloud(bucket, client), 15 * 60 * 1000);
68
+ await syncFromCloud(shouldBackup, bucket, client);
69
+ const syncIntervalId = setInterval(() => syncFromCloud(shouldBackup, bucket, client), 15 * 60 * 1000);
71
70
  // Continuous sync: mirror local changes to cloud
72
71
  const write = async (key, value) => {
72
+ if (!shouldBackup(key)) {
73
+ if (!shouldPersist(key))
74
+ console.warn("[monoidentity cloud]", key, "isn't marked to be backed up or saved");
75
+ return;
76
+ }
77
+ console.debug("[monoidentity cloud] saving", key);
73
78
  const url = `${bucket.base}/${key}`;
74
79
  if (value != undefined) {
75
80
  // PUT content (unconditional to start; you can add If-Match/If-None-Match for safety)
@@ -103,9 +108,6 @@ export const backupCloud = async (bucket) => {
103
108
  if (!key.startsWith("monoidentity/"))
104
109
  return;
105
110
  key = key.slice("monoidentity/".length);
106
- if (isJunk(key))
107
- return;
108
- console.debug("[monoidentity cloud] saving", key);
109
111
  const promise = write(key, event.detail.value).catch((err) => {
110
112
  console.warn("[monoidentity cloud] save failed", key, err);
111
113
  });
@@ -1 +1 @@
1
- export declare const backupLocally: (requestBackup: (startBackup: () => void) => void) => Promise<void>;
1
+ export declare const backupLocally: (shouldBackup: (path: string) => boolean, requestBackup: (startBackup: () => void) => void) => Promise<void>;
@@ -1,8 +1,9 @@
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
+ import { shouldPersist } from "./_should.js";
4
5
  let unmount;
5
- const saveToDir = (dir) => {
6
+ const saveToDir = (shouldBackup, dir) => {
6
7
  let dirCache = {};
7
8
  const getDirCached = async (route) => {
8
9
  let key = "";
@@ -25,6 +26,11 @@ const saveToDir = (dir) => {
25
26
  const pathParts = key.split("/");
26
27
  const name = pathParts.at(-1);
27
28
  const value = event.detail.value;
29
+ if (!shouldBackup(key)) {
30
+ if (!shouldPersist(key))
31
+ console.warn("[monoidentity backup]", key, "isn't marked to be backed up or saved");
32
+ return;
33
+ }
28
34
  console.debug("[monoidentity backup] saving", name);
29
35
  const parent = await getDirCached(pathParts.slice(0, -1));
30
36
  if (value != undefined) {
@@ -42,7 +48,7 @@ const saveToDir = (dir) => {
42
48
  removeEventListener(STORAGE_EVENT, listener);
43
49
  };
44
50
  };
45
- export const backupLocally = async (requestBackup) => {
51
+ export const backupLocally = async (shouldBackup, requestBackup) => {
46
52
  if (!canBackup)
47
53
  return;
48
54
  if (localStorage["monoidentity-x/backup"] == "off")
@@ -53,7 +59,7 @@ export const backupLocally = async (requestBackup) => {
53
59
  const dir = await get("backup", handles);
54
60
  if (!dir)
55
61
  throw new Error("No backup handle found");
56
- unmount = saveToDir(dir);
62
+ unmount = saveToDir(shouldBackup, dir);
57
63
  }
58
64
  else {
59
65
  localStorage["monoidentity-x/backup"] = "off";
@@ -85,7 +91,7 @@ export const backupLocally = async (requestBackup) => {
85
91
  location.reload();
86
92
  return;
87
93
  }
88
- unmount = saveToDir(dir);
94
+ unmount = saveToDir(shouldBackup, dir);
89
95
  });
90
96
  }
91
97
  };
@@ -1,2 +1,2 @@
1
1
  import { type Intent } from "./utils-transport.js";
2
- export declare const trackReady: (app: string, intents: Intent[], requestBackup: (startBackup: () => void) => void) => Promise<void>;
2
+ export declare const trackReady: (app: string, intents: Intent[], shouldBackup: (path: string) => boolean, requestBackup: (startBackup: () => void) => void) => Promise<void>;
@@ -5,7 +5,7 @@ import {} from "./utils-transport.js";
5
5
  import { conf, setLoginRecognized } from "./storage.js";
6
6
  import { backupLocally } from "./storage/backuplocally.js";
7
7
  import { backupCloud } from "./storage/backupcloud.js";
8
- export const trackReady = async (app, intents, requestBackup) => {
8
+ export const trackReady = async (app, intents, shouldBackup, requestBackup) => {
9
9
  conf(app);
10
10
  let setup = localStorage["monoidentity-x/setup"]
11
11
  ? JSON.parse(localStorage["monoidentity-x/setup"])
@@ -33,10 +33,10 @@ export const trackReady = async (app, intents, requestBackup) => {
33
33
  throw new Error("halt: redirecting");
34
34
  }
35
35
  if (setup.method == "localStorage") {
36
- await backupLocally(requestBackup);
36
+ await backupLocally(shouldBackup, requestBackup);
37
37
  }
38
38
  if (setup.method == "cloud") {
39
- await backupCloud(setup);
39
+ await backupCloud(shouldBackup, setup);
40
40
  }
41
41
  for (const provision of provisions) {
42
42
  if ("createLoginRecognized" in provision) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monoidentity",
3
- "version": "0.13.1",
3
+ "version": "0.14.0",
4
4
  "repository": "KTibow/monoidentity",
5
5
  "author": {
6
6
  "name": "KTibow"