From 81317c0f4cf6ac034602b12499d30c9cd72ef71f Mon Sep 17 00:00:00 2001 From: Jonathan Poltak Samosir Date: Fri, 26 Jul 2024 11:13:03 +0700 Subject: [PATCH] Move non-BG specific PKM util fns into own module - This fixes a problem where linkedom's service worker was being included in the options UI script bundle. - It stemmed from certain PKM util fns being imported into the options UI code, but living in the same module which (indirectly) imported linkedom/worker. As they lived together, it all got brought in together even though that linkedom/worker stuff wasn't being imported in the UI specifically. - Those non-BG specific PKM util fns have now been moved to their own `utils.ts` module --- src/annotations/background/storage.ts | 6 +- .../background/backend/simple-http.ts | 2 +- .../ui/backup-pane/panes/restore-where.tsx | 2 +- .../ui/backup-pane/panes/setup-location.jsx | 2 +- src/backup-restore/ui/utils.js | 2 +- src/custom-lists/background/storage.ts | 6 +- src/options/imports/components/Import.tsx | 2 +- src/page-indexing/background/storage.ts | 6 +- .../background/handle-incoming-data.ts | 2 +- .../background/backend/simple-http.ts | 2 +- .../background/backend/utils.ts | 84 ------------------- src/pkm-integrations/utils.ts | 84 +++++++++++++++++++ 12 files changed, 97 insertions(+), 103 deletions(-) create mode 100644 src/pkm-integrations/utils.ts diff --git a/src/annotations/background/storage.ts b/src/annotations/background/storage.ts index 15fba65a74..eba902cc15 100644 --- a/src/annotations/background/storage.ts +++ b/src/annotations/background/storage.ts @@ -18,10 +18,8 @@ import { STORAGE_VERSIONS } from 'src/storage/constants' import type { Annotation, AnnotListEntry } from 'src/annotations/types' import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize' import type { PKMSyncBackgroundModule } from 'src/pkm-integrations/background' -import { - isPkmSyncEnabled, - shareAnnotationWithPKM, -} from 'src/pkm-integrations/background/backend/utils' +import { shareAnnotationWithPKM } from 'src/pkm-integrations/background/backend/utils' +import { isPkmSyncEnabled } from 'src/pkm-integrations/utils' import type { Storage } from 'webextension-polyfill' export default class AnnotationStorage extends StorageModule { diff --git a/src/backup-restore/background/backend/simple-http.ts b/src/backup-restore/background/backend/simple-http.ts index b472e895ef..9d1175ee41 100644 --- a/src/backup-restore/background/backend/simple-http.ts +++ b/src/backup-restore/background/backend/simple-http.ts @@ -9,7 +9,7 @@ import { separateDataFromImageChanges, shouldWriteImages, } from 'src/backup-restore/background/backend/utils' -import { getPkmSyncKey } from 'src/pkm-integrations/background/backend/utils' +import { getPkmSyncKey } from 'src/pkm-integrations/utils' import type { Storage } from 'webextension-polyfill' export class MemexLocalBackend extends BackupBackend { diff --git a/src/backup-restore/ui/backup-pane/panes/restore-where.tsx b/src/backup-restore/ui/backup-pane/panes/restore-where.tsx index 30fb3906a9..101f73a297 100644 --- a/src/backup-restore/ui/backup-pane/panes/restore-where.tsx +++ b/src/backup-restore/ui/backup-pane/panes/restore-where.tsx @@ -7,7 +7,7 @@ import { ProviderList } from 'src/backup-restore/ui/backup-pane/components/provi import { DownloadOverlay } from '../components/overlays' import { fetchBackupPath, checkServerStatus } from '../../utils' import { PrimaryAction } from '@worldbrain/memex-common/lib/common-ui/components/PrimaryAction' -import { getFolder } from 'src/pkm-integrations/background/backend/utils' +import { getFolder } from 'src/pkm-integrations/utils' const settingsStyle = require('src/options/settings/components/settings.css') const STYLES = require('../../styles.css') diff --git a/src/backup-restore/ui/backup-pane/panes/setup-location.jsx b/src/backup-restore/ui/backup-pane/panes/setup-location.jsx index 4596f5200b..ba97a6db28 100644 --- a/src/backup-restore/ui/backup-pane/panes/setup-location.jsx +++ b/src/backup-restore/ui/backup-pane/panes/setup-location.jsx @@ -9,7 +9,7 @@ import Icon from '@worldbrain/memex-common/lib/common-ui/components/icon' import SettingSection from '@worldbrain/memex-common/lib/common-ui/components/setting-section' import TextField from '@worldbrain/memex-common/lib/common-ui/components/text-field' import ButtonBar from 'src/options/imports/components/ButtonBar' -import { getFolder } from 'src/pkm-integrations/background/backend/utils' +import { getFolder } from 'src/pkm-integrations/utils' export default class SetupLocation extends React.Component { state = { diff --git a/src/backup-restore/ui/utils.js b/src/backup-restore/ui/utils.js index c06f5e845b..bd5a2525a8 100644 --- a/src/backup-restore/ui/utils.js +++ b/src/backup-restore/ui/utils.js @@ -1,6 +1,6 @@ import { remoteFunction } from 'src/util/webextensionRPC' import { LOCAL_SERVER_ENDPOINTS } from './backup-pane/constants' -import { getPkmSyncKey } from 'src/pkm-integrations/background/backend/utils' +import { getPkmSyncKey } from 'src/pkm-integrations/utils' export async function redirectToGDriveLogin() { window.location.href = await remoteFunction('getBackupProviderLoginLink')({ returnUrl: 'http://memex.cloud/backup/auth-redirect/google-drive', diff --git a/src/custom-lists/background/storage.ts b/src/custom-lists/background/storage.ts index f2ea988c7e..3179cf0d1b 100644 --- a/src/custom-lists/background/storage.ts +++ b/src/custom-lists/background/storage.ts @@ -31,10 +31,8 @@ import { insertOrderedItemBeforeIndex, pushOrderedItem, } from '@worldbrain/memex-common/lib/utils/item-ordering' -import { - isPkmSyncEnabled, - sharePageWithPKM, -} from 'src/pkm-integrations/background/backend/utils' +import { sharePageWithPKM } from 'src/pkm-integrations/background/backend/utils' +import { isPkmSyncEnabled } from 'src/pkm-integrations/utils' import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize' import { buildMaterializedPath, diff --git a/src/options/imports/components/Import.tsx b/src/options/imports/components/Import.tsx index b6a6362bd2..f549d07984 100644 --- a/src/options/imports/components/Import.tsx +++ b/src/options/imports/components/Import.tsx @@ -10,7 +10,7 @@ import styled from 'styled-components' import SettingSection from '@worldbrain/memex-common/lib/common-ui/components/setting-section' import { PrimaryAction } from '@worldbrain/memex-common/lib/common-ui/components/PrimaryAction' import TextField from '@worldbrain/memex-common/lib/common-ui/components/text-field' -import { getFolder } from 'src/pkm-integrations/background/backend/utils' +import { getFolder } from 'src/pkm-integrations/utils' import { MemexLocalBackend } from 'src/pkm-integrations/background/backend' import { TooltipBox } from '@worldbrain/memex-common/lib/common-ui/components/tooltip-box' import Checkbox from 'src/common-ui/components/Checkbox' diff --git a/src/page-indexing/background/storage.ts b/src/page-indexing/background/storage.ts index d27cea9f29..20fbe3d627 100644 --- a/src/page-indexing/background/storage.ts +++ b/src/page-indexing/background/storage.ts @@ -23,10 +23,8 @@ import { ContentFingerprint, LocationSchemeType, } from '@worldbrain/memex-common/lib/personal-cloud/storage/types' -import { - isPkmSyncEnabled, - sharePageWithPKM, -} from 'src/pkm-integrations/background/backend/utils' +import { isPkmSyncEnabled } from 'src/pkm-integrations/utils' +import { sharePageWithPKM } from 'src/pkm-integrations/background/backend/utils' import type { AuthenticatedUser } from '@worldbrain/memex-common/lib/authentication/types' import type { Bookmark, diff --git a/src/personal-cloud/background/handle-incoming-data.ts b/src/personal-cloud/background/handle-incoming-data.ts index c5f28dbaae..d65e68f32a 100644 --- a/src/personal-cloud/background/handle-incoming-data.ts +++ b/src/personal-cloud/background/handle-incoming-data.ts @@ -8,10 +8,10 @@ import { transformPageHTML } from '@worldbrain/memex-stemmer/lib/transform-page- import { transformPageText } from '@worldbrain/memex-stemmer/lib/transform-page-text' import type { PKMSyncBackgroundModule } from 'src/pkm-integrations/background' import { - isPkmSyncEnabled, shareAnnotationWithPKM, sharePageWithPKM, } from 'src/pkm-integrations/background/backend/utils' +import { isPkmSyncEnabled } from 'src/pkm-integrations/utils' import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize' import type { ImageSupportBackground } from 'src/image-support/background' import type { Browser } from 'webextension-polyfill' diff --git a/src/pkm-integrations/background/backend/simple-http.ts b/src/pkm-integrations/background/backend/simple-http.ts index b13e97523a..7ee9ce635e 100644 --- a/src/pkm-integrations/background/backend/simple-http.ts +++ b/src/pkm-integrations/background/backend/simple-http.ts @@ -1,4 +1,4 @@ -import { getPkmSyncKey } from './utils' +import { getPkmSyncKey } from 'src/pkm-integrations/utils' import type { Storage } from 'webextension-polyfill' import type { LocalFolder } from 'src/sidebar/annotations-sidebar/containers/types' diff --git a/src/pkm-integrations/background/backend/utils.ts b/src/pkm-integrations/background/backend/utils.ts index a2c7f9deb8..3f01bbd9bd 100644 --- a/src/pkm-integrations/background/backend/utils.ts +++ b/src/pkm-integrations/background/backend/utils.ts @@ -1,7 +1,5 @@ import resolveImgSrc from '@worldbrain/memex-common/lib/annotations/replace-img-src-with-cloud-address.service-worker' import type { AutoPk } from '@worldbrain/memex-common/lib/storage/types' -import { LOCAL_SERVER_ROOT } from 'src/backup-restore/ui/backup-pane/constants' -import type { Storage } from 'webextension-polyfill' import type { PKMSyncBackgroundModule } from '..' export async function shareAnnotationWithPKM( @@ -103,88 +101,6 @@ export async function sharePageWithPKM( } } -export async function getPkmSyncKey(deps: { storageAPI: Storage.Static }) { - let data = await deps.storageAPI.local.get('PKMSYNCpkmSyncKey') - - let pkmSyncKey = data.PKMSYNCpkmSyncKey - - // If pkmSyncKey does not exist, create a new one and store it in local storage - if (!pkmSyncKey) { - // Generate a random string for pkmSyncKey - pkmSyncKey = - Math.random().toString(36).substring(2, 15) + - Math.random().toString(36).substring(2, 15) - await deps.storageAPI.local.set({ PKMSYNCpkmSyncKey: pkmSyncKey }) - } - - return pkmSyncKey -} - -export async function isPkmSyncEnabled(deps: { storageAPI: Storage.Static }) { - try { - const data = await deps.storageAPI.local.get('PKMSYNCpkmFolders') - if ( - data.PKMSYNCpkmFolders && - (data.PKMSYNCpkmFolders.obsidianFolder?.length > 0 || - data.PKMSYNCpkmFolders.logSeqFolder?.length > 0) - ) { - return true - } - - return false - } catch (e) { - return false - } -} - -export async function getFolder( - pkmToSync: string, - deps: { storageAPI: Storage.Static }, -) { - const pkmSyncKey = await getPkmSyncKey(deps) - - const serverToTalkTo = LOCAL_SERVER_ROOT - - const getFolderPath = async (pkmToSync: string) => { - const response = await fetch(`${serverToTalkTo}/set-directory`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - pkmSyncType: pkmToSync, - syncKey: pkmSyncKey, - }), - }) - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`) - } - const directoryPath = await response.text() - - return directoryPath - } - - const folderPath = await getFolderPath(pkmToSync) - - // Fetch the existing "PKMSYNCpkmFolders" from local storage - let data = await deps.storageAPI.local.get('PKMSYNCpkmFolders') - data = data.PKMSYNCpkmFolders || {} - - // Update the value in it that corresponds to the pkmToSync - if (pkmToSync === 'logseq') { - data['logSeqFolder'] = folderPath - } else if (pkmToSync === 'obsidian') { - data['obsidianFolder'] = folderPath - } else if (pkmToSync === 'backup') { - data['backupFolder'] = folderPath - } - - // Write the update to local storage - await deps.storageAPI.local.set({ PKMSYNCpkmFolders: data }) - - return data -} - export type rabbitHoleDocument = { pageTitle: string fullUrl: string diff --git a/src/pkm-integrations/utils.ts b/src/pkm-integrations/utils.ts new file mode 100644 index 0000000000..7393b67afc --- /dev/null +++ b/src/pkm-integrations/utils.ts @@ -0,0 +1,84 @@ +import { LOCAL_SERVER_ROOT } from 'src/backup-restore/ui/backup-pane/constants' +import type { Storage } from 'webextension-polyfill' + +export async function getPkmSyncKey(deps: { storageAPI: Storage.Static }) { + let data = await deps.storageAPI.local.get('PKMSYNCpkmSyncKey') + + let pkmSyncKey = data.PKMSYNCpkmSyncKey + + // If pkmSyncKey does not exist, create a new one and store it in local storage + if (!pkmSyncKey) { + // Generate a random string for pkmSyncKey + pkmSyncKey = + Math.random().toString(36).substring(2, 15) + + Math.random().toString(36).substring(2, 15) + await deps.storageAPI.local.set({ PKMSYNCpkmSyncKey: pkmSyncKey }) + } + + return pkmSyncKey +} + +export async function isPkmSyncEnabled(deps: { storageAPI: Storage.Static }) { + try { + const data = await deps.storageAPI.local.get('PKMSYNCpkmFolders') + if ( + data.PKMSYNCpkmFolders && + (data.PKMSYNCpkmFolders.obsidianFolder?.length > 0 || + data.PKMSYNCpkmFolders.logSeqFolder?.length > 0) + ) { + return true + } + + return false + } catch (e) { + return false + } +} + +export async function getFolder( + pkmToSync: string, + deps: { storageAPI: Storage.Static }, +) { + const pkmSyncKey = await getPkmSyncKey(deps) + + const serverToTalkTo = LOCAL_SERVER_ROOT + + const getFolderPath = async (pkmToSync: string) => { + const response = await fetch(`${serverToTalkTo}/set-directory`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + pkmSyncType: pkmToSync, + syncKey: pkmSyncKey, + }), + }) + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) + } + const directoryPath = await response.text() + + return directoryPath + } + + const folderPath = await getFolderPath(pkmToSync) + + // Fetch the existing "PKMSYNCpkmFolders" from local storage + let data = await deps.storageAPI.local.get('PKMSYNCpkmFolders') + data = data.PKMSYNCpkmFolders || {} + + // Update the value in it that corresponds to the pkmToSync + if (pkmToSync === 'logseq') { + data['logSeqFolder'] = folderPath + } else if (pkmToSync === 'obsidian') { + data['obsidianFolder'] = folderPath + } else if (pkmToSync === 'backup') { + data['backupFolder'] = folderPath + } + + // Write the update to local storage + await deps.storageAPI.local.set({ PKMSYNCpkmFolders: data }) + + return data +}