@phis/ui 0.8.1 → 0.8.2
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/README.md +9 -2
- package/components/controls/phi-table-control.js +3 -3
- package/components/media/media-upload-flow.d.ts +62 -5
- package/components/media/media-upload-flow.js +62 -24
- package/components/media/phi-area-upload-widget.d.ts +44 -1
- package/components/media/phi-area-upload-widget.js +32 -7
- package/components/media/phi-media-upload.d.ts +10 -3
- package/components/media/phi-media-upload.js +11 -8
- package/components/shell/phi-widget-icon.js +1 -0
- package/components/widgets/client/html-editor-link-node.d.ts +12 -0
- package/components/widgets/client/html-editor-link-node.js +60 -0
- package/components/widgets/client/html-editor.js +10 -6
- package/components/widgets/client/shared/phi-collection-view-binding.js +33 -26
- package/constants/cms-widget-types.d.ts +1 -0
- package/constants/cms-widget-types.js +3 -0
- package/constants/runtime-module-ownership.js +1 -0
- package/gateway/site-proxy.d.ts +12 -0
- package/gateway/site-proxy.js +20 -0
- package/net/phi-next-proxy.d.ts +7 -0
- package/net/phi-next-proxy.js +1 -1
- package/package.json +27 -14
- package/plugins/runtime-modules/area-contributions/admin.d.ts +1 -1
- package/plugins/runtime-modules/area-contributions/admin.js +2 -0
- package/plugins/runtime-modules/avatar/addresses.d.ts +1 -1
- package/plugins/runtime-modules/cdn/authoring-widgets.d.ts +3 -0
- package/plugins/runtime-modules/cdn/authoring-widgets.js +7 -0
- package/plugins/runtime-modules/cdn/authoring.d.ts +1 -0
- package/plugins/runtime-modules/cdn/authoring.js +8 -0
- package/plugins/runtime-modules/cdn/bytes.d.ts +5 -0
- package/plugins/runtime-modules/cdn/bytes.js +17 -0
- package/plugins/runtime-modules/cdn/client-data-providers.d.ts +7 -0
- package/plugins/runtime-modules/cdn/client-data-providers.js +9 -0
- package/plugins/runtime-modules/cdn/data-providers.d.ts +134 -0
- package/plugins/runtime-modules/cdn/data-providers.js +117 -0
- package/plugins/runtime-modules/cdn/definition.d.ts +140 -0
- package/plugins/runtime-modules/cdn/definition.js +23 -0
- package/plugins/runtime-modules/cdn/ids.d.ts +18 -0
- package/plugins/runtime-modules/cdn/ids.js +20 -0
- package/plugins/runtime-modules/cdn/module.d.ts +131 -0
- package/plugins/runtime-modules/cdn/module.js +4 -0
- package/plugins/runtime-modules/cdn/platform.d.ts +25 -0
- package/plugins/runtime-modules/cdn/platform.js +73 -0
- package/plugins/runtime-modules/cdn/presets.d.ts +2 -0
- package/plugins/runtime-modules/cdn/presets.js +20 -0
- package/plugins/runtime-modules/cdn/server.d.ts +1 -0
- package/plugins/runtime-modules/cdn/server.js +14 -0
- package/plugins/runtime-modules/cdn/services/table.d.ts +11 -0
- package/plugins/runtime-modules/cdn/services/table.js +296 -0
- package/plugins/runtime-modules/cdn/services/upload.d.ts +20 -0
- package/plugins/runtime-modules/cdn/services/upload.js +146 -0
- package/plugins/runtime-modules/cdn/table-params.d.ts +10 -0
- package/plugins/runtime-modules/cdn/table-params.js +10 -0
- package/plugins/runtime-modules/cdn/trees/admin-cdn-label-set.d.ts +95 -0
- package/plugins/runtime-modules/cdn/trees/admin-cdn-label-set.js +156 -0
- package/plugins/runtime-modules/cdn/trees/phi-default-admin-cdn-page-tree.d.ts +18 -0
- package/plugins/runtime-modules/cdn/trees/phi-default-admin-cdn-page-tree.js +479 -0
- package/plugins/runtime-modules/cdn/widgets/upload/client.d.ts +12 -0
- package/plugins/runtime-modules/cdn/widgets/upload/client.js +138 -0
- package/plugins/runtime-modules/cdn/widgets/upload/config.d.ts +29 -0
- package/plugins/runtime-modules/cdn/widgets/upload/config.js +43 -0
- package/plugins/runtime-modules/cdn/widgets/upload/label-types.d.ts +24 -0
- package/plugins/runtime-modules/cdn/widgets/upload/label-types.js +1 -0
- package/plugins/runtime-modules/cdn/widgets/upload/labels.d.ts +4 -0
- package/plugins/runtime-modules/cdn/widgets/upload/labels.js +72 -0
- package/plugins/runtime-modules/cdn/widgets/upload/plugin.d.ts +3 -0
- package/plugins/runtime-modules/cdn/widgets/upload/plugin.js +7 -0
- package/plugins/runtime-modules/cdn/widgets/upload/server.d.ts +8 -0
- package/plugins/runtime-modules/cdn/widgets/upload/server.js +17 -0
- package/plugins/runtime-modules/cdn/widgets.d.ts +2 -0
- package/plugins/runtime-modules/cdn/widgets.js +13 -0
- package/plugins/runtime-modules/client-authoring-providers/admin.d.ts +1 -1
- package/plugins/runtime-modules/client-authoring-providers/admin.js +6 -0
- package/plugins/runtime-modules/client-manifests/admin.js +11 -1
- package/plugins/runtime-modules/core/widgets/collection-view/asset-binding.js +1 -1
- package/plugins/runtime-modules/core/widgets/collection-view/site-address-binding.js +3 -1
- package/plugins/runtime-modules/core/widgets/table/client.js +6 -4
- package/types/table-widget.d.ts +2 -2
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { PHIS_CDN_ADMIN_PAGE_SIZE_DEFAULT, PHIS_CDN_ADMIN_PAGE_SIZE_MAX, PhisCdnCheckObjectFlags, PhisCdnCheckReportFlags, } from "@phis/contracts/addon";
|
|
3
|
+
import { isPhiRecord } from "../../../../helpers/is-record";
|
|
4
|
+
import { readPhiJsonError, requestPhiJson } from "../../../../helpers/client-json-request";
|
|
5
|
+
import { PHI_CDN_RUNTIME_DATA_PROVIDER_KEYS, PHI_CDN_TABLE_RESOURCE_KEYS, } from "../ids";
|
|
6
|
+
import { buildPhiTablePageParams, readPhiTableStringFilter, } from "../../../../components/widgets/client/shared/phi-table-provider-request";
|
|
7
|
+
import { PHI_TABLE_PROVIDER_REFUSED_CODE } from "../../../../components/widgets/client/shared/phi-table-provider-refusal";
|
|
8
|
+
import { PhiTableProviderError, } from "../../../../types/table-widget";
|
|
9
|
+
import { createPhiTableProviderClient } from "../../../../components/widgets/client/shared/phi-table-provider";
|
|
10
|
+
import { PHI_CDN_RUNTIME_DATA_PROVIDER_DESCRIPTORS } from "../data-providers";
|
|
11
|
+
import { readPhiCdnPlatformTags } from "../platform";
|
|
12
|
+
import { formatPhiCdnBytes } from "../bytes";
|
|
13
|
+
import { PHI_CDN_TABLE_PARAM_KEYS } from "../table-params";
|
|
14
|
+
export const PHI_CDN_ADMIN_API_PATH = "/api/site/admin/cdn";
|
|
15
|
+
function readParamText(params, key, fallback) {
|
|
16
|
+
const value = params?.[key];
|
|
17
|
+
return typeof value === "string" && value.trim() ? value : fallback;
|
|
18
|
+
}
|
|
19
|
+
function readNoCdnText(params) {
|
|
20
|
+
return readParamText(params, PHI_CDN_TABLE_PARAM_KEYS.noCdnText, "This Site has no CDN. The operator attaches one with phis cdn attach.");
|
|
21
|
+
}
|
|
22
|
+
/** A refusal as Core says it: `{ error: <code>, message }`; the sentence is the one the person reads. */
|
|
23
|
+
function readRefusal(reply, subject) {
|
|
24
|
+
return new PhiTableProviderError("request-failed", readPhiJsonError(reply.payload, `${subject} failed with status ${reply.status}.`, ["message", "error"]));
|
|
25
|
+
}
|
|
26
|
+
function readErrorCode(reply) {
|
|
27
|
+
return isPhiRecord(reply.payload) && typeof reply.payload.error === "string" ? reply.payload.error : null;
|
|
28
|
+
}
|
|
29
|
+
/** A key's three segments, each its own path segment as Core's routes take them. */
|
|
30
|
+
function buildFilePath(key) {
|
|
31
|
+
const segments = key.split("/");
|
|
32
|
+
if (segments.length !== 3 || segments.some((segment) => !segment)) {
|
|
33
|
+
throw new PhiTableProviderError("invalid-action-value", `"${key}" is not a CDN key.`);
|
|
34
|
+
}
|
|
35
|
+
return `${PHI_CDN_ADMIN_API_PATH}/files/${segments.map(encodeURIComponent).join("/")}`;
|
|
36
|
+
}
|
|
37
|
+
/** One catalogue entry as the table and the record read it. */
|
|
38
|
+
export function toPhiCdnFileRow(file, locale) {
|
|
39
|
+
return {
|
|
40
|
+
key: file.key,
|
|
41
|
+
name: file.name,
|
|
42
|
+
version: file.version,
|
|
43
|
+
fileName: file.fileName,
|
|
44
|
+
sizeBytes: file.sizeBytes,
|
|
45
|
+
sizeLabel: formatPhiCdnBytes(file.sizeBytes, locale),
|
|
46
|
+
storage: file.storage,
|
|
47
|
+
platform: readPhiCdnPlatformTags(file.os, file.arch),
|
|
48
|
+
sha256: file.sha256,
|
|
49
|
+
contentType: file.contentType,
|
|
50
|
+
publishedAt: file.publishedAt,
|
|
51
|
+
url: file.url,
|
|
52
|
+
state: file.state,
|
|
53
|
+
origin: file.origin.kind === "addon" ? file.origin.addonId ?? "addon" : file.origin.kind,
|
|
54
|
+
addonId: file.origin.addonId,
|
|
55
|
+
actorUserId: file.origin.actorUserId,
|
|
56
|
+
sourceUrl: file.origin.sourceUrl,
|
|
57
|
+
withdrawDisabled: file.state !== "offered",
|
|
58
|
+
deleteDisabled: file.state === "deleted",
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function isAdminFile(value) {
|
|
62
|
+
return isPhiRecord(value) &&
|
|
63
|
+
typeof value.key === "string" &&
|
|
64
|
+
typeof value.name === "string" &&
|
|
65
|
+
typeof value.version === "string" &&
|
|
66
|
+
typeof value.fileName === "string" &&
|
|
67
|
+
typeof value.sizeBytes === "number" &&
|
|
68
|
+
typeof value.sha256 === "string" &&
|
|
69
|
+
typeof value.url === "string" &&
|
|
70
|
+
typeof value.state === "string" &&
|
|
71
|
+
typeof value.os === "number" &&
|
|
72
|
+
typeof value.arch === "number" &&
|
|
73
|
+
isPhiRecord(value.origin) &&
|
|
74
|
+
typeof value.origin.kind === "string";
|
|
75
|
+
}
|
|
76
|
+
function readList(value) {
|
|
77
|
+
if (!isPhiRecord(value) ||
|
|
78
|
+
!Array.isArray(value.files) ||
|
|
79
|
+
!value.files.every(isAdminFile) ||
|
|
80
|
+
typeof value.total !== "number" ||
|
|
81
|
+
(value.cdn !== null && !isPhiRecord(value.cdn))) {
|
|
82
|
+
throw new PhiTableProviderError("invalid-response", "The CDN list answer is not in the shape Core states.");
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
/*
|
|
87
|
+
* The rows this Provider answered last, by key: what a row action and the detail read are about. A row
|
|
88
|
+
* action carries the row's key and nothing else, and the address to copy or the record to show is the
|
|
89
|
+
* one the reader just saw.
|
|
90
|
+
*/
|
|
91
|
+
const lastRows = new Map();
|
|
92
|
+
function buildListParams(query) {
|
|
93
|
+
const params = buildPhiTablePageParams(query, PHIS_CDN_ADMIN_PAGE_SIZE_DEFAULT);
|
|
94
|
+
const pageSize = Number(params.get("pageSize"));
|
|
95
|
+
if (pageSize > PHIS_CDN_ADMIN_PAGE_SIZE_MAX)
|
|
96
|
+
params.set("pageSize", String(PHIS_CDN_ADMIN_PAGE_SIZE_MAX));
|
|
97
|
+
const search = query.search?.trim() ?? "";
|
|
98
|
+
const state = readPhiTableStringFilter(query, "state");
|
|
99
|
+
const name = readPhiTableStringFilter(query, "name");
|
|
100
|
+
if (search)
|
|
101
|
+
params.set("q", search);
|
|
102
|
+
if (state)
|
|
103
|
+
params.set("state", state);
|
|
104
|
+
if (name)
|
|
105
|
+
params.set("name", name);
|
|
106
|
+
const [sort] = query.sorts ?? [];
|
|
107
|
+
if (sort && (sort.key === "publishedAt" || sort.key === "name")) {
|
|
108
|
+
params.set("sort", sort.key);
|
|
109
|
+
params.set("dir", sort.direction === "ascending" ? "asc" : "desc");
|
|
110
|
+
}
|
|
111
|
+
return params;
|
|
112
|
+
}
|
|
113
|
+
async function requestList(params, signal) {
|
|
114
|
+
const reply = await requestPhiJson(`${PHI_CDN_ADMIN_API_PATH}?${params.toString()}`, { signal });
|
|
115
|
+
if (!reply.ok)
|
|
116
|
+
throw readRefusal(reply, "The CDN list");
|
|
117
|
+
return readList(reply.payload);
|
|
118
|
+
}
|
|
119
|
+
async function queryFiles({ query, params, locale, signal }) {
|
|
120
|
+
const list = await requestList(buildListParams(query), signal);
|
|
121
|
+
// A Site without a CDN is no failure of the source: the Table says how one is attached, in its rows' place.
|
|
122
|
+
if (list.cdn === null) {
|
|
123
|
+
throw new PhiTableProviderError(PHI_TABLE_PROVIDER_REFUSED_CODE, readNoCdnText(params));
|
|
124
|
+
}
|
|
125
|
+
const rows = list.files.map((file) => toPhiCdnFileRow(file, locale));
|
|
126
|
+
for (const row of rows)
|
|
127
|
+
lastRows.set(String(row.key), row);
|
|
128
|
+
return { rows, total: list.total };
|
|
129
|
+
}
|
|
130
|
+
/** What one check found, one row per finding. */
|
|
131
|
+
export function toPhiCdnCheckRows(report, locale) {
|
|
132
|
+
return [
|
|
133
|
+
...report.unrecorded.map((entry) => ({
|
|
134
|
+
id: `unrecorded:${entry.key}`,
|
|
135
|
+
key: entry.key,
|
|
136
|
+
finding: "unrecorded",
|
|
137
|
+
sizeLabel: formatPhiCdnBytes(entry.sizeBytes, locale),
|
|
138
|
+
recordedLabel: null,
|
|
139
|
+
reason: entry.reason,
|
|
140
|
+
adoptable: (entry.flags & PhisCdnCheckObjectFlags.Adoptable) !== 0,
|
|
141
|
+
})),
|
|
142
|
+
...report.missing.map((key) => ({
|
|
143
|
+
id: `missing:${key}`,
|
|
144
|
+
key,
|
|
145
|
+
finding: "missing",
|
|
146
|
+
sizeLabel: null,
|
|
147
|
+
recordedLabel: null,
|
|
148
|
+
reason: null,
|
|
149
|
+
adoptable: false,
|
|
150
|
+
})),
|
|
151
|
+
...report.sizeMismatch.map((entry) => ({
|
|
152
|
+
id: `size-mismatch:${entry.key}`,
|
|
153
|
+
key: entry.key,
|
|
154
|
+
finding: "size-mismatch",
|
|
155
|
+
sizeLabel: formatPhiCdnBytes(entry.stored, locale),
|
|
156
|
+
recordedLabel: formatPhiCdnBytes(entry.recorded, locale),
|
|
157
|
+
reason: null,
|
|
158
|
+
adoptable: false,
|
|
159
|
+
})),
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
function readCheckReport(value) {
|
|
163
|
+
if (!isPhiRecord(value) ||
|
|
164
|
+
typeof value.flags !== "number" ||
|
|
165
|
+
typeof value.objects !== "number" ||
|
|
166
|
+
typeof value.recorded !== "number" ||
|
|
167
|
+
!Array.isArray(value.unrecorded) ||
|
|
168
|
+
!Array.isArray(value.missing) ||
|
|
169
|
+
!Array.isArray(value.sizeMismatch)) {
|
|
170
|
+
throw new PhiTableProviderError("invalid-response", "The CDN check answer is not in the shape Core states.");
|
|
171
|
+
}
|
|
172
|
+
return value;
|
|
173
|
+
}
|
|
174
|
+
async function queryCheck({ params, locale, signal }) {
|
|
175
|
+
const reply = await requestPhiJson(`${PHI_CDN_ADMIN_API_PATH}/check`, { method: "POST", signal });
|
|
176
|
+
if (!reply.ok) {
|
|
177
|
+
if (readErrorCode(reply) === "no_cdn") {
|
|
178
|
+
throw new PhiTableProviderError(PHI_TABLE_PROVIDER_REFUSED_CODE, readNoCdnText(params));
|
|
179
|
+
}
|
|
180
|
+
throw readRefusal(reply, "The CDN check");
|
|
181
|
+
}
|
|
182
|
+
const report = readCheckReport(reply.payload);
|
|
183
|
+
const rows = toPhiCdnCheckRows(report, locale);
|
|
184
|
+
return {
|
|
185
|
+
rows,
|
|
186
|
+
total: rows.length,
|
|
187
|
+
summary: {
|
|
188
|
+
objects: report.objects,
|
|
189
|
+
recorded: report.recorded,
|
|
190
|
+
consistent: (report.flags & PhisCdnCheckReportFlags.Consistent) !== 0,
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
async function queryRows(request) {
|
|
195
|
+
if (request.resourceKey === PHI_CDN_TABLE_RESOURCE_KEYS.files)
|
|
196
|
+
return await queryFiles(request);
|
|
197
|
+
if (request.resourceKey === PHI_CDN_TABLE_RESOURCE_KEYS.check)
|
|
198
|
+
return await queryCheck(request);
|
|
199
|
+
throw new PhiTableProviderError("resource-not-found", `Unknown CDN resource "${request.resourceKey}".`);
|
|
200
|
+
}
|
|
201
|
+
async function readRecord({ resourceKey, rowIdentity, locale, signal }) {
|
|
202
|
+
if (resourceKey !== PHI_CDN_TABLE_RESOURCE_KEYS.files || typeof rowIdentity !== "string" || !rowIdentity) {
|
|
203
|
+
throw new PhiTableProviderError("record-not-found", "A CDN file is read by its key.");
|
|
204
|
+
}
|
|
205
|
+
const shown = lastRows.get(rowIdentity);
|
|
206
|
+
if (shown)
|
|
207
|
+
return shown;
|
|
208
|
+
// Not among the rows shown: the one file of that name, version and file name, whatever its state.
|
|
209
|
+
const [name = "", version = "", fileName = ""] = rowIdentity.split("/");
|
|
210
|
+
const list = await requestList(new URLSearchParams({ name, q: fileName, state: "all", pageSize: String(PHIS_CDN_ADMIN_PAGE_SIZE_MAX) }), signal);
|
|
211
|
+
const file = list.files.find((entry) => entry.key === rowIdentity && entry.version === version);
|
|
212
|
+
if (!file) {
|
|
213
|
+
throw new PhiTableProviderError("record-not-found", `No CDN file "${rowIdentity}".`);
|
|
214
|
+
}
|
|
215
|
+
return toPhiCdnFileRow(file, locale);
|
|
216
|
+
}
|
|
217
|
+
function readMutatedFile(value) {
|
|
218
|
+
const file = isPhiRecord(value) ? value.file : null;
|
|
219
|
+
if (!isAdminFile(file)) {
|
|
220
|
+
throw new PhiTableProviderError("invalid-response", "The CDN answer carries no file.");
|
|
221
|
+
}
|
|
222
|
+
return file;
|
|
223
|
+
}
|
|
224
|
+
async function copyAddress(key, params) {
|
|
225
|
+
const url = lastRows.get(key)?.url;
|
|
226
|
+
const copyFailed = readParamText(params, PHI_CDN_TABLE_PARAM_KEYS.copyFailedText, "The browser did not allow copying.");
|
|
227
|
+
if (typeof url !== "string" || !url) {
|
|
228
|
+
return { status: "rejected", invalidation: "none", errorCode: "copy-failed", message: copyFailed };
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
await navigator.clipboard.writeText(url);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
return { status: "rejected", invalidation: "none", errorCode: "copy-failed", message: copyFailed };
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
status: "accepted",
|
|
238
|
+
invalidation: "none",
|
|
239
|
+
message: readParamText(params, PHI_CDN_TABLE_PARAM_KEYS.copiedText, "Address copied."),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
async function mutate(request) {
|
|
243
|
+
if (request.kind !== "action") {
|
|
244
|
+
throw new PhiTableProviderError("mutation-not-supported", "The CDN table changes nothing in place.");
|
|
245
|
+
}
|
|
246
|
+
const key = typeof request.rowIdentity === "string" ? request.rowIdentity : "";
|
|
247
|
+
if (!key) {
|
|
248
|
+
throw new PhiTableProviderError("invalid-action-value", `"${request.actionKey}" acts on one file.`);
|
|
249
|
+
}
|
|
250
|
+
if (request.resourceKey === PHI_CDN_TABLE_RESOURCE_KEYS.check && request.actionKey === "adopt") {
|
|
251
|
+
const body = { keys: [key] };
|
|
252
|
+
const reply = await requestPhiJson(`${PHI_CDN_ADMIN_API_PATH}/check/adopt`, {
|
|
253
|
+
method: "POST",
|
|
254
|
+
body,
|
|
255
|
+
signal: request.signal,
|
|
256
|
+
});
|
|
257
|
+
if (!reply.ok)
|
|
258
|
+
throw readRefusal(reply, "Adopting");
|
|
259
|
+
const refused = reply.payload?.refused?.find((entry) => entry.key === key);
|
|
260
|
+
if (refused) {
|
|
261
|
+
return { status: "rejected", invalidation: "none", errorCode: "adopt-refused", message: refused.reason };
|
|
262
|
+
}
|
|
263
|
+
// The check is read again: what was found has changed.
|
|
264
|
+
return { status: "accepted", invalidation: "view" };
|
|
265
|
+
}
|
|
266
|
+
if (request.resourceKey !== PHI_CDN_TABLE_RESOURCE_KEYS.files) {
|
|
267
|
+
throw new PhiTableProviderError("action-not-supported", `Unsupported CDN action "${request.actionKey}".`);
|
|
268
|
+
}
|
|
269
|
+
if (request.actionKey === "copy-url") {
|
|
270
|
+
return await copyAddress(key, request.params);
|
|
271
|
+
}
|
|
272
|
+
if (request.actionKey === "withdraw" || request.actionKey === "delete") {
|
|
273
|
+
const reply = await requestPhiJson(request.actionKey === "withdraw" ? `${buildFilePath(key)}/withdraw` : buildFilePath(key), { method: request.actionKey === "withdraw" ? "POST" : "DELETE", signal: request.signal });
|
|
274
|
+
if (!reply.ok)
|
|
275
|
+
throw readRefusal(reply, request.actionKey === "withdraw" ? "Withdrawing" : "Deleting");
|
|
276
|
+
const file = readMutatedFile(reply.payload);
|
|
277
|
+
lastRows.delete(file.key);
|
|
278
|
+
// The state filter may no longer hold the row, so the view is read again rather than patched.
|
|
279
|
+
return { status: "accepted", invalidation: "view" };
|
|
280
|
+
}
|
|
281
|
+
throw new PhiTableProviderError("action-not-supported", `Unsupported CDN action "${request.actionKey}".`);
|
|
282
|
+
}
|
|
283
|
+
// Its own descriptor by key, not by position: a Provider added to the list must not take its place.
|
|
284
|
+
const resources = PHI_CDN_RUNTIME_DATA_PROVIDER_DESCRIPTORS
|
|
285
|
+
.find((descriptor) => descriptor.key === PHI_CDN_RUNTIME_DATA_PROVIDER_KEYS.table)?.resources ?? [];
|
|
286
|
+
export const PhiCdnTableProviderClient = createPhiTableProviderClient({
|
|
287
|
+
key: PHI_CDN_RUNTIME_DATA_PROVIDER_KEYS.table,
|
|
288
|
+
resources,
|
|
289
|
+
query: queryRows,
|
|
290
|
+
readRecord,
|
|
291
|
+
mutate,
|
|
292
|
+
});
|
|
293
|
+
/** For tests: forget the rows answered so far. */
|
|
294
|
+
export function resetPhiCdnTableRowsForTests() {
|
|
295
|
+
lastRows.clear();
|
|
296
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { PhisCdnAdminFileV1 } from "@phis/contracts/addon";
|
|
2
|
+
import { type PhiUploadTarget } from "../../../../components/media/media-upload-flow";
|
|
3
|
+
/** The abort reason that deletes the session instead of leaving it to be resumed. */
|
|
4
|
+
export declare const PHI_CDN_UPLOAD_DISCARD = "phi-cdn-upload-discard";
|
|
5
|
+
/** What a file is published as: its key's three segments and its platform. */
|
|
6
|
+
export type PhiCdnUploadInput = {
|
|
7
|
+
name: string;
|
|
8
|
+
version: string;
|
|
9
|
+
fileName: string;
|
|
10
|
+
/** `PhisCdnFileOs` bits. */
|
|
11
|
+
os: number;
|
|
12
|
+
/** `PhisCdnFileArch` bits. */
|
|
13
|
+
arch: number;
|
|
14
|
+
};
|
|
15
|
+
/** One file into the Site's CDN as `input` names it. */
|
|
16
|
+
export declare function createPhiCdnUploadTarget(input: PhiCdnUploadInput, { pollIntervalMs }?: {
|
|
17
|
+
pollIntervalMs?: number;
|
|
18
|
+
}): PhiUploadTarget<PhisCdnAdminFileV1>;
|
|
19
|
+
/** The names the catalogue holds, for the upload dialog to offer: every state, in name order. */
|
|
20
|
+
export declare function loadPhiCdnNames(signal?: AbortSignal): Promise<string[]>;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { isPhiRecord } from "../../../../helpers/is-record";
|
|
2
|
+
import { readPhiJsonError, requestPhiJson } from "../../../../helpers/client-json-request";
|
|
3
|
+
import { buildPhiMediaClientDigest } from "../../../../components/media/media-upload-digest";
|
|
4
|
+
import { PhiMediaUploadError, } from "../../../../components/media/media-upload-flow";
|
|
5
|
+
/**
|
|
6
|
+
* The Site's CDN as an upload target (`runPhiUploadSession`): the Media upload's sequence -- the parts
|
|
7
|
+
* hashed as the Media upload divides a file, the parts the storage does not hold sent with a heartbeat
|
|
8
|
+
* beside them, retried as Media's are -- carried to Core's CDN routes (`/api/site/admin/cdn/uploads`).
|
|
9
|
+
*
|
|
10
|
+
* What is the CDN's own is only what its routes say differently:
|
|
11
|
+
*
|
|
12
|
+
* - **A part goes to Core**, `PUT …/parts/<n>`, which streams it on to the bucket; it answers no entity
|
|
13
|
+
* tag (`partReceipt: "none"`), because Core completes the assembly itself.
|
|
14
|
+
* - **Resuming is opening.** The same key, size and part digests answer the session that is open, with the
|
|
15
|
+
* parts that arrived, so the plan addresses only what is missing. Nothing tells Core a page went away: a
|
|
16
|
+
* session whose heartbeat stops expires on its own, and until then it can be taken up again.
|
|
17
|
+
* - **Cancel discards.** Stopped with `PHI_CDN_UPLOAD_DISCARD` as the abort reason, the session is deleted
|
|
18
|
+
* and what arrived goes with it; stopped for any other reason -- the dialog closed -- it is left.
|
|
19
|
+
* - **Settling is hashing.** Completed, Core hashes the whole file from the bucket and records it; the
|
|
20
|
+
* target asks until it has (`finish`).
|
|
21
|
+
*
|
|
22
|
+
* Limits are Core's: one file's size, the Space's quota, the types the CDN serves. A refusal is a
|
|
23
|
+
* `PhiMediaUploadError` with Core's code and Core's sentence.
|
|
24
|
+
*/
|
|
25
|
+
const PHI_CDN_UPLOAD_API_PATH = "/api/site/admin/cdn/uploads";
|
|
26
|
+
/** The abort reason that deletes the session instead of leaving it to be resumed. */
|
|
27
|
+
export const PHI_CDN_UPLOAD_DISCARD = "phi-cdn-upload-discard";
|
|
28
|
+
/** How long between two questions about a completed upload while Core hashes it. */
|
|
29
|
+
const DEFAULT_POLL_INTERVAL_MS = 1000;
|
|
30
|
+
function readCoreRefusal(reply) {
|
|
31
|
+
const code = isPhiRecord(reply.payload) && typeof reply.payload.error === "string"
|
|
32
|
+
? reply.payload.error
|
|
33
|
+
: `status-${reply.status}`;
|
|
34
|
+
return new PhiMediaUploadError(readPhiJsonError(reply.payload, `The CDN answered with status ${reply.status}.`, ["message", "error"]), code, reply.status);
|
|
35
|
+
}
|
|
36
|
+
function readUploadState(value) {
|
|
37
|
+
if (!isPhiRecord(value) ||
|
|
38
|
+
typeof value.token !== "string" ||
|
|
39
|
+
typeof value.state !== "string" ||
|
|
40
|
+
typeof value.partSizeBytes !== "number" ||
|
|
41
|
+
typeof value.partCount !== "number" ||
|
|
42
|
+
!Array.isArray(value.uploaded) ||
|
|
43
|
+
typeof value.heartbeatIntervalMs !== "number") {
|
|
44
|
+
throw new PhiMediaUploadError("The CDN upload answer is not in the shape Core states.", "invalid-response", 0);
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
function sleep(ms, signal) {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
const timer = setTimeout(() => {
|
|
51
|
+
signal?.removeEventListener("abort", onAbort);
|
|
52
|
+
resolve();
|
|
53
|
+
}, ms);
|
|
54
|
+
function onAbort() {
|
|
55
|
+
clearTimeout(timer);
|
|
56
|
+
reject(new PhiMediaUploadError("The upload was stopped.", "cancelled", 0));
|
|
57
|
+
}
|
|
58
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
const uploadPath = (token) => `${PHI_CDN_UPLOAD_API_PATH}/${encodeURIComponent(token)}`;
|
|
62
|
+
/** Asks after a completed upload until Core has hashed and recorded it, or says why it did not. */
|
|
63
|
+
async function waitForPublished(state, signal, pollIntervalMs) {
|
|
64
|
+
let current = state;
|
|
65
|
+
while (current.state === "hashing") {
|
|
66
|
+
await sleep(pollIntervalMs, signal);
|
|
67
|
+
const asked = await requestPhiJson(uploadPath(current.token), { ...(signal ? { signal } : {}) });
|
|
68
|
+
if (!asked.ok)
|
|
69
|
+
throw readCoreRefusal(asked);
|
|
70
|
+
current = readUploadState(asked.payload);
|
|
71
|
+
}
|
|
72
|
+
if (current.state === "published" && current.file)
|
|
73
|
+
return current.file;
|
|
74
|
+
if (current.state === "aborted")
|
|
75
|
+
throw new PhiMediaUploadError("The upload was aborted.", "cancelled", 0);
|
|
76
|
+
throw new PhiMediaUploadError(current.error?.message ?? "Core did not publish the file.", current.error?.code ?? "failed", 0);
|
|
77
|
+
}
|
|
78
|
+
/** One file into the Site's CDN as `input` names it. */
|
|
79
|
+
export function createPhiCdnUploadTarget(input, { pollIntervalMs = DEFAULT_POLL_INTERVAL_MS } = {}) {
|
|
80
|
+
return {
|
|
81
|
+
open: async (file, signal) => {
|
|
82
|
+
const digest = await buildPhiMediaClientDigest(file);
|
|
83
|
+
if (!digest) {
|
|
84
|
+
throw new PhiMediaUploadError("This browser cannot compute the file's checksum here (it needs a secure connection).", "checksum-unavailable", 0);
|
|
85
|
+
}
|
|
86
|
+
const init = {
|
|
87
|
+
name: input.name,
|
|
88
|
+
version: input.version,
|
|
89
|
+
fileName: input.fileName,
|
|
90
|
+
sizeBytes: file.size,
|
|
91
|
+
...(file.type ? { contentType: file.type } : {}),
|
|
92
|
+
os: input.os,
|
|
93
|
+
arch: input.arch,
|
|
94
|
+
partSha256: "sha256" in digest ? [digest.sha256] : digest.partSha256,
|
|
95
|
+
};
|
|
96
|
+
const opened = await requestPhiJson(PHI_CDN_UPLOAD_API_PATH, {
|
|
97
|
+
method: "POST",
|
|
98
|
+
body: init,
|
|
99
|
+
...(signal ? { signal } : {}),
|
|
100
|
+
});
|
|
101
|
+
if (!opened.ok)
|
|
102
|
+
throw readCoreRefusal(opened);
|
|
103
|
+
const state = readUploadState(opened.payload);
|
|
104
|
+
const held = new Set(state.uploaded);
|
|
105
|
+
const parts = Array.from({ length: state.partCount }, (_, index) => index + 1)
|
|
106
|
+
.filter((partNumber) => !held.has(partNumber));
|
|
107
|
+
return {
|
|
108
|
+
plan: {
|
|
109
|
+
kind: "multipart-put",
|
|
110
|
+
method: "PUT",
|
|
111
|
+
uploadId: state.token,
|
|
112
|
+
partSizeBytes: state.partSizeBytes,
|
|
113
|
+
parts: parts.map((partNumber) => ({ partNumber, url: `${uploadPath(state.token)}/parts/${partNumber}` })),
|
|
114
|
+
},
|
|
115
|
+
uploaded: [...held].map((partNumber) => ({ partNumber, eTag: "" })),
|
|
116
|
+
heartbeat: { url: `${uploadPath(state.token)}/heartbeat`, intervalMs: Math.max(1000, state.heartbeatIntervalMs) },
|
|
117
|
+
partReceipt: "none",
|
|
118
|
+
abandon: async (_error, stopped) => {
|
|
119
|
+
if (stopped?.aborted && stopped.reason === PHI_CDN_UPLOAD_DISCARD) {
|
|
120
|
+
await requestPhiJson(uploadPath(state.token), { method: "DELETE" }).catch(() => undefined);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
finish: async (_completion, stopped) => {
|
|
124
|
+
// A session taken up again after its completion was sent is hashing already: only the wait is left.
|
|
125
|
+
if (state.state === "hashing" || state.state === "published") {
|
|
126
|
+
return await waitForPublished(state, stopped, pollIntervalMs);
|
|
127
|
+
}
|
|
128
|
+
const completed = await requestPhiJson(`${uploadPath(state.token)}/complete`, { method: "POST" });
|
|
129
|
+
if (!completed.ok)
|
|
130
|
+
throw readCoreRefusal(completed);
|
|
131
|
+
return await waitForPublished(readUploadState(completed.payload), stopped, pollIntervalMs);
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/** The names the catalogue holds, for the upload dialog to offer: every state, in name order. */
|
|
138
|
+
export async function loadPhiCdnNames(signal) {
|
|
139
|
+
const params = new URLSearchParams({ state: "all", sort: "name", dir: "asc", page: "1", pageSize: "200" });
|
|
140
|
+
const reply = await requestPhiJson(`/api/site/admin/cdn?${params.toString()}`, {
|
|
141
|
+
...(signal ? { signal } : {}),
|
|
142
|
+
});
|
|
143
|
+
if (!reply.ok || !Array.isArray(reply.payload?.files))
|
|
144
|
+
return [];
|
|
145
|
+
return [...new Set(reply.payload.files.map((file) => file.name).filter((name) => typeof name === "string"))];
|
|
146
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The words the CDN table Provider says itself, handed over by the placement as `source.params` in the
|
|
3
|
+
* reader's language: the Provider has no Label Set, and the Page that binds it does.
|
|
4
|
+
*/
|
|
5
|
+
export declare const PHI_CDN_TABLE_PARAM_KEYS: {
|
|
6
|
+
/** Where the rows would be when the Site has no CDN; it names `phis cdn attach`. */
|
|
7
|
+
readonly noCdnText: "noCdnText";
|
|
8
|
+
readonly copiedText: "copiedText";
|
|
9
|
+
readonly copyFailedText: "copyFailedText";
|
|
10
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The words the CDN table Provider says itself, handed over by the placement as `source.params` in the
|
|
3
|
+
* reader's language: the Provider has no Label Set, and the Page that binds it does.
|
|
4
|
+
*/
|
|
5
|
+
export const PHI_CDN_TABLE_PARAM_KEYS = {
|
|
6
|
+
/** Where the rows would be when the Site has no CDN; it names `phis cdn attach`. */
|
|
7
|
+
noCdnText: "noCdnText",
|
|
8
|
+
copiedText: "copiedText",
|
|
9
|
+
copyFailedText: "copyFailedText",
|
|
10
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import "server-only";
|
|
2
|
+
import { type PhiGlobalTranslatorOptions } from "../../../../gateway/tr";
|
|
3
|
+
export declare function getPhiAdminCdnPageLabels(options: PhiGlobalTranslatorOptions): Promise<{
|
|
4
|
+
pageTitle: string;
|
|
5
|
+
pageDescription: string;
|
|
6
|
+
tableLabel: string;
|
|
7
|
+
columns: {
|
|
8
|
+
name: string;
|
|
9
|
+
version: string;
|
|
10
|
+
file: string;
|
|
11
|
+
size: string;
|
|
12
|
+
storage: string;
|
|
13
|
+
storageHint: string;
|
|
14
|
+
platform: string;
|
|
15
|
+
sha256: string;
|
|
16
|
+
sha256Hint: string;
|
|
17
|
+
state: string;
|
|
18
|
+
origin: string;
|
|
19
|
+
published: string;
|
|
20
|
+
};
|
|
21
|
+
storage: {
|
|
22
|
+
cdn: string;
|
|
23
|
+
link: string;
|
|
24
|
+
};
|
|
25
|
+
states: {
|
|
26
|
+
offered: string;
|
|
27
|
+
withdrawn: string;
|
|
28
|
+
deleted: string;
|
|
29
|
+
};
|
|
30
|
+
origins: {
|
|
31
|
+
cli: string;
|
|
32
|
+
upload: string;
|
|
33
|
+
addon: string;
|
|
34
|
+
};
|
|
35
|
+
filterState: string;
|
|
36
|
+
filterStateAll: string;
|
|
37
|
+
searchPlaceholder: string;
|
|
38
|
+
emptyTitle: string;
|
|
39
|
+
emptyText: string;
|
|
40
|
+
noCdn: string;
|
|
41
|
+
footer: string;
|
|
42
|
+
actions: {
|
|
43
|
+
upload: string;
|
|
44
|
+
check: string;
|
|
45
|
+
view: string;
|
|
46
|
+
copy: string;
|
|
47
|
+
withdraw: string;
|
|
48
|
+
delete: string;
|
|
49
|
+
adopt: string;
|
|
50
|
+
};
|
|
51
|
+
copied: string;
|
|
52
|
+
copyFailed: string;
|
|
53
|
+
confirmCancel: string;
|
|
54
|
+
withdraw: {
|
|
55
|
+
title: string;
|
|
56
|
+
text: string;
|
|
57
|
+
ok: string;
|
|
58
|
+
};
|
|
59
|
+
delete: {
|
|
60
|
+
title: string;
|
|
61
|
+
text: string;
|
|
62
|
+
linkNote: string;
|
|
63
|
+
ok: string;
|
|
64
|
+
};
|
|
65
|
+
uploadTitle: string;
|
|
66
|
+
detail: {
|
|
67
|
+
title: string;
|
|
68
|
+
key: string;
|
|
69
|
+
url: string;
|
|
70
|
+
sourceUrl: string;
|
|
71
|
+
contentType: string;
|
|
72
|
+
sizeBytes: string;
|
|
73
|
+
addon: string;
|
|
74
|
+
actor: string;
|
|
75
|
+
};
|
|
76
|
+
check: {
|
|
77
|
+
title: string;
|
|
78
|
+
tableLabel: string;
|
|
79
|
+
columns: {
|
|
80
|
+
key: string;
|
|
81
|
+
finding: string;
|
|
82
|
+
size: string;
|
|
83
|
+
recorded: string;
|
|
84
|
+
reason: string;
|
|
85
|
+
};
|
|
86
|
+
findings: {
|
|
87
|
+
unrecorded: string;
|
|
88
|
+
missing: string;
|
|
89
|
+
"size-mismatch": string;
|
|
90
|
+
};
|
|
91
|
+
empty: string;
|
|
92
|
+
footer: string;
|
|
93
|
+
footerConsistent: string;
|
|
94
|
+
};
|
|
95
|
+
}>;
|