32cd2100d9dd11140705c848850e86c4f79191c4
max
Tue Sep 1 07:02:04 2026 -0700
Reusable "view snapshot" sessions for durable, minimal Share-a-link links
"Share a link" could only share the whole cart: it saved a session holding
every track, setting and position, which bloated the central db and leaked
the sharer's unrelated tracks to whoever opened the link. Anonymous share
sessions (under the reserved user "l") were also never reaped, so they
accumulated forever, and their 8-char names were generated client-side with
no uniqueness check, so two shares could collide and silently overwrite.
Adds a lightweight "snapshot session" facility (lib/snapshotSession.c): a
snapshot stores only the handful of cart variables a feature declares (a
registered snapshotType, e.g. "blat" -> {db, blatLastBigBed}), moves only
those variables' trash files into durable sessionData storage, and is saved
under a "__"-prefixed name. The "__" marks it machine-made: hidden from the
My Sessions list by default and eligible for reaping. For the anonymous "l"
owner the durable files fan out over two extra hash levels so one directory
never fills with millions of entries.
Every anonymous link now shares one server-side name generator
(snapshotNewName): a unique (db-checked), crypto-strong, "__"-prefixed token,
so tokens never collide. The hgSession doSaveSessionJson endpoint gained
hgS_snapshotType (save a minimal snapshot rather than the whole cart) and
hgS_doAnonName (reserve a unique anonymous name without saving, so the
top-right dialog can preview the exact link before it is created).
Wired three callers to the facility:
- hgc htcBlatAlign "Share a link": a minimal "blat" snapshot instead of a
full-cart anonymous session.
- hgBlat results "Share a link": creates a "blat" snapshot on click and
reveals its ?u=&s= reopen link (rebuilt from the durable bigPsl by the
existing doShareReopen), replacing the trash-only reveal.
- top-right "Share a link": anonymous links use the reserved server name;
logged-in named shares are unchanged.
snapshotReaper (hg/utils) garbage-collects abandoned anonymous snapshots: it
deletes user "l" "__" rows whose lastUse is older than the TTL (hg.conf
snapshot.ttlDays, default ~4 years) and removes their durable files. lastUse
is bumped on every open by the existing session load, so a link stays alive
as long as it is used. Meant to run from the trash-cleaner cron.
Also folds in the recent Share-dialog work in these files: the auto share
name is a short "_" prefix instead of "share_", the dialog previews the link
and creates it only when the button is clicked (no orphan session just from
opening the dialog), an optional name field with an overwrite warning, and
the anonymous save path forces the reap-eligible "__" name.
refs #38197
diff --git src/hg/js/topLinks.js src/hg/js/topLinks.js
index 0ffa1e94e6b..b20c006ee79 100644
--- src/hg/js/topLinks.js
+++ src/hg/js/topLinks.js
@@ -1,386 +1,552 @@
// topLinks.js - behavior for the two top-right menu-bar links, "Login" and "Share a link".
//
// Login: when the user is logged in, the menu item shows their username and opens a small
// account dialog (change password, sign out). When logged out it is a plain link to hgSession,
// so this script does nothing for it.
//
// Share a link (browser pages only): saves the current view as a session and shows a copyable
// short link. Logged-in users get to name the session and choose whether others may open it;
// logged-out users get an anonymous link created on the fly.
//
// The C code that builds the menu bar (lib/web.c menuBar() for CGI pages, hgMenubar.c for static
// pages) fills in the data-* attributes that this script reads. The dialog is a self-contained
// overlay so it works on static pages too (no jQuery UI dependency).
/* jshint esversion: 8 */
/* global $, document, window, URL, getHgsid, copyToClipboard */
var topLinks = (function() {
"use strict";
// Small helper to build an element with properties and inline styles set from the DOM
// (assigning to element.style.* is CSP-safe, unlike a style="" attribute in markup).
function el(tag, props, styles) {
var e = document.createElement(tag);
var k;
if (props)
for (k in props)
if (props.hasOwnProperty(k)) e[k] = props[k];
if (styles)
for (k in styles)
if (styles.hasOwnProperty(k)) e.style[k] = styles[k];
return e;
}
var closeCurrent = null; // closer for the currently open modal, if any
function closeModal() {
if (closeCurrent) { closeCurrent(); closeCurrent = null; }
}
// Show a centered modal with the given title and body node. Returns the body element so the
// caller can replace its contents later (e.g. swap a spinner for the resulting link).
function showModal(titleText, bodyNode, widthPx) {
closeModal();
var overlay = el("div", {}, {
position: "fixed", top: "0", left: "0", right: "0", bottom: "0",
background: "rgba(0,0,0,0.4)", zIndex: "10000"
});
var box = el("div", {}, {
position: "fixed", top: "90px", left: "50%", transform: "translateX(-50%)",
background: "#fff", color: "#000", border: "1px solid #888", borderRadius: "4px",
boxShadow: "0 2px 12px rgba(0,0,0,0.4)", width: (widthPx || 430) + "px", maxWidth: "92%",
zIndex: "10001", fontSize: "14px"
});
var titleBar = el("div", {}, {
background: "#00457c", color: "#fff", padding: "8px 12px", fontWeight: "bold",
borderTopLeftRadius: "4px", borderTopRightRadius: "4px", position: "relative"
});
titleBar.appendChild(document.createTextNode(titleText));
var x = el("span", {title: "Close", textContent: "×"}, {
position: "absolute", right: "10px", top: "5px", cursor: "pointer",
fontSize: "18px", lineHeight: "18px"
});
x.addEventListener("click", closeModal);
titleBar.appendChild(x);
var body = el("div", {}, {padding: "14px"});
if (bodyNode) body.appendChild(bodyNode);
box.appendChild(titleBar);
box.appendChild(body);
overlay.appendChild(box);
document.body.appendChild(overlay);
// Swallow Escape and clicks so they don't also reach an underlying dialog's own
// close-on-escape / click-outside handlers (e.g. the hgc item-details popup).
function onKey(ev) {
if (ev.key === "Escape") {
ev.preventDefault();
ev.stopImmediatePropagation();
closeModal();
}
}
overlay.addEventListener("click", function(ev) {
if (ev.target === overlay)
closeModal();
ev.stopPropagation();
});
document.addEventListener("keydown", onKey, true); // capture: run before other handlers
closeCurrent = function() {
document.removeEventListener("keydown", onKey, true);
if (overlay.parentNode) overlay.parentNode.removeChild(overlay);
};
return body;
}
// ---- Login / account dialog --------------------------------------------------------------
function showLoginDialog(link) {
var user = link.getAttribute("data-username");
var logoutUrl = link.getAttribute("data-logouturl");
var changePwUrl = link.getAttribute("data-changepwurl");
var changeEmailUrl = link.getAttribute("data-changeemailurl");
var body = document.createElement("div");
var p = el("p");
p.appendChild(document.createTextNode("Signed in as "));
p.appendChild(el("b", {textContent: user})); // textContent avoids HTML injection
p.appendChild(document.createTextNode("."));
body.appendChild(p);
// helper: add an