@steve31415/baselib 5.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ /** The Lurch app's package (lurch android/app/build.gradle.kts applicationId). */
2
+ export declare const LURCH_PACKAGE = "dev.secondthoughts.lurch";
3
+ /**
4
+ * Hosts whose links keep today's behaviour, so Android can open the site's own
5
+ * app or the app stays where it is signed in. Matched with their subdomains
6
+ * (www., m., mobile., music.). The one list for every app; extend it here.
7
+ */
8
+ export declare const KEEP_HOSTS: readonly string[];
9
+ export declare function isAndroid(userAgent?: string): boolean;
10
+ /** Firefox and its Android variants (Focus, Nightly) all carry `Firefox/<version>`. */
11
+ export declare function isFirefox(userAgent?: string): boolean;
12
+ /** True where article links are handed to Firefox: Android, in a browser that is not Firefox. */
13
+ export declare function handsOffToFirefox(userAgent?: string): boolean;
14
+ /** True for an http(s) URL on a host in KEEP_HOSTS (or a subdomain of one). False for anything unparsable. */
15
+ export declare function isKeptHost(href: string): boolean;
16
+ /**
17
+ * Build an Android intent URL that opens a URL in Firefox via Lurch, falling
18
+ * back to the current browser if Lurch isn't installed. Example output:
19
+ * intent://firefox?url=https%3A%2F%2Ft.co%2Fabc#Intent;scheme=lurch;package=dev.secondthoughts.lurch;S.browser_fallback_url=https%3A%2F%2Ft.co%2Fabc;end
20
+ */
21
+ export declare function firefoxIntentUrl(url: string): string;
22
+ /** True when `firefoxHandoffHref` would rewrite this link: a handing-off browser, an http(s) link, a host not kept. */
23
+ export declare function wouldHandOff(href: string, userAgent?: string): boolean;
24
+ /** The href an outbound article link should carry: the Firefox hand-off where `wouldHandOff`, else `href` unchanged. */
25
+ export declare function firefoxHandoffHref(href: string, userAgent?: string): string;
@@ -0,0 +1,78 @@
1
+ // Firefox hand-off for article links on Steve's Android phone (Reader's
2
+ // docs/PHONE-READING-PLAN.md, step 3). The apps run in Chrome; the articles
3
+ // they link to read better in Firefox for Android, which has uBlock Origin and
4
+ // Reader's own extension. Browser-only in use, but pure: every function takes
5
+ // the user agent, defaulting to the current browser's.
6
+ //
7
+ // How: Chrome will not honour a Firefox package hint for an http(s) link (it
8
+ // reserves browser choice to the user and shows a chooser), but it hands a
9
+ // `lurch://` link straight to the Lurch app, an ordinary app that then launches
10
+ // Firefox explicitly (lurch OpenInFirefoxActivity). The intent URL carries a
11
+ // browser_fallback_url, so without Lurch the link opens in the current browser.
12
+ //
13
+ // When: only on Android (the intent:// scheme is Android-only) and only in a
14
+ // browser that is not Firefox (in Firefox the plain link already opens there).
15
+ // Hosts that should open their own app keep the plain link: KEEP_HOSTS.
16
+ /** The Lurch app's package (lurch android/app/build.gradle.kts applicationId). */
17
+ export const LURCH_PACKAGE = 'dev.secondthoughts.lurch';
18
+ /**
19
+ * Hosts whose links keep today's behaviour, so Android can open the site's own
20
+ * app or the app stays where it is signed in. Matched with their subdomains
21
+ * (www., m., mobile., music.). The one list for every app; extend it here.
22
+ */
23
+ export const KEEP_HOSTS = [
24
+ // X / Twitter: permalinks open the X app.
25
+ 'x.com',
26
+ 'twitter.com',
27
+ // YouTube: the YouTube app.
28
+ 'youtube.com',
29
+ 'youtu.be',
30
+ // The fleet's own apps: installed in Chrome and signed in there.
31
+ 'apps.snewman.net',
32
+ ];
33
+ export function isAndroid(userAgent = currentUserAgent()) {
34
+ return /Android/i.test(userAgent);
35
+ }
36
+ /** Firefox and its Android variants (Focus, Nightly) all carry `Firefox/<version>`. */
37
+ export function isFirefox(userAgent = currentUserAgent()) {
38
+ return /\bFirefox\//.test(userAgent);
39
+ }
40
+ /** True where article links are handed to Firefox: Android, in a browser that is not Firefox. */
41
+ export function handsOffToFirefox(userAgent = currentUserAgent()) {
42
+ return isAndroid(userAgent) && !isFirefox(userAgent);
43
+ }
44
+ /** True for an http(s) URL on a host in KEEP_HOSTS (or a subdomain of one). False for anything unparsable. */
45
+ export function isKeptHost(href) {
46
+ const host = httpHost(href);
47
+ return host !== null && KEEP_HOSTS.some((kept) => host === kept || host.endsWith(`.${kept}`));
48
+ }
49
+ /**
50
+ * Build an Android intent URL that opens a URL in Firefox via Lurch, falling
51
+ * back to the current browser if Lurch isn't installed. Example output:
52
+ * intent://firefox?url=https%3A%2F%2Ft.co%2Fabc#Intent;scheme=lurch;package=dev.secondthoughts.lurch;S.browser_fallback_url=https%3A%2F%2Ft.co%2Fabc;end
53
+ */
54
+ export function firefoxIntentUrl(url) {
55
+ const enc = encodeURIComponent(url);
56
+ return `intent://firefox?url=${enc}#Intent;scheme=lurch;package=${LURCH_PACKAGE};S.browser_fallback_url=${enc};end`;
57
+ }
58
+ /** True when `firefoxHandoffHref` would rewrite this link: a handing-off browser, an http(s) link, a host not kept. */
59
+ export function wouldHandOff(href, userAgent = currentUserAgent()) {
60
+ return handsOffToFirefox(userAgent) && httpHost(href) !== null && !isKeptHost(href);
61
+ }
62
+ /** The href an outbound article link should carry: the Firefox hand-off where `wouldHandOff`, else `href` unchanged. */
63
+ export function firefoxHandoffHref(href, userAgent = currentUserAgent()) {
64
+ return wouldHandOff(href, userAgent) ? firefoxIntentUrl(href) : href;
65
+ }
66
+ function httpHost(href) {
67
+ if (!/^https?:\/\//i.test(href))
68
+ return null;
69
+ try {
70
+ return new URL(href).hostname.toLowerCase();
71
+ }
72
+ catch {
73
+ return null;
74
+ }
75
+ }
76
+ function currentUserAgent() {
77
+ return typeof navigator === 'undefined' ? '' : navigator.userAgent;
78
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Plasticine new-world shared platform library: logging, auth, service-to-service auth, db, HTTP, sync, app updates",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -112,6 +112,10 @@
112
112
  "./extension-self-update": {
113
113
  "types": "./dist/extension-self-update.d.ts",
114
114
  "default": "./dist/extension-self-update.js"
115
+ },
116
+ "./firefox-handoff": {
117
+ "types": "./dist/firefox-handoff.d.ts",
118
+ "default": "./dist/firefox-handoff.js"
115
119
  }
116
120
  },
117
121
  "bin": {
@@ -137,8 +141,8 @@
137
141
  },
138
142
  "dependencies": {
139
143
  "@google-cloud/cloud-sql-connector": "^1.8.2",
140
- "@hono/node-server": "^1.14.0",
141
- "@hono/node-ws": "^1.3.1",
144
+ "@hono/node-server": "^1.19.19",
145
+ "@hono/node-ws": "1.3.1",
142
146
  "@modelcontextprotocol/sdk": "^1.32.1",
143
147
  "google-auth-library": "^10.5.0",
144
148
  "hono": "^4.7.0",