-
-
Notifications
You must be signed in to change notification settings - Fork 676
feat: load shared desktop settings from URL #2017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { normalizeDesktopSettings, type DesktopSettings } from "../hooks/useDesktopSettings"; | ||
| import { resolveLanguage } from "../i18n/languages"; | ||
|
|
||
| export const DESKTOP_SETTINGS_URL_PARAMS = ["settingsUrl", "settingUrl"] as const; | ||
| const LANGUAGE_URL_PARAMS = ["locale", "lang"] as const; | ||
| const DEFAULT_FETCH_TIMEOUT_MS = 10_000; | ||
|
|
||
| /** | ||
| * Keep URL-controlled settings limited to presentation. In particular, a | ||
| * shared link must never supply credentials, plugin sources, or local paths. | ||
| */ | ||
| export function normalizeSharedDesktopSettings(settings: Record<string, unknown>): DesktopSettings { | ||
| return normalizeDesktopSettings({ | ||
| language: settings.language, | ||
| layout: settings.layout, | ||
| theme: settings.theme, | ||
| uiProfile: settings.uiProfile, | ||
| }); | ||
| } | ||
|
|
||
| export function desktopSettingsUrl(search: string): string | null { | ||
| const params = new URLSearchParams(search); | ||
| for (const name of DESKTOP_SETTINGS_URL_PARAMS) { | ||
| const value = params.get(name)?.trim(); | ||
| if (value) return value; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export async function fetchDesktopSettings( | ||
| url: string, | ||
| options: { fetchImpl?: typeof fetch; timeoutMs?: number } = {}, | ||
| ): Promise<DesktopSettings> { | ||
| const { fetchImpl = fetch, timeoutMs = DEFAULT_FETCH_TIMEOUT_MS } = options; | ||
| const response = await fetchImpl(url, { | ||
| cache: "no-cache", | ||
| credentials: "same-origin", | ||
| signal: AbortSignal.timeout(timeoutMs), | ||
| }); | ||
|
giswqs marked this conversation as resolved.
|
||
| if (!response.ok) { | ||
| const status = response.statusText | ||
| ? `${response.status} ${response.statusText}` | ||
| : String(response.status); | ||
| throw new Error(`Could not load desktop settings from ${url} (HTTP ${status}).`); | ||
| } | ||
|
|
||
| let parsed: unknown; | ||
| try { | ||
| parsed = JSON.parse(await response.text()) as unknown; | ||
| } catch (error) { | ||
| throw new Error(`Desktop settings at ${url} are not valid JSON.`, { | ||
| cause: error, | ||
| }); | ||
| } | ||
| if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { | ||
| throw new Error(`Desktop settings at ${url} must be a JSON object.`); | ||
| } | ||
| return normalizeSharedDesktopSettings(parsed as Record<string, unknown>); | ||
| } | ||
|
|
||
| /** Resolve a shared language only when a valid locale/lang URL override is absent. */ | ||
|
giswqs marked this conversation as resolved.
|
||
| export function sharedSettingsLanguage( | ||
| search: string, | ||
| language: string, | ||
| availableLanguages: readonly string[], | ||
| ): string | null { | ||
| const params = new URLSearchParams(search); | ||
| for (const name of LANGUAGE_URL_PARAMS) { | ||
| if (resolveLanguage(params.get(name), availableLanguages)) return null; | ||
| } | ||
| return resolveLanguage(language, availableLanguages); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { describe, it } from "node:test"; | ||
| import { | ||
| desktopSettingsUrl, | ||
| fetchDesktopSettings, | ||
| sharedSettingsLanguage, | ||
| } from "../apps/geolibre-desktop/src/lib/desktop-settings-url"; | ||
|
|
||
| describe("desktop settings URL", () => { | ||
| it("prefers settingsUrl and accepts the discussion's settingUrl spelling", () => { | ||
| assert.equal( | ||
| desktopSettingsUrl("?settingsUrl=https%3A%2F%2Fexample.com%2Fa.json"), | ||
| "https://example.com/a.json", | ||
| ); | ||
| assert.equal( | ||
| desktopSettingsUrl("?settingUrl=https%3A%2F%2Fexample.com%2Fb.json"), | ||
| "https://example.com/b.json", | ||
| ); | ||
| assert.equal( | ||
| desktopSettingsUrl("?settingsUrl=&settingUrl=https%3A%2F%2Fexample.com%2Fb.json"), | ||
| "https://example.com/b.json", | ||
| ); | ||
| assert.equal(desktopSettingsUrl("?url=project.json"), null); | ||
| }); | ||
|
|
||
| it("fetches without trusting a stale cached settings file and normalizes it", async () => { | ||
| let init: RequestInit | undefined; | ||
| const fetchImpl = (async (_url: string, requestInit?: RequestInit) => { | ||
| init = requestInit; | ||
| return new Response( | ||
| JSON.stringify({ | ||
| layout: { toolbarLabels: false }, | ||
| uiProfile: { enabled: true, hiddenMenus: ["help", "help"] }, | ||
| shareToken: "remote-token-must-not-load", | ||
| cesiumIonToken: "remote-cesium-token-must-not-load", | ||
| aiProfiles: [{ id: "remote", fieldValues: { API_KEY: "secret" } }], | ||
| pluginManifestUrls: ["https://evil.example/plugin.json"], | ||
| startup: { mode: "specific", projectPath: "/remote/path" }, | ||
| }), | ||
| ); | ||
| }) as typeof fetch; | ||
|
|
||
| const settings = await fetchDesktopSettings("https://example.com/settings.json", { | ||
| fetchImpl, | ||
| timeoutMs: 500, | ||
| }); | ||
| assert.equal(init?.cache, "no-cache"); | ||
| assert.equal(init?.credentials, "same-origin"); | ||
| assert.ok(init?.signal); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| assert.equal(settings.layout.toolbarLabels, false); | ||
| assert.deepEqual(settings.uiProfile.hiddenMenus, ["help"]); | ||
| assert.equal(settings.shareToken, ""); | ||
| assert.equal(settings.cesiumIonToken, ""); | ||
| assert.deepEqual(settings.aiProfiles, []); | ||
| assert.deepEqual(settings.pluginManifestUrls, []); | ||
| assert.equal(settings.startup.mode, "default"); | ||
| }); | ||
|
|
||
| it("reports HTTP, malformed JSON, and non-object documents", async () => { | ||
| await assert.rejects( | ||
| fetchDesktopSettings("https://example.com/missing.json", { | ||
| fetchImpl: async () => new Response("", { status: 404 }), | ||
| }), | ||
| /HTTP 404/, | ||
| ); | ||
| await assert.rejects( | ||
| fetchDesktopSettings("https://example.com/bad.json", { | ||
| fetchImpl: async () => new Response("{"), | ||
| }), | ||
| /not valid JSON/, | ||
| ); | ||
| await assert.rejects( | ||
| fetchDesktopSettings("https://example.com/list.json", { | ||
| fetchImpl: async () => new Response("[]"), | ||
| }), | ||
| /must be a JSON object/, | ||
| ); | ||
| }); | ||
|
|
||
| it("lets a remote language replace the saved language before render", () => { | ||
| const available = ["en", "de", "fr"]; | ||
| // The app may have initialized from a saved `de`; main.tsx switches to the | ||
| // resolved remote `fr` before mounting React. | ||
| assert.equal(sharedSettingsLanguage("?settingsUrl=settings.json", "fr", available), "fr"); | ||
| assert.equal(sharedSettingsLanguage("?settingsUrl=settings.json", "unknown", available), null); | ||
| }); | ||
|
|
||
| it("keeps locale and lang URL parameters above a remote language", () => { | ||
| const available = ["en", "de", "fr"]; | ||
| assert.equal(sharedSettingsLanguage("?locale=de", "fr", available), null); | ||
| assert.equal(sharedSettingsLanguage("?lang=de", "fr", available), null); | ||
| // An unsupported explicit locale follows the existing fallback behavior. | ||
| assert.equal(sharedSettingsLanguage("?locale=unknown", "fr", available), "fr"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.