@hot-updater/server 0.21.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/LICENSE +21 -0
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/adapters/drizzle.cjs +9 -0
- package/dist/adapters/drizzle.d.cts +1 -0
- package/dist/adapters/drizzle.d.ts +1 -0
- package/dist/adapters/drizzle.js +3 -0
- package/dist/adapters/kysely.cjs +9 -0
- package/dist/adapters/kysely.d.cts +1 -0
- package/dist/adapters/kysely.d.ts +1 -0
- package/dist/adapters/kysely.js +3 -0
- package/dist/adapters/mongodb.cjs +9 -0
- package/dist/adapters/mongodb.d.cts +1 -0
- package/dist/adapters/mongodb.d.ts +1 -0
- package/dist/adapters/mongodb.js +3 -0
- package/dist/adapters/prisma.cjs +9 -0
- package/dist/adapters/prisma.d.cts +1 -0
- package/dist/adapters/prisma.d.ts +1 -0
- package/dist/adapters/prisma.js +3 -0
- package/dist/adapters/typeorm.cjs +9 -0
- package/dist/adapters/typeorm.d.cts +1 -0
- package/dist/adapters/typeorm.d.ts +1 -0
- package/dist/adapters/typeorm.js +3 -0
- package/dist/calculatePagination.cjs +27 -0
- package/dist/calculatePagination.js +26 -0
- package/dist/db/index.cjs +289 -0
- package/dist/db/index.d.cts +62 -0
- package/dist/db/index.d.ts +62 -0
- package/dist/db/index.js +284 -0
- package/dist/handler.cjs +141 -0
- package/dist/handler.d.cts +37 -0
- package/dist/handler.d.ts +37 -0
- package/dist/handler.js +140 -0
- package/dist/index.cjs +6 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/schema/v1.cjs +26 -0
- package/dist/schema/v1.js +24 -0
- package/dist/types/index.d.cts +20 -0
- package/dist/types/index.d.ts +20 -0
- package/package.json +73 -0
- package/src/adapters/drizzle.ts +1 -0
- package/src/adapters/kysely.ts +1 -0
- package/src/adapters/mongodb.ts +1 -0
- package/src/adapters/prisma.ts +1 -0
- package/src/adapters/typeorm.ts +1 -0
- package/src/calculatePagination.ts +34 -0
- package/src/db/index.spec.ts +231 -0
- package/src/db/index.ts +490 -0
- package/src/handler-standalone-integration.spec.ts +341 -0
- package/src/handler.ts +231 -0
- package/src/index.ts +3 -0
- package/src/schema/v1.ts +22 -0
- package/src/types/index.ts +21 -0
package/dist/db/index.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { calculatePagination } from "../calculatePagination.js";
|
|
2
|
+
import { createHandler } from "../handler.js";
|
|
3
|
+
import { v1 } from "../schema/v1.js";
|
|
4
|
+
import { NIL_UUID } from "@hot-updater/core";
|
|
5
|
+
import { filterCompatibleAppVersions } from "@hot-updater/plugin-core";
|
|
6
|
+
import { fumadb } from "fumadb";
|
|
7
|
+
|
|
8
|
+
//#region src/db/index.ts
|
|
9
|
+
const HotUpdaterDB = fumadb({
|
|
10
|
+
namespace: "hot-updater",
|
|
11
|
+
schemas: [v1]
|
|
12
|
+
});
|
|
13
|
+
function hotUpdater(options) {
|
|
14
|
+
const client = HotUpdaterDB.client(options.database);
|
|
15
|
+
const cwd = options.cwd ?? process.cwd();
|
|
16
|
+
const storagePlugins = (options?.storagePlugins ?? []).map((plugin) => typeof plugin === "function" ? plugin({ cwd }) : plugin);
|
|
17
|
+
const resolveFileUrl = async (storageUri) => {
|
|
18
|
+
if (!storageUri) return null;
|
|
19
|
+
const protocol = new URL(storageUri).protocol.replace(":", "");
|
|
20
|
+
if (protocol === "http" || protocol === "https") return storageUri;
|
|
21
|
+
const plugin = storagePlugins.find((p) => p.supportedProtocol === protocol);
|
|
22
|
+
if (!plugin) throw new Error(`No storage plugin for protocol: ${protocol}`);
|
|
23
|
+
const { fileUrl } = await plugin.getDownloadUrl(storageUri);
|
|
24
|
+
if (!fileUrl) throw new Error("Storage plugin returned empty fileUrl");
|
|
25
|
+
return fileUrl;
|
|
26
|
+
};
|
|
27
|
+
const api = {
|
|
28
|
+
async getBundleById(id) {
|
|
29
|
+
const version = await client.version();
|
|
30
|
+
const result = await client.orm(version).findFirst("bundles", {
|
|
31
|
+
select: [
|
|
32
|
+
"id",
|
|
33
|
+
"platform",
|
|
34
|
+
"should_force_update",
|
|
35
|
+
"enabled",
|
|
36
|
+
"file_hash",
|
|
37
|
+
"git_commit_hash",
|
|
38
|
+
"message",
|
|
39
|
+
"channel",
|
|
40
|
+
"storage_uri",
|
|
41
|
+
"target_app_version",
|
|
42
|
+
"fingerprint_hash",
|
|
43
|
+
"metadata"
|
|
44
|
+
],
|
|
45
|
+
where: (b) => b.and(b.isNotNull("id"), b("id", "=", id))
|
|
46
|
+
});
|
|
47
|
+
if (!result) return null;
|
|
48
|
+
return {
|
|
49
|
+
id: result.id,
|
|
50
|
+
platform: result.platform,
|
|
51
|
+
shouldForceUpdate: Boolean(result.should_force_update),
|
|
52
|
+
enabled: Boolean(result.enabled),
|
|
53
|
+
fileHash: result.file_hash,
|
|
54
|
+
gitCommitHash: result.git_commit_hash ?? null,
|
|
55
|
+
message: result.message ?? null,
|
|
56
|
+
channel: result.channel,
|
|
57
|
+
storageUri: result.storage_uri,
|
|
58
|
+
targetAppVersion: result.target_app_version ?? null,
|
|
59
|
+
fingerprintHash: result.fingerprint_hash ?? null
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
async getUpdateInfo(args) {
|
|
63
|
+
const version = await client.version();
|
|
64
|
+
const orm = client.orm(version);
|
|
65
|
+
const toUpdateInfo = (row, status) => ({
|
|
66
|
+
id: row.id,
|
|
67
|
+
shouldForceUpdate: status === "ROLLBACK" ? true : Boolean(row.should_force_update),
|
|
68
|
+
message: row.message ?? null,
|
|
69
|
+
status,
|
|
70
|
+
storageUri: row.storage_uri ?? null,
|
|
71
|
+
fileHash: row.file_hash ?? null
|
|
72
|
+
});
|
|
73
|
+
const INIT_BUNDLE_ROLLBACK_UPDATE_INFO = {
|
|
74
|
+
id: NIL_UUID,
|
|
75
|
+
message: null,
|
|
76
|
+
shouldForceUpdate: true,
|
|
77
|
+
status: "ROLLBACK",
|
|
78
|
+
storageUri: null,
|
|
79
|
+
fileHash: null
|
|
80
|
+
};
|
|
81
|
+
const appVersionStrategy = async ({ platform, appVersion, bundleId, minBundleId = NIL_UUID, channel = "production" }) => {
|
|
82
|
+
const versionRows = await orm.findMany("bundles", {
|
|
83
|
+
select: ["target_app_version"],
|
|
84
|
+
where: (b) => b.and(b("platform", "=", platform))
|
|
85
|
+
});
|
|
86
|
+
const compatibleVersions = filterCompatibleAppVersions(Array.from(new Set((versionRows ?? []).map((r) => r.target_app_version).filter((v) => Boolean(v)))), appVersion);
|
|
87
|
+
const candidates = ((compatibleVersions.length === 0 ? [] : await orm.findMany("bundles", {
|
|
88
|
+
select: [
|
|
89
|
+
"id",
|
|
90
|
+
"should_force_update",
|
|
91
|
+
"message",
|
|
92
|
+
"storage_uri",
|
|
93
|
+
"file_hash",
|
|
94
|
+
"channel",
|
|
95
|
+
"target_app_version",
|
|
96
|
+
"enabled"
|
|
97
|
+
],
|
|
98
|
+
where: (b) => b.and(b("enabled", "=", true), b("platform", "=", platform), b("id", ">=", minBundleId ?? NIL_UUID), b("channel", "=", channel), b.isNotNull("target_app_version"))
|
|
99
|
+
})) ?? []).filter((r) => r.target_app_version ? compatibleVersions.includes(r.target_app_version) : false);
|
|
100
|
+
const byIdDesc = (a, b) => b.id.localeCompare(a.id);
|
|
101
|
+
const sorted = (candidates ?? []).slice().sort(byIdDesc);
|
|
102
|
+
const latestCandidate = sorted[0] ?? null;
|
|
103
|
+
const currentBundle = sorted.find((b) => b.id === bundleId);
|
|
104
|
+
const updateCandidate = sorted.find((b) => b.id.localeCompare(bundleId) > 0) ?? null;
|
|
105
|
+
const rollbackCandidate = sorted.find((b) => b.id.localeCompare(bundleId) < 0) ?? null;
|
|
106
|
+
if (bundleId === NIL_UUID) {
|
|
107
|
+
if (latestCandidate && latestCandidate.id !== bundleId) return toUpdateInfo(latestCandidate, "UPDATE");
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
if (currentBundle) {
|
|
111
|
+
if (latestCandidate && latestCandidate.id.localeCompare(currentBundle.id) > 0) return toUpdateInfo(latestCandidate, "UPDATE");
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
if (updateCandidate) return toUpdateInfo(updateCandidate, "UPDATE");
|
|
115
|
+
if (rollbackCandidate) return toUpdateInfo(rollbackCandidate, "ROLLBACK");
|
|
116
|
+
if (minBundleId && bundleId.localeCompare(minBundleId) <= 0) return null;
|
|
117
|
+
return INIT_BUNDLE_ROLLBACK_UPDATE_INFO;
|
|
118
|
+
};
|
|
119
|
+
const fingerprintStrategy = async ({ platform, fingerprintHash, bundleId, minBundleId = NIL_UUID, channel = "production" }) => {
|
|
120
|
+
const candidates = await orm.findMany("bundles", {
|
|
121
|
+
select: [
|
|
122
|
+
"id",
|
|
123
|
+
"should_force_update",
|
|
124
|
+
"message",
|
|
125
|
+
"storage_uri",
|
|
126
|
+
"file_hash",
|
|
127
|
+
"channel",
|
|
128
|
+
"fingerprint_hash",
|
|
129
|
+
"enabled"
|
|
130
|
+
],
|
|
131
|
+
where: (b) => b.and(b("enabled", "=", true), b("platform", "=", platform), b("id", ">=", minBundleId ?? NIL_UUID), b("channel", "=", channel), b("fingerprint_hash", "=", fingerprintHash))
|
|
132
|
+
});
|
|
133
|
+
const byIdDesc = (a, b) => b.id.localeCompare(a.id);
|
|
134
|
+
const sorted = (candidates ?? []).slice().sort(byIdDesc);
|
|
135
|
+
const latestCandidate = sorted[0] ?? null;
|
|
136
|
+
const currentBundle = sorted.find((b) => b.id === bundleId);
|
|
137
|
+
const updateCandidate = sorted.find((b) => b.id.localeCompare(bundleId) > 0) ?? null;
|
|
138
|
+
const rollbackCandidate = sorted.find((b) => b.id.localeCompare(bundleId) < 0) ?? null;
|
|
139
|
+
if (bundleId === NIL_UUID) {
|
|
140
|
+
if (latestCandidate && latestCandidate.id !== bundleId) return toUpdateInfo(latestCandidate, "UPDATE");
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
if (currentBundle) {
|
|
144
|
+
if (latestCandidate && latestCandidate.id.localeCompare(currentBundle.id) > 0) return toUpdateInfo(latestCandidate, "UPDATE");
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
if (updateCandidate) return toUpdateInfo(updateCandidate, "UPDATE");
|
|
148
|
+
if (rollbackCandidate) return toUpdateInfo(rollbackCandidate, "ROLLBACK");
|
|
149
|
+
if (minBundleId && bundleId.localeCompare(minBundleId) <= 0) return null;
|
|
150
|
+
return INIT_BUNDLE_ROLLBACK_UPDATE_INFO;
|
|
151
|
+
};
|
|
152
|
+
if (args._updateStrategy === "appVersion") return appVersionStrategy(args);
|
|
153
|
+
if (args._updateStrategy === "fingerprint") return fingerprintStrategy(args);
|
|
154
|
+
return null;
|
|
155
|
+
},
|
|
156
|
+
async getAppUpdateInfo(args) {
|
|
157
|
+
const info = await this.getUpdateInfo(args);
|
|
158
|
+
if (!info) return null;
|
|
159
|
+
const { storageUri,...rest } = info;
|
|
160
|
+
const fileUrl = await resolveFileUrl(storageUri ?? null);
|
|
161
|
+
return {
|
|
162
|
+
...rest,
|
|
163
|
+
fileUrl
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
async getChannels() {
|
|
167
|
+
const version = await client.version();
|
|
168
|
+
const rows = await client.orm(version).findMany("bundles", {
|
|
169
|
+
select: ["channel"],
|
|
170
|
+
where: (b) => b.isNotNull("channel")
|
|
171
|
+
});
|
|
172
|
+
const set = new Set(rows?.map((r) => r.channel) ?? []);
|
|
173
|
+
return Array.from(set);
|
|
174
|
+
},
|
|
175
|
+
async getBundles(options$1) {
|
|
176
|
+
const version = await client.version();
|
|
177
|
+
const orm = client.orm(version);
|
|
178
|
+
const { where, limit, offset } = options$1;
|
|
179
|
+
const all = (await orm.findMany("bundles", {
|
|
180
|
+
select: [
|
|
181
|
+
"id",
|
|
182
|
+
"platform",
|
|
183
|
+
"should_force_update",
|
|
184
|
+
"enabled",
|
|
185
|
+
"file_hash",
|
|
186
|
+
"git_commit_hash",
|
|
187
|
+
"message",
|
|
188
|
+
"channel",
|
|
189
|
+
"storage_uri",
|
|
190
|
+
"target_app_version",
|
|
191
|
+
"fingerprint_hash",
|
|
192
|
+
"metadata"
|
|
193
|
+
],
|
|
194
|
+
where: (b) => b.and(b.isNotNull("id"), where?.channel ? b("channel", "=", where.channel) : b.isNotNull("id"), where?.platform ? b("platform", "=", where.platform) : b.isNotNull("id"))
|
|
195
|
+
})).map((r) => ({
|
|
196
|
+
id: r.id,
|
|
197
|
+
platform: r.platform,
|
|
198
|
+
shouldForceUpdate: Boolean(r.should_force_update),
|
|
199
|
+
enabled: Boolean(r.enabled),
|
|
200
|
+
fileHash: r.file_hash,
|
|
201
|
+
gitCommitHash: r.git_commit_hash ?? null,
|
|
202
|
+
message: r.message ?? null,
|
|
203
|
+
channel: r.channel,
|
|
204
|
+
storageUri: r.storage_uri,
|
|
205
|
+
targetAppVersion: r.target_app_version ?? null,
|
|
206
|
+
fingerprintHash: r.fingerprint_hash ?? null
|
|
207
|
+
})).sort((a, b) => b.id.localeCompare(a.id));
|
|
208
|
+
const total = all.length;
|
|
209
|
+
return {
|
|
210
|
+
data: all.slice(offset, offset + limit),
|
|
211
|
+
pagination: calculatePagination(total, {
|
|
212
|
+
limit,
|
|
213
|
+
offset
|
|
214
|
+
})
|
|
215
|
+
};
|
|
216
|
+
},
|
|
217
|
+
async insertBundle(bundle) {
|
|
218
|
+
const version = await client.version();
|
|
219
|
+
const orm = client.orm(version);
|
|
220
|
+
const values = {
|
|
221
|
+
id: bundle.id,
|
|
222
|
+
platform: bundle.platform,
|
|
223
|
+
should_force_update: bundle.shouldForceUpdate,
|
|
224
|
+
enabled: bundle.enabled,
|
|
225
|
+
file_hash: bundle.fileHash,
|
|
226
|
+
git_commit_hash: bundle.gitCommitHash,
|
|
227
|
+
message: bundle.message,
|
|
228
|
+
channel: bundle.channel,
|
|
229
|
+
storage_uri: bundle.storageUri,
|
|
230
|
+
target_app_version: bundle.targetAppVersion,
|
|
231
|
+
fingerprint_hash: bundle.fingerprintHash,
|
|
232
|
+
metadata: bundle.metadata ?? {}
|
|
233
|
+
};
|
|
234
|
+
const { id,...updateValues } = values;
|
|
235
|
+
await orm.upsert("bundles", {
|
|
236
|
+
where: (b) => b("id", "=", id),
|
|
237
|
+
create: values,
|
|
238
|
+
update: updateValues
|
|
239
|
+
});
|
|
240
|
+
},
|
|
241
|
+
async updateBundleById(bundleId, newBundle) {
|
|
242
|
+
const version = await client.version();
|
|
243
|
+
const orm = client.orm(version);
|
|
244
|
+
const current = await this.getBundleById(bundleId);
|
|
245
|
+
if (!current) throw new Error("targetBundleId not found");
|
|
246
|
+
const merged = {
|
|
247
|
+
...current,
|
|
248
|
+
...newBundle
|
|
249
|
+
};
|
|
250
|
+
const values = {
|
|
251
|
+
id: merged.id,
|
|
252
|
+
platform: merged.platform,
|
|
253
|
+
should_force_update: merged.shouldForceUpdate,
|
|
254
|
+
enabled: merged.enabled,
|
|
255
|
+
file_hash: merged.fileHash,
|
|
256
|
+
git_commit_hash: merged.gitCommitHash,
|
|
257
|
+
message: merged.message,
|
|
258
|
+
channel: merged.channel,
|
|
259
|
+
storage_uri: merged.storageUri,
|
|
260
|
+
target_app_version: merged.targetAppVersion,
|
|
261
|
+
fingerprint_hash: merged.fingerprintHash,
|
|
262
|
+
metadata: merged.metadata ?? {}
|
|
263
|
+
};
|
|
264
|
+
const { id: id2,...updateValues2 } = values;
|
|
265
|
+
await orm.upsert("bundles", {
|
|
266
|
+
where: (b) => b("id", "=", id2),
|
|
267
|
+
create: values,
|
|
268
|
+
update: updateValues2
|
|
269
|
+
});
|
|
270
|
+
},
|
|
271
|
+
async deleteBundleById(bundleId) {
|
|
272
|
+
const version = await client.version();
|
|
273
|
+
await client.orm(version).deleteMany("bundles", { where: (b) => b("id", "=", bundleId) });
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
return {
|
|
277
|
+
...api,
|
|
278
|
+
handler: createHandler(api, options?.basePath ? { basePath: options.basePath } : {}),
|
|
279
|
+
createMigrator: () => client.createMigrator()
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
//#endregion
|
|
284
|
+
export { HotUpdaterDB, hotUpdater };
|
package/dist/handler.cjs
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/handler.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a Web Standard Request handler for Hot Updater API
|
|
5
|
+
* This handler is framework-agnostic and works with any framework
|
|
6
|
+
* that supports Web Standard Request/Response (Hono, Elysia, etc.)
|
|
7
|
+
*/
|
|
8
|
+
function createHandler(api, options = {}) {
|
|
9
|
+
const basePath = options.basePath ?? "/api";
|
|
10
|
+
return async (request) => {
|
|
11
|
+
try {
|
|
12
|
+
const url = new URL(request.url);
|
|
13
|
+
const path = url.pathname;
|
|
14
|
+
const method = request.method;
|
|
15
|
+
const routePath = path.startsWith(basePath) ? path.slice(basePath.length) : path;
|
|
16
|
+
if (routePath === "/update" && method === "POST") {
|
|
17
|
+
const body = await request.json();
|
|
18
|
+
const updateInfo = await api.getAppUpdateInfo(body);
|
|
19
|
+
if (!updateInfo) return new Response(JSON.stringify(null), {
|
|
20
|
+
status: 200,
|
|
21
|
+
headers: { "Content-Type": "application/json" }
|
|
22
|
+
});
|
|
23
|
+
return new Response(JSON.stringify(updateInfo), {
|
|
24
|
+
status: 200,
|
|
25
|
+
headers: { "Content-Type": "application/json" }
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const fingerprintMatch = routePath.match(/^\/fingerprint\/([^/]+)\/([^/]+)\/([^/]+)\/([^/]+)\/([^/]+)$/);
|
|
29
|
+
if (fingerprintMatch && method === "GET") {
|
|
30
|
+
const [, platform, fingerprintHash, channel, minBundleId, bundleId] = fingerprintMatch;
|
|
31
|
+
const updateInfo = await api.getAppUpdateInfo({
|
|
32
|
+
_updateStrategy: "fingerprint",
|
|
33
|
+
platform,
|
|
34
|
+
fingerprintHash,
|
|
35
|
+
channel,
|
|
36
|
+
minBundleId,
|
|
37
|
+
bundleId
|
|
38
|
+
});
|
|
39
|
+
if (!updateInfo) return new Response(JSON.stringify(null), {
|
|
40
|
+
status: 200,
|
|
41
|
+
headers: { "Content-Type": "application/json" }
|
|
42
|
+
});
|
|
43
|
+
return new Response(JSON.stringify(updateInfo), {
|
|
44
|
+
status: 200,
|
|
45
|
+
headers: { "Content-Type": "application/json" }
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const appVersionMatch = routePath.match(/^\/app-version\/([^/]+)\/([^/]+)\/([^/]+)\/([^/]+)\/([^/]+)$/);
|
|
49
|
+
if (appVersionMatch && method === "GET") {
|
|
50
|
+
const [, platform, appVersion, channel, minBundleId, bundleId] = appVersionMatch;
|
|
51
|
+
const updateInfo = await api.getAppUpdateInfo({
|
|
52
|
+
_updateStrategy: "appVersion",
|
|
53
|
+
platform,
|
|
54
|
+
appVersion,
|
|
55
|
+
channel,
|
|
56
|
+
minBundleId,
|
|
57
|
+
bundleId
|
|
58
|
+
});
|
|
59
|
+
if (!updateInfo) return new Response(JSON.stringify(null), {
|
|
60
|
+
status: 200,
|
|
61
|
+
headers: { "Content-Type": "application/json" }
|
|
62
|
+
});
|
|
63
|
+
return new Response(JSON.stringify(updateInfo), {
|
|
64
|
+
status: 200,
|
|
65
|
+
headers: { "Content-Type": "application/json" }
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const getBundleMatch = routePath.match(/^\/bundles\/([^/]+)$/);
|
|
69
|
+
if (getBundleMatch && method === "GET") {
|
|
70
|
+
const id = getBundleMatch[1];
|
|
71
|
+
const bundle = await api.getBundleById(id);
|
|
72
|
+
if (!bundle) return new Response(JSON.stringify({ error: "Bundle not found" }), {
|
|
73
|
+
status: 404,
|
|
74
|
+
headers: { "Content-Type": "application/json" }
|
|
75
|
+
});
|
|
76
|
+
return new Response(JSON.stringify(bundle), {
|
|
77
|
+
status: 200,
|
|
78
|
+
headers: { "Content-Type": "application/json" }
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (routePath === "/bundles" && method === "GET") {
|
|
82
|
+
const channel = url.searchParams.get("channel") ?? void 0;
|
|
83
|
+
const platform = url.searchParams.get("platform") ?? void 0;
|
|
84
|
+
const limit = Number(url.searchParams.get("limit")) || 50;
|
|
85
|
+
const offset = Number(url.searchParams.get("offset")) || 0;
|
|
86
|
+
const result = await api.getBundles({
|
|
87
|
+
where: {
|
|
88
|
+
...channel && { channel },
|
|
89
|
+
...platform && { platform }
|
|
90
|
+
},
|
|
91
|
+
limit,
|
|
92
|
+
offset
|
|
93
|
+
});
|
|
94
|
+
return new Response(JSON.stringify(result.data), {
|
|
95
|
+
status: 200,
|
|
96
|
+
headers: { "Content-Type": "application/json" }
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (routePath === "/bundles" && method === "POST") {
|
|
100
|
+
const body = await request.json();
|
|
101
|
+
const bundles = Array.isArray(body) ? body : [body];
|
|
102
|
+
for (const bundle of bundles) await api.insertBundle(bundle);
|
|
103
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
104
|
+
status: 201,
|
|
105
|
+
headers: { "Content-Type": "application/json" }
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (routePath.startsWith("/bundles/") && method === "DELETE") {
|
|
109
|
+
const id = routePath.slice(9);
|
|
110
|
+
await api.deleteBundleById(id);
|
|
111
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
112
|
+
status: 200,
|
|
113
|
+
headers: { "Content-Type": "application/json" }
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (routePath === "/channels" && method === "GET") {
|
|
117
|
+
const channels = await api.getChannels();
|
|
118
|
+
return new Response(JSON.stringify({ channels }), {
|
|
119
|
+
status: 200,
|
|
120
|
+
headers: { "Content-Type": "application/json" }
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return new Response(JSON.stringify({ error: "Not found" }), {
|
|
124
|
+
status: 404,
|
|
125
|
+
headers: { "Content-Type": "application/json" }
|
|
126
|
+
});
|
|
127
|
+
} catch (error) {
|
|
128
|
+
console.error("Hot Updater handler error:", error);
|
|
129
|
+
return new Response(JSON.stringify({
|
|
130
|
+
error: "Internal server error",
|
|
131
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
132
|
+
}), {
|
|
133
|
+
status: 500,
|
|
134
|
+
headers: { "Content-Type": "application/json" }
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
//#endregion
|
|
141
|
+
exports.createHandler = createHandler;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { PaginationInfo } from "./types/index.cjs";
|
|
2
|
+
import { AppUpdateInfo, AppVersionGetBundlesArgs, Bundle, FingerprintGetBundlesArgs } from "@hot-updater/core";
|
|
3
|
+
|
|
4
|
+
//#region src/handler.d.ts
|
|
5
|
+
interface HandlerAPI {
|
|
6
|
+
getAppUpdateInfo: (args: AppVersionGetBundlesArgs | FingerprintGetBundlesArgs) => Promise<AppUpdateInfo | null>;
|
|
7
|
+
getBundleById: (id: string) => Promise<Bundle | null>;
|
|
8
|
+
getBundles: (options: {
|
|
9
|
+
where?: {
|
|
10
|
+
channel?: string;
|
|
11
|
+
platform?: string;
|
|
12
|
+
};
|
|
13
|
+
limit: number;
|
|
14
|
+
offset: number;
|
|
15
|
+
}) => Promise<{
|
|
16
|
+
data: Bundle[];
|
|
17
|
+
pagination: PaginationInfo;
|
|
18
|
+
}>;
|
|
19
|
+
insertBundle: (bundle: Bundle) => Promise<void>;
|
|
20
|
+
deleteBundleById: (bundleId: string) => Promise<void>;
|
|
21
|
+
getChannels: () => Promise<string[]>;
|
|
22
|
+
}
|
|
23
|
+
interface HandlerOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Base path for all routes
|
|
26
|
+
* @default "/api"
|
|
27
|
+
*/
|
|
28
|
+
basePath?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates a Web Standard Request handler for Hot Updater API
|
|
32
|
+
* This handler is framework-agnostic and works with any framework
|
|
33
|
+
* that supports Web Standard Request/Response (Hono, Elysia, etc.)
|
|
34
|
+
*/
|
|
35
|
+
declare function createHandler(api: HandlerAPI, options?: HandlerOptions): (request: Request) => Promise<Response>;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { HandlerAPI, HandlerOptions, createHandler };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { PaginationInfo } from "./types/index.js";
|
|
2
|
+
import { AppUpdateInfo, AppVersionGetBundlesArgs, Bundle, FingerprintGetBundlesArgs } from "@hot-updater/core";
|
|
3
|
+
|
|
4
|
+
//#region src/handler.d.ts
|
|
5
|
+
interface HandlerAPI {
|
|
6
|
+
getAppUpdateInfo: (args: AppVersionGetBundlesArgs | FingerprintGetBundlesArgs) => Promise<AppUpdateInfo | null>;
|
|
7
|
+
getBundleById: (id: string) => Promise<Bundle | null>;
|
|
8
|
+
getBundles: (options: {
|
|
9
|
+
where?: {
|
|
10
|
+
channel?: string;
|
|
11
|
+
platform?: string;
|
|
12
|
+
};
|
|
13
|
+
limit: number;
|
|
14
|
+
offset: number;
|
|
15
|
+
}) => Promise<{
|
|
16
|
+
data: Bundle[];
|
|
17
|
+
pagination: PaginationInfo;
|
|
18
|
+
}>;
|
|
19
|
+
insertBundle: (bundle: Bundle) => Promise<void>;
|
|
20
|
+
deleteBundleById: (bundleId: string) => Promise<void>;
|
|
21
|
+
getChannels: () => Promise<string[]>;
|
|
22
|
+
}
|
|
23
|
+
interface HandlerOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Base path for all routes
|
|
26
|
+
* @default "/api"
|
|
27
|
+
*/
|
|
28
|
+
basePath?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates a Web Standard Request handler for Hot Updater API
|
|
32
|
+
* This handler is framework-agnostic and works with any framework
|
|
33
|
+
* that supports Web Standard Request/Response (Hono, Elysia, etc.)
|
|
34
|
+
*/
|
|
35
|
+
declare function createHandler(api: HandlerAPI, options?: HandlerOptions): (request: Request) => Promise<Response>;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { HandlerAPI, HandlerOptions, createHandler };
|
package/dist/handler.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
//#region src/handler.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Web Standard Request handler for Hot Updater API
|
|
4
|
+
* This handler is framework-agnostic and works with any framework
|
|
5
|
+
* that supports Web Standard Request/Response (Hono, Elysia, etc.)
|
|
6
|
+
*/
|
|
7
|
+
function createHandler(api, options = {}) {
|
|
8
|
+
const basePath = options.basePath ?? "/api";
|
|
9
|
+
return async (request) => {
|
|
10
|
+
try {
|
|
11
|
+
const url = new URL(request.url);
|
|
12
|
+
const path = url.pathname;
|
|
13
|
+
const method = request.method;
|
|
14
|
+
const routePath = path.startsWith(basePath) ? path.slice(basePath.length) : path;
|
|
15
|
+
if (routePath === "/update" && method === "POST") {
|
|
16
|
+
const body = await request.json();
|
|
17
|
+
const updateInfo = await api.getAppUpdateInfo(body);
|
|
18
|
+
if (!updateInfo) return new Response(JSON.stringify(null), {
|
|
19
|
+
status: 200,
|
|
20
|
+
headers: { "Content-Type": "application/json" }
|
|
21
|
+
});
|
|
22
|
+
return new Response(JSON.stringify(updateInfo), {
|
|
23
|
+
status: 200,
|
|
24
|
+
headers: { "Content-Type": "application/json" }
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
const fingerprintMatch = routePath.match(/^\/fingerprint\/([^/]+)\/([^/]+)\/([^/]+)\/([^/]+)\/([^/]+)$/);
|
|
28
|
+
if (fingerprintMatch && method === "GET") {
|
|
29
|
+
const [, platform, fingerprintHash, channel, minBundleId, bundleId] = fingerprintMatch;
|
|
30
|
+
const updateInfo = await api.getAppUpdateInfo({
|
|
31
|
+
_updateStrategy: "fingerprint",
|
|
32
|
+
platform,
|
|
33
|
+
fingerprintHash,
|
|
34
|
+
channel,
|
|
35
|
+
minBundleId,
|
|
36
|
+
bundleId
|
|
37
|
+
});
|
|
38
|
+
if (!updateInfo) return new Response(JSON.stringify(null), {
|
|
39
|
+
status: 200,
|
|
40
|
+
headers: { "Content-Type": "application/json" }
|
|
41
|
+
});
|
|
42
|
+
return new Response(JSON.stringify(updateInfo), {
|
|
43
|
+
status: 200,
|
|
44
|
+
headers: { "Content-Type": "application/json" }
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const appVersionMatch = routePath.match(/^\/app-version\/([^/]+)\/([^/]+)\/([^/]+)\/([^/]+)\/([^/]+)$/);
|
|
48
|
+
if (appVersionMatch && method === "GET") {
|
|
49
|
+
const [, platform, appVersion, channel, minBundleId, bundleId] = appVersionMatch;
|
|
50
|
+
const updateInfo = await api.getAppUpdateInfo({
|
|
51
|
+
_updateStrategy: "appVersion",
|
|
52
|
+
platform,
|
|
53
|
+
appVersion,
|
|
54
|
+
channel,
|
|
55
|
+
minBundleId,
|
|
56
|
+
bundleId
|
|
57
|
+
});
|
|
58
|
+
if (!updateInfo) return new Response(JSON.stringify(null), {
|
|
59
|
+
status: 200,
|
|
60
|
+
headers: { "Content-Type": "application/json" }
|
|
61
|
+
});
|
|
62
|
+
return new Response(JSON.stringify(updateInfo), {
|
|
63
|
+
status: 200,
|
|
64
|
+
headers: { "Content-Type": "application/json" }
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const getBundleMatch = routePath.match(/^\/bundles\/([^/]+)$/);
|
|
68
|
+
if (getBundleMatch && method === "GET") {
|
|
69
|
+
const id = getBundleMatch[1];
|
|
70
|
+
const bundle = await api.getBundleById(id);
|
|
71
|
+
if (!bundle) return new Response(JSON.stringify({ error: "Bundle not found" }), {
|
|
72
|
+
status: 404,
|
|
73
|
+
headers: { "Content-Type": "application/json" }
|
|
74
|
+
});
|
|
75
|
+
return new Response(JSON.stringify(bundle), {
|
|
76
|
+
status: 200,
|
|
77
|
+
headers: { "Content-Type": "application/json" }
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
if (routePath === "/bundles" && method === "GET") {
|
|
81
|
+
const channel = url.searchParams.get("channel") ?? void 0;
|
|
82
|
+
const platform = url.searchParams.get("platform") ?? void 0;
|
|
83
|
+
const limit = Number(url.searchParams.get("limit")) || 50;
|
|
84
|
+
const offset = Number(url.searchParams.get("offset")) || 0;
|
|
85
|
+
const result = await api.getBundles({
|
|
86
|
+
where: {
|
|
87
|
+
...channel && { channel },
|
|
88
|
+
...platform && { platform }
|
|
89
|
+
},
|
|
90
|
+
limit,
|
|
91
|
+
offset
|
|
92
|
+
});
|
|
93
|
+
return new Response(JSON.stringify(result.data), {
|
|
94
|
+
status: 200,
|
|
95
|
+
headers: { "Content-Type": "application/json" }
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (routePath === "/bundles" && method === "POST") {
|
|
99
|
+
const body = await request.json();
|
|
100
|
+
const bundles = Array.isArray(body) ? body : [body];
|
|
101
|
+
for (const bundle of bundles) await api.insertBundle(bundle);
|
|
102
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
103
|
+
status: 201,
|
|
104
|
+
headers: { "Content-Type": "application/json" }
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (routePath.startsWith("/bundles/") && method === "DELETE") {
|
|
108
|
+
const id = routePath.slice(9);
|
|
109
|
+
await api.deleteBundleById(id);
|
|
110
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
111
|
+
status: 200,
|
|
112
|
+
headers: { "Content-Type": "application/json" }
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (routePath === "/channels" && method === "GET") {
|
|
116
|
+
const channels = await api.getChannels();
|
|
117
|
+
return new Response(JSON.stringify({ channels }), {
|
|
118
|
+
status: 200,
|
|
119
|
+
headers: { "Content-Type": "application/json" }
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return new Response(JSON.stringify({ error: "Not found" }), {
|
|
123
|
+
status: 404,
|
|
124
|
+
headers: { "Content-Type": "application/json" }
|
|
125
|
+
});
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.error("Hot Updater handler error:", error);
|
|
128
|
+
return new Response(JSON.stringify({
|
|
129
|
+
error: "Internal server error",
|
|
130
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
131
|
+
}), {
|
|
132
|
+
status: 500,
|
|
133
|
+
headers: { "Content-Type": "application/json" }
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
//#endregion
|
|
140
|
+
export { createHandler };
|
package/dist/index.cjs
ADDED
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Bundle, PaginatedResult, PaginationInfo, PaginationOptions } from "./types/index.cjs";
|
|
2
|
+
import { DatabaseAPI, DatabaseAdapter, HotUpdaterAPI, HotUpdaterClient, HotUpdaterDB, HotUpdaterOptions, StoragePluginFactory, hotUpdater } from "./db/index.cjs";
|
|
3
|
+
import { HandlerAPI, HandlerOptions, createHandler } from "./handler.cjs";
|
|
4
|
+
export { Bundle, DatabaseAPI, DatabaseAdapter, HandlerAPI, HandlerOptions, HotUpdaterAPI, HotUpdaterClient, HotUpdaterDB, HotUpdaterOptions, PaginatedResult, PaginationInfo, PaginationOptions, StoragePluginFactory, createHandler, hotUpdater };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Bundle, PaginatedResult, PaginationInfo, PaginationOptions } from "./types/index.js";
|
|
2
|
+
import { DatabaseAPI, DatabaseAdapter, HotUpdaterAPI, HotUpdaterClient, HotUpdaterDB, HotUpdaterOptions, StoragePluginFactory, hotUpdater } from "./db/index.js";
|
|
3
|
+
import { HandlerAPI, HandlerOptions, createHandler } from "./handler.js";
|
|
4
|
+
export { Bundle, DatabaseAPI, DatabaseAdapter, HandlerAPI, HandlerOptions, HotUpdaterAPI, HotUpdaterClient, HotUpdaterDB, HotUpdaterOptions, PaginatedResult, PaginationInfo, PaginationOptions, StoragePluginFactory, createHandler, hotUpdater };
|
package/dist/index.js
ADDED