@happyvertical/smrt-capacitor-session 0.62.8 → 0.62.10
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/AGENTS.md
CHANGED
|
@@ -13,17 +13,22 @@ native-bearer stack, which has no WebView.
|
|
|
13
13
|
While the app is resumed it polls the cookie jars of the current page, `server.url` and `plugins.SmrtSession.origins` origins every `POLL_MS` (500 ms)
|
|
14
14
|
and calls `CookieManager.flush()` when it changed; it also flushes on pause, stop and
|
|
15
15
|
destroy. Polling is required: a fetch-set cookie raises no page load or lifecycle event,
|
|
16
|
-
and `am force-stop` of a foreground app runs no callback.
|
|
16
|
+
and `am force-stop` of a foreground app runs no callback. On each page load it also checks
|
|
17
|
+
the jar and injects `res/raw/smrt_session_watch.js`, a `PerformanceObserver` that calls
|
|
18
|
+
`checkCookies()` when a fetch, XHR or beacon completes, so a fetch-set sign-in cookie is
|
|
19
|
+
flushed within one bridge hop instead of up to `POLL_MS` later (#5160).
|
|
17
20
|
- `.../CookieFlushPolicy.java`: the pure change detector, tested without Android.
|
|
18
21
|
- `test/CookieFlushPolicyTest.java`: plain-Java test run by `pnpm test` (needs a JDK;
|
|
19
|
-
skipped locally without one, a failure under `CI`).
|
|
22
|
+
skipped locally without one, a failure under `CI`). `pnpm test` also runs the watch
|
|
23
|
+
script against fakes in a Node vm.
|
|
20
24
|
- `scripts/validate-plugin.mjs`: structural validation, also the `build` and `typecheck` step.
|
|
21
25
|
|
|
22
26
|
## Invariants
|
|
23
27
|
|
|
24
28
|
- Android only, no JS entry. The plugin loads on `cap sync` plugin registration; the web app
|
|
25
29
|
adds no code. `Capacitor.Plugins.SmrtSession.flush()` exists for an explicit flush.
|
|
26
|
-
- The loss window is
|
|
30
|
+
- The loss window is one bridge hop after a fetch/XHR/beacon that set a watched cookie, and at
|
|
31
|
+
most `POLL_MS` after any other cookie change. Cookies of other origins
|
|
27
32
|
are flushed on pause/stop, not polled.
|
|
28
33
|
- No credential, token or cookie value is read, stored or logged; the snapshot string stays in memory.
|
|
29
34
|
- The Gradle compile happens in the consuming app (`cap sync`, assemble), against its Capacitor.
|
package/README.md
CHANGED
|
@@ -10,10 +10,17 @@ pnpm add @happyvertical/smrt-capacitor-session # in the Capacitor shell, not t
|
|
|
10
10
|
pnpm exec cap sync android
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
`cap sync` registers the plugin; no web or Java change is needed. The plugin flushes
|
|
14
|
-
|
|
13
|
+
`cap sync` registers the plugin; no web or Java change is needed. The plugin flushes as soon
|
|
14
|
+
as a fetch, XHR or beacon that changed a watched cookie completes (a `use:enhance` sign-in),
|
|
15
|
+
otherwise within 500 ms of any cookie change, and on pause, stop and destroy. A page can
|
|
15
16
|
force one with `Capacitor.Plugins.SmrtSession.flush()`.
|
|
16
17
|
|
|
18
|
+
On every page load the plugin checks the jar and injects a small `PerformanceObserver`
|
|
19
|
+
(`android/src/main/res/raw/smrt_session_watch.js`) that calls
|
|
20
|
+
`Capacitor.Plugins.SmrtSession.checkCookies()` when a fetch, XHR or beacon completes; the
|
|
21
|
+
plugin flushes only if the watched cookies changed. It reads no cookie and adds no idle work
|
|
22
|
+
(#5160).
|
|
23
|
+
|
|
17
24
|
Android only (the plugin has no iOS code). Requires `@capacitor/core` >= 8.
|
|
18
25
|
|
|
19
26
|
## Which origins are covered
|
package/android/src/main/java/com/happyvertical/smrt/capacitor/session/SmrtSessionPlugin.java
CHANGED
|
@@ -4,6 +4,10 @@ import android.os.Handler;
|
|
|
4
4
|
import android.os.Looper;
|
|
5
5
|
import android.webkit.CookieManager;
|
|
6
6
|
import android.webkit.WebView;
|
|
7
|
+
import java.io.ByteArrayOutputStream;
|
|
8
|
+
import java.io.IOException;
|
|
9
|
+
import java.io.InputStream;
|
|
10
|
+
import java.nio.charset.StandardCharsets;
|
|
7
11
|
import java.util.ArrayList;
|
|
8
12
|
import java.util.List;
|
|
9
13
|
import java.util.Set;
|
|
@@ -11,6 +15,7 @@ import com.getcapacitor.Logger;
|
|
|
11
15
|
import com.getcapacitor.Plugin;
|
|
12
16
|
import com.getcapacitor.PluginCall;
|
|
13
17
|
import com.getcapacitor.PluginMethod;
|
|
18
|
+
import com.getcapacitor.WebViewListener;
|
|
14
19
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
15
20
|
|
|
16
21
|
/**
|
|
@@ -30,9 +35,13 @@ import com.getcapacitor.annotation.CapacitorPlugin;
|
|
|
30
35
|
* and `am force-stop` of a foreground app runs none either. This plugin
|
|
31
36
|
* therefore watches the cookie jar of the current page while the app is
|
|
32
37
|
* resumed, flushes within POLL_MS of any change, and also flushes on pause,
|
|
33
|
-
* stop and destroy.
|
|
34
|
-
*
|
|
35
|
-
*
|
|
38
|
+
* stop and destroy. To close that POLL_MS window for a fetch-set cookie
|
|
39
|
+
* (#5160), every page load also checks the jar and installs a small observer
|
|
40
|
+
* (res/raw/smrt_session_watch.js) that calls {@code checkCookies()} as soon as
|
|
41
|
+
* a fetch, XHR or beacon completes, so the sign-in cookie is flushed within one
|
|
42
|
+
* bridge hop of its response. It registers itself when the app runs `cap sync`;
|
|
43
|
+
* the web app needs no call. `flush()` is exposed for a page that wants a
|
|
44
|
+
* deterministic flush right after it sets a cookie.
|
|
36
45
|
*/
|
|
37
46
|
@CapacitorPlugin(name = "SmrtSession")
|
|
38
47
|
public class SmrtSessionPlugin extends Plugin {
|
|
@@ -42,6 +51,18 @@ public class SmrtSessionPlugin extends Plugin {
|
|
|
42
51
|
private final Handler handler = new Handler(Looper.getMainLooper());
|
|
43
52
|
private final CookieFlushPolicy policy = new CookieFlushPolicy();
|
|
44
53
|
private boolean polling = false;
|
|
54
|
+
private String watchScript;
|
|
55
|
+
|
|
56
|
+
/** Checks the jar and installs the fetch-completion observer on every page load. */
|
|
57
|
+
private final WebViewListener pageListener = new WebViewListener() {
|
|
58
|
+
@Override
|
|
59
|
+
public void onPageLoaded(WebView webView) {
|
|
60
|
+
flushIfChanged();
|
|
61
|
+
if (watchScript != null) {
|
|
62
|
+
webView.evaluateJavascript(watchScript, null);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
45
66
|
|
|
46
67
|
private final Runnable poll = new Runnable() {
|
|
47
68
|
@Override
|
|
@@ -57,11 +78,15 @@ public class SmrtSessionPlugin extends Plugin {
|
|
|
57
78
|
@Override
|
|
58
79
|
public void load() {
|
|
59
80
|
Logger.info(getLogTag(), "session cookie flush active");
|
|
81
|
+
watchScript = readWatchScript();
|
|
82
|
+
// Not added here: Bridge.Builder replaces the listener list after plugins load.
|
|
83
|
+
handler.post(this::attachPageListener);
|
|
60
84
|
startPolling();
|
|
61
85
|
}
|
|
62
86
|
|
|
63
87
|
@Override
|
|
64
88
|
protected void handleOnResume() {
|
|
89
|
+
attachPageListener();
|
|
65
90
|
startPolling();
|
|
66
91
|
}
|
|
67
92
|
|
|
@@ -88,6 +113,37 @@ public class SmrtSessionPlugin extends Plugin {
|
|
|
88
113
|
call.resolve();
|
|
89
114
|
}
|
|
90
115
|
|
|
116
|
+
/** Check-and-flush, called by the page observer when a fetch, XHR or beacon completes. */
|
|
117
|
+
@PluginMethod
|
|
118
|
+
public void checkCookies(PluginCall call) {
|
|
119
|
+
getBridge().executeOnMainThread(() -> {
|
|
120
|
+
flushIfChanged();
|
|
121
|
+
call.resolve();
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private String readWatchScript() {
|
|
126
|
+
try (InputStream in = getContext().getResources().openRawResource(R.raw.smrt_session_watch)) {
|
|
127
|
+
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
128
|
+
byte[] buf = new byte[4096];
|
|
129
|
+
for (int n; (n = in.read(buf)) != -1; ) {
|
|
130
|
+
out.write(buf, 0, n);
|
|
131
|
+
}
|
|
132
|
+
return out.toString(StandardCharsets.UTF_8.name());
|
|
133
|
+
} catch (IOException | RuntimeException e) {
|
|
134
|
+
Logger.warn(getLogTag(), "session watch script unavailable; polling only");
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Idempotent: the listener list is not readable, so remove before adding. */
|
|
140
|
+
private void attachPageListener() {
|
|
141
|
+
if (getBridge() != null) {
|
|
142
|
+
getBridge().removeWebViewListener(pageListener);
|
|
143
|
+
getBridge().addWebViewListener(pageListener);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
91
147
|
private void startPolling() {
|
|
92
148
|
if (polling) {
|
|
93
149
|
return;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Installed by SmrtSessionPlugin on every page load (happyvertical/smrt#5160).
|
|
2
|
+
// A cookie set by a fetch response (a SvelteKit use:enhance sign-in) raises no
|
|
3
|
+
// page load, so tell the plugin as soon as a fetch, XHR or beacon completes; it
|
|
4
|
+
// flushes the WebView cookie jar only if the watched cookies changed. Reads no
|
|
5
|
+
// cookie, sends nothing but the nudge, and is a no-op outside the Capacitor shell.
|
|
6
|
+
(function () {
|
|
7
|
+
var w = window;
|
|
8
|
+
if (w.__smrtSessionWatch) return;
|
|
9
|
+
var plugin = w.Capacitor && w.Capacitor.Plugins && w.Capacitor.Plugins.SmrtSession;
|
|
10
|
+
if (!plugin || typeof plugin.checkCookies !== 'function' || typeof w.PerformanceObserver !== 'function') return;
|
|
11
|
+
w.__smrtSessionWatch = true;
|
|
12
|
+
var kinds = { fetch: true, xmlhttprequest: true, beacon: true };
|
|
13
|
+
new w.PerformanceObserver(function (list) {
|
|
14
|
+
var entries = list.getEntries();
|
|
15
|
+
for (var i = 0; i < entries.length; i++) {
|
|
16
|
+
if (kinds[entries[i].initiatorType]) {
|
|
17
|
+
Promise.resolve(plugin.checkCookies()).catch(function () {});
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}).observe({ type: 'resource' });
|
|
22
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@happyvertical/smrt-capacitor-session",
|
|
3
|
-
"version": "0.62.
|
|
3
|
+
"version": "0.62.10",
|
|
4
4
|
"description": "Capacitor Android plugin that keeps a WebView cookie session durable: flushes the WebView CookieManager to disk when the cookie jar changes and on pause, stop and destroy",
|
|
5
5
|
"author": "HappyVertical",
|
|
6
6
|
"license": "MIT",
|