@aramiworks/app-updates 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +118 -2
- package/android/build.gradle +27 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/so/arami/appupdates/AppUpdatesModule.kt +123 -0
- package/app.plugin.js +1 -0
- package/build/cache.d.ts +12 -0
- package/build/cache.js +104 -0
- package/build/env.d.ts +3 -0
- package/build/env.js +53 -0
- package/build/index.d.ts +8 -0
- package/build/index.js +13 -0
- package/build/native.d.ts +56 -0
- package/build/native.js +159 -0
- package/build/openStore.d.ts +20 -0
- package/build/openStore.js +49 -0
- package/build/ota.d.ts +33 -0
- package/build/ota.js +102 -0
- package/build/sources/appStore.d.ts +15 -0
- package/build/sources/appStore.js +26 -0
- package/build/storeCheck.d.ts +40 -0
- package/build/storeCheck.js +112 -0
- package/build/tester.d.ts +22 -0
- package/build/tester.js +78 -0
- package/build/throttle.d.ts +3 -0
- package/build/throttle.js +11 -0
- package/build/types.d.ts +7 -0
- package/build/types.js +2 -0
- package/build/useAppUpdate.d.ts +3 -0
- package/build/useAppUpdate.js +7 -0
- package/build/useAppUpdateCore.d.ts +43 -0
- package/build/useAppUpdateCore.js +209 -0
- package/build/version.d.ts +13 -0
- package/build/version.js +32 -0
- package/expo-module.config.json +6 -0
- package/package.json +61 -4
- package/plugin/build/gradle.d.ts +16 -0
- package/plugin/build/gradle.js +172 -0
- package/plugin/build/index.d.ts +16 -0
- package/plugin/build/index.js +28 -0
package/build/native.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isFirebaseAvailable = isFirebaseAvailable;
|
|
4
|
+
exports.getDistribution = getDistribution;
|
|
5
|
+
exports.getPlayUpdateInfo = getPlayUpdateInfo;
|
|
6
|
+
exports.startPlayImmediateUpdate = startPlayImmediateUpdate;
|
|
7
|
+
exports.isTesterSignedIn = isTesterSignedIn;
|
|
8
|
+
exports.signInTester = signInTester;
|
|
9
|
+
exports.checkFirebaseRelease = checkFirebaseRelease;
|
|
10
|
+
exports.installFirebaseRelease = installFirebaseRelease;
|
|
11
|
+
const expo_1 = require("expo");
|
|
12
|
+
const react_native_1 = require("react-native");
|
|
13
|
+
const NO_PLAY_UPDATE = {
|
|
14
|
+
updateAvailable: false,
|
|
15
|
+
availableVersionCode: 0,
|
|
16
|
+
immediateAllowed: false,
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The Android native module, or null off Android or when it isn't linked
|
|
20
|
+
* (Expo Go, tests). Looked up per call so nothing native loads on iOS or web.
|
|
21
|
+
*/
|
|
22
|
+
function nativeModule() {
|
|
23
|
+
if (react_native_1.Platform.OS !== 'android')
|
|
24
|
+
return null;
|
|
25
|
+
return (0, expo_1.requireOptionalNativeModule)('AppUpdates');
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The app's optional `@react-native-firebase/app-distribution` install (iOS only),
|
|
29
|
+
* or null when it is absent or not usable. Resolved lazily at call time; supports
|
|
30
|
+
* the modular API (v22+) and the namespaced default export.
|
|
31
|
+
*/
|
|
32
|
+
function loadRnfbAppDistribution() {
|
|
33
|
+
if (react_native_1.Platform.OS !== 'ios')
|
|
34
|
+
return null;
|
|
35
|
+
try {
|
|
36
|
+
const rnfb = require('@react-native-firebase/app-distribution');
|
|
37
|
+
if (typeof rnfb?.getAppDistribution === 'function') {
|
|
38
|
+
const instance = rnfb.getAppDistribution();
|
|
39
|
+
return {
|
|
40
|
+
isTesterSignedIn: () => rnfb.isTesterSignedIn(instance),
|
|
41
|
+
signInTester: () => rnfb.signInTester(instance),
|
|
42
|
+
checkForUpdate: () => rnfb.checkForUpdate(instance),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
if (typeof rnfb?.default === 'function')
|
|
46
|
+
return rnfb.default();
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Whether Firebase App Distribution can work in this build: the Android native
|
|
55
|
+
* module is linked, or on iOS the app installed `@react-native-firebase/app-distribution`.
|
|
56
|
+
* False on web and in Expo Go.
|
|
57
|
+
*/
|
|
58
|
+
function isFirebaseAvailable() {
|
|
59
|
+
if (react_native_1.Platform.OS === 'android')
|
|
60
|
+
return nativeModule() !== null;
|
|
61
|
+
if (react_native_1.Platform.OS === 'ios')
|
|
62
|
+
return loadRnfbAppDistribution() !== null;
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The distribution channel from the `app_updates_distribution` string resource
|
|
67
|
+
* the config plugin writes per Gradle flavor. Defaults to "play" on Android
|
|
68
|
+
* (resource absent or unknown) and is "none" on iOS and web.
|
|
69
|
+
*/
|
|
70
|
+
function getDistribution() {
|
|
71
|
+
if (react_native_1.Platform.OS !== 'android')
|
|
72
|
+
return 'none';
|
|
73
|
+
const value = nativeModule()?.getDistribution();
|
|
74
|
+
return value === 'firebase' ? 'firebase' : 'play';
|
|
75
|
+
}
|
|
76
|
+
/** Google Play in-app update availability. Nothing available off Android. */
|
|
77
|
+
async function getPlayUpdateInfo() {
|
|
78
|
+
const mod = nativeModule();
|
|
79
|
+
if (!mod)
|
|
80
|
+
return { ...NO_PLAY_UPDATE };
|
|
81
|
+
return mod.getPlayUpdateInfo();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Starts Play's immediate (full-screen) update flow from the current activity.
|
|
85
|
+
* Resolves false when it couldn't start (no activity, no update, or immediate
|
|
86
|
+
* updates not allowed).
|
|
87
|
+
*/
|
|
88
|
+
async function startPlayImmediateUpdate() {
|
|
89
|
+
const mod = nativeModule();
|
|
90
|
+
if (!mod)
|
|
91
|
+
return false;
|
|
92
|
+
return mod.startPlayImmediateUpdate();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Firebase calls are meaningful only on the `firebase` distribution. In the
|
|
96
|
+
* `play` flavor the Android API-only artifact reports `isTesterSignedIn` false
|
|
97
|
+
* and the other calls reject ("API not implemented"), so callers must use
|
|
98
|
+
* them only when `getDistribution()` is `firebase` (or on iOS). On iOS they use
|
|
99
|
+
* the app's optional `@react-native-firebase/app-distribution` install and
|
|
100
|
+
* return "nothing" values when it is absent. Errors propagate.
|
|
101
|
+
*/
|
|
102
|
+
/** Whether a Firebase App Distribution tester is signed in on this device. */
|
|
103
|
+
async function isTesterSignedIn() {
|
|
104
|
+
const rnfb = loadRnfbAppDistribution();
|
|
105
|
+
if (rnfb)
|
|
106
|
+
return rnfb.isTesterSignedIn();
|
|
107
|
+
const mod = nativeModule();
|
|
108
|
+
if (!mod)
|
|
109
|
+
return false;
|
|
110
|
+
return mod.isTesterSignedIn();
|
|
111
|
+
}
|
|
112
|
+
/** Opens the Firebase App Distribution tester sign-in flow. */
|
|
113
|
+
async function signInTester() {
|
|
114
|
+
const rnfb = loadRnfbAppDistribution();
|
|
115
|
+
if (rnfb) {
|
|
116
|
+
await rnfb.signInTester();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const mod = nativeModule();
|
|
120
|
+
if (!mod)
|
|
121
|
+
return;
|
|
122
|
+
await mod.signInTester();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The newest Firebase App Distribution release, or null when this build is the newest.
|
|
126
|
+
* Only meaningful on the `firebase` distribution (see above).
|
|
127
|
+
*
|
|
128
|
+
* Call this only after `isTesterSignedIn()` resolves true: when no tester is
|
|
129
|
+
* signed in, the native `checkForNewRelease` opens the sign-in flow on its own.
|
|
130
|
+
* This wrapper does not guard against that; callers must.
|
|
131
|
+
*/
|
|
132
|
+
async function checkFirebaseRelease() {
|
|
133
|
+
const rnfb = loadRnfbAppDistribution();
|
|
134
|
+
if (rnfb) {
|
|
135
|
+
const release = await rnfb.checkForUpdate();
|
|
136
|
+
return release?.displayVersion ? { displayVersion: release.displayVersion } : null;
|
|
137
|
+
}
|
|
138
|
+
const mod = nativeModule();
|
|
139
|
+
if (!mod)
|
|
140
|
+
return null;
|
|
141
|
+
return mod.checkFirebaseRelease();
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Installs the newest Firebase App Distribution release: downloads it on Android,
|
|
145
|
+
* opens its download URL on iOS. Only meaningful on the `firebase` distribution.
|
|
146
|
+
*/
|
|
147
|
+
async function installFirebaseRelease() {
|
|
148
|
+
const rnfb = loadRnfbAppDistribution();
|
|
149
|
+
if (rnfb) {
|
|
150
|
+
const release = await rnfb.checkForUpdate();
|
|
151
|
+
if (release?.downloadURL)
|
|
152
|
+
await react_native_1.Linking.openURL(release.downloadURL);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const mod = nativeModule();
|
|
156
|
+
if (!mod)
|
|
157
|
+
return;
|
|
158
|
+
await mod.installFirebaseRelease();
|
|
159
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { StoreUpdate } from './types';
|
|
2
|
+
export interface OpenStoreDeps {
|
|
3
|
+
openURL: (url: string) => Promise<unknown>;
|
|
4
|
+
startPlayImmediateUpdate: () => Promise<boolean>;
|
|
5
|
+
installFirebaseRelease: () => Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Opens the update for the given source.
|
|
9
|
+
*
|
|
10
|
+
* - `app-store`: opens `update.url`; does nothing when it is null.
|
|
11
|
+
* - `play`: starts Google's immediate update flow. A `true` result only means the
|
|
12
|
+
* flow STARTED; its outcome isn't observed here (the hook re-checks when the app
|
|
13
|
+
* returns to the foreground). When it resolves `false` or throws, falls back to
|
|
14
|
+
* opening `update.url` (the Play Store listing), if any.
|
|
15
|
+
* - `firebase`: installs the newest Firebase App Distribution release.
|
|
16
|
+
*
|
|
17
|
+
* Errors from `openURL` and `installFirebaseRelease` propagate to the caller; the
|
|
18
|
+
* button handler shows nothing new and the gate stays up.
|
|
19
|
+
*/
|
|
20
|
+
export declare function openStore(update: StoreUpdate, deps?: Partial<OpenStoreDeps>): Promise<void>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.openStore = openStore;
|
|
4
|
+
const react_native_1 = require("react-native");
|
|
5
|
+
const native_1 = require("./native");
|
|
6
|
+
/**
|
|
7
|
+
* Opens the update for the given source.
|
|
8
|
+
*
|
|
9
|
+
* - `app-store`: opens `update.url`; does nothing when it is null.
|
|
10
|
+
* - `play`: starts Google's immediate update flow. A `true` result only means the
|
|
11
|
+
* flow STARTED; its outcome isn't observed here (the hook re-checks when the app
|
|
12
|
+
* returns to the foreground). When it resolves `false` or throws, falls back to
|
|
13
|
+
* opening `update.url` (the Play Store listing), if any.
|
|
14
|
+
* - `firebase`: installs the newest Firebase App Distribution release.
|
|
15
|
+
*
|
|
16
|
+
* Errors from `openURL` and `installFirebaseRelease` propagate to the caller; the
|
|
17
|
+
* button handler shows nothing new and the gate stays up.
|
|
18
|
+
*/
|
|
19
|
+
async function openStore(update, deps = {}) {
|
|
20
|
+
const d = {
|
|
21
|
+
openURL: (url) => react_native_1.Linking.openURL(url),
|
|
22
|
+
startPlayImmediateUpdate: native_1.startPlayImmediateUpdate,
|
|
23
|
+
installFirebaseRelease: native_1.installFirebaseRelease,
|
|
24
|
+
...deps,
|
|
25
|
+
};
|
|
26
|
+
const openUrl = async () => {
|
|
27
|
+
if (update.url)
|
|
28
|
+
await d.openURL(update.url);
|
|
29
|
+
};
|
|
30
|
+
switch (update.source) {
|
|
31
|
+
case 'app-store':
|
|
32
|
+
await openUrl();
|
|
33
|
+
return;
|
|
34
|
+
case 'play': {
|
|
35
|
+
let started = false;
|
|
36
|
+
try {
|
|
37
|
+
started = await d.startPlayImmediateUpdate();
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
started = false;
|
|
41
|
+
}
|
|
42
|
+
if (!started)
|
|
43
|
+
await openUrl();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
case 'firebase':
|
|
47
|
+
await d.installFirebaseRelease();
|
|
48
|
+
}
|
|
49
|
+
}
|
package/build/ota.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Total budget for the cold-start check + download. */
|
|
2
|
+
export declare const COLD_START_OTA_LIMIT_MS = 10000;
|
|
3
|
+
/** The subset of `expo-updates` this module uses (explicit so tests can fake it). */
|
|
4
|
+
export interface UpdatesApi {
|
|
5
|
+
isEnabled: boolean;
|
|
6
|
+
checkForUpdateAsync(): Promise<{
|
|
7
|
+
isAvailable: boolean;
|
|
8
|
+
}>;
|
|
9
|
+
fetchUpdateAsync(): Promise<{
|
|
10
|
+
isNew: boolean;
|
|
11
|
+
manifest?: {
|
|
12
|
+
id?: string;
|
|
13
|
+
} | object;
|
|
14
|
+
}>;
|
|
15
|
+
reloadAsync(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export interface OtaDeps {
|
|
18
|
+
updates?: UpdatesApi;
|
|
19
|
+
}
|
|
20
|
+
export type ColdStartOtaResult = 'reloading' | 'none' | 'timed-out' | 'failed';
|
|
21
|
+
/**
|
|
22
|
+
* Mandatory cold-start OTA: check + download within COLD_START_OTA_LIMIT_MS, then reload.
|
|
23
|
+
* After a timeout it never reloads, even if the download later finishes.
|
|
24
|
+
*/
|
|
25
|
+
export declare function runColdStartOta(deps?: OtaDeps): Promise<ColdStartOtaResult>;
|
|
26
|
+
/**
|
|
27
|
+
* Optional warm OTA: check + download in the background; never reloads, never throws.
|
|
28
|
+
* `updateId` is the downloaded manifest's id (when present) so callers can act once per update.
|
|
29
|
+
*/
|
|
30
|
+
export declare function checkWarmOta(deps?: OtaDeps): Promise<{
|
|
31
|
+
ready: boolean;
|
|
32
|
+
updateId: string | null;
|
|
33
|
+
}>;
|
package/build/ota.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.COLD_START_OTA_LIMIT_MS = void 0;
|
|
37
|
+
exports.runColdStartOta = runColdStartOta;
|
|
38
|
+
exports.checkWarmOta = checkWarmOta;
|
|
39
|
+
const ExpoUpdates = __importStar(require("expo-updates"));
|
|
40
|
+
/** Total budget for the cold-start check + download. */
|
|
41
|
+
exports.COLD_START_OTA_LIMIT_MS = 10_000;
|
|
42
|
+
const defaultUpdates = () => ExpoUpdates;
|
|
43
|
+
async function checkAndFetch(updates) {
|
|
44
|
+
const check = await updates.checkForUpdateAsync();
|
|
45
|
+
if (!check.isAvailable)
|
|
46
|
+
return { downloaded: false, updateId: null };
|
|
47
|
+
const fetched = await updates.fetchUpdateAsync();
|
|
48
|
+
if (!fetched.isNew)
|
|
49
|
+
return { downloaded: false, updateId: null };
|
|
50
|
+
const id = fetched.manifest?.id;
|
|
51
|
+
return { downloaded: true, updateId: typeof id === 'string' ? id : null };
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Mandatory cold-start OTA: check + download within COLD_START_OTA_LIMIT_MS, then reload.
|
|
55
|
+
* After a timeout it never reloads, even if the download later finishes.
|
|
56
|
+
*/
|
|
57
|
+
async function runColdStartOta(deps = {}) {
|
|
58
|
+
const updates = deps.updates ?? defaultUpdates();
|
|
59
|
+
if (!updates.isEnabled)
|
|
60
|
+
return 'none';
|
|
61
|
+
let timer;
|
|
62
|
+
const work = checkAndFetch(updates).then((r) => r, () => 'failed');
|
|
63
|
+
const timeout = new Promise((resolve) => {
|
|
64
|
+
timer = setTimeout(() => resolve('timed-out'), exports.COLD_START_OTA_LIMIT_MS);
|
|
65
|
+
});
|
|
66
|
+
try {
|
|
67
|
+
const outcome = await Promise.race([work, timeout]);
|
|
68
|
+
if (outcome === 'timed-out')
|
|
69
|
+
return 'timed-out';
|
|
70
|
+
if (outcome === 'failed')
|
|
71
|
+
return 'failed';
|
|
72
|
+
if (!outcome.downloaded)
|
|
73
|
+
return 'none';
|
|
74
|
+
try {
|
|
75
|
+
await updates.reloadAsync();
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return 'failed';
|
|
79
|
+
}
|
|
80
|
+
return 'reloading';
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
if (timer !== undefined)
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Optional warm OTA: check + download in the background; never reloads, never throws.
|
|
89
|
+
* `updateId` is the downloaded manifest's id (when present) so callers can act once per update.
|
|
90
|
+
*/
|
|
91
|
+
async function checkWarmOta(deps = {}) {
|
|
92
|
+
const updates = deps.updates ?? defaultUpdates();
|
|
93
|
+
if (!updates.isEnabled)
|
|
94
|
+
return { ready: false, updateId: null };
|
|
95
|
+
try {
|
|
96
|
+
const r = await checkAndFetch(updates);
|
|
97
|
+
return { ready: r.downloaded, updateId: r.updateId };
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return { ready: false, updateId: null };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface AppStoreOptions {
|
|
2
|
+
bundleId: string;
|
|
3
|
+
country: string | null;
|
|
4
|
+
fetchFn?: typeof fetch;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Asks Apple's public iTunes lookup for the live App Store version.
|
|
9
|
+
* Returns null when the app is not listed (or the result is malformed);
|
|
10
|
+
* throws on HTTP, network or parse errors (the caller fails open).
|
|
11
|
+
*/
|
|
12
|
+
export declare function checkAppStore(opts: AppStoreOptions): Promise<{
|
|
13
|
+
version: string;
|
|
14
|
+
url: string;
|
|
15
|
+
} | null>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkAppStore = checkAppStore;
|
|
4
|
+
/**
|
|
5
|
+
* Asks Apple's public iTunes lookup for the live App Store version.
|
|
6
|
+
* Returns null when the app is not listed (or the result is malformed);
|
|
7
|
+
* throws on HTTP, network or parse errors (the caller fails open).
|
|
8
|
+
*/
|
|
9
|
+
async function checkAppStore(opts) {
|
|
10
|
+
const fetchFn = opts.fetchFn ?? fetch;
|
|
11
|
+
const country = (opts.country?.trim() || 'us').toLowerCase();
|
|
12
|
+
const url = `https://itunes.apple.com/lookup?bundleId=${encodeURIComponent(opts.bundleId)}` +
|
|
13
|
+
`&country=${encodeURIComponent(country)}`;
|
|
14
|
+
const res = await fetchFn(url, { signal: opts.signal });
|
|
15
|
+
if (!res.ok) {
|
|
16
|
+
throw new Error(`App Store lookup failed with HTTP ${res.status}`);
|
|
17
|
+
}
|
|
18
|
+
const body = (await res.json());
|
|
19
|
+
if (!body.resultCount)
|
|
20
|
+
return null;
|
|
21
|
+
const first = body.results?.[0];
|
|
22
|
+
if (!first || typeof first.version !== 'string' || typeof first.trackViewUrl !== 'string') {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return { version: first.version, url: first.trackViewUrl };
|
|
26
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type FirebaseRelease, type NativeDistribution, type PlayUpdateInfo } from './native';
|
|
2
|
+
import { type AppStoreOptions } from './sources/appStore';
|
|
3
|
+
import type { StoreUpdate } from './types';
|
|
4
|
+
/** Budget for one store lookup before it resolves `{ kind: 'error' }` (8 seconds). */
|
|
5
|
+
export declare const STORE_CHECK_TIMEOUT_MS = 8000;
|
|
6
|
+
export interface StoreCheckEnv {
|
|
7
|
+
platform: 'ios' | 'android' | 'web';
|
|
8
|
+
channel: string | null;
|
|
9
|
+
installed: string | null;
|
|
10
|
+
bundleId: string | null;
|
|
11
|
+
country: string | null;
|
|
12
|
+
distribution: NativeDistribution;
|
|
13
|
+
}
|
|
14
|
+
export interface StoreCheckDeps {
|
|
15
|
+
checkAppStore: (opts: AppStoreOptions) => Promise<{
|
|
16
|
+
version: string;
|
|
17
|
+
url: string;
|
|
18
|
+
} | null>;
|
|
19
|
+
getPlayUpdateInfo: () => Promise<PlayUpdateInfo>;
|
|
20
|
+
isFirebaseAvailable: () => boolean;
|
|
21
|
+
isTesterSignedIn: () => Promise<boolean>;
|
|
22
|
+
checkFirebaseRelease: () => Promise<FirebaseRelease | null>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* `update`: a newer version exists. `none`: the source answered definitively that nothing
|
|
26
|
+
* newer exists. `error`: unknown (throw, timeout, missing input, or Firebase can't be verified).
|
|
27
|
+
*/
|
|
28
|
+
export type StoreCheckResult = {
|
|
29
|
+
kind: 'update';
|
|
30
|
+
update: StoreUpdate;
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'none';
|
|
33
|
+
} | {
|
|
34
|
+
kind: 'error';
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Checks the store this build ships through for a newer major.minor.
|
|
38
|
+
* Never throws: errors and an 8 s timeout resolve `{ kind: 'error' }`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function checkStoreUpdate(env: StoreCheckEnv, deps?: Partial<StoreCheckDeps>): Promise<StoreCheckResult>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.STORE_CHECK_TIMEOUT_MS = void 0;
|
|
4
|
+
exports.checkStoreUpdate = checkStoreUpdate;
|
|
5
|
+
const native_1 = require("./native");
|
|
6
|
+
const appStore_1 = require("./sources/appStore");
|
|
7
|
+
const version_1 = require("./version");
|
|
8
|
+
/** Budget for one store lookup before it resolves `{ kind: 'error' }` (8 seconds). */
|
|
9
|
+
exports.STORE_CHECK_TIMEOUT_MS = 8_000;
|
|
10
|
+
const REAL_DEPS = {
|
|
11
|
+
checkAppStore: appStore_1.checkAppStore,
|
|
12
|
+
getPlayUpdateInfo: native_1.getPlayUpdateInfo,
|
|
13
|
+
isFirebaseAvailable: native_1.isFirebaseAvailable,
|
|
14
|
+
isTesterSignedIn: native_1.isTesterSignedIn,
|
|
15
|
+
checkFirebaseRelease: native_1.checkFirebaseRelease,
|
|
16
|
+
};
|
|
17
|
+
const NONE = { kind: 'none' };
|
|
18
|
+
const ERROR = { kind: 'error' };
|
|
19
|
+
/** A definitive candidate, a definitive "nothing" (null) or an unknown (ERROR). */
|
|
20
|
+
async function findCandidate(env, deps, signal) {
|
|
21
|
+
// Android develop builds are the firebase flavor; iOS reports distribution "none",
|
|
22
|
+
// so develop is the only iOS Firebase signal. Never call Firebase in the play flavor.
|
|
23
|
+
const isFirebase = env.channel === 'develop' || (env.platform === 'android' && env.distribution === 'firebase');
|
|
24
|
+
if (isFirebase) {
|
|
25
|
+
// Signed out, or no Firebase at all: we can't verify, so it is unknown, not "nothing".
|
|
26
|
+
if (!deps.isFirebaseAvailable())
|
|
27
|
+
return ERROR;
|
|
28
|
+
if (!(await deps.isTesterSignedIn()))
|
|
29
|
+
return ERROR;
|
|
30
|
+
const release = await deps.checkFirebaseRelease();
|
|
31
|
+
if (!release)
|
|
32
|
+
return null;
|
|
33
|
+
const version = (0, version_1.parseMajorMinor)(release.displayVersion);
|
|
34
|
+
return version ? { version, source: 'firebase', url: null } : ERROR;
|
|
35
|
+
}
|
|
36
|
+
if (env.platform === 'ios') {
|
|
37
|
+
if (!env.bundleId)
|
|
38
|
+
return ERROR;
|
|
39
|
+
const listing = await deps.checkAppStore({
|
|
40
|
+
bundleId: env.bundleId,
|
|
41
|
+
country: env.country,
|
|
42
|
+
signal,
|
|
43
|
+
});
|
|
44
|
+
if (!listing)
|
|
45
|
+
return null;
|
|
46
|
+
const version = (0, version_1.parseMajorMinor)(listing.version);
|
|
47
|
+
return version ? { version, source: 'app-store', url: listing.url } : ERROR;
|
|
48
|
+
}
|
|
49
|
+
if (env.platform === 'android') {
|
|
50
|
+
if (!env.bundleId)
|
|
51
|
+
return ERROR;
|
|
52
|
+
const info = await deps.getPlayUpdateInfo();
|
|
53
|
+
if (!info.updateAvailable)
|
|
54
|
+
return null;
|
|
55
|
+
const code = info.availableVersionCode;
|
|
56
|
+
if (!Number.isInteger(code) || code < 0)
|
|
57
|
+
return ERROR;
|
|
58
|
+
const { major, minor } = (0, version_1.decodeVersionCode)(code);
|
|
59
|
+
return {
|
|
60
|
+
version: { major, minor },
|
|
61
|
+
source: 'play',
|
|
62
|
+
url: `https://play.google.com/store/apps/details?id=${encodeURIComponent(env.bundleId)}`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Checks the store this build ships through for a newer major.minor.
|
|
69
|
+
* Never throws: errors and an 8 s timeout resolve `{ kind: 'error' }`.
|
|
70
|
+
*/
|
|
71
|
+
async function checkStoreUpdate(env, deps) {
|
|
72
|
+
try {
|
|
73
|
+
if (env.platform === 'web')
|
|
74
|
+
return NONE;
|
|
75
|
+
const installed = (0, version_1.parseMajorMinor)(env.installed);
|
|
76
|
+
if (!installed)
|
|
77
|
+
return ERROR;
|
|
78
|
+
const controller = new AbortController();
|
|
79
|
+
let timer;
|
|
80
|
+
const timeout = new Promise((resolve) => {
|
|
81
|
+
timer = setTimeout(() => {
|
|
82
|
+
controller.abort();
|
|
83
|
+
resolve(ERROR);
|
|
84
|
+
}, exports.STORE_CHECK_TIMEOUT_MS);
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
const candidate = await Promise.race([
|
|
88
|
+
findCandidate(env, { ...REAL_DEPS, ...deps }, controller.signal),
|
|
89
|
+
timeout,
|
|
90
|
+
]);
|
|
91
|
+
if (candidate && 'kind' in candidate)
|
|
92
|
+
return ERROR;
|
|
93
|
+
if (!candidate || !(0, version_1.isNewerMajorMinor)(installed, candidate.version))
|
|
94
|
+
return NONE;
|
|
95
|
+
return {
|
|
96
|
+
kind: 'update',
|
|
97
|
+
update: {
|
|
98
|
+
installed: (0, version_1.formatMajorMinor)(installed),
|
|
99
|
+
available: (0, version_1.formatMajorMinor)(candidate.version),
|
|
100
|
+
source: candidate.source,
|
|
101
|
+
url: candidate.url,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
clearTimeout(timer);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return ERROR;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { KeyValueStore } from './cache';
|
|
2
|
+
import type { StoreCheckEnv } from './storeCheck';
|
|
3
|
+
export declare const TESTER_DISMISSED_KEY = "aramiworks.app-updates.tester-dismissed-at";
|
|
4
|
+
export declare const TESTER_DISMISS_MS: number;
|
|
5
|
+
export interface TesterState {
|
|
6
|
+
/** null when unknown or not applicable. */
|
|
7
|
+
signedIn: boolean | null;
|
|
8
|
+
promptVisible: boolean;
|
|
9
|
+
signIn: () => Promise<void>;
|
|
10
|
+
dismissPrompt: () => void;
|
|
11
|
+
}
|
|
12
|
+
export declare const INERT_TESTER: TesterState;
|
|
13
|
+
/** The Firebase tester prompt only applies to Firebase-distributed builds. */
|
|
14
|
+
export declare function isFirebaseBuild(env: StoreCheckEnv): boolean;
|
|
15
|
+
interface TesterDeps {
|
|
16
|
+
store: KeyValueStore;
|
|
17
|
+
now: () => number;
|
|
18
|
+
isTesterSignedIn: () => Promise<boolean>;
|
|
19
|
+
signInTester: () => Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export declare function useTester(applicable: boolean, deps: TesterDeps, recheckStore: () => void, isMounted: () => boolean): TesterState;
|
|
22
|
+
export {};
|
package/build/tester.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.INERT_TESTER = exports.TESTER_DISMISS_MS = exports.TESTER_DISMISSED_KEY = void 0;
|
|
4
|
+
exports.isFirebaseBuild = isFirebaseBuild;
|
|
5
|
+
exports.useTester = useTester;
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
exports.TESTER_DISMISSED_KEY = 'aramiworks.app-updates.tester-dismissed-at';
|
|
8
|
+
exports.TESTER_DISMISS_MS = 7 * 24 * 60 * 60 * 1000;
|
|
9
|
+
exports.INERT_TESTER = {
|
|
10
|
+
signedIn: null,
|
|
11
|
+
promptVisible: false,
|
|
12
|
+
signIn: async () => { },
|
|
13
|
+
dismissPrompt: () => { },
|
|
14
|
+
};
|
|
15
|
+
/** The Firebase tester prompt only applies to Firebase-distributed builds. */
|
|
16
|
+
function isFirebaseBuild(env) {
|
|
17
|
+
if (env.platform === 'android') {
|
|
18
|
+
return env.distribution === 'firebase' || env.channel === 'develop';
|
|
19
|
+
}
|
|
20
|
+
return env.platform === 'ios' && env.channel === 'develop';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Dismissal counts while it is under 7 days old. Future or non-numeric timestamps
|
|
24
|
+
* count as expired. Expiry is judged when the hook loads (next launch), not live.
|
|
25
|
+
*/
|
|
26
|
+
async function loadDismissed(store, now) {
|
|
27
|
+
try {
|
|
28
|
+
const at = Number(await store.getItem(exports.TESTER_DISMISSED_KEY));
|
|
29
|
+
return Number.isFinite(at) && at > 0 && now - at >= 0 && now - at < exports.TESTER_DISMISS_MS;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function useTester(applicable, deps, recheckStore, isMounted) {
|
|
36
|
+
const [signedIn, setSignedIn] = (0, react_1.useState)(null);
|
|
37
|
+
const [dismissed, setDismissed] = (0, react_1.useState)(null);
|
|
38
|
+
const { store, now, isTesterSignedIn, signInTester } = deps;
|
|
39
|
+
const querySignedIn = (0, react_1.useCallback)(async () => {
|
|
40
|
+
try {
|
|
41
|
+
return await isTesterSignedIn();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}, [isTesterSignedIn]);
|
|
47
|
+
(0, react_1.useEffect)(() => {
|
|
48
|
+
if (!applicable)
|
|
49
|
+
return;
|
|
50
|
+
querySignedIn().then((v) => {
|
|
51
|
+
if (isMounted())
|
|
52
|
+
setSignedIn(v);
|
|
53
|
+
});
|
|
54
|
+
loadDismissed(store, now()).then((d) => {
|
|
55
|
+
if (isMounted())
|
|
56
|
+
setDismissed(d);
|
|
57
|
+
});
|
|
58
|
+
}, [applicable, querySignedIn, store, now, isMounted]);
|
|
59
|
+
const signIn = (0, react_1.useCallback)(async () => {
|
|
60
|
+
await signInTester();
|
|
61
|
+
const v = await querySignedIn();
|
|
62
|
+
if (isMounted())
|
|
63
|
+
setSignedIn(v);
|
|
64
|
+
recheckStore();
|
|
65
|
+
}, [signInTester, querySignedIn, isMounted, recheckStore]);
|
|
66
|
+
const dismissPrompt = (0, react_1.useCallback)(() => {
|
|
67
|
+
setDismissed(true);
|
|
68
|
+
store.setItem(exports.TESTER_DISMISSED_KEY, String(now())).catch(() => { });
|
|
69
|
+
}, [store, now]);
|
|
70
|
+
if (!applicable)
|
|
71
|
+
return exports.INERT_TESTER;
|
|
72
|
+
return {
|
|
73
|
+
signedIn,
|
|
74
|
+
promptVisible: signedIn === false && dismissed === false,
|
|
75
|
+
signIn,
|
|
76
|
+
dismissPrompt,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CHECK_INTERVAL_MS = void 0;
|
|
4
|
+
exports.shouldCheck = shouldCheck;
|
|
5
|
+
/** Minimum gap between foreground store/OTA checks (15 minutes). */
|
|
6
|
+
exports.CHECK_INTERVAL_MS = 15 * 60 * 1000;
|
|
7
|
+
function shouldCheck(lastAt, now) {
|
|
8
|
+
if (lastAt === null || !Number.isFinite(lastAt) || lastAt > now)
|
|
9
|
+
return true;
|
|
10
|
+
return now - lastAt >= exports.CHECK_INTERVAL_MS;
|
|
11
|
+
}
|