@hot-updater/react-native 1.0.0-rc.16 → 1.0.0-rc.17
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/android/src/main/java/com/hotupdater/BundleFileStorageService.kt +491 -388
- package/android/src/main/java/com/hotupdater/BundleMetadata.kt +5 -11
- package/android/src/main/java/com/hotupdater/FileManagerService.kt +45 -0
- package/android/src/main/java/com/hotupdater/HotUpdaterException.kt +290 -12
- package/android/src/main/java/com/hotupdater/HotUpdaterImpl.kt +35 -19
- package/android/src/main/java/com/hotupdater/InstallIdentityService.kt +37 -0
- package/android/src/main/java/com/hotupdater/KeyValueStorageService.kt +86 -0
- package/android/src/main/java/com/hotupdater/NetworkState.kt +31 -0
- package/android/src/main/java/com/hotupdater/NoBackupStorage.kt +17 -0
- package/android/src/main/java/com/hotupdater/OkHttpDownloadService.kt +110 -52
- package/android/src/main/java/com/hotupdater/PreviousProcessExit.kt +89 -0
- package/android/src/main/java/com/hotupdater/ReactNativeValueConverters.kt +34 -0
- package/android/src/newarch/HotUpdaterModule.kt +74 -69
- package/android/src/oldarch/HotUpdaterModule.kt +63 -59
- package/android/src/oldarch/HotUpdaterSpec.kt +4 -6
- package/android/src/test/java/com/hotupdater/BsdiffPatchTest.kt +1 -1
- package/android/src/test/java/com/hotupdater/BundleFileStorageServiceTest.kt +446 -61
- package/android/src/test/java/com/hotupdater/HotUpdaterExceptionTest.kt +220 -0
- package/android/src/test/java/com/hotupdater/HotUpdaterImplTest.kt +19 -27
- package/android/src/test/java/com/hotupdater/InstallIdentityServiceTest.kt +71 -0
- package/android/src/test/java/com/hotupdater/KeyValueStorageServiceTest.kt +106 -0
- package/android/src/test/java/com/hotupdater/NoBackupContext.kt +11 -0
- package/android/src/test/java/com/hotupdater/OkHttpDownloadServiceTest.kt +309 -23
- package/android/src/test/java/com/hotupdater/PreviousProcessExitTest.kt +57 -0
- package/ios/HotUpdater/Internal/BundleFileStorageService.swift +238 -157
- package/ios/HotUpdater/Internal/BundleMetadata.swift +3 -53
- package/ios/HotUpdater/Internal/FileManagerService.swift +48 -0
- package/ios/HotUpdater/Internal/HotUpdater.mm +8 -18
- package/ios/HotUpdater/Internal/HotUpdaterImpl.swift +48 -29
- package/ios/HotUpdater/Internal/KeyValueStorageService.swift +71 -0
- package/ios/HotUpdater/Internal/URLSessionDownloadService.swift +108 -1
- package/ios/HotUpdater/Internal/UpdateFailure.swift +390 -0
- package/lib/commonjs/appReady.js +111 -0
- package/lib/commonjs/appReady.js.map +1 -0
- package/lib/commonjs/appReady.spec.js +360 -0
- package/lib/commonjs/appReady.spec.js.map +1 -0
- package/lib/commonjs/{notifyAppReadyInsights.test-utils.js → appReady.test-utils.js} +4 -3
- package/lib/commonjs/appReady.test-utils.js.map +1 -0
- package/lib/commonjs/baseURL.js +16 -0
- package/lib/commonjs/baseURL.js.map +1 -0
- package/lib/commonjs/checkForUpdate.js +104 -56
- package/lib/commonjs/checkForUpdate.js.map +1 -1
- package/lib/commonjs/checkForUpdate.releaseCatalog.spec.js +337 -74
- package/lib/commonjs/checkForUpdate.releaseCatalog.spec.js.map +1 -1
- package/lib/commonjs/clientPlugin.js +49 -0
- package/lib/commonjs/clientPlugin.js.map +1 -0
- package/lib/commonjs/entryImportGraph.spec.js +50 -0
- package/lib/commonjs/entryImportGraph.spec.js.map +1 -0
- package/lib/commonjs/fetchJSON.js +2 -6
- package/lib/commonjs/fetchJSON.js.map +1 -1
- package/lib/commonjs/httpClient.js +18 -165
- package/lib/commonjs/httpClient.js.map +1 -1
- package/lib/commonjs/httpClient.spec.js +26 -199
- package/lib/commonjs/httpClient.spec.js.map +1 -1
- package/lib/commonjs/index.js +49 -36
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.spec.js +108 -123
- package/lib/commonjs/index.spec.js.map +1 -1
- package/lib/commonjs/native.js +67 -48
- package/lib/commonjs/native.js.map +1 -1
- package/lib/commonjs/native.spec.js +80 -58
- package/lib/commonjs/native.spec.js.map +1 -1
- package/lib/commonjs/pluginHost.js +208 -0
- package/lib/commonjs/pluginHost.js.map +1 -0
- package/lib/commonjs/pluginHost.spec.js +292 -0
- package/lib/commonjs/pluginHost.spec.js.map +1 -0
- package/lib/commonjs/plugins/insights/eventId.js +129 -0
- package/lib/commonjs/plugins/insights/eventId.js.map +1 -0
- package/lib/commonjs/plugins/insights/eventId.spec.js +47 -0
- package/lib/commonjs/plugins/insights/eventId.spec.js.map +1 -0
- package/lib/commonjs/plugins/insights/index.js +272 -0
- package/lib/commonjs/plugins/insights/index.js.map +1 -0
- package/lib/commonjs/plugins/insights/insights.spec.js +640 -0
- package/lib/commonjs/plugins/insights/insights.spec.js.map +1 -0
- package/lib/commonjs/plugins/insights/sender.js +128 -0
- package/lib/commonjs/plugins/insights/sender.js.map +1 -0
- package/lib/commonjs/plugins/insights/sender.spec.js +265 -0
- package/lib/commonjs/plugins/insights/sender.spec.js.map +1 -0
- package/lib/commonjs/plugins/insights/state.js +145 -0
- package/lib/commonjs/plugins/insights/state.js.map +1 -0
- package/lib/commonjs/releaseCatalogCache.js +13 -2
- package/lib/commonjs/releaseCatalogCache.js.map +1 -1
- package/lib/commonjs/releaseCatalogCache.spec.js +18 -0
- package/lib/commonjs/releaseCatalogCache.spec.js.map +1 -1
- package/lib/commonjs/sdkVersion.js +1 -1
- package/lib/commonjs/specs/NativeHotUpdater.js.map +1 -1
- package/lib/commonjs/updateError.js +132 -0
- package/lib/commonjs/updateError.js.map +1 -0
- package/lib/commonjs/updateError.spec.js +149 -0
- package/lib/commonjs/updateError.spec.js.map +1 -0
- package/lib/commonjs/wrap.js +43 -25
- package/lib/commonjs/wrap.js.map +1 -1
- package/lib/commonjs/wrap.progress.spec.js +195 -0
- package/lib/commonjs/wrap.progress.spec.js.map +1 -0
- package/lib/commonjs/wrap.spec.js +94 -230
- package/lib/commonjs/wrap.spec.js.map +1 -1
- package/lib/module/appReady.js +103 -0
- package/lib/module/appReady.js.map +1 -0
- package/lib/module/appReady.spec.js +359 -0
- package/lib/module/appReady.spec.js.map +1 -0
- package/lib/module/{notifyAppReadyInsights.test-utils.js → appReady.test-utils.js} +4 -3
- package/lib/module/appReady.test-utils.js.map +1 -0
- package/lib/module/baseURL.js +11 -0
- package/lib/module/baseURL.js.map +1 -0
- package/lib/module/checkForUpdate.js +103 -57
- package/lib/module/checkForUpdate.js.map +1 -1
- package/lib/module/checkForUpdate.releaseCatalog.spec.js +337 -74
- package/lib/module/checkForUpdate.releaseCatalog.spec.js.map +1 -1
- package/lib/module/clientPlugin.js +44 -0
- package/lib/module/clientPlugin.js.map +1 -0
- package/lib/module/entryImportGraph.spec.js +50 -0
- package/lib/module/entryImportGraph.spec.js.map +1 -0
- package/lib/module/fetchJSON.js +2 -6
- package/lib/module/fetchJSON.js.map +1 -1
- package/lib/module/httpClient.js +18 -165
- package/lib/module/httpClient.js.map +1 -1
- package/lib/module/httpClient.spec.js +27 -200
- package/lib/module/httpClient.spec.js.map +1 -1
- package/lib/module/index.js +44 -38
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.spec.js +108 -123
- package/lib/module/index.spec.js.map +1 -1
- package/lib/module/native.js +63 -44
- package/lib/module/native.js.map +1 -1
- package/lib/module/native.spec.js +80 -58
- package/lib/module/native.spec.js.map +1 -1
- package/lib/module/pluginHost.js +200 -0
- package/lib/module/pluginHost.js.map +1 -0
- package/lib/module/pluginHost.spec.js +291 -0
- package/lib/module/pluginHost.spec.js.map +1 -0
- package/lib/module/plugins/insights/eventId.js +122 -0
- package/lib/module/plugins/insights/eventId.js.map +1 -0
- package/lib/module/plugins/insights/eventId.spec.js +47 -0
- package/lib/module/plugins/insights/eventId.spec.js.map +1 -0
- package/lib/module/plugins/insights/index.js +268 -0
- package/lib/module/plugins/insights/index.js.map +1 -0
- package/lib/module/plugins/insights/insights.spec.js +639 -0
- package/lib/module/plugins/insights/insights.spec.js.map +1 -0
- package/lib/module/plugins/insights/sender.js +123 -0
- package/lib/module/plugins/insights/sender.js.map +1 -0
- package/lib/module/plugins/insights/sender.spec.js +265 -0
- package/lib/module/plugins/insights/sender.spec.js.map +1 -0
- package/lib/module/plugins/insights/state.js +139 -0
- package/lib/module/plugins/insights/state.js.map +1 -0
- package/lib/module/releaseCatalogCache.js +13 -2
- package/lib/module/releaseCatalogCache.js.map +1 -1
- package/lib/module/releaseCatalogCache.spec.js +18 -0
- package/lib/module/releaseCatalogCache.spec.js.map +1 -1
- package/lib/module/sdkVersion.js +1 -1
- package/lib/module/specs/NativeHotUpdater.js.map +1 -1
- package/lib/module/updateError.js +126 -0
- package/lib/module/updateError.js.map +1 -0
- package/lib/module/updateError.spec.js +149 -0
- package/lib/module/updateError.spec.js.map +1 -0
- package/lib/module/wrap.js +43 -25
- package/lib/module/wrap.js.map +1 -1
- package/lib/module/wrap.progress.spec.js +196 -0
- package/lib/module/wrap.progress.spec.js.map +1 -0
- package/lib/module/wrap.spec.js +93 -229
- package/lib/module/wrap.spec.js.map +1 -1
- package/lib/typescript/commonjs/appReady.d.ts +23 -0
- package/lib/typescript/commonjs/appReady.d.ts.map +1 -0
- package/lib/typescript/commonjs/appReady.test-utils.d.ts +9 -0
- package/lib/typescript/commonjs/appReady.test-utils.d.ts.map +1 -0
- package/lib/typescript/commonjs/baseURL.d.ts +4 -0
- package/lib/typescript/commonjs/baseURL.d.ts.map +1 -0
- package/lib/typescript/commonjs/checkForUpdate.d.ts +15 -2
- package/lib/typescript/commonjs/checkForUpdate.d.ts.map +1 -1
- package/lib/typescript/commonjs/clientPlugin.d.ts +185 -0
- package/lib/typescript/commonjs/clientPlugin.d.ts.map +1 -0
- package/lib/typescript/commonjs/fetchJSON.d.ts +2 -3
- package/lib/typescript/commonjs/fetchJSON.d.ts.map +1 -1
- package/lib/typescript/commonjs/httpClient.d.ts +2 -36
- package/lib/typescript/commonjs/httpClient.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +3 -6
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/native.d.ts +29 -15
- package/lib/typescript/commonjs/native.d.ts.map +1 -1
- package/lib/typescript/commonjs/pluginHost.d.ts +29 -0
- package/lib/typescript/commonjs/pluginHost.d.ts.map +1 -0
- package/lib/typescript/commonjs/plugins/insights/eventId.d.ts +18 -0
- package/lib/typescript/commonjs/plugins/insights/eventId.d.ts.map +1 -0
- package/lib/typescript/commonjs/plugins/insights/index.d.ts +40 -0
- package/lib/typescript/commonjs/plugins/insights/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/plugins/insights/sender.d.ts +74 -0
- package/lib/typescript/commonjs/plugins/insights/sender.d.ts.map +1 -0
- package/lib/typescript/commonjs/plugins/insights/state.d.ts +46 -0
- package/lib/typescript/commonjs/plugins/insights/state.d.ts.map +1 -0
- package/lib/typescript/commonjs/releaseCatalogCache.d.ts +5 -1
- package/lib/typescript/commonjs/releaseCatalogCache.d.ts.map +1 -1
- package/lib/typescript/commonjs/sdkVersion.d.ts +1 -1
- package/lib/typescript/commonjs/specs/NativeHotUpdater.d.ts +34 -12
- package/lib/typescript/commonjs/specs/NativeHotUpdater.d.ts.map +1 -1
- package/lib/typescript/commonjs/updateError.d.ts +26 -0
- package/lib/typescript/commonjs/updateError.d.ts.map +1 -0
- package/lib/typescript/commonjs/wrap.d.ts.map +1 -1
- package/lib/typescript/commonjs/wrap.types.d.ts +6 -10
- package/lib/typescript/commonjs/wrap.types.d.ts.map +1 -1
- package/lib/typescript/module/appReady.d.ts +23 -0
- package/lib/typescript/module/appReady.d.ts.map +1 -0
- package/lib/typescript/module/appReady.test-utils.d.ts +9 -0
- package/lib/typescript/module/appReady.test-utils.d.ts.map +1 -0
- package/lib/typescript/module/baseURL.d.ts +4 -0
- package/lib/typescript/module/baseURL.d.ts.map +1 -0
- package/lib/typescript/module/checkForUpdate.d.ts +15 -2
- package/lib/typescript/module/checkForUpdate.d.ts.map +1 -1
- package/lib/typescript/module/clientPlugin.d.ts +185 -0
- package/lib/typescript/module/clientPlugin.d.ts.map +1 -0
- package/lib/typescript/module/fetchJSON.d.ts +2 -3
- package/lib/typescript/module/fetchJSON.d.ts.map +1 -1
- package/lib/typescript/module/httpClient.d.ts +2 -36
- package/lib/typescript/module/httpClient.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +3 -6
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/native.d.ts +29 -15
- package/lib/typescript/module/native.d.ts.map +1 -1
- package/lib/typescript/module/pluginHost.d.ts +29 -0
- package/lib/typescript/module/pluginHost.d.ts.map +1 -0
- package/lib/typescript/module/plugins/insights/eventId.d.ts +18 -0
- package/lib/typescript/module/plugins/insights/eventId.d.ts.map +1 -0
- package/lib/typescript/module/plugins/insights/index.d.ts +40 -0
- package/lib/typescript/module/plugins/insights/index.d.ts.map +1 -0
- package/lib/typescript/module/plugins/insights/sender.d.ts +74 -0
- package/lib/typescript/module/plugins/insights/sender.d.ts.map +1 -0
- package/lib/typescript/module/plugins/insights/state.d.ts +46 -0
- package/lib/typescript/module/plugins/insights/state.d.ts.map +1 -0
- package/lib/typescript/module/releaseCatalogCache.d.ts +5 -1
- package/lib/typescript/module/releaseCatalogCache.d.ts.map +1 -1
- package/lib/typescript/module/sdkVersion.d.ts +1 -1
- package/lib/typescript/module/specs/NativeHotUpdater.d.ts +34 -12
- package/lib/typescript/module/specs/NativeHotUpdater.d.ts.map +1 -1
- package/lib/typescript/module/updateError.d.ts +26 -0
- package/lib/typescript/module/updateError.d.ts.map +1 -0
- package/lib/typescript/module/wrap.d.ts.map +1 -1
- package/lib/typescript/module/wrap.types.d.ts +6 -10
- package/lib/typescript/module/wrap.types.d.ts.map +1 -1
- package/package.json +14 -2
- package/plugins/insights/package.json +8 -0
- package/src/appReady.spec.ts +393 -0
- package/src/{notifyAppReadyInsights.test-utils.ts → appReady.test-utils.ts} +7 -7
- package/src/appReady.ts +149 -0
- package/src/baseURL.ts +15 -0
- package/src/checkForUpdate.releaseCatalog.spec.ts +370 -76
- package/src/checkForUpdate.ts +135 -80
- package/src/clientPlugin.ts +231 -0
- package/src/entryImportGraph.spec.ts +65 -0
- package/src/fetchJSON.ts +3 -10
- package/src/httpClient.spec.ts +30 -227
- package/src/httpClient.ts +20 -256
- package/src/index.spec.ts +117 -93
- package/src/index.ts +67 -52
- package/src/native.spec.ts +98 -72
- package/src/native.ts +93 -77
- package/src/pluginHost.spec.ts +297 -0
- package/src/pluginHost.ts +262 -0
- package/src/plugins/insights/eventId.spec.ts +60 -0
- package/src/plugins/insights/eventId.ts +171 -0
- package/src/plugins/insights/index.ts +367 -0
- package/src/plugins/insights/insights.spec.ts +732 -0
- package/src/plugins/insights/sender.spec.ts +287 -0
- package/src/plugins/insights/sender.ts +221 -0
- package/src/plugins/insights/state.ts +184 -0
- package/src/releaseCatalogCache.spec.ts +22 -0
- package/src/releaseCatalogCache.ts +19 -4
- package/src/sdkVersion.ts +1 -1
- package/src/specs/NativeHotUpdater.ts +34 -13
- package/src/updateError.spec.ts +144 -0
- package/src/updateError.ts +153 -0
- package/src/wrap.progress.spec.ts +232 -0
- package/src/wrap.spec.ts +100 -283
- package/src/wrap.tsx +57 -26
- package/src/wrap.types.ts +6 -12
- package/lib/commonjs/notifyAppReadyInsights.boundary.spec.js +0 -225
- package/lib/commonjs/notifyAppReadyInsights.boundary.spec.js.map +0 -1
- package/lib/commonjs/notifyAppReadyInsights.js +0 -188
- package/lib/commonjs/notifyAppReadyInsights.js.map +0 -1
- package/lib/commonjs/notifyAppReadyInsights.spec.js +0 -378
- package/lib/commonjs/notifyAppReadyInsights.spec.js.map +0 -1
- package/lib/commonjs/notifyAppReadyInsights.test-utils.js.map +0 -1
- package/lib/commonjs/notifyAppReadyInsights.transport.spec.js +0 -171
- package/lib/commonjs/notifyAppReadyInsights.transport.spec.js.map +0 -1
- package/lib/commonjs/uuidv7.js +0 -41
- package/lib/commonjs/uuidv7.js.map +0 -1
- package/lib/commonjs/uuidv7.spec.js +0 -30
- package/lib/commonjs/uuidv7.spec.js.map +0 -1
- package/lib/module/notifyAppReadyInsights.boundary.spec.js +0 -224
- package/lib/module/notifyAppReadyInsights.boundary.spec.js.map +0 -1
- package/lib/module/notifyAppReadyInsights.js +0 -181
- package/lib/module/notifyAppReadyInsights.js.map +0 -1
- package/lib/module/notifyAppReadyInsights.spec.js +0 -377
- package/lib/module/notifyAppReadyInsights.spec.js.map +0 -1
- package/lib/module/notifyAppReadyInsights.test-utils.js.map +0 -1
- package/lib/module/notifyAppReadyInsights.transport.spec.js +0 -170
- package/lib/module/notifyAppReadyInsights.transport.spec.js.map +0 -1
- package/lib/module/uuidv7.js +0 -36
- package/lib/module/uuidv7.js.map +0 -1
- package/lib/module/uuidv7.spec.js +0 -30
- package/lib/module/uuidv7.spec.js.map +0 -1
- package/lib/typescript/commonjs/notifyAppReadyInsights.d.ts +0 -25
- package/lib/typescript/commonjs/notifyAppReadyInsights.d.ts.map +0 -1
- package/lib/typescript/commonjs/notifyAppReadyInsights.test-utils.d.ts +0 -8
- package/lib/typescript/commonjs/notifyAppReadyInsights.test-utils.d.ts.map +0 -1
- package/lib/typescript/commonjs/uuidv7.d.ts +0 -10
- package/lib/typescript/commonjs/uuidv7.d.ts.map +0 -1
- package/lib/typescript/module/notifyAppReadyInsights.d.ts +0 -25
- package/lib/typescript/module/notifyAppReadyInsights.d.ts.map +0 -1
- package/lib/typescript/module/notifyAppReadyInsights.test-utils.d.ts +0 -8
- package/lib/typescript/module/notifyAppReadyInsights.test-utils.d.ts.map +0 -1
- package/lib/typescript/module/uuidv7.d.ts +0 -10
- package/lib/typescript/module/uuidv7.d.ts.map +0 -1
- package/src/notifyAppReadyInsights.boundary.spec.ts +0 -242
- package/src/notifyAppReadyInsights.spec.ts +0 -362
- package/src/notifyAppReadyInsights.transport.spec.ts +0 -196
- package/src/notifyAppReadyInsights.ts +0 -293
- package/src/uuidv7.spec.ts +0 -25
- package/src/uuidv7.ts +0 -38
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { isUUIDv7 } from "@hot-updater/core";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { createUUIDv7 } from "./eventId";
|
|
5
|
+
import {
|
|
6
|
+
createInsightsEventSender,
|
|
7
|
+
type InsightsEventBody,
|
|
8
|
+
type InsightsEventGate,
|
|
9
|
+
} from "./sender";
|
|
10
|
+
|
|
11
|
+
const unchangedEvent = (): InsightsEventBody => ({
|
|
12
|
+
appVersion: "1.0.0",
|
|
13
|
+
channel: "production",
|
|
14
|
+
cohort: "123",
|
|
15
|
+
eventId: createUUIDv7(),
|
|
16
|
+
fingerprintHash: null,
|
|
17
|
+
fromBundleId: null,
|
|
18
|
+
fromReleaseId: null,
|
|
19
|
+
installId: "install-id",
|
|
20
|
+
platform: "ios",
|
|
21
|
+
sdkVersion: "test-sdk-version",
|
|
22
|
+
toBundleId: "bundle-id",
|
|
23
|
+
toReleaseId: null,
|
|
24
|
+
type: "UNCHANGED",
|
|
25
|
+
updateStrategy: null,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const downloadedEvent = (): InsightsEventBody => ({
|
|
29
|
+
...unchangedEvent(),
|
|
30
|
+
fromBundleId: "bundle-id",
|
|
31
|
+
toBundleId: "next-bundle-id",
|
|
32
|
+
type: "UPDATE_DOWNLOADED",
|
|
33
|
+
updateStrategy: "appVersion",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/** Answers each POST /events with the next response, then 204. */
|
|
37
|
+
const stubEventsEndpoint = (...responses: (number | Response | Error)[]) => {
|
|
38
|
+
const fetchMock = vi.fn(
|
|
39
|
+
async (_path: string, _init?: RequestInit): Promise<Response> => {
|
|
40
|
+
const next = responses.shift() ?? 204;
|
|
41
|
+
if (next instanceof Error) throw next;
|
|
42
|
+
return typeof next === "number"
|
|
43
|
+
? new Response(null, { status: next })
|
|
44
|
+
: next;
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
const sentEvents = () =>
|
|
48
|
+
fetchMock.mock.calls.map(
|
|
49
|
+
([, init]) => JSON.parse(String(init?.body)) as InsightsEventBody,
|
|
50
|
+
);
|
|
51
|
+
return { fetchMock, sentEvents };
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const openGate = () => ({
|
|
55
|
+
admit: () => true,
|
|
56
|
+
settle: vi.fn<InsightsEventGate["settle"]>(),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("Insights event delivery", () => {
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
vi.useFakeTimers();
|
|
62
|
+
// No jitter: the first retry waits exactly 1 s and the second 2 s.
|
|
63
|
+
vi.spyOn(Math, "random").mockReturnValue(0.5);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
afterEach(() => {
|
|
67
|
+
vi.useRealTimers();
|
|
68
|
+
vi.restoreAllMocks();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("posts to events and retries a 503 in the background under the same UUIDv7 eventId", async () => {
|
|
72
|
+
const { fetchMock, sentEvents } = stubEventsEndpoint(503, 204);
|
|
73
|
+
const gate = openGate();
|
|
74
|
+
const send = createInsightsEventSender(fetchMock, gate);
|
|
75
|
+
|
|
76
|
+
// Settles after the failed first attempt, without waiting for the retry.
|
|
77
|
+
await send(unchangedEvent());
|
|
78
|
+
expect(fetchMock).toHaveBeenCalledOnce();
|
|
79
|
+
expect(fetchMock.mock.calls[0]?.[0]).toBe("events");
|
|
80
|
+
expect(fetchMock.mock.calls[0]?.[1]?.method).toBe("POST");
|
|
81
|
+
|
|
82
|
+
await vi.advanceTimersByTimeAsync(1_000);
|
|
83
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
84
|
+
const [first, second] = sentEvents();
|
|
85
|
+
expect(isUUIDv7(first?.eventId)).toBe(true);
|
|
86
|
+
expect(second?.eventId).toBe(first?.eventId);
|
|
87
|
+
expect(gate.settle).toHaveBeenCalledExactlyOnceWith(
|
|
88
|
+
expect.objectContaining({ type: "UNCHANGED" }),
|
|
89
|
+
"delivered",
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("waits for a 429's Retry-After before the next attempt", async () => {
|
|
94
|
+
const { fetchMock } = stubEventsEndpoint(
|
|
95
|
+
new Response(null, { headers: { "Retry-After": "7" }, status: 429 }),
|
|
96
|
+
);
|
|
97
|
+
const send = createInsightsEventSender(fetchMock, openGate());
|
|
98
|
+
|
|
99
|
+
await send(unchangedEvent());
|
|
100
|
+
await vi.advanceTimersByTimeAsync(6_999);
|
|
101
|
+
expect(fetchMock).toHaveBeenCalledOnce();
|
|
102
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
103
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("caps a server's Retry-After at 30 seconds", async () => {
|
|
107
|
+
const { fetchMock } = stubEventsEndpoint(
|
|
108
|
+
new Response(null, { headers: { "Retry-After": "3600" }, status: 503 }),
|
|
109
|
+
);
|
|
110
|
+
const send = createInsightsEventSender(fetchMock, openGate());
|
|
111
|
+
|
|
112
|
+
await send(unchangedEvent());
|
|
113
|
+
await vi.advanceTimersByTimeAsync(29_999);
|
|
114
|
+
expect(fetchMock).toHaveBeenCalledOnce();
|
|
115
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
116
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("does not retry a 400", async () => {
|
|
120
|
+
const { fetchMock } = stubEventsEndpoint(400);
|
|
121
|
+
const gate = openGate();
|
|
122
|
+
const send = createInsightsEventSender(fetchMock, gate);
|
|
123
|
+
|
|
124
|
+
await expect(send(unchangedEvent())).rejects.toThrow(
|
|
125
|
+
"Expected HTTP 204 from /events, received 400",
|
|
126
|
+
);
|
|
127
|
+
await vi.runAllTimersAsync();
|
|
128
|
+
expect(fetchMock).toHaveBeenCalledOnce();
|
|
129
|
+
expect(gate.settle).toHaveBeenCalledWith(
|
|
130
|
+
expect.objectContaining({ type: "UNCHANGED" }),
|
|
131
|
+
"failed",
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("settles an UPDATE_FAILED event an older server refuses with 400, without retrying", async () => {
|
|
136
|
+
const { fetchMock } = stubEventsEndpoint(400);
|
|
137
|
+
const gate = openGate();
|
|
138
|
+
const send = createInsightsEventSender(fetchMock, gate);
|
|
139
|
+
|
|
140
|
+
await send({ ...unchangedEvent(), type: "UPDATE_FAILED" });
|
|
141
|
+
await vi.runAllTimersAsync();
|
|
142
|
+
|
|
143
|
+
expect(fetchMock).toHaveBeenCalledOnce();
|
|
144
|
+
expect(gate.settle).toHaveBeenCalledWith(
|
|
145
|
+
expect.objectContaining({ type: "UPDATE_FAILED" }),
|
|
146
|
+
"refused",
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("settles a 404, from a server without Insights, as Insights off without retrying", async () => {
|
|
151
|
+
const { fetchMock } = stubEventsEndpoint(404);
|
|
152
|
+
const gate = openGate();
|
|
153
|
+
const send = createInsightsEventSender(fetchMock, gate);
|
|
154
|
+
|
|
155
|
+
await send(unchangedEvent());
|
|
156
|
+
await vi.runAllTimersAsync();
|
|
157
|
+
|
|
158
|
+
expect(fetchMock).toHaveBeenCalledOnce();
|
|
159
|
+
expect(gate.settle).toHaveBeenCalledWith(
|
|
160
|
+
expect.objectContaining({ type: "UNCHANGED" }),
|
|
161
|
+
"disabled",
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("skips an event the gate does not admit", async () => {
|
|
166
|
+
const { fetchMock } = stubEventsEndpoint();
|
|
167
|
+
const gate = { admit: vi.fn(() => false), settle: vi.fn() };
|
|
168
|
+
const send = createInsightsEventSender(fetchMock, gate);
|
|
169
|
+
|
|
170
|
+
await send(unchangedEvent());
|
|
171
|
+
|
|
172
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
173
|
+
expect(gate.settle).not.toHaveBeenCalled();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("asks the gate before every attempt, after earlier events settled", async () => {
|
|
177
|
+
const { fetchMock } = stubEventsEndpoint(503, 204);
|
|
178
|
+
const order: string[] = [];
|
|
179
|
+
const gate: InsightsEventGate = {
|
|
180
|
+
admit: (event) => {
|
|
181
|
+
order.push(`admit ${event.type}`);
|
|
182
|
+
return true;
|
|
183
|
+
},
|
|
184
|
+
settle: (event, delivery) => {
|
|
185
|
+
order.push(`${delivery} ${event.type}`);
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
const send = createInsightsEventSender(fetchMock, gate);
|
|
189
|
+
|
|
190
|
+
await send(unchangedEvent());
|
|
191
|
+
await send(downloadedEvent());
|
|
192
|
+
await vi.runAllTimersAsync();
|
|
193
|
+
|
|
194
|
+
expect(order).toEqual([
|
|
195
|
+
"admit UNCHANGED",
|
|
196
|
+
"admit UNCHANGED",
|
|
197
|
+
"delivered UNCHANGED",
|
|
198
|
+
"admit UPDATE_DOWNLOADED",
|
|
199
|
+
"delivered UPDATE_DOWNLOADED",
|
|
200
|
+
]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("drops an event the gate stops admitting during its backoff", async () => {
|
|
204
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
205
|
+
const { fetchMock, sentEvents } = stubEventsEndpoint(503);
|
|
206
|
+
let wanted = true;
|
|
207
|
+
const gate = { admit: vi.fn(() => wanted), settle: vi.fn() };
|
|
208
|
+
const send = createInsightsEventSender(fetchMock, gate);
|
|
209
|
+
|
|
210
|
+
await send({ ...unchangedEvent(), type: "UPDATE_FAILED" });
|
|
211
|
+
wanted = false;
|
|
212
|
+
await vi.runAllTimersAsync();
|
|
213
|
+
wanted = true;
|
|
214
|
+
await send(downloadedEvent());
|
|
215
|
+
|
|
216
|
+
expect(sentEvents().map(({ type }) => type)).toEqual([
|
|
217
|
+
"UPDATE_FAILED",
|
|
218
|
+
"UPDATE_DOWNLOADED",
|
|
219
|
+
]);
|
|
220
|
+
expect(gate.settle).toHaveBeenCalledExactlyOnceWith(
|
|
221
|
+
expect.objectContaining({ type: "UPDATE_DOWNLOADED" }),
|
|
222
|
+
"delivered",
|
|
223
|
+
);
|
|
224
|
+
expect(warn).not.toHaveBeenCalled();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("stops after three attempts and warns once", async () => {
|
|
228
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
229
|
+
const { fetchMock } = stubEventsEndpoint(
|
|
230
|
+
500,
|
|
231
|
+
new Error("network unavailable"),
|
|
232
|
+
503,
|
|
233
|
+
);
|
|
234
|
+
const send = createInsightsEventSender(fetchMock, openGate());
|
|
235
|
+
|
|
236
|
+
await send(unchangedEvent());
|
|
237
|
+
await vi.runAllTimersAsync();
|
|
238
|
+
|
|
239
|
+
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
240
|
+
expect(warn).toHaveBeenCalledExactlyOnceWith(
|
|
241
|
+
"[HotUpdater] Insights UNCHANGED event was not delivered:",
|
|
242
|
+
expect.objectContaining({
|
|
243
|
+
message: "Expected HTTP 204 from /events, received 503",
|
|
244
|
+
}),
|
|
245
|
+
);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("sends a later event after an earlier event's retries without holding its caller", async () => {
|
|
249
|
+
const { fetchMock, sentEvents } = stubEventsEndpoint(503);
|
|
250
|
+
const send = createInsightsEventSender(fetchMock, openGate());
|
|
251
|
+
|
|
252
|
+
await send(unchangedEvent());
|
|
253
|
+
await send(downloadedEvent());
|
|
254
|
+
expect(fetchMock).toHaveBeenCalledOnce();
|
|
255
|
+
|
|
256
|
+
await vi.runAllTimersAsync();
|
|
257
|
+
const [first, retry, later] = sentEvents();
|
|
258
|
+
expect([first?.type, retry?.type, later?.type]).toEqual([
|
|
259
|
+
"UNCHANGED",
|
|
260
|
+
"UNCHANGED",
|
|
261
|
+
"UPDATE_DOWNLOADED",
|
|
262
|
+
]);
|
|
263
|
+
expect(retry?.eventId).toBe(first?.eventId);
|
|
264
|
+
expect(later?.eventId).not.toBe(first?.eventId);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("keeps sending events after one fails outside its requests", async () => {
|
|
268
|
+
vi.spyOn(console, "warn").mockImplementation(() => {
|
|
269
|
+
throw new Error("console unavailable");
|
|
270
|
+
});
|
|
271
|
+
const { fetchMock, sentEvents } = stubEventsEndpoint(500, 500, 500);
|
|
272
|
+
const send = createInsightsEventSender(fetchMock, openGate());
|
|
273
|
+
|
|
274
|
+
await send(unchangedEvent());
|
|
275
|
+
await vi.runAllTimersAsync();
|
|
276
|
+
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
277
|
+
|
|
278
|
+
// A queue left rejected would never send this event or settle its caller.
|
|
279
|
+
await send(downloadedEvent());
|
|
280
|
+
expect(sentEvents().map(({ type }) => type)).toEqual([
|
|
281
|
+
"UNCHANGED",
|
|
282
|
+
"UNCHANGED",
|
|
283
|
+
"UNCHANGED",
|
|
284
|
+
"UPDATE_DOWNLOADED",
|
|
285
|
+
]);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import type { HotUpdaterClientContext } from "../../clientPlugin";
|
|
2
|
+
|
|
3
|
+
export type InsightsEventType =
|
|
4
|
+
| "UNCHANGED"
|
|
5
|
+
| "UPDATE_DOWNLOADED"
|
|
6
|
+
| "UPDATE_APPLIED"
|
|
7
|
+
| "RECOVERED"
|
|
8
|
+
| "UPDATE_FAILED";
|
|
9
|
+
|
|
10
|
+
/** A `POST /events` body. */
|
|
11
|
+
export interface InsightsEventBody {
|
|
12
|
+
readonly eventId: string;
|
|
13
|
+
readonly type: InsightsEventType;
|
|
14
|
+
readonly installId: string;
|
|
15
|
+
readonly userId?: string;
|
|
16
|
+
readonly platform: "ios" | "android";
|
|
17
|
+
readonly appVersion: string;
|
|
18
|
+
readonly channel: string;
|
|
19
|
+
readonly cohort: string;
|
|
20
|
+
readonly fingerprintHash: string | null;
|
|
21
|
+
readonly sdkVersion: string;
|
|
22
|
+
readonly fromBundleId: string | null;
|
|
23
|
+
readonly fromReleaseId: string | null;
|
|
24
|
+
readonly toBundleId: string;
|
|
25
|
+
readonly toReleaseId: string | null;
|
|
26
|
+
readonly updateStrategy: "fingerprint" | "appVersion" | null;
|
|
27
|
+
readonly metadata?: InsightsEventMetadata;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Details beside an event's fields, by event type. */
|
|
31
|
+
export interface InsightsEventMetadata {
|
|
32
|
+
/** UPDATE_FAILED: where and why the update failed. */
|
|
33
|
+
readonly failure?: {
|
|
34
|
+
readonly stage: "check" | "download" | "install";
|
|
35
|
+
readonly reason: string;
|
|
36
|
+
readonly resource?: string;
|
|
37
|
+
readonly httpStatus?: number;
|
|
38
|
+
readonly transport?: string;
|
|
39
|
+
readonly originCode?: string;
|
|
40
|
+
readonly previousProcessExit?: string;
|
|
41
|
+
};
|
|
42
|
+
/** UPDATE_DOWNLOADED: how the bundle arrived. */
|
|
43
|
+
readonly delivery?: "patch" | "manifest" | "archive";
|
|
44
|
+
/** UPDATE_DOWNLOADED: a patch was tried, but a file or the archive came instead. */
|
|
45
|
+
readonly patchFallback?: true;
|
|
46
|
+
/** RECOVERED: why the previous main process exited, on Android 11+. */
|
|
47
|
+
readonly previousProcessExit?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** How the server answered one event. */
|
|
51
|
+
export type InsightsDelivery =
|
|
52
|
+
/** The server stored the event, or already had it under its id. */
|
|
53
|
+
| "delivered"
|
|
54
|
+
/** The server runs without Insights: a 404, since it mounts no `/events`. */
|
|
55
|
+
| "disabled"
|
|
56
|
+
/** A 400 for an `UPDATE_FAILED` event, from a server that does not know it. */
|
|
57
|
+
| "refused"
|
|
58
|
+
/** No attempt got an answer the SDK accepts. */
|
|
59
|
+
| "failed";
|
|
60
|
+
|
|
61
|
+
export interface InsightsEventGate {
|
|
62
|
+
/**
|
|
63
|
+
* Whether to send the event now. It is asked before every attempt, so an
|
|
64
|
+
* event superseded while it waits out a backoff is dropped.
|
|
65
|
+
*/
|
|
66
|
+
admit(event: InsightsEventBody): boolean;
|
|
67
|
+
/** Records how an admitted event ended, once per event. */
|
|
68
|
+
settle(event: InsightsEventBody, delivery: InsightsDelivery): void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Attempts per Insights event, the first one included. */
|
|
72
|
+
const INSIGHTS_MAX_ATTEMPTS = 3;
|
|
73
|
+
/** Wait before the second attempt; it doubles for each later attempt. */
|
|
74
|
+
const INSIGHTS_RETRY_BASE_DELAY_MS = 1000;
|
|
75
|
+
/** Longest wait between attempts, a server's `Retry-After` included. */
|
|
76
|
+
const INSIGHTS_RETRY_MAX_DELAY_MS = 30000;
|
|
77
|
+
|
|
78
|
+
type InsightsAttemptFailure = {
|
|
79
|
+
readonly error: Error;
|
|
80
|
+
/** A network error, timeout, 429 or 5xx may pass on a later attempt. */
|
|
81
|
+
readonly retryable: boolean;
|
|
82
|
+
readonly retryAfterMs: number | null;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Reads `Retry-After` as delay-seconds, the form a server under load sends;
|
|
87
|
+
* an HTTP-date falls back to the backoff.
|
|
88
|
+
*/
|
|
89
|
+
const parseRetryAfterMs = (value: string | null): number | null => {
|
|
90
|
+
const seconds = value?.trim();
|
|
91
|
+
return seconds && /^\d+$/.test(seconds) ? Number(seconds) * 1000 : null;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const getRetryDelayMs = (
|
|
95
|
+
failedAttempt: number,
|
|
96
|
+
retryAfterMs: number | null,
|
|
97
|
+
): number =>
|
|
98
|
+
Math.min(
|
|
99
|
+
retryAfterMs ??
|
|
100
|
+
// Jitter spreads the retries of installations that failed together.
|
|
101
|
+
INSIGHTS_RETRY_BASE_DELAY_MS *
|
|
102
|
+
2 ** (failedAttempt - 1) *
|
|
103
|
+
(0.5 + Math.random()),
|
|
104
|
+
INSIGHTS_RETRY_MAX_DELAY_MS,
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const wait = (ms: number) =>
|
|
108
|
+
new Promise<void>((resolve) => {
|
|
109
|
+
setTimeout(resolve, ms);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
/** Makes one POST, with the context's timeout, and settles with its outcome. */
|
|
113
|
+
const postInsightsEvent = async (
|
|
114
|
+
fetch: HotUpdaterClientContext["fetch"],
|
|
115
|
+
event: InsightsEventBody,
|
|
116
|
+
): Promise<Exclude<InsightsDelivery, "failed"> | InsightsAttemptFailure> => {
|
|
117
|
+
try {
|
|
118
|
+
const response = await fetch("events", {
|
|
119
|
+
body: JSON.stringify(event),
|
|
120
|
+
headers: { "Content-Type": "application/json" },
|
|
121
|
+
method: "POST",
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
if (response.status === 204) return "delivered";
|
|
125
|
+
if (response.status === 404) return "disabled";
|
|
126
|
+
if (response.status === 400 && event.type === "UPDATE_FAILED") {
|
|
127
|
+
return "refused";
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
error: new Error(
|
|
131
|
+
`Expected HTTP 204 from /events, received ${response.status}`,
|
|
132
|
+
),
|
|
133
|
+
retryable: response.status === 429 || response.status >= 500,
|
|
134
|
+
retryAfterMs: parseRetryAfterMs(response.headers.get("Retry-After")),
|
|
135
|
+
};
|
|
136
|
+
} catch (error: unknown) {
|
|
137
|
+
// fetch rejects only when no response arrived (a timeout or a network
|
|
138
|
+
// error), so a later attempt may still get through.
|
|
139
|
+
return {
|
|
140
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
141
|
+
retryable: true,
|
|
142
|
+
retryAfterMs: null,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export type InsightsEventSender = (event: InsightsEventBody) => Promise<void>;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Sends Insights events one at a time and retries each in the background.
|
|
151
|
+
*
|
|
152
|
+
* The server keeps an installation's latest report by arrival, so a later
|
|
153
|
+
* event waits behind an earlier one's retries; otherwise a retried
|
|
154
|
+
* UPDATE_APPLIED could land after the UPDATE_DOWNLOADED that followed it.
|
|
155
|
+
* Callers wait only for first attempts, so a report never waits out a
|
|
156
|
+
* backoff: once an event backs off, every waiting caller is released, and a
|
|
157
|
+
* failure after that only warns. The gate sees each event before each
|
|
158
|
+
* attempt, after every earlier event settled, so it decides with what the
|
|
159
|
+
* server already answered.
|
|
160
|
+
*/
|
|
161
|
+
export const createInsightsEventSender = (
|
|
162
|
+
fetch: HotUpdaterClientContext["fetch"],
|
|
163
|
+
gate: InsightsEventGate,
|
|
164
|
+
): InsightsEventSender => {
|
|
165
|
+
let queue = Promise.resolve();
|
|
166
|
+
let retrying = false;
|
|
167
|
+
const waiting = new Set<() => void>();
|
|
168
|
+
|
|
169
|
+
return (event) =>
|
|
170
|
+
new Promise<void>((resolve, reject) => {
|
|
171
|
+
let released = false;
|
|
172
|
+
const release = () => {
|
|
173
|
+
released = true;
|
|
174
|
+
waiting.delete(release);
|
|
175
|
+
resolve();
|
|
176
|
+
};
|
|
177
|
+
if (retrying) release();
|
|
178
|
+
else waiting.add(release);
|
|
179
|
+
|
|
180
|
+
queue = queue.then(async () => {
|
|
181
|
+
try {
|
|
182
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
183
|
+
if (!gate.admit(event)) {
|
|
184
|
+
release();
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
const outcome = await postInsightsEvent(fetch, event);
|
|
188
|
+
if (typeof outcome === "string") {
|
|
189
|
+
gate.settle(event, outcome);
|
|
190
|
+
release();
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
if (!outcome.retryable || attempt === INSIGHTS_MAX_ATTEMPTS) {
|
|
194
|
+
gate.settle(event, "failed");
|
|
195
|
+
waiting.delete(release);
|
|
196
|
+
if (released) {
|
|
197
|
+
console.warn(
|
|
198
|
+
`[HotUpdater] Insights ${event.type} event was not delivered:`,
|
|
199
|
+
outcome.error,
|
|
200
|
+
);
|
|
201
|
+
} else {
|
|
202
|
+
reject(outcome.error);
|
|
203
|
+
}
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
retrying = true;
|
|
207
|
+
for (const releaseWaiting of waiting) releaseWaiting();
|
|
208
|
+
await wait(getRetryDelayMs(attempt, outcome.retryAfterMs));
|
|
209
|
+
}
|
|
210
|
+
} catch (error: unknown) {
|
|
211
|
+
// A failed request settles in postInsightsEvent, so only a bug gets
|
|
212
|
+
// here. A rejected queue would leave every later event, and a
|
|
213
|
+
// caller waiting on one, pending forever.
|
|
214
|
+
waiting.delete(release);
|
|
215
|
+
if (!released) reject(error);
|
|
216
|
+
} finally {
|
|
217
|
+
retrying = false;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { HotUpdaterClientStorage } from "../../clientPlugin";
|
|
2
|
+
|
|
3
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
4
|
+
/** A day's failure keys kept, so a failing app cannot grow its storage. */
|
|
5
|
+
const MAX_FAILURE_KEYS = 32;
|
|
6
|
+
|
|
7
|
+
/** The UTC day of a time, as `YYYY-MM-DD`. */
|
|
8
|
+
export const utcDay = (time: number): string =>
|
|
9
|
+
new Date(time).toISOString().slice(0, 10);
|
|
10
|
+
|
|
11
|
+
/** The start of a time's UTC day, in milliseconds. */
|
|
12
|
+
export const utcDayStart = (time: number): number =>
|
|
13
|
+
Math.floor(time / DAY_MS) * DAY_MS;
|
|
14
|
+
|
|
15
|
+
/** What a report says runs: the fields the server compares to skip a same-day launch. */
|
|
16
|
+
export interface RunningReport {
|
|
17
|
+
readonly day: string;
|
|
18
|
+
readonly channel: string;
|
|
19
|
+
readonly appVersion: string;
|
|
20
|
+
readonly bundleId: string;
|
|
21
|
+
readonly releaseId: string | null;
|
|
22
|
+
readonly userId: string | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const sameReport = (left: RunningReport, right: RunningReport): boolean =>
|
|
26
|
+
left.day === right.day &&
|
|
27
|
+
left.channel === right.channel &&
|
|
28
|
+
left.appVersion === right.appVersion &&
|
|
29
|
+
left.bundleId === right.bundleId &&
|
|
30
|
+
left.releaseId === right.releaseId &&
|
|
31
|
+
left.userId === right.userId;
|
|
32
|
+
|
|
33
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
34
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
|
+
|
|
36
|
+
const isNullableString = (value: unknown): value is string | null =>
|
|
37
|
+
value === null || typeof value === "string";
|
|
38
|
+
|
|
39
|
+
const parseReport = (value: string | null): RunningReport | null => {
|
|
40
|
+
if (value === null) return null;
|
|
41
|
+
try {
|
|
42
|
+
const report: unknown = JSON.parse(value);
|
|
43
|
+
if (
|
|
44
|
+
isRecord(report) &&
|
|
45
|
+
typeof report.day === "string" &&
|
|
46
|
+
typeof report.channel === "string" &&
|
|
47
|
+
typeof report.appVersion === "string" &&
|
|
48
|
+
typeof report.bundleId === "string" &&
|
|
49
|
+
isNullableString(report.releaseId) &&
|
|
50
|
+
isNullableString(report.userId)
|
|
51
|
+
) {
|
|
52
|
+
return {
|
|
53
|
+
day: report.day,
|
|
54
|
+
channel: report.channel,
|
|
55
|
+
appVersion: report.appVersion,
|
|
56
|
+
bundleId: report.bundleId,
|
|
57
|
+
releaseId: report.releaseId,
|
|
58
|
+
userId: report.userId,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
// An unreadable value counts as none.
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const parseFailures = (
|
|
68
|
+
value: string | null,
|
|
69
|
+
): { readonly day: string; readonly keys: readonly string[] } | null => {
|
|
70
|
+
if (value === null) return null;
|
|
71
|
+
try {
|
|
72
|
+
const failures: unknown = JSON.parse(value);
|
|
73
|
+
if (
|
|
74
|
+
isRecord(failures) &&
|
|
75
|
+
typeof failures.day === "string" &&
|
|
76
|
+
Array.isArray(failures.keys) &&
|
|
77
|
+
failures.keys.every((key) => typeof key === "string")
|
|
78
|
+
) {
|
|
79
|
+
return { day: failures.day, keys: failures.keys as string[] };
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
// An unreadable value counts as none.
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The Insights plugin's memory across launches, in its plugin storage. Every
|
|
89
|
+
* read tolerates a missing or unreadable value, and a storage failure only
|
|
90
|
+
* costs a report that would otherwise have been skipped.
|
|
91
|
+
*/
|
|
92
|
+
export const createInsightsState = (storage: HotUpdaterClientStorage) => {
|
|
93
|
+
/** The server said Insights is off during this runtime. */
|
|
94
|
+
let pausedThisRuntime = false;
|
|
95
|
+
|
|
96
|
+
const read = (key: string): string | null => {
|
|
97
|
+
try {
|
|
98
|
+
return storage.get(key);
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const write = (key: string, value: string | null) => {
|
|
104
|
+
try {
|
|
105
|
+
storage.set(key, value);
|
|
106
|
+
} catch {
|
|
107
|
+
// Losing the state costs a report, never an update.
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
/**
|
|
113
|
+
* Whether reporting pauses: the server answered that Insights is off
|
|
114
|
+
* during this runtime or in the 24 hours before `now`. A pause that
|
|
115
|
+
* starts after `now`, from a clock that went back, has ended.
|
|
116
|
+
*/
|
|
117
|
+
isPaused(now: number): boolean {
|
|
118
|
+
if (pausedThisRuntime) return true;
|
|
119
|
+
const pausedAt = Number(read("pausedAt"));
|
|
120
|
+
return (
|
|
121
|
+
Number.isFinite(pausedAt) &&
|
|
122
|
+
pausedAt > 0 &&
|
|
123
|
+
pausedAt <= now &&
|
|
124
|
+
now < pausedAt + DAY_MS
|
|
125
|
+
);
|
|
126
|
+
},
|
|
127
|
+
pause(now: number) {
|
|
128
|
+
pausedThisRuntime = true;
|
|
129
|
+
write("pausedAt", String(now));
|
|
130
|
+
},
|
|
131
|
+
/** The server stored a report, so Insights is on. */
|
|
132
|
+
resume() {
|
|
133
|
+
if (read("pausedAt") !== null) write("pausedAt", null);
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
/** Whether a launch report repeats what the installation reported that day. */
|
|
137
|
+
repeatsReport(report: RunningReport): boolean {
|
|
138
|
+
const last = parseReport(read("report"));
|
|
139
|
+
return last !== null && sameReport(last, report);
|
|
140
|
+
},
|
|
141
|
+
/** Records a delivered report that says what runs. */
|
|
142
|
+
recordReport(report: RunningReport) {
|
|
143
|
+
write("report", JSON.stringify(report));
|
|
144
|
+
},
|
|
145
|
+
/**
|
|
146
|
+
* Forgets the last report after a delivered download or failure, so the
|
|
147
|
+
* next launch reports again, as the server records it after one.
|
|
148
|
+
*/
|
|
149
|
+
forgetReport() {
|
|
150
|
+
if (read("report") !== null) write("report", null);
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
/** Whether a failure was already reported, or refused, that day. */
|
|
154
|
+
hasFailure(day: string, key: string): boolean {
|
|
155
|
+
const failures = parseFailures(read("failures"));
|
|
156
|
+
return failures?.day === day && failures.keys.includes(key);
|
|
157
|
+
},
|
|
158
|
+
recordFailure(day: string, key: string) {
|
|
159
|
+
const failures = parseFailures(read("failures"));
|
|
160
|
+
const keys = failures?.day === day ? failures.keys : [];
|
|
161
|
+
if (keys.includes(key)) return;
|
|
162
|
+
write(
|
|
163
|
+
"failures",
|
|
164
|
+
JSON.stringify({ day, keys: [...keys, key].slice(-MAX_FAILURE_KEYS) }),
|
|
165
|
+
);
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
readUserId(): string | null {
|
|
169
|
+
try {
|
|
170
|
+
const user: unknown = JSON.parse(read("user") ?? "null");
|
|
171
|
+
return isRecord(user) && typeof user.userId === "string"
|
|
172
|
+
? user.userId
|
|
173
|
+
: null;
|
|
174
|
+
} catch {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
writeUserId(userId: string | null) {
|
|
179
|
+
write("user", userId === null ? null : JSON.stringify({ userId }));
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export type InsightsState = ReturnType<typeof createInsightsState>;
|