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

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 (145) hide show
  1. package/dist/bundleEventRow.cjs +42 -0
  2. package/dist/bundleEventRow.mjs +41 -0
  3. package/dist/coreApi.d.cts +131 -0
  4. package/dist/coreApi.d.mts +131 -0
  5. package/dist/database/adapter.cjs +37 -0
  6. package/dist/database/adapter.d.cts +150 -0
  7. package/dist/database/adapter.d.mts +150 -0
  8. package/dist/database/adapter.mjs +30 -0
  9. package/dist/database/memoryAdapter.cjs +102 -0
  10. package/dist/database/memoryAdapter.d.cts +16 -0
  11. package/dist/database/memoryAdapter.d.mts +16 -0
  12. package/dist/database/memoryAdapter.mjs +102 -0
  13. package/dist/database/values.cjs +110 -0
  14. package/dist/database/values.d.cts +30 -0
  15. package/dist/database/values.d.mts +30 -0
  16. package/dist/database/values.mjs +100 -0
  17. package/dist/database/verifyAdapter.cjs +129 -0
  18. package/dist/database/verifyAdapter.d.cts +29 -0
  19. package/dist/database/verifyAdapter.d.mts +29 -0
  20. package/dist/database/verifyAdapter.mjs +128 -0
  21. package/dist/databaseErrors.cjs +30 -0
  22. package/dist/databaseErrors.d.cts +21 -0
  23. package/dist/databaseErrors.d.mts +21 -0
  24. package/dist/databaseErrors.mjs +28 -0
  25. package/dist/databaseMetadata.cjs +2 -2
  26. package/dist/databaseMetadata.mjs +1 -1
  27. package/dist/index.cjs +10 -36
  28. package/dist/index.d.cts +13 -17
  29. package/dist/index.d.mts +13 -17
  30. package/dist/index.mjs +8 -14
  31. package/dist/insightsContract.cjs +26 -24
  32. package/dist/insightsContract.d.cts +6 -1
  33. package/dist/insightsContract.d.mts +6 -1
  34. package/dist/insightsContract.mjs +6 -5
  35. package/dist/insightsDistinctSummary.cjs +2 -1
  36. package/dist/insightsDistinctSummary.mjs +2 -1
  37. package/dist/insightsOverview.cjs +4 -4
  38. package/dist/insightsOverview.d.cts +3 -1
  39. package/dist/insightsOverview.d.mts +3 -1
  40. package/dist/insightsOverview.mjs +4 -5
  41. package/dist/internal.cjs +32 -8
  42. package/dist/internal.d.cts +11 -10
  43. package/dist/internal.d.mts +11 -10
  44. package/dist/internal.mjs +8 -5
  45. package/dist/releaseCatalogCompiler.cjs +1 -1
  46. package/dist/releaseCatalogCompiler.mjs +2 -2
  47. package/dist/releaseCatalogMutation.cjs +0 -292
  48. package/dist/releaseCatalogMutation.d.cts +4 -45
  49. package/dist/releaseCatalogMutation.d.mts +4 -45
  50. package/dist/releaseCatalogMutation.mjs +1 -288
  51. package/dist/releaseManagement.cjs +0 -221
  52. package/dist/releaseManagement.d.cts +4 -28
  53. package/dist/releaseManagement.d.mts +4 -28
  54. package/dist/releaseManagement.mjs +1 -216
  55. package/dist/types/databaseConfig.cjs +5 -0
  56. package/dist/types/databaseConfig.d.cts +33 -0
  57. package/dist/types/databaseConfig.d.mts +33 -0
  58. package/dist/types/databaseConfig.mjs +5 -0
  59. package/dist/types/databaseOperations.d.cts +5 -174
  60. package/dist/types/databaseOperations.d.mts +5 -174
  61. package/dist/types/index.cjs +2 -0
  62. package/dist/types/index.d.cts +8 -73
  63. package/dist/types/index.d.mts +8 -73
  64. package/dist/types/index.mjs +2 -1
  65. package/dist/types/insights.d.cts +172 -0
  66. package/dist/types/insights.d.mts +172 -0
  67. package/dist/types/internal.d.cts +4 -7
  68. package/dist/types/internal.d.mts +4 -7
  69. package/dist/types/models.d.cts +24 -0
  70. package/dist/types/models.d.mts +24 -0
  71. package/dist/types/public.d.cts +4 -2
  72. package/dist/types/public.d.mts +4 -2
  73. package/package.json +1 -1
  74. package/dist/calculatePagination.cjs +0 -25
  75. package/dist/calculatePagination.d.cts +0 -12
  76. package/dist/calculatePagination.d.mts +0 -12
  77. package/dist/calculatePagination.mjs +0 -25
  78. package/dist/createDatabasePlugin.cjs +0 -875
  79. package/dist/createDatabasePlugin.d.cts +0 -28
  80. package/dist/createDatabasePlugin.d.mts +0 -28
  81. package/dist/createDatabasePlugin.mjs +0 -871
  82. package/dist/databaseClient.cjs +0 -117
  83. package/dist/databaseClient.d.cts +0 -32
  84. package/dist/databaseClient.d.mts +0 -32
  85. package/dist/databaseClient.mjs +0 -114
  86. package/dist/databaseClientReads.cjs +0 -115
  87. package/dist/databaseClientReads.mjs +0 -113
  88. package/dist/databaseClientUpdates.cjs +0 -37
  89. package/dist/databaseClientUpdates.d.cts +0 -11
  90. package/dist/databaseClientUpdates.d.mts +0 -11
  91. package/dist/databaseClientUpdates.mjs +0 -35
  92. package/dist/databasePluginCrud.cjs +0 -87
  93. package/dist/databasePluginCrud.d.cts +0 -4
  94. package/dist/databasePluginCrud.d.mts +0 -4
  95. package/dist/databasePluginCrud.mjs +0 -86
  96. package/dist/databasePluginCrudValidation.d.cts +0 -6
  97. package/dist/databasePluginCrudValidation.d.mts +0 -6
  98. package/dist/databasePluginCrudValidationErrors.cjs +0 -11
  99. package/dist/databasePluginCrudValidationErrors.d.cts +0 -9
  100. package/dist/databasePluginCrudValidationErrors.d.mts +0 -9
  101. package/dist/databasePluginCrudValidationErrors.mjs +0 -11
  102. package/dist/databasePluginCrudValidationFields.cjs +0 -284
  103. package/dist/databasePluginCrudValidationFields.d.cts +0 -1
  104. package/dist/databasePluginCrudValidationFields.d.mts +0 -1
  105. package/dist/databasePluginCrudValidationFields.mjs +0 -272
  106. package/dist/databasePluginCrudValidationMutations.cjs +0 -88
  107. package/dist/databasePluginCrudValidationMutations.d.cts +0 -1
  108. package/dist/databasePluginCrudValidationMutations.d.mts +0 -1
  109. package/dist/databasePluginCrudValidationMutations.mjs +0 -82
  110. package/dist/databasePluginCrudValidationQueries.cjs +0 -103
  111. package/dist/databasePluginCrudValidationQueries.d.cts +0 -2
  112. package/dist/databasePluginCrudValidationQueries.d.mts +0 -2
  113. package/dist/databasePluginCrudValidationQueries.mjs +0 -97
  114. package/dist/databasePluginCrudValidationRows.cjs +0 -55
  115. package/dist/databasePluginCrudValidationRows.d.cts +0 -1
  116. package/dist/databasePluginCrudValidationRows.d.mts +0 -1
  117. package/dist/databasePluginCrudValidationRows.mjs +0 -53
  118. package/dist/databasePluginTransaction.cjs +0 -7
  119. package/dist/databasePluginTransaction.mjs +0 -7
  120. package/dist/insightsLatestQueries.cjs +0 -53
  121. package/dist/insightsLatestQueries.d.cts +0 -13
  122. package/dist/insightsLatestQueries.d.mts +0 -13
  123. package/dist/insightsLatestQueries.mjs +0 -52
  124. package/dist/paginateBundles.cjs +0 -53
  125. package/dist/paginateBundles.d.cts +0 -13
  126. package/dist/paginateBundles.d.mts +0 -13
  127. package/dist/paginateBundles.mjs +0 -53
  128. package/dist/queryBundles.cjs +0 -34
  129. package/dist/queryBundles.d.cts +0 -9
  130. package/dist/queryBundles.d.mts +0 -9
  131. package/dist/queryBundles.mjs +0 -32
  132. package/dist/requestBundleCache.cjs +0 -22
  133. package/dist/requestBundleCache.d.cts +0 -8
  134. package/dist/requestBundleCache.d.mts +0 -8
  135. package/dist/requestBundleCache.mjs +0 -22
  136. package/dist/types/database.d.cts +0 -5
  137. package/dist/types/database.d.mts +0 -5
  138. package/dist/types/databaseFields.cjs +0 -85
  139. package/dist/types/databaseFields.d.cts +0 -12
  140. package/dist/types/databaseFields.d.mts +0 -12
  141. package/dist/types/databaseFields.mjs +0 -85
  142. package/dist/types/databasePlugin.d.cts +0 -388
  143. package/dist/types/databasePlugin.d.mts +0 -388
  144. package/dist/types/databaseQuery.d.cts +0 -46
  145. package/dist/types/databaseQuery.d.mts +0 -46
@@ -0,0 +1,42 @@
1
+ const require_databaseErrors = require("./databaseErrors.cjs");
2
+ const require_databaseJsonValue = require("./databaseJsonValue.cjs");
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: require_databaseJsonValue.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 require_databaseErrors.DatabasePluginInputError("invalid-data");
36
+ for (const field of Object.keys(row)) if (!Object.hasOwn(BUNDLE_EVENT_FIELDS, field)) throw new require_databaseErrors.DatabasePluginInputError("invalid-field");
37
+ for (const [field, valid] of Object.entries(BUNDLE_EVENT_FIELDS)) if (!Object.hasOwn(row, field) || !valid(row[field])) throw new require_databaseErrors.DatabasePluginInputError("invalid-data");
38
+ if (!hasEventInvariants(row)) throw new require_databaseErrors.DatabasePluginInputError("invalid-data");
39
+ };
40
+ //#endregion
41
+ exports.isRecord = isRecord;
42
+ exports.validateBundleEventFields = validateBundleEventFields;
@@ -0,0 +1,41 @@
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 };
@@ -0,0 +1,131 @@
1
+ import { BundlePatchRow, BundleRow, ChannelRow, ReleaseCatalogRow, ReleaseRow } from "./types/databaseRows.cjs";
2
+ import { ChannelDeleteResult } from "./types/models.cjs";
3
+ import { Bundle } from "./types/index.cjs";
4
+ import { ReleaseCatalogMutationPreflight, ReleaseCatalogMutationResult, ReleaseCatalogRebuildPreflight, ReleaseCatalogRebuildResult } from "./releaseCatalogMutation.cjs";
5
+ import { PromoteReleaseResult, ReleasePolicyPatch } from "./releaseManagement.cjs";
6
+ //#region src/coreApi.d.ts
7
+ /** A keyset page: rows strictly after `after`, in `order`, at most `limit`. */
8
+ interface KeysetInput {
9
+ readonly order?: "asc" | "desc";
10
+ /** The last key a previous page returned. */
11
+ readonly after?: string;
12
+ readonly limit: number;
13
+ }
14
+ /** A release list's filter: exactly the sets the release indexes serve. */
15
+ type ReleaseFilter = {
16
+ readonly kind: "all";
17
+ } | {
18
+ readonly kind: "channelPlatform";
19
+ readonly channelId: string;
20
+ readonly platform: "ios" | "android";
21
+ readonly enabled?: boolean;
22
+ } | {
23
+ readonly kind: "bundle";
24
+ readonly bundleId: string;
25
+ } | {
26
+ readonly kind: "scope";
27
+ readonly scopeKey: string;
28
+ readonly enabled?: boolean;
29
+ };
30
+ interface BundleDetail {
31
+ readonly bundle: BundleRow;
32
+ readonly patches: BundlePatchRow[];
33
+ /** Patches that start from this bundle. */
34
+ readonly childCount: number;
35
+ }
36
+ /** How a deploy publishes its bundle in one channel's catalog. */
37
+ interface DeployReleasePolicy {
38
+ readonly channel: string;
39
+ readonly enabled: boolean;
40
+ readonly fingerprintHash: string | null;
41
+ readonly message: string | null;
42
+ readonly rolloutCohortCount?: number;
43
+ readonly shouldForceUpdate: boolean;
44
+ readonly targetAppVersion: string | null;
45
+ readonly targetCohorts?: string[];
46
+ }
47
+ /** A new bundle and the release that publishes it. */
48
+ interface BundleDeployment {
49
+ readonly bundle: Bundle;
50
+ readonly release: DeployReleasePolicy;
51
+ }
52
+ /** A new release for a bundle the database already holds, such as a republish. */
53
+ interface StoredBundleDeployment {
54
+ readonly bundleId: string;
55
+ readonly release: DeployReleasePolicy;
56
+ }
57
+ type Deployment = BundleDeployment | StoredBundleDeployment;
58
+ interface ReleaseTarget {
59
+ readonly releaseId: string;
60
+ /** Refuses the change when the release is at another revision. */
61
+ readonly expectedRevision?: number;
62
+ }
63
+ /**
64
+ * Core's reads and typed operations, the same in process and over the admin
65
+ * API: bundles, Releases, their Catalogs, and channels. Every read goes
66
+ * through a declared index, and lists page by key.
67
+ */
68
+ interface HotUpdaterCoreApi {
69
+ /**
70
+ * Checks the database before a command changes anything: its schema, and a
71
+ * self-hosted server's admin protocol. Throws what the first read would.
72
+ */
73
+ ready(): Promise<void>;
74
+ getBundle(id: string): Promise<BundleDetail | null>;
75
+ listBundles(input: KeysetInput & {
76
+ readonly platform?: "ios" | "android";
77
+ }): Promise<BundleDetail[]>;
78
+ /** One counter row: every bundle, or one platform's. */
79
+ countBundles(platform?: "ios" | "android"): Promise<number>;
80
+ listPatchesFromBase(baseBundleId: string, input: KeysetInput): Promise<BundlePatchRow[]>;
81
+ /** Each base bundle's child count, from its reference counter: one batch read of the bundle rows. */
82
+ countBundleChildren(baseBundleIds: readonly string[]): Promise<Record<string, number>>;
83
+ /**
84
+ * Auto-patch bases for a new bundle, from its candidate key's catalog
85
+ * scope: older bundles whose enabled releases serve the key's fingerprint,
86
+ * or a range intersecting its app version range; newest release first.
87
+ */
88
+ findBaseBundleIds(candidateKey: string, bundleId: string, limit: number): Promise<string[]>;
89
+ getRelease(id: string): Promise<ReleaseRow | null>;
90
+ listReleases(input: KeysetInput & {
91
+ readonly filter: ReleaseFilter;
92
+ }): Promise<ReleaseRow[]>;
93
+ getReleaseCatalogRow(scopeKey: string): Promise<ReleaseCatalogRow | null>;
94
+ listReleaseCatalogs(input: KeysetInput): Promise<ReleaseCatalogRow[]>;
95
+ listChannels(): Promise<ChannelRow[]>;
96
+ findChannelByName(name: string): Promise<ChannelRow | null>;
97
+ /**
98
+ * Writes each new bundle with its patches, and each release, with the next
99
+ * catalog of each scope, in one transaction. A stored bundle's deployment
100
+ * publishes a new release for it and writes no bundle.
101
+ */
102
+ deploy(deployments: readonly Deployment[]): Promise<ReleaseCatalogMutationResult[]>;
103
+ updateReleasePolicy(input: ReleaseTarget & {
104
+ readonly patch: ReleasePolicyPatch;
105
+ }): Promise<ReleaseCatalogMutationResult>;
106
+ /** What `updateReleasePolicy` would write, without writing it. */
107
+ preflightReleasePolicy(input: ReleaseTarget & {
108
+ readonly patch: ReleasePolicyPatch;
109
+ }): Promise<ReleaseCatalogMutationPreflight>;
110
+ /** Deletes a disabled release; an enabled one refuses. */
111
+ deleteRelease(input: ReleaseTarget): Promise<ReleaseCatalogMutationResult>;
112
+ /** Copies a bundle release into another channel, and with `move` disables the source. */
113
+ promoteRelease(input: ReleaseTarget & {
114
+ readonly targetChannel: string;
115
+ readonly action?: "copy" | "move";
116
+ }): Promise<PromoteReleaseResult>;
117
+ rebuildReleaseCatalog(scopeKey: string): Promise<ReleaseCatalogRebuildResult>;
118
+ preflightReleaseCatalogRebuild(scopeKey: string): Promise<ReleaseCatalogRebuildPreflight>;
119
+ /** The channel named `name`, created when missing. */
120
+ ensureChannel(name: string): Promise<ChannelRow>;
121
+ /** Deletes a channel no release uses. */
122
+ deleteChannel(id: string): Promise<ChannelDeleteResult>;
123
+ /** Changes a bundle's fields; `patches`, when given, replaces its patches. */
124
+ updateBundle(id: string, update: Partial<Omit<Bundle, "id" | "patches">> & {
125
+ readonly patches?: Bundle["patches"];
126
+ }): Promise<void>;
127
+ /** Deletes bundles with their patches; a bundle a release uses refuses. */
128
+ deleteBundles(ids: readonly string[]): Promise<void>;
129
+ }
130
+ //#endregion
131
+ export { BundleDeployment, BundleDetail, DeployReleasePolicy, Deployment, HotUpdaterCoreApi, KeysetInput, ReleaseFilter, ReleaseTarget, StoredBundleDeployment };
@@ -0,0 +1,131 @@
1
+ import { BundlePatchRow, BundleRow, ChannelRow, ReleaseCatalogRow, ReleaseRow } from "./types/databaseRows.mjs";
2
+ import { ChannelDeleteResult } from "./types/models.mjs";
3
+ import { Bundle } from "./types/index.mjs";
4
+ import { ReleaseCatalogMutationPreflight, ReleaseCatalogMutationResult, ReleaseCatalogRebuildPreflight, ReleaseCatalogRebuildResult } from "./releaseCatalogMutation.mjs";
5
+ import { PromoteReleaseResult, ReleasePolicyPatch } from "./releaseManagement.mjs";
6
+ //#region src/coreApi.d.ts
7
+ /** A keyset page: rows strictly after `after`, in `order`, at most `limit`. */
8
+ interface KeysetInput {
9
+ readonly order?: "asc" | "desc";
10
+ /** The last key a previous page returned. */
11
+ readonly after?: string;
12
+ readonly limit: number;
13
+ }
14
+ /** A release list's filter: exactly the sets the release indexes serve. */
15
+ type ReleaseFilter = {
16
+ readonly kind: "all";
17
+ } | {
18
+ readonly kind: "channelPlatform";
19
+ readonly channelId: string;
20
+ readonly platform: "ios" | "android";
21
+ readonly enabled?: boolean;
22
+ } | {
23
+ readonly kind: "bundle";
24
+ readonly bundleId: string;
25
+ } | {
26
+ readonly kind: "scope";
27
+ readonly scopeKey: string;
28
+ readonly enabled?: boolean;
29
+ };
30
+ interface BundleDetail {
31
+ readonly bundle: BundleRow;
32
+ readonly patches: BundlePatchRow[];
33
+ /** Patches that start from this bundle. */
34
+ readonly childCount: number;
35
+ }
36
+ /** How a deploy publishes its bundle in one channel's catalog. */
37
+ interface DeployReleasePolicy {
38
+ readonly channel: string;
39
+ readonly enabled: boolean;
40
+ readonly fingerprintHash: string | null;
41
+ readonly message: string | null;
42
+ readonly rolloutCohortCount?: number;
43
+ readonly shouldForceUpdate: boolean;
44
+ readonly targetAppVersion: string | null;
45
+ readonly targetCohorts?: string[];
46
+ }
47
+ /** A new bundle and the release that publishes it. */
48
+ interface BundleDeployment {
49
+ readonly bundle: Bundle;
50
+ readonly release: DeployReleasePolicy;
51
+ }
52
+ /** A new release for a bundle the database already holds, such as a republish. */
53
+ interface StoredBundleDeployment {
54
+ readonly bundleId: string;
55
+ readonly release: DeployReleasePolicy;
56
+ }
57
+ type Deployment = BundleDeployment | StoredBundleDeployment;
58
+ interface ReleaseTarget {
59
+ readonly releaseId: string;
60
+ /** Refuses the change when the release is at another revision. */
61
+ readonly expectedRevision?: number;
62
+ }
63
+ /**
64
+ * Core's reads and typed operations, the same in process and over the admin
65
+ * API: bundles, Releases, their Catalogs, and channels. Every read goes
66
+ * through a declared index, and lists page by key.
67
+ */
68
+ interface HotUpdaterCoreApi {
69
+ /**
70
+ * Checks the database before a command changes anything: its schema, and a
71
+ * self-hosted server's admin protocol. Throws what the first read would.
72
+ */
73
+ ready(): Promise<void>;
74
+ getBundle(id: string): Promise<BundleDetail | null>;
75
+ listBundles(input: KeysetInput & {
76
+ readonly platform?: "ios" | "android";
77
+ }): Promise<BundleDetail[]>;
78
+ /** One counter row: every bundle, or one platform's. */
79
+ countBundles(platform?: "ios" | "android"): Promise<number>;
80
+ listPatchesFromBase(baseBundleId: string, input: KeysetInput): Promise<BundlePatchRow[]>;
81
+ /** Each base bundle's child count, from its reference counter: one batch read of the bundle rows. */
82
+ countBundleChildren(baseBundleIds: readonly string[]): Promise<Record<string, number>>;
83
+ /**
84
+ * Auto-patch bases for a new bundle, from its candidate key's catalog
85
+ * scope: older bundles whose enabled releases serve the key's fingerprint,
86
+ * or a range intersecting its app version range; newest release first.
87
+ */
88
+ findBaseBundleIds(candidateKey: string, bundleId: string, limit: number): Promise<string[]>;
89
+ getRelease(id: string): Promise<ReleaseRow | null>;
90
+ listReleases(input: KeysetInput & {
91
+ readonly filter: ReleaseFilter;
92
+ }): Promise<ReleaseRow[]>;
93
+ getReleaseCatalogRow(scopeKey: string): Promise<ReleaseCatalogRow | null>;
94
+ listReleaseCatalogs(input: KeysetInput): Promise<ReleaseCatalogRow[]>;
95
+ listChannels(): Promise<ChannelRow[]>;
96
+ findChannelByName(name: string): Promise<ChannelRow | null>;
97
+ /**
98
+ * Writes each new bundle with its patches, and each release, with the next
99
+ * catalog of each scope, in one transaction. A stored bundle's deployment
100
+ * publishes a new release for it and writes no bundle.
101
+ */
102
+ deploy(deployments: readonly Deployment[]): Promise<ReleaseCatalogMutationResult[]>;
103
+ updateReleasePolicy(input: ReleaseTarget & {
104
+ readonly patch: ReleasePolicyPatch;
105
+ }): Promise<ReleaseCatalogMutationResult>;
106
+ /** What `updateReleasePolicy` would write, without writing it. */
107
+ preflightReleasePolicy(input: ReleaseTarget & {
108
+ readonly patch: ReleasePolicyPatch;
109
+ }): Promise<ReleaseCatalogMutationPreflight>;
110
+ /** Deletes a disabled release; an enabled one refuses. */
111
+ deleteRelease(input: ReleaseTarget): Promise<ReleaseCatalogMutationResult>;
112
+ /** Copies a bundle release into another channel, and with `move` disables the source. */
113
+ promoteRelease(input: ReleaseTarget & {
114
+ readonly targetChannel: string;
115
+ readonly action?: "copy" | "move";
116
+ }): Promise<PromoteReleaseResult>;
117
+ rebuildReleaseCatalog(scopeKey: string): Promise<ReleaseCatalogRebuildResult>;
118
+ preflightReleaseCatalogRebuild(scopeKey: string): Promise<ReleaseCatalogRebuildPreflight>;
119
+ /** The channel named `name`, created when missing. */
120
+ ensureChannel(name: string): Promise<ChannelRow>;
121
+ /** Deletes a channel no release uses. */
122
+ deleteChannel(id: string): Promise<ChannelDeleteResult>;
123
+ /** Changes a bundle's fields; `patches`, when given, replaces its patches. */
124
+ updateBundle(id: string, update: Partial<Omit<Bundle, "id" | "patches">> & {
125
+ readonly patches?: Bundle["patches"];
126
+ }): Promise<void>;
127
+ /** Deletes bundles with their patches; a bundle a release uses refuses. */
128
+ deleteBundles(ids: readonly string[]): Promise<void>;
129
+ }
130
+ //#endregion
131
+ export { BundleDeployment, BundleDetail, DeployReleasePolicy, Deployment, HotUpdaterCoreApi, KeysetInput, ReleaseFilter, ReleaseTarget, StoredBundleDeployment };
@@ -0,0 +1,37 @@
1
+ //#region src/database/adapter.ts
2
+ const DATABASE_VERSION_COLUMN = "_v";
3
+ const DATABASE_MAX_QUERY_LIMIT = 500;
4
+ const DATABASE_MAX_MULTI_VALUES = 16;
5
+ var DatabaseSchemaError = class extends Error {
6
+ name = "DatabaseSchemaError";
7
+ };
8
+ const findPhysicalColumn = (table, name) => {
9
+ const column = table.columns.find((candidate) => candidate.name === name);
10
+ if (column === void 0) throw new DatabaseSchemaError(`${table.name} has no column ${name}.`);
11
+ return column;
12
+ };
13
+ const findPhysicalIndex = (table, name) => {
14
+ const index = table.indexes.find((candidate) => candidate.name === name);
15
+ if (index === void 0) throw new DatabaseSchemaError(`${table.name} has no index ${name}.`);
16
+ return index;
17
+ };
18
+ /**
19
+ * The columns that order an index read and close its cursors: the sort
20
+ * columns, then the key columns that neither `eq` nor `sort` fixes.
21
+ */
22
+ const indexOrderColumns = (table, index) => [...index.sort, ...table.key.filter((column) => !index.sort.includes(column) && !index.eq.includes(column))];
23
+ /** The primary key of a row, in key-column order. */
24
+ const rowKey = (table, row) => table.key.map((column) => {
25
+ const value = row[column];
26
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") throw new DatabaseSchemaError(`${table.name}.${column} is a key column and must be a string, number, or boolean.`);
27
+ return value;
28
+ });
29
+ //#endregion
30
+ exports.DATABASE_MAX_MULTI_VALUES = DATABASE_MAX_MULTI_VALUES;
31
+ exports.DATABASE_MAX_QUERY_LIMIT = DATABASE_MAX_QUERY_LIMIT;
32
+ exports.DATABASE_VERSION_COLUMN = DATABASE_VERSION_COLUMN;
33
+ exports.DatabaseSchemaError = DatabaseSchemaError;
34
+ exports.findPhysicalColumn = findPhysicalColumn;
35
+ exports.findPhysicalIndex = findPhysicalIndex;
36
+ exports.indexOrderColumns = indexOrderColumns;
37
+ exports.rowKey = rowKey;
@@ -0,0 +1,150 @@
1
+ //#region src/database/adapter.d.ts
2
+ /**
3
+ * The storage adapter contract. An adapter implements `get`, `query`, and
4
+ * `write` with its backend's native features and knows nothing about Hot
5
+ * Updater's domain or plugins. Adapter authors import it from the public
6
+ * `@hot-updater/server/database` subpath.
7
+ */
8
+ type DatabaseJson = null | boolean | number | string | readonly DatabaseJson[] | {
9
+ readonly [key: string]: DatabaseJson;
10
+ };
11
+ /** A stored value: a string, number, boolean, null, or JSON document. */
12
+ type DatabaseValue = DatabaseJson;
13
+ /** A value allowed in keys and indexes. */
14
+ type DatabaseKeyValue = string | number | boolean;
15
+ /** Primary-key values in the table's key-column order. */
16
+ type DatabaseKey = readonly DatabaseKeyValue[];
17
+ type StoredRow = {
18
+ readonly [column: string]: DatabaseValue;
19
+ };
20
+ /** `integer` holds safe integers; `number` holds any finite number. */
21
+ type PhysicalColumnType = "string" | "integer" | "number" | "boolean" | "json";
22
+ interface PhysicalColumn {
23
+ readonly name: string;
24
+ readonly type: PhysicalColumnType;
25
+ readonly nullable: boolean;
26
+ /** Strings only: the maximum length in UTF-16 code units; unbounded text when absent. */
27
+ readonly maxLength?: number;
28
+ /** Strings only: ASCII text, stored in single bytes where a backend limits index size (MySQL). */
29
+ readonly ascii?: true;
30
+ /** A whole number the backend stores when a writer omits the column; the engine's own columns default to 0. */
31
+ readonly default?: number;
32
+ /** A multi-valued field: an array of 1–16 values of `type`, indexable in `eq` only. */
33
+ readonly multi?: true;
34
+ }
35
+ interface PhysicalIndex {
36
+ readonly name: string;
37
+ readonly eq: readonly string[];
38
+ readonly sort: readonly string[];
39
+ /** The `eq` values identify at most one row. */
40
+ readonly unique?: true;
41
+ }
42
+ interface PhysicalTable {
43
+ /** The logical name; each adapter applies its own table prefix. */
44
+ readonly name: string;
45
+ readonly columns: readonly PhysicalColumn[];
46
+ /** Primary-key columns, in order. */
47
+ readonly key: readonly string[];
48
+ readonly indexes: readonly PhysicalIndex[];
49
+ }
50
+ interface QueryBound {
51
+ /** A prefix of the index's order columns (see `indexOrderColumns`). */
52
+ readonly values: readonly DatabaseKeyValue[];
53
+ readonly inclusive: boolean;
54
+ }
55
+ interface QueryRequest {
56
+ readonly index: string;
57
+ /** Values for the index's `eq` columns, in order. */
58
+ readonly eq: readonly DatabaseKeyValue[];
59
+ readonly lower?: QueryBound;
60
+ readonly upper?: QueryBound;
61
+ readonly order: "asc" | "desc";
62
+ /** 1–500 rows. */
63
+ readonly limit: number;
64
+ }
65
+ /** The row's `_v` at read time. */
66
+ interface WriteGuard {
67
+ readonly v: number;
68
+ }
69
+ /** At most one op per (table, key); guards see the pre-write state. */
70
+ type WriteOp = {
71
+ readonly type: "insert";
72
+ readonly table: PhysicalTable;
73
+ /** The complete row, `_v` included; the key must not exist. */
74
+ readonly row: StoredRow;
75
+ } | {
76
+ readonly type: "patch";
77
+ readonly table: PhysicalTable;
78
+ readonly key: DatabaseKey;
79
+ /** Named non-key columns only; the adapter sets `_v` to `guard.v + 1`. */
80
+ readonly set: StoredRow;
81
+ readonly guard: WriteGuard;
82
+ readonly previous: StoredRow;
83
+ } | {
84
+ readonly type: "delete";
85
+ readonly table: PhysicalTable;
86
+ readonly key: DatabaseKey;
87
+ readonly guard: WriteGuard;
88
+ readonly previous: StoredRow;
89
+ } | {
90
+ readonly type: "increment";
91
+ readonly table: PhysicalTable;
92
+ readonly key: DatabaseKey;
93
+ readonly by: Readonly<Record<string, number>>;
94
+ /** The row to create, before `by` applies, when the key does not exist. */
95
+ readonly init?: StoredRow;
96
+ readonly guard?: WriteGuard;
97
+ } | {
98
+ readonly type: "check";
99
+ readonly table: PhysicalTable;
100
+ readonly key: DatabaseKey;
101
+ readonly guard: WriteGuard;
102
+ };
103
+ type WriteResult = {
104
+ readonly ok: true;
105
+ } |
106
+ /** A guard failed, unique violations and missing rows included. */
107
+ {
108
+ readonly ok: false;
109
+ readonly failedOp: number;
110
+ } |
111
+ /** Transient: deadlock, serialization, SQLITE_BUSY, TransactionConflict, ABORTED. */
112
+ {
113
+ readonly ok: false;
114
+ readonly retry: true;
115
+ };
116
+ interface DatabaseAdapter {
117
+ readonly id: string;
118
+ /** True when the ops commit atomically in one write. */
119
+ fits(ops: readonly WriteOp[]): boolean;
120
+ /** Strongly consistent batch point read; results follow the order of keys. */
121
+ get(table: PhysicalTable, keys: readonly DatabaseKey[]): Promise<readonly (StoredRow | null)[]>;
122
+ /**
123
+ * Index range read; loops native pages until `limit` rows or the range ends, never a short page.
124
+ * A row read from a key-value index copy may lack `_v`; the engine reads it whole with `get` where it guards.
125
+ */
126
+ query(table: PhysicalTable, request: QueryRequest): Promise<readonly StoredRow[]>;
127
+ write(ops: readonly WriteOp[]): Promise<WriteResult>;
128
+ /** Called only by `@hot-updater/server/db` tooling. */
129
+ readonly migrations?: {
130
+ apply(tables: readonly PhysicalTable[]): Promise<void>;
131
+ };
132
+ dispose?(): Promise<void>;
133
+ }
134
+ declare const DATABASE_VERSION_COLUMN = "_v";
135
+ declare const DATABASE_MAX_QUERY_LIMIT = 500;
136
+ declare const DATABASE_MAX_MULTI_VALUES = 16;
137
+ declare class DatabaseSchemaError extends Error {
138
+ readonly name = "DatabaseSchemaError";
139
+ }
140
+ declare const findPhysicalColumn: (table: PhysicalTable, name: string) => PhysicalColumn;
141
+ declare const findPhysicalIndex: (table: PhysicalTable, name: string) => PhysicalIndex;
142
+ /**
143
+ * The columns that order an index read and close its cursors: the sort
144
+ * columns, then the key columns that neither `eq` nor `sort` fixes.
145
+ */
146
+ declare const indexOrderColumns: (table: PhysicalTable, index: PhysicalIndex) => readonly string[];
147
+ /** The primary key of a row, in key-column order. */
148
+ declare const rowKey: (table: PhysicalTable, row: StoredRow) => DatabaseKey;
149
+ //#endregion
150
+ export { DATABASE_MAX_MULTI_VALUES, DATABASE_MAX_QUERY_LIMIT, DATABASE_VERSION_COLUMN, DatabaseAdapter, DatabaseJson, DatabaseKey, DatabaseKeyValue, DatabaseSchemaError, DatabaseValue, PhysicalColumn, PhysicalColumnType, PhysicalIndex, PhysicalTable, QueryBound, QueryRequest, StoredRow, WriteGuard, WriteOp, WriteResult, findPhysicalColumn, findPhysicalIndex, indexOrderColumns, rowKey };