@hot-updater/server 1.0.0-rc.2 → 1.0.0-rc.21
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 +12 -14
- package/dist/adapters/drizzle.cjs +32 -75
- package/dist/adapters/drizzle.d.cts +13 -5
- package/dist/adapters/drizzle.d.mts +13 -5
- package/dist/adapters/drizzle.mjs +32 -77
- package/dist/adapters/drizzleExecutor.cjs +114 -0
- package/dist/adapters/drizzleExecutor.d.cts +9 -0
- package/dist/adapters/drizzleExecutor.d.mts +9 -0
- package/dist/adapters/drizzleExecutor.mjs +112 -0
- package/dist/adapters/kysely.cjs +22 -31
- package/dist/adapters/kysely.d.cts +10 -6
- package/dist/adapters/kysely.d.mts +10 -6
- package/dist/adapters/kysely.mjs +23 -33
- package/dist/adapters/kyselyExecutor.cjs +32 -0
- package/dist/adapters/kyselyExecutor.d.cts +11 -0
- package/dist/adapters/kyselyExecutor.d.mts +11 -0
- package/dist/adapters/kyselyExecutor.mjs +32 -0
- package/dist/adapters/mongodb.cjs +18 -54
- package/dist/adapters/mongodb.d.cts +10 -5
- package/dist/adapters/mongodb.d.mts +10 -5
- package/dist/adapters/mongodb.mjs +19 -56
- package/dist/adapters/mongodbAdapter.cjs +192 -0
- package/dist/adapters/mongodbAdapter.d.cts +9 -0
- package/dist/adapters/mongodbAdapter.d.mts +9 -0
- package/dist/adapters/mongodbAdapter.mjs +191 -0
- package/dist/adapters/prisma.cjs +35 -267
- package/dist/adapters/prisma.d.cts +12 -7
- package/dist/adapters/prisma.d.mts +12 -7
- package/dist/adapters/prisma.mjs +36 -268
- package/dist/adapters/prismaExecutor.cjs +63 -0
- package/dist/adapters/prismaExecutor.mjs +63 -0
- package/dist/adapters/sqlProviders.cjs +14 -0
- package/dist/adapters/sqlProviders.mjs +14 -0
- package/dist/assembly/assemblePlugins.cjs +144 -0
- package/dist/assembly/assemblePlugins.d.cts +5 -0
- package/dist/assembly/assemblePlugins.d.mts +5 -0
- package/dist/assembly/assemblePlugins.mjs +139 -0
- package/dist/assembly/clientPlugins.cjs +88 -0
- package/dist/assembly/clientPlugins.mjs +88 -0
- package/dist/assembly/configError.d.cts +2 -0
- package/dist/assembly/configError.d.mts +2 -0
- package/dist/assembly/configError.mjs +2 -0
- package/dist/core/api.cjs +51 -0
- package/dist/core/api.d.cts +54 -0
- package/dist/core/api.d.mts +54 -0
- package/dist/core/api.mjs +51 -0
- package/dist/core/baseCandidates.cjs +28 -0
- package/dist/core/baseCandidates.mjs +28 -0
- package/dist/core/operations.cjs +351 -0
- package/dist/core/operations.mjs +351 -0
- package/dist/core/reads.cjs +273 -0
- package/dist/core/reads.d.cts +9 -0
- package/dist/core/reads.d.mts +9 -0
- package/dist/core/reads.mjs +267 -0
- package/dist/core/releases.cjs +193 -0
- package/dist/core/releases.mjs +190 -0
- package/dist/core/writes.cjs +96 -0
- package/dist/core/writes.mjs +91 -0
- package/dist/createHotUpdaterCore.cjs +72 -91
- package/dist/createHotUpdaterCore.d.cts +106 -38
- package/dist/createHotUpdaterCore.d.mts +106 -38
- package/dist/createHotUpdaterCore.mjs +74 -89
- package/dist/db/engineDrizzleSchema.cjs +53 -0
- package/dist/db/engineDrizzleSchema.mjs +53 -0
- package/dist/db/enginePrismaSchema.cjs +84 -0
- package/dist/db/enginePrismaSchema.d.cts +5 -0
- package/dist/db/enginePrismaSchema.d.mts +5 -0
- package/dist/db/enginePrismaSchema.mjs +83 -0
- package/dist/db/releaseCatalog.cjs +24 -47
- package/dist/db/releaseCatalog.mjs +20 -44
- package/dist/db/updateArtifacts.cjs +43 -72
- package/dist/db/updateArtifacts.mjs +40 -69
- package/dist/handler.cjs +59 -43
- package/dist/handler.d.cts +3 -5
- package/dist/handler.d.mts +3 -5
- package/dist/handler.mjs +61 -43
- package/dist/handlerAdminRoutes.cjs +374 -0
- package/dist/handlerAdminRoutes.mjs +372 -0
- package/dist/handlerParameters.cjs +6 -11
- package/dist/handlerParameters.mjs +6 -10
- package/dist/handlerReleaseCatalogRoutes.cjs +32 -15
- package/dist/handlerReleaseCatalogRoutes.mjs +32 -15
- package/dist/handlerTypes.d.cts +2 -55
- package/dist/handlerTypes.d.mts +2 -55
- package/dist/handlerVersionRoutes.cjs +20 -4
- package/dist/handlerVersionRoutes.mjs +20 -4
- package/dist/index.cjs +9 -9
- package/dist/index.d.cts +8 -9
- package/dist/index.d.mts +8 -9
- package/dist/index.mjs +3 -4
- package/dist/node.cjs +3 -6
- package/dist/node.d.cts +2 -2
- package/dist/node.d.mts +2 -2
- package/dist/node.mjs +4 -4
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/plugins/api-keys/index.cjs +9 -0
- package/dist/plugins/api-keys/index.d.cts +1 -0
- package/dist/plugins/api-keys/index.d.mts +1 -0
- package/dist/plugins/api-keys/index.mjs +2 -0
- package/dist/plugins/definePlugin.d.cts +2 -0
- package/dist/plugins/definePlugin.d.mts +2 -0
- package/dist/plugins/insights/index.cjs +9 -0
- package/dist/plugins/insights/index.d.cts +1 -0
- package/dist/plugins/insights/index.d.mts +1 -0
- package/dist/plugins/insights/index.mjs +2 -0
- package/dist/plugins/official.cjs +30 -0
- package/dist/plugins/official.mjs +29 -0
- package/dist/storageAccess.cjs +13 -10
- package/dist/storageAccess.mjs +13 -10
- package/dist/types/index.d.cts +2 -28
- package/dist/types/index.d.mts +2 -28
- package/package.json +26 -14
- package/dist/_virtual/_rolldown/runtime.cjs +0 -23
- package/dist/adapters/databaseConstraintErrors.cjs +0 -22
- package/dist/adapters/databaseConstraintErrors.mjs +0 -21
- package/dist/adapters/databasePluginUtils.cjs +0 -116
- package/dist/adapters/databasePluginUtils.mjs +0 -106
- package/dist/adapters/drizzleCrud.cjs +0 -311
- package/dist/adapters/drizzleCrud.mjs +0 -308
- package/dist/adapters/drizzleLazyDB.cjs +0 -106
- package/dist/adapters/drizzleLazyDB.mjs +0 -106
- package/dist/adapters/drizzleQuery.cjs +0 -69
- package/dist/adapters/drizzleQuery.mjs +0 -69
- package/dist/adapters/insightsTransaction.cjs +0 -22
- package/dist/adapters/insightsTransaction.mjs +0 -22
- package/dist/adapters/kyselyCrud.cjs +0 -305
- package/dist/adapters/kyselyCrud.mjs +0 -304
- package/dist/adapters/kyselyQuery.cjs +0 -56
- package/dist/adapters/kyselyQuery.mjs +0 -56
- package/dist/adapters/mongodbCollections.cjs +0 -38
- package/dist/adapters/mongodbCollections.mjs +0 -32
- package/dist/adapters/mongodbQuery.cjs +0 -94
- package/dist/adapters/mongodbQuery.mjs +0 -86
- package/dist/adapters/mongodbReads.cjs +0 -210
- package/dist/adapters/mongodbReads.mjs +0 -210
- package/dist/adapters/mongodbWrites.cjs +0 -148
- package/dist/adapters/mongodbWrites.mjs +0 -148
- package/dist/adapters/prismaQuery.cjs +0 -57
- package/dist/adapters/prismaQuery.mjs +0 -56
- package/dist/adapters/prismaRows.cjs +0 -219
- package/dist/adapters/prismaRows.mjs +0 -209
- package/dist/apiKeys.cjs +0 -143
- package/dist/apiKeys.d.cts +0 -49
- package/dist/apiKeys.d.mts +0 -49
- package/dist/apiKeys.mjs +0 -133
- package/dist/db/createBundleDiff.cjs +0 -144
- package/dist/db/createBundleDiff.d.cts +0 -16
- package/dist/db/createBundleDiff.d.mts +0 -16
- package/dist/db/createBundleDiff.mjs +0 -141
- package/dist/db/databasePluginCore.cjs +0 -137
- package/dist/db/databasePluginCore.mjs +0 -137
- package/dist/db/fixedMigrator.cjs +0 -79
- package/dist/db/fixedMigrator.mjs +0 -78
- package/dist/db/fixedMigratorMongo.cjs +0 -321
- package/dist/db/fixedMigratorMongo.mjs +0 -321
- package/dist/db/fixedMigratorShared.cjs +0 -23
- package/dist/db/fixedMigratorShared.mjs +0 -18
- package/dist/db/index.cjs +0 -27
- package/dist/db/index.d.cts +0 -12
- package/dist/db/index.d.mts +0 -12
- package/dist/db/index.mjs +0 -22
- package/dist/db/mongoMigrationExecution.cjs +0 -11
- package/dist/db/mongoMigrationExecution.mjs +0 -11
- package/dist/db/releaseCatalog.d.cts +0 -16
- package/dist/db/releaseCatalog.d.mts +0 -16
- package/dist/db/schema/insightsCollation.cjs +0 -14
- package/dist/db/schema/insightsCollation.mjs +0 -14
- package/dist/db/schema/mongodb.cjs +0 -25
- package/dist/db/schema/mongodb.mjs +0 -25
- package/dist/db/schema/registry.cjs +0 -21
- package/dist/db/schema/registry.mjs +0 -17
- package/dist/db/schema/sql.cjs +0 -105
- package/dist/db/schema/sql.mjs +0 -97
- package/dist/db/schema/sqlOperations.cjs +0 -27
- package/dist/db/schema/sqlOperations.mjs +0 -26
- package/dist/db/schemaGenerators.cjs +0 -275
- package/dist/db/schemaGenerators.mjs +0 -273
- package/dist/db/schemaReadiness.cjs +0 -26
- package/dist/db/schemaReadiness.d.cts +0 -9
- package/dist/db/schemaReadiness.d.mts +0 -9
- package/dist/db/schemaReadiness.mjs +0 -25
- package/dist/db/sqlMigrationExecution.cjs +0 -67
- package/dist/db/sqlMigrationExecution.mjs +0 -67
- package/dist/db/types.cjs +0 -22
- package/dist/db/types.d.cts +0 -118
- package/dist/db/types.d.mts +0 -118
- package/dist/db/types.mjs +0 -19
- package/dist/handlerBundleRoutes.cjs +0 -155
- package/dist/handlerBundleRoutes.mjs +0 -155
- package/dist/handlerReleaseManagementRoutes.cjs +0 -124
- package/dist/handlerReleaseManagementRoutes.mjs +0 -124
- package/dist/insights/domain.d.cts +0 -89
- package/dist/insights/domain.d.mts +0 -89
- package/dist/insights/errors.cjs +0 -15
- package/dist/insights/errors.mjs +0 -14
- package/dist/insights/eventInput.cjs +0 -155
- package/dist/insights/eventInput.mjs +0 -153
- package/dist/insights/provider.cjs +0 -314
- package/dist/insights/provider.d.cts +0 -6
- package/dist/insights/provider.d.mts +0 -6
- package/dist/insights/provider.mjs +0 -314
- package/dist/insights/queryInput.cjs +0 -93
- package/dist/insights/queryInput.mjs +0 -91
- package/dist/insights/routes.cjs +0 -58
- package/dist/insights/routes.mjs +0 -56
- package/dist/insights/types.d.cts +0 -32
- package/dist/insights/types.d.mts +0 -32
- package/dist/schema/dsl.cjs +0 -103
- package/dist/schema/dsl.mjs +0 -87
- package/dist/schema/index.cjs +0 -20
- package/dist/schema/index.mjs +0 -7
- package/dist/schema/settings.cjs +0 -9
- package/dist/schema/settings.mjs +0 -9
- package/dist/schema/types.cjs +0 -8
- package/dist/schema/types.mjs +0 -6
- package/dist/schema/v1_0_0.cjs +0 -406
- package/dist/schema/v1_0_0.mjs +0 -398
- package/src/adapters/databaseConstraintErrors.ts +0 -25
- package/src/adapters/databasePluginTestDatabase.ts +0 -120
- package/src/adapters/databasePluginUtils.spec.ts +0 -29
- package/src/adapters/databasePluginUtils.ts +0 -214
- package/src/adapters/drizzle.spec.ts +0 -336
- package/src/adapters/drizzle.ts +0 -162
- package/src/adapters/drizzleCrud.ts +0 -571
- package/src/adapters/drizzleLazyDB.ts +0 -271
- package/src/adapters/drizzleQuery.ts +0 -164
- package/src/adapters/insightsTransaction.ts +0 -32
- package/src/adapters/kysely.spec.ts +0 -174
- package/src/adapters/kysely.ts +0 -110
- package/src/adapters/kyselyCrud.ts +0 -709
- package/src/adapters/kyselyQuery.ts +0 -128
- package/src/adapters/mongodb.integration.spec.ts +0 -210
- package/src/adapters/mongodb.spec.ts +0 -209
- package/src/adapters/mongodb.ts +0 -138
- package/src/adapters/mongodbCollections.ts +0 -67
- package/src/adapters/mongodbQuery.ts +0 -255
- package/src/adapters/mongodbReads.ts +0 -380
- package/src/adapters/mongodbRows.spec.ts +0 -64
- package/src/adapters/mongodbRows.ts +0 -118
- package/src/adapters/mongodbTestClient.ts +0 -377
- package/src/adapters/mongodbTestFilter.ts +0 -169
- package/src/adapters/mongodbWrites.ts +0 -287
- package/src/adapters/prisma.spec.ts +0 -293
- package/src/adapters/prisma.ts +0 -565
- package/src/adapters/prismaQuery.ts +0 -129
- package/src/adapters/prismaRows.spec.ts +0 -98
- package/src/adapters/prismaRows.ts +0 -337
- package/src/adapters/prismaTestClient.ts +0 -413
- package/src/apiKeys.integration.spec.ts +0 -192
- package/src/apiKeys.spec.ts +0 -162
- package/src/apiKeys.ts +0 -220
- package/src/calculatePagination.ts +0 -34
- package/src/createHotUpdaterCore.ts +0 -246
- package/src/db/bundleRows.spec.ts +0 -164
- package/src/db/bundleRows.ts +0 -10
- package/src/db/createBundleDiff.spec.ts +0 -450
- package/src/db/createBundleDiff.ts +0 -364
- package/src/db/databasePluginCore.spec.ts +0 -62
- package/src/db/databasePluginCore.testFixtures.ts +0 -76
- package/src/db/databasePluginCore.ts +0 -185
- package/src/db/databasePluginUpdateCheck.bench.ts +0 -110
- package/src/db/fixedMigrator.spec.ts +0 -296
- package/src/db/fixedMigrator.ts +0 -147
- package/src/db/fixedMigratorMongo.spec.ts +0 -239
- package/src/db/fixedMigratorMongo.ts +0 -425
- package/src/db/fixedMigratorShared.ts +0 -36
- package/src/db/hotUpdaterSchema.ts +0 -5
- package/src/db/index.spec.ts +0 -1269
- package/src/db/index.ts +0 -47
- package/src/db/mongoMigrationExecution.ts +0 -23
- package/src/db/node-sqlite.d.ts +0 -25
- package/src/db/releaseCatalog.ts +0 -148
- package/src/db/schema/definitions.ts +0 -1
- package/src/db/schema/insightsCollation.ts +0 -21
- package/src/db/schema/mongodb.ts +0 -40
- package/src/db/schema/registry.ts +0 -55
- package/src/db/schema/sql.ts +0 -244
- package/src/db/schema/sqlOperations.ts +0 -63
- package/src/db/schema/types.ts +0 -1
- package/src/db/schemaGenerators.ts +0 -494
- package/src/db/schemaReadiness.ts +0 -34
- package/src/db/sqlMigrationExecution.spec.ts +0 -65
- package/src/db/sqlMigrationExecution.ts +0 -108
- package/src/db/types.spec.ts +0 -63
- package/src/db/types.ts +0 -228
- package/src/db/updateArtifacts.spec.ts +0 -359
- package/src/db/updateArtifacts.ts +0 -541
- package/src/handler-standalone.integration.spec.ts +0 -528
- package/src/handler.spec.ts +0 -190
- package/src/handler.testFixtures.ts +0 -30
- package/src/handler.ts +0 -245
- package/src/handlerAuth.integration.spec.ts +0 -33
- package/src/handlerBundleRoutes.ts +0 -259
- package/src/handlerErrors.ts +0 -7
- package/src/handlerManagement.spec.ts +0 -262
- package/src/handlerParameters.ts +0 -105
- package/src/handlerReleaseCatalogRoutes.ts +0 -154
- package/src/handlerReleaseManagementRoutes.ts +0 -213
- package/src/handlerTypes.ts +0 -96
- package/src/handlerVersionRoutes.ts +0 -12
- package/src/index.ts +0 -30
- package/src/insights/domain.ts +0 -103
- package/src/insights/errors.ts +0 -11
- package/src/insights/eventInput.ts +0 -225
- package/src/insights/insights.integration.spec.ts +0 -381
- package/src/insights/provider.spec.ts +0 -385
- package/src/insights/provider.ts +0 -591
- package/src/insights/queryInput.ts +0 -141
- package/src/insights/routes.ts +0 -104
- package/src/insights/types.ts +0 -55
- package/src/internalRouter.ts +0 -104
- package/src/node.spec.ts +0 -119
- package/src/node.ts +0 -111
- package/src/releaseCatalogRoutes.integration.spec.ts +0 -238
- package/src/runtime.spec.ts +0 -194
- package/src/runtime.testFixtures.ts +0 -71
- package/src/schema/dsl-all-versions.spec.ts +0 -37
- package/src/schema/dsl.ts +0 -187
- package/src/schema/index.ts +0 -10
- package/src/schema/settings.ts +0 -15
- package/src/schema/types.ts +0 -79
- package/src/schema/v1_0_0.spec.ts +0 -220
- package/src/schema/v1_0_0.ts +0 -529
- package/src/storageAccess.spec.ts +0 -152
- package/src/storageAccess.ts +0 -141
- package/src/types/index.ts +0 -34
- package/src/version.ts +0 -3
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
const require_errors = require("./errors.cjs");
|
|
2
|
-
const require_eventInput = require("./eventInput.cjs");
|
|
3
|
-
let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
|
|
4
|
-
//#region src/insights/provider.ts
|
|
5
|
-
const DEFAULT_PAGE_LIMIT = 50;
|
|
6
|
-
const MAX_PAGE_LIMIT = 100;
|
|
7
|
-
const MAX_EVENT_ID_LENGTH = 1024;
|
|
8
|
-
const MAX_IDENTITY_LENGTH = 255;
|
|
9
|
-
const MAX_CURSOR_LENGTH = 8192;
|
|
10
|
-
const WINDOW_MS = {
|
|
11
|
-
"24h": 864e5,
|
|
12
|
-
"7d": 6048e5,
|
|
13
|
-
"30d": 2592e6
|
|
14
|
-
};
|
|
15
|
-
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
16
|
-
const requireString = (value, label, maximumLength) => {
|
|
17
|
-
if (typeof value !== "string" || value.length === 0 || value.length > maximumLength || new TextDecoder("utf-8", { ignoreBOM: true }).decode(new TextEncoder().encode(value)) !== value) throw new require_errors.InsightsBadRequestError(`Invalid ${label}.`);
|
|
18
|
-
return value;
|
|
19
|
-
};
|
|
20
|
-
const requireTimestamp = (value, label) => {
|
|
21
|
-
if (!Number.isSafeInteger(value) || Number(value) < 0) throw new require_errors.InsightsBadRequestError(`Invalid ${label}.`);
|
|
22
|
-
return Number(value);
|
|
23
|
-
};
|
|
24
|
-
const readLimit = (value) => {
|
|
25
|
-
const limit = value ?? DEFAULT_PAGE_LIMIT;
|
|
26
|
-
if (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_PAGE_LIMIT) throw new require_errors.InsightsBadRequestError("Invalid page limit.");
|
|
27
|
-
return limit;
|
|
28
|
-
};
|
|
29
|
-
const encodeBase64Url = (value) => {
|
|
30
|
-
const bytes = new TextEncoder().encode(value);
|
|
31
|
-
let binary = "";
|
|
32
|
-
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
33
|
-
return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/u, "");
|
|
34
|
-
};
|
|
35
|
-
const decodeBase64Url = (value) => {
|
|
36
|
-
if (value.length === 0 || value.length > MAX_CURSOR_LENGTH) throw new require_errors.InsightsBadRequestError("Invalid Insights cursor.");
|
|
37
|
-
try {
|
|
38
|
-
const base64 = value.replaceAll("-", "+").replaceAll("_", "/");
|
|
39
|
-
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
40
|
-
const binary = atob(padded);
|
|
41
|
-
const bytes = Uint8Array.from(binary, (character) => character.charCodeAt(0));
|
|
42
|
-
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
43
|
-
} catch {
|
|
44
|
-
throw new require_errors.InsightsBadRequestError("Invalid Insights cursor.");
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const encodeCursor = (value) => encodeBase64Url(JSON.stringify(value));
|
|
48
|
-
const decodeCursor = (value) => {
|
|
49
|
-
try {
|
|
50
|
-
return JSON.parse(decodeBase64Url(value));
|
|
51
|
-
} catch (error) {
|
|
52
|
-
if (error instanceof require_errors.InsightsBadRequestError) throw error;
|
|
53
|
-
throw new require_errors.InsightsBadRequestError("Invalid Insights cursor.");
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
const readScope = (input) => {
|
|
57
|
-
if (input.platform !== "ios" && input.platform !== "android") throw new require_errors.InsightsBadRequestError("Invalid Insights platform.");
|
|
58
|
-
return {
|
|
59
|
-
platform: input.platform,
|
|
60
|
-
channel: requireString(input.channel, "channel", MAX_EVENT_ID_LENGTH)
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
const bundleFilter = (input) => {
|
|
64
|
-
const scope = readScope(input);
|
|
65
|
-
const bundleId = requireString(input.bundleId, "bundle ID", MAX_EVENT_ID_LENGTH);
|
|
66
|
-
switch (input.outcome) {
|
|
67
|
-
case "applied": return {
|
|
68
|
-
...scope,
|
|
69
|
-
type: "UPDATE_APPLIED",
|
|
70
|
-
toBundleId: bundleId
|
|
71
|
-
};
|
|
72
|
-
case "recovered": return {
|
|
73
|
-
...scope,
|
|
74
|
-
type: "RECOVERED",
|
|
75
|
-
fromBundleId: bundleId
|
|
76
|
-
};
|
|
77
|
-
case "adopted": return {
|
|
78
|
-
...scope,
|
|
79
|
-
type: "RELEASE_ADOPTED",
|
|
80
|
-
toBundleId: bundleId
|
|
81
|
-
};
|
|
82
|
-
default: throw new require_errors.InsightsBadRequestError("Invalid Insights outcome.");
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
const sameFilter = (left, right) => {
|
|
86
|
-
if (!isRecord(left) || left.kind !== right.kind) return false;
|
|
87
|
-
switch (right.kind) {
|
|
88
|
-
case "all": return true;
|
|
89
|
-
case "installationMovement": return left.installId === right.installId;
|
|
90
|
-
case "bundle": return left.platform === right.platform && left.channel === right.channel && left.type === right.type && (right.type === "RECOVERED" ? left.fromBundleId === right.fromBundleId : left.toBundleId === right.toBundleId);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
const readEventCursor = (value, filter) => {
|
|
94
|
-
const cursor = decodeCursor(value);
|
|
95
|
-
if (!isRecord(cursor) || cursor.version !== 2 || cursor.kind !== "events" || !isRecord(cursor.filter) || !isRecord(cursor.after)) throw new require_errors.InsightsBadRequestError("Invalid Insights cursor.");
|
|
96
|
-
if (!sameFilter(cursor.filter, filter)) throw new require_errors.InsightsBadRequestError("Insights cursor does not match the requested events.");
|
|
97
|
-
if (typeof cursor.after.id !== "string" || !(0, _hot_updater_plugin_core.isUUIDv7)(cursor.after.id)) throw new require_errors.InsightsBadRequestError("Invalid Insights event cursor ID.");
|
|
98
|
-
return {
|
|
99
|
-
after: {
|
|
100
|
-
id: cursor.after.id,
|
|
101
|
-
receivedAtMs: requireTimestamp(cursor.after.receivedAtMs, "event cursor")
|
|
102
|
-
},
|
|
103
|
-
beforeReceivedAtMs: requireTimestamp(cursor.beforeReceivedAtMs, "event cutoff"),
|
|
104
|
-
kind: "events",
|
|
105
|
-
sinceMs: requireTimestamp(cursor.sinceMs, "event start"),
|
|
106
|
-
filter,
|
|
107
|
-
version: 2
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
const readUserInstallationCursor = (value, userId) => {
|
|
111
|
-
const cursor = decodeCursor(value);
|
|
112
|
-
if (!isRecord(cursor) || cursor.version !== 1 || cursor.kind !== "user-installations" || cursor.userId !== userId) throw new require_errors.InsightsBadRequestError("Insights cursor does not match the requested user ID.");
|
|
113
|
-
return {
|
|
114
|
-
afterInstallId: requireString(cursor.afterInstallId, "installation cursor", MAX_IDENTITY_LENGTH),
|
|
115
|
-
kind: "user-installations",
|
|
116
|
-
userId,
|
|
117
|
-
version: 1
|
|
118
|
-
};
|
|
119
|
-
};
|
|
120
|
-
const compareEventNewest = (left, right) => right.received_at_ms - left.received_at_ms || (0, _hot_updater_plugin_core.compareInsightsText)(right.id, left.id);
|
|
121
|
-
const isAfterEventCursor = (row, after) => row.received_at_ms < after.receivedAtMs || row.received_at_ms === after.receivedAtMs && (0, _hot_updater_plugin_core.compareInsightsText)(row.id, after.id) < 0;
|
|
122
|
-
const assertEventRows = (rows, input) => {
|
|
123
|
-
if (rows.length > input.limit) throw new Error("Insights database returned too many event rows.");
|
|
124
|
-
for (let index = 0; index < rows.length; index += 1) {
|
|
125
|
-
const row = rows[index];
|
|
126
|
-
const previous = rows[index - 1];
|
|
127
|
-
if (!row || typeof row.id !== "string" || !(0, _hot_updater_plugin_core.isUUIDv7)(row.id) || !Number.isSafeInteger(row.received_at_ms) || row.received_at_ms >= input.beforeReceivedAtMs || row.received_at_ms < input.sinceMs || previous !== void 0 && compareEventNewest(previous, row) >= 0 || input.after !== void 0 && !isAfterEventCursor(row, input.after) || input.filter.kind === "installationMovement" && (row.install_id !== input.filter.installId || !(0, _hot_updater_plugin_core.isInsightsMovementEvent)(row)) || input.filter.kind === "bundle" && (row.platform !== input.filter.platform || row.channel !== input.filter.channel || row.type !== input.filter.type || (input.filter.type === "RECOVERED" ? row.from_bundle_id !== input.filter.fromBundleId : row.to_bundle_id !== input.filter.toBundleId))) throw new Error("Insights database returned invalid event rows.");
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const toEventHistoryRow = (row) => ({
|
|
131
|
-
appVersion: row.app_version,
|
|
132
|
-
channel: row.channel,
|
|
133
|
-
cohort: row.cohort,
|
|
134
|
-
fromBundleId: row.from_bundle_id,
|
|
135
|
-
id: row.id,
|
|
136
|
-
installId: row.install_id,
|
|
137
|
-
platform: row.platform,
|
|
138
|
-
receivedAtMs: row.received_at_ms,
|
|
139
|
-
toBundleId: row.to_bundle_id,
|
|
140
|
-
type: row.type,
|
|
141
|
-
userId: row.user_id,
|
|
142
|
-
username: row.username
|
|
143
|
-
});
|
|
144
|
-
const toInstallationRow = (row) => ({
|
|
145
|
-
appVersion: row.app_version,
|
|
146
|
-
channel: row.channel,
|
|
147
|
-
cohort: row.cohort,
|
|
148
|
-
installId: row.install_id,
|
|
149
|
-
lastKnownBundleId: row.to_bundle_id,
|
|
150
|
-
latestStatus: row.type,
|
|
151
|
-
platform: row.platform,
|
|
152
|
-
receivedAtMs: row.received_at_ms,
|
|
153
|
-
userId: row.user_id,
|
|
154
|
-
username: row.username
|
|
155
|
-
});
|
|
156
|
-
const pageEventRows = async (model, input, filter, map) => {
|
|
157
|
-
const limit = readLimit(input.limit);
|
|
158
|
-
const cursor = input.cursor === void 0 ? void 0 : readEventCursor(input.cursor, filter);
|
|
159
|
-
const beforeReceivedAtMs = cursor?.beforeReceivedAtMs ?? (input.beforeReceivedAtMs === void 0 ? Date.now() : requireTimestamp(input.beforeReceivedAtMs, "event cutoff"));
|
|
160
|
-
if (cursor !== void 0 && input.beforeReceivedAtMs !== void 0 && input.beforeReceivedAtMs !== beforeReceivedAtMs) throw new require_errors.InsightsBadRequestError("Insights cursor does not match the requested event cutoff.");
|
|
161
|
-
const sinceMs = cursor?.sinceMs ?? (input.sinceMs === void 0 ? 0 : requireTimestamp(input.sinceMs, "event start"));
|
|
162
|
-
if (sinceMs > beforeReceivedAtMs || input.sinceMs !== void 0 && input.sinceMs !== sinceMs || cursor !== void 0 && (cursor.after.receivedAtMs < sinceMs || cursor.after.receivedAtMs >= beforeReceivedAtMs)) throw new require_errors.InsightsBadRequestError("Insights cursor or range does not match the requested event start.");
|
|
163
|
-
const databaseInput = {
|
|
164
|
-
filter,
|
|
165
|
-
sinceMs,
|
|
166
|
-
beforeReceivedAtMs,
|
|
167
|
-
...cursor === void 0 ? {} : { after: cursor.after },
|
|
168
|
-
limit: limit + 1
|
|
169
|
-
};
|
|
170
|
-
const rows = await model.listEvents(databaseInput);
|
|
171
|
-
assertEventRows(rows, databaseInput);
|
|
172
|
-
const pageRows = rows.slice(0, limit);
|
|
173
|
-
const last = pageRows.at(-1);
|
|
174
|
-
return {
|
|
175
|
-
beforeReceivedAtMs,
|
|
176
|
-
data: pageRows.map(map),
|
|
177
|
-
nextCursor: rows.length > limit && last ? encodeCursor({
|
|
178
|
-
after: {
|
|
179
|
-
id: last.id,
|
|
180
|
-
receivedAtMs: last.received_at_ms
|
|
181
|
-
},
|
|
182
|
-
beforeReceivedAtMs,
|
|
183
|
-
kind: "events",
|
|
184
|
-
filter,
|
|
185
|
-
sinceMs,
|
|
186
|
-
version: 2
|
|
187
|
-
}) : null
|
|
188
|
-
};
|
|
189
|
-
};
|
|
190
|
-
const assertInstallationRows = (rows, input) => {
|
|
191
|
-
if (rows.length > input.limit) throw new Error("Insights database returned too many installation rows.");
|
|
192
|
-
for (let index = 0; index < rows.length; index += 1) {
|
|
193
|
-
const row = rows[index];
|
|
194
|
-
const previous = rows[index - 1];
|
|
195
|
-
if (!row || row.user_id !== input.userId || input.afterInstallId !== void 0 && (0, _hot_updater_plugin_core.compareInsightsText)(row.install_id, input.afterInstallId) <= 0 || previous !== void 0 && (0, _hot_updater_plugin_core.compareInsightsText)(previous.install_id, row.install_id) >= 0) throw new Error("Insights database returned invalid installation rows.");
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
const createInsightsProvider = (model) => Object.freeze({
|
|
199
|
-
async appendBundleEvent(input) {
|
|
200
|
-
const event = require_eventInput.createBundleEventRow(input);
|
|
201
|
-
await model.record({
|
|
202
|
-
event,
|
|
203
|
-
installation: (0, _hot_updater_plugin_core.toInsightsInstallationRow)(event)
|
|
204
|
-
});
|
|
205
|
-
},
|
|
206
|
-
listEvents(input) {
|
|
207
|
-
const filter = input.bundle === void 0 ? { kind: "all" } : {
|
|
208
|
-
kind: "bundle",
|
|
209
|
-
...bundleFilter(input.bundle)
|
|
210
|
-
};
|
|
211
|
-
return pageEventRows(model, input, filter, toEventHistoryRow);
|
|
212
|
-
},
|
|
213
|
-
async listInstallationEvents(input) {
|
|
214
|
-
if ("bundle" in input && input.bundle !== void 0) throw new require_errors.InsightsBadRequestError("Installation movement queries cannot include a bundle filter.");
|
|
215
|
-
const installId = requireString(input.installId, "install ID", MAX_IDENTITY_LENGTH);
|
|
216
|
-
return pageEventRows(model, input, {
|
|
217
|
-
kind: "installationMovement",
|
|
218
|
-
installId
|
|
219
|
-
}, (row) => toEventHistoryRow(row));
|
|
220
|
-
},
|
|
221
|
-
async getInstallation({ installId }) {
|
|
222
|
-
const normalizedInstallId = requireString(installId, "install ID", MAX_IDENTITY_LENGTH);
|
|
223
|
-
const rows = await model.findInstallations({ installId: normalizedInstallId });
|
|
224
|
-
const row = rows[0] ?? null;
|
|
225
|
-
if (rows.length > 1 || row !== null && row.install_id !== normalizedInstallId) throw new Error("Insights database returned an invalid installation.");
|
|
226
|
-
return row === null ? null : toInstallationRow(row);
|
|
227
|
-
},
|
|
228
|
-
async pageInstallationsByCurrentUserId(input) {
|
|
229
|
-
const userId = requireString(input.userId, "user ID", MAX_IDENTITY_LENGTH);
|
|
230
|
-
const limit = readLimit(input.limit);
|
|
231
|
-
const cursor = input.cursor === void 0 ? void 0 : readUserInstallationCursor(input.cursor, userId);
|
|
232
|
-
const databaseInput = {
|
|
233
|
-
userId,
|
|
234
|
-
...cursor === void 0 ? {} : { afterInstallId: cursor.afterInstallId },
|
|
235
|
-
limit: limit + 1
|
|
236
|
-
};
|
|
237
|
-
const rows = await model.findInstallations(databaseInput);
|
|
238
|
-
assertInstallationRows(rows, databaseInput);
|
|
239
|
-
const pageRows = rows.slice(0, limit);
|
|
240
|
-
const last = pageRows.at(-1);
|
|
241
|
-
return {
|
|
242
|
-
data: pageRows.map(toInstallationRow),
|
|
243
|
-
nextCursor: rows.length > limit && last ? encodeCursor({
|
|
244
|
-
afterInstallId: last.install_id,
|
|
245
|
-
kind: "user-installations",
|
|
246
|
-
userId,
|
|
247
|
-
version: 1
|
|
248
|
-
}) : null
|
|
249
|
-
};
|
|
250
|
-
},
|
|
251
|
-
async getReportingOverview(input) {
|
|
252
|
-
const scope = readScope(input);
|
|
253
|
-
const { window } = input;
|
|
254
|
-
if (!Object.hasOwn(WINDOW_MS, window)) throw new require_errors.InsightsBadRequestError("Invalid reporting installation window.");
|
|
255
|
-
const beforeReceivedAtMs = Date.now();
|
|
256
|
-
const sinceMs = Math.max(0, beforeReceivedAtMs - WINDOW_MS[window]);
|
|
257
|
-
const measure = async (count) => {
|
|
258
|
-
const value = await count;
|
|
259
|
-
if (!Number.isSafeInteger(value) || value < 0) throw new Error("Insights database returned an invalid count.");
|
|
260
|
-
return {
|
|
261
|
-
count: value,
|
|
262
|
-
measuredAtMs: Date.now()
|
|
263
|
-
};
|
|
264
|
-
};
|
|
265
|
-
const bundleId = input.bundleId === void 0 ? void 0 : requireString(input.bundleId, "bundle ID", MAX_EVENT_ID_LENGTH);
|
|
266
|
-
const reporting = measure(model.countInstallations({
|
|
267
|
-
...scope,
|
|
268
|
-
sinceMs
|
|
269
|
-
}));
|
|
270
|
-
if (bundleId === void 0) return {
|
|
271
|
-
...scope,
|
|
272
|
-
window,
|
|
273
|
-
sinceMs,
|
|
274
|
-
beforeReceivedAtMs,
|
|
275
|
-
reportingInstallations: await reporting
|
|
276
|
-
};
|
|
277
|
-
const countOutcome = (outcome) => measure(model.countEvents({
|
|
278
|
-
filter: bundleFilter({
|
|
279
|
-
...scope,
|
|
280
|
-
bundleId,
|
|
281
|
-
outcome
|
|
282
|
-
}),
|
|
283
|
-
sinceMs,
|
|
284
|
-
beforeReceivedAtMs
|
|
285
|
-
}));
|
|
286
|
-
const [reportingInstallations, bundleInstallations, appliedReports, recoveredReports, adoptedReports] = await Promise.all([
|
|
287
|
-
reporting,
|
|
288
|
-
measure(model.countInstallations({
|
|
289
|
-
...scope,
|
|
290
|
-
sinceMs,
|
|
291
|
-
bundleId
|
|
292
|
-
})),
|
|
293
|
-
countOutcome("applied"),
|
|
294
|
-
countOutcome("recovered"),
|
|
295
|
-
countOutcome("adopted")
|
|
296
|
-
]);
|
|
297
|
-
return {
|
|
298
|
-
...scope,
|
|
299
|
-
window,
|
|
300
|
-
sinceMs,
|
|
301
|
-
beforeReceivedAtMs,
|
|
302
|
-
reportingInstallations,
|
|
303
|
-
bundle: {
|
|
304
|
-
bundleId,
|
|
305
|
-
reportingInstallations: bundleInstallations,
|
|
306
|
-
appliedReports,
|
|
307
|
-
recoveredReports,
|
|
308
|
-
adoptedReports
|
|
309
|
-
}
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
//#endregion
|
|
314
|
-
exports.createInsightsProvider = createInsightsProvider;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { InsightsProvider } from "./types.cjs";
|
|
2
|
-
import { InsightsModel } from "@hot-updater/plugin-core";
|
|
3
|
-
//#region src/insights/provider.d.ts
|
|
4
|
-
declare const createInsightsProvider: (model: InsightsModel) => InsightsProvider;
|
|
5
|
-
//#endregion
|
|
6
|
-
export { createInsightsProvider };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { InsightsProvider } from "./types.mjs";
|
|
2
|
-
import { InsightsModel } from "@hot-updater/plugin-core";
|
|
3
|
-
//#region src/insights/provider.d.ts
|
|
4
|
-
declare const createInsightsProvider: (model: InsightsModel) => InsightsProvider;
|
|
5
|
-
//#endregion
|
|
6
|
-
export { createInsightsProvider };
|
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
import { InsightsBadRequestError } from "./errors.mjs";
|
|
2
|
-
import { createBundleEventRow } from "./eventInput.mjs";
|
|
3
|
-
import { compareInsightsText, isInsightsMovementEvent, isUUIDv7, toInsightsInstallationRow } from "@hot-updater/plugin-core";
|
|
4
|
-
//#region src/insights/provider.ts
|
|
5
|
-
const DEFAULT_PAGE_LIMIT = 50;
|
|
6
|
-
const MAX_PAGE_LIMIT = 100;
|
|
7
|
-
const MAX_EVENT_ID_LENGTH = 1024;
|
|
8
|
-
const MAX_IDENTITY_LENGTH = 255;
|
|
9
|
-
const MAX_CURSOR_LENGTH = 8192;
|
|
10
|
-
const WINDOW_MS = {
|
|
11
|
-
"24h": 864e5,
|
|
12
|
-
"7d": 6048e5,
|
|
13
|
-
"30d": 2592e6
|
|
14
|
-
};
|
|
15
|
-
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
16
|
-
const requireString = (value, label, maximumLength) => {
|
|
17
|
-
if (typeof value !== "string" || value.length === 0 || value.length > maximumLength || new TextDecoder("utf-8", { ignoreBOM: true }).decode(new TextEncoder().encode(value)) !== value) throw new InsightsBadRequestError(`Invalid ${label}.`);
|
|
18
|
-
return value;
|
|
19
|
-
};
|
|
20
|
-
const requireTimestamp = (value, label) => {
|
|
21
|
-
if (!Number.isSafeInteger(value) || Number(value) < 0) throw new InsightsBadRequestError(`Invalid ${label}.`);
|
|
22
|
-
return Number(value);
|
|
23
|
-
};
|
|
24
|
-
const readLimit = (value) => {
|
|
25
|
-
const limit = value ?? DEFAULT_PAGE_LIMIT;
|
|
26
|
-
if (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_PAGE_LIMIT) throw new InsightsBadRequestError("Invalid page limit.");
|
|
27
|
-
return limit;
|
|
28
|
-
};
|
|
29
|
-
const encodeBase64Url = (value) => {
|
|
30
|
-
const bytes = new TextEncoder().encode(value);
|
|
31
|
-
let binary = "";
|
|
32
|
-
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
33
|
-
return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/u, "");
|
|
34
|
-
};
|
|
35
|
-
const decodeBase64Url = (value) => {
|
|
36
|
-
if (value.length === 0 || value.length > MAX_CURSOR_LENGTH) throw new InsightsBadRequestError("Invalid Insights cursor.");
|
|
37
|
-
try {
|
|
38
|
-
const base64 = value.replaceAll("-", "+").replaceAll("_", "/");
|
|
39
|
-
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
40
|
-
const binary = atob(padded);
|
|
41
|
-
const bytes = Uint8Array.from(binary, (character) => character.charCodeAt(0));
|
|
42
|
-
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
43
|
-
} catch {
|
|
44
|
-
throw new InsightsBadRequestError("Invalid Insights cursor.");
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const encodeCursor = (value) => encodeBase64Url(JSON.stringify(value));
|
|
48
|
-
const decodeCursor = (value) => {
|
|
49
|
-
try {
|
|
50
|
-
return JSON.parse(decodeBase64Url(value));
|
|
51
|
-
} catch (error) {
|
|
52
|
-
if (error instanceof InsightsBadRequestError) throw error;
|
|
53
|
-
throw new InsightsBadRequestError("Invalid Insights cursor.");
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
const readScope = (input) => {
|
|
57
|
-
if (input.platform !== "ios" && input.platform !== "android") throw new InsightsBadRequestError("Invalid Insights platform.");
|
|
58
|
-
return {
|
|
59
|
-
platform: input.platform,
|
|
60
|
-
channel: requireString(input.channel, "channel", MAX_EVENT_ID_LENGTH)
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
const bundleFilter = (input) => {
|
|
64
|
-
const scope = readScope(input);
|
|
65
|
-
const bundleId = requireString(input.bundleId, "bundle ID", MAX_EVENT_ID_LENGTH);
|
|
66
|
-
switch (input.outcome) {
|
|
67
|
-
case "applied": return {
|
|
68
|
-
...scope,
|
|
69
|
-
type: "UPDATE_APPLIED",
|
|
70
|
-
toBundleId: bundleId
|
|
71
|
-
};
|
|
72
|
-
case "recovered": return {
|
|
73
|
-
...scope,
|
|
74
|
-
type: "RECOVERED",
|
|
75
|
-
fromBundleId: bundleId
|
|
76
|
-
};
|
|
77
|
-
case "adopted": return {
|
|
78
|
-
...scope,
|
|
79
|
-
type: "RELEASE_ADOPTED",
|
|
80
|
-
toBundleId: bundleId
|
|
81
|
-
};
|
|
82
|
-
default: throw new InsightsBadRequestError("Invalid Insights outcome.");
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
const sameFilter = (left, right) => {
|
|
86
|
-
if (!isRecord(left) || left.kind !== right.kind) return false;
|
|
87
|
-
switch (right.kind) {
|
|
88
|
-
case "all": return true;
|
|
89
|
-
case "installationMovement": return left.installId === right.installId;
|
|
90
|
-
case "bundle": return left.platform === right.platform && left.channel === right.channel && left.type === right.type && (right.type === "RECOVERED" ? left.fromBundleId === right.fromBundleId : left.toBundleId === right.toBundleId);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
const readEventCursor = (value, filter) => {
|
|
94
|
-
const cursor = decodeCursor(value);
|
|
95
|
-
if (!isRecord(cursor) || cursor.version !== 2 || cursor.kind !== "events" || !isRecord(cursor.filter) || !isRecord(cursor.after)) throw new InsightsBadRequestError("Invalid Insights cursor.");
|
|
96
|
-
if (!sameFilter(cursor.filter, filter)) throw new InsightsBadRequestError("Insights cursor does not match the requested events.");
|
|
97
|
-
if (typeof cursor.after.id !== "string" || !isUUIDv7(cursor.after.id)) throw new InsightsBadRequestError("Invalid Insights event cursor ID.");
|
|
98
|
-
return {
|
|
99
|
-
after: {
|
|
100
|
-
id: cursor.after.id,
|
|
101
|
-
receivedAtMs: requireTimestamp(cursor.after.receivedAtMs, "event cursor")
|
|
102
|
-
},
|
|
103
|
-
beforeReceivedAtMs: requireTimestamp(cursor.beforeReceivedAtMs, "event cutoff"),
|
|
104
|
-
kind: "events",
|
|
105
|
-
sinceMs: requireTimestamp(cursor.sinceMs, "event start"),
|
|
106
|
-
filter,
|
|
107
|
-
version: 2
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
const readUserInstallationCursor = (value, userId) => {
|
|
111
|
-
const cursor = decodeCursor(value);
|
|
112
|
-
if (!isRecord(cursor) || cursor.version !== 1 || cursor.kind !== "user-installations" || cursor.userId !== userId) throw new InsightsBadRequestError("Insights cursor does not match the requested user ID.");
|
|
113
|
-
return {
|
|
114
|
-
afterInstallId: requireString(cursor.afterInstallId, "installation cursor", MAX_IDENTITY_LENGTH),
|
|
115
|
-
kind: "user-installations",
|
|
116
|
-
userId,
|
|
117
|
-
version: 1
|
|
118
|
-
};
|
|
119
|
-
};
|
|
120
|
-
const compareEventNewest = (left, right) => right.received_at_ms - left.received_at_ms || compareInsightsText(right.id, left.id);
|
|
121
|
-
const isAfterEventCursor = (row, after) => row.received_at_ms < after.receivedAtMs || row.received_at_ms === after.receivedAtMs && compareInsightsText(row.id, after.id) < 0;
|
|
122
|
-
const assertEventRows = (rows, input) => {
|
|
123
|
-
if (rows.length > input.limit) throw new Error("Insights database returned too many event rows.");
|
|
124
|
-
for (let index = 0; index < rows.length; index += 1) {
|
|
125
|
-
const row = rows[index];
|
|
126
|
-
const previous = rows[index - 1];
|
|
127
|
-
if (!row || typeof row.id !== "string" || !isUUIDv7(row.id) || !Number.isSafeInteger(row.received_at_ms) || row.received_at_ms >= input.beforeReceivedAtMs || row.received_at_ms < input.sinceMs || previous !== void 0 && compareEventNewest(previous, row) >= 0 || input.after !== void 0 && !isAfterEventCursor(row, input.after) || input.filter.kind === "installationMovement" && (row.install_id !== input.filter.installId || !isInsightsMovementEvent(row)) || input.filter.kind === "bundle" && (row.platform !== input.filter.platform || row.channel !== input.filter.channel || row.type !== input.filter.type || (input.filter.type === "RECOVERED" ? row.from_bundle_id !== input.filter.fromBundleId : row.to_bundle_id !== input.filter.toBundleId))) throw new Error("Insights database returned invalid event rows.");
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const toEventHistoryRow = (row) => ({
|
|
131
|
-
appVersion: row.app_version,
|
|
132
|
-
channel: row.channel,
|
|
133
|
-
cohort: row.cohort,
|
|
134
|
-
fromBundleId: row.from_bundle_id,
|
|
135
|
-
id: row.id,
|
|
136
|
-
installId: row.install_id,
|
|
137
|
-
platform: row.platform,
|
|
138
|
-
receivedAtMs: row.received_at_ms,
|
|
139
|
-
toBundleId: row.to_bundle_id,
|
|
140
|
-
type: row.type,
|
|
141
|
-
userId: row.user_id,
|
|
142
|
-
username: row.username
|
|
143
|
-
});
|
|
144
|
-
const toInstallationRow = (row) => ({
|
|
145
|
-
appVersion: row.app_version,
|
|
146
|
-
channel: row.channel,
|
|
147
|
-
cohort: row.cohort,
|
|
148
|
-
installId: row.install_id,
|
|
149
|
-
lastKnownBundleId: row.to_bundle_id,
|
|
150
|
-
latestStatus: row.type,
|
|
151
|
-
platform: row.platform,
|
|
152
|
-
receivedAtMs: row.received_at_ms,
|
|
153
|
-
userId: row.user_id,
|
|
154
|
-
username: row.username
|
|
155
|
-
});
|
|
156
|
-
const pageEventRows = async (model, input, filter, map) => {
|
|
157
|
-
const limit = readLimit(input.limit);
|
|
158
|
-
const cursor = input.cursor === void 0 ? void 0 : readEventCursor(input.cursor, filter);
|
|
159
|
-
const beforeReceivedAtMs = cursor?.beforeReceivedAtMs ?? (input.beforeReceivedAtMs === void 0 ? Date.now() : requireTimestamp(input.beforeReceivedAtMs, "event cutoff"));
|
|
160
|
-
if (cursor !== void 0 && input.beforeReceivedAtMs !== void 0 && input.beforeReceivedAtMs !== beforeReceivedAtMs) throw new InsightsBadRequestError("Insights cursor does not match the requested event cutoff.");
|
|
161
|
-
const sinceMs = cursor?.sinceMs ?? (input.sinceMs === void 0 ? 0 : requireTimestamp(input.sinceMs, "event start"));
|
|
162
|
-
if (sinceMs > beforeReceivedAtMs || input.sinceMs !== void 0 && input.sinceMs !== sinceMs || cursor !== void 0 && (cursor.after.receivedAtMs < sinceMs || cursor.after.receivedAtMs >= beforeReceivedAtMs)) throw new InsightsBadRequestError("Insights cursor or range does not match the requested event start.");
|
|
163
|
-
const databaseInput = {
|
|
164
|
-
filter,
|
|
165
|
-
sinceMs,
|
|
166
|
-
beforeReceivedAtMs,
|
|
167
|
-
...cursor === void 0 ? {} : { after: cursor.after },
|
|
168
|
-
limit: limit + 1
|
|
169
|
-
};
|
|
170
|
-
const rows = await model.listEvents(databaseInput);
|
|
171
|
-
assertEventRows(rows, databaseInput);
|
|
172
|
-
const pageRows = rows.slice(0, limit);
|
|
173
|
-
const last = pageRows.at(-1);
|
|
174
|
-
return {
|
|
175
|
-
beforeReceivedAtMs,
|
|
176
|
-
data: pageRows.map(map),
|
|
177
|
-
nextCursor: rows.length > limit && last ? encodeCursor({
|
|
178
|
-
after: {
|
|
179
|
-
id: last.id,
|
|
180
|
-
receivedAtMs: last.received_at_ms
|
|
181
|
-
},
|
|
182
|
-
beforeReceivedAtMs,
|
|
183
|
-
kind: "events",
|
|
184
|
-
filter,
|
|
185
|
-
sinceMs,
|
|
186
|
-
version: 2
|
|
187
|
-
}) : null
|
|
188
|
-
};
|
|
189
|
-
};
|
|
190
|
-
const assertInstallationRows = (rows, input) => {
|
|
191
|
-
if (rows.length > input.limit) throw new Error("Insights database returned too many installation rows.");
|
|
192
|
-
for (let index = 0; index < rows.length; index += 1) {
|
|
193
|
-
const row = rows[index];
|
|
194
|
-
const previous = rows[index - 1];
|
|
195
|
-
if (!row || row.user_id !== input.userId || input.afterInstallId !== void 0 && compareInsightsText(row.install_id, input.afterInstallId) <= 0 || previous !== void 0 && compareInsightsText(previous.install_id, row.install_id) >= 0) throw new Error("Insights database returned invalid installation rows.");
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
const createInsightsProvider = (model) => Object.freeze({
|
|
199
|
-
async appendBundleEvent(input) {
|
|
200
|
-
const event = createBundleEventRow(input);
|
|
201
|
-
await model.record({
|
|
202
|
-
event,
|
|
203
|
-
installation: toInsightsInstallationRow(event)
|
|
204
|
-
});
|
|
205
|
-
},
|
|
206
|
-
listEvents(input) {
|
|
207
|
-
const filter = input.bundle === void 0 ? { kind: "all" } : {
|
|
208
|
-
kind: "bundle",
|
|
209
|
-
...bundleFilter(input.bundle)
|
|
210
|
-
};
|
|
211
|
-
return pageEventRows(model, input, filter, toEventHistoryRow);
|
|
212
|
-
},
|
|
213
|
-
async listInstallationEvents(input) {
|
|
214
|
-
if ("bundle" in input && input.bundle !== void 0) throw new InsightsBadRequestError("Installation movement queries cannot include a bundle filter.");
|
|
215
|
-
const installId = requireString(input.installId, "install ID", MAX_IDENTITY_LENGTH);
|
|
216
|
-
return pageEventRows(model, input, {
|
|
217
|
-
kind: "installationMovement",
|
|
218
|
-
installId
|
|
219
|
-
}, (row) => toEventHistoryRow(row));
|
|
220
|
-
},
|
|
221
|
-
async getInstallation({ installId }) {
|
|
222
|
-
const normalizedInstallId = requireString(installId, "install ID", MAX_IDENTITY_LENGTH);
|
|
223
|
-
const rows = await model.findInstallations({ installId: normalizedInstallId });
|
|
224
|
-
const row = rows[0] ?? null;
|
|
225
|
-
if (rows.length > 1 || row !== null && row.install_id !== normalizedInstallId) throw new Error("Insights database returned an invalid installation.");
|
|
226
|
-
return row === null ? null : toInstallationRow(row);
|
|
227
|
-
},
|
|
228
|
-
async pageInstallationsByCurrentUserId(input) {
|
|
229
|
-
const userId = requireString(input.userId, "user ID", MAX_IDENTITY_LENGTH);
|
|
230
|
-
const limit = readLimit(input.limit);
|
|
231
|
-
const cursor = input.cursor === void 0 ? void 0 : readUserInstallationCursor(input.cursor, userId);
|
|
232
|
-
const databaseInput = {
|
|
233
|
-
userId,
|
|
234
|
-
...cursor === void 0 ? {} : { afterInstallId: cursor.afterInstallId },
|
|
235
|
-
limit: limit + 1
|
|
236
|
-
};
|
|
237
|
-
const rows = await model.findInstallations(databaseInput);
|
|
238
|
-
assertInstallationRows(rows, databaseInput);
|
|
239
|
-
const pageRows = rows.slice(0, limit);
|
|
240
|
-
const last = pageRows.at(-1);
|
|
241
|
-
return {
|
|
242
|
-
data: pageRows.map(toInstallationRow),
|
|
243
|
-
nextCursor: rows.length > limit && last ? encodeCursor({
|
|
244
|
-
afterInstallId: last.install_id,
|
|
245
|
-
kind: "user-installations",
|
|
246
|
-
userId,
|
|
247
|
-
version: 1
|
|
248
|
-
}) : null
|
|
249
|
-
};
|
|
250
|
-
},
|
|
251
|
-
async getReportingOverview(input) {
|
|
252
|
-
const scope = readScope(input);
|
|
253
|
-
const { window } = input;
|
|
254
|
-
if (!Object.hasOwn(WINDOW_MS, window)) throw new InsightsBadRequestError("Invalid reporting installation window.");
|
|
255
|
-
const beforeReceivedAtMs = Date.now();
|
|
256
|
-
const sinceMs = Math.max(0, beforeReceivedAtMs - WINDOW_MS[window]);
|
|
257
|
-
const measure = async (count) => {
|
|
258
|
-
const value = await count;
|
|
259
|
-
if (!Number.isSafeInteger(value) || value < 0) throw new Error("Insights database returned an invalid count.");
|
|
260
|
-
return {
|
|
261
|
-
count: value,
|
|
262
|
-
measuredAtMs: Date.now()
|
|
263
|
-
};
|
|
264
|
-
};
|
|
265
|
-
const bundleId = input.bundleId === void 0 ? void 0 : requireString(input.bundleId, "bundle ID", MAX_EVENT_ID_LENGTH);
|
|
266
|
-
const reporting = measure(model.countInstallations({
|
|
267
|
-
...scope,
|
|
268
|
-
sinceMs
|
|
269
|
-
}));
|
|
270
|
-
if (bundleId === void 0) return {
|
|
271
|
-
...scope,
|
|
272
|
-
window,
|
|
273
|
-
sinceMs,
|
|
274
|
-
beforeReceivedAtMs,
|
|
275
|
-
reportingInstallations: await reporting
|
|
276
|
-
};
|
|
277
|
-
const countOutcome = (outcome) => measure(model.countEvents({
|
|
278
|
-
filter: bundleFilter({
|
|
279
|
-
...scope,
|
|
280
|
-
bundleId,
|
|
281
|
-
outcome
|
|
282
|
-
}),
|
|
283
|
-
sinceMs,
|
|
284
|
-
beforeReceivedAtMs
|
|
285
|
-
}));
|
|
286
|
-
const [reportingInstallations, bundleInstallations, appliedReports, recoveredReports, adoptedReports] = await Promise.all([
|
|
287
|
-
reporting,
|
|
288
|
-
measure(model.countInstallations({
|
|
289
|
-
...scope,
|
|
290
|
-
sinceMs,
|
|
291
|
-
bundleId
|
|
292
|
-
})),
|
|
293
|
-
countOutcome("applied"),
|
|
294
|
-
countOutcome("recovered"),
|
|
295
|
-
countOutcome("adopted")
|
|
296
|
-
]);
|
|
297
|
-
return {
|
|
298
|
-
...scope,
|
|
299
|
-
window,
|
|
300
|
-
sinceMs,
|
|
301
|
-
beforeReceivedAtMs,
|
|
302
|
-
reportingInstallations,
|
|
303
|
-
bundle: {
|
|
304
|
-
bundleId,
|
|
305
|
-
reportingInstallations: bundleInstallations,
|
|
306
|
-
appliedReports,
|
|
307
|
-
recoveredReports,
|
|
308
|
-
adoptedReports
|
|
309
|
-
}
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
//#endregion
|
|
314
|
-
export { createInsightsProvider };
|