@hot-updater/plugin-insights 1.0.0-rc.20
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 +33 -0
- package/README.md +24 -0
- package/client/package.json +7 -0
- package/dist/client/eventId.cjs +181 -0
- package/dist/client/eventId.mjs +179 -0
- package/dist/client/index.cjs +234 -0
- package/dist/client/index.d.cts +42 -0
- package/dist/client/index.d.mts +42 -0
- package/dist/client/index.mjs +233 -0
- package/dist/client/sender.cjs +103 -0
- package/dist/client/sender.mjs +103 -0
- package/dist/client/state.cjs +121 -0
- package/dist/client/state.mjs +119 -0
- package/dist/internal.cjs +9 -0
- package/dist/internal.d.cts +5 -0
- package/dist/internal.d.mts +5 -0
- package/dist/internal.mjs +5 -0
- package/dist/server/contract.cjs +193 -0
- package/dist/server/contract.mjs +189 -0
- package/dist/server/domain.d.cts +149 -0
- package/dist/server/domain.d.mts +149 -0
- package/dist/server/errors.cjs +15 -0
- package/dist/server/errors.d.cts +6 -0
- package/dist/server/errors.d.mts +6 -0
- package/dist/server/errors.mjs +14 -0
- package/dist/server/eventInput.cjs +284 -0
- package/dist/server/eventInput.d.cts +6 -0
- package/dist/server/eventInput.d.mts +6 -0
- package/dist/server/eventInput.mjs +282 -0
- package/dist/server/eventRow.cjs +49 -0
- package/dist/server/eventRow.d.cts +64 -0
- package/dist/server/eventRow.d.mts +64 -0
- package/dist/server/eventRow.mjs +47 -0
- package/dist/server/index.cjs +128 -0
- package/dist/server/index.d.cts +394 -0
- package/dist/server/index.d.mts +394 -0
- package/dist/server/index.mjs +121 -0
- package/dist/server/model.cjs +17 -0
- package/dist/server/model.d.cts +10 -0
- package/dist/server/model.d.mts +10 -0
- package/dist/server/model.mjs +17 -0
- package/dist/server/modelTypes.d.cts +176 -0
- package/dist/server/modelTypes.d.mts +176 -0
- package/dist/server/overview.cjs +109 -0
- package/dist/server/overview.d.cts +15 -0
- package/dist/server/overview.d.mts +15 -0
- package/dist/server/overview.mjs +107 -0
- package/dist/server/provider.cjs +375 -0
- package/dist/server/provider.d.cts +6 -0
- package/dist/server/provider.d.mts +6 -0
- package/dist/server/provider.mjs +374 -0
- package/dist/server/queryInput.cjs +93 -0
- package/dist/server/queryInput.mjs +91 -0
- package/dist/server/reads.cjs +681 -0
- package/dist/server/reads.d.cts +60 -0
- package/dist/server/reads.d.mts +60 -0
- package/dist/server/reads.mjs +674 -0
- package/dist/server/recordEvent.cjs +319 -0
- package/dist/server/recordEvent.d.cts +17 -0
- package/dist/server/recordEvent.d.mts +17 -0
- package/dist/server/recordEvent.mjs +316 -0
- package/dist/server/routes.cjs +96 -0
- package/dist/server/routes.d.cts +36 -0
- package/dist/server/routes.d.mts +36 -0
- package/dist/server/routes.mjs +95 -0
- package/dist/server/schema.cjs +489 -0
- package/dist/server/schema.d.cts +706 -0
- package/dist/server/schema.d.mts +706 -0
- package/dist/server/schema.mjs +483 -0
- package/dist/server/testing/eventRow.d.mts +64 -0
- package/dist/server/testing/expectInsightsIndex.mjs +18 -0
- package/dist/server/testing/fixtures.d.mts +8 -0
- package/dist/server/testing/fixtures.mjs +25 -0
- package/dist/server/testing/index.d.mts +24 -0
- package/dist/server/testing/index.mjs +25 -0
- package/dist/server/testing/insightsModelTests.mjs +714 -0
- package/dist/server/testing/modelTypes.d.mts +176 -0
- package/dist/server/testing/setupInsightsHttpTestSuite.d.mts +11 -0
- package/dist/server/testing/setupInsightsHttpTestSuite.mjs +107 -0
- package/dist/server/testing/setupInsightsModelTestSuite.d.mts +11 -0
- package/dist/server/testing/setupInsightsModelTestSuite.mjs +31 -0
- package/dist/server/testing/types.d.mts +26 -0
- package/dist/server/types.d.cts +33 -0
- package/dist/server/types.d.mts +33 -0
- package/package.json +68 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { BundleEventFailure, BundleEventFailureReason, BundleEventFailureStage, DatabaseBundleEventMetadata } from "./eventRow.cjs";
|
|
2
|
+
import { InsightsScope } from "./modelTypes.cjs";
|
|
3
|
+
//#region src/server/domain.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Where an update failed and why, as a report carries it. Unknown values of
|
|
6
|
+
* its sets read as `unknown`, so a newer client's failure still counts.
|
|
7
|
+
*/
|
|
8
|
+
type BundleEventFailureInput = {
|
|
9
|
+
readonly stage: BundleEventFailureStage;
|
|
10
|
+
readonly reason: BundleEventFailureReason;
|
|
11
|
+
readonly resource?: NonNullable<BundleEventFailure["resource"]>;
|
|
12
|
+
/** The response status of an `http` failure. */
|
|
13
|
+
readonly httpStatus?: number;
|
|
14
|
+
readonly transport?: NonNullable<BundleEventFailure["transport"]>;
|
|
15
|
+
/** The storage origin's error code, such as `ExpiredToken`. */
|
|
16
|
+
readonly originCode?: string;
|
|
17
|
+
/** Android's `ApplicationExitInfo` reason for the process before this one. */
|
|
18
|
+
readonly previousProcessExit?: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The body of `POST /events`. Fields the server does not know are ignored, so
|
|
22
|
+
* a newer client's report still records on an older server.
|
|
23
|
+
*/
|
|
24
|
+
type CreateBundleEventRequestBase = {
|
|
25
|
+
/**
|
|
26
|
+
* The report's idempotency key: a canonical lowercase UUIDv7 that the
|
|
27
|
+
* client creates once per report and repeats on every retry. The server
|
|
28
|
+
* stores the report under it, so a retried report counts once, and a
|
|
29
|
+
* report under an ID already stored changes nothing. Without it, the
|
|
30
|
+
* server creates the ID, and a retry counts as another report.
|
|
31
|
+
*/
|
|
32
|
+
readonly eventId?: string;
|
|
33
|
+
readonly installId: string;
|
|
34
|
+
readonly toBundleId: string;
|
|
35
|
+
readonly userId?: string;
|
|
36
|
+
readonly platform: "ios" | "android";
|
|
37
|
+
readonly appVersion: string;
|
|
38
|
+
readonly channel: string;
|
|
39
|
+
readonly cohort: string;
|
|
40
|
+
readonly fingerprintHash: string | null;
|
|
41
|
+
readonly sdkVersion?: string | null;
|
|
42
|
+
readonly fromReleaseId: string | null;
|
|
43
|
+
readonly toReleaseId: string | null;
|
|
44
|
+
};
|
|
45
|
+
type Movement = CreateBundleEventRequestBase & {
|
|
46
|
+
readonly fromBundleId: string;
|
|
47
|
+
readonly updateStrategy: "fingerprint" | "appVersion";
|
|
48
|
+
};
|
|
49
|
+
type CreateBundleEventRequest = (Movement & {
|
|
50
|
+
readonly type: "UPDATE_DOWNLOADED";
|
|
51
|
+
readonly metadata?: {
|
|
52
|
+
/** How the bundle arrived. */
|
|
53
|
+
readonly delivery?: NonNullable<DatabaseBundleEventMetadata["delivery"]>;
|
|
54
|
+
/** A patch was tried, and the files or the archive came instead. */
|
|
55
|
+
readonly patchFallback?: true;
|
|
56
|
+
};
|
|
57
|
+
}) | (Movement & {
|
|
58
|
+
readonly type: "UPDATE_APPLIED";
|
|
59
|
+
}) | (Movement & {
|
|
60
|
+
readonly type: "RECOVERED";
|
|
61
|
+
readonly metadata?: {
|
|
62
|
+
/** Android's `ApplicationExitInfo` reason for the crashed process. */
|
|
63
|
+
readonly previousProcessExit?: string;
|
|
64
|
+
};
|
|
65
|
+
}) | (Movement & {
|
|
66
|
+
/**
|
|
67
|
+
* An update check, download, or install that failed: `fromBundleId`
|
|
68
|
+
* runs, and `toBundleId` is the target (for a check, the running bundle).
|
|
69
|
+
*/
|
|
70
|
+
readonly type: "UPDATE_FAILED";
|
|
71
|
+
readonly metadata: {
|
|
72
|
+
readonly failure: BundleEventFailureInput;
|
|
73
|
+
};
|
|
74
|
+
}) | (CreateBundleEventRequestBase & {
|
|
75
|
+
readonly type: "UNCHANGED";
|
|
76
|
+
readonly fromBundleId: null;
|
|
77
|
+
readonly updateStrategy: null;
|
|
78
|
+
});
|
|
79
|
+
type ActiveInstallationWindow = "24h" | "7d" | "30d";
|
|
80
|
+
type EventHistoryRow = {
|
|
81
|
+
readonly id: string;
|
|
82
|
+
readonly installId: string;
|
|
83
|
+
readonly type: "UPDATE_DOWNLOADED" | "UPDATE_APPLIED" | "RECOVERED" | "UPDATE_FAILED" | "UNCHANGED";
|
|
84
|
+
readonly fromBundleId: string | null;
|
|
85
|
+
readonly toBundleId: string;
|
|
86
|
+
readonly userId: string | null;
|
|
87
|
+
readonly platform: "ios" | "android";
|
|
88
|
+
readonly appVersion: string;
|
|
89
|
+
readonly channel: string;
|
|
90
|
+
readonly cohort: string;
|
|
91
|
+
readonly receivedAtMs: number;
|
|
92
|
+
/** `UPDATE_FAILED`: where the update failed and why. */
|
|
93
|
+
readonly failure?: BundleEventFailureInput;
|
|
94
|
+
/** `UPDATE_DOWNLOADED`: how the bundle arrived, when the client said. */
|
|
95
|
+
readonly delivery?: NonNullable<DatabaseBundleEventMetadata["delivery"]>;
|
|
96
|
+
/** `UPDATE_DOWNLOADED`: a patch failed, and the files or the archive came instead. */
|
|
97
|
+
readonly patchFallback?: true;
|
|
98
|
+
/** `RECOVERED`: Android's reason the crashed process exited. */
|
|
99
|
+
readonly previousProcessExit?: string;
|
|
100
|
+
};
|
|
101
|
+
type InstallationHistoryRow = EventHistoryRow & {
|
|
102
|
+
readonly type: "UPDATE_DOWNLOADED" | "UPDATE_APPLIED" | "RECOVERED" | "UPDATE_FAILED";
|
|
103
|
+
readonly fromBundleId: string;
|
|
104
|
+
};
|
|
105
|
+
type InstallationRow = {
|
|
106
|
+
readonly installId: string;
|
|
107
|
+
readonly userId: string | null;
|
|
108
|
+
readonly lastKnownBundleId: string;
|
|
109
|
+
readonly pendingBundleId: string | null;
|
|
110
|
+
readonly pendingReleaseId: string | null;
|
|
111
|
+
readonly latestStatus: EventHistoryRow["type"];
|
|
112
|
+
readonly platform: "ios" | "android";
|
|
113
|
+
readonly appVersion: string;
|
|
114
|
+
readonly channel: string;
|
|
115
|
+
readonly cohort: string;
|
|
116
|
+
readonly receivedAtMs: number;
|
|
117
|
+
};
|
|
118
|
+
type CursorPage<T> = {
|
|
119
|
+
readonly data: readonly T[];
|
|
120
|
+
readonly nextCursor: string | null;
|
|
121
|
+
};
|
|
122
|
+
type EventCursorPage<T extends EventHistoryRow> = CursorPage<T> & {
|
|
123
|
+
readonly beforeReceivedAtMs: number;
|
|
124
|
+
};
|
|
125
|
+
type InsightsBundleSelection = InsightsScope & {
|
|
126
|
+
readonly bundleId: string;
|
|
127
|
+
/** `failed`: update failures that targeted the bundle. */
|
|
128
|
+
readonly outcome: "downloaded" | "applied" | "recovered" | "failed";
|
|
129
|
+
};
|
|
130
|
+
type InsightsCountMeasurement = {
|
|
131
|
+
readonly count: number;
|
|
132
|
+
readonly measuredAtMs: number;
|
|
133
|
+
};
|
|
134
|
+
type ReportingOverview = InsightsScope & {
|
|
135
|
+
readonly window: ActiveInstallationWindow;
|
|
136
|
+
readonly sinceMs: number;
|
|
137
|
+
readonly beforeReceivedAtMs: number;
|
|
138
|
+
readonly reportingInstallations: InsightsCountMeasurement;
|
|
139
|
+
readonly bundle?: {
|
|
140
|
+
readonly bundleId: string;
|
|
141
|
+
readonly reportingInstallations: InsightsCountMeasurement;
|
|
142
|
+
readonly downloadedReports: InsightsCountMeasurement;
|
|
143
|
+
readonly appliedReports: InsightsCountMeasurement;
|
|
144
|
+
readonly recoveredReports: InsightsCountMeasurement;
|
|
145
|
+
readonly failedReports: InsightsCountMeasurement;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
//#endregion
|
|
149
|
+
export { ActiveInstallationWindow, BundleEventFailureInput, CreateBundleEventRequest, CreateBundleEventRequestBase, CursorPage, EventCursorPage, EventHistoryRow, InsightsBundleSelection, InsightsCountMeasurement, InstallationHistoryRow, InstallationRow, ReportingOverview };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { BundleEventFailure, BundleEventFailureReason, BundleEventFailureStage, DatabaseBundleEventMetadata } from "./eventRow.mjs";
|
|
2
|
+
import { InsightsScope } from "./modelTypes.mjs";
|
|
3
|
+
//#region src/server/domain.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Where an update failed and why, as a report carries it. Unknown values of
|
|
6
|
+
* its sets read as `unknown`, so a newer client's failure still counts.
|
|
7
|
+
*/
|
|
8
|
+
type BundleEventFailureInput = {
|
|
9
|
+
readonly stage: BundleEventFailureStage;
|
|
10
|
+
readonly reason: BundleEventFailureReason;
|
|
11
|
+
readonly resource?: NonNullable<BundleEventFailure["resource"]>;
|
|
12
|
+
/** The response status of an `http` failure. */
|
|
13
|
+
readonly httpStatus?: number;
|
|
14
|
+
readonly transport?: NonNullable<BundleEventFailure["transport"]>;
|
|
15
|
+
/** The storage origin's error code, such as `ExpiredToken`. */
|
|
16
|
+
readonly originCode?: string;
|
|
17
|
+
/** Android's `ApplicationExitInfo` reason for the process before this one. */
|
|
18
|
+
readonly previousProcessExit?: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The body of `POST /events`. Fields the server does not know are ignored, so
|
|
22
|
+
* a newer client's report still records on an older server.
|
|
23
|
+
*/
|
|
24
|
+
type CreateBundleEventRequestBase = {
|
|
25
|
+
/**
|
|
26
|
+
* The report's idempotency key: a canonical lowercase UUIDv7 that the
|
|
27
|
+
* client creates once per report and repeats on every retry. The server
|
|
28
|
+
* stores the report under it, so a retried report counts once, and a
|
|
29
|
+
* report under an ID already stored changes nothing. Without it, the
|
|
30
|
+
* server creates the ID, and a retry counts as another report.
|
|
31
|
+
*/
|
|
32
|
+
readonly eventId?: string;
|
|
33
|
+
readonly installId: string;
|
|
34
|
+
readonly toBundleId: string;
|
|
35
|
+
readonly userId?: string;
|
|
36
|
+
readonly platform: "ios" | "android";
|
|
37
|
+
readonly appVersion: string;
|
|
38
|
+
readonly channel: string;
|
|
39
|
+
readonly cohort: string;
|
|
40
|
+
readonly fingerprintHash: string | null;
|
|
41
|
+
readonly sdkVersion?: string | null;
|
|
42
|
+
readonly fromReleaseId: string | null;
|
|
43
|
+
readonly toReleaseId: string | null;
|
|
44
|
+
};
|
|
45
|
+
type Movement = CreateBundleEventRequestBase & {
|
|
46
|
+
readonly fromBundleId: string;
|
|
47
|
+
readonly updateStrategy: "fingerprint" | "appVersion";
|
|
48
|
+
};
|
|
49
|
+
type CreateBundleEventRequest = (Movement & {
|
|
50
|
+
readonly type: "UPDATE_DOWNLOADED";
|
|
51
|
+
readonly metadata?: {
|
|
52
|
+
/** How the bundle arrived. */
|
|
53
|
+
readonly delivery?: NonNullable<DatabaseBundleEventMetadata["delivery"]>;
|
|
54
|
+
/** A patch was tried, and the files or the archive came instead. */
|
|
55
|
+
readonly patchFallback?: true;
|
|
56
|
+
};
|
|
57
|
+
}) | (Movement & {
|
|
58
|
+
readonly type: "UPDATE_APPLIED";
|
|
59
|
+
}) | (Movement & {
|
|
60
|
+
readonly type: "RECOVERED";
|
|
61
|
+
readonly metadata?: {
|
|
62
|
+
/** Android's `ApplicationExitInfo` reason for the crashed process. */
|
|
63
|
+
readonly previousProcessExit?: string;
|
|
64
|
+
};
|
|
65
|
+
}) | (Movement & {
|
|
66
|
+
/**
|
|
67
|
+
* An update check, download, or install that failed: `fromBundleId`
|
|
68
|
+
* runs, and `toBundleId` is the target (for a check, the running bundle).
|
|
69
|
+
*/
|
|
70
|
+
readonly type: "UPDATE_FAILED";
|
|
71
|
+
readonly metadata: {
|
|
72
|
+
readonly failure: BundleEventFailureInput;
|
|
73
|
+
};
|
|
74
|
+
}) | (CreateBundleEventRequestBase & {
|
|
75
|
+
readonly type: "UNCHANGED";
|
|
76
|
+
readonly fromBundleId: null;
|
|
77
|
+
readonly updateStrategy: null;
|
|
78
|
+
});
|
|
79
|
+
type ActiveInstallationWindow = "24h" | "7d" | "30d";
|
|
80
|
+
type EventHistoryRow = {
|
|
81
|
+
readonly id: string;
|
|
82
|
+
readonly installId: string;
|
|
83
|
+
readonly type: "UPDATE_DOWNLOADED" | "UPDATE_APPLIED" | "RECOVERED" | "UPDATE_FAILED" | "UNCHANGED";
|
|
84
|
+
readonly fromBundleId: string | null;
|
|
85
|
+
readonly toBundleId: string;
|
|
86
|
+
readonly userId: string | null;
|
|
87
|
+
readonly platform: "ios" | "android";
|
|
88
|
+
readonly appVersion: string;
|
|
89
|
+
readonly channel: string;
|
|
90
|
+
readonly cohort: string;
|
|
91
|
+
readonly receivedAtMs: number;
|
|
92
|
+
/** `UPDATE_FAILED`: where the update failed and why. */
|
|
93
|
+
readonly failure?: BundleEventFailureInput;
|
|
94
|
+
/** `UPDATE_DOWNLOADED`: how the bundle arrived, when the client said. */
|
|
95
|
+
readonly delivery?: NonNullable<DatabaseBundleEventMetadata["delivery"]>;
|
|
96
|
+
/** `UPDATE_DOWNLOADED`: a patch failed, and the files or the archive came instead. */
|
|
97
|
+
readonly patchFallback?: true;
|
|
98
|
+
/** `RECOVERED`: Android's reason the crashed process exited. */
|
|
99
|
+
readonly previousProcessExit?: string;
|
|
100
|
+
};
|
|
101
|
+
type InstallationHistoryRow = EventHistoryRow & {
|
|
102
|
+
readonly type: "UPDATE_DOWNLOADED" | "UPDATE_APPLIED" | "RECOVERED" | "UPDATE_FAILED";
|
|
103
|
+
readonly fromBundleId: string;
|
|
104
|
+
};
|
|
105
|
+
type InstallationRow = {
|
|
106
|
+
readonly installId: string;
|
|
107
|
+
readonly userId: string | null;
|
|
108
|
+
readonly lastKnownBundleId: string;
|
|
109
|
+
readonly pendingBundleId: string | null;
|
|
110
|
+
readonly pendingReleaseId: string | null;
|
|
111
|
+
readonly latestStatus: EventHistoryRow["type"];
|
|
112
|
+
readonly platform: "ios" | "android";
|
|
113
|
+
readonly appVersion: string;
|
|
114
|
+
readonly channel: string;
|
|
115
|
+
readonly cohort: string;
|
|
116
|
+
readonly receivedAtMs: number;
|
|
117
|
+
};
|
|
118
|
+
type CursorPage<T> = {
|
|
119
|
+
readonly data: readonly T[];
|
|
120
|
+
readonly nextCursor: string | null;
|
|
121
|
+
};
|
|
122
|
+
type EventCursorPage<T extends EventHistoryRow> = CursorPage<T> & {
|
|
123
|
+
readonly beforeReceivedAtMs: number;
|
|
124
|
+
};
|
|
125
|
+
type InsightsBundleSelection = InsightsScope & {
|
|
126
|
+
readonly bundleId: string;
|
|
127
|
+
/** `failed`: update failures that targeted the bundle. */
|
|
128
|
+
readonly outcome: "downloaded" | "applied" | "recovered" | "failed";
|
|
129
|
+
};
|
|
130
|
+
type InsightsCountMeasurement = {
|
|
131
|
+
readonly count: number;
|
|
132
|
+
readonly measuredAtMs: number;
|
|
133
|
+
};
|
|
134
|
+
type ReportingOverview = InsightsScope & {
|
|
135
|
+
readonly window: ActiveInstallationWindow;
|
|
136
|
+
readonly sinceMs: number;
|
|
137
|
+
readonly beforeReceivedAtMs: number;
|
|
138
|
+
readonly reportingInstallations: InsightsCountMeasurement;
|
|
139
|
+
readonly bundle?: {
|
|
140
|
+
readonly bundleId: string;
|
|
141
|
+
readonly reportingInstallations: InsightsCountMeasurement;
|
|
142
|
+
readonly downloadedReports: InsightsCountMeasurement;
|
|
143
|
+
readonly appliedReports: InsightsCountMeasurement;
|
|
144
|
+
readonly recoveredReports: InsightsCountMeasurement;
|
|
145
|
+
readonly failedReports: InsightsCountMeasurement;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
//#endregion
|
|
149
|
+
export { ActiveInstallationWindow, BundleEventFailureInput, CreateBundleEventRequest, CreateBundleEventRequestBase, CursorPage, EventCursorPage, EventHistoryRow, InsightsBundleSelection, InsightsCountMeasurement, InstallationHistoryRow, InstallationRow, ReportingOverview };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/server/errors.ts
|
|
2
|
+
var InsightsBadRequestError = class extends Error {
|
|
3
|
+
name = "InsightsBadRequestError";
|
|
4
|
+
};
|
|
5
|
+
var InsightsPayloadTooLargeError = class extends Error {
|
|
6
|
+
maximumBytes;
|
|
7
|
+
name = "InsightsPayloadTooLargeError";
|
|
8
|
+
constructor(maximumBytes) {
|
|
9
|
+
super(`Event payload exceeds ${maximumBytes} bytes`);
|
|
10
|
+
this.maximumBytes = maximumBytes;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
exports.InsightsBadRequestError = InsightsBadRequestError;
|
|
15
|
+
exports.InsightsPayloadTooLargeError = InsightsPayloadTooLargeError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/server/errors.ts
|
|
2
|
+
var InsightsBadRequestError = class extends Error {
|
|
3
|
+
name = "InsightsBadRequestError";
|
|
4
|
+
};
|
|
5
|
+
var InsightsPayloadTooLargeError = class extends Error {
|
|
6
|
+
maximumBytes;
|
|
7
|
+
name = "InsightsPayloadTooLargeError";
|
|
8
|
+
constructor(maximumBytes) {
|
|
9
|
+
super(`Event payload exceeds ${maximumBytes} bytes`);
|
|
10
|
+
this.maximumBytes = maximumBytes;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { InsightsBadRequestError, InsightsPayloadTooLargeError };
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
const require_errors = require("./errors.cjs");
|
|
2
|
+
let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
|
|
3
|
+
//#region src/server/eventInput.ts
|
|
4
|
+
const MAX_EVENT_STRING_LENGTH = 1024;
|
|
5
|
+
const MAX_IDENTITY_LENGTH = 255;
|
|
6
|
+
const EVENT_BODY_MAX_BYTES = 16384;
|
|
7
|
+
function isRecord(value) {
|
|
8
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
function requireStringField(payload, key) {
|
|
11
|
+
const value = payload[key];
|
|
12
|
+
if (typeof value !== "string" || value.length === 0 || value.length > MAX_EVENT_STRING_LENGTH || new TextDecoder("utf-8", { ignoreBOM: true }).decode(new TextEncoder().encode(value)) !== value) throw new require_errors.InsightsBadRequestError(`Invalid event field: ${key}`);
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
function requireNullableStringField(payload, key) {
|
|
16
|
+
if (payload[key] === null) return null;
|
|
17
|
+
return requireStringField(payload, key);
|
|
18
|
+
}
|
|
19
|
+
function requireIdentityField(payload, key) {
|
|
20
|
+
const value = requireStringField(payload, key);
|
|
21
|
+
if (value.length > MAX_IDENTITY_LENGTH) throw new require_errors.InsightsBadRequestError(`Invalid event field: ${key}`);
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The report's idempotency key, when the client sends one: it creates the
|
|
26
|
+
* UUIDv7 once and repeats it on every retry of that report.
|
|
27
|
+
*/
|
|
28
|
+
function readEventId(payload) {
|
|
29
|
+
const value = payload.eventId;
|
|
30
|
+
if (value === void 0) return void 0;
|
|
31
|
+
if (!(0, _hot_updater_plugin_core.isUUIDv7)(value)) throw new require_errors.InsightsBadRequestError("Invalid event field: eventId");
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
/** One value of an open set: any other value reads as `unknown`. */
|
|
35
|
+
const oneOf = (values, value) => values.includes(value) ? value : "unknown";
|
|
36
|
+
const STAGES = [
|
|
37
|
+
"check",
|
|
38
|
+
"download",
|
|
39
|
+
"install"
|
|
40
|
+
];
|
|
41
|
+
const REASONS = [
|
|
42
|
+
"network",
|
|
43
|
+
"http",
|
|
44
|
+
"invalid_response",
|
|
45
|
+
"hash_mismatch",
|
|
46
|
+
"signature",
|
|
47
|
+
"patch",
|
|
48
|
+
"extract",
|
|
49
|
+
"storage"
|
|
50
|
+
];
|
|
51
|
+
const RESOURCES = [
|
|
52
|
+
"catalog",
|
|
53
|
+
"artifact",
|
|
54
|
+
"manifest",
|
|
55
|
+
"file",
|
|
56
|
+
"patch",
|
|
57
|
+
"archive"
|
|
58
|
+
];
|
|
59
|
+
const TRANSPORTS = [
|
|
60
|
+
"timeout",
|
|
61
|
+
"dns",
|
|
62
|
+
"tls",
|
|
63
|
+
"connection",
|
|
64
|
+
"offline",
|
|
65
|
+
"cancelled"
|
|
66
|
+
];
|
|
67
|
+
const DELIVERIES = [
|
|
68
|
+
"patch",
|
|
69
|
+
"manifest",
|
|
70
|
+
"archive"
|
|
71
|
+
];
|
|
72
|
+
/** A storage error code or an Android exit reason: letters, digits, and `._-`. */
|
|
73
|
+
const CODE = /^[A-Za-z0-9._-]{1,64}$/;
|
|
74
|
+
const readCode = (value) => typeof value === "string" && CODE.test(value) ? value : void 0;
|
|
75
|
+
const readMetadata = (payload) => isRecord(payload.metadata) ? payload.metadata : {};
|
|
76
|
+
/**
|
|
77
|
+
* What an UPDATE_FAILED report says failed. It never refuses the report: an
|
|
78
|
+
* unknown value of a set reads as `unknown`, a missing or malformed failure
|
|
79
|
+
* as an unknown stage and reason, and a malformed optional field is left out,
|
|
80
|
+
* as is a null one.
|
|
81
|
+
*/
|
|
82
|
+
function readFailure(value) {
|
|
83
|
+
const failure = isRecord(value) ? value : {};
|
|
84
|
+
const httpStatus = failure.httpStatus;
|
|
85
|
+
const originCode = readCode(failure.originCode);
|
|
86
|
+
const previousProcessExit = readCode(failure.previousProcessExit);
|
|
87
|
+
return {
|
|
88
|
+
stage: oneOf(STAGES, failure.stage),
|
|
89
|
+
reason: oneOf(REASONS, failure.reason),
|
|
90
|
+
...failure.resource == null ? {} : { resource: oneOf(RESOURCES, failure.resource) },
|
|
91
|
+
...Number.isSafeInteger(httpStatus) && httpStatus >= 100 && httpStatus <= 599 ? { httpStatus } : {},
|
|
92
|
+
...failure.transport == null ? {} : { transport: oneOf(TRANSPORTS, failure.transport) },
|
|
93
|
+
...originCode === void 0 ? {} : { originCode },
|
|
94
|
+
...previousProcessExit === void 0 ? {} : { previousProcessExit }
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
async function readBoundedText(request) {
|
|
98
|
+
const contentLength = request.headers.get("content-length");
|
|
99
|
+
const declaredByteLength = Number(contentLength);
|
|
100
|
+
if (contentLength !== null && Number.isSafeInteger(declaredByteLength) && declaredByteLength > 16384) throw new require_errors.InsightsPayloadTooLargeError(EVENT_BODY_MAX_BYTES);
|
|
101
|
+
if (request.body === null) return "";
|
|
102
|
+
const reader = request.body.getReader();
|
|
103
|
+
const decoder = new TextDecoder();
|
|
104
|
+
let byteLength = 0;
|
|
105
|
+
let text = "";
|
|
106
|
+
while (true) {
|
|
107
|
+
const result = await reader.read();
|
|
108
|
+
if (result.done) break;
|
|
109
|
+
byteLength += result.value.byteLength;
|
|
110
|
+
if (byteLength > 16384) {
|
|
111
|
+
await reader.cancel();
|
|
112
|
+
throw new require_errors.InsightsPayloadTooLargeError(EVENT_BODY_MAX_BYTES);
|
|
113
|
+
}
|
|
114
|
+
text += decoder.decode(result.value, { stream: true });
|
|
115
|
+
}
|
|
116
|
+
return text + decoder.decode();
|
|
117
|
+
}
|
|
118
|
+
async function parseJson(request) {
|
|
119
|
+
const text = await readBoundedText(request);
|
|
120
|
+
try {
|
|
121
|
+
return JSON.parse(text);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (error instanceof SyntaxError) throw new require_errors.InsightsBadRequestError("Invalid event payload");
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function requireEvent(payload) {
|
|
128
|
+
if (!isRecord(payload)) throw new require_errors.InsightsBadRequestError("Invalid event payload");
|
|
129
|
+
const platform = requireStringField(payload, "platform");
|
|
130
|
+
if (platform !== "ios" && platform !== "android") throw new require_errors.InsightsBadRequestError("Invalid event field: platform");
|
|
131
|
+
const eventId = readEventId(payload);
|
|
132
|
+
const base = {
|
|
133
|
+
...eventId === void 0 ? {} : { eventId },
|
|
134
|
+
installId: requireIdentityField(payload, "installId"),
|
|
135
|
+
toBundleId: requireStringField(payload, "toBundleId"),
|
|
136
|
+
...payload.userId === void 0 ? {} : { userId: requireIdentityField(payload, "userId") },
|
|
137
|
+
platform,
|
|
138
|
+
appVersion: requireStringField(payload, "appVersion"),
|
|
139
|
+
channel: requireStringField(payload, "channel"),
|
|
140
|
+
cohort: requireStringField(payload, "cohort"),
|
|
141
|
+
fingerprintHash: requireNullableStringField(payload, "fingerprintHash"),
|
|
142
|
+
sdkVersion: payload.sdkVersion === void 0 ? null : requireNullableStringField(payload, "sdkVersion"),
|
|
143
|
+
fromReleaseId: requireNullableStringField(payload, "fromReleaseId"),
|
|
144
|
+
toReleaseId: requireNullableStringField(payload, "toReleaseId")
|
|
145
|
+
};
|
|
146
|
+
const type = requireStringField(payload, "type");
|
|
147
|
+
const movement = () => {
|
|
148
|
+
const updateStrategy = requireStringField(payload, "updateStrategy");
|
|
149
|
+
if (updateStrategy !== "fingerprint" && updateStrategy !== "appVersion") throw new require_errors.InsightsBadRequestError("Invalid event field: updateStrategy");
|
|
150
|
+
return {
|
|
151
|
+
...base,
|
|
152
|
+
fromBundleId: requireStringField(payload, "fromBundleId"),
|
|
153
|
+
updateStrategy
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
const metadata = readMetadata(payload);
|
|
157
|
+
switch (type) {
|
|
158
|
+
case "UPDATE_DOWNLOADED": {
|
|
159
|
+
const delivery = metadata.delivery;
|
|
160
|
+
const read = {
|
|
161
|
+
...delivery == null ? {} : { delivery: oneOf(DELIVERIES, delivery) },
|
|
162
|
+
...metadata.patchFallback === true ? { patchFallback: true } : {}
|
|
163
|
+
};
|
|
164
|
+
return {
|
|
165
|
+
...movement(),
|
|
166
|
+
type,
|
|
167
|
+
...Object.keys(read).length === 0 ? {} : { metadata: read }
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
case "UPDATE_APPLIED": return {
|
|
171
|
+
...movement(),
|
|
172
|
+
type
|
|
173
|
+
};
|
|
174
|
+
case "RECOVERED": {
|
|
175
|
+
const previousProcessExit = readCode(metadata.previousProcessExit);
|
|
176
|
+
return {
|
|
177
|
+
...movement(),
|
|
178
|
+
type,
|
|
179
|
+
...previousProcessExit === void 0 ? {} : { metadata: { previousProcessExit } }
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
case "UPDATE_FAILED": return {
|
|
183
|
+
...movement(),
|
|
184
|
+
type,
|
|
185
|
+
metadata: { failure: readFailure(metadata.failure) }
|
|
186
|
+
};
|
|
187
|
+
case "UNCHANGED":
|
|
188
|
+
if (payload.fromBundleId !== null || payload.updateStrategy !== null) throw new require_errors.InsightsBadRequestError("Invalid unchanged event shape");
|
|
189
|
+
return {
|
|
190
|
+
...base,
|
|
191
|
+
type,
|
|
192
|
+
fromBundleId: null,
|
|
193
|
+
updateStrategy: null
|
|
194
|
+
};
|
|
195
|
+
default: throw new require_errors.InsightsBadRequestError("Invalid event field: type");
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async function parseBundleEventRequest(request) {
|
|
199
|
+
return requireEvent(await parseJson(request));
|
|
200
|
+
}
|
|
201
|
+
function createBundleEventRow(input) {
|
|
202
|
+
input = requireEvent(input);
|
|
203
|
+
const base = {
|
|
204
|
+
app_version: input.appVersion,
|
|
205
|
+
channel: input.channel,
|
|
206
|
+
from_release_id: input.fromReleaseId,
|
|
207
|
+
id: input.eventId ?? (0, _hot_updater_plugin_core.createUUIDv7)(),
|
|
208
|
+
install_id: input.installId,
|
|
209
|
+
platform: input.platform,
|
|
210
|
+
received_at_ms: Date.now(),
|
|
211
|
+
to_bundle_id: input.toBundleId,
|
|
212
|
+
to_release_id: input.toReleaseId,
|
|
213
|
+
user_id: input.userId ?? null
|
|
214
|
+
};
|
|
215
|
+
const metadata = {
|
|
216
|
+
cohort: input.cohort,
|
|
217
|
+
fingerprint_hash: input.fingerprintHash,
|
|
218
|
+
sdk_version: input.sdkVersion ?? null,
|
|
219
|
+
update_strategy: input.updateStrategy
|
|
220
|
+
};
|
|
221
|
+
switch (input.type) {
|
|
222
|
+
case "UPDATE_DOWNLOADED": {
|
|
223
|
+
const { delivery, patchFallback } = input.metadata ?? {};
|
|
224
|
+
return {
|
|
225
|
+
...base,
|
|
226
|
+
from_bundle_id: input.fromBundleId,
|
|
227
|
+
type: input.type,
|
|
228
|
+
metadata: {
|
|
229
|
+
...metadata,
|
|
230
|
+
...delivery === void 0 ? {} : { delivery },
|
|
231
|
+
...patchFallback ? { patch_fallback: true } : {}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
case "UPDATE_APPLIED": return {
|
|
236
|
+
...base,
|
|
237
|
+
from_bundle_id: input.fromBundleId,
|
|
238
|
+
type: input.type,
|
|
239
|
+
metadata
|
|
240
|
+
};
|
|
241
|
+
case "RECOVERED": {
|
|
242
|
+
const exit = input.metadata?.previousProcessExit;
|
|
243
|
+
return {
|
|
244
|
+
...base,
|
|
245
|
+
from_bundle_id: input.fromBundleId,
|
|
246
|
+
type: input.type,
|
|
247
|
+
metadata: {
|
|
248
|
+
...metadata,
|
|
249
|
+
...exit === void 0 ? {} : { previous_process_exit: exit }
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
case "UPDATE_FAILED": {
|
|
254
|
+
const { failure } = input.metadata;
|
|
255
|
+
return {
|
|
256
|
+
...base,
|
|
257
|
+
from_bundle_id: input.fromBundleId,
|
|
258
|
+
type: input.type,
|
|
259
|
+
metadata: {
|
|
260
|
+
...metadata,
|
|
261
|
+
failure: {
|
|
262
|
+
stage: failure.stage,
|
|
263
|
+
reason: failure.reason,
|
|
264
|
+
...failure.resource === void 0 ? {} : { resource: failure.resource },
|
|
265
|
+
...failure.httpStatus === void 0 ? {} : { http_status: failure.httpStatus },
|
|
266
|
+
...failure.transport === void 0 ? {} : { transport: failure.transport },
|
|
267
|
+
...failure.originCode === void 0 ? {} : { origin_code: failure.originCode },
|
|
268
|
+
...failure.previousProcessExit === void 0 ? {} : { previous_process_exit: failure.previousProcessExit }
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
case "UNCHANGED": return {
|
|
274
|
+
...base,
|
|
275
|
+
from_bundle_id: null,
|
|
276
|
+
type: input.type,
|
|
277
|
+
metadata
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
//#endregion
|
|
282
|
+
exports.EVENT_BODY_MAX_BYTES = EVENT_BODY_MAX_BYTES;
|
|
283
|
+
exports.createBundleEventRow = createBundleEventRow;
|
|
284
|
+
exports.parseBundleEventRequest = parseBundleEventRequest;
|