@jskit-ai/mobile-capacitor 0.2.4 → 0.2.5
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 +3 -3
- package/src/client/browser.js +21 -0
- package/src/client/providers/MobileCapacitorClientProvider.js +38 -12
- package/src/client/runtime/apiRequestClient.js +11 -9
- package/src/client/runtime/globalCapacitorAppAdapter.js +113 -99
- package/src/client/runtime/mobileCapacitorRuntime.js +67 -86
- package/src/client/runtime/mobileLaunchRouting.js +54 -55
- package/src/client/runtime/oauthLaunchClient.js +27 -24
- package/src/client/types.js +76 -0
- package/test/mobileLaunchRouting.test.js +12 -0
- package/test/provider.test.js +34 -0
- package/test/runtime.test.js +124 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/mobile-capacitor",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
|
-
"@jskit-ai/kernel": "0.2.
|
|
64
|
-
"@jskit-ai/shell-web": "0.2.
|
|
63
|
+
"@jskit-ai/kernel": "0.2.5",
|
|
64
|
+
"@jskit-ai/shell-web": "0.2.5"
|
|
65
65
|
}
|
|
66
66
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createTextError } from "./i18n.js";
|
|
2
|
+
|
|
3
|
+
// What this package reads from the browser, written once. Outside a browser, as in a test, there is no window.
|
|
4
|
+
|
|
5
|
+
/** The page's location outside a browser: it has no origin and refuses to navigate. */
|
|
6
|
+
const NO_PAGE_LOCATION = Object.freeze({
|
|
7
|
+
origin: "",
|
|
8
|
+
assign() {
|
|
9
|
+
throw createTextError("jskit.mobile.oauth.browserUnavailable");
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The page's location, or NO_PAGE_LOCATION outside a browser.
|
|
15
|
+
* @returns {import("./types.js").PageLocation}
|
|
16
|
+
*/
|
|
17
|
+
function pageLocation() {
|
|
18
|
+
return typeof window === "object" && window.location ? window.location : NO_PAGE_LOCATION;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { pageLocation };
|
|
@@ -7,14 +7,21 @@ import { createCapacitorAwareOAuthLaunchClient } from "../runtime/oauthLaunchCli
|
|
|
7
7
|
|
|
8
8
|
const GLOBAL_FETCH_RESTORE_KEY = Symbol.for("jskit.mobile.capacitor.restoreFetch");
|
|
9
9
|
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
function keepPageFetch() {}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Inside a Capacitor shell, sends the page's relative API requests to the application's server.
|
|
14
|
+
* @param {object} options
|
|
15
|
+
* @param {import("../types.js").CapacitorAppAdapter} options.adapter
|
|
16
|
+
* @param {string} [options.apiBaseUrl]
|
|
17
|
+
* @param {typeof globalThis} [options.globalObject]
|
|
18
|
+
* @returns {() => void} Puts the page's own fetch back.
|
|
19
|
+
*/
|
|
20
|
+
function installCapacitorAwareGlobalFetch({ adapter, apiBaseUrl = "", globalObject = globalThis }) {
|
|
21
|
+
if (adapter.available !== true) {
|
|
22
|
+
return keepPageFetch;
|
|
16
23
|
}
|
|
17
|
-
if (
|
|
24
|
+
if (globalObject[GLOBAL_FETCH_RESTORE_KEY]) {
|
|
18
25
|
return globalObject[GLOBAL_FETCH_RESTORE_KEY];
|
|
19
26
|
}
|
|
20
27
|
|
|
@@ -62,8 +69,12 @@ const MobileCapacitorClientProvider = defineProvider({
|
|
|
62
69
|
})
|
|
63
70
|
};
|
|
64
71
|
},
|
|
72
|
+
/**
|
|
73
|
+
* @param {unknown} _dependencies
|
|
74
|
+
* @param {{ outputs: { mobile: import("../types.js").MobileCapability } }} lifecycle
|
|
75
|
+
*/
|
|
65
76
|
shutdown(_dependencies, { outputs }) {
|
|
66
|
-
outputs.mobile.restoreFetch
|
|
77
|
+
outputs.mobile.restoreFetch();
|
|
67
78
|
}
|
|
68
79
|
});
|
|
69
80
|
|
|
@@ -80,23 +91,38 @@ const MobileCapacitorRuntimeProvider = defineProvider({
|
|
|
80
91
|
provides: {
|
|
81
92
|
mobileRuntime: "client.mobile-runtime"
|
|
82
93
|
},
|
|
94
|
+
/**
|
|
95
|
+
* @param {object} capabilities
|
|
96
|
+
* @param {import("../types.js").MobileCapability} capabilities.mobile
|
|
97
|
+
* @param {import("../types.js").ClientRouter} capabilities.router
|
|
98
|
+
* @param {{ mobileCallback: import("../types.js").MobileAuthCallbackCompleter, guard: import("../types.js").MobileAuthGuardRuntime }} [capabilities.auth]
|
|
99
|
+
* @param {{ placement: import("../types.js").MobilePlacementRuntime }} [capabilities.shell]
|
|
100
|
+
*/
|
|
83
101
|
setup({ auth, mobile, router, shell }) {
|
|
84
102
|
return {
|
|
85
103
|
mobileRuntime: createMobileCapacitorRuntime({
|
|
86
104
|
router,
|
|
87
105
|
mobileConfig: getClientAppConfig().mobile || {},
|
|
88
106
|
adapter: mobile.adapter,
|
|
89
|
-
placementRuntime: shell?.placement
|
|
90
|
-
authCallbackCompleter: auth?.mobileCallback
|
|
91
|
-
authGuardRuntime: auth?.guard
|
|
107
|
+
placementRuntime: shell?.placement,
|
|
108
|
+
authCallbackCompleter: auth?.mobileCallback,
|
|
109
|
+
authGuardRuntime: auth?.guard
|
|
92
110
|
})
|
|
93
111
|
};
|
|
94
112
|
},
|
|
113
|
+
/**
|
|
114
|
+
* @param {unknown} _dependencies
|
|
115
|
+
* @param {{ outputs: { mobileRuntime: import("../types.js").MobileCapacitorRuntime } }} lifecycle
|
|
116
|
+
*/
|
|
95
117
|
async boot(_dependencies, { outputs }) {
|
|
96
118
|
await outputs.mobileRuntime.initialize();
|
|
97
119
|
},
|
|
120
|
+
/**
|
|
121
|
+
* @param {unknown} _dependencies
|
|
122
|
+
* @param {{ outputs: { mobileRuntime: import("../types.js").MobileCapacitorRuntime } }} lifecycle
|
|
123
|
+
*/
|
|
98
124
|
shutdown(_dependencies, { outputs }) {
|
|
99
|
-
outputs.mobileRuntime.dispose
|
|
125
|
+
outputs.mobileRuntime.dispose();
|
|
100
126
|
}
|
|
101
127
|
});
|
|
102
128
|
|
|
@@ -77,7 +77,7 @@ function normalizeFetchInput(input = "") {
|
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
if (
|
|
80
|
+
if (input instanceof URL) {
|
|
81
81
|
return Object.freeze({
|
|
82
82
|
rawUrl: input.toString(),
|
|
83
83
|
rebuild(nextUrl) {
|
|
@@ -86,7 +86,7 @@ function normalizeFetchInput(input = "") {
|
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
if (
|
|
89
|
+
if (input instanceof Request) {
|
|
90
90
|
return Object.freeze({
|
|
91
91
|
rawUrl: String(input.url || ""),
|
|
92
92
|
rebuild(nextUrl) {
|
|
@@ -103,17 +103,19 @@ function normalizeFetchInput(input = "") {
|
|
|
103
103
|
});
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
/**
|
|
107
|
+
* @param {object} options
|
|
108
|
+
* @param {typeof fetch} [options.fetchImpl]
|
|
109
|
+
* @param {import("../types.js").CapacitorAppAdapter} options.adapter
|
|
110
|
+
* @param {string} [options.apiBaseUrl]
|
|
111
|
+
*/
|
|
106
112
|
function createCapacitorAwareFetch({
|
|
107
113
|
fetchImpl = globalThis.fetch,
|
|
108
|
-
adapter
|
|
114
|
+
adapter,
|
|
109
115
|
apiBaseUrl = ""
|
|
110
|
-
}
|
|
111
|
-
if (typeof fetchImpl !== "function") {
|
|
112
|
-
throw new Error("createCapacitorAwareFetch requires fetchImpl.");
|
|
113
|
-
}
|
|
114
|
-
|
|
116
|
+
}) {
|
|
115
117
|
return async function capacitorAwareFetch(input, init) {
|
|
116
|
-
if (adapter
|
|
118
|
+
if (adapter.available !== true) {
|
|
117
119
|
return fetchImpl(input, init);
|
|
118
120
|
}
|
|
119
121
|
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Capacitor App plugin on the page, or null when there is none.
|
|
3
|
+
* @param {{ Capacitor?: { Plugins?: { App?: unknown }, App?: unknown } }} [globalObject]
|
|
4
|
+
* @returns {import("../types.js").CapacitorAppPlugin | null}
|
|
5
|
+
*/
|
|
1
6
|
function resolveCapacitorAppPlugin(globalObject = globalThis) {
|
|
2
|
-
|
|
3
|
-
return null;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
const pluginFromPlugins = globalObject?.Capacitor?.Plugins?.App;
|
|
7
|
+
const pluginFromPlugins = globalObject.Capacitor?.Plugins?.App;
|
|
7
8
|
if (pluginFromPlugins && typeof pluginFromPlugins === "object") {
|
|
8
9
|
return pluginFromPlugins;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
const directPlugin = globalObject
|
|
12
|
+
const directPlugin = globalObject.Capacitor?.App;
|
|
12
13
|
if (directPlugin && typeof directPlugin === "object") {
|
|
13
14
|
return directPlugin;
|
|
14
15
|
}
|
|
@@ -16,116 +17,129 @@ function resolveCapacitorAppPlugin(globalObject = globalThis) {
|
|
|
16
17
|
return null;
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Whether the App plugin has the named function.
|
|
22
|
+
* @param {import("../types.js").CapacitorAppPlugin} plugin
|
|
23
|
+
* @param {keyof import("../types.js").CapacitorAppPlugin} name
|
|
24
|
+
*/
|
|
25
|
+
function pluginHasFunction(plugin, name) {
|
|
26
|
+
// readable-check: allow method-probe - platform object: the application's shell supplies the App plugin; this package does not depend on @capacitor/app
|
|
27
|
+
return typeof plugin[name] === "function";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function readNoLaunchUrl() {
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function removeNoListener() {}
|
|
35
|
+
|
|
36
|
+
function subscribeToNothing() {
|
|
37
|
+
return removeNoListener;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function exitNothing() {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @returns {import("../types.js").CapacitorAppAdapter} */
|
|
19
45
|
function createNoopCapacitorAppAdapter() {
|
|
20
46
|
return Object.freeze({
|
|
21
47
|
available: false,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return () => {};
|
|
27
|
-
},
|
|
28
|
-
subscribeToBackButton() {
|
|
29
|
-
return () => {};
|
|
30
|
-
},
|
|
31
|
-
async exitApp() {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
48
|
+
getInitialLaunchUrl: readNoLaunchUrl,
|
|
49
|
+
subscribeToLaunchUrls: subscribeToNothing,
|
|
50
|
+
subscribeToBackButton: subscribeToNothing,
|
|
51
|
+
exitApp: exitNothing
|
|
34
52
|
});
|
|
35
53
|
}
|
|
36
54
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
55
|
+
/**
|
|
56
|
+
* @param {object} [options]
|
|
57
|
+
* @param {object} [options.globalObject]
|
|
58
|
+
* @param {import("../types.js").CapacitorAppPlugin | null} [options.appPlugin]
|
|
59
|
+
* @returns {import("../types.js").CapacitorAppAdapter}
|
|
60
|
+
*/
|
|
61
|
+
function createGlobalCapacitorAppAdapter({ globalObject = globalThis, appPlugin = resolveCapacitorAppPlugin(globalObject) } = {}) {
|
|
62
|
+
if (appPlugin === null) {
|
|
40
63
|
return createNoopCapacitorAppAdapter();
|
|
41
64
|
}
|
|
42
65
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (typeof plugin.getLaunchUrl !== "function") {
|
|
47
|
-
return "";
|
|
48
|
-
}
|
|
66
|
+
const canReadLaunchUrl = pluginHasFunction(appPlugin, "getLaunchUrl");
|
|
67
|
+
const canListen = pluginHasFunction(appPlugin, "addListener");
|
|
68
|
+
const canExit = pluginHasFunction(appPlugin, "exitApp");
|
|
49
69
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
70
|
+
async function getInitialLaunchUrl() {
|
|
71
|
+
if (!canReadLaunchUrl) {
|
|
72
|
+
return "";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const payload = await appPlugin.getLaunchUrl();
|
|
76
|
+
return String(payload?.url || "").trim();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// The plugin hands its listener back later, so a subscription disposed before then removes it when it arrives.
|
|
80
|
+
function subscribeToPluginEvent(eventName, onEvent) {
|
|
81
|
+
if (!canListen) {
|
|
82
|
+
return removeNoListener;
|
|
83
|
+
}
|
|
57
84
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
handler(String(event?.url || "").trim());
|
|
68
|
-
})
|
|
69
|
-
)
|
|
70
|
-
.then((listenerHandle) => {
|
|
71
|
-
removeListener = listenerHandle;
|
|
72
|
-
if (disposed && typeof removeListener?.remove === "function") {
|
|
73
|
-
return removeListener.remove();
|
|
74
|
-
}
|
|
75
|
-
return null;
|
|
76
|
-
})
|
|
77
|
-
.catch(() => {});
|
|
78
|
-
|
|
79
|
-
return () => {
|
|
80
|
-
disposed = true;
|
|
81
|
-
if (typeof removeListener?.remove === "function") {
|
|
82
|
-
void removeListener.remove();
|
|
85
|
+
let disposed = false;
|
|
86
|
+
let listenerHandle = null;
|
|
87
|
+
|
|
88
|
+
Promise.resolve(
|
|
89
|
+
appPlugin.addListener(eventName, (event) => {
|
|
90
|
+
if (disposed) {
|
|
91
|
+
return;
|
|
83
92
|
}
|
|
84
|
-
};
|
|
85
|
-
},
|
|
86
|
-
subscribeToBackButton(handler = () => {}) {
|
|
87
|
-
if (typeof plugin.addListener !== "function") {
|
|
88
|
-
return () => {};
|
|
89
|
-
}
|
|
90
93
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
handler({
|
|
101
|
-
canGoBack: event?.canGoBack === true
|
|
102
|
-
});
|
|
103
|
-
})
|
|
104
|
-
)
|
|
105
|
-
.then((listenerHandle) => {
|
|
106
|
-
removeListener = listenerHandle;
|
|
107
|
-
if (disposed && typeof removeListener?.remove === "function") {
|
|
108
|
-
return removeListener.remove();
|
|
109
|
-
}
|
|
110
|
-
return null;
|
|
111
|
-
})
|
|
112
|
-
.catch(() => {});
|
|
113
|
-
|
|
114
|
-
return () => {
|
|
115
|
-
disposed = true;
|
|
116
|
-
if (typeof removeListener?.remove === "function") {
|
|
117
|
-
void removeListener.remove();
|
|
94
|
+
onEvent(event);
|
|
95
|
+
})
|
|
96
|
+
)
|
|
97
|
+
.then((handle) => {
|
|
98
|
+
listenerHandle = handle;
|
|
99
|
+
if (disposed) {
|
|
100
|
+
return listenerHandle.remove();
|
|
118
101
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
102
|
+
return null;
|
|
103
|
+
})
|
|
104
|
+
.catch(() => {});
|
|
105
|
+
|
|
106
|
+
return () => {
|
|
107
|
+
disposed = true;
|
|
108
|
+
if (listenerHandle) {
|
|
109
|
+
void listenerHandle.remove();
|
|
124
110
|
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
125
113
|
|
|
126
|
-
|
|
127
|
-
|
|
114
|
+
function subscribeToLaunchUrls(handler) {
|
|
115
|
+
return subscribeToPluginEvent("appUrlOpen", (event) => {
|
|
116
|
+
handler(String(event?.url || "").trim());
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function subscribeToBackButton(handler) {
|
|
121
|
+
return subscribeToPluginEvent("backButton", (event) => {
|
|
122
|
+
handler({
|
|
123
|
+
canGoBack: event?.canGoBack === true
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function exitApp() {
|
|
129
|
+
if (!canExit) {
|
|
130
|
+
return false;
|
|
128
131
|
}
|
|
132
|
+
|
|
133
|
+
await appPlugin.exitApp();
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return Object.freeze({
|
|
138
|
+
available: canReadLaunchUrl || canListen,
|
|
139
|
+
getInitialLaunchUrl,
|
|
140
|
+
subscribeToLaunchUrls,
|
|
141
|
+
subscribeToBackButton,
|
|
142
|
+
exitApp
|
|
129
143
|
});
|
|
130
144
|
}
|
|
131
145
|
|
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
import { resolveMobileConfig } from "@jskit-ai/kernel/client";
|
|
2
2
|
import { registerMobileLaunchRouting } from "./mobileLaunchRouting.js";
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
/** The placement runtime of an application without a shell: there is no placement context. */
|
|
5
|
+
const NO_PLACEMENT_RUNTIME = Object.freeze({
|
|
6
|
+
getContext() {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
/** The callback completer of an application without auth: nothing completes, so the link routes as it came. */
|
|
12
|
+
const NO_AUTH_CALLBACK_COMPLETER = Object.freeze({
|
|
13
|
+
async completeFromUrl() {
|
|
14
|
+
return {
|
|
15
|
+
completed: false
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/** The guard of an application without auth: nothing to wait for, refresh or read. */
|
|
21
|
+
const NO_AUTH_GUARD_RUNTIME = Object.freeze({
|
|
22
|
+
async initialize() {
|
|
23
|
+
return null;
|
|
24
|
+
},
|
|
25
|
+
async refresh() {
|
|
26
|
+
return null;
|
|
27
|
+
},
|
|
28
|
+
getState() {
|
|
29
|
+
return {
|
|
30
|
+
oauthDefaultProvider: ""
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
});
|
|
4
34
|
|
|
5
35
|
function extractPathname(value = "") {
|
|
6
36
|
const normalizedValue = String(value || "").trim();
|
|
@@ -16,35 +46,8 @@ function extractPathname(value = "") {
|
|
|
16
46
|
}
|
|
17
47
|
}
|
|
18
48
|
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
return value;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (typeof value === "function") {
|
|
25
|
-
return Object.freeze({
|
|
26
|
-
completeFromUrl: value
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function normalizeAuthGuardRuntime(value = null) {
|
|
34
|
-
if (
|
|
35
|
-
value &&
|
|
36
|
-
typeof value === "object" &&
|
|
37
|
-
typeof value.getState === "function" &&
|
|
38
|
-
(typeof value.initialize === "function" || typeof value.refresh === "function")
|
|
39
|
-
) {
|
|
40
|
-
return value;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function isAuthCallbackTargetPath(targetPath = "", mobileConfig = {}) {
|
|
47
|
-
const callbackPath = String(mobileConfig?.auth?.callbackPath || "").trim();
|
|
49
|
+
function isAuthCallbackTargetPath(targetPath, mobileConfig) {
|
|
50
|
+
const callbackPath = String(mobileConfig.auth.callbackPath || "").trim();
|
|
48
51
|
if (!callbackPath) {
|
|
49
52
|
return false;
|
|
50
53
|
}
|
|
@@ -52,25 +55,29 @@ function isAuthCallbackTargetPath(targetPath = "", mobileConfig = {}) {
|
|
|
52
55
|
return extractPathname(targetPath) === callbackPath;
|
|
53
56
|
}
|
|
54
57
|
|
|
58
|
+
/**
|
|
59
|
+
* @param {object} options
|
|
60
|
+
* @param {import("../types.js").ClientRouter} options.router
|
|
61
|
+
* @param {object} options.mobileConfig The application's `config.mobile`.
|
|
62
|
+
* @param {import("../types.js").CapacitorAppAdapter} options.adapter
|
|
63
|
+
* @param {import("../types.js").MobilePlacementRuntime} [options.placementRuntime] Absent without a shell.
|
|
64
|
+
* @param {import("../types.js").MobileAuthCallbackCompleter} [options.authCallbackCompleter] Absent without auth.
|
|
65
|
+
* @param {import("../types.js").MobileAuthGuardRuntime} [options.authGuardRuntime] Absent without auth.
|
|
66
|
+
* @param {import("../types.js").MobileLaunchLogger} [options.logger]
|
|
67
|
+
*/
|
|
55
68
|
function createMobileCapacitorRuntime({
|
|
56
69
|
router,
|
|
57
|
-
mobileConfig
|
|
58
|
-
adapter
|
|
59
|
-
placementRuntime =
|
|
60
|
-
authCallbackCompleter =
|
|
61
|
-
authGuardRuntime =
|
|
62
|
-
logger
|
|
63
|
-
}
|
|
64
|
-
if (!router || typeof router.replace !== "function") {
|
|
65
|
-
throw new TypeError("createMobileCapacitorRuntime requires router.replace().");
|
|
66
|
-
}
|
|
67
|
-
|
|
70
|
+
mobileConfig,
|
|
71
|
+
adapter,
|
|
72
|
+
placementRuntime = NO_PLACEMENT_RUNTIME,
|
|
73
|
+
authCallbackCompleter = NO_AUTH_CALLBACK_COMPLETER,
|
|
74
|
+
authGuardRuntime = NO_AUTH_GUARD_RUNTIME,
|
|
75
|
+
logger
|
|
76
|
+
}) {
|
|
68
77
|
const resolvedMobileConfig = resolveMobileConfig({
|
|
69
78
|
mobile: mobileConfig
|
|
70
79
|
});
|
|
71
|
-
|
|
72
|
-
const resolvedAuthCallbackCompleter = normalizeCallbackCompleter(authCallbackCompleter);
|
|
73
|
-
const resolvedAuthGuardRuntime = normalizeAuthGuardRuntime(authGuardRuntime);
|
|
80
|
+
/** @type {import("../types.js").MobileLaunchRouting | null} */
|
|
74
81
|
let launchRouting = null;
|
|
75
82
|
let initialized = false;
|
|
76
83
|
let lastAppliedPath = "";
|
|
@@ -78,20 +85,11 @@ function createMobileCapacitorRuntime({
|
|
|
78
85
|
let removeBackButtonListener = null;
|
|
79
86
|
|
|
80
87
|
async function ensureAuthGuardReady() {
|
|
81
|
-
if (!resolvedAuthGuardRuntime) {
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
88
|
if (authGuardReadyPromise) {
|
|
86
89
|
return authGuardReadyPromise;
|
|
87
90
|
}
|
|
88
91
|
|
|
89
|
-
authGuardReadyPromise = (
|
|
90
|
-
if (typeof resolvedAuthGuardRuntime.initialize === "function") {
|
|
91
|
-
return resolvedAuthGuardRuntime.initialize();
|
|
92
|
-
}
|
|
93
|
-
return resolvedAuthGuardRuntime.refresh();
|
|
94
|
-
})();
|
|
92
|
+
authGuardReadyPromise = authGuardRuntime.initialize();
|
|
95
93
|
|
|
96
94
|
try {
|
|
97
95
|
return await authGuardReadyPromise;
|
|
@@ -102,28 +100,20 @@ function createMobileCapacitorRuntime({
|
|
|
102
100
|
}
|
|
103
101
|
|
|
104
102
|
async function resolveTargetPath({ originalUrl = "", normalizedTargetPath = "" } = {}) {
|
|
105
|
-
if (!
|
|
103
|
+
if (!isAuthCallbackTargetPath(normalizedTargetPath, resolvedMobileConfig)) {
|
|
106
104
|
return normalizedTargetPath;
|
|
107
105
|
}
|
|
108
106
|
|
|
109
|
-
const fallbackReturnTo = String(router.currentRoute
|
|
110
|
-
const authResult = await
|
|
107
|
+
const fallbackReturnTo = String(router.currentRoute.value.fullPath || "/").trim() || "/";
|
|
108
|
+
const authResult = await authCallbackCompleter.completeFromUrl({
|
|
111
109
|
url: originalUrl,
|
|
112
110
|
fallbackReturnTo,
|
|
113
|
-
placementContext: placementRuntime
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
defaultProvider:
|
|
117
|
-
resolvedAuthGuardRuntime && typeof resolvedAuthGuardRuntime.getState === "function"
|
|
118
|
-
? String(resolvedAuthGuardRuntime.getState()?.oauthDefaultProvider || "")
|
|
119
|
-
: "",
|
|
120
|
-
refreshSession:
|
|
121
|
-
resolvedAuthGuardRuntime && typeof resolvedAuthGuardRuntime.refresh === "function"
|
|
122
|
-
? () => resolvedAuthGuardRuntime.refresh()
|
|
123
|
-
: async () => null
|
|
111
|
+
placementContext: placementRuntime.getContext(),
|
|
112
|
+
defaultProvider: String(authGuardRuntime.getState().oauthDefaultProvider || ""),
|
|
113
|
+
refreshSession: () => authGuardRuntime.refresh()
|
|
124
114
|
});
|
|
125
115
|
|
|
126
|
-
if (authResult
|
|
116
|
+
if (authResult.completed === true) {
|
|
127
117
|
return authResult.returnTo || fallbackReturnTo;
|
|
128
118
|
}
|
|
129
119
|
|
|
@@ -134,30 +124,21 @@ function createMobileCapacitorRuntime({
|
|
|
134
124
|
return registerMobileLaunchRouting({
|
|
135
125
|
router,
|
|
136
126
|
mobileConfig: resolvedMobileConfig,
|
|
137
|
-
getInitialLaunchUrl: () =>
|
|
138
|
-
subscribeToLaunchUrls: (handler) =>
|
|
127
|
+
getInitialLaunchUrl: () => adapter.getInitialLaunchUrl(),
|
|
128
|
+
subscribeToLaunchUrls: (handler) => adapter.subscribeToLaunchUrls(handler),
|
|
139
129
|
resolveTargetPath,
|
|
140
130
|
logger
|
|
141
131
|
});
|
|
142
132
|
}
|
|
143
133
|
|
|
144
134
|
function wireBackButtonHandling() {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
if (!router || typeof router.back !== "function") {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
removeBackButtonListener = resolvedAdapter.subscribeToBackButton(async (event = {}) => {
|
|
153
|
-
if (event?.canGoBack === true) {
|
|
135
|
+
removeBackButtonListener = adapter.subscribeToBackButton(async (event) => {
|
|
136
|
+
if (event.canGoBack === true) {
|
|
154
137
|
router.back();
|
|
155
138
|
return;
|
|
156
139
|
}
|
|
157
140
|
|
|
158
|
-
|
|
159
|
-
await resolvedAdapter.exitApp();
|
|
160
|
-
}
|
|
141
|
+
await adapter.exitApp();
|
|
161
142
|
});
|
|
162
143
|
}
|
|
163
144
|
|
|
@@ -185,10 +166,10 @@ function createMobileCapacitorRuntime({
|
|
|
185
166
|
}
|
|
186
167
|
|
|
187
168
|
function dispose() {
|
|
188
|
-
if (launchRouting
|
|
169
|
+
if (launchRouting) {
|
|
189
170
|
launchRouting.dispose();
|
|
190
171
|
}
|
|
191
|
-
if (
|
|
172
|
+
if (removeBackButtonListener) {
|
|
192
173
|
removeBackButtonListener();
|
|
193
174
|
}
|
|
194
175
|
launchRouting = null;
|
|
@@ -199,7 +180,7 @@ function createMobileCapacitorRuntime({
|
|
|
199
180
|
function getState() {
|
|
200
181
|
return Object.freeze({
|
|
201
182
|
initialized,
|
|
202
|
-
available:
|
|
183
|
+
available: adapter.available === true,
|
|
203
184
|
enabled: resolvedMobileConfig.enabled === true,
|
|
204
185
|
lastAppliedPath
|
|
205
186
|
});
|
|
@@ -2,6 +2,13 @@ import { resolveMobileConfig } from "@jskit-ai/kernel/client";
|
|
|
2
2
|
import { normalizePathname } from "@jskit-ai/kernel/shared/surface/paths";
|
|
3
3
|
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
4
4
|
import { normalizeHttpOrigin } from "@jskit-ai/kernel/shared/support/returnToPath";
|
|
5
|
+
import { pageLocation } from "../browser.js";
|
|
6
|
+
|
|
7
|
+
/** The logger launch routing reports to when the application gives it none. */
|
|
8
|
+
const SILENT_LAUNCH_LOGGER = Object.freeze({
|
|
9
|
+
info() {},
|
|
10
|
+
warn() {}
|
|
11
|
+
});
|
|
5
12
|
|
|
6
13
|
function buildNormalizedRoutePath(pathname = "/", search = "", hash = "") {
|
|
7
14
|
const normalizedPathname = normalizePathname(pathname);
|
|
@@ -110,31 +117,30 @@ function normalizeIncomingAppUrl(url = "", mobileConfig, { currentOrigin = "", a
|
|
|
110
117
|
return "";
|
|
111
118
|
}
|
|
112
119
|
|
|
120
|
+
/**
|
|
121
|
+
* @param {object} options
|
|
122
|
+
* @param {import("../types.js").ClientRouter} options.router
|
|
123
|
+
* @param {object} options.mobileConfig
|
|
124
|
+
* @param {() => Promise<string>} options.getInitialLaunchUrl
|
|
125
|
+
* @param {import("../types.js").CapacitorAppAdapter["subscribeToLaunchUrls"]} options.subscribeToLaunchUrls
|
|
126
|
+
* @param {(target: { originalUrl: string, normalizedTargetPath: string, reason: string, mobileConfig: object, router: import("../types.js").ClientRouter }) => Promise<string> | string} options.resolveTargetPath
|
|
127
|
+
* @param {string} [options.currentOrigin]
|
|
128
|
+
* @param {string | string[]} [options.allowedHttpOrigins]
|
|
129
|
+
* @param {import("../types.js").MobileLaunchLogger} [options.logger]
|
|
130
|
+
*/
|
|
113
131
|
function registerMobileLaunchRouting({
|
|
114
132
|
router,
|
|
115
|
-
mobileConfig
|
|
116
|
-
getInitialLaunchUrl
|
|
117
|
-
subscribeToLaunchUrls
|
|
118
|
-
resolveTargetPath
|
|
119
|
-
currentOrigin =
|
|
133
|
+
mobileConfig,
|
|
134
|
+
getInitialLaunchUrl,
|
|
135
|
+
subscribeToLaunchUrls,
|
|
136
|
+
resolveTargetPath,
|
|
137
|
+
currentOrigin = pageLocation().origin,
|
|
120
138
|
allowedHttpOrigins = [],
|
|
121
|
-
logger =
|
|
122
|
-
}
|
|
123
|
-
if (!router || typeof router.replace !== "function") {
|
|
124
|
-
throw new TypeError("registerMobileLaunchRouting requires router.replace().");
|
|
125
|
-
}
|
|
126
|
-
|
|
139
|
+
logger = SILENT_LAUNCH_LOGGER
|
|
140
|
+
}) {
|
|
127
141
|
const resolvedMobileConfig = resolveMobileConfig({
|
|
128
142
|
mobile: mobileConfig
|
|
129
143
|
});
|
|
130
|
-
const runtimeLogger =
|
|
131
|
-
logger && typeof logger === "object"
|
|
132
|
-
? logger
|
|
133
|
-
: {
|
|
134
|
-
info() {},
|
|
135
|
-
warn() {},
|
|
136
|
-
error() {}
|
|
137
|
-
};
|
|
138
144
|
|
|
139
145
|
async function applyIncomingUrl(url = "", reason = "manual") {
|
|
140
146
|
const normalizedTargetPath = normalizeIncomingAppUrl(url, resolvedMobileConfig, {
|
|
@@ -145,35 +151,30 @@ function registerMobileLaunchRouting({
|
|
|
145
151
|
return "";
|
|
146
152
|
}
|
|
147
153
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
);
|
|
159
|
-
resolvedTargetPath = normalizeResolvedRoutePath(nextTargetPath, normalizedTargetPath);
|
|
160
|
-
}
|
|
154
|
+
const nextTargetPath = await resolveTargetPath(
|
|
155
|
+
Object.freeze({
|
|
156
|
+
originalUrl: String(url || ""),
|
|
157
|
+
normalizedTargetPath,
|
|
158
|
+
reason,
|
|
159
|
+
mobileConfig: resolvedMobileConfig,
|
|
160
|
+
router
|
|
161
|
+
})
|
|
162
|
+
);
|
|
163
|
+
const resolvedTargetPath = normalizeResolvedRoutePath(nextTargetPath, normalizedTargetPath);
|
|
161
164
|
|
|
162
|
-
const currentFullPath = String(router.currentRoute
|
|
165
|
+
const currentFullPath = String(router.currentRoute.value.fullPath || "").trim();
|
|
163
166
|
if (currentFullPath === resolvedTargetPath) {
|
|
164
167
|
return resolvedTargetPath;
|
|
165
168
|
}
|
|
166
169
|
|
|
167
170
|
await router.replace(resolvedTargetPath);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
);
|
|
176
|
-
}
|
|
171
|
+
logger.info(
|
|
172
|
+
{
|
|
173
|
+
reason,
|
|
174
|
+
targetPath: resolvedTargetPath
|
|
175
|
+
},
|
|
176
|
+
"Mobile launch routing applied incoming app URL."
|
|
177
|
+
);
|
|
177
178
|
return resolvedTargetPath;
|
|
178
179
|
}
|
|
179
180
|
|
|
@@ -190,25 +191,23 @@ function registerMobileLaunchRouting({
|
|
|
190
191
|
resolvedMobileConfig.enabled === true
|
|
191
192
|
? subscribeToLaunchUrls((nextUrl) => {
|
|
192
193
|
Promise.resolve(applyIncomingUrl(nextUrl, "launch-event")).catch((error) => {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
);
|
|
200
|
-
}
|
|
194
|
+
logger.warn(
|
|
195
|
+
{
|
|
196
|
+
error: String(error?.message || error || "unknown error")
|
|
197
|
+
},
|
|
198
|
+
"Mobile launch routing failed to apply incoming app URL."
|
|
199
|
+
);
|
|
201
200
|
});
|
|
202
201
|
})
|
|
203
202
|
: () => {};
|
|
204
203
|
|
|
204
|
+
function dispose() {
|
|
205
|
+
unsubscribe();
|
|
206
|
+
}
|
|
207
|
+
|
|
205
208
|
return Object.freeze({
|
|
206
209
|
initialize,
|
|
207
|
-
dispose
|
|
208
|
-
if (typeof unsubscribe === "function") {
|
|
209
|
-
unsubscribe();
|
|
210
|
-
}
|
|
211
|
-
},
|
|
210
|
+
dispose,
|
|
212
211
|
applyIncomingUrl
|
|
213
212
|
});
|
|
214
213
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Browser } from "@capacitor/browser";
|
|
2
2
|
import { resolveCapacitorAbsoluteHttpUrl } from "./apiRequestClient.js";
|
|
3
|
+
import { pageLocation } from "../browser.js";
|
|
3
4
|
import { createTextError } from "../i18n.js";
|
|
4
5
|
|
|
5
6
|
function resolveCapacitorLaunchUrl(url = "", apiBaseUrl = "") {
|
|
@@ -10,36 +11,38 @@ function resolveCapacitorLaunchUrl(url = "", apiBaseUrl = "") {
|
|
|
10
11
|
});
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
/**
|
|
15
|
+
* @param {object} options
|
|
16
|
+
* @param {import("../types.js").CapacitorAppAdapter} options.adapter
|
|
17
|
+
* @param {{ open: (options: { url: string }) => Promise<void> }} [options.browserPlugin]
|
|
18
|
+
* @param {Pick<import("../types.js").PageLocation, "assign">} [options.location]
|
|
19
|
+
* @param {string} [options.apiBaseUrl]
|
|
20
|
+
*/
|
|
13
21
|
function createCapacitorAwareOAuthLaunchClient({
|
|
14
|
-
adapter
|
|
22
|
+
adapter,
|
|
15
23
|
browserPlugin = Browser,
|
|
16
|
-
location =
|
|
24
|
+
location = pageLocation(),
|
|
17
25
|
apiBaseUrl = ""
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const normalizedUrl = String(url || "").trim();
|
|
25
|
-
if (!normalizedUrl) {
|
|
26
|
-
throw createTextError("jskit.mobile.oauth.urlRequired");
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (adapter?.available === true && browserPlugin && typeof browserPlugin.open === "function") {
|
|
30
|
-
await browserPlugin.open({
|
|
31
|
-
url: resolveCapacitorLaunchUrl(normalizedUrl, apiBaseUrl)
|
|
32
|
-
});
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (!resolvedLocation || typeof resolvedLocation.assign !== "function") {
|
|
37
|
-
throw createTextError("jskit.mobile.oauth.browserUnavailable");
|
|
38
|
-
}
|
|
26
|
+
}) {
|
|
27
|
+
async function open({ url = "" } = {}) {
|
|
28
|
+
const normalizedUrl = String(url || "").trim();
|
|
29
|
+
if (!normalizedUrl) {
|
|
30
|
+
throw createTextError("jskit.mobile.oauth.urlRequired");
|
|
31
|
+
}
|
|
39
32
|
|
|
40
|
-
|
|
33
|
+
if (adapter.available === true) {
|
|
34
|
+
await browserPlugin.open({
|
|
35
|
+
url: resolveCapacitorLaunchUrl(normalizedUrl, apiBaseUrl)
|
|
36
|
+
});
|
|
41
37
|
return true;
|
|
42
38
|
}
|
|
39
|
+
|
|
40
|
+
location.assign(normalizedUrl);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return Object.freeze({
|
|
45
|
+
open
|
|
43
46
|
});
|
|
44
47
|
}
|
|
45
48
|
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// The types of what mobile-capacitor's client hands out and receives across its files.
|
|
2
|
+
|
|
3
|
+
/** @typedef {import("@jskit-ai/kernel/client/types").ClientRouter} ClientRouter */
|
|
4
|
+
/** @typedef {ReturnType<typeof import("@jskit-ai/kernel/client").resolveMobileConfig>} MobileConfig */
|
|
5
|
+
/** @typedef {ReturnType<typeof import("./runtime/mobileCapacitorRuntime.js").createMobileCapacitorRuntime>} MobileCapacitorRuntime */
|
|
6
|
+
/** @typedef {ReturnType<typeof import("./runtime/mobileLaunchRouting.js").registerMobileLaunchRouting>} MobileLaunchRouting */
|
|
7
|
+
/** @typedef {ReturnType<typeof import("./runtime/oauthLaunchClient.js").createCapacitorAwareOAuthLaunchClient>} CapacitorOAuthLaunchClient */
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What the mobile runtime asks of the native shell. Outside a shell, createNoopCapacitorAppAdapter() has every member.
|
|
11
|
+
* @typedef {object} CapacitorAppAdapter
|
|
12
|
+
* @property {boolean} available Whether a Capacitor shell is there.
|
|
13
|
+
* @property {() => Promise<string>} getInitialLaunchUrl
|
|
14
|
+
* @property {(handler: (url: string) => void) => () => void} subscribeToLaunchUrls
|
|
15
|
+
* @property {(handler: (event: { canGoBack: boolean }) => unknown) => () => void} subscribeToBackButton
|
|
16
|
+
* @property {() => Promise<boolean>} exitApp Whether the shell could leave the application.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The handle `addListener` gives back.
|
|
21
|
+
* @typedef {object} CapacitorListenerHandle
|
|
22
|
+
* @property {() => unknown} remove
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The Capacitor App plugin on the page. The application's shell supplies it, so each function may be missing.
|
|
27
|
+
* @typedef {object} CapacitorAppPlugin
|
|
28
|
+
* @property {() => Promise<{ url?: string } | undefined>} [getLaunchUrl]
|
|
29
|
+
* @property {(eventName: string, handler: (event: Record<string, unknown>) => void) => Promise<CapacitorListenerHandle> | CapacitorListenerHandle} [addListener]
|
|
30
|
+
* @property {() => Promise<void>} [exitApp]
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The page's location, as far as OAuth launching and launch routing use it.
|
|
35
|
+
* @typedef {object} PageLocation
|
|
36
|
+
* @property {string} origin
|
|
37
|
+
* @property {(url: string) => void} assign
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The part of `client.shell`'s placement runtime the mobile runtime reads.
|
|
42
|
+
* @typedef {object} MobilePlacementRuntime
|
|
43
|
+
* @property {() => object | null} getContext
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The part of `client.auth`'s `mobileCallback` the mobile runtime calls.
|
|
48
|
+
* @typedef {object} MobileAuthCallbackCompleter
|
|
49
|
+
* @property {(input: { url: string, fallbackReturnTo: string, placementContext: object | null, defaultProvider: string, refreshSession: () => Promise<unknown> }) => Promise<{ completed: boolean, returnTo?: string }>} completeFromUrl
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The part of `client.auth`'s `guard` the mobile runtime calls.
|
|
54
|
+
* @typedef {object} MobileAuthGuardRuntime
|
|
55
|
+
* @property {() => Promise<unknown>} initialize
|
|
56
|
+
* @property {() => Promise<unknown>} refresh
|
|
57
|
+
* @property {() => { oauthDefaultProvider?: string }} getState
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The logger launch routing reports to.
|
|
62
|
+
* @typedef {object} MobileLaunchLogger
|
|
63
|
+
* @property {(details: object, message: string) => void} info
|
|
64
|
+
* @property {(details: object, message: string) => void} warn
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* What `client.mobile` provides.
|
|
69
|
+
* @typedef {object} MobileCapability
|
|
70
|
+
* @property {CapacitorAppAdapter} adapter
|
|
71
|
+
* @property {MobileConfig} config
|
|
72
|
+
* @property {CapacitorOAuthLaunchClient} oauthLaunch
|
|
73
|
+
* @property {() => void} restoreFetch Puts the page's own fetch back; does nothing when it was never replaced.
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
export {};
|
|
@@ -7,6 +7,13 @@ function mobile(config) {
|
|
|
7
7
|
return resolveMobileConfig({ mobile: config });
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
// What the mobile runtime always passes, for tests that exercise only one of these.
|
|
11
|
+
const LAUNCH_ROUTING_DOUBLES = Object.freeze({
|
|
12
|
+
getInitialLaunchUrl: async () => "",
|
|
13
|
+
subscribeToLaunchUrls: () => () => {},
|
|
14
|
+
resolveTargetPath: ({ normalizedTargetPath }) => normalizedTargetPath
|
|
15
|
+
});
|
|
16
|
+
|
|
10
17
|
test("normalizeIncomingAppUrl normalizes custom-scheme auth callback routes into router paths", () => {
|
|
11
18
|
const normalized = normalizeIncomingAppUrl("convict://auth/login?code=abc&oauthProvider=google", mobile({
|
|
12
19
|
enabled: true,
|
|
@@ -79,6 +86,7 @@ test("normalizeIncomingAppUrl rejects unowned schemes and domains", () => {
|
|
|
79
86
|
test("registerMobileLaunchRouting initializes and applies the initial launch URL", async () => {
|
|
80
87
|
const replaceCalls = [];
|
|
81
88
|
const runtime = registerMobileLaunchRouting({
|
|
89
|
+
...LAUNCH_ROUTING_DOUBLES,
|
|
82
90
|
router: {
|
|
83
91
|
currentRoute: {
|
|
84
92
|
value: {
|
|
@@ -108,6 +116,7 @@ test("registerMobileLaunchRouting subscribes to later launch URLs and routes the
|
|
|
108
116
|
const replaceCalls = [];
|
|
109
117
|
let listener = null;
|
|
110
118
|
const runtime = registerMobileLaunchRouting({
|
|
119
|
+
...LAUNCH_ROUTING_DOUBLES,
|
|
111
120
|
router: {
|
|
112
121
|
currentRoute: {
|
|
113
122
|
value: {
|
|
@@ -145,6 +154,7 @@ test("registerMobileLaunchRouting subscribes to later launch URLs and routes the
|
|
|
145
154
|
test("registerMobileLaunchRouting lets a resolver override the final route target", async () => {
|
|
146
155
|
const replaceCalls = [];
|
|
147
156
|
const runtime = registerMobileLaunchRouting({
|
|
157
|
+
...LAUNCH_ROUTING_DOUBLES,
|
|
148
158
|
router: {
|
|
149
159
|
currentRoute: {
|
|
150
160
|
value: {
|
|
@@ -179,6 +189,7 @@ test("registerMobileLaunchRouting lets a resolver override the final route targe
|
|
|
179
189
|
test("registerMobileLaunchRouting forwards unknown deep-link paths to the normal router", async () => {
|
|
180
190
|
const replaceCalls = [];
|
|
181
191
|
const runtime = registerMobileLaunchRouting({
|
|
192
|
+
...LAUNCH_ROUTING_DOUBLES,
|
|
182
193
|
router: {
|
|
183
194
|
currentRoute: {
|
|
184
195
|
value: {
|
|
@@ -207,6 +218,7 @@ test("registerMobileLaunchRouting forwards unknown deep-link paths to the normal
|
|
|
207
218
|
test("registerMobileLaunchRouting no-ops when mobile config is disabled", async () => {
|
|
208
219
|
const replaceCalls = [];
|
|
209
220
|
const runtime = registerMobileLaunchRouting({
|
|
221
|
+
...LAUNCH_ROUTING_DOUBLES,
|
|
210
222
|
router: {
|
|
211
223
|
currentRoute: {
|
|
212
224
|
value: {
|
package/test/provider.test.js
CHANGED
|
@@ -100,6 +100,40 @@ test("mobile providers publish, boot, and dispose their declared capabilities",
|
|
|
100
100
|
}
|
|
101
101
|
});
|
|
102
102
|
|
|
103
|
+
test("mobile providers start and stop on a page with no Capacitor, leaving fetch alone", async () => {
|
|
104
|
+
const originalCapacitor = globalThis.Capacitor;
|
|
105
|
+
const originalConfig = globalThis.__JSKIT_CLIENT_APP_CONFIG__;
|
|
106
|
+
const originalFetch = globalThis.fetch;
|
|
107
|
+
let capabilities = null;
|
|
108
|
+
|
|
109
|
+
globalThis.__JSKIT_CLIENT_APP_CONFIG__ = { mobile: { enabled: false } };
|
|
110
|
+
delete globalThis.Capacitor;
|
|
111
|
+
|
|
112
|
+
const runtime = createCapabilityRuntime({
|
|
113
|
+
inputs: {
|
|
114
|
+
"client.router": createRouter()
|
|
115
|
+
},
|
|
116
|
+
providers: [
|
|
117
|
+
MobileCapacitorClientProvider,
|
|
118
|
+
MobileCapacitorRuntimeProvider,
|
|
119
|
+
createMobileProbe((value) => {
|
|
120
|
+
capabilities = value;
|
|
121
|
+
})
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
await runtime.start();
|
|
127
|
+
assert.equal(capabilities.mobile.adapter.available, false);
|
|
128
|
+
assert.equal(globalThis.fetch, originalFetch);
|
|
129
|
+
await runtime.shutdown();
|
|
130
|
+
assert.equal(globalThis.fetch, originalFetch);
|
|
131
|
+
} finally {
|
|
132
|
+
globalThis.Capacitor = originalCapacitor;
|
|
133
|
+
globalThis.__JSKIT_CLIENT_APP_CONFIG__ = originalConfig;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
103
137
|
test("mobile provider installs and restores the Capacitor-aware fetch wrapper", async () => {
|
|
104
138
|
const originalCapacitor = globalThis.Capacitor;
|
|
105
139
|
const originalConfig = globalThis.__JSKIT_CLIENT_APP_CONFIG__;
|
package/test/runtime.test.js
CHANGED
|
@@ -2,7 +2,8 @@ import assert from "node:assert/strict";
|
|
|
2
2
|
import test from "node:test";
|
|
3
3
|
import {
|
|
4
4
|
createGlobalCapacitorAppAdapter,
|
|
5
|
-
createNoopCapacitorAppAdapter
|
|
5
|
+
createNoopCapacitorAppAdapter,
|
|
6
|
+
resolveCapacitorAppPlugin
|
|
6
7
|
} from "../src/client/runtime/globalCapacitorAppAdapter.js";
|
|
7
8
|
import { createMobileCapacitorRuntime } from "../src/client/runtime/mobileCapacitorRuntime.js";
|
|
8
9
|
import {
|
|
@@ -84,6 +85,77 @@ test("createGlobalCapacitorAppAdapter reads launch URLs from a Capacitor App plu
|
|
|
84
85
|
assert.equal(listeners.has("backButton"), false);
|
|
85
86
|
});
|
|
86
87
|
|
|
88
|
+
test("createGlobalCapacitorAppAdapter is the disabled adapter when the page has no App plugin", async () => {
|
|
89
|
+
const adapter = createGlobalCapacitorAppAdapter({ globalObject: {} });
|
|
90
|
+
|
|
91
|
+
assert.equal(adapter.available, false);
|
|
92
|
+
assert.equal(await adapter.exitApp(), false);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("createGlobalCapacitorAppAdapter finds the plugin under Capacitor.Plugins or Capacitor", () => {
|
|
96
|
+
const appPlugin = { addListener() {} };
|
|
97
|
+
|
|
98
|
+
assert.equal(resolveCapacitorAppPlugin({ Capacitor: { Plugins: { App: appPlugin } } }), appPlugin);
|
|
99
|
+
assert.equal(resolveCapacitorAppPlugin({ Capacitor: { App: appPlugin } }), appPlugin);
|
|
100
|
+
assert.equal(resolveCapacitorAppPlugin({}), null);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("createGlobalCapacitorAppAdapter works with a plugin that has only some of its functions", async () => {
|
|
104
|
+
const listening = createGlobalCapacitorAppAdapter({
|
|
105
|
+
appPlugin: {
|
|
106
|
+
addListener() {
|
|
107
|
+
return { remove() {} };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
assert.equal(listening.available, true);
|
|
112
|
+
assert.equal(await listening.getInitialLaunchUrl(), "");
|
|
113
|
+
assert.equal(await listening.exitApp(), false);
|
|
114
|
+
|
|
115
|
+
const launching = createGlobalCapacitorAppAdapter({
|
|
116
|
+
appPlugin: {
|
|
117
|
+
async getLaunchUrl() {
|
|
118
|
+
return { url: " convict://w/acme " };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
assert.equal(launching.available, true);
|
|
123
|
+
assert.equal(await launching.getInitialLaunchUrl(), "convict://w/acme");
|
|
124
|
+
assert.equal(typeof launching.subscribeToLaunchUrls(() => {}), "function");
|
|
125
|
+
assert.equal(typeof launching.subscribeToBackButton(() => {}), "function");
|
|
126
|
+
|
|
127
|
+
const empty = createGlobalCapacitorAppAdapter({ appPlugin: {} });
|
|
128
|
+
assert.equal(empty.available, false);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("createGlobalCapacitorAppAdapter removes a listener that arrives after its subscription was disposed", async () => {
|
|
132
|
+
const removed = [];
|
|
133
|
+
let fire = null;
|
|
134
|
+
const adapter = createGlobalCapacitorAppAdapter({
|
|
135
|
+
appPlugin: {
|
|
136
|
+
async addListener(eventName, handler) {
|
|
137
|
+
fire = handler;
|
|
138
|
+
return {
|
|
139
|
+
remove() {
|
|
140
|
+
removed.push(eventName);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const urls = [];
|
|
148
|
+
const unsubscribe = adapter.subscribeToLaunchUrls((url) => {
|
|
149
|
+
urls.push(url);
|
|
150
|
+
});
|
|
151
|
+
unsubscribe();
|
|
152
|
+
fire({ url: "convict://w/acme" });
|
|
153
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
154
|
+
|
|
155
|
+
assert.deepEqual(urls, []);
|
|
156
|
+
assert.deepEqual(removed, ["appUrlOpen"]);
|
|
157
|
+
});
|
|
158
|
+
|
|
87
159
|
test("createCapacitorAwareOAuthLaunchClient uses Capacitor Browser inside the shell", async () => {
|
|
88
160
|
const openCalls = [];
|
|
89
161
|
const launchClient = createCapacitorAwareOAuthLaunchClient({
|
|
@@ -198,6 +270,46 @@ test("createCapacitorAwareOAuthLaunchClient falls back to browser navigation out
|
|
|
198
270
|
]);
|
|
199
271
|
});
|
|
200
272
|
|
|
273
|
+
test("createCapacitorAwareOAuthLaunchClient refuses to navigate outside a browser", async () => {
|
|
274
|
+
const launchClient = createCapacitorAwareOAuthLaunchClient({
|
|
275
|
+
adapter: createNoopCapacitorAppAdapter()
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
await assert.rejects(
|
|
279
|
+
launchClient.open({ url: "/api/oauth/google/start" }),
|
|
280
|
+
(error) => error.messageCode === "mobile.oauth.browserUnavailable"
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
test("mobile capacitor runtime routes an auth callback as it came when the application has no auth", async () => {
|
|
285
|
+
const replaceCalls = [];
|
|
286
|
+
const runtime = createMobileCapacitorRuntime({
|
|
287
|
+
router: {
|
|
288
|
+
currentRoute: {
|
|
289
|
+
value: {
|
|
290
|
+
fullPath: "/home"
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
async replace(target) {
|
|
294
|
+
replaceCalls.push(target);
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
mobileConfig: {
|
|
298
|
+
enabled: true,
|
|
299
|
+
auth: {
|
|
300
|
+
customScheme: "convict",
|
|
301
|
+
callbackPath: "/auth/login"
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
adapter: createNoopCapacitorAppAdapter()
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const targetPath = await runtime.applyIncomingUrl("convict://auth/login?code=abc", "launch-event");
|
|
308
|
+
|
|
309
|
+
assert.equal(targetPath, "/auth/login?code=abc");
|
|
310
|
+
assert.deepEqual(replaceCalls, ["/auth/login?code=abc"]);
|
|
311
|
+
});
|
|
312
|
+
|
|
201
313
|
test("mobile capacitor runtime routes an initial launch URL through kernel mobile routing", async () => {
|
|
202
314
|
const replaceCalls = [];
|
|
203
315
|
const runtime = createMobileCapacitorRuntime({
|
|
@@ -224,6 +336,9 @@ test("mobile capacitor runtime routes an initial launch URL through kernel mobil
|
|
|
224
336
|
},
|
|
225
337
|
subscribeToLaunchUrls() {
|
|
226
338
|
return () => {};
|
|
339
|
+
},
|
|
340
|
+
subscribeToBackButton() {
|
|
341
|
+
return () => {};
|
|
227
342
|
}
|
|
228
343
|
}
|
|
229
344
|
});
|
|
@@ -269,6 +384,9 @@ test("mobile capacitor runtime waits for auth guard initialization before applyi
|
|
|
269
384
|
},
|
|
270
385
|
subscribeToLaunchUrls() {
|
|
271
386
|
return () => {};
|
|
387
|
+
},
|
|
388
|
+
subscribeToBackButton() {
|
|
389
|
+
return () => {};
|
|
272
390
|
}
|
|
273
391
|
},
|
|
274
392
|
authGuardRuntime: {
|
|
@@ -346,6 +464,11 @@ test("mobile capacitor runtime resolves successful auth callbacks to the returne
|
|
|
346
464
|
}
|
|
347
465
|
},
|
|
348
466
|
authGuardRuntime: {
|
|
467
|
+
async initialize() {
|
|
468
|
+
return {
|
|
469
|
+
authenticated: true
|
|
470
|
+
};
|
|
471
|
+
},
|
|
349
472
|
getState() {
|
|
350
473
|
return {
|
|
351
474
|
oauthDefaultProvider: "google"
|