diff --git a/CHANGELOG.md b/CHANGELOG.md index cc45700..5892026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,11 @@ release path documented in [`TODO.md`](TODO.md). - The command line, a second invocation, and macOS `open-url` all accept `musrflow://open/#c=…` links alongside the existing http(s) and bare fragment forms, restoring through the same shared-configuration path. +- Legacy web deep links are now accepted too: + `musrflow://…/muSRFlow?action=plot&YEAR=…&BeamLine=…&RunNumbers=…` (and the + equivalent `http(s)://…/muSRFlow?…` URL) initialize the run controls instead + of an encoded fragment, and run the plot when `action=plot` is present — + matching the web page's query-string behavior. - The web Share flow gains a desktop-app link that emits the identical encoded fragment under `musrflow://open/`, for users who installed the desktop build. - `npm run checksums` generates `md5`/`sha256` sidecar files for the packaged diff --git a/docs/DESKTOP_DEPLOYMENT.md b/docs/DESKTOP_DEPLOYMENT.md index c4af577..1b30b13 100644 --- a/docs/DESKTOP_DEPLOYMENT.md +++ b/docs/DESKTOP_DEPLOYMENT.md @@ -110,7 +110,11 @@ claims the scheme at startup with The launch path accepts the same links as the web form: the full `http(s)://...#c=...` URL, the bare `#c=...` fragment, and `musrflow://open/` -(optionally with a path component) followed by the fragment. +(optionally with a path component) followed by the fragment. Legacy web-style +deep links are also accepted: `musrflow://host/muSRFlow?action=plot&YEAR=...&BeamLine=...&RunNumbers=...` +(or the equivalent `http(s)://.../muSRFlow?...` URL). Those carry the run/beam-line +selection in the query string instead of an encoded fragment and initialize the +run controls, running the plot when `action=plot` is present. ## Versioning diff --git a/main.js b/main.js index a7c12e2..37da592 100644 --- a/main.js +++ b/main.js @@ -126,17 +126,35 @@ function createParameterPlotWindow(options = {}) { // Create the Electron shell; fitting logic remains in the shared renderer. // A configuration link is an http(s)://…/muSRFlow/#c=… (or #cfg=…) URL, or the // desktop protocol form musrflow://open/#c=… that this app is registered to -// receive. Only such URLs are accepted from the command line, a second -// instance, or open-url; the HTTP target is never fetched, mirroring the web -// restore path, and musrflow:// links only carry the fragment (no content). +// receive. A legacy web deep link carries run parameters in the query string +// instead (musrflow://…/muSRFlow?action=plot&YEAR=…&BeamLine=…&RunNumbers=…, or +// the equivalent http(s)://…/muSRFlow?… URL) and is forwarded the same way. +// Only such URLs are accepted from the command line, a second instance, or +// open-url; the HTTP target is never fetched, mirroring the web restore path. function findSharedConfigurationURL(args) { if (!Array.isArray(args)) return ""; for (const argument of args) { const value = String(argument || ""); - if (/^(?:https?:\/\/|musrflow:\/\/)/i.test(value) && /#(?:c|cfg)=/.test(value)) { + // Bare fragment (starting #c=/#cfg=) is the app's own restore form. + if (/^#(?:c|cfg)=/.test(value)) { return value; } - if (/^#(?:c|cfg)=/.test(value)) { + if (!/^(?:https?:\/\/|musrflow:\/\/)/i.test(value)) { + continue; + } + // Encoded configuration fragment on a full URL. + if (/#(?:c|cfg)=/.test(value)) { + return value; + } + // Legacy web deep link: query-string parameters instead of a fragment. + const queryAt = value.indexOf("?"); + if (queryAt < 0) { + continue; + } + const path = value.slice(0, queryAt).replace(/\/+$/, ""); + const ownProtocol = /^musrflow:\/\//i.test(value); + const muSRFlowPath = /\/muSRFlow$/i.test(path); + if (ownProtocol || muSRFlowPath) { return value; } } @@ -363,7 +381,7 @@ if (relaunchingForForwardedX11) { // may fire before the window exists. app.on('open-url', function (event, url) { event.preventDefault(); - if (!url || !/^(?:https?:\/\/|musrflow:\/\/)/i.test(url) || !/#(?:c|cfg)=/.test(url)) { + if (!url || !findSharedConfigurationURL([url])) { return; } if (mainWindow) { diff --git a/muSRFitelec.js b/muSRFitelec.js index bbadeea..db6d497 100644 --- a/muSRFitelec.js +++ b/muSRFitelec.js @@ -624,6 +624,30 @@ function openElectronSharedConfiguration() { }, 0); } +// A legacy (web-style) deep link carries run/beam-line/year selection in the +// query string (musrflow://…/muSRFlow?action=plot&YEAR=…&BeamLine=…&RunNumbers=…) +// instead of an encoded #c= configuration fragment. It reuses the web shell's +// parser to initialize the same GUI controls and, for action=plot, starts the +// established Plot flow once any initial-parameter guess is ready. Invalid +// values surface through the URL-parameter warning channel, same as on the web. +async function handleDesktopLegacyURL(sharedURL) { + let search = ""; + try { + search = new URL(String(sharedURL)).search; + } catch (_error) { + return; + } + if (!search) { + return; + } + let result = (typeof applyWebURLParameters === "function") + ? applyWebURLParameters(search) + : { applied: {}, errors: [], action: "" }; + if (result.action === "plot" && typeof triggerWebURLAction === "function") { + await triggerWebURLAction(result); + } +} + async function restoreSharedConfigurationFromSource(sourceText, errorElement) { let decoded = await decodeElectronSharedConfiguration(sourceText || ""); if (decoded.error) { @@ -1897,12 +1921,18 @@ function firstLoad() { }); electronIpcRenderer.on('openSharedConfiguration', function(sharedURL) { - // A deep link (http(s)://.../muSRFlow/#c=... or a bare #c= fragment) - // restores immediately, following exactly the same decode/restore path as - // the paste dialog minus the dialog itself. With no URL the Share menu - // paste dialog opens. + // A configuration deep link (http(s)://.../muSRFlow/#c=... or a bare #c= + // fragment) restores immediately, following exactly the same decode/restore + // path as the paste dialog minus the dialog itself. A legacy web deep link + // (musrflow://.../muSRFlow?action=plot&YEAR=...&BeamLine=...&RunNumbers=...) + // carries query-string run parameters and is applied through the shared web + // parser, which may also trigger a Plot. With no URL the Share menu paste + // dialog opens. if (sharedURL && typeof sharedURL === "string") { - return restoreSharedConfigurationFromSource(sharedURL, null); + if (/#(?:c|cfg)=/.test(sharedURL)) { + return restoreSharedConfigurationFromSource(sharedURL, null); + } + return handleDesktopLegacyURL(sharedURL); } openElectronSharedConfiguration(); }); diff --git a/tests/shared-configuration.test.js b/tests/shared-configuration.test.js index dd7dea1..56a07e7 100644 --- a/tests/shared-configuration.test.js +++ b/tests/shared-configuration.test.js @@ -249,6 +249,95 @@ function testElectronMenuAndRendererAreConnected() { assert.strictEqual(typeof env.ipcListeners.openSharedConfiguration, "function"); } +// A legacy web deep link (musrflow://…/muSRFlow?action=plot&YEAR=…&BeamLine=…& +// RunNumbers=…) is dispatched through the shared web URL parser instead of the +// encoded-fragment restore path, and its plot action runs the established Plot +// flow. This mirrors exactly what the Electron renderer does when the main +// process forwards such a link from the command line or a second instance. +async function testElectronLegacyDeepLinkUsesWebParser() { + const env = loadSharedConfigurationHelpers(); + const lib = env.sandbox; + let parsedSearch = ""; + let triggerCalls = 0; + let triggeredResult = null; + lib.applyWebURLParameters = function(search) { + parsedSearch = search; + return { + applied: { BeamLine: "LEM", YEAR: "2026", RunNumbers: "3557" }, + errors: [], action: "plot" + }; + }; + lib.triggerWebURLAction = async function(result) { + triggerCalls++; + triggeredResult = result; + return 1; + }; + lib.firstLoad(); + + const legacyURL = "musrflow://musruser.psi.ch/muSRFlow?action=plot&YEAR=2026&BeamLine=LEM&RunNumbers=3557"; + await env.ipcListeners.openSharedConfiguration(legacyURL); + assert.strictEqual(parsedSearch, "?action=plot&YEAR=2026&BeamLine=LEM&RunNumbers=3557"); + assert.strictEqual(triggerCalls, 1); + assert.strictEqual(triggeredResult.action, "plot"); + + // A legacy URL without a plot action initializes controls only. + triggerCalls = 0; + lib.applyWebURLParameters = function() { + return { applied: { BeamLine: "LEM" }, errors: [], action: "" }; + }; + await env.ipcListeners.openSharedConfiguration("musrflow://musruser.psi.ch/muSRFlow?BeamLine=LEM"); + assert.strictEqual(triggerCalls, 0); + + // A musrflow:// URL with no query string is ignored, not restored. + triggerCalls = 0; + let parserCalls = 0; + lib.applyWebURLParameters = function() { parserCalls++; return { applied: {}, action: "" }; }; + await env.ipcListeners.openSharedConfiguration("musrflow://open/"); + assert.strictEqual(triggerCalls, 0); + assert.strictEqual(parserCalls, 0); +} + +// Extract a top-level function body from a source file and evaluate it so the +// main-process deep-link gate can be exercised without launching Electron. +function evaluateMainFunction(name) { + const source = fs.readFileSync(path.join(repoRoot, "main.js"), "utf8"); + const marker = "function " + name + "("; + const start = source.indexOf(marker); + assert.ok(start >= 0, "missing function " + name + " in main.js"); + const bodyStart = source.indexOf("{", start) + 1; + let depth = 1; + let i = bodyStart; + for (; i < source.length && depth > 0; i++) { + if (source[i] === "{") depth++; + else if (source[i] === "}") depth--; + } + assert.strictEqual(depth, 0, "unbalanced brace in " + name); + return new Function("args", source.slice(bodyStart, i - 1)); +} + +function testMainProcessAcceptsLegacyQueryDeepLinks() { + const find = evaluateMainFunction("findSharedConfigurationURL"); + + const legacy = "musrflow://musruser.psi.ch/muSRFlow?action=plot&YEAR=2026&BeamLine=LEM&RunNumbers=3557"; + assert.strictEqual(find([legacy]), legacy); + assert.strictEqual( + find(["https://musruser.psi.ch/muSRFlow?action=plot&BeamLine=LEM&RunNumbers=3557"]), + "https://musruser.psi.ch/muSRFlow?action=plot&BeamLine=LEM&RunNumbers=3557"); + + // Encoded-fragment links (the native desktop form) and bare fragments are + // still recognized unchanged. + assert.strictEqual(find(["musrflow://open/#c=abc"]), "musrflow://open/#c=abc"); + assert.strictEqual(find(["https://musruser.psi.ch/muSRFlow/#cfg=abc"]), + "https://musruser.psi.ch/muSRFlow/#cfg=abc"); + assert.strictEqual(find(["#c=abc"]), "#c=abc"); + + // Unrelated, query-less, or protocol-but-not-muSRFlow values are rejected. + assert.strictEqual(find([]), ""); + assert.strictEqual(find(["musrflow://musruser.psi.ch/muSRFlow"]), ""); + assert.strictEqual(find(["https://example.com/other?action=plot"]), ""); + assert.strictEqual(find(["/data/file.msr"]), ""); +} + function testTransientWarningIsPositionedAndTimed() { const env = loadSharedConfigurationHelpers(); const lib = env.sandbox; @@ -1117,8 +1206,10 @@ async function main() { await testElectronCodecReadsWebCompatibleLinks(); testSharedConfigurationIncludesDetectorMaps(); testElectronMenuAndRendererAreConnected(); + testMainProcessAcceptsLegacyQueryDeepLinks(); testWebDesktopLinkButton(); testTransientWarningIsPositionedAndTimed(); + await testElectronLegacyDeepLinkUsesWebParser(); await testElectronShareProvidesVisibleConfirmation(); await testSharedRestoreCannotChangeLocalPreferences(); await testElectronDialogRestoresStateAndMarksEditorStale();