Skip to content

Commit 1efc836

Browse files
committed
fix(website): count only OpenScreen release assets on github.com as downloads
1 parent ddb5911 commit 1efc836

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎website/src/lib/download-target.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,6 @@ test("a download click names the asset kind and the release, and other links are
163163
assert.equal(downloadClick(STORE_INSTALLER_URL)?.kind, "store");
164164
assert.equal(downloadClick("https://github.com/getopenscreen/openscreen/releases/latest"), null);
165165
assert.equal(downloadClick(STORE_URL), null);
166+
assert.equal(downloadClick("https://github.com/someone/else/releases/download/v1/x.dmg"), null);
167+
assert.equal(downloadClick(`https://example.com/?next=${base}/Openscreen-mac-arm64.dmg`), null);
166168
});

‎website/src/lib/download-target.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,19 @@ export type DownloadClick = {
117117
file: string | null;
118118
};
119119

120-
/** What a link downloads, for the analytics event, or null when it downloads nothing. */
120+
const RELEASE_ASSET_PATH = /^\/getopenscreen\/openscreen\/releases\/download\/([^/]+)\/([^/]+)$/;
121+
122+
/** What a link downloads, for the analytics event, or null when it downloads nothing: an
123+
* OpenScreen release asset on github.com, or the Store installer. */
121124
export function downloadClick(href: string): DownloadClick | null {
122125
if (href.startsWith(STORE_INSTALLER_URL)) return { kind: "store", version: null, file: null };
123-
const match = /\/releases\/download\/([^/]+)\/([^/?#]+)/.exec(href);
126+
let url: URL;
127+
try {
128+
url = new URL(href);
129+
} catch {
130+
return null;
131+
}
132+
const match = url.hostname === "github.com" ? RELEASE_ASSET_PATH.exec(url.pathname) : null;
124133
if (!match) return null;
125134
const file = decodeURIComponent(match[2]);
126135
const kind = (Object.keys(ASSET_PATTERNS) as AssetKind[]).find((k) =>

0 commit comments

Comments
 (0)