From f7a4d7ef39be6957192e1f3ffe6de4493760eb4e Mon Sep 17 00:00:00 2001 From: Paul Gebheim Date: Sat, 28 Nov 2026 22:57:36 +0000 Subject: [PATCH] feat: add a server region setting for region-locked Cloud accounts Supernote Cloud runs regional deployments and accounts are locked to one. Add a cloudRegion setting (international or China) so users can point the plugin at their region's servers. - cloud client: CLOUD_REGION_BASE_URLS + setRegion() repoints the base URL and drops the per-host CSRF token - main: cloudRegion setting (default international, validated on load); switching region signs out, since a token belongs to one deployment - setup flow: Server region dropdown shown before sign-in - tests: default + China request URLs; settings factory gains cloudRegion --- src/cloud/client.ts | 24 +++++++++++++++++++++--- src/main.ts | 18 ++++++++++++++++++ src/ui/setup-flow.ts | 22 ++++++++++++++++++++++ tests/cloud-client.test.ts | 30 ++++++++++++++++++++++++++++++ tests/transcription-draft.test.ts | 1 + 5 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/cloud/client.ts b/src/cloud/client.ts index d31af85..9ce1c54 100644 --- a/src/cloud/client.ts +++ b/src/cloud/client.ts @@ -15,7 +15,15 @@ import { normalizeVerificationCode, } from "./verification-code"; -const BASE_URL = "https://cloud.supernote.com/api"; +// Supernote runs separate, region-locked Cloud deployments: an account +// registered on one does not exist on the other. The client defaults to the +// international deployment; setRegion() switches before login. +export type SupernoteCloudRegion = "international" | "china"; + +export const CLOUD_REGION_BASE_URLS: Record = { + international: "https://cloud.supernote.com/api", + china: "https://cloud.supernote.com.cn/api", +}; export interface RequestOptions { url: string; @@ -106,6 +114,7 @@ export class SupernoteCloudClient { private token: string | null; private csrfToken: string | null = null; + private baseUrl = CLOUD_REGION_BASE_URLS.international; constructor( token: string | null, @@ -115,6 +124,15 @@ export class SupernoteCloudClient this.token = token; } + // Points the client at a different Cloud deployment. CSRF tokens are + // per-host, so any captured one is dropped. Auth tokens are NOT cleared + // here — the caller owns session semantics (a token from another + // deployment is meaningless, so the plugin signs out on region change). + setRegion(region: SupernoteCloudRegion): void { + this.baseUrl = CLOUD_REGION_BASE_URLS[region]; + this.csrfToken = null; + } + get accessToken(): string | null { return this.token; } @@ -440,7 +458,7 @@ export class SupernoteCloudClient } const response = await this.request({ - url: `${BASE_URL}${path}`, + url: `${this.baseUrl}${path}`, method: "POST", headers, body: JSON.stringify(payload), @@ -494,7 +512,7 @@ export class SupernoteCloudClient private async refreshCsrf(): Promise { const response = await this.request({ - url: `${BASE_URL}/csrf`, + url: `${this.baseUrl}/csrf`, method: "GET", }); this.assertHttpStatus(response.status); diff --git a/src/main.ts b/src/main.ts index 840fc5d..654f1a3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,6 +17,7 @@ import { SupernoteAuthExpiredError, SupernoteCloudClient, type RequestExecutor, + type SupernoteCloudRegion, type SupernoteVerificationChallenge, type SupernoteVerificationRequired, } from "./cloud/client"; @@ -179,6 +180,8 @@ import { } from "./viewer/state"; export interface SupernoteSyncSettings { + /** Which Supernote Cloud deployment the account lives on (region-locked). */ + cloudRegion: SupernoteCloudRegion; targetFolder: string; pushFolder: string; autoSyncMinutes: number; @@ -216,6 +219,7 @@ export interface PairDisablePreview { } const DEFAULT_SETTINGS: SupernoteSyncSettings = { + cloudRegion: "international", targetFolder: "supernote", pushFolder: "Document/Obsidian", autoSyncMinutes: 0, @@ -448,6 +452,10 @@ export default class SupernoteSyncPlugin extends Plugin { ? stored.pairDirectoryId : null, settings: { + cloudRegion: + storedSettings?.cloudRegion === "china" + ? "china" + : DEFAULT_SETTINGS.cloudRegion, targetFolder: storedSettings?.targetFolder ?? DEFAULT_SETTINGS.targetFolder, pushFolder: storedSettings?.pushFolder ?? DEFAULT_SETTINGS.pushFolder, @@ -532,6 +540,7 @@ export default class SupernoteSyncPlugin extends Plugin { }; this.cloud = new SupernoteCloudClient(this.data.token, request); + this.cloud.setRegion(this.data.settings.cloudRegion); this.vaultStore = new ObsidianVaultStore( this.app.vault, this.app.fileManager, @@ -770,8 +779,13 @@ export default class SupernoteSyncPlugin extends Plugin { } async updateSettings(update: Partial): Promise { + const regionChanged = + update.cloudRegion !== undefined && + update.cloudRegion !== this.data.settings.cloudRegion; const nextData: PluginData = { ...this.data, + // A Cloud token belongs to one deployment; switching region signs out. + ...(regionChanged ? { token: null } : {}), settings: { ...this.data.settings, ...update }, }; if (update.autoSyncMinutes !== undefined) { @@ -780,6 +794,10 @@ export default class SupernoteSyncPlugin extends Plugin { } await this.persistData(nextData); this.data = nextData; + if (regionChanged) { + this.cloud.setRegion(this.data.settings.cloudRegion); + this.cloud.logout(); + } if (update.watchHooks !== undefined) { this.refreshWatchHookCommands(); } diff --git a/src/ui/setup-flow.ts b/src/ui/setup-flow.ts index 4f8f221..c163899 100644 --- a/src/ui/setup-flow.ts +++ b/src/ui/setup-flow.ts @@ -74,6 +74,28 @@ export const setupFlowView = ( let email = ""; let password = ""; let passwordInput: HTMLInputElement | null = null; + // Supernote accounts are region-locked: an account registered on the + // Chinese deployment does not exist on the international one (and vice + // versa), so the region must be right before signing in. Hidden once + // signed in — changing it signs you out (see updateSettings). + if (!plugin.isLoggedIn) { + new Setting(container) + .setName("Server region") + .setDesc( + "Where your Supernote account was registered. Pick China if your account was created on cloud.supernote.com.cn — the wrong region looks like a wrong password.", + ) + .addDropdown((dropdown) => + dropdown + .addOption("international", "International") + .addOption("china", "China") + .setValue(plugin.settings.cloudRegion) + .onChange((value) => { + void plugin.updateSettings({ + cloudRegion: value === "china" ? "china" : "international", + }); + }), + ); + } new Setting(container) .setName("Account") .setDesc(prerequisite(plugin, "account").detail) diff --git a/tests/cloud-client.test.ts b/tests/cloud-client.test.ts index 537505c..5d7e741 100644 --- a/tests/cloud-client.test.ts +++ b/tests/cloud-client.test.ts @@ -385,3 +385,33 @@ describe("SupernoteCloudClient", () => { }); }); }); + +describe("SupernoteCloudClient regions", () => { + const captureUrls = () => { + const urls: string[] = []; + const request: RequestExecutor = vi.fn(async (options) => { + urls.push(options.url); + return { + status: 200, + json: { success: true }, + arrayBuffer: new ArrayBuffer(0), + }; + }); + return { urls, request }; + }; + + it("defaults to the international deployment", async () => { + const { urls, request } = captureUrls(); + const client = new SupernoteCloudClient(null, request); + await client.login("user@example.com", "pw").catch(() => undefined); + expect(urls[0]).toMatch(/^https:\/\/cloud\.supernote\.com\/api\//); + }); + + it('sends requests to the China deployment after setRegion("china")', async () => { + const { urls, request } = captureUrls(); + const client = new SupernoteCloudClient(null, request); + client.setRegion("china"); + await client.login("user@example.com", "pw").catch(() => undefined); + expect(urls[0]).toMatch(/^https:\/\/cloud\.supernote\.com\.cn\/api\//); + }); +}); diff --git a/tests/transcription-draft.test.ts b/tests/transcription-draft.test.ts index c047452..ec52eca 100644 --- a/tests/transcription-draft.test.ts +++ b/tests/transcription-draft.test.ts @@ -10,6 +10,7 @@ import type { SupernoteSyncSettings } from "../src/main"; const settings = ( update: Partial = {}, ): SupernoteSyncSettings => ({ + cloudRegion: "international", targetFolder: "supernote", pushFolder: "Document/Obsidian", autoSyncMinutes: 0,