@hot-updater/server 0.36.6 → 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/databaseConstraintErrors.cjs +23 -0
- package/dist/adapters/databaseConstraintErrors.mjs +21 -0
- package/dist/adapters/databasePluginUtils.cjs +117 -0
- package/dist/adapters/databasePluginUtils.mjs +106 -0
- package/dist/adapters/drizzle.cjs +53 -176
- package/dist/adapters/drizzle.d.cts +6 -4
- package/dist/adapters/drizzle.d.mts +6 -4
- package/dist/adapters/drizzle.mjs +54 -177
- package/dist/adapters/drizzleCrud.cjs +250 -0
- package/dist/adapters/drizzleCrud.mjs +249 -0
- package/dist/adapters/drizzleLazyDB.cjs +84 -36
- package/dist/adapters/drizzleLazyDB.mjs +84 -36
- package/dist/adapters/drizzleQuery.cjs +68 -0
- package/dist/adapters/drizzleQuery.mjs +67 -0
- package/dist/adapters/kysely.cjs +18 -138
- package/dist/adapters/kysely.d.cts +3 -12
- package/dist/adapters/kysely.d.mts +3 -12
- package/dist/adapters/kysely.mjs +19 -139
- package/dist/adapters/kyselyCrud.cjs +254 -0
- package/dist/adapters/kyselyCrud.mjs +253 -0
- package/dist/adapters/kyselyQuery.cjs +56 -0
- package/dist/adapters/kyselyQuery.mjs +55 -0
- package/dist/adapters/mongodb.cjs +21 -174
- package/dist/adapters/mongodb.d.cts +3 -2
- package/dist/adapters/mongodb.d.mts +3 -2
- package/dist/adapters/mongodb.mjs +21 -174
- package/dist/adapters/mongodbCollections.cjs +36 -0
- package/dist/adapters/mongodbCollections.mjs +30 -0
- package/dist/adapters/mongodbQuery.cjs +77 -0
- package/dist/adapters/mongodbQuery.mjs +70 -0
- package/dist/adapters/mongodbReads.cjs +148 -0
- package/dist/adapters/mongodbReads.mjs +147 -0
- package/dist/adapters/mongodbWrites.cjs +134 -0
- package/dist/adapters/mongodbWrites.mjs +133 -0
- package/dist/adapters/prisma.cjs +205 -180
- package/dist/adapters/prisma.d.cts +3 -3
- package/dist/adapters/prisma.d.mts +3 -3
- package/dist/adapters/prisma.mjs +206 -181
- package/dist/adapters/prismaQuery.cjs +58 -0
- package/dist/adapters/prismaQuery.mjs +56 -0
- package/dist/adapters/prismaRows.cjs +198 -0
- package/dist/adapters/prismaRows.mjs +188 -0
- package/dist/analytics/bounded/activeOverview.cjs +87 -0
- package/dist/analytics/bounded/activeOverview.mjs +85 -0
- package/dist/analytics/bounded/installationSearch.cjs +42 -0
- package/dist/analytics/bounded/installationSearch.mjs +42 -0
- package/dist/analytics/bounded/persistence.cjs +39 -0
- package/dist/analytics/bounded/persistence.mjs +38 -0
- package/dist/analytics/bounded/provider.cjs +170 -0
- package/dist/analytics/bounded/provider.d.cts +7 -0
- package/dist/analytics/bounded/provider.d.mts +7 -0
- package/dist/analytics/bounded/provider.mjs +170 -0
- package/dist/analytics/bounded/scan.cjs +144 -0
- package/dist/analytics/bounded/scan.mjs +138 -0
- package/dist/analytics/domain.d.cts +121 -0
- package/dist/analytics/domain.d.mts +121 -0
- package/dist/analytics/errors.cjs +22 -0
- package/dist/analytics/errors.mjs +20 -0
- package/dist/analytics/eventInput.cjs +111 -0
- package/dist/analytics/eventInput.mjs +111 -0
- package/dist/analytics/persistence.d.cts +6 -0
- package/dist/analytics/persistence.d.mts +6 -0
- package/dist/analytics/queryInput.cjs +58 -0
- package/dist/analytics/queryInput.mjs +55 -0
- package/dist/analytics/routes.cjs +63 -0
- package/dist/analytics/routes.mjs +61 -0
- package/dist/analytics/types.d.cts +17 -0
- package/dist/analytics/types.d.mts +17 -0
- package/dist/apiKeys.cjs +139 -0
- package/dist/apiKeys.d.cts +50 -0
- package/dist/apiKeys.d.mts +50 -0
- package/dist/apiKeys.mjs +133 -0
- package/dist/createHotUpdaterCore.cjs +48 -26
- package/dist/createHotUpdaterCore.d.cts +49 -17
- package/dist/createHotUpdaterCore.d.mts +49 -17
- package/dist/createHotUpdaterCore.mjs +51 -29
- package/dist/db/createBundleDiff.cjs +45 -68
- package/dist/db/createBundleDiff.d.cts +3 -3
- package/dist/db/createBundleDiff.d.mts +3 -3
- package/dist/db/createBundleDiff.mjs +47 -68
- package/dist/db/databasePluginCore.cjs +138 -0
- package/dist/db/databasePluginCore.mjs +137 -0
- package/dist/db/fixedMigrator.cjs +36 -93
- package/dist/db/fixedMigrator.mjs +36 -93
- package/dist/db/fixedMigratorMongo.cjs +319 -0
- package/dist/db/fixedMigratorMongo.mjs +319 -0
- package/dist/db/fixedMigratorShared.cjs +23 -0
- package/dist/db/fixedMigratorShared.mjs +18 -0
- package/dist/db/mongoMigrationExecution.cjs +11 -0
- package/dist/db/mongoMigrationExecution.mjs +11 -0
- package/dist/db/releaseCatalog.cjs +66 -0
- package/dist/db/releaseCatalog.d.cts +17 -0
- package/dist/db/releaseCatalog.d.mts +17 -0
- package/dist/db/releaseCatalog.mjs +64 -0
- package/dist/db/schema/mongodb.cjs +14 -6
- package/dist/db/schema/mongodb.mjs +14 -6
- package/dist/db/schema/registry.cjs +0 -2
- package/dist/db/schema/registry.mjs +1 -2
- package/dist/db/schema/sql.cjs +31 -14
- package/dist/db/schema/sql.mjs +32 -13
- package/dist/db/schema/sqlOperations.cjs +4 -4
- package/dist/db/schema/sqlOperations.mjs +5 -5
- package/dist/db/schemaGenerators.cjs +52 -18
- package/dist/db/schemaGenerators.mjs +52 -18
- package/dist/db/schemaReadiness.cjs +4 -3
- package/dist/db/schemaReadiness.mjs +4 -3
- package/dist/db/sqlMigrationExecution.cjs +67 -0
- package/dist/db/sqlMigrationExecution.mjs +66 -0
- package/dist/db/types.cjs +2 -6
- package/dist/db/types.d.cts +60 -16
- package/dist/db/types.d.mts +60 -16
- package/dist/db/types.mjs +3 -6
- package/dist/db/updateArtifacts.cjs +114 -60
- package/dist/db/updateArtifacts.mjs +117 -62
- package/dist/handler.cjs +108 -282
- package/dist/handler.d.cts +3 -46
- package/dist/handler.d.mts +3 -46
- package/dist/handler.mjs +107 -281
- package/dist/handlerBundleRoutes.cjs +155 -0
- package/dist/handlerBundleRoutes.mjs +155 -0
- package/dist/handlerErrors.cjs +9 -0
- package/dist/handlerErrors.mjs +9 -0
- package/dist/handlerParameters.cjs +39 -0
- package/dist/handlerParameters.mjs +34 -0
- package/dist/handlerReleaseCatalogRoutes.cjs +111 -0
- package/dist/handlerReleaseCatalogRoutes.mjs +110 -0
- package/dist/handlerReleaseManagementRoutes.cjs +125 -0
- package/dist/handlerReleaseManagementRoutes.mjs +124 -0
- package/dist/handlerTypes.d.cts +63 -0
- package/dist/handlerTypes.d.mts +63 -0
- package/dist/handlerVersionRoutes.cjs +10 -0
- package/dist/handlerVersionRoutes.d.cts +4 -0
- package/dist/handlerVersionRoutes.d.mts +4 -0
- package/dist/handlerVersionRoutes.mjs +9 -0
- package/dist/index.cjs +10 -1
- package/dist/index.d.cts +9 -3
- package/dist/index.d.mts +9 -3
- package/dist/index.mjs +5 -2
- package/dist/node.cjs +19 -12
- package/dist/node.d.cts +15 -10
- package/dist/node.d.mts +15 -10
- package/dist/node.mjs +19 -13
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/schema/dsl.cjs +19 -2
- package/dist/schema/dsl.mjs +17 -3
- package/dist/schema/index.cjs +1 -8
- package/dist/schema/index.mjs +2 -8
- package/dist/schema/types.cjs +3 -1
- package/dist/schema/types.mjs +3 -2
- package/dist/schema/v1_0_0.cjs +364 -0
- package/dist/schema/v1_0_0.mjs +357 -0
- package/dist/storageAccess.cjs +69 -25
- package/dist/storageAccess.mjs +68 -25
- package/dist/types/index.d.cts +4 -3
- package/dist/types/index.d.mts +5 -4
- package/package.json +8 -7
- package/src/adapters/databaseConstraintErrors.ts +25 -0
- package/src/adapters/databasePluginTestDatabase.ts +106 -0
- package/src/adapters/databasePluginUtils.spec.ts +29 -0
- package/src/adapters/databasePluginUtils.ts +214 -0
- package/src/adapters/drizzle.spec.ts +208 -47
- package/src/adapters/drizzle.ts +106 -293
- package/src/adapters/drizzleCrud.ts +419 -0
- package/src/adapters/drizzleLazyDB.ts +206 -100
- package/src/adapters/drizzleQuery.ts +164 -0
- package/src/adapters/kysely.spec.ts +126 -88
- package/src/adapters/kysely.ts +76 -334
- package/src/adapters/kyselyCrud.ts +588 -0
- package/src/adapters/kyselyQuery.ts +128 -0
- package/src/adapters/mongodb.spec.ts +141 -0
- package/src/adapters/mongodb.ts +60 -288
- package/src/adapters/mongodbCollections.ts +62 -0
- package/src/adapters/mongodbQuery.ts +226 -0
- package/src/adapters/mongodbReads.ts +311 -0
- package/src/adapters/mongodbRows.spec.ts +64 -0
- package/src/adapters/mongodbRows.ts +118 -0
- package/src/adapters/mongodbTestClient.ts +342 -0
- package/src/adapters/mongodbTestFilter.ts +166 -0
- package/src/adapters/mongodbWrites.ts +260 -0
- package/src/adapters/prisma.spec.ts +196 -0
- package/src/adapters/prisma.ts +417 -292
- package/src/adapters/prismaQuery.ts +129 -0
- package/src/adapters/prismaRows.spec.ts +98 -0
- package/src/adapters/prismaRows.ts +301 -0
- package/src/adapters/prismaTestClient.ts +396 -0
- package/src/analytics/analytics.integration.spec.ts +144 -0
- package/src/analytics/bounded/activeOverview.spec.ts +189 -0
- package/src/analytics/bounded/activeOverview.ts +148 -0
- package/src/analytics/bounded/installationSearch.spec.ts +130 -0
- package/src/analytics/bounded/installationSearch.ts +69 -0
- package/src/analytics/bounded/persistence.ts +43 -0
- package/src/analytics/bounded/provider.spec.ts +219 -0
- package/src/analytics/bounded/provider.ts +229 -0
- package/src/analytics/bounded/scan.ts +235 -0
- package/src/analytics/domain.ts +139 -0
- package/src/analytics/errors.ts +18 -0
- package/src/analytics/eventInput.ts +168 -0
- package/src/analytics/persistence.ts +10 -0
- package/src/analytics/queryInput.ts +102 -0
- package/src/analytics/routes.ts +123 -0
- package/src/analytics/types.ts +44 -0
- package/src/apiKeys.integration.spec.ts +192 -0
- package/src/apiKeys.spec.ts +162 -0
- package/src/apiKeys.ts +220 -0
- package/src/createHotUpdaterCore.ts +164 -100
- package/src/db/bundleRows.spec.ts +164 -0
- package/src/db/bundleRows.ts +10 -140
- package/src/db/createBundleDiff.spec.ts +172 -127
- package/src/db/createBundleDiff.ts +91 -110
- package/src/db/databasePluginCore.spec.ts +62 -0
- package/src/db/databasePluginCore.testFixtures.ts +76 -0
- package/src/db/databasePluginCore.ts +185 -0
- package/src/db/databasePluginUpdateCheck.bench.ts +107 -0
- package/src/db/fixedMigrator.spec.ts +184 -42
- package/src/db/fixedMigrator.ts +64 -205
- package/src/db/fixedMigratorMongo.spec.ts +192 -0
- package/src/db/fixedMigratorMongo.ts +425 -0
- package/src/db/fixedMigratorShared.ts +36 -0
- package/src/db/hotUpdaterSchema.ts +0 -1
- package/src/db/index.spec.ts +333 -1025
- package/src/db/mongoMigrationExecution.ts +23 -0
- package/src/db/node-sqlite.d.ts +25 -0
- package/src/db/releaseCatalog.ts +148 -0
- package/src/db/schema/mongodb.ts +20 -6
- package/src/db/schema/sql.ts +52 -11
- package/src/db/schema/sqlOperations.ts +5 -4
- package/src/db/schemaGenerators.ts +107 -23
- package/src/db/schemaReadiness.ts +2 -1
- package/src/db/sqlMigrationExecution.spec.ts +65 -0
- package/src/db/sqlMigrationExecution.ts +108 -0
- package/src/db/types.spec.ts +55 -0
- package/src/db/types.ts +129 -65
- package/src/db/updateArtifacts.spec.ts +359 -0
- package/src/db/updateArtifacts.ts +290 -126
- package/src/handler-standalone.integration.spec.ts +401 -364
- package/src/handler.spec.ts +137 -582
- package/src/handler.testFixtures.ts +30 -0
- package/src/handler.ts +218 -616
- package/src/handlerAuth.integration.spec.ts +33 -0
- package/src/handlerBundleRoutes.ts +259 -0
- package/src/handlerErrors.ts +7 -0
- package/src/handlerManagement.spec.ts +262 -0
- package/src/handlerParameters.ts +105 -0
- package/src/handlerReleaseCatalogRoutes.ts +154 -0
- package/src/handlerReleaseManagementRoutes.ts +213 -0
- package/src/handlerTypes.ts +96 -0
- package/src/handlerVersionRoutes.ts +12 -0
- package/src/index.ts +22 -2
- package/src/node.spec.ts +79 -10
- package/src/node.ts +20 -17
- package/src/releaseCatalogRoutes.integration.spec.ts +238 -0
- package/src/runtime.spec.ts +126 -959
- package/src/runtime.testFixtures.ts +71 -0
- package/src/schema/dsl-all-versions.spec.ts +26 -17
- package/src/schema/dsl.ts +41 -2
- package/src/schema/index.ts +3 -9
- package/src/schema/types.ts +9 -3
- package/src/schema/v1_0_0.spec.ts +146 -0
- package/src/schema/v1_0_0.ts +465 -0
- package/src/storageAccess.spec.ts +127 -32
- package/src/storageAccess.ts +106 -55
- package/src/types/index.ts +2 -1
- package/dist/db/bundleRows.cjs +0 -82
- package/dist/db/bundleRows.d.cts +0 -33
- package/dist/db/bundleRows.d.mts +0 -33
- package/dist/db/bundleRows.mjs +0 -79
- package/dist/db/pluginCore.cjs +0 -242
- package/dist/db/pluginCore.mjs +0 -241
- package/dist/db/schema/sqlMigrations.cjs +0 -74
- package/dist/db/schema/sqlMigrations.mjs +0 -73
- package/dist/db/schemaEnhancements.cjs +0 -20
- package/dist/db/schemaEnhancements.mjs +0 -19
- package/dist/route.cjs +0 -7
- package/dist/route.mjs +0 -7
- package/dist/schema/v0_21_0.cjs +0 -38
- package/dist/schema/v0_21_0.mjs +0 -37
- package/dist/schema/v0_29_0.cjs +0 -45
- package/dist/schema/v0_29_0.mjs +0 -44
- package/dist/schema/v0_31_0.cjs +0 -82
- package/dist/schema/v0_31_0.mjs +0 -80
- package/src/db/pluginCore.spec.ts +0 -1095
- package/src/db/pluginCore.ts +0 -475
- package/src/db/pluginUpdateCheck.bench.ts +0 -254
- package/src/db/schema/sqlMigrations.ts +0 -219
- package/src/db/schemaEnhancements.ts +0 -55
- package/src/route.ts +0 -7
- package/src/schema/v0_21_0.ts +0 -52
- package/src/schema/v0_29_0.ts +0 -62
- package/src/schema/v0_31_0.spec.ts +0 -73
- package/src/schema/v0_31_0.ts +0 -120
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { AnalyticsScanLimitExceededError } from "../errors.mjs";
|
|
2
|
+
import { ACTIVE_BUNDLE_EVENT_TYPES, getActiveWindowDefinition } from "./activeOverview.mjs";
|
|
3
|
+
//#region src/analytics/bounded/scan.ts
|
|
4
|
+
const ANALYTICS_SCAN_MAX_ROWS = 5e4;
|
|
5
|
+
const ANALYTICS_MATERIALIZATION_LIMIT = ANALYTICS_SCAN_MAX_ROWS + 1;
|
|
6
|
+
const ANALYTICS_SCAN_PAGE_SIZE = 1e3;
|
|
7
|
+
const ANALYTICS_LOWER_BOUND_ID = "00000000-0000-0000-0000-000000000000";
|
|
8
|
+
const compareCodePoints = (left, right) => {
|
|
9
|
+
if (left < right) return -1;
|
|
10
|
+
if (left > right) return 1;
|
|
11
|
+
return 0;
|
|
12
|
+
};
|
|
13
|
+
const compareEventNewest = (left, right) => right.received_at_ms - left.received_at_ms || compareCodePoints(right.id, left.id);
|
|
14
|
+
const compareEventOldest = (left, right) => left.received_at_ms - right.received_at_ms || compareCodePoints(left.id, right.id);
|
|
15
|
+
const materializeEventRows = async (scope) => {
|
|
16
|
+
const rows = [];
|
|
17
|
+
const seenIds = /* @__PURE__ */ new Set();
|
|
18
|
+
let after = scope.lowerBoundMs === void 0 ? void 0 : {
|
|
19
|
+
receivedAtMs: scope.lowerBoundMs,
|
|
20
|
+
id: ANALYTICS_LOWER_BOUND_ID
|
|
21
|
+
};
|
|
22
|
+
while (rows.length < ANALYTICS_MATERIALIZATION_LIMIT) {
|
|
23
|
+
const limit = Math.min(ANALYTICS_SCAN_PAGE_SIZE, ANALYTICS_MATERIALIZATION_LIMIT - rows.length);
|
|
24
|
+
const page = await scope.persistence.scan({
|
|
25
|
+
beforeReceivedAtMs: scope.cutoffMs,
|
|
26
|
+
...after === void 0 ? {} : { after },
|
|
27
|
+
limit
|
|
28
|
+
});
|
|
29
|
+
if (page.length === 0) break;
|
|
30
|
+
if (page.length > limit) throw new AnalyticsPersistenceOrderError();
|
|
31
|
+
let previous = after === void 0 ? void 0 : {
|
|
32
|
+
received_at_ms: after.receivedAtMs,
|
|
33
|
+
id: after.id
|
|
34
|
+
};
|
|
35
|
+
for (const row of page) {
|
|
36
|
+
if (row.received_at_ms >= scope.cutoffMs || previous !== void 0 && compareEventOldest(previous, row) >= 0 || seenIds.has(row.id)) throw new AnalyticsPersistenceOrderError();
|
|
37
|
+
seenIds.add(row.id);
|
|
38
|
+
rows.push(row);
|
|
39
|
+
previous = row;
|
|
40
|
+
}
|
|
41
|
+
const last = page.at(-1);
|
|
42
|
+
if (last !== void 0) after = {
|
|
43
|
+
receivedAtMs: last.received_at_ms,
|
|
44
|
+
id: last.id
|
|
45
|
+
};
|
|
46
|
+
if (rows.length > 5e4) throw new AnalyticsScanLimitExceededError(ANALYTICS_SCAN_MAX_ROWS);
|
|
47
|
+
}
|
|
48
|
+
return rows;
|
|
49
|
+
};
|
|
50
|
+
var AnalyticsPersistenceOrderError = class extends Error {
|
|
51
|
+
name = "AnalyticsPersistenceOrderError";
|
|
52
|
+
constructor() {
|
|
53
|
+
super("Analytics persistence did not advance its scan cursor.");
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const materializeActiveRows = async (scope, window) => {
|
|
57
|
+
const definition = getActiveWindowDefinition(window);
|
|
58
|
+
const durationMs = definition.bucketCount * definition.bucketSizeMs;
|
|
59
|
+
return (await materializeEventRows({
|
|
60
|
+
...scope,
|
|
61
|
+
lowerBoundMs: scope.cutoffMs - durationMs
|
|
62
|
+
})).filter((row) => row.received_at_ms >= scope.cutoffMs - durationMs && ACTIVE_BUNDLE_EVENT_TYPES.includes(row.type));
|
|
63
|
+
};
|
|
64
|
+
const startOfUtcHour = (value) => {
|
|
65
|
+
const date = new Date(value);
|
|
66
|
+
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours());
|
|
67
|
+
};
|
|
68
|
+
const startOfUtcDay = (value) => {
|
|
69
|
+
const date = new Date(value);
|
|
70
|
+
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
|
|
71
|
+
};
|
|
72
|
+
const getWindowRange = (window, now) => {
|
|
73
|
+
if (window === "24h") return {
|
|
74
|
+
sizeMs: 3600 * 1e3,
|
|
75
|
+
rangeStart: startOfUtcHour(now) - 1380 * 60 * 1e3
|
|
76
|
+
};
|
|
77
|
+
const days = window === "7d" ? 7 : 30;
|
|
78
|
+
return {
|
|
79
|
+
sizeMs: 1440 * 60 * 1e3,
|
|
80
|
+
rangeStart: startOfUtcDay(now) - (days - 1) * 24 * 60 * 60 * 1e3
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const materializeRowsForWindow = async (scope, window) => {
|
|
84
|
+
if (window === "all") return materializeEventRows(scope);
|
|
85
|
+
const range = getWindowRange(window, scope.cutoffMs);
|
|
86
|
+
return (await materializeEventRows({
|
|
87
|
+
...scope,
|
|
88
|
+
lowerBoundMs: range.rangeStart
|
|
89
|
+
})).filter(({ received_at_ms }) => received_at_ms >= range.rangeStart);
|
|
90
|
+
};
|
|
91
|
+
const bucketStart = (receivedAtMs, sizeMs) => sizeMs === 3600 * 1e3 ? startOfUtcHour(receivedAtMs) : startOfUtcDay(receivedAtMs);
|
|
92
|
+
const createSeries = (request) => {
|
|
93
|
+
const range = request.window === "all" ? void 0 : getWindowRange(request.window, request.cutoffMs);
|
|
94
|
+
const sizeMs = range?.sizeMs ?? 1440 * 60 * 1e3;
|
|
95
|
+
const installIdsByBucket = /* @__PURE__ */ new Map();
|
|
96
|
+
let oldestMs = request.cutoffMs;
|
|
97
|
+
for (const row of request.rows) {
|
|
98
|
+
oldestMs = Math.min(oldestMs, row.received_at_ms);
|
|
99
|
+
const start = bucketStart(row.received_at_ms, sizeMs);
|
|
100
|
+
const installIds = installIdsByBucket.get(start) ?? /* @__PURE__ */ new Set();
|
|
101
|
+
installIds.add(row.install_id);
|
|
102
|
+
installIdsByBucket.set(start, installIds);
|
|
103
|
+
}
|
|
104
|
+
const first = range?.rangeStart ?? startOfUtcDay(oldestMs);
|
|
105
|
+
const last = bucketStart(request.cutoffMs, sizeMs);
|
|
106
|
+
return Array.from({ length: Math.floor((last - first) / sizeMs) + 1 }, (_, index) => {
|
|
107
|
+
const start = first + index * sizeMs;
|
|
108
|
+
return {
|
|
109
|
+
bucketStartMs: start,
|
|
110
|
+
value: installIdsByBucket.get(start)?.size ?? 0
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
const collectEventActivity = (request) => {
|
|
115
|
+
const range = request.window === "all" ? void 0 : getWindowRange(request.window, request.cutoffMs);
|
|
116
|
+
const rows = range === void 0 ? request.rows : request.rows.filter(({ received_at_ms }) => received_at_ms >= range.rangeStart && received_at_ms < request.cutoffMs);
|
|
117
|
+
const installs = /* @__PURE__ */ new Set();
|
|
118
|
+
const installsByCohort = /* @__PURE__ */ new Map();
|
|
119
|
+
for (const row of rows) {
|
|
120
|
+
installs.add(row.install_id);
|
|
121
|
+
const cohort = installsByCohort.get(row.cohort) ?? /* @__PURE__ */ new Set();
|
|
122
|
+
cohort.add(row.install_id);
|
|
123
|
+
installsByCohort.set(row.cohort, cohort);
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
summary: installs.size,
|
|
127
|
+
cohorts: [...installsByCohort].sort(([left], [right]) => compareCodePoints(left, right)).map(([cohort, installIds]) => ({
|
|
128
|
+
cohort,
|
|
129
|
+
value: installIds.size
|
|
130
|
+
})),
|
|
131
|
+
series: createSeries({
|
|
132
|
+
...request,
|
|
133
|
+
rows
|
|
134
|
+
})
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
//#endregion
|
|
138
|
+
export { ANALYTICS_SCAN_MAX_ROWS, collectEventActivity, compareCodePoints, compareEventNewest, materializeActiveRows, materializeEventRows, materializeRowsForWindow };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
//#region src/analytics/domain.d.ts
|
|
2
|
+
type CreateBundleEventRequestBase = {
|
|
3
|
+
readonly installId: string;
|
|
4
|
+
readonly toBundleId: string;
|
|
5
|
+
readonly userId?: string;
|
|
6
|
+
readonly username?: string;
|
|
7
|
+
readonly platform: "ios" | "android";
|
|
8
|
+
readonly appVersion: string;
|
|
9
|
+
readonly channel: string;
|
|
10
|
+
readonly cohort: string;
|
|
11
|
+
readonly fingerprintHash: string | null;
|
|
12
|
+
readonly sdkVersion?: string | null;
|
|
13
|
+
readonly fromReleaseId: string | null;
|
|
14
|
+
readonly toReleaseId: string | null;
|
|
15
|
+
};
|
|
16
|
+
type CreateBundleEventRequest = (CreateBundleEventRequestBase & {
|
|
17
|
+
readonly type: "UPDATE_APPLIED";
|
|
18
|
+
readonly fromBundleId: string;
|
|
19
|
+
readonly updateStrategy: "fingerprint" | "appVersion";
|
|
20
|
+
}) | (CreateBundleEventRequestBase & {
|
|
21
|
+
readonly type: "RECOVERED";
|
|
22
|
+
readonly fromBundleId: string;
|
|
23
|
+
readonly updateStrategy: "fingerprint" | "appVersion";
|
|
24
|
+
}) | (CreateBundleEventRequestBase & {
|
|
25
|
+
readonly type: "RELEASE_ADOPTED";
|
|
26
|
+
readonly fromBundleId: string;
|
|
27
|
+
readonly updateStrategy: "fingerprint" | "appVersion";
|
|
28
|
+
}) | (CreateBundleEventRequestBase & {
|
|
29
|
+
readonly type: "UNCHANGED";
|
|
30
|
+
readonly fromBundleId: null;
|
|
31
|
+
readonly updateStrategy: null;
|
|
32
|
+
});
|
|
33
|
+
type BundleEventSummary = {
|
|
34
|
+
readonly installed: number;
|
|
35
|
+
readonly recovered: number;
|
|
36
|
+
};
|
|
37
|
+
type BundleEventSummaryByBundle = BundleEventSummary & {
|
|
38
|
+
readonly bundleId: string;
|
|
39
|
+
};
|
|
40
|
+
type BundleEventAnalyticsWindow = "24h" | "7d" | "30d" | "all";
|
|
41
|
+
type ActiveInstallationWindow = "24h" | "7d" | "30d";
|
|
42
|
+
type InstallationSearchRow = {
|
|
43
|
+
readonly installId: string;
|
|
44
|
+
readonly username: string | null;
|
|
45
|
+
readonly userId: string | null;
|
|
46
|
+
readonly lastKnownBundleId: string;
|
|
47
|
+
readonly latestStatus: "UPDATE_APPLIED" | "RECOVERED" | "RELEASE_ADOPTED" | "UNCHANGED";
|
|
48
|
+
readonly platform: "ios" | "android";
|
|
49
|
+
readonly appVersion: string;
|
|
50
|
+
readonly channel: string;
|
|
51
|
+
readonly cohort: string;
|
|
52
|
+
readonly receivedAtMs: number;
|
|
53
|
+
};
|
|
54
|
+
type InstallationHistoryRow = {
|
|
55
|
+
readonly id: string;
|
|
56
|
+
readonly type: "UPDATE_APPLIED" | "RECOVERED";
|
|
57
|
+
readonly fromBundleId: string;
|
|
58
|
+
readonly toBundleId: string;
|
|
59
|
+
readonly username: string | null;
|
|
60
|
+
readonly userId: string | null;
|
|
61
|
+
readonly platform: "ios" | "android";
|
|
62
|
+
readonly appVersion: string;
|
|
63
|
+
readonly channel: string;
|
|
64
|
+
readonly cohort: string;
|
|
65
|
+
readonly receivedAtMs: number;
|
|
66
|
+
};
|
|
67
|
+
type OffsetPaginationResult<TData> = {
|
|
68
|
+
readonly data: readonly TData[];
|
|
69
|
+
readonly pagination: {
|
|
70
|
+
readonly total: number;
|
|
71
|
+
readonly limit: number;
|
|
72
|
+
readonly offset: number;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
type AnalyticsSeriesPoint = {
|
|
76
|
+
readonly bucketStartMs: number;
|
|
77
|
+
readonly value: number;
|
|
78
|
+
};
|
|
79
|
+
type AnalyticsCohortPoint = {
|
|
80
|
+
readonly cohort: string;
|
|
81
|
+
readonly value: number;
|
|
82
|
+
};
|
|
83
|
+
type BundleEventAnalyticsResult = {
|
|
84
|
+
readonly summary: BundleEventSummary;
|
|
85
|
+
readonly series: {
|
|
86
|
+
readonly installed: readonly AnalyticsSeriesPoint[];
|
|
87
|
+
readonly recovered: readonly AnalyticsSeriesPoint[];
|
|
88
|
+
};
|
|
89
|
+
readonly cohorts: {
|
|
90
|
+
readonly installed: readonly AnalyticsCohortPoint[];
|
|
91
|
+
readonly recovered: readonly AnalyticsCohortPoint[];
|
|
92
|
+
};
|
|
93
|
+
readonly recentEvents: OffsetPaginationResult<InstallationHistoryRow>;
|
|
94
|
+
};
|
|
95
|
+
type BundleEventOverview = {
|
|
96
|
+
readonly trackedInstallations: number;
|
|
97
|
+
readonly bundles: readonly {
|
|
98
|
+
readonly bundleId: string;
|
|
99
|
+
readonly installations: number;
|
|
100
|
+
}[];
|
|
101
|
+
};
|
|
102
|
+
type ActiveInstallationOverview = {
|
|
103
|
+
readonly asOfMs: number;
|
|
104
|
+
readonly window: ActiveInstallationWindow;
|
|
105
|
+
readonly activeInstallations: number;
|
|
106
|
+
readonly series: readonly AnalyticsSeriesPoint[];
|
|
107
|
+
readonly bundleSeries: readonly {
|
|
108
|
+
readonly bundleId: string;
|
|
109
|
+
readonly series: readonly AnalyticsSeriesPoint[];
|
|
110
|
+
}[];
|
|
111
|
+
readonly bundles: readonly {
|
|
112
|
+
readonly bundleId: string;
|
|
113
|
+
readonly installations: number;
|
|
114
|
+
}[];
|
|
115
|
+
};
|
|
116
|
+
type ActiveInstallationInput = {
|
|
117
|
+
readonly window: ActiveInstallationWindow;
|
|
118
|
+
readonly userId?: string;
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
export { ActiveInstallationInput, ActiveInstallationOverview, ActiveInstallationWindow, AnalyticsCohortPoint, AnalyticsSeriesPoint, BundleEventAnalyticsResult, BundleEventAnalyticsWindow, BundleEventOverview, BundleEventSummary, BundleEventSummaryByBundle, CreateBundleEventRequest, CreateBundleEventRequestBase, InstallationHistoryRow, InstallationSearchRow, OffsetPaginationResult };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
//#region src/analytics/domain.d.ts
|
|
2
|
+
type CreateBundleEventRequestBase = {
|
|
3
|
+
readonly installId: string;
|
|
4
|
+
readonly toBundleId: string;
|
|
5
|
+
readonly userId?: string;
|
|
6
|
+
readonly username?: string;
|
|
7
|
+
readonly platform: "ios" | "android";
|
|
8
|
+
readonly appVersion: string;
|
|
9
|
+
readonly channel: string;
|
|
10
|
+
readonly cohort: string;
|
|
11
|
+
readonly fingerprintHash: string | null;
|
|
12
|
+
readonly sdkVersion?: string | null;
|
|
13
|
+
readonly fromReleaseId: string | null;
|
|
14
|
+
readonly toReleaseId: string | null;
|
|
15
|
+
};
|
|
16
|
+
type CreateBundleEventRequest = (CreateBundleEventRequestBase & {
|
|
17
|
+
readonly type: "UPDATE_APPLIED";
|
|
18
|
+
readonly fromBundleId: string;
|
|
19
|
+
readonly updateStrategy: "fingerprint" | "appVersion";
|
|
20
|
+
}) | (CreateBundleEventRequestBase & {
|
|
21
|
+
readonly type: "RECOVERED";
|
|
22
|
+
readonly fromBundleId: string;
|
|
23
|
+
readonly updateStrategy: "fingerprint" | "appVersion";
|
|
24
|
+
}) | (CreateBundleEventRequestBase & {
|
|
25
|
+
readonly type: "RELEASE_ADOPTED";
|
|
26
|
+
readonly fromBundleId: string;
|
|
27
|
+
readonly updateStrategy: "fingerprint" | "appVersion";
|
|
28
|
+
}) | (CreateBundleEventRequestBase & {
|
|
29
|
+
readonly type: "UNCHANGED";
|
|
30
|
+
readonly fromBundleId: null;
|
|
31
|
+
readonly updateStrategy: null;
|
|
32
|
+
});
|
|
33
|
+
type BundleEventSummary = {
|
|
34
|
+
readonly installed: number;
|
|
35
|
+
readonly recovered: number;
|
|
36
|
+
};
|
|
37
|
+
type BundleEventSummaryByBundle = BundleEventSummary & {
|
|
38
|
+
readonly bundleId: string;
|
|
39
|
+
};
|
|
40
|
+
type BundleEventAnalyticsWindow = "24h" | "7d" | "30d" | "all";
|
|
41
|
+
type ActiveInstallationWindow = "24h" | "7d" | "30d";
|
|
42
|
+
type InstallationSearchRow = {
|
|
43
|
+
readonly installId: string;
|
|
44
|
+
readonly username: string | null;
|
|
45
|
+
readonly userId: string | null;
|
|
46
|
+
readonly lastKnownBundleId: string;
|
|
47
|
+
readonly latestStatus: "UPDATE_APPLIED" | "RECOVERED" | "RELEASE_ADOPTED" | "UNCHANGED";
|
|
48
|
+
readonly platform: "ios" | "android";
|
|
49
|
+
readonly appVersion: string;
|
|
50
|
+
readonly channel: string;
|
|
51
|
+
readonly cohort: string;
|
|
52
|
+
readonly receivedAtMs: number;
|
|
53
|
+
};
|
|
54
|
+
type InstallationHistoryRow = {
|
|
55
|
+
readonly id: string;
|
|
56
|
+
readonly type: "UPDATE_APPLIED" | "RECOVERED";
|
|
57
|
+
readonly fromBundleId: string;
|
|
58
|
+
readonly toBundleId: string;
|
|
59
|
+
readonly username: string | null;
|
|
60
|
+
readonly userId: string | null;
|
|
61
|
+
readonly platform: "ios" | "android";
|
|
62
|
+
readonly appVersion: string;
|
|
63
|
+
readonly channel: string;
|
|
64
|
+
readonly cohort: string;
|
|
65
|
+
readonly receivedAtMs: number;
|
|
66
|
+
};
|
|
67
|
+
type OffsetPaginationResult<TData> = {
|
|
68
|
+
readonly data: readonly TData[];
|
|
69
|
+
readonly pagination: {
|
|
70
|
+
readonly total: number;
|
|
71
|
+
readonly limit: number;
|
|
72
|
+
readonly offset: number;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
type AnalyticsSeriesPoint = {
|
|
76
|
+
readonly bucketStartMs: number;
|
|
77
|
+
readonly value: number;
|
|
78
|
+
};
|
|
79
|
+
type AnalyticsCohortPoint = {
|
|
80
|
+
readonly cohort: string;
|
|
81
|
+
readonly value: number;
|
|
82
|
+
};
|
|
83
|
+
type BundleEventAnalyticsResult = {
|
|
84
|
+
readonly summary: BundleEventSummary;
|
|
85
|
+
readonly series: {
|
|
86
|
+
readonly installed: readonly AnalyticsSeriesPoint[];
|
|
87
|
+
readonly recovered: readonly AnalyticsSeriesPoint[];
|
|
88
|
+
};
|
|
89
|
+
readonly cohorts: {
|
|
90
|
+
readonly installed: readonly AnalyticsCohortPoint[];
|
|
91
|
+
readonly recovered: readonly AnalyticsCohortPoint[];
|
|
92
|
+
};
|
|
93
|
+
readonly recentEvents: OffsetPaginationResult<InstallationHistoryRow>;
|
|
94
|
+
};
|
|
95
|
+
type BundleEventOverview = {
|
|
96
|
+
readonly trackedInstallations: number;
|
|
97
|
+
readonly bundles: readonly {
|
|
98
|
+
readonly bundleId: string;
|
|
99
|
+
readonly installations: number;
|
|
100
|
+
}[];
|
|
101
|
+
};
|
|
102
|
+
type ActiveInstallationOverview = {
|
|
103
|
+
readonly asOfMs: number;
|
|
104
|
+
readonly window: ActiveInstallationWindow;
|
|
105
|
+
readonly activeInstallations: number;
|
|
106
|
+
readonly series: readonly AnalyticsSeriesPoint[];
|
|
107
|
+
readonly bundleSeries: readonly {
|
|
108
|
+
readonly bundleId: string;
|
|
109
|
+
readonly series: readonly AnalyticsSeriesPoint[];
|
|
110
|
+
}[];
|
|
111
|
+
readonly bundles: readonly {
|
|
112
|
+
readonly bundleId: string;
|
|
113
|
+
readonly installations: number;
|
|
114
|
+
}[];
|
|
115
|
+
};
|
|
116
|
+
type ActiveInstallationInput = {
|
|
117
|
+
readonly window: ActiveInstallationWindow;
|
|
118
|
+
readonly userId?: string;
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
export { ActiveInstallationInput, ActiveInstallationOverview, ActiveInstallationWindow, AnalyticsCohortPoint, AnalyticsSeriesPoint, BundleEventAnalyticsResult, BundleEventAnalyticsWindow, BundleEventOverview, BundleEventSummary, BundleEventSummaryByBundle, CreateBundleEventRequest, CreateBundleEventRequestBase, InstallationHistoryRow, InstallationSearchRow, OffsetPaginationResult };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/analytics/errors.ts
|
|
2
|
+
var AnalyticsScanLimitExceededError = class extends Error {
|
|
3
|
+
constructor(limit) {
|
|
4
|
+
super(`Analytics event scan exceeded ${limit} rows.`);
|
|
5
|
+
this.limit = limit;
|
|
6
|
+
this.name = "AnalyticsScanLimitExceededError";
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
var AnalyticsBadRequestError = class extends Error {
|
|
10
|
+
name = "AnalyticsBadRequestError";
|
|
11
|
+
};
|
|
12
|
+
var AnalyticsPayloadTooLargeError = class extends Error {
|
|
13
|
+
name = "AnalyticsPayloadTooLargeError";
|
|
14
|
+
constructor(maximumBytes) {
|
|
15
|
+
super(`Event payload exceeds ${maximumBytes} bytes`);
|
|
16
|
+
this.maximumBytes = maximumBytes;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
exports.AnalyticsBadRequestError = AnalyticsBadRequestError;
|
|
21
|
+
exports.AnalyticsPayloadTooLargeError = AnalyticsPayloadTooLargeError;
|
|
22
|
+
exports.AnalyticsScanLimitExceededError = AnalyticsScanLimitExceededError;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/analytics/errors.ts
|
|
2
|
+
var AnalyticsScanLimitExceededError = class extends Error {
|
|
3
|
+
constructor(limit) {
|
|
4
|
+
super(`Analytics event scan exceeded ${limit} rows.`);
|
|
5
|
+
this.limit = limit;
|
|
6
|
+
this.name = "AnalyticsScanLimitExceededError";
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
var AnalyticsBadRequestError = class extends Error {
|
|
10
|
+
name = "AnalyticsBadRequestError";
|
|
11
|
+
};
|
|
12
|
+
var AnalyticsPayloadTooLargeError = class extends Error {
|
|
13
|
+
name = "AnalyticsPayloadTooLargeError";
|
|
14
|
+
constructor(maximumBytes) {
|
|
15
|
+
super(`Event payload exceeds ${maximumBytes} bytes`);
|
|
16
|
+
this.maximumBytes = maximumBytes;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { AnalyticsBadRequestError, AnalyticsPayloadTooLargeError, AnalyticsScanLimitExceededError };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const require_errors = require("./errors.cjs");
|
|
2
|
+
//#region src/analytics/eventInput.ts
|
|
3
|
+
const MAX_EVENT_STRING_LENGTH = 1024;
|
|
4
|
+
const EVENT_BODY_MAX_BYTES = 16 * 1024;
|
|
5
|
+
const eventKeys = new Set([
|
|
6
|
+
"type",
|
|
7
|
+
"installId",
|
|
8
|
+
"toBundleId",
|
|
9
|
+
"userId",
|
|
10
|
+
"username",
|
|
11
|
+
"platform",
|
|
12
|
+
"appVersion",
|
|
13
|
+
"channel",
|
|
14
|
+
"cohort",
|
|
15
|
+
"fingerprintHash",
|
|
16
|
+
"fromBundleId",
|
|
17
|
+
"fromReleaseId",
|
|
18
|
+
"toReleaseId",
|
|
19
|
+
"updateStrategy",
|
|
20
|
+
"sdkVersion"
|
|
21
|
+
]);
|
|
22
|
+
function isRecord(value) {
|
|
23
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
function requireStringField(payload, key) {
|
|
26
|
+
const value = payload[key];
|
|
27
|
+
if (typeof value !== "string" || value.length === 0 || value.length > MAX_EVENT_STRING_LENGTH) throw new require_errors.AnalyticsBadRequestError(`Invalid event field: ${key}`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function requireNullableStringField(payload, key) {
|
|
31
|
+
if (payload[key] === null) return null;
|
|
32
|
+
return requireStringField(payload, key);
|
|
33
|
+
}
|
|
34
|
+
async function readBoundedText(request) {
|
|
35
|
+
const contentLength = request.headers.get("content-length");
|
|
36
|
+
const declaredByteLength = Number(contentLength);
|
|
37
|
+
if (contentLength !== null && Number.isSafeInteger(declaredByteLength) && declaredByteLength > 16384) throw new require_errors.AnalyticsPayloadTooLargeError(EVENT_BODY_MAX_BYTES);
|
|
38
|
+
if (request.body === null) return "";
|
|
39
|
+
const reader = request.body.getReader();
|
|
40
|
+
const decoder = new TextDecoder();
|
|
41
|
+
let byteLength = 0;
|
|
42
|
+
let text = "";
|
|
43
|
+
while (true) {
|
|
44
|
+
const result = await reader.read();
|
|
45
|
+
if (result.done) break;
|
|
46
|
+
byteLength += result.value.byteLength;
|
|
47
|
+
if (byteLength > 16384) {
|
|
48
|
+
await reader.cancel();
|
|
49
|
+
throw new require_errors.AnalyticsPayloadTooLargeError(EVENT_BODY_MAX_BYTES);
|
|
50
|
+
}
|
|
51
|
+
text += decoder.decode(result.value, { stream: true });
|
|
52
|
+
}
|
|
53
|
+
return text + decoder.decode();
|
|
54
|
+
}
|
|
55
|
+
async function parseJson(request) {
|
|
56
|
+
const text = await readBoundedText(request);
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(text);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof SyntaxError) throw new require_errors.AnalyticsBadRequestError("Invalid event payload");
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function requireEvent(payload) {
|
|
65
|
+
if (!isRecord(payload) || Object.keys(payload).some((key) => !eventKeys.has(key))) throw new require_errors.AnalyticsBadRequestError("Invalid event payload");
|
|
66
|
+
const platform = requireStringField(payload, "platform");
|
|
67
|
+
if (platform !== "ios" && platform !== "android") throw new require_errors.AnalyticsBadRequestError("Invalid event field: platform");
|
|
68
|
+
const base = {
|
|
69
|
+
installId: requireStringField(payload, "installId"),
|
|
70
|
+
toBundleId: requireStringField(payload, "toBundleId"),
|
|
71
|
+
...payload.userId === void 0 ? {} : { userId: requireStringField(payload, "userId") },
|
|
72
|
+
...payload.username === void 0 ? {} : { username: requireStringField(payload, "username") },
|
|
73
|
+
platform,
|
|
74
|
+
appVersion: requireStringField(payload, "appVersion"),
|
|
75
|
+
channel: requireStringField(payload, "channel"),
|
|
76
|
+
cohort: requireStringField(payload, "cohort"),
|
|
77
|
+
fingerprintHash: requireNullableStringField(payload, "fingerprintHash"),
|
|
78
|
+
sdkVersion: payload.sdkVersion === void 0 ? null : requireNullableStringField(payload, "sdkVersion"),
|
|
79
|
+
fromReleaseId: requireNullableStringField(payload, "fromReleaseId"),
|
|
80
|
+
toReleaseId: requireNullableStringField(payload, "toReleaseId")
|
|
81
|
+
};
|
|
82
|
+
const type = requireStringField(payload, "type");
|
|
83
|
+
switch (type) {
|
|
84
|
+
case "UPDATE_APPLIED":
|
|
85
|
+
case "RECOVERED":
|
|
86
|
+
case "RELEASE_ADOPTED": {
|
|
87
|
+
const updateStrategy = requireStringField(payload, "updateStrategy");
|
|
88
|
+
if (updateStrategy !== "fingerprint" && updateStrategy !== "appVersion") throw new require_errors.AnalyticsBadRequestError("Invalid event field: updateStrategy");
|
|
89
|
+
return {
|
|
90
|
+
...base,
|
|
91
|
+
type,
|
|
92
|
+
fromBundleId: requireStringField(payload, "fromBundleId"),
|
|
93
|
+
updateStrategy
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
case "UNCHANGED":
|
|
97
|
+
if (payload.fromBundleId !== null || payload.updateStrategy !== null) throw new require_errors.AnalyticsBadRequestError("Invalid unchanged event shape");
|
|
98
|
+
return {
|
|
99
|
+
...base,
|
|
100
|
+
type,
|
|
101
|
+
fromBundleId: null,
|
|
102
|
+
updateStrategy: null
|
|
103
|
+
};
|
|
104
|
+
default: throw new require_errors.AnalyticsBadRequestError("Invalid event field: type");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function parseBundleEventRequest(request) {
|
|
108
|
+
return requireEvent(await parseJson(request));
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
exports.parseBundleEventRequest = parseBundleEventRequest;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { AnalyticsBadRequestError, AnalyticsPayloadTooLargeError } from "./errors.mjs";
|
|
2
|
+
//#region src/analytics/eventInput.ts
|
|
3
|
+
const MAX_EVENT_STRING_LENGTH = 1024;
|
|
4
|
+
const EVENT_BODY_MAX_BYTES = 16 * 1024;
|
|
5
|
+
const eventKeys = new Set([
|
|
6
|
+
"type",
|
|
7
|
+
"installId",
|
|
8
|
+
"toBundleId",
|
|
9
|
+
"userId",
|
|
10
|
+
"username",
|
|
11
|
+
"platform",
|
|
12
|
+
"appVersion",
|
|
13
|
+
"channel",
|
|
14
|
+
"cohort",
|
|
15
|
+
"fingerprintHash",
|
|
16
|
+
"fromBundleId",
|
|
17
|
+
"fromReleaseId",
|
|
18
|
+
"toReleaseId",
|
|
19
|
+
"updateStrategy",
|
|
20
|
+
"sdkVersion"
|
|
21
|
+
]);
|
|
22
|
+
function isRecord(value) {
|
|
23
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
function requireStringField(payload, key) {
|
|
26
|
+
const value = payload[key];
|
|
27
|
+
if (typeof value !== "string" || value.length === 0 || value.length > MAX_EVENT_STRING_LENGTH) throw new AnalyticsBadRequestError(`Invalid event field: ${key}`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function requireNullableStringField(payload, key) {
|
|
31
|
+
if (payload[key] === null) return null;
|
|
32
|
+
return requireStringField(payload, key);
|
|
33
|
+
}
|
|
34
|
+
async function readBoundedText(request) {
|
|
35
|
+
const contentLength = request.headers.get("content-length");
|
|
36
|
+
const declaredByteLength = Number(contentLength);
|
|
37
|
+
if (contentLength !== null && Number.isSafeInteger(declaredByteLength) && declaredByteLength > 16384) throw new AnalyticsPayloadTooLargeError(EVENT_BODY_MAX_BYTES);
|
|
38
|
+
if (request.body === null) return "";
|
|
39
|
+
const reader = request.body.getReader();
|
|
40
|
+
const decoder = new TextDecoder();
|
|
41
|
+
let byteLength = 0;
|
|
42
|
+
let text = "";
|
|
43
|
+
while (true) {
|
|
44
|
+
const result = await reader.read();
|
|
45
|
+
if (result.done) break;
|
|
46
|
+
byteLength += result.value.byteLength;
|
|
47
|
+
if (byteLength > 16384) {
|
|
48
|
+
await reader.cancel();
|
|
49
|
+
throw new AnalyticsPayloadTooLargeError(EVENT_BODY_MAX_BYTES);
|
|
50
|
+
}
|
|
51
|
+
text += decoder.decode(result.value, { stream: true });
|
|
52
|
+
}
|
|
53
|
+
return text + decoder.decode();
|
|
54
|
+
}
|
|
55
|
+
async function parseJson(request) {
|
|
56
|
+
const text = await readBoundedText(request);
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(text);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof SyntaxError) throw new AnalyticsBadRequestError("Invalid event payload");
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function requireEvent(payload) {
|
|
65
|
+
if (!isRecord(payload) || Object.keys(payload).some((key) => !eventKeys.has(key))) throw new AnalyticsBadRequestError("Invalid event payload");
|
|
66
|
+
const platform = requireStringField(payload, "platform");
|
|
67
|
+
if (platform !== "ios" && platform !== "android") throw new AnalyticsBadRequestError("Invalid event field: platform");
|
|
68
|
+
const base = {
|
|
69
|
+
installId: requireStringField(payload, "installId"),
|
|
70
|
+
toBundleId: requireStringField(payload, "toBundleId"),
|
|
71
|
+
...payload.userId === void 0 ? {} : { userId: requireStringField(payload, "userId") },
|
|
72
|
+
...payload.username === void 0 ? {} : { username: requireStringField(payload, "username") },
|
|
73
|
+
platform,
|
|
74
|
+
appVersion: requireStringField(payload, "appVersion"),
|
|
75
|
+
channel: requireStringField(payload, "channel"),
|
|
76
|
+
cohort: requireStringField(payload, "cohort"),
|
|
77
|
+
fingerprintHash: requireNullableStringField(payload, "fingerprintHash"),
|
|
78
|
+
sdkVersion: payload.sdkVersion === void 0 ? null : requireNullableStringField(payload, "sdkVersion"),
|
|
79
|
+
fromReleaseId: requireNullableStringField(payload, "fromReleaseId"),
|
|
80
|
+
toReleaseId: requireNullableStringField(payload, "toReleaseId")
|
|
81
|
+
};
|
|
82
|
+
const type = requireStringField(payload, "type");
|
|
83
|
+
switch (type) {
|
|
84
|
+
case "UPDATE_APPLIED":
|
|
85
|
+
case "RECOVERED":
|
|
86
|
+
case "RELEASE_ADOPTED": {
|
|
87
|
+
const updateStrategy = requireStringField(payload, "updateStrategy");
|
|
88
|
+
if (updateStrategy !== "fingerprint" && updateStrategy !== "appVersion") throw new AnalyticsBadRequestError("Invalid event field: updateStrategy");
|
|
89
|
+
return {
|
|
90
|
+
...base,
|
|
91
|
+
type,
|
|
92
|
+
fromBundleId: requireStringField(payload, "fromBundleId"),
|
|
93
|
+
updateStrategy
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
case "UNCHANGED":
|
|
97
|
+
if (payload.fromBundleId !== null || payload.updateStrategy !== null) throw new AnalyticsBadRequestError("Invalid unchanged event shape");
|
|
98
|
+
return {
|
|
99
|
+
...base,
|
|
100
|
+
type,
|
|
101
|
+
fromBundleId: null,
|
|
102
|
+
updateStrategy: null
|
|
103
|
+
};
|
|
104
|
+
default: throw new AnalyticsBadRequestError("Invalid event field: type");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function parseBundleEventRequest(request) {
|
|
108
|
+
return requireEvent(await parseJson(request));
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
export { parseBundleEventRequest };
|