@hot-updater/plugin-core 1.0.0-rc.16 → 1.0.0-rc.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/database/adapter.cjs +9 -0
  2. package/dist/database/adapter.d.cts +34 -1
  3. package/dist/database/adapter.d.mts +34 -1
  4. package/dist/database/adapter.mjs +9 -1
  5. package/dist/{insightsDistinctSummary.mjs → database/distinct.cjs} +23 -10
  6. package/dist/database/distinct.d.cts +16 -0
  7. package/dist/database/distinct.d.mts +16 -0
  8. package/dist/{insightsDistinctSummary.cjs → database/distinct.mjs} +19 -14
  9. package/dist/database/memoryAdapter.cjs +13 -0
  10. package/dist/database/memoryAdapter.mjs +13 -0
  11. package/dist/database/verifyAdapter.cjs +7 -0
  12. package/dist/database/verifyAdapter.mjs +7 -0
  13. package/dist/databaseJsonValue.cjs +0 -2
  14. package/dist/databaseJsonValue.d.cts +3 -3
  15. package/dist/databaseJsonValue.d.mts +3 -3
  16. package/dist/databaseJsonValue.mjs +1 -2
  17. package/dist/index.cjs +0 -4
  18. package/dist/index.d.cts +5 -7
  19. package/dist/index.d.mts +5 -7
  20. package/dist/index.mjs +2 -3
  21. package/dist/internal.cjs +9 -16
  22. package/dist/internal.d.cts +6 -8
  23. package/dist/internal.d.mts +6 -8
  24. package/dist/internal.mjs +4 -5
  25. package/dist/types/databaseConfig.d.cts +29 -1
  26. package/dist/types/databaseConfig.d.mts +29 -1
  27. package/dist/types/databaseRows.d.cts +1 -53
  28. package/dist/types/databaseRows.d.mts +1 -53
  29. package/dist/types/index.d.cts +4 -5
  30. package/dist/types/index.d.mts +4 -5
  31. package/dist/types/internal.d.cts +3 -4
  32. package/dist/types/internal.d.mts +3 -4
  33. package/dist/types/models.d.cts +2 -12
  34. package/dist/types/models.d.mts +2 -12
  35. package/dist/types/public.d.cts +4 -5
  36. package/dist/types/public.d.mts +4 -5
  37. package/package.json +1 -1
  38. package/dist/bundleEventRow.cjs +0 -42
  39. package/dist/bundleEventRow.mjs +0 -41
  40. package/dist/insightsContract.cjs +0 -194
  41. package/dist/insightsContract.d.cts +0 -14
  42. package/dist/insightsContract.d.mts +0 -14
  43. package/dist/insightsContract.mjs +0 -190
  44. package/dist/insightsDistinctSummary.d.cts +0 -12
  45. package/dist/insightsDistinctSummary.d.mts +0 -12
  46. package/dist/insightsOverview.cjs +0 -139
  47. package/dist/insightsOverview.d.cts +0 -42
  48. package/dist/insightsOverview.d.mts +0 -42
  49. package/dist/insightsOverview.mjs +0 -134
  50. package/dist/types/insights.d.cts +0 -172
  51. package/dist/types/insights.d.mts +0 -172
@@ -1,41 +0,0 @@
1
- import { DatabasePluginInputError } from "./databaseErrors.mjs";
2
- import { isDatabaseBundleEventMetadata } from "./databaseJsonValue.mjs";
3
- //#region src/bundleEventRow.ts
4
- const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
5
- const isIdentity = (value) => typeof value === "string" && value.length > 0 && value.length <= 255;
6
- const isTextOrNull = (value) => value === null || typeof value === "string";
7
- /** Each bundle event field and what it holds. */
8
- const BUNDLE_EVENT_FIELDS = {
9
- id: (value) => typeof value === "string",
10
- type: (value) => value === "UPDATE_DOWNLOADED" || value === "UPDATE_APPLIED" || value === "RECOVERED" || value === "UNCHANGED",
11
- install_id: isIdentity,
12
- user_id: (value) => value === null || isIdentity(value),
13
- from_bundle_id: isTextOrNull,
14
- from_release_id: isTextOrNull,
15
- to_release_id: isTextOrNull,
16
- to_bundle_id: (value) => typeof value === "string",
17
- platform: (value) => value === "ios" || value === "android",
18
- app_version: (value) => typeof value === "string",
19
- channel: (value) => typeof value === "string",
20
- metadata: isDatabaseBundleEventMetadata,
21
- received_at_ms: (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0
22
- };
23
- const updateStrategyOf = (row) => isRecord(row.metadata) ? row.metadata.update_strategy : void 0;
24
- /**
25
- * A movement names the bundle it left and how the device updates; an
26
- * unchanged report names neither.
27
- */
28
- const hasEventInvariants = (row) => (row.type === "UPDATE_DOWNLOADED" || row.type === "UPDATE_APPLIED" || row.type === "RECOVERED") && typeof row.from_bundle_id === "string" && (updateStrategyOf(row) === "fingerprint" || updateStrategyOf(row) === "appVersion") || row.type === "UNCHANGED" && row.from_bundle_id === null && updateStrategyOf(row) === null;
29
- /**
30
- * Throws unless `row` has exactly the bundle event fields, each of its type,
31
- * and the invariants of its event type: `invalid-field` for an unknown field,
32
- * `invalid-data` for anything else.
33
- */
34
- const validateBundleEventFields = (row) => {
35
- if (!isRecord(row)) throw new DatabasePluginInputError("invalid-data");
36
- for (const field of Object.keys(row)) if (!Object.hasOwn(BUNDLE_EVENT_FIELDS, field)) throw new DatabasePluginInputError("invalid-field");
37
- for (const [field, valid] of Object.entries(BUNDLE_EVENT_FIELDS)) if (!Object.hasOwn(row, field) || !valid(row[field])) throw new DatabasePluginInputError("invalid-data");
38
- if (!hasEventInvariants(row)) throw new DatabasePluginInputError("invalid-data");
39
- };
40
- //#endregion
41
- export { isRecord, validateBundleEventFields };
@@ -1,194 +0,0 @@
1
- const require_databaseErrors = require("./databaseErrors.cjs");
2
- const require_bundleEventRow = require("./bundleEventRow.cjs");
3
- let _hot_updater_core = require("@hot-updater/core");
4
- //#region src/insightsContract.ts
5
- const encoder = new TextEncoder();
6
- /** Exact UTF-8 byte ordering, without case folding or Unicode normalization. */
7
- const compareInsightsText = (left, right) => {
8
- const a = encoder.encode(left);
9
- const b = encoder.encode(right);
10
- for (let index = 0; index < Math.min(a.length, b.length); index += 1) if (a[index] !== b[index]) return a[index] - b[index];
11
- return a.length - b.length;
12
- };
13
- const isWellFormedText = (value) => {
14
- for (const character of value) {
15
- const point = character.codePointAt(0);
16
- if (point >= 55296 && point <= 57343) return false;
17
- }
18
- return true;
19
- };
20
- const isIdentity = (value) => typeof value === "string" && value.length > 0 && value.length <= 255 && isWellFormedText(value);
21
- const isText = (value) => typeof value === "string" && value.length > 0 && isWellFormedText(value);
22
- const isTimestamp = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
23
- const isLimit = (value) => isTimestamp(value) && value >= 1 && value <= 101;
24
- const hasOnlyKeys = (value, keys) => Object.keys(value).every((key) => keys.includes(key));
25
- const hasScope = (value) => (value.platform === "ios" || value.platform === "android") && isText(value.channel);
26
- const isBundleFilter = (value, withKind = false) => {
27
- if (!require_bundleEventRow.isRecord(value) || !hasScope(value)) return false;
28
- const keys = [
29
- "platform",
30
- "channel",
31
- "type",
32
- ...withKind ? ["kind"] : []
33
- ];
34
- return value.type === "RECOVERED" ? isText(value.fromBundleId) && hasOnlyKeys(value, [...keys, "fromBundleId"]) : (value.type === "UPDATE_DOWNLOADED" || value.type === "UPDATE_APPLIED" || value.type === "UNCHANGED") && isText(value.toBundleId) && hasOnlyKeys(value, [...keys, "toBundleId"]);
35
- };
36
- const isEventFilter = (value) => {
37
- if (!require_bundleEventRow.isRecord(value)) return false;
38
- if (value.kind === "all") return hasOnlyKeys(value, ["kind"]);
39
- if (value.kind === "installationMovement") return isIdentity(value.installId) && hasOnlyKeys(value, ["kind", "installId"]);
40
- return value.kind === "bundle" && isBundleFilter(value, true);
41
- };
42
- const validateRow = (model, row, result = false) => {
43
- try {
44
- require_bundleEventRow.validateBundleEventFields(row);
45
- if (!require_bundleEventRow.isRecord(row) || typeof row.id !== "string" || !(0, _hot_updater_core.isUUIDv7)(row.id) || [...Object.values(row), ...require_bundleEventRow.isRecord(row.metadata) ? Object.values(row.metadata) : []].some((value) => typeof value === "string" && !isWellFormedText(value))) throw new require_databaseErrors.DatabasePluginInputError("invalid-data");
46
- } catch (error) {
47
- if (result) throw new require_databaseErrors.DatabasePluginInputError("invalid-result");
48
- throw error;
49
- }
50
- };
51
- /** Throws `invalid-data` unless `row` is a complete, well-formed bundle event row. */
52
- const assertBundleEventRow = (row) => validateRow("bundle_events", row);
53
- /** Downloads and applied transitions belong in installation history. */
54
- const isInsightsMovementEvent = (event) => event.type === "UPDATE_DOWNLOADED" || event.type === "UPDATE_APPLIED" || event.type === "RECOVERED";
55
- const matchesInsightsEventFilter = (event, filter) => {
56
- if (filter.kind === "all") return true;
57
- if (filter.kind === "installationMovement") return event.install_id === filter.installId && isInsightsMovementEvent(event);
58
- return event.type === filter.type && event.platform === filter.platform && event.channel === filter.channel && (filter.type === "RECOVERED" ? event.from_bundle_id === filter.fromBundleId : event.to_bundle_id === filter.toBundleId);
59
- };
60
- const invalidQuery = () => {
61
- throw new require_databaseErrors.DatabasePluginInputError("invalid-query");
62
- };
63
- const invalidResult = () => {
64
- throw new require_databaseErrors.DatabasePluginInputError("invalid-result");
65
- };
66
- const validateCount = (count) => isTimestamp(count) ? count : invalidResult();
67
- const isTimeRange = (value) => require_bundleEventRow.isRecord(value) && hasOnlyKeys(value, ["start", "end"]) && isTimestamp(value.start) && isTimestamp(value.end) && value.start < value.end;
68
- const isReleaseReference = (value) => require_bundleEventRow.isRecord(value) && hasOnlyKeys(value, [
69
- "releaseId",
70
- "platform",
71
- "channel"
72
- ]) && isIdentity(value.releaseId) && hasScope(value);
73
- const validateAggregateResult = (value) => {
74
- const result = require_bundleEventRow.isRecord(value) ? value : invalidResult();
75
- if (!isTimestamp(result.measuredAtMs)) invalidResult();
76
- const coverage = result.coverage;
77
- if (!require_bundleEventRow.isRecord(coverage) || coverage.kind !== "complete" && coverage.kind !== "partial" || coverage.sinceMs !== null && !isTimestamp(coverage.sinceMs)) invalidResult();
78
- };
79
- /** Validate custom and bundled providers at the same public boundary. */
80
- const createValidatedInsightsModel = (model) => ({
81
- async recordEvent(input) {
82
- if (!require_bundleEventRow.isRecord(input) || !hasOnlyKeys(input, ["event"])) throw new require_databaseErrors.DatabasePluginInputError("invalid-data");
83
- validateRow("bundle_events", input.event);
84
- await model.recordEvent(input);
85
- },
86
- async listEvents(input) {
87
- if (!require_bundleEventRow.isRecord(input) || !hasOnlyKeys(input, [
88
- "filter",
89
- "sinceMs",
90
- "beforeReceivedAtMs",
91
- "after",
92
- "limit"
93
- ]) || !isEventFilter(input.filter) || !isLimit(input.limit) || !isTimestamp(input.beforeReceivedAtMs) || input.sinceMs !== void 0 && !isTimestamp(input.sinceMs)) invalidQuery();
94
- const sinceMs = input.sinceMs ?? 0;
95
- if (sinceMs > input.beforeReceivedAtMs || input.after !== void 0 && (!require_bundleEventRow.isRecord(input.after) || !hasOnlyKeys(input.after, ["receivedAtMs", "id"]) || !isTimestamp(input.after.receivedAtMs) || typeof input.after.id !== "string" || !(0, _hot_updater_core.isUUIDv7)(input.after.id) || input.after.receivedAtMs < sinceMs || input.after.receivedAtMs >= input.beforeReceivedAtMs)) invalidQuery();
96
- const rows = await model.listEvents(input);
97
- if (!Array.isArray(rows) || rows.length > input.limit) invalidResult();
98
- let previous = input.after;
99
- for (const row of rows) {
100
- validateRow("bundle_events", row, true);
101
- if (row.received_at_ms < sinceMs || row.received_at_ms >= input.beforeReceivedAtMs || !matchesInsightsEventFilter(row, input.filter) || previous !== void 0 && (row.received_at_ms > previous.receivedAtMs || row.received_at_ms === previous.receivedAtMs && row.id >= previous.id)) invalidResult();
102
- previous = {
103
- receivedAtMs: row.received_at_ms,
104
- id: row.id
105
- };
106
- }
107
- return rows;
108
- },
109
- async findLatestEvents(input) {
110
- if (!require_bundleEventRow.isRecord(input)) invalidQuery();
111
- if ("installId" in input) {
112
- if (!hasOnlyKeys(input, ["installId"]) || !isIdentity(input.installId)) invalidQuery();
113
- } else if (!hasOnlyKeys(input, [
114
- "userId",
115
- "afterInstallId",
116
- "limit"
117
- ]) || !isIdentity(input.userId) || !isLimit(input.limit) || input.afterInstallId !== void 0 && !isIdentity(input.afterInstallId)) invalidQuery();
118
- const rows = await model.findLatestEvents(input);
119
- if (!Array.isArray(rows) || rows.length > ("installId" in input ? 1 : input.limit)) invalidResult();
120
- let previous = "installId" in input ? void 0 : input.afterInstallId;
121
- for (const row of rows) {
122
- validateRow("bundle_events", row, true);
123
- if ("installId" in input ? row.install_id !== input.installId : row.user_id !== input.userId || previous !== void 0 && compareInsightsText(row.install_id, previous) <= 0) invalidResult();
124
- previous = row.install_id;
125
- }
126
- return rows;
127
- },
128
- async countLatestEvents(input) {
129
- if (!require_bundleEventRow.isRecord(input) || !hasOnlyKeys(input, [
130
- "platform",
131
- "channel",
132
- "sinceMs",
133
- "bundle"
134
- ]) || !hasScope(input) || !isTimestamp(input.sinceMs) || input.bundle !== void 0 && (!Array.isArray(input.bundle) || input.bundle.length < 1 || input.bundle.length > 2 || input.bundle.some((bundle) => !require_bundleEventRow.isRecord(bundle) || !hasOnlyKeys(bundle, [
135
- "field",
136
- "value",
137
- "types"
138
- ]) || bundle.field !== "from_bundle_id" && bundle.field !== "to_bundle_id" || !isText(bundle.value) || !Array.isArray(bundle.types) || bundle.types.length === 0 || bundle.types.some((type) => ![
139
- "UNCHANGED",
140
- "UPDATE_DOWNLOADED",
141
- "UPDATE_APPLIED",
142
- "RECOVERED"
143
- ].includes(type))))) invalidQuery();
144
- return validateCount(await model.countLatestEvents(input));
145
- },
146
- async countEvents(input) {
147
- if (!require_bundleEventRow.isRecord(input) || !hasOnlyKeys(input, [
148
- "filter",
149
- "sinceMs",
150
- "beforeReceivedAtMs"
151
- ]) || !isBundleFilter(input.filter) || !isTimestamp(input.sinceMs) || !isTimestamp(input.beforeReceivedAtMs) || input.sinceMs > input.beforeReceivedAtMs) invalidQuery();
152
- return validateCount(await model.countEvents(input));
153
- },
154
- async getReleaseActivity(input) {
155
- if (!require_bundleEventRow.isRecord(input) || !hasOnlyKeys(input, [
156
- "releases",
157
- "scope",
158
- "timeRange"
159
- ])) invalidQuery();
160
- const releases = input.releases;
161
- const scope = input.scope;
162
- const timeRange = input.timeRange;
163
- const releaseQuery = Array.isArray(releases) && releases.length > 0 && releases.length <= 100 && releases.every(isReleaseReference) && scope === void 0 && (timeRange === void 0 || isTimeRange(timeRange));
164
- const scopeQuery = releases === void 0 && require_bundleEventRow.isRecord(scope) && hasOnlyKeys(scope, ["platform", "channel"]) && hasScope(scope) && isTimeRange(timeRange);
165
- if (!releaseQuery && !scopeQuery) invalidQuery();
166
- const result = await model.getReleaseActivity(input);
167
- validateAggregateResult(result);
168
- if (!Array.isArray(result.data)) invalidResult();
169
- return result;
170
- },
171
- async getAppUsage(input) {
172
- if (!require_bundleEventRow.isRecord(input) || !hasOnlyKeys(input, [
173
- "channel",
174
- "platform",
175
- "appVersion",
176
- "timeRange",
177
- "intervalMs"
178
- ]) || !isText(input.channel) || ![
179
- "all",
180
- "ios",
181
- "android"
182
- ].includes(input.platform) || input.appVersion !== void 0 && !isText(input.appVersion) || !isTimeRange(input.timeRange) || !isTimestamp(input.intervalMs) || input.intervalMs < 36e5) invalidQuery();
183
- const result = await model.getAppUsage(input);
184
- validateAggregateResult(result);
185
- if (!isTimestamp(result.activeInstallations) || !Array.isArray(result.points) || !Array.isArray(result.appVersions) || !Array.isArray(result.versions) || !Array.isArray(result.platforms) || !Array.isArray(result.bundleDistribution)) invalidResult();
186
- return result;
187
- }
188
- });
189
- //#endregion
190
- exports.assertBundleEventRow = assertBundleEventRow;
191
- exports.compareInsightsText = compareInsightsText;
192
- exports.createValidatedInsightsModel = createValidatedInsightsModel;
193
- exports.isInsightsMovementEvent = isInsightsMovementEvent;
194
- exports.matchesInsightsEventFilter = matchesInsightsEventFilter;
@@ -1,14 +0,0 @@
1
- import { BundleEventRow } from "./types/databaseRows.cjs";
2
- import { InsightsModel } from "./types/insights.cjs";
3
- import "./types/index.cjs";
4
- //#region src/insightsContract.d.ts
5
- /** Exact UTF-8 byte ordering, without case folding or Unicode normalization. */
6
- declare const compareInsightsText: (left: string, right: string) => number;
7
- /** Throws `invalid-data` unless `row` is a complete, well-formed bundle event row. */
8
- declare const assertBundleEventRow: (row: unknown) => void;
9
- /** Downloads and applied transitions belong in installation history. */
10
- declare const isInsightsMovementEvent: (event: Pick<BundleEventRow, "type">) => boolean;
11
- /** Validate custom and bundled providers at the same public boundary. */
12
- declare const createValidatedInsightsModel: (model: InsightsModel) => InsightsModel;
13
- //#endregion
14
- export { assertBundleEventRow, compareInsightsText, createValidatedInsightsModel, isInsightsMovementEvent };
@@ -1,14 +0,0 @@
1
- import { BundleEventRow } from "./types/databaseRows.mjs";
2
- import { InsightsModel } from "./types/insights.mjs";
3
- import "./types/index.mjs";
4
- //#region src/insightsContract.d.ts
5
- /** Exact UTF-8 byte ordering, without case folding or Unicode normalization. */
6
- declare const compareInsightsText: (left: string, right: string) => number;
7
- /** Throws `invalid-data` unless `row` is a complete, well-formed bundle event row. */
8
- declare const assertBundleEventRow: (row: unknown) => void;
9
- /** Downloads and applied transitions belong in installation history. */
10
- declare const isInsightsMovementEvent: (event: Pick<BundleEventRow, "type">) => boolean;
11
- /** Validate custom and bundled providers at the same public boundary. */
12
- declare const createValidatedInsightsModel: (model: InsightsModel) => InsightsModel;
13
- //#endregion
14
- export { assertBundleEventRow, compareInsightsText, createValidatedInsightsModel, isInsightsMovementEvent };
@@ -1,190 +0,0 @@
1
- import { DatabasePluginInputError } from "./databaseErrors.mjs";
2
- import { isRecord, validateBundleEventFields } from "./bundleEventRow.mjs";
3
- import { isUUIDv7 } from "./uuidv7.mjs";
4
- //#region src/insightsContract.ts
5
- const encoder = new TextEncoder();
6
- /** Exact UTF-8 byte ordering, without case folding or Unicode normalization. */
7
- const compareInsightsText = (left, right) => {
8
- const a = encoder.encode(left);
9
- const b = encoder.encode(right);
10
- for (let index = 0; index < Math.min(a.length, b.length); index += 1) if (a[index] !== b[index]) return a[index] - b[index];
11
- return a.length - b.length;
12
- };
13
- const isWellFormedText = (value) => {
14
- for (const character of value) {
15
- const point = character.codePointAt(0);
16
- if (point >= 55296 && point <= 57343) return false;
17
- }
18
- return true;
19
- };
20
- const isIdentity = (value) => typeof value === "string" && value.length > 0 && value.length <= 255 && isWellFormedText(value);
21
- const isText = (value) => typeof value === "string" && value.length > 0 && isWellFormedText(value);
22
- const isTimestamp = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
23
- const isLimit = (value) => isTimestamp(value) && value >= 1 && value <= 101;
24
- const hasOnlyKeys = (value, keys) => Object.keys(value).every((key) => keys.includes(key));
25
- const hasScope = (value) => (value.platform === "ios" || value.platform === "android") && isText(value.channel);
26
- const isBundleFilter = (value, withKind = false) => {
27
- if (!isRecord(value) || !hasScope(value)) return false;
28
- const keys = [
29
- "platform",
30
- "channel",
31
- "type",
32
- ...withKind ? ["kind"] : []
33
- ];
34
- return value.type === "RECOVERED" ? isText(value.fromBundleId) && hasOnlyKeys(value, [...keys, "fromBundleId"]) : (value.type === "UPDATE_DOWNLOADED" || value.type === "UPDATE_APPLIED" || value.type === "UNCHANGED") && isText(value.toBundleId) && hasOnlyKeys(value, [...keys, "toBundleId"]);
35
- };
36
- const isEventFilter = (value) => {
37
- if (!isRecord(value)) return false;
38
- if (value.kind === "all") return hasOnlyKeys(value, ["kind"]);
39
- if (value.kind === "installationMovement") return isIdentity(value.installId) && hasOnlyKeys(value, ["kind", "installId"]);
40
- return value.kind === "bundle" && isBundleFilter(value, true);
41
- };
42
- const validateRow = (model, row, result = false) => {
43
- try {
44
- validateBundleEventFields(row);
45
- if (!isRecord(row) || typeof row.id !== "string" || !isUUIDv7(row.id) || [...Object.values(row), ...isRecord(row.metadata) ? Object.values(row.metadata) : []].some((value) => typeof value === "string" && !isWellFormedText(value))) throw new DatabasePluginInputError("invalid-data");
46
- } catch (error) {
47
- if (result) throw new DatabasePluginInputError("invalid-result");
48
- throw error;
49
- }
50
- };
51
- /** Throws `invalid-data` unless `row` is a complete, well-formed bundle event row. */
52
- const assertBundleEventRow = (row) => validateRow("bundle_events", row);
53
- /** Downloads and applied transitions belong in installation history. */
54
- const isInsightsMovementEvent = (event) => event.type === "UPDATE_DOWNLOADED" || event.type === "UPDATE_APPLIED" || event.type === "RECOVERED";
55
- const matchesInsightsEventFilter = (event, filter) => {
56
- if (filter.kind === "all") return true;
57
- if (filter.kind === "installationMovement") return event.install_id === filter.installId && isInsightsMovementEvent(event);
58
- return event.type === filter.type && event.platform === filter.platform && event.channel === filter.channel && (filter.type === "RECOVERED" ? event.from_bundle_id === filter.fromBundleId : event.to_bundle_id === filter.toBundleId);
59
- };
60
- const invalidQuery = () => {
61
- throw new DatabasePluginInputError("invalid-query");
62
- };
63
- const invalidResult = () => {
64
- throw new DatabasePluginInputError("invalid-result");
65
- };
66
- const validateCount = (count) => isTimestamp(count) ? count : invalidResult();
67
- const isTimeRange = (value) => isRecord(value) && hasOnlyKeys(value, ["start", "end"]) && isTimestamp(value.start) && isTimestamp(value.end) && value.start < value.end;
68
- const isReleaseReference = (value) => isRecord(value) && hasOnlyKeys(value, [
69
- "releaseId",
70
- "platform",
71
- "channel"
72
- ]) && isIdentity(value.releaseId) && hasScope(value);
73
- const validateAggregateResult = (value) => {
74
- const result = isRecord(value) ? value : invalidResult();
75
- if (!isTimestamp(result.measuredAtMs)) invalidResult();
76
- const coverage = result.coverage;
77
- if (!isRecord(coverage) || coverage.kind !== "complete" && coverage.kind !== "partial" || coverage.sinceMs !== null && !isTimestamp(coverage.sinceMs)) invalidResult();
78
- };
79
- /** Validate custom and bundled providers at the same public boundary. */
80
- const createValidatedInsightsModel = (model) => ({
81
- async recordEvent(input) {
82
- if (!isRecord(input) || !hasOnlyKeys(input, ["event"])) throw new DatabasePluginInputError("invalid-data");
83
- validateRow("bundle_events", input.event);
84
- await model.recordEvent(input);
85
- },
86
- async listEvents(input) {
87
- if (!isRecord(input) || !hasOnlyKeys(input, [
88
- "filter",
89
- "sinceMs",
90
- "beforeReceivedAtMs",
91
- "after",
92
- "limit"
93
- ]) || !isEventFilter(input.filter) || !isLimit(input.limit) || !isTimestamp(input.beforeReceivedAtMs) || input.sinceMs !== void 0 && !isTimestamp(input.sinceMs)) invalidQuery();
94
- const sinceMs = input.sinceMs ?? 0;
95
- if (sinceMs > input.beforeReceivedAtMs || input.after !== void 0 && (!isRecord(input.after) || !hasOnlyKeys(input.after, ["receivedAtMs", "id"]) || !isTimestamp(input.after.receivedAtMs) || typeof input.after.id !== "string" || !isUUIDv7(input.after.id) || input.after.receivedAtMs < sinceMs || input.after.receivedAtMs >= input.beforeReceivedAtMs)) invalidQuery();
96
- const rows = await model.listEvents(input);
97
- if (!Array.isArray(rows) || rows.length > input.limit) invalidResult();
98
- let previous = input.after;
99
- for (const row of rows) {
100
- validateRow("bundle_events", row, true);
101
- if (row.received_at_ms < sinceMs || row.received_at_ms >= input.beforeReceivedAtMs || !matchesInsightsEventFilter(row, input.filter) || previous !== void 0 && (row.received_at_ms > previous.receivedAtMs || row.received_at_ms === previous.receivedAtMs && row.id >= previous.id)) invalidResult();
102
- previous = {
103
- receivedAtMs: row.received_at_ms,
104
- id: row.id
105
- };
106
- }
107
- return rows;
108
- },
109
- async findLatestEvents(input) {
110
- if (!isRecord(input)) invalidQuery();
111
- if ("installId" in input) {
112
- if (!hasOnlyKeys(input, ["installId"]) || !isIdentity(input.installId)) invalidQuery();
113
- } else if (!hasOnlyKeys(input, [
114
- "userId",
115
- "afterInstallId",
116
- "limit"
117
- ]) || !isIdentity(input.userId) || !isLimit(input.limit) || input.afterInstallId !== void 0 && !isIdentity(input.afterInstallId)) invalidQuery();
118
- const rows = await model.findLatestEvents(input);
119
- if (!Array.isArray(rows) || rows.length > ("installId" in input ? 1 : input.limit)) invalidResult();
120
- let previous = "installId" in input ? void 0 : input.afterInstallId;
121
- for (const row of rows) {
122
- validateRow("bundle_events", row, true);
123
- if ("installId" in input ? row.install_id !== input.installId : row.user_id !== input.userId || previous !== void 0 && compareInsightsText(row.install_id, previous) <= 0) invalidResult();
124
- previous = row.install_id;
125
- }
126
- return rows;
127
- },
128
- async countLatestEvents(input) {
129
- if (!isRecord(input) || !hasOnlyKeys(input, [
130
- "platform",
131
- "channel",
132
- "sinceMs",
133
- "bundle"
134
- ]) || !hasScope(input) || !isTimestamp(input.sinceMs) || input.bundle !== void 0 && (!Array.isArray(input.bundle) || input.bundle.length < 1 || input.bundle.length > 2 || input.bundle.some((bundle) => !isRecord(bundle) || !hasOnlyKeys(bundle, [
135
- "field",
136
- "value",
137
- "types"
138
- ]) || bundle.field !== "from_bundle_id" && bundle.field !== "to_bundle_id" || !isText(bundle.value) || !Array.isArray(bundle.types) || bundle.types.length === 0 || bundle.types.some((type) => ![
139
- "UNCHANGED",
140
- "UPDATE_DOWNLOADED",
141
- "UPDATE_APPLIED",
142
- "RECOVERED"
143
- ].includes(type))))) invalidQuery();
144
- return validateCount(await model.countLatestEvents(input));
145
- },
146
- async countEvents(input) {
147
- if (!isRecord(input) || !hasOnlyKeys(input, [
148
- "filter",
149
- "sinceMs",
150
- "beforeReceivedAtMs"
151
- ]) || !isBundleFilter(input.filter) || !isTimestamp(input.sinceMs) || !isTimestamp(input.beforeReceivedAtMs) || input.sinceMs > input.beforeReceivedAtMs) invalidQuery();
152
- return validateCount(await model.countEvents(input));
153
- },
154
- async getReleaseActivity(input) {
155
- if (!isRecord(input) || !hasOnlyKeys(input, [
156
- "releases",
157
- "scope",
158
- "timeRange"
159
- ])) invalidQuery();
160
- const releases = input.releases;
161
- const scope = input.scope;
162
- const timeRange = input.timeRange;
163
- const releaseQuery = Array.isArray(releases) && releases.length > 0 && releases.length <= 100 && releases.every(isReleaseReference) && scope === void 0 && (timeRange === void 0 || isTimeRange(timeRange));
164
- const scopeQuery = releases === void 0 && isRecord(scope) && hasOnlyKeys(scope, ["platform", "channel"]) && hasScope(scope) && isTimeRange(timeRange);
165
- if (!releaseQuery && !scopeQuery) invalidQuery();
166
- const result = await model.getReleaseActivity(input);
167
- validateAggregateResult(result);
168
- if (!Array.isArray(result.data)) invalidResult();
169
- return result;
170
- },
171
- async getAppUsage(input) {
172
- if (!isRecord(input) || !hasOnlyKeys(input, [
173
- "channel",
174
- "platform",
175
- "appVersion",
176
- "timeRange",
177
- "intervalMs"
178
- ]) || !isText(input.channel) || ![
179
- "all",
180
- "ios",
181
- "android"
182
- ].includes(input.platform) || input.appVersion !== void 0 && !isText(input.appVersion) || !isTimeRange(input.timeRange) || !isTimestamp(input.intervalMs) || input.intervalMs < 36e5) invalidQuery();
183
- const result = await model.getAppUsage(input);
184
- validateAggregateResult(result);
185
- if (!isTimestamp(result.activeInstallations) || !Array.isArray(result.points) || !Array.isArray(result.appVersions) || !Array.isArray(result.versions) || !Array.isArray(result.platforms) || !Array.isArray(result.bundleDistribution)) invalidResult();
186
- return result;
187
- }
188
- });
189
- //#endregion
190
- export { assertBundleEventRow, compareInsightsText, createValidatedInsightsModel, isInsightsMovementEvent, matchesInsightsEventFilter };
@@ -1,12 +0,0 @@
1
- //#region src/insightsDistinctSummary.d.ts
2
- declare const getInsightsDistinctRegister: (identity: string) => {
3
- index: number;
4
- rank: number;
5
- character: string;
6
- };
7
- declare const addInsightsDistinct: (summary: string | null | undefined, identity: string) => string;
8
- declare const mergeInsightsDistinct: (summaries: readonly (string | null | undefined)[]) => string;
9
- declare const countInsightsDistinct: (summary: string | null | undefined) => number;
10
- declare const emptyInsightsDistinct: () => string;
11
- //#endregion
12
- export { addInsightsDistinct, countInsightsDistinct, emptyInsightsDistinct, getInsightsDistinctRegister, mergeInsightsDistinct };
@@ -1,12 +0,0 @@
1
- //#region src/insightsDistinctSummary.d.ts
2
- declare const getInsightsDistinctRegister: (identity: string) => {
3
- index: number;
4
- rank: number;
5
- character: string;
6
- };
7
- declare const addInsightsDistinct: (summary: string | null | undefined, identity: string) => string;
8
- declare const mergeInsightsDistinct: (summaries: readonly (string | null | undefined)[]) => string;
9
- declare const countInsightsDistinct: (summary: string | null | undefined) => number;
10
- declare const emptyInsightsDistinct: () => string;
11
- //#endregion
12
- export { addInsightsDistinct, countInsightsDistinct, emptyInsightsDistinct, getInsightsDistinctRegister, mergeInsightsDistinct };
@@ -1,139 +0,0 @@
1
- //#region src/insightsOverview.ts
2
- const encodeIdentity = (identity) => [
3
- identity.scopeKind,
4
- identity.releaseKind,
5
- identity.releaseId,
6
- identity.channel,
7
- identity.platform,
8
- identity.appVersionKind,
9
- identity.appVersion,
10
- identity.periodKind,
11
- String(identity.bucketStartMs)
12
- ].map((value) => `${new TextEncoder().encode(value).length}:${value}`).join("");
13
- const fnv = (value, seed) => {
14
- let result = seed;
15
- for (const byte of new TextEncoder().encode(value)) {
16
- result ^= BigInt(byte);
17
- result = BigInt.asUintN(64, result * 1099511628211n);
18
- }
19
- return result.toString(16).padStart(16, "0");
20
- };
21
- /** A 128-bit hex key for a string: two FNV-1a 64 hashes with different seeds. */
22
- const insightsKey = (value) => `${fnv(value, 14695981039346656037n)}${fnv(value, 9521211207457086692n)}`;
23
- const insightsOverviewId = (identity) => insightsKey(encodeIdentity(identity));
24
- const insightsOverviewValues = (identity) => ({
25
- id: insightsOverviewId(identity),
26
- scope_kind: identity.scopeKind,
27
- release_kind: identity.releaseKind,
28
- release_id: identity.releaseId,
29
- channel: identity.channel,
30
- platform: identity.platform,
31
- app_version_kind: identity.appVersionKind,
32
- app_version: identity.appVersion,
33
- period_kind: identity.periodKind,
34
- bucket_start_ms: identity.bucketStartMs
35
- });
36
- const hour = (value) => Math.floor(value / 36e5) * 36e5;
37
- const currentInsightsReleaseId = (event) => event.type === "UPDATE_DOWNLOADED" ? event.from_release_id : event.to_release_id;
38
- const insightsDistributionIdentity = (event) => {
39
- const releaseId = currentInsightsReleaseId(event);
40
- return {
41
- scopeKind: "distribution",
42
- releaseKind: releaseId === null ? "embedded" : "specific",
43
- releaseId: releaseId ?? "",
44
- channel: event.channel,
45
- platform: event.platform,
46
- appVersionKind: "specific",
47
- appVersion: event.app_version,
48
- periodKind: "latest",
49
- bucketStartMs: hour(event.received_at_ms)
50
- };
51
- };
52
- const insightsOverviewDeltas = (event) => {
53
- const bucketStartMs = hour(event.received_at_ms);
54
- const counters = /* @__PURE__ */ new Map();
55
- const append = (identity, values) => {
56
- const key = encodeIdentity(identity);
57
- const previous = counters.get(key);
58
- counters.set(key, {
59
- identity,
60
- downloads: (previous?.downloads ?? 0) + values.downloads,
61
- launches: (previous?.launches ?? 0) + values.launches,
62
- failedLaunches: (previous?.failedLaunches ?? 0) + values.failedLaunches,
63
- launchIdentity: previous?.launchIdentity ?? values.launchIdentity,
64
- activityIdentity: previous?.activityIdentity ?? values.activityIdentity
65
- });
66
- };
67
- const metric = (releaseId, values) => {
68
- const launchIdentity = values.launches > 0 ? event.install_id : void 0;
69
- if (releaseId !== null) for (const [periodKind, start] of [["lifetime", 0], ["hour", bucketStartMs]]) append({
70
- scopeKind: "release",
71
- releaseKind: "specific",
72
- releaseId,
73
- channel: event.channel,
74
- platform: event.platform,
75
- appVersionKind: "all",
76
- appVersion: "",
77
- periodKind,
78
- bucketStartMs: start
79
- }, {
80
- ...values,
81
- ...periodKind === "hour" ? { launchIdentity } : {}
82
- });
83
- append({
84
- scopeKind: "channel",
85
- releaseKind: "all",
86
- releaseId: "",
87
- channel: event.channel,
88
- platform: event.platform,
89
- appVersionKind: "all",
90
- appVersion: "",
91
- periodKind: "hour",
92
- bucketStartMs
93
- }, {
94
- ...values,
95
- launchIdentity
96
- });
97
- };
98
- if (event.type === "UPDATE_DOWNLOADED") metric(event.to_release_id, {
99
- downloads: 1,
100
- launches: 0,
101
- failedLaunches: 0
102
- });
103
- else {
104
- metric(event.to_release_id, {
105
- downloads: 0,
106
- launches: 1,
107
- failedLaunches: 0
108
- });
109
- if (event.type === "RECOVERED") metric(event.from_release_id, {
110
- downloads: 0,
111
- launches: 0,
112
- failedLaunches: 1
113
- });
114
- }
115
- for (const platform of [event.platform, "all"]) for (const appVersionKind of ["specific", "all"]) append({
116
- scopeKind: "usage",
117
- releaseKind: "all",
118
- releaseId: "",
119
- channel: event.channel,
120
- platform,
121
- appVersionKind,
122
- appVersion: appVersionKind === "specific" ? event.app_version : "",
123
- periodKind: "hour",
124
- bucketStartMs
125
- }, {
126
- downloads: 0,
127
- launches: 0,
128
- failedLaunches: 0,
129
- activityIdentity: event.install_id
130
- });
131
- return [...counters.values()];
132
- };
133
- //#endregion
134
- exports.currentInsightsReleaseId = currentInsightsReleaseId;
135
- exports.insightsDistributionIdentity = insightsDistributionIdentity;
136
- exports.insightsKey = insightsKey;
137
- exports.insightsOverviewDeltas = insightsOverviewDeltas;
138
- exports.insightsOverviewId = insightsOverviewId;
139
- exports.insightsOverviewValues = insightsOverviewValues;