@jskit-ai/mobile-capacitor 0.1.182 → 0.1.183

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/mobile-capacitor",
3
- "version": "0.1.182",
3
+ "version": "0.1.183",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1"
@@ -59,7 +59,7 @@
59
59
  }
60
60
  },
61
61
  "peerDependencies": {
62
- "@jskit-ai/kernel": "0.1.245",
63
- "@jskit-ai/shell-web": "0.1.251"
62
+ "@jskit-ai/kernel": "0.1.246",
63
+ "@jskit-ai/shell-web": "0.1.252"
64
64
  }
65
65
  }
@@ -1,4 +1,5 @@
1
- import { registerMobileLaunchRouting, resolveMobileConfig } from "@jskit-ai/kernel/client";
1
+ import { resolveMobileConfig } from "@jskit-ai/kernel/client";
2
+ import { registerMobileLaunchRouting } from "./mobileLaunchRouting.js";
2
3
  import { createNoopCapacitorAppAdapter } from "./globalCapacitorAppAdapter.js";
3
4
 
4
5
  function extractPathname(value = "") {
@@ -0,0 +1,216 @@
1
+ import { resolveMobileConfig } from "@jskit-ai/kernel/client";
2
+ import { normalizePathname } from "@jskit-ai/kernel/shared/surface/paths";
3
+ import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
4
+ import { normalizeHttpOrigin } from "@jskit-ai/kernel/shared/support/returnToPath";
5
+
6
+ function buildNormalizedRoutePath(pathname = "/", search = "", hash = "") {
7
+ const normalizedPathname = normalizePathname(pathname);
8
+ const normalizedSearch = String(search || "").trim().replace(/^\?+/, "");
9
+ const normalizedHash = String(hash || "").trim();
10
+
11
+ return `${normalizedPathname}${normalizedSearch ? `?${normalizedSearch}` : ""}${normalizedHash}`;
12
+ }
13
+
14
+ function normalizeResolvedRoutePath(value = "", fallback = "") {
15
+ const normalizedValue = normalizeText(value);
16
+ if (!normalizedValue) {
17
+ return fallback;
18
+ }
19
+
20
+ try {
21
+ const parsed = new URL(normalizedValue, "https://jskit.invalid");
22
+ return buildNormalizedRoutePath(parsed.pathname, parsed.search, parsed.hash);
23
+ } catch {
24
+ return fallback;
25
+ }
26
+ }
27
+
28
+ function normalizeAllowedHttpOrigins({ mobileConfig, currentOrigin = "", allowedHttpOrigins = [] } = {}) {
29
+ const origins = new Set();
30
+
31
+ const maybeAddOrigin = (value = "") => {
32
+ const origin = normalizeHttpOrigin(value);
33
+ if (origin) {
34
+ origins.add(origin);
35
+ }
36
+ };
37
+
38
+ maybeAddOrigin(currentOrigin);
39
+
40
+ const explicitOrigins = Array.isArray(allowedHttpOrigins) ? allowedHttpOrigins : [allowedHttpOrigins];
41
+ for (const entry of explicitOrigins) {
42
+ maybeAddOrigin(entry);
43
+ }
44
+
45
+ for (const domain of mobileConfig.auth.appLinkDomains) {
46
+ maybeAddOrigin(`https://${domain}`);
47
+ }
48
+
49
+ return origins;
50
+ }
51
+
52
+ function normalizeCustomSchemeRoutePath(parsedUrl) {
53
+ const host = normalizeText(parsedUrl?.host);
54
+ const pathname = normalizePathname(parsedUrl?.pathname || "/");
55
+
56
+ if (!host) {
57
+ return pathname;
58
+ }
59
+
60
+ if (pathname === "/") {
61
+ return `/${host}`;
62
+ }
63
+
64
+ return normalizePathname(`/${host}${pathname}`);
65
+ }
66
+
67
+ // mobileConfig is the resolved config from resolveMobileConfig().
68
+ function normalizeIncomingAppUrl(url = "", mobileConfig, { currentOrigin = "", allowedHttpOrigins = [] } = {}) {
69
+ const normalizedUrl = normalizeText(url);
70
+ if (!normalizedUrl) {
71
+ return "";
72
+ }
73
+
74
+ if (normalizedUrl.startsWith("/")) {
75
+ try {
76
+ const parsed = new URL(normalizedUrl, "https://jskit.invalid");
77
+ return buildNormalizedRoutePath(parsed.pathname, parsed.search, parsed.hash);
78
+ } catch {
79
+ return "";
80
+ }
81
+ }
82
+
83
+ let parsedUrl;
84
+ try {
85
+ parsedUrl = new URL(normalizedUrl);
86
+ } catch {
87
+ return "";
88
+ }
89
+
90
+ const allowedOrigins = normalizeAllowedHttpOrigins({
91
+ mobileConfig,
92
+ currentOrigin,
93
+ allowedHttpOrigins
94
+ });
95
+ const protocol = String(parsedUrl.protocol || "").toLowerCase();
96
+ const customScheme = mobileConfig.auth.customScheme;
97
+
98
+ if (customScheme && protocol === `${customScheme}:`) {
99
+ return buildNormalizedRoutePath(
100
+ normalizeCustomSchemeRoutePath(parsedUrl),
101
+ parsedUrl.search,
102
+ parsedUrl.hash
103
+ );
104
+ }
105
+
106
+ if (allowedOrigins.has(normalizeHttpOrigin(normalizedUrl))) {
107
+ return buildNormalizedRoutePath(parsedUrl.pathname, parsedUrl.search, parsedUrl.hash);
108
+ }
109
+
110
+ return "";
111
+ }
112
+
113
+ function registerMobileLaunchRouting({
114
+ router,
115
+ mobileConfig = {},
116
+ getInitialLaunchUrl = async () => "",
117
+ subscribeToLaunchUrls = () => () => {},
118
+ resolveTargetPath = null,
119
+ currentOrigin = typeof window === "object" && window?.location ? String(window.location.origin || "") : "",
120
+ allowedHttpOrigins = [],
121
+ logger = null
122
+ } = {}) {
123
+ if (!router || typeof router.replace !== "function") {
124
+ throw new TypeError("registerMobileLaunchRouting requires router.replace().");
125
+ }
126
+
127
+ const resolvedMobileConfig = resolveMobileConfig({
128
+ mobile: mobileConfig
129
+ });
130
+ const runtimeLogger =
131
+ logger && typeof logger === "object"
132
+ ? logger
133
+ : {
134
+ info() {},
135
+ warn() {},
136
+ error() {}
137
+ };
138
+
139
+ async function applyIncomingUrl(url = "", reason = "manual") {
140
+ const normalizedTargetPath = normalizeIncomingAppUrl(url, resolvedMobileConfig, {
141
+ currentOrigin,
142
+ allowedHttpOrigins
143
+ });
144
+ if (!normalizedTargetPath) {
145
+ return "";
146
+ }
147
+
148
+ let resolvedTargetPath = normalizedTargetPath;
149
+ if (typeof resolveTargetPath === "function") {
150
+ const nextTargetPath = await resolveTargetPath(
151
+ Object.freeze({
152
+ originalUrl: String(url || ""),
153
+ normalizedTargetPath,
154
+ reason,
155
+ mobileConfig: resolvedMobileConfig,
156
+ router
157
+ })
158
+ );
159
+ resolvedTargetPath = normalizeResolvedRoutePath(nextTargetPath, normalizedTargetPath);
160
+ }
161
+
162
+ const currentFullPath = String(router.currentRoute?.value?.fullPath || "").trim();
163
+ if (currentFullPath === resolvedTargetPath) {
164
+ return resolvedTargetPath;
165
+ }
166
+
167
+ await router.replace(resolvedTargetPath);
168
+ if (typeof runtimeLogger.info === "function") {
169
+ runtimeLogger.info(
170
+ {
171
+ reason,
172
+ targetPath: resolvedTargetPath
173
+ },
174
+ "Mobile launch routing applied incoming app URL."
175
+ );
176
+ }
177
+ return resolvedTargetPath;
178
+ }
179
+
180
+ async function initialize() {
181
+ if (resolvedMobileConfig.enabled !== true) {
182
+ return "";
183
+ }
184
+
185
+ const initialLaunchUrl = await getInitialLaunchUrl();
186
+ return applyIncomingUrl(initialLaunchUrl, "initial-launch");
187
+ }
188
+
189
+ const unsubscribe =
190
+ resolvedMobileConfig.enabled === true
191
+ ? subscribeToLaunchUrls((nextUrl) => {
192
+ Promise.resolve(applyIncomingUrl(nextUrl, "launch-event")).catch((error) => {
193
+ if (typeof runtimeLogger.warn === "function") {
194
+ runtimeLogger.warn(
195
+ {
196
+ error: String(error?.message || error || "unknown error")
197
+ },
198
+ "Mobile launch routing failed to apply incoming app URL."
199
+ );
200
+ }
201
+ });
202
+ })
203
+ : () => {};
204
+
205
+ return Object.freeze({
206
+ initialize,
207
+ dispose() {
208
+ if (typeof unsubscribe === "function") {
209
+ unsubscribe();
210
+ }
211
+ },
212
+ applyIncomingUrl
213
+ });
214
+ }
215
+
216
+ export { normalizeIncomingAppUrl, registerMobileLaunchRouting };
@@ -0,0 +1,246 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { resolveMobileConfig } from "@jskit-ai/kernel/client";
4
+ import { normalizeIncomingAppUrl, registerMobileLaunchRouting } from "../src/client/runtime/mobileLaunchRouting.js";
5
+
6
+ function mobile(config) {
7
+ return resolveMobileConfig({ mobile: config });
8
+ }
9
+
10
+ test("normalizeIncomingAppUrl normalizes custom-scheme auth callback routes into router paths", () => {
11
+ const normalized = normalizeIncomingAppUrl("convict://auth/login?code=abc&oauthProvider=google", mobile({
12
+ enabled: true,
13
+ auth: {
14
+ customScheme: "convict"
15
+ }
16
+ }));
17
+
18
+ assert.equal(normalized, "/auth/login?code=abc&oauthProvider=google");
19
+ });
20
+
21
+ test("normalizeIncomingAppUrl normalizes custom-scheme workspace routes", () => {
22
+ const normalized = normalizeIncomingAppUrl("convict://w/acme/workouts/2026-05-07?tab=today", mobile({
23
+ enabled: true,
24
+ auth: {
25
+ customScheme: "convict"
26
+ }
27
+ }));
28
+
29
+ assert.equal(normalized, "/w/acme/workouts/2026-05-07?tab=today");
30
+ });
31
+
32
+ test("normalizeIncomingAppUrl normalizes allowed HTTPS app links", () => {
33
+ const normalized = normalizeIncomingAppUrl("https://app.example.com/auth/login?code=abc", mobile({
34
+ enabled: true,
35
+ auth: {
36
+ appLinkDomains: ["app.example.com"]
37
+ }
38
+ }));
39
+
40
+ assert.equal(normalized, "/auth/login?code=abc");
41
+ });
42
+
43
+ test("normalizeIncomingAppUrl accepts same-origin HTTP URLs when explicitly allowed", () => {
44
+ const normalized = normalizeIncomingAppUrl(
45
+ "http://192.168.1.10:5173/w/acme",
46
+ mobile({
47
+ enabled: true
48
+ }),
49
+ {
50
+ allowedHttpOrigins: ["http://192.168.1.10:5173"]
51
+ }
52
+ );
53
+
54
+ assert.equal(normalized, "/w/acme");
55
+ });
56
+
57
+ test("normalizeIncomingAppUrl rejects unowned schemes and domains", () => {
58
+ assert.equal(
59
+ normalizeIncomingAppUrl("otherapp://auth/login?code=abc", mobile({
60
+ enabled: true,
61
+ auth: {
62
+ customScheme: "convict"
63
+ }
64
+ })),
65
+ ""
66
+ );
67
+
68
+ assert.equal(
69
+ normalizeIncomingAppUrl("https://evil.example.com/auth/login?code=abc", mobile({
70
+ enabled: true,
71
+ auth: {
72
+ appLinkDomains: ["app.example.com"]
73
+ }
74
+ })),
75
+ ""
76
+ );
77
+ });
78
+
79
+ test("registerMobileLaunchRouting initializes and applies the initial launch URL", async () => {
80
+ const replaceCalls = [];
81
+ const runtime = registerMobileLaunchRouting({
82
+ router: {
83
+ currentRoute: {
84
+ value: {
85
+ fullPath: "/home"
86
+ }
87
+ },
88
+ async replace(target) {
89
+ replaceCalls.push(target);
90
+ }
91
+ },
92
+ mobileConfig: {
93
+ enabled: true,
94
+ auth: {
95
+ customScheme: "convict"
96
+ }
97
+ },
98
+ getInitialLaunchUrl: async () => "convict://auth/login?code=abc"
99
+ });
100
+
101
+ const targetPath = await runtime.initialize();
102
+
103
+ assert.equal(targetPath, "/auth/login?code=abc");
104
+ assert.deepEqual(replaceCalls, ["/auth/login?code=abc"]);
105
+ });
106
+
107
+ test("registerMobileLaunchRouting subscribes to later launch URLs and routes them", async () => {
108
+ const replaceCalls = [];
109
+ let listener = null;
110
+ const runtime = registerMobileLaunchRouting({
111
+ router: {
112
+ currentRoute: {
113
+ value: {
114
+ fullPath: "/home"
115
+ }
116
+ },
117
+ async replace(target) {
118
+ replaceCalls.push(target);
119
+ }
120
+ },
121
+ mobileConfig: {
122
+ enabled: true,
123
+ auth: {
124
+ customScheme: "convict"
125
+ }
126
+ },
127
+ subscribeToLaunchUrls(handler) {
128
+ listener = handler;
129
+ return () => {
130
+ listener = null;
131
+ };
132
+ }
133
+ });
134
+
135
+ assert.equal(typeof listener, "function");
136
+ listener("convict://w/acme");
137
+ await Promise.resolve();
138
+ await Promise.resolve();
139
+ assert.deepEqual(replaceCalls, ["/w/acme"]);
140
+
141
+ runtime.dispose();
142
+ assert.equal(listener, null);
143
+ });
144
+
145
+ test("registerMobileLaunchRouting lets a resolver override the final route target", async () => {
146
+ const replaceCalls = [];
147
+ const runtime = registerMobileLaunchRouting({
148
+ router: {
149
+ currentRoute: {
150
+ value: {
151
+ fullPath: "/home"
152
+ }
153
+ },
154
+ async replace(target) {
155
+ replaceCalls.push(target);
156
+ }
157
+ },
158
+ mobileConfig: {
159
+ enabled: true,
160
+ auth: {
161
+ customScheme: "convict"
162
+ }
163
+ },
164
+ getInitialLaunchUrl: async () => "convict://auth/login?code=abc",
165
+ resolveTargetPath({ originalUrl, normalizedTargetPath, reason }) {
166
+ assert.equal(originalUrl, "convict://auth/login?code=abc");
167
+ assert.equal(normalizedTargetPath, "/auth/login?code=abc");
168
+ assert.equal(reason, "initial-launch");
169
+ return "/w/acme";
170
+ }
171
+ });
172
+
173
+ const targetPath = await runtime.initialize();
174
+
175
+ assert.equal(targetPath, "/w/acme");
176
+ assert.deepEqual(replaceCalls, ["/w/acme"]);
177
+ });
178
+
179
+ test("registerMobileLaunchRouting forwards unknown deep-link paths to the normal router", async () => {
180
+ const replaceCalls = [];
181
+ const runtime = registerMobileLaunchRouting({
182
+ router: {
183
+ currentRoute: {
184
+ value: {
185
+ fullPath: "/home"
186
+ }
187
+ },
188
+ async replace(target) {
189
+ replaceCalls.push(target);
190
+ }
191
+ },
192
+ mobileConfig: {
193
+ enabled: true,
194
+ auth: {
195
+ customScheme: "convict"
196
+ }
197
+ },
198
+ getInitialLaunchUrl: async () => "convict://w/acme/does-not-exist"
199
+ });
200
+
201
+ const targetPath = await runtime.initialize();
202
+
203
+ assert.equal(targetPath, "/w/acme/does-not-exist");
204
+ assert.deepEqual(replaceCalls, ["/w/acme/does-not-exist"]);
205
+ });
206
+
207
+ test("registerMobileLaunchRouting no-ops when mobile config is disabled", async () => {
208
+ const replaceCalls = [];
209
+ const runtime = registerMobileLaunchRouting({
210
+ router: {
211
+ currentRoute: {
212
+ value: {
213
+ fullPath: "/home"
214
+ }
215
+ },
216
+ async replace(target) {
217
+ replaceCalls.push(target);
218
+ }
219
+ },
220
+ mobileConfig: {
221
+ enabled: false,
222
+ auth: {
223
+ customScheme: "convict"
224
+ }
225
+ },
226
+ getInitialLaunchUrl: async () => "convict://auth/login?code=abc",
227
+ subscribeToLaunchUrls() {
228
+ throw new Error("subscribeToLaunchUrls should not be called when mobile is disabled");
229
+ }
230
+ });
231
+
232
+ const targetPath = await runtime.initialize();
233
+ assert.equal(targetPath, "");
234
+ assert.deepEqual(replaceCalls, []);
235
+ });
236
+
237
+ test("normalizeIncomingAppUrl compares allowed origins by their normalised HTTP origin", () => {
238
+ const config = mobile({ enabled: true, auth: { appLinkDomains: ["App.Example.com"] } });
239
+
240
+ assert.equal(normalizeIncomingAppUrl("HTTPS://APP.example.com:443/w/acme?x=1", config), "/w/acme?x=1");
241
+ assert.equal(
242
+ normalizeIncomingAppUrl("http://Localhost:5173/w/acme", config, { allowedHttpOrigins: ["http://localhost:5173/ignored"] }),
243
+ "/w/acme"
244
+ );
245
+ assert.equal(normalizeIncomingAppUrl("ftp://app.example.com/w/acme", config), "");
246
+ });