Compare commits

...

138 Commits

Author SHA1 Message Date
vorotamoroz
badec46d9a 0.25.0 released 2025-07-19 15:21:36 +09:00
vorotamoroz
355e41f488 bump for beta 2025-07-14 00:36:09 +09:00
vorotamoroz
e0e7e1b5ca ### Fixed
- The encryption algorithm now uses HKDF with a master key.
- `Fetch everything from the remote` now works correctly.
- Extra log messages during QR code decoding have been removed.

### Changed
- Some settings have been moved to the `Patches` pane:

### Behavioural and API Changes
- `DirectFileManipulatorV2` now requires new settings (as you may already know, E2EEAlgorithm).
- The database version has been increased to `12` from `10`.
2025-07-14 00:33:40 +09:00
vorotamoroz
ce4b61557a bump 2025-07-10 11:24:59 +01:00
vorotamoroz
52b02f3888 ## 0.24.31
### Fixed

- The description of `Enable Developers' Debug Tools.` has been refined.
- Automatic conflict checking and resolution has been improved.
- Resolving conflicts dialogue will not be shown for the multiple files at once.
2025-07-10 11:12:44 +01:00
vorotamoroz
7535999388 Update updates.md 2025-07-09 22:28:50 +09:00
vorotamoroz
dccf8580b8 Update updates.md 2025-07-09 22:27:52 +09:00
vorotamoroz
e3964f3c5d bump 2025-07-09 12:48:37 +01:00
vorotamoroz
375e7bde31 ### New Feature
- New chunking algorithm `V3: Fine deduplication` has been added, and will be recommended after updates.
- New language `ko` (Korean) has been added.
- Chinese (Simplified) translation has been updated.

### Fixed

- Numeric settings are now never lost the focus during the value changing.

### Improved
- All translations have rewritten into YAML format, to easier manage and contribution.
- Doctor recommendations have now shown in the user-friendly notation.

### Refactored

- Never ending `ObsidianLiveSyncSettingTag.ts` finally had separated into each pane's file.
- Some commented-out codes have been removed.
2025-07-09 12:15:59 +01:00
vorotamoroz
1179438df8 bump 2025-06-20 12:43:39 +01:00
vorotamoroz
47ea8f6859 ## 0.24.29
### Fixed

- Synchronisation with buckets now works correctly, regardless of whether a prefix is set or the bucket has been (re-) initialised (#664).
- An information message is now displayed again, during any automatic synchronisation is enabled (#662).

### Tidied up

- Importing paths have been tidied up.
2025-06-20 12:43:15 +01:00
vorotamoroz
670fe16486 Merge branch 'main' of https://github.com/vrtmrz/obsidian-livesync 2025-06-16 02:53:30 +01:00
vorotamoroz
3f0093916c Add some note 2025-06-16 02:52:59 +01:00
vorotamoroz
9503474d06 bump 2025-06-15 18:49:31 +09:00
vorotamoroz
ddf7b243e4 ## 0.24.28
### Fixed

- Batch Update is no longer available in LiveSync mode to avoid unexpected behaviour. (#653)
- Now compatible with Cloudflare R2 again for bucket synchronisation.
- Prevention of broken behaviour due to database connection failures added (#649).
2025-06-15 18:49:16 +09:00
vorotamoroz
f37561c3c1 Update Library 2025-06-15 18:24:19 +09:00
vorotamoroz
f01429decc Merge pull request #633 from jmarmstrong1207/patch-1
Add simple docker compose to the setup guide
2025-06-10 11:27:22 +09:00
vorotamoroz
c0fcb66924 bump 2025-06-10 02:52:45 +01:00
vorotamoroz
5f76b9809b ## 0.24.27
### Improved

- We can use prefix for path for the Bucket synchronisation.
- The "Use Request API to avoid `inevitable` CORS problem" option is now promoted to the normal setting, not a niche patch.

### Fixed

- Now switching replicators applied immediately, without the need to restart Obsidian.

### Tidied up

- Some dependencies have been updated to the latest version.
2025-06-10 02:51:47 +01:00
vorotamoroz
d61d6fec37 bump manifest 2025-05-14 13:55:42 +01:00
vorotamoroz
9fdd622824 bump 2025-05-14 13:55:17 +01:00
vorotamoroz
3b8d03a189 ## 0.24.26
### New Features

- Automatic display-language changing according to the Obsidian language
  setting.
- Now we can limit files to be synchronised even in the hidden files.
- "Use Request API to avoid `inevitable` CORS problem" has been implemented.
- `Show status icon instead of file warnings banner` has been implemented.

### Improved

- All regular expressions can be inverted by prefixing `!!` now.

### Fixed

- No longer unexpected files will be gathered during hidden file sync.
- No longer broken `\n` and new-line characters during the bucket
  synchronisation.
- We can purge the remote bucket again if we using MinIO instead of AWS S3 or
  Cloudflare R2.
- Purging the remote bucket is now more reliable.
- Some wrong messages have been fixed.

### Behaviour changed

- Entering into the deeper directories to gather the hidden files is now limited
  by `/` or `\/` prefixed ignore filters.

### Etcetera

- Some code has been tidied up.
- Trying less warning-suppressing and be more safer-coding.
- Dependent libraries have been updated to the latest version.
- Some build processes have been separated to `pre` and `post` processes.
2025-05-14 13:11:03 +01:00
James Armstrong
1f1a39e5a0 Add simple docker compose 2025-04-28 06:10:29 -07:00
vorotamoroz
d0e92cff7a refine readme 2025-04-23 06:45:03 +01:00
vorotamoroz
5addddc792 Update doc 2025-04-23 05:51:51 +01:00
vorotamoroz
d978892661 Update docs 2025-04-23 05:11:59 +01:00
vorotamoroz
cfb061a6a2 add note to troubleshooting 2025-04-23 05:10:40 +01:00
vorotamoroz
381055fc93 bump 2025-04-22 11:29:42 +01:00
vorotamoroz
37d12916fc ## 0.24.25
### Improved

- Peer-to-peer synchronisation has been got more robust.

### Fixed

- No longer broken falsy values in settings during set-up by the QR code generation.

### Refactored

- Some `window` references now have pointed to `globalThis`.
- Some sloppy-import has been fixed.
- A server side implementation `Synchromesh` has been suffixed with `deno` instead of `server` now.
2025-04-22 11:28:55 +01:00
vorotamoroz
944aa846c4 bump 2025-04-15 11:10:37 +01:00
vorotamoroz
abca808e29 ### Fixed
- No longer broken JSON files including `\n`, during the bucket synchronisation. (#623)
- Custom headers and JWT tokens are now correctly sent to the server during configuration checking. (#624)

### Improved

- Bucket synchronisation has been enhanced for better performance and reliability.
    - Now less duplicated chunks are sent to the server.
    - Fetching conflicted files from the server is now more reliable.
    - Dependent libraries have been updated to the latest version.
2025-04-15 11:09:49 +01:00
vorotamoroz
90bb610133 Update submodule 2025-04-14 03:23:24 +01:00
vorotamoroz
9c5e9fe63b Conclusion stated. 2025-04-11 14:13:22 +01:00
vorotamoroz
00dfae24d7 bump 2025-04-10 14:28:03 +01:00
vorotamoroz
d8a41fe45d ### New Feature
- Now, we can send custom headers to the server.
- Authentication with JWT in CouchDB is now supported.

### Improved

- The QR Code for set-up can be shown also from the setting dialogue now.
- Conflict checking for preventing unexpected overwriting on the boot-up process has been quite faster.

### Fixed

- Some bugs on Dev and Testing modules have been fixed.
2025-04-10 14:24:33 +01:00
vorotamoroz
30467d1c25 Add link to P2P pseudo client. 2025-04-04 12:30:21 +01:00
vorotamoroz
f8351f1d45 Merge branch 'main' of https://github.com/vrtmrz/obsidian-livesync 2025-04-04 11:52:12 +01:00
vorotamoroz
5924af98ab Update Library (Probably no effect) 2025-04-04 11:52:05 +01:00
vorotamoroz
2769b61da4 Update setup_own_server.md
Add note; #609
2025-04-04 18:24:13 +09:00
vorotamoroz
bb4409221d Update README.md 2025-04-03 20:57:15 +09:00
vorotamoroz
f398c14200 Bump for release-mistake. 2025-04-01 10:42:29 +01:00
vorotamoroz
27d58508dc Missed 2025-04-01 10:38:12 +01:00
vorotamoroz
d4dea5b226 bump 2025-04-01 10:21:38 +01:00
vorotamoroz
c79dc30cba ## 0.24.21
### Fixed

- No longer conflicted files are handled in the boot-up process. No more unexpected overwriting.
    - It ignores `Always overwrite with a newer file`, and always be prevented for the safety. Please pick it manually or open the file.
- Some log messages on conflict resolution has been corrected.
- Automatic merge notifications, displayed on the grounds of `same`, have been degraded to logs.

### Improved

- Now we can fetch the remote database with keeping local files completely intact.
    - In new option, all files are stored into the local database before the fetching, and will be merged automatically or detected as conflicts.
- The dialogue presenting options when performing `Fetch` are now more informative.

### Refactored

- Some class methods have been fixed its arguments to be more consistent.
- Types have been defined for some conditional results.
2025-04-01 10:20:21 +01:00
vorotamoroz
b3119ee8a9 bump and update dependencies 2025-03-24 18:55:02 +09:00
vorotamoroz
2a1d71da5c Improved: show details of TypeError using Obsidian API. 2025-03-24 12:04:58 +09:00
vorotamoroz
24f31ed19e Merge branch 'main' of https://github.com/vrtmrz/obsidian-livesync 2025-03-19 04:43:06 +01:00
vorotamoroz
a982629ae6 Update draft : possibly I should share cSpell dictionary. 2025-03-19 04:42:50 +01:00
vorotamoroz
85140aecab Update README.md 2025-03-05 20:40:04 +09:00
vorotamoroz
3f2e23ee88 bump 2025-03-05 11:13:58 +00:00
vorotamoroz
6049c19e8a ## 0.24.19
### New Feature

- Now we can generate a QR Code for transferring the configuration to another device.
2025-03-05 11:12:00 +00:00
vorotamoroz
65648683a3 bump 2025-02-28 12:02:45 +00:00
vorotamoroz
5d70f2c1e9 ## 0.24.18
### Fixed

- Now no chunk creation errors will be raised after switching `Compute revisions for chunks`.
- Some invisible file can be handled correctly (e.g., `writing-goals-history.csv`).
- Fetching configuration from the server is now saves the configuration immediately (if we are not in the wizard).

### Improved

- Mismatched configuration dialogue is now more informative, and rewritten to more user-friendly.
- Applying configuration mismatch is now without rebuilding (at our own risks).
- Now, rebuilding is decided more fine grained.

### Improved internally

- Translations can be nested. i.e., task:`Some procedure`, check: `%{task} checking`, checkfailed: `%{check} failed` produces `Some procedure checking failed`.
2025-02-28 11:58:15 +00:00
vorotamoroz
cbcfdc453e update default vault and bump for release 2025-02-27 13:39:28 +00:00
vorotamoroz
a4eb21593c bump 2025-02-27 13:24:51 +00:00
vorotamoroz
05eb2c8262 ## 0.24.16
### Improved

#### Peer-to-Peer

- Now peer-to-peer synchronisation checks the settings are compatible with each other.
- Peer-to-peer synchronisation now handles the platform and detects pseudo-clients.

#### General

- New migration method has been implemented, that called `Doctor`.

- The minimum interval for replication to be caused when an event occurs can now be configurable.
- Some detail note has been added and change nuance about the `Report` in the setting dialogue, which had less informative.

### Behaviour and default changed

- `Compute revisions for chunks` are backed into enabled again. it is necessary for garbage collection of chunks.

### Refactored

- Platform specific codes are more separated. No longer `node` modules were used in the browser and Obsidian.
2025-02-27 13:23:11 +00:00
vorotamoroz
fecefa3631 ### Fixed
- Now, even without WeakRef, Polyfill is used and the whole thing works without error. However, if you can switch WebView Engine, it is recommended to switch to a WebView Engine that supports WeakRef.

And bumped.
2025-02-20 10:40:18 +00:00
vorotamoroz
f8c4d5ccb0 Add a bit more. 2025-02-18 13:48:41 +00:00
vorotamoroz
e63e79bc8e Add note of flag files. 2025-02-18 13:36:15 +00:00
vorotamoroz
ed76125f3d bump 2025-02-18 13:02:54 +00:00
vorotamoroz
70f4e23474 ## 0.24.14
### Fixed

- Resolving conflicts of JSON files (and sensibly merging them) is now working fine, again!
    - And, failure logs are more informative.
- More robust to release the event listeners on unwatching the local database.

### Refactored

- JSON file conflict resolution dialogue has been rewritten into svelte v5.
- Upgrade eslint.
- Remove unnecessary pragma comments for eslint.
2025-02-18 12:59:18 +00:00
vorotamoroz
f6d5b78cc8 bump 2025-02-17 11:35:34 +00:00
vorotamoroz
405624b51b ## 0.24.13
### Fixed
#### General Replication
- No longer unexpected errors occur when the replication is stopped during for some reason (e.g., network disconnection).
#### Peer-to-Peer Synchronisation
- Set-up process will not receive data from unexpected sources.
- No longer resource leaks while enabling the `broadcasting changes`
- Logs are less verbose.
- Received data is now correctly dispatched to other devices.
- `Timeout` error now more informative.
- No longer timeout error occurs for reporting the progress to other devices.
- Decision dialogues for the same thing are not shown multiply at the same time anymore.
- Disconnection of the peer-to-peer synchronisation is now more robust and less error-prone.
#### Webpeer
- Now we can toggle Peers' configuration.
### Refactored
- Cross-platform compatibility layer has been improved.
- Common events are moved to the common library.
- Displaying replication status of the peer-to-peer synchronisation is separated from the main-log-logic.
- Some file names have been changed to be more consistent.
2025-02-17 11:33:35 +00:00
vorotamoroz
90c0ff22b9 Add paths for future maintenance. 2025-02-17 11:30:42 +00:00
vorotamoroz
67568ea886 bump 2025-02-14 11:28:01 +00:00
vorotamoroz
cc29b4058d 0.24.12
Fixed
-  No longer unnecessary acknowledgements are sent when starting peer-to-peer synchronisation.

Refactored
- Platform impedance-matching-layer has been improved.
- Some UIs have been get isomorphic among Obsidian and web applications (for `webpeer`).
2025-02-14 11:15:22 +00:00
vorotamoroz
4e8243b3d5 Update release.yml 2025-02-13 22:02:22 +09:00
vorotamoroz
4eb1787784 bump 2025-02-13 12:58:15 +00:00
vorotamoroz
1cd1465f2c 0.24.11
Improved

- New Translation: `es` (Spanish) by @zeedif (Thank you so much)!
- Now all of messages can be selectable and copyable, also on the iPhone, iPad, and Android devices. Now we can copy or share the messages easily.

New Feature

- Peer-to-Peer Synchronisation has been implemented!

Fixed

- No longer memory or resource leaks when the plug-in is disabled.
- Now deleted chunks are correctly detected on conflict resolution, and we are guided to resurrect them.
- Hanging issue during the initial synchronisation has been fixed.
- Some unnecessary logs have been removed.
- Now all modal dialogues are correctly closed when the plug-in is disabled.

Refactor

- Several interfaces have been moved to the separated library.
- Translations have been moved to each language file, and during the build, they are merged into one file.
- Non-mobile friendly code has been removed and replaced with the safer code.
- Started writing Platform impedance-matching-layer.
- Svelte has been updated to v5.
- Some function have got more robust type definitions.
- Terser optimisation has slightly improved.
- During the build, analysis meta-file of the bundled codes will be generated.
2025-02-13 12:48:00 +00:00
vorotamoroz
45ceca8bb6 run prettier and update lib 2025-02-12 03:44:46 +00:00
vorotamoroz
7b385aab9e Merge branch 'main' of https://github.com/vrtmrz/obsidian-livesync 2025-02-12 03:22:50 +00:00
vorotamoroz
98411e5f48 Merge pull request #573 from zeedif/main
Migrate UI strings to $tf for translation support
2025-02-12 12:21:44 +09:00
vorotamoroz
b6687e2fb0 Adding bundle size analysis 2025-02-12 03:18:59 +00:00
Zeedif
658cbb7ded Merge branch 'main' into main 2025-02-06 20:46:26 -06:00
vorotamoroz
08a48154fa Add note 2025-02-05 01:35:17 +00:00
vorotamoroz
62501a5940 bump again 2025-02-05 01:33:31 +00:00
vorotamoroz
ccb3dd52de Dep update 2025-02-05 01:10:03 +00:00
vorotamoroz
3e5f4c8946 bump 2025-02-05 01:04:21 +00:00
vorotamoroz
54e64c59a9 - Prettified some source.
- Fixed the issue which the filename is shown as `undefined`.
- Fixed the issue where files transferred at short intervals were not reflected.
- Updated dependency (including new translation)
2025-02-05 00:58:51 +00:00
CRuiz
588840ff8b Reemplazar $tf por $msg 2025-02-04 08:22:43 -06:00
CRuiz
e6b8dfb279 Actualizar mensaje de recomendación de URI en la migración del módulo 2025-01-22 15:57:36 -06:00
CRuiz
73782c5389 Add translation ids 2025-01-22 13:41:18 -06:00
vorotamoroz
f2b667d75e bump 2025-01-22 11:56:36 +00:00
vorotamoroz
9b1588a65b ## 0.24.8
### Fixed
-   Some parallel-processing tasks are now performed more safely.
-   Some error messages has been fixed.
### Improved
-   Synchronisation is now more efficient and faster.
-   Saving chunks is a bit more robust.
### New Feature
-   We can remove orphaned chunks again, now!
2025-01-22 11:55:56 +00:00
vorotamoroz
0629bc04bb Merge branch 'main' of https://github.com/vrtmrz/obsidian-livesync 2025-01-07 11:33:13 +00:00
vorotamoroz
b0e97e6c96 Bump 2025-01-07 11:31:58 +00:00
vorotamoroz
7f853b0222 - 0.24.7
- Fixed (Security)
   - Assigning IDs to chunks has been corrected for more safety.
  - Fixed
    - Conflict resolution dialogue has been fixed
    - Resolving conflicts by timestamp has been fixed
  - Improved
    -  Notifications can be suppressed for the hidden files update now.
    -  No longer uses the old-xxhash and sha1 for generating the chunk ID.
2025-01-07 11:28:56 +00:00
vorotamoroz
3d4ad4a3b4 Update README.md
Add acknowledgements
2025-01-02 15:02:35 +09:00
vorotamoroz
4b1fff852a Merge pull request #544 from dcvdiego/main
make self-hosted docs slightly more readable
2024-12-23 12:28:37 +09:00
dcvdiego
08d7d24baf Update setup_own_server.md 2024-12-21 16:26:55 +00:00
vorotamoroz
9db3c3df0a bump 2024-12-14 02:32:07 +09:00
vorotamoroz
672940ad6f Fixed: No longer the empty-and-stilled log pane. 2024-12-14 02:31:31 +09:00
vorotamoroz
2338601fae 0.24.5 (by Library Fixing)
Fixed
- Fix some wrong behaviour during comparing JSON objects

Improved
- Reactive values are now shown more smoothly (Logs,a status line.
2024-12-13 10:59:05 +00:00
vorotamoroz
3e657b38a9 bump 2024-12-12 11:12:04 +00:00
vorotamoroz
21861d8c51 ## 0.24.4
### Fixed

-   Fixed so many inefficient and buggy modules inherited from the past.

### Improved

-   Tasks are now executed in an efficient asynchronous library.
-   On-demand chunk fetching is now more efficient and keeps the interval between requests.
    -   This will reduce the load on the server and the network.
    -   And, safe for the Cloudant.
2024-12-12 11:10:50 +00:00
vorotamoroz
3bb4aba395 Update dependencies 2024-12-12 11:10:29 +00:00
vorotamoroz
751de5a13e bump 2024-12-09 00:46:15 +00:00
vorotamoroz
29229f809b Update the doc 2024-12-09 00:40:15 +00:00
vorotamoroz
2d0dc2a389 Merge pull request #543 from Volkor3-16/main
Fix up wording of messages
2024-12-09 09:12:51 +09:00
Volkor
6cbe319b80 remove a unneeded "the" 2024-12-04 02:02:25 +11:00
Volkor
e9fe58f818 run pretty - revert crlf to lf
since most modern ides handle lf on windows fine, and the entire project is in lf already, i'll set this back
2024-12-04 01:13:12 +11:00
Volkor
61524e1c44 FIX: manual setup button 2024-12-04 00:17:41 +11:00
vorotamoroz
c25eaa09c9 fixed: Fixed font-family of in-editor status. 2024-12-03 10:15:53 +00:00
Volkor
71987e6814 Change a few more lines to more correctly reflect the settings 2024-11-27 23:39:47 +11:00
vorotamoroz
b15d0710e5 bump 2024-11-21 11:40:37 +00:00
vorotamoroz
9d304b3233 v0.24.2
Rewritten

-   Hidden File Sync is now respects the file changes on the storage. Not simply comparing modified times.
    -   This makes hidden file sync more robust and reliable.

Fixed

-   `Scan hidden files before replication` is now configurable again.
-   Some unexpected errors are now handled more gracefully.
-   Meaningless event passing during boot sequence is now prevented.
-   Error handling for non-existing files has been fixed.
-   Hidden files will not be batched to avoid the potential error.
    -   This behaviour had been causing the error in the previous versions in specific situations.
-   The log which checking automatic conflict resolution is now in verbose level.
-   Replication log (skipping non-targetting files) shows the correct information.
-   The dialogue that asking enabling optional feature during `Rebuild Everything` now prevents to show the `overwrite` option.
    -   The rebuilding device is the first, meaningless.
-   Files with different modified time but identical content are no longer processed repeatedly.
-   Some unexpected errors which caused after terminating plug-in are now avoided.
-

Improved

-   JSON files are now more transferred efficiently.
    -   Now the JSON files are transferred in more fine chunks, which makes the transfer more efficient.
2024-11-21 11:40:15 +00:00
dcvdiego
d062b13040 make docs slightly more readable 2024-11-19 16:28:55 +00:00
Volkor
7eceab59af Fix up wording of messages 2024-11-18 21:55:22 +11:00
vorotamoroz
ed5cb3e043 Format utility 2024-11-12 01:09:07 +00:00
vorotamoroz
574fdf9202 Format submodule 2024-11-12 01:08:35 +00:00
vorotamoroz
9ec7b809a9 I have adapted the new-line-char to the codebase. But I am not sure if this is the right thing to do. 2024-11-12 01:04:46 +00:00
vorotamoroz
4d302aff9d Merge pull request #533 from doublethefish/chore/formatting
chore(formatting): adds prettier for consistent coding style
2024-11-12 09:53:50 +09:00
vorotamoroz
b70009f4a9 Merge branch 'main' into chore/formatting 2024-11-12 09:37:52 +09:00
Frank Harrison
c24ee32f37 chore(formatting): ignores generated code in pretty 2024-11-11 09:45:53 +00:00
Frank Harrison
012d0aa4df chore(format): also format json (no effect)
Adds json-file formating to the prettier command. Currently this has no
effect, but if the `tabWidth` is changed to 2 and the `printWidth`
reduced to 88, then it will also impact the json files and keep the
formatting consistent across code and data.
2024-11-11 09:41:56 +00:00
Frank Harrison
5c97e5b672 chore(format): no intentional behaviour change - runs pretty 2024-11-11 09:41:52 +00:00
Frank Harrison
6e1eb36f3b chore(format): adds prettier and commands to run it
... including ci/cd check-only commands.

The code is quite complex and missing a lot of dev-ops types checks and
standards. One of the key problems with codebases like this is
on-boarding new developers to the codebase (like myself). When there is
a consistent and enforced coding-style (irrespective of what the coding
style is) it makes it _significantly_ easier for collaborators and
maintainers to get on with the job in hand. It also, from a day-2-day
developer perspective, significantly reduces cognitive overhead re
reading code.
Finally this is a "trial balloon" PR, if this patch is accepted I will
likely do more work on testing and docs for the project.

- The new prettier config is a non-standard setup, but a close-match to
  how the code _currently_ looks.
- 120 col-width print width (instead of the better and more
  information-dense 88), this is so the diff after applying prettier to
  the code is less disruptive, whilst still showing the benefits of using
  a prettier.
- We use `tabWidth` setting of 4 as the code uses that more common
  setting instead of the more compact 2 spaces - note that 2 often leads
  to more readable and compact code.
- We enforce trailing commas, as that seems to be the norm in this
  code-base. We choose the `es5` standard here.
- We enforce tailing semi-colons (`semi`) as the majority of code used
  that flavour of `js`/`ts`.
- For now we only run on code and not json files.

This is designed such that `npm run pretty` re-formats the code for
development, and when integrated with ci/cd, `prettyCheck` will return
non-zero exit codes when formatting doesn't match the coding standards.
2024-11-11 09:41:35 +00:00
vorotamoroz
8809aee327 Add default values to a new SetupURI. 2024-11-11 08:05:10 +00:00
vorotamoroz
fc04c557fc Merge branch 'main' of https://github.com/vrtmrz/obsidian-livesync 2024-11-11 01:23:07 +00:00
vorotamoroz
115a0d2d8a bump 2024-11-11 01:22:40 +00:00
vorotamoroz
2c97289ec8 Fixed
-   Vault History can show the correct information of match-or-not for each file and database even if it is a binary file.
-   `Sync settings via markdown` is now hidden during the setup wizard.
-   Verify and Fix will ignore the hidden files if the hidden file sync is disabled.

New feature
-   Now we can fetch the tweaks from the remote database while the setting dialogue and wizard are processing.

Improved
-   More things are moved to the modules.
    -   Includes the Main codebase. Now `main.ts` is almost stub.
-   EventHub is now more robust and typesafe.
2024-11-11 00:58:31 +00:00
vorotamoroz
6d472d17fd Fix output order 2 2024-11-09 10:31:34 +09:00
vorotamoroz
3a3aabfd11 Fix scripting 2024-11-09 10:21:05 +09:00
vorotamoroz
8b45dd1d24 .. 2024-11-08 11:12:35 +00:00
vorotamoroz
a2b36ccf31 v0.24.0! 2024-10-28 11:18:29 +09:00
vorotamoroz
25e30fa09d Merge tag '0.24.0.dev-rc8' 2024-10-28 10:24:38 +09:00
vorotamoroz
8f5bc387b4 Update manifest-beta.json 2024-10-25 20:44:32 +09:00
vorotamoroz
5afe24c460 0.24.0.dev-rc8 2024-10-25 12:39:32 +01:00
vorotamoroz
658a09f1cc Update manifest-beta.json 2024-10-24 20:46:08 +09:00
vorotamoroz
293c731437 Update manifest-beta.json 2024-10-22 18:11:18 +09:00
vorotamoroz
9e8d126259 Update manifest-beta.json 2024-10-21 17:52:28 +09:00
vorotamoroz
1f0ad4eb1e Update manifest-beta.json 2024-10-18 19:23:48 +09:00
vorotamoroz
5023d6da0b Merge pull request #474 from nyawox/per-file-sync-grammar
fix: per-file customization sync description grammar
2024-10-18 11:43:03 +09:00
vorotamoroz
49160c7d57 Merge branch 'main' into per-file-sync-grammar 2024-10-18 11:42:49 +09:00
vorotamoroz
4434224c29 Merge pull request #479 from fuhrysteve/patch-1
add username and password to setup URI instructions
2024-10-18 11:40:23 +09:00
vorotamoroz
cf3b9e5522 Add manifest beta 2024-10-17 10:26:19 +01:00
Stephen J. Fuhry
f778107727 add username and password to setup URI instructions 2024-08-07 14:43:38 -04:00
nyawox
12d825ea49 fix: per-file customization sync description grammar 2024-07-26 23:38:01 +09:00
127 changed files with 24489 additions and 10497 deletions

View File

@@ -1,9 +0,0 @@
node_modules
build
.eslintrc.js.bak
src/lib/src/patches/pouchdb-utils
esbuild.config.mjs
rollup.config.js
src/lib/test
src/lib/src/cli
main.js

View File

@@ -1,13 +1,34 @@
{ {
"root": true, "root": true,
"parser": "@typescript-eslint/parser", "parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"], "plugins": [
"extends": ["eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended"], "@typescript-eslint",
"eslint-plugin-svelte",
"eslint-plugin-import"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": { "parserOptions": {
"sourceType": "module", "sourceType": "module",
"project": ["tsconfig.json"] "project": [
"tsconfig.json"
]
}, },
"ignorePatterns": [], "ignorePatterns": [
"**/node_modules/*",
"**/jest.config.js",
"src/lib/coverage",
"src/lib/browsertest",
"**/test.ts",
"**/tests.ts",
"**/**test.ts",
"**/**.test.ts",
"esbuild.*.mjs",
"terser.*.mjs"
],
"rules": { "rules": {
"no-unused-vars": "off", "no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [ "@typescript-eslint/no-unused-vars": [

View File

@@ -17,7 +17,7 @@ jobs:
- name: Use Node.js - name: Use Node.js
uses: actions/setup-node@v1 uses: actions/setup-node@v1
with: with:
node-version: '18.x' # You might need to adjust this value to your own version node-version: '22.x' # You might need to adjust this value to your own version
# Get the version number and put it in a variable # Get the version number and put it in a variable
- name: Get Version - name: Get Version
id: version id: version

3
.gitignore vendored
View File

@@ -10,6 +10,9 @@ package-lock.json
main.js main.js
main_org.js main_org.js
*.js.map *.js.map
meta.json
meta-*.json
# obsidian # obsidian
data.json data.json

2
.prettierignore Normal file
View File

@@ -0,0 +1,2 @@
pouchdb-browser.js
main_org.js

7
.prettierrc Normal file
View File

@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"printWidth": 120,
"semi": true,
"endOfLine": "lf"
}

View File

@@ -1,30 +1,39 @@
<!-- For translation: 20240227r0 -->
# Self-hosted LiveSync # Self-hosted LiveSync
[Japanese docs](./README_ja.md) - [Chinese docs](./README_cn.md). [Japanese docs](./README_ja.md) - [Chinese docs](./README_cn.md).
Self-hosted LiveSync is a community-implemented synchronization plugin, available on every obsidian-compatible platform and using CouchDB or Object Storage (e.g., MinIO, S3, R2, etc.) as the server.
Self-hosted LiveSync is a community-developed synchronisation plug-in available on all Obsidian-compatible platforms. It leverages robust server solutions such as CouchDB or object storage systems (e.g., MinIO, S3, R2, etc.) to ensure reliable data synchronisation.
Additionally, it supports peer-to-peer synchronisation using WebRTC now (experimental), enabling you to synchronise your notes directly between devices without relying on a server.
![obsidian_live_sync_demo](https://user-images.githubusercontent.com/45774780/137355323-f57a8b09-abf2-4501-836c-8cb7d2ff24a3.gif) ![obsidian_live_sync_demo](https://user-images.githubusercontent.com/45774780/137355323-f57a8b09-abf2-4501-836c-8cb7d2ff24a3.gif)
Note: This plugin cannot synchronise with the official "Obsidian Sync". >[!IMPORTANT]
> This plug-in is not compatible with the official "Obsidian Sync" and cannot synchronise with it.
## Features ## Features
- Synchronise vaults efficiently with minimal traffic.
- Handle conflicting modifications effectively.
- Automatically merge simple conflicts.
- Use open-source solutions for the server.
- Compatible solutions are supported.
- Support end-to-end encryption.
- Synchronise settings, snippets, themes, and plug-ins via [Customisation Sync (Beta)](#customization-sync) or [Hidden File Sync](#hiddenfilesync).
- Enable WebRTC peer-to-peer synchronisation without requiring a `host` (Experimental).
- This feature is still in the experimental stage. Please exercise caution when using it.
- WebRTC is a peer-to-peer synchronisation method, so **at least one device must be online to synchronise**.
- Instead of keeping your device online as a stable peer, you can use two pseudo-peers:
- [livesync-serverpeer](https://github.com/vrtmrz/livesync-serverpeer): A pseudo-client running on the server for receiving and sending data between devices.
- [webpeer](https://github.com/vrtmrz/livesync-commonlib/tree/main/apps/webpeer): A pseudo-client for receiving and sending data between devices.
- A pre-built instance is available at [fancy-syncing.vrtmrz.net/webpeer](https://fancy-syncing.vrtmrz.net/webpeer/) (hosted on the vrtmrz blog site). This is also peer-to-peer. Feel free to use it.
- For more information, refer to the [English explanatory article](https://fancy-syncing.vrtmrz.net/blog/0034-p2p-sync-en.html) or the [Japanese explanatory article](https://fancy-syncing.vrtmrz.net/blog/0034-p2p-sync).
- Synchronize vaults very efficiently with less traffic. This plug-in may be particularly useful for researchers, engineers, and developers who need to keep their notes fully self-hosted for security reasons. It is also suitable for anyone seeking the peace of mind that comes with knowing their notes remain entirely private.
- Good at conflicted modification.
- Automatic merging for simple conflicts.
- Using OSS solution for the server.
- Compatible solutions can be used.
- Supporting End-to-end encryption.
- Synchronisation of settings, snippets, themes, and plug-ins, via [Customization sync(Beta)](#customization-sync) or [Hidden File Sync](#hiddenfilesync)
- WebClip from [obsidian-livesync-webclip](https://chrome.google.com/webstore/detail/obsidian-livesync-webclip/jfpaflmpckblieefkegjncjoceapakdf)
This plug-in might be useful for researchers, engineers, and developers with a need to keep their notes fully self-hosted for security reasons. Or just anyone who would like the peace of mind of knowing that their notes are fully private.
>[!IMPORTANT] >[!IMPORTANT]
> - Before installing or upgrading this plug-in, please back your vault up. > - Before installing or upgrading this plug-in, please back up your vault.
> - Do not enable this plugin with another synchronization solution at the same time (including iCloud and Obsidian Sync). > - Do not enable this plug-in alongside another synchronisation solution at the same time (including iCloud and Obsidian Sync).
> - This is a synchronization plugin. Not a backup solution. Do not rely on this for backup. > - For backups, we also provide a plug-in called [Differential ZIP Backup](https://github.com/vrtmrz/diffzip).
## How to use ## How to use
@@ -43,9 +52,11 @@ This plug-in might be useful for researchers, engineers, and developers with a n
1. [Setup CouchDB on fly.io](docs/setup_flyio.md) 1. [Setup CouchDB on fly.io](docs/setup_flyio.md)
2. [Setup your CouchDB](docs/setup_own_server.md) 2. [Setup your CouchDB](docs/setup_own_server.md)
2. Configure plug-in in [Quick Setup](docs/quick_setup.md) 2. Configure plug-in in [Quick Setup](docs/quick_setup.md)
> [!TIP] > [!TIP]
> Now, fly.io has become not free. Fortunately, even though there are some issues, we are still able to use IBM Cloudant. Here is [Setup IBM Cloudant](docs/setup_cloudant.md). It will be updated soon! > Fly.io is no longer free. Fortunately, despite some issues, we can still use IBM Cloudant. Refer to [Setup IBM Cloudant](docs/setup_cloudant.md).
> And also, we can use peer-to-peer synchronisation without a server. Or very cheap Object Storage -- Cloudflare R2 can be used for free.
> HOWEVER, most importantly, we can use the server that we trust. Therefore, please set up your own server.
> CouchDB can be run on a Raspberry Pi. (But please be careful about the security of your server).
## Information in StatusBar ## Information in StatusBar
@@ -75,10 +86,16 @@ Synchronization status is shown in the status bar with the following icons.
To prevent file and database corruption, please wait to stop Obsidian until all progress indicators have disappeared as possible (The plugin will also try to resume, though). Especially in case of if you have deleted or renamed files. To prevent file and database corruption, please wait to stop Obsidian until all progress indicators have disappeared as possible (The plugin will also try to resume, though). Especially in case of if you have deleted or renamed files.
## Tips and Troubleshooting ## Tips and Troubleshooting
If you are having problems getting the plugin working see: [Tips and Troubleshooting](docs/troubleshooting.md) If you are having problems getting the plugin working see: [Tips and Troubleshooting](docs/troubleshooting.md).
## Acknowledgements
The project has been in continual progress and harmony thanks to:
- Many [Contributors](https://github.com/vrtmrz/obsidian-livesync/graphs/contributors).
- Many [GitHub Sponsors](https://github.com/sponsors/vrtmrz#sponsors).
- JetBrains Community Programs / Support for Open-Source Projects. <img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.png" alt="JetBrains logo" height="24">
May those who have contributed be honoured and remembered for their kindness and generosity.
## License ## License

93
README_es.md Normal file
View File

@@ -0,0 +1,93 @@
<!-- For translation: 20240227r0 -->
# Self-hosted LiveSync
[Documentación en inglés](./README_ja.md) - [Documentación en japonés](./README_ja.md) - [Documentación en chino](./README_cn.md).
Self-hosted LiveSync es un plugin de sincronización implementado por la comunidad, disponible en todas las plataformas compatibles con Obsidian y utiliza CouchDB o Almacenamiento de Objetos (por ejemplo, MinIO, S3, R2, etc.) como servidor.
![Demostración de Obsidian Live Sync](https://user-images.githubusercontent.com/45774780/137355323-f57a8b09-abf2-4501-836c-8cb7d2ff24a3.gif)
Nota: Este plugin no puede sincronizarse con el "Obsidian Sync" oficial.
## Características
- Sincroniza bóvedas de manera eficiente con menos tráfico.
- Buen manejo de modificaciones en conflicto.
- Fusión automática para conflictos simples.
- Uso de soluciones de código abierto para el servidor.
- Pueden usarse soluciones compatibles.
- Soporte de cifrado de extremo a extremo.
- Sincronización de configuraciones, fragmentos, temas y complementos a través de [Sincronización de personalización \(Beta\)](#customization-sync) o [Sincronización de archivos ocultos](#hiddenfilesync)
- WebClip de [obsidian-livesync-webclip](https://chrome.google.com/webstore/detail/obsidian-livesync-webclip/jfpaflmpckblieefkegjncjoceapakdf)
Este plugin puede ser útil para investigadores, ingenieros y desarrolladores que necesitan mantener sus notas totalmente autoalojadas por razones de seguridad, o para aquellos que deseen tener la tranquilidad de saber que sus notas son totalmente privadas.
>[!IMPORTANTE]
> - Antes de instalar o actualizar este plugin, realice un respaldo de su bóveda.
> - No active este plugin junto con otra solución de sincronización al mismo tiempo (incluyendo iCloud y Obsidian Sync).
> - Este es un plugin de sincronización, no una solución de respaldo. No confíe en él para realizar respaldos.
## Cómo usar
### Configuración en 3 minutos - CouchDB en fly.io
**Recomendado para principiantes**
[![Configuración de LiveSync en Fly.io 2024 usando Google Colab](https://img.youtube.com/vi/7sa_I1832Xc/0.jpg)](https://www.youtube.com/watch?v=7sa_I1832Xc)
1. [Configurar CouchDB en fly.io](docs/setup_flyio_es.md)
2. Configurar el plugin en [Configuración rápida](docs/quick_setup_es.md)
### Configuración manual
1. Configurar el servidor
1. [Configurar CouchDB en fly.io](docs/setup_flyio_es.md)
2. [Configurar su CouchDB](docs/setup_own_server_es.md)
2. Configura el plugin en [Configuración rápida](docs/quick_setup_es.md)
> [!CONSEJO]
> Actualmente, fly.io ya no es gratuito. Afortunadamente, aunque hay algunos problemas, aún podemos usar IBM Cloudant. Aquí está como [Configurar IBM Cloudant](docs/setup_cloudant.md). ¡Se actualizará pronto!
## Información en la barra de estado
El estado de sincronización se muestra en la barra de estado con los siguientes iconos.
- Indicador de actividad
- 📲 Solicitud de red
- Estado
- ⏹️ Detenido
- 💤 LiveSync activado. Esperando cambios
- ⚡️ Sincronización en progreso
- ⚠ Ocurrió un error
- Indicador estadístico
- ↑ Chunks y metadatos subidos
- ↓ Chunks y metadatos descargados
- Indicador de progreso
- 📥 Elementos transferidos sin procesar
- 📄 Operación de base de datos en curso
- 💾 Procesos de escritura en almacenamiento en curso
- ⏳ Procesos de lectura en almacenamiento en curso
- 🛫 Procesos de lectura en almacenamiento pendientes
- 📬 Procesos de lectura en almacenamiento por lotes
- ⚙️ Procesos de almacenamiento de archivos ocultos en curso o pendientes
- 🧩 Chunks en espera
- 🔌 Elementos de personalización en curso (Configuración, fragmentos y plugins)
Para prevenir la corrupción de archivos y bases de datos, antes de detener Obsidian espere hasta que todos los indicadores de progreso hayan desaparecido (el plugin también intentará reanudar, sin embargo). Especialmente en caso de que haya eliminado o renombrado archivos.
## Consejos y Solución de Problemas
Si tienes problemas para hacer funcionar el plugin, consulta: [Consejos y solución de problemas](docs/troubleshooting_es.md).
## Agradecimientos
El proyecto ha progresado y mantenido en armonía gracias a:
- Muchos [Colaboradores](https://github.com/vrtmrz/obsidian-livesync/graphs/contributors)
- Muchos [Patrocinadores de GitHub](https://github.com/sponsors/vrtmrz#sponsors)
- Programas comunitarios de JetBrains / Soporte para Proyectos de Código Abierto <img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.png" alt="JetBrains logo." height="24">
Que aquellos que han contribuido sean honrados y recordados por su amabilidad y generosidad.
## Licencia
Licenciado bajo la Licencia MIT.

View File

@@ -25,10 +25,10 @@ npm run buildDev
## Make messages to be translated ## Make messages to be translated
1. Find the message that you want to be translated. 1. Find the message that you want to be translated.
2. Change the literal to a tagged template literal using `$f`, like below. 2. Change the literal to use `$tf`, like below.
```diff ```diff
- Logger("Could not determine passphrase to save data.json! You probably make the configuration sure again!", LOG_LEVEL_URGENT); - Logger("Could not determine passphrase to save data.json! You probably make the configuration sure again!", LOG_LEVEL_URGENT);
+ Logger($f`Could not determine passphrase to save data.json! You probably make the configuration sure again!`, LOG_LEVEL_URGENT); + Logger($tf('someKeyForPassphraseError'), LOG_LEVEL_URGENT);
``` ```
3. Make the PR to `https://github.com/vrtmrz/obsidian-livesync`. 3. Make the PR to `https://github.com/vrtmrz/obsidian-livesync`.
4. Follow the steps of "Add translations for already defined terms" to add the translations. 4. Follow the steps of "Add translations for already defined terms" to add the translations.

BIN
docs/all_toggles.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

View File

@@ -1,6 +1,6 @@
# Keep newborn chunks in Eden. # Keep newborn chunks in Eden
NOTE: This is the planned feature design document. This is planned, but not be implemented now (v0.23.3). This has not reached the design freeze and will be added to from time to time. Notice: deprecated. please refer to the result section of this document.
## Goal ## Goal
@@ -19,15 +19,18 @@ Reduce the number of chunks which in volatile, and reduce the usage of storage o
- The problem is that this unnecessary chunking slows down both local and remote operations. - The problem is that this unnecessary chunking slows down both local and remote operations.
## Prerequisite ## Prerequisite
- The implementation must be able to control the size of the document appropriately so that it does not become non-transferable (1). - The implementation must be able to control the size of the document appropriately so that it does not become non-transferable (1).
- The implementation must be such that data corruption can be avoided even if forward compatibility is not maintained; due to the nature of Self-hosted LiveSync, backward version connexions are expected. - The implementation must be such that data corruption can be avoided even if forward compatibility is not maintained; due to the nature of Self-hosted LiveSync, backward version connexions are expected.
- Viewed as a feature: - Viewed as a feature:
- This feature should be disabled for migration users. - This feature should be disabled for migration users.
- This feature should be enabled for new users and after rebuilds of migrated users. - This feature should be enabled for new users and after rebuilds of migrated users.
- Therefore, back into the implementation view, Ideally, the implementation should be such that data recovery can be achieved by immediately upgrading after replication. - Therefore, back into the implementation view, Ideally, the implementation should be such that data recovery can be achieved by immediately upgrading after replication.
## Outlined methods and implementation plans ## Outlined methods and implementation plans
### Abstract ### Abstract
To store and transfer only stable chunks independently and share them from multiple documents after stabilisation, new chunks, i.e. chunks that are considered non-stable, are modified to be stored in the document and transferred with the document. In this case, care should be taken not to exceed prerequisite (1). To store and transfer only stable chunks independently and share them from multiple documents after stabilisation, new chunks, i.e. chunks that are considered non-stable, are modified to be stored in the document and transferred with the document. In this case, care should be taken not to exceed prerequisite (1).
If this is achieved, the non-leaf document will not be transferred, and even if it is, the chunk will be stored in the document, so that the size can be reduced by the compaction. If this is achieved, the non-leaf document will not be transferred, and even if it is, the chunk will be stored in the document, so that the size can be reduced by the compaction.
@@ -40,11 +43,11 @@ Details are given below.
type EntryWithEden = { type EntryWithEden = {
eden: { eden: {
[key: DocumentID]: { [key: DocumentID]: {
data: string, data: string;
epoch: number, // The document revision which this chunk has been born. epoch: number; // The document revision which this chunk has been born.
} };
} };
} };
``` ```
2. The following configuration items are added: 2. The following configuration items are added:
Note: These configurations should be shared as `Tweaks value` between each client. Note: These configurations should be shared as `Tweaks value` between each client.
@@ -63,6 +66,7 @@ Details are given below.
5. In End-to-End Encryption, property `eden` of documents will also be encrypted. 5. In End-to-End Encryption, property `eden` of documents will also be encrypted.
### Note ### Note
- When this feature has been enabled, forward compatibility is temporarily lost. However, it is detected as missing chunks, and this data is not reflected in the storage in the old version. Therefore, no data loss will occur. - When this feature has been enabled, forward compatibility is temporarily lost. However, it is detected as missing chunks, and this data is not reflected in the storage in the old version. Therefore, no data loss will occur.
## Test strategy ## Test strategy
@@ -77,5 +81,26 @@ Details are given below.
- Indeed, we lack a fulfilled configuration table. Efforts will be made and, if they can be produced, this document will then be referenced. But not required while in the experimental or beta feature. - Indeed, we lack a fulfilled configuration table. Efforts will be made and, if they can be produced, this document will then be referenced. But not required while in the experimental or beta feature.
- However, this might be an essential feature. Further efforts are desired. - However, this might be an essential feature. Further efforts are desired.
## Results from actual operation
After implementing this feature, we have been using it for a while. The following results were obtained.
- Drawbacks were thought not to be a problem, but they were actually a problem:
- A document with `Eden` has a quite larger history compared to a document without `Eden`.
- Self-hosted LiveSync does not perform compaction aggressively, which results in the remote database becoming partially bloated.
- Compaction of the Remote Database (CouchDB) requires the same amount of free space as the size of the database. Therefore, it is not possible to perform compaction on a remote database if we reached to the maximum size of the database. It means that when we detect it, it is too late.
- We have mentioned that `We need compaction` in previous sections. However, but it was so hard to be determined whether the compaction is required or not, until the database is bloated. (Of course, it requires some time to compact the database, and, literally, some document loses its history. It is not a good idea to perform frequently and meaninglessly. We need manual decision, but indeed difficult to normal users).
### Consideration and Conclusion ### Consideration and Conclusion
To be described after implemented, tested, and, released.
This feature results in two aspects:
- For the users who are familiar with the CouchDB, this feature is a bit useful. They can watch and handle the database by themselves.
- For the users who are not familiar with the CouchDB, i.e., normal users, this feature is not so useful, either. They are not familiar with the database, and they do not know how to handle it. Therefore, they cannot decide whether the compaction is required or not.
Hence, this feature would be kept as an experimental feature, but it is not enabled by default. In addition to that, it is marked as deprecated. Detailed notice will be noisy for the users who are not familiar with the CouchDB. Details would be kept in this document, for the future.
It is not recommended to use this feature, unless the person who is familiar with the CouchDB and the database management.
Vorotamoroz has written this document. Bias: I am the first author of this plug-in, familiar with the CouchDB.
Research and development has been frozen on 2025-04-11. But, bugs will be fixed if they are found. Please feel free to report them.

View File

@@ -6,7 +6,7 @@ There are many settings in Self-hosted LiveSync. This document describes each se
| Icon | Description | | Icon | Description |
| :--: | ------------------------------------------------------------------ | | :--: | ------------------------------------------------------------------ |
| 💬 | [0. Update Information](#0-update-information) | | 💬 | [0. Change Log](#0-change-log) |
| 🧙‍♂️ | [1. Setup](#1-setup) | | 🧙‍♂️ | [1. Setup](#1-setup) |
| ⚙️ | [2. General Settings](#2-general-settings) | | ⚙️ | [2. General Settings](#2-general-settings) |
| 🛰️ | [3. Remote Configuration](#3-remote-configuration) | | 🛰️ | [3. Remote Configuration](#3-remote-configuration) |
@@ -19,7 +19,7 @@ There are many settings in Self-hosted LiveSync. This document describes each se
| 🩹 | [10. Patches (Edge Case)](#10-patches-edge-case) | | 🩹 | [10. Patches (Edge Case)](#10-patches-edge-case) |
| 🎛️ | [11. Maintenance](#11-maintenance) | | 🎛️ | [11. Maintenance](#11-maintenance) |
## 0. Update Information ## 0. Change Log
This pane shows version up information. You can check what has been changed in recent versions. This pane shows version up information. You can check what has been changed in recent versions.
@@ -31,21 +31,21 @@ This pane is used for setting up Self-hosted LiveSync. There are several options
Most preferred method to setup Self-hosted LiveSync. You can setup Self-hosted LiveSync with a few clicks. Most preferred method to setup Self-hosted LiveSync. You can setup Self-hosted LiveSync with a few clicks.
#### Use the copied setup URI #### Connect with Setup URI
Setup the Self-hosted LiveSync with the `setup URI` which is [copied from another device](#copy-current-settings-as-a-new-setup-uri) or the setup script. Setup the Self-hosted LiveSync with the `setup URI` which is [copied from another device](#copy-current-settings-as-a-new-setup-uri) or the setup script.
#### Minimal setup #### Manual setup
Step-by-step setup for Self-hosted LiveSync. You can setup Self-hosted LiveSync manually with Minimal setting items. Step-by-step setup for Self-hosted LiveSync. You can setup Self-hosted LiveSync manually with Minimal setting items.
#### Enable LiveSync on this device as the setup was completed manually #### Enable LiveSync
This button only appears when the setup was not completed. If you have completed the setup manually, you can enable LiveSync on this device by this button. This button only appears when the setup was not completed. If you have completed the setup manually, you can enable LiveSync on this device by this button.
### 2. To setup the other devices ### 2. To setup other devices
#### Copy current settings as a new setup URI #### Copy the current settings to a Setup URI
You can copy the current settings as a new setup URI. And this URI can be used to setup the other devices as [Use the copied setup URI](#use-the-copied-setup-uri). You can copy the current settings as a new setup URI. And this URI can be used to setup the other devices as [Use the copied setup URI](#use-the-copied-setup-uri).
@@ -71,7 +71,7 @@ Following panes will be shown when you enable this setting.
| 🔌 | [6. Customization sync (Advanced)](#6-customization-sync-advanced) | | 🔌 | [6. Customization sync (Advanced)](#6-customization-sync-advanced) |
| 🔧 | [8. Advanced (Advanced)](#8-advanced-advanced) | | 🔧 | [8. Advanced (Advanced)](#8-advanced-advanced) |
#### Enable power user features #### Enable poweruser features
Setting key: usePowerUserMode Setting key: usePowerUserMode
@@ -152,7 +152,7 @@ Setting key: notifyThresholdOfRemoteStorageSize
MB (0 to disable). We can get a notification when the estimated remote storage size exceeds this value. MB (0 to disable). We can get a notification when the estimated remote storage size exceeds this value.
### 3. Confidentiality ### 3. Privacy & Encryption
#### End-to-End Encryption #### End-to-End Encryption
@@ -184,7 +184,13 @@ This is an experimental feature and not recommended. If you enable this, the ite
--- ---
### 4. Minio,S3,R2 ### 4. Fetch settings
#### Fetch config from remote server
Fetch necessary settings from already configured remote server.
### 5. Minio,S3,R2
#### Endpoint URL #### Endpoint URL
@@ -209,15 +215,15 @@ Setting key: bucket
#### Use Custom HTTP Handler #### Use Custom HTTP Handler
Setting key: useCustomRequestHandler Setting key: useCustomRequestHandler
If your Object Storage could not configured accepting CORS, enable this. Enable this if your Object Storage doesn't support CORS
#### Test Connection #### Test Connection
#### Apply Settings #### Apply Settings
### 5. CouchDB ### 6. CouchDB
#### URI #### Server URI
Setting key: couchDB_URI Setting key: couchDB_URI
@@ -231,17 +237,17 @@ username
Setting key: couchDB_PASSWORD Setting key: couchDB_PASSWORD
password password
#### Database name #### Database Name
Setting key: couchDB_DBNAME Setting key: couchDB_DBNAME
#### Test Database Connection #### Test Database Connection
Open database connection. If the remote database is not found and you have the privilege to create a database, the database will be created. Open database connection. If the remote database is not found and you have permission to create a database, the database will be created.
#### Check and fix database configuration #### Validate Database Configuration
Check the database configuration, and fix if there are any problems. Checks and fixes any potential issues with the database config.
#### Apply Settings #### Apply Settings
@@ -254,7 +260,7 @@ Check the database configuration, and fix if there are any problems.
Setting key: preset Setting key: preset
Apply preset configuration Apply preset configuration
### 2. Synchronization Methods ### 2. Synchronization Method
#### Sync Mode #### Sync Mode
@@ -268,22 +274,22 @@ Interval (sec)
#### Sync on Save #### Sync on Save
Setting key: syncOnSave Setting key: syncOnSave
When you save a file, sync automatically Starts synchronisation when a file is saved.
#### Sync on Editor Save #### Sync on Editor Save
Setting key: syncOnEditorSave Setting key: syncOnEditorSave
When you save a file in the editor, sync automatically When you save a file in the editor, start a sync automatically
#### Sync on File Open #### Sync on File Open
Setting key: syncOnFileOpen Setting key: syncOnFileOpen
When you open a file, sync automatically Forces the file to be synced when opened.
#### Sync on Start #### Sync on Startup
Setting key: syncOnStart Setting key: syncOnStart
Start synchronization after launching Obsidian. Automatically Sync all files when opening Obsidian.
#### Sync after merging file #### Sync after merging file
@@ -312,34 +318,36 @@ Saving will be performed forcefully after this number of seconds.
#### Use the trash bin #### Use the trash bin
Setting key: trashInsteadDelete Setting key: trashInsteadDelete
Do not delete files that are deleted in remote, just move to trash. Move remotely deleted files to the trash, instead of deleting.
#### Keep empty folder #### Keep empty folder
Setting key: doNotDeleteFolder Setting key: doNotDeleteFolder
Normally, a folder is deleted when it becomes empty after a synchronization. Enabling this will prevent it from getting deleted Should we keep folders that don't have any files inside?
### 5. Conflict resolution (Advanced) ### 5. Conflict resolution (Advanced)
#### Always overwrite with a newer file (beta) #### (BETA) Always overwrite with a newer file
Setting key: resolveConflictsByNewerFile Setting key: resolveConflictsByNewerFile
(Def off) Resolve conflicts by newer files automatically. Testing only - Resolve file conflicts by syncing newer copies of the file, this can overwrite modified files. Be Warned.
#### Postpone resolution of inactive files #### Delay conflict resolution of inactive files
Setting key: checkConflictOnlyOnOpen Setting key: checkConflictOnlyOnOpen
Should we only check for conflicts when a file is opened?
#### Postpone manual resolution of inactive files #### Delay merge conflict prompt for inactive files.
Setting key: showMergeDialogOnlyOnActive Setting key: showMergeDialogOnlyOnActive
Should we prompt you about conflicting files when a file is opened?
### 6. Sync settings via markdown (Advanced) ### 6. Sync settings via markdown (Advanced)
#### Filename #### Filename
Setting key: settingSyncFile Setting key: settingSyncFile
If you set this, all settings are saved in a markdown file. You will be notified when new settings arrive. You can set different files by the platform. Save settings to a markdown file. You will be notified when new settings arrive. You can set different files by the platform.
#### Write credentials in the file #### Write credentials in the file
@@ -350,7 +358,7 @@ Setting key: writeCredentialsForSettingSync
Setting key: notifyAllSettingSyncFile Setting key: notifyAllSettingSyncFile
### 7. Hidden files (Advanced) ### 7. Hidden Files (Advanced)
#### Hidden file synchronization #### Hidden file synchronization
@@ -390,7 +398,7 @@ If this is set, changes to local files which are matched by the ignore files wil
#### Ignore files #### Ignore files
Setting key: ignoreFiles Setting key: ignoreFiles
We can use multiple ignore files, e.g.) `.gitignore, .dockerignore` Comma separated `.gitignore, .dockerignore`
### 2. Hidden Files (Advanced) ### 2. Hidden Files (Advanced)
@@ -451,7 +459,7 @@ Warning! This will have a serious impact on performance. And the logs will not b
#### Suspend file watching #### Suspend file watching
Setting key: suspendFileWatching Setting key: suspendFileWatching
Stop watching for file change. Stop watching for file changes.
#### Suspend database reflecting #### Suspend database reflecting
@@ -464,6 +472,10 @@ Stop reflecting database changes to storage files.
This will recreate chunks for all files. If there were missing chunks, this may fix the errors. This will recreate chunks for all files. If there were missing chunks, this may fix the errors.
#### Resolve All conflicted files by the newer one
Resolve all conflicted files by the newer one. Caution: This will overwrite the older one, and cannot resurrect the overwritten one.
#### Verify and repair all files #### Verify and repair all files
Compare the content of files between on local database and storage. If not matched, you will be asked which one you want to keep. Compare the content of files between on local database and storage. If not matched, you will be asked which one you want to keep.
@@ -520,16 +532,6 @@ Setting key: concurrencyOfReadChunksOnline
Setting key: minimumIntervalOfReadChunksOnline Setting key: minimumIntervalOfReadChunksOnline
#### Send chunks in bulk
Setting key: sendChunksBulk
If this enabled, all chunks will be sent in bulk. This is useful for the environment that has a high latency.
#### Maximum size of chunks to send in one request
Setting key: sendChunksBulkMaxSize
MB
## 9. Power users (Power User) ## 9. Power users (Power User)
### 1. Remote Database Tweak ### 1. Remote Database Tweak
@@ -563,7 +565,7 @@ Setting key: enableCompression
#### Batch size #### Batch size
Setting key: batch_size Setting key: batch_size
Number of change feed items to process at a time. Defaults to 50. Minimum is 2. Number of changes to sync at a time. Defaults to 50. Minimum is 2.
#### Batch limit #### Batch limit
@@ -586,6 +588,13 @@ Setting key: configPassphraseStore
Setting key: configPassphrase Setting key: configPassphrase
This passphrase will not be copied to another device. It will be set to `Default` until you configure it again. This passphrase will not be copied to another device. It will be set to `Default` until you configure it again.
### 4. Developer
#### Enable Developers' Debug Tools.
Setting key: enableDebugTools
Requires restart of Obsidian
## 10. Patches (Edge Case) ## 10. Patches (Edge Case)
### 1. Compatibility (Metadata) ### 1. Compatibility (Metadata)
@@ -601,15 +610,15 @@ Setting key: automaticallyDeleteMetadataOfDeletedFiles
### 2. Compatibility (Conflict Behaviour) ### 2. Compatibility (Conflict Behaviour)
#### Always resolve conflicts manually #### Always prompt merge conflicts
Setting key: disableMarkdownAutoMerge Setting key: disableMarkdownAutoMerge
If this switch is turned on, a merge dialog will be displayed, even if the sensible-merge is possible automatically. (Turn on to previous behavior) Should we prompt you for every single merge, even if we can safely merge automatcially?
#### Always reflect synchronized changes even if the note has a conflict #### Apply Latest Change if Conflicting
Setting key: writeDocumentsIfConflicted Setting key: writeDocumentsIfConflicted
Turn on to previous behavior Enable this option to automatically apply the most recent change to documents even when it conflicts
### 3. Compatibility (Database structure) ### 3. Compatibility (Database structure)
@@ -655,7 +664,7 @@ Setting key: doNotSuspendOnFetching
#### Keep empty folder #### Keep empty folder
Setting key: doNotDeleteFolder Setting key: doNotDeleteFolder
Normally, a folder is deleted when it becomes empty after a synchronization. Enabling this will prevent it from getting deleted Should we keep folders that don't have any files inside?
### 7. Edge case addressing (Processing) ### 7. Edge case addressing (Processing)
@@ -679,15 +688,15 @@ Setting key: disableCheckingConfigMismatch
### 1. Scram! ### 1. Scram!
#### Lock remote #### Lock Server
Lock remote to prevent synchronization with other devices. Lock the remote server to prevent synchronization with other devices.
#### Emergency restart #### Emergency restart
place the flag file to prevent all operation and restart. Disables all synchronization and restart.
### 2. Data-complementary Operations ### 2. Syncing
#### Resend #### Resend
@@ -719,9 +728,9 @@ Rebuild local and remote database with local files.
### 5. Rebuilding Operations (Remote Only) ### 5. Rebuilding Operations (Remote Only)
#### Perform compaction #### Perform cleanup
Compaction discards all of Eden in the non-latest revisions, reducing the storage usage. However, this operation requires the same free space on the remote as the current database. Reduces storage space by discarding all non-latest revisions. This requires the same amount of free space on the remote server and the local client.
#### Overwrite remote #### Overwrite remote
@@ -733,18 +742,18 @@ Initialise all journal history, On the next sync, every item will be received an
#### Purge all journal counter #### Purge all journal counter
Purge all sending and downloading cache. Purge all download/upload cache.
#### Make empty the bucket #### Fresh Start Wipe
Delete all data on the remote. Delete all data on the remote server.
### 6. Niches ### 6. Deprecated
#### (Obsolete) Clean up databases #### Run database cleanup
Delete unused chunks to shrink the database. However, this feature could be not effective in some cases. Please use rebuild everything instead. Attempt to shrink the database by deleting unused chunks. This may not work consistently. Use the 'Rebuild everything' under Total Overhaul.
### 7. Reset ### 7. Reset
#### Discard local database to reset or uninstall Self-hosted LiveSync #### Delete local database to reset or uninstall Self-hosted LiveSync

View File

@@ -31,7 +31,7 @@ export hostname=localhost:5984
export username=goojdasjdas #Please change as you like. export username=goojdasjdas #Please change as you like.
export password=kpkdasdosakpdsa #Please change as you like export password=kpkdasdosakpdsa #Please change as you like
# Prepare directories which saving data and configurations. # Prepare directories which save data and configurations.
mkdir couchdb-data mkdir couchdb-data
mkdir couchdb-etc mkdir couchdb-etc
``` ```
@@ -45,19 +45,36 @@ $ docker run --name couchdb-for-ols --rm -it -e COUCHDB_USER=${username} -e COUC
If your container has been exited, please check the permission of couchdb-data, and couchdb-etc. If your container has been exited, please check the permission of couchdb-data, and couchdb-etc.
Once CouchDB run, these directories will be owned by uid:`5984`. Please chown it for you again. Once CouchDB run, these directories will be owned by uid:`5984`. Please chown it for you again.
2. Enable it in background 2. Enable it in the background
``` ```
$ docker run --name couchdb-for-ols -d --restart always -e COUCHDB_USER=${username} -e COUCHDB_PASSWORD=${password} -v ${PWD}/couchdb-data:/opt/couchdb/data -v ${PWD}/couchdb-etc:/opt/couchdb/etc/local.d -p 5984:5984 couchdb $ docker run --name couchdb-for-ols -d --restart always -e COUCHDB_USER=${username} -e COUCHDB_PASSWORD=${password} -v ${PWD}/couchdb-data:/opt/couchdb/data -v ${PWD}/couchdb-etc:/opt/couchdb/etc/local.d -p 5984:5984 couchdb
``` ```
If you prefer a compose file instead of docker run, here is the equivalent below:
```
services:
couchdb:
image: couchdb:latest
container_name: couchdb-for-ols
user: 1000:1000
environment:
- COUCHDB_USER=${username}
- COUCHDB_PASSWORD=${password}
volumes:
- ./couchdb-data:/opt/couchdb/data
- ./couchdb-etc:/opt/couchdb/etc/local.d
ports:
- 5984:5984
restart: unless-stopped
```
### B. Install CouchDB directly ### B. Install CouchDB directly
Please refer the [official document](https://docs.couchdb.org/en/stable/install/index.html). However, we do not have to configure it fully. Just administrator needs to be configured. Please refer to the [official document](https://docs.couchdb.org/en/stable/install/index.html). However, we do not have to configure it fully. Just the administrator needs to be configured.
## 2. Run couchdb-init.sh for initialise ## 2. Run couchdb-init.sh for initialise
``` ```
$ curl -s https://raw.githubusercontent.com/vrtmrz/obsidian-livesync/main/utils/couchdb/couchdb-init.sh | bash curl -s https://raw.githubusercontent.com/vrtmrz/obsidian-livesync/main/utils/couchdb/couchdb-init.sh | bash
``` ```
If it results like following: If it results like the following:
``` ```
-- Configuring CouchDB by REST APIs... --> -- Configuring CouchDB by REST APIs... -->
{"ok":true} {"ok":true}
@@ -80,10 +97,15 @@ Your CouchDB has been initialised successfully. If you want this manually, pleas
- You can skip this instruction if you using only in intranet and only with desktop devices. - You can skip this instruction if you using only in intranet and only with desktop devices.
- For mobile devices, Obsidian requires a valid SSL certificate. Usually, it needs exposing the internet. - For mobile devices, Obsidian requires a valid SSL certificate. Usually, it needs exposing the internet.
Whatever solutions we can use. For the simplicity, following sample uses Cloudflare Zero Trust for testing. Whatever solutions we can use. For simplicity, the following sample uses Cloudflare Zero Trust for testing.
```
cloudflared tunnel --url http://localhost:5984
```
You will then get the following output:
``` ```
$ cloudflared tunnel --url http://localhost:5984
2024-02-14T10:35:25Z INF Thank you for trying Cloudflare Tunnel. Doing so, without a Cloudflare account, is a quick way to experiment and try it out. However, be aware that these account-less Tunnels have no uptime guarantee. If you intend to use Tunnels in production you should use a pre-created named tunnel by following: https://developers.cloudflare.com/cloudflare-one/connections/connect-apps 2024-02-14T10:35:25Z INF Thank you for trying Cloudflare Tunnel. Doing so, without a Cloudflare account, is a quick way to experiment and try it out. However, be aware that these account-less Tunnels have no uptime guarantee. If you intend to use Tunnels in production you should use a pre-created named tunnel by following: https://developers.cloudflare.com/cloudflare-one/connections/connect-apps
2024-02-14T10:35:25Z INF Requesting new quick Tunnel on trycloudflare.com... 2024-02-14T10:35:25Z INF Requesting new quick Tunnel on trycloudflare.com...
2024-02-14T10:35:26Z INF +--------------------------------------------------------------------------------------------+ 2024-02-14T10:35:26Z INF +--------------------------------------------------------------------------------------------+
@@ -94,19 +116,33 @@ $ cloudflared tunnel --url http://localhost:5984
: :
: :
``` ```
Now `https://tiles-photograph-routine-groundwater.trycloudflare.com` is our server. Make it into background once please. Now `https://tiles-photograph-routine-groundwater.trycloudflare.com` is our server. Make it into the background once, please.
## 4. Client Setup ## 4. Client Setup
> [!TIP] > [!TIP]
> Now manually configuration is not recommended for some reasons. However, if you want to do so, please use `Setup wizard`. The recommended extra configurations will be also set. > Now manual configuration is not recommended for some reasons. However, if you want to do so, please use `Setup wizard`. The recommended extra configurations will be also set.
### 1. Generate the setup URI on a desktop device or server ### 1. Generate the setup URI on a desktop device or server
```bash ```bash
$ export hostname=https://tiles-photograph-routine-groundwater.trycloudflare.com #Point to your vault export hostname=https://tiles-photograph-routine-groundwater.trycloudflare.com #Point to your vault
$ export database=obsidiannotes #Please change as you like export database=obsidiannotes #Please change as you like
$ export passphrase=dfsapkdjaskdjasdas #Please change as you like export passphrase=dfsapkdjaskdjasdas #Please change as you like
$ deno run -A https://raw.githubusercontent.com/vrtmrz/obsidian-livesync/main/utils/flyio/generate_setupuri.ts export username=johndoe
export password=abc123
deno run -A https://raw.githubusercontent.com/vrtmrz/obsidian-livesync/main/utils/flyio/generate_setupuri.ts
```
> [!TIP]
> What is the `passphrase`? Is it different from `uri_passphrase`?
> Yes, the `passphrase` we have exported now is for an End-to-End Encryption passphrase.
> And, `uri_passphrase` that used in the `generate_setupuri.ts` is a different one; for decrypting Set-up URI at using that.
> Why: I (vorotamoroz) think that the passphrase of the Setup-URI should be different from the E2EE passphrase to prevent exposure caused by operational errors or the possibility of evil in our environment. On top of that, I believe that it is desirable for the Setup-URI to be random. Setup-URI is inevitably long, so it goes through the clipboard. I think that its passphrase should not go through the same path, so it should essentially be typed manually.
> Hence, if we keep empty for uri_passphrase, generate_setupuri.ts generates an adjective-noun-randomnumber passphrase so that we can remember it without going through the clipboard.
You will then get the following output:
```bash
obsidian://setuplivesync?settings=%5B%22tm2DpsOE74nJAryprZO2M93wF%2Fvg.......4b26ed33230729%22%5D obsidian://setuplivesync?settings=%5B%22tm2DpsOE74nJAryprZO2M93wF%2Fvg.......4b26ed33230729%22%5D
Your passphrase of Setup-URI is: patient-haze Your passphrase of Setup-URI is: patient-haze

View File

@@ -1,10 +1,24 @@
# Terms used in this project # Notes on Terminology, Spelling, Vocabulary Conventions
## Terms ## Spelling and Vocabulary conventions
### Chunks 1. Almost all of the english words are written in British English. For example, "organisation" instead of "organization", "synchronisation" instead of "synchronization", etc. This convention originated from the author's personal preference but is now maintained for consistency.
<!-- TBW, sorry for the draft! -->
2. Idiomatic terms, such as used in HTML, CSS, and JavaScript, are usually be aligned with the language used in the technology. For example, "color" instead of "colour", "program" instead of "programme", etc. Especially, terms which are used for attributes, properties, and methods are notable.
<!-- Please feel free to write any terms that should be mentioned. And please make pull request. I would love to fill the rest. --> 3. We use `dialogue` in documentation for consistency. While `dialog` may appear in source code, particularly in class names, method names, and attributes (following technical conventions in No. 2), we consistently use `dialogue` for user-facing messages and general documentation text. This approach balances No. 1 with No. 2.
<!-- ### Chunks -->
4. Contractions are not used. For example, "do not" instead of "don't", "cannot" instead of "can't", etc. especially `'d`.
- We may encounter difficulties with tenses.
5. However, try using affirmative forms, `Discard` instead of `Do not keep`, `Continue` instead of `Do not stop`, etc.
- Some languages, such as Japanese, have a different meaning for `yes` and `no` between affirmative and negative questions.
## Terminology
- Self-hosted LiveSync
- This plug-in name. `Self-hosted` is one word.
- LiveSync
- Very confusing term.
- As shorten-form of `Self-hosted LiveSync`.
- As a name of synchronisation mode. This should be changed to `Continuos`, in contrast to `Periodic`.

View File

@@ -1,8 +1,16 @@
<!-- 2024-02-15 -->
# Tips and Troubleshooting # Tips and Troubleshooting
- [Tips and Troubleshooting](#tips-and-troubleshooting) - [Tips and Troubleshooting](#tips-and-troubleshooting)
- [Tips](#tips)
- [CORS avoidance](#cors-avoidance)
- [CORS configuration with reverse proxy](#cors-configuration-with-reverse-proxy)
- [Nginx](#nginx)
- [Nginx and subdirectory](#nginx-and-subdirectory)
- [Caddy](#caddy)
- [Caddy and subdirectory](#caddy-and-subdirectory)
- [Apache](#apache)
- [Show all setting panes](#show-all-setting-panes)
- [How to resolve `Tweaks Mismatched of Changed`](#how-to-resolve-tweaks-mismatched-of-changed)
- [Notable bugs and fixes](#notable-bugs-and-fixes) - [Notable bugs and fixes](#notable-bugs-and-fixes)
- [Binary files get bigger on iOS](#binary-files-get-bigger-on-ios) - [Binary files get bigger on iOS](#binary-files-get-bigger-on-ios)
- [Some setting name has been changed](#some-setting-name-has-been-changed) - [Some setting name has been changed](#some-setting-name-has-been-changed)
@@ -14,25 +22,145 @@
- [Why are the logs volatile and ephemeral?](#why-are-the-logs-volatile-and-ephemeral) - [Why are the logs volatile and ephemeral?](#why-are-the-logs-volatile-and-ephemeral)
- [Some network logs are not written into the file.](#some-network-logs-are-not-written-into-the-file) - [Some network logs are not written into the file.](#some-network-logs-are-not-written-into-the-file)
- [If a file were deleted or trimmed, the capacity of the database should be reduced, right?](#if-a-file-were-deleted-or-trimmed-the-capacity-of-the-database-should-be-reduced-right) - [If a file were deleted or trimmed, the capacity of the database should be reduced, right?](#if-a-file-were-deleted-or-trimmed-the-capacity-of-the-database-should-be-reduced-right)
- [How to launch the DevTools](#how-to-launch-the-devtools)
- [On Desktop Devices](#on-desktop-devices)
- [On Android](#on-android)
- [On iOS, iPadOS devices](#on-ios-ipados-devices)
- [How can I use the DevTools?](#how-can-i-use-the-devtools) - [How can I use the DevTools?](#how-can-i-use-the-devtools)
- [Checking the network log](#checking-the-network-log) - [Checking the network log](#checking-the-network-log)
- [Troubleshooting](#troubleshooting) - [Troubleshooting](#troubleshooting)
- [While using Cloudflare Tunnels, often Obsidian API fallback and `524` error occurs.](#while-using-cloudflare-tunnels-often-obsidian-api-fallback-and-524-error-occurs)
- [On the mobile device, cannot synchronise on the local network!](#on-the-mobile-device-cannot-synchronise-on-the-local-network) - [On the mobile device, cannot synchronise on the local network!](#on-the-mobile-device-cannot-synchronise-on-the-local-network)
- [I think that something bad happening on the vault...](#i-think-that-something-bad-happening-on-the-vault) - [I think that something bad happening on the vault...](#i-think-that-something-bad-happening-on-the-vault)
- [Tips](#tips)
- [How to resolve `Tweaks Mismatched of Changed`](#how-to-resolve-tweaks-mismatched-of-changed)
- [Old tips](#old-tips) - [Old tips](#old-tips)
<!-- - --> <!-- - -->
## Tips
### CORS avoidance
If we are unable to configure CORS properly for any reason (for example, if we cannot configure non-administered network devices), we may choose to ignore CORS.
To use the Obsidian API (also known as the Non-Native API) to bypass CORS, we can enable the toggle ``Use Request API to avoid `inevitable` CORS problem``.
<!-- Add **Long explanation of CORS** here for integrity -->
### CORS configuration with reverse proxy
- IMPORTANT: CouchDB handles CORS by itself. Do not process CORS on the reverse
proxy.
- Do not process `Option` requests on the reverse proxy!
- Make sure `host` and `X-Forwarded-For` headers are forwarded to the CouchDB.
- If you are using a subdirectory, make sure to handle it properly. More
detailed information is in the
[CouchDB documentation](https://docs.couchdb.org/en/stable/best-practices/reverse-proxies.html).
Minimal configurations are as follows:
#### Nginx
```nginx
location / {
proxy_pass http://localhost:5984;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
```
#### Nginx and subdirectory
```nginx
location /couchdb {
rewrite ^ $request_uri;
rewrite ^/couchdb/(.*) /$1 break;
proxy_pass http://localhost:5984$uri;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /_session {
proxy_pass http://localhost:5984/_session;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
```
#### Caddy
```caddyfile
domain.com {
reverse_proxy localhost:5984
}
```
#### Caddy and subdirectory
```caddyfile
domain.com {
reverse_proxy /couchdb/* localhost:5984
reverse_proxy /_session/* localhost:5984/_session
}
```
#### Apache
Sorry, Apache is not recommended for CouchDB. Omit the configuration from here.
Please refer to the
[Official documentation](https://docs.couchdb.org/en/stable/best-practices/reverse-proxies.html#reverse-proxying-with-apache-http-server).
### Show all setting panes
Full pane is not shown by default. To show all panes, please toggle all in
`🧙‍♂️ Wizard` -> `Enable extra and advanced features`.
For your information, the all panes are as follows:
![All Panes](all_toggles.png)
### How to resolve `Tweaks Mismatched of Changed`
(Since v0.23.17)
If you have changed some configurations or tweaks which should be unified
between the devices, you will be asked how to reflect (or not) other devices at
the next synchronisation. It also occurs on the device itself, where changes are
made, to prevent unexpected configuration changes from unwanted propagation.\
(We may thank this behaviour if we have synchronised or backed up and restored
Self-hosted LiveSync. At least, for me so).
Following dialogue will be shown: ![Dialogue](tweak_mismatch_dialogue.png)
- If we want to propagate the setting of the device, we should choose
`Update with mine`.
- On other devices, we should choose `Use configured` to accept and use the
configured configuration.
- `Dismiss` can postpone a decision. However, we cannot synchronise until we
have decided.
Rest assured that in most cases we can choose `Use configured`. (Unless you are
certain that you have not changed the configuration).
If we see it for the first time, it reflects the settings of the device that has
been synchronised with the remote for the first time since the upgrade.
Probably, we can accept that.
<!-- Add here -->
## Notable bugs and fixes ## Notable bugs and fixes
### Binary files get bigger on iOS ### Binary files get bigger on iOS
- Reported at: v0.20.x - Reported at: v0.20.x
- Fixed at: v0.21.2 (Fixed but not reviewed) - Fixed at: v0.21.2 (Fixed but not reviewed)
- Required action: larger files will not be fixed automatically, please perform `Verify and repair all files`. If our local database and storage are not matched, we will be asked to apply which one. - Required action: larger files will not be fixed automatically, please perform
`Verify and repair all files`. If our local database and storage are not
matched, we will be asked to apply which one.
### Some setting name has been changed ### Some setting name has been changed
- Fixed at: v0.22.6 - Fixed at: v0.22.6
| Previous name | New name | | Previous name | New name |
@@ -46,103 +174,191 @@
### Why `Use an old adapter for compatibility` is somehow enabled in my vault? ### Why `Use an old adapter for compatibility` is somehow enabled in my vault?
Because you are a compassionate and experienced user. Before v0.17.16, we used an old adapter for the local database. At that time, current default adapter has not been stable. Because you are a compassionate and experienced user. Before v0.17.16, we used
The new adapter has better performance and has a new feature like purging. Therefore, we should use new adapters and current default is so. an old adapter for the local database. At that time, current default adapter has
not been stable. The new adapter has better performance and has a new feature
like purging. Therefore, we should use new adapters and current default is so.
However, when switching from an old adapter to a new adapter, some converting or local database rebuilding is required, and it takes a few time. It was a long time ago now, but we once inconvenienced everyone in a hurry when we changed the format of our database. However, when switching from an old adapter to a new adapter, some converting or
For these reasons, this toggle is automatically on if we have upgraded from vault which using an old adapter. local database rebuilding is required, and it takes a few time. It was a long
time ago now, but we once inconvenienced everyone in a hurry when we changed the
format of our database. For these reasons, this toggle is automatically on if we
have upgraded from vault which using an old adapter.
When you rebuild everything or fetch from the remote again, you will be asked to switch this. When you rebuild everything or fetch from the remote again, you will be asked to
switch this.
Therefore, experienced users (especially those stable enough not to have to rebuild the database) may have this toggle enabled in their Vault. Therefore, experienced users (especially those stable enough not to have to
Please disable it when you have enough time. rebuild the database) may have this toggle enabled in their Vault. Please
disable it when you have enough time.
### ZIP (or any extensions) files were not synchronised. Why? ### ZIP (or any extensions) files were not synchronised. Why?
It depends on Obsidian detects. May toggling `Detect all extensions` of `File and links` (setting of Obsidian) will help us.
It depends on Obsidian detects. May toggling `Detect all extensions` of
`File and links` (setting of Obsidian) will help us.
### I hope to report the issue, but you said you needs `Report`. How to make it? ### I hope to report the issue, but you said you needs `Report`. How to make it?
We can copy the report to the clipboard, by pressing the `Make report` button on the `Hatch` pane.
![Screenshot](../images/hatch.png) We can copy the report to the clipboard, by pressing the `Make report` button on
the `Hatch` pane. ![Screenshot](../images/hatch.png)
### Where can I check the log? ### Where can I check the log?
We can launch the log pane by `Show log` on the command palette.
And if you have troubled something, please enable the `Verbose Log` on the `General Setting` pane.
However, the logs would not be kept so long and cleared when restarted. If you want to check the logs, please enable `Write logs into the file` temporarily. We can launch the log pane by `Show log` on the command palette. And if you have
troubled something, please enable the `Verbose Log` on the `General Setting`
pane.
However, the logs would not be kept so long and cleared when restarted. If you
want to check the logs, please enable `Write logs into the file` temporarily.
![ScreenShot](../images/write_logs_into_the_file.png) ![ScreenShot](../images/write_logs_into_the_file.png)
> [!IMPORTANT] > [!IMPORTANT]
>
> - Writing logs into the file will impact the performance. > - Writing logs into the file will impact the performance.
> - Please make sure that you have erased all your confidential information before reporting issue. > - Please make sure that you have erased all your confidential information
> before reporting issue.
### Why are the logs volatile and ephemeral? ### Why are the logs volatile and ephemeral?
To avoid unexpected exposure to our confidential things. To avoid unexpected exposure to our confidential things.
### Some network logs are not written into the file. ### Some network logs are not written into the file.
Especially the CORS error will be reported as a general error to the plug-in for security reasons. So we cannot detect and log it. We are only able to investigate them by [Checking the network log](#checking-the-network-log).
Especially the CORS error will be reported as a general error to the plug-in for
security reasons. So we cannot detect and log it. We are only able to
investigate them by [Checking the network log](#checking-the-network-log).
### If a file were deleted or trimmed, the capacity of the database should be reduced, right? ### If a file were deleted or trimmed, the capacity of the database should be reduced, right?
No, even though if files were deleted, chunks were not deleted.
Self-hosted LiveSync splits the files into multiple chunks and transfers only newly created. This behaviour enables us to less traffic. And, the chunks will be shared between the files to reduce the total usage of the database.
And one more thing, we can handle the conflicts on any device even though it has happened on other devices. This means that conflicts will happen in the past, after the time we have synchronised. Hence we cannot collect and delete the unused chunks even though if we are not currently referenced. No, even though if files were deleted, chunks were not deleted. Self-hosted
LiveSync splits the files into multiple chunks and transfers only newly created.
This behaviour enables us to less traffic. And, the chunks will be shared
between the files to reduce the total usage of the database.
And one more thing, we can handle the conflicts on any device even though it has
happened on other devices. This means that conflicts will happen in the past,
after the time we have synchronised. Hence we cannot collect and delete the
unused chunks even though if we are not currently referenced.
To shrink the database size, `Rebuild everything` only reliably and effectively.
But do not worry, if we have synchronised well. We have the actual and real
files. Only it takes a bit of time and traffics.
### How to launch the DevTools
#### On Desktop Devices
We can launch the DevTools by pressing `ctrl`+`shift`+`i` (`Command`+`shift`+`i` on Mac).
#### On Android
Please refer to [Remote debug Android devices](https://developer.chrome.com/docs/devtools/remote-debugging/).
Once the DevTools have been launched, everything operates the same as on a PC.
#### On iOS, iPadOS devices
If we have a Mac, we can inspect from Safari on the Mac. Please refer to [Inspecting iOS and iPadOS](https://developer.apple.com/documentation/safari-developer-tools/inspecting-ios).
To shrink the database size, `Rebuild everything` only reliably and effectively. But do not worry, if we have synchronised well. We have the actual and real files. Only it takes a bit of time and traffics.
### How can I use the DevTools? ### How can I use the DevTools?
#### Checking the network log #### Checking the network log
1. Open the network pane. 1. Open the network pane.
2. Find the requests marked in red. 2. Find the requests marked in red.\
![Errored](../images/devtools1.png) ![Errored](../images/devtools1.png)
3. Capture the `Headers`, `Payload`, and, `Response`. **Please be sure to keep important information confidential**. If the `Response` contains secrets, you can omitted that. 3. Capture the `Headers`, `Payload`, and, `Response`. **Please be sure to keep
Note: Headers contains a some credentials. **The path of the request URL, Remote Address, authority, and authorization must be concealed.** important information confidential**. If the `Response` contains secrets, you
![Concealed sample](../images/devtools2.png) can omitted that. Note: Headers contains a some credentials. **The path of
the request URL, Remote Address, authority, and authorization must be
concealed.**\
![Concealed sample](../images/devtools2.png)
## Troubleshooting ## Troubleshooting
<!-- Add here --> <!-- Add here -->
### While using Cloudflare Tunnels, often Obsidian API fallback and `524` error occurs.
A `524` error occurs when the request to the server is not completed within a
`specified time`. This is a timeout error from Cloudflare. From the reported
issue, it seems to be 100 seconds. (#627).
Therefore, this error returns from Cloudflare, not from the server. Hence, the
result contains no CORS field. It means that this response makes the Obsidian
API fallback.
However, even if the Obsidian API fallback occurs, the request is still not
completed within the `specified time`, 100 seconds.
To solve this issue, we need to configure the timeout settings.
Please enable the toggle in `💪 Power users` -> `CouchDB Connection Tweak` ->
`Use timeouts instead of heartbeats`.
### On the mobile device, cannot synchronise on the local network! ### On the mobile device, cannot synchronise on the local network!
Obsidian mobile is not able to connect to the non-secure end-point, such as starting with `http://`. Make sure your URI of CouchDB. Also not able to use a self-signed certificate.
Obsidian mobile is not able to connect to the non-secure end-point, such as
starting with `http://`. Make sure your URI of CouchDB. Also not able to use a
self-signed certificate.
### I think that something bad happening on the vault... ### I think that something bad happening on the vault...
Place `redflag.md` on top of the vault, and restart Obsidian. The most simple way is to create a new note and rename it to `redflag`. Of course, we can put it without Obsidian.
If there is `redflag.md`, Self-hosted LiveSync suspends all database and storage processes. Place `redflag.md` on top of the vault, and restart Obsidian. The most simple
way is to create a new note and rename it to `redflag`. Of course, we can put it
without Obsidian.
## Tips If there is `redflag.md`, Self-hosted LiveSync suspends all database and storage
processes.
### How to resolve `Tweaks Mismatched of Changed` There are some options to use `redflag.md`.
(Since v0.23.17) | Filename | Human-Friendly Name | Description |
| ------------- | ------------------- | ------------------------------------------------------------------------------------ |
| `redflag.md` | - | Suspends all processes. |
| `redflag2.md` | `flag_rebuild.md` | Suspends all processes, and rebuild both local and remote databases by local files. |
| `redflag3.md` | `flag_fetch.md` | Suspends all processes, discard the local database, and fetch from the remote again. |
If you have changed some configurations or tweaks which should be unified between the devices, you will be asked how to reflect (or not) other devices at the next synchronisation. It also occurs on the device itself, where changes are made, to prevent unexpected configuration changes from unwanted propagation. When fetching everything remotely or performing a rebuild, restarting Obsidian
(We may thank this behaviour if we have synchronised or backed up and restored Self-hosted LiveSync. At least, for me so). is performed once for safety reasons. At that time, Self-hosted LiveSync uses
these files to determine whether the process should be carried out. (The use of
Following dialogue will be shown: normal markdown files is a trick to externally force cancellation in the event
![Dialogue](tweak_mismatch_dialogue.png) of faults in the rebuild or fetch function itself, especially on mobile
devices). This mechanism is also used for set-up. And just for information,
- If we want to propagate the setting of the device, we should choose `Update with mine`. these files are also not subject to synchronisation.
- On other devices, we should choose `Use configured` to accept and use the configured configuration.
- `Dismiss` can postpone a decision. However, we cannot synchronise until we have decided.
Rest assured that in most cases we can choose `Use configured`. (Unless you are certain that you have not changed the configuration).
If we see it for the first time, it reflects the settings of the device that has been synchronised with the remote for the first time since the upgrade. Probably, we can accept that.
<!-- Add here -->
However, occasionally the deletion of files may fail. This should generally work
normally after restarting Obsidian. (As far as I can observe).
### Old tips ### Old tips
- Rarely, a file in the database could be corrupted. The plugin will not write to local storage when a file looks corrupted. If a local version of the file is on your device, the corruption could be fixed by editing the local file and synchronizing it. But if the file does not exist on any of your devices, then it can not be rescued. In this case, you can delete these items from the settings dialog.
- To stop the boot-up sequence (eg. for fixing problems on databases), you can put a `redflag.md` file (or directory) at the root of your vault.
Tip for iOS: a redflag directory can be created at the root of the vault using the File application.
- Also, with `redflag2.md` placed, we can automatically rebuild both the local and the remote databases during the boot-up sequence. With `redflag3.md`, we can discard only the local database and fetch from the remote again.
- Q: The database is growing, how can I shrink it down?
A: each of the docs is saved with their past 100 revisions for detecting and resolving conflicts. Picturing that one device has been offline for a while, and comes online again. The device has to compare its notes with the remotely saved ones. If there exists a historic revision in which the note used to be identical, it could be updated safely (like git fast-forward). Even if that is not in revision histories, we only have to check the differences after the revision that both devices commonly have. This is like git's conflict-resolving method. So, We have to make the database again like an enlarged git repo if you want to solve the root of the problem.
- And more technical Information is in the [Technical Information](tech_info.md)
- If you want to synchronize files without obsidian, you can use [filesystem-livesync](https://github.com/vrtmrz/filesystem-livesync).
- WebClipper is also available on Chrome Web Store:[obsidian-livesync-webclip](https://chrome.google.com/webstore/detail/obsidian-livesync-webclip/jfpaflmpckblieefkegjncjoceapakdf)
Repo is here: [obsidian-livesync-webclip](https://github.com/vrtmrz/obsidian-livesync-webclip). (Docs are a work in progress.) - Rarely, a file in the database could be corrupted. The plugin will not write
to local storage when a file looks corrupted. If a local version of the file
is on your device, the corruption could be fixed by editing the local file and
synchronizing it. But if the file does not exist on any of your devices, then
it can not be rescued. In this case, you can delete these items from the
settings dialog.
- To stop the boot-up sequence (eg. for fixing problems on databases), you can
put a `redflag.md` file (or directory) at the root of your vault. Tip for iOS:
a redflag directory can be created at the root of the vault using the File
application.
- Also, with `redflag2.md` placed, we can automatically rebuild both the local
and the remote databases during the boot-up sequence. With `redflag3.md`, we
can discard only the local database and fetch from the remote again.
- Q: The database is growing, how can I shrink it down? A: each of the docs is
saved with their past 100 revisions for detecting and resolving conflicts.
Picturing that one device has been offline for a while, and comes online
again. The device has to compare its notes with the remotely saved ones. If
there exists a historic revision in which the note used to be identical, it
could be updated safely (like git fast-forward). Even if that is not in
revision histories, we only have to check the differences after the revision
that both devices commonly have. This is like git's conflict-resolving method.
So, We have to make the database again like an enlarged git repo if you want
to solve the root of the problem.
- And more technical Information is in the [Technical Information](tech_info.md)
- If you want to synchronize files without obsidian, you can use
[filesystem-livesync](https://github.com/vrtmrz/filesystem-livesync).
- WebClipper is also available on Chrome Web
Store:[obsidian-livesync-webclip](https://chrome.google.com/webstore/detail/obsidian-livesync-webclip/jfpaflmpckblieefkegjncjoceapakdf)
Repo is here:
[obsidian-livesync-webclip](https://github.com/vrtmrz/obsidian-livesync-webclip).
(Docs are a work in progress.)

View File

@@ -4,12 +4,13 @@ import esbuild from "esbuild";
import process from "process"; import process from "process";
import builtins from "builtin-modules"; import builtins from "builtin-modules";
import sveltePlugin from "esbuild-svelte"; import sveltePlugin from "esbuild-svelte";
import sveltePreprocess from "svelte-preprocess"; import { sveltePreprocess } from "svelte-preprocess";
import fs from "node:fs"; import fs from "node:fs";
// import terser from "terser"; // import terser from "terser";
import { minify } from "terser"; import { minify } from "terser";
import inlineWorkerPlugin from "esbuild-plugin-inline-worker"; import inlineWorkerPlugin from "esbuild-plugin-inline-worker";
import { terserOption } from "./terser.config.mjs"; import { terserOption } from "./terser.config.mjs";
import path from "node:path";
const prod = process.argv[2] === "production"; const prod = process.argv[2] === "production";
const keepTest = true; //!prod; const keepTest = true; //!prod;
@@ -18,6 +19,46 @@ const manifestJson = JSON.parse(fs.readFileSync("./manifest.json") + "");
const packageJson = JSON.parse(fs.readFileSync("./package.json") + ""); const packageJson = JSON.parse(fs.readFileSync("./package.json") + "");
const updateInfo = JSON.stringify(fs.readFileSync("./updates.md") + ""); const updateInfo = JSON.stringify(fs.readFileSync("./updates.md") + "");
const moduleAliasPlugin = {
name: "module-alias",
setup(build) {
build.onResolve({ filter: /.(dev)(.ts|)$/ }, (args) => {
// console.log(args.path);
if (prod) {
let prodTs = args.path.replace(".dev", ".prod");
const statFile = prodTs.endsWith(".ts") ? prodTs : prodTs + ".ts";
const realPath = path.join(args.resolveDir, statFile);
console.log(`Checking ${statFile}`);
if (fs.existsSync(realPath)) {
console.log(`Replaced ${args.path} with ${prodTs}`);
return {
path: realPath,
namespace: "file",
};
}
}
return null;
});
build.onResolve({ filter: /.(platform)(.ts|)$/ }, (args) => {
// console.log(args.path);
if (prod) {
let prodTs = args.path.replace(".platform", ".obsidian");
const statFile = prodTs.endsWith(".ts") ? prodTs : prodTs + ".ts";
const realPath = path.join(args.resolveDir, statFile);
console.log(`Checking ${statFile}`);
if (fs.existsSync(realPath)) {
console.log(`Replaced ${args.path} with ${prodTs}`);
return {
path: realPath,
namespace: "file",
};
}
}
return null;
});
},
};
/** @type esbuild.Plugin[] */ /** @type esbuild.Plugin[] */
const plugins = [ const plugins = [
{ {
@@ -26,10 +67,22 @@ const plugins = [
let count = 0; let count = 0;
build.onEnd(async (result) => { build.onEnd(async (result) => {
if (count++ === 0) { if (count++ === 0) {
console.log("first build:", result); console.log("first build:");
if (prod) {
console.log("MetaFile:");
if (result.metafile) {
fs.writeFileSync("meta.json", JSON.stringify(result.metafile));
let text = await esbuild.analyzeMetafile(result.metafile, {
verbose: true,
});
// console.log(text);
}
}
} else { } else {
console.log("subsequent build:"); console.log("subsequent build:");
} }
const filename = `meta-${prod ? "prod" : "dev"}.json`;
await fs.promises.writeFile(filename, JSON.stringify(result.metafile, null, 2));
if (prod) { if (prod) {
console.log("Performing terser"); console.log("Performing terser");
const src = fs.readFileSync("./main_org.js").toString(); const src = fs.readFileSync("./main_org.js").toString();
@@ -47,7 +100,22 @@ const plugins = [
}, },
]; ];
const externals = ["obsidian", "electron", "crypto", "@codemirror/autocomplete", "@codemirror/collab", "@codemirror/commands", "@codemirror/language", "@codemirror/lint", "@codemirror/search", "@codemirror/state", "@codemirror/view", "@lezer/common", "@lezer/highlight", "@lezer/lr"]; const externals = [
"obsidian",
"electron",
"crypto",
"@codemirror/autocomplete",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
];
const context = await esbuild.context({ const context = await esbuild.context({
banner: { banner: {
js: "// Leave it all to terser", js: "// Leave it all to terser",
@@ -66,6 +134,7 @@ const context = await esbuild.context({
target: "es2018", target: "es2018",
logLevel: "info", logLevel: "info",
platform: "browser", platform: "browser",
metafile: true,
sourcemap: prod ? false : "inline", sourcemap: prod ? false : "inline",
treeShaking: false, treeShaking: false,
outfile: "main_org.js", outfile: "main_org.js",
@@ -77,6 +146,7 @@ const context = await esbuild.context({
dropLabels: prod && !keepTest ? ["TEST", "DEV"] : [], dropLabels: prod && !keepTest ? ["TEST", "DEV"] : [],
// keepNames: true, // keepNames: true,
plugins: [ plugins: [
moduleAliasPlugin,
inlineWorkerPlugin({ inlineWorkerPlugin({
external: externals, external: externals,
treeShaking: true, treeShaking: true,

99
eslint.config.mjs Normal file
View File

@@ -0,0 +1,99 @@
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import svelte from "eslint-plugin-svelte";
import _import from "eslint-plugin-import";
import { fixupPluginRules } from "@eslint/compat";
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
});
export default [
{
ignores: [
"**/node_modules/*",
"**/jest.config.js",
"src/lib/coverage",
"src/lib/browsertest",
"**/test.ts",
"**/tests.ts",
"**/**test.ts",
"**/**.test.ts",
"**/esbuild.*.mjs",
"**/terser.*.mjs",
"**/node_modules",
"**/build",
"**/.eslintrc.js.bak",
"src/lib/src/patches/pouchdb-utils",
"**/esbuild.config.mjs",
"**/rollup.config.js",
"modules/octagonal-wheels/rollup.config.js",
"modules/octagonal-wheels/dist/**/*",
"src/lib/test",
"src/lib/src/cli",
"**/main.js",
"src/lib/apps/webpeer/*"
],
},
...compat.extends(
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
),
{
plugins: {
"@typescript-eslint": typescriptEslint,
svelte,
import: fixupPluginRules(_import),
},
languageOptions: {
parser: tsParser,
ecmaVersion: 5,
sourceType: "module",
parserOptions: {
project: ["tsconfig.json"],
},
},
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
args: "none",
},
],
"no-unused-labels": "off",
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off",
"require-await": "error",
"@typescript-eslint/require-await": "warn",
"@typescript-eslint/no-misused-promises": "warn",
"@typescript-eslint/no-floating-promises": "warn",
"no-async-promise-executor": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"no-constant-condition": [
"error",
{
checkLoops: false,
},
],
},
},
];

View File

@@ -1,7 +1,7 @@
{ {
"id": "obsidian-livesync", "id": "obsidian-livesync",
"name": "Self-hosted LiveSync", "name": "Self-hosted LiveSync",
"version": "0.24.0.dev-rc7", "version": "0.25.0",
"minAppVersion": "0.9.12", "minAppVersion": "0.9.12",
"description": "Community implementation of self-hosted livesync. Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.", "description": "Community implementation of self-hosted livesync. Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.",
"author": "vorotamoroz", "author": "vorotamoroz",

View File

@@ -1,7 +1,7 @@
{ {
"id": "obsidian-livesync", "id": "obsidian-livesync",
"name": "Self-hosted LiveSync", "name": "Self-hosted LiveSync",
"version": "0.23.23", "version": "0.25.0",
"minAppVersion": "0.9.12", "minAppVersion": "0.9.12",
"description": "Community implementation of self-hosted livesync. Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.", "description": "Community implementation of self-hosted livesync. Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.",
"author": "vorotamoroz", "author": "vorotamoroz",

12284
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,28 @@
{ {
"name": "obsidian-livesync", "name": "obsidian-livesync",
"version": "0.24.0.dev-rc7", "version": "0.25.0",
"description": "Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.", "description": "Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.",
"main": "main.js", "main": "main.js",
"type": "module", "type": "module",
"scripts": { "scripts": {
"bakei18n": "npx tsx ./src/lib/_tools/bakei18n.ts",
"i18n:bakejson": "npx tsx ./src/lib/_tools/bakei18n.ts",
"i18n:yaml2json": "npx tsx ./src/lib/_tools/yaml2json.ts",
"i18n:json2yaml": "npx tsx ./src/lib/_tools/json2yaml.ts",
"prettyjson": "prettier --config ./.prettierrc ./src/lib/src/common/messagesJson/*.json --write --log-level error",
"postbakei18n": "prettier --config ./.prettierrc ./src/lib/src/common/messages/*.ts --write --log-level error",
"posti18n:yaml2json": "npm run prettyjson",
"predev": "npm run bakei18n",
"dev": "node esbuild.config.mjs", "dev": "node esbuild.config.mjs",
"prebuild": "npm run bakei18n",
"build": "node esbuild.config.mjs production", "build": "node esbuild.config.mjs production",
"buildDev": "node esbuild.config.mjs dev", "buildDev": "node esbuild.config.mjs dev",
"lint": "eslint src", "lint": "eslint src",
"svelte-check": "svelte-check --tsconfig ./tsconfig.json", "svelte-check": "svelte-check --tsconfig ./tsconfig.json",
"tsc-check": "tsc --noEmit", "tsc-check": "tsc --noEmit",
"pretty": "npm run prettyNoWrite -- --write --log-level error",
"prettyCheck": "npm run prettyNoWrite -- --check",
"prettyNoWrite": "prettier --config ./.prettierrc \"**/*.js\" \"**/*.ts\" \"**/*.json\" ",
"check": "npm run lint && npm run svelte-check && npm run tsc-check" "check": "npm run lint && npm run svelte-check && npm run tsc-check"
}, },
"keywords": [], "keywords": [],
@@ -18,9 +30,12 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@chialab/esbuild-plugin-worker": "^0.18.1", "@chialab/esbuild-plugin-worker": "^0.18.1",
"@eslint/compat": "^1.2.7",
"@eslint/eslintrc": "^3.3.0",
"@eslint/js": "^9.21.0",
"@tsconfig/svelte": "^5.0.4", "@tsconfig/svelte": "^5.0.4",
"@types/diff-match-patch": "^1.0.36", "@types/diff-match-patch": "^1.0.36",
"@types/node": "^22.5.4", "@types/node": "^22.13.8",
"@types/pouchdb": "^6.4.2", "@types/pouchdb": "^6.4.2",
"@types/pouchdb-adapter-http": "^6.1.6", "@types/pouchdb-adapter-http": "^6.1.6",
"@types/pouchdb-adapter-idb": "^6.1.7", "@types/pouchdb-adapter-idb": "^6.1.7",
@@ -29,17 +44,18 @@
"@types/pouchdb-mapreduce": "^6.1.10", "@types/pouchdb-mapreduce": "^6.1.10",
"@types/pouchdb-replication": "^6.4.7", "@types/pouchdb-replication": "^6.4.7",
"@types/transform-pouch": "^1.0.6", "@types/transform-pouch": "^1.0.6",
"@typescript-eslint/eslint-plugin": "^8.4.0", "@typescript-eslint/eslint-plugin": "8.25.0",
"@typescript-eslint/parser": "^8.4.0", "@typescript-eslint/parser": "8.25.0",
"builtin-modules": "^4.0.0", "builtin-modules": "5.0.0",
"esbuild": "0.23.1", "esbuild": "0.25.0",
"esbuild-svelte": "^0.8.1", "esbuild-svelte": "^0.9.0",
"eslint": "^8.57.0", "eslint": "^9.21.0",
"eslint-config-airbnb-base": "^15.0.0", "eslint-plugin-import": "^2.31.0",
"eslint-plugin-import": "^2.30.0", "eslint-plugin-svelte": "^3.0.2",
"events": "^3.3.0", "events": "^3.3.0",
"obsidian": "^1.6.6", "glob": "^11.0.3",
"postcss": "^8.4.45", "obsidian": "^1.8.7",
"postcss": "^8.5.3",
"postcss-load-config": "^6.0.1", "postcss-load-config": "^6.0.1",
"pouchdb-adapter-http": "^9.0.0", "pouchdb-adapter-http": "^9.0.0",
"pouchdb-adapter-idb": "^9.0.0", "pouchdb-adapter-idb": "^9.0.0",
@@ -51,26 +67,32 @@
"pouchdb-merge": "^9.0.0", "pouchdb-merge": "^9.0.0",
"pouchdb-replication": "^9.0.0", "pouchdb-replication": "^9.0.0",
"pouchdb-utils": "^9.0.0", "pouchdb-utils": "^9.0.0",
"svelte": "^4.2.19", "prettier": "3.5.2",
"svelte-preprocess": "^6.0.2", "svelte": "5.28.6",
"terser": "^5.31.6", "svelte-preprocess": "^6.0.3",
"terser": "^5.39.0",
"transform-pouch": "^2.0.0", "transform-pouch": "^2.0.0",
"tslib": "^2.7.0", "tslib": "^2.8.1",
"typescript": "^5.5.4" "tsx": "^4.19.4",
"typescript": "5.7.3",
"yaml": "^2.8.0"
}, },
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.645.0", "@aws-sdk/client-s3": "^3.808.0",
"@smithy/fetch-http-handler": "^3.2.4", "@smithy/fetch-http-handler": "^5.0.2",
"@smithy/protocol-http": "^4.1.0", "@smithy/md5-js": "^4.0.2",
"@smithy/querystring-builder": "^3.0.3", "@smithy/middleware-apply-body-checksum": "^4.1.0",
"@smithy/protocol-http": "^5.1.0",
"@smithy/querystring-builder": "^4.0.2",
"diff-match-patch": "^1.0.5", "diff-match-patch": "^1.0.5",
"esbuild-plugin-inline-worker": "^0.1.1", "esbuild-plugin-inline-worker": "^0.1.1",
"fflate": "^0.8.2", "fflate": "^0.8.2",
"idb": "^8.0.0", "idb": "^8.0.3",
"minimatch": "^10.0.1", "minimatch": "^10.0.1",
"octagonal-wheels": "^0.1.15", "octagonal-wheels": "^0.1.35",
"svelte-check": "^4.0.4", "qrcode-generator": "^1.4.4",
"xxhash-wasm": "0.4.2", "svelte-check": "^4.1.7",
"trystero": "^0.21.5",
"xxhash-wasm-102": "npm:xxhash-wasm@^1.0.2" "xxhash-wasm-102": "npm:xxhash-wasm@^1.0.2"
} }
} }

View File

@@ -0,0 +1,28 @@
import { ItemView } from "obsidian";
import { type mount, unmount } from "svelte";
export abstract class SvelteItemView extends ItemView {
abstract instantiateComponent(target: HTMLElement): ReturnType<typeof mount> | Promise<ReturnType<typeof mount>>;
component?: ReturnType<typeof mount>;
async onOpen() {
await super.onOpen();
this.contentEl.empty();
await this._dismountComponent();
this.component = await this.instantiateComponent(this.contentEl);
return;
}
async _dismountComponent() {
if (this.component) {
await unmount(this.component);
this.component = undefined;
}
}
async onClose() {
await super.onClose();
if (this.component) {
await unmount(this.component);
this.component = undefined;
}
return;
}
}

View File

@@ -1,29 +1,45 @@
export const EVENT_LAYOUT_READY = "layout-ready"; import { eventHub } from "../lib/src/hub/hub";
import type ObsidianLiveSyncPlugin from "../main";
export const EVENT_PLUGIN_LOADED = "plugin-loaded"; export const EVENT_PLUGIN_LOADED = "plugin-loaded";
export const EVENT_PLUGIN_UNLOADED = "plugin-unloaded"; export const EVENT_PLUGIN_UNLOADED = "plugin-unloaded";
export const EVENT_SETTING_SAVED = "setting-saved";
export const EVENT_FILE_RENAMED = "file-renamed";
export const EVENT_FILE_SAVED = "file-saved"; export const EVENT_FILE_SAVED = "file-saved";
export const EVENT_LEAF_ACTIVE_CHANGED = "leaf-active-changed"; export const EVENT_LEAF_ACTIVE_CHANGED = "leaf-active-changed";
export const EVENT_LOG_ADDED = "log-added";
export const EVENT_REQUEST_OPEN_SETTINGS = "request-open-settings"; export const EVENT_REQUEST_OPEN_SETTINGS = "request-open-settings";
export const EVENT_REQUEST_OPEN_SETTING_WIZARD = "request-open-setting-wizard"; export const EVENT_REQUEST_OPEN_SETTING_WIZARD = "request-open-setting-wizard";
export const EVENT_REQUEST_OPEN_SETUP_URI = "request-open-setup-uri"; export const EVENT_REQUEST_OPEN_SETUP_URI = "request-open-setup-uri";
export const EVENT_REQUEST_COPY_SETUP_URI = "request-copy-setup-uri"; export const EVENT_REQUEST_COPY_SETUP_URI = "request-copy-setup-uri";
export const EVENT_REQUEST_SHOW_SETUP_QR = "request-show-setup-qr";
export const EVENT_REQUEST_SHOW_HISTORY = "show-history";
export const EVENT_REQUEST_RELOAD_SETTING_TAB = "reload-setting-tab"; export const EVENT_REQUEST_RELOAD_SETTING_TAB = "reload-setting-tab";
export const EVENT_REQUEST_OPEN_PLUGIN_SYNC_DIALOG = "request-open-plugin-sync-dialog"; export const EVENT_REQUEST_OPEN_PLUGIN_SYNC_DIALOG = "request-open-plugin-sync-dialog";
export const EVENT_REQUEST_OPEN_P2P = "request-open-p2p";
export const EVENT_REQUEST_CLOSE_P2P = "request-close-p2p";
export const EVENT_REQUEST_RUN_DOCTOR = "request-run-doctor";
// export const EVENT_FILE_CHANGED = "file-changed"; // export const EVENT_FILE_CHANGED = "file-changed";
import { eventHub } from "../lib/src/hub/hub"; declare global {
// TODO: Add overloads for the emit method to allow for type checking interface LSEvents {
[EVENT_PLUGIN_LOADED]: ObsidianLiveSyncPlugin;
[EVENT_PLUGIN_UNLOADED]: undefined;
[EVENT_REQUEST_OPEN_PLUGIN_SYNC_DIALOG]: undefined;
[EVENT_REQUEST_OPEN_SETTINGS]: undefined;
[EVENT_REQUEST_OPEN_SETTING_WIZARD]: undefined;
[EVENT_REQUEST_RELOAD_SETTING_TAB]: undefined;
[EVENT_LEAF_ACTIVE_CHANGED]: undefined;
[EVENT_REQUEST_CLOSE_P2P]: undefined;
[EVENT_REQUEST_OPEN_P2P]: undefined;
[EVENT_REQUEST_OPEN_SETUP_URI]: undefined;
[EVENT_REQUEST_COPY_SETUP_URI]: undefined;
[EVENT_REQUEST_SHOW_SETUP_QR]: undefined;
[EVENT_REQUEST_RUN_DOCTOR]: string;
}
}
export * from "../lib/src/events/coreEvents.ts";
export { eventHub }; export { eventHub };

View File

@@ -0,0 +1,12 @@
import type { TFile } from "../deps";
import type { FilePathWithPrefix, LoadedEntry } from "../lib/src/common/types";
export const EVENT_REQUEST_SHOW_HISTORY = "show-history";
declare global {
interface LSEvents {
[EVENT_REQUEST_SHOW_HISTORY]:
| { file: TFile; fileOnDB: LoadedEntry }
| { file: FilePathWithPrefix; fileOnDB: LoadedEntry };
}
}

View File

@@ -1,5 +1,11 @@
import { type PluginManifest, TFile } from "../deps.ts"; import { type PluginManifest, TFile } from "../deps.ts";
import { type DatabaseEntry, type EntryBody, type FilePath, type UXFileInfoStub, type UXInternalFileInfoStub } from "../lib/src/common/types.ts"; import {
type DatabaseEntry,
type EntryBody,
type FilePath,
type UXFileInfoStub,
type UXInternalFileInfoStub,
} from "../lib/src/common/types.ts";
export interface PluginDataEntry extends DatabaseEntry { export interface PluginDataEntry extends DatabaseEntry {
deviceVaultName: string; deviceVaultName: string;
@@ -55,15 +61,15 @@ export type FileEventArgs = {
cache?: CacheData; cache?: CacheData;
oldPath?: string; oldPath?: string;
ctx?: any; ctx?: any;
} };
export type FileEventItem = { export type FileEventItem = {
type: FileEventType, type: FileEventType;
args: FileEventArgs, args: FileEventArgs;
key: string, key: string;
skipBatchWait?: boolean, skipBatchWait?: boolean;
cancelled?: boolean, cancelled?: boolean;
batched?: boolean batched?: boolean;
} };
// Hidden items (Now means `chunk`) // Hidden items (Now means `chunk`)
export const CHeader = "h:"; export const CHeader = "h:";
@@ -82,4 +88,4 @@ export const ICXHeader = "ix:";
export const FileWatchEventQueueMax = 10; export const FileWatchEventQueueMax = 10;
export const configURIBase = "obsidian://setuplivesync?settings="; export const configURIBase = "obsidian://setuplivesync?settings=";
export const configURIBaseQR = "obsidian://setuplivesync?settingsQR=";

View File

@@ -1,20 +1,49 @@
import { normalizePath, Platform, TAbstractFile, type RequestUrlParam, requestUrl } from "../deps.ts"; import { normalizePath, Platform, TAbstractFile, type RequestUrlParam, requestUrl } from "../deps.ts";
import { path2id_base, id2path_base, isValidFilenameInLinux, isValidFilenameInDarwin, isValidFilenameInWidows, isValidFilenameInAndroid, stripAllPrefixes } from "../lib/src/string_and_binary/path.ts"; import {
path2id_base,
id2path_base,
isValidFilenameInLinux,
isValidFilenameInDarwin,
isValidFilenameInWidows,
isValidFilenameInAndroid,
stripAllPrefixes,
} from "../lib/src/string_and_binary/path.ts";
import { Logger } from "../lib/src/common/logger.ts"; import { Logger } from "../lib/src/common/logger.ts";
import { LOG_LEVEL_VERBOSE, type AnyEntry, type DocumentID, type EntryHasPath, type FilePath, type FilePathWithPrefix, type UXFileInfo, type UXFileInfoStub } from "../lib/src/common/types.ts"; import {
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
type AnyEntry,
type CouchDBCredentials,
type DocumentID,
type EntryHasPath,
type FilePath,
type FilePathWithPrefix,
type UXFileInfo,
type UXFileInfoStub,
} from "../lib/src/common/types.ts";
import { CHeader, ICHeader, ICHeaderLength, ICXHeader, PSCHeader } from "./types.ts"; import { CHeader, ICHeader, ICHeaderLength, ICXHeader, PSCHeader } from "./types.ts";
import type ObsidianLiveSyncPlugin from "../main.ts"; import type ObsidianLiveSyncPlugin from "../main.ts";
import { writeString } from "../lib/src/string_and_binary/convert.ts"; import { writeString } from "../lib/src/string_and_binary/convert.ts";
import { fireAndForget } from "../lib/src/common/utils.ts"; import { fireAndForget } from "../lib/src/common/utils.ts";
import { sameChangePairs } from "./stores.ts"; import { sameChangePairs } from "./stores.ts";
import type { KeyValueDatabase } from "./KeyValueDB.ts";
import { scheduleTask } from "octagonal-wheels/concurrency/task";
import { EVENT_PLUGIN_UNLOADED, eventHub } from "./events.ts";
import { promiseWithResolver, type PromiseWithResolvers } from "octagonal-wheels/promises";
import { AuthorizationHeaderGenerator } from "../lib/src/replication/httplib.ts";
export { scheduleTask, setPeriodicTask, cancelTask, cancelAllTasks, cancelPeriodicTask, cancelAllPeriodicTask, } from "../lib/src/concurrency/task.ts"; export { scheduleTask, cancelTask, cancelAllTasks } from "../lib/src/concurrency/task.ts";
// For backward compatibility, using the path for determining id. // For backward compatibility, using the path for determining id.
// Only CouchDB unacceptable ID (that starts with an underscore) has been prefixed with "/". // Only CouchDB unacceptable ID (that starts with an underscore) has been prefixed with "/".
// The first slash will be deleted when the path is normalized. // The first slash will be deleted when the path is normalized.
export async function path2id(filename: FilePathWithPrefix | FilePath, obfuscatePassphrase: string | false, caseInsensitive: boolean): Promise<DocumentID> { export async function path2id(
filename: FilePathWithPrefix | FilePath,
obfuscatePassphrase: string | false,
caseInsensitive: boolean
): Promise<DocumentID> {
const temp = filename.split(":"); const temp = filename.split(":");
const path = temp.pop(); const path = temp.pop();
const normalizedPath = normalizePath(path as FilePath); const normalizedPath = normalizePath(path as FilePath);
@@ -35,7 +64,6 @@ export function id2path(id: DocumentID, entry?: EntryHasPath): FilePathWithPrefi
} }
export function getPath(entry: AnyEntry) { export function getPath(entry: AnyEntry) {
return id2path(entry._id, entry); return id2path(entry._id, entry);
} }
export function getPathWithoutPrefix(entry: AnyEntry) { export function getPathWithoutPrefix(entry: AnyEntry) {
const f = getPath(entry); const f = getPath(entry);
@@ -45,11 +73,26 @@ export function getPathWithoutPrefix(entry: AnyEntry) {
export function getPathFromTFile(file: TAbstractFile) { export function getPathFromTFile(file: TAbstractFile) {
return file.path as FilePath; return file.path as FilePath;
} }
export function isInternalFile(file: UXFileInfoStub | string | FilePathWithPrefix) {
if (typeof file == "string") return file.startsWith(ICHeader);
if (file.isInternal) return true;
return false;
}
export function getPathFromUXFileInfo(file: UXFileInfoStub | string | FilePathWithPrefix) { export function getPathFromUXFileInfo(file: UXFileInfoStub | string | FilePathWithPrefix) {
if (typeof file == "string") return file as FilePathWithPrefix; if (typeof file == "string") return file as FilePathWithPrefix;
return file.path; return file.path;
} }
export function getStoragePathFromUXFileInfo(file: UXFileInfoStub | string | FilePathWithPrefix) {
if (typeof file == "string") return stripAllPrefixes(file as FilePathWithPrefix);
return stripAllPrefixes(file.path);
}
export function getDatabasePathFromUXFileInfo(file: UXFileInfoStub | string | FilePathWithPrefix) {
if (typeof file == "string" && file.startsWith(ICXHeader)) return file as FilePathWithPrefix;
const prefix = isInternalFile(file) ? ICHeader : "";
if (typeof file == "string") return (prefix + stripAllPrefixes(file as FilePathWithPrefix)) as FilePathWithPrefix;
return (prefix + stripAllPrefixes(file.path)) as FilePathWithPrefix;
}
const memos: { [key: string]: any } = {}; const memos: { [key: string]: any } = {};
export function memoObject<T>(key: string, obj: T): T { export function memoObject<T>(key: string, obj: T): T {
@@ -59,7 +102,7 @@ export function memoObject<T>(key: string, obj: T): T {
export async function memoIfNotExist<T>(key: string, func: () => T | Promise<T>): Promise<T> { export async function memoIfNotExist<T>(key: string, func: () => T | Promise<T>): Promise<T> {
if (!(key in memos)) { if (!(key in memos)) {
const w = func(); const w = func();
const v = w instanceof Promise ? (await w) : w; const v = w instanceof Promise ? await w : w;
memos[key] = v; memos[key] = v;
} }
return memos[key] as T; return memos[key] as T;
@@ -75,7 +118,6 @@ export function disposeMemoObject(key: string) {
delete memos[key]; delete memos[key];
} }
export function isValidPath(filename: string) { export function isValidPath(filename: string) {
if (Platform.isDesktop) { if (Platform.isDesktop) {
// if(Platform.isMacOS) return isValidFilenameInDarwin(filename); // if(Platform.isMacOS) return isValidFilenameInDarwin(filename);
@@ -94,7 +136,6 @@ export function trimPrefix(target: string, prefix: string) {
return target.startsWith(prefix) ? target.substring(prefix.length) : target; return target.startsWith(prefix) ? target.substring(prefix.length) : target;
} }
/** /**
* returns is internal chunk of file * returns is internal chunk of file
* @param id ID * @param id ID
@@ -107,7 +148,7 @@ export function stripInternalMetadataPrefix<T extends FilePath | FilePathWithPre
return id.substring(ICHeaderLength) as T; return id.substring(ICHeaderLength) as T;
} }
export function id2InternalMetadataId(id: DocumentID): DocumentID { export function id2InternalMetadataId(id: DocumentID): DocumentID {
return ICHeader + id as DocumentID; return (ICHeader + id) as DocumentID;
} }
// const CHeaderLength = CHeader.length; // const CHeaderLength = CHeader.length;
@@ -122,14 +163,16 @@ export function isCustomisationSyncMetadata(str: string): boolean {
return str.startsWith(ICXHeader); return str.startsWith(ICXHeader);
} }
export class PeriodicProcessor { export class PeriodicProcessor {
_process: () => Promise<any>; _process: () => Promise<any>;
_timer?: number; _timer?: number = undefined;
_plugin: ObsidianLiveSyncPlugin; _plugin: ObsidianLiveSyncPlugin;
constructor(plugin: ObsidianLiveSyncPlugin, process: () => Promise<any>) { constructor(plugin: ObsidianLiveSyncPlugin, process: () => Promise<any>) {
this._plugin = plugin; this._plugin = plugin;
this._process = process; this._process = process;
eventHub.onceEvent(EVENT_PLUGIN_UNLOADED, () => {
this.disable();
});
} }
async process() { async process() {
try { try {
@@ -141,12 +184,16 @@ export class PeriodicProcessor {
enable(interval: number) { enable(interval: number) {
this.disable(); this.disable();
if (interval == 0) return; if (interval == 0) return;
this._timer = window.setInterval(() => fireAndForget(async () => { this._timer = window.setInterval(
await this.process(); () =>
if (this._plugin._unloaded) { fireAndForget(async () => {
this.disable(); await this.process();
} if (this._plugin.$$isUnloaded()) {
}), interval); this.disable();
}
}),
interval
);
this._plugin.registerInterval(this._timer); this._plugin.registerInterval(this._timer);
} }
disable() { disable() {
@@ -157,11 +204,21 @@ export class PeriodicProcessor {
} }
} }
export const _requestToCouchDBFetch = async (baseUri: string, username: string, password: string, path?: string, body?: string | any, method?: string) => { export const _requestToCouchDBFetch = async (
baseUri: string,
username: string,
password: string,
path?: string,
body?: string | any,
method?: string
) => {
const utf8str = String.fromCharCode.apply(null, [...writeString(`${username}:${password}`)]); const utf8str = String.fromCharCode.apply(null, [...writeString(`${username}:${password}`)]);
const encoded = window.btoa(utf8str); const encoded = window.btoa(utf8str);
const authHeader = "Basic " + encoded; const authHeader = "Basic " + encoded;
const transformedHeaders: Record<string, string> = { authorization: authHeader, "content-type": "application/json" }; const transformedHeaders: Record<string, string> = {
authorization: authHeader,
"content-type": "application/json",
};
const uri = `${baseUri}/${path}`; const uri = `${baseUri}/${path}`;
const requestParam = { const requestParam = {
url: uri, url: uri,
@@ -171,13 +228,21 @@ export const _requestToCouchDBFetch = async (baseUri: string, username: string,
body: JSON.stringify(body), body: JSON.stringify(body),
}; };
return await fetch(uri, requestParam); return await fetch(uri, requestParam);
} };
export const _requestToCouchDB = async (baseUri: string, username: string, password: string, origin: string, path?: string, body?: any, method?: string) => { export const _requestToCouchDB = async (
const utf8str = String.fromCharCode.apply(null, [...writeString(`${username}:${password}`)]); baseUri: string,
const encoded = window.btoa(utf8str); credentials: CouchDBCredentials,
const authHeader = "Basic " + encoded; origin: string,
const transformedHeaders: Record<string, string> = { authorization: authHeader, origin: origin }; path?: string,
body?: any,
method?: string,
customHeaders?: Record<string, string>
) => {
// Create each time to avoid caching.
const authHeaderGen = new AuthorizationHeaderGenerator();
const authHeader = await authHeaderGen.getAuthorizationHeader(credentials);
const transformedHeaders: Record<string, string> = { authorization: authHeader, origin: origin, ...customHeaders };
const uri = `${baseUri}/${path}`; const uri = `${baseUri}/${path}`;
const requestParam: RequestUrlParam = { const requestParam: RequestUrlParam = {
url: uri, url: uri,
@@ -187,23 +252,57 @@ export const _requestToCouchDB = async (baseUri: string, username: string, passw
body: body ? JSON.stringify(body) : undefined, body: body ? JSON.stringify(body) : undefined,
}; };
return await requestUrl(requestParam); return await requestUrl(requestParam);
} };
export const requestToCouchDB = async (baseUri: string, username: string, password: string, origin: string = "", key?: string, body?: string, method?: string) => { /**
* @deprecated Use requestToCouchDBWithCredentials instead.
*/
export const requestToCouchDB = async (
baseUri: string,
username: string,
password: string,
origin: string = "",
key?: string,
body?: string,
method?: string,
customHeaders?: Record<string, string>
) => {
const uri = `_node/_local/_config${key ? "/" + key : ""}`; const uri = `_node/_local/_config${key ? "/" + key : ""}`;
return await _requestToCouchDB(baseUri, username, password, origin, uri, body, method); return await _requestToCouchDB(
baseUri,
{ username, password, type: "basic" },
origin,
uri,
body,
method,
customHeaders
);
}; };
export function requestToCouchDBWithCredentials(
baseUri: string,
credentials: CouchDBCredentials,
origin: string = "",
key?: string,
body?: string,
method?: string,
customHeaders?: Record<string, string>
) {
const uri = `_node/_local/_config${key ? "/" + key : ""}`;
return _requestToCouchDB(baseUri, credentials, origin, uri, body, method, customHeaders);
}
export const BASE_IS_NEW = Symbol("base"); export const BASE_IS_NEW = Symbol("base");
export const TARGET_IS_NEW = Symbol("target"); export const TARGET_IS_NEW = Symbol("target");
export const EVEN = Symbol("even"); export const EVEN = Symbol("even");
// Why 2000? : ZIP FILE Does not have enough resolution. // Why 2000? : ZIP FILE Does not have enough resolution.
const resolution = 2000; const resolution = 2000;
export function compareMTime(baseMTime: number, targetMTime: number): typeof BASE_IS_NEW | typeof TARGET_IS_NEW | typeof EVEN { export function compareMTime(
const truncatedBaseMTime = (~~(baseMTime / resolution)) * resolution; baseMTime: number,
const truncatedTargetMTime = (~~(targetMTime / resolution)) * resolution; targetMTime: number
): typeof BASE_IS_NEW | typeof TARGET_IS_NEW | typeof EVEN {
const truncatedBaseMTime = ~~(baseMTime / resolution) * resolution;
const truncatedTargetMTime = ~~(targetMTime / resolution) * resolution;
// Logger(`Resolution MTime ${truncatedBaseMTime} and ${truncatedTargetMTime} `, LOG_LEVEL_VERBOSE); // Logger(`Resolution MTime ${truncatedBaseMTime} and ${truncatedTargetMTime} `, LOG_LEVEL_VERBOSE);
if (truncatedBaseMTime == truncatedTargetMTime) return EVEN; if (truncatedBaseMTime == truncatedTargetMTime) return EVEN;
if (truncatedBaseMTime > truncatedTargetMTime) return BASE_IS_NEW; if (truncatedBaseMTime > truncatedTargetMTime) return BASE_IS_NEW;
@@ -211,30 +310,43 @@ export function compareMTime(baseMTime: number, targetMTime: number): typeof BAS
throw new Error("Unexpected error"); throw new Error("Unexpected error");
} }
function getKey(file: AnyEntry | string | UXFileInfoStub) {
const key = typeof file == "string" ? file : stripAllPrefixes(file.path);
return key;
}
export function markChangesAreSame(file: AnyEntry | string | UXFileInfoStub, mtime1: number, mtime2: number) { export function markChangesAreSame(file: AnyEntry | string | UXFileInfoStub, mtime1: number, mtime2: number) {
if (mtime1 === mtime2) return true; if (mtime1 === mtime2) return true;
const key = typeof file == "string" ? file : "_id" in file ? file._id : file.path; const key = getKey(file);
const pairs = sameChangePairs.get(key, []) || []; const pairs = sameChangePairs.get(key, []) || [];
if (pairs.some(e => e == mtime1 || e == mtime2)) { if (pairs.some((e) => e == mtime1 || e == mtime2)) {
sameChangePairs.set(key, [...new Set([...pairs, mtime1, mtime2])]); sameChangePairs.set(key, [...new Set([...pairs, mtime1, mtime2])]);
} else { } else {
sameChangePairs.set(key, [mtime1, mtime2]); sameChangePairs.set(key, [mtime1, mtime2]);
} }
} }
export function unmarkChanges(file: AnyEntry | string | UXFileInfoStub) {
const key = getKey(file);
sameChangePairs.delete(key);
}
export function isMarkedAsSameChanges(file: UXFileInfoStub | AnyEntry | string, mtimes: number[]) { export function isMarkedAsSameChanges(file: UXFileInfoStub | AnyEntry | string, mtimes: number[]) {
const key = typeof file == "string" ? file : "_id" in file ? file._id : file.path; const key = getKey(file);
const pairs = sameChangePairs.get(key, []) || []; const pairs = sameChangePairs.get(key, []) || [];
if (mtimes.every(e => pairs.indexOf(e) !== -1)) { if (mtimes.every((e) => pairs.indexOf(e) !== -1)) {
return EVEN; return EVEN;
} }
} }
export function compareFileFreshness(baseFile: UXFileInfoStub | AnyEntry | undefined, checkTarget: UXFileInfo | AnyEntry | undefined): typeof BASE_IS_NEW | typeof TARGET_IS_NEW | typeof EVEN { export function compareFileFreshness(
baseFile: UXFileInfoStub | AnyEntry | undefined,
checkTarget: UXFileInfo | AnyEntry | undefined
): typeof BASE_IS_NEW | typeof TARGET_IS_NEW | typeof EVEN {
if (baseFile === undefined && checkTarget == undefined) return EVEN; if (baseFile === undefined && checkTarget == undefined) return EVEN;
if (baseFile == undefined) return TARGET_IS_NEW; if (baseFile == undefined) return TARGET_IS_NEW;
if (checkTarget == undefined) return BASE_IS_NEW; if (checkTarget == undefined) return BASE_IS_NEW;
const modifiedBase = "stat" in baseFile ? baseFile?.stat?.mtime ?? 0 : baseFile?.mtime ?? 0; const modifiedBase = "stat" in baseFile ? (baseFile?.stat?.mtime ?? 0) : (baseFile?.mtime ?? 0);
const modifiedTarget = "stat" in checkTarget ? checkTarget?.stat?.mtime ?? 0 : checkTarget?.mtime ?? 0; const modifiedTarget = "stat" in checkTarget ? (checkTarget?.stat?.mtime ?? 0) : (checkTarget?.mtime ?? 0);
if (modifiedBase && modifiedTarget && isMarkedAsSameChanges(baseFile, [modifiedBase, modifiedTarget])) { if (modifiedBase && modifiedTarget && isMarkedAsSameChanges(baseFile, [modifiedBase, modifiedTarget])) {
return EVEN; return EVEN;
@@ -242,21 +354,27 @@ export function compareFileFreshness(baseFile: UXFileInfoStub | AnyEntry | undef
return compareMTime(modifiedBase, modifiedTarget); return compareMTime(modifiedBase, modifiedTarget);
} }
const _cached = new Map<string, { const _cached = new Map<
value: any; string,
context: Map<string, any>; {
}>(); value: any;
context: Map<string, any>;
}
>();
export type MemoOption = { export type MemoOption = {
key: string; key: string;
forceUpdate?: boolean; forceUpdate?: boolean;
validator?: (context: Map<string, any>) => boolean; validator?: (context: Map<string, any>) => boolean;
} };
export function useMemo<T>({ key, forceUpdate, validator }: MemoOption, updateFunc: (context: Map<string, any>, prev: T) => T): T { export function useMemo<T>(
{ key, forceUpdate, validator }: MemoOption,
updateFunc: (context: Map<string, any>, prev: T) => T
): T {
const cached = _cached.get(key); const cached = _cached.get(key);
const context = cached?.context || new Map<string, any>(); const context = cached?.context || new Map<string, any>();
if (cached && !forceUpdate && (!validator || validator && !validator(context))) { if (cached && !forceUpdate && (!validator || (validator && !validator(context)))) {
return cached.value; return cached.value;
} }
const value = updateFunc(context, cached?.value); const value = updateFunc(context, cached?.value);
@@ -267,11 +385,14 @@ export function useMemo<T>({ key, forceUpdate, validator }: MemoOption, updateFu
} }
// const _static = new Map<string, any>(); // const _static = new Map<string, any>();
const _staticObj = new Map<string, { const _staticObj = new Map<
value: any string,
}>(); {
value: any;
}
>();
export function useStatic<T>(key: string): { value: (T | undefined) }; export function useStatic<T>(key: string): { value: T | undefined };
export function useStatic<T>(key: string, initial: T): { value: T }; export function useStatic<T>(key: string, initial: T): { value: T };
export function useStatic<T>(key: string, initial?: T) { export function useStatic<T>(key: string, initial?: T) {
// if (!_static.has(key) && initial) { // if (!_static.has(key) && initial) {
@@ -288,9 +409,9 @@ export function useStatic<T>(key: string, initial?: T) {
return this._buf as T; return this._buf as T;
}, },
set value(value: T) { set value(value: T) {
this._buf = value this._buf = value;
} },
} };
_staticObj.set(key, obj); _staticObj.set(key, obj);
return obj; return obj;
} }
@@ -308,6 +429,255 @@ export function displayRev(rev: string) {
return `${number}-${hash.substring(0, 6)}`; return `${number}-${hash.substring(0, 6)}`;
} }
// export function getPathFromUXFileInfo(file: UXFileInfoStub | UXFileInfo | string) { type DocumentProps = {
// return (typeof file == "string" ? file : file.path) as FilePathWithPrefix; id: DocumentID;
// } rev?: string;
prefixedPath: FilePathWithPrefix;
path: FilePath;
isDeleted: boolean;
revDisplay: string;
shortenedId: string;
shortenedPath: string;
};
export function getDocProps(doc: AnyEntry): DocumentProps {
const id = doc._id;
const shortenedId = id.substring(0, 10);
const prefixedPath = getPath(doc);
const path = stripAllPrefixes(prefixedPath);
const rev = doc._rev;
const revDisplay = rev ? displayRev(rev) : "0-NOREVS";
// const prefix = prefixedPath.substring(0, prefixedPath.length - path.length);
const shortenedPath = path.substring(0, 10);
const isDeleted = doc._deleted || doc.deleted || false;
return { id, rev, revDisplay, prefixedPath, path, isDeleted, shortenedId, shortenedPath };
}
export function getLogLevel(showNotice: boolean) {
return showNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO;
}
export type MapLike<K, V> = {
set(key: K, value: V): Map<K, V>;
clear(): void;
delete(key: K): boolean;
get(key: K): V | undefined;
has(key: K): boolean;
keys: () => IterableIterator<K>;
get size(): number;
};
export async function autosaveCache<K, V>(db: KeyValueDatabase, mapKey: string): Promise<MapLike<K, V>> {
const savedData = (await db.get<Map<K, V>>(mapKey)) ?? new Map<K, V>();
const _commit = () => {
try {
scheduleTask("commit-map-save-" + mapKey, 250, async () => {
await db.set(mapKey, savedData);
});
} catch {
// NO OP.
}
};
return {
set(key: K, value: V) {
const modified = savedData.get(key) !== value;
const result = savedData.set(key, value);
if (modified) {
_commit();
}
return result;
},
clear(): void {
savedData.clear();
_commit();
},
delete(key: K): boolean {
const result = savedData.delete(key);
if (result) {
_commit();
}
return result;
},
get(key: K): V | undefined {
return savedData.get(key);
},
has(key) {
return savedData.has(key);
},
keys() {
return savedData.keys();
},
get size() {
return savedData.size;
},
};
}
export function onlyInNTimes(n: number, proc: (progress: number) => any) {
let counter = 0;
return function () {
if (counter++ % n == 0) {
proc(counter);
}
};
}
const waitingTasks = {} as Record<string, { task?: PromiseWithResolvers<any>; previous: number; leastNext: number }>;
export function rateLimitedSharedExecution<T>(key: string, interval: number, proc: () => Promise<T>): Promise<T> {
if (!(key in waitingTasks)) {
waitingTasks[key] = { task: undefined, previous: 0, leastNext: 0 };
}
if (waitingTasks[key].task) {
// Extend the previous execution time.
waitingTasks[key].leastNext = Date.now() + interval;
return waitingTasks[key].task.promise;
}
const previous = waitingTasks[key].previous;
const delay = previous == 0 ? 0 : Math.max(interval - (Date.now() - previous), 0);
const task = promiseWithResolver<T>();
void task.promise.finally(() => {
if (waitingTasks[key].task === task) {
waitingTasks[key].task = undefined;
waitingTasks[key].previous = Math.max(Date.now(), waitingTasks[key].leastNext);
}
});
waitingTasks[key] = {
task,
previous: Date.now(),
leastNext: Date.now() + interval,
};
void scheduleTask("thin-out-" + key, delay, async () => {
try {
task.resolve(await proc());
} catch (ex) {
task.reject(ex);
}
});
return task.promise;
}
export function updatePreviousExecutionTime(key: string, timeDelta: number = 0) {
if (!(key in waitingTasks)) {
waitingTasks[key] = { task: undefined, previous: 0, leastNext: 0 };
}
waitingTasks[key].leastNext = Math.max(Date.now() + timeDelta, waitingTasks[key].leastNext);
}
const prefixMapObject = {
s: {
1: "V",
2: "W",
3: "X",
4: "Y",
5: "Z",
},
o: {
1: "v",
2: "w",
3: "x",
4: "y",
5: "z",
},
} as Record<string, Record<number, string>>;
const decodePrefixMapObject = Object.fromEntries(
Object.entries(prefixMapObject).flatMap(([prefix, map]) =>
Object.entries(map).map(([len, char]) => [char, { prefix, len: parseInt(len) }])
)
);
const prefixMapNumber = {
n: {
1: "a",
2: "b",
3: "c",
4: "d",
5: "e",
},
N: {
1: "A",
2: "B",
3: "C",
4: "D",
5: "E",
},
} as Record<string, Record<number, string>>;
const decodePrefixMapNumber = Object.fromEntries(
Object.entries(prefixMapNumber).flatMap(([prefix, map]) =>
Object.entries(map).map(([len, char]) => [char, { prefix, len: parseInt(len) }])
)
);
export function encodeAnyArray(obj: any[]): string {
const tempArray = obj.map((v) => {
if (v === null) return "n";
if (v === false) return "f";
if (v === true) return "t";
if (v === undefined) return "u";
if (typeof v == "number") {
const b36 = v.toString(36);
const strNum = v.toString();
const expression = b36.length < strNum.length ? "N" : "n";
const encodedStr = expression == "N" ? b36 : strNum;
const len = encodedStr.length.toString(36);
const lenLen = len.length;
const prefix2 = prefixMapNumber[expression][lenLen];
return prefix2 + len + encodedStr;
}
const str = typeof v == "string" ? v : JSON.stringify(v);
const prefix = typeof v == "string" ? "s" : "o";
const length = str.length.toString(36);
const lenLen = length.length;
const prefix2 = prefixMapObject[prefix][lenLen];
return prefix2 + length + str;
});
const w = tempArray.join("");
return w;
}
const decodeMapConstant = {
u: undefined,
n: null,
f: false,
t: true,
} as Record<string, any>;
export function decodeAnyArray(str: string): any[] {
const result = [];
let i = 0;
while (i < str.length) {
const char = str[i];
i++;
if (char in decodeMapConstant) {
result.push(decodeMapConstant[char]);
continue;
}
if (char in decodePrefixMapNumber) {
const { prefix, len } = decodePrefixMapNumber[char];
const lenStr = str.substring(i, i + len);
i += len;
const radix = prefix == "N" ? 36 : 10;
const lenNum = parseInt(lenStr, 36);
const value = str.substring(i, i + lenNum);
i += lenNum;
result.push(parseInt(value, radix));
continue;
}
const { prefix, len } = decodePrefixMapObject[char];
const lenStr = str.substring(i, i + len);
i += len;
const lenNum = parseInt(lenStr, 36);
const value = str.substring(i, i + lenNum);
i += lenNum;
if (prefix == "s") {
result.push(value);
} else {
result.push(JSON.parse(value));
}
}
return result;
}

View File

@@ -1,13 +1,39 @@
import { type FilePath } from "./lib/src/common/types.ts"; import { type FilePath } from "./lib/src/common/types.ts";
export { export {
addIcon, App, debounce, Editor, FuzzySuggestModal, MarkdownRenderer, MarkdownView, Modal, Notice, Platform, Plugin, PluginSettingTab, requestUrl, sanitizeHTMLToDom, Setting, stringifyYaml, TAbstractFile, TextAreaComponent, TFile, TFolder, addIcon,
parseYaml, ItemView, WorkspaceLeaf App,
debounce,
Editor,
FuzzySuggestModal,
MarkdownRenderer,
MarkdownView,
Modal,
Notice,
Platform,
Plugin,
PluginSettingTab,
requestUrl,
sanitizeHTMLToDom,
Setting,
stringifyYaml,
TAbstractFile,
TextAreaComponent,
TFile,
TFolder,
parseYaml,
ItemView,
WorkspaceLeaf,
} from "obsidian"; } from "obsidian";
export type { DataWriteOptions, PluginManifest, RequestUrlParam, RequestUrlResponse, MarkdownFileInfo, ListedFiles } from "obsidian"; export type {
import { DataWriteOptions,
normalizePath as normalizePath_ PluginManifest,
RequestUrlParam,
RequestUrlResponse,
MarkdownFileInfo,
ListedFiles,
} from "obsidian"; } from "obsidian";
import { normalizePath as normalizePath_ } from "obsidian";
const normalizePath = normalizePath_ as <T extends string | FilePath>(from: T) => T; const normalizePath = normalizePath_ as <T extends string | FilePath>(from: T) => T;
export { normalizePath } export { normalizePath };
export { type Diff, DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, diff_match_patch } from "diff-match-patch"; export { type Diff, DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, diff_match_patch } from "diff-match-patch";

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,10 @@
<script lang="ts"> <script lang="ts">
import { ConfigSync, PluginDataExDisplayV2, type IPluginDataExDisplay } from "./CmdConfigSync.ts"; import {
ConfigSync,
PluginDataExDisplayV2,
type IPluginDataExDisplay,
type PluginDataExFile,
} from "./CmdConfigSync.ts";
import { Logger } from "../../lib/src/common/logger"; import { Logger } from "../../lib/src/common/logger";
import { type FilePath, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "../../lib/src/common/types"; import { type FilePath, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "../../lib/src/common/types";
import { getDocData, timeDeltaToHumanReadable, unique } from "../../lib/src/common/utils"; import { getDocData, timeDeltaToHumanReadable, unique } from "../../lib/src/common/utils";
@@ -15,7 +20,11 @@
export let applyAllPluse = 0; export let applyAllPluse = 0;
export let applyData: (data: IPluginDataExDisplay) => Promise<boolean>; export let applyData: (data: IPluginDataExDisplay) => Promise<boolean>;
export let compareData: (dataA: IPluginDataExDisplay, dataB: IPluginDataExDisplay, compareEach?: boolean) => Promise<boolean>; export let compareData: (
dataA: IPluginDataExDisplay,
dataB: IPluginDataExDisplay,
compareEach?: boolean
) => Promise<boolean>;
export let deleteData: (data: IPluginDataExDisplay) => Promise<boolean>; export let deleteData: (data: IPluginDataExDisplay) => Promise<boolean>;
export let hidden: boolean; export let hidden: boolean;
export let plugin: ObsidianLiveSyncPlugin; export let plugin: ObsidianLiveSyncPlugin;
@@ -151,7 +160,11 @@
canCompare = result.canCompare; canCompare = result.canCompare;
pickToCompare = false; pickToCompare = false;
if (canCompare) { if (canCompare) {
if (local?.files.length == remote?.files.length && local?.files.length == 1 && local?.files[0].filename == remote?.files[0].filename) { if (
local?.files.length == remote?.files.length &&
local?.files.length == 1 &&
local?.files[0].filename == remote?.files[0].filename
) {
pickToCompare = false; pickToCompare = false;
} else { } else {
pickToCompare = true; pickToCompare = true;
@@ -250,7 +263,11 @@
const selectedItem = list.find((e) => e.term == selected); const selectedItem = list.find((e) => e.term == selected);
await compareItems(local, selectedItem); await compareItems(local, selectedItem);
} }
async function compareItems(local: IPluginDataExDisplay | undefined, remote: IPluginDataExDisplay | undefined, filename?: string) { async function compareItems(
local: IPluginDataExDisplay | undefined,
remote: IPluginDataExDisplay | undefined,
filename?: string
) {
if (local && remote) { if (local && remote) {
if (!filename) { if (!filename) {
if (await compareData(local, remote)) { if (await compareData(local, remote)) {
@@ -258,8 +275,10 @@
} }
return; return;
} else { } else {
const localCopy = local instanceof PluginDataExDisplayV2 ? new PluginDataExDisplayV2(local) : { ...local }; const localCopy =
const remoteCopy = remote instanceof PluginDataExDisplayV2 ? new PluginDataExDisplayV2(remote) : { ...remote }; local instanceof PluginDataExDisplayV2 ? new PluginDataExDisplayV2(local) : { ...local };
const remoteCopy =
remote instanceof PluginDataExDisplayV2 ? new PluginDataExDisplayV2(remote) : { ...remote };
localCopy.files = localCopy.files.filter((e) => e.filename == filename); localCopy.files = localCopy.files.filter((e) => e.filename == filename);
remoteCopy.files = remoteCopy.files.filter((e) => e.filename == filename); remoteCopy.files = remoteCopy.files.filter((e) => e.filename == filename);
if (await compareData(localCopy, remoteCopy, true)) { if (await compareData(localCopy, remoteCopy, true)) {
@@ -287,9 +306,17 @@
menu.addItem((item) => item.setTitle("Compare file").setIsLabel(true)); menu.addItem((item) => item.setTitle("Compare file").setIsLabel(true));
menu.addSeparator(); menu.addSeparator();
const files = unique(local.files.map((e) => e.filename).concat(selectedItem.files.map((e) => e.filename))); const files = unique(local.files.map((e) => e.filename).concat(selectedItem.files.map((e) => e.filename)));
const convDate = (dt: PluginDataExFile | undefined) => {
if (!dt) return "(Missing)";
const d = new Date(dt.mtime);
return d.toLocaleString();
};
for (const filename of files) { for (const filename of files) {
menu.addItem((item) => { menu.addItem((item) => {
item.setTitle(filename).onClick((e) => compareItems(local, selectedItem, filename)); const localFile = local.files.find((e) => e.filename == filename);
const remoteFile = selectedItem.files.find((e) => e.filename == filename);
const title = `${filename} (${convDate(localFile)} <--> ${convDate(remoteFile)})`;
item.setTitle(title).onClick((e) => compareItems(local, selectedItem, filename));
}); });
} }
menu.showAtMouseEvent(evt); menu.showAtMouseEvent(evt);
@@ -321,7 +348,7 @@
</script> </script>
{#if terms.length > 0} {#if terms.length > 0}
<span class="spacer" /> <span class="spacer"></span>
{#if !hidden} {#if !hidden}
<span class="chip-wrap"> <span class="chip-wrap">
<span class="chip modified">{freshness}</span> <span class="chip modified">{freshness}</span>
@@ -343,12 +370,15 @@
<button on:click={compareSelected}>⮂</button> <button on:click={compareSelected}>⮂</button>
{/if} {/if}
{:else} {:else}
<button disabled /> <!-- svelte-ignore a11y_consider_explicit_label -->
<button disabled></button>
{/if} {/if}
<button on:click={applySelected}>✓</button> <button on:click={applySelected}>✓</button>
{:else} {:else}
<button disabled /> <!-- svelte-ignore a11y_consider_explicit_label -->
<button disabled /> <button disabled></button>
<!-- svelte-ignore a11y_consider_explicit_label -->
<button disabled></button>
{/if} {/if}
{#if isMaintenanceMode} {#if isMaintenanceMode}
{#if selected != ""} {#if selected != ""}
@@ -359,10 +389,12 @@
{/if} {/if}
{/if} {/if}
{:else} {:else}
<span class="spacer" /> <span class="spacer"></span>
<span class="message even">All the same or non-existent</span> <span class="message even">All the same or non-existent</span>
<button disabled /> <!-- svelte-ignore a11y_consider_explicit_label -->
<button disabled /> <button disabled></button>
<!-- svelte-ignore a11y_consider_explicit_label -->
<button disabled></button>
{/if} {/if}
<style> <style>

View File

@@ -1,9 +1,10 @@
import { mount, unmount } from "svelte";
import { App, Modal } from "../../deps.ts"; import { App, Modal } from "../../deps.ts";
import ObsidianLiveSyncPlugin from "../../main.ts"; import ObsidianLiveSyncPlugin from "../../main.ts";
import PluginPane from "./PluginPane.svelte"; import PluginPane from "./PluginPane.svelte";
export class PluginDialogModal extends Modal { export class PluginDialogModal extends Modal {
plugin: ObsidianLiveSyncPlugin; plugin: ObsidianLiveSyncPlugin;
component: PluginPane | undefined; component: ReturnType<typeof mount> | undefined;
isOpened() { isOpened() {
return this.component != undefined; return this.component != undefined;
} }
@@ -18,17 +19,18 @@ export class PluginDialogModal extends Modal {
this.contentEl.style.overflow = "auto"; this.contentEl.style.overflow = "auto";
this.contentEl.style.display = "flex"; this.contentEl.style.display = "flex";
this.contentEl.style.flexDirection = "column"; this.contentEl.style.flexDirection = "column";
this.titleEl.setText("Customization Sync (Beta3)") this.titleEl.setText("Customization Sync (Beta3)");
if (!this.component) { if (!this.component) {
this.component = new PluginPane({ this.component = mount(PluginPane, {
target: contentEl, props: { plugin: this.plugin }, target: contentEl,
props: { plugin: this.plugin },
}); });
} }
} }
onClose() { onClose() {
if (this.component) { if (this.component) {
this.component.$destroy(); void unmount(this.component);
this.component = undefined; this.component = undefined;
} }
} }

View File

@@ -1,28 +1,43 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte"; import { onMount } from "svelte";
import ObsidianLiveSyncPlugin from "../../main"; import ObsidianLiveSyncPlugin from "../../main";
import { ConfigSync, type IPluginDataExDisplay, pluginIsEnumerating, pluginList, pluginManifestStore, pluginV2Progress } from "./CmdConfigSync.ts"; import {
ConfigSync,
type IPluginDataExDisplay,
pluginIsEnumerating,
pluginList,
pluginManifestStore,
pluginV2Progress,
} from "./CmdConfigSync.ts";
import PluginCombo from "./PluginCombo.svelte"; import PluginCombo from "./PluginCombo.svelte";
import { Menu, type PluginManifest } from "obsidian"; import { Menu, type PluginManifest } from "obsidian";
import { unique } from "../../lib/src/common/utils"; import { unique } from "../../lib/src/common/utils";
import { MODE_SELECTIVE, MODE_AUTOMATIC, MODE_PAUSED, type SYNC_MODE, type PluginSyncSettingEntry, MODE_SHINY } from "../../lib/src/common/types"; import {
MODE_SELECTIVE,
MODE_AUTOMATIC,
MODE_PAUSED,
type SYNC_MODE,
MODE_SHINY,
} from "../../lib/src/common/types";
import { normalizePath } from "../../deps"; import { normalizePath } from "../../deps";
import { HiddenFileSync } from "../HiddenFileSync/CmdHiddenFileSync.ts"; import { HiddenFileSync } from "../HiddenFileSync/CmdHiddenFileSync.ts";
import { LOG_LEVEL_NOTICE, Logger } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_NOTICE, Logger } from "octagonal-wheels/common/logger";
export let plugin: ObsidianLiveSyncPlugin; export let plugin: ObsidianLiveSyncPlugin;
$: hideNotApplicable = false; $: hideNotApplicable = false;
$: thisTerm = plugin.deviceAndVaultName; $: thisTerm = plugin.$$getDeviceAndVaultName();
const addOn = plugin.getAddOn(ConfigSync.name) as ConfigSync; const addOn = plugin.getAddOn(ConfigSync.name) as ConfigSync;
if (!addOn) { if (!addOn) {
const msg = "AddOn Module (ConfigSync) has not been loaded. This is very unexpected situation. Please report this issue."; const msg =
"AddOn Module (ConfigSync) has not been loaded. This is very unexpected situation. Please report this issue.";
Logger(msg, LOG_LEVEL_NOTICE); Logger(msg, LOG_LEVEL_NOTICE);
throw new Error(msg); throw new Error(msg);
} }
const addOnHiddenFileSync = plugin.getAddOn(HiddenFileSync.name) as HiddenFileSync; const addOnHiddenFileSync = plugin.getAddOn(HiddenFileSync.name) as HiddenFileSync;
if (!addOnHiddenFileSync) { if (!addOnHiddenFileSync) {
const msg = "AddOn Module (HiddenFileSync) has not been loaded. This is very unexpected situation. Please report this issue."; const msg =
"AddOn Module (HiddenFileSync) has not been loaded. This is very unexpected situation. Please report this issue.";
Logger(msg, LOG_LEVEL_NOTICE); Logger(msg, LOG_LEVEL_NOTICE);
throw new Error(msg); throw new Error(msg);
} }
@@ -99,7 +114,11 @@
async function applyData(data: IPluginDataExDisplay): Promise<boolean> { async function applyData(data: IPluginDataExDisplay): Promise<boolean> {
return await addOn.applyData(data); return await addOn.applyData(data);
} }
async function compareData(docA: IPluginDataExDisplay, docB: IPluginDataExDisplay, compareEach = false): Promise<boolean> { async function compareData(
docA: IPluginDataExDisplay,
docB: IPluginDataExDisplay,
compareEach = false
): Promise<boolean> {
return await addOn.compareUsingDisplayData(docA, docB, compareEach); return await addOn.compareUsingDisplayData(docA, docB, compareEach);
} }
async function deleteData(data: IPluginDataExDisplay): Promise<boolean> { async function deleteData(data: IPluginDataExDisplay): Promise<boolean> {
@@ -130,7 +149,7 @@
setMode(key, MODE_AUTOMATIC); setMode(key, MODE_AUTOMATIC);
const configDir = normalizePath(plugin.app.vault.configDir); const configDir = normalizePath(plugin.app.vault.configDir);
const files = (plugin.settings.pluginSyncExtendedSetting[key]?.files ?? []).map((e) => `${configDir}/${e}`); const files = (plugin.settings.pluginSyncExtendedSetting[key]?.files ?? []).map((e) => `${configDir}/${e}`);
addOnHiddenFileSync.syncInternalFilesAndDatabase(direction, true, false, files); addOnHiddenFileSync.initialiseInternalFileSync(direction, true, files);
} }
function askOverwriteModeForAutomatic(evt: MouseEvent, key: string) { function askOverwriteModeForAutomatic(evt: MouseEvent, key: string) {
const menu = new Menu(); const menu = new Menu();
@@ -199,7 +218,7 @@
.filter((e) => `${e.category}/${e.name}` == key) .filter((e) => `${e.category}/${e.name}` == key)
.map((e) => e.files) .map((e) => e.files)
.flat() .flat()
.map((e) => e.filename), .map((e) => e.filename)
); );
if (mode == MODE_SELECTIVE) { if (mode == MODE_SELECTIVE) {
automaticList.delete(key); automaticList.delete(key);
@@ -249,7 +268,15 @@
.map((e) => ({ category: e[0], name: e[1], displayName: e[1] })), .map((e) => ({ category: e[0], name: e[1], displayName: e[1] })),
] ]
.sort((a, b) => (a.displayName ?? a.name).localeCompare(b.displayName ?? b.name)) .sort((a, b) => (a.displayName ?? a.name).localeCompare(b.displayName ?? b.name))
.reduce((p, c) => ({ ...p, [c.category]: unique(c.category in p ? [...p[c.category], c.displayName ?? c.name] : [c.displayName ?? c.name]) }), {} as Record<string, string[]>); .reduce(
(p, c) => ({
...p,
[c.category]: unique(
c.category in p ? [...p[c.category], c.displayName ?? c.name] : [c.displayName ?? c.name]
),
}),
{} as Record<string, string[]>
);
} }
$: { $: {
displayKeys = computeDisplayKeys(list); displayKeys = computeDisplayKeys(list);
@@ -337,7 +364,12 @@
</div> </div>
<div class="body"> <div class="body">
{#if mode == MODE_SELECTIVE || mode == MODE_SHINY} {#if mode == MODE_SELECTIVE || mode == MODE_SHINY}
<PluginCombo {...options} isFlagged={mode == MODE_SHINY} list={list.filter((e) => e.category == key && e.name == name)} hidden={false} /> <PluginCombo
{...options}
isFlagged={mode == MODE_SHINY}
list={list.filter((e) => e.category == key && e.name == name)}
hidden={false}
/>
{:else} {:else}
<div class="statusnote">{TITLES[mode]}</div> <div class="statusnote">{TITLES[mode]}</div>
{/if} {/if}
@@ -359,7 +391,10 @@
{@const modeEtc = automaticListDisp.get(bindKeyETC) ?? MODE_SELECTIVE} {@const modeEtc = automaticListDisp.get(bindKeyETC) ?? MODE_SELECTIVE}
<div class="labelrow {hideEven ? 'hideeven' : ''}"> <div class="labelrow {hideEven ? 'hideeven' : ''}">
<div class="title"> <div class="title">
<button class="status" on:click={(evt) => askMode(evt, `${PREFIX_PLUGIN_ALL}/${name}`, bindKeyAll)}> <button
class="status"
on:click={(evt) => askMode(evt, `${PREFIX_PLUGIN_ALL}/${name}`, bindKeyAll)}
>
{getIcon(modeAll)} {getIcon(modeAll)}
</button> </button>
<span class="name">{nameMap.get(`plugins/${name}`) || name}</span> <span class="name">{nameMap.get(`plugins/${name}`) || name}</span>
@@ -373,14 +408,22 @@
{#if modeAll == MODE_SELECTIVE || modeAll == MODE_SHINY} {#if modeAll == MODE_SELECTIVE || modeAll == MODE_SHINY}
<div class="filerow {hideEven ? 'hideeven' : ''}"> <div class="filerow {hideEven ? 'hideeven' : ''}">
<div class="filetitle"> <div class="filetitle">
<button class="status" on:click={(evt) => askMode(evt, `${PREFIX_PLUGIN_MAIN}/${name}/MAIN`, bindKeyMain)}> <button
class="status"
on:click={(evt) => askMode(evt, `${PREFIX_PLUGIN_MAIN}/${name}/MAIN`, bindKeyMain)}
>
{getIcon(modeMain)} {getIcon(modeMain)}
</button> </button>
<span class="name">MAIN</span> <span class="name">MAIN</span>
</div> </div>
<div class="body"> <div class="body">
{#if modeMain == MODE_SELECTIVE || modeMain == MODE_SHINY} {#if modeMain == MODE_SELECTIVE || modeMain == MODE_SHINY}
<PluginCombo {...options} isFlagged={modeMain == MODE_SHINY} list={filterList(listX, ["PLUGIN_MAIN"])} hidden={false} /> <PluginCombo
{...options}
isFlagged={modeMain == MODE_SHINY}
list={filterList(listX, ["PLUGIN_MAIN"])}
hidden={false}
/>
{:else} {:else}
<div class="statusnote">{TITLES[modeMain]}</div> <div class="statusnote">{TITLES[modeMain]}</div>
{/if} {/if}
@@ -388,14 +431,22 @@
</div> </div>
<div class="filerow {hideEven ? 'hideeven' : ''}"> <div class="filerow {hideEven ? 'hideeven' : ''}">
<div class="filetitle"> <div class="filetitle">
<button class="status" on:click={(evt) => askMode(evt, `${PREFIX_PLUGIN_DATA}/${name}`, bindKeyData)}> <button
class="status"
on:click={(evt) => askMode(evt, `${PREFIX_PLUGIN_DATA}/${name}`, bindKeyData)}
>
{getIcon(modeData)} {getIcon(modeData)}
</button> </button>
<span class="name">DATA</span> <span class="name">DATA</span>
</div> </div>
<div class="body"> <div class="body">
{#if modeData == MODE_SELECTIVE || modeData == MODE_SHINY} {#if modeData == MODE_SELECTIVE || modeData == MODE_SHINY}
<PluginCombo {...options} isFlagged={modeData == MODE_SHINY} list={filterList(listX, ["PLUGIN_DATA"])} hidden={false} /> <PluginCombo
{...options}
isFlagged={modeData == MODE_SHINY}
list={filterList(listX, ["PLUGIN_DATA"])}
hidden={false}
/>
{:else} {:else}
<div class="statusnote">{TITLES[modeData]}</div> <div class="statusnote">{TITLES[modeData]}</div>
{/if} {/if}
@@ -404,14 +455,22 @@
{#if useSyncPluginEtc} {#if useSyncPluginEtc}
<div class="filerow {hideEven ? 'hideeven' : ''}"> <div class="filerow {hideEven ? 'hideeven' : ''}">
<div class="filetitle"> <div class="filetitle">
<button class="status" on:click={(evt) => askMode(evt, `${PREFIX_PLUGIN_ETC}/${name}`, bindKeyETC)}> <button
class="status"
on:click={(evt) => askMode(evt, `${PREFIX_PLUGIN_ETC}/${name}`, bindKeyETC)}
>
{getIcon(modeEtc)} {getIcon(modeEtc)}
</button> </button>
<span class="name">Other files</span> <span class="name">Other files</span>
</div> </div>
<div class="body"> <div class="body">
{#if modeEtc == MODE_SELECTIVE || modeEtc == MODE_SHINY} {#if modeEtc == MODE_SELECTIVE || modeEtc == MODE_SHINY}
<PluginCombo {...options} isFlagged={modeEtc == MODE_SHINY} list={filterList(listX, ["PLUGIN_ETC"])} hidden={false} /> <PluginCombo
{...options}
isFlagged={modeEtc == MODE_SHINY}
list={filterList(listX, ["PLUGIN_ETC"])}
hidden={false}
/>
{:else} {:else}
<div class="statusnote">{TITLES[modeEtc]}</div> <div class="statusnote">{TITLES[modeEtc]}</div>
{/if} {/if}
@@ -483,10 +542,12 @@
padding-right: 4px; padding-right: 4px;
flex-wrap: wrap; flex-wrap: wrap;
} }
.filerow.hideeven:has(.even),
.labelrow.hideeven:has(.even) { .filerow.hideeven:has(:global(.even)),
.labelrow.hideeven:has(:global(.even)) {
display: none; display: none;
} }
.noterow { .noterow {
min-height: 2em; min-height: 2em;
display: flex; display: flex;

View File

@@ -2,13 +2,14 @@ import { App, Modal } from "../../deps.ts";
import { type FilePath, type LoadedEntry } from "../../lib/src/common/types.ts"; import { type FilePath, type LoadedEntry } from "../../lib/src/common/types.ts";
import JsonResolvePane from "./JsonResolvePane.svelte"; import JsonResolvePane from "./JsonResolvePane.svelte";
import { waitForSignal } from "../../lib/src/common/utils.ts"; import { waitForSignal } from "../../lib/src/common/utils.ts";
import { mount, unmount } from "svelte";
export class JsonResolveModal extends Modal { export class JsonResolveModal extends Modal {
// result: Array<[number, string]>; // result: Array<[number, string]>;
filename: FilePath; filename: FilePath;
callback?: (keepRev?: string, mergedStr?: string) => Promise<void>; callback?: (keepRev?: string, mergedStr?: string) => Promise<void>;
docs: LoadedEntry[]; docs: LoadedEntry[];
component?: JsonResolvePane; component?: ReturnType<typeof mount>;
nameA: string; nameA: string;
nameB: string; nameB: string;
defaultSelect: string; defaultSelect: string;
@@ -16,10 +17,18 @@ export class JsonResolveModal extends Modal {
hideLocal: boolean; hideLocal: boolean;
title: string = "Conflicted Setting"; title: string = "Conflicted Setting";
constructor(app: App, filename: FilePath, constructor(
docs: LoadedEntry[], callback: (keepRev?: string, mergedStr?: string) => Promise<void>, app: App,
nameA?: string, nameB?: string, defaultSelect?: string, filename: FilePath,
keepOrder?: boolean, hideLocal?: boolean, title: string = "Conflicted Setting") { docs: LoadedEntry[],
callback: (keepRev?: string, mergedStr?: string) => Promise<void>,
nameA?: string,
nameB?: string,
defaultSelect?: string,
keepOrder?: boolean,
hideLocal?: boolean,
title: string = "Conflicted Setting"
) {
super(app); super(app);
this.callback = callback; this.callback = callback;
this.filename = filename; this.filename = filename;
@@ -47,7 +56,7 @@ export class JsonResolveModal extends Modal {
contentEl.empty(); contentEl.empty();
if (this.component == undefined) { if (this.component == undefined) {
this.component = new JsonResolvePane({ this.component = mount(JsonResolvePane, {
target: contentEl, target: contentEl,
props: { props: {
docs: this.docs, docs: this.docs,
@@ -57,14 +66,14 @@ export class JsonResolveModal extends Modal {
defaultSelect: this.defaultSelect, defaultSelect: this.defaultSelect,
keepOrder: this.keepOrder, keepOrder: this.keepOrder,
hideLocal: this.hideLocal, hideLocal: this.hideLocal,
callback: (keepRev: string | undefined, mergedStr: string | undefined) => this.UICallback(keepRev, mergedStr), callback: (keepRev: string | undefined, mergedStr: string | undefined) =>
this.UICallback(keepRev, mergedStr),
}, },
}); });
} }
return; return;
} }
onClose() { onClose() {
const { contentEl } = this; const { contentEl } = this;
contentEl.empty(); contentEl.empty();
@@ -73,7 +82,7 @@ export class JsonResolveModal extends Modal {
void this.callback(undefined); void this.callback(undefined);
} }
if (this.component != undefined) { if (this.component != undefined) {
this.component.$destroy(); void unmount(this.component);
this.component = undefined; this.component = undefined;
} }
} }

View File

@@ -2,29 +2,64 @@
import { type Diff, DIFF_DELETE, DIFF_INSERT, diff_match_patch } from "../../deps.ts"; import { type Diff, DIFF_DELETE, DIFF_INSERT, diff_match_patch } from "../../deps.ts";
import type { FilePath, LoadedEntry } from "../../lib/src/common/types.ts"; import type { FilePath, LoadedEntry } from "../../lib/src/common/types.ts";
import { decodeBinary, readString } from "../../lib/src/string_and_binary/convert.ts"; import { decodeBinary, readString } from "../../lib/src/string_and_binary/convert.ts";
import { getDocData, mergeObject } from "../../lib/src/common/utils.ts"; import { getDocData, isObjectDifferent, mergeObject } from "../../lib/src/common/utils.ts";
export let docs: LoadedEntry[] = []; interface Props {
export let callback: (keepRev?: string, mergedStr?: string) => Promise<void> = async (_, __) => { docs?: LoadedEntry[];
Promise.resolve(); callback?: (keepRev?: string, mergedStr?: string) => Promise<void>;
}; filename?: FilePath;
export let filename: FilePath = "" as FilePath; nameA?: string;
export let nameA: string = "A"; nameB?: string;
export let nameB: string = "B"; defaultSelect?: string;
export let defaultSelect: string = ""; keepOrder?: boolean;
export let keepOrder = false; hideLocal?: boolean;
export let hideLocal: boolean = false; }
let docA: LoadedEntry;
let docB: LoadedEntry; let {
let docAContent = ""; docs = $bindable([]),
let docBContent = ""; callback = $bindable((async (_, __) => {
let objA: any = {}; Promise.resolve();
let objB: any = {}; }) as (keepRev?: string, mergedStr?: string) => Promise<void>),
let objAB: any = {}; filename = $bindable("" as FilePath),
let objBA: any = {}; nameA = $bindable("A"),
let diffs: Diff[]; nameB = $bindable("B"),
defaultSelect = $bindable("" as string),
keepOrder = $bindable(false),
hideLocal = $bindable(false),
}: Props = $props();
type JSONData = Record<string | number | symbol, any> | [any];
const docsArray = $derived.by(() => {
if (docs && docs.length >= 1) {
if (keepOrder || docs[0].mtime < docs[1].mtime) {
return { a: docs[0], b: docs[1] } as const;
} else {
return { a: docs[1], b: docs[0] } as const;
}
}
return { a: false, b: false } as const;
});
const docA = $derived(docsArray.a);
const docB = $derived(docsArray.b);
const docAContent = $derived(docA && docToString(docA));
const docBContent = $derived(docB && docToString(docB));
function parseJson(json: string | false) {
if (json === false) return false;
try {
return JSON.parse(json) as JSONData;
} catch (ex) {
return false;
}
}
const objA = $derived(parseJson(docAContent) || {});
const objB = $derived(parseJson(docBContent) || {});
const objAB = $derived(mergeObject(objA, objB));
const objBAw = $derived(mergeObject(objB, objA));
const objBA = $derived(isObjectDifferent(objBAw, objAB) ? objBAw : false);
let diffs: Diff[] = $derived.by(() => (objA && selectedObj ? getJsonDiff(objA, selectedObj) : []));
type SelectModes = "" | "A" | "B" | "AB" | "BA"; type SelectModes = "" | "A" | "B" | "AB" | "BA";
let mode: SelectModes = defaultSelect as SelectModes; let mode: SelectModes = $state(defaultSelect as SelectModes);
function docToString(doc: LoadedEntry) { function docToString(doc: LoadedEntry) {
return doc.datatype == "plain" ? getDocData(doc.data) : readString(new Uint8Array(decodeBinary(doc.data))); return doc.datatype == "plain" ? getDocData(doc.data) : readString(new Uint8Array(decodeBinary(doc.data)));
@@ -45,6 +80,7 @@
return getDiff(JSON.stringify(a, null, 2), JSON.stringify(b, null, 2)); return getDiff(JSON.stringify(a, null, 2), JSON.stringify(b, null, 2));
} }
function apply() { function apply() {
if (!docA || !docB) return;
if (docA._id == docB._id) { if (docA._id == docB._id) {
if (mode == "A") return callback(docA._rev!, undefined); if (mode == "A") return callback(docA._rev!, undefined);
if (mode == "B") return callback(docB._rev!, undefined); if (mode == "B") return callback(docB._rev!, undefined);
@@ -59,50 +95,23 @@
function cancel() { function cancel() {
callback(undefined, undefined); callback(undefined, undefined);
} }
$: { const mergedObjs = $derived.by(
if (docs && docs.length >= 1) { () =>
if (keepOrder || docs[0].mtime < docs[1].mtime) { ({
docA = docs[0]; "": false,
docB = docs[1]; A: objA,
} else { B: objB,
docA = docs[1]; AB: objAB,
docB = docs[0]; BA: objBA,
} }) as Record<SelectModes, JSONData | false>
docAContent = docToString(docA); );
docBContent = docToString(docB);
try { let selectedObj = $derived(mode in mergedObjs ? mergedObjs[mode] : {});
objA = false;
objB = false;
objA = JSON.parse(docAContent);
objB = JSON.parse(docBContent);
objAB = mergeObject(objA, objB);
objBA = mergeObject(objB, objA);
if (JSON.stringify(objAB) == JSON.stringify(objBA)) {
objBA = false;
}
} catch (ex) {
objBA = false;
objAB = false;
}
}
}
$: mergedObjs = {
"": false,
A: objA,
B: objB,
AB: objAB,
BA: objBA,
};
$: selectedObj = mode in mergedObjs ? mergedObjs[mode] : {}; let modesSrc = $state([] as ["" | "A" | "B" | "AB" | "BA", string][]);
$: {
diffs = getJsonDiff(objA, selectedObj);
}
let modes = [] as ["" | "A" | "B" | "AB" | "BA", string][]; const modes = $derived.by(() => {
$: { let newModes = [] as typeof modesSrc;
let newModes = [] as typeof modes;
if (!hideLocal) { if (!hideLocal) {
newModes.push(["", "Not now"]); newModes.push(["", "Not now"]);
@@ -111,15 +120,15 @@
newModes.push(["B", nameB || "B"]); newModes.push(["B", nameB || "B"]);
newModes.push(["AB", `${nameA || "A"} + ${nameB || "B"}`]); newModes.push(["AB", `${nameA || "A"} + ${nameB || "B"}`]);
newModes.push(["BA", `${nameB || "B"} + ${nameA || "A"}`]); newModes.push(["BA", `${nameB || "B"} + ${nameA || "A"}`]);
modes = newModes; return newModes;
} });
</script> </script>
<h2>{filename}</h2> <h2>{filename}</h2>
{#if !docA || !docB} {#if !docA || !docB}
<div class="message">Just for a minute, please!</div> <div class="message">Just for a minute, please!</div>
<div class="buttons"> <div class="buttons">
<button on:click={apply}>Dismiss</button> <button onclick={apply}>Dismiss</button>
</div> </div>
{:else} {:else}
<div class="options"> <div class="options">
@@ -136,7 +145,9 @@
{#if selectedObj != false} {#if selectedObj != false}
<div class="op-scrollable json-source"> <div class="op-scrollable json-source">
{#each diffs as diff} {#each diffs as diff}
<span class={diff[0] == DIFF_DELETE ? "deleted" : diff[0] == DIFF_INSERT ? "added" : "normal"}>{diff[1]}</span> <span class={diff[0] == DIFF_DELETE ? "deleted" : diff[0] == DIFF_INSERT ? "added" : "normal"}
>{diff[1]}</span
>
{/each} {/each}
</div> </div>
{:else} {:else}
@@ -145,38 +156,40 @@
<div class="infos"> <div class="infos">
<table> <table>
<tr> <tbody>
<th>{nameA}</th> <tr>
<td <th>{nameA}</th>
>{#if docA._id == docB._id} <td
Rev:{revStringToRevNumber(docA._rev)} >{#if docA._id == docB._id}
{/if} Rev:{revStringToRevNumber(docA._rev)}
{new Date(docA.mtime).toLocaleString()}</td {/if}
> {new Date(docA.mtime).toLocaleString()}</td
<td> >
{docAContent.length} letters <td>
</td> {docAContent && docAContent.length} letters
</tr> </td>
<tr> </tr>
<th>{nameB}</th> <tr>
<td <th>{nameB}</th>
>{#if docA._id == docB._id} <td
Rev:{revStringToRevNumber(docB._rev)} >{#if docA._id == docB._id}
{/if} Rev:{revStringToRevNumber(docB._rev)}
{new Date(docB.mtime).toLocaleString()}</td {/if}
> {new Date(docB.mtime).toLocaleString()}</td
<td> >
{docBContent.length} letters <td>
</td> {docBContent && docBContent.length} letters
</tr> </td>
</tr>
</tbody>
</table> </table>
</div> </div>
<div class="buttons"> <div class="buttons">
{#if hideLocal} {#if hideLocal}
<button on:click={cancel}>Cancel</button> <button onclick={cancel}>Cancel</button>
{/if} {/if}
<button on:click={apply}>Apply</button> <button onclick={apply}>Apply</button>
</div> </div>
{/if} {/if}
@@ -203,6 +216,7 @@
overflow-y: scroll; overflow-y: scroll;
max-height: 60vh; max-height: 60vh;
user-select: text; user-select: text;
-webkit-user-select: text;
} }
.json-source { .json-source {
white-space: pre; white-space: pre;

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,19 @@
import { Logger } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_VERBOSE, Logger } from "octagonal-wheels/common/logger";
import { getPath } from "../common/utils.ts"; import { getPath } from "../common/utils.ts";
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, type AnyEntry, type DocumentID, type EntryHasPath, type FilePath, type FilePathWithPrefix, type LOG_LEVEL } from "../lib/src/common/types.ts"; import {
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
type AnyEntry,
type DocumentID,
type EntryHasPath,
type FilePath,
type FilePathWithPrefix,
type LOG_LEVEL,
} from "../lib/src/common/types.ts";
import type ObsidianLiveSyncPlugin from "../main.ts"; import type ObsidianLiveSyncPlugin from "../main.ts";
import { MARK_DONE } from "../modules/features/ModuleLog.ts";
let noticeIndex = 0;
export abstract class LiveSyncCommands { export abstract class LiveSyncCommands {
plugin: ObsidianLiveSyncPlugin; plugin: ObsidianLiveSyncPlugin;
get app() { get app() {
@@ -33,13 +43,13 @@ export abstract class LiveSyncCommands {
abstract onload(): void | Promise<void>; abstract onload(): void | Promise<void>;
_isMainReady() { _isMainReady() {
return this.plugin._isMainReady(); return this.plugin.$$isReady();
} }
_isMainSuspended() { _isMainSuspended() {
return this.plugin._isMainSuspended(); return this.plugin.$$isSuspended();
} }
_isDatabaseReady() { _isDatabaseReady() {
return this.plugin._isDatabaseReady(); return this.plugin.$$isDatabaseReady();
} }
_log = (msg: any, level: LOG_LEVEL = LOG_LEVEL_INFO, key?: string) => { _log = (msg: any, level: LOG_LEVEL = LOG_LEVEL_INFO, key?: string) => {
@@ -49,4 +59,34 @@ export abstract class LiveSyncCommands {
// console.log(msg); // console.log(msg);
Logger(msg, level, key); Logger(msg, level, key);
}; };
_verbose = (msg: any, key?: string) => {
this._log(msg, LOG_LEVEL_VERBOSE, key);
};
_info = (msg: any, key?: string) => {
this._log(msg, LOG_LEVEL_INFO, key);
};
_notice = (msg: any, key?: string) => {
this._log(msg, LOG_LEVEL_NOTICE, key);
};
_progress = (prefix: string = "", level: LOG_LEVEL = LOG_LEVEL_NOTICE) => {
const key = `keepalive-progress-${noticeIndex++}`;
return {
log: (msg: any) => {
this._log(prefix + msg, level, key);
},
once: (msg: any) => {
this._log(prefix + msg, level);
},
done: (msg: string = "Done") => {
this._log(prefix + msg + MARK_DONE, level, key);
},
};
};
_debug = (msg: any, key?: string) => {
this._log(msg, LOG_LEVEL_VERBOSE, key);
};
} }

View File

@@ -0,0 +1,265 @@
import { sizeToHumanReadable } from "octagonal-wheels/number";
import { LOG_LEVEL_NOTICE, type MetaEntry } from "../../lib/src/common/types";
import { getNoFromRev } from "../../lib/src/pouchdb/LiveSyncLocalDB";
import type { IObsidianModule } from "../../modules/AbstractObsidianModule";
import { LiveSyncCommands } from "../LiveSyncCommands";
export class LocalDatabaseMaintenance extends LiveSyncCommands implements IObsidianModule {
$everyOnload(): Promise<boolean> {
return Promise.resolve(true);
}
onunload(): void {
// NO OP.
}
onload(): void | Promise<void> {
// NO OP.
}
async allChunks(includeDeleted: boolean = false) {
const p = this._progress("", LOG_LEVEL_NOTICE);
p.log("Retrieving chunks informations..");
try {
const ret = await this.localDatabase.allChunks(includeDeleted);
return ret;
} finally {
p.done();
}
}
get database() {
return this.localDatabase.localDatabase;
}
clearHash() {
this.localDatabase.hashCaches.clear();
}
async confirm(title: string, message: string, affirmative = "Yes", negative = "No") {
return (
(await this.plugin.confirm.askSelectStringDialogue(message, [affirmative, negative], {
title,
defaultAction: affirmative,
})) === affirmative
);
}
isAvailable() {
if (!this.settings.doNotUseFixedRevisionForChunks) {
this._notice("Please enable 'Compute revisions for chunks' in settings to use Garbage Collection.");
return false;
}
if (this.settings.readChunksOnline) {
this._notice("Please disable 'Read chunks online' in settings to use Garbage Collection.");
return false;
}
return true;
}
/**
* Resurrect deleted chunks that are still used in the database.
*/
async resurrectChunks() {
if (!this.isAvailable()) return;
const { used, existing } = await this.allChunks(true);
const excessiveDeletions = [...existing]
.filter(([key, e]) => e._deleted)
.filter(([key, e]) => used.has(e._id))
.map(([key, e]) => e);
const completelyLostChunks = [] as string[];
// Data lost chunks : chunks that are deleted and data is purged.
const dataLostChunks = [...existing]
.filter(([key, e]) => e._deleted && e.data === "")
.map(([key, e]) => e)
.filter((e) => used.has(e._id));
for (const e of dataLostChunks) {
// Retrieve the data from the previous revision.
const doc = await this.database.get(e._id, { rev: e._rev, revs: true, revs_info: true, conflicts: true });
const history = doc._revs_info || [];
// Chunks are immutable. So, we can resurrect the chunk by copying the data from any of previous revisions.
let resurrected = null as null | string;
const availableRevs = history
.filter((e) => e.status == "available")
.map((e) => e.rev)
.sort((a, b) => getNoFromRev(a) - getNoFromRev(b));
for (const rev of availableRevs) {
const revDoc = await this.database.get(e._id, { rev: rev });
if (revDoc.type == "leaf" && revDoc.data !== "") {
// Found the data.
resurrected = revDoc.data;
break;
}
}
// If the data is not found, we cannot resurrect the chunk, add it to the excessiveDeletions.
if (resurrected !== null) {
excessiveDeletions.push({ ...e, data: resurrected, _deleted: false });
} else {
completelyLostChunks.push(e._id);
}
}
// Chunks to be resurrected.
const resurrectChunks = excessiveDeletions.filter((e) => e.data !== "").map((e) => ({ ...e, _deleted: false }));
if (resurrectChunks.length == 0) {
this._notice("No chunks are found to be resurrected.");
return;
}
const message = `We have following chunks that are deleted but still used in the database.
- Completely lost chunks: ${completelyLostChunks.length}
- Resurrectable chunks: ${resurrectChunks.length}
Do you want to resurrect these chunks?`;
if (await this.confirm("Resurrect Chunks", message, "Resurrect", "Cancel")) {
const result = await this.database.bulkDocs(resurrectChunks);
this.clearHash();
const resurrectedChunks = result.filter((e) => "ok" in e).map((e) => e.id);
this._notice(`Resurrected chunks: ${resurrectedChunks.length} / ${resurrectChunks.length}`);
} else {
this._notice("Resurrect operation is cancelled.");
}
}
/**
* Commit deletion of files that are marked as deleted.
* This method makes the deletion permanent, and the files will not be recovered.
* After this, chunks that are used in the deleted files become ready for compaction.
*/
async commitFileDeletion() {
if (!this.isAvailable()) return;
const p = this._progress("", LOG_LEVEL_NOTICE);
p.log("Searching for deleted files..");
const docs = await this.database.allDocs<MetaEntry>({ include_docs: true });
const deletedDocs = docs.rows.filter(
(e) => (e.doc?.type == "newnote" || e.doc?.type == "plain") && e.doc?.deleted
);
if (deletedDocs.length == 0) {
p.done("No deleted files found.");
return;
}
p.log(`Found ${deletedDocs.length} deleted files.`);
const message = `We have following files that are marked as deleted.
- Deleted files: ${deletedDocs.length}
Are you sure to delete these files permanently?
Note: **Make sure to synchronise all devices before deletion.**
> [!Note]
> This operation affects the database permanently. Deleted files will not be recovered after this operation.
> And, the chunks that are used in the deleted files will be ready for compaction.`;
const deletingDocs = deletedDocs.map((e) => ({ ...e.doc, _deleted: true }) as MetaEntry);
if (await this.confirm("Delete Files", message, "Delete", "Cancel")) {
const result = await this.database.bulkDocs(deletingDocs);
this.clearHash();
p.done(`Deleted ${result.filter((e) => "ok" in e).length} / ${deletedDocs.length} files.`);
} else {
p.done("Deletion operation is cancelled.");
}
}
/**
* Commit deletion of chunks that are not used in the database.
* This method makes the deletion permanent, and the chunks will not be recovered if the database run compaction.
* After this, the database can shrink the database size by compaction.
* It is recommended to compact the database after this operation (History should be kept once before compaction).
*/
async commitChunkDeletion() {
if (!this.isAvailable()) return;
const { existing } = await this.allChunks(true);
const deletedChunks = [...existing].filter(([key, e]) => e._deleted && e.data !== "").map(([key, e]) => e);
const deletedNotVacantChunks = deletedChunks.map((e) => ({ ...e, data: "", _deleted: true }));
const size = deletedChunks.reduce((acc, e) => acc + e.data.length, 0);
const humanSize = sizeToHumanReadable(size);
const message = `We have following chunks that are marked as deleted.
- Deleted chunks: ${deletedNotVacantChunks.length} (${humanSize})
Are you sure to delete these chunks permanently?
Note: **Make sure to synchronise all devices before deletion.**
> [!Note]
> This operation finally reduces the capacity of the remote.`;
if (deletedNotVacantChunks.length == 0) {
this._notice("No deleted chunks found.");
return;
}
if (await this.confirm("Delete Chunks", message, "Delete", "Cancel")) {
const result = await this.database.bulkDocs(deletedNotVacantChunks);
this.clearHash();
this._notice(
`Deleted chunks: ${result.filter((e) => "ok" in e).length} / ${deletedNotVacantChunks.length}`
);
} else {
this._notice("Deletion operation is cancelled.");
}
}
/**
* Compact the database.
* This method removes all deleted chunks that are not used in the database.
* Make sure all devices are synchronized before running this method.
*/
async markUnusedChunks() {
if (!this.isAvailable()) return;
const { used, existing } = await this.allChunks();
const existChunks = [...existing];
const unusedChunks = existChunks.filter(([key, e]) => !used.has(e._id)).map(([key, e]) => e);
const deleteChunks = unusedChunks.map((e) => ({
...e,
_deleted: true,
}));
const size = deleteChunks.reduce((acc, e) => acc + e.data.length, 0);
const humanSize = sizeToHumanReadable(size);
if (deleteChunks.length == 0) {
this._notice("No unused chunks found.");
return;
}
const message = `We have following chunks that are not used from any files.
- Chunks: ${deleteChunks.length} (${humanSize})
Are you sure to mark these chunks to be deleted?
Note: **Make sure to synchronise all devices before deletion.**
> [!Note]
> This operation will not reduces the capacity of the remote until permanent deletion.`;
if (await this.confirm("Mark unused chunks", message, "Mark", "Cancel")) {
const result = await this.database.bulkDocs(deleteChunks);
this.clearHash();
this._notice(`Marked chunks: ${result.filter((e) => "ok" in e).length} / ${deleteChunks.length}`);
}
}
async removeUnusedChunks() {
const { used, existing } = await this.allChunks();
const existChunks = [...existing];
const unusedChunks = existChunks.filter(([key, e]) => !used.has(e._id)).map(([key, e]) => e);
const deleteChunks = unusedChunks.map((e) => ({
...e,
data: "",
_deleted: true,
}));
const size = unusedChunks.reduce((acc, e) => acc + e.data.length, 0);
const humanSize = sizeToHumanReadable(size);
if (deleteChunks.length == 0) {
this._notice("No unused chunks found.");
return;
}
const message = `We have following chunks that are not used from any files.
- Chunks: ${deleteChunks.length} (${humanSize})
Are you sure to delete these chunks?
Note: **Make sure to synchronise all devices before deletion.**
> [!Note]
> Chunks referenced from deleted files are not deleted. Please run "Commit File Deletion" before this operation.`;
if (await this.confirm("Mark unused chunks", message, "Mark", "Cancel")) {
const result = await this.database.bulkDocs(deleteChunks);
this._notice(`Deleted chunks: ${result.filter((e) => "ok" in e).length} / ${deleteChunks.length}`);
this.clearHash();
}
}
}

View File

@@ -0,0 +1,179 @@
import type { IObsidianModule } from "../../modules/AbstractObsidianModule";
import { P2PReplicatorPaneView, VIEW_TYPE_P2P } from "./P2PReplicator/P2PReplicatorPaneView.ts";
import {
AutoAccepting,
LOG_LEVEL_NOTICE,
REMOTE_P2P,
type EntryDoc,
type P2PSyncSetting,
type RemoteDBSettings,
} from "../../lib/src/common/types.ts";
import { LiveSyncCommands } from "../LiveSyncCommands.ts";
import { LiveSyncTrysteroReplicator } from "../../lib/src/replication/trystero/LiveSyncTrysteroReplicator.ts";
import { EVENT_REQUEST_OPEN_P2P, eventHub } from "../../common/events.ts";
import type { LiveSyncAbstractReplicator } from "../../lib/src/replication/LiveSyncAbstractReplicator.ts";
import { Logger } from "octagonal-wheels/common/logger";
import type { CommandShim } from "../../lib/src/replication/trystero/P2PReplicatorPaneCommon.ts";
import {
P2PReplicatorMixIn,
removeP2PReplicatorInstance,
type P2PReplicatorBase,
} from "../../lib/src/replication/trystero/P2PReplicatorCore.ts";
import { reactiveSource } from "octagonal-wheels/dataobject/reactive_v2";
import type { Confirm } from "../../lib/src/interfaces/Confirm.ts";
import type ObsidianLiveSyncPlugin from "../../main.ts";
import type { SimpleStore } from "octagonal-wheels/databases/SimpleStoreBase";
import { getPlatformName } from "../../lib/src/PlatformAPIs/obsidian/Environment.ts";
class P2PReplicatorCommandBase extends LiveSyncCommands implements P2PReplicatorBase {
storeP2PStatusLine = reactiveSource("");
getSettings(): P2PSyncSetting {
return this.plugin.settings;
}
get settings() {
return this.plugin.settings;
}
getDB() {
return this.plugin.localDatabase.localDatabase;
}
get confirm(): Confirm {
return this.plugin.confirm;
}
_simpleStore!: SimpleStore<any>;
simpleStore(): SimpleStore<any> {
return this._simpleStore;
}
constructor(plugin: ObsidianLiveSyncPlugin) {
super(plugin);
}
async handleReplicatedDocuments(docs: EntryDoc[]): Promise<void> {
// console.log("Processing Replicated Docs", docs);
return await this.plugin.$$parseReplicationResult(docs as PouchDB.Core.ExistingDocument<EntryDoc>[]);
}
onunload(): void {
throw new Error("Method not implemented.");
}
onload(): void | Promise<void> {
throw new Error("Method not implemented.");
}
init() {
this._simpleStore = this.plugin.$$getSimpleStore("p2p-sync");
return Promise.resolve(this);
}
}
export class P2PReplicator
extends P2PReplicatorMixIn(P2PReplicatorCommandBase)
implements IObsidianModule, CommandShim
{
storeP2PStatusLine = reactiveSource("");
$anyNewReplicator(settingOverride: Partial<RemoteDBSettings> = {}): Promise<LiveSyncAbstractReplicator> {
const settings = { ...this.settings, ...settingOverride };
if (settings.remoteType == REMOTE_P2P) {
return Promise.resolve(new LiveSyncTrysteroReplicator(this.plugin));
}
return undefined!;
}
override getPlatform(): string {
return getPlatformName();
}
override onunload(): void {
removeP2PReplicatorInstance();
void this.close();
}
override onload(): void | Promise<void> {
eventHub.onEvent(EVENT_REQUEST_OPEN_P2P, () => {
void this.openPane();
});
this.p2pLogCollector.p2pReplicationLine.onChanged((line) => {
this.storeP2PStatusLine.value = line.value;
});
}
async $everyOnInitializeDatabase(): Promise<boolean> {
await this.initialiseP2PReplicator();
return Promise.resolve(true);
}
async $allSuspendExtraSync() {
this.plugin.settings.P2P_Enabled = false;
this.plugin.settings.P2P_AutoAccepting = AutoAccepting.NONE;
this.plugin.settings.P2P_AutoBroadcast = false;
this.plugin.settings.P2P_AutoStart = false;
this.plugin.settings.P2P_AutoSyncPeers = "";
this.plugin.settings.P2P_AutoWatchPeers = "";
return await Promise.resolve(true);
}
async $everyOnLoadStart() {
return await Promise.resolve();
}
async openPane() {
await this.plugin.$$showView(VIEW_TYPE_P2P);
}
async $everyOnloadStart(): Promise<boolean> {
this.plugin.registerView(VIEW_TYPE_P2P, (leaf) => new P2PReplicatorPaneView(leaf, this.plugin));
this.plugin.addCommand({
id: "open-p2p-replicator",
name: "P2P Sync : Open P2P Replicator",
callback: async () => {
await this.openPane();
},
});
this.plugin.addCommand({
id: "p2p-establish-connection",
name: "P2P Sync : Connect to the Signalling Server",
checkCallback: (isChecking) => {
if (isChecking) {
return !(this._replicatorInstance?.server?.isServing ?? false);
}
void this.open();
},
});
this.plugin.addCommand({
id: "p2p-close-connection",
name: "P2P Sync : Disconnect from the Signalling Server",
checkCallback: (isChecking) => {
if (isChecking) {
return this._replicatorInstance?.server?.isServing ?? false;
}
Logger(`Closing P2P Connection`, LOG_LEVEL_NOTICE);
void this.close();
},
});
this.plugin.addCommand({
id: "replicate-now-by-p2p",
name: "Replicate now by P2P",
checkCallback: (isChecking) => {
if (isChecking) {
if (this.settings.remoteType == REMOTE_P2P) return false;
if (!this._replicatorInstance?.server?.isServing) return false;
return true;
}
void this._replicatorInstance?.replicateFromCommand(false);
},
});
this.plugin
.addRibbonIcon("waypoints", "P2P Replicator", async () => {
await this.openPane();
})
.addClass("livesync-ribbon-replicate-p2p");
return await Promise.resolve(true);
}
$everyAfterResumeProcess(): Promise<boolean> {
if (this.settings.P2P_Enabled && this.settings.P2P_AutoStart) {
setTimeout(() => void this.open(), 100);
}
return Promise.resolve(true);
}
}

View File

@@ -0,0 +1,481 @@
<script lang="ts">
import { onMount, setContext } from "svelte";
import { AutoAccepting, DEFAULT_SETTINGS, type P2PSyncSetting } from "../../../lib/src/common/types";
import {
AcceptedStatus,
ConnectionStatus,
type CommandShim,
type PeerStatus,
type PluginShim,
} from "../../../lib/src/replication/trystero/P2PReplicatorPaneCommon";
import PeerStatusRow from "../P2PReplicator/PeerStatusRow.svelte";
import { EVENT_LAYOUT_READY, eventHub } from "../../../common/events";
import {
type PeerInfo,
type P2PServerInfo,
EVENT_SERVER_STATUS,
EVENT_REQUEST_STATUS,
EVENT_P2P_REPLICATOR_STATUS,
} from "../../../lib/src/replication/trystero/TrysteroReplicatorP2PServer";
import { type P2PReplicatorStatus } from "../../../lib/src/replication/trystero/TrysteroReplicator";
import { $msg as _msg } from "../../../lib/src/common/i18n";
interface Props {
plugin: PluginShim;
cmdSync: CommandShim;
}
let { plugin, cmdSync }: Props = $props();
// const cmdSync = plugin.getAddOn<P2PReplicator>("P2PReplicator")!;
setContext("getReplicator", () => cmdSync);
const initialSettings = { ...plugin.settings };
let settings = $state<P2PSyncSetting>(initialSettings);
// const vaultName = plugin.$$getVaultName();
// const dbKey = `${vaultName}-p2p-device-name`;
const initialDeviceName = cmdSync.getConfig("p2p_device_name") ?? plugin.$$getVaultName();
let deviceName = $state<string>(initialDeviceName);
let eP2PEnabled = $state<boolean>(initialSettings.P2P_Enabled);
let eRelay = $state<string>(initialSettings.P2P_relays);
let eRoomId = $state<string>(initialSettings.P2P_roomID);
let ePassword = $state<string>(initialSettings.P2P_passphrase);
let eAppId = $state<string>(initialSettings.P2P_AppID);
let eDeviceName = $state<string>(initialDeviceName);
let eAutoAccept = $state<boolean>(initialSettings.P2P_AutoAccepting == AutoAccepting.ALL);
let eAutoStart = $state<boolean>(initialSettings.P2P_AutoStart);
let eAutoBroadcast = $state<boolean>(initialSettings.P2P_AutoBroadcast);
const isP2PEnabledModified = $derived.by(() => eP2PEnabled !== settings.P2P_Enabled);
const isRelayModified = $derived.by(() => eRelay !== settings.P2P_relays);
const isRoomIdModified = $derived.by(() => eRoomId !== settings.P2P_roomID);
const isPasswordModified = $derived.by(() => ePassword !== settings.P2P_passphrase);
const isAppIdModified = $derived.by(() => eAppId !== settings.P2P_AppID);
const isDeviceNameModified = $derived.by(() => eDeviceName !== deviceName);
const isAutoAcceptModified = $derived.by(() => eAutoAccept !== (settings.P2P_AutoAccepting == AutoAccepting.ALL));
const isAutoStartModified = $derived.by(() => eAutoStart !== settings.P2P_AutoStart);
const isAutoBroadcastModified = $derived.by(() => eAutoBroadcast !== settings.P2P_AutoBroadcast);
const isAnyModified = $derived.by(
() =>
isP2PEnabledModified ||
isRelayModified ||
isRoomIdModified ||
isPasswordModified ||
isAppIdModified ||
isDeviceNameModified ||
isAutoAcceptModified ||
isAutoStartModified ||
isAutoBroadcastModified
);
async function saveAndApply() {
const newSettings = {
...plugin.settings,
P2P_Enabled: eP2PEnabled,
P2P_relays: eRelay,
P2P_roomID: eRoomId,
P2P_passphrase: ePassword,
P2P_AppID: eAppId,
P2P_AutoAccepting: eAutoAccept ? AutoAccepting.ALL : AutoAccepting.NONE,
P2P_AutoStart: eAutoStart,
P2P_AutoBroadcast: eAutoBroadcast,
};
plugin.settings = newSettings;
cmdSync.setConfig("p2p_device_name", eDeviceName);
deviceName = eDeviceName;
await plugin.saveSettings();
}
async function revert() {
eP2PEnabled = settings.P2P_Enabled;
eRelay = settings.P2P_relays;
eRoomId = settings.P2P_roomID;
ePassword = settings.P2P_passphrase;
eAppId = settings.P2P_AppID;
eAutoAccept = settings.P2P_AutoAccepting == AutoAccepting.ALL;
eAutoStart = settings.P2P_AutoStart;
eAutoBroadcast = settings.P2P_AutoBroadcast;
}
let serverInfo = $state<P2PServerInfo | undefined>(undefined);
let replicatorInfo = $state<P2PReplicatorStatus | undefined>(undefined);
const applyLoadSettings = (d: P2PSyncSetting, force: boolean) => {
const { P2P_relays, P2P_roomID, P2P_passphrase, P2P_AppID, P2P_AutoAccepting } = d;
if (force || !isP2PEnabledModified) eP2PEnabled = d.P2P_Enabled;
if (force || !isRelayModified) eRelay = P2P_relays;
if (force || !isRoomIdModified) eRoomId = P2P_roomID;
if (force || !isPasswordModified) ePassword = P2P_passphrase;
if (force || !isAppIdModified) eAppId = P2P_AppID;
const newAutoAccept = P2P_AutoAccepting === AutoAccepting.ALL;
if (force || !isAutoAcceptModified) eAutoAccept = newAutoAccept;
if (force || !isAutoStartModified) eAutoStart = d.P2P_AutoStart;
if (force || !isAutoBroadcastModified) eAutoBroadcast = d.P2P_AutoBroadcast;
settings = d;
};
onMount(() => {
const r = eventHub.onEvent("setting-saved", async (d) => {
applyLoadSettings(d, false);
closeServer();
});
const rx = eventHub.onEvent(EVENT_LAYOUT_READY, () => {
applyLoadSettings(plugin.settings, true);
});
const r2 = eventHub.onEvent(EVENT_SERVER_STATUS, (status) => {
serverInfo = status;
advertisements = status?.knownAdvertisements ?? [];
});
const r3 = eventHub.onEvent(EVENT_P2P_REPLICATOR_STATUS, (status) => {
replicatorInfo = status;
});
eventHub.emitEvent(EVENT_REQUEST_STATUS);
return () => {
r();
r2();
r3();
};
});
let isConnected = $derived.by(() => {
return serverInfo?.isConnected ?? false;
});
let serverPeerId = $derived.by(() => {
return serverInfo?.serverPeerId ?? "";
});
let advertisements = $state<PeerInfo[]>([]);
let autoSyncPeers = $derived.by(() =>
settings.P2P_AutoSyncPeers.split(",")
.map((e) => e.trim())
.filter((e) => e)
);
let autoWatchPeers = $derived.by(() =>
settings.P2P_AutoWatchPeers.split(",")
.map((e) => e.trim())
.filter((e) => e)
);
let syncOnCommand = $derived.by(() =>
settings.P2P_SyncOnReplication.split(",")
.map((e) => e.trim())
.filter((e) => e)
);
const peers = $derived.by(() =>
advertisements.map((ad) => {
let accepted: AcceptedStatus;
const isTemporaryAccepted = ad.isTemporaryAccepted;
if (isTemporaryAccepted === undefined) {
if (ad.isAccepted === undefined) {
accepted = AcceptedStatus.UNKNOWN;
} else {
accepted = ad.isAccepted ? AcceptedStatus.ACCEPTED : AcceptedStatus.DENIED;
}
} else if (isTemporaryAccepted === true) {
accepted = AcceptedStatus.ACCEPTED_IN_SESSION;
} else {
accepted = AcceptedStatus.DENIED_IN_SESSION;
}
const isFetching = replicatorInfo?.replicatingFrom.indexOf(ad.peerId) !== -1;
const isSending = replicatorInfo?.replicatingTo.indexOf(ad.peerId) !== -1;
const isWatching = replicatorInfo?.watchingPeers.indexOf(ad.peerId) !== -1;
const syncOnStart = autoSyncPeers.indexOf(ad.name) !== -1;
const watchOnStart = autoWatchPeers.indexOf(ad.name) !== -1;
const syncOnReplicationCommand = syncOnCommand.indexOf(ad.name) !== -1;
const st: PeerStatus = {
name: ad.name,
peerId: ad.peerId,
accepted: accepted,
status: ad.isAccepted ? ConnectionStatus.CONNECTED : ConnectionStatus.DISCONNECTED,
isSending: isSending,
isFetching: isFetching,
isWatching: isWatching,
syncOnConnect: syncOnStart,
watchOnConnect: watchOnStart,
syncOnReplicationCommand: syncOnReplicationCommand,
};
return st;
})
);
function useDefaultRelay() {
eRelay = DEFAULT_SETTINGS.P2P_relays;
}
function _generateRandom() {
return (Math.floor(Math.random() * 1000) + 1000).toString().substring(1);
}
function generateRandom(length: number) {
let buf = "";
while (buf.length < length) {
buf += "-" + _generateRandom();
}
return buf.substring(1, length);
}
function chooseRandom() {
eRoomId = generateRandom(12) + "-" + Math.random().toString(36).substring(2, 5);
}
async function openServer() {
await cmdSync.open();
}
async function closeServer() {
await cmdSync.close();
}
function startBroadcasting() {
void cmdSync.enableBroadcastCastings();
}
function stopBroadcasting() {
void cmdSync.disableBroadcastCastings();
}
const initialDialogStatusKey = `p2p-dialog-status`;
const getDialogStatus = () => {
try {
const initialDialogStatus = JSON.parse(cmdSync.getConfig(initialDialogStatusKey) ?? "{}") as {
notice?: boolean;
setting?: boolean;
};
return initialDialogStatus;
} catch (e) {
return {};
}
};
const initialDialogStatus = getDialogStatus();
let isNoticeOpened = $state<boolean>(initialDialogStatus.notice ?? true);
let isSettingOpened = $state<boolean>(initialDialogStatus.setting ?? true);
$effect(() => {
const dialogStatus = {
notice: isNoticeOpened,
setting: isSettingOpened,
};
cmdSync.setConfig(initialDialogStatusKey, JSON.stringify(dialogStatus));
});
</script>
<article>
<h1>Peer to Peer Replicator</h1>
<details bind:open={isNoticeOpened}>
<summary>{_msg("P2P.Note.Summary")}</summary>
<p class="important">{_msg("P2P.Note.important_note")}</p>
<p class="important-sub">
{_msg("P2P.Note.important_note_sub")}
</p>
{#each _msg("P2P.Note.description").split("\n\n") as paragraph}
<p>{paragraph}</p>
{/each}
</details>
<h2>Connection Settings</h2>
<details bind:open={isSettingOpened}>
<summary>{eRelay}</summary>
<table class="settings">
<tbody>
<tr>
<th> Enable P2P Replicator </th>
<td>
<label class={{ "is-dirty": isP2PEnabledModified }}>
<input type="checkbox" bind:checked={eP2PEnabled} />
</label>
</td>
</tr><tr>
<th> Relay settings </th>
<td>
<label class={{ "is-dirty": isRelayModified }}>
<input
type="text"
placeholder="wss://exp-relay.vrtmrz.net, wss://xxxxx"
bind:value={eRelay}
autocomplete="off"
/>
<button onclick={() => useDefaultRelay()}> Use vrtmrz's relay </button>
</label>
</td>
</tr>
<tr>
<th> Room ID </th>
<td>
<label class={{ "is-dirty": isRoomIdModified }}>
<input
type="text"
placeholder="anything-you-like"
bind:value={eRoomId}
autocomplete="off"
spellcheck="false"
autocorrect="off"
/>
<button onclick={() => chooseRandom()}> Use Random Number </button>
</label>
<span>
<small>
This can isolate your connections between devices. Use the same Room ID for the same
devices.</small
>
</span>
</td>
</tr>
<tr>
<th> Password </th>
<td>
<label class={{ "is-dirty": isPasswordModified }}>
<input type="password" placeholder="password" bind:value={ePassword} />
</label>
<span>
<small> This password is used to encrypt the connection. Use something long enough. </small>
</span>
</td>
</tr>
<tr>
<th> This device name </th>
<td>
<label class={{ "is-dirty": isDeviceNameModified }}>
<input type="text" placeholder="iphone-16" bind:value={eDeviceName} autocomplete="off" />
</label>
<span>
<small>
Device name to identify the device. Please use shorter one for the stable peer
detection, i.e., "iphone-16" or "macbook-2021".
</small>
</span>
</td>
</tr>
<tr>
<th> Auto Connect </th>
<td>
<label class={{ "is-dirty": isAutoStartModified }}>
<input type="checkbox" bind:checked={eAutoStart} />
</label>
</td>
</tr>
<tr>
<th> Start change-broadcasting on Connect </th>
<td>
<label class={{ "is-dirty": isAutoBroadcastModified }}>
<input type="checkbox" bind:checked={eAutoBroadcast} />
</label>
</td>
</tr>
<!-- <tr>
<th> Auto Accepting </th>
<td>
<label class={{ "is-dirty": isAutoAcceptModified }}>
<input type="checkbox" bind:checked={eAutoAccept} />
</label>
</td>
</tr> -->
</tbody>
</table>
<button disabled={!isAnyModified} class="button mod-cta" onclick={saveAndApply}>Save and Apply</button>
<button disabled={!isAnyModified} class="button" onclick={revert}>Revert changes</button>
</details>
<div>
<h2>Signaling Server Connection</h2>
<div>
{#if !isConnected}
<p>No Connection</p>
{:else}
<p>Connected to Signaling Server (as Peer ID: {serverPeerId})</p>
{/if}
</div>
<div>
{#if !isConnected}
<button onclick={openServer}>Connect</button>
{:else}
<button onclick={closeServer}>Disconnect</button>
{#if replicatorInfo?.isBroadcasting !== undefined}
{#if replicatorInfo?.isBroadcasting}
<button onclick={stopBroadcasting}>Stop Broadcasting</button>
{:else}
<button onclick={startBroadcasting}>Start Broadcasting</button>
{/if}
{/if}
<details>
<summary>Broadcasting?</summary>
<p>
<small>
If you want to use `LiveSync`, you should broadcast changes. All `watching` peers which
detects this will start the replication for fetching. <br />
However, This should not be enabled if you want to increase your secrecy more.
</small>
</p>
</details>
{/if}
</div>
</div>
<div>
<h2>Peers</h2>
<table class="peers">
<thead>
<tr>
<th>Name</th>
<th>Action</th>
<th>Command</th>
</tr>
</thead>
<tbody>
{#each peers as peer}
<PeerStatusRow peerStatus={peer}></PeerStatusRow>
{/each}
</tbody>
</table>
</div>
</article>
<style>
article {
max-width: 100%;
}
article p {
user-select: text;
-webkit-user-select: text;
}
h2 {
margin-top: var(--size-4-1);
margin-bottom: var(--size-4-1);
padding-bottom: var(--size-4-1);
border-bottom: 1px solid var(--background-modifier-border);
}
label.is-dirty {
background-color: var(--background-modifier-error);
}
input {
background-color: transparent;
}
th {
/* display: flex;
justify-content: center;
align-items: center; */
min-height: var(--input-height);
}
td {
min-height: var(--input-height);
}
td > label {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
min-height: var(--input-height);
}
td > label > * {
margin: auto var(--size-4-1);
}
table.peers {
width: 100%;
}
.important {
color: var(--text-error);
font-size: 1.2em;
font-weight: bold;
}
.important-sub {
color: var(--text-warning);
}
.settings label {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
flex-wrap: wrap;
}
</style>

View File

@@ -0,0 +1,198 @@
import { Menu, WorkspaceLeaf } from "obsidian";
import ReplicatorPaneComponent from "./P2PReplicatorPane.svelte";
import type ObsidianLiveSyncPlugin from "../../../main.ts";
import { mount } from "svelte";
import { SvelteItemView } from "../../../common/SvelteItemView.ts";
import { eventHub } from "../../../common/events.ts";
import { unique } from "octagonal-wheels/collection";
import { LOG_LEVEL_NOTICE, REMOTE_P2P } from "../../../lib/src/common/types.ts";
import { Logger } from "../../../lib/src/common/logger.ts";
import { P2PReplicator } from "../CmdP2PReplicator.ts";
import {
EVENT_P2P_PEER_SHOW_EXTRA_MENU,
type PeerStatus,
} from "../../../lib/src/replication/trystero/P2PReplicatorPaneCommon.ts";
export const VIEW_TYPE_P2P = "p2p-replicator";
function addToList(item: string, list: string) {
return unique(
list
.split(",")
.map((e) => e.trim())
.concat(item)
.filter((p) => p)
).join(",");
}
function removeFromList(item: string, list: string) {
return list
.split(",")
.map((e) => e.trim())
.filter((p) => p !== item)
.filter((p) => p)
.join(",");
}
export class P2PReplicatorPaneView extends SvelteItemView {
plugin: ObsidianLiveSyncPlugin;
icon = "waypoints";
title: string = "";
navigation = false;
getIcon(): string {
return "waypoints";
}
get replicator() {
const r = this.plugin.getAddOn<P2PReplicator>(P2PReplicator.name);
if (!r || !r._replicatorInstance) {
throw new Error("Replicator not found");
}
return r._replicatorInstance;
}
async replicateFrom(peer: PeerStatus) {
await this.replicator.replicateFrom(peer.peerId);
}
async replicateTo(peer: PeerStatus) {
await this.replicator.requestSynchroniseToPeer(peer.peerId);
}
async getRemoteConfig(peer: PeerStatus) {
Logger(
`Requesting remote config for ${peer.name}. Please input the passphrase on the remote device`,
LOG_LEVEL_NOTICE
);
const remoteConfig = await this.replicator.getRemoteConfig(peer.peerId);
if (remoteConfig) {
Logger(`Remote config for ${peer.name} is retrieved successfully`);
const DROP = "Yes, and drop local database";
const KEEP = "Yes, but keep local database";
const CANCEL = "No, cancel";
const yn = await this.plugin.confirm.askSelectStringDialogue(
`Do you really want to apply the remote config? This will overwrite your current config immediately and restart.
And you can also drop the local database to rebuild from the remote device.`,
[DROP, KEEP, CANCEL] as const,
{
defaultAction: CANCEL,
title: "Apply Remote Config ",
}
);
if (yn === DROP || yn === KEEP) {
if (yn === DROP) {
if (remoteConfig.remoteType !== REMOTE_P2P) {
const yn2 = await this.plugin.confirm.askYesNoDialog(
`Do you want to set the remote type to "P2P Sync" to rebuild by "P2P replication"?`,
{
title: "Rebuild from remote device",
}
);
if (yn2 === "yes") {
remoteConfig.remoteType = REMOTE_P2P;
remoteConfig.P2P_RebuildFrom = peer.name;
}
}
}
this.plugin.settings = remoteConfig;
await this.plugin.saveSettings();
if (yn === DROP) {
await this.plugin.rebuilder.scheduleFetch();
} else {
await this.plugin.$$scheduleAppReload();
}
} else {
Logger(`Cancelled\nRemote config for ${peer.name} is not applied`, LOG_LEVEL_NOTICE);
}
} else {
Logger(`Cannot retrieve remote config for ${peer.peerId}`);
}
}
async toggleProp(peer: PeerStatus, prop: "syncOnConnect" | "watchOnConnect" | "syncOnReplicationCommand") {
const settingMap = {
syncOnConnect: "P2P_AutoSyncPeers",
watchOnConnect: "P2P_AutoWatchPeers",
syncOnReplicationCommand: "P2P_SyncOnReplication",
} as const;
const targetSetting = settingMap[prop];
if (peer[prop]) {
this.plugin.settings[targetSetting] = removeFromList(peer.name, this.plugin.settings[targetSetting]);
await this.plugin.saveSettings();
} else {
this.plugin.settings[targetSetting] = addToList(peer.name, this.plugin.settings[targetSetting]);
await this.plugin.saveSettings();
}
await this.plugin.saveSettings();
}
m?: Menu;
constructor(leaf: WorkspaceLeaf, plugin: ObsidianLiveSyncPlugin) {
super(leaf);
this.plugin = plugin;
eventHub.onEvent(EVENT_P2P_PEER_SHOW_EXTRA_MENU, ({ peer, event }) => {
if (this.m) {
this.m.hide();
}
this.m = new Menu()
.addItem((item) => item.setTitle("📥 Only Fetch").onClick(() => this.replicateFrom(peer)))
.addItem((item) => item.setTitle("📤 Only Send").onClick(() => this.replicateTo(peer)))
.addSeparator()
.addItem((item) => {
item.setTitle("🔧 Get Configuration").onClick(async () => {
await this.getRemoteConfig(peer);
});
})
.addSeparator()
.addItem((item) => {
const mark = peer.syncOnConnect ? "checkmark" : null;
item.setTitle("Toggle Sync on connect")
.onClick(async () => {
await this.toggleProp(peer, "syncOnConnect");
})
.setIcon(mark);
})
.addItem((item) => {
const mark = peer.watchOnConnect ? "checkmark" : null;
item.setTitle("Toggle Watch on connect")
.onClick(async () => {
await this.toggleProp(peer, "watchOnConnect");
})
.setIcon(mark);
})
.addItem((item) => {
const mark = peer.syncOnReplicationCommand ? "checkmark" : null;
item.setTitle("Toggle Sync on `Replicate now` command")
.onClick(async () => {
await this.toggleProp(peer, "syncOnReplicationCommand");
})
.setIcon(mark);
});
this.m.showAtPosition({ x: event.x, y: event.y });
});
}
getViewType() {
return VIEW_TYPE_P2P;
}
getDisplayText() {
return "Peer-to-Peer Replicator";
}
override async onClose(): Promise<void> {
await super.onClose();
if (this.m) {
this.m.hide();
}
}
instantiateComponent(target: HTMLElement) {
const cmdSync = this.plugin.getAddOn<P2PReplicator>(P2PReplicator.name);
if (!cmdSync) {
throw new Error("Replicator not found");
}
return mount(ReplicatorPaneComponent, {
target: target,
props: {
plugin: cmdSync.plugin,
cmdSync: cmdSync,
},
});
}
}

View File

@@ -0,0 +1,259 @@
<script lang="ts">
import { getContext } from "svelte";
import { AcceptedStatus, type PeerStatus } from "../../../lib/src/replication/trystero/P2PReplicatorPaneCommon";
import type { P2PReplicator } from "../CmdP2PReplicator";
import { eventHub } from "../../../common/events";
import { EVENT_P2P_PEER_SHOW_EXTRA_MENU } from "../../../lib/src/replication/trystero/P2PReplicatorPaneCommon";
interface Props {
peerStatus: PeerStatus;
}
let { peerStatus }: Props = $props();
let peer = $derived(peerStatus);
function select<T extends string | number | symbol, U>(d: T, cond: Record<T, U>): U;
function select<T extends string | number | symbol, U, V>(d: T, cond: Record<T, U>, def: V): U | V;
function select<T extends string | number | symbol, U>(d: T, cond: Record<T, U>, def?: U): U | undefined {
return d in cond ? cond[d] : def;
}
let statusChips = $derived.by(() =>
[
peer.isWatching ? ["WATCHING"] : [],
peer.isFetching ? ["FETCHING"] : [],
peer.isSending ? ["SENDING"] : [],
].flat()
);
let acceptedStatusChip = $derived.by(() =>
select(
peer.accepted.toString(),
{
[AcceptedStatus.ACCEPTED]: "ACCEPTED",
[AcceptedStatus.ACCEPTED_IN_SESSION]: "ACCEPTED (in session)",
[AcceptedStatus.DENIED_IN_SESSION]: "DENIED (in session)",
[AcceptedStatus.DENIED]: "DENIED",
[AcceptedStatus.UNKNOWN]: "NEW",
},
""
)
);
const classList = {
["SENDING"]: "connected",
["FETCHING"]: "connected",
["WATCHING"]: "connected-live",
["WAITING"]: "waiting",
["ACCEPTED"]: "accepted",
["DENIED"]: "denied",
["NEW"]: "unknown",
};
let isAccepted = $derived.by(
() => peer.accepted === AcceptedStatus.ACCEPTED || peer.accepted === AcceptedStatus.ACCEPTED_IN_SESSION
);
let isDenied = $derived.by(
() => peer.accepted === AcceptedStatus.DENIED || peer.accepted === AcceptedStatus.DENIED_IN_SESSION
);
let isNew = $derived.by(() => peer.accepted === AcceptedStatus.UNKNOWN);
function makeDecision(isAccepted: boolean, isTemporary: boolean) {
cmdReplicator._replicatorInstance?.server?.makeDecision({
peerId: peer.peerId,
name: peer.name,
decision: isAccepted,
isTemporary: isTemporary,
});
}
function revokeDecision() {
cmdReplicator._replicatorInstance?.server?.revokeDecision({
peerId: peer.peerId,
name: peer.name,
});
}
const cmdReplicator = getContext<() => P2PReplicator>("getReplicator")();
const replicator = cmdReplicator._replicatorInstance!;
const peerAttrLabels = $derived.by(() => {
const attrs = [];
if (peer.syncOnConnect) {
attrs.push("✔ SYNC");
}
if (peer.watchOnConnect) {
attrs.push("✔ WATCH");
}
if (peer.syncOnReplicationCommand) {
attrs.push("✔ SELECT");
}
return attrs;
});
function startWatching() {
replicator.watchPeer(peer.peerId);
}
function stopWatching() {
replicator.unwatchPeer(peer.peerId);
}
function sync() {
replicator.sync(peer.peerId, false);
}
function moreMenu(evt: MouseEvent) {
eventHub.emitEvent(EVENT_P2P_PEER_SHOW_EXTRA_MENU, { peer, event: evt });
}
</script>
<tr>
<td>
<div class="info">
<div class="row name">
<span class="peername">{peer.name}</span>
</div>
<div class="row peer-id">
<span class="peerid">({peer.peerId})</span>
</div>
</div>
<div class="status-chips">
<div class="row">
<span class="chip {select(acceptedStatusChip, classList)}">{acceptedStatusChip}</span>
</div>
{#if isAccepted}
<div class="row">
{#each statusChips as chip}
<span class="chip {select(chip, classList)}">{chip}</span>
{/each}
</div>
{/if}
<div class="row">
{#each peerAttrLabels as attr}
<span class="chip attr">{attr}</span>
{/each}
</div>
</div>
</td>
<td>
<div class="buttons">
<div class="row">
{#if isNew}
{#if !isAccepted}
<button class="button" onclick={() => makeDecision(true, true)}>Accept in session</button>
<button class="button mod-cta" onclick={() => makeDecision(true, false)}>Accept</button>
{/if}
{#if !isDenied}
<button class="button" onclick={() => makeDecision(false, true)}>Deny in session</button>
<button class="button mod-warning" onclick={() => makeDecision(false, false)}>Deny</button>
{/if}
{:else}
<button class="button mod-warning" onclick={() => revokeDecision()}>Revoke</button>
{/if}
</div>
</div>
</td>
<td>
{#if isAccepted}
<div class="buttons">
<div class="row">
<button class="button" onclick={sync} disabled={peer.isSending || peer.isFetching}>🔄</button>
<!-- <button class="button" onclick={replicateFrom} disabled={peer.isFetching}>📥</button>
<button class="button" onclick={replicateTo} disabled={peer.isSending}>📤</button> -->
{#if peer.isWatching}
<button class="button" onclick={stopWatching}>Stop ⚡</button>
{:else}
<button class="button" onclick={startWatching} title="live"></button>
{/if}
<button class="button" onclick={moreMenu}>...</button>
</div>
</div>
{/if}
</td>
</tr>
<style>
tr:nth-child(odd) {
background-color: var(--background-primary-alt);
}
.info {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: var(--size-4-1) var(--size-4-1);
}
.peer-id {
font-size: 0.8em;
}
.status-chips {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
/* min-width: 10em; */
}
.buttons {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.buttons .row {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
/* padding: var(--size-4-1) var(--size-4-1); */
}
.chip {
display: inline-block;
padding: 4px 8px;
margin: 4px;
border-radius: 4px;
font-size: 0.75em;
font-weight: bold;
background-color: var(--tag-background);
border: var(--tag-border-width) solid var(--tag-border-color);
}
.chip.connected {
background-color: var(--background-modifier-success);
color: var(--text-normal);
}
.chip.connected-live {
background-color: var(--background-modifier-success);
border-color: var(--background-modifier-success);
color: var(--text-normal);
}
.chip.accepted {
background-color: var(--background-modifier-success);
color: var(--text-normal);
}
.chip.waiting {
background-color: var(--background-secondary);
}
.chip.unknown {
background-color: var(--background-primary);
color: var(--text-warning);
}
.chip.denied {
background-color: var(--background-modifier-error);
color: var(--text-error);
}
.chip.attr {
background-color: var(--background-secondary);
}
.button {
margin: var(--size-4-1);
}
.button.affirmative {
background-color: var(--interactive-accent);
color: var(--text-normal);
}
.button.affirmative:hover {
background-color: var(--interactive-accent-hover);
}
.button.negative {
background-color: var(--background-modifier-error);
color: var(--text-error);
}
.button.negative:hover {
background-color: var(--background-modifier-error-hover);
}
</style>

Submodule src/lib updated: 5079b0bf79...ab02f72aa5

View File

@@ -1,25 +1,41 @@
import { Plugin } from "./deps"; import { Plugin } from "./deps";
import { type EntryDoc, type LoadedEntry, type ObsidianLiveSyncSettings, type LOG_LEVEL, VER, DEFAULT_SETTINGS, type diff_result, type DatabaseConnectingStatus, type EntryHasPath, type DocumentID, type FilePathWithPrefix, type FilePath, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE, type HasSettings, type MetaEntry, type UXFileInfoStub, type MISSING_OR_ERROR, type AUTO_MERGED, } from "./lib/src/common/types.ts"; import {
type EntryDoc,
type LoadedEntry,
type ObsidianLiveSyncSettings,
type LOG_LEVEL,
type diff_result,
type DatabaseConnectingStatus,
type EntryHasPath,
type DocumentID,
type FilePathWithPrefix,
type FilePath,
LOG_LEVEL_INFO,
type HasSettings,
type MetaEntry,
type UXFileInfoStub,
type MISSING_OR_ERROR,
type AUTO_MERGED,
type RemoteDBSettings,
type TweakValues,
type CouchDBCredentials,
} from "./lib/src/common/types.ts";
import { type FileEventItem } from "./common/types.ts"; import { type FileEventItem } from "./common/types.ts";
import { fireAndForget, type SimpleStore } from "./lib/src/common/utils.ts"; import { type SimpleStore } from "./lib/src/common/utils.ts";
import { Logger } from "./lib/src/common/logger.ts";
import { cancelAllPeriodicTask, cancelAllTasks } from "./common/utils.ts";
import { versionNumberString2Number } from "./lib/src/string_and_binary/convert.ts";
import { LiveSyncLocalDB, type LiveSyncLocalDBEnv } from "./lib/src/pouchdb/LiveSyncLocalDB.ts"; import { LiveSyncLocalDB, type LiveSyncLocalDBEnv } from "./lib/src/pouchdb/LiveSyncLocalDB.ts";
import { LiveSyncAbstractReplicator, type LiveSyncReplicatorEnv } from "./lib/src/replication/LiveSyncAbstractReplicator.js"; import {
LiveSyncAbstractReplicator,
type LiveSyncReplicatorEnv,
} from "./lib/src/replication/LiveSyncAbstractReplicator.js";
import { type KeyValueDatabase } from "./common/KeyValueDB.ts"; import { type KeyValueDatabase } from "./common/KeyValueDB.ts";
import { LiveSyncCommands } from "./features/LiveSyncCommands.ts"; import { LiveSyncCommands } from "./features/LiveSyncCommands.ts";
import { HiddenFileSync } from "./features/HiddenFileSync/CmdHiddenFileSync.ts"; import { HiddenFileSync } from "./features/HiddenFileSync/CmdHiddenFileSync.ts";
import { ConfigSync } from "./features/ConfigSync/CmdConfigSync.ts"; import { ConfigSync } from "./features/ConfigSync/CmdConfigSync.ts";
import { stopAllRunningProcessors } from "./lib/src/concurrency/processor.js";
import { reactiveSource, type ReactiveValue } from "./lib/src/dataobject/reactive.js"; import { reactiveSource, type ReactiveValue } from "./lib/src/dataobject/reactive.js";
import { type LiveSyncJournalReplicatorEnv } from "./lib/src/replication/journal/LiveSyncJournalReplicator.js"; import { type LiveSyncJournalReplicatorEnv } from "./lib/src/replication/journal/LiveSyncJournalReplicator.js";
import { type LiveSyncCouchDBReplicatorEnv } from "./lib/src/replication/couchdb/LiveSyncReplicator.js"; import { type LiveSyncCouchDBReplicatorEnv } from "./lib/src/replication/couchdb/LiveSyncReplicator.js";
import type { CheckPointInfo } from "./lib/src/replication/journal/JournalSyncTypes.js"; import type { CheckPointInfo } from "./lib/src/replication/journal/JournalSyncTypes.js";
import { ObsHttpHandler } from "./modules/essentialObsidian/APILib/ObsHttpHandler.js"; import { ObsHttpHandler } from "./modules/essentialObsidian/APILib/ObsHttpHandler.js";
import { $f, setLang } from "./lib/src/common/i18n.ts";
import { eventHub } from "./lib/src/hub/hub.ts";
import { EVENT_LAYOUT_READY, EVENT_PLUGIN_LOADED, EVENT_PLUGIN_UNLOADED, EVENT_REQUEST_RELOAD_SETTING_TAB, EVENT_SETTING_SAVED } from "./common/events.ts";
import type { IObsidianModule } from "./modules/AbstractObsidianModule.ts"; import type { IObsidianModule } from "./modules/AbstractObsidianModule.ts";
import { ModuleDev } from "./modules/extras/ModuleDev.ts"; import { ModuleDev } from "./modules/extras/ModuleDev.ts";
@@ -36,7 +52,7 @@ import { ModuleRedFlag } from "./modules/coreFeatures/ModuleRedFlag.ts";
import { ModuleObsidianMenu } from "./modules/essentialObsidian/ModuleObsidianMenu.ts"; import { ModuleObsidianMenu } from "./modules/essentialObsidian/ModuleObsidianMenu.ts";
import { ModuleSetupObsidian } from "./modules/features/ModuleSetupObsidian.ts"; import { ModuleSetupObsidian } from "./modules/features/ModuleSetupObsidian.ts";
import type { StorageAccess } from "./modules/interfaces/StorageAccess.ts"; import type { StorageAccess } from "./modules/interfaces/StorageAccess.ts";
import type { Confirm } from "./modules/interfaces/Confirm.ts"; import type { Confirm } from "./lib/src/interfaces/Confirm.ts";
import type { Rebuilder } from "./modules/interfaces/DatabaseRebuilder.ts"; import type { Rebuilder } from "./modules/interfaces/DatabaseRebuilder.ts";
import type { DatabaseFileAccess } from "./modules/interfaces/DatabaseFileAccess.ts"; import type { DatabaseFileAccess } from "./modules/interfaces/DatabaseFileAccess.ts";
import { ModuleDatabaseFileAccess } from "./modules/core/ModuleDatabaseFileAccess.ts"; import { ModuleDatabaseFileAccess } from "./modules/core/ModuleDatabaseFileAccess.ts";
@@ -64,7 +80,10 @@ import { ModuleResolvingMismatchedTweaks } from "./modules/coreFeatures/ModuleRe
import { ModuleIntegratedTest } from "./modules/extras/ModuleIntegratedTest.ts"; import { ModuleIntegratedTest } from "./modules/extras/ModuleIntegratedTest.ts";
import { ModuleRebuilder } from "./modules/core/ModuleRebuilder.ts"; import { ModuleRebuilder } from "./modules/core/ModuleRebuilder.ts";
import { ModuleReplicateTest } from "./modules/extras/ModuleReplicateTest.ts"; import { ModuleReplicateTest } from "./modules/extras/ModuleReplicateTest.ts";
import { ModuleLiveSyncMain } from "./modules/main/ModuleLiveSyncMain.ts";
import { ModuleExtraSyncObsidian } from "./modules/extraFeaturesObsidian/ModuleExtraSyncObsidian.ts";
import { LocalDatabaseMaintenance } from "./features/LocalDatabaseMainte/CmdLocalDatabaseMainte.ts";
import { P2PReplicator } from "./features/P2PSync/CmdP2PReplicator.ts";
function throwShouldBeOverridden(): never { function throwShouldBeOverridden(): never {
throw new Error("This function should be overridden by the module."); throw new Error("This function should be overridden by the module.");
@@ -83,34 +102,15 @@ const InterceptiveAny = Promise.resolve(undefined);
* All of above performed on injectModules function. * All of above performed on injectModules function.
*/ */
export default class ObsidianLiveSyncPlugin extends Plugin implements LiveSyncLocalDBEnv, LiveSyncReplicatorEnv, LiveSyncJournalReplicatorEnv, LiveSyncCouchDBReplicatorEnv, HasSettings<ObsidianLiveSyncSettings> { export default class ObsidianLiveSyncPlugin
extends Plugin
_log = Logger; implements
settings!: ObsidianLiveSyncSettings; LiveSyncLocalDBEnv,
localDatabase!: LiveSyncLocalDB; LiveSyncReplicatorEnv,
replicator!: LiveSyncAbstractReplicator; LiveSyncJournalReplicatorEnv,
LiveSyncCouchDBReplicatorEnv,
_suspended = false; HasSettings<ObsidianLiveSyncSettings>
get suspended() { {
return this._suspended || !this.settings?.isConfigured;
}
set suspended(value: boolean) {
this._suspended = value;
}
get shouldBatchSave() {
return this.settings?.batchSave && this.settings?.liveSync != true;
}
get batchSaveMinimumDelay(): number {
return this.settings?.batchSaveMinimumDelay ?? DEFAULT_SETTINGS.batchSaveMinimumDelay
}
get batchSaveMaximumDelay(): number {
return this.settings?.batchSaveMaximumDelay ?? DEFAULT_SETTINGS.batchSaveMaximumDelay
}
deviceAndVaultName = "";
isReady = false;
packageVersion = "";
manifestVersion = "";
// --> Module System // --> Module System
getAddOn<T extends LiveSyncCommands>(cls: string) { getAddOn<T extends LiveSyncCommands>(cls: string) {
for (const addon of this.addOns) { for (const addon of this.addOns) {
@@ -120,9 +120,16 @@ export default class ObsidianLiveSyncPlugin extends Plugin implements LiveSyncLo
} }
// Keep order to display the dialogue in order. // Keep order to display the dialogue in order.
addOns = [new ConfigSync(this), new HiddenFileSync(this)] as LiveSyncCommands[]; addOns = [
new ConfigSync(this),
new HiddenFileSync(this),
new LocalDatabaseMaintenance(this),
new P2PReplicator(this),
] as LiveSyncCommands[];
modules = [ modules = [
new ModuleLiveSyncMain(this),
new ModuleExtraSyncObsidian(this, this),
// Only on Obsidian // Only on Obsidian
new ModuleDatabaseFileAccess(this), new ModuleDatabaseFileAccess(this),
// Common // Common
@@ -166,48 +173,83 @@ export default class ObsidianLiveSyncPlugin extends Plugin implements LiveSyncLo
new ModuleIntegratedTest(this, this), new ModuleIntegratedTest(this, this),
] as (IObsidianModule | AbstractModule)[]; ] as (IObsidianModule | AbstractModule)[];
injected = injectModules(this, [...this.modules, ...this.addOns] as ICoreModule[]); injected = injectModules(this, [...this.modules, ...this.addOns] as ICoreModule[]);
// <-- Module System
$$addLog(message: any, level: LOG_LEVEL = LOG_LEVEL_INFO, key = ""): void { throwShouldBeOverridden() } $$isSuspended(): boolean {
throwShouldBeOverridden();
}
$$setSuspended(value: boolean): void {
throwShouldBeOverridden();
}
$$isDatabaseReady(): boolean {
throwShouldBeOverridden();
}
$$getDeviceAndVaultName(): string {
throwShouldBeOverridden();
}
$$setDeviceAndVaultName(name: string): void {
throwShouldBeOverridden();
}
$$addLog(message: any, level: LOG_LEVEL = LOG_LEVEL_INFO, key = ""): void {
throwShouldBeOverridden();
}
$$isReady(): boolean {
throwShouldBeOverridden();
}
$$markIsReady(): void {
throwShouldBeOverridden();
}
$$resetIsReady(): void {
throwShouldBeOverridden();
}
// Following are plugged by the modules. // Following are plugged by the modules.
settings!: ObsidianLiveSyncSettings;
localDatabase!: LiveSyncLocalDB;
simpleStore!: SimpleStore<CheckPointInfo>;
replicator!: LiveSyncAbstractReplicator;
confirm!: Confirm; confirm!: Confirm;
storageAccess!: StorageAccess; storageAccess!: StorageAccess;
databaseFileAccess!: DatabaseFileAccess; databaseFileAccess!: DatabaseFileAccess;
fileHandler!: ModuleFileHandler; fileHandler!: ModuleFileHandler;
rebuilder!: Rebuilder; rebuilder!: Rebuilder;
// implementing interfaces
kvDB!: KeyValueDatabase; kvDB!: KeyValueDatabase;
last_successful_post = false; getDatabase(): PouchDB.Database<EntryDoc> {
return this.localDatabase.localDatabase;
}
getSettings(): ObsidianLiveSyncSettings {
return this.settings;
}
totalFileEventCount = 0; $$markFileListPossiblyChanged(): void {
throwShouldBeOverridden();
}
$$customFetchHandler(): ObsHttpHandler { $$customFetchHandler(): ObsHttpHandler {
throw new Error("This function should be overridden by the module."); throwShouldBeOverridden();
} }
customFetchHandler() { $$getLastPostFailedBySize(): boolean {
return this.$$customFetchHandler(); throwShouldBeOverridden();
} }
getLastPostFailedBySize() { $$isStorageInsensitive(): boolean {
return !this.last_successful_post; throwShouldBeOverridden();
} }
$$shouldCheckCaseInsensitive(): boolean {
throwShouldBeOverridden();
getDatabase(): PouchDB.Database<EntryDoc> { return this.localDatabase.localDatabase; }
getSettings(): ObsidianLiveSyncSettings { return this.settings; }
getIsMobile(): boolean { return this.isMobile; }
$$isStorageInsensitive(): boolean { throwShouldBeOverridden() }
get shouldCheckCaseInsensitive() {
if (this.$$isStorageInsensitive()) return false;
return !this.settings.handleFilenameCaseSensitive;
} }
_unloaded = false; $$isUnloaded(): boolean {
throwShouldBeOverridden();
}
requestCount = reactiveSource(0); requestCount = reactiveSource(0);
responseCount = reactiveSource(0); responseCount = reactiveSource(0);
@@ -222,9 +264,6 @@ export default class ObsidianLiveSyncPlugin extends Plugin implements LiveSyncLo
processingFileEventCount = reactiveSource(0); processingFileEventCount = reactiveSource(0);
_totalProcessingCount?: ReactiveValue<number>; _totalProcessingCount?: ReactiveValue<number>;
get isReloadingScheduled() {
return this._totalProcessingCount !== undefined;
}
replicationStat = reactiveSource({ replicationStat = reactiveSource({
sent: 0, sent: 0,
@@ -233,73 +272,104 @@ export default class ObsidianLiveSyncPlugin extends Plugin implements LiveSyncLo
maxPushSeq: 0, maxPushSeq: 0,
lastSyncPullSeq: 0, lastSyncPullSeq: 0,
lastSyncPushSeq: 0, lastSyncPushSeq: 0,
syncStatus: "CLOSED" as DatabaseConnectingStatus syncStatus: "CLOSED" as DatabaseConnectingStatus,
}); });
get isMobile() { return this.$$isMobile(); } $$isReloadingScheduled(): boolean {
throwShouldBeOverridden();
}
// Plug-in's overrideable functions $$getReplicator(): LiveSyncAbstractReplicator {
onload() { void this.onLiveSyncLoad(); }
async saveSettings() { await this.$$saveSettingData(); }
onunload() { return void this.onLiveSyncUnload(); }
// <-- Plug-in's overrideable functions
$$connectRemoteCouchDB(uri: string, auth: {
username: string;
password: string
}, disableRequestURI: boolean, passphrase: string | false, useDynamicIterationCount: boolean, performSetup: boolean, skipInfo: boolean, compression: boolean): Promise<string | {
db: PouchDB.Database<EntryDoc>;
info: PouchDB.Core.DatabaseInfo
}> {
throwShouldBeOverridden(); throwShouldBeOverridden();
} }
$$connectRemoteCouchDB(
uri: string,
auth: CouchDBCredentials,
disableRequestURI: boolean,
passphrase: string | false,
useDynamicIterationCount: boolean,
performSetup: boolean,
skipInfo: boolean,
compression: boolean,
customHeaders: Record<string, string>,
useRequestAPI: boolean,
getPBKDF2Salt: () => Promise<Uint8Array>
): Promise<
| string
| {
db: PouchDB.Database<EntryDoc>;
info: PouchDB.Core.DatabaseInfo;
}
> {
throwShouldBeOverridden();
}
$$isMobile(): boolean { throwShouldBeOverridden(); } $$isMobile(): boolean {
$$vaultName(): string { throwShouldBeOverridden(); } throwShouldBeOverridden();
}
$$vaultName(): string {
throwShouldBeOverridden();
}
// --> Path // --> Path
$$getActiveFilePath(): FilePathWithPrefix | undefined { throwShouldBeOverridden(); } $$getActiveFilePath(): FilePathWithPrefix | undefined {
throwShouldBeOverridden();
}
// <-- Path // <-- Path
// --> Path conversion // --> Path conversion
$$id2path(id: DocumentID, entry?: EntryHasPath, stripPrefix?: boolean): FilePathWithPrefix { throwShouldBeOverridden(); } $$id2path(id: DocumentID, entry?: EntryHasPath, stripPrefix?: boolean): FilePathWithPrefix {
throwShouldBeOverridden();
}
$$path2id(filename: FilePathWithPrefix | FilePath, prefix?: string): Promise<DocumentID> { throwShouldBeOverridden(); } $$path2id(filename: FilePathWithPrefix | FilePath, prefix?: string): Promise<DocumentID> {
throwShouldBeOverridden();
}
// <!-- Path conversion // <!-- Path conversion
// --> Database // --> Database
$$createPouchDBInstance<T extends object>(name?: string, options?: PouchDB.Configuration.DatabaseConfiguration): PouchDB.Database<T> { throwShouldBeOverridden(); } $$createPouchDBInstance<T extends object>(
name?: string,
options?: PouchDB.Configuration.DatabaseConfiguration
): PouchDB.Database<T> {
throwShouldBeOverridden();
}
$allOnDBUnload(db: LiveSyncLocalDB): void { return; } $allOnDBUnload(db: LiveSyncLocalDB): void {
$allOnDBClose(db: LiveSyncLocalDB): void { return; } return;
}
$allOnDBClose(db: LiveSyncLocalDB): void {
return;
}
// <!-- Database // <!-- Database
$anyNewReplicator(settingOverride: Partial<ObsidianLiveSyncSettings> = {}): Promise<LiveSyncAbstractReplicator> { throwShouldBeOverridden(); } $anyNewReplicator(settingOverride: Partial<ObsidianLiveSyncSettings> = {}): Promise<LiveSyncAbstractReplicator> {
throwShouldBeOverridden();
}
$everyOnInitializeDatabase(db: LiveSyncLocalDB): Promise<boolean> { return InterceptiveEvery; } $everyOnInitializeDatabase(db: LiveSyncLocalDB): Promise<boolean> {
return InterceptiveEvery;
}
$everyOnResetDatabase(db: LiveSyncLocalDB): Promise<boolean> { return InterceptiveEvery; } $everyOnResetDatabase(db: LiveSyncLocalDB): Promise<boolean> {
return InterceptiveEvery;
getReplicator() {
return this.replicator;
} }
// end interfaces // end interfaces
$$getVaultName(): string { throwShouldBeOverridden(); } $$getVaultName(): string {
throwShouldBeOverridden();
}
simpleStore!: SimpleStore<CheckPointInfo> $$getSimpleStore<T>(kind: string): SimpleStore<T> {
throwShouldBeOverridden();
$$getSimpleStore<T>(kind: string): SimpleStore<T> { throwShouldBeOverridden(); } }
// trench!: Trench; // trench!: Trench;
// --> Events // --> Events
/* /*
@@ -336,206 +406,118 @@ export default class ObsidianLiveSyncPlugin extends Plugin implements LiveSyncLo
6. localDatabase.onunload 6. localDatabase.onunload
7. replicator.closeReplication 7. replicator.closeReplication
8. localDatabase.close 8. localDatabase.close
9. (event) EVENT_PLATFORM_UNLOADED
*/ */
$everyOnLayoutReady(): Promise<boolean> {
$everyOnLayoutReady(): Promise<boolean> { return InterceptiveEvery } return InterceptiveEvery;
$everyOnFirstInitialize(): Promise<boolean> { return InterceptiveEvery } }
$everyOnFirstInitialize(): Promise<boolean> {
return InterceptiveEvery;
}
// Some Module should call this function to start the plugin. // Some Module should call this function to start the plugin.
async onLiveSyncReady() { $$onLiveSyncReady(): Promise<false | undefined> {
if (!await this.$everyOnLayoutReady()) return; throwShouldBeOverridden();
eventHub.emitEvent(EVENT_LAYOUT_READY);
if (this.settings.suspendFileWatching || this.settings.suspendParseReplicationResult) {
const ANSWER_KEEP = "Keep this plug-in suspended";
const ANSWER_RESUME = "Resume and restart Obsidian";
const message = `Self-hosted LiveSync has been configured to ignore some events. Is this intentional for you?
| Type | Status | Note |
|:---:|:---:|---|
| Storage Events | ${this.settings.suspendFileWatching ? "suspended" : "active"} | Every modification will be ignored |
| Database Events | ${this.settings.suspendParseReplicationResult ? "suspended" : "active"} | Every synchronised change will be postponed |
Do you want to resume them and restart Obsidian?
> [!DETAILS]-
> These flags are set by the plug-in while rebuilding, or fetching. If the process ends abnormally, it may be kept unintended.
> If you are not sure, you can try to rerun these processes. Make sure to back your vault up.
`;
if (await this.confirm.askSelectStringDialogue(message, [ANSWER_KEEP, ANSWER_RESUME], { defaultAction: ANSWER_KEEP, title: "Scram Enabled" }) == ANSWER_RESUME) {
this.settings.suspendFileWatching = false;
this.settings.suspendParseReplicationResult = false;
await this.saveSettings();
await this.$$scheduleAppReload();
return;
}
}
const isInitialized = await this.$$initializeDatabase(false, false);
if (!isInitialized) {
//TODO:stop all sync.
return false;
}
if (!await this.$everyOnFirstInitialize()) return;
await this.realizeSettingSyncMode();
fireAndForget(async () => {
Logger(`Additional safety scan..`, LOG_LEVEL_VERBOSE);
if (!await this.$allScanStat()) {
Logger(`Additional safety scan has been failed on some module`, LOG_LEVEL_NOTICE);
} else {
Logger(`Additional safety scan done`, LOG_LEVEL_VERBOSE);
}
});
} }
wireUpEvents() { $$wireUpEvents(): void {
eventHub.onEvent(EVENT_SETTING_SAVED, (evt: CustomEvent<ObsidianLiveSyncSettings>) => { throwShouldBeOverridden();
const settings = evt.detail; }
this.localDatabase.settings = settings; $$onLiveSyncLoad(): Promise<void> {
setLang(settings.displayLanguage); throwShouldBeOverridden();
eventHub.emitEvent(EVENT_REQUEST_RELOAD_SETTING_TAB);
});
eventHub.onEvent(EVENT_SETTING_SAVED, (evt: CustomEvent<ObsidianLiveSyncSettings>) => {
fireAndForget(() => this.realizeSettingSyncMode());
})
} }
$$onLiveSyncUnload(): Promise<void> {
async onLiveSyncLoad() { throwShouldBeOverridden();
this.wireUpEvents();
// debugger;
eventHub.emitEvent(EVENT_PLUGIN_LOADED, this);
Logger("loading plugin");
if (!await this.$everyOnloadStart()) {
Logger("Plugin initialising has been cancelled by some module", LOG_LEVEL_NOTICE);
return;
}
// this.addUIs();
//@ts-ignore
const manifestVersion: string = MANIFEST_VERSION || "0.0.0";
//@ts-ignore
const packageVersion: string = PACKAGE_VERSION || "0.0.0";
this.manifestVersion = manifestVersion;
this.packageVersion = packageVersion;
Logger($f`Self-hosted LiveSync${" v"}${manifestVersion} ${packageVersion}`);
await this.$$loadSettings();
if (!await this.$everyOnloadAfterLoadSettings()) {
Logger("Plugin initialising has been cancelled by some module", LOG_LEVEL_NOTICE);
return;
}
const lsKey = "obsidian-live-sync-ver" + this.$$getVaultName();
const last_version = localStorage.getItem(lsKey);
const lastVersion = ~~(versionNumberString2Number(manifestVersion) / 1000);
if (lastVersion > this.settings.lastReadUpdates && this.settings.isConfigured) {
Logger($f`You have some unread release notes! Please read them once!`, LOG_LEVEL_NOTICE);
}
//@ts-ignore
if (this.isMobile) {
this.settings.disableRequestURI = true;
}
if (last_version && Number(last_version) < VER) {
this.settings.liveSync = false;
this.settings.syncOnSave = false;
this.settings.syncOnEditorSave = false;
this.settings.syncOnStart = false;
this.settings.syncOnFileOpen = false;
this.settings.syncAfterMerge = false;
this.settings.periodicReplication = false;
this.settings.versionUpFlash = $f`Self-hosted LiveSync has been upgraded and some behaviors have changed incompatibly. All automatic synchronization is now disabled temporary. Ensure that other devices are also upgraded, and enable synchronization again.`;
await this.saveSettings();
}
localStorage.setItem(lsKey, `${VER}`);
await this.$$openDatabase();
this.realizeSettingSyncMode = this.realizeSettingSyncMode.bind(this);
// this.$$parseReplicationResult = this.$$parseReplicationResult.bind(this);
// this.$$replicate = this.$$replicate.bind(this);
this.onLiveSyncReady = this.onLiveSyncReady.bind(this);
await this.$everyOnload();
await Promise.all(this.addOns.map(e => e.onload()));
}
async onLiveSyncUnload() {
eventHub.emitEvent(EVENT_PLUGIN_UNLOADED);
await this.$allStartOnUnload();
cancelAllPeriodicTask();
cancelAllTasks();
stopAllRunningProcessors();
await this.$allOnUnload();
this._unloaded = true;
for (const addOn of this.addOns) {
addOn.onunload();
}
if (this.localDatabase != null) {
this.localDatabase.onunload();
if (this.replicator) {
this.replicator?.closeReplication();
}
await this.localDatabase.close();
}
Logger($f`unloading plugin`);
} }
$allScanStat(): Promise<boolean> { $allScanStat(): Promise<boolean> {
return InterceptiveAll; return InterceptiveAll;
} }
$everyOnloadStart(): Promise<boolean> { return InterceptiveEvery; } $everyOnloadStart(): Promise<boolean> {
return InterceptiveEvery;
$everyOnloadAfterLoadSettings(): Promise<boolean> { return InterceptiveEvery; }
$everyOnload(): Promise<boolean> { return InterceptiveEvery; }
$anyHandlerProcessesFileEvent(item: FileEventItem): Promise<boolean | undefined> { return InterceptiveAny; }
$allStartOnUnload(): Promise<boolean> { return InterceptiveAll }
$allOnUnload(): Promise<boolean> { return InterceptiveAll; }
$$openDatabase(): Promise<boolean> { throwShouldBeOverridden() }
async realizeSettingSyncMode() {
await this.$everyBeforeSuspendProcess();
await this.$everyBeforeRealizeSetting();
this.localDatabase.refreshSettings();
await this.$everyCommitPendingFileEvent();
await this.$everyRealizeSettingSyncMode();
// disable all sync temporary.
if (this.suspended) return;
await this.$everyOnResumeProcess();
await this.$everyAfterResumeProcess();
await this.$everyAfterRealizeSetting();
} }
$$performRestart() { throwShouldBeOverridden(); }
$$clearUsedPassphrase(): void { throwShouldBeOverridden() } $everyOnloadAfterLoadSettings(): Promise<boolean> {
$$loadSettings(): Promise<void> { throwShouldBeOverridden() } return InterceptiveEvery;
}
$$saveDeviceAndVaultName(): void { throwShouldBeOverridden(); } $everyOnload(): Promise<boolean> {
return InterceptiveEvery;
}
$$saveSettingData(): Promise<void> { throwShouldBeOverridden() } $anyHandlerProcessesFileEvent(item: FileEventItem): Promise<boolean | undefined> {
return InterceptiveAny;
}
$anyProcessOptionalFileEvent(path: FilePath): Promise<boolean | undefined> { return InterceptiveAny; } $allStartOnUnload(): Promise<boolean> {
return InterceptiveAll;
}
$allOnUnload(): Promise<boolean> {
return InterceptiveAll;
}
$everyCommitPendingFileEvent(): Promise<boolean> { return InterceptiveEvery } $$openDatabase(): Promise<boolean> {
throwShouldBeOverridden();
}
$$realizeSettingSyncMode(): Promise<void> {
throwShouldBeOverridden();
}
$$performRestart() {
throwShouldBeOverridden();
}
$$clearUsedPassphrase(): void {
throwShouldBeOverridden();
}
$$loadSettings(): Promise<void> {
throwShouldBeOverridden();
}
$$saveDeviceAndVaultName(): void {
throwShouldBeOverridden();
}
$$saveSettingData(): Promise<void> {
throwShouldBeOverridden();
}
$anyProcessOptionalFileEvent(path: FilePath): Promise<boolean | undefined> {
return InterceptiveAny;
}
$everyCommitPendingFileEvent(): Promise<boolean> {
return InterceptiveEvery;
}
// -> // ->
$anyGetOptionalConflictCheckMethod(path: FilePathWithPrefix): Promise<boolean | undefined | "newer"> { return InterceptiveAny; } $anyGetOptionalConflictCheckMethod(path: FilePathWithPrefix): Promise<boolean | undefined | "newer"> {
return InterceptiveAny;
}
$$queueConflictCheckIfOpen(file: FilePathWithPrefix): Promise<void> { throwShouldBeOverridden() } $$queueConflictCheckIfOpen(file: FilePathWithPrefix): Promise<void> {
throwShouldBeOverridden();
}
$$queueConflictCheck(file: FilePathWithPrefix): Promise<void> { throwShouldBeOverridden() } $$queueConflictCheck(file: FilePathWithPrefix): Promise<void> {
throwShouldBeOverridden();
}
$$waitForAllConflictProcessed(): Promise<boolean> { throwShouldBeOverridden() } $$waitForAllConflictProcessed(): Promise<boolean> {
throwShouldBeOverridden();
}
//<-- Conflict Check //<-- Conflict Check
$anyProcessOptionalSyncFiles(doc: LoadedEntry): Promise<boolean | undefined> { return InterceptiveAny; } $anyProcessOptionalSyncFiles(doc: LoadedEntry): Promise<boolean | undefined> {
return InterceptiveAny;
$anyProcessReplicatedDoc(doc: MetaEntry): Promise<boolean | undefined> { return InterceptiveAny; } }
$anyProcessReplicatedDoc(doc: MetaEntry): Promise<boolean | undefined> {
return InterceptiveAny;
}
//---> Sync //---> Sync
$$parseReplicationResult(docs: Array<PouchDB.Core.ExistingDocument<EntryDoc>>): void { $$parseReplicationResult(docs: Array<PouchDB.Core.ExistingDocument<EntryDoc>>): void {
@@ -564,86 +546,177 @@ Do you want to resume them and restart Obsidian?
$everyAfterResumeProcess(): Promise<boolean> { $everyAfterResumeProcess(): Promise<boolean> {
return InterceptiveEvery; return InterceptiveEvery;
} }
$$checkAndAskResolvingMismatchedTweaks(preferred: Partial<TweakValues>): Promise<[TweakValues | boolean, boolean]> {
throwShouldBeOverridden();
}
$$askResolvingMismatchedTweaks(preferredSource: TweakValues): Promise<"OK" | "CHECKAGAIN" | "IGNORE"> {
throwShouldBeOverridden();
}
$$checkAndAskUseRemoteConfiguration(
settings: RemoteDBSettings
): Promise<{ result: false | TweakValues; requireFetch: boolean }> {
throwShouldBeOverridden();
}
$$askUseRemoteConfiguration(
trialSetting: RemoteDBSettings,
preferred: TweakValues
): Promise<{ result: false | TweakValues; requireFetch: boolean }> {
throwShouldBeOverridden();
}
$everyBeforeReplicate(showMessage: boolean): Promise<boolean> {
return InterceptiveEvery;
}
$$replicate(showMessage: boolean = false): Promise<boolean | void> {
throwShouldBeOverridden();
}
$$replicateByEvent(showMessage: boolean = false): Promise<boolean | void> {
throwShouldBeOverridden();
}
$$askResolvingMismatchedTweaks(): Promise<"OK" | "CHECKAGAIN" | "IGNORE"> { throwShouldBeOverridden(); } $everyOnDatabaseInitialized(showingNotice: boolean): Promise<boolean> {
$everyBeforeReplicate(showMessage: boolean): Promise<boolean> { return InterceptiveEvery; } throwShouldBeOverridden();
$$replicate(showMessage: boolean = false): Promise<boolean | void> { throwShouldBeOverridden() } }
$everyOnDatabaseInitialized(showingNotice: boolean): Promise<boolean> { throwShouldBeOverridden() } $$initializeDatabase(showingNotice: boolean = false, reopenDatabase = true): Promise<boolean> {
throwShouldBeOverridden();
}
$$initializeDatabase(showingNotice: boolean = false, reopenDatabase = true): Promise<boolean> { throwShouldBeOverridden() } $anyAfterConnectCheckFailed(): Promise<boolean | "CHECKAGAIN" | undefined> {
return InterceptiveAny;
}
$anyAfterConnectCheckFailed(): Promise<boolean | "CHECKAGAIN" | undefined> { return InterceptiveAny; } $$replicateAllToServer(
showingNotice: boolean = false,
$$replicateAllToServer(showingNotice: boolean = false, sendChunksInBulkDisabled: boolean = false): Promise<boolean> { throwShouldBeOverridden() } sendChunksInBulkDisabled: boolean = false
$$replicateAllFromServer(showingNotice: boolean = false): Promise<boolean> { throwShouldBeOverridden() } ): Promise<boolean> {
throwShouldBeOverridden();
}
$$replicateAllFromServer(showingNotice: boolean = false): Promise<boolean> {
throwShouldBeOverridden();
}
// Remote Governing // Remote Governing
$$markRemoteLocked(lockByClean: boolean = false): Promise<void> { throwShouldBeOverridden() } $$markRemoteLocked(lockByClean: boolean = false): Promise<void> {
throwShouldBeOverridden();
}
$$markRemoteUnlocked(): Promise<void> { throwShouldBeOverridden() } $$markRemoteUnlocked(): Promise<void> {
throwShouldBeOverridden();
}
$$markRemoteResolved(): Promise<void> { throwShouldBeOverridden() } $$markRemoteResolved(): Promise<void> {
throwShouldBeOverridden();
}
// <-- Remote Governing // <-- Remote Governing
$$isFileSizeExceeded(size: number): boolean {
$$isFileSizeExceeded(size: number): boolean { throwShouldBeOverridden() }
$$performFullScan(showingNotice?: boolean): Promise<void> { throwShouldBeOverridden(); }
$anyResolveConflictByUI(filename: FilePathWithPrefix, conflictCheckResult: diff_result): Promise<boolean | undefined> { return InterceptiveAny; }
$$resolveConflictByDeletingRev(path: FilePathWithPrefix, deleteRevision: string, subTitle = ""): Promise<typeof MISSING_OR_ERROR | typeof AUTO_MERGED> { throwShouldBeOverridden(); }
$$resolveConflict(filename: FilePathWithPrefix): Promise<void> { throwShouldBeOverridden(); }
$anyResolveConflictByNewest(filename: FilePathWithPrefix): Promise<boolean> { throwShouldBeOverridden(); }
$$waitForReplicationOnce(): Promise<boolean | void> { throwShouldBeOverridden(); }
$$resetLocalDatabase(): Promise<void> { throwShouldBeOverridden(); }
$$tryResetRemoteDatabase(): Promise<void> { throwShouldBeOverridden(); }
$$tryCreateRemoteDatabase(): Promise<void> { throwShouldBeOverridden(); }
$$isIgnoredByIgnoreFiles(file: string | UXFileInfoStub): Promise<boolean> { throwShouldBeOverridden(); }
$$isTargetFile(file: string | UXFileInfoStub, keepFileCheckList = false): Promise<boolean> { throwShouldBeOverridden(); }
$$askReload(message?: string) { throwShouldBeOverridden(); }
$$scheduleAppReload() { throwShouldBeOverridden(); }
//--- Setup
$allSuspendAllSync(): Promise<boolean> { return InterceptiveAll; }
$allSuspendExtraSync(): Promise<boolean> { return InterceptiveAll; }
$allAskUsingOptionalSyncFeature(opt: {
enableFetch?: boolean,
enableOverwrite?: boolean
}): Promise<boolean> {
throwShouldBeOverridden(); throwShouldBeOverridden();
} }
$anyConfigureOptionalSyncFeature(mode: string): Promise<void> { throwShouldBeOverridden(); }
$$showView(viewType: string): Promise<void> { throwShouldBeOverridden(); } $$performFullScan(showingNotice?: boolean): Promise<void> {
throwShouldBeOverridden();
}
$anyResolveConflictByUI(
filename: FilePathWithPrefix,
conflictCheckResult: diff_result
): Promise<boolean | undefined> {
return InterceptiveAny;
}
$$resolveConflictByDeletingRev(
path: FilePathWithPrefix,
deleteRevision: string,
subTitle = ""
): Promise<typeof MISSING_OR_ERROR | typeof AUTO_MERGED> {
throwShouldBeOverridden();
}
$$resolveConflict(filename: FilePathWithPrefix): Promise<void> {
throwShouldBeOverridden();
}
$anyResolveConflictByNewest(filename: FilePathWithPrefix): Promise<boolean> {
throwShouldBeOverridden();
}
$$resetLocalDatabase(): Promise<void> {
throwShouldBeOverridden();
}
$$tryResetRemoteDatabase(): Promise<void> {
throwShouldBeOverridden();
}
$$tryCreateRemoteDatabase(): Promise<void> {
throwShouldBeOverridden();
}
$$isIgnoredByIgnoreFiles(file: string | UXFileInfoStub): Promise<boolean> {
throwShouldBeOverridden();
}
$$isTargetFile(file: string | UXFileInfoStub, keepFileCheckList = false): Promise<boolean> {
throwShouldBeOverridden();
}
$$askReload(message?: string) {
throwShouldBeOverridden();
}
$$scheduleAppReload() {
throwShouldBeOverridden();
}
//--- Setup
$allSuspendAllSync(): Promise<boolean> {
return InterceptiveAll;
}
$allSuspendExtraSync(): Promise<boolean> {
return InterceptiveAll;
}
$allAskUsingOptionalSyncFeature(opt: { enableFetch?: boolean; enableOverwrite?: boolean }): Promise<boolean> {
throwShouldBeOverridden();
}
$anyConfigureOptionalSyncFeature(mode: string): Promise<void> {
throwShouldBeOverridden();
}
$$showView(viewType: string): Promise<void> {
throwShouldBeOverridden();
}
// For Development: Ensure reliability MORE AND MORE. May the this plug-in helps all of us. // For Development: Ensure reliability MORE AND MORE. May the this plug-in helps all of us.
$everyModuleTest(): Promise<boolean> { return InterceptiveEvery; } $everyModuleTest(): Promise<boolean> {
$everyModuleTestMultiDevice(): Promise<boolean> { return InterceptiveEvery; } return InterceptiveEvery;
$$addTestResult(name: string, key: string, result: boolean, summary?: string, message?: string): void { throwShouldBeOverridden(); } }
$everyModuleTestMultiDevice(): Promise<boolean> {
return InterceptiveEvery;
}
$$addTestResult(name: string, key: string, result: boolean, summary?: string, message?: string): void {
throwShouldBeOverridden();
}
_isMainReady(): boolean { return this.isReady; } _isThisModuleEnabled(): boolean {
_isMainSuspended(): boolean { return this.suspended; } return true;
_isThisModuleEnabled(): boolean { return true; } }
_isDatabaseReady(): boolean { return this.localDatabase.isReady; }
$anyGetAppId(): Promise<string | undefined> { return InterceptiveAny; } $anyGetAppId(): Promise<string | undefined> {
return InterceptiveAny;
}
// Plug-in's overrideable functions
onload() {
void this.$$onLiveSyncLoad();
}
async saveSettings() {
await this.$$saveSettingData();
}
onunload() {
return void this.$$onLiveSyncUnload();
}
// <-- Plug-in's overrideable functions
} }
// For now, // For now,
export type LiveSyncCore = ObsidianLiveSyncPlugin; export type LiveSyncCore = ObsidianLiveSyncPlugin;

View File

@@ -3,8 +3,14 @@ import type { LOG_LEVEL } from "../lib/src/common/types";
import type { LiveSyncCore } from "../main"; import type { LiveSyncCore } from "../main";
import { unique } from "octagonal-wheels/collection"; import { unique } from "octagonal-wheels/collection";
import type { IObsidianModule } from "./AbstractObsidianModule.ts"; import type { IObsidianModule } from "./AbstractObsidianModule.ts";
import type { ICoreModuleBase, AllInjectableProps, AllExecuteProps, EveryExecuteProps, AnyExecuteProps, ICoreModule } from "./ModuleTypes"; import type {
ICoreModuleBase,
AllInjectableProps,
AllExecuteProps,
EveryExecuteProps,
AnyExecuteProps,
ICoreModule,
} from "./ModuleTypes";
function isOverridableKey(key: string): key is keyof ICoreModuleBase { function isOverridableKey(key: string): key is keyof ICoreModuleBase {
return key.startsWith("$"); return key.startsWith("$");
@@ -14,7 +20,6 @@ function isInjectableKey(key: string): key is keyof AllInjectableProps {
return key.startsWith("$$"); return key.startsWith("$$");
} }
function isAllExecuteKey(key: string): key is keyof AllExecuteProps { function isAllExecuteKey(key: string): key is keyof AllExecuteProps {
return key.startsWith("$all"); return key.startsWith("$all");
} }
@@ -35,15 +40,17 @@ function isAnyExecuteKey(key: string): key is keyof AnyExecuteProps {
* All of above performed on injectModules function. * All of above performed on injectModules function.
*/ */
export function injectModules<T extends ICoreModule>(target: T, modules: ICoreModule[]) { export function injectModules<T extends ICoreModule>(target: T, modules: ICoreModule[]) {
const allKeys = unique([...Object.keys(Object.getOwnPropertyDescriptors(target)), const allKeys = unique([
...Object.keys(Object.getOwnPropertyDescriptors(Object.getPrototypeOf(target)))]).filter(e => e.startsWith("$")) as (keyof ICoreModule)[]; ...Object.keys(Object.getOwnPropertyDescriptors(target)),
...Object.keys(Object.getOwnPropertyDescriptors(Object.getPrototypeOf(target))),
]).filter((e) => e.startsWith("$")) as (keyof ICoreModule)[];
const moduleMap = new Map<string, IObsidianModule[]>(); const moduleMap = new Map<string, IObsidianModule[]>();
for (const module of modules) { for (const module of modules) {
for (const key of allKeys) { for (const key of allKeys) {
if (isOverridableKey(key)) { if (isOverridableKey(key)) {
if (key in module) { if (key in module) {
const list = moduleMap.get(key) || []; const list = moduleMap.get(key) || [];
if (typeof module[key] === 'function') { if (typeof module[key] === "function") {
module[key] = module[key].bind(module) as any; module[key] = module[key].bind(module) as any;
} }
list.push(module); list.push(module);
@@ -74,7 +81,7 @@ export function injectModules<T extends ICoreModule>(target: T, modules: ICoreMo
} }
} }
return true; return true;
} };
for (const module of modules) { for (const module of modules) {
Logger(`[${module.constructor.name}]: Injected (All) ${key} `, LOG_LEVEL_VERBOSE); Logger(`[${module.constructor.name}]: Injected (All) ${key} `, LOG_LEVEL_VERBOSE);
} }
@@ -94,7 +101,7 @@ export function injectModules<T extends ICoreModule>(target: T, modules: ICoreMo
} }
} }
return true; return true;
} };
for (const module of modules) { for (const module of modules) {
Logger(`[${module.constructor.name}]: Injected (Every) ${key} `, LOG_LEVEL_VERBOSE); Logger(`[${module.constructor.name}]: Injected (Every) ${key} `, LOG_LEVEL_VERBOSE);
} }
@@ -115,7 +122,7 @@ export function injectModules<T extends ICoreModule>(target: T, modules: ICoreMo
} }
} }
return false; return false;
} };
for (const module of modules) { for (const module of modules) {
Logger(`[${module.constructor.name}]: Injected (Any) ${key} `, LOG_LEVEL_VERBOSE); Logger(`[${module.constructor.name}]: Injected (Any) ${key} `, LOG_LEVEL_VERBOSE);
} }
@@ -127,7 +134,6 @@ export function injectModules<T extends ICoreModule>(target: T, modules: ICoreMo
return true; return true;
} }
export abstract class AbstractModule { export abstract class AbstractModule {
_log = (msg: any, level: LOG_LEVEL = LOG_LEVEL_INFO, key?: string) => { _log = (msg: any, level: LOG_LEVEL = LOG_LEVEL_INFO, key?: string) => {
if (typeof msg === "string" && level !== LOG_LEVEL_NOTICE) { if (typeof msg === "string" && level !== LOG_LEVEL_NOTICE) {
@@ -173,8 +179,7 @@ export abstract class AbstractModule {
return this.testFail(`${key} failed: ${ret}`); return this.testFail(`${key} failed: ${ret}`);
} }
this.addTestResult(key, true, ""); this.addTestResult(key, true, "");
} } catch (ex: any) {
catch (ex: any) {
this.addTestResult(key, false, "Failed by Exception", ex.toString()); this.addTestResult(key, false, "Failed by Exception", ex.toString());
return this.testFail(`${key} failed: ${ex}`); return this.testFail(`${key} failed: ${ex}`);
} }

View File

@@ -4,15 +4,12 @@ import type ObsidianLiveSyncPlugin from "../main";
import { AbstractModule } from "./AbstractModule.ts"; import { AbstractModule } from "./AbstractModule.ts";
import type { ChainableExecuteFunction, OverridableFunctionsKeys } from "./ModuleTypes"; import type { ChainableExecuteFunction, OverridableFunctionsKeys } from "./ModuleTypes";
export type IObsidianModuleBase = OverridableFunctionsKeys<ObsidianLiveSyncPlugin>; export type IObsidianModuleBase = OverridableFunctionsKeys<ObsidianLiveSyncPlugin>;
export type IObsidianModule = Prettify<Partial<IObsidianModuleBase>> export type IObsidianModule = Prettify<Partial<IObsidianModuleBase>>;
export type ModuleKeys = keyof IObsidianModule; export type ModuleKeys = keyof IObsidianModule;
export type ChainableModuleProps = ChainableExecuteFunction<ObsidianLiveSyncPlugin>; export type ChainableModuleProps = ChainableExecuteFunction<ObsidianLiveSyncPlugin>;
export abstract class AbstractObsidianModule extends AbstractModule { export abstract class AbstractObsidianModule extends AbstractModule {
addCommand = this.plugin.addCommand.bind(this.plugin); addCommand = this.plugin.addCommand.bind(this.plugin);
registerView = this.plugin.registerView.bind(this.plugin); registerView = this.plugin.registerView.bind(this.plugin);
addRibbonIcon = this.plugin.addRibbonIcon.bind(this.plugin); addRibbonIcon = this.plugin.addRibbonIcon.bind(this.plugin);
@@ -31,25 +28,27 @@ export abstract class AbstractObsidianModule extends AbstractModule {
return this.plugin.app; return this.plugin.app;
} }
constructor(public plugin: ObsidianLiveSyncPlugin, public core: LiveSyncCore) { constructor(
public plugin: ObsidianLiveSyncPlugin,
public core: LiveSyncCore
) {
super(core); super(core);
} }
saveSettings = this.plugin.saveSettings.bind(this.plugin); saveSettings = this.plugin.saveSettings.bind(this.plugin);
_isMainReady() { _isMainReady() {
return this.core._isMainReady(); return this.core.$$isReady();
} }
_isMainSuspended() { _isMainSuspended() {
return this.core._isMainSuspended(); return this.core.$$isSuspended();
} }
_isDatabaseReady() { _isDatabaseReady() {
return this.core._isDatabaseReady(); return this.core.$$isDatabaseReady();
} }
//should be overridden //should be overridden
_isThisModuleEnabled() { _isThisModuleEnabled() {
return true return true;
} }
} }

View File

@@ -1,38 +1,53 @@
import type { Prettify } from "../lib/src/common/types"; import type { Prettify } from "../lib/src/common/types";
import type { LiveSyncCore } from "../main"; import type { LiveSyncCore } from "../main";
export type OverridableFunctionsKeys<T> = { export type OverridableFunctionsKeys<T> = {
[K in keyof T as K extends `$${string}` ? K : never]: T[K]; [K in keyof T as K extends `$${string}` ? K : never]: T[K];
} };
export type ChainableExecuteFunction<T> = { export type ChainableExecuteFunction<T> = {
[K in keyof T as K extends `$${string}` ? (T[K] extends (...args: any) => ChainableFunctionResult ? K : never) : never]: T[K]; [K in keyof T as K extends `$${string}`
} ? T[K] extends (...args: any) => ChainableFunctionResult
? K
: never
: never]: T[K];
};
export type ICoreModuleBase = OverridableFunctionsKeys<LiveSyncCore>; export type ICoreModuleBase = OverridableFunctionsKeys<LiveSyncCore>;
export type ICoreModule = Prettify<Partial<ICoreModuleBase>> export type ICoreModule = Prettify<Partial<ICoreModuleBase>>;
export type CoreModuleKeys = keyof ICoreModule; export type CoreModuleKeys = keyof ICoreModule;
export type ChainableFunctionResult = export type ChainableFunctionResult =
Promise<boolean | undefined | string> | Promise<boolean | undefined | string>
| Promise<boolean | undefined> | Promise<boolean | undefined>
| Promise<boolean> | Promise<boolean>
| Promise<void>; | Promise<void>;
export type ChainableFunctionResultOrAll = Promise<boolean | undefined | string | void>; export type ChainableFunctionResultOrAll = Promise<boolean | undefined | string | void>;
type AllExecuteFunction<T> = { type AllExecuteFunction<T> = {
[K in keyof T as K extends `$all${string}` ? (T[K] extends (...args: any[]) => ChainableFunctionResultOrAll ? K : never) : never]: T[K]; [K in keyof T as K extends `$all${string}`
} ? T[K] extends (...args: any[]) => ChainableFunctionResultOrAll
? K
: never
: never]: T[K];
};
type EveryExecuteFunction<T> = { type EveryExecuteFunction<T> = {
[K in keyof T as K extends `$every${string}` ? (T[K] extends (...args: any[]) => ChainableFunctionResult ? K : never) : never]: T[K]; [K in keyof T as K extends `$every${string}`
} ? T[K] extends (...args: any[]) => ChainableFunctionResult
? K
: never
: never]: T[K];
};
type AnyExecuteFunction<T> = { type AnyExecuteFunction<T> = {
[K in keyof T as K extends `$any${string}` ? (T[K] extends (...args: any[]) => ChainableFunctionResult ? K : never) : never]: T[K]; [K in keyof T as K extends `$any${string}`
} ? T[K] extends (...args: any[]) => ChainableFunctionResult
? K
: never
: never]: T[K];
};
type InjectableFunction<T> = { type InjectableFunction<T> = {
[K in keyof T as K extends `$$${string}` ? (T[K] extends (...args: any[]) => any ? K : never) : never]: T[K]; [K in keyof T as K extends `$$${string}` ? (T[K] extends (...args: any[]) => any ? K : never) : never]: T[K];
} };
export type AllExecuteProps = AllExecuteFunction<LiveSyncCore>; export type AllExecuteProps = AllExecuteFunction<LiveSyncCore>;
export type EveryExecuteProps = EveryExecuteFunction<LiveSyncCore>; export type EveryExecuteProps = EveryExecuteFunction<LiveSyncCore>;
export type AnyExecuteProps = AnyExecuteFunction<LiveSyncCore>; export type AnyExecuteProps = AnyExecuteFunction<LiveSyncCore>;

View File

@@ -1,14 +1,35 @@
import { LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import { EVENT_FILE_SAVED, eventHub } from "../../common/events"; import { EVENT_FILE_SAVED, eventHub } from "../../common/events";
import { getPathFromUXFileInfo, isInternalMetadata, markChangesAreSame } from "../../common/utils"; import {
import type { UXFileInfoStub, FilePathWithPrefix, UXFileInfo, MetaEntry, LoadedEntry, FilePath, SavingEntry } from "../../lib/src/common/types"; getDatabasePathFromUXFileInfo,
getStoragePathFromUXFileInfo,
isInternalMetadata,
markChangesAreSame,
} from "../../common/utils";
import type {
UXFileInfoStub,
FilePathWithPrefix,
UXFileInfo,
MetaEntry,
LoadedEntry,
FilePath,
SavingEntry,
DocumentID,
} from "../../lib/src/common/types";
import type { DatabaseFileAccess } from "../interfaces/DatabaseFileAccess"; import type { DatabaseFileAccess } from "../interfaces/DatabaseFileAccess";
import { type IObsidianModule } from "../AbstractObsidianModule.ts"; import { type IObsidianModule } from "../AbstractObsidianModule.ts";
import { isPlainText, shouldBeIgnored } from "../../lib/src/string_and_binary/path"; import { isPlainText, shouldBeIgnored, stripAllPrefixes } from "../../lib/src/string_and_binary/path";
import { createBlob, createTextBlob, delay, determineTypeFromBlob, isDocContentSame, readContent } from "../../lib/src/common/utils"; import {
createBlob,
createTextBlob,
delay,
determineTypeFromBlob,
isDocContentSame,
readContent,
} from "../../lib/src/common/utils";
import { serialized } from "octagonal-wheels/concurrency/lock"; import { serialized } from "octagonal-wheels/concurrency/lock";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import { ICHeader } from "../../common/types.ts";
export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidianModule, DatabaseFileAccess { export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidianModule, DatabaseFileAccess {
$everyOnload(): Promise<boolean> { $everyOnload(): Promise<boolean> {
@@ -18,7 +39,7 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
async $everyModuleTest(): Promise<boolean> { async $everyModuleTest(): Promise<boolean> {
if (!this.settings.enableDebugTools) return Promise.resolve(true); if (!this.settings.enableDebugTools) return Promise.resolve(true);
const testString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec purus nec nunc" const testString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec purus nec nunc";
// Before test, we need to delete completely. // Before test, we need to delete completely.
const conflicts = await this.getConflictedRevs("autoTest.md" as FilePathWithPrefix); const conflicts = await this.getConflictedRevs("autoTest.md" as FilePathWithPrefix);
for (const rev of conflicts) { for (const rev of conflicts) {
@@ -27,16 +48,21 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
await this.delete("autoTest.md" as FilePathWithPrefix); await this.delete("autoTest.md" as FilePathWithPrefix);
// OK, begin! // OK, begin!
await this._test("storeContent", async () => (await this.storeContent("autoTest.md" as FilePathWithPrefix, testString))); await this._test(
"storeContent",
async () => await this.storeContent("autoTest.md" as FilePathWithPrefix, testString)
);
// For test, we need to clear the caches. // For test, we need to clear the caches.
await this.localDatabase.hashCaches.clear(); await this.localDatabase.hashCaches.clear();
await this._test("readContent", async () => { await this._test("readContent", async () => {
const content = await this.fetch("autoTest.md" as FilePathWithPrefix); const content = await this.fetch("autoTest.md" as FilePathWithPrefix);
if (!content) return "File not found"; if (!content) return "File not found";
if (content.deleted) return "File is deleted"; if (content.deleted) return "File is deleted";
return (await content.body.text() == testString) ? true : `Content is not same ${await content.body.text()}`; return (await content.body.text()) == testString
? true
: `Content is not same ${await content.body.text()}`;
}); });
await this._test("delete", async () => (await this.delete("autoTest.md" as FilePathWithPrefix))); await this._test("delete", async () => await this.delete("autoTest.md" as FilePathWithPrefix));
await this._test("read deleted content", async () => { await this._test("read deleted content", async () => {
const content = await this.fetch("autoTest.md" as FilePathWithPrefix); const content = await this.fetch("autoTest.md" as FilePathWithPrefix);
if (!content) return true; if (!content) return true;
@@ -48,8 +74,8 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
} }
async checkIsTargetFile(file: UXFileInfoStub | FilePathWithPrefix): Promise<boolean> { async checkIsTargetFile(file: UXFileInfoStub | FilePathWithPrefix): Promise<boolean> {
const path = getPathFromUXFileInfo(file); const path = getStoragePathFromUXFileInfo(file);
if (!await this.core.$$isTargetFile(path)) { if (!(await this.core.$$isTargetFile(path))) {
this._log(`File is not target`, LOG_LEVEL_VERBOSE); this._log(`File is not target`, LOG_LEVEL_VERBOSE);
return false; return false;
} }
@@ -61,10 +87,10 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
} }
async delete(file: UXFileInfoStub | FilePathWithPrefix, rev?: string): Promise<boolean> { async delete(file: UXFileInfoStub | FilePathWithPrefix, rev?: string): Promise<boolean> {
if (!await this.checkIsTargetFile(file)) { if (!(await this.checkIsTargetFile(file))) {
return true; return true;
} }
const fullPath = getPathFromUXFileInfo(file); const fullPath = getDatabasePathFromUXFileInfo(file);
try { try {
this._log(`deleteDB By path:${fullPath}`); this._log(`deleteDB By path:${fullPath}`);
return await this.deleteFromDBbyPath(fullPath, rev); return await this.deleteFromDBbyPath(fullPath, rev);
@@ -85,6 +111,7 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
async storeContent(path: FilePathWithPrefix, content: string): Promise<boolean> { async storeContent(path: FilePathWithPrefix, content: string): Promise<boolean> {
const blob = createTextBlob(content); const blob = createTextBlob(content);
const bytes = (await blob.arrayBuffer()).byteLength; const bytes = (await blob.arrayBuffer()).byteLength;
const isInternal = path.startsWith(".") ? true : undefined;
const dummyUXFileInfo: UXFileInfo = { const dummyUXFileInfo: UXFileInfo = {
name: path.split("/").pop() as string, name: path.split("/").pop() as string,
path: path, path: path,
@@ -95,13 +122,19 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
type: "file", type: "file",
}, },
body: blob, body: blob,
} isInternal,
};
return await this._store(dummyUXFileInfo, true, false, false); return await this._store(dummyUXFileInfo, true, false, false);
} }
async _store(file: UXFileInfo, force: boolean = false, skipCheck?: boolean, onlyChunks?: boolean): Promise<boolean> { async _store(
file: UXFileInfo,
force: boolean = false,
skipCheck?: boolean,
onlyChunks?: boolean
): Promise<boolean> {
if (!skipCheck) { if (!skipCheck) {
if (!await this.checkIsTargetFile(file)) { if (!(await this.checkIsTargetFile(file))) {
return true; return true;
} }
} }
@@ -109,18 +142,47 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
this._log("File seems bad", LOG_LEVEL_VERBOSE); this._log("File seems bad", LOG_LEVEL_VERBOSE);
return false; return false;
} }
const path = getPathFromUXFileInfo(file); // const path = getPathFromUXFileInfo(file);
const isPlain = isPlainText(file.name); const isPlain = isPlainText(file.name);
const possiblyLarge = !isPlain; const possiblyLarge = !isPlain;
const content = file.body; const content = file.body;
if (possiblyLarge) this._log(`Processing: ${path}`, LOG_LEVEL_VERBOSE);
const datatype = determineTypeFromBlob(content); const datatype = determineTypeFromBlob(content);
const fullPath = file.path; const idPrefix = file.isInternal ? ICHeader : "";
const id = await this.core.$$path2id(fullPath); const fullPath = getStoragePathFromUXFileInfo(file);
const fullPathOnDB = getDatabasePathFromUXFileInfo(file);
if (possiblyLarge) this._log(`Processing: ${fullPath}`, LOG_LEVEL_VERBOSE);
// if (isInternalMetadata(fullPath)) {
// this._log(`Internal file: ${fullPath}`, LOG_LEVEL_VERBOSE);
// return false;
// }
if (file.isInternal) {
if (file.deleted) {
file.stat = {
size: 0,
ctime: Date.now(),
mtime: Date.now(),
type: "file",
};
} else if (file.stat == undefined) {
const stat = await this.core.storageAccess.statHidden(file.path);
if (!stat) {
// We stored actually deleted or not since here, so this is an unexpected case. we should raise an error.
this._log(`Internal file not found: ${fullPath}`, LOG_LEVEL_VERBOSE);
return false;
}
file.stat = stat;
}
}
const idMain = await this.core.$$path2id(fullPath);
const id = (idPrefix + idMain) as DocumentID;
const d: SavingEntry = { const d: SavingEntry = {
_id: id, _id: id,
path: file.path, path: fullPathOnDB,
data: content, data: content,
ctime: file.stat.ctime, ctime: file.stat.ctime,
mtime: file.stat.mtime, mtime: file.stat.mtime,
@@ -133,28 +195,38 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
//upsert should locked //upsert should locked
const msg = `STORAGE -> DB (${datatype}) `; const msg = `STORAGE -> DB (${datatype}) `;
const isNotChanged = await serialized("file-" + fullPath, async () => { const isNotChanged = await serialized("file-" + fullPath, async () => {
if (force) {
this._log(msg + "Force writing " + fullPath, LOG_LEVEL_VERBOSE);
return false;
}
// Commented out temporarily: this checks that the file was made ourself. // Commented out temporarily: this checks that the file was made ourself.
// if (this.core.storageAccess.recentlyTouched(file)) { // if (this.core.storageAccess.recentlyTouched(file)) {
// return true; // return true;
// } // }
try { try {
const old = await this.localDatabase.getDBEntry(fullPath, undefined, false, true, false); const old = await this.localDatabase.getDBEntry(d.path, undefined, false, true, false);
if (old !== false) { if (old !== false) {
const oldData = { data: old.data, deleted: old._deleted || old.deleted }; const oldData = { data: old.data, deleted: old._deleted || old.deleted };
const newData = { data: d.data, deleted: d._deleted || d.deleted }; const newData = { data: d.data, deleted: d._deleted || d.deleted };
if (oldData.deleted != newData.deleted) return false; if (oldData.deleted != newData.deleted) return false;
if (!await isDocContentSame(old.data, newData.data)) return false; if (!(await isDocContentSame(old.data, newData.data))) return false;
this._log(msg + "Skipped (not changed) " + fullPath + ((d._deleted || d.deleted) ? " (deleted)" : ""), LOG_LEVEL_VERBOSE); this._log(
msg + "Skipped (not changed) " + fullPath + (d._deleted || d.deleted ? " (deleted)" : ""),
LOG_LEVEL_VERBOSE
);
markChangesAreSame(old, d.mtime, old.mtime); markChangesAreSame(old, d.mtime, old.mtime);
return true; return true;
// d._rev = old._rev; // d._rev = old._rev;
} }
} catch (ex) { } catch (ex) {
if (force) { this._log(
this._log(msg + "Error, Could not check the diff for the old one." + (force ? "force writing." : "") + fullPath + ((d._deleted || d.deleted) ? " (deleted)" : ""), LOG_LEVEL_VERBOSE); msg +
} else { "Error, Could not check the diff for the old one." +
this._log(msg + "Error, Could not check the diff for the old one." + fullPath + ((d._deleted || d.deleted) ? " (deleted)" : ""), LOG_LEVEL_VERBOSE); (force ? "force writing." : "") +
} fullPath +
(d._deleted || d.deleted ? " (deleted)" : ""),
LOG_LEVEL_VERBOSE
);
this._log(ex, LOG_LEVEL_VERBOSE); this._log(ex, LOG_LEVEL_VERBOSE);
return !force; return !force;
} }
@@ -173,10 +245,10 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
} }
async getConflictedRevs(file: UXFileInfoStub | FilePathWithPrefix): Promise<string[]> { async getConflictedRevs(file: UXFileInfoStub | FilePathWithPrefix): Promise<string[]> {
if (!await this.checkIsTargetFile(file)) { if (!(await this.checkIsTargetFile(file))) {
return []; return [];
} }
const filename = getPathFromUXFileInfo(file); const filename = getDatabasePathFromUXFileInfo(file);
const doc = await this.localDatabase.getDBEntryMeta(filename, { conflicts: true }, true); const doc = await this.localDatabase.getDBEntryMeta(filename, { conflicts: true }, true);
if (doc === false) { if (doc === false) {
return []; return [];
@@ -184,9 +256,13 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
return doc._conflicts || []; return doc._conflicts || [];
} }
async fetch(file: UXFileInfoStub | FilePathWithPrefix, async fetch(
rev?: string, waitForReady?: boolean, skipCheck = false): Promise<UXFileInfo | false> { file: UXFileInfoStub | FilePathWithPrefix,
if (skipCheck && !await this.checkIsTargetFile(file)) { rev?: string,
waitForReady?: boolean,
skipCheck = false
): Promise<UXFileInfo | false> {
if (skipCheck && !(await this.checkIsTargetFile(file))) {
return false; return false;
} }
@@ -195,9 +271,10 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
return false; return false;
} }
const data = createBlob(readContent(entry)); const data = createBlob(readContent(entry));
const path = stripAllPrefixes(entry.path);
const fileInfo: UXFileInfo = { const fileInfo: UXFileInfo = {
name: entry.path.split("/").pop() as string, name: path.split("/").pop() as string,
path: entry.path, path: path,
stat: { stat: {
size: entry.size, size: entry.size,
ctime: entry.ctime, ctime: entry.ctime,
@@ -206,39 +283,49 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
}, },
body: data, body: data,
deleted: entry.deleted || entry._deleted, deleted: entry.deleted || entry._deleted,
} };
if (isInternalMetadata(entry.path)) { if (isInternalMetadata(entry.path)) {
fileInfo.isInternal = true; fileInfo.isInternal = true;
} }
return fileInfo; return fileInfo;
} }
async fetchEntryMeta(file: UXFileInfoStub | FilePathWithPrefix, async fetchEntryMeta(
rev?: string, skipCheck = false): Promise<MetaEntry | false> { file: UXFileInfoStub | FilePathWithPrefix,
const filename = getPathFromUXFileInfo(file); rev?: string,
if (skipCheck && !await this.checkIsTargetFile(file)) { skipCheck = false
): Promise<MetaEntry | false> {
const dbFileName = getDatabasePathFromUXFileInfo(file);
if (skipCheck && !(await this.checkIsTargetFile(file))) {
return false; return false;
} }
const doc = await this.localDatabase.getDBEntryMeta( const doc = await this.localDatabase.getDBEntryMeta(dbFileName, rev ? { rev: rev } : undefined, true);
filename, rev ? { rev: rev } : undefined, true);
if (doc === false) { if (doc === false) {
return false; return false;
} }
return doc as MetaEntry; return doc as MetaEntry;
} }
async fetchEntryFromMeta(meta: MetaEntry, waitForReady: boolean = true, skipCheck = false): Promise<LoadedEntry | false> { async fetchEntryFromMeta(
if (skipCheck && !await this.checkIsTargetFile(meta.path)) { meta: MetaEntry,
waitForReady: boolean = true,
skipCheck = false
): Promise<LoadedEntry | false> {
if (skipCheck && !(await this.checkIsTargetFile(meta.path))) {
return false; return false;
} }
const doc = await this.localDatabase.getDBEntryFromMeta(meta as LoadedEntry, undefined, false, waitForReady, true); const doc = await this.localDatabase.getDBEntryFromMeta(meta as LoadedEntry, false, waitForReady);
if (doc === false) { if (doc === false) {
return false; return false;
} }
return doc; return doc;
} }
async fetchEntry(file: UXFileInfoStub | FilePathWithPrefix, async fetchEntry(
rev?: string, waitForReady: boolean = true, skipCheck = false): Promise<LoadedEntry | false> { file: UXFileInfoStub | FilePathWithPrefix,
if (skipCheck && !await this.checkIsTargetFile(file)) { rev?: string,
waitForReady: boolean = true,
skipCheck = false
): Promise<LoadedEntry | false> {
if (skipCheck && !(await this.checkIsTargetFile(file))) {
return false; return false;
} }
const entry = await this.fetchEntryMeta(file, rev, true); const entry = await this.fetchEntryMeta(file, rev, true);
@@ -249,7 +336,7 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
return doc; return doc;
} }
async deleteFromDBbyPath(fullPath: FilePath | FilePathWithPrefix, rev?: string): Promise<boolean> { async deleteFromDBbyPath(fullPath: FilePath | FilePathWithPrefix, rev?: string): Promise<boolean> {
if (!await this.checkIsTargetFile(fullPath)) { if (!(await this.checkIsTargetFile(fullPath))) {
this._log(`storeFromStorage: File is not target: ${fullPath}`); this._log(`storeFromStorage: File is not target: ${fullPath}`);
return true; return true;
} }
@@ -258,5 +345,4 @@ export class ModuleDatabaseFileAccess extends AbstractModule implements IObsidia
eventHub.emitEvent(EVENT_FILE_SAVED); eventHub.emitEvent(EVENT_FILE_SAVED);
return ret; return ret;
} }
} }

View File

@@ -1,16 +1,30 @@
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import { serialized } from "octagonal-wheels/concurrency/lock"; import { serialized } from "octagonal-wheels/concurrency/lock";
import type { FileEventItem } from "../../common/types"; import type { FileEventItem } from "../../common/types";
import type { FilePath, FilePathWithPrefix, MetaEntry, UXFileInfo, UXFileInfoStub, UXInternalFileInfoStub } from "../../lib/src/common/types"; import type {
FilePath,
FilePathWithPrefix,
MetaEntry,
UXFileInfo,
UXFileInfoStub,
UXInternalFileInfoStub,
} from "../../lib/src/common/types";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import { compareFileFreshness, EVEN, getPath, getPathFromUXFileInfo, getPathWithoutPrefix, markChangesAreSame } from "../../common/utils"; import {
compareFileFreshness,
EVEN,
getPath,
getPathWithoutPrefix,
getStoragePathFromUXFileInfo,
markChangesAreSame,
} from "../../common/utils";
import { getDocDataAsArray, isDocContentSame, readContent } from "../../lib/src/common/utils"; import { getDocDataAsArray, isDocContentSame, readContent } from "../../lib/src/common/utils";
import { shouldBeIgnored } from "../../lib/src/string_and_binary/path"; import { shouldBeIgnored } from "../../lib/src/string_and_binary/path";
import type { ICoreModule } from "../ModuleTypes"; import type { ICoreModule } from "../ModuleTypes";
import { Semaphore } from "octagonal-wheels/concurrency/semaphore"; import { Semaphore } from "octagonal-wheels/concurrency/semaphore";
import { eventHub } from "../../common/events.ts";
export class ModuleFileHandler extends AbstractModule implements ICoreModule { export class ModuleFileHandler extends AbstractModule implements ICoreModule {
get db() { get db() {
return this.core.databaseFileAccess; return this.core.databaseFileAccess;
} }
@@ -31,10 +45,14 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
if (!readFile) { if (!readFile) {
throw new Error(`File ${file.path} is not exist on the storage`); throw new Error(`File ${file.path} is not exist on the storage`);
} }
return readFile return readFile;
} }
async storeFileToDB(info: UXFileInfoStub | UXFileInfo | UXInternalFileInfoStub | FilePathWithPrefix, force: boolean = false, onlyChunks: boolean = false): Promise<boolean | undefined> { async storeFileToDB(
info: UXFileInfoStub | UXFileInfo | UXInternalFileInfoStub | FilePathWithPrefix,
force: boolean = false,
onlyChunks: boolean = false
): Promise<boolean | undefined> {
const file = typeof info === "string" ? this.storage.getFileStub(info) : info; const file = typeof info === "string" ? this.storage.getFileStub(info) : info;
if (file == null) { if (file == null) {
this._log(`File ${info} is not exist on the storage`, LOG_LEVEL_VERBOSE); this._log(`File ${info} is not exist on the storage`, LOG_LEVEL_VERBOSE);
@@ -42,7 +60,10 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
} }
// const file = item.args.file; // const file = item.args.file;
if (file.isInternal) { if (file.isInternal) {
this._log(`Internal file ${file.path} is not allowed to be processed on processFileEvent`, LOG_LEVEL_VERBOSE); this._log(
`Internal file ${file.path} is not allowed to be processed on processFileEvent`,
LOG_LEVEL_VERBOSE
);
return false; return false;
} }
// First, check the file on the database // First, check the file on the database
@@ -74,11 +95,9 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
if (!shouldApplied) { if (!shouldApplied) {
readFile = await this.readFileFromStub(file); readFile = await this.readFileFromStub(file);
if (await isDocContentSame(getDocDataAsArray(entry.data), readFile.body)) { if (await isDocContentSame(getDocDataAsArray(entry.data), readFile.body)) {
if (shouldApplied) { // Timestamp is different but the content is same. therefore, two timestamps should be handled as same.
// Timestamp is different but the content is same. therefore, two timestamps should be handled as same. // So, mark the changes are same.
// So, mark the changes are same. markChangesAreSame(file, file.stat.mtime, entry.mtime);
markChangesAreSame(file, file.stat.mtime, entry.mtime);
}
} else { } else {
shouldApplied = true; shouldApplied = true;
} }
@@ -114,7 +133,10 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
} }
// const file = item.args.file; // const file = item.args.file;
if (file.isInternal) { if (file.isInternal) {
this._log(`Internal file ${file.path} is not allowed to be processed on processFileEvent`, LOG_LEVEL_VERBOSE); this._log(
`Internal file ${file.path} is not allowed to be processed on processFileEvent`,
LOG_LEVEL_VERBOSE
);
return false; return false;
} }
// First, check the file on the database // First, check the file on the database
@@ -135,24 +157,34 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
return await this.db.delete(file); return await this.db.delete(file);
} }
async deleteRevisionFromDB(info: UXFileInfoStub | FilePath | FilePathWithPrefix, rev: string): Promise<boolean | undefined> { async deleteRevisionFromDB(
info: UXFileInfoStub | FilePath | FilePathWithPrefix,
rev: string
): Promise<boolean | undefined> {
//TODO: Possibly check the conflicting. //TODO: Possibly check the conflicting.
return await this.db.delete(info, rev); return await this.db.delete(info, rev);
} }
async resolveConflictedByDeletingRevision(
async resolveConflictedByDeletingRevision(info: UXFileInfoStub | FilePath, rev: string): Promise<boolean | undefined> { info: UXFileInfoStub | FilePath,
if (!await this.deleteRevisionFromDB(info, rev)) { rev: string
this._log(`Failed to delete the conflicted revision ${rev} of ${getPathFromUXFileInfo(info)}`, LOG_LEVEL_VERBOSE); ): Promise<boolean | undefined> {
const path = getStoragePathFromUXFileInfo(info);
if (!(await this.deleteRevisionFromDB(info, rev))) {
this._log(`Failed to delete the conflicted revision ${rev} of ${path}`, LOG_LEVEL_VERBOSE);
return false; return false;
} }
if (!await this.dbToStorageWithSpecificRev(info, rev, true)) { if (!(await this.dbToStorageWithSpecificRev(info, rev, true))) {
this._log(`Failed to apply the resolved revision ${rev} of ${getPathFromUXFileInfo(info)} to the storage`, LOG_LEVEL_VERBOSE); this._log(`Failed to apply the resolved revision ${rev} of ${path} to the storage`, LOG_LEVEL_VERBOSE);
return false; return false;
} }
} }
async dbToStorageWithSpecificRev(info: UXFileInfoStub | UXFileInfo | FilePath | null, rev: string, force?: boolean): Promise<boolean> { async dbToStorageWithSpecificRev(
info: UXFileInfoStub | UXFileInfo | FilePath | null,
rev: string,
force?: boolean
): Promise<boolean> {
const file = typeof info === "string" ? this.storage.getFileStub(info) : info; const file = typeof info === "string" ? this.storage.getFileStub(info) : info;
if (file == null) { if (file == null) {
this._log(`File ${info} is not exist on the storage`, LOG_LEVEL_VERBOSE); this._log(`File ${info} is not exist on the storage`, LOG_LEVEL_VERBOSE);
@@ -166,14 +198,17 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
return await this.dbToStorage(docEntry, file, force); return await this.dbToStorage(docEntry, file, force);
} }
async dbToStorage(entryInfo: MetaEntry | FilePathWithPrefix, info: UXFileInfoStub | UXFileInfo | FilePath | null, force?: boolean): Promise<boolean> { async dbToStorage(
entryInfo: MetaEntry | FilePathWithPrefix,
info: UXFileInfoStub | UXFileInfo | FilePath | null,
force?: boolean
): Promise<boolean> {
const file = typeof info === "string" ? this.storage.getFileStub(info) : info; const file = typeof info === "string" ? this.storage.getFileStub(info) : info;
const mode = file == null ? "create" : "modify"; const mode = file == null ? "create" : "modify";
const pathFromEntryInfo = typeof entryInfo === "string" ? entryInfo : getPath(entryInfo);
const docEntry = typeof entryInfo === "string" ? const docEntry = await this.db.fetchEntryMeta(pathFromEntryInfo, undefined, true);
await this.db.fetchEntryMeta(entryInfo, undefined, true) : await this.db.fetchEntryMeta(entryInfo.path, undefined, true);
if (!docEntry) { if (!docEntry) {
this._log(`File ${entryInfo} is not exist on the database`, LOG_LEVEL_VERBOSE); this._log(`File ${pathFromEntryInfo} is not exist on the database`, LOG_LEVEL_VERBOSE);
return false; return false;
} }
const path = getPath(docEntry); const path = getPath(docEntry);
@@ -223,12 +258,9 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
} }
const docData = readContent(docRead); const docData = readContent(docRead);
if (!existOnStorage) { if (existOnStorage && !force) {
// The file is not exist on the storage. We do not care about the differences. // The file is exist on the storage. Let's check the difference between the file and the entry.
await this.storage.ensureDir(path); // But, if force is true, then it should be updated.
return await this.storage.writeFileAuto(path, docData, { ctime: docRead.ctime, mtime: docRead.mtime });
}
if (!force) {
// Ok, we have to compare. // Ok, we have to compare.
let shouldApplied = false; let shouldApplied = false;
// 1. if the time stamp is far different, then it should be updated. // 1. if the time stamp is far different, then it should be updated.
@@ -240,7 +272,7 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
} }
// 2. if not, the content should be checked. // 2. if not, the content should be checked.
if (shouldApplied) { if (!shouldApplied) {
const readFile = await this.readFileFromStub(existDoc); const readFile = await this.readFileFromStub(existDoc);
if (await isDocContentSame(docData, readFile.body)) { if (await isDocContentSame(docData, readFile.body)) {
// The content is same. So, we do not need to update the file. // The content is same. So, we do not need to update the file.
@@ -257,10 +289,15 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
return true; return true;
} }
// Let's apply the changes. // Let's apply the changes.
} else {
this._log(
`File ${docRead.path} ${existOnStorage ? "(new) " : ""} ${force ? " (forced)" : ""}`,
LOG_LEVEL_VERBOSE
);
} }
await this.storage.ensureDir(path); await this.storage.ensureDir(path);
const ret = await this.storage.writeFileAuto(path, docData, { ctime: docRead.ctime, mtime: docRead.mtime }); const ret = await this.storage.writeFileAuto(path, docData, { ctime: docRead.ctime, mtime: docRead.mtime });
this.storage.touched(path); await this.storage.touched(path);
this.storage.triggerFileEvent(mode, path); this.storage.triggerFileEvent(mode, path);
return ret; return ret;
} }
@@ -269,7 +306,7 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
const eventItem = item.args; const eventItem = item.args;
const type = item.type; const type = item.type;
const path = eventItem.file.path; const path = eventItem.file.path;
if (!await this.core.$$isTargetFile(path)) { if (!(await this.core.$$isTargetFile(path))) {
this._log(`File ${path} is not the target file`, LOG_LEVEL_VERBOSE); this._log(`File ${path} is not the target file`, LOG_LEVEL_VERBOSE);
return false; return false;
} }
@@ -297,7 +334,7 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
async $anyProcessReplicatedDoc(entry: MetaEntry): Promise<boolean | undefined> { async $anyProcessReplicatedDoc(entry: MetaEntry): Promise<boolean | undefined> {
return await serialized(entry.path, async () => { return await serialized(entry.path, async () => {
if (!await this.core.$$isTargetFile(entry.path)) { if (!(await this.core.$$isTargetFile(entry.path))) {
this._log(`File ${entry.path} is not the target file`, LOG_LEVEL_VERBOSE); this._log(`File ${entry.path} is not the target file`, LOG_LEVEL_VERBOSE);
return false; return false;
} }
@@ -313,13 +350,17 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
// Nothing to do and other modules should also nothing to do. // Nothing to do and other modules should also nothing to do.
return true; return true;
} else { } else {
this._log(`Processing ${path} (${entry._id.substring(0, 8)}: ${entry._rev?.substring(0, 5)}) :Started...`, LOG_LEVEL_VERBOSE); this._log(
`Processing ${path} (${entry._id.substring(0, 8)}: ${entry._rev?.substring(0, 5)}) :Started...`,
LOG_LEVEL_VERBOSE
);
// Before writing (or skipped ), merging dialogue should be cancelled.
eventHub.emitEvent("conflict-cancelled", path);
const ret = await this.dbToStorage(entry, targetFile); const ret = await this.dbToStorage(entry, targetFile);
this._log(`Processing ${path} (${entry._id.substring(0, 8)} :${entry._rev?.substring(0, 5)}) : Done`); this._log(`Processing ${path} (${entry._id.substring(0, 8)} :${entry._rev?.substring(0, 5)}) : Done`);
return ret; return ret;
} }
}); });
} }
async createAllChunks(showingNotice?: boolean): Promise<void> { async createAllChunks(showingNotice?: boolean): Promise<void> {
@@ -330,11 +371,16 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
const filesStorageSrc = this.storage.getFiles(); const filesStorageSrc = this.storage.getFiles();
const incProcessed = () => { const incProcessed = () => {
processed++; processed++;
if (processed % 25 == 0) this._log(`Creating missing chunks: ${processed} of ${total} files`, showingNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO, "chunkCreation"); if (processed % 25 == 0)
} this._log(
`Creating missing chunks: ${processed} of ${total} files`,
showingNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO,
"chunkCreation"
);
};
const total = filesStorageSrc.length; const total = filesStorageSrc.length;
const procAllChunks = filesStorageSrc.map(async (file) => { const procAllChunks = filesStorageSrc.map(async (file) => {
if (!await this.core.$$isTargetFile(file)) { if (!(await this.core.$$isTargetFile(file))) {
incProcessed(); incProcessed();
return true; return true;
} }
@@ -353,6 +399,10 @@ export class ModuleFileHandler extends AbstractModule implements ICoreModule {
} }
}); });
await Promise.all(procAllChunks); await Promise.all(procAllChunks);
this._log(`Creating chunks Done: ${processed} of ${total} files`, showingNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO, "chunkCreation"); this._log(
`Creating chunks Done: ${processed} of ${total} files`,
showingNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO,
"chunkCreation"
);
} }
} }

View File

@@ -1,11 +1,10 @@
import { $f } from "../../lib/src/common/i18n"; import { $msg } from "../../lib/src/common/i18n";
import { LiveSyncLocalDB } from "../../lib/src/pouchdb/LiveSyncLocalDB.ts"; import { LiveSyncLocalDB } from "../../lib/src/pouchdb/LiveSyncLocalDB.ts";
import { initializeStores } from "../../common/stores.ts"; import { initializeStores } from "../../common/stores.ts";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
export class ModuleLocalDatabaseObsidian extends AbstractModule implements ICoreModule { export class ModuleLocalDatabaseObsidian extends AbstractModule implements ICoreModule {
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
return Promise.resolve(true); return Promise.resolve(true);
} }
@@ -14,10 +13,13 @@ export class ModuleLocalDatabaseObsidian extends AbstractModule implements ICore
await this.localDatabase.close(); await this.localDatabase.close();
} }
const vaultName = this.core.$$getVaultName(); const vaultName = this.core.$$getVaultName();
this._log($f`Waiting for ready...`); this._log($msg("moduleLocalDatabase.logWaitingForReady"));
this.core.localDatabase = new LiveSyncLocalDB(vaultName, this.core); this.core.localDatabase = new LiveSyncLocalDB(vaultName, this.core);
initializeStores(vaultName); initializeStores(vaultName);
return await this.localDatabase.initializeDatabase(); return await this.localDatabase.initializeDatabase();
} }
$$isDatabaseReady(): boolean {
return this.localDatabase != null && this.localDatabase.isReady;
}
} }

View File

@@ -3,7 +3,6 @@ import { AbstractModule } from "../AbstractModule";
import type { ICoreModule } from "../ModuleTypes"; import type { ICoreModule } from "../ModuleTypes";
export class ModulePeriodicProcess extends AbstractModule implements ICoreModule { export class ModulePeriodicProcess extends AbstractModule implements ICoreModule {
periodicSyncProcessor = new PeriodicProcessor(this.core, async () => await this.core.$$replicate()); periodicSyncProcessor = new PeriodicProcessor(this.core, async () => await this.core.$$replicate());
_disablePeriodic() { _disablePeriodic() {
@@ -11,20 +10,19 @@ export class ModulePeriodicProcess extends AbstractModule implements ICoreModule
return Promise.resolve(true); return Promise.resolve(true);
} }
_resumePeriodic() { _resumePeriodic() {
this.periodicSyncProcessor.enable(this.settings.periodicReplication ? this.settings.periodicReplicationInterval * 1000 : 0); this.periodicSyncProcessor.enable(
this.settings.periodicReplication ? this.settings.periodicReplicationInterval * 1000 : 0
);
return Promise.resolve(true); return Promise.resolve(true);
} }
$allOnUnload() { $allOnUnload() {
return this._disablePeriodic(); return this._disablePeriodic();
} }
$everyBeforeRealizeSetting(): Promise<boolean> { $everyBeforeRealizeSetting(): Promise<boolean> {
return this._disablePeriodic(); return this._disablePeriodic();
} }
$everyBeforeSuspendProcess(): Promise<boolean> { $everyBeforeSuspendProcess(): Promise<boolean> {
return this._disablePeriodic(); return this._disablePeriodic();
} }
$everyAfterResumeProcess(): Promise<boolean> { $everyAfterResumeProcess(): Promise<boolean> {
return this._resumePeriodic(); return this._resumePeriodic();

View File

@@ -3,7 +3,10 @@ import type { ICoreModule } from "../ModuleTypes";
import { PouchDB } from "../../lib/src/pouchdb/pouchdb-browser"; import { PouchDB } from "../../lib/src/pouchdb/pouchdb-browser";
export class ModulePouchDB extends AbstractModule implements ICoreModule { export class ModulePouchDB extends AbstractModule implements ICoreModule {
$$createPouchDBInstance<T extends object>(name?: string, options?: PouchDB.Configuration.DatabaseConfiguration): PouchDB.Database<T> { $$createPouchDBInstance<T extends object>(
name?: string,
options?: PouchDB.Configuration.DatabaseConfiguration
): PouchDB.Database<T> {
const optionPass = options ?? {}; const optionPass = options ?? {};
if (this.settings.useIndexedDBAdapter) { if (this.settings.useIndexedDBAdapter) {
optionPass.adapter = "indexeddb"; optionPass.adapter = "indexeddb";

View File

@@ -1,18 +1,27 @@
import { delay } from "octagonal-wheels/promises"; import { delay } from "octagonal-wheels/promises";
import { FLAGMD_REDFLAG2_HR, FLAGMD_REDFLAG3_HR, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE, REMOTE_COUCHDB, REMOTE_MINIO } from "../../lib/src/common/types.ts"; import {
FLAGMD_REDFLAG2_HR,
FLAGMD_REDFLAG3_HR,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
REMOTE_COUCHDB,
REMOTE_MINIO,
} from "../../lib/src/common/types.ts";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import type { Rebuilder } from "../interfaces/DatabaseRebuilder.ts"; import type { Rebuilder } from "../interfaces/DatabaseRebuilder.ts";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
import type { LiveSyncCouchDBReplicator } from "../../lib/src/replication/couchdb/LiveSyncReplicator.ts"; import type { LiveSyncCouchDBReplicator } from "../../lib/src/replication/couchdb/LiveSyncReplicator.ts";
import { fetchAllUsedChunks } from "../../lib/src/pouchdb/utils_couchdb.ts"; import { fetchAllUsedChunks } from "@/lib/src/pouchdb/chunks.ts";
import { EVENT_DATABASE_REBUILT, eventHub } from "src/common/events.ts";
export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebuilder { export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebuilder {
$everyOnload(): Promise<boolean> { $everyOnload(): Promise<boolean> {
this.core.rebuilder = this; this.core.rebuilder = this;
return Promise.resolve(true); return Promise.resolve(true);
} }
async $performRebuildDB(method: "localOnly" | "remoteOnly" | "rebuildBothByThisDevice" | "localOnlyWithChunks"): Promise<void> { async $performRebuildDB(
method: "localOnly" | "remoteOnly" | "rebuildBothByThisDevice" | "localOnlyWithChunks"
): Promise<void> {
if (method == "localOnly") { if (method == "localOnly") {
await this.$fetchLocal(); await this.$fetchLocal();
} }
@@ -27,11 +36,13 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
} }
} }
async askUsingOptionalFeature(opt: { async askUsingOptionalFeature(opt: { enableFetch?: boolean; enableOverwrite?: boolean }) {
enableFetch?: boolean; if (
enableOverwrite?: boolean; (await this.core.confirm.askYesNoDialog(
}) { "Do you want to enable extra features? If you are new to Self-hosted LiveSync, try the core feature first!",
if (await this.core.confirm.askYesNoDialog("Do you want to enable extra features? If you are new to Self-hosted LiveSync, try the core feature first!", { title: "Enable extra features", defaultOption: "No", timeout: 15 }) == "yes") { { title: "Enable extra features", defaultOption: "No", timeout: 15 }
)) == "yes"
) {
await this.core.$allAskUsingOptionalSyncFeature(opt); await this.core.$allAskUsingOptionalSyncFeature(opt);
} }
} }
@@ -40,7 +51,7 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
await this.core.$allSuspendExtraSync(); await this.core.$allSuspendExtraSync();
this.core.settings.isConfigured = true; this.core.settings.isConfigured = true;
await this.core.realizeSettingSyncMode(); await this.core.$$realizeSettingSyncMode();
await this.core.$$markRemoteLocked(); await this.core.$$markRemoteLocked();
await this.core.$$tryResetRemoteDatabase(); await this.core.$$tryResetRemoteDatabase();
await this.core.$$markRemoteLocked(); await this.core.$$markRemoteLocked();
@@ -55,12 +66,11 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
return this.rebuildRemote(); return this.rebuildRemote();
} }
async rebuildEverything() { async rebuildEverything() {
await this.core.$allSuspendExtraSync(); await this.core.$allSuspendExtraSync();
await this.askUseNewAdapter(); await this.askUseNewAdapter();
this.core.settings.isConfigured = true; this.core.settings.isConfigured = true;
await this.core.realizeSettingSyncMode(); await this.core.$$realizeSettingSyncMode();
await this.resetLocalDatabase(); await this.resetLocalDatabase();
await delay(1000); await delay(1000);
await this.core.$$initializeDatabase(true); await this.core.$$initializeDatabase(true);
@@ -68,20 +78,20 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
await this.core.$$tryResetRemoteDatabase(); await this.core.$$tryResetRemoteDatabase();
await this.core.$$markRemoteLocked(); await this.core.$$markRemoteLocked();
await delay(500); await delay(500);
await this.askUsingOptionalFeature({ enableOverwrite: true }); // We do not have any other devices' data, so we do not need to ask for overwriting.
await this.askUsingOptionalFeature({ enableOverwrite: false });
await delay(1000); await delay(1000);
await this.core.$$replicateAllToServer(true); await this.core.$$replicateAllToServer(true);
await delay(1000); await delay(1000);
await this.core.$$replicateAllToServer(true, true); await this.core.$$replicateAllToServer(true, true);
} }
$rebuildEverything(): Promise<void> { $rebuildEverything(): Promise<void> {
return this.rebuildEverything(); return this.rebuildEverything();
} }
$fetchLocal(makeLocalChunkBeforeSync?: boolean): Promise<void> { $fetchLocal(makeLocalChunkBeforeSync?: boolean, preventMakeLocalFilesBeforeSync?: boolean): Promise<void> {
return this.fetchLocal(makeLocalChunkBeforeSync); return this.fetchLocal(makeLocalChunkBeforeSync, preventMakeLocalFilesBeforeSync);
} }
async scheduleRebuild(): Promise<void> { async scheduleRebuild(): Promise<void> {
@@ -116,7 +126,6 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
await this.localDatabase.resetDatabase(); await this.localDatabase.resetDatabase();
} }
async suspendAllSync() { async suspendAllSync() {
this.core.settings.liveSync = false; this.core.settings.liveSync = false;
this.core.settings.periodicReplication = false; this.core.settings.periodicReplication = false;
@@ -130,7 +139,10 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
async suspendReflectingDatabase() { async suspendReflectingDatabase() {
if (this.core.settings.doNotSuspendOnFetching) return; if (this.core.settings.doNotSuspendOnFetching) return;
if (this.core.settings.remoteType == REMOTE_MINIO) return; if (this.core.settings.remoteType == REMOTE_MINIO) return;
this._log(`Suspending reflection: Database and storage changes will not be reflected in each other until completely finished the fetching.`, LOG_LEVEL_NOTICE); this._log(
`Suspending reflection: Database and storage changes will not be reflected in each other until completely finished the fetching.`,
LOG_LEVEL_NOTICE
);
this.core.settings.suspendParseReplicationResult = true; this.core.settings.suspendParseReplicationResult = true;
this.core.settings.suspendFileWatching = true; this.core.settings.suspendFileWatching = true;
await this.core.saveSettings(); await this.core.saveSettings();
@@ -144,7 +156,6 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
await this.core.$$performFullScan(true); await this.core.$$performFullScan(true);
await this.core.$everyBeforeReplicate(false); //TODO: Check actual need of this. await this.core.$everyBeforeReplicate(false); //TODO: Check actual need of this.
await this.core.saveSettings(); await this.core.saveSettings();
} }
async askUseNewAdapter() { async askUseNewAdapter() {
if (!this.core.settings.useIndexedDBAdapter) { if (!this.core.settings.useIndexedDBAdapter) {
@@ -153,24 +164,35 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
const CHOICE_NO = "No, keep compatibility"; const CHOICE_NO = "No, keep compatibility";
const choices = [CHOICE_YES, CHOICE_NO]; const choices = [CHOICE_YES, CHOICE_NO];
const ret = await this.core.confirm.confirmWithMessage("Database adapter", message, choices, CHOICE_YES, 10); const ret = await this.core.confirm.confirmWithMessage(
"Database adapter",
message,
choices,
CHOICE_YES,
10
);
if (ret == CHOICE_YES) { if (ret == CHOICE_YES) {
this.core.settings.useIndexedDBAdapter = true; this.core.settings.useIndexedDBAdapter = true;
} }
} }
} }
async fetchLocal(makeLocalChunkBeforeSync?: boolean) { async fetchLocal(makeLocalChunkBeforeSync?: boolean, preventMakeLocalFilesBeforeSync?: boolean) {
await this.core.$allSuspendExtraSync(); await this.core.$allSuspendExtraSync();
await this.askUseNewAdapter(); await this.askUseNewAdapter();
this.core.settings.isConfigured = true; this.core.settings.isConfigured = true;
await this.suspendReflectingDatabase(); await this.suspendReflectingDatabase();
await this.core.realizeSettingSyncMode(); await this.core.$$realizeSettingSyncMode();
await this.resetLocalDatabase(); await this.resetLocalDatabase();
await delay(1000); await delay(1000);
await this.core.$$openDatabase(); await this.core.$$openDatabase();
this.core.isReady = true; // this.core.isReady = true;
this.core.$$markIsReady();
if (makeLocalChunkBeforeSync) { if (makeLocalChunkBeforeSync) {
await this.core.fileHandler.createAllChunks(true); await this.core.fileHandler.createAllChunks(true);
} else if (!preventMakeLocalFilesBeforeSync) {
await this.core.$$initializeDatabase(true);
} else {
// Do not create local file entries before sync (Means use remote information)
} }
await this.core.$$markRemoteResolved(); await this.core.$$markRemoteResolved();
await delay(500); await delay(500);
@@ -197,12 +219,21 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
const suffix = (await this.core.$anyGetAppId()) || ""; const suffix = (await this.core.$anyGetAppId()) || "";
this.core.settings.additionalSuffixOfDatabaseName = suffix; this.core.settings.additionalSuffixOfDatabaseName = suffix;
await this.core.$$resetLocalDatabase(); await this.core.$$resetLocalDatabase();
eventHub.emitEvent(EVENT_DATABASE_REBUILT);
} }
async fetchRemoteChunks() { async fetchRemoteChunks() {
if (!this.core.settings.doNotSuspendOnFetching && this.core.settings.readChunksOnline && this.core.settings.remoteType == REMOTE_COUCHDB) { if (
!this.core.settings.doNotSuspendOnFetching &&
this.core.settings.readChunksOnline &&
this.core.settings.remoteType == REMOTE_COUCHDB
) {
this._log(`Fetching chunks`, LOG_LEVEL_NOTICE); this._log(`Fetching chunks`, LOG_LEVEL_NOTICE);
const replicator = this.core.getReplicator() as LiveSyncCouchDBReplicator; const replicator = this.core.$$getReplicator() as LiveSyncCouchDBReplicator;
const remoteDB = await replicator.connectRemoteCouchDBWithSetting(this.settings, this.core.getIsMobile(), true); const remoteDB = await replicator.connectRemoteCouchDBWithSetting(
this.settings,
this.core.$$isMobile(),
true
);
if (typeof remoteDB == "string") { if (typeof remoteDB == "string") {
this._log(remoteDB, LOG_LEVEL_NOTICE); this._log(remoteDB, LOG_LEVEL_NOTICE);
} else { } else {
@@ -217,7 +248,12 @@ export class ModuleRebuilder extends AbstractModule implements ICoreModule, Rebu
let i = 0; let i = 0;
for (const file of files) { for (const file of files) {
if (i++ % 10) this._log(`Check and Processing ${i} / ${files.length}`, LOG_LEVEL_NOTICE, "resolveAllConflictedFilesByNewerOnes"); if (i++ % 10)
this._log(
`Check and Processing ${i} / ${files.length}`,
LOG_LEVEL_NOTICE,
"resolveAllConflictedFilesByNewerOnes"
);
await this.core.$anyResolveConflictByNewest(file); await this.core.$anyResolveConflictByNewest(file);
} }
this._log(`Done!`, LOG_LEVEL_NOTICE, "resolveAllConflictedFilesByNewerOnes"); this._log(`Done!`, LOG_LEVEL_NOTICE, "resolveAllConflictedFilesByNewerOnes");

View File

@@ -4,171 +4,248 @@ import { AbstractModule } from "../AbstractModule";
import type { ICoreModule } from "../ModuleTypes"; import type { ICoreModule } from "../ModuleTypes";
import { Logger, LOG_LEVEL_NOTICE, LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger"; import { Logger, LOG_LEVEL_NOTICE, LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import { isLockAcquired, shareRunningResult, skipIfDuplicated } from "octagonal-wheels/concurrency/lock"; import { isLockAcquired, shareRunningResult, skipIfDuplicated } from "octagonal-wheels/concurrency/lock";
import { purgeUnreferencedChunks, balanceChunkPurgedDBs } from "../../lib/src/pouchdb/utils_couchdb"; import { balanceChunkPurgedDBs } from "@/lib/src/pouchdb/chunks";
import { purgeUnreferencedChunks } from "@/lib/src/pouchdb/chunks";
import { LiveSyncCouchDBReplicator } from "../../lib/src/replication/couchdb/LiveSyncReplicator"; import { LiveSyncCouchDBReplicator } from "../../lib/src/replication/couchdb/LiveSyncReplicator";
import { throttle } from "octagonal-wheels/function"; import { throttle } from "octagonal-wheels/function";
import { arrayToChunkedArray } from "octagonal-wheels/collection"; import { arrayToChunkedArray } from "octagonal-wheels/collection";
import { SYNCINFO_ID, VER, type EntryBody, type EntryDoc, type LoadedEntry, type MetaEntry } from "../../lib/src/common/types"; import {
SYNCINFO_ID,
VER,
type EntryBody,
type EntryDoc,
type EntryLeaf,
type LoadedEntry,
type MetaEntry,
} from "../../lib/src/common/types";
import { QueueProcessor } from "octagonal-wheels/concurrency/processor"; import { QueueProcessor } from "octagonal-wheels/concurrency/processor";
import { getPath, isChunk, isValidPath, scheduleTask } from "../../common/utils"; import {
import { sendValue } from "octagonal-wheels/messagepassing/signal"; getPath,
isChunk,
isValidPath,
rateLimitedSharedExecution,
scheduleTask,
updatePreviousExecutionTime,
} from "../../common/utils";
import { isAnyNote } from "../../lib/src/common/utils"; import { isAnyNote } from "../../lib/src/common/utils";
import { EVENT_FILE_SAVED, eventHub } from "../../common/events"; import { EVENT_FILE_SAVED, EVENT_SETTING_SAVED, eventHub } from "../../common/events";
import type { LiveSyncAbstractReplicator } from "../../lib/src/replication/LiveSyncAbstractReplicator";
import { globalSlipBoard } from "../../lib/src/bureau/bureau";
import { $msg } from "../../lib/src/common/i18n";
const KEY_REPLICATION_ON_EVENT = "replicationOnEvent";
const REPLICATION_ON_EVENT_FORECASTED_TIME = 5000;
export class ModuleReplicator extends AbstractModule implements ICoreModule { export class ModuleReplicator extends AbstractModule implements ICoreModule {
_replicatorType?: string;
$everyOnloadAfterLoadSettings(): Promise<boolean> { $everyOnloadAfterLoadSettings(): Promise<boolean> {
eventHub.onEvent(EVENT_FILE_SAVED, () => { eventHub.onEvent(EVENT_FILE_SAVED, () => {
if (this.settings.syncOnSave && !this.core.suspended) { if (this.settings.syncOnSave && !this.core.$$isSuspended()) {
scheduleTask("perform-replicate-after-save", 250, () => this.core.$$waitForReplicationOnce()); scheduleTask("perform-replicate-after-save", 250, () => this.core.$$replicateByEvent());
} }
}) });
eventHub.onEvent(EVENT_SETTING_SAVED, (setting) => {
if (this._replicatorType !== setting.remoteType) {
void this.setReplicator();
}
});
return Promise.resolve(true); return Promise.resolve(true);
} }
async setReplicator() { async setReplicator() {
const replicator = await this.core.$anyNewReplicator(); const replicator = await this.core.$anyNewReplicator();
if (!replicator) { if (!replicator) {
this._log("No replicator is available, this is the fatal error.", LOG_LEVEL_NOTICE); this._log($msg("Replicator.Message.InitialiseFatalError"), LOG_LEVEL_NOTICE);
return false return false;
}
if (this.core.replicator) {
await this.core.replicator.closeReplication();
this._log("Replicator closed for changing", LOG_LEVEL_VERBOSE);
} }
this.core.replicator = replicator; this.core.replicator = replicator;
this._replicatorType = this.settings.remoteType;
await yieldMicrotask(); await yieldMicrotask();
return true; return true;
} }
$$getReplicator(): LiveSyncAbstractReplicator {
return this.core.replicator;
}
$everyOnInitializeDatabase(db: LiveSyncLocalDB): Promise<boolean> { $everyOnInitializeDatabase(db: LiveSyncLocalDB): Promise<boolean> {
return this.setReplicator(); return this.setReplicator();
} }
$everyOnResetDatabase(db: LiveSyncLocalDB): Promise<boolean> { $everyOnResetDatabase(db: LiveSyncLocalDB): Promise<boolean> {
return this.setReplicator(); return this.setReplicator();
}
async ensureReplicatorPBKDF2Salt(showMessage: boolean = false): Promise<boolean> {
// Checking salt
const replicator = this.core.$$getReplicator();
return await replicator.ensurePBKDF2Salt(this.settings, showMessage, true);
} }
async $everyBeforeReplicate(showMessage: boolean): Promise<boolean> { async $everyBeforeReplicate(showMessage: boolean): Promise<boolean> {
// Checking salt
if (!(await this.ensureReplicatorPBKDF2Salt(showMessage))) {
Logger("Failed to ensure PBKDF2 salt for replication.", LOG_LEVEL_NOTICE);
return false;
}
await this.loadQueuedFiles(); await this.loadQueuedFiles();
return true; return true;
} }
async $$replicate(showMessage: boolean = false): Promise<boolean | void> { async $$replicate(showMessage: boolean = false): Promise<boolean | void> {
try {
updatePreviousExecutionTime(KEY_REPLICATION_ON_EVENT, REPLICATION_ON_EVENT_FORECASTED_TIME);
return await this.$$_replicate(showMessage);
} finally {
updatePreviousExecutionTime(KEY_REPLICATION_ON_EVENT);
}
}
/**
* obsolete method. No longer maintained and will be removed in the future.
* @deprecated v0.24.17
* @param showMessage If true, show message to the user.
*/
async cleaned(showMessage: boolean) {
Logger(`The remote database has been cleaned.`, showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO);
await skipIfDuplicated("cleanup", async () => {
const count = await purgeUnreferencedChunks(this.localDatabase.localDatabase, true);
const message = `The remote database has been cleaned up.
To synchronize, this device must be also cleaned up. ${count} chunk(s) will be erased from this device.
However, If there are many chunks to be deleted, maybe fetching again is faster.
We will lose the history of this device if we fetch the remote database again.
Even if you choose to clean up, you will see this option again if you exit Obsidian and then synchronise again.`;
const CHOICE_FETCH = "Fetch again";
const CHOICE_CLEAN = "Cleanup";
const CHOICE_DISMISS = "Dismiss";
const ret = await this.core.confirm.confirmWithMessage(
"Cleaned",
message,
[CHOICE_FETCH, CHOICE_CLEAN, CHOICE_DISMISS],
CHOICE_DISMISS,
30
);
if (ret == CHOICE_FETCH) {
await this.core.rebuilder.$performRebuildDB("localOnly");
}
if (ret == CHOICE_CLEAN) {
const replicator = this.core.$$getReplicator();
if (!(replicator instanceof LiveSyncCouchDBReplicator)) return;
const remoteDB = await replicator.connectRemoteCouchDBWithSetting(
this.settings,
this.core.$$isMobile(),
true
);
if (typeof remoteDB == "string") {
Logger(remoteDB, LOG_LEVEL_NOTICE);
return false;
}
await purgeUnreferencedChunks(this.localDatabase.localDatabase, false);
this.localDatabase.hashCaches.clear();
// Perform the synchronisation once.
if (await this.core.replicator.openReplication(this.settings, false, showMessage, true)) {
await balanceChunkPurgedDBs(this.localDatabase.localDatabase, remoteDB.db);
await purgeUnreferencedChunks(this.localDatabase.localDatabase, false);
this.localDatabase.hashCaches.clear();
await this.core.$$getReplicator().markRemoteResolved(this.settings);
Logger("The local database has been cleaned up.", showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO);
} else {
Logger(
"Replication has been cancelled. Please try it again.",
showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
);
}
}
});
}
async $$_replicate(showMessage: boolean = false): Promise<boolean | void> {
//--? //--?
if (!this.core.isReady) return; if (!this.core.$$isReady()) return;
if (isLockAcquired("cleanup")) { if (isLockAcquired("cleanup")) {
Logger("Database cleaning up is in process. replication has been cancelled", LOG_LEVEL_NOTICE); Logger($msg("Replicator.Message.Cleaned"), LOG_LEVEL_NOTICE);
return; return;
} }
if (this.settings.versionUpFlash != "") { if (this.settings.versionUpFlash != "") {
Logger("Open settings and check message, please. replication has been cancelled.", LOG_LEVEL_NOTICE); Logger($msg("Replicator.Message.VersionUpFlash"), LOG_LEVEL_NOTICE);
return; return;
} }
if (!await this.core.$everyCommitPendingFileEvent()) { if (!(await this.core.$everyCommitPendingFileEvent())) {
Logger("Some file events are pending. Replication has been cancelled.", LOG_LEVEL_NOTICE); Logger($msg("Replicator.Message.Pending"), LOG_LEVEL_NOTICE);
return false; return false;
} }
if (!await this.core.$everyBeforeReplicate(showMessage)) { if (!(await this.core.$everyBeforeReplicate(showMessage))) {
Logger(`Replication has been cancelled by some module failure`, LOG_LEVEL_NOTICE); Logger($msg("Replicator.Message.SomeModuleFailed"), LOG_LEVEL_NOTICE);
return false; return false;
} }
//<-- Here could be an module. //<-- Here could be an module.
const ret = await this.core.replicator.openReplication(this.settings, false, showMessage, false); const ret = await this.core.replicator.openReplication(this.settings, false, showMessage, false);
if (!ret) { if (!ret) {
if (this.core.replicator.tweakSettingsMismatched) { if (this.core.replicator.tweakSettingsMismatched && this.core.replicator.preferredTweakValue) {
await this.core.$$askResolvingMismatchedTweaks(); await this.core.$$askResolvingMismatchedTweaks(this.core.replicator.preferredTweakValue);
} else { } else {
if (this.core.replicator?.remoteLockedAndDeviceNotAccepted) { if (this.core.replicator?.remoteLockedAndDeviceNotAccepted) {
if (this.core.replicator.remoteCleaned && this.settings.useIndexedDBAdapter) { if (this.core.replicator.remoteCleaned && this.settings.useIndexedDBAdapter) {
Logger(`The remote database has been cleaned.`, showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO); await this.cleaned(showMessage);
await skipIfDuplicated("cleanup", async () => {
const count = await purgeUnreferencedChunks(this.localDatabase.localDatabase, true);
const message = `The remote database has been cleaned up.
To synchronize, this device must be also cleaned up. ${count} chunk(s) will be erased from this device.
However, If there are many chunks to be deleted, maybe fetching again is faster.
We will lose the history of this device if we fetch the remote database again.
Even if you choose to clean up, you will see this option again if you exit Obsidian and then synchronise again.`
const CHOICE_FETCH = "Fetch again";
const CHOICE_CLEAN = "Cleanup";
const CHOICE_DISMISS = "Dismiss";
const ret = await this.core.confirm.confirmWithMessage("Cleaned", message, [
CHOICE_FETCH,
CHOICE_CLEAN,
CHOICE_DISMISS], CHOICE_DISMISS, 30);
if (ret == CHOICE_FETCH) {
await this.core.rebuilder.$performRebuildDB("localOnly");
}
if (ret == CHOICE_CLEAN) {
const replicator = this.core.getReplicator();
if (!(replicator instanceof LiveSyncCouchDBReplicator)) return;
const remoteDB = await replicator.connectRemoteCouchDBWithSetting(this.settings, this.core.getIsMobile(), true);
if (typeof remoteDB == "string") {
Logger(remoteDB, LOG_LEVEL_NOTICE);
return false;
}
await purgeUnreferencedChunks(this.localDatabase.localDatabase, false);
this.localDatabase.hashCaches.clear();
// Perform the synchronisation once.
if (await this.core.replicator.openReplication(this.settings, false, showMessage, true)) {
await balanceChunkPurgedDBs(this.localDatabase.localDatabase, remoteDB.db);
await purgeUnreferencedChunks(this.localDatabase.localDatabase, false);
this.localDatabase.hashCaches.clear();
await this.core.getReplicator().markRemoteResolved(this.settings);
Logger("The local database has been cleaned up.", showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO)
} else {
Logger("Replication has been cancelled. Please try it again.", showMessage ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO)
}
}
});
} else { } else {
const message = ` const message = $msg("Replicator.Dialogue.Locked.Message");
The remote database has been rebuilt. const CHOICE_FETCH = $msg("Replicator.Dialogue.Locked.Action.Fetch");
To synchronize, this device must fetch everything again once. const CHOICE_DISMISS = $msg("Replicator.Dialogue.Locked.Action.Dismiss");
Or if you are sure know what had been happened, we can unlock the database from the setting dialog. const CHOICE_UNLOCK = $msg("Replicator.Dialogue.Locked.Action.Unlock");
` const ret = await this.core.confirm.askSelectStringDialogue(
const CHOICE_FETCH = "Fetch again"; message,
const CHOICE_DISMISS = "Dismiss"; [CHOICE_FETCH, CHOICE_UNLOCK, CHOICE_DISMISS],
const ret = await this.core.confirm.confirmWithMessage("Locked", message, [ {
CHOICE_FETCH, title: $msg("Replicator.Dialogue.Locked.Title"),
CHOICE_DISMISS], CHOICE_DISMISS, 10); defaultAction: CHOICE_DISMISS,
timeout: 60,
}
);
if (ret == CHOICE_FETCH) { if (ret == CHOICE_FETCH) {
const CHOICE_RESTART = "Restart"; this._log($msg("Replicator.Dialogue.Locked.Message.Fetch"), LOG_LEVEL_NOTICE);
const CHOICE_WITHOUT_RESTART = "Without restart"; await this.core.rebuilder.scheduleFetch();
if (await this.core.confirm.askSelectStringDialogue( this.core.$$scheduleAppReload();
"Self-hosted LiveSync restarts Obsidian to fetch everything safely. However, you can do it without restarting. Please choose one.", return;
[CHOICE_RESTART, CHOICE_WITHOUT_RESTART], { } else if (ret == CHOICE_UNLOCK) {
title: "Fetch again", await this.core.replicator.markRemoteResolved(this.settings);
defaultAction: CHOICE_RESTART, this._log($msg("Replicator.Dialogue.Locked.Message.Unlocked"), LOG_LEVEL_NOTICE);
timeout: 30, return;
}
) == CHOICE_RESTART) {
await this.core.rebuilder.scheduleFetch();
// await this.core.$$scheduleAppReload();
return;
} else {
await this.core.rebuilder.$performRebuildDB("localOnly");
}
} }
} }
} }
} }
} }
return ret; return ret;
} }
async $$replicateByEvent(): Promise<boolean | void> {
const least = this.settings.syncMinimumInterval;
if (least > 0) {
return rateLimitedSharedExecution(KEY_REPLICATION_ON_EVENT, least, async () => {
return await this.$$replicate();
});
}
return await shareRunningResult(`replication`, () => this.core.$$replicate());
}
$$parseReplicationResult(docs: Array<PouchDB.Core.ExistingDocument<EntryDoc>>): void { $$parseReplicationResult(docs: Array<PouchDB.Core.ExistingDocument<EntryDoc>>): void {
if (this.settings.suspendParseReplicationResult && !this.replicationResultProcessor.isSuspended) { if (this.settings.suspendParseReplicationResult && !this.replicationResultProcessor.isSuspended) {
this.replicationResultProcessor.suspend() this.replicationResultProcessor.suspend();
} }
this.replicationResultProcessor.enqueueAll(docs); this.replicationResultProcessor.enqueueAll(docs);
if (!this.settings.suspendParseReplicationResult && this.replicationResultProcessor.isSuspended) { if (!this.settings.suspendParseReplicationResult && this.replicationResultProcessor.isSuspended) {
this.replicationResultProcessor.resume() this.replicationResultProcessor.resume();
} }
} }
_saveQueuedFiles = throttle(() => { _saveQueuedFiles = throttle(() => {
const saveData = this.replicationResultProcessor._queue.filter(e => e !== undefined && e !== null).map((e) => e?._id ?? "" as string) as string[]; const saveData = this.replicationResultProcessor._queue
const kvDBKey = "queued-files" .filter((e) => e !== undefined && e !== null)
.map((e) => e?._id ?? ("" as string)) as string[];
const kvDBKey = "queued-files";
// localStorage.setItem(lsKey, saveData); // localStorage.setItem(lsKey, saveData);
fireAndForget(() => this.core.kvDB.set(kvDBKey, saveData)); fireAndForget(() => this.core.kvDB.set(kvDBKey, saveData));
}, 100); }, 100);
@@ -178,151 +255,204 @@ Or if you are sure know what had been happened, we can unlock the database from
async loadQueuedFiles() { async loadQueuedFiles() {
if (this.settings.suspendParseReplicationResult) return; if (this.settings.suspendParseReplicationResult) return;
if (!this.settings.isConfigured) return; if (!this.settings.isConfigured) return;
const kvDBKey = "queued-files" try {
// const ids = [...new Set(JSON.parse(localStorage.getItem(lsKey) || "[]"))] as string[]; const kvDBKey = "queued-files";
const ids = [...new Set(await this.core.kvDB.get<string[]>(kvDBKey) ?? [])]; // const ids = [...new Set(JSON.parse(localStorage.getItem(lsKey) || "[]"))] as string[];
const batchSize = 100; const ids = [...new Set((await this.core.kvDB.get<string[]>(kvDBKey)) ?? [])];
const chunkedIds = arrayToChunkedArray(ids, batchSize); const batchSize = 100;
for await (const idsBatch of chunkedIds) { const chunkedIds = arrayToChunkedArray(ids, batchSize);
const ret = await this.localDatabase.allDocsRaw<EntryDoc>({
keys: idsBatch,
include_docs: true,
limit: 100
});
const docs = ret.rows.filter(e => e.doc).map(e => e.doc) as PouchDB.Core.ExistingDocument<EntryDoc>[];
const errors = ret.rows.filter(e => !e.doc && !e.value.deleted);
if (errors.length > 0) {
Logger("Some queued processes were not resurrected");
Logger(JSON.stringify(errors), LOG_LEVEL_VERBOSE);
}
this.replicationResultProcessor.enqueueAll(docs);
await this.replicationResultProcessor.waitForAllProcessed();
}
// suspendParseReplicationResult is true, so we have to resume it if it is suspended.
if (this.replicationResultProcessor.isSuspended) {
this.replicationResultProcessor.resume();
}
for await (const idsBatch of chunkedIds) {
const ret = await this.localDatabase.allDocsRaw<EntryDoc>({
keys: idsBatch,
include_docs: true,
limit: 100,
});
const docs = ret.rows
.filter((e) => e.doc)
.map((e) => e.doc) as PouchDB.Core.ExistingDocument<EntryDoc>[];
const errors = ret.rows.filter((e) => !e.doc && !e.value.deleted);
if (errors.length > 0) {
Logger("Some queued processes were not resurrected");
Logger(JSON.stringify(errors), LOG_LEVEL_VERBOSE);
}
this.replicationResultProcessor.enqueueAll(docs);
}
} catch (e) {
Logger(`Failed to load queued files.`, LOG_LEVEL_NOTICE);
Logger(e, LOG_LEVEL_VERBOSE);
} finally {
// Check again before awaiting,
if (this.replicationResultProcessor.isSuspended) {
this.replicationResultProcessor.resume();
}
}
// Wait for all queued files to be processed.
try {
await this.replicationResultProcessor.waitForAllProcessed();
} catch (e) {
Logger(`Failed to wait for all queued files to be processed.`, LOG_LEVEL_NOTICE);
Logger(e, LOG_LEVEL_VERBOSE);
}
} }
replicationResultProcessor = new QueueProcessor(async (docs: PouchDB.Core.ExistingDocument<EntryDoc>[]) => { replicationResultProcessor = new QueueProcessor(
if (this.settings.suspendParseReplicationResult) return; async (docs: PouchDB.Core.ExistingDocument<EntryDoc>[]) => {
const change = docs[0]; if (this.settings.suspendParseReplicationResult) return;
if (!change) return; const change = docs[0];
if (isChunk(change._id)) { if (!change) return;
// SendSignal? if (isChunk(change._id)) {
// this.parseIncomingChunk(change); globalSlipBoard.submit("read-chunk", change._id, change as EntryLeaf);
sendValue(`leaf-${change._id}`, change);
return;
}
if (await this.core.$anyModuleParsedReplicationResultItem(change)) return;
// any addon needs this item?
// for (const proc of this.core.addOns) {
// if (await proc.parseReplicationResultItem(change)) {
// return;
// }
// }
if (change.type == "versioninfo") {
if (change.version > VER) {
this.core.replicator.closeReplication();
Logger(`Remote database updated to incompatible version. update your Self-hosted LiveSync plugin.`, LOG_LEVEL_NOTICE);
}
return;
}
if (change._id == SYNCINFO_ID || // Synchronisation information data
change._id.startsWith("_design") //design document
) {
return;
}
if (isAnyNote(change)) {
const docPath = getPath(change);
if (!await this.core.$$isTargetFile(docPath)) {
Logger(`Skipped: ${docPath}`, LOG_LEVEL_VERBOSE);
return; return;
} }
if (this.databaseQueuedProcessor._isSuspended) { if (await this.core.$anyModuleParsedReplicationResultItem(change)) return;
Logger(`Processing scheduled: ${docPath}`, LOG_LEVEL_INFO); // any addon needs this item?
} // for (const proc of this.core.addOns) {
const size = change.size; // if (await proc.parseReplicationResultItem(change)) {
if (this.core.$$isFileSizeExceeded(size)) { // return;
Logger(`Processing ${docPath} has been skipped due to file size exceeding the limit`, LOG_LEVEL_NOTICE); // }
// }
if (change.type == "versioninfo") {
if (change.version > VER) {
this.core.replicator.closeReplication();
Logger(
`Remote database updated to incompatible version. update your Self-hosted LiveSync plugin.`,
LOG_LEVEL_NOTICE
);
}
return; return;
} }
this.databaseQueuedProcessor.enqueue(change); if (
} change._id == SYNCINFO_ID || // Synchronisation information data
return; change._id.startsWith("_design") //design document
}, { ) {
batchSize: 1, return;
suspended: true, }
concurrentLimit: 100, if (isAnyNote(change)) {
delay: 0, const docPath = getPath(change);
totalRemainingReactiveSource: this.core.replicationResultCount if (!(await this.core.$$isTargetFile(docPath))) {
}).replaceEnqueueProcessor((queue, newItem) => { Logger(`Skipped: ${docPath}`, LOG_LEVEL_VERBOSE);
const q = queue.filter(e => e._id != newItem._id); return;
return [...q, newItem]; }
}).startPipeline().onUpdateProgress(() => { if (this.databaseQueuedProcessor._isSuspended) {
this.saveQueuedFiles(); Logger(`Processing scheduled: ${docPath}`, LOG_LEVEL_INFO);
}); }
const size = change.size;
databaseQueuedProcessor = new QueueProcessor(async (docs: EntryBody[]) => { if (this.core.$$isFileSizeExceeded(size)) {
const dbDoc = docs[0] as LoadedEntry; // It has no `data` Logger(
const path = getPath(dbDoc); `Processing ${docPath} has been skipped due to file size exceeding the limit`,
LOG_LEVEL_NOTICE
// If `Read chunks online` is disabled, chunks should be transferred before here. );
// However, in some cases, chunks are after that. So, if missing chunks exist, we have to wait for them. return;
const doc = await this.localDatabase.getDBEntryFromMeta({ ...dbDoc }, {}, false, true, true); }
if (!doc) { this.databaseQueuedProcessor.enqueue(change);
Logger(`Something went wrong while gathering content of ${path} (${dbDoc._id.substring(0, 8)}, ${dbDoc._rev?.substring(0, 10)}) `, LOG_LEVEL_NOTICE) }
return; return;
},
{
batchSize: 1,
suspended: true,
concurrentLimit: 100,
delay: 0,
totalRemainingReactiveSource: this.core.replicationResultCount,
} }
)
.replaceEnqueueProcessor((queue, newItem) => {
const q = queue.filter((e) => e._id != newItem._id);
return [...q, newItem];
})
.startPipeline()
.onUpdateProgress(() => {
this.saveQueuedFiles();
});
if (await this.core.$anyProcessOptionalSyncFiles(dbDoc)) { databaseQueuedProcessor = new QueueProcessor(
// Already processed async (docs: EntryBody[]) => {
} else if (isValidPath(getPath(doc))) { const dbDoc = docs[0] as LoadedEntry; // It has no `data`
this.storageApplyingProcessor.enqueue(doc as MetaEntry); const path = getPath(dbDoc);
} else {
Logger(`Skipped: ${doc._id.substring(0, 8)}`, LOG_LEVEL_VERBOSE); // If `Read chunks online` is disabled, chunks should be transferred before here.
// However, in some cases, chunks are after that. So, if missing chunks exist, we have to wait for them.
const doc = await this.localDatabase.getDBEntryFromMeta({ ...dbDoc }, false, true);
if (!doc) {
Logger(
`Something went wrong while gathering content of ${path} (${dbDoc._id.substring(0, 8)}, ${dbDoc._rev?.substring(0, 10)}) `,
LOG_LEVEL_NOTICE
);
return;
}
if (await this.core.$anyProcessOptionalSyncFiles(dbDoc)) {
// Already processed
} else if (isValidPath(getPath(doc))) {
this.storageApplyingProcessor.enqueue(doc as MetaEntry);
} else {
Logger(`Skipped: ${path} (${doc._id.substring(0, 8)})`, LOG_LEVEL_VERBOSE);
}
return;
},
{
suspended: true,
batchSize: 1,
concurrentLimit: 10,
yieldThreshold: 1,
delay: 0,
totalRemainingReactiveSource: this.core.databaseQueueCount,
} }
return; )
}, { .replaceEnqueueProcessor((queue, newItem) => {
suspended: true, const q = queue.filter((e) => e._id != newItem._id);
batchSize: 1, return [...q, newItem];
concurrentLimit: 10, })
yieldThreshold: 1, .startPipeline();
delay: 0,
totalRemainingReactiveSource: this.core.databaseQueueCount
}).replaceEnqueueProcessor((queue, newItem) => {
const q = queue.filter(e => e._id != newItem._id);
return [...q, newItem];
}).startPipeline();
storageApplyingProcessor = new QueueProcessor(
storageApplyingProcessor = new QueueProcessor(async (docs: MetaEntry[]) => { async (docs: MetaEntry[]) => {
const entry = docs[0]; const entry = docs[0];
await this.core.$anyProcessReplicatedDoc(entry); await this.core.$anyProcessReplicatedDoc(entry);
return; return;
}, { },
suspended: true, {
batchSize: 1, suspended: true,
concurrentLimit: 6, batchSize: 1,
yieldThreshold: 1, concurrentLimit: 6,
delay: 0, yieldThreshold: 1,
totalRemainingReactiveSource: this.core.storageApplyingCount delay: 0,
}).replaceEnqueueProcessor((queue, newItem) => { totalRemainingReactiveSource: this.core.storageApplyingCount,
const q = queue.filter(e => e._id != newItem._id); }
return [...q, newItem]; )
}).startPipeline() .replaceEnqueueProcessor((queue, newItem) => {
const q = queue.filter((e) => e._id != newItem._id);
return [...q, newItem];
})
.startPipeline();
$everyBeforeSuspendProcess(): Promise<boolean> { $everyBeforeSuspendProcess(): Promise<boolean> {
this.core.replicator.closeReplication(); this.core.replicator.closeReplication();
return Promise.resolve(true); return Promise.resolve(true);
} }
async $$replicateAllToServer(showingNotice: boolean = false, sendChunksInBulkDisabled: boolean = false): Promise<boolean> { async $$replicateAllToServer(
if (!this.core.isReady) return false; showingNotice: boolean = false,
if (!await this.core.$everyBeforeReplicate(showingNotice)) { sendChunksInBulkDisabled: boolean = false
Logger(`Replication has been cancelled by some module failure`, LOG_LEVEL_NOTICE); ): Promise<boolean> {
if (!this.core.$$isReady()) return false;
if (!(await this.core.$everyBeforeReplicate(showingNotice))) {
Logger($msg("Replicator.Message.SomeModuleFailed"), LOG_LEVEL_NOTICE);
return false; return false;
} }
if (!sendChunksInBulkDisabled) { if (!sendChunksInBulkDisabled) {
if (this.core.replicator instanceof LiveSyncCouchDBReplicator) { if (this.core.replicator instanceof LiveSyncCouchDBReplicator) {
if (await this.core.confirm.askYesNoDialog("Do you want to send all chunks before replication?", { defaultOption: "No", timeout: 20 }) == "yes") { if (
(await this.core.confirm.askYesNoDialog("Do you want to send all chunks before replication?", {
defaultOption: "No",
timeout: 20,
})) == "yes"
) {
await this.core.replicator.sendChunks(this.core.settings, undefined, true, 0); await this.core.replicator.sendChunks(this.core.settings, undefined, true, 0);
} }
} }
@@ -334,18 +464,11 @@ Or if you are sure know what had been happened, we can unlock the database from
return !checkResult; return !checkResult;
} }
async $$replicateAllFromServer(showingNotice: boolean = false): Promise<boolean> { async $$replicateAllFromServer(showingNotice: boolean = false): Promise<boolean> {
if (!this.core.isReady) return false; if (!this.core.$$isReady()) return false;
const ret = await this.core.replicator.replicateAllFromServer(this.settings, showingNotice); const ret = await this.core.replicator.replicateAllFromServer(this.settings, showingNotice);
if (ret) return true; if (ret) return true;
const checkResult = await this.core.$anyAfterConnectCheckFailed(); const checkResult = await this.core.$anyAfterConnectCheckFailed();
if (checkResult == "CHECKAGAIN") return await this.core.$$replicateAllFromServer(showingNotice); if (checkResult == "CHECKAGAIN") return await this.core.$$replicateAllFromServer(showingNotice);
return !checkResult; return !checkResult;
} }
async $$waitForReplicationOnce(): Promise<boolean | void> {
return await shareRunningResult(`replication`, () => this.core.$$replicate());
}
} }

View File

@@ -1,5 +1,5 @@
import { fireAndForget } from "octagonal-wheels/promises"; import { fireAndForget } from "octagonal-wheels/promises";
import { REMOTE_MINIO, type RemoteDBSettings } from "../../lib/src/common/types"; import { REMOTE_MINIO, REMOTE_P2P, type RemoteDBSettings } from "../../lib/src/common/types";
import { LiveSyncCouchDBReplicator } from "../../lib/src/replication/couchdb/LiveSyncReplicator"; import { LiveSyncCouchDBReplicator } from "../../lib/src/replication/couchdb/LiveSyncReplicator";
import type { LiveSyncAbstractReplicator } from "../../lib/src/replication/LiveSyncAbstractReplicator"; import type { LiveSyncAbstractReplicator } from "../../lib/src/replication/LiveSyncAbstractReplicator";
import { AbstractModule } from "../AbstractModule"; import { AbstractModule } from "../AbstractModule";
@@ -9,14 +9,13 @@ export class ModuleReplicatorCouchDB extends AbstractModule implements ICoreModu
$anyNewReplicator(settingOverride: Partial<RemoteDBSettings> = {}): Promise<LiveSyncAbstractReplicator> { $anyNewReplicator(settingOverride: Partial<RemoteDBSettings> = {}): Promise<LiveSyncAbstractReplicator> {
const settings = { ...this.settings, ...settingOverride }; const settings = { ...this.settings, ...settingOverride };
// If new remote types were added, add them here. Do not use `REMOTE_COUCHDB` directly for the safety valve. // If new remote types were added, add them here. Do not use `REMOTE_COUCHDB` directly for the safety valve.
if (settings.remoteType == REMOTE_MINIO) { if (settings.remoteType == REMOTE_MINIO || settings.remoteType == REMOTE_P2P) {
return undefined! return undefined!;
} }
return Promise.resolve(new LiveSyncCouchDBReplicator(this.core)); return Promise.resolve(new LiveSyncCouchDBReplicator(this.core));
} }
$everyAfterResumeProcess(): Promise<boolean> { $everyAfterResumeProcess(): Promise<boolean> {
if (this.settings.remoteType != REMOTE_MINIO) { if (this.settings.remoteType != REMOTE_MINIO && this.settings.remoteType != REMOTE_P2P) {
// If LiveSync enabled, open replication // If LiveSync enabled, open replication
if (this.settings.liveSync) { if (this.settings.liveSync) {
fireAndForget(() => this.core.replicator.openReplication(this.settings, true, false, false)); fireAndForget(() => this.core.replicator.openReplication(this.settings, true, false, false));

View File

@@ -12,6 +12,4 @@ export class ModuleReplicatorMinIO extends AbstractModule implements ICoreModule
} }
return undefined!; return undefined!;
} }
} }

View File

@@ -0,0 +1,30 @@
import { REMOTE_P2P, type RemoteDBSettings } from "../../lib/src/common/types";
import type { LiveSyncAbstractReplicator } from "../../lib/src/replication/LiveSyncAbstractReplicator";
import { AbstractModule } from "../AbstractModule";
import type { ICoreModule } from "../ModuleTypes";
import { LiveSyncTrysteroReplicator } from "../../lib/src/replication/trystero/LiveSyncTrysteroReplicator";
export class ModuleReplicatorP2P extends AbstractModule implements ICoreModule {
$anyNewReplicator(settingOverride: Partial<RemoteDBSettings> = {}): Promise<LiveSyncAbstractReplicator> {
const settings = { ...this.settings, ...settingOverride };
if (settings.remoteType == REMOTE_P2P) {
return Promise.resolve(new LiveSyncTrysteroReplicator(this.core));
}
return undefined!;
}
$everyAfterResumeProcess(): Promise<boolean> {
if (this.settings.remoteType == REMOTE_P2P) {
// // If LiveSync enabled, open replication
// if (this.settings.liveSync) {
// fireAndForget(() => this.core.replicator.openReplication(this.settings, true, false, false));
// }
// // If sync on start enabled, open replication
// if (!this.settings.liveSync && this.settings.syncOnStart) {
// // Possibly ok as if only share the result
// fireAndForget(() => this.core.replicator.openReplication(this.settings, false, false, false));
// }
}
return Promise.resolve(true);
}
}

View File

@@ -1,22 +1,35 @@
import { LRUCache } from "octagonal-wheels/memory/LRUCache"; import { LRUCache } from "octagonal-wheels/memory/LRUCache";
import { getPathFromUXFileInfo, id2path, isInternalMetadata, path2id, stripInternalMetadataPrefix, useMemo } from "../../common/utils"; import {
import { LOG_LEVEL_VERBOSE, type DocumentID, type EntryHasPath, type FilePath, type FilePathWithPrefix, type ObsidianLiveSyncSettings, type UXFileInfoStub } from "../../lib/src/common/types"; getStoragePathFromUXFileInfo,
import { addPrefix, isAcceptedAll, stripAllPrefixes } from "../../lib/src/string_and_binary/path"; id2path,
isInternalMetadata,
path2id,
stripInternalMetadataPrefix,
useMemo,
} from "../../common/utils";
import {
LOG_LEVEL_VERBOSE,
type DocumentID,
type EntryHasPath,
type FilePath,
type FilePathWithPrefix,
type ObsidianLiveSyncSettings,
type UXFileInfoStub,
} from "../../lib/src/common/types";
import { addPrefix, isAcceptedAll } from "../../lib/src/string_and_binary/path";
import { AbstractModule } from "../AbstractModule"; import { AbstractModule } from "../AbstractModule";
import type { ICoreModule } from "../ModuleTypes"; import type { ICoreModule } from "../ModuleTypes";
import { EVENT_REQUEST_RELOAD_SETTING_TAB, EVENT_SETTING_SAVED, eventHub } from "../../common/events"; import { EVENT_REQUEST_RELOAD_SETTING_TAB, EVENT_SETTING_SAVED, eventHub } from "../../common/events";
import { isDirty } from "../../lib/src/common/utils"; import { isDirty } from "../../lib/src/common/utils";
export class ModuleTargetFilter extends AbstractModule implements ICoreModule { export class ModuleTargetFilter extends AbstractModule implements ICoreModule {
reloadIgnoreFiles() { reloadIgnoreFiles() {
this.ignoreFiles = this.settings.ignoreFiles.split(",").map(e => e.trim()); this.ignoreFiles = this.settings.ignoreFiles.split(",").map((e) => e.trim());
} }
$everyOnload(): Promise<boolean> { $everyOnload(): Promise<boolean> {
eventHub.onEvent(EVENT_SETTING_SAVED, (evt: CustomEvent<ObsidianLiveSyncSettings>) => { eventHub.onEvent(EVENT_SETTING_SAVED, (evt: ObsidianLiveSyncSettings) => {
this.reloadIgnoreFiles(); this.reloadIgnoreFiles();
}); });
eventHub.onEvent(EVENT_REQUEST_RELOAD_SETTING_TAB, (evt: CustomEvent<ObsidianLiveSyncSettings>) => { eventHub.onEvent(EVENT_REQUEST_RELOAD_SETTING_TAB, () => {
this.reloadIgnoreFiles(); this.reloadIgnoreFiles();
}); });
return Promise.resolve(true); return Promise.resolve(true);
@@ -32,10 +45,13 @@ export class ModuleTargetFilter extends AbstractModule implements ICoreModule {
} }
async $$path2id(filename: FilePathWithPrefix | FilePath, prefix?: string): Promise<DocumentID> { async $$path2id(filename: FilePathWithPrefix | FilePath, prefix?: string): Promise<DocumentID> {
const destPath = addPrefix(filename, prefix ?? ""); const destPath = addPrefix(filename, prefix ?? "");
return await path2id(destPath, this.settings.usePathObfuscation ? this.settings.passphrase : "", !this.settings.handleFilenameCaseSensitive); return await path2id(
destPath,
this.settings.usePathObfuscation ? this.settings.passphrase : "",
!this.settings.handleFilenameCaseSensitive
);
} }
$$isFileSizeExceeded(size: number) { $$isFileSizeExceeded(size: number) {
if (this.settings.syncMaxSizeInMB > 0 && size > 0) { if (this.settings.syncMaxSizeInMB > 0 && size > 0) {
if (this.settings.syncMaxSizeInMB * 1024 * 1024 < size) { if (this.settings.syncMaxSizeInMB * 1024 * 1024 < size) {
@@ -45,57 +61,62 @@ export class ModuleTargetFilter extends AbstractModule implements ICoreModule {
return false; return false;
} }
$$markFileListPossiblyChanged(): void {
this.totalFileEventCount++;
}
totalFileEventCount = 0;
get fileListPossiblyChanged() { get fileListPossiblyChanged() {
if (isDirty("totalFileEventCount", this.core.totalFileEventCount)) { if (isDirty("totalFileEventCount", this.totalFileEventCount)) {
return true; return true;
} }
return false; return false;
} }
async $$isTargetFile(file: string | UXFileInfoStub, keepFileCheckList = false) { async $$isTargetFile(file: string | UXFileInfoStub, keepFileCheckList = false) {
const fileCount = useMemo<Record<string, number>>(
const fileCount = useMemo<Record<string, number>>({ {
key: "fileCount", // forceUpdate: !keepFileCheckList, key: "fileCount", // forceUpdate: !keepFileCheckList,
}, (ctx, prev) => { },
if (keepFileCheckList && prev) return prev; (ctx, prev) => {
if (!keepFileCheckList && prev && !this.fileListPossiblyChanged) { if (keepFileCheckList && prev) return prev;
return prev; if (!keepFileCheckList && prev && !this.fileListPossiblyChanged) {
}
const fileList = (ctx.get("fileList") ?? []) as FilePathWithPrefix[];
// const fileNameList = (ctx.get("fileNameList") ?? []) as FilePath[];
// const fileNames =
const vaultFiles = this.core.storageAccess.getFileNames().sort();
if (prev && vaultFiles.length == fileList.length) {
const fl3 = new Set([...fileList, ...vaultFiles]);
if (fileList.length == fl3.size && vaultFiles.length == fl3.size) {
return prev; return prev;
} }
} const fileList = (ctx.get("fileList") ?? []) as FilePathWithPrefix[];
ctx.set("fileList", vaultFiles); // const fileNameList = (ctx.get("fileNameList") ?? []) as FilePath[];
// const fileNames =
const vaultFiles = this.core.storageAccess.getFileNames().sort();
const fileCount: Record<string, number> = {}; if (prev && vaultFiles.length == fileList.length) {
for (const file of vaultFiles) { const fl3 = new Set([...fileList, ...vaultFiles]);
const lc = file.toLowerCase(); if (fileList.length == fl3.size && vaultFiles.length == fl3.size) {
if (!fileCount[lc]) { return prev;
fileCount[lc] = 1; }
} else {
fileCount[lc]++;
} }
} ctx.set("fileList", vaultFiles);
return fileCount;
})
const filepath = getPathFromUXFileInfo(file); const fileCount: Record<string, number> = {};
for (const file of vaultFiles) {
const lc = file.toLowerCase();
if (!fileCount[lc]) {
fileCount[lc] = 1;
} else {
fileCount[lc]++;
}
}
return fileCount;
}
);
const filepath = getStoragePathFromUXFileInfo(file);
const lc = filepath.toLowerCase(); const lc = filepath.toLowerCase();
if (this.core.shouldCheckCaseInsensitive) { if (this.core.$$shouldCheckCaseInsensitive()) {
if (lc in fileCount && fileCount[lc] > 1) { if (lc in fileCount && fileCount[lc] > 1) {
return false; return false;
} }
} }
const fileNameLC = getPathFromUXFileInfo(file).split("/").pop()?.toLowerCase(); const fileNameLC = getStoragePathFromUXFileInfo(file).split("/").pop()?.toLowerCase();
if (this.settings.useIgnoreFiles) { if (this.settings.useIgnoreFiles) {
if (this.ignoreFiles.some(e => e.toLowerCase() == fileNameLC)) { if (this.ignoreFiles.some((e) => e.toLowerCase() == fileNameLC)) {
// We must reload ignore files due to the its change. // We must reload ignore files due to the its change.
await this.readIgnoreFile(filepath); await this.readIgnoreFile(filepath);
} }
@@ -104,11 +125,11 @@ export class ModuleTargetFilter extends AbstractModule implements ICoreModule {
} }
} }
if (!this.localDatabase?.isTargetFile(filepath)) return false; if (!this.localDatabase?.isTargetFile(filepath)) return false;
return true return true;
} }
ignoreFileCache = new LRUCache<string, string[] | false>(300, 250000, true); ignoreFileCache = new LRUCache<string, string[] | false>(300, 250000, true);
ignoreFiles = [] as string[] ignoreFiles = [] as string[];
async readIgnoreFile(path: string) { async readIgnoreFile(path: string) {
try { try {
const file = await this.core.storageAccess.readFileText(path); const file = await this.core.storageAccess.readFileText(path);
@@ -133,12 +154,12 @@ export class ModuleTargetFilter extends AbstractModule implements ICoreModule {
if (!this.settings.useIgnoreFiles) { if (!this.settings.useIgnoreFiles) {
return false; return false;
} }
const filepath = getPathFromUXFileInfo(file) const filepath = getStoragePathFromUXFileInfo(file);
if (this.ignoreFileCache.has(filepath)) { if (this.ignoreFileCache.has(filepath)) {
// Renew // Renew
await this.readIgnoreFile(filepath); await this.readIgnoreFile(filepath);
} }
if (!await isAcceptedAll(stripAllPrefixes(filepath), this.ignoreFiles, (filename) => this.getIgnoreFile(filename))) { if (!(await isAcceptedAll(filepath, this.ignoreFiles, (filename) => this.getIgnoreFile(filename)))) {
return true; return true;
} }
return false; return false;

View File

@@ -2,32 +2,27 @@ import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-w
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import { sizeToHumanReadable } from "octagonal-wheels/number"; import { sizeToHumanReadable } from "octagonal-wheels/number";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
import { $msg } from "src/lib/src/common/i18n.ts";
export class ModuleCheckRemoteSize extends AbstractModule implements ICoreModule { export class ModuleCheckRemoteSize extends AbstractModule implements ICoreModule {
async $allScanStat(): Promise<boolean> { async $allScanStat(): Promise<boolean> {
this._log(`Checking storage sizes`, LOG_LEVEL_VERBOSE); this._log($msg("moduleCheckRemoteSize.logCheckingStorageSizes"), LOG_LEVEL_VERBOSE);
if (this.settings.notifyThresholdOfRemoteStorageSize < 0) { if (this.settings.notifyThresholdOfRemoteStorageSize < 0) {
const message = `We can set a maximum database capacity warning, **to take action before running out of space on the remote storage**. const message = $msg("moduleCheckRemoteSize.msgSetDBCapacity");
Do you want to enable this? const ANSWER_0 = $msg("moduleCheckRemoteSize.optionNoWarn");
const ANSWER_800 = $msg("moduleCheckRemoteSize.option800MB");
const ANSWER_2000 = $msg("moduleCheckRemoteSize.option2GB");
const ASK_ME_NEXT_TIME = $msg("moduleCheckRemoteSize.optionAskMeLater");
> [!MORE]- const ret = await this.core.confirm.askSelectStringDialogue(
> - 0: Do not warn about storage size. message,
> This is recommended if you have enough space on the remote storage especially you have self-hosted. And you can check the storage size and rebuild manually. [ANSWER_0, ANSWER_800, ANSWER_2000, ASK_ME_NEXT_TIME],
> - 800: Warn if the remote storage size exceeds 800MB. {
> This is recommended if you are using fly.io with 1GB limit or IBM Cloudant. defaultAction: ASK_ME_NEXT_TIME,
> - 2000: Warn if the remote storage size exceeds 2GB. title: $msg("moduleCheckRemoteSize.titleDatabaseSizeNotify"),
timeout: 40,
If we have reached the limit, we will be asked to enlarge the limit step by step. }
` );
const ANSWER_0 = "No, never warn please";
const ANSWER_800 = "800MB (Cloudant, fly.io)";
const ANSWER_2000 = "2GB (Standard)";
const ASK_ME_NEXT_TIME = "Ask me later";
const ret = await this.core.confirm.askSelectStringDialogue(message, [ANSWER_0, ANSWER_800, ANSWER_2000, ASK_ME_NEXT_TIME], {
defaultAction: ASK_ME_NEXT_TIME,
title: "Setting up database size notification", timeout: 40
});
if (ret == ANSWER_0) { if (ret == ANSWER_0) {
this.settings.notifyThresholdOfRemoteStorageSize = 0; this.settings.notifyThresholdOfRemoteStorageSize = 0;
await this.core.saveSettings(); await this.core.saveSettings();
@@ -46,35 +41,30 @@ If we have reached the limit, we will be asked to enlarge the limit step by step
if (estimatedSize) { if (estimatedSize) {
const maxSize = this.settings.notifyThresholdOfRemoteStorageSize * 1024 * 1024; const maxSize = this.settings.notifyThresholdOfRemoteStorageSize * 1024 * 1024;
if (estimatedSize > maxSize) { if (estimatedSize > maxSize) {
const message = `**Your database is getting larger!** But do not worry, we can address it now. The time before running out of space on the remote storage. const message = $msg("moduleCheckRemoteSize.msgDatabaseGrowing", {
estimatedSize: sizeToHumanReadable(estimatedSize),
| Measured size | Configured size | maxSize: sizeToHumanReadable(maxSize),
| --- | --- |
| ${sizeToHumanReadable(estimatedSize)} | ${sizeToHumanReadable(maxSize)} |
> [!MORE]-
> If you have been using it for many years, there may be unreferenced chunks - that is, garbage - accumulating in the database. Therefore, we recommend rebuilding everything. It will probably become much smaller.
>
> If the volume of your vault is simply increasing, it is better to rebuild everything after organizing the files. Self-hosted LiveSync does not delete the actual data even if you delete it to speed up the process. It is roughly [documented](https://github.com/vrtmrz/obsidian-livesync/blob/main/docs/tech_info.md).
>
> If you don't mind the increase, you can increase the notification limit by 100MB. This is the case if you are running it on your own server. However, it is better to rebuild everything from time to time.
>
> [!WARNING]
> If you perform rebuild everything, make sure all devices are synchronised. The plug-in will merge as much as possible, though.
\n`;
const newMax = ~~(estimatedSize / 1024 / 1024) + 100;
const ANSWER_ENLARGE_LIMIT = `increase to ${newMax}MB`;
const ANSWER_REBUILD = "Rebuild Everything Now";
const ANSWER_IGNORE = "Dismiss";
const ret = await this.core.confirm.askSelectStringDialogue(message, [ANSWER_ENLARGE_LIMIT, ANSWER_REBUILD, ANSWER_IGNORE,], {
defaultAction: ANSWER_IGNORE,
title: "Remote storage size exceeded the limit", timeout: 60
}); });
const newMax = ~~(estimatedSize / 1024 / 1024) + 100;
const ANSWER_ENLARGE_LIMIT = $msg("moduleCheckRemoteSize.optionIncreaseLimit", {
newMax: newMax.toString(),
});
const ANSWER_REBUILD = $msg("moduleCheckRemoteSize.optionRebuildAll");
const ANSWER_IGNORE = $msg("moduleCheckRemoteSize.optionDismiss");
const ret = await this.core.confirm.askSelectStringDialogue(
message,
[ANSWER_ENLARGE_LIMIT, ANSWER_REBUILD, ANSWER_IGNORE],
{
defaultAction: ANSWER_IGNORE,
title: $msg("moduleCheckRemoteSize.titleDatabaseSizeLimitExceeded"),
timeout: 60,
}
);
if (ret == ANSWER_REBUILD) { if (ret == ANSWER_REBUILD) {
const ret = await this.core.confirm.askYesNoDialog("This may take a bit of a long time. Do you really want to rebuild everything now?", { defaultOption: "No" }); const ret = await this.core.confirm.askYesNoDialog(
$msg("moduleCheckRemoteSize.msgConfirmRebuild"),
{ defaultOption: "No" }
);
if (ret == "yes") { if (ret == "yes") {
this.core.settings.notifyThresholdOfRemoteStorageSize = -1; this.core.settings.notifyThresholdOfRemoteStorageSize = -1;
await this.saveSettings(); await this.saveSettings();
@@ -82,20 +72,37 @@ If we have reached the limit, we will be asked to enlarge the limit step by step
} }
} else if (ret == ANSWER_ENLARGE_LIMIT) { } else if (ret == ANSWER_ENLARGE_LIMIT) {
this.settings.notifyThresholdOfRemoteStorageSize = ~~(estimatedSize / 1024 / 1024) + 100; this.settings.notifyThresholdOfRemoteStorageSize = ~~(estimatedSize / 1024 / 1024) + 100;
this._log(`Threshold has been enlarged to ${this.settings.notifyThresholdOfRemoteStorageSize}MB`, LOG_LEVEL_NOTICE); this._log(
$msg("moduleCheckRemoteSize.logThresholdEnlarged", {
size: this.settings.notifyThresholdOfRemoteStorageSize.toString(),
}),
LOG_LEVEL_NOTICE
);
await this.core.saveSettings(); await this.core.saveSettings();
} else { } else {
// Dismiss or Close the dialog // Dismiss or Close the dialog
} }
this._log(`Remote storage size: ${sizeToHumanReadable(estimatedSize)} exceeded ${sizeToHumanReadable(this.settings.notifyThresholdOfRemoteStorageSize * 1024 * 1024)} `, LOG_LEVEL_INFO); this._log(
$msg("moduleCheckRemoteSize.logExceededWarning", {
measuredSize: sizeToHumanReadable(estimatedSize),
notifySize: sizeToHumanReadable(
this.settings.notifyThresholdOfRemoteStorageSize * 1024 * 1024
),
}),
LOG_LEVEL_INFO
);
} else { } else {
this._log(`Remote storage size: ${sizeToHumanReadable(estimatedSize)}`, LOG_LEVEL_INFO); this._log(
$msg("moduleCheckRemoteSize.logCurrentStorageSize", {
measuredSize: sizeToHumanReadable(estimatedSize),
}),
LOG_LEVEL_INFO
);
} }
} }
} }
} }
return true; return true;
} }
} }

View File

@@ -5,7 +5,6 @@ import { sendValue } from "octagonal-wheels/messagepassing/signal";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
export class ModuleConflictChecker extends AbstractModule implements ICoreModule { export class ModuleConflictChecker extends AbstractModule implements ICoreModule {
async $$queueConflictCheckIfOpen(file: FilePathWithPrefix): Promise<void> { async $$queueConflictCheckIfOpen(file: FilePathWithPrefix): Promise<void> {
const path = file; const path = file;
if (this.settings.checkConflictOnlyOnOpen) { if (this.settings.checkConflictOnlyOnOpen) {
@@ -36,40 +35,42 @@ export class ModuleConflictChecker extends AbstractModule implements ICoreModule
} }
// TODO-> Move to ModuleConflictResolver? // TODO-> Move to ModuleConflictResolver?
conflictResolveQueue = new QueueProcessor(async (filenames: FilePathWithPrefix[]) => { conflictResolveQueue = new QueueProcessor(
await this.core.$$resolveConflict(filenames[0]); async (filenames: FilePathWithPrefix[]) => {
}, { const filename = filenames[0];
suspended: false, return await this.core.$$resolveConflict(filename);
batchSize: 1, },
concurrentLimit: 1, {
delay: 10, suspended: false,
keepResultUntilDownstreamConnected: false batchSize: 1,
}).replaceEnqueueProcessor((queue, newEntity) => { // No need to limit concurrency to `1` here, subsequent process will handle it,
// And, some cases, we do not need to synchronised. (e.g., auto-merge available).
// Therefore, limiting global concurrency is performed on resolver with the UI.
concurrentLimit: 10,
delay: 0,
keepResultUntilDownstreamConnected: false,
}
).replaceEnqueueProcessor((queue, newEntity) => {
const filename = newEntity; const filename = newEntity;
sendValue("cancel-resolve-conflict:" + filename, true); sendValue("cancel-resolve-conflict:" + filename, true);
const newQueue = [...queue].filter(e => e != newEntity); const newQueue = [...queue].filter((e) => e != newEntity);
return [...newQueue, newEntity]; return [...newQueue, newEntity];
}); });
conflictCheckQueue = // First process - Check is the file actually need resolve - conflictCheckQueue = // First process - Check is the file actually need resolve -
new QueueProcessor((files: FilePathWithPrefix[]) => { new QueueProcessor(
const filename = files[0]; (files: FilePathWithPrefix[]) => {
// const file = await this.core.storageAccess.isExists(filename); const filename = files[0];
// if (!file) return []; return Promise.resolve([filename]);
// if (!(file instanceof TFile)) return; },
// if ((file instanceof TFolder)) return []; {
// Check again? suspended: false,
return Promise.resolve([filename]); batchSize: 1,
// this.conflictResolveQueue.enqueueWithKey(filename, { filename, file }); concurrentLimit: 10,
}, { delay: 0,
suspended: false, keepResultUntilDownstreamConnected: true,
batchSize: 1, pipeTo: this.conflictResolveQueue,
concurrentLimit: 5, totalRemainingReactiveSource: this.core.conflictProcessQueueCount,
delay: 10, }
keepResultUntilDownstreamConnected: true, );
pipeTo: this.conflictResolveQueue,
totalRemainingReactiveSource: this.core.conflictProcessQueueCount
});
} }

View File

@@ -1,34 +1,71 @@
import { serialized } from "octagonal-wheels/concurrency/lock"; import { serialized } from "octagonal-wheels/concurrency/lock";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import { AUTO_MERGED, CANCELLED, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, MISSING_OR_ERROR, NOT_CONFLICTED, type diff_check_result, type FilePathWithPrefix } from "../../lib/src/common/types"; import {
import { compareMTime, displayRev, TARGET_IS_NEW } from "../../common/utils"; AUTO_MERGED,
CANCELLED,
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
MISSING_OR_ERROR,
NOT_CONFLICTED,
type diff_check_result,
type FilePathWithPrefix,
} from "../../lib/src/common/types";
import {
compareMTime,
displayRev,
isCustomisationSyncMetadata,
isPluginMetadata,
TARGET_IS_NEW,
} from "../../common/utils";
import diff_match_patch from "diff-match-patch"; import diff_match_patch from "diff-match-patch";
import { stripAllPrefixes, isPlainText } from "../../lib/src/string_and_binary/path"; import { stripAllPrefixes, isPlainText } from "../../lib/src/string_and_binary/path";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
import { eventHub } from "../../common/events.ts";
declare global {
interface LSEvents {
"conflict-cancelled": FilePathWithPrefix;
}
}
export class ModuleConflictResolver extends AbstractModule implements ICoreModule { export class ModuleConflictResolver extends AbstractModule implements ICoreModule {
async $$resolveConflictByDeletingRev(
async $$resolveConflictByDeletingRev(path: FilePathWithPrefix, deleteRevision: string, subTitle = ""): Promise<typeof MISSING_OR_ERROR | typeof AUTO_MERGED> { path: FilePathWithPrefix,
deleteRevision: string,
subTitle = ""
): Promise<typeof MISSING_OR_ERROR | typeof AUTO_MERGED> {
const title = `Resolving ${subTitle ? `[${subTitle}]` : ""}:`; const title = `Resolving ${subTitle ? `[${subTitle}]` : ""}:`;
if (!await this.core.fileHandler.deleteRevisionFromDB(path, deleteRevision)) { if (!(await this.core.fileHandler.deleteRevisionFromDB(path, deleteRevision))) {
this._log(`${title} Could not delete conflicted revision ${displayRev(deleteRevision)} of ${path}`, LOG_LEVEL_NOTICE); this._log(
`${title} Could not delete conflicted revision ${displayRev(deleteRevision)} of ${path}`,
LOG_LEVEL_NOTICE
);
return MISSING_OR_ERROR; return MISSING_OR_ERROR;
} }
this._log(`${title} Conflicted revision deleted ${displayRev(deleteRevision)} ${path}`, LOG_LEVEL_INFO); eventHub.emitEvent("conflict-cancelled", path);
this._log(
`${title} Conflicted revision has been deleted ${displayRev(deleteRevision)} ${path}`,
LOG_LEVEL_INFO
);
if ((await this.core.databaseFileAccess.getConflictedRevs(path)).length != 0) { if ((await this.core.databaseFileAccess.getConflictedRevs(path)).length != 0) {
this._log(`${title} some conflicts are left in ${path}`, LOG_LEVEL_INFO); this._log(`${title} some conflicts are left in ${path}`, LOG_LEVEL_INFO);
return AUTO_MERGED; return AUTO_MERGED;
} }
if (isPluginMetadata(path) || isCustomisationSyncMetadata(path)) {
this._log(`${title} ${path} is a plugin metadata file, no need to write to storage`, LOG_LEVEL_INFO);
return AUTO_MERGED;
}
// If no conflicts were found, write the resolved content to the storage. // If no conflicts were found, write the resolved content to the storage.
if (!await this.core.fileHandler.dbToStorage(path, stripAllPrefixes(path), true)) { if (!(await this.core.fileHandler.dbToStorage(path, stripAllPrefixes(path), true))) {
this._log(`Could not write the resolved content to the storage: ${path}`, LOG_LEVEL_NOTICE); this._log(`Could not write the resolved content to the storage: ${path}`, LOG_LEVEL_NOTICE);
return MISSING_OR_ERROR; return MISSING_OR_ERROR;
} }
this._log(`${path} Has been merged automatically`, LOG_LEVEL_NOTICE); const level = subTitle.indexOf("same") !== -1 ? LOG_LEVEL_INFO : LOG_LEVEL_NOTICE;
this._log(`${path} has been merged automatically`, level);
return AUTO_MERGED; return AUTO_MERGED;
} }
async checkConflictAndPerformAutoMerge(path: FilePathWithPrefix): Promise<diff_check_result> { async checkConflictAndPerformAutoMerge(path: FilePathWithPrefix): Promise<diff_check_result> {
// //
const ret = await this.localDatabase.tryAutoMerge(path, !this.settings.disableMarkdownAutoMerge); const ret = await this.localDatabase.tryAutoMerge(path, !this.settings.disableMarkdownAutoMerge);
@@ -40,7 +77,7 @@ export class ModuleConflictResolver extends AbstractModule implements ICoreModul
const p = ret.result; const p = ret.result;
// Merged content is coming. // Merged content is coming.
// 1. Store the merged content to the storage // 1. Store the merged content to the storage
if (!await this.core.databaseFileAccess.storeContent(path, p)) { if (!(await this.core.databaseFileAccess.storeContent(path, p))) {
this._log(`Merged content cannot be stored:${path}`, LOG_LEVEL_NOTICE); this._log(`Merged content cannot be stored:${path}`, LOG_LEVEL_NOTICE);
return MISSING_OR_ERROR; return MISSING_OR_ERROR;
} }
@@ -65,13 +102,19 @@ export class ModuleConflictResolver extends AbstractModule implements ICoreModul
const isBinary = !isPlainText(path); const isBinary = !isPlainText(path);
const alwaysNewer = this.settings.resolveConflictsByNewerFile; const alwaysNewer = this.settings.resolveConflictsByNewerFile;
if (isSame || isBinary || alwaysNewer) { if (isSame || isBinary || alwaysNewer) {
const result = compareMTime(leftLeaf.mtime, rightLeaf.mtime) const result = compareMTime(leftLeaf.mtime, rightLeaf.mtime);
let loser = leftLeaf; let loser = leftLeaf;
// if (lMtime > rMtime) { // if (lMtime > rMtime) {
if (result != TARGET_IS_NEW) { if (result != TARGET_IS_NEW) {
loser = rightLeaf; loser = rightLeaf;
} }
const subTitle = [`${isSame ? "same" : ""}`, `${isBinary ? "binary" : ""}`, `${alwaysNewer ? "alwaysNewer" : ""}`].join(","); const subTitle = [
`${isSame ? "same" : ""}`,
`${isBinary ? "binary" : ""}`,
`${alwaysNewer ? "alwaysNewer" : ""}`,
]
.filter((e) => e.trim())
.join(",");
return await this.core.$$resolveConflictByDeletingRev(path, loser.rev, subTitle); return await this.core.$$resolveConflictByDeletingRev(path, loser.rev, subTitle);
} }
// make diff. // make diff.
@@ -86,54 +129,83 @@ export class ModuleConflictResolver extends AbstractModule implements ICoreModul
}; };
} }
async $$resolveConflict(filename: FilePathWithPrefix): Promise<void> { async $$resolveConflict(filename: FilePathWithPrefix): Promise<void> {
// const filename = filenames[0]; // const filename = filenames[0];
return await serialized(`conflict-resolve:${filename}`, async () => { return await serialized(`conflict-resolve:${filename}`, async () => {
const conflictCheckResult = await this.checkConflictAndPerformAutoMerge(filename); const conflictCheckResult = await this.checkConflictAndPerformAutoMerge(filename);
if (conflictCheckResult === MISSING_OR_ERROR || conflictCheckResult === NOT_CONFLICTED || conflictCheckResult === CANCELLED) { if (
conflictCheckResult === MISSING_OR_ERROR ||
conflictCheckResult === NOT_CONFLICTED ||
conflictCheckResult === CANCELLED
) {
// nothing to do. // nothing to do.
this._log(`conflict:Nothing to do:${filename}`); this._log(`[conflict] Not conflicted or cancelled: ${filename}`, LOG_LEVEL_VERBOSE);
return; return;
} }
if (conflictCheckResult === AUTO_MERGED) { if (conflictCheckResult === AUTO_MERGED) {
//auto resolved, but need check again; //auto resolved, but need check again;
if (this.settings.syncAfterMerge && !this.core.suspended) { if (this.settings.syncAfterMerge && !this.core.$$isSuspended()) {
//Wait for the running replication, if not running replication, run it once. //Wait for the running replication, if not running replication, run it once.
await this.core.$$waitForReplicationOnce(); await this.core.$$replicateByEvent();
} }
this._log("conflict:Automatically merged, but we have to check it again"); this._log("[conflict] Automatically merged, but we have to check it again");
await this.core.$$queueConflictCheck(filename); await this.core.$$queueConflictCheck(filename);
return; return;
} }
if (this.settings.showMergeDialogOnlyOnActive) { if (this.settings.showMergeDialogOnlyOnActive) {
const af = this.core.$$getActiveFilePath(); const af = this.core.$$getActiveFilePath();
if (af && af != filename) { if (af && af != filename) {
this._log(`${filename} is conflicted. Merging process has been postponed to the file have got opened.`, LOG_LEVEL_NOTICE); this._log(
`[conflict] ${filename} is conflicted. Merging process has been postponed to the file have got opened.`,
LOG_LEVEL_NOTICE
);
return; return;
} }
} }
this._log("conflict:Manual merge required!"); this._log("[conflict] Manual merge required!");
eventHub.emitEvent("conflict-cancelled", filename);
await this.core.$anyResolveConflictByUI(filename, conflictCheckResult); await this.core.$anyResolveConflictByUI(filename, conflictCheckResult);
}); });
} }
async $anyResolveConflictByNewest(filename: FilePathWithPrefix): Promise<boolean> { async $anyResolveConflictByNewest(filename: FilePathWithPrefix): Promise<boolean> {
const currentRev = await this.core.databaseFileAccess.fetchEntryMeta(filename, undefined, true);
if (currentRev == false) {
this._log(`Could not get current revision of ${filename}`);
return Promise.resolve(false);
}
const revs = await this.core.databaseFileAccess.getConflictedRevs(filename); const revs = await this.core.databaseFileAccess.getConflictedRevs(filename);
if (revs.length == 0) { if (revs.length == 0) {
return Promise.resolve(true); return Promise.resolve(true);
} }
const mTimeAndRev = (await Promise.all(revs.map(async (rev) => { const mTimeAndRev = (
const leaf = await this.core.databaseFileAccess.fetchEntryMeta(filename, rev); [
if (leaf == false) { [currentRev.mtime, currentRev._rev],
return [0, rev] as [number, string]; ...(await Promise.all(
revs.map(async (rev) => {
const leaf = await this.core.databaseFileAccess.fetchEntryMeta(filename, rev);
if (leaf == false) {
return [0, rev] as [number, string];
}
return [leaf.mtime, rev] as [number, string];
})
)),
] as [number, string][]
).sort((a, b) => {
const diff = b[0] - a[0];
if (diff == 0) {
return a[1].localeCompare(b[1], "en", { numeric: true });
} }
return [leaf.mtime, rev] as [number, string]; return diff;
}))).sort((a, b) => b[0] - a[0]); });
this._log(`Resolving conflict by newest: ${filename} (Newest: ${new Date(mTimeAndRev[0][0]).toLocaleString()}) (${mTimeAndRev.length} revisions exists)`); // console.warn(mTimeAndRev);
this._log(
`Resolving conflict by newest: ${filename} (Newest: ${new Date(mTimeAndRev[0][0]).toLocaleString()}) (${mTimeAndRev.length} revisions exists)`
);
for (let i = 1; i < mTimeAndRev.length; i++) { for (let i = 1; i < mTimeAndRev.length; i++) {
this._log(`conflict: Deleting the older revision ${mTimeAndRev[i][1]} (${new Date(mTimeAndRev[i][0]).toLocaleString()}) of ${filename}`); this._log(
`conflict: Deleting the older revision ${mTimeAndRev[i][1]} (${new Date(mTimeAndRev[i][0]).toLocaleString()}) of ${filename}`
);
await this.core.$$resolveConflictByDeletingRev(filename, mTimeAndRev[i][1], "NEWEST"); await this.core.$$resolveConflictByDeletingRev(filename, mTimeAndRev[i][1], "NEWEST");
} }
return true; return true;

View File

@@ -1,11 +1,17 @@
import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import { normalizePath } from "../../deps.ts"; import { normalizePath } from "../../deps.ts";
import { FLAGMD_REDFLAG, FLAGMD_REDFLAG2, FLAGMD_REDFLAG2_HR, FLAGMD_REDFLAG3, FLAGMD_REDFLAG3_HR } from "../../lib/src/common/types.ts"; import {
FLAGMD_REDFLAG,
FLAGMD_REDFLAG2,
FLAGMD_REDFLAG2_HR,
FLAGMD_REDFLAG3,
FLAGMD_REDFLAG3_HR,
} from "../../lib/src/common/types.ts";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
import { $msg } from "../../lib/src/common/i18n.ts";
export class ModuleRedFlag extends AbstractModule implements ICoreModule { export class ModuleRedFlag extends AbstractModule implements ICoreModule {
async isFlagFileExist(path: string) { async isFlagFileExist(path: string) {
const redflag = await this.core.storageAccess.isExists(normalizePath(path)); const redflag = await this.core.storageAccess.isExists(normalizePath(path));
if (redflag) { if (redflag) {
@@ -26,9 +32,11 @@ export class ModuleRedFlag extends AbstractModule implements ICoreModule {
} }
} }
isRedFlagRaised = async () => await this.isFlagFileExist(FLAGMD_REDFLAG) isRedFlagRaised = async () => await this.isFlagFileExist(FLAGMD_REDFLAG);
isRedFlag2Raised = async () => await this.isFlagFileExist(FLAGMD_REDFLAG2) || await this.isFlagFileExist(FLAGMD_REDFLAG2_HR) isRedFlag2Raised = async () =>
isRedFlag3Raised = async () => await this.isFlagFileExist(FLAGMD_REDFLAG3) || await this.isFlagFileExist(FLAGMD_REDFLAG3_HR) (await this.isFlagFileExist(FLAGMD_REDFLAG2)) || (await this.isFlagFileExist(FLAGMD_REDFLAG2_HR));
isRedFlag3Raised = async () =>
(await this.isFlagFileExist(FLAGMD_REDFLAG3)) || (await this.isFlagFileExist(FLAGMD_REDFLAG3_HR));
async deleteRedFlag2() { async deleteRedFlag2() {
await this.deleteFlagFile(FLAGMD_REDFLAG2); await this.deleteFlagFile(FLAGMD_REDFLAG2);
@@ -47,14 +55,24 @@ export class ModuleRedFlag extends AbstractModule implements ICoreModule {
if (isRedFlagRaised || isRedFlag2Raised || isRedFlag3Raised) { if (isRedFlagRaised || isRedFlag2Raised || isRedFlag3Raised) {
if (isRedFlag2Raised) { if (isRedFlag2Raised) {
if (await this.core.confirm.askYesNoDialog("Rebuild everything has been scheduled! Are you sure to rebuild everything?", { defaultOption: "Yes", timeout: 0 }) !== "yes") { if (
(await this.core.confirm.askYesNoDialog(
"Rebuild everything has been scheduled! Are you sure to rebuild everything?",
{ defaultOption: "Yes", timeout: 0 }
)) !== "yes"
) {
await this.deleteRedFlag2(); await this.deleteRedFlag2();
await this.core.$$performRestart(); await this.core.$$performRestart();
return false; return false;
} }
} }
if (isRedFlag3Raised) { if (isRedFlag3Raised) {
if (await this.core.confirm.askYesNoDialog("Fetch again has been scheduled! Are you sure?", { defaultOption: "Yes", timeout: 0 }) !== "yes") { if (
(await this.core.confirm.askYesNoDialog("Fetch again has been scheduled! Are you sure?", {
defaultOption: "Yes",
timeout: 0,
})) !== "yes"
) {
await this.deleteRedFlag3(); await this.deleteRedFlag3();
await this.core.$$performRestart(); await this.core.$$performRestart();
return false; return false;
@@ -66,39 +84,82 @@ export class ModuleRedFlag extends AbstractModule implements ICoreModule {
this.settings.suspendFileWatching = true; this.settings.suspendFileWatching = true;
await this.saveSettings(); await this.saveSettings();
if (isRedFlag2Raised) { if (isRedFlag2Raised) {
this._log(`${FLAGMD_REDFLAG2} or ${FLAGMD_REDFLAG2_HR} has been detected! Self-hosted LiveSync suspends all sync and rebuild everything.`, LOG_LEVEL_NOTICE); this._log(
`${FLAGMD_REDFLAG2} or ${FLAGMD_REDFLAG2_HR} has been detected! Self-hosted LiveSync suspends all sync and rebuild everything.`,
LOG_LEVEL_NOTICE
);
await this.core.rebuilder.$rebuildEverything(); await this.core.rebuilder.$rebuildEverything();
await this.deleteRedFlag2(); await this.deleteRedFlag2();
if (await this.core.confirm.askYesNoDialog("Do you want to resume file and database processing, and restart obsidian now?", { defaultOption: "Yes", timeout: 15 }) == "yes") { if (
(await this.core.confirm.askYesNoDialog(
"Do you want to resume file and database processing, and restart obsidian now?",
{ defaultOption: "Yes", timeout: 15 }
)) == "yes"
) {
this.settings.suspendFileWatching = false; this.settings.suspendFileWatching = false;
await this.saveSettings(); await this.saveSettings();
this.core.$$performRestart(); this.core.$$performRestart();
return false; return false;
} }
} else if (isRedFlag3Raised) { } else if (isRedFlag3Raised) {
this._log(`${FLAGMD_REDFLAG3} or ${FLAGMD_REDFLAG3_HR} has been detected! Self-hosted LiveSync will discard the local database and fetch everything from the remote once again.`, LOG_LEVEL_NOTICE); this._log(
const makeLocalChunkBeforeSync = ((await this.core.confirm.askYesNoDialog(`Do you want to create local chunks before fetching? `${FLAGMD_REDFLAG3} or ${FLAGMD_REDFLAG3_HR} has been detected! Self-hosted LiveSync will discard the local database and fetch everything from the remote once again.`,
> [!MORE]- LOG_LEVEL_NOTICE
> If creating local chunks before fetching, only the difference between the local and remote will be fetched. );
const method1 = $msg("RedFlag.Fetch.Method.FetchSafer");
const method2 = $msg("RedFlag.Fetch.Method.FetchSmoother");
const method3 = $msg("RedFlag.Fetch.Method.FetchTraditional");
const methods = [method1, method2, method3] as const;
const chunkMode = await this.core.confirm.askSelectStringDialogue(
$msg("RedFlag.Fetch.Method.Desc"),
methods,
{
defaultAction: method1,
timeout: 0,
title: $msg("RedFlag.Fetch.Method.Title"),
}
);
let makeLocalChunkBeforeSync = false;
let makeLocalFilesBeforeSync = false;
if (chunkMode === method1) {
makeLocalFilesBeforeSync = true;
} else if (chunkMode === method2) {
makeLocalChunkBeforeSync = true;
} else if (chunkMode === method3) {
// Do nothing.
} else {
this._log("Cancelled the fetch operation", LOG_LEVEL_NOTICE);
return false;
}
await this.core.rebuilder.$fetchLocal(makeLocalChunkBeforeSync, !makeLocalFilesBeforeSync);
`, { defaultOption: "Yes", title: "Trick to transfer efficiently" })) == "yes");
await this.core.rebuilder.$fetchLocal(makeLocalChunkBeforeSync);
await this.deleteRedFlag3(); await this.deleteRedFlag3();
if (this.settings.suspendFileWatching) { if (this.settings.suspendFileWatching) {
if (await this.core.confirm.askYesNoDialog("Do you want to resume file and database processing, and restart obsidian now?", { defaultOption: "Yes", timeout: 15 }) == "yes") { if (
(await this.core.confirm.askYesNoDialog(
"Do you want to resume file and database processing, and restart obsidian now?",
{ defaultOption: "Yes", timeout: 15 }
)) == "yes"
) {
this.settings.suspendFileWatching = false; this.settings.suspendFileWatching = false;
await this.saveSettings(); await this.saveSettings();
this.core.$$performRestart(); this.core.$$performRestart();
return false; return false;
} }
} else { } else {
this._log("Your content of files will be synchronised gradually. Please wait for the completion.", LOG_LEVEL_NOTICE); this._log(
"Your content of files will be synchronised gradually. Please wait for the completion.",
LOG_LEVEL_NOTICE
);
} }
} else { } else {
// Case of FLAGMD_REDFLAG. // Case of FLAGMD_REDFLAG.
this.settings.writeLogToTheFile = true; this.settings.writeLogToTheFile = true;
// await this.plugin.openDatabase(); // await this.plugin.openDatabase();
const warningMessage = "The red flag is raised! The whole initialize steps are skipped, and any file changes are not captured."; const warningMessage =
"The red flag is raised! The whole initialize steps are skipped, and any file changes are not captured.";
this._log(warningMessage, LOG_LEVEL_NOTICE); this._log(warningMessage, LOG_LEVEL_NOTICE);
} }
} }
@@ -108,5 +169,4 @@ export class ModuleRedFlag extends AbstractModule implements ICoreModule {
} }
return true; return true;
} }
} }

View File

@@ -1,84 +1,154 @@
import { Logger, LOG_LEVEL_NOTICE } from "octagonal-wheels/common/logger"; import { Logger, LOG_LEVEL_NOTICE } from "octagonal-wheels/common/logger";
import { extractObject } from "octagonal-wheels/object"; import { extractObject } from "octagonal-wheels/object";
import { TweakValuesShouldMatchedTemplate, CompatibilityBreakingTweakValues, confName, type TweakValues } from "../../lib/src/common/types.ts"; import {
TweakValuesShouldMatchedTemplate,
IncompatibleChanges,
confName,
type TweakValues,
type RemoteDBSettings,
IncompatibleChangesInSpecificPattern,
CompatibleButLossyChanges,
} from "../../lib/src/common/types.ts";
import { escapeMarkdownValue } from "../../lib/src/common/utils.ts"; import { escapeMarkdownValue } from "../../lib/src/common/utils.ts";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
import { $msg } from "../../lib/src/common/i18n.ts";
export class ModuleResolvingMismatchedTweaks extends AbstractModule implements ICoreModule { export class ModuleResolvingMismatchedTweaks extends AbstractModule implements ICoreModule {
async $anyAfterConnectCheckFailed(): Promise<boolean | "CHECKAGAIN" | undefined> { async $anyAfterConnectCheckFailed(): Promise<boolean | "CHECKAGAIN" | undefined> {
if (!this.core.replicator.tweakSettingsMismatched) return false; if (!this.core.replicator.tweakSettingsMismatched && !this.core.replicator.preferredTweakValue) return false;
const ret = await this.core.$$askResolvingMismatchedTweaks(); const preferred = this.core.replicator.preferredTweakValue;
if (!preferred) return false;
const ret = await this.core.$$askResolvingMismatchedTweaks(preferred);
if (ret == "OK") return false; if (ret == "OK") return false;
if (ret == "CHECKAGAIN") return "CHECKAGAIN"; if (ret == "CHECKAGAIN") return "CHECKAGAIN";
if (ret == "IGNORE") return true; if (ret == "IGNORE") return true;
} }
async $$askResolvingMismatchedTweaks(): Promise<"OK" | "CHECKAGAIN" | "IGNORE"> { async $$checkAndAskResolvingMismatchedTweaks(
if (!this.core.replicator.tweakSettingsMismatched) { preferred: Partial<TweakValues>
return "OK"; ): Promise<[TweakValues | boolean, boolean]> {
}
const preferred = extractObject(TweakValuesShouldMatchedTemplate, this.core.replicator.preferredTweakValue!);
const mine = extractObject(TweakValuesShouldMatchedTemplate, this.settings); const mine = extractObject(TweakValuesShouldMatchedTemplate, this.settings);
const items = Object.entries(TweakValuesShouldMatchedTemplate); const items = Object.entries(TweakValuesShouldMatchedTemplate);
let rebuildRequired = false; let rebuildRequired = false;
let rebuildRecommended = false;
// Making tables: // Making tables:
let table = `| Value name | This device | Configured | \n` + `|: --- |: --- :|: ---- :| \n`; // let table = `| Value name | This device | Configured | \n` + `|: --- |: --- :|: ---- :| \n`;
const tableRows = [];
// const items = [mine,preferred] // const items = [mine,preferred]
for (const v of items) { for (const v of items) {
const key = v[0] as keyof typeof TweakValuesShouldMatchedTemplate; const key = v[0] as keyof typeof TweakValuesShouldMatchedTemplate;
const valueMine = escapeMarkdownValue(mine[key]); const valueMine = escapeMarkdownValue(mine[key]);
const valuePreferred = escapeMarkdownValue(preferred[key]); const valuePreferred = escapeMarkdownValue(preferred[key]);
if (valueMine == valuePreferred) continue; if (valueMine == valuePreferred) continue;
if (CompatibilityBreakingTweakValues.indexOf(key) !== -1) { if (IncompatibleChanges.indexOf(key) !== -1) {
rebuildRequired = true; rebuildRequired = true;
} }
table += `| ${confName(key)} | ${valueMine} | ${valuePreferred} | \n`; for (const pattern of IncompatibleChangesInSpecificPattern) {
if (pattern.key !== key) continue;
// if from value supplied, check if current value have been violated : in other words, if the current value is the same as the from value, it should require a rebuild.
const isFromConditionMet = "from" in pattern ? pattern.from === mine[key] : false;
// and, if to value supplied, same as above.
const isToConditionMet = "to" in pattern ? pattern.to === preferred[key] : false;
// if either of them is true, it should require a rebuild, if the pattern is not a recommendation.
if (isFromConditionMet || isToConditionMet) {
if (pattern.isRecommendation) {
rebuildRecommended = true;
} else {
rebuildRequired = true;
}
}
}
if (CompatibleButLossyChanges.indexOf(key) !== -1) {
rebuildRecommended = true;
}
// table += `| ${confName(key)} | ${valueMine} | ${valuePreferred} | \n`;
tableRows.push(
$msg("TweakMismatchResolve.Table.Row", {
name: confName(key),
self: valueMine,
remote: valuePreferred,
})
);
} }
const additionalMessage = rebuildRequired ? ` const additionalMessage =
rebuildRequired && this.core.settings.isConfigured
? $msg("TweakMismatchResolve.Message.WarningIncompatibleRebuildRequired")
: "";
const additionalMessage2 =
rebuildRecommended && this.core.settings.isConfigured
? $msg("TweakMismatchResolve.Message.WarningIncompatibleRebuildRecommended")
: "";
**Note**: We have detected that some of the values are different to make incompatible the local database with the remote database. const table = $msg("TweakMismatchResolve.Table", { rows: tableRows.join("\n") });
If you choose to use the configured values, the local database will be rebuilt, and if you choose to use the values of this device, the remote database will be rebuilt.
Both of them takes a few minutes. Please choose after considering the situation.` : "";
const message = ` const message = $msg("TweakMismatchResolve.Message.MainTweakResolving", {
Your configuration has not been matched with the one on the remote server. table: table,
(Which you had decided once before, or set by initially synchronised device). additionalMessage: [additionalMessage, additionalMessage2].filter((v) => v).join("\n"),
});
Configured values: const CHOICE_USE_REMOTE = $msg("TweakMismatchResolve.Action.UseRemote");
const CHOICE_USE_REMOTE_WITH_REBUILD = $msg("TweakMismatchResolve.Action.UseRemoteWithRebuild");
const CHOICE_USE_REMOTE_PREVENT_REBUILD = $msg("TweakMismatchResolve.Action.UseRemoteAcceptIncompatible");
const CHOICE_USE_MINE = $msg("TweakMismatchResolve.Action.UseMine");
const CHOICE_USE_MINE_WITH_REBUILD = $msg("TweakMismatchResolve.Action.UseMineWithRebuild");
const CHOICE_USE_MINE_PREVENT_REBUILD = $msg("TweakMismatchResolve.Action.UseMineAcceptIncompatible");
const CHOICE_DISMISS = $msg("TweakMismatchResolve.Action.Dismiss");
${table} const CHOICE_AND_VALUES = [] as [string, [result: TweakValues | boolean, rebuild: boolean]][];
Please select which one you want to use. if (rebuildRequired) {
CHOICE_AND_VALUES.push([CHOICE_USE_REMOTE_WITH_REBUILD, [preferred, true]]);
CHOICE_AND_VALUES.push([CHOICE_USE_MINE_WITH_REBUILD, [true, true]]);
CHOICE_AND_VALUES.push([CHOICE_USE_REMOTE_PREVENT_REBUILD, [preferred, false]]);
CHOICE_AND_VALUES.push([CHOICE_USE_MINE_PREVENT_REBUILD, [true, false]]);
} else if (rebuildRecommended) {
CHOICE_AND_VALUES.push([CHOICE_USE_REMOTE, [preferred, false]]);
CHOICE_AND_VALUES.push([CHOICE_USE_MINE, [true, false]]);
CHOICE_AND_VALUES.push([CHOICE_USE_REMOTE_WITH_REBUILD, [true, true]]);
CHOICE_AND_VALUES.push([CHOICE_USE_MINE_WITH_REBUILD, [true, true]]);
} else {
CHOICE_AND_VALUES.push([CHOICE_USE_REMOTE, [preferred, false]]);
CHOICE_AND_VALUES.push([CHOICE_USE_MINE, [true, false]]);
}
CHOICE_AND_VALUES.push([CHOICE_DISMISS, [false, false]]);
const CHOICES = Object.fromEntries(CHOICE_AND_VALUES) as Record<
string,
[TweakValues | boolean, performRebuild: boolean]
>;
const retKey = await this.core.confirm.askSelectStringDialogue(message, Object.keys(CHOICES), {
title: $msg("TweakMismatchResolve.Title.TweakResolving"),
timeout: 60,
defaultAction: CHOICE_DISMISS,
});
if (!retKey) return [false, false];
return CHOICES[retKey];
}
- Use configured: Update settings of this device by configured one on the remote server. async $$askResolvingMismatchedTweaks(): Promise<"OK" | "CHECKAGAIN" | "IGNORE"> {
You should select this if you have changed the settings on ** another device **. if (!this.core.replicator.tweakSettingsMismatched) {
- Update with mine: Update settings on the remote server by the settings of this device. return "OK";
You should select this if you have changed the settings on ** this device **. }
- Dismiss: Ignore this message and keep the current settings. const tweaks = this.core.replicator.preferredTweakValue;
You cannot synchronise until you resolve this issue without enabling \`Do not check configuration mismatch before replication\`.${additionalMessage}`; if (!tweaks) {
return "IGNORE";
}
const preferred = extractObject(TweakValuesShouldMatchedTemplate, tweaks);
const CHOICE_USE_REMOTE = "Use configured"; const [conf, rebuildRequired] = await this.core.$$checkAndAskResolvingMismatchedTweaks(preferred);
const CHOICE_USR_MINE = "Update with mine"; if (!conf) return "IGNORE";
const CHOICE_DISMISS = "Dismiss";
const CHOICE_AND_VALUES = [
[CHOICE_USE_REMOTE, preferred],
[CHOICE_USR_MINE, true],
[CHOICE_DISMISS, false]]
const CHOICES = Object.fromEntries(CHOICE_AND_VALUES) as Record<string, TweakValues | boolean>;
const retKey = await this.core.confirm.confirmWithMessage("Tweaks Mismatched or Changed", message, Object.keys(CHOICES), CHOICE_DISMISS, 60);
if (!retKey) return "IGNORE";
const conf = CHOICES[retKey];
if (conf === true) { if (conf === true) {
await this.core.replicator.setPreferredRemoteTweakSettings(this.settings); await this.core.replicator.setPreferredRemoteTweakSettings(this.settings);
if (rebuildRequired) { if (rebuildRequired) {
await this.core.rebuilder.$rebuildRemote(); await this.core.rebuilder.$rebuildRemote();
} }
Logger(`Tweak values on the remote server have been updated. Your other device will see this message.`, LOG_LEVEL_NOTICE); Logger(
`Tweak values on the remote server have been updated. Your other device will see this message.`,
LOG_LEVEL_NOTICE
);
return "CHECKAGAIN"; return "CHECKAGAIN";
} }
if (conf) { if (conf) {
@@ -92,6 +162,114 @@ Please select which one you want to use.
return "CHECKAGAIN"; return "CHECKAGAIN";
} }
return "IGNORE"; return "IGNORE";
}
async $$checkAndAskUseRemoteConfiguration(
trialSetting: RemoteDBSettings
): Promise<{ result: false | TweakValues; requireFetch: boolean }> {
const replicator = await this.core.$anyNewReplicator(trialSetting);
if (await replicator.tryConnectRemote(trialSetting)) {
const preferred = await replicator.getRemotePreferredTweakValues(trialSetting);
if (preferred) {
return await this.$$askUseRemoteConfiguration(trialSetting, preferred);
} else {
this._log("Failed to get the preferred tweak values from the remote server.", LOG_LEVEL_NOTICE);
}
return { result: false, requireFetch: false };
} else {
this._log("Failed to connect to the remote server.", LOG_LEVEL_NOTICE);
return { result: false, requireFetch: false };
}
}
async $$askUseRemoteConfiguration(
trialSetting: RemoteDBSettings,
preferred: TweakValues
): Promise<{ result: false | TweakValues; requireFetch: boolean }> {
const items = Object.entries(TweakValuesShouldMatchedTemplate);
let rebuildRequired = false;
let rebuildRecommended = false;
// Making tables:
// let table = `| Value name | This device | On Remote | \n` + `|: --- |: ---- :|: ---- :| \n`;
let differenceCount = 0;
const tableRows = [] as string[];
// const items = [mine,preferred]
for (const v of items) {
const key = v[0] as keyof typeof TweakValuesShouldMatchedTemplate;
const remoteValueForDisplay = escapeMarkdownValue(preferred[key]);
const currentValueForDisplay = `${escapeMarkdownValue((trialSetting as TweakValues)?.[key])}`;
if ((trialSetting as TweakValues)?.[key] !== preferred[key]) {
if (IncompatibleChanges.indexOf(key) !== -1) {
rebuildRequired = true;
}
for (const pattern of IncompatibleChangesInSpecificPattern) {
if (pattern.key !== key) continue;
// if from value supplied, check if current value have been violated : in other words, if the current value is the same as the from value, it should require a rebuild.
const isFromConditionMet =
"from" in pattern ? pattern.from === (trialSetting as TweakValues)?.[key] : false;
// and, if to value supplied, same as above.
const isToConditionMet = "to" in pattern ? pattern.to === preferred[key] : false;
// if either of them is true, it should require a rebuild, if the pattern is not a recommendation.
if (isFromConditionMet || isToConditionMet) {
if (pattern.isRecommendation) {
rebuildRecommended = true;
} else {
rebuildRequired = true;
}
}
}
if (CompatibleButLossyChanges.indexOf(key) !== -1) {
rebuildRecommended = true;
}
} else {
continue;
}
tableRows.push(
$msg("TweakMismatchResolve.Table.Row", {
name: confName(key),
self: currentValueForDisplay,
remote: remoteValueForDisplay,
})
);
differenceCount++;
}
if (differenceCount === 0) {
this._log("The settings in the remote database are the same as the local database.", LOG_LEVEL_NOTICE);
return { result: false, requireFetch: false };
}
const additionalMessage =
rebuildRequired && this.core.settings.isConfigured
? $msg("TweakMismatchResolve.Message.UseRemote.WarningRebuildRequired")
: "";
const additionalMessage2 =
rebuildRecommended && this.core.settings.isConfigured
? $msg("TweakMismatchResolve.Message.UseRemote.WarningRebuildRecommended")
: "";
const table = $msg("TweakMismatchResolve.Table", { rows: tableRows.join("\n") });
const message = $msg("TweakMismatchResolve.Message.Main", {
table: table,
additionalMessage: [additionalMessage, additionalMessage2].filter((v) => v).join("\n"),
});
const CHOICE_USE_REMOTE = $msg("TweakMismatchResolve.Action.UseConfigured");
const CHOICE_DISMISS = $msg("TweakMismatchResolve.Action.Dismiss");
// const CHOICE_AND_VALUES = [
// [CHOICE_USE_REMOTE, preferred],
// [CHOICE_DISMISS, false]]
const CHOICES = [CHOICE_USE_REMOTE, CHOICE_DISMISS];
const retKey = await this.core.confirm.askSelectStringDialogue(message, CHOICES, {
title: $msg("TweakMismatchResolve.Title.UseRemoteConfig"),
timeout: 0,
defaultAction: CHOICE_DISMISS,
});
if (!retKey) return { result: false, requireFetch: false };
if (retKey === CHOICE_DISMISS) return { result: false, requireFetch: false };
if (retKey === CHOICE_USE_REMOTE) {
return { result: { ...trialSetting, ...preferred }, requireFetch: rebuildRequired };
}
return { result: false, requireFetch: false };
} }
} }

View File

@@ -1,13 +1,20 @@
import { normalizePath, TFile, TFolder, type ListedFiles } from "obsidian"; import { TFile, TFolder, type ListedFiles } from "obsidian";
import { SerializedFileAccess } from "./storageLib/SerializedFileAccess"; import { SerializedFileAccess } from "./storageLib/SerializedFileAccess";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import type { FilePath, FilePathWithPrefix, UXDataWriteOptions, UXFileInfo, UXFileInfoStub, UXFolderInfo, UXStat } from "../../lib/src/common/types"; import type {
FilePath,
FilePathWithPrefix,
UXDataWriteOptions,
UXFileInfo,
UXFileInfoStub,
UXFolderInfo,
UXStat,
} from "../../lib/src/common/types";
import { TFileToUXFileInfoStub, TFolderToUXFileInfoStub } from "./storageLib/utilObsidian.ts"; import { TFileToUXFileInfoStub, TFolderToUXFileInfoStub } from "./storageLib/utilObsidian.ts";
import { StorageEventManagerObsidian, type StorageEventManager } from "./storageLib/StorageEventManager"; import { StorageEventManagerObsidian, type StorageEventManager } from "./storageLib/StorageEventManager";
import type { StorageAccess } from "../interfaces/StorageAccess"; import type { StorageAccess } from "../interfaces/StorageAccess";
import { createBlob } from "../../lib/src/common/utils"; import { createBlob, type CustomRegExp } from "../../lib/src/common/utils";
export class ModuleFileAccessObsidian extends AbstractObsidianModule implements IObsidianModule, StorageAccess { export class ModuleFileAccessObsidian extends AbstractObsidianModule implements IObsidianModule, StorageAccess {
vaultAccess!: SerializedFileAccess; vaultAccess!: SerializedFileAccess;
@@ -43,12 +50,33 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
return this.vaultAccess.isStorageInsensitive(); return this.vaultAccess.isStorageInsensitive();
} }
$$shouldCheckCaseInsensitive(): boolean {
if (this.$$isStorageInsensitive()) return false;
return !this.settings.handleFilenameCaseSensitive;
}
async writeFileAuto(path: string, data: string | ArrayBuffer, opt?: UXDataWriteOptions): Promise<boolean> { async writeFileAuto(path: string, data: string | ArrayBuffer, opt?: UXDataWriteOptions): Promise<boolean> {
const file = this.vaultAccess.getAbstractFileByPath(path); const file = this.vaultAccess.getAbstractFileByPath(path);
if (file instanceof TFile) { if (file instanceof TFile) {
return this.vaultAccess.vaultModify(file, data, opt); return this.vaultAccess.vaultModify(file, data, opt);
} else if (file === null) { } else if (file === null) {
return await this.vaultAccess.vaultCreate(path, data, opt) instanceof TFile; if (!path.endsWith(".md")) {
// Very rare case, we encountered this case with `writing-goals-history.csv` file.
// Indeed, that file not appears in the File Explorer, but it exists in the vault.
// Hence, we cannot retrieve the file from the vault by getAbstractFileByPath, and we cannot write it via vaultModify.
// It makes `File already exists` error.
// Therefore, we need to write it via adapterWrite.
// Maybe there are others like this, so I will write it via adapterWrite.
// This is a workaround for the issue, but I don't know if this is the right solution.
// (So limits to non-md files).
// Has Obsidian been patched?, anyway, writing directly might be a safer approach.
// However, does changes of that file trigger file-change event?
await this.vaultAccess.adapterWrite(path, data, opt);
// For safety, check existence
return await this.vaultAccess.adapterExists(path);
} else {
return (await this.vaultAccess.vaultCreate(path, data, opt)) instanceof TFile;
}
} else { } else {
this._log(`Could not write file (Possibly already exists as a folder): ${path}`, LOG_LEVEL_VERBOSE); this._log(`Could not write file (Possibly already exists as a folder): ${path}`, LOG_LEVEL_VERBOSE);
return false; return false;
@@ -85,7 +113,6 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
} }
async appendHiddenFile(path: string, data: string, opt?: UXDataWriteOptions): Promise<boolean> { async appendHiddenFile(path: string, data: string, opt?: UXDataWriteOptions): Promise<boolean> {
try { try {
await this.vaultAccess.adapterAppend(path, data, opt); await this.vaultAccess.adapterAppend(path, data, opt);
return true; return true;
} catch (e) { } catch (e) {
@@ -102,19 +129,21 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
ctime: file.stat.ctime, ctime: file.stat.ctime,
mtime: file.stat.mtime, mtime: file.stat.mtime,
size: file.stat.size, size: file.stat.size,
type: "file" type: "file",
}); });
} else { } else {
throw new Error(`Could not stat file (Possibly does not exist): ${path}`); throw new Error(`Could not stat file (Possibly does not exist): ${path}`);
} }
} }
statHidden(path: string): Promise<UXStat | null> { statHidden(path: string): Promise<UXStat | null> {
return this.vaultAccess.adapterStat(path); return this.vaultAccess.tryAdapterStat(path);
} }
async removeHidden(path: string): Promise<boolean> { async removeHidden(path: string): Promise<boolean> {
try { try {
await this.vaultAccess.adapterRemove(path); await this.vaultAccess.adapterRemove(path);
if (this.vaultAccess.tryAdapterStat(path) !== null) {
return false;
}
return true; return true;
} catch (e) { } catch (e) {
this._log(`Could not remove hidden file: ${path}`, LOG_LEVEL_VERBOSE); this._log(`Could not remove hidden file: ${path}`, LOG_LEVEL_VERBOSE);
@@ -132,7 +161,7 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
return await this.vaultAccess.adapterReadBinary(path); return await this.vaultAccess.adapterReadBinary(path);
} }
async isExistsIncludeHidden(path: string): Promise<boolean> { async isExistsIncludeHidden(path: string): Promise<boolean> {
return await this.vaultAccess.adapterStat(path) !== null; return (await this.vaultAccess.tryAdapterStat(path)) !== null;
} }
async ensureDir(path: string): Promise<boolean> { async ensureDir(path: string): Promise<boolean> {
try { try {
@@ -145,8 +174,9 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
} }
} }
triggerFileEvent(event: string, path: string): void { triggerFileEvent(event: string, path: string): void {
// this.app.vault.trigger("file-change", path); const file = this.vaultAccess.getAbstractFileByPath(path);
this.vaultAccess.trigger(event, this.vaultAccess.getAbstractFileByPath(normalizePath(path))); if (file === null) return;
this.vaultAccess.trigger(event, file);
} }
async triggerHiddenFile(path: string): Promise<void> { async triggerHiddenFile(path: string): Promise<void> {
//@ts-ignore internal function //@ts-ignore internal function
@@ -172,8 +202,8 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
const data = await this.vaultAccess.vaultReadAuto(file); const data = await this.vaultAccess.vaultReadAuto(file);
return { return {
...stub, ...stub,
body: createBlob(data) body: createBlob(data),
} };
} }
getStub(path: string): UXFileInfoStub | UXFolderInfo | null { getStub(path: string): UXFileInfoStub | UXFolderInfo | null {
const file = this.vaultAccess.getAbstractFileByPath(path); const file = this.vaultAccess.getAbstractFileByPath(path);
@@ -185,16 +215,16 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
return null; return null;
} }
getFiles(): UXFileInfoStub[] { getFiles(): UXFileInfoStub[] {
return this.vaultAccess.getFiles().map(f => TFileToUXFileInfoStub(f)); return this.vaultAccess.getFiles().map((f) => TFileToUXFileInfoStub(f));
} }
getFileNames(): FilePath[] { getFileNames(): FilePath[] {
return this.vaultAccess.getFiles().map(f => f.path as FilePath); return this.vaultAccess.getFiles().map((f) => f.path as FilePath);
} }
async getFilesIncludeHidden( async getFilesIncludeHidden(
basePath: string, basePath: string,
includeFilter?: RegExp[], includeFilter?: CustomRegExp[],
excludeFilter?: RegExp[], excludeFilter?: CustomRegExp[],
skipFolder: string[] = [".git", ".trash", "node_modules"] skipFolder: string[] = [".git", ".trash", "node_modules"]
): Promise<FilePath[]> { ): Promise<FilePath[]> {
let w: ListedFiles; let w: ListedFiles;
@@ -205,20 +235,15 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
this._log(ex, LOG_LEVEL_VERBOSE); this._log(ex, LOG_LEVEL_VERBOSE);
return []; return [];
} }
skipFolder = skipFolder.map(e => e.toLowerCase()); skipFolder = skipFolder.map((e) => e.toLowerCase());
let files = [] as string[]; let files = [] as string[];
for (const file of w.files) { for (const file of w.files) {
if (excludeFilter && excludeFilter.some(ee => file.match(ee))) { if (includeFilter && includeFilter.length > 0) {
// If excludeFilter and includeFilter are both set, the file will be included in the list. if (!includeFilter.some((e) => e.test(file))) continue;
if (includeFilter) {
if (!includeFilter.some(e => file.match(e))) continue;
} else {
continue;
}
} }
if (includeFilter) { if (excludeFilter && excludeFilter.some((ee) => ee.test(file))) {
if (!includeFilter.some(e => file.match(e))) continue; continue;
} }
if (await this.plugin.$$isIgnoredByIgnoreFiles(file)) continue; if (await this.plugin.$$isIgnoredByIgnoreFiles(file)) continue;
files.push(file); files.push(file);
@@ -226,31 +251,27 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
for (const v of w.folders) { for (const v of w.folders) {
const folderName = (v.split("/").pop() ?? "").toLowerCase(); const folderName = (v.split("/").pop() ?? "").toLowerCase();
if (skipFolder.some(e => folderName === e)) { if (skipFolder.some((e) => folderName === e)) {
continue continue;
}
if (excludeFilter && excludeFilter.some(e => v.match(e))) {
if (includeFilter) {
if (!includeFilter.some(e => v.match(e))) {
continue;
}
}
} }
if (includeFilter) { if (excludeFilter && excludeFilter.some((e) => e.test(v))) {
if (!includeFilter.some(e => v.match(e))) continue; continue;
}
if (await this.plugin.$$isIgnoredByIgnoreFiles(v)) {
continue;
} }
// OK, deep dive! // OK, deep dive!
files = files.concat(await this.getFilesIncludeHidden(v, includeFilter, excludeFilter, skipFolder)); files = files.concat(await this.getFilesIncludeHidden(v, includeFilter, excludeFilter, skipFolder));
} }
return files as FilePath[]; return files as FilePath[];
} }
touched(file: UXFileInfoStub | FilePathWithPrefix): void { async touched(file: UXFileInfoStub | FilePathWithPrefix): Promise<void> {
const path = typeof file === "string" ? file : file.path; const path = typeof file === "string" ? file : file.path;
this.vaultAccess.touch(path as FilePath); await this.vaultAccess.touch(path as FilePath);
} }
recentlyTouched(file: UXFileInfoStub | FilePathWithPrefix): boolean { recentlyTouched(file: UXFileInfoStub | FilePathWithPrefix): boolean {
const xFile = typeof file === "string" ? this.vaultAccess.getAbstractFileByPath(file) as TFile : file; const xFile = typeof file === "string" ? (this.vaultAccess.getAbstractFileByPath(file) as TFile) : file;
if (xFile === null) return false; if (xFile === null) return false;
if (xFile instanceof TFolder) return false; if (xFile instanceof TFolder) return false;
return this.vaultAccess.recentlyTouched(xFile); return this.vaultAccess.recentlyTouched(xFile);
@@ -295,7 +316,7 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
async _deleteVaultItem(file: TFile | TFolder) { async _deleteVaultItem(file: TFile | TFolder) {
if (file instanceof TFile) { if (file instanceof TFile) {
if (!await this.core.$$isTargetFile(file.path)) return; if (!(await this.core.$$isTargetFile(file.path))) return;
} }
const dir = file.parent; const dir = file.parent;
if (this.settings.trashInsteadDelete) { if (this.settings.trashInsteadDelete) {
@@ -308,7 +329,9 @@ export class ModuleFileAccessObsidian extends AbstractObsidianModule implements
this._log(`files: ${dir.children.length}`); this._log(`files: ${dir.children.length}`);
if (dir.children.length == 0) { if (dir.children.length == 0) {
if (!this.settings.doNotDeleteFolder) { if (!this.settings.doNotDeleteFolder) {
this._log(`All files under the parent directory (${dir.path}) have been deleted, so delete this one.`); this._log(
`All files under the parent directory (${dir.path}) have been deleted, so delete this one.`
);
await this._deleteVaultItem(dir); await this._deleteVaultItem(dir);
} }
} }

View File

@@ -1,17 +1,26 @@
import { AbstractObsidianModule, type IObsidianModule } from '../AbstractObsidianModule.ts'; // ModuleInputUIObsidian.ts
import { scheduleTask } from 'octagonal-wheels/concurrency/task'; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { disposeMemoObject, memoIfNotExist, memoObject, retrieveMemoObject } from '../../common/utils.ts'; import { scheduleTask } from "octagonal-wheels/concurrency/task";
import { askSelectString, askString, askYesNo, confirmWithMessage, confirmWithMessageWithWideButton } from './UILib/dialogs.ts'; import { disposeMemoObject, memoIfNotExist, memoObject, retrieveMemoObject } from "../../common/utils.ts";
import { Notice } from '../../deps.ts'; import {
import type { Confirm } from '../interfaces/Confirm.ts'; askSelectString,
askString,
askYesNo,
confirmWithMessage,
confirmWithMessageWithWideButton,
} from "./UILib/dialogs.ts";
import { Notice } from "../../deps.ts";
import type { Confirm } from "../../lib/src/interfaces/Confirm.ts";
import { setConfirmInstance } from "../../lib/src/PlatformAPIs/obsidian/Confirm.ts";
import { $msg } from "src/lib/src/common/i18n.ts";
// This module cannot be a common module because it depends on Obsidian's API. // This module cannot be a common module because it depends on Obsidian's API.
// However, we have to make compatible one for other platform. // However, we have to make compatible one for other platform.
export class ModuleInputUIObsidian extends AbstractObsidianModule implements IObsidianModule, Confirm { export class ModuleInputUIObsidian extends AbstractObsidianModule implements IObsidianModule, Confirm {
$everyOnload(): Promise<boolean> { $everyOnload(): Promise<boolean> {
this.core.confirm = this; this.core.confirm = this;
setConfirmInstance(this);
return Promise.resolve(true); return Promise.resolve(true);
} }
@@ -22,17 +31,43 @@ export class ModuleInputUIObsidian extends AbstractObsidianModule implements IOb
return askString(this.app, title, key, placeholder, isPassword); return askString(this.app, title, key, placeholder, isPassword);
} }
async askYesNoDialog(message: string, opt: { title?: string, defaultOption?: "Yes" | "No", timeout?: number } = { title: "Confirmation" }): Promise<"yes" | "no"> { async askYesNoDialog(
const ret = await confirmWithMessageWithWideButton(this.plugin, opt.title || "Confirmation", message, ["Yes", "No"], opt.defaultOption ?? "No", opt.timeout); message: string,
return ret == "Yes" ? "yes" : "no"; opt: { title?: string; defaultOption?: "Yes" | "No"; timeout?: number } = { title: "Confirmation" }
): Promise<"yes" | "no"> {
const defaultTitle = $msg("moduleInputUIObsidian.defaultTitleConfirmation");
const yesLabel = $msg("moduleInputUIObsidian.optionYes");
const noLabel = $msg("moduleInputUIObsidian.optionNo");
const defaultOption = opt.defaultOption === "Yes" ? yesLabel : noLabel;
const ret = await confirmWithMessageWithWideButton(
this.plugin,
opt.title || defaultTitle,
message,
[yesLabel, noLabel],
defaultOption,
opt.timeout
);
return ret === yesLabel ? "yes" : "no";
} }
askSelectString(message: string, items: string[]): Promise<string> { askSelectString(message: string, items: string[]): Promise<string> {
return askSelectString(this.app, message, items); return askSelectString(this.app, message, items);
} }
askSelectStringDialogue(message: string, buttons: string[], opt: { title?: string, defaultAction: (typeof buttons)[number], timeout?: number }): Promise<(typeof buttons)[number] | false> { askSelectStringDialogue<T extends readonly string[]>(
return confirmWithMessageWithWideButton(this.plugin, opt.title || "Select", message, buttons, opt.defaultAction, opt.timeout); message: string,
buttons: T,
opt: { title?: string; defaultAction: T[number]; timeout?: number }
): Promise<T[number] | false> {
const defaultTitle = $msg("moduleInputUIObsidian.defaultTitleSelect");
return confirmWithMessageWithWideButton(
this.plugin,
opt.title || defaultTitle,
message,
buttons,
opt.defaultAction,
opt.timeout
);
} }
askInPopup(key: string, dialogText: string, anchorCallback: (anchor: HTMLAnchorElement) => void) { askInPopup(key: string, dialogText: string, anchorCallback: (anchor: HTMLAnchorElement) => void) {
@@ -40,9 +75,11 @@ export class ModuleInputUIObsidian extends AbstractObsidianModule implements IOb
const [beforeText, afterText] = dialogText.split("{HERE}", 2); const [beforeText, afterText] = dialogText.split("{HERE}", 2);
doc.createEl("span", undefined, (a) => { doc.createEl("span", undefined, (a) => {
a.appendText(beforeText); a.appendText(beforeText);
a.appendChild(a.createEl("a", undefined, (anchor) => { a.appendChild(
anchorCallback(anchor); a.createEl("a", undefined, (anchor) => {
})); anchorCallback(anchor);
})
);
a.appendText(afterText); a.appendText(afterText);
}); });
}); });
@@ -55,8 +92,7 @@ export class ModuleInputUIObsidian extends AbstractObsidianModule implements IOb
} }
scheduleTask(popupKey + "-close", 20000, () => { scheduleTask(popupKey + "-close", 20000, () => {
const popup = retrieveMemoObject<Notice>(popupKey); const popup = retrieveMemoObject<Notice>(popupKey);
if (!popup) if (!popup) return;
return;
if (popup?.noticeEl?.isShown()) { if (popup?.noticeEl?.isShown()) {
popup.hide(); popup.hide();
} }
@@ -64,8 +100,14 @@ export class ModuleInputUIObsidian extends AbstractObsidianModule implements IOb
}); });
}); });
} }
confirmWithMessage(title: string, contentMd: string, buttons: string[], defaultAction: (typeof buttons)[number], timeout?: number): Promise<(typeof buttons)[number] | false> {
confirmWithMessage(
title: string,
contentMd: string,
buttons: string[],
defaultAction: (typeof buttons)[number],
timeout?: number
): Promise<(typeof buttons)[number] | false> {
return confirmWithMessage(this.plugin, title, contentMd, buttons, defaultAction, timeout); return confirmWithMessage(this.plugin, title, contentMd, buttons, defaultAction, timeout);
} }
} }

View File

@@ -1,25 +1,20 @@
import { ButtonComponent } from "obsidian"; import { ButtonComponent } from "obsidian";
import { App, FuzzySuggestModal, MarkdownRenderer, Modal, Plugin, Setting } from "../../../deps.ts"; import { App, FuzzySuggestModal, MarkdownRenderer, Modal, Plugin, Setting } from "../../../deps.ts";
import { EVENT_PLUGIN_UNLOADED, eventHub } from "../../../common/events.ts"; import { EVENT_PLUGIN_UNLOADED, eventHub } from "../../../common/events.ts";
import { delay } from "octagonal-wheels/promises";
class AutoClosableModal extends Modal { class AutoClosableModal extends Modal {
removeEvent: (() => void) | undefined; _closeByUnload() {
eventHub.off(EVENT_PLUGIN_UNLOADED, this._closeByUnload);
this.close();
}
constructor(app: App) { constructor(app: App) {
super(app); super(app);
this.removeEvent = eventHub.on(EVENT_PLUGIN_UNLOADED, async () => { this._closeByUnload = this._closeByUnload.bind(this);
await delay(100); eventHub.once(EVENT_PLUGIN_UNLOADED, this._closeByUnload);
if (!this.removeEvent) return;
this.close();
this.removeEvent = undefined;
});
} }
onClose() { onClose() {
if (this.removeEvent) { eventHub.off(EVENT_PLUGIN_UNLOADED, this._closeByUnload);
this.removeEvent();
this.removeEvent = undefined
}
} }
} }
@@ -32,7 +27,14 @@ export class InputStringDialog extends AutoClosableModal {
isManuallyClosed = false; isManuallyClosed = false;
isPassword = false; isPassword = false;
constructor(app: App, title: string, key: string, placeholder: string, isPassword: boolean, onSubmit: (result: string | false) => void) { constructor(
app: App,
title: string,
key: string,
placeholder: string,
isPassword: boolean,
onSubmit: (result: string | false) => void
) {
super(app); super(app);
this.onSubmit = onSubmit; this.onSubmit = onSubmit;
this.title = title; this.title = title;
@@ -45,27 +47,32 @@ export class InputStringDialog extends AutoClosableModal {
const { contentEl } = this; const { contentEl } = this;
this.titleEl.setText(this.title); this.titleEl.setText(this.title);
const formEl = contentEl.createDiv(); const formEl = contentEl.createDiv();
new Setting(formEl).setName(this.key).setClass(this.isPassword ? "password-input" : "normal-input").addText((text) => new Setting(formEl)
text.onChange((value) => { .setName(this.key)
this.result = value; .setClass(this.isPassword ? "password-input" : "normal-input")
}) .addText((text) =>
); text.onChange((value) => {
new Setting(formEl).addButton((btn) => this.result = value;
btn
.setButtonText("Ok")
.setCta()
.onClick(() => {
this.isManuallyClosed = true;
this.close();
}) })
).addButton((btn) => );
btn new Setting(formEl)
.setButtonText("Cancel") .addButton((btn) =>
.setCta() btn
.onClick(() => { .setButtonText("Ok")
this.close(); .setCta()
}) .onClick(() => {
); this.isManuallyClosed = true;
this.close();
})
)
.addButton((btn) =>
btn
.setButtonText("Cancel")
.setCta()
.onClick(() => {
this.close();
})
);
} }
onClose() { onClose() {
@@ -81,13 +88,18 @@ export class InputStringDialog extends AutoClosableModal {
} }
export class PopoverSelectString extends FuzzySuggestModal<string> { export class PopoverSelectString extends FuzzySuggestModal<string> {
app: App; app: App;
callback: ((e: string) => void) | undefined = () => { }; callback: ((e: string) => void) | undefined = () => {};
getItemsFun: () => string[] = () => { getItemsFun: () => string[] = () => {
return ["yes", "no"]; return ["yes", "no"];
};
} constructor(
app: App,
constructor(app: App, note: string, placeholder: string | undefined, getItemsFun: (() => string[]) | undefined, callback: (e: string) => void) { note: string,
placeholder: string | undefined,
getItemsFun: (() => string[]) | undefined,
callback: (e: string) => void
) {
super(app); super(app);
this.app = app; this.app = app;
this.setPlaceholder((placeholder ?? "y/n) ") + note); this.setPlaceholder((placeholder ?? "y/n) ") + note);
@@ -118,12 +130,11 @@ export class PopoverSelectString extends FuzzySuggestModal<string> {
} }
} }
export class MessageBox extends AutoClosableModal { export class MessageBox<T extends readonly string[]> extends AutoClosableModal {
plugin: Plugin; plugin: Plugin;
title: string; title: string;
contentMd: string; contentMd: string;
buttons: string[]; buttons: T;
result: string | false = false; result: string | false = false;
isManuallyClosed = false; isManuallyClosed = false;
defaultAction: string | undefined; defaultAction: string | undefined;
@@ -134,7 +145,16 @@ export class MessageBox extends AutoClosableModal {
onSubmit: (result: string | false) => void; onSubmit: (result: string | false) => void;
constructor(plugin: Plugin, title: string, contentMd: string, buttons: string[], defaultAction: (typeof buttons)[number], timeout: number | undefined, wideButton: boolean, onSubmit: (result: (typeof buttons)[number] | false) => void) { constructor(
plugin: Plugin,
title: string,
contentMd: string,
buttons: T,
defaultAction: T[number],
timeout: number | undefined,
wideButton: boolean,
onSubmit: (result: T[number] | false) => void
) {
super(plugin.app); super(plugin.app);
this.plugin = plugin; this.plugin = plugin;
this.title = title; this.title = title;
@@ -169,6 +189,7 @@ export class MessageBox extends AutoClosableModal {
this.titleEl.setText(this.title); this.titleEl.setText(this.title);
const div = contentEl.createDiv(); const div = contentEl.createDiv();
div.style.userSelect = "text"; div.style.userSelect = "text";
div.style["webkitUserSelect"] = "text";
void MarkdownRenderer.render(this.plugin.app, this.contentMd, div, "/", this.plugin); void MarkdownRenderer.render(this.plugin.app, this.contentMd, div, "/", this.plugin);
const buttonSetting = new Setting(contentEl); const buttonSetting = new Setting(contentEl);
const labelWrapper = contentEl.createDiv(); const labelWrapper = contentEl.createDiv();
@@ -196,20 +217,18 @@ export class MessageBox extends AutoClosableModal {
this.timer = undefined; this.timer = undefined;
this.defaultButtonComponent?.setButtonText(`${this.defaultAction}`); this.defaultButtonComponent?.setButtonText(`${this.defaultAction}`);
} }
}) });
for (const button of this.buttons) { for (const button of this.buttons) {
buttonSetting.addButton((btn) => { buttonSetting.addButton((btn) => {
btn btn.setButtonText(button).onClick(() => {
.setButtonText(button) this.isManuallyClosed = true;
.onClick(() => { this.result = button;
this.isManuallyClosed = true; if (this.timer) {
this.result = button; clearInterval(this.timer);
if (this.timer) { this.timer = undefined;
clearInterval(this.timer); }
this.timer = undefined; this.close();
} });
this.close();
})
if (button == this.defaultAction) { if (button == this.defaultAction) {
this.defaultButtonComponent = btn; this.defaultButtonComponent = btn;
btn.setCta(); btn.setCta();
@@ -219,8 +238,7 @@ export class MessageBox extends AutoClosableModal {
btn.buttonEl.style.width = "100%"; btn.buttonEl.style.width = "100%";
} }
return btn; return btn;
} });
)
} }
} }
@@ -240,25 +258,42 @@ export class MessageBox extends AutoClosableModal {
} }
} }
export function confirmWithMessage<T extends readonly string[]>(
plugin: Plugin,
title: string,
export function confirmWithMessage(plugin: Plugin, title: string, contentMd: string, buttons: string[], defaultAction: (typeof buttons)[number], timeout?: number): Promise<(typeof buttons)[number] | false> { contentMd: string,
buttons: T,
defaultAction: T[number],
timeout?: number
): Promise<T[number] | false> {
return new Promise((res) => { return new Promise((res) => {
const dialog = new MessageBox(plugin, title, contentMd, buttons, defaultAction, timeout, false, (result) => res(result)); const dialog = new MessageBox(plugin, title, contentMd, buttons, defaultAction, timeout, false, (result) =>
res(result)
);
dialog.open(); dialog.open();
}); });
} }
export function confirmWithMessageWithWideButton(plugin: Plugin, title: string, contentMd: string, buttons: string[], defaultAction: (typeof buttons)[number], timeout?: number): Promise<(typeof buttons)[number] | false> { export function confirmWithMessageWithWideButton<T extends readonly string[]>(
plugin: Plugin,
title: string,
contentMd: string,
buttons: T,
defaultAction: T[number],
timeout?: number
): Promise<T[number] | false> {
return new Promise((res) => { return new Promise((res) => {
const dialog = new MessageBox(plugin, title, contentMd, buttons, defaultAction, timeout, true, (result) => res(result)); const dialog = new MessageBox(plugin, title, contentMd, buttons, defaultAction, timeout, true, (result) =>
res(result)
);
dialog.open(); dialog.open();
}); });
} }
export const askYesNo = (app: App, message: string): Promise<"yes" | "no"> => { export const askYesNo = (app: App, message: string): Promise<"yes" | "no"> => {
return new Promise((res) => { return new Promise((res) => {
const popover = new PopoverSelectString(app, message, undefined, undefined, (result) => res(result as "yes" | "no")); const popover = new PopoverSelectString(app, message, undefined, undefined, (result) =>
res(result as "yes" | "no")
);
popover.open(); popover.open();
}); });
}; };
@@ -271,11 +306,15 @@ export const askSelectString = (app: App, message: string, items: string[]): Pro
}); });
}; };
export const askString = (
export const askString = (app: App, title: string, key: string, placeholder: string, isPassword: boolean = false): Promise<string | false> => { app: App,
title: string,
key: string,
placeholder: string,
isPassword: boolean = false
): Promise<string | false> => {
return new Promise((res) => { return new Promise((res) => {
const dialog = new InputStringDialog(app, title, key, placeholder, isPassword, (result) => res(result)); const dialog = new InputStringDialog(app, title, key, placeholder, isPassword, (result) => res(result));
dialog.open(); dialog.open();
}); });
}; };

View File

@@ -9,7 +9,7 @@ import { markChangesAreSame } from "../../../common/utils.ts";
import { type UXFileInfo } from "../../../lib/src/common/types.ts"; import { type UXFileInfo } from "../../../lib/src/common/types.ts";
function getFileLockKey(file: TFile | TFolder | string | UXFileInfo) { function getFileLockKey(file: TFile | TFolder | string | UXFileInfo) {
return `fl:${typeof (file) == "string" ? file : file.path}`; return `fl:${typeof file == "string" ? file : file.path}`;
} }
function toArrayBuffer(arr: Uint8Array | ArrayBuffer | DataView): ArrayBufferLike { function toArrayBuffer(arr: Uint8Array | ArrayBuffer | DataView): ArrayBufferLike {
if (arr instanceof Uint8Array) { if (arr instanceof Uint8Array) {
@@ -35,13 +35,20 @@ async function processWriteFile<T>(file: TFile | TFolder | string | UXFileInfo,
} }
export class SerializedFileAccess { export class SerializedFileAccess {
app: App app: App;
plugin: HasSettings<{ handleFilenameCaseSensitive: boolean }> plugin: HasSettings<{ handleFilenameCaseSensitive: boolean }>;
constructor(app: App, plugin: typeof this["plugin"]) { constructor(app: App, plugin: (typeof this)["plugin"]) {
this.app = app; this.app = app;
this.plugin = plugin; this.plugin = plugin;
} }
async tryAdapterStat(file: TFile | string) {
const path = file instanceof TFile ? file.path : file;
return await processReadFile(file, async () => {
if (!(await this.app.vault.adapter.exists(path))) return null;
return this.app.vault.adapter.stat(path);
});
}
async adapterStat(file: TFile | string) { async adapterStat(file: TFile | string) {
const path = file instanceof TFile ? file.path : file; const path = file instanceof TFile ? file.path : file;
return await processReadFile(file, () => this.app.vault.adapter.stat(path)); return await processReadFile(file, () => this.app.vault.adapter.stat(path));
@@ -72,10 +79,12 @@ export class SerializedFileAccess {
async adapterWrite(file: TFile | string, data: string | ArrayBuffer | Uint8Array, options?: DataWriteOptions) { async adapterWrite(file: TFile | string, data: string | ArrayBuffer | Uint8Array, options?: DataWriteOptions) {
const path = file instanceof TFile ? file.path : file; const path = file instanceof TFile ? file.path : file;
if (typeof (data) === "string") { if (typeof data === "string") {
return await processWriteFile(file, () => this.app.vault.adapter.write(path, data, options)); return await processWriteFile(file, () => this.app.vault.adapter.write(path, data, options));
} else { } else {
return await processWriteFile(file, () => this.app.vault.adapter.writeBinary(path, toArrayBuffer(data), options)); return await processWriteFile(file, () =>
this.app.vault.adapter.writeBinary(path, toArrayBuffer(data), options)
);
} }
} }
@@ -97,19 +106,17 @@ export class SerializedFileAccess {
return await processReadFile(file, () => this.app.vault.readBinary(file)); return await processReadFile(file, () => this.app.vault.readBinary(file));
} }
async vaultModify(file: TFile, data: string | ArrayBuffer | Uint8Array, options?: DataWriteOptions) { async vaultModify(file: TFile, data: string | ArrayBuffer | Uint8Array, options?: DataWriteOptions) {
if (typeof (data) === "string") { if (typeof data === "string") {
return await processWriteFile(file, async () => { return await processWriteFile(file, async () => {
const oldData = await this.app.vault.read(file); const oldData = await this.app.vault.read(file);
if (data === oldData) { if (data === oldData) {
if (options && options.mtime) markChangesAreSame(file.path, file.stat.mtime, options.mtime); if (options && options.mtime) markChangesAreSame(file.path, file.stat.mtime, options.mtime);
return true; return true;
} }
await this.app.vault.modify(file, data, options) await this.app.vault.modify(file, data, options);
return true; return true;
} });
);
} else { } else {
return await processWriteFile(file, async () => { return await processWriteFile(file, async () => {
const oldData = await this.app.vault.readBinary(file); const oldData = await this.app.vault.readBinary(file);
@@ -117,13 +124,17 @@ export class SerializedFileAccess {
if (options && options.mtime) markChangesAreSame(file.path, file.stat.mtime, options.mtime); if (options && options.mtime) markChangesAreSame(file.path, file.stat.mtime, options.mtime);
return true; return true;
} }
await this.app.vault.modifyBinary(file, toArrayBuffer(data), options) await this.app.vault.modifyBinary(file, toArrayBuffer(data), options);
return true; return true;
}); });
} }
} }
async vaultCreate(path: string, data: string | ArrayBuffer | Uint8Array, options?: DataWriteOptions): Promise<TFile> { async vaultCreate(
if (typeof (data) === "string") { path: string,
data: string | ArrayBuffer | Uint8Array,
options?: DataWriteOptions
): Promise<TFile> {
if (typeof data === "string") {
return await processWriteFile(path, () => this.app.vault.create(path, data, options)); return await processWriteFile(path, () => this.app.vault.create(path, data, options));
} else { } else {
return await processWriteFile(path, () => this.app.vault.createBinary(path, toArrayBuffer(data), options)); return await processWriteFile(path, () => this.app.vault.createBinary(path, toArrayBuffer(data), options));
@@ -135,7 +146,7 @@ export class SerializedFileAccess {
} }
async adapterAppend(normalizedPath: string, data: string, options?: DataWriteOptions) { async adapterAppend(normalizedPath: string, data: string, options?: DataWriteOptions) {
return await this.app.vault.adapter.append(normalizedPath, data, options) return await this.app.vault.adapter.append(normalizedPath, data, options);
} }
async delete(file: TFile | TFolder, force = false) { async delete(file: TFile | TFolder, force = false) {
@@ -145,8 +156,6 @@ export class SerializedFileAccess {
return await processWriteFile(file, () => this.app.vault.trash(file, force)); return await processWriteFile(file, () => this.app.vault.trash(file, force));
} }
isStorageInsensitive(): boolean { isStorageInsensitive(): boolean {
//@ts-ignore //@ts-ignore
return this.app.vault.adapter.insensitive ?? true; return this.app.vault.adapter.insensitive ?? true;
@@ -188,18 +197,25 @@ export class SerializedFileAccess {
} }
} }
touchedFiles: string[] = []; touchedFiles: string[] = [];
_statInternal(file: FilePath) {
return this.app.vault.adapter.stat(file);
}
touch(file: TFile | FilePath) { async touch(file: TFile | FilePath) {
const f = file instanceof TFile ? file : this.getAbstractFileByPath(file) as TFile; const path = file instanceof TFile ? (file.path as FilePath) : file;
const key = `${f.path}-${f.stat.mtime}-${f.stat.size}`; const statOrg = file instanceof TFile ? file.stat : await this._statInternal(path);
const stat = statOrg || { mtime: 0, size: 0 };
const key = `${path}-${stat.mtime}-${stat.size}`;
this.touchedFiles.unshift(key); this.touchedFiles.unshift(key);
this.touchedFiles = this.touchedFiles.slice(0, 100); this.touchedFiles = this.touchedFiles.slice(0, 100);
} }
recentlyTouched(file: TFile | InternalFileInfo | UXFileInfoStub) { recentlyTouched(file: TFile | InternalFileInfo | UXFileInfoStub) {
const key = "stat" in file ? `${file.path}-${file.stat.mtime}-${file.stat.size}` : `${file.path}-${file.mtime}-${file.size}`; const key =
"stat" in file
? `${file.path}-${file.stat.mtime}-${file.stat.size}`
: `${file.path}-${file.mtime}-${file.size}`;
if (this.touchedFiles.indexOf(key) == -1) return false; if (this.touchedFiles.indexOf(key) == -1) return false;
return true; return true;
} }

View File

@@ -1,18 +1,32 @@
import { TAbstractFile, TFile, TFolder } from "../../../deps.ts"; import { TAbstractFile, TFile, TFolder } from "../../../deps.ts";
import { Logger } from "../../../lib/src/common/logger.ts"; import { Logger } from "../../../lib/src/common/logger.ts";
import { shouldBeIgnored } from "../../../lib/src/string_and_binary/path.ts"; import { shouldBeIgnored } from "../../../lib/src/string_and_binary/path.ts";
import { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE, type FilePath, type FilePathWithPrefix, type UXFileInfoStub, type UXInternalFileInfoStub } from "../../../lib/src/common/types.ts"; import {
import { delay, fireAndForget } from "../../../lib/src/common/utils.ts"; DEFAULT_SETTINGS,
LOG_LEVEL_DEBUG,
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
type FilePath,
type FilePathWithPrefix,
type UXFileInfoStub,
type UXInternalFileInfoStub,
} from "../../../lib/src/common/types.ts";
import { delay, fireAndForget, getFileRegExp } from "../../../lib/src/common/utils.ts";
import { type FileEventItem, type FileEventType } from "../../../common/types.ts"; import { type FileEventItem, type FileEventType } from "../../../common/types.ts";
import { serialized, skipIfDuplicated } from "../../../lib/src/concurrency/lock.ts"; import { serialized, skipIfDuplicated } from "../../../lib/src/concurrency/lock.ts";
import { finishAllWaitingForTimeout, finishWaitingForTimeout, isWaitingForTimeout, waitForTimeout } from "../../../lib/src/concurrency/task.ts"; import {
finishAllWaitingForTimeout,
finishWaitingForTimeout,
isWaitingForTimeout,
waitForTimeout,
} from "../../../lib/src/concurrency/task.ts";
import { Semaphore } from "../../../lib/src/concurrency/semaphore.ts"; import { Semaphore } from "../../../lib/src/concurrency/semaphore.ts";
import type { LiveSyncCore } from "../../../main.ts"; import type { LiveSyncCore } from "../../../main.ts";
import { InternalFileToUXFileInfoStub, TFileToUXFileInfoStub } from "./utilObsidian.ts"; import { InternalFileToUXFileInfoStub, TFileToUXFileInfoStub } from "./utilObsidian.ts";
import ObsidianLiveSyncPlugin from "../../../main.ts"; import ObsidianLiveSyncPlugin from "../../../main.ts";
// import { InternalFileToUXFileInfo } from "../platforms/obsidian.ts"; // import { InternalFileToUXFileInfo } from "../platforms/obsidian.ts";
export type FileEvent = { export type FileEvent = {
type: FileEventType; type: FileEventType;
file: UXFileInfoStub | UXInternalFileInfoStub; file: UXFileInfoStub | UXInternalFileInfoStub;
@@ -21,30 +35,26 @@ export type FileEvent = {
skipBatchWait?: boolean; skipBatchWait?: boolean;
}; };
export abstract class StorageEventManager { export abstract class StorageEventManager {
abstract beginWatch(): void; abstract beginWatch(): void;
abstract flushQueue(): void; abstract flushQueue(): void;
abstract appendQueue(items: FileEvent[], ctx?: any): Promise<void>; abstract appendQueue(items: FileEvent[], ctx?: any): Promise<void>;
abstract cancelQueue(key: string): void; abstract cancelQueue(key: string): void;
abstract isWaiting(filename: FilePath): boolean; abstract isWaiting(filename: FilePath): boolean;
} }
export class StorageEventManagerObsidian extends StorageEventManager { export class StorageEventManagerObsidian extends StorageEventManager {
plugin: ObsidianLiveSyncPlugin; plugin: ObsidianLiveSyncPlugin;
core: LiveSyncCore; core: LiveSyncCore;
get shouldBatchSave() { get shouldBatchSave() {
return this.plugin.shouldBatchSave; return this.core.settings?.batchSave && this.core.settings?.liveSync != true;
} }
get batchSaveMinimumDelay(): number { get batchSaveMinimumDelay(): number {
return this.plugin.batchSaveMinimumDelay; return this.core.settings?.batchSaveMinimumDelay ?? DEFAULT_SETTINGS.batchSaveMinimumDelay;
} }
get batchSaveMaximumDelay(): number { get batchSaveMaximumDelay(): number {
return this.plugin.batchSaveMaximumDelay return this.core.settings?.batchSaveMaximumDelay ?? DEFAULT_SETTINGS.batchSaveMaximumDelay;
} }
constructor(plugin: ObsidianLiveSyncPlugin, core: LiveSyncCore) { constructor(plugin: ObsidianLiveSyncPlugin, core: LiveSyncCore) {
super(); super();
@@ -83,10 +93,11 @@ export class StorageEventManagerObsidian extends StorageEventManager {
} }
const data = info?.data as string; const data = info?.data as string;
const fi: FileEvent = { const fi: FileEvent = {
type: "CHANGED", file: TFileToUXFileInfoStub(file), cachedData: data, type: "CHANGED",
} file: TFileToUXFileInfoStub(file),
void this.appendQueue([ cachedData: data,
fi]) };
void this.appendQueue([fi]);
} }
watchVaultCreate(file: TAbstractFile, ctx?: any) { watchVaultCreate(file: TAbstractFile, ctx?: any) {
@@ -109,17 +120,27 @@ export class StorageEventManagerObsidian extends StorageEventManager {
watchVaultRename(file: TAbstractFile, oldFile: string, ctx?: any) { watchVaultRename(file: TAbstractFile, oldFile: string, ctx?: any) {
if (file instanceof TFile) { if (file instanceof TFile) {
const fileInfo = TFileToUXFileInfoStub(file); const fileInfo = TFileToUXFileInfoStub(file);
void this.appendQueue([ void this.appendQueue(
{ [
type: "DELETE", file: { {
path: oldFile as FilePath, name: file.name, stat: { type: "DELETE",
mtime: file.stat.mtime, file: {
ctime: file.stat.ctime, path: oldFile as FilePath,
size: file.stat.size, name: file.name,
type: "file" stat: {
}, deleted: true mtime: file.stat.mtime,
}, skipBatchWait: true ctime: file.stat.ctime,
}, { type: "CREATE", file: fileInfo, skipBatchWait: true },], ctx); size: file.stat.size,
type: "file",
},
deleted: true,
},
skipBatchWait: true,
},
{ type: "CREATE", file: fileInfo, skipBatchWait: true },
],
ctx
);
} }
} }
// Watch raw events (Internal API) // Watch raw events (Internal API)
@@ -140,70 +161,83 @@ export class StorageEventManagerObsidian extends StorageEventManager {
if (!this.plugin.settings.syncInternalFiles && !this.plugin.settings.usePluginSync) return; if (!this.plugin.settings.syncInternalFiles && !this.plugin.settings.usePluginSync) return;
if (!this.plugin.settings.watchInternalFileChanges) return; if (!this.plugin.settings.watchInternalFileChanges) return;
if (!path.startsWith(this.plugin.app.vault.configDir)) return; if (!path.startsWith(this.plugin.app.vault.configDir)) return;
const ignorePatterns = this.plugin.settings.syncInternalFilesIgnorePatterns const ignorePatterns = getFileRegExp(this.plugin.settings, "syncInternalFilesIgnorePatterns");
.replace(/\n| /g, "") const targetPatterns = getFileRegExp(this.plugin.settings, "syncInternalFilesTargetPatterns");
.split(",").filter(e => e).map(e => new RegExp(e, "i")); if (ignorePatterns.some((e) => e.test(path))) return;
if (ignorePatterns.some(e => path.match(e))) return; if (!targetPatterns.some((e) => e.test(path))) return;
if (path.endsWith("/")) { if (path.endsWith("/")) {
// Folder // Folder
return; return;
} }
void this.appendQueue([
{ void this.appendQueue(
type: "INTERNAL", file: InternalFileToUXFileInfoStub(path), [
}], null); {
type: "INTERNAL",
file: InternalFileToUXFileInfoStub(path),
skipBatchWait: true, // Internal files should be processed immediately.
},
],
null
);
} }
// Cache file and waiting to can be proceed. // Cache file and waiting to can be proceed.
async appendQueue(params: FileEvent[], ctx?: any) { async appendQueue(params: FileEvent[], ctx?: any) {
if (!this.plugin.settings.isConfigured) return; if (!this.core.settings.isConfigured) return;
if (this.plugin.settings.suspendFileWatching) return; if (this.core.settings.suspendFileWatching) return;
this.plugin.totalFileEventCount++; this.core.$$markFileListPossiblyChanged();
// Flag up to be reload // Flag up to be reload
const processFiles = new Set<FilePath>(); const processFiles = new Set<FilePath>();
for (const param of params) { for (const param of params) {
if (shouldBeIgnored(param.file.path)) { if (shouldBeIgnored(param.file.path)) {
continue; continue;
} }
const atomicKey = [ const atomicKey = [0, 0, 0, 0, 0, 0].map((e) => `${Math.floor(Math.random() * 100000)}`).join("-");
0,
0,
0,
0,
0,
0].map(e => `${Math.floor(Math.random() * 100000)}`).join("-");
const type = param.type; const type = param.type;
const file = param.file; const file = param.file;
const oldPath = param.oldPath; const oldPath = param.oldPath;
if (type !== "INTERNAL") { if (type !== "INTERNAL") {
const size = (file as UXFileInfoStub).stat.size; const size = (file as UXFileInfoStub).stat.size;
if (this.plugin.$$isFileSizeExceeded(size) && (type == "CREATE" || type == "CHANGED")) { if (this.core.$$isFileSizeExceeded(size) && (type == "CREATE" || type == "CHANGED")) {
Logger(`The storage file has been changed but exceeds the maximum size. Skipping: ${param.file.path}`, LOG_LEVEL_NOTICE); Logger(
`The storage file has been changed but exceeds the maximum size. Skipping: ${param.file.path}`,
LOG_LEVEL_NOTICE
);
continue; continue;
} }
} }
if (file instanceof TFolder) continue; if (file instanceof TFolder) continue;
if (!await this.plugin.$$isTargetFile(file.path)) continue; if (!(await this.core.$$isTargetFile(file.path))) continue;
// Stop cache using to prevent the corruption; // Stop cache using to prevent the corruption;
// let cache: null | string | ArrayBuffer; // let cache: null | string | ArrayBuffer;
// new file or something changed, cache the changes. // new file or something changed, cache the changes.
if (file instanceof TFile && (type == "CREATE" || type == "CHANGED")) { // if (file instanceof TFile && (type == "CREATE" || type == "CHANGED")) {
// Wait for a bit while to let the writer has marked `touched` at the file. if (file instanceof TFile || !file.isFolder) {
await delay(10); if (type == "CREATE" || type == "CHANGED") {
if (this.plugin.storageAccess.recentlyTouched(file)) { // Wait for a bit while to let the writer has marked `touched` at the file.
continue; await delay(10);
if (this.core.storageAccess.recentlyTouched(file.path)) {
continue;
}
} }
} }
let cache: string | undefined = undefined; let cache: string | undefined = undefined;
if (param.cachedData) { if (param.cachedData) {
cache = param.cachedData cache = param.cachedData;
} }
this.enqueue({ this.enqueue({
type, args: { type,
file: file, oldPath, cache, ctx, args: {
}, skipBatchWait: param.skipBatchWait, key: atomicKey file: file,
}) oldPath,
cache,
ctx,
},
skipBatchWait: param.skipBatchWait,
key: atomicKey,
});
processFiles.add(file.path as FilePath); processFiles.add(file.path as FilePath);
if (oldPath) { if (oldPath) {
processFiles.add(oldPath as FilePath); processFiles.add(oldPath as FilePath);
@@ -238,7 +272,7 @@ export class StorageEventManagerObsidian extends StorageEventManager {
Logger(`Processing ${filename}: Started`, LOG_LEVEL_DEBUG); Logger(`Processing ${filename}: Started`, LOG_LEVEL_DEBUG);
let noMoreFiles = false; let noMoreFiles = false;
do { do {
const target = this.bufferedQueuedItems.find(e => e.args.file.path == filename); const target = this.bufferedQueuedItems.find((e) => e.args.file.path == filename);
if (target === undefined) { if (target === undefined) {
noMoreFiles = true; noMoreFiles = true;
break; break;
@@ -251,7 +285,7 @@ export class StorageEventManagerObsidian extends StorageEventManager {
// } // }
const type = target.type; const type = target.type;
if (target.cancelled) { if (target.cancelled) {
Logger(`Processing ${filename}: Cancelled (scheduled): ${operationType}`, LOG_LEVEL_DEBUG) Logger(`Processing ${filename}: Cancelled (scheduled): ${operationType}`, LOG_LEVEL_DEBUG);
this.cancelStandingBy(target); this.cancelStandingBy(target);
continue; continue;
} }
@@ -261,19 +295,31 @@ export class StorageEventManagerObsidian extends StorageEventManager {
let canWait = true; let canWait = true;
const now = Date.now(); const now = Date.now();
if (waitedSince !== undefined) { if (waitedSince !== undefined) {
if (waitedSince + (this.batchSaveMaximumDelay * 1000) < now) { if (waitedSince + this.batchSaveMaximumDelay * 1000 < now) {
Logger(`Processing ${filename}: Could not wait no more: ${operationType}`, LOG_LEVEL_INFO) Logger(
`Processing ${filename}: Could not wait no more: ${operationType}`,
LOG_LEVEL_INFO
);
canWait = false; canWait = false;
} }
} }
if (canWait) { if (canWait) {
if (waitedSince === undefined) this.waitedSince.set(filename, now) if (waitedSince === undefined) this.waitedSince.set(filename, now);
target.batched = true target.batched = true;
Logger(`Processing ${filename}: Waiting for batch save delay: ${operationType}`, LOG_LEVEL_DEBUG) Logger(
`Processing ${filename}: Waiting for batch save delay: ${operationType}`,
LOG_LEVEL_DEBUG
);
this.updateStatus(); this.updateStatus();
const result = await waitForTimeout(`storage-event-manager-batchsave-${filename}`, this.batchSaveMinimumDelay * 1000); const result = await waitForTimeout(
`storage-event-manager-batchsave-${filename}`,
this.batchSaveMinimumDelay * 1000
);
if (!result) { if (!result) {
Logger(`Processing ${filename}: Cancelled by new queue: ${operationType}`, LOG_LEVEL_DEBUG) Logger(
`Processing ${filename}: Cancelled by new queue: ${operationType}`,
LOG_LEVEL_DEBUG
);
// If could not wait for the timeout, possibly we got a new queue. therefore, currently processing one should be cancelled // If could not wait for the timeout, possibly we got a new queue. therefore, currently processing one should be cancelled
this.cancelStandingBy(target); this.cancelStandingBy(target);
continue; continue;
@@ -281,16 +327,19 @@ export class StorageEventManagerObsidian extends StorageEventManager {
} }
} }
} else { } else {
Logger(`Processing ${filename}:Requested to perform immediately ${filename}: ${operationType}`, LOG_LEVEL_DEBUG) Logger(
`Processing ${filename}:Requested to perform immediately ${filename}: ${operationType}`,
LOG_LEVEL_DEBUG
);
} }
Logger(`Processing ${filename}: Request main to process: ${operationType}`, LOG_LEVEL_DEBUG) Logger(`Processing ${filename}: Request main to process: ${operationType}`, LOG_LEVEL_DEBUG);
await this.requestProcessQueue(target); await this.requestProcessQueue(target);
} while (!noMoreFiles) } while (!noMoreFiles);
} finally { } finally {
release() release();
} }
Logger(`Processing ${filename}: Finished`, LOG_LEVEL_DEBUG); Logger(`Processing ${filename}: Finished`, LOG_LEVEL_DEBUG);
}) });
} }
cancelStandingBy(fei: FileEventItem) { cancelStandingBy(fei: FileEventItem) {
@@ -302,30 +351,30 @@ export class StorageEventManagerObsidian extends StorageEventManager {
try { try {
this.processingCount++; this.processingCount++;
this.bufferedQueuedItems.remove(fei); this.bufferedQueuedItems.remove(fei);
this.updateStatus() this.updateStatus();
this.waitedSince.delete(fei.args.file.path); this.waitedSince.delete(fei.args.file.path);
await this.handleFileEvent(fei); await this.handleFileEvent(fei);
} finally { } finally {
this.processingCount--; this.processingCount--;
this.updateStatus() this.updateStatus();
} }
} }
isWaiting(filename: FilePath) { isWaiting(filename: FilePath) {
return isWaitingForTimeout(`storage-event-manager-batchsave-${filename}`); return isWaitingForTimeout(`storage-event-manager-batchsave-${filename}`);
} }
flushQueue() { flushQueue() {
this.bufferedQueuedItems.forEach(e => e.skipBatchWait = true) this.bufferedQueuedItems.forEach((e) => (e.skipBatchWait = true));
finishAllWaitingForTimeout("storage-event-manager-batchsave-", true); finishAllWaitingForTimeout("storage-event-manager-batchsave-", true);
} }
cancelQueue(key: string) { cancelQueue(key: string) {
this.bufferedQueuedItems.forEach(e => { this.bufferedQueuedItems.forEach((e) => {
if (e.key === key) e.skipBatchWait = true if (e.key === key) e.skipBatchWait = true;
}) });
} }
updateStatus() { updateStatus() {
const allItems = this.bufferedQueuedItems.filter(e => !e.cancelled) const allItems = this.bufferedQueuedItems.filter((e) => !e.cancelled);
const batchedCount = allItems.filter(e => e.batched && !e.skipBatchWait).length; const batchedCount = allItems.filter((e) => e.batched && !e.skipBatchWait).length;
this.core.batched.value = batchedCount this.core.batched.value = batchedCount;
this.core.processing.value = this.processingCount; this.core.processing.value = this.processingCount;
this.core.totalQueued.value = allItems.length - batchedCount; this.core.totalQueued.value = allItems.length - batchedCount;
} }
@@ -334,15 +383,13 @@ export class StorageEventManagerObsidian extends StorageEventManager {
const file = queue.args.file; const file = queue.args.file;
const lockKey = `handleFile:${file.path}`; const lockKey = `handleFile:${file.path}`;
return await serialized(lockKey, async () => { return await serialized(lockKey, async () => {
// TODO CHECK
const key = `file-last-proc-${queue.type}-${file.path}`;
const last = Number(await this.core.kvDB.get(key) || 0);
if (queue.type == "INTERNAL" || file.isInternal) { if (queue.type == "INTERNAL" || file.isInternal) {
await this.plugin.$anyProcessOptionalFileEvent(file.path as unknown as FilePath); await this.core.$anyProcessOptionalFileEvent(file.path as unknown as FilePath);
} else { } else {
// let mtime = file.stat.mtime; const key = `file-last-proc-${queue.type}-${file.path}`;
const last = Number((await this.core.kvDB.get(key)) || 0);
if (queue.type == "DELETE") { if (queue.type == "DELETE") {
await this.plugin.$anyHandlerProcessesFileEvent(queue); await this.core.$anyHandlerProcessesFileEvent(queue);
} else { } else {
if (file.stat.mtime == last) { if (file.stat.mtime == last) {
Logger(`File has been already scanned on ${queue.type}, skip: ${file.path}`, LOG_LEVEL_VERBOSE); Logger(`File has been already scanned on ${queue.type}, skip: ${file.path}`, LOG_LEVEL_VERBOSE);
@@ -350,8 +397,11 @@ export class StorageEventManagerObsidian extends StorageEventManager {
// this.cancelRelativeEvent(queue); // this.cancelRelativeEvent(queue);
return; return;
} }
if (!await this.plugin.$anyHandlerProcessesFileEvent(queue)) { if (!(await this.core.$anyHandlerProcessesFileEvent(queue))) {
Logger(`STORAGE -> DB: Handler failed, cancel the relative operations: ${file.path}`, LOG_LEVEL_INFO); Logger(
`STORAGE -> DB: Handler failed, cancel the relative operations: ${file.path}`,
LOG_LEVEL_INFO
);
// cancel running queues and remove one of atomic operation (e.g. rename) // cancel running queues and remove one of atomic operation (e.g. rename)
this.cancelRelativeEvent(queue); this.cancelRelativeEvent(queue);
return; return;

View File

@@ -6,10 +6,22 @@ import type { SerializedFileAccess } from "./SerializedFileAccess.ts";
import { addPrefix, isPlainText } from "../../../lib/src/string_and_binary/path.ts"; import { addPrefix, isPlainText } from "../../../lib/src/string_and_binary/path.ts";
import { LOG_LEVEL_VERBOSE, Logger } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_VERBOSE, Logger } from "octagonal-wheels/common/logger";
import { createBlob } from "../../../lib/src/common/utils.ts"; import { createBlob } from "../../../lib/src/common/utils.ts";
import type { FilePath, FilePathWithPrefix, UXFileInfo, UXFileInfoStub, UXFolderInfo, UXInternalFileInfoStub } from "../../../lib/src/common/types.ts"; import type {
FilePath,
FilePathWithPrefix,
UXFileInfo,
UXFileInfoStub,
UXFolderInfo,
UXInternalFileInfoStub,
} from "../../../lib/src/common/types.ts";
import type { LiveSyncCore } from "../../../main.ts"; import type { LiveSyncCore } from "../../../main.ts";
export async function TFileToUXFileInfo(core: LiveSyncCore, file: TFile, prefix?: string, deleted?: boolean): Promise<UXFileInfo> { export async function TFileToUXFileInfo(
core: LiveSyncCore,
file: TFile,
prefix?: string,
deleted?: boolean
): Promise<UXFileInfo> {
const isPlain = isPlainText(file.name); const isPlain = isPlainText(file.name);
const possiblyLarge = !isPlain; const possiblyLarge = !isPlain;
let content: Blob; let content: Blob;
@@ -34,13 +46,16 @@ export async function TFileToUXFileInfo(core: LiveSyncCore, file: TFile, prefix?
type: "file", type: "file",
}, },
body: content, body: content,
} };
} }
export async function InternalFileToUXFileInfo(fullPath: string, vaultAccess: SerializedFileAccess, prefix: string = ICHeader): Promise<UXFileInfo> { export async function InternalFileToUXFileInfo(
fullPath: string,
vaultAccess: SerializedFileAccess,
prefix: string = ICHeader
): Promise<UXFileInfo> {
const name = fullPath.split("/").pop() as string; const name = fullPath.split("/").pop() as string;
const stat = await vaultAccess.adapterStat(fullPath); const stat = await vaultAccess.tryAdapterStat(fullPath);
if (stat == null) throw new Error(`File not found: ${fullPath}`); if (stat == null) throw new Error(`File not found: ${fullPath}`);
if (stat.type == "folder") throw new Error(`File not found: ${fullPath}`); if (stat.type == "folder") throw new Error(`File not found: ${fullPath}`);
const file = await vaultAccess.adapterReadAuto(fullPath); const file = await vaultAccess.adapterReadAuto(fullPath);
@@ -64,7 +79,7 @@ export async function InternalFileToUXFileInfo(fullPath: string, vaultAccess: Se
type: "file", type: "file",
}, },
body: content, body: content,
} };
} }
export function TFileToUXFileInfoStub(file: TFile | TAbstractFile, deleted?: boolean): UXFileInfoStub { export function TFileToUXFileInfoStub(file: TFile | TAbstractFile, deleted?: boolean): UXFileInfoStub {
@@ -81,8 +96,8 @@ export function TFileToUXFileInfoStub(file: TFile | TAbstractFile, deleted?: boo
ctime: file.stat.ctime, ctime: file.stat.ctime,
type: "file", type: "file",
}, },
deleted: deleted deleted: deleted,
} };
return ret; return ret;
} }
export function InternalFileToUXFileInfoStub(filename: FilePathWithPrefix, deleted?: boolean): UXInternalFileInfoStub { export function InternalFileToUXFileInfoStub(filename: FilePathWithPrefix, deleted?: boolean): UXInternalFileInfoStub {
@@ -93,8 +108,8 @@ export function InternalFileToUXFileInfoStub(filename: FilePathWithPrefix, delet
isFolder: false, isFolder: false,
stat: undefined, stat: undefined,
isInternal: true, isInternal: true,
deleted deleted,
} };
return ret; return ret;
} }
export function TFolderToUXFileInfoStub(file: TFolder): UXFolderInfo { export function TFolderToUXFileInfoStub(file: TFolder): UXFolderInfo {
@@ -103,7 +118,7 @@ export function TFolderToUXFileInfoStub(file: TFolder): UXFolderInfo {
path: file.path as FilePathWithPrefix, path: file.path as FilePathWithPrefix,
parent: file.parent?.path as FilePath | undefined, parent: file.parent?.path as FilePath | undefined,
isFolder: true, isFolder: true,
children: file.children.map(e => TFileToUXFileInfoStub(e)), children: file.children.map((e) => TFileToUXFileInfoStub(e)),
} };
return ret; return ret;
} }

View File

@@ -1,24 +1,36 @@
import { unique } from "octagonal-wheels/collection"; import { unique } from "octagonal-wheels/collection";
import { QueueProcessor } from "octagonal-wheels/concurrency/processor";
import { throttle } from "octagonal-wheels/function"; import { throttle } from "octagonal-wheels/function";
import { eventHub } from "../../common/events.ts"; import { eventHub } from "../../common/events.ts";
import { BASE_IS_NEW, compareFileFreshness, EVEN, getPath, isValidPath, TARGET_IS_NEW } from "../../common/utils.ts"; import { BASE_IS_NEW, compareFileFreshness, EVEN, getPath, isValidPath, TARGET_IS_NEW } from "../../common/utils.ts";
import { type FilePathWithPrefixLC, type FilePathWithPrefix, type MetaEntry, isMetaEntry, type EntryDoc, LOG_LEVEL_VERBOSE, LOG_LEVEL_NOTICE, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, type UXFileInfoStub } from "../../lib/src/common/types.ts"; import {
type FilePathWithPrefixLC,
type FilePathWithPrefix,
type MetaEntry,
isMetaEntry,
type EntryDoc,
LOG_LEVEL_VERBOSE,
LOG_LEVEL_NOTICE,
LOG_LEVEL_INFO,
LOG_LEVEL_DEBUG,
type UXFileInfoStub,
} from "../../lib/src/common/types.ts";
import { isAnyNote } from "../../lib/src/common/utils.ts"; import { isAnyNote } from "../../lib/src/common/utils.ts";
import { stripAllPrefixes } from "../../lib/src/string_and_binary/path.ts"; import { stripAllPrefixes } from "../../lib/src/string_and_binary/path.ts";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
import { withConcurrency } from "octagonal-wheels/iterable/map";
export class ModuleInitializerFile extends AbstractModule implements ICoreModule { export class ModuleInitializerFile extends AbstractModule implements ICoreModule {
async $$performFullScan(showingNotice?: boolean): Promise<void> { async $$performFullScan(showingNotice?: boolean): Promise<void> {
this._log("Opening the key-value database", LOG_LEVEL_VERBOSE); this._log("Opening the key-value database", LOG_LEVEL_VERBOSE);
const isInitialized = await (this.core.kvDB.get<boolean>("initialized")) || false; const isInitialized = (await this.core.kvDB.get<boolean>("initialized")) || false;
// synchronize all files between database and storage. // synchronize all files between database and storage.
if (!this.settings.isConfigured) { if (!this.settings.isConfigured) {
if (showingNotice) { if (showingNotice) {
this._log("LiveSync is not configured yet. Synchronising between the storage and the local database is now prevented.", LOG_LEVEL_NOTICE, "syncAll"); this._log(
"LiveSync is not configured yet. Synchronising between the storage and the local database is now prevented.",
LOG_LEVEL_NOTICE,
"syncAll"
);
} }
return; return;
} }
@@ -47,33 +59,41 @@ export class ModuleInitializerFile extends AbstractModule implements ICoreModule
return path as FilePathWithPrefixLC; return path as FilePathWithPrefixLC;
} }
return (path as string).toLowerCase() as FilePathWithPrefixLC; return (path as string).toLowerCase() as FilePathWithPrefixLC;
} };
// If handleFilenameCaseSensitive is enabled, `FilePathWithPrefixLC` is the same as `FilePathWithPrefix`. // If handleFilenameCaseSensitive is enabled, `FilePathWithPrefixLC` is the same as `FilePathWithPrefix`.
const storageFileNameMap = Object.fromEntries(_filesStorage.map((e) => [ const storageFileNameMap = Object.fromEntries(
e.path, e] as [FilePathWithPrefix, UXFileInfoStub])); _filesStorage.map((e) => [e.path, e] as [FilePathWithPrefix, UXFileInfoStub])
);
const storageFileNames = Object.keys(storageFileNameMap) as FilePathWithPrefix[]; const storageFileNames = Object.keys(storageFileNameMap) as FilePathWithPrefix[];
const storageFileNameCapsPair = storageFileNames.map((e) => [ const storageFileNameCapsPair = storageFileNames.map(
e, convertCase(e)] as [FilePathWithPrefix, FilePathWithPrefixLC]); (e) => [e, convertCase(e)] as [FilePathWithPrefix, FilePathWithPrefixLC]
);
// const storageFileNameCS2CI = Object.fromEntries(storageFileNameCapsPair) as Record<FilePathWithPrefix, FilePathWithPrefixLC>; // const storageFileNameCS2CI = Object.fromEntries(storageFileNameCapsPair) as Record<FilePathWithPrefix, FilePathWithPrefixLC>;
const storageFileNameCI2CS = Object.fromEntries(storageFileNameCapsPair.map(e => [ const storageFileNameCI2CS = Object.fromEntries(storageFileNameCapsPair.map((e) => [e[1], e[0]])) as Record<
e[1], e[0]])) as Record<FilePathWithPrefixLC, FilePathWithPrefix>; FilePathWithPrefixLC,
FilePathWithPrefix
>;
this._log("Collecting local files on the DB", LOG_LEVEL_VERBOSE); this._log("Collecting local files on the DB", LOG_LEVEL_VERBOSE);
const _DBEntries = [] as MetaEntry[]; const _DBEntries = [] as MetaEntry[];
// const _DBEntriesTask = [] as (() => Promise<MetaEntry | false>)[];
let count = 0; let count = 0;
for await (const doc of this.localDatabase.findAllNormalDocs()) { // Fetch all documents from the database (including conflicts to prevent overwriting).
for await (const doc of this.localDatabase.findAllNormalDocs({ conflicts: true })) {
count++; count++;
if (count % 25 == 0) this._log(`Collecting local files on the DB: ${count}`, showingNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO, "syncAll"); if (count % 25 == 0)
this._log(
`Collecting local files on the DB: ${count}`,
showingNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO,
"syncAll"
);
const path = getPath(doc); const path = getPath(doc);
if (isValidPath(path) && await this.core.$$isTargetFile(path, true)) { if (isValidPath(path) && (await this.core.$$isTargetFile(path, true))) {
if (!isMetaEntry(doc)) { if (!isMetaEntry(doc)) {
this._log(`Invalid entry: ${path}`, LOG_LEVEL_INFO); this._log(`Invalid entry: ${path}`, LOG_LEVEL_INFO);
continue; continue;
@@ -82,24 +102,28 @@ export class ModuleInitializerFile extends AbstractModule implements ICoreModule
} }
} }
const databaseFileNameMap = Object.fromEntries(_DBEntries.map((e) => [ const databaseFileNameMap = Object.fromEntries(
getPath(e), e] as [FilePathWithPrefix, MetaEntry])); _DBEntries.map((e) => [getPath(e), e] as [FilePathWithPrefix, MetaEntry])
);
const databaseFileNames = Object.keys(databaseFileNameMap) as FilePathWithPrefix[]; const databaseFileNames = Object.keys(databaseFileNameMap) as FilePathWithPrefix[];
const databaseFileNameCapsPair = databaseFileNames.map((e) => [ const databaseFileNameCapsPair = databaseFileNames.map(
e, convertCase(e)] as [FilePathWithPrefix, FilePathWithPrefixLC]); (e) => [e, convertCase(e)] as [FilePathWithPrefix, FilePathWithPrefixLC]
);
// const databaseFileNameCS2CI = Object.fromEntries(databaseFileNameCapsPair) as Record<FilePathWithPrefix, FilePathWithPrefixLC>; // const databaseFileNameCS2CI = Object.fromEntries(databaseFileNameCapsPair) as Record<FilePathWithPrefix, FilePathWithPrefixLC>;
const databaseFileNameCI2CS = Object.fromEntries(databaseFileNameCapsPair.map(e => [ const databaseFileNameCI2CS = Object.fromEntries(databaseFileNameCapsPair.map((e) => [e[1], e[0]])) as Record<
e[1], e[0]])) as Record<FilePathWithPrefix, FilePathWithPrefixLC>; FilePathWithPrefix,
FilePathWithPrefixLC
>;
const allFiles = unique([ const allFiles = unique([
...Object.keys(databaseFileNameCI2CS), ...Object.keys(databaseFileNameCI2CS),
...Object.keys(storageFileNameCI2CS)]) as FilePathWithPrefixLC[]; ...Object.keys(storageFileNameCI2CS),
]) as FilePathWithPrefixLC[];
this._log(`Total files in the database: ${databaseFileNames.length}`, LOG_LEVEL_VERBOSE, "syncAll"); this._log(`Total files in the database: ${databaseFileNames.length}`, LOG_LEVEL_VERBOSE, "syncAll");
this._log(`Total files in the storage: ${storageFileNames.length}`, LOG_LEVEL_VERBOSE, "syncAll"); this._log(`Total files in the storage: ${storageFileNames.length}`, LOG_LEVEL_VERBOSE, "syncAll");
this._log(`Total files: ${allFiles.length}`, LOG_LEVEL_VERBOSE, "syncAll"); this._log(`Total files: ${allFiles.length}`, LOG_LEVEL_VERBOSE, "syncAll");
const filesExistOnlyInStorage = allFiles.filter((e) => !databaseFileNameCI2CS[e]); const filesExistOnlyInStorage = allFiles.filter((e) => !databaseFileNameCI2CS[e]);
const filesExistOnlyInDatabase = allFiles.filter((e) => !storageFileNameCI2CS[e]); const filesExistOnlyInDatabase = allFiles.filter((e) => !storageFileNameCI2CS[e]);
const filesExistBoth = allFiles.filter((e) => databaseFileNameCI2CS[e] && storageFileNameCI2CS[e]); const filesExistBoth = allFiles.filter((e) => databaseFileNameCI2CS[e] && storageFileNameCI2CS[e]);
@@ -127,93 +151,112 @@ export class ModuleInitializerFile extends AbstractModule implements ICoreModule
if (!this.localDatabase.isReady) throw Error("Database is not ready!"); if (!this.localDatabase.isReady) throw Error("Database is not ready!");
let success = 0; let success = 0;
let failed = 0; let failed = 0;
const step = 10; let total = 0;
const processor = new QueueProcessor(async (e) => { for await (const result of withConcurrency(
try { objects,
await callback(e[0]); async (e) => {
try {
await callback(e);
return true;
} catch (ex) {
this._log(`Error while ${procedureName}`, LOG_LEVEL_NOTICE);
this._log(ex, LOG_LEVEL_VERBOSE);
return false;
}
},
10
)) {
if (result) {
success++; success++;
// return } else {
} catch (ex) {
this._log(`Error while ${procedureName}`, LOG_LEVEL_NOTICE);
this._log(ex, LOG_LEVEL_VERBOSE);
failed++; failed++;
} }
if ((success + failed) % step == 0) { total++;
const msg = `${procedureName}: DONE:${success}, FAILED:${failed}, LAST:${processor._queue.length}`; const msg = `${procedureName}: DONE:${success}, FAILED:${failed}, LAST:${objects.length - total}`;
updateLog(procedureName, msg); updateLog(procedureName, msg);
} }
return;
}, {
batchSize: 1,
concurrentLimit: 10,
delay: 0,
suspended: true,
maintainDelay: false,
interval: 0
}, objects)
await processor.waitForAllDoneAndTerminate();
const msg = `${procedureName} All done: DONE:${success}, FAILED:${failed}`; const msg = `${procedureName} All done: DONE:${success}, FAILED:${failed}`;
updateLog(procedureName, msg) updateLog(procedureName, msg);
} };
initProcess.push(runAll("UPDATE DATABASE", filesExistOnlyInStorage, async (e) => { initProcess.push(
// console.warn("UPDATE DATABASE", e); runAll("UPDATE DATABASE", filesExistOnlyInStorage, async (e) => {
const file = storageFileNameMap[storageFileNameCI2CS[e]]; // Exists in storage but not in database.
if (!this.core.$$isFileSizeExceeded(file.stat.size)) { const file = storageFileNameMap[storageFileNameCI2CS[e]];
const path = file.path; if (!this.core.$$isFileSizeExceeded(file.stat.size)) {
await this.core.fileHandler.storeFileToDB(file); const path = file.path;
// fireAndForget(() => this.checkAndApplySettingFromMarkdown(path, true)); await this.core.fileHandler.storeFileToDB(file);
eventHub.emitEvent("event-file-changed", { file: path, automated: true }); // fireAndForget(() => this.checkAndApplySettingFromMarkdown(path, true));
} else { eventHub.emitEvent("event-file-changed", { file: path, automated: true });
this._log(`UPDATE DATABASE: ${e} has been skipped due to file size exceeding the limit`, logLevel);
}
}));
initProcess.push(runAll("UPDATE STORAGE", filesExistOnlyInDatabase, async (e) => {
const w = databaseFileNameMap[databaseFileNameCI2CS[e]];
const path = getPath(w) ?? e;
if (w && !(w.deleted || w._deleted)) {
if (!this.core.$$isFileSizeExceeded(w.size)) {
// await this.pullFile(path, undefined, false, undefined, false);
// Memo: No need to force
await this.core.fileHandler.dbToStorage(path, null, true);
// fireAndForget(() => this.checkAndApplySettingFromMarkdown(e, true));
eventHub.emitEvent("event-file-changed", {
file: e, automated: true
});
this._log(`Check or pull from db:${path} OK`);
} else { } else {
this._log(`UPDATE STORAGE: ${path} has been skipped due to file size exceeding the limit`, logLevel); this._log(`UPDATE DATABASE: ${e} has been skipped due to file size exceeding the limit`, logLevel);
} }
} else if (w) { })
this._log(`Deletion history skipped: ${path}`, LOG_LEVEL_VERBOSE); );
} else { initProcess.push(
this._log(`entry not found: ${path}`); runAll("UPDATE STORAGE", filesExistOnlyInDatabase, async (e) => {
} const w = databaseFileNameMap[databaseFileNameCI2CS[e]];
})); // Exists in database but not in storage.
const path = getPath(w) ?? e;
if (w && !(w.deleted || w._deleted)) {
if (!this.core.$$isFileSizeExceeded(w.size)) {
// Prevent applying the conflicted state to the storage.
if (w._conflicts?.length ?? 0 > 0) {
this._log(`UPDATE STORAGE: ${path} has conflicts. skipped (x)`, LOG_LEVEL_INFO);
return;
}
// await this.pullFile(path, undefined, false, undefined, false);
// Memo: No need to force
await this.core.fileHandler.dbToStorage(path, null, true);
// fireAndForget(() => this.checkAndApplySettingFromMarkdown(e, true));
eventHub.emitEvent("event-file-changed", {
file: e,
automated: true,
});
this._log(`Check or pull from db:${path} OK`);
} else {
this._log(
`UPDATE STORAGE: ${path} has been skipped due to file size exceeding the limit`,
logLevel
);
}
} else if (w) {
this._log(`Deletion history skipped: ${path}`, LOG_LEVEL_VERBOSE);
} else {
this._log(`entry not found: ${path}`);
}
})
);
const fileMap = filesExistBoth.map(path => { const fileMap = filesExistBoth.map((path) => {
const file = storageFileNameMap[storageFileNameCI2CS[path]]; const file = storageFileNameMap[storageFileNameCI2CS[path]];
const doc = databaseFileNameMap[databaseFileNameCI2CS[path]]; const doc = databaseFileNameMap[databaseFileNameCI2CS[path]];
return { file, doc } return { file, doc };
}) });
initProcess.push(runAll("SYNC DATABASE AND STORAGE", fileMap, async (e) => { initProcess.push(
const { file, doc } = e; runAll("SYNC DATABASE AND STORAGE", fileMap, async (e) => {
if (!this.core.$$isFileSizeExceeded(file.stat.size) && !this.core.$$isFileSizeExceeded(doc.size)) { const { file, doc } = e;
await this.syncFileBetweenDBandStorage(file, doc); // Prevent applying the conflicted state to the storage.
// fireAndForget(() => this.checkAndApplySettingFromMarkdown(getPath(doc), true)); if (doc._conflicts?.length ?? 0 > 0) {
eventHub.emitEvent("event-file-changed", { this._log(`SYNC DATABASE AND STORAGE: ${file.path} has conflicts. skipped`, LOG_LEVEL_INFO);
file: getPath(doc), automated: true return;
}); }
} else { if (!this.core.$$isFileSizeExceeded(file.stat.size) && !this.core.$$isFileSizeExceeded(doc.size)) {
this._log(`SYNC DATABASE AND STORAGE: ${getPath(doc)} has been skipped due to file size exceeding the limit`, logLevel); await this.syncFileBetweenDBandStorage(file, doc);
} } else {
})) this._log(
`SYNC DATABASE AND STORAGE: ${getPath(doc)} has been skipped due to file size exceeding the limit`,
logLevel
);
}
})
);
await Promise.all(initProcess); await Promise.all(initProcess);
// this.setStatusBarText(`NOW TRACKING!`); // this.setStatusBarText(`NOW TRACKING!`);
this._log("Initialized, NOW TRACKING!"); this._log("Initialized, NOW TRACKING!");
if (!isInitialized) { if (!isInitialized) {
await (this.core.kvDB.set("initialized", true)) await this.core.kvDB.set("initialized", true);
} }
if (showingNotice) { if (showingNotice) {
this._log("Initialize done!", LOG_LEVEL_NOTICE, "syncAll"); this._log("Initialize done!", LOG_LEVEL_NOTICE, "syncAll");
@@ -222,14 +265,14 @@ export class ModuleInitializerFile extends AbstractModule implements ICoreModule
async syncFileBetweenDBandStorage(file: UXFileInfoStub, doc: MetaEntry) { async syncFileBetweenDBandStorage(file: UXFileInfoStub, doc: MetaEntry) {
if (!doc) { if (!doc) {
throw new Error(`Missing doc:${(file as any).path}`) throw new Error(`Missing doc:${(file as any).path}`);
} }
if ("path" in file) { if ("path" in file) {
const w = this.core.storageAccess.getFileStub((file as any).path); const w = this.core.storageAccess.getFileStub((file as any).path);
if (w) { if (w) {
file = w; file = w;
} else { } else {
throw new Error(`Missing file:${(file as any).path}`) throw new Error(`Missing file:${(file as any).path}`);
} }
} }
@@ -239,22 +282,30 @@ export class ModuleInitializerFile extends AbstractModule implements ICoreModule
if (!this.core.$$isFileSizeExceeded(file.stat.size)) { if (!this.core.$$isFileSizeExceeded(file.stat.size)) {
this._log("STORAGE -> DB :" + file.path); this._log("STORAGE -> DB :" + file.path);
await this.core.fileHandler.storeFileToDB(file); await this.core.fileHandler.storeFileToDB(file);
eventHub.emitEvent("event-file-changed", {
file: file.path, automated: true
});
} else { } else {
this._log(`STORAGE -> DB : ${file.path} has been skipped due to file size exceeding the limit`, LOG_LEVEL_NOTICE); this._log(
`STORAGE -> DB : ${file.path} has been skipped due to file size exceeding the limit`,
LOG_LEVEL_NOTICE
);
} }
break; break;
case TARGET_IS_NEW: case TARGET_IS_NEW:
if (!this.core.$$isFileSizeExceeded(doc.size)) { if (!this.core.$$isFileSizeExceeded(doc.size)) {
this._log("STORAGE <- DB :" + file.path); this._log("STORAGE <- DB :" + file.path);
if (!await this.core.fileHandler.dbToStorage(doc, stripAllPrefixes(file.path), true)) { if (await this.core.fileHandler.dbToStorage(doc, stripAllPrefixes(file.path), true)) {
eventHub.emitEvent("event-file-changed", {
file: file.path,
automated: true,
});
} else {
this._log(`STORAGE <- DB : Cloud not read ${file.path}, possibly deleted`, LOG_LEVEL_NOTICE); this._log(`STORAGE <- DB : Cloud not read ${file.path}, possibly deleted`, LOG_LEVEL_NOTICE);
} }
return caches; return caches;
} else { } else {
this._log(`STORAGE <- DB : ${file.path} has been skipped due to file size exceeding the limit`, LOG_LEVEL_NOTICE); this._log(
`STORAGE <- DB : ${file.path} has been skipped due to file size exceeding the limit`,
LOG_LEVEL_NOTICE
);
} }
break; break;
case EVEN: case EVEN:
@@ -263,31 +314,29 @@ export class ModuleInitializerFile extends AbstractModule implements ICoreModule
default: default:
this._log("STORAGE ?? DB :" + file.path + " Something got weird"); this._log("STORAGE ?? DB :" + file.path + " Something got weird");
} }
} }
// This method uses an old version of database accessor, which is not recommended. // This method uses an old version of database accessor, which is not recommended.
// TODO: Fix // TODO: Fix
async collectDeletedFiles() { async collectDeletedFiles() {
const limitDays = this.settings.automaticallyDeleteMetadataOfDeletedFiles; const limitDays = this.settings.automaticallyDeleteMetadataOfDeletedFiles;
if (limitDays <= 0) return; if (limitDays <= 0) return;
this._log(`Checking expired file history`); this._log(`Checking expired file history`);
const limit = Date.now() - (86400 * 1000 * limitDays); const limit = Date.now() - 86400 * 1000 * limitDays;
const notes: { const notes: {
path: string, path: string;
mtime: number, mtime: number;
ttl: number, ttl: number;
doc: PouchDB.Core.ExistingDocument<EntryDoc & PouchDB.Core.AllDocsMeta> doc: PouchDB.Core.ExistingDocument<EntryDoc & PouchDB.Core.AllDocsMeta>;
}[] = []; }[] = [];
for await (const doc of this.localDatabase.findAllDocs({ conflicts: true })) { for await (const doc of this.localDatabase.findAllDocs({ conflicts: true })) {
if (isAnyNote(doc)) { if (isAnyNote(doc)) {
if (doc.deleted && (doc.mtime - limit) < 0) { if (doc.deleted && doc.mtime - limit < 0) {
notes.push({ notes.push({
path: getPath(doc), path: getPath(doc),
mtime: doc.mtime, mtime: doc.mtime,
ttl: (doc.mtime - limit) / 1000 / 86400, ttl: (doc.mtime - limit) / 1000 / 86400,
doc: doc doc: doc,
}); });
} }
} }
@@ -307,21 +356,21 @@ export class ModuleInitializerFile extends AbstractModule implements ICoreModule
} }
async $$initializeDatabase(showingNotice: boolean = false, reopenDatabase = true): Promise<boolean> { async $$initializeDatabase(showingNotice: boolean = false, reopenDatabase = true): Promise<boolean> {
this.core.isReady = false; this.core.$$resetIsReady();
if ((!reopenDatabase) || await this.core.$$openDatabase()) { if (!reopenDatabase || (await this.core.$$openDatabase())) {
if (this.localDatabase.isReady) { if (this.localDatabase.isReady) {
await this.core.$$performFullScan(showingNotice); await this.core.$$performFullScan(showingNotice);
} }
if (!await this.core.$everyOnDatabaseInitialized(showingNotice)) { if (!(await this.core.$everyOnDatabaseInitialized(showingNotice))) {
this._log(`Initializing database has been failed on some module`, LOG_LEVEL_NOTICE); this._log(`Initializing database has been failed on some module`, LOG_LEVEL_NOTICE);
return false; return false;
} }
this.core.isReady = true; this.core.$$markIsReady();
// run queued event once. // run queued event once.
await this.core.$everyCommitPendingFileEvent(); await this.core.$everyCommitPendingFileEvent();
return true; return true;
} else { } else {
this.core.isReady = false; this.core.$$resetIsReady();
return false; return false;
} }
} }

View File

@@ -6,7 +6,6 @@ import { AbstractModule } from "../AbstractModule.ts";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
export class ModuleKeyValueDB extends AbstractModule implements ICoreModule { export class ModuleKeyValueDB extends AbstractModule implements ICoreModule {
tryCloseKvDB() { tryCloseKvDB() {
try { try {
this.core.kvDB?.close(); this.core.kvDB?.close();
@@ -42,7 +41,7 @@ export class ModuleKeyValueDB extends AbstractModule implements ICoreModule {
} }
async $everyOnloadAfterLoadSettings(): Promise<boolean> { async $everyOnloadAfterLoadSettings(): Promise<boolean> {
if (!await this.openKeyValueDB()) { if (!(await this.openKeyValueDB())) {
return false; return false;
} }
this.core.simpleStore = this.core.$$getSimpleStore<any>("os"); this.core.simpleStore = this.core.$$getSimpleStore<any>("os");
@@ -60,11 +59,21 @@ export class ModuleKeyValueDB extends AbstractModule implements ICoreModule {
delete: async (key: string): Promise<void> => { delete: async (key: string): Promise<void> => {
await this.core.kvDB.del(`${prefix}${key}`); await this.core.kvDB.del(`${prefix}${key}`);
}, },
keys: async (from: string | undefined, to: string | undefined, count?: number | undefined): Promise<string[]> => { keys: async (
const ret = this.core.kvDB.keys(IDBKeyRange.bound(`${prefix}${from || ""}`, `${prefix}${to || ""}`), count); from: string | undefined,
return (await ret).map(e => e.toString()).filter(e => e.startsWith(prefix)).map(e => e.substring(prefix.length)); to: string | undefined,
} count?: number | undefined
} ): Promise<string[]> => {
const ret = this.core.kvDB.keys(
IDBKeyRange.bound(`${prefix}${from || ""}`, `${prefix}${to || ""}`),
count
);
return (await ret)
.map((e) => e.toString())
.filter((e) => e.startsWith(prefix))
.map((e) => e.substring(prefix.length));
},
};
} }
$everyOnInitializeDatabase(db: LiveSyncLocalDB): Promise<boolean> { $everyOnInitializeDatabase(db: LiveSyncLocalDB): Promise<boolean> {
return this.openKeyValueDB(); return this.openKeyValueDB();
@@ -72,7 +81,7 @@ export class ModuleKeyValueDB extends AbstractModule implements ICoreModule {
async $everyOnResetDatabase(db: LiveSyncLocalDB): Promise<boolean> { async $everyOnResetDatabase(db: LiveSyncLocalDB): Promise<boolean> {
try { try {
const kvDBKey = "queued-files" const kvDBKey = "queued-files";
await this.core.kvDB.del(kvDBKey); await this.core.kvDB.del(kvDBKey);
// localStorage.removeItem(lsKey); // localStorage.removeItem(lsKey);
await this.core.kvDB.destroy(); await this.core.kvDB.destroy();

View File

@@ -1,218 +1,197 @@
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from 'octagonal-wheels/common/logger.js'; import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import { SETTING_VERSION_SUPPORT_CASE_INSENSITIVE } from '../../lib/src/common/types.js'; import { type ObsidianLiveSyncSettings } from "../../lib/src/common/types.js";
import { EVENT_REQUEST_OPEN_SETTING_WIZARD, EVENT_REQUEST_OPEN_SETTINGS, EVENT_REQUEST_OPEN_SETUP_URI, eventHub } from '../../common/events.ts'; import {
EVENT_REQUEST_OPEN_P2P,
EVENT_REQUEST_OPEN_SETTING_WIZARD,
EVENT_REQUEST_OPEN_SETTINGS,
EVENT_REQUEST_OPEN_SETUP_URI,
EVENT_REQUEST_RUN_DOCTOR,
eventHub,
} from "../../common/events.ts";
import { AbstractModule } from "../AbstractModule.ts"; import { AbstractModule } from "../AbstractModule.ts";
import type { ICoreModule } from "../ModuleTypes.ts"; import type { ICoreModule } from "../ModuleTypes.ts";
import { $msg } from "src/lib/src/common/i18n.ts";
const URI_DOC = "https://github.com/vrtmrz/obsidian-livesync/blob/main/README.md#how-to-use"; import { checkUnsuitableValues, RuleLevel, type RuleForType } from "../../lib/src/common/configForDoc.ts";
import { getConfName, type AllSettingItemKey } from "../features/SettingDialogue/settingConstants.ts";
export class ModuleMigration extends AbstractModule implements ICoreModule { export class ModuleMigration extends AbstractModule implements ICoreModule {
async migrateUsingDoctor(skipRebuild: boolean = false, activateReason = "updated", forceRescan = false) {
const r = checkUnsuitableValues(this.core.settings);
if (!forceRescan && r.version == this.settings.doctorProcessedVersion) {
const isIssueFound = Object.keys(r.rules).length > 0;
const msg = isIssueFound ? "Issues found" : "No issues found";
this._log(`${msg} but marked as to be silent`, LOG_LEVEL_VERBOSE);
return;
}
const issues = Object.entries(r.rules);
if (issues.length == 0) {
this._log(
$msg("Doctor.Message.NoIssues"),
activateReason !== "updated" ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO
);
return;
} else {
const OPT_YES = `${$msg("Doctor.Button.Yes")}` as const;
const OPT_NO = `${$msg("Doctor.Button.No")}` as const;
const OPT_DISMISS = `${$msg("Doctor.Button.DismissThisVersion")}` as const;
// this._log(`Issues found in ${key}`, LOG_LEVEL_VERBOSE);
const issues = Object.keys(r.rules)
.map((key) => `- ${getConfName(key as AllSettingItemKey)}`)
.join("\n");
const msg = await this.core.confirm.askSelectStringDialogue(
$msg("Doctor.Dialogue.Main", { activateReason, issues }),
[OPT_YES, OPT_NO, OPT_DISMISS],
{
title: $msg("Doctor.Dialogue.Title"),
defaultAction: OPT_YES,
}
);
if (msg == OPT_DISMISS) {
this.settings.doctorProcessedVersion = r.version;
await this.core.saveSettings();
this._log("Marked as to be silent", LOG_LEVEL_VERBOSE);
return;
}
if (msg != OPT_YES) return;
let shouldRebuild = false;
let shouldRebuildLocal = false;
const issueItems = Object.entries(r.rules) as [keyof ObsidianLiveSyncSettings, RuleForType<any>][];
this._log(`${issueItems.length} Issue(s) found `, LOG_LEVEL_VERBOSE);
let idx = 0;
const applySettings = {} as Partial<ObsidianLiveSyncSettings>;
const OPT_FIX = `${$msg("Doctor.Button.Fix")}` as const;
const OPT_SKIP = `${$msg("Doctor.Button.Skip")}` as const;
const OPT_FIXBUTNOREBUILD = `${$msg("Doctor.Button.FixButNoRebuild")}` as const;
let skipped = 0;
for (const [key, value] of issueItems) {
const levelMap = {
[RuleLevel.Necessary]: $msg("Doctor.Level.Necessary"),
[RuleLevel.Recommended]: $msg("Doctor.Level.Recommended"),
[RuleLevel.Optional]: $msg("Doctor.Level.Optional"),
[RuleLevel.Must]: $msg("Doctor.Level.Must"),
};
const level = value.level ? levelMap[value.level] : "Unknown";
const options = [OPT_FIX] as [typeof OPT_FIX | typeof OPT_SKIP | typeof OPT_FIXBUTNOREBUILD];
if ((!skipRebuild && value.requireRebuild) || value.requireRebuildLocal) {
options.push(OPT_FIXBUTNOREBUILD);
}
options.push(OPT_SKIP);
const note = skipRebuild
? ""
: `${value.requireRebuild ? $msg("Doctor.Message.RebuildRequired") : ""}${value.requireRebuildLocal ? $msg("Doctor.Message.RebuildLocalRequired") : ""}`;
const ret = await this.core.confirm.askSelectStringDialogue(
$msg("Doctor.Dialogue.MainFix", {
name: getConfName(key as AllSettingItemKey),
current: `${this.settings[key]}`,
reason: value.reasonFunc?.(this.settings) ?? value.reason ?? " N/A ",
ideal: `${value.valueDisplayFunc ? value.valueDisplayFunc(this.settings) : value.value}`,
//@ts-ignore
level: `${level}`,
note: note,
}),
options,
{
title: $msg("Doctor.Dialogue.TitleFix", { current: `${++idx}`, total: `${issueItems.length}` }),
defaultAction: OPT_FIX,
}
);
if (ret == OPT_FIX || ret == OPT_FIXBUTNOREBUILD) {
//@ts-ignore
applySettings[key] = value.value;
if (ret == OPT_FIX) {
shouldRebuild = shouldRebuild || value.requireRebuild || false;
shouldRebuildLocal = shouldRebuildLocal || value.requireRebuildLocal || false;
}
} else {
skipped++;
}
}
if (Object.keys(applySettings).length > 0) {
this.settings = {
...this.settings,
...applySettings,
};
}
if (skipped == 0) {
this.settings.doctorProcessedVersion = r.version;
} else {
if (
(await this.core.confirm.askYesNoDialog($msg("Doctor.Message.SomeSkipped"), {
title: $msg("Doctor.Dialogue.TitleAlmostDone"),
defaultOption: "No",
})) == "no"
) {
// Some skipped, and user wants
this.settings.doctorProcessedVersion = r.version;
}
}
await this.core.saveSettings();
if (!skipRebuild) {
if (shouldRebuild) {
await this.core.rebuilder.scheduleRebuild();
await this.core.$$performRestart();
} else if (shouldRebuildLocal) {
await this.core.rebuilder.scheduleFetch();
await this.core.$$performRestart();
}
}
}
}
async migrateDisableBulkSend() { async migrateDisableBulkSend() {
if (this.settings.sendChunksBulk) { if (this.settings.sendChunksBulk) {
this._log("Send chunks in bulk has been enabled, however, this feature had been corrupted. Sorry for your inconvenience. Automatically disabled.", LOG_LEVEL_NOTICE); this._log($msg("moduleMigration.logBulkSendCorrupted"), LOG_LEVEL_NOTICE);
this.settings.sendChunksBulk = false; this.settings.sendChunksBulk = false;
this.settings.sendChunksBulkMaxSize = 1; this.settings.sendChunksBulkMaxSize = 1;
await this.saveSettings(); await this.saveSettings();
} }
} }
async migrationCheck() {
const old = this.settings.settingVersion;
const current = SETTING_VERSION_SUPPORT_CASE_INSENSITIVE;
// Check each migrations(old -> current)
if (!await this.migrateToCaseInsensitive(old, current)) {
this._log(`Migration failed or cancelled from ${old} to ${current}`, LOG_LEVEL_NOTICE);
return;
}
}
async migrateToCaseInsensitive(old: number, current: number) {
if (this.settings.handleFilenameCaseSensitive !== undefined && this.settings.doNotUseFixedRevisionForChunks !== undefined) {
if (current < SETTING_VERSION_SUPPORT_CASE_INSENSITIVE) {
this.settings.settingVersion = SETTING_VERSION_SUPPORT_CASE_INSENSITIVE;
await this.saveSettings();
}
return true;
}
if (old >= SETTING_VERSION_SUPPORT_CASE_INSENSITIVE && this.settings.handleFilenameCaseSensitive !== undefined && this.settings.doNotUseFixedRevisionForChunks !== undefined) {
return true;
}
let remoteHandleFilenameCaseSensitive: undefined | boolean = undefined;
let remoteDoNotUseFixedRevisionForChunks: undefined | boolean = undefined;
let remoteChecked = false;
try {
const remoteInfo = await this.core.replicator.getRemotePreferredTweakValues(this.settings);
if (remoteInfo) {
remoteHandleFilenameCaseSensitive = "handleFilenameCaseSensitive" in remoteInfo ? remoteInfo.handleFilenameCaseSensitive : false;
remoteDoNotUseFixedRevisionForChunks = "doNotUseFixedRevisionForChunks" in remoteInfo ? remoteInfo.doNotUseFixedRevisionForChunks : false;
if (remoteHandleFilenameCaseSensitive !== undefined || remoteDoNotUseFixedRevisionForChunks !== undefined) {
remoteChecked = true;
}
} else {
this._log("Failed to fetch remote tweak values", LOG_LEVEL_INFO);
}
} catch (ex) {
this._log("Could not get remote tweak values", LOG_LEVEL_INFO);
this._log(ex, LOG_LEVEL_VERBOSE);
}
if (remoteChecked) {
// The case that the remote could be checked.
if (remoteHandleFilenameCaseSensitive && remoteDoNotUseFixedRevisionForChunks) {
// Migrated, but configured as same as old behaviour.
this.settings.handleFilenameCaseSensitive = true;
this.settings.doNotUseFixedRevisionForChunks = true;
this.settings.settingVersion = SETTING_VERSION_SUPPORT_CASE_INSENSITIVE;
this._log(`Migrated to db:${current} with the same behaviour as before`, LOG_LEVEL_INFO);
await this.saveSettings();
return true;
}
const message = `As you may already know, the self-hosted LiveSync has changed its default behaviour and database structure.
And thankfully, with your time and efforts, the remote database appears to have already been migrated. Congratulations!
However, we need a bit more. The configuration of this device is not compatible with the remote database. We will need to fetch the remote database again. Should we fetch from the remote again now?
___Note: We cannot synchronise until the configuration has been changed and the database has been fetched again.___
___Note2: The chunks are completely immutable, we can fetch only the metadata and difference.___
`;
const OPTION_FETCH = "Yes, fetch again";
const DISMISS = "No, please ask again";
const options = [OPTION_FETCH, DISMISS];
const ret = await this.core.confirm.confirmWithMessage("Case Sensitivity", message, options, "No, please ask again", 40);
if (ret == OPTION_FETCH) {
this.settings.handleFilenameCaseSensitive = remoteHandleFilenameCaseSensitive || false;
this.settings.doNotUseFixedRevisionForChunks = remoteDoNotUseFixedRevisionForChunks || false;
this.settings.settingVersion = SETTING_VERSION_SUPPORT_CASE_INSENSITIVE;
await this.saveSettings();
try {
await this.core.rebuilder.scheduleFetch();
return;
} catch (ex) {
this._log("Failed to create redflag2", LOG_LEVEL_VERBOSE);
this._log(ex, LOG_LEVEL_VERBOSE);
}
return false;
} else {
return false;
}
}
const ENABLE_BOTH = "Enable both";
const ENABLE_FILENAME_CASE_INSENSITIVE = "Enable only #1";
const ENABLE_FIXED_REVISION_FOR_CHUNKS = "Enable only #2";
const ADJUST_TO_REMOTE = "Adjust to remote";
const DISMISS = "Decide it later";
const KEEP = "Keep previous behaviour";
const message = `Since v0.23.21, the self-hosted LiveSync has changed the default behaviour and database structure. The following changes have been made:
1. **Case sensitivity of filenames**
The handling of filenames is now case-insensitive. This is a beneficial change for most platforms, other than Linux and iOS, which do not manage filename case sensitivity effectively.
(On These, a warning will be displayed for files with the same name but different cases).
2. **Revision handling of the chunks**
Chunks are immutable, which allows their revisions to be fixed. This change will enhance the performance of file saving.
___However, to enable either of these changes, both remote and local databases need to be rebuilt. This process takes a few minutes, and we recommend doing it when you have ample time.___
- If you wish to maintain the previous behaviour, you can skip this process by using \`${KEEP}\`.
- If you do not have enough time, please choose \`${DISMISS}\`. You will be prompted again later.
- If you have rebuilt the database on another device, please select \`${DISMISS}\` and try synchronizing again. Since a difference has been detected, you will be prompted again.
`;
const options = [
ENABLE_BOTH,
ENABLE_FILENAME_CASE_INSENSITIVE,
ENABLE_FIXED_REVISION_FOR_CHUNKS];
if (remoteChecked) {
options.push(ADJUST_TO_REMOTE);
}
options.push(KEEP, DISMISS);
const ret = await this.core.confirm.confirmWithMessage("Case Sensitivity", message, options, DISMISS, 40);
console.dir(ret);
switch (ret) {
case ENABLE_BOTH:
this.settings.handleFilenameCaseSensitive = false;
this.settings.doNotUseFixedRevisionForChunks = false;
break;
case ENABLE_FILENAME_CASE_INSENSITIVE:
this.settings.handleFilenameCaseSensitive = false;
this.settings.doNotUseFixedRevisionForChunks = true;
break;
case ENABLE_FIXED_REVISION_FOR_CHUNKS:
this.settings.doNotUseFixedRevisionForChunks = false;
this.settings.handleFilenameCaseSensitive = true;
break;
case KEEP:
this.settings.handleFilenameCaseSensitive = true;
this.settings.doNotUseFixedRevisionForChunks = true;
this.settings.settingVersion = SETTING_VERSION_SUPPORT_CASE_INSENSITIVE;
await this.saveSettings();
return true;
case DISMISS:
default:
return false;
}
this.settings.settingVersion = SETTING_VERSION_SUPPORT_CASE_INSENSITIVE;
await this.saveSettings();
await this.core.rebuilder.scheduleRebuild();
await this.core.$$performRestart();
}
async initialMessage() { async initialMessage() {
const message = `Your device has **not been set up yet**. Let me guide you through the setup process. const message = $msg("moduleMigration.msgInitialSetup", {
URI_DOC: $msg("moduleMigration.docUri"),
});
const USE_SETUP = $msg("moduleMigration.optionHaveSetupUri");
const NEXT = $msg("moduleMigration.optionNoSetupUri");
Please keep in mind that every dialogue content can be copied to the clipboard. If you need to refer to it later, you can paste it into a note in Obsidian. You can also translate it into your language using a translation tool. const ret = await this.core.confirm.askSelectStringDialogue(message, [USE_SETUP, NEXT], {
title: $msg("moduleMigration.titleWelcome"),
First, do you have **Setup URI**? defaultAction: USE_SETUP,
Note: If you do not know what it is, please refer to the [documentation](${URI_DOC}).
`;
const USE_SETUP = "Yes, I have";
const NEXT = "No, I do not have";
const ret = await this.core.confirm.askSelectStringDialogue(message, [
USE_SETUP, NEXT], {
title: "Welcome to Self-hosted LiveSync",
defaultAction: USE_SETUP
}); });
if (ret === USE_SETUP) { if (ret === USE_SETUP) {
eventHub.emitEvent(EVENT_REQUEST_OPEN_SETUP_URI); eventHub.emitEvent(EVENT_REQUEST_OPEN_SETUP_URI);
return false; return false;
} } else if (ret == NEXT) {
else if (ret == NEXT) {
return true; return true;
} }
return false; return false;
} }
async askAgainForSetupURI() { async askAgainForSetupURI() {
const message = `We strongly recommend that you generate a set-up URI and use it. const message = $msg("moduleMigration.msgRecommendSetupUri", { URI_DOC: $msg("moduleMigration.docUri") });
If you do not have knowledge about it, please refer to the [documentation](${URI_DOC}) (Sorry again, but it is important). const USE_MINIMAL = $msg("moduleMigration.optionSetupWizard");
const USE_P2P = $msg("moduleMigration.optionSetupViaP2P");
const USE_SETUP = $msg("moduleMigration.optionManualSetup");
const NEXT = $msg("moduleMigration.optionRemindNextLaunch");
How do you want to set it up manually?`; const ret = await this.core.confirm.askSelectStringDialogue(message, [USE_MINIMAL, USE_SETUP, USE_P2P, NEXT], {
title: $msg("moduleMigration.titleRecommendSetupUri"),
const USE_MINIMAL = "Take me into the setup wizard"; defaultAction: USE_MINIMAL,
const USE_SETUP = "Set it up all manually";
const NEXT = "Remind me at the next launch";
const ret = await this.core.confirm.askSelectStringDialogue(message, [
USE_MINIMAL, USE_SETUP, NEXT], {
title: "Recommendation to use Setup URI",
defaultAction: USE_MINIMAL
}); });
if (ret === USE_MINIMAL) { if (ret === USE_MINIMAL) {
eventHub.emitEvent(EVENT_REQUEST_OPEN_SETTING_WIZARD); eventHub.emitEvent(EVENT_REQUEST_OPEN_SETTING_WIZARD);
return false; return false;
} }
if (ret === USE_P2P) {
eventHub.emitEvent(EVENT_REQUEST_OPEN_P2P);
return false;
}
if (ret === USE_SETUP) { if (ret === USE_SETUP) {
eventHub.emitEvent(EVENT_REQUEST_OPEN_SETTINGS); eventHub.emitEvent(EVENT_REQUEST_OPEN_SETTINGS);
return false; return false;
} } else if (ret == NEXT) {
else if (ret == NEXT) {
return false; return false;
} }
return false; return false;
@@ -220,21 +199,28 @@ How do you want to set it up manually?`;
async $everyOnFirstInitialize(): Promise<boolean> { async $everyOnFirstInitialize(): Promise<boolean> {
if (!this.localDatabase.isReady) { if (!this.localDatabase.isReady) {
this._log(`Something went wrong! The local database is not ready`, LOG_LEVEL_NOTICE); this._log($msg("moduleMigration.logLocalDatabaseNotReady"), LOG_LEVEL_NOTICE);
return false; return false;
} }
if (this.settings.isConfigured) { if (this.settings.isConfigured) {
await this.migrationCheck(); await this.migrateUsingDoctor(false);
// await this.migrationCheck();
await this.migrateDisableBulkSend(); await this.migrateDisableBulkSend();
} }
if (!this.settings.isConfigured) { if (!this.settings.isConfigured) {
// Case sensitivity // Case sensitivity
if (!await this.initialMessage() || !await this.askAgainForSetupURI()) { if (!(await this.initialMessage()) || !(await this.askAgainForSetupURI())) {
this._log("The setup has been cancelled, Self-hosted LiveSync waiting for your setup!", LOG_LEVEL_NOTICE); this._log($msg("moduleMigration.logSetupCancelled"), LOG_LEVEL_NOTICE);
return false; return false;
} }
await this.migrateUsingDoctor(true);
} }
return true; return true;
} }
$everyOnLayoutReady(): Promise<boolean> {
eventHub.onEvent(EVENT_REQUEST_RUN_DOCTOR, async (reason) => {
await this.migrateUsingDoctor(false, reason, true);
});
return Promise.resolve(true);
}
} }

View File

@@ -3,10 +3,7 @@
// //
// Original Implementation is here: https://github.com/remotely-save/remotely-save/blob/28b99557a864ef59c19d2ad96101196e401718f0/src/remoteForS3.ts // Original Implementation is here: https://github.com/remotely-save/remotely-save/blob/28b99557a864ef59c19d2ad96101196e401718f0/src/remoteForS3.ts
import { import { FetchHttpHandler, type FetchHttpHandlerOptions } from "@smithy/fetch-http-handler";
FetchHttpHandler,
type FetchHttpHandlerOptions,
} from "@smithy/fetch-http-handler";
import { HttpRequest, HttpResponse, type HttpHandlerOptions } from "@smithy/protocol-http"; import { HttpRequest, HttpResponse, type HttpHandlerOptions } from "@smithy/protocol-http";
//@ts-ignore //@ts-ignore
import { requestTimeout } from "@smithy/fetch-http-handler/dist-es/request-timeout"; import { requestTimeout } from "@smithy/fetch-http-handler/dist-es/request-timeout";
@@ -25,20 +22,13 @@ import { requestUrl, type RequestUrlParam } from "../../../deps.ts";
export class ObsHttpHandler extends FetchHttpHandler { export class ObsHttpHandler extends FetchHttpHandler {
requestTimeoutInMs: number | undefined; requestTimeoutInMs: number | undefined;
reverseProxyNoSignUrl: string | undefined; reverseProxyNoSignUrl: string | undefined;
constructor( constructor(options?: FetchHttpHandlerOptions, reverseProxyNoSignUrl?: string) {
options?: FetchHttpHandlerOptions,
reverseProxyNoSignUrl?: string
) {
super(options); super(options);
this.requestTimeoutInMs = this.requestTimeoutInMs = options === undefined ? undefined : options.requestTimeout;
options === undefined ? undefined : options.requestTimeout;
this.reverseProxyNoSignUrl = reverseProxyNoSignUrl; this.reverseProxyNoSignUrl = reverseProxyNoSignUrl;
} }
// eslint-disable-next-line require-await // eslint-disable-next-line require-await
async handle( async handle(request: HttpRequest, { abortSignal }: HttpHandlerOptions = {}): Promise<{ response: HttpResponse }> {
request: HttpRequest,
{ abortSignal }: HttpHandlerOptions = {}
): Promise<{ response: HttpResponse }> {
if (abortSignal?.aborted) { if (abortSignal?.aborted) {
const abortError = new Error("Request aborted"); const abortError = new Error("Request aborted");
abortError.name = "AbortError"; abortError.name = "AbortError";
@@ -54,18 +44,13 @@ export class ObsHttpHandler extends FetchHttpHandler {
} }
const { port, method } = request; const { port, method } = request;
let url = `${request.protocol}//${request.hostname}${port ? `:${port}` : "" let url = `${request.protocol}//${request.hostname}${port ? `:${port}` : ""}${path}`;
}${path}`; if (this.reverseProxyNoSignUrl !== undefined && this.reverseProxyNoSignUrl !== "") {
if (
this.reverseProxyNoSignUrl !== undefined &&
this.reverseProxyNoSignUrl !== ""
) {
const urlObj = new URL(url); const urlObj = new URL(url);
urlObj.host = this.reverseProxyNoSignUrl; urlObj.host = this.reverseProxyNoSignUrl;
url = urlObj.href; url = urlObj.href;
} }
const body = const body = method === "GET" || method === "HEAD" ? undefined : request.body;
method === "GET" || method === "HEAD" ? undefined : request.body;
const transformedHeaders: Record<string, string> = {}; const transformedHeaders: Record<string, string> = {};
for (const key of Object.keys(request.headers)) { for (const key of Object.keys(request.headers)) {

View File

@@ -1,59 +1,119 @@
import { AbstractObsidianModule, type IObsidianModule } from '../AbstractObsidianModule.ts'; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { LOG_LEVEL_DEBUG, LOG_LEVEL_VERBOSE } from 'octagonal-wheels/common/logger'; import { LOG_LEVEL_DEBUG, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import { Notice, requestUrl, type RequestUrlParam, type RequestUrlResponse } from '../../deps.ts'; import { Notice, requestUrl, type RequestUrlParam, type RequestUrlResponse } from "../../deps.ts";
import { type EntryDoc, type FilePathWithPrefix } from '../../lib/src/common/types.ts'; import { type CouchDBCredentials, type EntryDoc, type FilePathWithPrefix } from "../../lib/src/common/types.ts";
import { getPathFromTFile } from '../../common/utils.ts'; import { getPathFromTFile } from "../../common/utils.ts";
import { disableEncryption, enableEncryption, isCloudantURI, isValidRemoteCouchDBURI, replicationFilter } from '../../lib/src/pouchdb/utils_couchdb.ts'; import { isCloudantURI, isValidRemoteCouchDBURI } from "../../lib/src/pouchdb/utils_couchdb.ts";
import { setNoticeClass } from '../../lib/src/mock_and_interop/wrapper.ts'; import { replicationFilter } from "@/lib/src/pouchdb/compress.ts";
import { ObsHttpHandler } from './APILib/ObsHttpHandler.ts'; import { disableEncryption } from "@/lib/src/pouchdb/encryption.ts";
import { PouchDB } from '../../lib/src/pouchdb/pouchdb-browser.ts'; import { enableEncryption } from "@/lib/src/pouchdb/encryption.ts";
import { reactive, reactiveSource } from 'octagonal-wheels/dataobject/reactive'; import { setNoticeClass } from "../../lib/src/mock_and_interop/wrapper.ts";
import { ObsHttpHandler } from "./APILib/ObsHttpHandler.ts";
import { PouchDB } from "../../lib/src/pouchdb/pouchdb-browser.ts";
import { AuthorizationHeaderGenerator } from "../../lib/src/replication/httplib.ts";
setNoticeClass(Notice); setNoticeClass(Notice);
async function fetchByAPI(request: RequestUrlParam, errorAsResult = false): Promise<RequestUrlResponse> {
async function fetchByAPI(request: RequestUrlParam): Promise<RequestUrlResponse> { const ret = await requestUrl({ ...request, throw: !errorAsResult });
const ret = await requestUrl(request);
if (ret.status - (ret.status % 100) !== 200) {
const er: Error & { status?: number } = new Error(`Request Error:${ret.status}`);
if (ret.json) {
er.message = ret.json.reason;
er.name = `${ret.json.error ?? ""}:${ret.json.message ?? ""}`;
}
er.status = ret.status;
throw er;
}
return ret; return ret;
} }
export class ModuleObsidianAPI extends AbstractObsidianModule implements IObsidianModule { export class ModuleObsidianAPI extends AbstractObsidianModule implements IObsidianModule {
_customHandler!: ObsHttpHandler; _customHandler!: ObsHttpHandler;
authHeaderSource = reactiveSource<string>("");
authHeader = reactive(() =>
this.authHeaderSource.value == "" ? "" : "Basic " + window.btoa(this.authHeaderSource.value));
_authHeader = new AuthorizationHeaderGenerator();
last_successful_post = false;
$$customFetchHandler(): ObsHttpHandler { $$customFetchHandler(): ObsHttpHandler {
if (!this._customHandler) this._customHandler = new ObsHttpHandler(undefined, undefined); if (!this._customHandler) this._customHandler = new ObsHttpHandler(undefined, undefined);
return this._customHandler; return this._customHandler;
} }
$$getLastPostFailedBySize(): boolean {
return !this.last_successful_post;
}
async $$connectRemoteCouchDB(uri: string, auth: { username: string; password: string }, disableRequestURI: boolean, passphrase: string | false, useDynamicIterationCount: boolean, performSetup: boolean, skipInfo: boolean, compression: boolean): Promise<string | { db: PouchDB.Database<EntryDoc>; info: PouchDB.Core.DatabaseInfo }> { async _fetchByAPI(url: string, authHeader: string, opts?: RequestInit): Promise<Response> {
const body = opts?.body as string;
const transformedHeaders = { ...(opts?.headers as Record<string, string>) };
if (authHeader != "") transformedHeaders["authorization"] = authHeader;
delete transformedHeaders["host"];
delete transformedHeaders["Host"];
delete transformedHeaders["content-length"];
delete transformedHeaders["Content-Length"];
const requestParam: RequestUrlParam = {
url,
method: opts?.method,
body: body,
headers: transformedHeaders,
contentType:
transformedHeaders?.["content-type"] ?? transformedHeaders?.["Content-Type"] ?? "application/json",
};
const r = await fetchByAPI(requestParam, true);
return new Response(r.arrayBuffer, {
headers: r.headers,
status: r.status,
statusText: `${r.status}`,
});
}
async fetchByAPI(
url: string,
localURL: string,
method: string,
authHeader: string,
opts?: RequestInit
): Promise<Response> {
const body = opts?.body as string;
const size = body ? ` (${body.length})` : "";
try {
const r = await this._fetchByAPI(url, authHeader, opts);
this.plugin.requestCount.value = this.plugin.requestCount.value + 1;
if (method == "POST" || method == "PUT") {
this.last_successful_post = r.status - (r.status % 100) == 200;
} else {
this.last_successful_post = true;
}
this._log(`HTTP:${method}${size} to:${localURL} -> ${r.status}`, LOG_LEVEL_DEBUG);
return r;
} catch (ex) {
this._log(`HTTP:${method}${size} to:${localURL} -> failed`, LOG_LEVEL_VERBOSE);
// limit only in bulk_docs.
if (url.toString().indexOf("_bulk_docs") !== -1) {
this.last_successful_post = false;
}
this._log(ex);
throw ex;
} finally {
this.plugin.responseCount.value = this.plugin.responseCount.value + 1;
}
}
async $$connectRemoteCouchDB(
uri: string,
auth: CouchDBCredentials,
disableRequestURI: boolean,
passphrase: string | false,
useDynamicIterationCount: boolean,
performSetup: boolean,
skipInfo: boolean,
compression: boolean,
customHeaders: Record<string, string>,
useRequestAPI: boolean,
getPBKDF2Salt: () => Promise<Uint8Array>
): Promise<string | { db: PouchDB.Database<EntryDoc>; info: PouchDB.Core.DatabaseInfo }> {
if (!isValidRemoteCouchDBURI(uri)) return "Remote URI is not valid"; if (!isValidRemoteCouchDBURI(uri)) return "Remote URI is not valid";
if (uri.toLowerCase() != uri) return "Remote URI and database name could not contain capital letters."; if (uri.toLowerCase() != uri) return "Remote URI and database name could not contain capital letters.";
if (uri.indexOf(" ") !== -1) return "Remote URI and database name could not contain spaces."; if (uri.indexOf(" ") !== -1) return "Remote URI and database name could not contain spaces.";
const userNameAndPassword = (auth.username && auth.password) ? `${auth.username}:${auth.password}` : ""; // let authHeader = await this._authHeader.getAuthorizationHeader(auth);
if (this.authHeaderSource.value != userNameAndPassword) {
this.authHeaderSource.value = userNameAndPassword;
}
const authHeader = this.authHeader.value;
// const _this = this;
const conf: PouchDB.HttpAdapter.HttpAdapterConfiguration = { const conf: PouchDB.HttpAdapter.HttpAdapterConfiguration = {
adapter: "http", adapter: "http",
auth, auth: "username" in auth ? auth : undefined,
skip_setup: !performSetup, skip_setup: !performSetup,
fetch: async (url: string | Request, opts?: RequestInit) => { fetch: async (url: string | Request, opts?: RequestInit) => {
const authHeader = await this._authHeader.getAuthorizationHeader(auth);
let size = ""; let size = "";
const localURL = url.toString().substring(uri.length); const localURL = url.toString().substring(uri.length);
const method = opts?.method ?? "GET"; const method = opts?.method ?? "GET";
@@ -62,99 +122,103 @@ export class ModuleObsidianAPI extends AbstractObsidianModule implements IObsidi
if (opts_length > 1000 * 1000 * 10) { if (opts_length > 1000 * 1000 * 10) {
// over 10MB // over 10MB
if (isCloudantURI(uri)) { if (isCloudantURI(uri)) {
this.plugin.last_successful_post = false; this.last_successful_post = false;
this._log("This request should fail on IBM Cloudant.", LOG_LEVEL_VERBOSE); this._log("This request should fail on IBM Cloudant.", LOG_LEVEL_VERBOSE);
throw new Error("This request should fail on IBM Cloudant."); throw new Error("This request should fail on IBM Cloudant.");
} }
} }
size = ` (${opts_length})`; size = ` (${opts_length})`;
} }
if (!disableRequestURI && typeof url == "string" && typeof (opts?.body ?? "") == "string") { try {
const body = opts?.body as string; const headers = new Headers(opts?.headers);
if (customHeaders) {
const transformedHeaders = { ...(opts?.headers as Record<string, string>) }; for (const [key, value] of Object.entries(customHeaders)) {
if (authHeader != "") transformedHeaders["authorization"] = authHeader; if (key && value) {
delete transformedHeaders["host"]; headers.append(key, value);
delete transformedHeaders["Host"]; }
delete transformedHeaders["content-length"]; }
delete transformedHeaders["Content-Length"]; }
const requestParam: RequestUrlParam = { if (!("username" in auth)) {
url, headers.append("authorization", authHeader);
method: opts?.method, }
body: body,
headers: transformedHeaders,
contentType: "application/json",
// contentType: opts.headers,
};
try { try {
this.plugin.requestCount.value = this.plugin.requestCount.value + 1; this.plugin.requestCount.value = this.plugin.requestCount.value + 1;
const r = await fetchByAPI(requestParam); const response: Response = await (useRequestAPI
? this._fetchByAPI(url.toString(), authHeader, { ...opts, headers })
: fetch(url, { ...opts, headers }));
if (method == "POST" || method == "PUT") { if (method == "POST" || method == "PUT") {
this.plugin.last_successful_post = r.status - (r.status % 100) == 200; this.last_successful_post = response.ok;
} else { } else {
this.plugin.last_successful_post = true; this.last_successful_post = true;
} }
this._log(`HTTP:${method}${size} to:${localURL} -> ${r.status}`, LOG_LEVEL_DEBUG); this._log(`HTTP:${method}${size} to:${localURL} -> ${response.status}`, LOG_LEVEL_DEBUG);
if (Math.floor(response.status / 100) !== 2) {
return new Response(r.arrayBuffer, { if (response.status == 404) {
headers: r.headers, if (method === "GET" && localURL.indexOf("/_local/") === -1) {
status: r.status, this._log(
statusText: `${r.status}`, `Just checkpoint or some server information has been missing. The 404 error shown above is not an error.`,
}); LOG_LEVEL_VERBOSE
} catch (ex) { );
this._log(`HTTP:${method}${size} to:${localURL} -> failed`, LOG_LEVEL_VERBOSE); }
// limit only in bulk_docs. } else {
if (url.toString().indexOf("_bulk_docs") !== -1) { const r = response.clone();
this.plugin.last_successful_post = false; this._log(
} `The request may have failed. The reason sent by the server: ${r.status}: ${r.statusText}`,
this._log(ex); LOG_LEVEL_NOTICE
throw ex; );
} finally { try {
this.plugin.responseCount.value = this.plugin.responseCount.value + 1; const result = await r.text();
} this._log(result, LOG_LEVEL_VERBOSE);
} } catch (_) {
this._log("Cloud not fetch response body", LOG_LEVEL_VERBOSE);
try { this._log(_, LOG_LEVEL_VERBOSE);
if (this.settings.enableDebugTools) { }
// Issue #407
(opts!.headers as Headers).append("ngrok-skip-browser-warning", "123");
}
this.plugin.requestCount.value = this.plugin.requestCount.value + 1;
const response: Response = await fetch(url, opts);
if (method == "POST" || method == "PUT") {
this.plugin.last_successful_post = response.ok;
} else {
this.plugin.last_successful_post = true;
}
this._log(`HTTP:${method}${size} to:${localURL} -> ${response.status}`, LOG_LEVEL_DEBUG);
if (Math.floor(response.status / 100) !== 2) {
if (method != "GET" && localURL.indexOf("/_local/") === -1 && !localURL.endsWith("/")) {
const r = response.clone();
this._log(`The request may have failed. The reason sent by the server: ${r.status}: ${r.statusText}`);
try {
this._log(await (await r.blob()).text(), LOG_LEVEL_VERBOSE);
} catch (_) {
this._log("Cloud not parse response", LOG_LEVEL_VERBOSE);
this._log(_, LOG_LEVEL_VERBOSE);
} }
} else {
this._log(`Just checkpoint or some server information has been missing. The 404 error shown above is not an error.`, LOG_LEVEL_VERBOSE)
} }
return response;
} catch (ex) {
if (ex instanceof TypeError) {
if (useRequestAPI) {
this._log("Failed to request by API.");
throw ex;
}
this._log(
"Failed to fetch by native fetch API. Trying to fetch by API to get more information."
);
const resp2 = await this.fetchByAPI(url.toString(), localURL, method, authHeader, {
...opts,
headers,
});
if (resp2.status / 100 == 2) {
this._log(
"The request was successful by API. But the native fetch API failed! Please check CORS settings on the remote database!. While this condition, you cannot enable LiveSync",
LOG_LEVEL_NOTICE
);
return resp2;
}
const r2 = resp2.clone();
const msg = await r2.text();
this._log(`Failed to fetch by API. ${resp2.status}: ${msg}`, LOG_LEVEL_NOTICE);
return resp2;
}
throw ex;
} }
return response; } catch (ex: any) {
} catch (ex) {
this._log(`HTTP:${method}${size} to:${localURL} -> failed`, LOG_LEVEL_VERBOSE); this._log(`HTTP:${method}${size} to:${localURL} -> failed`, LOG_LEVEL_VERBOSE);
const msg = ex instanceof Error ? `${ex?.name}:${ex?.message}` : ex?.toString();
this._log(`Failed to fetch: ${msg}`, LOG_LEVEL_NOTICE);
this._log(ex, LOG_LEVEL_VERBOSE);
// limit only in bulk_docs. // limit only in bulk_docs.
if (url.toString().indexOf("_bulk_docs") !== -1) { if (url.toString().indexOf("_bulk_docs") !== -1) {
this.plugin.last_successful_post = false; this.last_successful_post = false;
} }
this._log(ex); this._log(ex);
throw ex; throw ex;
} finally { } finally {
this.plugin.responseCount.value = this.plugin.responseCount.value + 1; this.plugin.responseCount.value = this.plugin.responseCount.value + 1;
} }
// return await fetch(url, opts); // return await fetch(url, opts);
}, },
}; };
@@ -163,7 +227,14 @@ export class ModuleObsidianAPI extends AbstractObsidianModule implements IObsidi
replicationFilter(db, compression); replicationFilter(db, compression);
disableEncryption(); disableEncryption();
if (passphrase !== "false" && typeof passphrase === "string") { if (passphrase !== "false" && typeof passphrase === "string") {
enableEncryption(db, passphrase, useDynamicIterationCount, false); enableEncryption(
db,
passphrase,
useDynamicIterationCount,
false,
getPBKDF2Salt,
this.settings.E2EEAlgorithm
);
} }
if (skipInfo) { if (skipInfo) {
return { db: db, info: { db_name: "", doc_count: 0, update_seq: "" } }; return { db: db, info: { db_name: "", doc_count: 0, update_seq: "" } };
@@ -172,10 +243,7 @@ export class ModuleObsidianAPI extends AbstractObsidianModule implements IObsidi
const info = await db.info(); const info = await db.info();
return { db: db, info: info }; return { db: db, info: info };
} catch (ex: any) { } catch (ex: any) {
let msg = `${ex?.name}:${ex?.message}`; const msg = `${ex?.name}:${ex?.message}`;
if (ex?.name == "TypeError" && ex?.message == "Failed to fetch") {
msg += "\n**Note** This error caused by many reasons. The only sure thing is you didn't touch the server.\nTo check details, open inspector.";
}
this._log(ex, LOG_LEVEL_VERBOSE); this._log(ex, LOG_LEVEL_VERBOSE);
return msg; return msg;
} }
@@ -190,7 +258,10 @@ export class ModuleObsidianAPI extends AbstractObsidianModule implements IObsidi
return this.app.vault.getName(); return this.app.vault.getName();
} }
$$getVaultName(): string { $$getVaultName(): string {
return this.core.$$vaultName() + (this.settings?.additionalSuffixOfDatabaseName ? ("-" + this.settings.additionalSuffixOfDatabaseName) : ""); return (
this.core.$$vaultName() +
(this.settings?.additionalSuffixOfDatabaseName ? "-" + this.settings.additionalSuffixOfDatabaseName : "")
);
} }
$$getActiveFilePath(): FilePathWithPrefix | undefined { $$getActiveFilePath(): FilePathWithPrefix | undefined {
const file = this.app.workspace.getActiveFile(); const file = this.app.workspace.getActiveFile();
@@ -201,7 +272,6 @@ export class ModuleObsidianAPI extends AbstractObsidianModule implements IObsidi
} }
$anyGetAppId(): Promise<string | undefined> { $anyGetAppId(): Promise<string | undefined> {
return Promise.resolve(`${("appId" in this.app ? this.app.appId : "")}`); return Promise.resolve(`${"appId" in this.app ? this.app.appId : ""}`);
} }
} }

View File

@@ -1,25 +1,35 @@
import { AbstractObsidianModule, type IObsidianModule } from '../AbstractObsidianModule.ts'; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { EVENT_FILE_RENAMED, EVENT_LEAF_ACTIVE_CHANGED, eventHub } from '../../common/events.js'; import { EVENT_FILE_RENAMED, EVENT_LEAF_ACTIVE_CHANGED, eventHub } from "../../common/events.js";
import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from 'octagonal-wheels/common/logger'; import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import { scheduleTask } from 'octagonal-wheels/concurrency/task'; import { scheduleTask } from "octagonal-wheels/concurrency/task";
import { type TFile } from '../../deps.ts'; import { type TFile } from "../../deps.ts";
import { fireAndForget } from 'octagonal-wheels/promises'; import { fireAndForget } from "octagonal-wheels/promises";
import { type FilePathWithPrefix } from '../../lib/src/common/types.ts'; import { type FilePathWithPrefix } from "../../lib/src/common/types.ts";
import { reactive, reactiveSource } from 'octagonal-wheels/dataobject/reactive'; import { reactive, reactiveSource } from "octagonal-wheels/dataobject/reactive";
import { collectingChunks, pluginScanningCount, hiddenFilesEventCount, hiddenFilesProcessingCount } from '../../lib/src/mock_and_interop/stores.ts'; import {
collectingChunks,
pluginScanningCount,
hiddenFilesEventCount,
hiddenFilesProcessingCount,
} from "../../lib/src/mock_and_interop/stores.ts";
export class ModuleObsidianEvents extends AbstractObsidianModule implements IObsidianModule { export class ModuleObsidianEvents extends AbstractObsidianModule implements IObsidianModule {
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
// this.registerEvent(this.app.workspace.on("editor-change", )); // this.registerEvent(this.app.workspace.on("editor-change", ));
this.plugin.registerEvent(this.app.vault.on("rename", (file, oldPath) => { this.plugin.registerEvent(
eventHub.emitEvent(EVENT_FILE_RENAMED, { newPath: file.path, old: oldPath }); this.app.vault.on("rename", (file, oldPath) => {
})); eventHub.emitEvent(EVENT_FILE_RENAMED, {
this.plugin.registerEvent(this.app.workspace.on("active-leaf-change", () => eventHub.emitEvent(EVENT_LEAF_ACTIVE_CHANGED))); newPath: file.path as FilePathWithPrefix,
old: oldPath as FilePathWithPrefix,
});
})
);
this.plugin.registerEvent(
this.app.workspace.on("active-leaf-change", () => eventHub.emitEvent(EVENT_LEAF_ACTIVE_CHANGED))
);
return Promise.resolve(true); return Promise.resolve(true);
} }
$$performRestart(): void { $$performRestart(): void {
this._performAppReload(); this._performAppReload();
} }
@@ -33,22 +43,20 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
swapSaveCommand() { swapSaveCommand() {
this._log("Modifying callback of the save command", LOG_LEVEL_VERBOSE); this._log("Modifying callback of the save command", LOG_LEVEL_VERBOSE);
const saveCommandDefinition = (this.app as any).commands?.commands?.[ const saveCommandDefinition = (this.app as any).commands?.commands?.["editor:save-file"];
"editor:save-file"
];
const save = saveCommandDefinition?.callback; const save = saveCommandDefinition?.callback;
if (typeof save === "function") { if (typeof save === "function") {
this.initialCallback = save; this.initialCallback = save;
saveCommandDefinition.callback = () => { saveCommandDefinition.callback = () => {
scheduleTask("syncOnEditorSave", 250, () => { scheduleTask("syncOnEditorSave", 250, () => {
if (this.plugin._unloaded) { if (this.core.$$isUnloaded()) {
this._log("Unload and remove the handler.", LOG_LEVEL_VERBOSE); this._log("Unload and remove the handler.", LOG_LEVEL_VERBOSE);
saveCommandDefinition.callback = this.initialCallback; saveCommandDefinition.callback = this.initialCallback;
this.initialCallback = undefined; this.initialCallback = undefined;
} else { } else {
if (this.settings.syncOnEditorSave) { if (this.settings.syncOnEditorSave) {
this._log("Sync on Editor Save.", LOG_LEVEL_VERBOSE); this._log("Sync on Editor Save.", LOG_LEVEL_VERBOSE);
fireAndForget(() => this.core.$$replicate()); fireAndForget(() => this.core.$$replicateByEvent());
} }
} }
}); });
@@ -60,7 +68,7 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
//@ts-ignore //@ts-ignore
window.CodeMirrorAdapter.commands.save = () => { window.CodeMirrorAdapter.commands.save = () => {
//@ts-ignore //@ts-ignore
_this.app.commands.executeCommandById('editor:save-file') _this.app.commands.executeCommandById("editor:save-file");
// _this.app.performCommand('editor:save-file'); // _this.app.performCommand('editor:save-file');
}; };
} }
@@ -98,14 +106,14 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
// TODO:FIXME AT V0.17.31, this logic has been disabled. // TODO:FIXME AT V0.17.31, this logic has been disabled.
if (navigator.onLine && this.localDatabase.needScanning) { if (navigator.onLine && this.localDatabase.needScanning) {
this.localDatabase.needScanning = false; this.localDatabase.needScanning = false;
await this.plugin.$$performFullScan(); await this.core.$$performFullScan();
} }
} }
async watchWindowVisibilityAsync() { async watchWindowVisibilityAsync() {
if (this.settings.suspendFileWatching) return; if (this.settings.suspendFileWatching) return;
if (!this.settings.isConfigured) return; if (!this.settings.isConfigured) return;
if (!this.plugin.isReady) return; if (!this.core.$$isReady()) return;
if (this.isLastHidden && !this.hasFocus) { if (this.isLastHidden && !this.hasFocus) {
// NO OP while non-focused after made hidden; // NO OP while non-focused after made hidden;
@@ -124,7 +132,7 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
await this.core.$everyBeforeSuspendProcess(); await this.core.$everyBeforeSuspendProcess();
} else { } else {
// suspend all temporary. // suspend all temporary.
if (this.plugin.suspended) return; if (this.core.$$isSuspended()) return;
if (!this.hasFocus) return; if (!this.hasFocus) return;
await this.core.$everyOnResumeProcess(); await this.core.$everyOnResumeProcess();
await this.core.$everyAfterResumeProcess(); await this.core.$everyAfterResumeProcess();
@@ -133,7 +141,7 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
watchWorkspaceOpen(file: TFile | null) { watchWorkspaceOpen(file: TFile | null) {
if (this.settings.suspendFileWatching) return; if (this.settings.suspendFileWatching) return;
if (!this.settings.isConfigured) return; if (!this.settings.isConfigured) return;
if (!this.plugin.isReady) return; if (!this.core.$$isReady()) return;
if (!file) return; if (!file) return;
scheduleTask("watch-workspace-open", 500, () => fireAndForget(() => this.watchWorkspaceOpenAsync(file))); scheduleTask("watch-workspace-open", 500, () => fireAndForget(() => this.watchWorkspaceOpenAsync(file)));
} }
@@ -141,13 +149,13 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
async watchWorkspaceOpenAsync(file: TFile) { async watchWorkspaceOpenAsync(file: TFile) {
if (this.settings.suspendFileWatching) return; if (this.settings.suspendFileWatching) return;
if (!this.settings.isConfigured) return; if (!this.settings.isConfigured) return;
if (!this.plugin.isReady) return; if (!this.core.$$isReady()) return;
await this.core.$everyCommitPendingFileEvent(); await this.core.$everyCommitPendingFileEvent();
if (file == null) { if (file == null) {
return; return;
} }
if (this.settings.syncOnFileOpen && !this.plugin.suspended) { if (this.settings.syncOnFileOpen && !this.core.$$isSuspended()) {
await this.core.$$replicate(); await this.core.$$replicateByEvent();
} }
await this.core.$$queueConflictCheckIfOpen(file.path as FilePathWithPrefix); await this.core.$$queueConflictCheckIfOpen(file.path as FilePathWithPrefix);
} }
@@ -158,9 +166,8 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
return Promise.resolve(true); return Promise.resolve(true);
} }
$$askReload(message?: string) { $$askReload(message?: string) {
if (this.core.isReloadingScheduled) { if (this.core.$$isReloadingScheduled()) {
this._log(`Reloading is already scheduled`, LOG_LEVEL_VERBOSE); this._log(`Reloading is already scheduled`, LOG_LEVEL_VERBOSE);
return; return;
} }
@@ -168,16 +175,17 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
const RESTART_NOW = "Yes, restart immediately"; const RESTART_NOW = "Yes, restart immediately";
const RESTART_AFTER_STABLE = "Yes, schedule a restart after stabilisation"; const RESTART_AFTER_STABLE = "Yes, schedule a restart after stabilisation";
const RETRY_LATER = "No, Leave it to me"; const RETRY_LATER = "No, Leave it to me";
const ret = await this.core.confirm.askSelectStringDialogue(message || "Do you want to restart and reload Obsidian now?", [ const ret = await this.core.confirm.askSelectStringDialogue(
RESTART_AFTER_STABLE, message || "Do you want to restart and reload Obsidian now?",
RESTART_NOW, [RESTART_AFTER_STABLE, RESTART_NOW, RETRY_LATER],
RETRY_LATER], { defaultAction: RETRY_LATER }); { defaultAction: RETRY_LATER }
);
if (ret == RESTART_NOW) { if (ret == RESTART_NOW) {
this._performAppReload(); this._performAppReload();
} else if (ret == RESTART_AFTER_STABLE) { } else if (ret == RESTART_AFTER_STABLE) {
this.core.$$scheduleAppReload(); this.core.$$scheduleAppReload();
} }
}) });
} }
$$scheduleAppReload() { $$scheduleAppReload() {
if (!this.core._totalProcessingCount) { if (!this.core._totalProcessingCount) {
@@ -194,24 +202,39 @@ export class ModuleObsidianEvents extends AbstractObsidianModule implements IObs
const proc = this.core.processingFileEventCount.value; const proc = this.core.processingFileEventCount.value;
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const __ = __tick.value; const __ = __tick.value;
return dbCount + replicationCount + storageApplyingCount + chunkCount + pluginScanCount + hiddenFilesCount + conflictProcessCount + e + proc; return (
}) dbCount +
this.plugin.registerInterval(setInterval(() => { replicationCount +
__tick.value++; storageApplyingCount +
}, 1000) as unknown as number); chunkCount +
pluginScanCount +
hiddenFilesCount +
conflictProcessCount +
e +
proc
);
});
this.plugin.registerInterval(
setInterval(() => {
__tick.value++;
}, 1000) as unknown as number
);
let stableCheck = 3; let stableCheck = 3;
this.core._totalProcessingCount.onChanged(e => { this.core._totalProcessingCount.onChanged((e) => {
if (e.value == 0) { if (e.value == 0) {
if (stableCheck-- <= 0) { if (stableCheck-- <= 0) {
this._performAppReload(); this._performAppReload();
} }
this._log(`Obsidian will be restarted soon! (Within ${stableCheck} seconds)`, LOG_LEVEL_NOTICE, "restart-notice"); this._log(
`Obsidian will be restarted soon! (Within ${stableCheck} seconds)`,
LOG_LEVEL_NOTICE,
"restart-notice"
);
} else { } else {
stableCheck = 3; stableCheck = 3;
} }
}) });
} }
} }
} }

View File

@@ -2,11 +2,10 @@ import { fireAndForget } from "octagonal-wheels/promises";
import { addIcon, type Editor, type MarkdownFileInfo, type MarkdownView } from "../../deps.ts"; import { addIcon, type Editor, type MarkdownFileInfo, type MarkdownView } from "../../deps.ts";
import { LOG_LEVEL_NOTICE, type FilePathWithPrefix } from "../../lib/src/common/types.ts"; import { LOG_LEVEL_NOTICE, type FilePathWithPrefix } from "../../lib/src/common/types.ts";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { $msg } from "src/lib/src/common/i18n.ts";
export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsidianModule { export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsidianModule {
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
// UI // UI
addIcon( addIcon(
"replicate", "replicate",
@@ -18,23 +17,22 @@ export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsid
</g>` </g>`
); );
this.addRibbonIcon("replicate", "Replicate", async () => { this.addRibbonIcon("replicate", $msg("moduleObsidianMenu.replicate"), async () => {
await this.plugin.$$replicate(true); await this.core.$$replicate(true);
}).addClass("livesync-ribbon-replicate"); }).addClass("livesync-ribbon-replicate");
this.addCommand({ this.addCommand({
id: "livesync-replicate", id: "livesync-replicate",
name: "Replicate now", name: "Replicate now",
callback: async () => { callback: async () => {
await this.plugin.$$replicate(); await this.core.$$replicate();
}, },
}); });
this.addCommand({ this.addCommand({
id: "livesync-dump", id: "livesync-dump",
name: "Dump information of this doc ", name: "Dump information of this doc ",
callback: () => { callback: () => {
const file = this.plugin.$$getActiveFilePath(); const file = this.core.$$getActiveFilePath();
if (!file) return; if (!file) return;
fireAndForget(() => this.localDatabase.getDBEntry(file, {}, true, false)); fireAndForget(() => this.localDatabase.getDBEntry(file, {}, true, false));
}, },
@@ -45,7 +43,7 @@ export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsid
editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => {
const file = view.file; const file = view.file;
if (!file) return; if (!file) return;
void this.plugin.$$queueConflictCheckIfOpen(file.path as FilePathWithPrefix); void this.core.$$queueConflictCheckIfOpen(file.path as FilePathWithPrefix);
}, },
}); });
@@ -60,23 +58,23 @@ export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsid
this.settings.liveSync = true; this.settings.liveSync = true;
this._log("LiveSync Enabled.", LOG_LEVEL_NOTICE); this._log("LiveSync Enabled.", LOG_LEVEL_NOTICE);
} }
await this.plugin.realizeSettingSyncMode(); await this.core.$$realizeSettingSyncMode();
await this.plugin.saveSettings(); await this.core.$$saveSettingData();
}, },
}); });
this.addCommand({ this.addCommand({
id: "livesync-suspendall", id: "livesync-suspendall",
name: "Toggle All Sync.", name: "Toggle All Sync.",
callback: async () => { callback: async () => {
if (this.plugin.suspended) { if (this.core.$$isSuspended()) {
this.plugin.suspended = false; this.core.$$setSuspended(false);
this._log("Self-hosted LiveSync resumed", LOG_LEVEL_NOTICE); this._log("Self-hosted LiveSync resumed", LOG_LEVEL_NOTICE);
} else { } else {
this.plugin.suspended = true; this.core.$$setSuspended(true);
this._log("Self-hosted LiveSync suspended", LOG_LEVEL_NOTICE); this._log("Self-hosted LiveSync suspended", LOG_LEVEL_NOTICE);
} }
await this.plugin.realizeSettingSyncMode(); await this.core.$$realizeSettingSyncMode();
await this.plugin.saveSettings(); await this.core.$$saveSettingData();
}, },
}); });
@@ -84,9 +82,9 @@ export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsid
id: "livesync-scan-files", id: "livesync-scan-files",
name: "Scan storage and database again", name: "Scan storage and database again",
callback: async () => { callback: async () => {
await this.plugin.$$performFullScan(true) await this.core.$$performFullScan(true);
} },
}) });
this.addCommand({ this.addCommand({
id: "livesync-runbatch", id: "livesync-runbatch",
@@ -94,22 +92,20 @@ export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsid
callback: async () => { callback: async () => {
await this.core.$everyCommitPendingFileEvent(); await this.core.$everyCommitPendingFileEvent();
}, },
}) });
// TODO, Replicator is possibly one of features. It should be moved to features. // TODO, Replicator is possibly one of features. It should be moved to features.
this.addCommand({ this.addCommand({
id: "livesync-abortsync", id: "livesync-abortsync",
name: "Abort synchronization immediately", name: "Abort synchronization immediately",
callback: () => { callback: () => {
this.plugin.replicator.terminateSync(); this.core.replicator.terminateSync();
}, },
}) });
return Promise.resolve(true); return Promise.resolve(true);
} }
$everyOnload(): Promise<boolean> { $everyOnload(): Promise<boolean> {
this.app.workspace.onLayoutReady(this.plugin.onLiveSyncReady.bind(this.plugin)); this.app.workspace.onLayoutReady(this.core.$$onLiveSyncReady.bind(this.core));
// eslint-disable-next-line no-unused-labels
return Promise.resolve(true); return Promise.resolve(true);
} }
@@ -124,13 +120,10 @@ export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsid
await leaves[0].setViewState({ await leaves[0].setViewState({
type: viewType, type: viewType,
active: true, active: true,
}) });
} }
if (leaves.length > 0) { if (leaves.length > 0) {
this.app.workspace.revealLeaf( await this.app.workspace.revealLeaf(leaves[0]);
leaves[0]
);
} }
} }
} }

View File

@@ -0,0 +1,12 @@
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
export class ModuleExtraSyncObsidian extends AbstractObsidianModule implements IObsidianModule {
deviceAndVaultName: string = "";
$$getDeviceAndVaultName(): string {
return this.deviceAndVaultName;
}
$$setDeviceAndVaultName(name: string): void {
this.deviceAndVaultName = name;
}
}

View File

@@ -1,4 +1,4 @@
import { fireAndForget } from "octagonal-wheels/promises"; import { delay, fireAndForget } from "octagonal-wheels/promises";
import { __onMissingTranslation } from "../../lib/src/common/i18n"; import { __onMissingTranslation } from "../../lib/src/common/i18n";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
@@ -6,90 +6,134 @@ import { eventHub } from "../../common/events";
import { enableTestFunction } from "./devUtil/testUtils.ts"; import { enableTestFunction } from "./devUtil/testUtils.ts";
import { TestPaneView, VIEW_TYPE_TEST } from "./devUtil/TestPaneView.ts"; import { TestPaneView, VIEW_TYPE_TEST } from "./devUtil/TestPaneView.ts";
import { writable } from "svelte/store"; import { writable } from "svelte/store";
import type { FilePathWithPrefix } from "../../lib/src/common/types.ts";
export class ModuleDev extends AbstractObsidianModule implements IObsidianModule { export class ModuleDev extends AbstractObsidianModule implements IObsidianModule {
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
__onMissingTranslation(() => { }); __onMissingTranslation(() => {});
return Promise.resolve(true); return Promise.resolve(true);
} }
async onMissingTranslation(key: string): Promise<void> {
const now = new Date();
const filename = `missing-translation-`;
const time = now.toISOString().split("T")[0];
const outFile = `${filename}${time}.jsonl`;
const piece = JSON.stringify({
[key]: {},
});
const writePiece = piece.substring(1, piece.length - 1) + ",";
try {
await this.core.storageAccess.ensureDir(this.app.vault.configDir + "/ls-debug/");
await this.core.storageAccess.appendHiddenFile(
this.app.vault.configDir + "/ls-debug/" + outFile,
writePiece + "\n"
);
} catch (ex) {
this._log(`Could not write ${outFile}`, LOG_LEVEL_VERBOSE);
this._log(`Missing translation: ${writePiece}`, LOG_LEVEL_VERBOSE);
this._log(ex, LOG_LEVEL_VERBOSE);
}
}
$everyOnloadAfterLoadSettings(): Promise<boolean> { $everyOnloadAfterLoadSettings(): Promise<boolean> {
if (!this.settings.enableDebugTools) return Promise.resolve(true); if (!this.settings.enableDebugTools) return Promise.resolve(true);
// eslint-disable-next-line no-unused-labels this.onMissingTranslation = this.onMissingTranslation.bind(this);
__onMissingTranslation((key) => { __onMissingTranslation((key) => {
const now = new Date(); void this.onMissingTranslation(key);
const filename = `missing-translation-` });
const time = now.toISOString().split("T")[0];
const outFile = `${filename}${time}.jsonl`;
const piece = JSON.stringify(
{
[key]: {}
}
)
const writePiece = piece.substring(1, piece.length - 1) + ",";
fireAndForget(async () => {
try {
await this.core.storageAccess.ensureDir(this.app.vault.configDir + "/ls-debug/");
await this.core.storageAccess.appendHiddenFile(this.app.vault.configDir + "/ls-debug/" + outFile, writePiece + "\n")
} catch (ex) {
this._log(`Could not write ${outFile}`, LOG_LEVEL_VERBOSE);
this._log(`Missing translation: ${writePiece}`, LOG_LEVEL_VERBOSE);
this._log(ex, LOG_LEVEL_VERBOSE);
}
});
})
type STUB = { type STUB = {
toc: Set<string>, toc: Set<string>;
stub: { [key: string]: { [key: string]: Map<string, Record<string, string>> } } stub: { [key: string]: { [key: string]: Map<string, Record<string, string>> } };
}; };
eventHub.onEvent("document-stub-created", (e: CustomEvent<STUB>) => { eventHub.onEvent("document-stub-created", (detail: STUB) => {
fireAndForget(async () => { fireAndForget(async () => {
const stub = e.detail.stub; const stub = detail.stub;
const toc = e.detail.toc; const toc = detail.toc;
const stubDocX = const stubDocX = Object.entries(stub)
Object.entries(stub).map(([key, value]) => { .map(([key, value]) => {
return [`## ${key}`, Object.entries(value). return [
map(([key2, value2]) => { `## ${key}`,
return [`### ${key2}`, Object.entries(value)
([...(value2.entries())].map(([key3, value3]) => { .map(([key2, value2]) => {
// return `#### ${key3}` + "\n" + JSON.stringify(value3); return [
const isObsolete = value3["is_obsolete"] ? " (obsolete)" : ""; `### ${key2}`,
const desc = value3["desc"] ?? ""; [...value2.entries()].map(([key3, value3]) => {
const key = value3["key"] ? "Setting key: " + value3["key"] + "\n" : ""; // return `#### ${key3}` + "\n" + JSON.stringify(value3);
return `#### ${key3}${isObsolete}\n${key}${desc}\n` const isObsolete = value3["is_obsolete"] ? " (obsolete)" : "";
}))].flat() const desc = value3["desc"] ?? "";
}).flat()].flat() const key = value3["key"] ? "Setting key: " + value3["key"] + "\n" : "";
}).flat(); return `#### ${key3}${isObsolete}\n${key}${desc}\n`;
const stubDocMD = ` }),
].flat();
})
.flat(),
].flat();
})
.flat();
const stubDocMD =
`
| Icon | Description | | Icon | Description |
| :---: | ----------------------------------------------------------------- | | :---: | ----------------------------------------------------------------- |
` + ` +
[...toc.values()].map(e => `${e}`).join("\n") + "\n\n" + [...toc.values()].map((e) => `${e}`).join("\n") +
"\n\n" +
stubDocX.join("\n"); stubDocX.join("\n");
await this.core.storageAccess.writeHiddenFileAuto(this.app.vault.configDir + "/ls-debug/stub-doc.md", stubDocMD); await this.core.storageAccess.writeHiddenFileAuto(
}) this.app.vault.configDir + "/ls-debug/stub-doc.md",
stubDocMD
);
});
}); });
enableTestFunction(this.plugin); enableTestFunction(this.plugin);
this.registerView( this.registerView(VIEW_TYPE_TEST, (leaf) => new TestPaneView(leaf, this.plugin, this));
VIEW_TYPE_TEST,
(leaf) => new TestPaneView(leaf, this.plugin, this)
);
this.addCommand({ this.addCommand({
id: "view-test", id: "view-test",
name: "Open Test dialogue", name: "Open Test dialogue",
callback: () => { callback: () => {
void this.core.$$showView(VIEW_TYPE_TEST); void this.core.$$showView(VIEW_TYPE_TEST);
} },
}); });
return Promise.resolve(true); return Promise.resolve(true);
} }
async $everyOnLayoutReady(): Promise<boolean> { async $everyOnLayoutReady(): Promise<boolean> {
if (!this.settings.enableDebugTools) return Promise.resolve(true); if (!this.settings.enableDebugTools) return Promise.resolve(true);
if (await this.core.storageAccess.isExistsIncludeHidden("_SHOWDIALOGAUTO.md")) { // if (await this.core.storageAccess.isExistsIncludeHidden("_SHOWDIALOGAUTO.md")) {
void this.core.$$showView(VIEW_TYPE_TEST); // void this.core.$$showView(VIEW_TYPE_TEST);
} // }
this.addCommand({
id: "test-create-conflict",
name: "Create conflict",
callback: async () => {
const filename = "test-create-conflict.md";
const content = `# Test create conflict\n\n`;
const w = await this.core.databaseFileAccess.store({
name: filename as FilePathWithPrefix,
path: filename as FilePathWithPrefix,
body: new Blob([content], { type: "text/markdown" }),
stat: {
ctime: new Date().getTime(),
mtime: new Date().getTime(),
size: content.length,
type: "file",
},
});
if (w) {
const id = await this.core.$$path2id(filename as FilePathWithPrefix);
const f = await this.core.localDatabase.getRaw(id);
console.log(f);
console.log(f._rev);
const revConflict = f._rev.split("-")[0] + "-" + (parseInt(f._rev.split("-")[1]) + 1).toString();
console.log(await this.core.localDatabase.bulkDocsRaw([f], { new_edits: false }));
console.log(
await this.core.localDatabase.bulkDocsRaw([{ ...f, _rev: revConflict }], { new_edits: false })
);
}
},
});
await delay(1);
return true; return true;
} }
testResults = writable<[boolean, string, string][]>([]); testResults = writable<[boolean, string, string][]>([]);

View File

@@ -4,11 +4,10 @@ import { shareRunningResult } from "octagonal-wheels/concurrency/lock";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule";
export class ModuleIntegratedTest extends AbstractObsidianModule implements IObsidianModule { export class ModuleIntegratedTest extends AbstractObsidianModule implements IObsidianModule {
async waitFor(proc: () => Promise<boolean>, timeout = 10000): Promise<boolean> { async waitFor(proc: () => Promise<boolean>, timeout = 10000): Promise<boolean> {
await delay(100); await delay(100);
const start = Date.now(); const start = Date.now();
while (!await proc()) { while (!(await proc())) {
if (timeout > 0) { if (timeout > 0) {
if (Date.now() - start > timeout) { if (Date.now() - start > timeout) {
this._log(`Timeout`); this._log(`Timeout`);
@@ -40,25 +39,27 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
} }
} }
async assert(proc: () => Promise<boolean>): Promise<boolean> { async assert(proc: () => Promise<boolean>): Promise<boolean> {
if (!await proc()) { if (!(await proc())) {
this._log(`Assertion failed`); this._log(`Assertion failed`);
return false; return false;
} }
return true; return true;
} }
async _orDie(key: string, proc: () => Promise<boolean>): Promise<true> | never { async _orDie(key: string, proc: () => Promise<boolean>): Promise<true> | never {
if (!await this._test(key, proc)) { if (!(await this._test(key, proc))) {
throw new Error(`${key}`); throw new Error(`${key}`);
} }
return true; return true;
} }
tryReplicate() { tryReplicate() {
if (!this.settings.liveSync) { if (!this.settings.liveSync) {
return shareRunningResult("replicate-test", async () => { await this.core.$$replicate() }); return shareRunningResult("replicate-test", async () => {
await this.core.$$replicate();
});
} }
} }
async readStorageContent(file: FilePathWithPrefix): Promise<string | undefined> { async readStorageContent(file: FilePathWithPrefix): Promise<string | undefined> {
if (!await this.core.storageAccess.isExistsIncludeHidden(file)) { if (!(await this.core.storageAccess.isExistsIncludeHidden(file))) {
return undefined; return undefined;
} }
return await this.core.storageAccess.readHiddenFileText(file); return await this.core.storageAccess.readHiddenFileText(file);
@@ -70,13 +71,14 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
await this.core.$anyResolveConflictByNewest(stepFile); await this.core.$anyResolveConflictByNewest(stepFile);
await this.core.storageAccess.writeFileAuto(stepFile, stepContent); await this.core.storageAccess.writeFileAuto(stepFile, stepContent);
await this._orDie(`Wait for acknowledge ${no}`, async () => { await this._orDie(`Wait for acknowledge ${no}`, async () => {
if (!await this.waitWithReplicating( if (
async () => { !(await this.waitWithReplicating(async () => {
return await this.storageContentIsEqual(stepAckFile, stepContent) return await this.storageContentIsEqual(stepAckFile, stepContent);
}, 20000) }, 20000))
) return false; )
return false;
return true; return true;
}) });
return true; return true;
} }
async _join(no: number, title: string): Promise<boolean> { async _join(no: number, title: string): Promise<boolean> {
@@ -86,14 +88,14 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
const stepContent = `Step ${no}`; const stepContent = `Step ${no}`;
await this._orDie(`Wait for step ${no} (${title})`, async () => { await this._orDie(`Wait for step ${no} (${title})`, async () => {
if (!await this.waitWithReplicating( if (
async () => { !(await this.waitWithReplicating(async () => {
return await this.storageContentIsEqual(stepFile, stepContent) return await this.storageContentIsEqual(stepFile, stepContent);
}, 20000) }, 20000))
) return false; )
return false;
return true; return true;
} });
)
await this.core.$anyResolveConflictByNewest(stepAckFile); await this.core.$anyResolveConflictByNewest(stepAckFile);
await this.core.storageAccess.writeFileAuto(stepAckFile, stepContent); await this.core.storageAccess.writeFileAuto(stepAckFile, stepContent);
await this.tryReplicate(); await this.tryReplicate();
@@ -105,13 +107,13 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
title, title,
isGameChanger, isGameChanger,
proc, proc,
check check,
}: { }: {
step: number, step: number;
title: string, title: string;
isGameChanger: boolean, isGameChanger: boolean;
proc: () => Promise<any>, proc: () => Promise<any>;
check: () => Promise<boolean>, check: () => Promise<boolean>;
}): Promise<boolean> { }): Promise<boolean> {
if (isGameChanger) { if (isGameChanger) {
await this._proceed(step, title); await this._proceed(step, title);
@@ -121,9 +123,7 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
this._log(`Error: ${e}`); this._log(`Error: ${e}`);
return false; return false;
} }
return await this._orDie(`Step ${step} - ${title}`, return await this._orDie(`Step ${step} - ${title}`, async () => await this.waitWithReplicating(check));
async () => await this.waitWithReplicating(check)
);
} else { } else {
return await this._join(step, title); return await this._join(step, title);
} }
@@ -135,14 +135,21 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
// async testReceiver(testMain: (testFileName: FilePathWithPrefix) => Promise<boolean>): Promise<boolean> { // async testReceiver(testMain: (testFileName: FilePathWithPrefix) => Promise<boolean>): Promise<boolean> {
// } // }
async nonLiveTestRunner(isLeader: boolean, testMain: (testFileName: FilePathWithPrefix, isLeader: boolean) => Promise<boolean>): Promise<boolean> { async nonLiveTestRunner(
isLeader: boolean,
testMain: (testFileName: FilePathWithPrefix, isLeader: boolean) => Promise<boolean>
): Promise<boolean> {
const storage = this.core.storageAccess; const storage = this.core.storageAccess;
// const database = this.core.databaseFileAccess; // const database = this.core.databaseFileAccess;
// const _orDie = this._orDie.bind(this); // const _orDie = this._orDie.bind(this);
const testCommandFile = "IT.md" as FilePathWithPrefix; const testCommandFile = "IT.md" as FilePathWithPrefix;
const textCommandResponseFile = "ITx.md" as FilePathWithPrefix; const textCommandResponseFile = "ITx.md" as FilePathWithPrefix;
let testFileName: FilePathWithPrefix; let testFileName: FilePathWithPrefix;
this.addTestResult("-------Starting ... ", true, `Test as ${isLeader ? "Leader" : "Receiver"} command file ${testCommandFile}`); this.addTestResult(
"-------Starting ... ",
true,
`Test as ${isLeader ? "Leader" : "Receiver"} command file ${testCommandFile}`
);
if (isLeader) { if (isLeader) {
await this._proceed(0, "start"); await this._proceed(0, "start");
} }
@@ -154,14 +161,14 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => await storage.removeHidden(testCommandFile), proc: async () => await storage.removeHidden(testCommandFile),
check: async () => !(await storage.isExistsIncludeHidden(testCommandFile)), check: async () => !(await storage.isExistsIncludeHidden(testCommandFile)),
}) });
await this.performStep({ await this.performStep({
step: 1, step: 1,
title: "Make sure that command File Not Exists On Receiver", title: "Make sure that command File Not Exists On Receiver",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => await storage.removeHidden(textCommandResponseFile), proc: async () => await storage.removeHidden(textCommandResponseFile),
check: async () => !(await storage.isExistsIncludeHidden(textCommandResponseFile)), check: async () => !(await storage.isExistsIncludeHidden(textCommandResponseFile)),
}) });
await this.performStep({ await this.performStep({
step: 2, step: 2,
@@ -173,14 +180,14 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
await storage.writeFileAuto(testCommandFile, testFileName); await storage.writeFileAuto(testCommandFile, testFileName);
}, },
check: () => Promise.resolve(true), check: () => Promise.resolve(true),
}) });
await this.performStep({ await this.performStep({
step: 3, step: 3,
title: "Wait for the command file to be arrived", title: "Wait for the command file to be arrived",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: async () => await storage.isExistsIncludeHidden(testCommandFile), check: async () => await storage.isExistsIncludeHidden(testCommandFile),
}) });
await this.performStep({ await this.performStep({
step: 4, step: 4,
@@ -190,34 +197,31 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
await storage.writeHiddenFileAuto(textCommandResponseFile, "!"); await storage.writeHiddenFileAuto(textCommandResponseFile, "!");
}, },
check: () => Promise.resolve(true), check: () => Promise.resolve(true),
}) });
await this.performStep({ await this.performStep({
step: 5, step: 5,
title: "Wait for the response file to be arrived", title: "Wait for the response file to be arrived",
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => { }, proc: async () => {},
check: async () => await storage.isExistsIncludeHidden(textCommandResponseFile), check: async () => await storage.isExistsIncludeHidden(textCommandResponseFile),
}) });
await this.performStep({ await this.performStep({
step: 6, step: 6,
title: "Proceed to begin the test", title: "Proceed to begin the test",
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => { proc: async () => {},
},
check: () => Promise.resolve(true), check: () => Promise.resolve(true),
}); });
await this.performStep({ await this.performStep({
step: 6, step: 6,
title: "Begin the test", title: "Begin the test",
isGameChanger: !false, isGameChanger: !false,
proc: async () => { proc: async () => {},
},
check: () => { check: () => {
return Promise.resolve(true); return Promise.resolve(true);
}, },
}) });
// await this.step(0, isLeader, true); // await this.step(0, isLeader, true);
try { try {
this.addTestResult("** Main------", true, ``); this.addTestResult("** Main------", true, ``);
@@ -237,12 +241,15 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
// Make sure the // Make sure the
} }
async testBasic(filename: FilePathWithPrefix, isLeader: boolean): Promise<boolean> { async testBasic(filename: FilePathWithPrefix, isLeader: boolean): Promise<boolean> {
const storage = this.core.storageAccess; const storage = this.core.storageAccess;
const database = this.core.databaseFileAccess; const database = this.core.databaseFileAccess;
await this.addTestResult(`---**Starting Basic Test**---`, true, `Test as ${isLeader ? "Leader" : "Receiver"} command file ${filename}`); await this.addTestResult(
`---**Starting Basic Test**---`,
true,
`Test as ${isLeader ? "Leader" : "Receiver"} command file ${filename}`
);
// if (isLeader) { // if (isLeader) {
// await this._proceed(0); // await this._proceed(0);
// } // }
@@ -252,10 +259,9 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
step: 0, step: 0,
title: "Make sure that file is not exist", title: "Make sure that file is not exist",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: async () => !(await storage.isExists(filename)), check: async () => !(await storage.isExists(filename)),
}) });
await this.performStep({ await this.performStep({
step: 1, step: 1,
@@ -263,47 +269,47 @@ export class ModuleIntegratedTest extends AbstractObsidianModule implements IObs
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => await storage.writeFileAuto(filename, "Hello World"), proc: async () => await storage.writeFileAuto(filename, "Hello World"),
check: async () => await storage.isExists(filename), check: async () => await storage.isExists(filename),
}) });
await this.performStep({ await this.performStep({
step: 2, step: 2,
title: "Make sure the file is arrived", title: "Make sure the file is arrived",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: async () => await storage.isExists(filename), check: async () => await storage.isExists(filename),
}) });
await this.performStep({ await this.performStep({
step: 3, step: 3,
title: "Update to Hello World 2", title: "Update to Hello World 2",
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => await storage.writeFileAuto(filename, "Hello World 2"), proc: async () => await storage.writeFileAuto(filename, "Hello World 2"),
check: async () => await this.storageContentIsEqual(filename, "Hello World 2"), check: async () => await this.storageContentIsEqual(filename, "Hello World 2"),
}) });
await this.performStep({ await this.performStep({
step: 4, step: 4,
title: "Make sure the modified file is arrived", title: "Make sure the modified file is arrived",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: async () => await this.storageContentIsEqual(filename, "Hello World 2"), check: async () => await this.storageContentIsEqual(filename, "Hello World 2"),
}) });
await this.performStep({ await this.performStep({
step: 5, step: 5,
title: "Update to Hello World 3", title: "Update to Hello World 3",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => await storage.writeFileAuto(filename, "Hello World 3"), proc: async () => await storage.writeFileAuto(filename, "Hello World 3"),
check: async () => await this.storageContentIsEqual(filename, "Hello World 3"), check: async () => await this.storageContentIsEqual(filename, "Hello World 3"),
}) });
await this.performStep({ await this.performStep({
step: 6, step: 6,
title: "Make sure the modified file is arrived", title: "Make sure the modified file is arrived",
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => { }, proc: async () => {},
check: async () => await this.storageContentIsEqual(filename, "Hello World 3"), check: async () => await this.storageContentIsEqual(filename, "Hello World 3"),
}) });
const multiLineContent = `Line1:A const multiLineContent = `Line1:A
Line2:B Line2:B
Line3:C Line3:C
Line4:D` Line4:D`;
await this.performStep({ await this.performStep({
step: 7, step: 7,
@@ -311,38 +317,35 @@ Line4:D`
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => await storage.writeFileAuto(filename, multiLineContent), proc: async () => await storage.writeFileAuto(filename, multiLineContent),
check: async () => await this.storageContentIsEqual(filename, multiLineContent), check: async () => await this.storageContentIsEqual(filename, multiLineContent),
}) });
await this.performStep({ await this.performStep({
step: 8, step: 8,
title: "Make sure the modified file is arrived", title: "Make sure the modified file is arrived",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: async () => await this.storageContentIsEqual(filename, multiLineContent), check: async () => await this.storageContentIsEqual(filename, multiLineContent),
}) });
// While LiveSync, possibly cannot cause the conflict. // While LiveSync, possibly cannot cause the conflict.
if (!this.settings.liveSync) { if (!this.settings.liveSync) {
// Step 9 Make Conflict But Resolvable // Step 9 Make Conflict But Resolvable
const multiLineContentL = `Line1:A const multiLineContentL = `Line1:A
Line2:B Line2:B
Line3:C! Line3:C!
Line4:D` Line4:D`;
const multiLineContentC = `Line1:A const multiLineContentC = `Line1:A
Line2:bbbbb Line2:bbbbb
Line3:C Line3:C
Line4:D` Line4:D`;
await this.performStep({ await this.performStep({
step: 9, step: 9,
title: "Progress to be conflicted", title: "Progress to be conflicted",
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => { }, proc: async () => {},
check: () => Promise.resolve(true), check: () => Promise.resolve(true),
}) });
await storage.writeFileAuto(filename, isLeader ? multiLineContentL : multiLineContentC); await storage.writeFileAuto(filename, isLeader ? multiLineContentL : multiLineContentC);
@@ -350,62 +353,62 @@ Line4:D`
step: 10, step: 10,
title: "Update As Conflicted", title: "Update As Conflicted",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: () => Promise.resolve(true), check: () => Promise.resolve(true),
}) });
await this.performStep({ await this.performStep({
step: 10, step: 10,
title: "Make sure Automatically resolved", title: "Make sure Automatically resolved",
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => { }, proc: async () => {},
check: async () => (await database.getConflictedRevs(filename)).length === 0, check: async () => (await database.getConflictedRevs(filename)).length === 0,
}) });
await this.performStep({ await this.performStep({
step: 11, step: 11,
title: "Make sure Automatically resolved", title: "Make sure Automatically resolved",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: async () => (await database.getConflictedRevs(filename)).length === 0, check: async () => (await database.getConflictedRevs(filename)).length === 0,
}) });
const sensiblyMergedContent = `Line1:A const sensiblyMergedContent = `Line1:A
Line2:bbbbb Line2:bbbbb
Line3:C! Line3:C!
Line4:D` Line4:D`;
await this.performStep({ await this.performStep({
step: 12, step: 12,
title: "Make sure Sensibly Merged on Leader", title: "Make sure Sensibly Merged on Leader",
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => { }, proc: async () => {},
check: async () => await this.storageContentIsEqual(filename, sensiblyMergedContent), check: async () => await this.storageContentIsEqual(filename, sensiblyMergedContent),
}) });
await this.performStep({ await this.performStep({
step: 13, step: 13,
title: "Make sure Sensibly Merged on Receiver", title: "Make sure Sensibly Merged on Receiver",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: async () => await this.storageContentIsEqual(filename, sensiblyMergedContent), check: async () => await this.storageContentIsEqual(filename, sensiblyMergedContent),
}) });
} }
await this.performStep({ await this.performStep({
step: 14, step: 14,
title: "Delete File", title: "Delete File",
isGameChanger: isLeader, isGameChanger: isLeader,
proc: async () => { await storage.removeHidden(filename) }, proc: async () => {
check: async () => !await storage.isExists(filename), await storage.removeHidden(filename);
}) },
check: async () => !(await storage.isExists(filename)),
});
await this.performStep({ await this.performStep({
step: 15, step: 15,
title: "Make sure File is deleted", title: "Make sure File is deleted",
isGameChanger: !isLeader, isGameChanger: !isLeader,
proc: async () => { }, proc: async () => {},
check: async () => !await storage.isExists(filename), check: async () => !(await storage.isExists(filename)),
}) });
this._log(`The Basic Test has been completed`, LOG_LEVEL_NOTICE); this._log(`The Basic Test has been completed`, LOG_LEVEL_NOTICE);
return true; return true;
} }
@@ -429,9 +432,8 @@ Line4:D`
this._log(`Starting Test`); this._log(`Starting Test`);
await this.testBasicEvent(isLeader); await this.testBasicEvent(isLeader);
if (this.settings.remoteType == REMOTE_MINIO) await this.testBasicLive(isLeader); if (this.settings.remoteType == REMOTE_MINIO) await this.testBasicLive(isLeader);
} catch (e) { } catch (e) {
this._log(e) this._log(e);
this._log(`Error: ${e}`); this._log(`Error: ${e}`);
return Promise.resolve(false); return Promise.resolve(false);
} }

View File

@@ -2,15 +2,20 @@ import { delay } from "octagonal-wheels/promises";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "octagonal-wheels/common/logger";
import { eventHub } from "../../common/events"; import { eventHub } from "../../common/events";
import { webcrypto } from "crypto"; import { getWebCrypto } from "../../lib/src/mods.ts";
import { uint8ArrayToHexString } from "octagonal-wheels/binary/hex"; import { uint8ArrayToHexString } from "octagonal-wheels/binary/hex";
import { parseYaml, requestUrl, stringifyYaml } from "obsidian"; import { parseYaml, requestUrl, stringifyYaml } from "obsidian";
import type { FilePath } from "../../lib/src/common/types.ts"; import type { FilePath } from "../../lib/src/common/types.ts";
import { scheduleTask } from "octagonal-wheels/concurrency/task"; import { scheduleTask } from "octagonal-wheels/concurrency/task";
import { getFileRegExp } from "../../lib/src/common/utils.ts";
declare global {
interface LSEvents {
"debug-sync-status": string[];
}
}
export class ModuleReplicateTest extends AbstractObsidianModule implements IObsidianModule { export class ModuleReplicateTest extends AbstractObsidianModule implements IObsidianModule {
testRootPath = "_test/"; testRootPath = "_test/";
testInfoPath = "_testinfo/"; testInfoPath = "_testinfo/";
@@ -18,7 +23,6 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
return this.core.$$getVaultName().indexOf("dev") >= 0 && this.core.$$vaultName().indexOf("recv") < 0; return this.core.$$getVaultName().indexOf("dev") >= 0 && this.core.$$vaultName().indexOf("recv") < 0;
} }
get nameByKind() { get nameByKind() {
if (!this.isLeader) { if (!this.isLeader) {
return "RECV"; return "RECV";
@@ -45,7 +49,6 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
} }
} }
async dumpList() { async dumpList() {
if (this.settings.syncInternalFiles) { if (this.settings.syncInternalFiles) {
this._log("Write file list (Include Hidden)"); this._log("Write file list (Include Hidden)");
@@ -69,39 +72,38 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
void this._dumpFileList("files.md").finally(() => { void this._dumpFileList("files.md").finally(() => {
void this.refreshSyncStatus(); void this.refreshSyncStatus();
}); });
},
} });
})
this.addCommand({ this.addCommand({
id: "dump-file-structure-ih", id: "dump-file-structure-ih",
name: "Dump Structure (Include Hidden)", name: "Dump Structure (Include Hidden)",
callback: () => { callback: () => {
const d = "files.md"; const d = "files.md";
void this._dumpFileListIncludeHidden(d); void this._dumpFileListIncludeHidden(d);
} },
}) });
this.addCommand({ this.addCommand({
id: "dump-file-structure-auto", id: "dump-file-structure-auto",
name: "Dump Structure", name: "Dump Structure",
callback: () => { callback: () => {
void this.dumpList(); void this.dumpList();
} },
}) });
this.addCommand({ this.addCommand({
id: "dump-file-test", id: "dump-file-test",
name: `Perform Test (Dev) ${this.isLeader ? "(Leader)" : "(Recv)"}`, name: `Perform Test (Dev) ${this.isLeader ? "(Leader)" : "(Recv)"}`,
callback: () => { callback: () => {
void this.performTestManually(); void this.performTestManually();
} },
}) });
this.addCommand({ this.addCommand({
id: "watch-sync-result", id: "watch-sync-result",
name: `Watch sync result is matched between devices`, name: `Watch sync result is matched between devices`,
callback: () => { callback: () => {
this.watchIsSynchronised = !this.watchIsSynchronised; this.watchIsSynchronised = !this.watchIsSynchronised;
void this.refreshSyncStatus(); void this.refreshSyncStatus();
} },
}) });
this.app.vault.on("modify", async (file) => { this.app.vault.on("modify", async (file) => {
if (file.path.startsWith(this.testInfoPath)) { if (file.path.startsWith(this.testInfoPath)) {
await this.refreshSyncStatus(); await this.refreshSyncStatus();
@@ -111,7 +113,7 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
return true; return true;
}); });
} }
}) });
this.statusBarSyncStatus = this.plugin.addStatusBarItem(); this.statusBarSyncStatus = this.plugin.addStatusBarItem();
return Promise.resolve(true); return Promise.resolve(true);
} }
@@ -158,12 +160,16 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
} }
} }
async _dumpFileList(outFile?: string) { async _dumpFileList(outFile?: string) {
if (!this.core || !this.core.storageAccess) {
this._log("No storage access", LOG_LEVEL_INFO);
return;
}
const files = this.core.storageAccess.getFiles(); const files = this.core.storageAccess.getFiles();
const out = [] as any[]; const out = [] as any[];
const webcrypto = await getWebCrypto();
for (const file of files) { for (const file of files) {
if (!await this.core.$$isTargetFile(file.path)) { if (!(await this.core.$$isTargetFile(file.path))) {
continue; continue;
} }
if (file.path.startsWith(this.testInfoPath)) continue; if (file.path.startsWith(this.testInfoPath)) continue;
@@ -177,8 +183,8 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
name: file.name, name: file.name,
size: stat.size, size: stat.size,
mtime: stat.mtime, mtime: stat.mtime,
hash: hashStr hash: hashStr,
} };
// const fileLine = `-${file.path}:${stat.size}:${stat.mtime}:${hashStr}`; // const fileLine = `-${file.path}:${stat.size}:${stat.mtime}:${hashStr}`;
out.push(item); out.push(item);
} }
@@ -195,12 +201,12 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
} }
async _dumpFileListIncludeHidden(outFile?: string) { async _dumpFileListIncludeHidden(outFile?: string) {
const ignorePatterns = this.settings.syncInternalFilesIgnorePatterns const ignorePatterns = getFileRegExp(this.plugin.settings, "syncInternalFilesIgnorePatterns");
.replace(/\n| /g, "") const targetPatterns = getFileRegExp(this.plugin.settings, "syncInternalFilesTargetPatterns");
.split(",").filter(e => e).map(e => new RegExp(e, "i"));
const out = [] as any[]; const out = [] as any[];
const files = await this.core.storageAccess.getFilesIncludeHidden("", undefined, ignorePatterns); const files = await this.core.storageAccess.getFilesIncludeHidden("", targetPatterns, ignorePatterns);
console.dir(files); // console.dir(files);
const webcrypto = await getWebCrypto();
for (const file of files) { for (const file of files) {
// if (!await this.core.$$isTargetFile(file)) { // if (!await this.core.$$isTargetFile(file)) {
// continue; // continue;
@@ -216,8 +222,8 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
name: file.split("/").pop(), name: file.split("/").pop(),
size: stat.size, size: stat.size,
mtime: stat.mtime, mtime: stat.mtime,
hash: hashStr hash: hashStr,
} };
// const fileLine = `-${file.path}:${stat.size}:${stat.mtime}:${hashStr}`; // const fileLine = `-${file.path}:${stat.size}:${stat.mtime}:${hashStr}`;
out.push(item); out.push(item);
} }
@@ -257,27 +263,27 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
"docs/tech_info.md", "docs/tech_info.md",
"docs/terms.md", "docs/terms.md",
"docs/troubleshooting.md", "docs/troubleshooting.md",
'images/1.png', "images/1.png",
'images/2.png', "images/2.png",
'images/corrupted_data.png', "images/corrupted_data.png",
'images/hatch.png', "images/hatch.png",
'images/lock_pattern1.png', "images/lock_pattern1.png",
'images/lock_pattern2.png', "images/lock_pattern2.png",
'images/quick_setup_1.png', "images/quick_setup_1.png",
'images/quick_setup_2.png', "images/quick_setup_2.png",
'images/quick_setup_3.png', "images/quick_setup_3.png",
'images/quick_setup_3b.png', "images/quick_setup_3b.png",
'images/quick_setup_4.png', "images/quick_setup_4.png",
'images/quick_setup_5.png', "images/quick_setup_5.png",
'images/quick_setup_6.png', "images/quick_setup_6.png",
'images/quick_setup_7.png', "images/quick_setup_7.png",
'images/quick_setup_8.png', "images/quick_setup_8.png",
'images/quick_setup_9_1.png', "images/quick_setup_9_1.png",
'images/quick_setup_9_2.png', "images/quick_setup_9_2.png",
'images/quick_setup_10.png', "images/quick_setup_10.png",
'images/remote_db_setting.png', "images/remote_db_setting.png",
'images/write_logs_into_the_file.png', "images/write_logs_into_the_file.png",
] ];
for (const file of files) { for (const file of files) {
const remote = remoteTopDir + file; const remote = remoteTopDir + file;
const local = this.testRootPath + file; const local = this.testRootPath + file;
@@ -297,7 +303,7 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
async waitFor(proc: () => Promise<boolean>, timeout = 10000): Promise<boolean> { async waitFor(proc: () => Promise<boolean>, timeout = 10000): Promise<boolean> {
await delay(100); await delay(100);
const start = Date.now(); const start = Date.now();
while (!await proc()) { while (!(await proc())) {
if (timeout > 0) { if (timeout > 0) {
if (Date.now() - start > timeout) { if (Date.now() - start > timeout) {
this._log(`Timeout`); this._log(`Timeout`);
@@ -310,7 +316,6 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
} }
async testConflictedManually1() { async testConflictedManually1() {
await this.core.$$replicate(); await this.core.$$replicate();
const commonFile = `Resolve! const commonFile = `Resolve!
@@ -322,18 +327,20 @@ export class ModuleReplicateTest extends AbstractObsidianModule implements IObsi
await this.core.$$replicate(); await this.core.$$replicate();
await this.core.$$replicate(); await this.core.$$replicate();
if (await this.core.confirm.askYesNoDialog("Ready to begin the test conflict Manually 1?", { timeout: 30, defaultOption: "Yes" }) == "no") { if (
(await this.core.confirm.askYesNoDialog("Ready to begin the test conflict Manually 1?", {
timeout: 30,
defaultOption: "Yes",
})) == "no"
) {
return; return;
} }
const fileA = `Resolve to KEEP THIS const fileA = `Resolve to KEEP THIS
Willy Wonka, Willy Wonka, the amazing chocolatier!!` Willy Wonka, Willy Wonka, the amazing chocolatier!!`;
const fileB = `Resolve to DISCARD THIS const fileB = `Resolve to DISCARD THIS
Charlie Bucket, Charlie Bucket, the amazing chocolatier!!` Charlie Bucket, Charlie Bucket, the amazing chocolatier!!`;
if (this.isLeader) { if (this.isLeader) {
await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "wonka.md", fileA); await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "wonka.md", fileA);
@@ -341,25 +348,37 @@ Charlie Bucket, Charlie Bucket, the amazing chocolatier!!`
await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "wonka.md", fileB); await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "wonka.md", fileB);
} }
if (await this.core.confirm.askYesNoDialog("Ready to check the result of Manually 1?", { timeout: 30, defaultOption: "Yes" }) == "no") { if (
(await this.core.confirm.askYesNoDialog("Ready to check the result of Manually 1?", {
timeout: 30,
defaultOption: "Yes",
})) == "no"
) {
return; return;
} }
await this.core.$$replicate(); await this.core.$$replicate();
await this.core.$$replicate(); await this.core.$$replicate();
if (
if (!await this.waitFor(async () => { !(await this.waitFor(async () => {
await this.core.$$replicate(); await this.core.$$replicate();
return await this.__assertStorageContent(this.testRootPath + "wonka.md" as FilePath, fileA, false, true) == true; return (
}, 30000)) { (await this.__assertStorageContent(
return await this.__assertStorageContent(this.testRootPath + "wonka.md" as FilePath, fileA, false, true); (this.testRootPath + "wonka.md") as FilePath,
fileA,
false,
true
)) == true
);
}, 30000))
) {
return await this.__assertStorageContent((this.testRootPath + "wonka.md") as FilePath, fileA, false, true);
} }
return true; return true;
// We have to check the result // We have to check the result
} }
async testConflictedManually2() { async testConflictedManually2() {
await this.core.$$replicate(); await this.core.$$replicate();
const commonFile = `Resolve To concatenate const commonFile = `Resolve To concatenate
@@ -371,56 +390,82 @@ ABCDEFG`;
await this.core.$$replicate(); await this.core.$$replicate();
await this.core.$$replicate(); await this.core.$$replicate();
if (await this.core.confirm.askYesNoDialog("Ready to begin the test conflict Manually 2?", { timeout: 30, defaultOption: "Yes" }) == "no") { if (
(await this.core.confirm.askYesNoDialog("Ready to begin the test conflict Manually 2?", {
timeout: 30,
defaultOption: "Yes",
})) == "no"
) {
return; return;
} }
const fileA = `Resolve to Concatenate const fileA = `Resolve to Concatenate
ABCDEFGHIJKLMNOPQRSTYZ` ABCDEFGHIJKLMNOPQRSTYZ`;
const fileB = `Resolve to Concatenate const fileB = `Resolve to Concatenate
AJKLMNOPQRSTUVWXYZ` AJKLMNOPQRSTUVWXYZ`;
const concatenated = `Resolve to Concatenate const concatenated = `Resolve to Concatenate
ABCDEFGHIJKLMNOPQRSTUVWXYZ` ABCDEFGHIJKLMNOPQRSTUVWXYZ`;
if (this.isLeader) { if (this.isLeader) {
await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "concat.md", fileA); await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "concat.md", fileA);
} else { } else {
await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "concat.md", fileB); await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "concat.md", fileB);
} }
if (await this.core.confirm.askYesNoDialog("Ready to test conflict Manually 2?", { timeout: 30, defaultOption: "Yes" }) == "no") { if (
(await this.core.confirm.askYesNoDialog("Ready to test conflict Manually 2?", {
timeout: 30,
defaultOption: "Yes",
})) == "no"
) {
return; return;
} }
await this.core.$$replicate(); await this.core.$$replicate();
await this.core.$$replicate(); await this.core.$$replicate();
if (
if (!await this.waitFor(async () => { !(await this.waitFor(async () => {
await this.core.$$replicate(); await this.core.$$replicate();
return await this.__assertStorageContent(this.testRootPath + "concat.md" as FilePath, concatenated, false, true) == true; return (
}, 30000)) { (await this.__assertStorageContent(
return await this.__assertStorageContent(this.testRootPath + "concat.md" as FilePath, concatenated, false, true); (this.testRootPath + "concat.md") as FilePath,
concatenated,
false,
true
)) == true
);
}, 30000))
) {
return await this.__assertStorageContent(
(this.testRootPath + "concat.md") as FilePath,
concatenated,
false,
true
);
} }
return true; return true;
} }
async testConflictAutomatic() { async testConflictAutomatic() {
if (this.isLeader) { if (this.isLeader) {
const baseDoc = `Tasks! const baseDoc = `Tasks!
- [ ] Task 1 - [ ] Task 1
- [ ] Task 2 - [ ] Task 2
- [ ] Task 3 - [ ] Task 3
- [ ] Task 4 - [ ] Task 4
` `;
await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "task.md", baseDoc); await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "task.md", baseDoc);
} }
await delay(100) await delay(100);
await this.core.$$replicate(); await this.core.$$replicate();
await this.core.$$replicate(); await this.core.$$replicate();
if (await this.core.confirm.askYesNoDialog("Ready to test conflict?", { timeout: 30, defaultOption: "Yes" }) == "no") { if (
(await this.core.confirm.askYesNoDialog("Ready to test conflict?", {
timeout: 30,
defaultOption: "Yes",
})) == "no"
) {
return; return;
} }
const mod1Doc = `Tasks! const mod1Doc = `Tasks!
@@ -428,14 +473,14 @@ ABCDEFGHIJKLMNOPQRSTUVWXYZ`
- [v] Task 2 - [v] Task 2
- [ ] Task 3 - [ ] Task 3
- [ ] Task 4 - [ ] Task 4
` `;
const mod2Doc = `Tasks! const mod2Doc = `Tasks!
- [ ] Task 1 - [ ] Task 1
- [ ] Task 2 - [ ] Task 2
- [v] Task 3 - [v] Task 3
- [ ] Task 4 - [ ] Task 4
` `;
if (this.isLeader) { if (this.isLeader) {
await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "task.md", mod1Doc); await this.core.storageAccess.writeHiddenFileAuto(this.testRootPath + "task.md", mod1Doc);
} else { } else {
@@ -445,7 +490,10 @@ ABCDEFGHIJKLMNOPQRSTUVWXYZ`
await this.core.$$replicate(); await this.core.$$replicate();
await this.core.$$replicate(); await this.core.$$replicate();
await delay(1000); await delay(1000);
if (await this.core.confirm.askYesNoDialog("Ready to check result?", { timeout: 30, defaultOption: "Yes" }) == "no") { if (
(await this.core.confirm.askYesNoDialog("Ready to check result?", { timeout: 30, defaultOption: "Yes" })) ==
"no"
) {
return; return;
} }
await this.core.$$replicate(); await this.core.$$replicate();
@@ -455,8 +503,8 @@ ABCDEFGHIJKLMNOPQRSTUVWXYZ`
- [v] Task 2 - [v] Task 2
- [v] Task 3 - [v] Task 3
- [ ] Task 4 - [ ] Task 4
` `;
return this.__assertStorageContent(this.testRootPath + "task.md" as FilePath, mergedDoc, false, true); return this.__assertStorageContent((this.testRootPath + "task.md") as FilePath, mergedDoc, false, true);
} }
async checkConflictResolution() { async checkConflictResolution() {
@@ -465,24 +513,27 @@ ABCDEFGHIJKLMNOPQRSTUVWXYZ`
await this.core.rebuilder.resolveAllConflictedFilesByNewerOnes(); await this.core.rebuilder.resolveAllConflictedFilesByNewerOnes();
await this.core.$$replicate(); await this.core.$$replicate();
await delay(1000); await delay(1000);
if (!await this.testConflictAutomatic()) { if (!(await this.testConflictAutomatic())) {
this._log("Conflict resolution (Auto) failed", LOG_LEVEL_NOTICE); this._log("Conflict resolution (Auto) failed", LOG_LEVEL_NOTICE);
return false; return false;
} }
if (!await this.testConflictedManually1()) { if (!(await this.testConflictedManually1())) {
this._log("Conflict resolution (Manual1) failed", LOG_LEVEL_NOTICE); this._log("Conflict resolution (Manual1) failed", LOG_LEVEL_NOTICE);
return false; return false;
} }
if (!await this.testConflictedManually2()) { if (!(await this.testConflictedManually2())) {
this._log("Conflict resolution (Manual2) failed", LOG_LEVEL_NOTICE); this._log("Conflict resolution (Manual2) failed", LOG_LEVEL_NOTICE);
return false; return false;
} }
return true; return true;
} }
async __assertStorageContent(
async __assertStorageContent(fileName: FilePath, content: string, inverted = false, showResult = false): Promise<boolean | string> { fileName: FilePath,
content: string,
inverted = false,
showResult = false
): Promise<boolean | string> {
try { try {
const fileContent = await this.core.storageAccess.readHiddenFileText(fileName); const fileContent = await this.core.storageAccess.readHiddenFileText(fileName);
let result = fileContent === content; let result = fileContent === content;

View File

@@ -41,7 +41,7 @@
isReady = true; isReady = true;
// performTest(); // performTest();
eventHub.once(EVENT_LAYOUT_READY, async () => { eventHub.onceEvent(EVENT_LAYOUT_READY, async () => {
if (await plugin.storageAccess.isExistsIncludeHidden("_AUTO_TEST.md")) { if (await plugin.storageAccess.isExistsIncludeHidden("_AUTO_TEST.md")) {
new Notice("Auto test file found, running tests..."); new Notice("Auto test file found, running tests...");
fireAndForget(async () => { fireAndForget(async () => {
@@ -83,7 +83,7 @@
$: resultLines = $results; $: resultLines = $results;
let syncStatus = [] as string[]; let syncStatus = [] as string[];
eventHub.on<string[]>("debug-sync-status", (status) => { eventHub.onEvent("debug-sync-status", (status) => {
syncStatus = [...status]; syncStatus = [...status];
}); });
</script> </script>

View File

@@ -1,19 +1,15 @@
import { import { ItemView, WorkspaceLeaf } from "obsidian";
ItemView, import TestPaneComponent from "./TestPane.svelte";
WorkspaceLeaf import type ObsidianLiveSyncPlugin from "../../../main.ts";
} from "obsidian";
import TestPaneComponent from "./TestPane.svelte"
import type ObsidianLiveSyncPlugin from "../../../main.ts"
import type { ModuleDev } from "../ModuleDev.ts"; import type { ModuleDev } from "../ModuleDev.ts";
export const VIEW_TYPE_TEST = "ols-pane-test"; export const VIEW_TYPE_TEST = "ols-pane-test";
//Log view //Log view
export class TestPaneView extends ItemView { export class TestPaneView extends ItemView {
component?: TestPaneComponent; component?: TestPaneComponent;
plugin: ObsidianLiveSyncPlugin; plugin: ObsidianLiveSyncPlugin;
moduleDev: ModuleDev; moduleDev: ModuleDev;
icon = "view-log"; icon = "view-log";
title: string = "Self-hosted LiveSync Test and Results" title: string = "Self-hosted LiveSync Test and Results";
navigation = true; navigation = true;
getIcon(): string { getIcon(): string {
@@ -26,7 +22,6 @@ export class TestPaneView extends ItemView {
this.moduleDev = moduleDev; this.moduleDev = moduleDev;
} }
getViewType() { getViewType() {
return VIEW_TYPE_TEST; return VIEW_TYPE_TEST;
} }
@@ -35,19 +30,17 @@ export class TestPaneView extends ItemView {
return "Self-hosted LiveSync Test and Results"; return "Self-hosted LiveSync Test and Results";
} }
// eslint-disable-next-line require-await
async onOpen() { async onOpen() {
this.component = new TestPaneComponent({ this.component = new TestPaneComponent({
target: this.contentEl, target: this.contentEl,
props: { props: {
plugin: this.plugin, plugin: this.plugin,
moduleDev: this.moduleDev moduleDev: this.moduleDev,
}, },
}); });
await Promise.resolve(); await Promise.resolve();
} }
// eslint-disable-next-line require-await
async onClose() { async onClose() {
this.component?.$destroy(); this.component?.$destroy();
await Promise.resolve(); await Promise.resolve();

View File

@@ -7,40 +7,44 @@ export function enableTestFunction(plugin_: ObsidianLiveSyncPlugin) {
plugin = plugin_; plugin = plugin_;
} }
export function addDebugFileLog(message: any, stackLog = false) { export function addDebugFileLog(message: any, stackLog = false) {
fireAndForget(serialized("debug-log", async () => { fireAndForget(
const now = new Date(); serialized("debug-log", async () => {
const filename = `debug-log` const now = new Date();
const time = now.toISOString().split("T")[0]; const filename = `debug-log`;
const outFile = `${filename}${time}.jsonl`; const time = now.toISOString().split("T")[0];
// const messageContent = typeof message == "string" ? message : message instanceof Error ? `${message.name}:${message.message}` : JSON.stringify(message, null, 2); const outFile = `${filename}${time}.jsonl`;
const timestamp = now.toLocaleString(); // const messageContent = typeof message == "string" ? message : message instanceof Error ? `${message.name}:${message.message}` : JSON.stringify(message, null, 2);
const timestampEpoch = now; const timestamp = now.toLocaleString();
let out = { "timestamp": timestamp, epoch: timestampEpoch, } as Record<string, any>; const timestampEpoch = now;
if (message instanceof Error) { let out = { timestamp: timestamp, epoch: timestampEpoch } as Record<string, any>;
// debugger; if (message instanceof Error) {
// console.dir(message.stack); // debugger;
out = { ...out, message }; // console.dir(message.stack);
} else if (stackLog) { out = { ...out, message };
if (stackLog) { } else if (stackLog) {
const stackE = new Error(); if (stackLog) {
const stack = stackE.stack; const stackE = new Error();
out = { ...out, stack } const stack = stackE.stack;
out = { ...out, stack };
}
} }
} if (typeof message == "object") {
if (typeof message == "object") { out = { ...out, ...message };
out = { ...out, ...message, } } else {
} else { out = {
out = { result: message,
result: message };
} }
} // const out = "--" + timestamp + "--\n" + messageContent + " " + (stack || "");
// const out = "--" + timestamp + "--\n" + messageContent + " " + (stack || ""); // const out
// const out try {
try { await plugin.storageAccess.appendHiddenFile(
await plugin.storageAccess.appendHiddenFile(plugin.app.vault.configDir + "/ls-debug/" + outFile, JSON.stringify(out) + "\n") plugin.app.vault.configDir + "/ls-debug/" + outFile,
} catch { JSON.stringify(out) + "\n"
);
//NO OP } catch {
} //NO OP
})); }
})
);
} }

View File

@@ -7,7 +7,7 @@ const measures = new Map<string, MeasureResult>();
function clearResult(name: string) { function clearResult(name: string) {
measures.set(name, [0, 0]); measures.set(name, [0, 0]);
} }
async function measureEach(name: string, proc: () => (void | Promise<void>)) { async function measureEach(name: string, proc: () => void | Promise<void>) {
const [times, spent] = measures.get(name) ?? [0, 0]; const [times, spent] = measures.get(name) ?? [0, 0];
const start = performance.now(); const start = performance.now();
@@ -15,57 +15,76 @@ async function measureEach(name: string, proc: () => (void | Promise<void>)) {
if (result instanceof Promise) await result; if (result instanceof Promise) await result;
const end = performance.now(); const end = performance.now();
measures.set(name, [times + 1, spent + (end - start)]); measures.set(name, [times + 1, spent + (end - start)]);
} }
function formatNumber(num: number) { function formatNumber(num: number) {
return num.toLocaleString('en-US', { maximumFractionDigits: 2 }); return num.toLocaleString("en-US", { maximumFractionDigits: 2 });
} }
async function measure(name: string, proc: () => (void | Promise<void>), times: number = 10000, duration: number = 1000): Promise<NamedMeasureResult> { async function measure(
name: string,
proc: () => void | Promise<void>,
times: number = 10000,
duration: number = 1000
): Promise<NamedMeasureResult> {
const from = Date.now(); const from = Date.now();
let last = times; let last = times;
clearResult(name); clearResult(name);
do { do {
await measureEach(name, proc); await measureEach(name, proc);
} while (last-- > 0 && (Date.now() - from) < duration) } while (last-- > 0 && Date.now() - from < duration);
return [name, measures.get(name) as MeasureResult]; return [name, measures.get(name) as MeasureResult];
} }
// eslint-disable-next-line require-await, @typescript-eslint/require-await // eslint-disable-next-line require-await, @typescript-eslint/require-await
async function formatPerfResults(items: NamedMeasureResult[]) { async function formatPerfResults(items: NamedMeasureResult[]) {
return `| Name | Runs | Each | Total |\n| --- | --- | --- | --- | \n` + items.map(e => `| ${e[0]} | ${e[1][0]} | ${e[1][0] != 0 ? formatNumber(e[1][1] / e[1][0]) : "-"} | ${formatNumber(e[1][0])} |`).join("\n"); return (
`| Name | Runs | Each | Total |\n| --- | --- | --- | --- | \n` +
items
.map(
(e) =>
`| ${e[0]} | ${e[1][0]} | ${e[1][0] != 0 ? formatNumber(e[1][1] / e[1][0]) : "-"} | ${formatNumber(e[1][0])} |`
)
.join("\n")
);
} }
export async function perf_trench(plugin: ObsidianLiveSyncPlugin) { export async function perf_trench(plugin: ObsidianLiveSyncPlugin) {
clearResult("trench"); clearResult("trench");
const trench = new Trench(plugin.simpleStore); const trench = new Trench(plugin.simpleStore);
const result = [] as NamedMeasureResult[]; const result = [] as NamedMeasureResult[];
result.push(await measure("trench-short-string", async () => { result.push(
const p = trench.evacuate("string"); await measure("trench-short-string", async () => {
await p(); const p = trench.evacuate("string");
})); await p();
})
);
{ {
const testBinary = await plugin.storageAccess.readHiddenFileBinary("testdata/10kb.png"); const testBinary = await plugin.storageAccess.readHiddenFileBinary("testdata/10kb.png");
const uint8Array = new Uint8Array(testBinary); const uint8Array = new Uint8Array(testBinary);
result.push(await measure("trench-binary-10kb", async () => { result.push(
const p = trench.evacuate(uint8Array); await measure("trench-binary-10kb", async () => {
await p(); const p = trench.evacuate(uint8Array);
})); await p();
})
);
} }
{ {
const testBinary = await plugin.storageAccess.readHiddenFileBinary("testdata/100kb.jpeg"); const testBinary = await plugin.storageAccess.readHiddenFileBinary("testdata/100kb.jpeg");
const uint8Array = new Uint8Array(testBinary); const uint8Array = new Uint8Array(testBinary);
result.push(await measure("trench-binary-100kb", async () => { result.push(
const p = trench.evacuate(uint8Array); await measure("trench-binary-100kb", async () => {
await p(); const p = trench.evacuate(uint8Array);
})); await p();
})
);
} }
{ {
const testBinary = await plugin.storageAccess.readHiddenFileBinary("testdata/1mb.png"); const testBinary = await plugin.storageAccess.readHiddenFileBinary("testdata/1mb.png");
const uint8Array = new Uint8Array(testBinary); const uint8Array = new Uint8Array(testBinary);
result.push(await measure("trench-binary-1mb", async () => { result.push(
const p = trench.evacuate(uint8Array); await measure("trench-binary-1mb", async () => {
await p(); const p = trench.evacuate(uint8Array);
})); await p();
})
);
} }
return formatPerfResults(result); return formatPerfResults(result);
} }

View File

@@ -2,7 +2,14 @@ import { TFile, Modal, App, DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, diff_match_pat
import { getPathFromTFile, isValidPath } from "../../../common/utils.ts"; import { getPathFromTFile, isValidPath } from "../../../common/utils.ts";
import { decodeBinary, escapeStringToHTML, readString } from "../../../lib/src/string_and_binary/convert.ts"; import { decodeBinary, escapeStringToHTML, readString } from "../../../lib/src/string_and_binary/convert.ts";
import ObsidianLiveSyncPlugin from "../../../main.ts"; import ObsidianLiveSyncPlugin from "../../../main.ts";
import { type DocumentID, type FilePathWithPrefix, type LoadedEntry, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "../../../lib/src/common/types.ts"; import {
type DocumentID,
type FilePathWithPrefix,
type LoadedEntry,
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
} from "../../../lib/src/common/types.ts";
import { Logger } from "../../../lib/src/common/logger.ts"; import { Logger } from "../../../lib/src/common/logger.ts";
import { isErrorOfMissingDoc } from "../../../lib/src/pouchdb/utils_couchdb.ts"; import { isErrorOfMissingDoc } from "../../../lib/src/pouchdb/utils_couchdb.ts";
import { fireAndForget, getDocData, readContent } from "../../../lib/src/common/utils.ts"; import { fireAndForget, getDocData, readContent } from "../../../lib/src/common/utils.ts";
@@ -19,7 +26,7 @@ function isComparableText(path: string) {
} }
function isComparableTextDecode(path: string) { function isComparableTextDecode(path: string) {
const ext = path.split(".").splice(-1)[0].toLowerCase(); const ext = path.split(".").splice(-1)[0].toLowerCase();
return ["json"].includes(ext) return ["json"].includes(ext);
} }
function readDocument(w: LoadedEntry) { function readDocument(w: LoadedEntry) {
if (w.data.length == 0) return ""; if (w.data.length == 0) return "";
@@ -54,10 +61,16 @@ export class DocumentHistoryModal extends Modal {
currentDeleted = false; currentDeleted = false;
initialRev?: string; initialRev?: string;
constructor(app: App, plugin: ObsidianLiveSyncPlugin, file: TFile | FilePathWithPrefix, id?: DocumentID, revision?: string) { constructor(
app: App,
plugin: ObsidianLiveSyncPlugin,
file: TFile | FilePathWithPrefix,
id?: DocumentID,
revision?: string
) {
super(app); super(app);
this.plugin = plugin; this.plugin = plugin;
this.file = (file instanceof TFile) ? getPathFromTFile(file) : file; this.file = file instanceof TFile ? getPathFromTFile(file) : file;
this.id = id; this.id = id;
this.initialRev = revision; this.initialRev = revision;
if (!file && id) { if (!file && id) {
@@ -85,9 +98,9 @@ export class DocumentHistoryModal extends Modal {
this.range.max = "0"; this.range.max = "0";
this.range.value = ""; this.range.value = "";
this.range.disabled = true; this.range.disabled = true;
this.contentView.setText(`History of this file was not recorded.`); this.contentView.setText(`We don't have any history for this note.`);
} else { } else {
this.contentView.setText(`Error occurred.`); this.contentView.setText(`Error while loading file.`);
Logger(ex, LOG_LEVEL_VERBOSE); Logger(ex, LOG_LEVEL_VERBOSE);
} }
} }
@@ -95,7 +108,7 @@ export class DocumentHistoryModal extends Modal {
async loadRevs(initialRev?: string) { async loadRevs(initialRev?: string) {
if (this.revs_info.length == 0) return; if (this.revs_info.length == 0) return;
if (initialRev) { if (initialRev) {
const rIndex = this.revs_info.findIndex(e => e.rev == initialRev); const rIndex = this.revs_info.findIndex((e) => e.rev == initialRev);
if (rIndex >= 0) { if (rIndex >= 0) {
this.range.value = `${this.revs_info.length - 1 - rIndex}`; this.range.value = `${this.revs_info.length - 1 - rIndex}`;
} }
@@ -163,8 +176,7 @@ export class DocumentHistoryModal extends Modal {
} else if (isImage(this.file)) { } else if (isImage(this.file)) {
const src = this.generateBlobURL("base", w1data); const src = this.generateBlobURL("base", w1data);
const overlay = this.generateBlobURL("overlay", readDocument(w2) as Uint8Array); const overlay = this.generateBlobURL("overlay", readDocument(w2) as Uint8Array);
result = result = `<div class='ls-imgdiff-wrap'>
`<div class='ls-imgdiff-wrap'>
<div class='overlay'> <div class='overlay'>
<img class='img-base' src="${src}"> <img class='img-base' src="${src}">
<img class='img-overlay' src='${overlay}'> <img class='img-overlay' src='${overlay}'>
@@ -174,14 +186,12 @@ export class DocumentHistoryModal extends Modal {
} }
} }
} }
} }
if (result == undefined) { if (result == undefined) {
if (typeof w1data != "string") { if (typeof w1data != "string") {
if (isImage(this.file)) { if (isImage(this.file)) {
const src = this.generateBlobURL("base", w1data); const src = this.generateBlobURL("base", w1data);
result = result = `<div class='ls-imgdiff-wrap'>
`<div class='ls-imgdiff-wrap'>
<div class='overlay'> <div class='overlay'>
<img class='img-base' src="${src}"> <img class='img-base' src="${src}">
</div> </div>
@@ -193,7 +203,8 @@ export class DocumentHistoryModal extends Modal {
} }
} }
if (result == undefined) result = typeof w1data == "string" ? escapeStringToHTML(w1data) : "Binary file"; if (result == undefined) result = typeof w1data == "string" ? escapeStringToHTML(w1data) : "Binary file";
this.contentView.innerHTML = (this.currentDeleted ? "(At this revision, the file has been deleted)\n" : "") + result; this.contentView.innerHTML =
(this.currentDeleted ? "(At this revision, the file has been deleted)\n" : "") + result;
} }
} }
@@ -257,9 +268,9 @@ export class DocumentHistoryModal extends Modal {
const leaf = this.plugin.app.workspace.getLeaf(false); const leaf = this.plugin.app.workspace.getLeaf(false);
await leaf.openFile(targetFile); await leaf.openFile(targetFile);
} else { } else {
Logger("The file could not view on the editor", LOG_LEVEL_NOTICE) Logger("Unable to display the file in the editor", LOG_LEVEL_NOTICE);
} }
} };
buttons.createEl("button", { text: "Back to this revision" }, (e) => { buttons.createEl("button", { text: "Back to this revision" }, (e) => {
e.addClass("mod-cta"); e.addClass("mod-cta");
e.addEventListener("click", () => { e.addEventListener("click", () => {
@@ -285,9 +296,9 @@ export class DocumentHistoryModal extends Modal {
onClose() { onClose() {
const { contentEl } = this; const { contentEl } = this;
contentEl.empty(); contentEl.empty();
this.BlobURLs.forEach(value => { this.BlobURLs.forEach((value) => {
console.log(value); console.log(value);
if (value) URL.revokeObjectURL(value); if (value) URL.revokeObjectURL(value);
}) });
} }
} }

View File

@@ -6,7 +6,6 @@
import { diff_match_patch } from "../../../deps.ts"; import { diff_match_patch } from "../../../deps.ts";
import { DocumentHistoryModal } from "../DocumentHistory/DocumentHistoryModal.ts"; import { DocumentHistoryModal } from "../DocumentHistory/DocumentHistoryModal.ts";
import { isPlainText, stripAllPrefixes } from "../../../lib/src/string_and_binary/path.ts"; import { isPlainText, stripAllPrefixes } from "../../../lib/src/string_and_binary/path.ts";
import { TFile } from "../../../deps.ts";
import { getPath } from "../../../common/utils.ts"; import { getPath } from "../../../common/utils.ts";
export let plugin: ObsidianLiveSyncPlugin; export let plugin: ObsidianLiveSyncPlugin;
@@ -67,7 +66,10 @@
for (const revInfo of reversedRevs) { for (const revInfo of reversedRevs) {
if (revInfo.status == "available") { if (revInfo.status == "available") {
const doc = (!isPlain && showDiffInfo) || (checkStorageDiff && revInfo.rev == docA._rev) ? await db.getDBEntry(path, { rev: revInfo.rev }, false, false, true) : await db.getDBEntryMeta(path, { rev: revInfo.rev }, true); const doc =
(!isPlain && showDiffInfo) || (checkStorageDiff && revInfo.rev == docA._rev)
? await db.getDBEntry(path, { rev: revInfo.rev }, false, false, true)
: await db.getDBEntryMeta(path, { rev: revInfo.rev }, true);
if (doc === false) continue; if (doc === false) continue;
const rev = revInfo.rev; const rev = revInfo.rev;
@@ -95,7 +97,10 @@
[DIFF_EQUAL]: 0, [DIFF_EQUAL]: 0,
[DIFF_INSERT]: 0, [DIFF_INSERT]: 0,
} as { [key: number]: number }; } as { [key: number]: number };
const px = diff.reduce((p, c) => ({ ...p, [c[0]]: (p[c[0]] ?? 0) + c[1].length }), pxInit); const px = diff.reduce(
(p, c) => ({ ...p, [c[0]]: (p[c[0]] ?? 0) + c[1].length }),
pxInit
);
diffDetail = `-${px[DIFF_DELETE]}, +${px[DIFF_INSERT]}`; diffDetail = `-${px[DIFF_DELETE]}, +${px[DIFF_INSERT]}`;
} }
} }
@@ -105,9 +110,13 @@
} }
if (rev == docA._rev) { if (rev == docA._rev) {
if (checkStorageDiff) { if (checkStorageDiff) {
const isExist = await plugin.storageAccess.isExists(stripAllPrefixes(getPath(docA))); const isExist = await plugin.storageAccess.isExistsIncludeHidden(
stripAllPrefixes(getPath(docA))
);
if (isExist) { if (isExist) {
const data = await plugin.storageAccess.readFileAuto(stripAllPrefixes(getPath(docA))); const data = await plugin.storageAccess.readHiddenFileBinary(
stripAllPrefixes(getPath(docA))
);
const d = readAsBlob(doc); const d = readAsBlob(doc);
const result = await isDocContentSame(data, d); const result = await isDocContentSame(data, d);
if (result) { if (result) {
@@ -188,19 +197,28 @@
<div class="globalhistory"> <div class="globalhistory">
<h1>Vault history</h1> <h1>Vault history</h1>
<div class="control"> <div class="control">
<div class="row"><label for="">From:</label><input type="date" bind:value={dispDateFrom} disabled={loading} /></div> <div class="row">
<label for="">From:</label><input type="date" bind:value={dispDateFrom} disabled={loading} />
</div>
<div class="row"><label for="">To:</label><input type="date" bind:value={dispDateTo} disabled={loading} /></div> <div class="row"><label for="">To:</label><input type="date" bind:value={dispDateTo} disabled={loading} /></div>
<div class="row"> <div class="row">
<label for="">Info:</label> <label for="">Info:</label>
<label><input type="checkbox" bind:checked={showDiffInfo} disabled={loading} /><span>Diff</span></label> <label><input type="checkbox" bind:checked={showDiffInfo} disabled={loading} /><span>Diff</span></label>
<label><input type="checkbox" bind:checked={showChunkCorrected} disabled={loading} /><span>Chunks</span></label> <label
<label><input type="checkbox" bind:checked={checkStorageDiff} disabled={loading} /><span>File integrity</span></label> ><input type="checkbox" bind:checked={showChunkCorrected} disabled={loading} /><span>Chunks</span
></label
>
<label
><input type="checkbox" bind:checked={checkStorageDiff} disabled={loading} /><span>File integrity</span
></label
>
</div> </div>
</div> </div>
{#if loading} {#if loading}
<div class="">Gathering information...</div> <div class="">Gathering information...</div>
{/if} {/if}
<table> <table>
<tbody>
<tr> <tr>
<th> Date </th> <th> Date </th>
<th> Path </th> <th> Path </th>
@@ -213,7 +231,7 @@
<tr> <tr>
<td colspan="5" class="more"> <td colspan="5" class="more">
{#if loading} {#if loading}
<div class="" /> <div class=""></div>
{:else} {:else}
<div><button on:click={() => nextWeek()}>+1 week</button></div> <div><button on:click={() => nextWeek()}>+1 week</button></div>
{/if} {/if}
@@ -258,12 +276,13 @@
<tr> <tr>
<td colspan="5" class="more"> <td colspan="5" class="more">
{#if loading} {#if loading}
<div class="" /> <div class=""></div>
{:else} {:else}
<div><button on:click={() => prevWeek()}>+1 week</button></div> <div><button on:click={() => prevWeek()}>+1 week</button></div>
{/if} {/if}
</td> </td>
</tr> </tr>
</tbody>
</table> </table>
</div> </div>

View File

@@ -1,14 +1,20 @@
import { import { WorkspaceLeaf } from "../../../deps.ts";
ItemView,
WorkspaceLeaf
} from "../../../deps.ts";
import GlobalHistoryComponent from "./GlobalHistory.svelte"; import GlobalHistoryComponent from "./GlobalHistory.svelte";
import type ObsidianLiveSyncPlugin from "../../../main.ts"; import type ObsidianLiveSyncPlugin from "../../../main.ts";
import { SvelteItemView } from "../../../common/SvelteItemView.ts";
import { mount } from "svelte";
export const VIEW_TYPE_GLOBAL_HISTORY = "global-history"; export const VIEW_TYPE_GLOBAL_HISTORY = "global-history";
export class GlobalHistoryView extends ItemView { export class GlobalHistoryView extends SvelteItemView {
instantiateComponent(target: HTMLElement) {
return mount(GlobalHistoryComponent, {
target: target,
props: {
plugin: this.plugin,
},
});
}
component?: GlobalHistoryComponent;
plugin: ObsidianLiveSyncPlugin; plugin: ObsidianLiveSyncPlugin;
icon = "clock"; icon = "clock";
title: string = ""; title: string = "";
@@ -23,7 +29,6 @@ export class GlobalHistoryView extends ItemView {
this.plugin = plugin; this.plugin = plugin;
} }
getViewType() { getViewType() {
return VIEW_TYPE_GLOBAL_HISTORY; return VIEW_TYPE_GLOBAL_HISTORY;
} }
@@ -31,19 +36,4 @@ export class GlobalHistoryView extends ItemView {
getDisplayText() { getDisplayText() {
return "Vault history"; return "Vault history";
} }
async onOpen() {
this.component = new GlobalHistoryComponent({
target: this.contentEl,
props: {
plugin: this.plugin,
},
});
await Promise.resolve();
}
async onClose() {
this.component?.$destroy();
await Promise.resolve();
}
} }

View File

@@ -1,10 +1,19 @@
import { App, Modal } from "../../../deps.ts"; import { App, Modal } from "../../../deps.ts";
import { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT } from "diff-match-patch"; import { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT } from "diff-match-patch";
import { CANCELLED, LEAVE_TO_SUBSEQUENT, RESULT_TIMED_OUT, type diff_result } from "../../../lib/src/common/types.ts"; import { CANCELLED, LEAVE_TO_SUBSEQUENT, type diff_result } from "../../../lib/src/common/types.ts";
import { escapeStringToHTML } from "../../../lib/src/string_and_binary/convert.ts"; import { escapeStringToHTML } from "../../../lib/src/string_and_binary/convert.ts";
import { delay, fireAndForget, sendValue, waitForValue } from "../../../lib/src/common/utils.ts"; import { delay } from "../../../lib/src/common/utils.ts";
import { eventHub } from "../../../common/events.ts";
import { globalSlipBoard } from "../../../lib/src/bureau/bureau.ts";
export type MergeDialogResult = typeof CANCELLED | typeof LEAVE_TO_SUBSEQUENT | string;
declare global {
interface Slips extends LSSlips {
"conflict-resolved": typeof CANCELLED | MergeDialogResult;
}
}
export type MergeDialogResult = typeof LEAVE_TO_SUBSEQUENT | typeof CANCELLED | string;
export class ConflictResolveModal extends Modal { export class ConflictResolveModal extends Modal {
result: diff_result; result: diff_result;
filename: string; filename: string;
@@ -18,6 +27,7 @@ export class ConflictResolveModal extends Modal {
pluginPickMode: boolean = false; pluginPickMode: boolean = false;
localName: string = "Keep A"; localName: string = "Keep A";
remoteName: string = "Keep B"; remoteName: string = "Keep B";
offEvent?: ReturnType<typeof eventHub.onEvent>;
constructor(app: App, filename: string, diff: diff_result, pluginPickMode?: boolean, remoteName?: string) { constructor(app: App, filename: string, diff: diff_result, pluginPickMode?: boolean, remoteName?: string) {
super(app); super(app);
@@ -27,28 +37,26 @@ export class ConflictResolveModal extends Modal {
if (this.pluginPickMode) { if (this.pluginPickMode) {
this.title = "Pick a version"; this.title = "Pick a version";
this.remoteName = `Use ${remoteName || "Remote"}`; this.remoteName = `Use ${remoteName || "Remote"}`;
this.localName = "Use Local" this.localName = "Use Local";
} }
// Send cancel signal for the previous merge dialogue // Send cancel signal for the previous merge dialogue
// if not there, simply be ignored. // if not there, simply be ignored.
// sendValue("close-resolve-conflict:" + this.filename, false); // sendValue("close-resolve-conflict:" + this.filename, false);
sendValue("cancel-resolve-conflict:" + this.filename, true);
} }
onOpen() { onOpen() {
const { contentEl } = this; const { contentEl } = this;
// Send cancel signal for the previous merge dialogue // Send cancel signal for the previous merge dialogue
// if not there, simply be ignored. // if not there, simply be ignored.
sendValue("cancel-resolve-conflict:" + this.filename, true); globalSlipBoard.submit("conflict-resolved", this.filename, CANCELLED);
setTimeout(() => { if (this.offEvent) {
fireAndForget(async () => { this.offEvent();
const forceClose = await waitForValue("cancel-resolve-conflict:" + this.filename); }
// debugger; this.offEvent = eventHub.onEvent("conflict-cancelled", (path) => {
if (forceClose) { if (path === this.filename) {
this.sendResponse(CANCELLED); this.sendResponse(CANCELLED);
} }
}); });
}, 10)
// sendValue("close-resolve-conflict:" + this.filename, false); // sendValue("close-resolve-conflict:" + this.filename, false);
this.titleEl.setText(this.title); this.titleEl.setText(this.title);
contentEl.empty(); contentEl.empty();
@@ -60,28 +68,47 @@ export class ConflictResolveModal extends Modal {
const x1 = v[0]; const x1 = v[0];
const x2 = v[1]; const x2 = v[1];
if (x1 == DIFF_DELETE) { if (x1 == DIFF_DELETE) {
diff += "<span class='deleted'>" + escapeStringToHTML(x2).replace(/\n/g, "<span class='ls-mark-cr'></span>\n") + "</span>"; diff +=
"<span class='deleted'>" +
escapeStringToHTML(x2).replace(/\n/g, "<span class='ls-mark-cr'></span>\n") +
"</span>";
} else if (x1 == DIFF_EQUAL) { } else if (x1 == DIFF_EQUAL) {
diff += "<span class='normal'>" + escapeStringToHTML(x2).replace(/\n/g, "<span class='ls-mark-cr'></span>\n") + "</span>"; diff +=
"<span class='normal'>" +
escapeStringToHTML(x2).replace(/\n/g, "<span class='ls-mark-cr'></span>\n") +
"</span>";
} else if (x1 == DIFF_INSERT) { } else if (x1 == DIFF_INSERT) {
diff += "<span class='added'>" + escapeStringToHTML(x2).replace(/\n/g, "<span class='ls-mark-cr'></span>\n") + "</span>"; diff +=
"<span class='added'>" +
escapeStringToHTML(x2).replace(/\n/g, "<span class='ls-mark-cr'></span>\n") +
"</span>";
} }
} }
diff = diff.replace(/\n/g, "<br>"); diff = diff.replace(/\n/g, "<br>");
div.innerHTML = diff; div.innerHTML = diff;
const div2 = contentEl.createDiv(""); const div2 = contentEl.createDiv("");
const date1 = new Date(this.result.left.mtime).toLocaleString() + (this.result.left.deleted ? " (Deleted)" : ""); const date1 =
const date2 = new Date(this.result.right.mtime).toLocaleString() + (this.result.right.deleted ? " (Deleted)" : ""); new Date(this.result.left.mtime).toLocaleString() + (this.result.left.deleted ? " (Deleted)" : "");
const date2 =
new Date(this.result.right.mtime).toLocaleString() + (this.result.right.deleted ? " (Deleted)" : "");
div2.innerHTML = ` div2.innerHTML = `
<span class='deleted'>A:${date1}</span><br /><span class='added'>B:${date2}</span><br> <span class='deleted'>A:${date1}</span><br /><span class='added'>B:${date2}</span><br>
`; `;
contentEl.createEl("button", { text: this.localName }, (e) => e.addEventListener("click", () => this.sendResponse(this.result.right.rev))).style.marginRight = "4px"; contentEl.createEl("button", { text: this.localName }, (e) =>
contentEl.createEl("button", { text: this.remoteName }, (e) => e.addEventListener("click", () => this.sendResponse(this.result.left.rev))).style.marginRight = "4px"; e.addEventListener("click", () => this.sendResponse(this.result.right.rev))
).style.marginRight = "4px";
contentEl.createEl("button", { text: this.remoteName }, (e) =>
e.addEventListener("click", () => this.sendResponse(this.result.left.rev))
).style.marginRight = "4px";
if (!this.pluginPickMode) { if (!this.pluginPickMode) {
contentEl.createEl("button", { text: "Concat both" }, (e) => e.addEventListener("click", () => this.sendResponse(LEAVE_TO_SUBSEQUENT))).style.marginRight = "4px"; contentEl.createEl("button", { text: "Concat both" }, (e) =>
e.addEventListener("click", () => this.sendResponse(LEAVE_TO_SUBSEQUENT))
).style.marginRight = "4px";
} }
contentEl.createEl("button", { text: !this.pluginPickMode ? "Not now" : "Cancel" }, (e) => e.addEventListener("click", () => this.sendResponse(CANCELLED))).style.marginRight = "4px"; contentEl.createEl("button", { text: !this.pluginPickMode ? "Not now" : "Cancel" }, (e) =>
e.addEventListener("click", () => this.sendResponse(CANCELLED))
).style.marginRight = "4px";
} }
sendResponse(result: MergeDialogResult) { sendResponse(result: MergeDialogResult) {
@@ -92,18 +119,19 @@ export class ConflictResolveModal extends Modal {
onClose() { onClose() {
const { contentEl } = this; const { contentEl } = this;
contentEl.empty(); contentEl.empty();
if (this.offEvent) {
this.offEvent();
}
if (this.consumed) { if (this.consumed) {
return; return;
} }
this.consumed = true; this.consumed = true;
sendValue("close-resolve-conflict:" + this.filename, this.response); globalSlipBoard.submit("conflict-resolved", this.filename, this.response);
sendValue("cancel-resolve-conflict:" + this.filename, false);
} }
async waitForResult(): Promise<MergeDialogResult> { async waitForResult(): Promise<MergeDialogResult> {
await delay(100); await delay(100);
const r = await waitForValue<MergeDialogResult>("close-resolve-conflict:" + this.filename); const r = await globalSlipBoard.awaitNext("conflict-resolved", this.filename);
if (r === RESULT_TIMED_OUT) return CANCELLED;
return r; return r;
} }
} }

View File

@@ -1,82 +1,105 @@
<script lang="ts"> <script lang="ts">
import { onDestroy, onMount } from "svelte"; import { onDestroy, onMount } from "svelte";
import { logMessages } from "../../../lib/src/mock_and_interop/stores"; import { logMessages } from "../../../lib/src/mock_and_interop/stores";
import type { ReactiveInstance } from "../../../lib/src/dataobject/reactive"; import { reactive, type ReactiveInstance } from "../../../lib/src/dataobject/reactive";
import { Logger } from "../../../lib/src/common/logger"; import { Logger } from "../../../lib/src/common/logger";
import { $msg as msg, currentLang as lang } from "../../../lib/src/common/i18n.ts";
let unsubscribe: () => void; let unsubscribe: () => void;
let messages = [] as string[]; let messages = $state([] as string[]);
let wrapRight = false; let wrapRight = $state(false);
let autoScroll = true; let autoScroll = $state(true);
let suspended = false; let suspended = $state(false);
function updateLog(logs: ReactiveInstance<string[]>) {
const e = logs.value; type Props = {
if (!suspended) { close: () => void;
messages = [...e]; };
setTimeout(() => { let { close }: Props = $props();
if (scroll) scroll.scrollTop = scroll.scrollHeight; // export let close: () => void;
}, 10); function updateLog(logs: ReactiveInstance<string[]>) {
} const e = logs.value;
if (!suspended) {
messages = [...e];
setTimeout(() => {
if (scroll) scroll.scrollTop = scroll.scrollHeight;
}, 10);
}
}
onMount(async () => {
const _logMessages = reactive(() => logMessages.value);
_logMessages.onChanged(updateLog);
Logger(msg("logPane.logWindowOpened", {}, lang));
unsubscribe = () => _logMessages.offChanged(updateLog);
});
onDestroy(() => {
if (unsubscribe) unsubscribe();
});
let scroll: HTMLDivElement;
function closeDialogue() {
close();
} }
onMount(async () => {
logMessages.onChanged(updateLog);
Logger("Log window opened");
unsubscribe = () => logMessages.offChanged(updateLog);
});
onDestroy(() => {
if (unsubscribe) unsubscribe();
});
let scroll: HTMLDivElement;
</script> </script>
<div class="logpane"> <div class="logpane">
<!-- <h1>Self-hosted LiveSync Log</h1> --> <!-- <h1>{msg("logPane.title", {}, lang)}</h1> -->
<div class="control"> <div class="control">
<div class="row"> <div class="row">
<label><input type="checkbox" bind:checked={wrapRight} /><span>Wrap</span></label> <label>
<label><input type="checkbox" bind:checked={autoScroll} /><span>Auto scroll</span></label> <input type="checkbox" bind:checked={wrapRight} />
<label><input type="checkbox" bind:checked={suspended} /><span>Pause</span></label> <span>{msg("logPane.wrap", {}, lang)}</span>
</div> </label>
</div> <label>
<div class="log" bind:this={scroll}> <input type="checkbox" bind:checked={autoScroll} />
{#each messages as line} <span>{msg("logPane.autoScroll", {}, lang)}</span>
<pre class:wrap-right={wrapRight}>{line}</pre> </label>
{/each} <label>
</div> <input type="checkbox" bind:checked={suspended} />
<span>{msg("logPane.pause", {}, lang)}</span>
</label>
<span class="spacer"></span>
<button onclick={() => closeDialogue()}>Close</button>
</div>
</div>
<div class="log" bind:this={scroll}>
{#each messages as line}
<pre class:wrap-right={wrapRight}>{line}</pre>
{/each}
</div>
</div> </div>
<style> <style>
* { * {
box-sizing: border-box; box-sizing: border-box;
} }
.logpane { .logpane {
display: flex; display: flex;
height: 100%; height: 100%;
flex-direction: column; flex-direction: column;
} }
.log { .log {
overflow-y: scroll; overflow-y: scroll;
user-select: text; user-select: text;
padding-bottom: 2em; -webkit-user-select: text;
} padding-bottom: 2em;
.log > pre { }
margin: 0; .log > pre {
} margin: 0;
.log > pre.wrap-right { }
word-break: break-all; .log > pre.wrap-right {
max-width: 100%; word-break: break-all;
width: 100%; max-width: 100%;
white-space: normal; width: 100%;
} white-space: normal;
.row { }
display: flex; .row {
flex-direction: row; display: flex;
justify-content: flex-end; flex-direction: row;
} justify-content: flex-end;
.row > label { }
display: flex; .row > label {
align-items: center; display: flex;
min-width: 5em; align-items: center;
margin-right: 1em; min-width: 5em;
} margin-right: 1em;
}
</style> </style>

View File

@@ -1,18 +1,27 @@
import { import { WorkspaceLeaf } from "obsidian";
ItemView,
WorkspaceLeaf
} from "obsidian";
import LogPaneComponent from "./LogPane.svelte"; import LogPaneComponent from "./LogPane.svelte";
import type ObsidianLiveSyncPlugin from "../../../main.ts"; import type ObsidianLiveSyncPlugin from "../../../main.ts";
import { SvelteItemView } from "../../../common/SvelteItemView.ts";
import { $msg } from "src/lib/src/common/i18n.ts";
import { mount } from "svelte";
export const VIEW_TYPE_LOG = "log-log"; export const VIEW_TYPE_LOG = "log-log";
//Log view //Log view
export class LogPaneView extends ItemView { export class LogPaneView extends SvelteItemView {
instantiateComponent(target: HTMLElement) {
return mount(LogPaneComponent, {
target: target,
props: {
close: () => {
this.leaf.detach();
},
},
});
}
component?: LogPaneComponent;
plugin: ObsidianLiveSyncPlugin; plugin: ObsidianLiveSyncPlugin;
icon = "view-log"; icon = "view-log";
title: string = ""; title: string = "";
navigation = true; navigation = false;
getIcon(): string { getIcon(): string {
return "view-log"; return "view-log";
@@ -23,26 +32,12 @@ export class LogPaneView extends ItemView {
this.plugin = plugin; this.plugin = plugin;
} }
getViewType() { getViewType() {
return VIEW_TYPE_LOG; return VIEW_TYPE_LOG;
} }
getDisplayText() { getDisplayText() {
return "Self-hosted LiveSync Log"; // TODO: This function is not reactive and does not update the title based on the current language
} return $msg("logPane.title");
async onOpen() {
this.component = new LogPaneComponent({
target: this.contentEl,
props: {
},
});
await Promise.resolve();
}
async onClose() {
this.component?.$destroy();
await Promise.resolve();
} }
} }

View File

@@ -1,23 +1,17 @@
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { VIEW_TYPE_GLOBAL_HISTORY, GlobalHistoryView } from "./GlobalHistory/GlobalHistoryView.ts"; import { VIEW_TYPE_GLOBAL_HISTORY, GlobalHistoryView } from "./GlobalHistory/GlobalHistoryView.ts";
export class ModuleObsidianGlobalHistory extends AbstractObsidianModule implements IObsidianModule { export class ModuleObsidianGlobalHistory extends AbstractObsidianModule implements IObsidianModule {
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
this.addCommand({ this.addCommand({
id: "livesync-global-history", id: "livesync-global-history",
name: "Show vault history", name: "Show vault history",
callback: () => { callback: () => {
this.showGlobalHistory() this.showGlobalHistory();
} },
}) });
this.registerView( this.registerView(VIEW_TYPE_GLOBAL_HISTORY, (leaf) => new GlobalHistoryView(leaf, this.plugin));
VIEW_TYPE_GLOBAL_HISTORY,
(leaf) => new GlobalHistoryView(leaf, this.plugin)
);
return Promise.resolve(true); return Promise.resolve(true);
} }
@@ -25,5 +19,4 @@ export class ModuleObsidianGlobalHistory extends AbstractObsidianModule implemen
showGlobalHistory() { showGlobalHistory() {
void this.core.$$showView(VIEW_TYPE_GLOBAL_HISTORY); void this.core.$$showView(VIEW_TYPE_GLOBAL_HISTORY);
} }
} }

View File

@@ -1,11 +1,21 @@
import { CANCELLED, LEAVE_TO_SUBSEQUENT, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE, MISSING_OR_ERROR, type DocumentID, type FilePathWithPrefix, type diff_result } from "../../lib/src/common/types.ts"; import {
CANCELLED,
LEAVE_TO_SUBSEQUENT,
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
MISSING_OR_ERROR,
type DocumentID,
type FilePathWithPrefix,
type diff_result,
} from "../../lib/src/common/types.ts";
import { ConflictResolveModal } from "./InteractiveConflictResolving/ConflictResolveModal.ts"; import { ConflictResolveModal } from "./InteractiveConflictResolving/ConflictResolveModal.ts";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { displayRev, getPath, getPathWithoutPrefix } from "../../common/utils.ts"; import { displayRev, getPath, getPathWithoutPrefix } from "../../common/utils.ts";
import { fireAndForget } from "octagonal-wheels/promises"; import { fireAndForget } from "octagonal-wheels/promises";
import { serialized } from "../../lib/src/concurrency/lock.ts";
export class ModuleInteractiveConflictResolver extends AbstractObsidianModule implements IObsidianModule { export class ModuleInteractiveConflictResolver extends AbstractObsidianModule implements IObsidianModule {
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
this.addCommand({ this.addCommand({
id: "livesync-conflictcheck", id: "livesync-conflictcheck",
@@ -13,92 +23,103 @@ export class ModuleInteractiveConflictResolver extends AbstractObsidianModule im
callback: async () => { callback: async () => {
await this.pickFileForResolve(); await this.pickFileForResolve();
}, },
}) });
this.addCommand({ this.addCommand({
id: "livesync-all-conflictcheck", id: "livesync-all-conflictcheck",
name: "Resolve all conflicted files", name: "Resolve all conflicted files",
callback: async () => { callback: async () => {
await this.allConflictCheck(); await this.allConflictCheck();
}, },
}) });
return Promise.resolve(true); return Promise.resolve(true);
} }
async $anyResolveConflictByUI(filename: FilePathWithPrefix, conflictCheckResult: diff_result): Promise<boolean> { async $anyResolveConflictByUI(filename: FilePathWithPrefix, conflictCheckResult: diff_result): Promise<boolean> {
this._log("Merge:open conflict dialog", LOG_LEVEL_VERBOSE); // UI for resolving conflicts should one-by-one.
const dialog = new ConflictResolveModal(this.app, filename, conflictCheckResult); return await serialized(`conflict-resolve-ui`, async () => {
dialog.open(); this._log("Merge:open conflict dialog", LOG_LEVEL_VERBOSE);
const selected = await dialog.waitForResult(); const dialog = new ConflictResolveModal(this.app, filename, conflictCheckResult);
if (selected === CANCELLED) { dialog.open();
// Cancelled by UI, or another conflict. const selected = await dialog.waitForResult();
this._log(`Merge: Cancelled ${filename}`, LOG_LEVEL_INFO); if (selected === CANCELLED) {
return false; // Cancelled by UI, or another conflict.
} this._log(`Merge: Cancelled ${filename}`, LOG_LEVEL_INFO);
const testDoc = await this.localDatabase.getDBEntry(filename, { conflicts: true }, false, true, true);
if (testDoc === false) {
this._log(`Merge: Could not read ${filename} from the local database`, LOG_LEVEL_VERBOSE);
return false;
}
if (!testDoc._conflicts) {
this._log(`Merge: Nothing to do ${filename}`, LOG_LEVEL_VERBOSE);
return false;
}
const toDelete = selected;
// const toKeep = conflictCheckResult.left.rev != toDelete ? conflictCheckResult.left.rev : conflictCheckResult.right.rev;
if (toDelete === LEAVE_TO_SUBSEQUENT) {
// Concatenate both conflicted revisions.
// Create a new file by concatenating both conflicted revisions.
const p = conflictCheckResult.diff.map((e) => e[1]).join("");
const delRev = testDoc._conflicts[0];
if (!await this.core.databaseFileAccess.storeContent(filename, p)) {
this._log(`Concatenated content cannot be stored:${filename}`, LOG_LEVEL_NOTICE);
return false; return false;
} }
// 2. As usual, delete the conflicted revision and if there are no conflicts, write the resolved content to the storage. const testDoc = await this.localDatabase.getDBEntry(filename, { conflicts: true }, false, true, true);
if (await this.core.$$resolveConflictByDeletingRev(filename, delRev, "UI Concatenated") == MISSING_OR_ERROR) { if (testDoc === false) {
this._log(`Concatenated saved, but cannot delete conflicted revisions: ${filename}, (${displayRev(delRev)})`, LOG_LEVEL_NOTICE); this._log(`Merge: Could not read ${filename} from the local database`, LOG_LEVEL_VERBOSE);
return false; return false;
} }
} else if (typeof toDelete === "string") { if (!testDoc._conflicts) {
// Select one of the conflicted revision to delete. this._log(`Merge: Nothing to do ${filename}`, LOG_LEVEL_VERBOSE);
if (await this.core.$$resolveConflictByDeletingRev(filename, toDelete, "UI Selected") == MISSING_OR_ERROR) { return false;
}
const toDelete = selected;
// const toKeep = conflictCheckResult.left.rev != toDelete ? conflictCheckResult.left.rev : conflictCheckResult.right.rev;
if (toDelete === LEAVE_TO_SUBSEQUENT) {
// Concatenate both conflicted revisions.
// Create a new file by concatenating both conflicted revisions.
const p = conflictCheckResult.diff.map((e) => e[1]).join("");
const delRev = testDoc._conflicts[0];
if (!(await this.core.databaseFileAccess.storeContent(filename, p))) {
this._log(`Concatenated content cannot be stored:${filename}`, LOG_LEVEL_NOTICE);
return false;
}
// 2. As usual, delete the conflicted revision and if there are no conflicts, write the resolved content to the storage.
if (
(await this.core.$$resolveConflictByDeletingRev(filename, delRev, "UI Concatenated")) ==
MISSING_OR_ERROR
) {
this._log(
`Concatenated saved, but cannot delete conflicted revisions: ${filename}, (${displayRev(delRev)})`,
LOG_LEVEL_NOTICE
);
return false;
}
} else if (typeof toDelete === "string") {
// Select one of the conflicted revision to delete.
if (
(await this.core.$$resolveConflictByDeletingRev(filename, toDelete, "UI Selected")) ==
MISSING_OR_ERROR
) {
this._log(`Merge: Something went wrong: ${filename}, (${toDelete})`, LOG_LEVEL_NOTICE);
return false;
}
} else {
this._log(`Merge: Something went wrong: ${filename}, (${toDelete})`, LOG_LEVEL_NOTICE); this._log(`Merge: Something went wrong: ${filename}, (${toDelete})`, LOG_LEVEL_NOTICE);
return false; return false;
} }
} else { // In here, some merge has been processed.
this._log(`Merge: Something went wrong: ${filename}, (${toDelete})`, LOG_LEVEL_NOTICE); // So we have to run replication if configured.
// TODO: Make this is as a event request
if (this.settings.syncAfterMerge && !this.core.$$isSuspended()) {
await this.core.$$replicateByEvent();
}
// And, check it again.
await this.core.$$queueConflictCheck(filename);
return false; return false;
} });
// In here, some merge has been processed.
// So we have to run replication if configured.
// TODO: Make this is as a event request
if (this.settings.syncAfterMerge && !this.plugin.suspended) {
await this.core.$$waitForReplicationOnce();
}
// And, check it again.
await this.core.$$queueConflictCheck(filename);
return false;
} }
async allConflictCheck() { async allConflictCheck() {
while (await this.pickFileForResolve()); while (await this.pickFileForResolve());
} }
async pickFileForResolve() { async pickFileForResolve() {
const notes: { id: DocumentID, path: FilePathWithPrefix, dispPath: string, mtime: number }[] = []; const notes: { id: DocumentID; path: FilePathWithPrefix; dispPath: string; mtime: number }[] = [];
for await (const doc of this.localDatabase.findAllDocs({ conflicts: true })) { for await (const doc of this.localDatabase.findAllDocs({ conflicts: true })) {
if (!("_conflicts" in doc)) continue; if (!("_conflicts" in doc)) continue;
notes.push({ id: doc._id, path: getPath(doc), dispPath: getPathWithoutPrefix(doc), mtime: doc.mtime }); notes.push({ id: doc._id, path: getPath(doc), dispPath: getPathWithoutPrefix(doc), mtime: doc.mtime });
} }
notes.sort((a, b) => b.mtime - a.mtime); notes.sort((a, b) => b.mtime - a.mtime);
const notesList = notes.map(e => e.dispPath); const notesList = notes.map((e) => e.dispPath);
if (notesList.length == 0) { if (notesList.length == 0) {
this._log("There are no conflicted documents", LOG_LEVEL_NOTICE); this._log("There are no conflicted documents", LOG_LEVEL_NOTICE);
return false; return false;
} }
const target = await this.plugin.confirm.askSelectString("File to resolve conflict", notesList); const target = await this.core.confirm.askSelectString("File to resolve conflict", notesList);
if (target) { if (target) {
const targetItem = notes.find(e => e.dispPath == target)!; const targetItem = notes.find((e) => e.dispPath == target)!;
await this.core.$$queueConflictCheck(targetItem.path); await this.core.$$queueConflictCheck(targetItem.path);
await this.core.$$waitForAllConflictProcessed(); await this.core.$$waitForAllConflictProcessed();
return true; return true;
@@ -107,28 +128,33 @@ export class ModuleInteractiveConflictResolver extends AbstractObsidianModule im
} }
async $allScanStat(): Promise<boolean> { async $allScanStat(): Promise<boolean> {
const notes: { path: string, mtime: number }[] = []; const notes: { path: string; mtime: number }[] = [];
this._log(`Checking conflicted files`, LOG_LEVEL_VERBOSE); this._log(`Checking conflicted files`, LOG_LEVEL_VERBOSE);
for await (const doc of this.localDatabase.findAllDocs({ conflicts: true })) { for await (const doc of this.localDatabase.findAllDocs({ conflicts: true })) {
if (!("_conflicts" in doc)) continue; if (!("_conflicts" in doc)) continue;
notes.push({ path: getPath(doc), mtime: doc.mtime }); notes.push({ path: getPath(doc), mtime: doc.mtime });
} }
if (notes.length > 0) { if (notes.length > 0) {
this.plugin.confirm.askInPopup(`conflicting-detected-on-safety`, `Some files have been left conflicted! Press {HERE} to resolve them, or you can do it later by "Pick a file to resolve conflict`, (anchor) => { this.core.confirm.askInPopup(
anchor.text = "HERE"; `conflicting-detected-on-safety`,
anchor.addEventListener("click", () => { `Some files have been left conflicted! Press {HERE} to resolve them, or you can do it later by "Pick a file to resolve conflict`,
fireAndForget(() => this.allConflictCheck()) (anchor) => {
}); anchor.text = "HERE";
} anchor.addEventListener("click", () => {
fireAndForget(() => this.allConflictCheck());
});
}
);
this._log(
`Some files have been left conflicted! Please resolve them by "Pick a file to resolve conflict". The list is written in the log.`,
LOG_LEVEL_VERBOSE
); );
this._log(`Some files have been left conflicted! Please resolve them by "Pick a file to resolve conflict". The list is written in the log.`, LOG_LEVEL_VERBOSE);
for (const note of notes) { for (const note of notes) {
this._log(`Conflicted: ${note.path}`); this._log(`Conflicted: ${note.path}`);
} }
} else { } else {
this._log(`There are no conflicted files`, LOG_LEVEL_VERBOSE); this._log(`There are no conflicting files`, LOG_LEVEL_VERBOSE);
} }
return true; return true;
} }
} }

View File

@@ -1,8 +1,23 @@
import { computed, reactive, reactiveSource, type ReactiveValue } from "octagonal-wheels/dataobject/reactive"; import { computed, reactive, reactiveSource, type ReactiveValue } from "octagonal-wheels/dataobject/reactive";
import { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE, PREFIXMD_LOGFILE, type DatabaseConnectingStatus, type LOG_LEVEL } from "../../lib/src/common/types.ts"; import {
LOG_LEVEL_DEBUG,
LOG_LEVEL_INFO,
LOG_LEVEL_VERBOSE,
PREFIXMD_LOGFILE,
type DatabaseConnectingStatus,
type LOG_LEVEL,
} from "../../lib/src/common/types.ts";
import { cancelTask, scheduleTask } from "octagonal-wheels/concurrency/task"; import { cancelTask, scheduleTask } from "octagonal-wheels/concurrency/task";
import { fireAndForget, isDirty, throttle } from "../../lib/src/common/utils.ts"; import { fireAndForget, isDirty, throttle } from "../../lib/src/common/utils.ts";
import { collectingChunks, pluginScanningCount, hiddenFilesEventCount, hiddenFilesProcessingCount, type LogEntry, logStore, logMessages } from "../../lib/src/mock_and_interop/stores.ts"; import {
collectingChunks,
pluginScanningCount,
hiddenFilesEventCount,
hiddenFilesProcessingCount,
type LogEntry,
logStore,
logMessages,
} from "../../lib/src/mock_and_interop/stores.ts";
import { eventHub } from "../../lib/src/hub/hub.ts"; import { eventHub } from "../../lib/src/hub/hub.ts";
import { EVENT_FILE_RENAMED, EVENT_LAYOUT_READY, EVENT_LEAF_ACTIVE_CHANGED } from "../../common/events.ts"; import { EVENT_FILE_RENAMED, EVENT_LAYOUT_READY, EVENT_LEAF_ACTIVE_CHANGED } from "../../common/events.ts";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
@@ -11,6 +26,8 @@ import { LOG_LEVEL_NOTICE, setGlobalLogFunction } from "octagonal-wheels/common/
import { QueueProcessor } from "octagonal-wheels/concurrency/processor"; import { QueueProcessor } from "octagonal-wheels/concurrency/processor";
import { LogPaneView, VIEW_TYPE_LOG } from "./Log/LogPaneView.ts"; import { LogPaneView, VIEW_TYPE_LOG } from "./Log/LogPaneView.ts";
import { serialized } from "octagonal-wheels/concurrency/lock"; import { serialized } from "octagonal-wheels/concurrency/lock";
import { $msg } from "src/lib/src/common/i18n.ts";
import { P2PLogCollector } from "../../lib/src/replication/trystero/P2PReplicatorCore.ts";
// This module cannot be a core module because it depends on the Obsidian UI. // This module cannot be a core module because it depends on the Obsidian UI.
@@ -22,15 +39,18 @@ setGlobalLogFunction((message: any, level?: number, key?: string) => {
let recentLogs = [] as string[]; let recentLogs = [] as string[];
// Recent log splicer // Recent log splicer
const recentLogProcessor = new QueueProcessor((logs: string[]) => { const recentLogProcessor = new QueueProcessor(
recentLogs = [...recentLogs, ...logs].splice(-200); (logs: string[]) => {
logMessages.value = recentLogs; recentLogs = [...recentLogs, ...logs].splice(-200);
}, { batchSize: 25, delay: 10, suspended: false, concurrentLimit: 1 }).resumePipeLine(); logMessages.value = recentLogs;
},
{ batchSize: 25, delay: 10, suspended: false, concurrentLimit: 1 }
).resumePipeLine();
// logStore.intercept(e => e.slice(Math.min(e.length - 200, 0))); // logStore.intercept(e => e.slice(Math.min(e.length - 200, 0)));
const showDebugLog = false;
export const MARK_DONE = "\u{2009}\u{2009}";
export class ModuleLog extends AbstractObsidianModule implements IObsidianModule { export class ModuleLog extends AbstractObsidianModule implements IObsidianModule {
registerView = this.plugin.registerView.bind(this.plugin); registerView = this.plugin.registerView.bind(this.plugin);
statusBar?: HTMLElement; statusBar?: HTMLElement;
@@ -41,9 +61,11 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
logHistory?: HTMLDivElement; logHistory?: HTMLDivElement;
messageArea?: HTMLDivElement; messageArea?: HTMLDivElement;
statusBarLabels!: ReactiveValue<{ message: string, status: string }>; statusBarLabels!: ReactiveValue<{ message: string; status: string }>;
statusLog = reactiveSource(""); statusLog = reactiveSource("");
activeFileStatus = reactiveSource("");
notifies: { [key: string]: { notice: Notice; count: number } } = {}; notifies: { [key: string]: { notice: Notice; count: number } } = {};
p2pLogCollector = new P2PLogCollector();
observeForLogs() { observeForLogs() {
const padSpaces = `\u{2007}`.repeat(10); const padSpaces = `\u{2007}`.repeat(10);
@@ -52,7 +74,7 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
const formatted = reactiveSource(""); const formatted = reactiveSource("");
let timer: ReturnType<typeof setTimeout> | undefined = undefined; let timer: ReturnType<typeof setTimeout> | undefined = undefined;
let maxLen = 1; let maxLen = 1;
numI.onChanged(numX => { numI.onChanged((numX) => {
const num = numX.value; const num = numX.value;
const numLen = `${Math.abs(num)}`.length + 1; const numLen = `${Math.abs(num)}`.length + 1;
maxLen = maxLen < numLen ? numLen : maxLen; maxLen = maxLen < numLen ? numLen : maxLen;
@@ -63,30 +85,30 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
maxLen = 1; maxLen = 1;
}, 3000); }, 3000);
} }
formatted.value = ` ${mark}${`${padSpaces}${num}`.slice(-(maxLen))}`; formatted.value = ` ${mark}${`${padSpaces}${num}`.slice(-maxLen)}`;
}) });
return computed(() => formatted.value); return computed(() => formatted.value);
} }
const labelReplication = padLeftSpComputed(this.plugin.replicationResultCount, `📥`); const labelReplication = padLeftSpComputed(this.core.replicationResultCount, `📥`);
const labelDBCount = padLeftSpComputed(this.plugin.databaseQueueCount, `📄`); const labelDBCount = padLeftSpComputed(this.core.databaseQueueCount, `📄`);
const labelStorageCount = padLeftSpComputed(this.plugin.storageApplyingCount, `💾`); const labelStorageCount = padLeftSpComputed(this.core.storageApplyingCount, `💾`);
const labelChunkCount = padLeftSpComputed(collectingChunks, `🧩`); const labelChunkCount = padLeftSpComputed(collectingChunks, `🧩`);
const labelPluginScanCount = padLeftSpComputed(pluginScanningCount, `🔌`); const labelPluginScanCount = padLeftSpComputed(pluginScanningCount, `🔌`);
const labelConflictProcessCount = padLeftSpComputed(this.plugin.conflictProcessQueueCount, `🔩`); const labelConflictProcessCount = padLeftSpComputed(this.core.conflictProcessQueueCount, `🔩`);
const hiddenFilesCount = reactive(() => hiddenFilesEventCount.value + hiddenFilesProcessingCount.value); const hiddenFilesCount = reactive(() => hiddenFilesEventCount.value - hiddenFilesProcessingCount.value);
const labelHiddenFilesCount = padLeftSpComputed(hiddenFilesCount, `⚙️`) const labelHiddenFilesCount = padLeftSpComputed(hiddenFilesCount, `⚙️`);
const queueCountLabelX = reactive(() => { const queueCountLabelX = reactive(() => {
return `${labelReplication()}${labelDBCount()}${labelStorageCount()}${labelChunkCount()}${labelPluginScanCount()}${labelHiddenFilesCount()}${labelConflictProcessCount()}`; return `${labelReplication()}${labelDBCount()}${labelStorageCount()}${labelChunkCount()}${labelPluginScanCount()}${labelHiddenFilesCount()}${labelConflictProcessCount()}`;
}) });
const queueCountLabel = () => queueCountLabelX.value; const queueCountLabel = () => queueCountLabelX.value;
const requestingStatLabel = computed(() => { const requestingStatLabel = computed(() => {
const diff = this.plugin.requestCount.value - this.plugin.responseCount.value; const diff = this.core.requestCount.value - this.core.responseCount.value;
return diff != 0 ? "📲 " : ""; return diff != 0 ? "📲 " : "";
}) });
const replicationStatLabel = computed(() => { const replicationStatLabel = computed(() => {
const e = this.plugin.replicationStat.value; const e = this.core.replicationStat.value;
const sent = e.sent; const sent = e.sent;
const arrived = e.arrived; const arrived = e.arrived;
const maxPullSeq = e.maxPullSeq; const maxPullSeq = e.maxPullSeq;
@@ -97,10 +119,10 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
let pullLast = ""; let pullLast = "";
let w = ""; let w = "";
const labels: Partial<Record<DatabaseConnectingStatus, string>> = { const labels: Partial<Record<DatabaseConnectingStatus, string>> = {
"CONNECTED": "⚡", CONNECTED: "⚡",
"JOURNAL_SEND": "📦↑", JOURNAL_SEND: "📦↑",
"JOURNAL_RECEIVE": "📦↓", JOURNAL_RECEIVE: "📦↓",
} };
switch (e.syncStatus) { switch (e.syncStatus) {
case "CLOSED": case "CLOSED":
case "COMPLETED": case "COMPLETED":
@@ -117,8 +139,18 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
case "JOURNAL_SEND": case "JOURNAL_SEND":
case "JOURNAL_RECEIVE": case "JOURNAL_RECEIVE":
w = labels[e.syncStatus] || "⚡"; w = labels[e.syncStatus] || "⚡";
pushLast = ((lastSyncPushSeq == 0) ? "" : (lastSyncPushSeq >= maxPushSeq ? " (LIVE)" : ` (${maxPushSeq - lastSyncPushSeq})`)); pushLast =
pullLast = ((lastSyncPullSeq == 0) ? "" : (lastSyncPullSeq >= maxPullSeq ? " (LIVE)" : ` (${maxPullSeq - lastSyncPullSeq})`)); lastSyncPushSeq == 0
? ""
: lastSyncPushSeq >= maxPushSeq
? " (LIVE)"
: ` (${maxPushSeq - lastSyncPushSeq})`;
pullLast =
lastSyncPullSeq == 0
? ""
: lastSyncPullSeq >= maxPullSeq
? " (LIVE)"
: ` (${maxPullSeq - lastSyncPullSeq})`;
break; break;
case "ERRORED": case "ERRORED":
w = "⚠"; w = "⚠";
@@ -127,44 +159,50 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
w = "?"; w = "?";
} }
return { w, sent, pushLast, arrived, pullLast }; return { w, sent, pushLast, arrived, pullLast };
}) });
const labelProc = padLeftSpComputed(this.plugin.processing, ``); const labelProc = padLeftSpComputed(this.core.processing, ``);
const labelPend = padLeftSpComputed(this.plugin.totalQueued, `🛫`); const labelPend = padLeftSpComputed(this.core.totalQueued, `🛫`);
const labelInBatchDelay = padLeftSpComputed(this.plugin.batched, `📬`); const labelInBatchDelay = padLeftSpComputed(this.core.batched, `📬`);
const waitingLabel = computed(() => { const waitingLabel = computed(() => {
return `${labelProc()}${labelPend()}${labelInBatchDelay()}`; return `${labelProc()}${labelPend()}${labelInBatchDelay()}`;
}) });
const statusLineLabel = computed(() => { const statusLineLabel = computed(() => {
const { w, sent, pushLast, arrived, pullLast } = replicationStatLabel(); const { w, sent, pushLast, arrived, pullLast } = replicationStatLabel();
const queued = queueCountLabel(); const queued = queueCountLabel();
const waiting = waitingLabel(); const waiting = waitingLabel();
const networkActivity = requestingStatLabel(); const networkActivity = requestingStatLabel();
const p2p = this.p2pLogCollector.p2pReplicationLine.value;
return { return {
message: `${networkActivity}Sync: ${w}${sent}${pushLast}${arrived}${pullLast}${waiting}${queued}`, message: `${networkActivity}Sync: ${w}${sent}${pushLast}${arrived}${pullLast}${waiting}${queued}${p2p == "" ? "" : "\n" + p2p}`,
}; };
}) });
const statusBarLabels = reactive(() => {
const scheduleMessage = this.plugin.isReloadingScheduled ? `WARNING! RESTARTING OBSIDIAN IS SCHEDULED!\n` : "";
const { message } = statusLineLabel();
const status = scheduleMessage + this.statusLog.value;
const statusBarLabels = reactive(() => {
const scheduleMessage = this.core.$$isReloadingScheduled()
? `WARNING! RESTARTING OBSIDIAN IS SCHEDULED!\n`
: "";
const { message } = statusLineLabel();
const fileStatus = this.activeFileStatus.value;
const status = scheduleMessage + this.statusLog.value;
const fileStatusIcon = `${fileStatus && this.settings.hideFileWarningNotice ? " ⛔ SKIP" : ""}`;
return { return {
message, status message: `${message}${fileStatusIcon}`,
} status,
}) };
});
this.statusBarLabels = statusBarLabels; this.statusBarLabels = statusBarLabels;
const applyToDisplay = throttle((label: typeof statusBarLabels.value) => { const applyToDisplay = throttle((label: typeof statusBarLabels.value) => {
// const v = label; // const v = label;
this.applyStatusBarText(); this.applyStatusBarText();
}, 20); }, 20);
statusBarLabels.onChanged(label => applyToDisplay(label.value)) statusBarLabels.onChanged((label) => applyToDisplay(label.value));
} }
$everyOnload(): Promise<boolean> { $everyOnload(): Promise<boolean> {
eventHub.onEvent(EVENT_LEAF_ACTIVE_CHANGED, () => this.onActiveLeafChange()); eventHub.onEvent(EVENT_LEAF_ACTIVE_CHANGED, () => this.onActiveLeafChange());
eventHub.onceEvent(EVENT_LAYOUT_READY, () => this.onActiveLeafChange()); eventHub.onceEvent(EVENT_LAYOUT_READY, () => this.onActiveLeafChange());
return Promise.resolve(true); return Promise.resolve(true);
} }
adjustStatusDivPosition() { adjustStatusDivPosition() {
@@ -181,27 +219,31 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
const thisFile = this.app.workspace.getActiveFile(); const thisFile = this.app.workspace.getActiveFile();
if (!thisFile) return ""; if (!thisFile) return "";
// Case Sensitivity // Case Sensitivity
if (this.core.shouldCheckCaseInsensitive) { if (this.core.$$shouldCheckCaseInsensitive()) {
const f = this.core.storageAccess.getFiles().map(e => e.path).filter(e => e.toLowerCase() == thisFile.path.toLowerCase()); const f = this.core.storageAccess
.getFiles()
.map((e) => e.path)
.filter((e) => e.toLowerCase() == thisFile.path.toLowerCase());
if (f.length > 1) return "Not synchronised: There are multiple files with the same name"; if (f.length > 1) return "Not synchronised: There are multiple files with the same name";
} }
if (!await this.core.$$isTargetFile(thisFile.path)) return "Not synchronised: not a target file"; if (!(await this.core.$$isTargetFile(thisFile.path))) return "Not synchronised: not a target file";
if (this.core.$$isFileSizeExceeded(thisFile.stat.size)) return "Not synchronised: File size exceeded"; if (this.core.$$isFileSizeExceeded(thisFile.stat.size)) return "Not synchronised: File size exceeded";
return ""; return "";
} }
async setFileStatus() { async setFileStatus() {
this.messageArea!.innerText = await this.getActiveFileStatus(); const fileStatus = await this.getActiveFileStatus();
this.activeFileStatus.value = fileStatus;
this.messageArea!.innerText = this.settings.hideFileWarningNotice ? "" : fileStatus;
} }
onActiveLeafChange() { onActiveLeafChange() {
fireAndForget(async () => { fireAndForget(async () => {
this.adjustStatusDivPosition(); this.adjustStatusDivPosition();
await this.setFileStatus(); await this.setFileStatus();
}); });
} }
nextFrameQueue: ReturnType<typeof requestAnimationFrame> | undefined = undefined; nextFrameQueue: ReturnType<typeof requestAnimationFrame> | undefined = undefined;
logLines: { ttl: number, message: string }[] = []; logLines: { ttl: number; message: string }[] = [];
applyStatusBarText() { applyStatusBarText() {
if (this.nextFrameQueue) { if (this.nextFrameQueue) {
@@ -222,10 +264,13 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
if (this.settings?.showStatusOnEditor && this.statusDiv) { if (this.settings?.showStatusOnEditor && this.statusDiv) {
if (this.settings.showLongerLogInsideEditor) { if (this.settings.showLongerLogInsideEditor) {
const now = new Date().getTime(); const now = new Date().getTime();
this.logLines = this.logLines.filter(e => e.ttl > now); this.logLines = this.logLines.filter((e) => e.ttl > now);
const minimumNext = this.logLines.reduce((a, b) => a < b.ttl ? a : b.ttl, Number.MAX_SAFE_INTEGER); const minimumNext = this.logLines.reduce(
(a, b) => (a < b.ttl ? a : b.ttl),
Number.MAX_SAFE_INTEGER
);
if (this.logLines.length > 0) setTimeout(() => this.applyStatusBarText(), minimumNext - now); if (this.logLines.length > 0) setTimeout(() => this.applyStatusBarText(), minimumNext - now);
const recent = this.logLines.map(e => e.message); const recent = this.logLines.map((e) => e.message);
const recentLogs = recent.reverse().join("\n"); const recentLogs = recent.reverse().join("\n");
if (isDirty("recentLogs", recentLogs)) this.logHistory!.innerText = recentLogs; if (isDirty("recentLogs", recentLogs)) this.logHistory!.innerText = recentLogs;
} }
@@ -237,14 +282,16 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
} }
}); });
scheduleTask("log-hide", 3000, () => { this.statusLog.value = "" }); scheduleTask("log-hide", 3000, () => {
this.statusLog.value = "";
});
} }
$allStartOnUnload(): Promise<boolean> { $allStartOnUnload(): Promise<boolean> {
if (this.statusDiv) { if (this.statusDiv) {
this.statusDiv.remove(); this.statusDiv.remove();
} }
document.querySelectorAll(`.livesync-status`)?.forEach(e => e.remove()); document.querySelectorAll(`.livesync-status`)?.forEach((e) => e.remove());
return Promise.resolve(true); return Promise.resolve(true);
} }
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
@@ -255,7 +302,7 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
<path d="m106 346v44h70v-44zm45 16h-20v-8h20z"/> <path d="m106 346v44h70v-44zm45 16h-20v-8h20z"/>
</g>` </g>`
); );
this.addRibbonIcon("view-log", "Show log", () => { this.addRibbonIcon("view-log", $msg("moduleLog.showLog"), () => {
void this.core.$$showView(VIEW_TYPE_LOG); void this.core.$$showView(VIEW_TYPE_LOG);
}).addClass("livesync-ribbon-showlog"); }).addClass("livesync-ribbon-showlog");
@@ -264,22 +311,28 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
name: "Show log", name: "Show log",
callback: () => { callback: () => {
void this.core.$$showView(VIEW_TYPE_LOG); void this.core.$$showView(VIEW_TYPE_LOG);
} },
}); });
this.registerView( this.registerView(VIEW_TYPE_LOG, (leaf) => new LogPaneView(leaf, this.plugin));
VIEW_TYPE_LOG,
(leaf) => new LogPaneView(leaf, this.plugin)
);
return Promise.resolve(true); return Promise.resolve(true);
} }
$everyOnloadAfterLoadSettings(): Promise<boolean> { $everyOnloadAfterLoadSettings(): Promise<boolean> {
logStore.pipeTo(new QueueProcessor(logs => logs.forEach(e => this.core.$$addLog(e.message, e.level, e.key)), { suspended: false, batchSize: 20, concurrentLimit: 1, delay: 0 })).startPipeline(); logStore
eventHub.onEvent(EVENT_FILE_RENAMED, (evt: CustomEvent<{ oldPath: string, newPath: string }>) => { .pipeTo(
new QueueProcessor((logs) => logs.forEach((e) => this.core.$$addLog(e.message, e.level, e.key)), {
suspended: false,
batchSize: 20,
concurrentLimit: 1,
delay: 0,
})
)
.startPipeline();
eventHub.onEvent(EVENT_FILE_RENAMED, (data) => {
void this.setFileStatus(); void this.setFileStatus();
}); });
const w = document.querySelectorAll(`.livesync-status`); const w = document.querySelectorAll(`.livesync-status`);
w.forEach(e => e.remove()); w.forEach((e) => e.remove());
this.observeForLogs(); this.observeForLogs();
@@ -290,7 +343,7 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
this.logHistory = this.statusDiv.createDiv({ cls: "livesync-status-loghistory" }); this.logHistory = this.statusDiv.createDiv({ cls: "livesync-status-loghistory" });
eventHub.onEvent(EVENT_LAYOUT_READY, () => this.adjustStatusDivPosition()); eventHub.onEvent(EVENT_LAYOUT_READY, () => this.adjustStatusDivPosition());
if (this.settings?.showStatusOnStatusbar) { if (this.settings?.showStatusOnStatusbar) {
this.statusBar = this.plugin.addStatusBarItem(); this.statusBar = this.core.addStatusBarItem();
this.statusBar.addClass("syncstatusbar"); this.statusBar.addClass("syncstatusbar");
} }
this.adjustStatusDivPosition(); this.adjustStatusDivPosition();
@@ -298,18 +351,23 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
} }
writeLogToTheFile(now: Date, vaultName: string, newMessage: string) { writeLogToTheFile(now: Date, vaultName: string, newMessage: string) {
fireAndForget(() => serialized("writeLog", async () => { fireAndForget(() =>
const time = now.toISOString().split("T")[0]; serialized("writeLog", async () => {
const logDate = `${PREFIXMD_LOGFILE}${time}.md`; const time = now.toISOString().split("T")[0];
const file = await this.core.storageAccess.isExists(normalizePath(logDate)); const logDate = `${PREFIXMD_LOGFILE}${time}.md`;
if (!file) { const file = await this.core.storageAccess.isExists(normalizePath(logDate));
await this.core.storageAccess.appendHiddenFile(normalizePath(logDate), "```\n"); if (!file) {
} await this.core.storageAccess.appendHiddenFile(normalizePath(logDate), "```\n");
await this.core.storageAccess.appendHiddenFile(normalizePath(logDate), vaultName + ":" + newMessage + "\n"); }
})); await this.core.storageAccess.appendHiddenFile(
normalizePath(logDate),
vaultName + ":" + newMessage + "\n"
);
})
);
} }
$$addLog(message: any, level: LOG_LEVEL = LOG_LEVEL_INFO, key = ""): void { $$addLog(message: any, level: LOG_LEVEL = LOG_LEVEL_INFO, key = ""): void {
if (level == LOG_LEVEL_DEBUG) { if (level == LOG_LEVEL_DEBUG && !showDebugLog) {
return; return;
} }
if (level < LOG_LEVEL_INFO && this.settings && this.settings.lessInformationInLog) { if (level < LOG_LEVEL_INFO && this.settings && this.settings.lessInformationInLog) {
@@ -318,10 +376,15 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
if (this.settings && !this.settings.showVerboseLog && level == LOG_LEVEL_VERBOSE) { if (this.settings && !this.settings.showVerboseLog && level == LOG_LEVEL_VERBOSE) {
return; return;
} }
const vaultName = this.plugin.$$getVaultName(); const vaultName = this.core.$$getVaultName();
const now = new Date(); const now = new Date();
const timestamp = now.toLocaleString(); const timestamp = now.toLocaleString();
const messageContent = typeof message == "string" ? message : message instanceof Error ? `${message.name}:${message.message}` : JSON.stringify(message, null, 2); const messageContent =
typeof message == "string"
? message
: message instanceof Error
? `${message.name}:${message.message}`
: JSON.stringify(message, null, 2);
if (message instanceof Error) { if (message instanceof Error) {
// debugger; // debugger;
console.dir(message.stack); console.dir(message.stack);
@@ -342,7 +405,7 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
if (!key) key = messageContent; if (!key) key = messageContent;
if (key in this.notifies) { if (key in this.notifies) {
// @ts-ignore // @ts-ignore
const isShown = this.notifies[key].notice.noticeEl?.isShown() const isShown = this.notifies[key].notice.noticeEl?.isShown();
if (!isShown) { if (!isShown) {
this.notifies[key].notice = new Notice(messageContent, 0); this.notifies[key].notice = new Notice(messageContent, 0);
} }
@@ -361,17 +424,17 @@ export class ModuleLog extends AbstractObsidianModule implements IObsidianModule
}; };
} }
const timeout = 5000; const timeout = 5000;
scheduleTask(`notify-${key}`, timeout, () => { if (!key.startsWith("keepalive-") || messageContent.indexOf(MARK_DONE) !== -1) {
const notify = this.notifies[key].notice; scheduleTask(`notify-${key}`, timeout, () => {
delete this.notifies[key]; const notify = this.notifies[key].notice;
try { delete this.notifies[key];
notify.hide(); try {
} catch { notify.hide();
// NO OP } catch {
} // NO OP
}) }
});
}
} }
} }
} }

View File

@@ -1,25 +1,21 @@
import { type TFile } from "obsidian"; import { type TFile } from "obsidian";
import { eventHub, EVENT_REQUEST_SHOW_HISTORY } from "../../common/events.ts"; import { eventHub } from "../../common/events.ts";
import { EVENT_REQUEST_SHOW_HISTORY } from "../../common/obsidianEvents.ts";
import type { FilePathWithPrefix, LoadedEntry, DocumentID } from "../../lib/src/common/types.ts"; import type { FilePathWithPrefix, LoadedEntry, DocumentID } from "../../lib/src/common/types.ts";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { DocumentHistoryModal } from "./DocumentHistory/DocumentHistoryModal.ts"; import { DocumentHistoryModal } from "./DocumentHistory/DocumentHistoryModal.ts";
import { getPath } from "../../common/utils.ts"; import { getPath } from "../../common/utils.ts";
import { fireAndForget } from "octagonal-wheels/promises"; import { fireAndForget } from "octagonal-wheels/promises";
export class ModuleObsidianDocumentHistory extends AbstractObsidianModule implements IObsidianModule { export class ModuleObsidianDocumentHistory extends AbstractObsidianModule implements IObsidianModule {
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
this.addCommand({ this.addCommand({
id: "livesync-history", id: "livesync-history",
name: "Show history", name: "Show history",
callback: () => { callback: () => {
const file = this.core.$$getActiveFilePath(); const file = this.core.$$getActiveFilePath();
if (file) this.showHistory(file, undefined); if (file) this.showHistory(file, undefined);
} },
}); });
this.addCommand({ this.addCommand({
@@ -29,9 +25,12 @@ export class ModuleObsidianDocumentHistory extends AbstractObsidianModule implem
fireAndForget(async () => await this.fileHistory()); fireAndForget(async () => await this.fileHistory());
}, },
}); });
eventHub.on(EVENT_REQUEST_SHOW_HISTORY, ({ file, fileOnDB }: { file: TFile, fileOnDB: LoadedEntry }) => { eventHub.onEvent(
this.showHistory(file, fileOnDB._id); EVENT_REQUEST_SHOW_HISTORY,
}) ({ file, fileOnDB }: { file: TFile | FilePathWithPrefix; fileOnDB: LoadedEntry }) => {
this.showHistory(file, fileOnDB._id);
}
);
return Promise.resolve(true); return Promise.resolve(true);
} }
@@ -40,17 +39,16 @@ export class ModuleObsidianDocumentHistory extends AbstractObsidianModule implem
} }
async fileHistory() { async fileHistory() {
const notes: { id: DocumentID, path: FilePathWithPrefix, dispPath: string, mtime: number }[] = []; const notes: { id: DocumentID; path: FilePathWithPrefix; dispPath: string; mtime: number }[] = [];
for await (const doc of this.localDatabase.findAllDocs()) { for await (const doc of this.localDatabase.findAllDocs()) {
notes.push({ id: doc._id, path: getPath(doc), dispPath: getPath(doc), mtime: doc.mtime }); notes.push({ id: doc._id, path: getPath(doc), dispPath: getPath(doc), mtime: doc.mtime });
} }
notes.sort((a, b) => b.mtime - a.mtime); notes.sort((a, b) => b.mtime - a.mtime);
const notesList = notes.map(e => e.dispPath); const notesList = notes.map((e) => e.dispPath);
const target = await this.plugin.confirm.askSelectString("File to view History", notesList); const target = await this.core.confirm.askSelectString("File to view History", notesList);
if (target) { if (target) {
const targetId = notes.find(e => e.dispPath == target)!; const targetId = notes.find((e) => e.dispPath == target)!;
this.showHistory(targetId.path, targetId.id); this.showHistory(targetId.path, targetId.id);
} }
} }
} }

View File

@@ -1,27 +1,70 @@
import { type IObsidianModule, AbstractObsidianModule } from "../AbstractObsidianModule.ts"; import { type IObsidianModule, AbstractObsidianModule } from "../AbstractObsidianModule.ts";
// import { PouchDB } from "../../lib/src/pouchdb/pouchdb-browser"; // import { PouchDB } from "../../lib/src/pouchdb/pouchdb-browser";
import { EVENT_REQUEST_RELOAD_SETTING_TAB, EVENT_SETTING_SAVED, eventHub } from "../../common/events"; import { EVENT_REQUEST_RELOAD_SETTING_TAB, EVENT_SETTING_SAVED, eventHub } from "../../common/events";
import { type BucketSyncSetting, type ConfigPassphraseStore, type CouchDBConnection, DEFAULT_SETTINGS, type ObsidianLiveSyncSettings, SALT_OF_PASSPHRASE } from "../../lib/src/common/types"; import {
type BucketSyncSetting,
ChunkAlgorithmNames,
type ConfigPassphraseStore,
type CouchDBConnection,
DEFAULT_SETTINGS,
type ObsidianLiveSyncSettings,
SALT_OF_PASSPHRASE,
} from "../../lib/src/common/types";
import { LOG_LEVEL_NOTICE, LOG_LEVEL_URGENT } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_NOTICE, LOG_LEVEL_URGENT } from "octagonal-wheels/common/logger";
import { encrypt, tryDecrypt } from "octagonal-wheels/encryption"; import { encrypt, tryDecrypt } from "octagonal-wheels/encryption";
import { setLang } from "../../lib/src/common/i18n"; import { $msg, setLang } from "../../lib/src/common/i18n";
import { isCloudantURI } from "../../lib/src/pouchdb/utils_couchdb"; import { isCloudantURI } from "../../lib/src/pouchdb/utils_couchdb";
import { getLanguage } from "obsidian";
import { SUPPORTED_I18N_LANGS, type I18N_LANGS } from "../../lib/src/common/rosetta.ts";
export class ModuleObsidianSettings extends AbstractObsidianModule implements IObsidianModule { export class ModuleObsidianSettings extends AbstractObsidianModule implements IObsidianModule {
async $everyOnLayoutReady(): Promise<boolean> {
getPassphrase(settings: ObsidianLiveSyncSettings) { let isChanged = false;
const methods: Record<ConfigPassphraseStore, (() => Promise<string | false>)> = { if (this.settings.displayLanguage == "") {
"": () => Promise.resolve("*"), const obsidianLanguage = getLanguage();
"LOCALSTORAGE": () => Promise.resolve(localStorage.getItem("ls-setting-passphrase") ?? false), if (
"ASK_AT_LAUNCH": () => this.plugin.confirm.askString("Passphrase", "passphrase", "") SUPPORTED_I18N_LANGS.indexOf(obsidianLanguage) !== -1 && // Check if the language is supported
obsidianLanguage != this.settings.displayLanguage && // Check if the language is different from the current setting
this.settings.displayLanguage != ""
) {
// Check if the current setting is not empty (Means migrated or installed).
this.settings.displayLanguage = obsidianLanguage as I18N_LANGS;
isChanged = true;
setLang(this.settings.displayLanguage);
} else if (this.settings.displayLanguage == "") {
this.settings.displayLanguage = "def";
setLang(this.settings.displayLanguage);
await this.core.$$saveSettingData();
}
} }
if (isChanged) {
const revert = $msg("dialog.yourLanguageAvailable.btnRevertToDefault");
if (
(await this.core.confirm.askSelectStringDialogue($msg(`dialog.yourLanguageAvailable`), ["OK", revert], {
defaultAction: "OK",
title: $msg(`dialog.yourLanguageAvailable.Title`),
})) == revert
) {
this.settings.displayLanguage = "def";
setLang(this.settings.displayLanguage);
}
await this.core.$$saveSettingData();
}
return true;
}
getPassphrase(settings: ObsidianLiveSyncSettings) {
const methods: Record<ConfigPassphraseStore, () => Promise<string | false>> = {
"": () => Promise.resolve("*"),
LOCALSTORAGE: () => Promise.resolve(localStorage.getItem("ls-setting-passphrase") ?? false),
ASK_AT_LAUNCH: () => this.core.confirm.askString("Passphrase", "passphrase", ""),
};
const method = settings.configPassphraseStore; const method = settings.configPassphraseStore;
const methodFunc = method in methods ? methods[method] : methods[""]; const methodFunc = method in methods ? methods[method] : methods[""];
return methodFunc(); return methodFunc();
} }
$$saveDeviceAndVaultName(): void { $$saveDeviceAndVaultName(): void {
const lsKey = "obsidian-live-sync-vaultanddevicename-" + this.plugin.$$getVaultName(); const lsKey = "obsidian-live-sync-vaultanddevicename-" + this.core.$$getVaultName();
localStorage.setItem(lsKey, this.plugin.deviceAndVaultName || ""); localStorage.setItem(lsKey, this.core.$$getDeviceAndVaultName() || "");
} }
usedPassphrase = ""; usedPassphrase = "";
@@ -29,7 +72,6 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
this.usedPassphrase = ""; this.usedPassphrase = "";
} }
async decryptConfigurationItem(encrypted: string, passphrase: string) { async decryptConfigurationItem(encrypted: string, passphrase: string) {
const dec = await tryDecrypt(encrypted, passphrase + SALT_OF_PASSPHRASE, false); const dec = await tryDecrypt(encrypted, passphrase + SALT_OF_PASSPHRASE, false);
if (dec) { if (dec) {
@@ -39,7 +81,6 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
return false; return false;
} }
async encryptConfigurationItem(src: string, settings: ObsidianLiveSyncSettings) { async encryptConfigurationItem(src: string, settings: ObsidianLiveSyncSettings) {
if (this.usedPassphrase != "") { if (this.usedPassphrase != "") {
return await encrypt(src, this.usedPassphrase + SALT_OF_PASSPHRASE, false); return await encrypt(src, this.usedPassphrase + SALT_OF_PASSPHRASE, false);
@@ -47,7 +88,10 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
const passphrase = await this.getPassphrase(settings); const passphrase = await this.getPassphrase(settings);
if (passphrase === false) { if (passphrase === false) {
this._log("Could not determine passphrase to save data.json! You probably make the configuration sure again!", LOG_LEVEL_URGENT); this._log(
"Failed to obtain passphrase when saving data.json! Please verify the configuration.",
LOG_LEVEL_URGENT
);
return ""; return "";
} }
const dec = await encrypt(src, passphrase + SALT_OF_PASSPHRASE, false); const dec = await encrypt(src, passphrase + SALT_OF_PASSPHRASE, false);
@@ -60,17 +104,22 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
} }
get appId() { get appId() {
return `${("appId" in this.app ? this.app.appId : "")}`; return `${"appId" in this.app ? this.app.appId : ""}`;
} }
async $$saveSettingData() { async $$saveSettingData() {
this.plugin.$$saveDeviceAndVaultName(); this.core.$$saveDeviceAndVaultName();
const settings = { ...this.settings }; const settings = { ...this.settings };
settings.deviceAndVaultName = ""; settings.deviceAndVaultName = "";
if (this.usedPassphrase == "" && !await this.getPassphrase(settings)) { if (this.usedPassphrase == "" && !(await this.getPassphrase(settings))) {
this._log("Could not determine passphrase for saving data.json! Our data.json have insecure items!", LOG_LEVEL_NOTICE); this._log("Failed to retrieve passphrase. data.json contains unencrypted items!", LOG_LEVEL_NOTICE);
} else { } else {
if (settings.couchDB_PASSWORD != "" || settings.couchDB_URI != "" || settings.couchDB_USER != "" || settings.couchDB_DBNAME) { if (
settings.couchDB_PASSWORD != "" ||
settings.couchDB_URI != "" ||
settings.couchDB_USER != "" ||
settings.couchDB_DBNAME
) {
const connectionSetting: CouchDBConnection & BucketSyncSetting = { const connectionSetting: CouchDBConnection & BucketSyncSetting = {
couchDB_DBNAME: settings.couchDB_DBNAME, couchDB_DBNAME: settings.couchDB_DBNAME,
couchDB_PASSWORD: settings.couchDB_PASSWORD, couchDB_PASSWORD: settings.couchDB_PASSWORD,
@@ -81,9 +130,22 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
endpoint: settings.endpoint, endpoint: settings.endpoint,
region: settings.region, region: settings.region,
secretKey: settings.secretKey, secretKey: settings.secretKey,
useCustomRequestHandler: settings.useCustomRequestHandler useCustomRequestHandler: settings.useCustomRequestHandler,
bucketCustomHeaders: settings.bucketCustomHeaders,
couchDB_CustomHeaders: settings.couchDB_CustomHeaders,
useJWT: settings.useJWT,
jwtKey: settings.jwtKey,
jwtAlgorithm: settings.jwtAlgorithm,
jwtKid: settings.jwtKid,
jwtExpDuration: settings.jwtExpDuration,
jwtSub: settings.jwtSub,
useRequestAPI: settings.useRequestAPI,
bucketPrefix: settings.bucketPrefix,
}; };
settings.encryptedCouchDBConnection = await this.encryptConfigurationItem(JSON.stringify(connectionSetting), settings); settings.encryptedCouchDBConnection = await this.encryptConfigurationItem(
JSON.stringify(connectionSetting),
settings
);
settings.couchDB_PASSWORD = ""; settings.couchDB_PASSWORD = "";
settings.couchDB_DBNAME = ""; settings.couchDB_DBNAME = "";
settings.couchDB_URI = ""; settings.couchDB_URI = "";
@@ -98,7 +160,6 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
settings.encryptedPassphrase = await this.encryptConfigurationItem(settings.passphrase, settings); settings.encryptedPassphrase = await this.encryptConfigurationItem(settings.passphrase, settings);
settings.passphrase = ""; settings.passphrase = "";
} }
} }
await this.core.saveData(settings); await this.core.saveData(settings);
eventHub.emitEvent(EVENT_SETTING_SAVED, settings); eventHub.emitEvent(EVENT_SETTING_SAVED, settings);
@@ -127,7 +188,7 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
} }
const passphrase = await this.getPassphrase(settings); const passphrase = await this.getPassphrase(settings);
if (passphrase === false) { if (passphrase === false) {
this._log("Could not determine passphrase for reading data.json! DO NOT synchronize with the remote before making sure your configuration is!", LOG_LEVEL_URGENT); this._log("No passphrase found for data.json! Verify configuration before syncing.", LOG_LEVEL_URGENT);
} else { } else {
if (settings.encryptedCouchDBConnection) { if (settings.encryptedCouchDBConnection) {
const keys = [ const keys = [
@@ -139,17 +200,23 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
"bucket", "bucket",
"endpoint", "endpoint",
"region", "region",
"secretKey"] as (keyof CouchDBConnection | keyof BucketSyncSetting)[]; "secretKey",
const decrypted = this.tryDecodeJson(await this.decryptConfigurationItem(settings.encryptedCouchDBConnection, passphrase)) as (CouchDBConnection & BucketSyncSetting); ] as (keyof CouchDBConnection | keyof BucketSyncSetting)[];
const decrypted = this.tryDecodeJson(
await this.decryptConfigurationItem(settings.encryptedCouchDBConnection, passphrase)
) as CouchDBConnection & BucketSyncSetting;
if (decrypted) { if (decrypted) {
for (const key of keys) { for (const key of keys) {
if (key in decrypted) { if (key in decrypted) {
//@ts-ignore //@ts-ignore
settings[key] = decrypted[key] settings[key] = decrypted[key];
} }
} }
} else { } else {
this._log("Could not decrypt passphrase for reading data.json! DO NOT synchronize with the remote before making sure your configuration is!", LOG_LEVEL_URGENT); this._log(
"Failed to decrypt passphrase from data.json! Ensure configuration is correct before syncing with remote.",
LOG_LEVEL_URGENT
);
for (const key of keys) { for (const key of keys) {
//@ts-ignore //@ts-ignore
settings[key] = ""; settings[key] = "";
@@ -162,13 +229,16 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
if (decrypted) { if (decrypted) {
settings.passphrase = decrypted; settings.passphrase = decrypted;
} else { } else {
this._log("Could not decrypt passphrase for reading data.json! DO NOT synchronize with the remote before making sure your configuration is!", LOG_LEVEL_URGENT); this._log(
"Failed to decrypt passphrase from data.json! Ensure configuration is correct before syncing with remote.",
LOG_LEVEL_URGENT
);
settings.passphrase = ""; settings.passphrase = "";
} }
} }
} }
this.settings = settings; this.settings = settings;
setLang(this.settings.displayLanguage); setLang(this.settings.displayLanguage);
if ("workingEncrypt" in this.settings) delete this.settings.workingEncrypt; if ("workingEncrypt" in this.settings) delete this.settings.workingEncrypt;
@@ -182,25 +252,44 @@ export class ModuleObsidianSettings extends AbstractObsidianModule implements IO
// So, use history is always enabled. // So, use history is always enabled.
this.settings.useHistory = true; this.settings.useHistory = true;
const lsKey = "obsidian-live-sync-vaultanddevicename-" + this.plugin.$$getVaultName(); const lsKey = "obsidian-live-sync-vaultanddevicename-" + this.core.$$getVaultName();
if (this.settings.deviceAndVaultName != "") { if (this.settings.deviceAndVaultName != "") {
if (!localStorage.getItem(lsKey)) { if (!localStorage.getItem(lsKey)) {
this.core.deviceAndVaultName = this.settings.deviceAndVaultName; this.core.$$setDeviceAndVaultName(this.settings.deviceAndVaultName);
localStorage.setItem(lsKey, this.core.deviceAndVaultName); this.$$saveDeviceAndVaultName();
this.settings.deviceAndVaultName = ""; this.settings.deviceAndVaultName = "";
} }
} }
if (isCloudantURI(this.settings.couchDB_URI) && this.settings.customChunkSize != 0) { if (isCloudantURI(this.settings.couchDB_URI) && this.settings.customChunkSize != 0) {
this._log("Configuration verification founds problems with your configuration. This has been fixed automatically. But you may already have data that cannot be synchronised. If this is the case, please rebuild everything.", LOG_LEVEL_NOTICE) this._log(
"Configuration issues detected and automatically resolved. However, unsynchronized data may exist. Consider rebuilding if necessary.",
LOG_LEVEL_NOTICE
);
this.settings.customChunkSize = 0; this.settings.customChunkSize = 0;
} }
this.core.deviceAndVaultName = localStorage.getItem(lsKey) || ""; this.core.$$setDeviceAndVaultName(localStorage.getItem(lsKey) || "");
if (this.core.deviceAndVaultName == "") { if (this.core.$$getDeviceAndVaultName() == "") {
if (this.settings.usePluginSync) { if (this.settings.usePluginSync) {
this._log("Device name is not set. Plug-in sync has been disabled.", LOG_LEVEL_NOTICE); this._log("Device name missing. Disabling plug-in sync.", LOG_LEVEL_NOTICE);
this.settings.usePluginSync = false; this.settings.usePluginSync = false;
} }
} }
// Splitter configurations have been replaced with chunkSplitterVersion.
if (this.settings.chunkSplitterVersion == "") {
if (this.settings.enableChunkSplitterV2) {
if (this.settings.useSegmenter) {
this.settings.chunkSplitterVersion = "v2-segmenter";
} else {
this.settings.chunkSplitterVersion = "v2";
}
} else {
this.settings.chunkSplitterVersion = "";
}
} else if (!(this.settings.chunkSplitterVersion in ChunkAlgorithmNames)) {
this.settings.chunkSplitterVersion = "";
}
// this.core.ignoreFiles = this.settings.ignoreFiles.split(",").map(e => e.trim()); // this.core.ignoreFiles = this.settings.ignoreFiles.split(",").map(e => e.trim());
eventHub.emitEvent(EVENT_REQUEST_RELOAD_SETTING_TAB); eventHub.emitEvent(EVENT_REQUEST_RELOAD_SETTING_TAB);
} }

View File

@@ -9,7 +9,6 @@ import { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE }
const SETTING_HEADER = "````yaml:livesync-setting\n"; const SETTING_HEADER = "````yaml:livesync-setting\n";
const SETTING_FOOTER = "\n````"; const SETTING_FOOTER = "\n````";
export class ModuleObsidianSettingsAsMarkdown extends AbstractObsidianModule implements IObsidianModule { export class ModuleObsidianSettingsAsMarkdown extends AbstractObsidianModule implements IObsidianModule {
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
this.addCommand({ this.addCommand({
id: "livesync-export-config", id: "livesync-export-config",
@@ -19,10 +18,10 @@ export class ModuleObsidianSettingsAsMarkdown extends AbstractObsidianModule imp
return this.settings.settingSyncFile != ""; return this.settings.settingSyncFile != "";
} }
fireAndForget(async () => { fireAndForget(async () => {
await this.plugin.$$saveSettingData(); await this.core.$$saveSettingData();
}); });
} },
}) });
this.addCommand({ this.addCommand({
id: "livesync-import-config", id: "livesync-import-config",
name: "Parse setting file", name: "Parse setting file",
@@ -33,33 +32,33 @@ export class ModuleObsidianSettingsAsMarkdown extends AbstractObsidianModule imp
return ret.body != ""; return ret.body != "";
} }
if (ctx.file) { if (ctx.file) {
const file = ctx.file const file = ctx.file;
fireAndForget(async () => await this.checkAndApplySettingFromMarkdown(file.path, false)); fireAndForget(async () => await this.checkAndApplySettingFromMarkdown(file.path, false));
} }
}, },
}) });
eventHub.on("event-file-changed", (info: { eventHub.onEvent("event-file-changed", (info: { file: FilePathWithPrefix; automated: boolean }) => {
file: FilePathWithPrefix, automated: boolean
}) => {
fireAndForget(() => this.checkAndApplySettingFromMarkdown(info.file, info.automated)); fireAndForget(() => this.checkAndApplySettingFromMarkdown(info.file, info.automated));
}); });
eventHub.onEvent(EVENT_SETTING_SAVED, (evt: CustomEvent<ObsidianLiveSyncSettings>) => { eventHub.onEvent(EVENT_SETTING_SAVED, (settings: ObsidianLiveSyncSettings) => {
const settings = evt.detail;
if (settings.settingSyncFile != "") { if (settings.settingSyncFile != "") {
fireAndForget(() => this.saveSettingToMarkdown(settings.settingSyncFile)); fireAndForget(() => this.saveSettingToMarkdown(settings.settingSyncFile));
} }
}) });
return Promise.resolve(true); return Promise.resolve(true);
} }
extractSettingFromWholeText(data: string): { extractSettingFromWholeText(data: string): {
preamble: string, body: string, postscript: string preamble: string;
body: string;
postscript: string;
} { } {
if (data.indexOf(SETTING_HEADER) === -1) { if (data.indexOf(SETTING_HEADER) === -1) {
return { return {
preamble: data, body: "", postscript: "" preamble: data,
} body: "",
postscript: "",
};
} }
const startMarkerPos = data.indexOf(SETTING_HEADER); const startMarkerPos = data.indexOf(SETTING_HEADER);
const dataStartPos = startMarkerPos == -1 ? data.length : startMarkerPos; const dataStartPos = startMarkerPos == -1 ? data.length : startMarkerPos;
@@ -69,27 +68,33 @@ export class ModuleObsidianSettingsAsMarkdown extends AbstractObsidianModule imp
const ret = { const ret = {
preamble: data.substring(0, dataStartPos), preamble: data.substring(0, dataStartPos),
body, body,
postscript: data.substring(dataEndPos + SETTING_FOOTER.length + 1) postscript: data.substring(dataEndPos + SETTING_FOOTER.length + 1),
} };
return ret; return ret;
} }
async parseSettingFromMarkdown(filename: string, data?: string) { async parseSettingFromMarkdown(filename: string, data?: string) {
const file = await this.core.storageAccess.isExists(filename); const file = await this.core.storageAccess.isExists(filename);
if (!file) return { if (!file)
preamble: "", body: "", postscript: "", return {
}; preamble: "",
body: "",
postscript: "",
};
if (data) { if (data) {
return this.extractSettingFromWholeText(data); return this.extractSettingFromWholeText(data);
} }
const parseData = data ?? await this.core.storageAccess.readFileText(filename); const parseData = data ?? (await this.core.storageAccess.readFileText(filename));
return this.extractSettingFromWholeText(parseData); return this.extractSettingFromWholeText(parseData);
} }
async checkAndApplySettingFromMarkdown(filename: string, automated?: boolean) { async checkAndApplySettingFromMarkdown(filename: string, automated?: boolean) {
if (automated && !this.settings.notifyAllSettingSyncFile) { if (automated && !this.settings.notifyAllSettingSyncFile) {
if (!this.settings.settingSyncFile || this.settings.settingSyncFile != filename) { if (!this.settings.settingSyncFile || this.settings.settingSyncFile != filename) {
this._log(`Setting file (${filename}) is not matched to the current configuration. skipped.`, LOG_LEVEL_DEBUG); this._log(
`Setting file (${filename}) does not match the current configuration. skipped.`,
LOG_LEVEL_DEBUG
);
return; return;
} }
} }
@@ -104,61 +109,80 @@ export class ModuleObsidianSettingsAsMarkdown extends AbstractObsidianModule imp
} }
if ("settingSyncFile" in newSetting && newSetting.settingSyncFile != filename) { if ("settingSyncFile" in newSetting && newSetting.settingSyncFile != filename) {
this._log("This setting file seems to backed up one. Please fix the filename or settingSyncFile value.", automated ? LOG_LEVEL_INFO : LOG_LEVEL_NOTICE); this._log(
"This setting file seems to backed up one. Please fix the filename or settingSyncFile value.",
automated ? LOG_LEVEL_INFO : LOG_LEVEL_NOTICE
);
return; return;
} }
let settingToApply = { ...DEFAULT_SETTINGS } as ObsidianLiveSyncSettings; let settingToApply = { ...DEFAULT_SETTINGS } as ObsidianLiveSyncSettings;
settingToApply = { ...settingToApply, ...newSetting } settingToApply = { ...settingToApply, ...newSetting };
if (!(settingToApply?.writeCredentialsForSettingSync)) { if (!settingToApply?.writeCredentialsForSettingSync) {
//New setting does not contains credentials. //New setting does not contains credentials.
settingToApply.couchDB_USER = this.settings.couchDB_USER; settingToApply.couchDB_USER = this.settings.couchDB_USER;
settingToApply.couchDB_PASSWORD = this.settings.couchDB_PASSWORD; settingToApply.couchDB_PASSWORD = this.settings.couchDB_PASSWORD;
settingToApply.passphrase = this.settings.passphrase; settingToApply.passphrase = this.settings.passphrase;
} }
const oldSetting = this.generateSettingForMarkdown(this.settings, settingToApply.writeCredentialsForSettingSync); const oldSetting = this.generateSettingForMarkdown(
this.settings,
settingToApply.writeCredentialsForSettingSync
);
if (!isObjectDifferent(oldSetting, this.generateSettingForMarkdown(settingToApply))) { if (!isObjectDifferent(oldSetting, this.generateSettingForMarkdown(settingToApply))) {
this._log("Setting markdown has been detected, but not changed.", automated ? LOG_LEVEL_INFO : LOG_LEVEL_NOTICE); this._log(
return "Setting markdown has been detected, but not changed.",
automated ? LOG_LEVEL_INFO : LOG_LEVEL_NOTICE
);
return;
} }
const addMsg = this.settings.settingSyncFile != filename ? " (This is not-active file)" : ""; const addMsg = this.settings.settingSyncFile != filename ? " (This is not-active file)" : "";
this.plugin.confirm.askInPopup("apply-setting-from-md", `Setting markdown ${filename}${addMsg} has been detected. Apply this from {HERE}.`, (anchor) => { this.core.confirm.askInPopup(
anchor.text = "HERE"; "apply-setting-from-md",
anchor.addEventListener("click", () => { `Setting markdown ${filename}${addMsg} has been detected. Apply this from {HERE}.`,
fireAndForget(async () => { (anchor) => {
const APPLY_ONLY = "Apply settings"; anchor.text = "HERE";
const APPLY_AND_RESTART = "Apply settings and restart obsidian"; anchor.addEventListener("click", () => {
const APPLY_AND_REBUILD = "Apply settings and restart obsidian with red_flag_rebuild.md"; fireAndForget(async () => {
const APPLY_AND_FETCH = "Apply settings and restart obsidian with red_flag_fetch.md"; const APPLY_ONLY = "Apply settings";
const CANCEL = "Cancel"; const APPLY_AND_RESTART = "Apply settings and restart obsidian";
const result = await this.plugin.confirm.askSelectStringDialogue("Ready for apply the setting.", [ const APPLY_AND_REBUILD = "Apply settings and restart obsidian with red_flag_rebuild.md";
APPLY_AND_RESTART, const APPLY_AND_FETCH = "Apply settings and restart obsidian with red_flag_fetch.md";
APPLY_ONLY, const CANCEL = "Cancel";
APPLY_AND_FETCH, const result = await this.core.confirm.askSelectStringDialogue(
APPLY_AND_REBUILD, "Ready for apply the setting.",
CANCEL], { defaultAction: APPLY_AND_RESTART }); [APPLY_AND_RESTART, APPLY_ONLY, APPLY_AND_FETCH, APPLY_AND_REBUILD, CANCEL],
if (result == APPLY_ONLY || result == APPLY_AND_RESTART || result == APPLY_AND_REBUILD || result == APPLY_AND_FETCH) { { defaultAction: APPLY_AND_RESTART }
this.plugin.settings = settingToApply; );
await this.plugin.$$saveSettingData(); if (
if (result == APPLY_ONLY) { result == APPLY_ONLY ||
this._log("Loaded settings have been applied!", LOG_LEVEL_NOTICE); result == APPLY_AND_RESTART ||
return; result == APPLY_AND_REBUILD ||
result == APPLY_AND_FETCH
) {
this.core.settings = settingToApply;
await this.core.$$saveSettingData();
if (result == APPLY_ONLY) {
this._log("Loaded settings have been applied!", LOG_LEVEL_NOTICE);
return;
}
if (result == APPLY_AND_REBUILD) {
await this.core.rebuilder.scheduleRebuild();
}
if (result == APPLY_AND_FETCH) {
await this.core.rebuilder.scheduleFetch();
}
this.core.$$performRestart();
} }
if (result == APPLY_AND_REBUILD) { });
await this.plugin.rebuilder.scheduleRebuild(); });
} }
if (result == APPLY_AND_FETCH) { );
await this.plugin.rebuilder.scheduleFetch();
}
this.plugin.$$performRestart();
}
})
})
})
} }
generateSettingForMarkdown(settings?: ObsidianLiveSyncSettings, keepCredential?: boolean): Partial<ObsidianLiveSyncSettings> { generateSettingForMarkdown(
settings?: ObsidianLiveSyncSettings,
keepCredential?: boolean
): Partial<ObsidianLiveSyncSettings> {
const saveData = { ...(settings ? settings : this.settings) } as Partial<ObsidianLiveSyncSettings>; const saveData = { ...(settings ? settings : this.settings) } as Partial<ObsidianLiveSyncSettings>;
delete saveData.encryptedCouchDBConnection; delete saveData.encryptedCouchDBConnection;
delete saveData.encryptedPassphrase; delete saveData.encryptedPassphrase;
@@ -167,6 +191,11 @@ export class ModuleObsidianSettingsAsMarkdown extends AbstractObsidianModule imp
delete saveData.couchDB_USER; delete saveData.couchDB_USER;
delete saveData.couchDB_PASSWORD; delete saveData.couchDB_PASSWORD;
delete saveData.passphrase; delete saveData.passphrase;
delete saveData.jwtKey;
delete saveData.jwtKid;
delete saveData.jwtSub;
delete saveData.couchDB_CustomHeaders;
delete saveData.bucketCustomHeaders;
} }
return saveData; return saveData;
} }
@@ -175,7 +204,6 @@ export class ModuleObsidianSettingsAsMarkdown extends AbstractObsidianModule imp
const saveData = this.generateSettingForMarkdown(); const saveData = this.generateSettingForMarkdown();
const file = await this.core.storageAccess.isExists(filename); const file = await this.core.storageAccess.isExists(filename);
if (!file) { if (!file) {
await this.core.storageAccess.ensureDir(filename); await this.core.storageAccess.ensureDir(filename);
const initialContent = `This file contains Self-hosted LiveSync settings as YAML. const initialContent = `This file contains Self-hosted LiveSync settings as YAML.
@@ -189,8 +217,11 @@ We can perform a command in this file.
**Note** Please handle it with all of your care if you have configured to write credentials in. **Note** Please handle it with all of your care if you have configured to write credentials in.
` `;
await this.core.storageAccess.writeFileAuto(filename, initialContent + SETTING_HEADER + "\n" + SETTING_FOOTER); await this.core.storageAccess.writeFileAuto(
filename,
initialContent + SETTING_HEADER + "\n" + SETTING_FOOTER
);
} }
// if (!(file instanceof TFile)) { // if (!(file instanceof TFile)) {
// this._log(`Markdown Setting: ${filename} already exists as a folder`, LOG_LEVEL_NOTICE); // this._log(`Markdown Setting: ${filename} already exists as a folder`, LOG_LEVEL_NOTICE);
@@ -204,9 +235,11 @@ We can perform a command in this file.
if (newBody == body) { if (newBody == body) {
this._log("Markdown setting: Nothing had been changed", LOG_LEVEL_VERBOSE); this._log("Markdown setting: Nothing had been changed", LOG_LEVEL_VERBOSE);
} else { } else {
await this.core.storageAccess.writeFileAuto(filename, preamble + SETTING_HEADER + newBody + SETTING_FOOTER + postscript); await this.core.storageAccess.writeFileAuto(
filename,
preamble + SETTING_HEADER + newBody + SETTING_FOOTER + postscript
);
this._log(`Markdown setting: ${filename} has been updated!`, LOG_LEVEL_VERBOSE); this._log(`Markdown setting: ${filename} has been updated!`, LOG_LEVEL_VERBOSE);
} }
} }
} }

View File

@@ -4,7 +4,6 @@ import { type IObsidianModule, AbstractObsidianModule } from "../AbstractObsidia
import { EVENT_REQUEST_OPEN_SETTING_WIZARD, EVENT_REQUEST_OPEN_SETTINGS, eventHub } from "../../common/events.ts"; import { EVENT_REQUEST_OPEN_SETTING_WIZARD, EVENT_REQUEST_OPEN_SETTINGS, eventHub } from "../../common/events.ts";
export class ModuleObsidianSettingDialogue extends AbstractObsidianModule implements IObsidianModule { export class ModuleObsidianSettingDialogue extends AbstractObsidianModule implements IObsidianModule {
settingTab!: ObsidianLiveSyncSettingTab; settingTab!: ObsidianLiveSyncSettingTab;
$everyOnloadStart(): Promise<boolean> { $everyOnloadStart(): Promise<boolean> {
@@ -28,8 +27,6 @@ export class ModuleObsidianSettingDialogue extends AbstractObsidianModule implem
} }
get appId() { get appId() {
return `${("appId" in this.app ? this.app.appId : "")}`; return `${"appId" in this.app ? this.app.appId : ""}`;
} }
} }

View File

@@ -1,15 +1,39 @@
import { type ObsidianLiveSyncSettings, DEFAULT_SETTINGS, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "../../lib/src/common/types.ts"; import {
import { configURIBase } from "../../common/types.ts"; type ObsidianLiveSyncSettings,
DEFAULT_SETTINGS,
KeyIndexOfSettings,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
} from "../../lib/src/common/types.ts";
import { configURIBase, configURIBaseQR } from "../../common/types.ts";
// import { PouchDB } from "../../lib/src/pouchdb/pouchdb-browser.js"; // import { PouchDB } from "../../lib/src/pouchdb/pouchdb-browser.js";
import { decrypt, encrypt } from "../../lib/src/encryption/e2ee_v2.ts"; import { decrypt, encrypt } from "../../lib/src/encryption/e2ee_v2.ts";
import { fireAndForget } from "../../lib/src/common/utils.ts"; import { fireAndForget } from "../../lib/src/common/utils.ts";
import { EVENT_REQUEST_COPY_SETUP_URI, EVENT_REQUEST_OPEN_SETUP_URI, eventHub } from "../../common/events.ts"; import {
EVENT_REQUEST_COPY_SETUP_URI,
EVENT_REQUEST_OPEN_SETUP_URI,
EVENT_REQUEST_SHOW_SETUP_QR,
eventHub,
} from "../../common/events.ts";
import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { AbstractObsidianModule, type IObsidianModule } from "../AbstractObsidianModule.ts";
import { decodeAnyArray, encodeAnyArray } from "../../common/utils.ts";
import qrcode from "qrcode-generator";
import { $msg } from "../../lib/src/common/i18n.ts";
export class ModuleSetupObsidian extends AbstractObsidianModule implements IObsidianModule { export class ModuleSetupObsidian extends AbstractObsidianModule implements IObsidianModule {
$everyOnload(): Promise<boolean> { $everyOnload(): Promise<boolean> {
this.registerObsidianProtocolHandler("setuplivesync", async (conf: any) => await this.setupWizard(conf.settings)); this.registerObsidianProtocolHandler("setuplivesync", async (conf: any) => {
if (conf.settings) {
await this.setupWizard(conf.settings);
} else if (conf.settingsQR) {
await this.decodeQR(conf.settingsQR);
}
});
this.addCommand({
id: "livesync-setting-qr",
name: "Show settings as a QR code",
callback: () => fireAndForget(this.encodeQR()),
});
this.addCommand({ this.addCommand({
id: "livesync-copysetupuri", id: "livesync-copysetupuri",
@@ -35,35 +59,101 @@ export class ModuleSetupObsidian extends AbstractObsidianModule implements IObsi
}); });
eventHub.onEvent(EVENT_REQUEST_OPEN_SETUP_URI, () => fireAndForget(() => this.command_openSetupURI())); eventHub.onEvent(EVENT_REQUEST_OPEN_SETUP_URI, () => fireAndForget(() => this.command_openSetupURI()));
eventHub.onEvent(EVENT_REQUEST_COPY_SETUP_URI, () => fireAndForget(() => this.command_copySetupURI())); eventHub.onEvent(EVENT_REQUEST_COPY_SETUP_URI, () => fireAndForget(() => this.command_copySetupURI()));
eventHub.onEvent(EVENT_REQUEST_SHOW_SETUP_QR, () => fireAndForget(() => this.encodeQR()));
return Promise.resolve(true); return Promise.resolve(true);
} }
async encodeQR() {
const settingArr = [];
const fullIndexes = Object.entries(KeyIndexOfSettings) as [keyof ObsidianLiveSyncSettings, number][];
for (const [settingKey, index] of fullIndexes) {
const settingValue = this.settings[settingKey];
if (index < 0) {
// This setting should be ignored.
continue;
}
settingArr[index] = settingValue;
}
const w = encodeAnyArray(settingArr);
const qr = qrcode(0, "L");
const uri = `${configURIBaseQR}${encodeURIComponent(w)}`;
qr.addData(uri);
qr.make();
const img = qr.createSvgTag(3);
const msg = $msg("Setup.QRCode", { qr_image: img });
await this.core.confirm.confirmWithMessage("Settings QR Code", msg, ["OK"], "OK");
return await Promise.resolve(w);
}
async decodeQR(qr: string) {
const settingArr = decodeAnyArray(qr);
// console.warn(settingArr);
const fullIndexes = Object.entries(KeyIndexOfSettings) as [keyof ObsidianLiveSyncSettings, number][];
const newSettings = { ...DEFAULT_SETTINGS } as ObsidianLiveSyncSettings;
for (const [settingKey, index] of fullIndexes) {
if (index < 0) {
// This setting should be ignored.
continue;
}
if (index >= settingArr.length) {
// Possibly a new setting added.
continue;
}
const settingValue = settingArr[index];
//@ts-ignore
newSettings[settingKey] = settingValue;
}
await this.applySettingWizard(this.settings, newSettings, "QR Code");
}
async command_copySetupURI(stripExtra = true) { async command_copySetupURI(stripExtra = true) {
const encryptingPassphrase = await this.core.confirm.askString("Encrypt your settings", "The passphrase to encrypt the setup URI", "", true); const encryptingPassphrase = await this.core.confirm.askString(
if (encryptingPassphrase === false) "Encrypt your settings",
return; "The passphrase to encrypt the setup URI",
const setting = { ...this.settings, configPassphraseStore: "", encryptedCouchDBConnection: "", encryptedPassphrase: "" } as Partial<ObsidianLiveSyncSettings>; "",
true
);
if (encryptingPassphrase === false) return;
const setting = {
...this.settings,
configPassphraseStore: "",
encryptedCouchDBConnection: "",
encryptedPassphrase: "",
} as Partial<ObsidianLiveSyncSettings>;
if (stripExtra) { if (stripExtra) {
delete setting.pluginSyncExtendedSetting; delete setting.pluginSyncExtendedSetting;
} }
const keys = Object.keys(setting) as (keyof ObsidianLiveSyncSettings)[]; const keys = Object.keys(setting) as (keyof ObsidianLiveSyncSettings)[];
for (const k of keys) { for (const k of keys) {
if (JSON.stringify(k in setting ? setting[k] : "") == JSON.stringify(k in DEFAULT_SETTINGS ? DEFAULT_SETTINGS[k] : "*")) { if (
JSON.stringify(k in setting ? setting[k] : "") ==
JSON.stringify(k in DEFAULT_SETTINGS ? DEFAULT_SETTINGS[k] : "*")
) {
delete setting[k]; delete setting[k];
} }
} }
const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(setting), encryptingPassphrase, false)); const encryptedSetting = encodeURIComponent(
const uri = `${configURIBase}${encryptedSetting}`; await encrypt(JSON.stringify(setting), encryptingPassphrase, false)
);
const uri = `${configURIBase}${encryptedSetting} `;
await navigator.clipboard.writeText(uri); await navigator.clipboard.writeText(uri);
this._log("Setup URI copied to clipboard", LOG_LEVEL_NOTICE); this._log("Setup URI copied to clipboard", LOG_LEVEL_NOTICE);
} }
async command_copySetupURIFull() { async command_copySetupURIFull() {
const encryptingPassphrase = await this.core.confirm.askString("Encrypt your settings", "The passphrase to encrypt the setup URI", "", true); const encryptingPassphrase = await this.core.confirm.askString(
if (encryptingPassphrase === false) "Encrypt your settings",
return; "The passphrase to encrypt the setup URI",
const setting = { ...this.settings, configPassphraseStore: "", encryptedCouchDBConnection: "", encryptedPassphrase: "" }; "",
const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(setting), encryptingPassphrase, false)); true
const uri = `${configURIBase}${encryptedSetting}`; );
if (encryptingPassphrase === false) return;
const setting = {
...this.settings,
configPassphraseStore: "",
encryptedCouchDBConnection: "",
encryptedPassphrase: "",
};
const encryptedSetting = encodeURIComponent(
await encrypt(JSON.stringify(setting), encryptingPassphrase, false)
);
const uri = `${configURIBase}${encryptedSetting} `;
await navigator.clipboard.writeText(uri); await navigator.clipboard.writeText(uri);
this._log("Setup URI copied to clipboard", LOG_LEVEL_NOTICE); this._log("Setup URI copied to clipboard", LOG_LEVEL_NOTICE);
} }
@@ -71,126 +161,168 @@ export class ModuleSetupObsidian extends AbstractObsidianModule implements IObsi
await this.command_copySetupURI(false); await this.command_copySetupURI(false);
} }
async command_openSetupURI() { async command_openSetupURI() {
const setupURI = await this.core.confirm.askString("Easy setup", "Set up URI", `${configURIBase}aaaaa`); const setupURI = await this.core.confirm.askString("Easy setup", "Set up URI", `${configURIBase} aaaaa`);
if (setupURI === false) if (setupURI === false) return;
return;
if (!setupURI.startsWith(`${configURIBase}`)) { if (!setupURI.startsWith(`${configURIBase}`)) {
this._log("Set up URI looks wrong.", LOG_LEVEL_NOTICE); this._log("Set up URI looks wrong.", LOG_LEVEL_NOTICE);
return; return;
} }
const config = decodeURIComponent(setupURI.substring(configURIBase.length)); const config = decodeURIComponent(setupURI.substring(configURIBase.length));
console.dir(config);
await this.setupWizard(config); await this.setupWizard(config);
} }
async applySettingWizard(
oldConf: ObsidianLiveSyncSettings,
newConf: ObsidianLiveSyncSettings,
method = "Setup URI"
) {
const result = await this.core.confirm.askYesNoDialog(
"Importing Configuration from the " + method + ". Are you sure to proceed ? ",
{}
);
if (result == "yes") {
const newSettingW = Object.assign({}, DEFAULT_SETTINGS, newConf) as ObsidianLiveSyncSettings;
this.core.replicator.closeReplication();
this.settings.suspendFileWatching = true;
console.dir(newSettingW);
// Back into the default method once.
newSettingW.configPassphraseStore = "";
newSettingW.encryptedPassphrase = "";
newSettingW.encryptedCouchDBConnection = "";
newSettingW.additionalSuffixOfDatabaseName = `${"appId" in this.app ? this.app.appId : ""} `;
const setupJustImport = "Don't sync anything, just apply the settings.";
const setupAsNew = "This is a new client - sync everything from the remote server.";
const setupAsMerge = "This is an existing client - merge existing files with the server.";
const setupAgain = "Initialise new server data - ideal for new or broken servers.";
const setupManually = "Continue and configure manually.";
newSettingW.syncInternalFiles = false;
newSettingW.usePluginSync = false;
newSettingW.isConfigured = true;
// Migrate completely obsoleted configuration.
if (!newSettingW.useIndexedDBAdapter) {
newSettingW.useIndexedDBAdapter = true;
}
const setupType = await this.core.confirm.askSelectStringDialogue(
"How would you like to set it up?",
[setupAsNew, setupAgain, setupAsMerge, setupJustImport, setupManually],
{ defaultAction: setupAsNew }
);
if (setupType == setupJustImport) {
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.saveSettings();
} else if (setupType == setupAsNew) {
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.saveSettings();
await this.core.rebuilder.$fetchLocal();
} else if (setupType == setupAsMerge) {
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.saveSettings();
await this.core.rebuilder.$fetchLocal(true);
} else if (setupType == setupAgain) {
const confirm =
"This operation will rebuild all databases with files on this device. Any files on the remote database not synced here will be lost.";
if (
(await this.core.confirm.askSelectStringDialogue(
"Are you sure you want to do this?",
["Cancel", confirm],
{ defaultAction: "Cancel" }
)) != confirm
) {
return;
}
this.core.settings = newSettingW;
await this.core.saveSettings();
this.core.$$clearUsedPassphrase();
await this.core.rebuilder.$rebuildEverything();
} else if (setupType == setupManually) {
const keepLocalDB = await this.core.confirm.askYesNoDialog("Keep local DB?", {
defaultOption: "No",
});
const keepRemoteDB = await this.core.confirm.askYesNoDialog("Keep remote DB?", {
defaultOption: "No",
});
if (keepLocalDB == "yes" && keepRemoteDB == "yes") {
// nothing to do. so peaceful.
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.$allSuspendAllSync();
await this.core.$allSuspendExtraSync();
await this.core.saveSettings();
const replicate = await this.core.confirm.askYesNoDialog("Unlock and replicate?", {
defaultOption: "Yes",
});
if (replicate == "yes") {
await this.core.$$replicate(true);
await this.core.$$markRemoteUnlocked();
}
this._log("Configuration loaded.", LOG_LEVEL_NOTICE);
return;
}
if (keepLocalDB == "no" && keepRemoteDB == "no") {
const reset = await this.core.confirm.askYesNoDialog("Drop everything?", {
defaultOption: "No",
});
if (reset != "yes") {
this._log("Cancelled", LOG_LEVEL_NOTICE);
this.core.settings = oldConf;
return;
}
}
let initDB;
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.saveSettings();
if (keepLocalDB == "no") {
await this.core.$$resetLocalDatabase();
await this.core.localDatabase.initializeDatabase();
const rebuild = await this.core.confirm.askYesNoDialog("Rebuild the database?", {
defaultOption: "Yes",
});
if (rebuild == "yes") {
initDB = this.core.$$initializeDatabase(true);
} else {
await this.core.$$markRemoteResolved();
}
}
if (keepRemoteDB == "no") {
await this.core.$$tryResetRemoteDatabase();
await this.core.$$markRemoteLocked();
}
if (keepLocalDB == "no" || keepRemoteDB == "no") {
const replicate = await this.core.confirm.askYesNoDialog("Replicate once?", {
defaultOption: "Yes",
});
if (replicate == "yes") {
if (initDB != null) {
await initDB;
}
await this.core.$$replicate(true);
}
}
}
this._log("Configuration loaded.", LOG_LEVEL_NOTICE);
} else {
this._log("Cancelled", LOG_LEVEL_NOTICE);
this.core.settings = oldConf;
return;
}
}
async setupWizard(confString: string) { async setupWizard(confString: string) {
try { try {
const oldConf = JSON.parse(JSON.stringify(this.settings)); const oldConf = JSON.parse(JSON.stringify(this.settings));
const encryptingPassphrase = await this.core.confirm.askString("Passphrase", "The passphrase to decrypt your setup URI", "", true); const encryptingPassphrase = await this.core.confirm.askString(
if (encryptingPassphrase === false) "Passphrase",
return; "The passphrase to decrypt your setup URI",
"",
true
);
if (encryptingPassphrase === false) return;
const newConf = await JSON.parse(await decrypt(confString, encryptingPassphrase, false)); const newConf = await JSON.parse(await decrypt(confString, encryptingPassphrase, false));
if (newConf) { if (newConf) {
const result = await this.core.confirm.askYesNoDialog("Importing Configuration from the Setup-URI. Are you sure to proceed?", {}); await this.applySettingWizard(oldConf, newConf);
if (result == "yes") {
const newSettingW = Object.assign({}, DEFAULT_SETTINGS, newConf) as ObsidianLiveSyncSettings;
this.core.replicator.closeReplication();
this.settings.suspendFileWatching = true;
console.dir(newSettingW);
// Back into the default method once.
newSettingW.configPassphraseStore = "";
newSettingW.encryptedPassphrase = "";
newSettingW.encryptedCouchDBConnection = "";
newSettingW.additionalSuffixOfDatabaseName = `${("appId" in this.app ? this.app.appId : "")}`
const setupJustImport = "Just import setting";
const setupAsNew = "Set it up as secondary or subsequent device";
const setupAsMerge = "Secondary device but try keeping local changes";
const setupAgain = "Reconfigure and reconstitute the data";
const setupManually = "Leave everything to me";
newSettingW.syncInternalFiles = false;
newSettingW.usePluginSync = false;
newSettingW.isConfigured = true;
// Migrate completely obsoleted configuration.
if (!newSettingW.useIndexedDBAdapter) {
newSettingW.useIndexedDBAdapter = true;
}
const setupType = await this.core.confirm.askSelectStringDialogue("How would you like to set it up?", [setupAsNew, setupAgain, setupAsMerge, setupJustImport, setupManually], { defaultAction: setupAsNew });
if (setupType == setupJustImport) {
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.saveSettings();
} else if (setupType == setupAsNew) {
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.rebuilder.$fetchLocal();
} else if (setupType == setupAsMerge) {
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.rebuilder.$fetchLocal(true);
} else if (setupType == setupAgain) {
const confirm = "I know this operation will rebuild all my databases with files on this device, and files that are on the remote database and I didn't synchronize to any other devices will be lost and want to proceed indeed.";
if (await this.core.confirm.askSelectStringDialogue("Do you really want to do this?", ["Cancel", confirm], { defaultAction: "Cancel" }) != confirm) {
return;
}
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.rebuilder.$rebuildEverything();
} else if (setupType == setupManually) {
const keepLocalDB = await this.core.confirm.askYesNoDialog("Keep local DB?", { defaultOption: "No" });
const keepRemoteDB = await this.core.confirm.askYesNoDialog("Keep remote DB?", { defaultOption: "No" });
if (keepLocalDB == "yes" && keepRemoteDB == "yes") {
// nothing to do. so peaceful.
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.$allSuspendAllSync();
await this.core.$allSuspendExtraSync();
await this.core.saveSettings();
const replicate = await this.core.confirm.askYesNoDialog("Unlock and replicate?", { defaultOption: "Yes" });
if (replicate == "yes") {
await this.core.$$replicate(true);
await this.core.$$markRemoteUnlocked();
}
this._log("Configuration loaded.", LOG_LEVEL_NOTICE);
return;
}
if (keepLocalDB == "no" && keepRemoteDB == "no") {
const reset = await this.core.confirm.askYesNoDialog("Drop everything?", { defaultOption: "No" });
if (reset != "yes") {
this._log("Cancelled", LOG_LEVEL_NOTICE);
this.core.settings = oldConf;
return;
}
}
let initDB;
this.core.settings = newSettingW;
this.core.$$clearUsedPassphrase();
await this.core.saveSettings();
if (keepLocalDB == "no") {
await this.core.$$resetLocalDatabase();
await this.core.localDatabase.initializeDatabase();
const rebuild = await this.core.confirm.askYesNoDialog("Rebuild the database?", { defaultOption: "Yes" });
if (rebuild == "yes") {
initDB = this.core.$$initializeDatabase(true);
} else {
await this.core.$$markRemoteResolved();
}
}
if (keepRemoteDB == "no") {
await this.core.$$tryResetRemoteDatabase();
await this.core.$$markRemoteLocked();
}
if (keepLocalDB == "no" || keepRemoteDB == "no") {
const replicate = await this.core.confirm.askYesNoDialog("Replicate once?", { defaultOption: "Yes" });
if (replicate == "yes") {
if (initDB != null) {
await initDB;
}
await this.core.$$replicate(true);
}
}
}
}
this._log("Configuration loaded.", LOG_LEVEL_NOTICE); this._log("Configuration loaded.", LOG_LEVEL_NOTICE);
} else { } else {
this._log("Cancelled.", LOG_LEVEL_NOTICE); this._log("Cancelled.", LOG_LEVEL_NOTICE);
@@ -200,6 +332,4 @@ export class ModuleSetupObsidian extends AbstractObsidianModule implements IObsi
this._log(ex, LOG_LEVEL_VERBOSE); this._log(ex, LOG_LEVEL_VERBOSE);
} }
} }
} }

View File

@@ -1,9 +1,30 @@
import { Setting, TextComponent, type ToggleComponent, type DropdownComponent, ButtonComponent, type TextAreaComponent, type ValueComponent } from "obsidian"; import {
Setting,
TextComponent,
type ToggleComponent,
type DropdownComponent,
ButtonComponent,
type TextAreaComponent,
type ValueComponent,
} from "obsidian";
import { unique } from "octagonal-wheels/collection"; import { unique } from "octagonal-wheels/collection";
import { LEVEL_ADVANCED, LEVEL_POWER_USER, statusDisplay, type ConfigurationItem } from "../../../lib/src/common/types.ts"; import {
import { type ObsidianLiveSyncSettingTab, type AutoWireOption, wrapMemo, type OnUpdateResult, createStub, findAttrFromParent } from "./ObsidianLiveSyncSettingTab.ts"; LEVEL_ADVANCED,
import { type AllSettingItemKey, getConfig, type AllSettings, type AllStringItemKey, type AllNumericItemKey, type AllBooleanItemKey } from "./settingConstants.ts"; LEVEL_POWER_USER,
statusDisplay,
type ConfigurationItem,
} from "../../../lib/src/common/types.ts";
import { createStub, type ObsidianLiveSyncSettingTab } from "./ObsidianLiveSyncSettingTab.ts";
import {
type AllSettingItemKey,
getConfig,
type AllSettings,
type AllStringItemKey,
type AllNumericItemKey,
type AllBooleanItemKey,
} from "./settingConstants.ts";
import { $msg } from "src/lib/src/common/i18n.ts";
import { findAttrFromParent, wrapMemo, type AutoWireOption, type OnUpdateResult } from "./SettingPane.ts";
export class LiveSyncSetting extends Setting { export class LiveSyncSetting extends Setting {
autoWiredComponent?: TextComponent | ToggleComponent | DropdownComponent | ButtonComponent | TextAreaComponent; autoWiredComponent?: TextComponent | ToggleComponent | DropdownComponent | ButtonComponent | TextAreaComponent;
@@ -29,8 +50,9 @@ export class LiveSyncSetting extends Setting {
DEV: { DEV: {
const paneName = findAttrFromParent(this.settingEl, "data-pane"); const paneName = findAttrFromParent(this.settingEl, "data-pane");
const panelName = findAttrFromParent(this.settingEl, "data-panel"); const panelName = findAttrFromParent(this.settingEl, "data-panel");
const itemName = typeof this.nameBuf == "string" ? this.nameBuf : this.nameBuf.textContent?.toString() ?? ""; const itemName =
const strValue = typeof value == "string" ? value : value.textContent?.toString() ?? ""; typeof this.nameBuf == "string" ? this.nameBuf : (this.nameBuf.textContent?.toString() ?? "");
const strValue = typeof value == "string" ? value : (value.textContent?.toString() ?? "");
createStub(itemName, key, strValue, panelName, paneName); createStub(itemName, key, strValue, panelName, paneName);
} }
@@ -59,7 +81,7 @@ export class LiveSyncSetting extends Setting {
autoWireSetting(key: AllSettingItemKey, opt?: AutoWireOption) { autoWireSetting(key: AllSettingItemKey, opt?: AutoWireOption) {
const conf = getConfig(key); const conf = getConfig(key);
if (!conf) { if (!conf) {
// throw new Error(`No such setting item :${key}`) // throw new Error($msg("liveSyncSetting.errorNoSuchSettingItem", { key }));
return; return;
} }
const name = `${conf.name}${statusDisplay(conf.status)}`; const name = `${conf.name}${statusDisplay(conf.status)}`;
@@ -111,9 +133,11 @@ export class LiveSyncSetting extends Setting {
} }
autoWireText(key: AllStringItemKey, opt?: AutoWireOption) { autoWireText(key: AllStringItemKey, opt?: AutoWireOption) {
const conf = this.autoWireSetting(key, opt); const conf = this.autoWireSetting(key, opt);
this.addText(text => { this.addText((text) => {
this.autoWiredComponent = text; this.autoWiredComponent = text;
const setValue = wrapMemo((value: string) => { text.setValue(value) }); const setValue = wrapMemo((value: string) => {
text.setValue(value);
});
this.invalidateValue = () => setValue(`${LiveSyncSetting.env.editingSettings[key]}`); this.invalidateValue = () => setValue(`${LiveSyncSetting.env.editingSettings[key]}`);
this.invalidateValue(); this.invalidateValue();
text.onChange(async (value) => { text.onChange(async (value) => {
@@ -129,9 +153,11 @@ export class LiveSyncSetting extends Setting {
} }
autoWireTextArea(key: AllStringItemKey, opt?: AutoWireOption) { autoWireTextArea(key: AllStringItemKey, opt?: AutoWireOption) {
const conf = this.autoWireSetting(key, opt); const conf = this.autoWireSetting(key, opt);
this.addTextArea(text => { this.addTextArea((text) => {
this.autoWiredComponent = text; this.autoWiredComponent = text;
const setValue = wrapMemo((value: string) => { text.setValue(value) }); const setValue = wrapMemo((value: string) => {
text.setValue(value);
});
this.invalidateValue = () => setValue(`${LiveSyncSetting.env.editingSettings[key]}`); this.invalidateValue = () => setValue(`${LiveSyncSetting.env.editingSettings[key]}`);
this.invalidateValue(); this.invalidateValue();
text.onChange(async (value) => { text.onChange(async (value) => {
@@ -145,18 +171,23 @@ export class LiveSyncSetting extends Setting {
}); });
return this; return this;
} }
autoWireNumeric(key: AllNumericItemKey, opt: AutoWireOption & { clampMin?: number; clampMax?: number; acceptZero?: boolean; }) { autoWireNumeric(
key: AllNumericItemKey,
opt: AutoWireOption & { clampMin?: number; clampMax?: number; acceptZero?: boolean }
) {
const conf = this.autoWireSetting(key, opt); const conf = this.autoWireSetting(key, opt);
this.addText(text => { this.addText((text) => {
this.autoWiredComponent = text; this.autoWiredComponent = text;
if (opt.clampMin) { if (opt.clampMin !== undefined) {
text.inputEl.setAttribute("min", `${opt.clampMin}`); text.inputEl.setAttribute("min", `${opt.clampMin}`);
} }
if (opt.clampMax) { if (opt.clampMax !== undefined) {
text.inputEl.setAttribute("max", `${opt.clampMax}`); text.inputEl.setAttribute("max", `${opt.clampMax}`);
} }
let lastError = false; let lastError = false;
const setValue = wrapMemo((value: string) => { text.setValue(value) }); const setValue = wrapMemo((value: string) => {
text.setValue(value);
});
this.invalidateValue = () => { this.invalidateValue = () => {
if (!lastError) setValue(`${LiveSyncSetting.env.editingSettings[key]}`); if (!lastError) setValue(`${LiveSyncSetting.env.editingSettings[key]}`);
}; };
@@ -166,8 +197,8 @@ export class LiveSyncSetting extends Setting {
const value = parsedValue; const value = parsedValue;
let hasError = false; let hasError = false;
if (isNaN(value)) hasError = true; if (isNaN(value)) hasError = true;
if (opt.clampMax && opt.clampMax < value) hasError = true; if (opt.clampMax !== undefined && opt.clampMax < value) hasError = true;
if (opt.clampMin && opt.clampMin > value) { if (opt.clampMin !== undefined && opt.clampMin > value) {
if (opt.acceptZero && value == 0) { if (opt.acceptZero && value == 0) {
// This is ok. // This is ok.
} else { } else {
@@ -180,7 +211,12 @@ export class LiveSyncSetting extends Setting {
text.inputEl.toggleClass("sls-item-invalid-value", false); text.inputEl.toggleClass("sls-item-invalid-value", false);
await this.commitValue(value); await this.commitValue(value);
} else { } else {
this.setTooltip(`The value should ${opt.clampMin || "~"} < value < ${opt.clampMax || "~"}`); this.setTooltip(
$msg("liveSyncSetting.valueShouldBeInRange", {
min: opt.clampMin?.toString() || "~",
max: opt.clampMax?.toString() || "~",
})
);
text.inputEl.toggleClass("sls-item-invalid-value", true); text.inputEl.toggleClass("sls-item-invalid-value", true);
lastError = true; lastError = true;
return false; return false;
@@ -193,9 +229,11 @@ export class LiveSyncSetting extends Setting {
} }
autoWireToggle(key: AllBooleanItemKey, opt?: AutoWireOption) { autoWireToggle(key: AllBooleanItemKey, opt?: AutoWireOption) {
const conf = this.autoWireSetting(key, opt); const conf = this.autoWireSetting(key, opt);
this.addToggle(toggle => { this.addToggle((toggle) => {
this.autoWiredComponent = toggle; this.autoWiredComponent = toggle;
const setValue = wrapMemo((value: boolean) => { toggle.setValue(opt?.invert ? !value : value) }); const setValue = wrapMemo((value: boolean) => {
toggle.setValue(opt?.invert ? !value : value);
});
this.invalidateValue = () => setValue(LiveSyncSetting.env.editingSettings[key] ?? false); this.invalidateValue = () => setValue(LiveSyncSetting.env.editingSettings[key] ?? false);
this.invalidateValue(); this.invalidateValue();
@@ -207,16 +245,15 @@ export class LiveSyncSetting extends Setting {
}); });
return this; return this;
} }
autoWireDropDown<T extends string>(key: AllStringItemKey, opt: AutoWireOption & { options: Record<T, string>; }) { autoWireDropDown<T extends string>(key: AllStringItemKey, opt: AutoWireOption & { options: Record<T, string> }) {
const conf = this.autoWireSetting(key, opt); const conf = this.autoWireSetting(key, opt);
this.addDropdown(dropdown => { this.addDropdown((dropdown) => {
this.autoWiredComponent = dropdown; this.autoWiredComponent = dropdown;
const setValue = wrapMemo((value: string) => { const setValue = wrapMemo((value: string) => {
dropdown.setValue(value); dropdown.setValue(value);
}); });
dropdown dropdown.addOptions(opt.options);
.addOptions(opt.options);
this.invalidateValue = () => setValue(LiveSyncSetting.env.editingSettings[key] || ""); this.invalidateValue = () => setValue(LiveSyncSetting.env.editingSettings[key] || "");
this.invalidateValue(); this.invalidateValue();
@@ -231,7 +268,7 @@ export class LiveSyncSetting extends Setting {
this.addButton((button) => { this.addButton((button) => {
this.applyButtonComponent = button; this.applyButtonComponent = button;
this.watchDirtyKeys = unique([...keys, ...this.watchDirtyKeys]); this.watchDirtyKeys = unique([...keys, ...this.watchDirtyKeys]);
button.setButtonText(text ?? "Apply"); button.setButtonText(text ?? $msg("liveSyncSettings.btnApply"));
button.onClick(async () => { button.onClick(async () => {
await LiveSyncSetting.env.saveSettings(keys); await LiveSyncSetting.env.saveSettings(keys);
LiveSyncSetting.env.reloadAllSettings(); LiveSyncSetting.env.reloadAllSettings();
@@ -264,7 +301,6 @@ export class LiveSyncSetting extends Setting {
const newConf = this._getComputedStatus(); const newConf = this._getComputedStatus();
const keys = Object.keys(newConf) as [keyof OnUpdateResult]; const keys = Object.keys(newConf) as [keyof OnUpdateResult];
for (const k of keys) { for (const k of keys) {
if (k in this.prevStatus && this.prevStatus[k] == newConf[k]) { if (k in this.prevStatus && this.prevStatus[k] == newConf[k]) {
continue; continue;
} }
@@ -277,8 +313,8 @@ export class LiveSyncSetting extends Setting {
case "classes": case "classes":
break; break;
case "disabled": case "disabled":
this.setDisabled((newConf[k] || false)); this.setDisabled(newConf[k] || false);
this.settingEl.toggleClass("sls-setting-disabled", (newConf[k] || false)); this.settingEl.toggleClass("sls-setting-disabled", newConf[k] || false);
this.prevStatus[k] = newConf[k]; this.prevStatus[k] = newConf[k];
break; break;
case "isCta": case "isCta":
@@ -327,7 +363,11 @@ export class LiveSyncSetting extends Setting {
} }
if (this.holdValue && this.selfKey) { if (this.holdValue && this.selfKey) {
const isDirty = LiveSyncSetting.env.isDirty(this.selfKey); const isDirty = LiveSyncSetting.env.isDirty(this.selfKey);
const alt = isDirty ? `Original: ${LiveSyncSetting.env.initialSettings![this.selfKey]}` : ""; const alt = isDirty
? $msg("liveSyncSetting.originalValue", {
value: String(LiveSyncSetting.env.initialSettings?.[this.selfKey] ?? ""),
})
: "";
this.controlEl.toggleClass("sls-item-dirty", isDirty); this.controlEl.toggleClass("sls-item-dirty", isDirty);
if (!this.hasPassword) { if (!this.hasPassword) {
this.nameEl.toggleClass("sls-item-dirty-help", isDirty); this.nameEl.toggleClass("sls-item-dirty-help", isDirty);

View File

@@ -1,31 +1,28 @@
<script lang="ts"> <script lang="ts">
export let patterns = [] as string[]; import type { CustomRegExpSource } from "../../../lib/src/common/types";
export let originals = [] as string[]; import { isInvertedRegExp, isValidRegExp } from "../../../lib/src/common/utils";
export let apply: (args: string[]) => Promise<void> = (_: string[]) => Promise.resolve(); export let patterns = [] as CustomRegExpSource[];
export let originals = [] as CustomRegExpSource[];
export let apply: (args: CustomRegExpSource[]) => Promise<void> = (_: CustomRegExpSource[]) => Promise.resolve();
function revert() { function revert() {
patterns = [...originals]; patterns = [...originals];
} }
const CHECK_OK = "✔"; const CHECK_OK = "✔";
const CHECK_NG = "⚠"; const CHECK_NG = "⚠";
const MARK_MODIFIED = "✏ "; const MARK_MODIFIED = "✏ ";
function checkRegExp(pattern: string) { function checkRegExp(pattern: CustomRegExpSource) {
if (pattern.trim() == "") return ""; return isValidRegExp(pattern) ? CHECK_OK : CHECK_NG;
try {
new RegExp(pattern);
return CHECK_OK;
} catch (ex) {
return CHECK_NG;
}
} }
$: statusName = patterns.map((e) => checkRegExp(e)); $: statusName = patterns.map((e) => checkRegExp(e));
$: modified = patterns.map((e, i) => (e != (originals?.[i] ?? "") ? MARK_MODIFIED : "")); $: modified = patterns.map((e, i) => (e != (originals?.[i] ?? "") ? MARK_MODIFIED : ""));
$: isInvertedExp = patterns.map((e) => isInvertedRegExp(e));
function remove(idx: number) { function remove(idx: number) {
patterns[idx] = ""; patterns[idx] = "" as CustomRegExpSource;
} }
function add() { function add() {
patterns = [...patterns, ""]; patterns = [...patterns, "" as CustomRegExpSource];
} }
</script> </script>
@@ -33,7 +30,9 @@
{#each patterns as pattern, idx} {#each patterns as pattern, idx}
<!-- svelte-ignore a11y-label-has-associated-control --> <!-- svelte-ignore a11y-label-has-associated-control -->
<li> <li>
<label>{modified[idx]}{statusName[idx]}</label><input type="text" bind:value={pattern} class={modified[idx]} /> <label>{modified[idx]}{statusName[idx]}</label>
<span class="chip">{isInvertedExp[idx] ? "INVERTED" : ""}</span>
<input type="text" bind:value={pattern} class={modified[idx]} />
<button class="iconbutton" on:click={() => remove(idx)}>🗑</button> <button class="iconbutton" on:click={() => remove(idx)}>🗑</button>
</li> </li>
{/each} {/each}
@@ -43,8 +42,16 @@
</label> </label>
</li> </li>
<li class="buttons"> <li class="buttons">
<button on:click={() => apply(patterns)} disabled={statusName.some((e) => e === CHECK_NG) || modified.every((e) => e === "")}>Apply </button> <button
<button on:click={() => revert()} disabled={statusName.some((e) => e === CHECK_NG) || modified.every((e) => e === "")}>Revert </button> on:click={() => apply(patterns)}
disabled={statusName.some((e) => e === CHECK_NG) || modified.every((e) => e === "")}
>Apply
</button>
<button
on:click={() => revert()}
disabled={statusName.some((e) => e === CHECK_NG) || modified.every((e) => e === "")}
>Revert
</button>
</li> </li>
</ul> </ul>
@@ -85,4 +92,14 @@
button.iconbutton { button.iconbutton {
max-width: 4em; max-width: 4em;
} }
.chip {
background-color: var(--tag-background);
color: var(--tag-color);
padding: var(--size-2-1) var(--size-4-1);
border-radius: 0.5em;
font-size: 0.8em;
}
.chip:empty {
display: none;
}
</style> </style>

View File

@@ -0,0 +1,44 @@
import { ChunkAlgorithmNames } from "../../../lib/src/common/types.ts";
import { LiveSyncSetting as Setting } from "./LiveSyncSetting.ts";
import type { ObsidianLiveSyncSettingTab } from "./ObsidianLiveSyncSettingTab.ts";
import type { PageFunctions } from "./SettingPane.ts";
export function paneAdvanced(this: ObsidianLiveSyncSettingTab, paneEl: HTMLElement, { addPanel }: PageFunctions): void {
void addPanel(paneEl, "Memory cache").then((paneEl) => {
new Setting(paneEl).autoWireNumeric("hashCacheMaxCount", { clampMin: 10 });
new Setting(paneEl).autoWireNumeric("hashCacheMaxAmount", { clampMin: 1 });
});
void addPanel(paneEl, "Local Database Tweak").then((paneEl) => {
paneEl.addClass("wizardHidden");
const items = ChunkAlgorithmNames;
new Setting(paneEl).autoWireDropDown("chunkSplitterVersion", {
options: items,
});
new Setting(paneEl).autoWireNumeric("customChunkSize", { clampMin: 0, acceptZero: true });
});
void addPanel(paneEl, "Transfer Tweak").then((paneEl) => {
new Setting(paneEl)
.setClass("wizardHidden")
.autoWireToggle("readChunksOnline", { onUpdate: this.onlyOnCouchDB });
new Setting(paneEl).setClass("wizardHidden").autoWireNumeric("concurrencyOfReadChunksOnline", {
clampMin: 10,
onUpdate: this.onlyOnCouchDB,
});
new Setting(paneEl).setClass("wizardHidden").autoWireNumeric("minimumIntervalOfReadChunksOnline", {
clampMin: 10,
onUpdate: this.onlyOnCouchDB,
});
// new Setting(paneEl)
// .setClass("wizardHidden")
// .autoWireToggle("sendChunksBulk", { onUpdate: onlyOnCouchDB })
// new Setting(paneEl)
// .setClass("wizardHidden")
// .autoWireNumeric("sendChunksBulkMaxSize", {
// clampMax: 100, clampMin: 1, onUpdate: onlyOnCouchDB
// })
});
}

Some files were not shown because too many files have changed in this diff Show More