@hot-updater/cloudflare 0.32.0 → 0.33.1

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.
@@ -1,194 +0,0 @@
1
- import {
2
- type AppVersionGetBundlesArgs,
3
- type Bundle,
4
- DEFAULT_ROLLOUT_COHORT_COUNT,
5
- type FingerprintGetBundlesArgs,
6
- type GetBundlesArgs,
7
- NIL_UUID,
8
- } from "@hot-updater/core";
9
- import {
10
- filterCompatibleAppVersions,
11
- getUpdateInfo as getUpdateInfoJS,
12
- } from "@hot-updater/js";
13
-
14
- const parseTargetCohorts = (value: string | null): string[] | null => {
15
- if (!value) return null;
16
- try {
17
- const parsed = JSON.parse(value) as unknown;
18
- if (Array.isArray(parsed)) {
19
- return parsed.filter((v): v is string => typeof v === "string");
20
- }
21
- } catch {
22
- return null;
23
- }
24
- return null;
25
- };
26
-
27
- type BundleRow = {
28
- id: string;
29
- platform: Bundle["platform"];
30
- should_force_update: number;
31
- enabled: number;
32
- file_hash: string;
33
- git_commit_hash: string | null;
34
- message: string | null;
35
- channel: string;
36
- storage_uri: string;
37
- target_app_version: string | null;
38
- fingerprint_hash: string | null;
39
- rollout_cohort_count: number | null;
40
- target_cohorts: string | null;
41
- };
42
-
43
- const convertToBundle = (row: BundleRow): Bundle => ({
44
- id: row.id,
45
- platform: row.platform,
46
- shouldForceUpdate: Boolean(row.should_force_update),
47
- enabled: Boolean(row.enabled),
48
- fileHash: row.file_hash,
49
- gitCommitHash: row.git_commit_hash,
50
- message: row.message,
51
- channel: row.channel,
52
- storageUri: row.storage_uri,
53
- targetAppVersion: row.target_app_version,
54
- fingerprintHash: row.fingerprint_hash,
55
- rolloutCohortCount: row.rollout_cohort_count ?? DEFAULT_ROLLOUT_COHORT_COUNT,
56
- targetCohorts: parseTargetCohorts(row.target_cohorts),
57
- });
58
-
59
- const appVersionStrategy = async (
60
- DB: D1Database,
61
- {
62
- platform,
63
- appVersion,
64
- bundleId,
65
- minBundleId = NIL_UUID,
66
- channel = "production",
67
- cohort,
68
- }: AppVersionGetBundlesArgs,
69
- ) => {
70
- const appVersionList = await DB.prepare(/* sql */ `
71
- SELECT
72
- target_app_version
73
- FROM bundles
74
- WHERE platform = ?
75
- AND channel = ?
76
- AND enabled = 1
77
- AND id >= ?
78
- AND target_app_version IS NOT NULL
79
- GROUP BY target_app_version
80
- `)
81
- .bind(platform, channel, minBundleId)
82
- .all<{ target_app_version: string; count: number }>();
83
-
84
- const targetAppVersionList = filterCompatibleAppVersions(
85
- appVersionList.results.map((group) => group.target_app_version),
86
- appVersion,
87
- );
88
-
89
- if (targetAppVersionList.length === 0) {
90
- return getUpdateInfoJS([], {
91
- platform,
92
- appVersion,
93
- bundleId,
94
- minBundleId,
95
- channel,
96
- cohort,
97
- _updateStrategy: "appVersion",
98
- });
99
- }
100
-
101
- const placeholders = targetAppVersionList.map(() => "?").join(", ");
102
- const rows = await DB.prepare(/* sql */ `
103
- SELECT
104
- id,
105
- platform,
106
- should_force_update,
107
- enabled,
108
- file_hash,
109
- git_commit_hash,
110
- message,
111
- channel,
112
- storage_uri,
113
- target_app_version,
114
- fingerprint_hash,
115
- rollout_cohort_count,
116
- target_cohorts
117
- FROM bundles
118
- WHERE enabled = 1
119
- AND platform = ?
120
- AND id >= ?
121
- AND channel = ?
122
- AND target_app_version IN (${placeholders})
123
- `)
124
- .bind(platform, minBundleId, channel, ...targetAppVersionList)
125
- .all<BundleRow>();
126
-
127
- return getUpdateInfoJS(rows.results.map(convertToBundle), {
128
- platform,
129
- appVersion,
130
- bundleId,
131
- minBundleId,
132
- channel,
133
- cohort,
134
- _updateStrategy: "appVersion",
135
- });
136
- };
137
-
138
- export const fingerprintStrategy = async (
139
- DB: D1Database,
140
- {
141
- platform,
142
- fingerprintHash,
143
- bundleId,
144
- minBundleId = NIL_UUID,
145
- channel = "production",
146
- cohort,
147
- }: FingerprintGetBundlesArgs,
148
- ) => {
149
- const rows = await DB.prepare(/* sql */ `
150
- SELECT
151
- id,
152
- platform,
153
- should_force_update,
154
- enabled,
155
- file_hash,
156
- git_commit_hash,
157
- message,
158
- channel,
159
- storage_uri,
160
- target_app_version,
161
- fingerprint_hash,
162
- rollout_cohort_count,
163
- target_cohorts
164
- FROM bundles
165
- WHERE enabled = 1
166
- AND platform = ?
167
- AND id >= ?
168
- AND channel = ?
169
- AND fingerprint_hash = ?
170
- `)
171
- .bind(platform, minBundleId, channel, fingerprintHash)
172
- .all<BundleRow>();
173
-
174
- return getUpdateInfoJS(rows.results.map(convertToBundle), {
175
- platform,
176
- fingerprintHash,
177
- bundleId,
178
- minBundleId,
179
- channel,
180
- cohort,
181
- _updateStrategy: "fingerprint",
182
- });
183
- };
184
-
185
- export const getUpdateInfo = (DB: D1Database, args: GetBundlesArgs) => {
186
- switch (args._updateStrategy) {
187
- case "appVersion":
188
- return appVersionStrategy(DB, args);
189
- case "fingerprint":
190
- return fingerprintStrategy(DB, args);
191
- default:
192
- return null;
193
- }
194
- };