@happyvertical/smrt-capacitor-session 0.0.0-stage → 0.62.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/AGENTS.md +29 -0
- package/CLAUDE.md +1 -0
- package/LICENSE +7 -0
- package/README.md +33 -2
- package/android/build.gradle +79 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/happyvertical/smrt/capacitor/session/CookieFlushPolicy.java +70 -0
- package/android/src/main/java/com/happyvertical/smrt/capacitor/session/SmrtSessionPlugin.java +134 -0
- package/package.json +40 -3
package/AGENTS.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# @happyvertical/smrt-capacitor-session
|
|
2
|
+
|
|
3
|
+
Capacitor Android plugin that keeps a WebView cookie session durable (#5077).
|
|
4
|
+
Android's WebView writes new cookies to disk lazily (about every 30 s), so a shell
|
|
5
|
+
killed soon after a cookie-session sign-in comes back signed out. This plugin is the
|
|
6
|
+
framework's answer for Capacitor shells that load a smrt web app and use its cookie
|
|
7
|
+
session (Hearth, #254); it is not part of the KMP `smrt-mobile` / `smrt-android`
|
|
8
|
+
native-bearer stack, which has no WebView.
|
|
9
|
+
|
|
10
|
+
## Layout
|
|
11
|
+
|
|
12
|
+
- `android/src/main/java/.../SmrtSessionPlugin.java`: `@CapacitorPlugin(name = "SmrtSession")`.
|
|
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
|
+
and calls `CookieManager.flush()` when it changed; it also flushes on pause, stop and
|
|
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.
|
|
17
|
+
- `.../CookieFlushPolicy.java`: the pure change detector, tested without Android.
|
|
18
|
+
- `test/CookieFlushPolicyTest.java`: plain-Java test run by `pnpm test` (needs a JDK;
|
|
19
|
+
skipped locally without one, a failure under `CI`).
|
|
20
|
+
- `scripts/validate-plugin.mjs`: structural validation, also the `build` and `typecheck` step.
|
|
21
|
+
|
|
22
|
+
## Invariants
|
|
23
|
+
|
|
24
|
+
- Android only, no JS entry. The plugin loads on `cap sync` plugin registration; the web app
|
|
25
|
+
adds no code. `Capacitor.Plugins.SmrtSession.flush()` exists for an explicit flush.
|
|
26
|
+
- The loss window is at most `POLL_MS` after a cookie change. Cookies of other origins
|
|
27
|
+
are flushed on pause/stop, not polled.
|
|
28
|
+
- No credential, token or cookie value is read, stored or logged; the snapshot string stays in memory.
|
|
29
|
+
- The Gradle compile happens in the consuming app (`cap sync`, assemble), against its Capacitor.
|
package/CLAUDE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@AGENTS.md
|
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright <2025> <Happy Vertical Corporation>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @happyvertical/smrt-capacitor-session
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Capacitor Android plugin that flushes the WebView `CookieManager` to disk so a cookie session
|
|
4
|
+
survives the app being killed right after sign-in (happyvertical/smrt#5077).
|
|
5
|
+
|
|
6
|
+
## Use
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add @happyvertical/smrt-capacitor-session # in the Capacitor shell, not the web app
|
|
10
|
+
pnpm exec cap sync android
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`cap sync` registers the plugin; no web or Java change is needed. The plugin flushes within
|
|
14
|
+
500 ms of any cookie change, and on pause, stop and destroy. A page can
|
|
15
|
+
force one with `Capacitor.Plugins.SmrtSession.flush()`.
|
|
16
|
+
|
|
17
|
+
Android only (the plugin has no iOS code). Requires `@capacitor/core` >= 8.
|
|
18
|
+
|
|
19
|
+
## Which origins are covered
|
|
20
|
+
|
|
21
|
+
The cookies of these origins are watched and flushed within 500 ms of a change (deduplicated):
|
|
22
|
+
|
|
23
|
+
1. the origin of the page the WebView is showing;
|
|
24
|
+
2. the origin of Capacitor's `server.url`, so a shell that loads the app remotely is covered
|
|
25
|
+
even while the page is on another origin;
|
|
26
|
+
3. extra origins listed in the plugin config:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
// capacitor.config.js
|
|
30
|
+
plugins: { SmrtSession: { origins: ['https://hearthed.org'] } }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
A shell that serves the app from `https://localhost` and signs in against a remote origin must set
|
|
34
|
+
`server.url` or list that origin. Cookies of any other origin are flushed on pause, stop and destroy only.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
capacitorVersion = System.getenv('CAPACITOR_VERSION')
|
|
3
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
4
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
5
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
|
|
6
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
buildscript {
|
|
10
|
+
repositories {
|
|
11
|
+
google()
|
|
12
|
+
mavenCentral()
|
|
13
|
+
maven {
|
|
14
|
+
url = "https://plugins.gradle.org/m2/"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
dependencies {
|
|
18
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
19
|
+
if (System.getenv("CAP_PLUGIN_PUBLISH") == "true") {
|
|
20
|
+
classpath 'io.github.gradle-nexus:publish-plugin:1.3.0'
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
apply plugin: 'com.android.library'
|
|
26
|
+
if (System.getenv("CAP_PLUGIN_PUBLISH") == "true") {
|
|
27
|
+
apply plugin: 'io.github.gradle-nexus.publish-plugin'
|
|
28
|
+
apply from: file('../../scripts/android/publish-root.gradle')
|
|
29
|
+
apply from: file('../../scripts/android/publish-module.gradle')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
android {
|
|
33
|
+
namespace = "com.happyvertical.smrt.capacitor.session"
|
|
34
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
35
|
+
defaultConfig {
|
|
36
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
37
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
38
|
+
versionCode 1
|
|
39
|
+
versionName "1.0"
|
|
40
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
41
|
+
}
|
|
42
|
+
buildTypes {
|
|
43
|
+
release {
|
|
44
|
+
minifyEnabled false
|
|
45
|
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
lintOptions {
|
|
49
|
+
abortOnError = false
|
|
50
|
+
}
|
|
51
|
+
compileOptions {
|
|
52
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
53
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
54
|
+
}
|
|
55
|
+
publishing {
|
|
56
|
+
singleVariant("release")
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
repositories {
|
|
61
|
+
google()
|
|
62
|
+
mavenCentral()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
dependencies {
|
|
67
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
68
|
+
|
|
69
|
+
if (System.getenv("CAP_PLUGIN_PUBLISH") == "true") {
|
|
70
|
+
implementation "com.capacitorjs:core:$capacitorVersion"
|
|
71
|
+
} else {
|
|
72
|
+
implementation project(':capacitor-android')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
76
|
+
testImplementation "junit:junit:$junitVersion"
|
|
77
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
78
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
79
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
package com.happyvertical.smrt.capacitor.session;
|
|
2
|
+
|
|
3
|
+
import java.net.URI;
|
|
4
|
+
import java.util.LinkedHashSet;
|
|
5
|
+
import java.util.Set;
|
|
6
|
+
import java.util.function.Function;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Decides when the WebView cookie jar needs flushing. Pure Java so it is tested
|
|
10
|
+
* without Android: the plugin feeds it a snapshot of the cookies and flushes
|
|
11
|
+
* when this says the jar changed since the last flush.
|
|
12
|
+
*/
|
|
13
|
+
final class CookieFlushPolicy {
|
|
14
|
+
|
|
15
|
+
private String lastFlushed;
|
|
16
|
+
|
|
17
|
+
/** Origin (scheme://host[:port]) of a URL, or null when it has none; deduplication key. */
|
|
18
|
+
static String origin(String url) {
|
|
19
|
+
if (url == null) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
URI uri = URI.create(url.trim());
|
|
24
|
+
if (uri.getScheme() == null || uri.getHost() == null) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return uri.getScheme() + "://" + uri.getHost() + (uri.getPort() == -1 ? "" : ":" + uri.getPort());
|
|
28
|
+
} catch (IllegalArgumentException e) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The distinct origins among {@code urls}, in order; nulls and non-URLs are skipped. */
|
|
34
|
+
static Set<String> origins(Iterable<String> urls) {
|
|
35
|
+
Set<String> out = new LinkedHashSet<>();
|
|
36
|
+
for (String url : urls) {
|
|
37
|
+
String o = origin(url);
|
|
38
|
+
if (o != null) {
|
|
39
|
+
out.add(o);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** One snapshot of the cookie jars of every origin; null when none has a cookie. */
|
|
46
|
+
static String snapshot(Set<String> origins, Function<String, String> cookiesFor) {
|
|
47
|
+
StringBuilder sb = null;
|
|
48
|
+
for (String o : origins) {
|
|
49
|
+
String c = cookiesFor.apply(o);
|
|
50
|
+
if (c != null) {
|
|
51
|
+
sb = (sb == null ? new StringBuilder() : sb.append('\n')).append(o).append('=').append(c);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return sb == null ? null : sb.toString();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** True when {@code snapshot} differs from the last one flushed; records it. */
|
|
58
|
+
synchronized boolean shouldFlush(String snapshot) {
|
|
59
|
+
if (snapshot == null ? lastFlushed == null : snapshot.equals(lastFlushed)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
lastFlushed = snapshot;
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Forget the last snapshot so the next one flushes (after an explicit flush or lifecycle flush). */
|
|
67
|
+
synchronized void reset() {
|
|
68
|
+
lastFlushed = null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
package com.happyvertical.smrt.capacitor.session;
|
|
2
|
+
|
|
3
|
+
import android.os.Handler;
|
|
4
|
+
import android.os.Looper;
|
|
5
|
+
import android.webkit.CookieManager;
|
|
6
|
+
import android.webkit.WebView;
|
|
7
|
+
import java.util.ArrayList;
|
|
8
|
+
import java.util.List;
|
|
9
|
+
import java.util.Set;
|
|
10
|
+
import com.getcapacitor.Logger;
|
|
11
|
+
import com.getcapacitor.Plugin;
|
|
12
|
+
import com.getcapacitor.PluginCall;
|
|
13
|
+
import com.getcapacitor.PluginMethod;
|
|
14
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Keeps a WebView cookie session durable (happyvertical/smrt#5077).
|
|
18
|
+
*
|
|
19
|
+
* Origins whose cookies are watched, deduplicated: the origin of the current
|
|
20
|
+
* page, the origin of Capacitor's {@code server.url}, and any extra origins
|
|
21
|
+
* listed in this plugin's config ({@code plugins.SmrtSession.origins}). A shell
|
|
22
|
+
* that serves the app from https://localhost but signs in against a remote
|
|
23
|
+
* origin must list that origin or set server.url. Cookies of any other origin
|
|
24
|
+
* are flushed only on pause, stop and destroy.
|
|
25
|
+
*
|
|
26
|
+
* Android's WebView holds new cookies in memory and writes them to disk on its
|
|
27
|
+
* own schedule (about every 30 seconds), so a process killed soon after sign-in
|
|
28
|
+
* loses the session. A sign-in that sets its cookie from a fetch, as a
|
|
29
|
+
* SvelteKit form action does, triggers no page load and no lifecycle callback,
|
|
30
|
+
* and `am force-stop` of a foreground app runs none either. This plugin
|
|
31
|
+
* therefore watches the cookie jar of the current page while the app is
|
|
32
|
+
* resumed, flushes within POLL_MS of any change, and also flushes on pause,
|
|
33
|
+
* stop and destroy. It registers itself when the app runs `cap sync`; the web
|
|
34
|
+
* app needs no call. `flush()` is exposed for a page that wants a deterministic
|
|
35
|
+
* flush right after it sets a cookie.
|
|
36
|
+
*/
|
|
37
|
+
@CapacitorPlugin(name = "SmrtSession")
|
|
38
|
+
public class SmrtSessionPlugin extends Plugin {
|
|
39
|
+
|
|
40
|
+
static final long POLL_MS = 500;
|
|
41
|
+
|
|
42
|
+
private final Handler handler = new Handler(Looper.getMainLooper());
|
|
43
|
+
private final CookieFlushPolicy policy = new CookieFlushPolicy();
|
|
44
|
+
private boolean polling = false;
|
|
45
|
+
|
|
46
|
+
private final Runnable poll = new Runnable() {
|
|
47
|
+
@Override
|
|
48
|
+
public void run() {
|
|
49
|
+
if (!polling) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
flushIfChanged();
|
|
53
|
+
handler.postDelayed(this, POLL_MS);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
@Override
|
|
58
|
+
public void load() {
|
|
59
|
+
Logger.info(getLogTag(), "session cookie flush active");
|
|
60
|
+
startPolling();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@Override
|
|
64
|
+
protected void handleOnResume() {
|
|
65
|
+
startPolling();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@Override
|
|
69
|
+
protected void handleOnPause() {
|
|
70
|
+
stopPolling();
|
|
71
|
+
flushNow();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@Override
|
|
75
|
+
protected void handleOnStop() {
|
|
76
|
+
flushNow();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Override
|
|
80
|
+
protected void handleOnDestroy() {
|
|
81
|
+
stopPolling();
|
|
82
|
+
flushNow();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@PluginMethod
|
|
86
|
+
public void flush(PluginCall call) {
|
|
87
|
+
flushNow();
|
|
88
|
+
call.resolve();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private void startPolling() {
|
|
92
|
+
if (polling) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
polling = true;
|
|
96
|
+
handler.post(poll);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private void stopPolling() {
|
|
100
|
+
polling = false;
|
|
101
|
+
handler.removeCallbacks(poll);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private Set<String> watchedOrigins() {
|
|
105
|
+
List<String> urls = new ArrayList<>();
|
|
106
|
+
WebView webView = getBridge() == null ? null : getBridge().getWebView();
|
|
107
|
+
if (webView != null) {
|
|
108
|
+
urls.add(webView.getUrl());
|
|
109
|
+
}
|
|
110
|
+
if (getBridge() != null && getBridge().getConfig() != null) {
|
|
111
|
+
urls.add(getBridge().getConfig().getServerUrl());
|
|
112
|
+
}
|
|
113
|
+
String[] extra = getConfig().getArray("origins");
|
|
114
|
+
if (extra != null) {
|
|
115
|
+
for (String o : extra) {
|
|
116
|
+
urls.add(o);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return CookieFlushPolicy.origins(urls);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private void flushIfChanged() {
|
|
123
|
+
CookieManager cookies = CookieManager.getInstance();
|
|
124
|
+
String snapshot = CookieFlushPolicy.snapshot(watchedOrigins(), cookies::getCookie);
|
|
125
|
+
if (policy.shouldFlush(snapshot)) {
|
|
126
|
+
cookies.flush();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private void flushNow() {
|
|
131
|
+
CookieManager.getInstance().flush();
|
|
132
|
+
policy.reset();
|
|
133
|
+
}
|
|
134
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@happyvertical/smrt-capacitor-session",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.62.5",
|
|
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
|
+
"author": "HappyVertical",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"files": [
|
|
9
|
+
"android/src/main/",
|
|
10
|
+
"android/build.gradle",
|
|
11
|
+
"CLAUDE.md",
|
|
12
|
+
"AGENTS.md"
|
|
13
|
+
],
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@capacitor/core": ">=8.0.0"
|
|
16
|
+
},
|
|
17
|
+
"capacitor": {
|
|
18
|
+
"android": {
|
|
19
|
+
"src": "android"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"capacitor",
|
|
24
|
+
"plugin",
|
|
25
|
+
"android",
|
|
26
|
+
"session"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"registry": "https://registry.npmjs.org",
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/happyvertical/smrt.git",
|
|
35
|
+
"directory": "packages/smrt-capacitor-session"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "node scripts/validate-plugin.mjs",
|
|
39
|
+
"test": "node scripts/validate-plugin.mjs --test",
|
|
40
|
+
"typecheck": "node scripts/validate-plugin.mjs",
|
|
41
|
+
"clean": "rm -rf test/out"
|
|
42
|
+
}
|
|
6
43
|
}
|