@hot-updater/server 1.0.0-rc.16 → 1.0.0-rc.18
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/dist/adapters/drizzle.cjs +5 -5
- package/dist/adapters/drizzle.mjs +4 -4
- package/dist/adapters/kysely.cjs +3 -3
- package/dist/adapters/kysely.mjs +2 -2
- package/dist/adapters/mongodb.cjs +3 -3
- package/dist/adapters/mongodb.mjs +2 -2
- package/dist/adapters/mongodbAdapter.cjs +19 -1
- package/dist/adapters/mongodbAdapter.mjs +19 -1
- package/dist/adapters/prisma.cjs +5 -5
- package/dist/adapters/prisma.mjs +4 -4
- package/dist/assembly/assemblePlugins.cjs +47 -28
- package/dist/assembly/assemblePlugins.d.cts +3 -7
- package/dist/assembly/assemblePlugins.d.mts +3 -7
- package/dist/assembly/assemblePlugins.mjs +46 -27
- package/dist/assembly/clientPlugins.cjs +86 -0
- package/dist/assembly/clientPlugins.d.cts +16 -0
- package/dist/assembly/clientPlugins.d.mts +16 -0
- package/dist/assembly/clientPlugins.mjs +86 -0
- package/dist/assembly/configError.cjs +7 -0
- package/dist/assembly/configError.d.cts +7 -0
- package/dist/assembly/configError.d.mts +7 -0
- package/dist/assembly/configError.mjs +7 -0
- package/dist/assembly/databasePlugins.cjs +5 -1
- package/dist/assembly/databasePlugins.d.cts +5 -5
- package/dist/assembly/databasePlugins.d.mts +5 -5
- package/dist/assembly/databasePlugins.mjs +5 -1
- package/dist/assembly/pluginCli.cjs +125 -0
- package/dist/assembly/pluginCli.d.cts +59 -0
- package/dist/assembly/pluginCli.d.mts +59 -0
- package/dist/assembly/pluginCli.mjs +121 -0
- package/dist/createHotUpdaterCore.cjs +14 -16
- package/dist/createHotUpdaterCore.d.cts +10 -3
- package/dist/createHotUpdaterCore.d.mts +10 -3
- package/dist/createHotUpdaterCore.mjs +11 -13
- package/dist/database/aggregateBatching.cjs +626 -0
- package/dist/database/aggregateBatching.d.cts +10 -0
- package/dist/database/aggregateBatching.d.mts +10 -0
- package/dist/database/aggregateBatching.mjs +625 -0
- package/dist/{insights/errors.cjs → database/busy.cjs} +2 -15
- package/dist/database/busy.d.cts +12 -0
- package/dist/database/busy.d.mts +12 -0
- package/dist/{insights/errors.mjs → database/busy.mjs} +3 -14
- package/dist/database/database.cjs +22 -3
- package/dist/database/database.d.cts +15 -2
- package/dist/database/database.d.mts +15 -2
- package/dist/database/database.mjs +22 -3
- package/dist/database/definitions.d.cts +10 -0
- package/dist/database/definitions.d.mts +10 -0
- package/dist/database/engine.d.cts +2 -1
- package/dist/database/engine.d.mts +2 -1
- package/dist/database/engineAggregates.cjs +7 -4
- package/dist/database/engineAggregates.mjs +8 -5
- package/dist/database/engineReads.cjs +5 -3
- package/dist/database/engineReads.mjs +6 -4
- package/dist/database/engineTransaction.cjs +2 -6
- package/dist/database/engineTransaction.mjs +2 -6
- package/dist/database/fence.cjs +2 -1
- package/dist/database/fence.mjs +2 -1
- package/dist/database/index.cjs +17 -8
- package/dist/database/index.d.cts +5 -4
- package/dist/database/index.d.mts +5 -4
- package/dist/database/index.mjs +6 -5
- package/dist/database/kv/kvAdapter.cjs +15 -3
- package/dist/database/kv/kvAdapter.d.cts +8 -0
- package/dist/database/kv/kvAdapter.d.mts +8 -0
- package/dist/database/kv/kvAdapter.mjs +16 -4
- package/dist/database/resolveSchema.cjs +4 -1
- package/dist/database/resolveSchema.d.cts +1 -1
- package/dist/database/resolveSchema.d.mts +1 -1
- package/dist/database/resolveSchema.mjs +4 -1
- package/dist/database/retention.cjs +92 -0
- package/dist/database/retention.mjs +90 -0
- package/dist/database/schema.cjs +5 -2
- package/dist/database/schema.d.cts +30 -1
- package/dist/database/schema.d.mts +30 -1
- package/dist/database/schema.mjs +5 -2
- package/dist/database/sql/sqlAdapter.cjs +8 -0
- package/dist/database/sql/sqlAdapter.mjs +9 -1
- package/dist/database/sql/sqlSchema.cjs +56 -0
- package/dist/database/sql/sqlSchema.mjs +55 -1
- package/dist/db/coreDatabase.cjs +85 -0
- package/dist/db/coreDatabase.d.cts +57 -0
- package/dist/db/coreDatabase.d.mts +57 -0
- package/dist/db/coreDatabase.mjs +78 -0
- package/dist/db/index.cjs +11 -3
- package/dist/db/index.d.cts +7 -3
- package/dist/db/index.d.mts +7 -3
- package/dist/db/index.mjs +6 -3
- package/dist/db/types.d.cts +3 -3
- package/dist/db/types.d.mts +3 -3
- package/dist/diff.cjs +3 -0
- package/dist/diff.d.cts +2 -0
- package/dist/diff.d.mts +2 -0
- package/dist/diff.mjs +2 -0
- package/dist/handler.cjs +2 -14
- package/dist/handler.mjs +1 -13
- package/dist/handlerReleaseCatalogRoutes.cjs +21 -2
- package/dist/handlerReleaseCatalogRoutes.mjs +21 -2
- package/dist/index.cjs +2 -6
- package/dist/index.d.cts +4 -7
- package/dist/index.d.mts +4 -7
- package/dist/index.mjs +2 -4
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/{apiKeys.cjs → plugins/api-keys/apiKeys.cjs} +1 -1
- package/dist/{apiKeys.d.cts → plugins/api-keys/apiKeys.d.cts} +2 -2
- package/dist/{apiKeys.d.mts → plugins/api-keys/apiKeys.d.mts} +2 -2
- package/dist/{apiKeys.mjs → plugins/api-keys/apiKeys.mjs} +1 -1
- package/dist/plugins/api-keys/cli.cjs +115 -0
- package/dist/plugins/api-keys/cli.mjs +114 -0
- package/dist/plugins/api-keys/index.cjs +9 -6
- package/dist/plugins/api-keys/index.d.cts +4 -5
- package/dist/plugins/api-keys/index.d.mts +4 -5
- package/dist/plugins/api-keys/index.mjs +9 -7
- package/dist/plugins/api-keys/model.d.cts +20 -2
- package/dist/plugins/api-keys/model.d.mts +20 -2
- package/dist/plugins/definePlugin.cjs +1 -1
- package/dist/plugins/definePlugin.d.cts +130 -3
- package/dist/plugins/definePlugin.d.mts +130 -3
- package/dist/plugins/definePlugin.mjs +1 -1
- package/dist/plugins/index.cjs +25 -4
- package/dist/plugins/index.d.cts +7 -4
- package/dist/plugins/index.d.mts +7 -4
- package/dist/plugins/index.mjs +6 -4
- package/dist/plugins/insights/contract.cjs +193 -0
- package/dist/plugins/insights/contract.mjs +189 -0
- package/dist/{insights → plugins/insights}/domain.d.cts +60 -15
- package/dist/{insights → plugins/insights}/domain.d.mts +60 -15
- package/dist/plugins/insights/errors.cjs +15 -0
- package/dist/plugins/insights/errors.mjs +14 -0
- package/dist/{insights → plugins/insights}/eventInput.cjs +155 -22
- package/dist/{insights → plugins/insights}/eventInput.mjs +155 -22
- package/dist/plugins/insights/eventRow.cjs +49 -0
- package/dist/plugins/insights/eventRow.d.cts +64 -0
- package/dist/plugins/insights/eventRow.d.mts +64 -0
- package/dist/plugins/insights/eventRow.mjs +47 -0
- package/dist/plugins/insights/index.cjs +106 -26
- package/dist/plugins/insights/index.d.cts +130 -27
- package/dist/plugins/insights/index.d.mts +130 -27
- package/dist/plugins/insights/index.mjs +106 -29
- package/dist/plugins/insights/model.cjs +2 -2
- package/dist/plugins/insights/model.d.cts +1 -1
- package/dist/plugins/insights/model.d.mts +1 -1
- package/dist/plugins/insights/model.mjs +1 -1
- package/dist/plugins/insights/modelTypes.d.cts +176 -0
- package/dist/plugins/insights/modelTypes.d.mts +176 -0
- package/dist/plugins/insights/overview.cjs +109 -0
- package/dist/plugins/insights/overview.d.cts +15 -0
- package/dist/plugins/insights/overview.d.mts +15 -0
- package/dist/plugins/insights/overview.mjs +107 -0
- package/dist/{insights → plugins/insights}/provider.cjs +33 -14
- package/dist/{insights → plugins/insights}/provider.d.cts +2 -2
- package/dist/{insights → plugins/insights}/provider.d.mts +2 -2
- package/dist/{insights → plugins/insights}/provider.mjs +30 -11
- package/dist/{insights → plugins/insights}/queryInput.cjs +2 -2
- package/dist/{insights → plugins/insights}/queryInput.mjs +2 -2
- package/dist/plugins/insights/reads.cjs +298 -88
- package/dist/plugins/insights/reads.d.cts +61 -0
- package/dist/plugins/insights/reads.d.mts +61 -0
- package/dist/plugins/insights/reads.mjs +298 -90
- package/dist/plugins/insights/recordEvent.cjs +223 -39
- package/dist/plugins/insights/recordEvent.d.cts +9 -4
- package/dist/plugins/insights/recordEvent.d.mts +9 -4
- package/dist/plugins/insights/recordEvent.mjs +221 -37
- package/dist/{insights → plugins/insights}/routes.cjs +3 -30
- package/dist/{insights → plugins/insights}/routes.mjs +4 -28
- package/dist/plugins/insights/schema.cjs +182 -40
- package/dist/plugins/insights/schema.d.cts +446 -6
- package/dist/plugins/insights/schema.d.mts +446 -6
- package/dist/plugins/insights/schema.mjs +180 -41
- package/dist/plugins/insights/testing/eventRow.d.mts +64 -0
- package/dist/plugins/insights/testing/expectInsightsIndex.mjs +18 -0
- package/dist/plugins/insights/testing/fixtures.d.mts +8 -0
- package/dist/plugins/insights/testing/fixtures.mjs +25 -0
- package/dist/plugins/insights/testing/index.d.mts +24 -0
- package/dist/plugins/insights/testing/index.mjs +25 -0
- package/dist/plugins/insights/testing/insightsModelTests.mjs +714 -0
- package/dist/plugins/insights/testing/modelTypes.d.mts +176 -0
- package/dist/plugins/insights/testing/setupInsightsHttpTestSuite.d.mts +11 -0
- package/dist/plugins/insights/testing/setupInsightsHttpTestSuite.mjs +107 -0
- package/dist/plugins/insights/testing/setupInsightsModelTestSuite.d.mts +11 -0
- package/dist/plugins/insights/testing/setupInsightsModelTestSuite.mjs +31 -0
- package/dist/plugins/insights/testing/types.d.mts +26 -0
- package/dist/{insights → plugins/insights}/types.d.cts +3 -2
- package/dist/{insights → plugins/insights}/types.d.mts +3 -2
- package/package.json +18 -7
- package/src/adapters/drizzle.conformance.integration.spec.ts +2 -2
- package/src/adapters/drizzle.readBudgets.integration.spec.ts +6 -4
- package/src/adapters/drizzle.spec.ts +15 -7
- package/src/adapters/drizzle.ts +6 -6
- package/src/adapters/kysely.conformance.integration.spec.ts +2 -2
- package/src/adapters/kysely.readBudgets.integration.spec.ts +4 -2
- package/src/adapters/kysely.spec.ts +13 -9
- package/src/adapters/kysely.ts +2 -2
- package/src/adapters/mongodb.integration.spec.ts +98 -7
- package/src/adapters/mongodb.ts +2 -2
- package/src/adapters/mongodbAdapter.ts +14 -0
- package/src/adapters/prisma.conformance.integration.spec.ts +2 -2
- package/src/adapters/prisma.integration.spec.ts +16 -8
- package/src/adapters/prisma.readBudgets.integration.spec.ts +5 -2
- package/src/adapters/prisma.spec.ts +17 -9
- package/src/adapters/prisma.ts +6 -6
- package/src/adapters/prismaTestClients.ts +8 -1
- package/src/assembly/assemblePlugins.ts +73 -40
- package/src/assembly/assembly.spec.ts +63 -11
- package/src/assembly/assembly.types.spec.ts +50 -2
- package/src/assembly/clientPlugins.ts +113 -0
- package/src/assembly/configError.ts +4 -0
- package/src/assembly/databasePlugins.ts +11 -5
- package/src/assembly/pluginCli.spec.ts +254 -0
- package/src/assembly/pluginCli.ts +248 -0
- package/src/assembly/{builtInRoutes.spec.ts → pluginRoutes.spec.ts} +10 -37
- package/src/createHotUpdaterCore.ts +21 -17
- package/src/database/aggregateBatching.spec.ts +577 -0
- package/src/database/aggregateBatching.ts +803 -0
- package/src/{insights/errors.spec.ts → database/busy.spec.ts} +2 -5
- package/src/{insights/errors.ts → database/busy.ts} +1 -13
- package/src/database/database.ts +39 -8
- package/src/database/definitions.ts +4 -0
- package/src/database/engineAggregates.spec.ts +4 -7
- package/src/database/engineAggregates.ts +5 -9
- package/src/database/engineReads.ts +7 -5
- package/src/database/engineTransaction.ts +1 -7
- package/src/database/fence.spec.ts +5 -5
- package/src/database/fence.ts +4 -0
- package/src/database/index.ts +13 -7
- package/src/database/kv/kvAdapter.batching.integration.spec.ts +15 -0
- package/src/database/kv/kvAdapter.conformance.integration.spec.ts +2 -0
- package/src/database/kv/kvAdapter.spec.ts +128 -1
- package/src/database/kv/kvAdapter.ts +30 -1
- package/src/database/kv/kvTestStore.ts +28 -1
- package/src/database/resolveSchema.ts +4 -1
- package/src/database/retention.spec.ts +314 -0
- package/src/database/retention.ts +129 -0
- package/src/database/schema.ts +44 -0
- package/src/database/sql/sqlAdapter.integration.spec.ts +4 -7
- package/src/database/sql/sqlAdapter.readBudgets.integration.spec.ts +18 -4
- package/src/database/sql/sqlAdapter.spec.ts +91 -3
- package/src/database/sql/sqlAdapter.ts +20 -0
- package/src/database/sql/sqlSchema.ts +81 -0
- package/src/databaseTestSuite.spec.ts +9 -4
- package/src/db/coreDatabase.ts +153 -0
- package/src/db/engineSql.spec.ts +8 -7
- package/src/db/index.ts +19 -2
- package/src/db/pluginTables.spec.ts +57 -27
- package/src/db/types.ts +3 -3
- package/src/diff.ts +6 -0
- package/src/handler-standalone.integration.spec.ts +2 -3
- package/src/handler.ts +1 -32
- package/src/handlerReleaseCatalogRoutes.ts +26 -2
- package/src/index.ts +0 -10
- package/src/{apiKeys.integration.spec.ts → plugins/api-keys/apiKeys.integration.spec.ts} +4 -4
- package/src/{apiKeys.spec.ts → plugins/api-keys/apiKeys.spec.ts} +3 -3
- package/src/{apiKeys.ts → plugins/api-keys/apiKeys.ts} +1 -1
- package/src/plugins/api-keys/apiKeysPlugin.spec.ts +11 -15
- package/src/plugins/api-keys/cli.spec.ts +187 -0
- package/src/plugins/api-keys/cli.ts +140 -0
- package/src/plugins/api-keys/index.ts +48 -41
- package/src/plugins/api-keys/model.ts +21 -2
- package/src/plugins/definePlugin.ts +144 -2
- package/src/plugins/index.ts +20 -2
- package/src/plugins/insights/contract.spec.ts +285 -0
- package/src/plugins/insights/contract.ts +372 -0
- package/src/{insights → plugins/insights}/domain.ts +73 -15
- package/src/plugins/insights/errors.ts +11 -0
- package/src/{insights → plugins/insights}/eventInput.ts +189 -29
- package/src/plugins/insights/eventRow.ts +209 -0
- package/src/plugins/insights/index.ts +181 -45
- package/src/{insights → plugins/insights}/insights.integration.spec.ts +330 -34
- package/src/plugins/insights/insightsRollout.integration.spec.ts +1 -2
- package/src/plugins/insights/model.spec.ts +3 -5
- package/src/plugins/insights/model.ts +2 -3
- package/src/plugins/insights/modelTypes.ts +197 -0
- package/src/plugins/insights/overview.spec.ts +64 -0
- package/src/plugins/insights/overview.ts +214 -0
- package/src/{insights → plugins/insights}/provider.spec.ts +21 -10
- package/src/{insights → plugins/insights}/provider.ts +58 -21
- package/src/{insights → plugins/insights}/queryInput.ts +2 -2
- package/src/plugins/insights/reads.spec.ts +340 -52
- package/src/plugins/insights/reads.ts +453 -123
- package/src/plugins/insights/recordEvent.spec.ts +474 -20
- package/src/plugins/insights/recordEvent.ts +301 -63
- package/src/plugins/insights/retention.spec.ts +115 -0
- package/src/{insights → plugins/insights}/routes.ts +1 -31
- package/src/plugins/insights/schema.ts +356 -176
- package/src/plugins/insights/testing/expectInsightsIndex.spec.ts +42 -0
- package/src/plugins/insights/testing/expectInsightsIndex.ts +20 -0
- package/src/plugins/insights/testing/fixtures.ts +30 -0
- package/src/plugins/insights/testing/index.ts +45 -0
- package/src/plugins/insights/testing/insightsModelTests.ts +860 -0
- package/src/plugins/insights/testing/setupInsightsHttpTestSuite.ts +152 -0
- package/src/plugins/insights/testing/setupInsightsModelTestSuite.ts +36 -0
- package/src/plugins/insights/testing/types.ts +30 -0
- package/src/{insights → plugins/insights}/types.ts +1 -1
- package/src/readBudgets.testFixtures.ts +2 -2
- package/src/releaseCatalogRoutes.integration.spec.ts +7 -0
- package/src/retention.spec.ts +73 -0
- package/src/runtime.spec.ts +11 -12
- package/src/runtime.testFixtures.ts +7 -2
- package/src/testUtilsMutation.integration.spec.ts +2 -1
- package/src/testUtilsPackage.integration.spec.ts +7 -3
- package/dist/db/builtInDatabase.cjs +0 -104
- package/dist/db/builtInDatabase.d.cts +0 -38
- package/dist/db/builtInDatabase.d.mts +0 -38
- package/dist/db/builtInDatabase.mjs +0 -97
- package/dist/plugins/builtIn.cjs +0 -7
- package/dist/plugins/builtIn.mjs +0 -6
- package/src/db/builtInDatabase.ts +0 -149
- package/src/plugins/builtIn.ts +0 -5
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createUUIDv7,
|
|
3
|
-
isUUIDv7,
|
|
4
|
-
type BundleEventRow,
|
|
5
|
-
} from "@hot-updater/plugin-core";
|
|
1
|
+
import { createUUIDv7, isUUIDv7 } from "@hot-updater/plugin-core";
|
|
6
2
|
|
|
7
3
|
import type {
|
|
4
|
+
BundleEventFailureInput,
|
|
8
5
|
CreateBundleEventRequest,
|
|
9
6
|
CreateBundleEventRequestBase,
|
|
10
7
|
} from "./domain";
|
|
@@ -12,6 +9,7 @@ import {
|
|
|
12
9
|
InsightsBadRequestError,
|
|
13
10
|
InsightsPayloadTooLargeError,
|
|
14
11
|
} from "./errors";
|
|
12
|
+
import type { BundleEventRow } from "./eventRow";
|
|
15
13
|
|
|
16
14
|
const MAX_EVENT_STRING_LENGTH = 1_024;
|
|
17
15
|
const MAX_IDENTITY_LENGTH = 255;
|
|
@@ -73,6 +71,81 @@ function readEventId(
|
|
|
73
71
|
return value;
|
|
74
72
|
}
|
|
75
73
|
|
|
74
|
+
/** One value of an open set: any other value reads as `unknown`. */
|
|
75
|
+
const oneOf = <const T extends string>(
|
|
76
|
+
values: readonly T[],
|
|
77
|
+
value: unknown,
|
|
78
|
+
): T | "unknown" => (values.includes(value as T) ? (value as T) : "unknown");
|
|
79
|
+
|
|
80
|
+
const STAGES = ["check", "download", "install"] as const;
|
|
81
|
+
const REASONS = [
|
|
82
|
+
"network",
|
|
83
|
+
"http",
|
|
84
|
+
"invalid_response",
|
|
85
|
+
"hash_mismatch",
|
|
86
|
+
"signature",
|
|
87
|
+
"patch",
|
|
88
|
+
"extract",
|
|
89
|
+
"storage",
|
|
90
|
+
] as const;
|
|
91
|
+
const RESOURCES = [
|
|
92
|
+
"catalog",
|
|
93
|
+
"artifact",
|
|
94
|
+
"manifest",
|
|
95
|
+
"file",
|
|
96
|
+
"patch",
|
|
97
|
+
"archive",
|
|
98
|
+
] as const;
|
|
99
|
+
const TRANSPORTS = [
|
|
100
|
+
"timeout",
|
|
101
|
+
"dns",
|
|
102
|
+
"tls",
|
|
103
|
+
"connection",
|
|
104
|
+
"offline",
|
|
105
|
+
"cancelled",
|
|
106
|
+
] as const;
|
|
107
|
+
const DELIVERIES = ["patch", "manifest", "archive"] as const;
|
|
108
|
+
|
|
109
|
+
/** A storage error code or an Android exit reason: letters, digits, and `._-`. */
|
|
110
|
+
const CODE = /^[A-Za-z0-9._-]{1,64}$/;
|
|
111
|
+
const readCode = (value: unknown): string | undefined =>
|
|
112
|
+
typeof value === "string" && CODE.test(value) ? value : undefined;
|
|
113
|
+
|
|
114
|
+
const readMetadata = (
|
|
115
|
+
payload: Readonly<Record<string, unknown>>,
|
|
116
|
+
): Readonly<Record<string, unknown>> =>
|
|
117
|
+
isRecord(payload.metadata) ? payload.metadata : {};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* What an UPDATE_FAILED report says failed. It never refuses the report: an
|
|
121
|
+
* unknown value of a set reads as `unknown`, a missing or malformed failure
|
|
122
|
+
* as an unknown stage and reason, and a malformed optional field is left out,
|
|
123
|
+
* as is a null one.
|
|
124
|
+
*/
|
|
125
|
+
function readFailure(value: unknown): BundleEventFailureInput {
|
|
126
|
+
const failure = isRecord(value) ? value : {};
|
|
127
|
+
const httpStatus = failure.httpStatus;
|
|
128
|
+
const originCode = readCode(failure.originCode);
|
|
129
|
+
const previousProcessExit = readCode(failure.previousProcessExit);
|
|
130
|
+
return {
|
|
131
|
+
stage: oneOf(STAGES, failure.stage),
|
|
132
|
+
reason: oneOf(REASONS, failure.reason),
|
|
133
|
+
...(failure.resource == null
|
|
134
|
+
? {}
|
|
135
|
+
: { resource: oneOf(RESOURCES, failure.resource) }),
|
|
136
|
+
...(Number.isSafeInteger(httpStatus) &&
|
|
137
|
+
(httpStatus as number) >= 100 &&
|
|
138
|
+
(httpStatus as number) <= 599
|
|
139
|
+
? { httpStatus: httpStatus as number }
|
|
140
|
+
: {}),
|
|
141
|
+
...(failure.transport == null
|
|
142
|
+
? {}
|
|
143
|
+
: { transport: oneOf(TRANSPORTS, failure.transport) }),
|
|
144
|
+
...(originCode === undefined ? {} : { originCode }),
|
|
145
|
+
...(previousProcessExit === undefined ? {} : { previousProcessExit }),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
76
149
|
async function readBoundedText(request: Request): Promise<string> {
|
|
77
150
|
const contentLength = request.headers.get("content-length");
|
|
78
151
|
const declaredByteLength = Number(contentLength);
|
|
@@ -131,9 +204,6 @@ function requireEvent(payload: unknown): CreateBundleEventRequest {
|
|
|
131
204
|
...(payload.userId === undefined
|
|
132
205
|
? {}
|
|
133
206
|
: { userId: requireIdentityField(payload, "userId") }),
|
|
134
|
-
...(payload.username === undefined
|
|
135
|
-
? {}
|
|
136
|
-
: { username: requireStringField(payload, "username") }),
|
|
137
207
|
platform,
|
|
138
208
|
appVersion: requireStringField(payload, "appVersion"),
|
|
139
209
|
channel: requireStringField(payload, "channel"),
|
|
@@ -147,23 +217,51 @@ function requireEvent(payload: unknown): CreateBundleEventRequest {
|
|
|
147
217
|
toReleaseId: requireNullableStringField(payload, "toReleaseId"),
|
|
148
218
|
};
|
|
149
219
|
const type = requireStringField(payload, "type");
|
|
220
|
+
const movement = () => {
|
|
221
|
+
const updateStrategy = requireStringField(payload, "updateStrategy");
|
|
222
|
+
if (updateStrategy !== "fingerprint" && updateStrategy !== "appVersion") {
|
|
223
|
+
throw new InsightsBadRequestError("Invalid event field: updateStrategy");
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
...base,
|
|
227
|
+
fromBundleId: requireStringField(payload, "fromBundleId"),
|
|
228
|
+
updateStrategy,
|
|
229
|
+
} as const;
|
|
230
|
+
};
|
|
231
|
+
const metadata = readMetadata(payload);
|
|
150
232
|
switch (type) {
|
|
151
|
-
case "UPDATE_DOWNLOADED":
|
|
233
|
+
case "UPDATE_DOWNLOADED": {
|
|
234
|
+
const delivery = metadata.delivery;
|
|
235
|
+
const read = {
|
|
236
|
+
...(delivery == null ? {} : { delivery: oneOf(DELIVERIES, delivery) }),
|
|
237
|
+
...(metadata.patchFallback === true
|
|
238
|
+
? { patchFallback: true as const }
|
|
239
|
+
: {}),
|
|
240
|
+
};
|
|
241
|
+
return {
|
|
242
|
+
...movement(),
|
|
243
|
+
type,
|
|
244
|
+
...(Object.keys(read).length === 0 ? {} : { metadata: read }),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
152
247
|
case "UPDATE_APPLIED":
|
|
248
|
+
return { ...movement(), type };
|
|
153
249
|
case "RECOVERED": {
|
|
154
|
-
const
|
|
155
|
-
if (updateStrategy !== "fingerprint" && updateStrategy !== "appVersion") {
|
|
156
|
-
throw new InsightsBadRequestError(
|
|
157
|
-
"Invalid event field: updateStrategy",
|
|
158
|
-
);
|
|
159
|
-
}
|
|
250
|
+
const previousProcessExit = readCode(metadata.previousProcessExit);
|
|
160
251
|
return {
|
|
161
|
-
...
|
|
252
|
+
...movement(),
|
|
162
253
|
type,
|
|
163
|
-
|
|
164
|
-
|
|
254
|
+
...(previousProcessExit === undefined
|
|
255
|
+
? {}
|
|
256
|
+
: { metadata: { previousProcessExit } }),
|
|
165
257
|
};
|
|
166
258
|
}
|
|
259
|
+
case "UPDATE_FAILED":
|
|
260
|
+
return {
|
|
261
|
+
...movement(),
|
|
262
|
+
type,
|
|
263
|
+
metadata: { failure: readFailure(metadata.failure) },
|
|
264
|
+
};
|
|
167
265
|
case "UNCHANGED":
|
|
168
266
|
if (payload.fromBundleId !== null || payload.updateStrategy !== null) {
|
|
169
267
|
throw new InsightsBadRequestError("Invalid unchanged event shape");
|
|
@@ -202,20 +300,82 @@ export function createBundleEventRow(
|
|
|
202
300
|
to_bundle_id: input.toBundleId,
|
|
203
301
|
to_release_id: input.toReleaseId,
|
|
204
302
|
user_id: input.userId ?? null,
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
},
|
|
303
|
+
};
|
|
304
|
+
const metadata = {
|
|
305
|
+
cohort: input.cohort,
|
|
306
|
+
fingerprint_hash: input.fingerprintHash,
|
|
307
|
+
sdk_version: input.sdkVersion ?? null,
|
|
308
|
+
update_strategy: input.updateStrategy,
|
|
212
309
|
};
|
|
213
310
|
switch (input.type) {
|
|
214
|
-
case "UPDATE_DOWNLOADED":
|
|
311
|
+
case "UPDATE_DOWNLOADED": {
|
|
312
|
+
const { delivery, patchFallback } = input.metadata ?? {};
|
|
313
|
+
return {
|
|
314
|
+
...base,
|
|
315
|
+
from_bundle_id: input.fromBundleId,
|
|
316
|
+
type: input.type,
|
|
317
|
+
metadata: {
|
|
318
|
+
...metadata,
|
|
319
|
+
...(delivery === undefined ? {} : { delivery }),
|
|
320
|
+
...(patchFallback ? { patch_fallback: true } : {}),
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
}
|
|
215
324
|
case "UPDATE_APPLIED":
|
|
216
|
-
|
|
217
|
-
|
|
325
|
+
return {
|
|
326
|
+
...base,
|
|
327
|
+
from_bundle_id: input.fromBundleId,
|
|
328
|
+
type: input.type,
|
|
329
|
+
metadata,
|
|
330
|
+
};
|
|
331
|
+
case "RECOVERED": {
|
|
332
|
+
const exit = input.metadata?.previousProcessExit;
|
|
333
|
+
return {
|
|
334
|
+
...base,
|
|
335
|
+
from_bundle_id: input.fromBundleId,
|
|
336
|
+
type: input.type,
|
|
337
|
+
metadata: {
|
|
338
|
+
...metadata,
|
|
339
|
+
...(exit === undefined ? {} : { previous_process_exit: exit }),
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
case "UPDATE_FAILED": {
|
|
344
|
+
const { failure } = input.metadata;
|
|
345
|
+
return {
|
|
346
|
+
...base,
|
|
347
|
+
from_bundle_id: input.fromBundleId,
|
|
348
|
+
type: input.type,
|
|
349
|
+
metadata: {
|
|
350
|
+
...metadata,
|
|
351
|
+
failure: {
|
|
352
|
+
stage: failure.stage,
|
|
353
|
+
reason: failure.reason,
|
|
354
|
+
...(failure.resource === undefined
|
|
355
|
+
? {}
|
|
356
|
+
: { resource: failure.resource }),
|
|
357
|
+
...(failure.httpStatus === undefined
|
|
358
|
+
? {}
|
|
359
|
+
: { http_status: failure.httpStatus }),
|
|
360
|
+
...(failure.transport === undefined
|
|
361
|
+
? {}
|
|
362
|
+
: { transport: failure.transport }),
|
|
363
|
+
...(failure.originCode === undefined
|
|
364
|
+
? {}
|
|
365
|
+
: { origin_code: failure.originCode }),
|
|
366
|
+
...(failure.previousProcessExit === undefined
|
|
367
|
+
? {}
|
|
368
|
+
: { previous_process_exit: failure.previousProcessExit }),
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
}
|
|
218
373
|
case "UNCHANGED":
|
|
219
|
-
return {
|
|
374
|
+
return {
|
|
375
|
+
...base,
|
|
376
|
+
from_bundle_id: null,
|
|
377
|
+
type: input.type,
|
|
378
|
+
metadata,
|
|
379
|
+
};
|
|
220
380
|
}
|
|
221
381
|
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DatabasePluginInputError,
|
|
3
|
+
type DatabaseJsonObject,
|
|
4
|
+
type Platform,
|
|
5
|
+
} from "@hot-updater/plugin-core";
|
|
6
|
+
import { isDatabaseJsonObject } from "@hot-updater/plugin-core/internal";
|
|
7
|
+
|
|
8
|
+
/** Where an update failed, and what failed; open sets, `unknown` for any other value. */
|
|
9
|
+
export type BundleEventFailureStage =
|
|
10
|
+
| "check"
|
|
11
|
+
| "download"
|
|
12
|
+
| "install"
|
|
13
|
+
| "unknown";
|
|
14
|
+
export type BundleEventFailureReason =
|
|
15
|
+
| "network"
|
|
16
|
+
| "http"
|
|
17
|
+
| "invalid_response"
|
|
18
|
+
| "hash_mismatch"
|
|
19
|
+
| "signature"
|
|
20
|
+
| "patch"
|
|
21
|
+
| "extract"
|
|
22
|
+
| "storage"
|
|
23
|
+
| "unknown";
|
|
24
|
+
|
|
25
|
+
/** An `UPDATE_FAILED` report's failure, as stored. */
|
|
26
|
+
export type BundleEventFailure = DatabaseJsonObject & {
|
|
27
|
+
readonly stage: BundleEventFailureStage;
|
|
28
|
+
readonly reason: BundleEventFailureReason;
|
|
29
|
+
/** What the client was fetching or writing. */
|
|
30
|
+
readonly resource?:
|
|
31
|
+
| "catalog"
|
|
32
|
+
| "artifact"
|
|
33
|
+
| "manifest"
|
|
34
|
+
| "file"
|
|
35
|
+
| "patch"
|
|
36
|
+
| "archive"
|
|
37
|
+
| "unknown";
|
|
38
|
+
readonly http_status?: number;
|
|
39
|
+
/** How the connection failed, when it did. */
|
|
40
|
+
readonly transport?:
|
|
41
|
+
| "timeout"
|
|
42
|
+
| "dns"
|
|
43
|
+
| "tls"
|
|
44
|
+
| "connection"
|
|
45
|
+
| "offline"
|
|
46
|
+
| "cancelled"
|
|
47
|
+
| "unknown";
|
|
48
|
+
/** The storage origin's error code, such as `ExpiredToken`. */
|
|
49
|
+
readonly origin_code?: string;
|
|
50
|
+
/** Android's `ApplicationExitInfo` reason for the process before this one. */
|
|
51
|
+
readonly previous_process_exit?: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Ancillary report data; queryable identity and lifecycle fields stay on the row. */
|
|
55
|
+
export type DatabaseBundleEventMetadata = DatabaseJsonObject & {
|
|
56
|
+
readonly cohort: string;
|
|
57
|
+
readonly update_strategy: "fingerprint" | "appVersion" | null;
|
|
58
|
+
readonly fingerprint_hash: string | null;
|
|
59
|
+
readonly sdk_version: string | null;
|
|
60
|
+
/** `UPDATE_FAILED`: what failed. */
|
|
61
|
+
readonly failure?: BundleEventFailure;
|
|
62
|
+
/** `UPDATE_DOWNLOADED`: how the bundle arrived. */
|
|
63
|
+
readonly delivery?: "patch" | "manifest" | "archive" | "unknown";
|
|
64
|
+
/** `UPDATE_DOWNLOADED`: a patch failed and the full files came instead. */
|
|
65
|
+
readonly patch_fallback?: boolean;
|
|
66
|
+
/** `RECOVERED`: Android's `ApplicationExitInfo` reason for the crashed process. */
|
|
67
|
+
readonly previous_process_exit?: string;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export type BundleEventRowBase = {
|
|
71
|
+
readonly id: string;
|
|
72
|
+
readonly install_id: string;
|
|
73
|
+
readonly user_id: string | null;
|
|
74
|
+
readonly from_release_id: string | null;
|
|
75
|
+
readonly to_release_id: string | null;
|
|
76
|
+
readonly to_bundle_id: string;
|
|
77
|
+
readonly platform: Platform;
|
|
78
|
+
readonly app_version: string;
|
|
79
|
+
readonly channel: string;
|
|
80
|
+
readonly metadata: DatabaseBundleEventMetadata;
|
|
81
|
+
readonly received_at_ms: number;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export type BundleEventRow = BundleEventRowBase &
|
|
85
|
+
(
|
|
86
|
+
| { readonly type: "UPDATE_DOWNLOADED"; readonly from_bundle_id: string }
|
|
87
|
+
| {
|
|
88
|
+
readonly type: "UPDATE_APPLIED" | "RECOVERED";
|
|
89
|
+
readonly from_bundle_id: string;
|
|
90
|
+
}
|
|
91
|
+
/** An update that failed: `from` is the running bundle, `to` the target (for a check, the running bundle). */
|
|
92
|
+
| { readonly type: "UPDATE_FAILED"; readonly from_bundle_id: string }
|
|
93
|
+
| { readonly type: "UNCHANGED"; readonly from_bundle_id: null }
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const isOptional = (
|
|
97
|
+
value: Readonly<Record<string, unknown>>,
|
|
98
|
+
key: string,
|
|
99
|
+
valid: (field: unknown) => boolean,
|
|
100
|
+
) => !Object.hasOwn(value, key) || valid(value[key]);
|
|
101
|
+
|
|
102
|
+
const isString = (value: unknown) => typeof value === "string";
|
|
103
|
+
|
|
104
|
+
/** An `UPDATE_FAILED` report's stored failure: a stage and a reason, and what else the client knew. */
|
|
105
|
+
const isDatabaseBundleEventFailure = (value: unknown): boolean =>
|
|
106
|
+
isDatabaseJsonObject(value) &&
|
|
107
|
+
isString(value.stage) &&
|
|
108
|
+
isString(value.reason) &&
|
|
109
|
+
isOptional(value, "resource", isString) &&
|
|
110
|
+
isOptional(value, "http_status", (status) => Number.isSafeInteger(status)) &&
|
|
111
|
+
isOptional(value, "transport", isString) &&
|
|
112
|
+
isOptional(value, "origin_code", isString) &&
|
|
113
|
+
isOptional(value, "previous_process_exit", isString);
|
|
114
|
+
|
|
115
|
+
export const isDatabaseBundleEventMetadata = (
|
|
116
|
+
value: unknown,
|
|
117
|
+
): value is DatabaseBundleEventMetadata =>
|
|
118
|
+
isDatabaseJsonObject(value) &&
|
|
119
|
+
typeof value.cohort === "string" &&
|
|
120
|
+
(value.update_strategy === null ||
|
|
121
|
+
value.update_strategy === "fingerprint" ||
|
|
122
|
+
value.update_strategy === "appVersion") &&
|
|
123
|
+
(value.fingerprint_hash === null ||
|
|
124
|
+
typeof value.fingerprint_hash === "string") &&
|
|
125
|
+
(value.sdk_version === null || typeof value.sdk_version === "string") &&
|
|
126
|
+
isOptional(value, "failure", isDatabaseBundleEventFailure) &&
|
|
127
|
+
isOptional(value, "delivery", isString) &&
|
|
128
|
+
isOptional(value, "patch_fallback", (flag) => typeof flag === "boolean") &&
|
|
129
|
+
isOptional(value, "previous_process_exit", isString);
|
|
130
|
+
|
|
131
|
+
export const isRecord = (
|
|
132
|
+
value: unknown,
|
|
133
|
+
): value is Readonly<Record<string, unknown>> =>
|
|
134
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
135
|
+
|
|
136
|
+
const isIdentity = (value: unknown): value is string =>
|
|
137
|
+
typeof value === "string" && value.length > 0 && value.length <= 255;
|
|
138
|
+
|
|
139
|
+
const isTextOrNull = (value: unknown) =>
|
|
140
|
+
value === null || typeof value === "string";
|
|
141
|
+
|
|
142
|
+
/** Each bundle event field and what it holds. */
|
|
143
|
+
const BUNDLE_EVENT_FIELDS: Readonly<
|
|
144
|
+
Record<string, (value: unknown) => boolean>
|
|
145
|
+
> = {
|
|
146
|
+
id: (value) => typeof value === "string",
|
|
147
|
+
type: (value) =>
|
|
148
|
+
value === "UPDATE_DOWNLOADED" ||
|
|
149
|
+
value === "UPDATE_APPLIED" ||
|
|
150
|
+
value === "RECOVERED" ||
|
|
151
|
+
value === "UPDATE_FAILED" ||
|
|
152
|
+
value === "UNCHANGED",
|
|
153
|
+
install_id: isIdentity,
|
|
154
|
+
user_id: (value) => value === null || isIdentity(value),
|
|
155
|
+
from_bundle_id: isTextOrNull,
|
|
156
|
+
from_release_id: isTextOrNull,
|
|
157
|
+
to_release_id: isTextOrNull,
|
|
158
|
+
to_bundle_id: (value) => typeof value === "string",
|
|
159
|
+
platform: (value) => value === "ios" || value === "android",
|
|
160
|
+
app_version: (value) => typeof value === "string",
|
|
161
|
+
channel: (value) => typeof value === "string",
|
|
162
|
+
metadata: isDatabaseBundleEventMetadata,
|
|
163
|
+
received_at_ms: (value) =>
|
|
164
|
+
typeof value === "number" && Number.isSafeInteger(value) && value >= 0,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const updateStrategyOf = (row: Readonly<Record<string, unknown>>) =>
|
|
168
|
+
isRecord(row.metadata) ? row.metadata.update_strategy : undefined;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* A movement, or a failed update, names the bundle it left and how the
|
|
172
|
+
* device updates, and a failed update says what failed; an unchanged report
|
|
173
|
+
* names neither.
|
|
174
|
+
*/
|
|
175
|
+
const hasEventInvariants = (row: Readonly<Record<string, unknown>>) =>
|
|
176
|
+
((row.type === "UPDATE_DOWNLOADED" ||
|
|
177
|
+
row.type === "UPDATE_APPLIED" ||
|
|
178
|
+
row.type === "RECOVERED" ||
|
|
179
|
+
row.type === "UPDATE_FAILED") &&
|
|
180
|
+
typeof row.from_bundle_id === "string" &&
|
|
181
|
+
(updateStrategyOf(row) === "fingerprint" ||
|
|
182
|
+
updateStrategyOf(row) === "appVersion") &&
|
|
183
|
+
(row.type !== "UPDATE_FAILED" ||
|
|
184
|
+
(isRecord(row.metadata) && isRecord(row.metadata.failure)))) ||
|
|
185
|
+
(row.type === "UNCHANGED" &&
|
|
186
|
+
row.from_bundle_id === null &&
|
|
187
|
+
updateStrategyOf(row) === null);
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Throws unless `row` has exactly the bundle event fields, each of its type,
|
|
191
|
+
* and the invariants of its event type: `invalid-field` for an unknown field,
|
|
192
|
+
* `invalid-data` for anything else.
|
|
193
|
+
*/
|
|
194
|
+
export const validateBundleEventFields = (row: unknown): void => {
|
|
195
|
+
if (!isRecord(row)) throw new DatabasePluginInputError("invalid-data");
|
|
196
|
+
for (const field of Object.keys(row)) {
|
|
197
|
+
if (!Object.hasOwn(BUNDLE_EVENT_FIELDS, field)) {
|
|
198
|
+
throw new DatabasePluginInputError("invalid-field");
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
for (const [field, valid] of Object.entries(BUNDLE_EVENT_FIELDS)) {
|
|
202
|
+
if (!Object.hasOwn(row, field) || !valid(row[field])) {
|
|
203
|
+
throw new DatabasePluginInputError("invalid-data");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (!hasEventInvariants(row)) {
|
|
207
|
+
throw new DatabasePluginInputError("invalid-data");
|
|
208
|
+
}
|
|
209
|
+
};
|