Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/cloud/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SupernoteCloudRegion, string> = {
international: "https://cloud.supernote.com/api",
china: "https://cloud.supernote.com.cn/api",
};

export interface RequestOptions {
url: string;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -494,7 +512,7 @@ export class SupernoteCloudClient

private async refreshCsrf(): Promise<void> {
const response = await this.request({
url: `${BASE_URL}/csrf`,
url: `${this.baseUrl}/csrf`,
method: "GET",
});
this.assertHttpStatus(response.status);
Expand Down
18 changes: 18 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SupernoteAuthExpiredError,
SupernoteCloudClient,
type RequestExecutor,
type SupernoteCloudRegion,
type SupernoteVerificationChallenge,
type SupernoteVerificationRequired,
} from "./cloud/client";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -216,6 +219,7 @@ export interface PairDisablePreview {
}

const DEFAULT_SETTINGS: SupernoteSyncSettings = {
cloudRegion: "international",
targetFolder: "supernote",
pushFolder: "Document/Obsidian",
autoSyncMinutes: 0,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -770,8 +779,13 @@ export default class SupernoteSyncPlugin extends Plugin {
}

async updateSettings(update: Partial<SupernoteSyncSettings>): Promise<void> {
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) {
Expand All @@ -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();
}
Expand Down
22 changes: 22 additions & 0 deletions src/ui/setup-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions tests/cloud-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\//);
});
});
1 change: 1 addition & 0 deletions tests/transcription-draft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { SupernoteSyncSettings } from "../src/main";
const settings = (
update: Partial<SupernoteSyncSettings> = {},
): SupernoteSyncSettings => ({
cloudRegion: "international",
targetFolder: "supernote",
pushFolder: "Document/Obsidian",
autoSyncMinutes: 0,
Expand Down