@hot-updater/server 1.2.0-rc.49 → 1.2.0-rc.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/drizzle.cjs +8 -4
- package/dist/adapters/drizzle.d.cts +3 -2
- package/dist/adapters/drizzle.d.mts +3 -2
- package/dist/adapters/drizzle.mjs +8 -4
- package/dist/adapters/drizzleExecutor.cjs +4 -2
- package/dist/adapters/drizzleExecutor.mjs +4 -2
- package/dist/adapters/kysely.cjs +3 -2
- package/dist/adapters/kysely.mjs +3 -2
- package/dist/adapters/mongodb.cjs +1 -1
- package/dist/adapters/mongodb.mjs +1 -1
- package/dist/adapters/mongodbAdapter.cjs +15 -3
- package/dist/adapters/mongodbAdapter.mjs +15 -3
- package/dist/adapters/prisma.cjs +9 -13
- package/dist/adapters/prisma.mjs +10 -14
- package/dist/adapters/prismaExecutor.cjs +25 -20
- package/dist/adapters/prismaExecutor.mjs +25 -20
- package/dist/core/api.d.cts +6 -1
- package/dist/core/api.d.mts +6 -1
- package/dist/core/operations.cjs +2 -20
- package/dist/core/operations.mjs +4 -22
- package/dist/core/reads.cjs +35 -3
- package/dist/core/reads.mjs +35 -3
- package/dist/core/releases.cjs +78 -26
- package/dist/core/releases.mjs +79 -29
- package/dist/db/engineDrizzleSchema.cjs +21 -5
- package/dist/db/engineDrizzleSchema.mjs +21 -6
- package/dist/db/enginePrismaSchema.cjs +6 -14
- package/dist/db/enginePrismaSchema.mjs +7 -15
- package/dist/db/releaseCatalog.cjs +13 -4
- package/dist/db/releaseCatalog.mjs +12 -5
- package/dist/handlerAdminRoutes.cjs +48 -8
- package/dist/handlerAdminRoutes.mjs +49 -9
- package/dist/handlerReleaseCatalogRoutes.cjs +43 -27
- package/dist/handlerReleaseCatalogRoutes.mjs +43 -27
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/package.json +6 -6
|
@@ -22,6 +22,10 @@ const answer = async (operation) => {
|
|
|
22
22
|
error: error.message
|
|
23
23
|
}, status);
|
|
24
24
|
}
|
|
25
|
+
if (error instanceof _hot_updater_plugin_core.ReleaseCatalogCompilationError && error.code === "CATALOG_OVERSIZE") return json({
|
|
26
|
+
code: error.code,
|
|
27
|
+
error: error.message
|
|
28
|
+
}, 400);
|
|
25
29
|
if (error instanceof _hot_updater_plugin_core.DatabaseBundleNotFoundError) return json({
|
|
26
30
|
code: "BUNDLE_NOT_FOUND",
|
|
27
31
|
error: error.message
|
|
@@ -41,9 +45,11 @@ const LIMITS = {
|
|
|
41
45
|
defaultValue: 50,
|
|
42
46
|
maxValue: 500
|
|
43
47
|
};
|
|
48
|
+
/** The most IDs one request names: a lookup's, or a write's. */
|
|
49
|
+
const MAX_IDS = 100;
|
|
44
50
|
/** `order`, `cursor`, and `limit`: one keyset page. */
|
|
45
|
-
const keyset = (url, maxValue = LIMITS.maxValue) => {
|
|
46
|
-
const order = url.searchParams.get("order") ??
|
|
51
|
+
const keyset = (url, maxValue = LIMITS.maxValue, defaultOrder = "desc") => {
|
|
52
|
+
const order = url.searchParams.get("order") ?? defaultOrder;
|
|
47
53
|
if (order !== "asc" && order !== "desc") throw new require_handlerErrors.HandlerBadRequestError("order must be 'asc' or 'desc'.");
|
|
48
54
|
const limitParam = url.searchParams.get("limit");
|
|
49
55
|
const limit = limitParam === null ? Math.min(LIMITS.defaultValue, maxValue) : Number(limitParam);
|
|
@@ -239,12 +245,40 @@ const createAdminRouteHandlers = () => ({
|
|
|
239
245
|
const rows = await core.listReleaseCatalogs(input);
|
|
240
246
|
return page(rows, input, (row) => row.scope_key);
|
|
241
247
|
}),
|
|
242
|
-
/**
|
|
248
|
+
/** Up to 100 scope keys that have releases, after `cursor`: one seek each. */
|
|
249
|
+
listReleaseScopeKeys: onCore(async (core, request) => {
|
|
250
|
+
const input = keyset(new URL(request.url), 100, "asc");
|
|
251
|
+
if (input.order !== "asc" || input.through !== void 0) throw new require_handlerErrors.HandlerBadRequestError("Release scopes are listed ascending, by limit and cursor only.");
|
|
252
|
+
const rows = await core.listReleaseScopeKeys({
|
|
253
|
+
limit: input.limit,
|
|
254
|
+
...input.after === void 0 ? {} : { after: input.after }
|
|
255
|
+
});
|
|
256
|
+
return page(rows, input, (scopeKey) => scopeKey);
|
|
257
|
+
}),
|
|
258
|
+
/**
|
|
259
|
+
* With `name`, the one channel of that name: 1 read. With `ids`, the
|
|
260
|
+
* channels of up to 100 IDs: 1 batch read. With `limit` or `cursor`, one
|
|
261
|
+
* page by name. With none, every channel, as servers before pages answered.
|
|
262
|
+
*/
|
|
243
263
|
getChannels: onCore(async (core, request) => {
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
264
|
+
const url = new URL(request.url);
|
|
265
|
+
const name = url.searchParams.get("name");
|
|
266
|
+
if (name !== null) {
|
|
267
|
+
const channel = await core.findChannelByName(name);
|
|
268
|
+
return channel === null ? notFound() : json({ data: channel });
|
|
269
|
+
}
|
|
270
|
+
const ids = url.searchParams.get("ids");
|
|
271
|
+
if (ids !== null) {
|
|
272
|
+
const list = ids.split(",").filter((id) => id.length > 0);
|
|
273
|
+
if (list.length === 0 || list.length > MAX_IDS) throw new require_handlerErrors.HandlerBadRequestError(`Channels by ID take ids=<channel id>,... with 1 to ${MAX_IDS} ids.`);
|
|
274
|
+
return json({ data: await core.getChannels(list) });
|
|
275
|
+
}
|
|
276
|
+
if (url.searchParams.has("limit") || url.searchParams.has("cursor")) {
|
|
277
|
+
const input = keyset(url, LIMITS.maxValue, "asc");
|
|
278
|
+
const rows = await core.listChannels(input);
|
|
279
|
+
return page(rows, input, (row) => row.name);
|
|
280
|
+
}
|
|
281
|
+
return json({ data: await core.listChannels() });
|
|
248
282
|
}),
|
|
249
283
|
/** The channel named in the body, created when missing. */
|
|
250
284
|
createChannel: onCore(async (core, request) => {
|
|
@@ -295,7 +329,7 @@ const createAdminRouteHandlers = () => ({
|
|
|
295
329
|
countBundles: onCore(async (core, request) => json({ data: await core.countBundles(platformOf(new URL(request.url))) })),
|
|
296
330
|
countBundleChildren: onCore(async (core, request) => {
|
|
297
331
|
const ids = (new URL(request.url).searchParams.get("ids") ?? "").split(",").filter((id) => id.length > 0);
|
|
298
|
-
if (ids.length === 0 || ids.length >
|
|
332
|
+
if (ids.length === 0 || ids.length > MAX_IDS) throw new require_handlerErrors.HandlerBadRequestError(`Bundle child counts take ids=<bundle id>,... with 1 to ${MAX_IDS} ids.`);
|
|
299
333
|
return json({ data: await core.countBundleChildren(ids) });
|
|
300
334
|
}),
|
|
301
335
|
listBundleChildren: onCore(async (core, request, params) => {
|
|
@@ -437,6 +471,7 @@ const createAdminRouteHandlers = () => ({
|
|
|
437
471
|
markIosDevicesInBuild: onCore(async (core, request) => {
|
|
438
472
|
const { buildId, ids } = await body(request);
|
|
439
473
|
if (!isText(buildId) || !Array.isArray(ids) || !ids.every((id) => isText(id))) throw new require_handlerErrors.HandlerBadRequestError("Marking iOS devices in a build needs buildId and ids.");
|
|
474
|
+
if (ids.length > MAX_IDS) throw new require_handlerErrors.HandlerBadRequestError(`Marking iOS devices in a build takes at most ${MAX_IDS} ids a request.`);
|
|
440
475
|
await core.markIosDevicesInBuild({
|
|
441
476
|
buildId,
|
|
442
477
|
ids
|
|
@@ -507,6 +542,11 @@ const ADMIN_ROUTES = [
|
|
|
507
542
|
path: "/release-catalogs",
|
|
508
543
|
handler: "listReleaseCatalogs"
|
|
509
544
|
},
|
|
545
|
+
{
|
|
546
|
+
method: "GET",
|
|
547
|
+
path: "/release-scopes",
|
|
548
|
+
handler: "listReleaseScopeKeys"
|
|
549
|
+
},
|
|
510
550
|
{
|
|
511
551
|
method: "POST",
|
|
512
552
|
path: "/release-catalogs/:scopeKey/rebuild",
|
|
@@ -3,7 +3,7 @@ import { NATIVE_BUILD_SUBMISSION_DESTINATIONS } from "./core/nativeBuildDestinat
|
|
|
3
3
|
import { NativeBuildInputError } from "./core/nativeBuilds.mjs";
|
|
4
4
|
import { HandlerBadRequestError } from "./handlerErrors.mjs";
|
|
5
5
|
import { decodeMaybe, isPlatform, parseBooleanSearchParam, requireRouteParam } from "./handlerParameters.mjs";
|
|
6
|
-
import { DatabaseBundleNotFoundError, DatabaseConstraintError, DatabaseRowReferencedError, ReleaseCatalogMutationError, ReleaseManagementError } from "@hot-updater/plugin-core";
|
|
6
|
+
import { DatabaseBundleNotFoundError, DatabaseConstraintError, DatabaseRowReferencedError, ReleaseCatalogCompilationError, ReleaseCatalogMutationError, ReleaseManagementError } from "@hot-updater/plugin-core";
|
|
7
7
|
//#region src/handlerAdminRoutes.ts
|
|
8
8
|
const json = (body, status = 200) => Response.json(body, { status });
|
|
9
9
|
const notFound = () => json({ error: "Not found" }, 404);
|
|
@@ -22,6 +22,10 @@ const answer = async (operation) => {
|
|
|
22
22
|
error: error.message
|
|
23
23
|
}, status);
|
|
24
24
|
}
|
|
25
|
+
if (error instanceof ReleaseCatalogCompilationError && error.code === "CATALOG_OVERSIZE") return json({
|
|
26
|
+
code: error.code,
|
|
27
|
+
error: error.message
|
|
28
|
+
}, 400);
|
|
25
29
|
if (error instanceof DatabaseBundleNotFoundError) return json({
|
|
26
30
|
code: "BUNDLE_NOT_FOUND",
|
|
27
31
|
error: error.message
|
|
@@ -41,9 +45,11 @@ const LIMITS = {
|
|
|
41
45
|
defaultValue: 50,
|
|
42
46
|
maxValue: 500
|
|
43
47
|
};
|
|
48
|
+
/** The most IDs one request names: a lookup's, or a write's. */
|
|
49
|
+
const MAX_IDS = 100;
|
|
44
50
|
/** `order`, `cursor`, and `limit`: one keyset page. */
|
|
45
|
-
const keyset = (url, maxValue = LIMITS.maxValue) => {
|
|
46
|
-
const order = url.searchParams.get("order") ??
|
|
51
|
+
const keyset = (url, maxValue = LIMITS.maxValue, defaultOrder = "desc") => {
|
|
52
|
+
const order = url.searchParams.get("order") ?? defaultOrder;
|
|
47
53
|
if (order !== "asc" && order !== "desc") throw new HandlerBadRequestError("order must be 'asc' or 'desc'.");
|
|
48
54
|
const limitParam = url.searchParams.get("limit");
|
|
49
55
|
const limit = limitParam === null ? Math.min(LIMITS.defaultValue, maxValue) : Number(limitParam);
|
|
@@ -239,12 +245,40 @@ const createAdminRouteHandlers = () => ({
|
|
|
239
245
|
const rows = await core.listReleaseCatalogs(input);
|
|
240
246
|
return page(rows, input, (row) => row.scope_key);
|
|
241
247
|
}),
|
|
242
|
-
/**
|
|
248
|
+
/** Up to 100 scope keys that have releases, after `cursor`: one seek each. */
|
|
249
|
+
listReleaseScopeKeys: onCore(async (core, request) => {
|
|
250
|
+
const input = keyset(new URL(request.url), 100, "asc");
|
|
251
|
+
if (input.order !== "asc" || input.through !== void 0) throw new HandlerBadRequestError("Release scopes are listed ascending, by limit and cursor only.");
|
|
252
|
+
const rows = await core.listReleaseScopeKeys({
|
|
253
|
+
limit: input.limit,
|
|
254
|
+
...input.after === void 0 ? {} : { after: input.after }
|
|
255
|
+
});
|
|
256
|
+
return page(rows, input, (scopeKey) => scopeKey);
|
|
257
|
+
}),
|
|
258
|
+
/**
|
|
259
|
+
* With `name`, the one channel of that name: 1 read. With `ids`, the
|
|
260
|
+
* channels of up to 100 IDs: 1 batch read. With `limit` or `cursor`, one
|
|
261
|
+
* page by name. With none, every channel, as servers before pages answered.
|
|
262
|
+
*/
|
|
243
263
|
getChannels: onCore(async (core, request) => {
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
264
|
+
const url = new URL(request.url);
|
|
265
|
+
const name = url.searchParams.get("name");
|
|
266
|
+
if (name !== null) {
|
|
267
|
+
const channel = await core.findChannelByName(name);
|
|
268
|
+
return channel === null ? notFound() : json({ data: channel });
|
|
269
|
+
}
|
|
270
|
+
const ids = url.searchParams.get("ids");
|
|
271
|
+
if (ids !== null) {
|
|
272
|
+
const list = ids.split(",").filter((id) => id.length > 0);
|
|
273
|
+
if (list.length === 0 || list.length > MAX_IDS) throw new HandlerBadRequestError(`Channels by ID take ids=<channel id>,... with 1 to ${MAX_IDS} ids.`);
|
|
274
|
+
return json({ data: await core.getChannels(list) });
|
|
275
|
+
}
|
|
276
|
+
if (url.searchParams.has("limit") || url.searchParams.has("cursor")) {
|
|
277
|
+
const input = keyset(url, LIMITS.maxValue, "asc");
|
|
278
|
+
const rows = await core.listChannels(input);
|
|
279
|
+
return page(rows, input, (row) => row.name);
|
|
280
|
+
}
|
|
281
|
+
return json({ data: await core.listChannels() });
|
|
248
282
|
}),
|
|
249
283
|
/** The channel named in the body, created when missing. */
|
|
250
284
|
createChannel: onCore(async (core, request) => {
|
|
@@ -295,7 +329,7 @@ const createAdminRouteHandlers = () => ({
|
|
|
295
329
|
countBundles: onCore(async (core, request) => json({ data: await core.countBundles(platformOf(new URL(request.url))) })),
|
|
296
330
|
countBundleChildren: onCore(async (core, request) => {
|
|
297
331
|
const ids = (new URL(request.url).searchParams.get("ids") ?? "").split(",").filter((id) => id.length > 0);
|
|
298
|
-
if (ids.length === 0 || ids.length >
|
|
332
|
+
if (ids.length === 0 || ids.length > MAX_IDS) throw new HandlerBadRequestError(`Bundle child counts take ids=<bundle id>,... with 1 to ${MAX_IDS} ids.`);
|
|
299
333
|
return json({ data: await core.countBundleChildren(ids) });
|
|
300
334
|
}),
|
|
301
335
|
listBundleChildren: onCore(async (core, request, params) => {
|
|
@@ -437,6 +471,7 @@ const createAdminRouteHandlers = () => ({
|
|
|
437
471
|
markIosDevicesInBuild: onCore(async (core, request) => {
|
|
438
472
|
const { buildId, ids } = await body(request);
|
|
439
473
|
if (!isText(buildId) || !Array.isArray(ids) || !ids.every((id) => isText(id))) throw new HandlerBadRequestError("Marking iOS devices in a build needs buildId and ids.");
|
|
474
|
+
if (ids.length > MAX_IDS) throw new HandlerBadRequestError(`Marking iOS devices in a build takes at most ${MAX_IDS} ids a request.`);
|
|
440
475
|
await core.markIosDevicesInBuild({
|
|
441
476
|
buildId,
|
|
442
477
|
ids
|
|
@@ -507,6 +542,11 @@ const ADMIN_ROUTES = [
|
|
|
507
542
|
path: "/release-catalogs",
|
|
508
543
|
handler: "listReleaseCatalogs"
|
|
509
544
|
},
|
|
545
|
+
{
|
|
546
|
+
method: "GET",
|
|
547
|
+
path: "/release-scopes",
|
|
548
|
+
handler: "listReleaseScopeKeys"
|
|
549
|
+
},
|
|
510
550
|
{
|
|
511
551
|
method: "POST",
|
|
512
552
|
path: "/release-catalogs/:scopeKey/rebuild",
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const require_releaseCatalog = require("./db/releaseCatalog.cjs");
|
|
1
2
|
const require_handlerParameters = require("./handlerParameters.cjs");
|
|
2
3
|
let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
|
|
3
4
|
let _hot_updater_protocol = require("@hot-updater/protocol");
|
|
@@ -20,9 +21,9 @@ const CATALOG_CACHE_CONTROL = "public, max-age=0, s-maxage=5";
|
|
|
20
21
|
* background. `stale-if-error=0` keeps origin errors visible: without it,
|
|
21
22
|
* Cloudflare serves a stale copy on error indefinitely.
|
|
22
23
|
*
|
|
23
|
-
* A device can see a catalog
|
|
24
|
-
* replaced it: the origin's 5-second catalog
|
|
25
|
-
* 5 stale at the CDN. A revoked API key can read a cached catalog up to
|
|
24
|
+
* A device can see a catalog, or a scope's lack of one, up to 15 seconds
|
|
25
|
+
* after a deploy or a Roll back replaced it: the origin's 5-second catalog
|
|
26
|
+
* memo, then 5 seconds fresh and 5 stale at the CDN. A revoked API key can read a cached catalog up to
|
|
26
27
|
* 10 seconds. Devices and caches that don't read the header keep
|
|
27
28
|
* `Cache-Control`.
|
|
28
29
|
*/
|
|
@@ -101,38 +102,53 @@ const catalogResponse = async (catalog, request) => {
|
|
|
101
102
|
};
|
|
102
103
|
const createReleaseCatalogRouteHandlers = () => {
|
|
103
104
|
const cache = /* @__PURE__ */ new Map();
|
|
105
|
+
const evict = (insertedHit) => {
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
let oldestMiss;
|
|
108
|
+
let oldest;
|
|
109
|
+
for (const [key, entry] of cache) if (entry.expiresAt <= now) cache.delete(key);
|
|
110
|
+
else {
|
|
111
|
+
if (entry.row === null) oldestMiss ??= key;
|
|
112
|
+
oldest ??= key;
|
|
113
|
+
}
|
|
114
|
+
if (cache.size <= ORIGIN_CACHE_MAX_ENTRIES) return;
|
|
115
|
+
const victim = oldestMiss ?? (insertedHit ? oldest : void 0);
|
|
116
|
+
if (victim !== void 0) cache.delete(victim);
|
|
117
|
+
};
|
|
104
118
|
const inFlight = /* @__PURE__ */ new Map();
|
|
105
|
-
const
|
|
106
|
-
const cached = cache.get(
|
|
119
|
+
const loadRow = async (scopeKey, load) => {
|
|
120
|
+
const cached = cache.get(scopeKey);
|
|
107
121
|
if (cached !== void 0 && cached.expiresAt > Date.now()) {
|
|
108
|
-
cache.delete(
|
|
109
|
-
cache.set(
|
|
110
|
-
return cached.
|
|
122
|
+
cache.delete(scopeKey);
|
|
123
|
+
cache.set(scopeKey, cached);
|
|
124
|
+
return cached.row;
|
|
111
125
|
}
|
|
112
|
-
if (cached !== void 0) cache.delete(
|
|
113
|
-
const pending = inFlight.get(
|
|
126
|
+
if (cached !== void 0) cache.delete(scopeKey);
|
|
127
|
+
const pending = inFlight.get(scopeKey);
|
|
114
128
|
if (pending !== void 0) return pending;
|
|
115
|
-
const next = load().then((
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const oldestKey = cache.keys().next().value;
|
|
123
|
-
if (oldestKey === void 0) break;
|
|
124
|
-
cache.delete(oldestKey);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return catalog;
|
|
129
|
+
const next = load().then((row) => {
|
|
130
|
+
cache.set(scopeKey, {
|
|
131
|
+
row,
|
|
132
|
+
expiresAt: Date.now() + ORIGIN_CACHE_TTL_MS
|
|
133
|
+
});
|
|
134
|
+
if (cache.size > ORIGIN_CACHE_MAX_ENTRIES) evict(row !== null);
|
|
135
|
+
return row;
|
|
128
136
|
});
|
|
129
|
-
inFlight.set(
|
|
137
|
+
inFlight.set(scopeKey, next);
|
|
130
138
|
try {
|
|
131
139
|
return await next;
|
|
132
140
|
} finally {
|
|
133
|
-
inFlight.delete(
|
|
141
|
+
inFlight.delete(scopeKey);
|
|
134
142
|
}
|
|
135
143
|
};
|
|
144
|
+
const loadCatalog = async (core, input) => {
|
|
145
|
+
const scopeKey = require_releaseCatalog.releaseCatalogScopeKeyOf(input);
|
|
146
|
+
const parsed = await loadRow(scopeKey, async () => {
|
|
147
|
+
const row = await core.getReleaseCatalogRow(scopeKey);
|
|
148
|
+
return row === null ? null : require_releaseCatalog.parseReleaseCatalogRow(row);
|
|
149
|
+
});
|
|
150
|
+
return require_releaseCatalog.projectParsedReleaseCatalogRow(parsed, input, scopeKey);
|
|
151
|
+
};
|
|
136
152
|
return {
|
|
137
153
|
appVersionReleaseCatalog: async (params, request, { core }) => {
|
|
138
154
|
const rawAppVersion = require_handlerParameters.requireRouteParam(params, "appVersion");
|
|
@@ -146,7 +162,7 @@ const createReleaseCatalogRouteHandlers = () => {
|
|
|
146
162
|
};
|
|
147
163
|
const invalid = invalidScope(input);
|
|
148
164
|
if (invalid) return invalid;
|
|
149
|
-
return catalogResponse(await loadCatalog(
|
|
165
|
+
return catalogResponse(await loadCatalog(core, input), request);
|
|
150
166
|
},
|
|
151
167
|
fingerprintReleaseCatalog: async (params, request, { core }) => {
|
|
152
168
|
const input = {
|
|
@@ -157,7 +173,7 @@ const createReleaseCatalogRouteHandlers = () => {
|
|
|
157
173
|
};
|
|
158
174
|
const invalid = invalidScope(input);
|
|
159
175
|
if (invalid) return invalid;
|
|
160
|
-
return catalogResponse(await loadCatalog(
|
|
176
|
+
return catalogResponse(await loadCatalog(core, input), request);
|
|
161
177
|
},
|
|
162
178
|
artifactV2: async (params, _request, { core }) => {
|
|
163
179
|
const info = await core.getArtifactInfo(require_handlerParameters.requireRouteParam(params, "targetBundleId"), require_handlerParameters.requireRouteParam(params, "currentBundleId"), 2);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseReleaseCatalogRow, projectParsedReleaseCatalogRow, releaseCatalogScopeKeyOf } from "./db/releaseCatalog.mjs";
|
|
1
2
|
import { requirePlatformParam, requireRouteParam } from "./handlerParameters.mjs";
|
|
2
3
|
import { canonicalizeAppVersion } from "@hot-updater/plugin-core";
|
|
3
4
|
import { ARTIFACT_FILES_REQUEST_LIMIT, createReleaseCatalogScopeKey } from "@hot-updater/protocol";
|
|
@@ -20,9 +21,9 @@ const CATALOG_CACHE_CONTROL = "public, max-age=0, s-maxage=5";
|
|
|
20
21
|
* background. `stale-if-error=0` keeps origin errors visible: without it,
|
|
21
22
|
* Cloudflare serves a stale copy on error indefinitely.
|
|
22
23
|
*
|
|
23
|
-
* A device can see a catalog
|
|
24
|
-
* replaced it: the origin's 5-second catalog
|
|
25
|
-
* 5 stale at the CDN. A revoked API key can read a cached catalog up to
|
|
24
|
+
* A device can see a catalog, or a scope's lack of one, up to 15 seconds
|
|
25
|
+
* after a deploy or a Roll back replaced it: the origin's 5-second catalog
|
|
26
|
+
* memo, then 5 seconds fresh and 5 stale at the CDN. A revoked API key can read a cached catalog up to
|
|
26
27
|
* 10 seconds. Devices and caches that don't read the header keep
|
|
27
28
|
* `Cache-Control`.
|
|
28
29
|
*/
|
|
@@ -101,38 +102,53 @@ const catalogResponse = async (catalog, request) => {
|
|
|
101
102
|
};
|
|
102
103
|
const createReleaseCatalogRouteHandlers = () => {
|
|
103
104
|
const cache = /* @__PURE__ */ new Map();
|
|
105
|
+
const evict = (insertedHit) => {
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
let oldestMiss;
|
|
108
|
+
let oldest;
|
|
109
|
+
for (const [key, entry] of cache) if (entry.expiresAt <= now) cache.delete(key);
|
|
110
|
+
else {
|
|
111
|
+
if (entry.row === null) oldestMiss ??= key;
|
|
112
|
+
oldest ??= key;
|
|
113
|
+
}
|
|
114
|
+
if (cache.size <= ORIGIN_CACHE_MAX_ENTRIES) return;
|
|
115
|
+
const victim = oldestMiss ?? (insertedHit ? oldest : void 0);
|
|
116
|
+
if (victim !== void 0) cache.delete(victim);
|
|
117
|
+
};
|
|
104
118
|
const inFlight = /* @__PURE__ */ new Map();
|
|
105
|
-
const
|
|
106
|
-
const cached = cache.get(
|
|
119
|
+
const loadRow = async (scopeKey, load) => {
|
|
120
|
+
const cached = cache.get(scopeKey);
|
|
107
121
|
if (cached !== void 0 && cached.expiresAt > Date.now()) {
|
|
108
|
-
cache.delete(
|
|
109
|
-
cache.set(
|
|
110
|
-
return cached.
|
|
122
|
+
cache.delete(scopeKey);
|
|
123
|
+
cache.set(scopeKey, cached);
|
|
124
|
+
return cached.row;
|
|
111
125
|
}
|
|
112
|
-
if (cached !== void 0) cache.delete(
|
|
113
|
-
const pending = inFlight.get(
|
|
126
|
+
if (cached !== void 0) cache.delete(scopeKey);
|
|
127
|
+
const pending = inFlight.get(scopeKey);
|
|
114
128
|
if (pending !== void 0) return pending;
|
|
115
|
-
const next = load().then((
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const oldestKey = cache.keys().next().value;
|
|
123
|
-
if (oldestKey === void 0) break;
|
|
124
|
-
cache.delete(oldestKey);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return catalog;
|
|
129
|
+
const next = load().then((row) => {
|
|
130
|
+
cache.set(scopeKey, {
|
|
131
|
+
row,
|
|
132
|
+
expiresAt: Date.now() + ORIGIN_CACHE_TTL_MS
|
|
133
|
+
});
|
|
134
|
+
if (cache.size > ORIGIN_CACHE_MAX_ENTRIES) evict(row !== null);
|
|
135
|
+
return row;
|
|
128
136
|
});
|
|
129
|
-
inFlight.set(
|
|
137
|
+
inFlight.set(scopeKey, next);
|
|
130
138
|
try {
|
|
131
139
|
return await next;
|
|
132
140
|
} finally {
|
|
133
|
-
inFlight.delete(
|
|
141
|
+
inFlight.delete(scopeKey);
|
|
134
142
|
}
|
|
135
143
|
};
|
|
144
|
+
const loadCatalog = async (core, input) => {
|
|
145
|
+
const scopeKey = releaseCatalogScopeKeyOf(input);
|
|
146
|
+
const parsed = await loadRow(scopeKey, async () => {
|
|
147
|
+
const row = await core.getReleaseCatalogRow(scopeKey);
|
|
148
|
+
return row === null ? null : parseReleaseCatalogRow(row);
|
|
149
|
+
});
|
|
150
|
+
return projectParsedReleaseCatalogRow(parsed, input, scopeKey);
|
|
151
|
+
};
|
|
136
152
|
return {
|
|
137
153
|
appVersionReleaseCatalog: async (params, request, { core }) => {
|
|
138
154
|
const rawAppVersion = requireRouteParam(params, "appVersion");
|
|
@@ -146,7 +162,7 @@ const createReleaseCatalogRouteHandlers = () => {
|
|
|
146
162
|
};
|
|
147
163
|
const invalid = invalidScope(input);
|
|
148
164
|
if (invalid) return invalid;
|
|
149
|
-
return catalogResponse(await loadCatalog(
|
|
165
|
+
return catalogResponse(await loadCatalog(core, input), request);
|
|
150
166
|
},
|
|
151
167
|
fingerprintReleaseCatalog: async (params, request, { core }) => {
|
|
152
168
|
const input = {
|
|
@@ -157,7 +173,7 @@ const createReleaseCatalogRouteHandlers = () => {
|
|
|
157
173
|
};
|
|
158
174
|
const invalid = invalidScope(input);
|
|
159
175
|
if (invalid) return invalid;
|
|
160
|
-
return catalogResponse(await loadCatalog(
|
|
176
|
+
return catalogResponse(await loadCatalog(core, input), request);
|
|
161
177
|
},
|
|
162
178
|
artifactV2: async (params, _request, { core }) => {
|
|
163
179
|
const info = await core.getArtifactInfo(requireRouteParam(params, "targetBundleId"), requireRouteParam(params, "currentBundleId"), 2);
|
package/dist/package.cjs
CHANGED
package/dist/package.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hot-updater/server",
|
|
3
|
-
"version": "1.2.0-rc.
|
|
3
|
+
"version": "1.2.0-rc.50",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "React Native OTA solution for self-hosted",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -50,11 +50,11 @@
|
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"rou3": "0.7.9",
|
|
52
52
|
"verkit": "0.4.0",
|
|
53
|
-
"@hot-updater/
|
|
54
|
-
"@hot-updater/plugin-
|
|
55
|
-
"@hot-updater/plugin-
|
|
56
|
-
"@hot-updater/plugin-insights": "1.2.0-rc.
|
|
57
|
-
"@hot-updater/
|
|
53
|
+
"@hot-updater/plugin-core": "1.2.0-rc.50",
|
|
54
|
+
"@hot-updater/plugin-api-keys": "1.2.0-rc.50",
|
|
55
|
+
"@hot-updater/plugin-remote-config": "1.2.0-rc.50",
|
|
56
|
+
"@hot-updater/plugin-insights": "1.2.0-rc.50",
|
|
57
|
+
"@hot-updater/protocol": "1.2.0-rc.50"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"@prisma/client": "*",
|