@livx.cc/native-kit 0.17.0 → 0.19.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.
- package/package.json +1 -1
- package/src/core/NativeKit.ts +4 -0
- package/src/core/types.ts +3 -0
- package/src/core/web-adapter.ts +1 -0
- package/src/index.ts +1 -0
- package/src/modules/billing/billing.ts +20 -0
- package/src/modules/updates.ts +167 -0
package/package.json
CHANGED
package/src/core/NativeKit.ts
CHANGED
|
@@ -26,6 +26,7 @@ import { ShareModule } from '../modules/share';
|
|
|
26
26
|
import { StorageModule } from '../modules/storage';
|
|
27
27
|
import { ToastModule } from '../modules/toast';
|
|
28
28
|
import { UiModule } from '../modules/ui';
|
|
29
|
+
import { UpdatesModule } from '../modules/updates';
|
|
29
30
|
|
|
30
31
|
export class NativeKitOptions {
|
|
31
32
|
/** Priority order; first adapter whose detect() passes wins. */
|
|
@@ -91,6 +92,7 @@ export class NativeKit {
|
|
|
91
92
|
public readonly browser = new BrowserModule(this);
|
|
92
93
|
public readonly oauth = new OAuthModule(this);
|
|
93
94
|
public readonly billing = new BillingModule(this);
|
|
95
|
+
public readonly updates = new UpdatesModule(this);
|
|
94
96
|
|
|
95
97
|
public handshakeInfo: Handshake | null = null;
|
|
96
98
|
public options: NativeKitOptions;
|
|
@@ -121,6 +123,8 @@ export class NativeKit {
|
|
|
121
123
|
);
|
|
122
124
|
}
|
|
123
125
|
this.handshakeInfo = handshake;
|
|
126
|
+
// Zero-config: a native server-loader app begins polling for remote updates.
|
|
127
|
+
this.updates.__autostart();
|
|
124
128
|
return this.handshakeInfo;
|
|
125
129
|
})();
|
|
126
130
|
}
|
package/src/core/types.ts
CHANGED
|
@@ -14,6 +14,9 @@ export interface AppInfo {
|
|
|
14
14
|
name: string;
|
|
15
15
|
version: string;
|
|
16
16
|
build?: string;
|
|
17
|
+
/** How the shell loads content: 'app'/'file' (bundled) or 'server' (remote URL). Drives
|
|
18
|
+
* remote-update detection (only meaningful for 'server'). Absent on web/older shells. */
|
|
19
|
+
loader?: 'app' | 'file' | 'server';
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
export interface Handshake {
|
package/src/core/web-adapter.ts
CHANGED
|
@@ -337,6 +337,7 @@ export class WebAdapter implements NativeKitAdapter {
|
|
|
337
337
|
case 'billing.purchase':
|
|
338
338
|
case 'billing.restore':
|
|
339
339
|
case 'billing.entitlements':
|
|
340
|
+
case 'billing.appReceipt':
|
|
340
341
|
case 'billing.manageSubscriptions':
|
|
341
342
|
throw new KitError('UNSUPPORTED', 'No in-app purchases on web — use a web checkout');
|
|
342
343
|
|
package/src/index.ts
CHANGED
|
@@ -31,6 +31,7 @@ export type { AudioMode, MediaDeviceLite } from './modules/media';
|
|
|
31
31
|
export type { NetworkStatus } from './modules/network';
|
|
32
32
|
export type { ActionOptions, AlertOptions, ConfirmOptions, SafeAreaInsets } from './modules/ui';
|
|
33
33
|
export type { MotionSample } from './modules/motion';
|
|
34
|
+
export type { UpdateStatus, UpdatesOptions } from './modules/updates';
|
|
34
35
|
export type { PickedContact } from './modules/contacts';
|
|
35
36
|
export type { CalendarEventOptions } from './modules/calendar';
|
|
36
37
|
export type { BrowserOptions } from './modules/browser';
|
|
@@ -79,6 +79,26 @@ export class BillingModule {
|
|
|
79
79
|
return out;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Confirm entitlements **silently** from the on-device App Store receipt — no Apple-ID
|
|
84
|
+
* prompt and no `restore()` round-trip (unlike {@link restore}/{@link entitlements} on
|
|
85
|
+
* StoreKit 1). For an in-place update of the SAME bundle id the receipt already holds the
|
|
86
|
+
* user's active/grandfathered subscriptions, so this is the zero-user-action path for
|
|
87
|
+
* migrating existing subscribers into a new build.
|
|
88
|
+
*
|
|
89
|
+
* Native iOS only, and only meaningful with a **server validator** (the bare receipt has no
|
|
90
|
+
* product id — a client-trusted validator can't interpret it). Resolves `[]` on the web,
|
|
91
|
+
* on Android (no StoreKit-1 receipt), or when no receipt is present (e.g. a dev build).
|
|
92
|
+
*/
|
|
93
|
+
async entitlementsFromReceipt(): Promise<Entitlement[]> {
|
|
94
|
+
await this.kit.ready();
|
|
95
|
+
if (this.capability !== 'native') return []; // web, or a shell with no native store handler (Android today)
|
|
96
|
+
if (this.validator instanceof ClientTrustedValidator) return []; // bare receipt has no product id → would grant a bogus active entitlement; needs a server validator
|
|
97
|
+
const receipt = await this.kit.invoke<PurchaseReceipt>('billing.appReceipt');
|
|
98
|
+
if (!receipt?.appReceipt) return [];
|
|
99
|
+
return this.validator.validate(receipt); // server-of-record turns the receipt into entitlements
|
|
100
|
+
}
|
|
101
|
+
|
|
82
102
|
/** Current entitlements from the configured server-of-record (web provider or validator). */
|
|
83
103
|
async entitlements(): Promise<Entitlement[]> {
|
|
84
104
|
await this.kit.ready();
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { NativeKit } from '../core/NativeKit';
|
|
2
|
+
import type { Unsubscribe } from '../core/types';
|
|
3
|
+
|
|
4
|
+
export interface UpdateStatus {
|
|
5
|
+
/** Version the running bundle BOOTED with (embedded), or '' if it couldn't be determined. */
|
|
6
|
+
current: string;
|
|
7
|
+
/** Version advertised by the live manifest, or '' if the fetch failed. */
|
|
8
|
+
latest: string;
|
|
9
|
+
/** Optional build identifier from the manifest. */
|
|
10
|
+
build?: string | number;
|
|
11
|
+
/** True only when both versions are known AND differ — never on an unknown `current`. */
|
|
12
|
+
updateAvailable: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface UpdatesOptions {
|
|
16
|
+
/** Version-manifest URL — a tiny JSON `{ version: string, build?: string|number }`.
|
|
17
|
+
* Default: `${location.origin}/version.json`. */
|
|
18
|
+
manifestUrl?: string;
|
|
19
|
+
/** Poll cadence (ms). Always also checks on app resume. 0 = resume-only. Default 60_000. */
|
|
20
|
+
pollIntervalMs?: number;
|
|
21
|
+
/** Version the running bundle booted with. Default: `window.__APP_VERSION__` →
|
|
22
|
+
* `<meta name="app-version">`. Supplying the bundle's OWN embedded version is what
|
|
23
|
+
* makes the prompt trustworthy (see class doc). */
|
|
24
|
+
currentVersion?: string;
|
|
25
|
+
/** Auto-show a persistent native "tap to reload" banner when an update is detected. Default true. */
|
|
26
|
+
autoPrompt?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Remote-update detection for `loader:'server'` apps.
|
|
31
|
+
*
|
|
32
|
+
* Polls a version manifest and compares it to the version the running bundle **booted with**
|
|
33
|
+
* (its embedded `__APP_VERSION__`), NOT to a previous manifest read. That distinction is the
|
|
34
|
+
* whole point: comparing manifest-to-manifest gives a phantom "update available" the moment a
|
|
35
|
+
* deploy bumps the manifest but ships the old bundle (a real bug we've hit). Embedded-vs-manifest
|
|
36
|
+
* only fires when the bytes actually running are behind the deploy.
|
|
37
|
+
*
|
|
38
|
+
* Auto-starts for server-loader native apps (zero app code); apps can also drive it manually via
|
|
39
|
+
* {@link check}/{@link onAvailable}/{@link reload} or re-{@link start} with their own options.
|
|
40
|
+
*/
|
|
41
|
+
export class UpdatesModule {
|
|
42
|
+
private started = false;
|
|
43
|
+
private opts: Required<UpdatesOptions> | null = null;
|
|
44
|
+
private timer: ReturnType<typeof setInterval> | null = null;
|
|
45
|
+
private unsubs: Unsubscribe[] = [];
|
|
46
|
+
private listeners = new Set<(s: UpdateStatus) => void>();
|
|
47
|
+
/** The auto banner is shown at most once per JS session — so a dismissed prompt doesn't nag on
|
|
48
|
+
* every poll. A reload (the intended response) boots a fresh bundle/context and resets this; until
|
|
49
|
+
* then, {@link onAvailable} listeners still fire each check so an app can re-surface it itself. */
|
|
50
|
+
private prompted = false;
|
|
51
|
+
|
|
52
|
+
constructor(private kit: NativeKit) {}
|
|
53
|
+
|
|
54
|
+
get capability() {
|
|
55
|
+
return this.kit.capability('updates');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Fetch the manifest and compare to the boot version. Reports the result up to the shell
|
|
59
|
+
* (for the dev-menu App Info screen) and fires {@link onAvailable} listeners on a fresh update. */
|
|
60
|
+
async check(): Promise<UpdateStatus> {
|
|
61
|
+
const o = this.opts ?? this.resolveOptions();
|
|
62
|
+
const current = o.currentVersion;
|
|
63
|
+
let latest = '';
|
|
64
|
+
let build: string | number | undefined;
|
|
65
|
+
try {
|
|
66
|
+
// `no-store` skips the HTTP cache — but a cache-first service worker would still intercept and
|
|
67
|
+
// serve a stale manifest, so the SW recipe must let `version.json` pass through to the network.
|
|
68
|
+
const res = await fetch(o.manifestUrl, { cache: 'no-store' });
|
|
69
|
+
if (res.ok) {
|
|
70
|
+
const j = (await res.json()) as { version?: string; build?: string | number };
|
|
71
|
+
latest = String(j?.version ?? '');
|
|
72
|
+
build = j?.build;
|
|
73
|
+
}
|
|
74
|
+
} catch (e) {
|
|
75
|
+
// Offline or no manifest deployed — not fatal; log and report "unknown latest".
|
|
76
|
+
console.warn('[updates] manifest fetch failed:', (e as Error)?.message ?? e);
|
|
77
|
+
}
|
|
78
|
+
const updateAvailable = !!current && !!latest && current !== latest;
|
|
79
|
+
const status: UpdateStatus = { current, latest, build, updateAvailable };
|
|
80
|
+
|
|
81
|
+
// Surface to native so the shake → App Info screen can show "got the latest?".
|
|
82
|
+
this.kit.invoke('app.reportWebVersion', status).catch((e) =>
|
|
83
|
+
console.warn('[updates] reportWebVersion failed:', (e as Error)?.message ?? e)
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
if (updateAvailable) {
|
|
87
|
+
this.listeners.forEach((cb) => cb(status));
|
|
88
|
+
if (o.autoPrompt && !this.prompted) {
|
|
89
|
+
this.prompted = true;
|
|
90
|
+
this.kit
|
|
91
|
+
.invoke('toast.banner', { id: 'appwrap.update', message: 'New version available — tap to reload' })
|
|
92
|
+
.catch((e) => console.warn('[updates] banner failed:', (e as Error)?.message ?? e));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return status;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Notify whenever a manifest check finds the running bundle is behind. */
|
|
99
|
+
onAvailable(cb: (s: UpdateStatus) => void): Unsubscribe {
|
|
100
|
+
this.listeners.add(cb);
|
|
101
|
+
return () => this.listeners.delete(cb);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Hard reload the WebView, bypassing cache (iOS `reloadFromOrigin`, Android cache-clear + reload). */
|
|
105
|
+
reload(): Promise<void> {
|
|
106
|
+
return this.kit.invoke('app.reload');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Begin polling + resume/banner wiring. Idempotent; re-calling replaces options. */
|
|
110
|
+
start(options?: UpdatesOptions): void {
|
|
111
|
+
this.opts = this.resolveOptions(options);
|
|
112
|
+
if (this.started) {
|
|
113
|
+
this.stop(/* keepStarted */ true);
|
|
114
|
+
}
|
|
115
|
+
this.started = true;
|
|
116
|
+
// Re-check whenever the app returns to the foreground (catches a deploy made while backgrounded).
|
|
117
|
+
this.unsubs.push(this.kit.on('app.resume', () => void this.check()));
|
|
118
|
+
// Banner tap (or any 'appwrap.update' action) → reload.
|
|
119
|
+
this.unsubs.push(
|
|
120
|
+
this.kit.on('toast.action', (p) => {
|
|
121
|
+
if ((p as { id?: string })?.id === 'appwrap.update') void this.reload();
|
|
122
|
+
})
|
|
123
|
+
);
|
|
124
|
+
if (this.opts.pollIntervalMs > 0) {
|
|
125
|
+
this.timer = setInterval(() => void this.check(), this.opts.pollIntervalMs);
|
|
126
|
+
}
|
|
127
|
+
void this.check();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
stop(keepStarted = false): void {
|
|
131
|
+
if (this.timer) clearInterval(this.timer);
|
|
132
|
+
this.timer = null;
|
|
133
|
+
this.unsubs.forEach((u) => u());
|
|
134
|
+
this.unsubs = [];
|
|
135
|
+
if (!keepStarted) this.started = false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Called by NativeKit.ready() — self-starts only for a native server-loader app. */
|
|
139
|
+
__autostart(): void {
|
|
140
|
+
if (this.started) return;
|
|
141
|
+
if (this.kit.is.native && this.kit.handshakeInfo?.app?.loader === 'server') this.start();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private resolveOptions(o?: UpdatesOptions): Required<UpdatesOptions> {
|
|
145
|
+
const origin = typeof location !== 'undefined' ? location.origin : '';
|
|
146
|
+
return {
|
|
147
|
+
manifestUrl: o?.manifestUrl ?? `${origin}/version.json`,
|
|
148
|
+
pollIntervalMs: o?.pollIntervalMs ?? 60_000,
|
|
149
|
+
currentVersion: o?.currentVersion ?? bootVersion(),
|
|
150
|
+
autoPrompt: o?.autoPrompt ?? true,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Embedded version the running bundle shipped with: `window.__APP_VERSION__`, then a
|
|
156
|
+
* `<meta name="app-version">` tag. '' (unknown) when neither is present — in which case we never
|
|
157
|
+
* raise a false update prompt. */
|
|
158
|
+
function bootVersion(): string {
|
|
159
|
+
const g = typeof window !== 'undefined' ? (window as any).__APP_VERSION__ : undefined;
|
|
160
|
+
if (g) return String(g);
|
|
161
|
+
if (typeof document !== 'undefined') {
|
|
162
|
+
const meta = document.querySelector('meta[name="app-version"]');
|
|
163
|
+
const c = meta?.getAttribute('content');
|
|
164
|
+
if (c) return c;
|
|
165
|
+
}
|
|
166
|
+
return '';
|
|
167
|
+
}
|