@kubohiroya/turbowarp-title-menu 0.4.0 → 0.6.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/README.ja.md +43 -2
- package/README.md +66 -3
- package/dist/extension-manifest.json +19 -0
- package/dist/lib/composition.js +2 -0
- package/dist/lib/extension-cache-dialog.js +389 -0
- package/dist/lib/extension-cache.js +91 -0
- package/dist/lib/locales.js +91 -2
- package/dist/turbowarp-title-menu.js +933 -11
- package/dist/types/composition.d.ts +4 -0
- package/dist/types/extension-cache-dialog.d.ts +76 -0
- package/dist/types/extension-cache.d.ts +89 -0
- package/dist/types/locales.d.ts +22 -0
- package/package.json +15 -7
- package/src/block-definitions.json +22 -0
- package/src/composition.ts +16 -0
- package/src/extension-cache-dialog.ts +525 -0
- package/src/extension-cache.ts +187 -0
- package/src/extension.ts +81 -1
- package/src/index.ts +4 -0
- package/src/locales.ts +100 -2
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
(function (Scratch) {
|
|
8
8
|
'use strict';
|
|
9
9
|
|
|
10
|
-
//#region node_modules/.pnpm/@kubohiroya+turbowarp-app-shell@0.
|
|
10
|
+
//#region node_modules/.pnpm/@kubohiroya+turbowarp-app-shell@0.6.1/node_modules/@kubohiroya/turbowarp-app-shell/dist/index.js
|
|
11
11
|
function isRecord$1(value) {
|
|
12
12
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13
13
|
}
|
|
@@ -684,27 +684,27 @@ var defaultSortDirections = Object.freeze({
|
|
|
684
684
|
updatedAt: "desc",
|
|
685
685
|
byteLength: "desc"
|
|
686
686
|
});
|
|
687
|
-
function requireLocales(value) {
|
|
687
|
+
function requireLocales$1(value) {
|
|
688
688
|
if (!isRecord(value) || Object.keys(value).length === 0) throw new TypeError("locales must contain at least one locale");
|
|
689
689
|
return value;
|
|
690
690
|
}
|
|
691
|
-
function textFor(locales, locale) {
|
|
691
|
+
function textFor$1(locales, locale) {
|
|
692
692
|
const text = locales[locale] ?? locales["en"] ?? Object.values(locales)[0];
|
|
693
693
|
if (text === void 0) throw new TypeError("locales must contain at least one locale");
|
|
694
694
|
return text;
|
|
695
695
|
}
|
|
696
|
-
function defaultFormatSize(byteLength) {
|
|
696
|
+
function defaultFormatSize$1(byteLength) {
|
|
697
697
|
if (byteLength < 1024) return `${byteLength} B`;
|
|
698
698
|
if (byteLength < 1048576) return `${(byteLength / 1024).toFixed(1)} KB`;
|
|
699
699
|
return `${(byteLength / 1048576).toFixed(1)} MB`;
|
|
700
700
|
}
|
|
701
|
-
function defaultFormatDate(isoDate) {
|
|
701
|
+
function defaultFormatDate$1(isoDate) {
|
|
702
702
|
const parsed = new Date(isoDate);
|
|
703
703
|
if (Number.isNaN(parsed.getTime())) return isoDate;
|
|
704
704
|
const pad = (value) => String(value).padStart(2, "0");
|
|
705
705
|
return `${parsed.getFullYear()}-${pad(parsed.getMonth() + 1)}-${pad(parsed.getDate())} ${pad(parsed.getHours())}:${pad(parsed.getMinutes())}`;
|
|
706
706
|
}
|
|
707
|
-
function defaultDescribeError(error) {
|
|
707
|
+
function defaultDescribeError$1(error) {
|
|
708
708
|
if (error instanceof Error && error.message.length > 0) return error.message;
|
|
709
709
|
return String(error);
|
|
710
710
|
}
|
|
@@ -712,10 +712,10 @@ function createDslFilesDialog(options) {
|
|
|
712
712
|
if (!isRecord(options)) throw new TypeError("DSL files dialog options must be an object");
|
|
713
713
|
const document = requireDocument(options.document ?? globalThis.document);
|
|
714
714
|
const mount = requireElement(options.mount ?? document.body, "mount");
|
|
715
|
-
const locales = requireLocales(options.locales);
|
|
716
|
-
const describeError = options.describeError ?? defaultDescribeError;
|
|
717
|
-
const formatSize = options.formatSize ?? defaultFormatSize;
|
|
718
|
-
const formatDate = options.formatDate ?? defaultFormatDate;
|
|
715
|
+
const locales = requireLocales$1(options.locales);
|
|
716
|
+
const describeError = options.describeError ?? defaultDescribeError$1;
|
|
717
|
+
const formatSize = options.formatSize ?? defaultFormatSize$1;
|
|
718
|
+
const formatDate = options.formatDate ?? defaultFormatDate$1;
|
|
719
719
|
let locale = options.initialLocale ?? "en";
|
|
720
720
|
let sort = options.initialSort ?? defaultDslSort;
|
|
721
721
|
let summaries = [];
|
|
@@ -903,7 +903,7 @@ function createDslFilesDialog(options) {
|
|
|
903
903
|
}
|
|
904
904
|
function render() {
|
|
905
905
|
if (disposed) return;
|
|
906
|
-
const text = textFor(locales, locale);
|
|
906
|
+
const text = textFor$1(locales, locale);
|
|
907
907
|
heading.textContent = text.title;
|
|
908
908
|
closeButton.textContent = text.close;
|
|
909
909
|
closeButton.setAttribute("aria-label", text.close);
|
|
@@ -990,6 +990,765 @@ function dispatchDslSourceEvent(type, record) {
|
|
|
990
990
|
target.dispatchEvent(event);
|
|
991
991
|
}
|
|
992
992
|
//#endregion
|
|
993
|
+
//#region node_modules/.pnpm/@kubohiroya+turbowarp-extension-loader@0.12.0/node_modules/@kubohiroya/turbowarp-extension-loader/dist/cache-gzip.js
|
|
994
|
+
/** Gzip utilities for persistent extension caches. The caller remains responsible for SHA-256 verification. */
|
|
995
|
+
async function gzipBytes(bytes) {
|
|
996
|
+
if (!(bytes instanceof Uint8Array)) throw new TypeError("Expected Uint8Array");
|
|
997
|
+
if (typeof CompressionStream !== "function") throw new Error("CompressionStream gzip unavailable");
|
|
998
|
+
return new Uint8Array(await new Response(new Blob([bytes]).stream().pipeThrough(new CompressionStream("gzip"))).arrayBuffer());
|
|
999
|
+
}
|
|
1000
|
+
async function gunzipBytes(compressed, maxOutputBytes) {
|
|
1001
|
+
if (!(compressed instanceof Uint8Array) || !Number.isSafeInteger(maxOutputBytes) || maxOutputBytes < 1) throw new TypeError("Invalid compressed bytes or output limit");
|
|
1002
|
+
if (typeof DecompressionStream !== "function") throw new Error("DecompressionStream gzip unavailable");
|
|
1003
|
+
const reader = new Blob([compressed]).stream().pipeThrough(new DecompressionStream("gzip")).getReader();
|
|
1004
|
+
const chunks = [];
|
|
1005
|
+
let size = 0;
|
|
1006
|
+
try {
|
|
1007
|
+
while (true) {
|
|
1008
|
+
const { done, value } = await reader.read();
|
|
1009
|
+
if (done) break;
|
|
1010
|
+
size += value.byteLength;
|
|
1011
|
+
if (size > maxOutputBytes) throw new Error("Decompressed cache exceeds byte limit");
|
|
1012
|
+
chunks.push(value);
|
|
1013
|
+
}
|
|
1014
|
+
} catch (error) {
|
|
1015
|
+
await reader.cancel(error).catch(() => {});
|
|
1016
|
+
throw error;
|
|
1017
|
+
} finally {
|
|
1018
|
+
reader.releaseLock();
|
|
1019
|
+
}
|
|
1020
|
+
const result = new Uint8Array(size);
|
|
1021
|
+
let offset = 0;
|
|
1022
|
+
for (const chunk of chunks) {
|
|
1023
|
+
result.set(chunk, offset);
|
|
1024
|
+
offset += chunk.byteLength;
|
|
1025
|
+
}
|
|
1026
|
+
return result;
|
|
1027
|
+
}
|
|
1028
|
+
//#endregion
|
|
1029
|
+
//#region node_modules/.pnpm/@kubohiroya+turbowarp-extension-loader@0.12.0/node_modules/@kubohiroya/turbowarp-extension-loader/dist/limits.js
|
|
1030
|
+
var MiB = 1048576;
|
|
1031
|
+
/**
|
|
1032
|
+
* The one source of the default size limits. A file the project codec accepts as an archive entry
|
|
1033
|
+
* must also be one the loader reads, so the per-extension and total defaults are the codec's entry
|
|
1034
|
+
* and expansion limits; real extensions such as a bundled story runtime reach several MiB.
|
|
1035
|
+
*/
|
|
1036
|
+
var DEFAULT_ENTRY_BYTES = 16 * MiB;
|
|
1037
|
+
var DEFAULT_TOTAL_BYTES = 64 * MiB;
|
|
1038
|
+
Object.freeze({
|
|
1039
|
+
maxExtensions: 64,
|
|
1040
|
+
maxBytes: DEFAULT_ENTRY_BYTES,
|
|
1041
|
+
maxTotalBytes: DEFAULT_TOTAL_BYTES,
|
|
1042
|
+
timeoutMs: 3e4
|
|
1043
|
+
});
|
|
1044
|
+
Math.ceil(DEFAULT_ENTRY_BYTES / 3) * 4 + 1024;
|
|
1045
|
+
//#endregion
|
|
1046
|
+
//#region node_modules/.pnpm/@kubohiroya+turbowarp-extension-loader@0.12.0/node_modules/@kubohiroya/turbowarp-extension-loader/dist/indexeddb-cache.js
|
|
1047
|
+
/**
|
|
1048
|
+
* The bucket for bytes that belong to no one project.
|
|
1049
|
+
*
|
|
1050
|
+
* A catalog extension is pinned to a version and shared by many projects, so filing it under
|
|
1051
|
+
* whichever project happened to fetch it first would make that project look responsible for
|
|
1052
|
+
* storage every other one reuses, and releasing that project would evict bytes the rest still
|
|
1053
|
+
* want. It is not a URI, so no project can name it.
|
|
1054
|
+
*/
|
|
1055
|
+
var SHARED_BUCKET = "shared";
|
|
1056
|
+
/** Persistent, content-addressed cache. The Loader independently verifies SHA-256 on every hit.
|
|
1057
|
+
* Storage is untrusted: this adapter is not an authorization or integrity boundary.
|
|
1058
|
+
*/
|
|
1059
|
+
function createIndexedDBContentCache({ indexedDB = globalThis.indexedDB, databaseName = "turbowarp-plus-extension-cache", appURI, sharedDigests, maxBytes = 33554432, maxDecompressedBytes = DEFAULT_ENTRY_BYTES, now = Date.now } = {}) {
|
|
1060
|
+
if (!indexedDB || typeof indexedDB.open !== "function") throw new Error("IndexedDB unavailable");
|
|
1061
|
+
if (typeof databaseName !== "string" || !databaseName || !Number.isSafeInteger(maxBytes) || maxBytes < 1 || !Number.isSafeInteger(maxDecompressedBytes) || maxDecompressedBytes < 1 || typeof now !== "function" || appURI !== void 0 && typeof appURI !== "string" && typeof appURI !== "function") throw new TypeError("Invalid IndexedDB cache options");
|
|
1062
|
+
/** The project a write is filed under, read when it happens. */
|
|
1063
|
+
function currentAppURI() {
|
|
1064
|
+
const uri = typeof appURI === "function" ? appURI() : appURI;
|
|
1065
|
+
if (uri !== void 0 && typeof uri !== "string") throw new TypeError("appURI must return a string or undefined");
|
|
1066
|
+
return uri;
|
|
1067
|
+
}
|
|
1068
|
+
let dbPromise;
|
|
1069
|
+
function open() {
|
|
1070
|
+
if (!dbPromise) dbPromise = new Promise((resolve, reject) => {
|
|
1071
|
+
const request = indexedDB.open(databaseName, 2);
|
|
1072
|
+
request.onupgradeneeded = () => {
|
|
1073
|
+
const db = request.result;
|
|
1074
|
+
if (!db.objectStoreNames.contains("content")) db.createObjectStore("content", { keyPath: "key" });
|
|
1075
|
+
if (!db.objectStoreNames.contains("usage")) {
|
|
1076
|
+
const usage = db.createObjectStore("usage", { keyPath: ["bucket", "key"] });
|
|
1077
|
+
usage.createIndex("key", "key");
|
|
1078
|
+
usage.createIndex("bucket", "bucket");
|
|
1079
|
+
request.transaction.objectStore("content").clear();
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
request.onsuccess = () => {
|
|
1083
|
+
const db = request.result;
|
|
1084
|
+
db.onversionchange = () => db.close();
|
|
1085
|
+
resolve(db);
|
|
1086
|
+
};
|
|
1087
|
+
request.onerror = () => reject(request.error || /* @__PURE__ */ new Error("IndexedDB open failed"));
|
|
1088
|
+
request.onblocked = () => reject(/* @__PURE__ */ new Error("IndexedDB upgrade blocked"));
|
|
1089
|
+
}).catch((error) => {
|
|
1090
|
+
dbPromise = void 0;
|
|
1091
|
+
throw error;
|
|
1092
|
+
});
|
|
1093
|
+
return dbPromise;
|
|
1094
|
+
}
|
|
1095
|
+
function transaction(db, mode, work, stores = "content") {
|
|
1096
|
+
return new Promise((resolve, reject) => {
|
|
1097
|
+
const tx = db.transaction(stores, mode);
|
|
1098
|
+
let value;
|
|
1099
|
+
tx.oncomplete = () => resolve(value);
|
|
1100
|
+
tx.onerror = () => reject(tx.error || /* @__PURE__ */ new Error("IndexedDB transaction failed"));
|
|
1101
|
+
tx.onabort = () => reject(tx.error || /* @__PURE__ */ new Error("IndexedDB transaction aborted"));
|
|
1102
|
+
try {
|
|
1103
|
+
work(tx.objectStore(typeof stores === "string" ? stores : stores[0]), (result) => {
|
|
1104
|
+
value = result;
|
|
1105
|
+
}, tx);
|
|
1106
|
+
} catch (error) {
|
|
1107
|
+
try {
|
|
1108
|
+
tx.abort();
|
|
1109
|
+
} catch {}
|
|
1110
|
+
reject(error);
|
|
1111
|
+
}
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
const keyValid = (key) => typeof key === "string" && /^[a-f0-9]{64}$/.test(key);
|
|
1115
|
+
const validBytes = (value) => value instanceof Uint8Array;
|
|
1116
|
+
const validRecord = (entry) => !!entry && entry.format === "gzip-v1" && entry.bytes instanceof ArrayBuffer && Number.isSafeInteger(entry.originalBytes) && entry.originalBytes >= 0 && entry.originalBytes <= maxDecompressedBytes && entry.bytes.byteLength <= maxBytes;
|
|
1117
|
+
const dropClaims = (usage, key) => {
|
|
1118
|
+
const claims = usage.index("key").getAllKeys(key);
|
|
1119
|
+
claims.onsuccess = () => {
|
|
1120
|
+
for (const claim of claims.result) usage.delete(claim);
|
|
1121
|
+
};
|
|
1122
|
+
};
|
|
1123
|
+
function sweep(store, usage, protectedKey = null) {
|
|
1124
|
+
const request = store.getAll();
|
|
1125
|
+
request.onsuccess = () => {
|
|
1126
|
+
let total = 0;
|
|
1127
|
+
const kept = [];
|
|
1128
|
+
for (const record of request.result) {
|
|
1129
|
+
if (!validRecord(record)) {
|
|
1130
|
+
store.delete(record.key);
|
|
1131
|
+
dropClaims(usage, record.key);
|
|
1132
|
+
continue;
|
|
1133
|
+
}
|
|
1134
|
+
total += record.bytes.byteLength;
|
|
1135
|
+
kept.push(record);
|
|
1136
|
+
}
|
|
1137
|
+
kept.sort((a, b) => (a.lastAccess || 0) - (b.lastAccess || 0));
|
|
1138
|
+
for (const record of kept) {
|
|
1139
|
+
if (total <= maxBytes) break;
|
|
1140
|
+
if (record.key === protectedKey) continue;
|
|
1141
|
+
store.delete(record.key);
|
|
1142
|
+
dropClaims(usage, record.key);
|
|
1143
|
+
total -= record.bytes.byteLength;
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
async function release(bucket, keys) {
|
|
1148
|
+
if (typeof bucket !== "string" || !bucket) throw new TypeError("A bucket name is required");
|
|
1149
|
+
if (keys !== null && (!Array.isArray(keys) || keys.some((key) => !keyValid(key)))) throw new TypeError("Cache keys must be SHA-256 digests");
|
|
1150
|
+
const wanted = keys === null ? null : new Set(keys);
|
|
1151
|
+
return transaction(await open(), "readwrite", (store, set, tx) => {
|
|
1152
|
+
const usage = tx.objectStore("usage");
|
|
1153
|
+
const result = {
|
|
1154
|
+
released: 0,
|
|
1155
|
+
freed: 0
|
|
1156
|
+
};
|
|
1157
|
+
set(result);
|
|
1158
|
+
const mine = usage.index("bucket").getAll(bucket);
|
|
1159
|
+
mine.onsuccess = () => {
|
|
1160
|
+
for (const claim of mine.result) {
|
|
1161
|
+
if (wanted !== null && !wanted.has(claim.key)) continue;
|
|
1162
|
+
usage.delete([bucket, claim.key]);
|
|
1163
|
+
result.released++;
|
|
1164
|
+
const others = usage.index("key").getAll(claim.key);
|
|
1165
|
+
others.onsuccess = () => {
|
|
1166
|
+
if (others.result.some((other) => other.bucket !== bucket)) return;
|
|
1167
|
+
const record = store.get(claim.key);
|
|
1168
|
+
record.onsuccess = () => {
|
|
1169
|
+
const entry = record.result;
|
|
1170
|
+
if (!entry) return;
|
|
1171
|
+
result.freed += entry.bytes?.byteLength || 0;
|
|
1172
|
+
store.delete(claim.key);
|
|
1173
|
+
};
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
};
|
|
1177
|
+
}, ["content", "usage"]);
|
|
1178
|
+
}
|
|
1179
|
+
return Object.freeze({
|
|
1180
|
+
async get(key, { signal } = {}) {
|
|
1181
|
+
if (!keyValid(key)) throw new TypeError("Invalid SHA-256 cache key");
|
|
1182
|
+
if (signal?.aborted) throw signal.reason;
|
|
1183
|
+
const record = await transaction(await open(), "readwrite", (store, set) => {
|
|
1184
|
+
const request = store.get(key);
|
|
1185
|
+
request.onsuccess = () => {
|
|
1186
|
+
const entry = request.result;
|
|
1187
|
+
if (!entry) {
|
|
1188
|
+
set(void 0);
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
if (!validRecord(entry)) {
|
|
1192
|
+
store.delete(key);
|
|
1193
|
+
set(void 0);
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
entry.lastAccess = now();
|
|
1197
|
+
store.put(entry);
|
|
1198
|
+
set({
|
|
1199
|
+
bytes: entry.bytes.slice(0),
|
|
1200
|
+
originalBytes: entry.originalBytes
|
|
1201
|
+
});
|
|
1202
|
+
};
|
|
1203
|
+
});
|
|
1204
|
+
if (signal?.aborted) throw signal.reason;
|
|
1205
|
+
if (!record) return void 0;
|
|
1206
|
+
const bytes = await gunzipBytes(new Uint8Array(record.bytes), maxDecompressedBytes);
|
|
1207
|
+
if (bytes.byteLength !== record.originalBytes) throw new Error("Cached decompressed length mismatch");
|
|
1208
|
+
if (signal?.aborted) throw signal.reason;
|
|
1209
|
+
return bytes;
|
|
1210
|
+
},
|
|
1211
|
+
async put(key, bytes, { signal } = {}) {
|
|
1212
|
+
if (!keyValid(key) || !validBytes(bytes)) throw new TypeError("Cache content requires a SHA-256 key and bytes");
|
|
1213
|
+
if (signal?.aborted) throw signal.reason;
|
|
1214
|
+
if (bytes.byteLength > maxDecompressedBytes) throw new RangeError("Cache input exceeds decompressed size limit");
|
|
1215
|
+
const uri = currentAppURI();
|
|
1216
|
+
const originalBytes = bytes.byteLength;
|
|
1217
|
+
const compressed = await gzipBytes(bytes);
|
|
1218
|
+
if (signal?.aborted) throw signal.reason;
|
|
1219
|
+
const db = await open();
|
|
1220
|
+
const bucket = uri === void 0 || sharedDigests?.has(key) ? SHARED_BUCKET : uri;
|
|
1221
|
+
return transaction(db, "readwrite", (store, _set, tx) => {
|
|
1222
|
+
const usage = tx.objectStore("usage");
|
|
1223
|
+
const time = now();
|
|
1224
|
+
if (compressed.byteLength <= maxBytes) {
|
|
1225
|
+
store.put({
|
|
1226
|
+
key,
|
|
1227
|
+
format: "gzip-v1",
|
|
1228
|
+
bytes: compressed.buffer,
|
|
1229
|
+
originalBytes,
|
|
1230
|
+
lastAccess: time
|
|
1231
|
+
});
|
|
1232
|
+
usage.put({
|
|
1233
|
+
bucket,
|
|
1234
|
+
key,
|
|
1235
|
+
lastAccess: time
|
|
1236
|
+
});
|
|
1237
|
+
} else {
|
|
1238
|
+
store.delete(key);
|
|
1239
|
+
usage.delete([bucket, key]);
|
|
1240
|
+
}
|
|
1241
|
+
sweep(store, usage, compressed.byteLength <= maxBytes ? key : null);
|
|
1242
|
+
}, ["content", "usage"]);
|
|
1243
|
+
},
|
|
1244
|
+
async clear() {
|
|
1245
|
+
return transaction(await open(), "readwrite", (store, _set, tx) => {
|
|
1246
|
+
store.clear();
|
|
1247
|
+
tx.objectStore("usage").clear();
|
|
1248
|
+
}, ["content", "usage"]);
|
|
1249
|
+
},
|
|
1250
|
+
async getSize() {
|
|
1251
|
+
return transaction(await open(), "readonly", (store, set) => {
|
|
1252
|
+
const request = store.getAll();
|
|
1253
|
+
request.onsuccess = () => set(request.result.reduce((size, record) => ({
|
|
1254
|
+
entries: size.entries + 1,
|
|
1255
|
+
bytes: size.bytes + (record.bytes?.byteLength || 0)
|
|
1256
|
+
}), {
|
|
1257
|
+
entries: 0,
|
|
1258
|
+
bytes: 0
|
|
1259
|
+
}));
|
|
1260
|
+
});
|
|
1261
|
+
},
|
|
1262
|
+
async listBuckets() {
|
|
1263
|
+
return transaction(await open(), "readonly", (store, set, tx) => {
|
|
1264
|
+
const claims = tx.objectStore("usage").getAll();
|
|
1265
|
+
const records = store.getAll();
|
|
1266
|
+
claims.onsuccess = () => {
|
|
1267
|
+
records.onsuccess = () => {
|
|
1268
|
+
const sizes = new Map(records.result.map((record) => [record.key, record.bytes?.byteLength || 0]));
|
|
1269
|
+
const totals = /* @__PURE__ */ new Map();
|
|
1270
|
+
for (const claim of claims.result) {
|
|
1271
|
+
const total = totals.get(claim.bucket) || {
|
|
1272
|
+
entries: 0,
|
|
1273
|
+
bytes: 0
|
|
1274
|
+
};
|
|
1275
|
+
total.entries++;
|
|
1276
|
+
total.bytes += sizes.get(claim.key) || 0;
|
|
1277
|
+
totals.set(claim.bucket, total);
|
|
1278
|
+
}
|
|
1279
|
+
set([...totals].map(([bucket, total]) => Object.freeze({
|
|
1280
|
+
bucket,
|
|
1281
|
+
...total
|
|
1282
|
+
})));
|
|
1283
|
+
};
|
|
1284
|
+
};
|
|
1285
|
+
}, ["content", "usage"]);
|
|
1286
|
+
},
|
|
1287
|
+
async listBucket(bucket) {
|
|
1288
|
+
if (typeof bucket !== "string" || !bucket) throw new TypeError("A bucket name is required");
|
|
1289
|
+
return transaction(await open(), "readonly", (store, set, tx) => {
|
|
1290
|
+
const claims = tx.objectStore("usage").index("bucket").getAll(bucket);
|
|
1291
|
+
const records = store.getAll();
|
|
1292
|
+
claims.onsuccess = () => {
|
|
1293
|
+
records.onsuccess = () => {
|
|
1294
|
+
const byKey = new Map(records.result.map((record) => [record.key, record]));
|
|
1295
|
+
set(claims.result.flatMap((claim) => {
|
|
1296
|
+
const record = byKey.get(claim.key);
|
|
1297
|
+
if (!record) return [];
|
|
1298
|
+
return [Object.freeze({
|
|
1299
|
+
bucket,
|
|
1300
|
+
key: claim.key,
|
|
1301
|
+
bytes: record.bytes?.byteLength || 0,
|
|
1302
|
+
originalBytes: record.originalBytes,
|
|
1303
|
+
lastAccess: claim.lastAccess
|
|
1304
|
+
})];
|
|
1305
|
+
}));
|
|
1306
|
+
};
|
|
1307
|
+
};
|
|
1308
|
+
}, ["content", "usage"]);
|
|
1309
|
+
},
|
|
1310
|
+
deleteBucket(bucket) {
|
|
1311
|
+
return release(bucket, null);
|
|
1312
|
+
},
|
|
1313
|
+
deleteFromBucket(bucket, keys) {
|
|
1314
|
+
return release(bucket, keys);
|
|
1315
|
+
},
|
|
1316
|
+
async close() {
|
|
1317
|
+
if (dbPromise) {
|
|
1318
|
+
(await dbPromise).close();
|
|
1319
|
+
dbPromise = void 0;
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
//#endregion
|
|
1325
|
+
//#region src/extension-cache.ts
|
|
1326
|
+
var ExtensionCacheError = class extends Error {
|
|
1327
|
+
constructor(code, message, cause) {
|
|
1328
|
+
super(message, cause === void 0 ? void 0 : { cause });
|
|
1329
|
+
this.name = "ExtensionCacheError";
|
|
1330
|
+
this.code = code;
|
|
1331
|
+
}
|
|
1332
|
+
};
|
|
1333
|
+
/**
|
|
1334
|
+
* Compares two buckets for display.
|
|
1335
|
+
*
|
|
1336
|
+
* The shared bucket leads because it is the one row that is not a project: it holds what the
|
|
1337
|
+
* catalog pinned for everyone, and an operator hunting for space should see it before deciding
|
|
1338
|
+
* that some individual project is to blame. The rest follow by stored size, because size is the
|
|
1339
|
+
* only reason anyone opens this screen; the bucket name breaks ties so the order is stable
|
|
1340
|
+
* between listings rather than reflecting insertion order.
|
|
1341
|
+
*/
|
|
1342
|
+
function compareBuckets(left, right) {
|
|
1343
|
+
if (left.shared !== right.shared) return left.shared ? -1 : 1;
|
|
1344
|
+
if (left.bytes !== right.bytes) return right.bytes - left.bytes;
|
|
1345
|
+
return left.bucket < right.bucket ? -1 : left.bucket > right.bucket ? 1 : 0;
|
|
1346
|
+
}
|
|
1347
|
+
function createExtensionCache(options = {}) {
|
|
1348
|
+
let cache = null;
|
|
1349
|
+
/**
|
|
1350
|
+
* Opens the cache on first use.
|
|
1351
|
+
*
|
|
1352
|
+
* A browser with storage blocked fails here rather than when the dialog is built, so the menu
|
|
1353
|
+
* entry stays clickable and the failure is reported in the dialog's own status line.
|
|
1354
|
+
*/
|
|
1355
|
+
function requireCache() {
|
|
1356
|
+
if (cache !== null) return cache;
|
|
1357
|
+
try {
|
|
1358
|
+
cache = createIndexedDBContentCache({
|
|
1359
|
+
...options.indexedDB === void 0 ? {} : { indexedDB: options.indexedDB },
|
|
1360
|
+
...options.databaseName === void 0 ? {} : { databaseName: options.databaseName }
|
|
1361
|
+
});
|
|
1362
|
+
} catch (error) {
|
|
1363
|
+
throw new ExtensionCacheError("unavailable", "the extension cache is unavailable", error);
|
|
1364
|
+
}
|
|
1365
|
+
return cache;
|
|
1366
|
+
}
|
|
1367
|
+
return Object.freeze({
|
|
1368
|
+
async listProjects() {
|
|
1369
|
+
return (await requireCache().listBuckets()).map((bucket) => {
|
|
1370
|
+
const title = options.titleFor?.(bucket.bucket);
|
|
1371
|
+
return {
|
|
1372
|
+
bucket: bucket.bucket,
|
|
1373
|
+
shared: bucket.bucket === SHARED_BUCKET,
|
|
1374
|
+
...title === void 0 || title.length === 0 ? {} : { title },
|
|
1375
|
+
entries: bucket.entries,
|
|
1376
|
+
bytes: bucket.bytes
|
|
1377
|
+
};
|
|
1378
|
+
}).sort(compareBuckets);
|
|
1379
|
+
},
|
|
1380
|
+
async listDigests(bucket) {
|
|
1381
|
+
return (await requireCache().listBucket(bucket)).map((entry) => ({
|
|
1382
|
+
key: entry.key,
|
|
1383
|
+
bytes: entry.bytes,
|
|
1384
|
+
originalBytes: entry.originalBytes,
|
|
1385
|
+
lastAccess: entry.lastAccess
|
|
1386
|
+
})).sort((left, right) => right.lastAccess - left.lastAccess);
|
|
1387
|
+
},
|
|
1388
|
+
releaseProject(bucket) {
|
|
1389
|
+
return requireCache().deleteBucket(bucket);
|
|
1390
|
+
},
|
|
1391
|
+
releaseDigests(bucket, keys) {
|
|
1392
|
+
return requireCache().deleteFromBucket(bucket, keys);
|
|
1393
|
+
},
|
|
1394
|
+
totals() {
|
|
1395
|
+
return requireCache().getSize();
|
|
1396
|
+
},
|
|
1397
|
+
async close() {
|
|
1398
|
+
const open = cache;
|
|
1399
|
+
cache = null;
|
|
1400
|
+
if (open !== null) await open.close();
|
|
1401
|
+
}
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
//#endregion
|
|
1405
|
+
//#region src/extension-cache-dialog.ts
|
|
1406
|
+
/** How much of a digest is shown. Enough to tell two rows apart; the full value is the title. */
|
|
1407
|
+
var digestPreviewLength = 12;
|
|
1408
|
+
function requireLocales(value) {
|
|
1409
|
+
if (!isRecord(value) || Object.keys(value).length === 0) throw new TypeError("locales must contain at least one locale");
|
|
1410
|
+
return value;
|
|
1411
|
+
}
|
|
1412
|
+
function textFor(locales, locale) {
|
|
1413
|
+
const text = locales[locale] ?? locales["en"] ?? Object.values(locales)[0];
|
|
1414
|
+
if (text === void 0) throw new TypeError("locales must contain at least one locale");
|
|
1415
|
+
return text;
|
|
1416
|
+
}
|
|
1417
|
+
/** Fills `{name}` placeholders, so a locale decides where a count or a size sits in a sentence. */
|
|
1418
|
+
function fill(template, values) {
|
|
1419
|
+
return template.replace(/\{(\w+)\}/g, (match, name) => values[name] ?? match);
|
|
1420
|
+
}
|
|
1421
|
+
function defaultFormatSize(byteLength) {
|
|
1422
|
+
if (byteLength < 1024) return `${byteLength} B`;
|
|
1423
|
+
if (byteLength < 1048576) return `${(byteLength / 1024).toFixed(1)} KB`;
|
|
1424
|
+
return `${(byteLength / 1048576).toFixed(1)} MB`;
|
|
1425
|
+
}
|
|
1426
|
+
function defaultFormatDate(epochMilliseconds) {
|
|
1427
|
+
const parsed = new Date(epochMilliseconds);
|
|
1428
|
+
if (Number.isNaN(parsed.getTime())) return String(epochMilliseconds);
|
|
1429
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
1430
|
+
return `${parsed.getFullYear()}-${pad(parsed.getMonth() + 1)}-${pad(parsed.getDate())} ${pad(parsed.getHours())}:${pad(parsed.getMinutes())}`;
|
|
1431
|
+
}
|
|
1432
|
+
function defaultDescribeError(error) {
|
|
1433
|
+
if (error instanceof Error && error.message.length > 0) return error.message;
|
|
1434
|
+
return String(error);
|
|
1435
|
+
}
|
|
1436
|
+
function createExtensionCacheDialog(options) {
|
|
1437
|
+
if (!isRecord(options)) throw new TypeError("extension cache dialog options must be an object");
|
|
1438
|
+
const document = requireDocument(options.document ?? globalThis.document);
|
|
1439
|
+
const mount = requireElement(options.mount ?? document.body, "mount");
|
|
1440
|
+
const locales = requireLocales(options.locales);
|
|
1441
|
+
const describeError = options.describeError ?? defaultDescribeError;
|
|
1442
|
+
const formatSize = options.formatSize ?? defaultFormatSize;
|
|
1443
|
+
const formatDate = options.formatDate ?? defaultFormatDate;
|
|
1444
|
+
let locale = options.initialLocale ?? "en";
|
|
1445
|
+
let projects = [];
|
|
1446
|
+
let totals = {
|
|
1447
|
+
entries: 0,
|
|
1448
|
+
bytes: 0
|
|
1449
|
+
};
|
|
1450
|
+
let expandedBucket = null;
|
|
1451
|
+
let digests = [];
|
|
1452
|
+
let confirmingBucket = null;
|
|
1453
|
+
let confirmingDigest = null;
|
|
1454
|
+
let status = "";
|
|
1455
|
+
let result = "";
|
|
1456
|
+
/** Whether the list on screen came from a listing that finished. See `render`. */
|
|
1457
|
+
let listed = false;
|
|
1458
|
+
let disposed = false;
|
|
1459
|
+
const root = document.createElement("section");
|
|
1460
|
+
const panel = document.createElement("div");
|
|
1461
|
+
const heading = document.createElement("h1");
|
|
1462
|
+
const closeButton = document.createElement("button");
|
|
1463
|
+
const toolbar = document.createElement("div");
|
|
1464
|
+
const refreshButton = document.createElement("button");
|
|
1465
|
+
const totalElement = document.createElement("p");
|
|
1466
|
+
const noticeElement = document.createElement("p");
|
|
1467
|
+
const listElement = document.createElement("div");
|
|
1468
|
+
const resultElement = document.createElement("p");
|
|
1469
|
+
const statusElement = document.createElement("p");
|
|
1470
|
+
root.setAttribute("data-turbowarp-title-menu-extension-cache", "true");
|
|
1471
|
+
root.style.cssText = "position:absolute;inset:0;z-index:2147483646;align-items:center;justify-content:center;padding:16px;box-sizing:border-box;background:rgba(0,0,0,.6);font-family:sans-serif;";
|
|
1472
|
+
root.style.display = "none";
|
|
1473
|
+
panel.style.cssText = "width:min(760px,96%);max-height:90%;display:flex;flex-direction:column;gap:10px;padding:18px;box-sizing:border-box;background:#ffffff;color:#1b1f27;border-radius:10px;box-shadow:0 10px 36px rgba(0,0,0,.45);overflow:hidden;";
|
|
1474
|
+
heading.style.cssText = "margin:0;font-size:20px;line-height:1.3;";
|
|
1475
|
+
toolbar.style.cssText = "display:flex;gap:8px;align-items:center;flex-wrap:wrap;";
|
|
1476
|
+
totalElement.style.cssText = "margin:0;font-size:13px;color:#1b1f27;";
|
|
1477
|
+
noticeElement.style.cssText = "margin:0;padding:8px 10px;font-size:12px;line-height:1.5;color:#45506a;background:#f2f4f8;border:1px solid #ccd2de;border-radius:8px;";
|
|
1478
|
+
listElement.style.cssText = "flex:1;min-height:0;overflow:auto;display:flex;flex-direction:column;gap:6px;";
|
|
1479
|
+
resultElement.style.cssText = "margin:0;min-height:18px;font-size:13px;color:#1f6feb;";
|
|
1480
|
+
statusElement.style.cssText = "margin:0;min-height:18px;font-size:13px;color:#8a1c1c;";
|
|
1481
|
+
const headerRow = document.createElement("div");
|
|
1482
|
+
headerRow.style.cssText = "display:flex;align-items:center;justify-content:space-between;gap:8px;";
|
|
1483
|
+
headerRow.appendChild(heading);
|
|
1484
|
+
headerRow.appendChild(closeButton);
|
|
1485
|
+
panel.appendChild(headerRow);
|
|
1486
|
+
toolbar.appendChild(refreshButton);
|
|
1487
|
+
toolbar.appendChild(totalElement);
|
|
1488
|
+
panel.appendChild(toolbar);
|
|
1489
|
+
panel.appendChild(noticeElement);
|
|
1490
|
+
panel.appendChild(listElement);
|
|
1491
|
+
panel.appendChild(resultElement);
|
|
1492
|
+
panel.appendChild(statusElement);
|
|
1493
|
+
root.appendChild(panel);
|
|
1494
|
+
const previousMountPosition = mount.style.position;
|
|
1495
|
+
if (previousMountPosition === "" || previousMountPosition === "static") mount.style.position = "relative";
|
|
1496
|
+
mount.appendChild(root);
|
|
1497
|
+
function styleButton(button, tone) {
|
|
1498
|
+
const palette = {
|
|
1499
|
+
primary: "background:#1f6feb;color:#ffffff;border:1px solid #1f6feb;",
|
|
1500
|
+
plain: "background:#f2f4f8;color:#1b1f27;border:1px solid #ccd2de;",
|
|
1501
|
+
danger: "background:#b42318;color:#ffffff;border:1px solid #b42318;",
|
|
1502
|
+
active: "background:#d7e6ff;color:#0b3a82;border:1px solid #1f6feb;"
|
|
1503
|
+
}[tone];
|
|
1504
|
+
button.style.cssText = `${palette}padding:5px 10px;border-radius:6px;font-size:13px;cursor:pointer;`;
|
|
1505
|
+
}
|
|
1506
|
+
function createButton(label, tone, onClick) {
|
|
1507
|
+
const button = document.createElement("button");
|
|
1508
|
+
button.type = "button";
|
|
1509
|
+
button.textContent = label;
|
|
1510
|
+
styleButton(button, tone);
|
|
1511
|
+
button.addEventListener("click", onClick);
|
|
1512
|
+
return button;
|
|
1513
|
+
}
|
|
1514
|
+
function run(operation) {
|
|
1515
|
+
try {
|
|
1516
|
+
Promise.resolve(operation()).catch(handleFailure);
|
|
1517
|
+
} catch (error) {
|
|
1518
|
+
handleFailure(error);
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
function handleFailure(error) {
|
|
1522
|
+
status = describeError(error);
|
|
1523
|
+
options.onError?.(error);
|
|
1524
|
+
render();
|
|
1525
|
+
}
|
|
1526
|
+
/** Every release goes through here, so the list, the total, and the status stay in step. */
|
|
1527
|
+
function release(operation) {
|
|
1528
|
+
run(async () => {
|
|
1529
|
+
status = "";
|
|
1530
|
+
try {
|
|
1531
|
+
const released = await operation();
|
|
1532
|
+
result = fill(textFor(locales, locale).releaseResult, {
|
|
1533
|
+
released: String(released.released),
|
|
1534
|
+
freed: formatSize(released.freed)
|
|
1535
|
+
});
|
|
1536
|
+
} catch (error) {
|
|
1537
|
+
handleFailure(error);
|
|
1538
|
+
return;
|
|
1539
|
+
}
|
|
1540
|
+
await reload();
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1543
|
+
/**
|
|
1544
|
+
* Reloads the whole screen.
|
|
1545
|
+
*
|
|
1546
|
+
* The expanded bucket is re-listed rather than filtered out of a stale list, because releasing
|
|
1547
|
+
* a digest from one bucket can remove bytes a second bucket claimed, which changes totals the
|
|
1548
|
+
* dialog never asked about. Asking again is cheap; guessing would be wrong.
|
|
1549
|
+
*/
|
|
1550
|
+
async function reload() {
|
|
1551
|
+
if (disposed) return;
|
|
1552
|
+
listed = false;
|
|
1553
|
+
projects = await options.listProjects();
|
|
1554
|
+
totals = await options.totals();
|
|
1555
|
+
if (expandedBucket !== null && !projects.some((entry) => entry.bucket === expandedBucket)) expandedBucket = null;
|
|
1556
|
+
digests = expandedBucket === null ? [] : await options.listDigests(expandedBucket);
|
|
1557
|
+
listed = true;
|
|
1558
|
+
render();
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* Reloads without letting the failure escape.
|
|
1562
|
+
*
|
|
1563
|
+
* A listing that failed must not leave the panel showing an empty list: "nothing is cached" and
|
|
1564
|
+
* "the cache could not be read" are different claims, and only one of them is true. So the
|
|
1565
|
+
* status line carries the failure while the panel stays open, instead of the promise rejecting
|
|
1566
|
+
* into a caller that has no way to say so on screen.
|
|
1567
|
+
*/
|
|
1568
|
+
async function reloadSafely() {
|
|
1569
|
+
try {
|
|
1570
|
+
await reload();
|
|
1571
|
+
} catch (error) {
|
|
1572
|
+
handleFailure(error);
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
function toggleExpanded(bucket) {
|
|
1576
|
+
expandedBucket = expandedBucket === bucket ? null : bucket;
|
|
1577
|
+
confirmingBucket = null;
|
|
1578
|
+
confirmingDigest = null;
|
|
1579
|
+
run(() => reload());
|
|
1580
|
+
}
|
|
1581
|
+
function renderDigestRow(digest, bucket, text) {
|
|
1582
|
+
const row = document.createElement("div");
|
|
1583
|
+
row.setAttribute("data-cache-digest", digest.key);
|
|
1584
|
+
row.style.cssText = "display:flex;align-items:center;gap:8px;padding:6px 8px;margin-left:18px;border:1px solid #eef1f6;border-radius:6px;";
|
|
1585
|
+
const key = document.createElement("span");
|
|
1586
|
+
key.textContent = `${digest.key.slice(0, digestPreviewLength)}…`;
|
|
1587
|
+
key.setAttribute("title", digest.key);
|
|
1588
|
+
key.style.cssText = "flex:1;min-width:0;font-family:monospace;font-size:12px;color:#1b1f27;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;";
|
|
1589
|
+
const used = document.createElement("span");
|
|
1590
|
+
used.textContent = fill(text.lastUsed, { date: formatDate(digest.lastAccess) });
|
|
1591
|
+
used.style.cssText = "font-size:12px;color:#5a6172;white-space:nowrap;";
|
|
1592
|
+
const size = document.createElement("span");
|
|
1593
|
+
size.textContent = formatSize(digest.bytes);
|
|
1594
|
+
size.setAttribute("title", fill(text.originalSize, { bytes: formatSize(digest.originalBytes) }));
|
|
1595
|
+
size.style.cssText = "font-size:12px;color:#5a6172;white-space:nowrap;min-width:64px;text-align:right;";
|
|
1596
|
+
row.appendChild(key);
|
|
1597
|
+
row.appendChild(used);
|
|
1598
|
+
row.appendChild(size);
|
|
1599
|
+
if (confirmingDigest === digest.key) {
|
|
1600
|
+
row.appendChild(createButton(text.confirmRelease, "danger", () => {
|
|
1601
|
+
confirmingDigest = null;
|
|
1602
|
+
release(() => options.onReleaseDigest(bucket, digest.key));
|
|
1603
|
+
}));
|
|
1604
|
+
row.appendChild(createButton(text.cancel, "plain", () => {
|
|
1605
|
+
confirmingDigest = null;
|
|
1606
|
+
render();
|
|
1607
|
+
}));
|
|
1608
|
+
return row;
|
|
1609
|
+
}
|
|
1610
|
+
row.appendChild(createButton(text.release, "plain", () => {
|
|
1611
|
+
confirmingDigest = digest.key;
|
|
1612
|
+
confirmingBucket = null;
|
|
1613
|
+
render();
|
|
1614
|
+
}));
|
|
1615
|
+
return row;
|
|
1616
|
+
}
|
|
1617
|
+
function renderProjectRow(project, text) {
|
|
1618
|
+
const row = document.createElement("div");
|
|
1619
|
+
row.setAttribute("data-cache-bucket", project.bucket);
|
|
1620
|
+
row.style.cssText = "display:flex;align-items:center;gap:8px;padding:8px;border:1px solid #e2e6ee;border-radius:8px;";
|
|
1621
|
+
const names = document.createElement("div");
|
|
1622
|
+
names.style.cssText = "flex:1;min-width:0;display:flex;flex-direction:column;gap:2px;";
|
|
1623
|
+
const title = document.createElement("span");
|
|
1624
|
+
title.textContent = project.shared ? text.shared : project.title ?? text.untitled;
|
|
1625
|
+
title.style.cssText = "font-size:14px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;";
|
|
1626
|
+
names.appendChild(title);
|
|
1627
|
+
if (!project.shared) {
|
|
1628
|
+
const uri = document.createElement("span");
|
|
1629
|
+
uri.textContent = project.bucket;
|
|
1630
|
+
uri.setAttribute("title", project.bucket);
|
|
1631
|
+
uri.style.cssText = "font-size:11px;font-family:monospace;color:#5a6172;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;";
|
|
1632
|
+
names.appendChild(uri);
|
|
1633
|
+
}
|
|
1634
|
+
row.appendChild(names);
|
|
1635
|
+
const count = document.createElement("span");
|
|
1636
|
+
count.textContent = fill(text.entryCount, { entries: String(project.entries) });
|
|
1637
|
+
count.style.cssText = "font-size:12px;color:#5a6172;white-space:nowrap;";
|
|
1638
|
+
const size = document.createElement("span");
|
|
1639
|
+
size.textContent = formatSize(project.bytes);
|
|
1640
|
+
size.style.cssText = "font-size:12px;color:#5a6172;white-space:nowrap;min-width:64px;text-align:right;";
|
|
1641
|
+
row.appendChild(count);
|
|
1642
|
+
row.appendChild(size);
|
|
1643
|
+
const expanded = expandedBucket === project.bucket;
|
|
1644
|
+
const expandButton = createButton(expanded ? text.collapse : text.expand, expanded ? "active" : "primary", () => toggleExpanded(project.bucket));
|
|
1645
|
+
expandButton.setAttribute("data-cache-expand", project.bucket);
|
|
1646
|
+
expandButton.setAttribute("aria-expanded", expanded ? "true" : "false");
|
|
1647
|
+
row.appendChild(expandButton);
|
|
1648
|
+
if (confirmingBucket === project.bucket) {
|
|
1649
|
+
row.appendChild(createButton(text.confirmRelease, "danger", () => {
|
|
1650
|
+
confirmingBucket = null;
|
|
1651
|
+
release(() => options.onReleaseProject(project.bucket));
|
|
1652
|
+
}));
|
|
1653
|
+
row.appendChild(createButton(text.cancel, "plain", () => {
|
|
1654
|
+
confirmingBucket = null;
|
|
1655
|
+
render();
|
|
1656
|
+
}));
|
|
1657
|
+
return row;
|
|
1658
|
+
}
|
|
1659
|
+
row.appendChild(createButton(text.release, "plain", () => {
|
|
1660
|
+
confirmingBucket = project.bucket;
|
|
1661
|
+
confirmingDigest = null;
|
|
1662
|
+
render();
|
|
1663
|
+
}));
|
|
1664
|
+
return row;
|
|
1665
|
+
}
|
|
1666
|
+
function render() {
|
|
1667
|
+
if (disposed) return;
|
|
1668
|
+
const text = textFor(locales, locale);
|
|
1669
|
+
heading.textContent = text.title;
|
|
1670
|
+
closeButton.textContent = text.close;
|
|
1671
|
+
closeButton.setAttribute("aria-label", text.close);
|
|
1672
|
+
refreshButton.textContent = text.refresh;
|
|
1673
|
+
noticeElement.textContent = text.notice;
|
|
1674
|
+
totalElement.textContent = fill(text.total, {
|
|
1675
|
+
entries: String(totals.entries),
|
|
1676
|
+
bytes: formatSize(totals.bytes)
|
|
1677
|
+
});
|
|
1678
|
+
resultElement.textContent = result;
|
|
1679
|
+
statusElement.textContent = status;
|
|
1680
|
+
listElement.replaceChildren();
|
|
1681
|
+
if (projects.length === 0) {
|
|
1682
|
+
if (!listed) return;
|
|
1683
|
+
const empty = document.createElement("p");
|
|
1684
|
+
empty.textContent = text.empty;
|
|
1685
|
+
empty.style.cssText = "margin:8px 0;font-size:14px;color:#5a6172;";
|
|
1686
|
+
listElement.appendChild(empty);
|
|
1687
|
+
return;
|
|
1688
|
+
}
|
|
1689
|
+
for (const project of projects) {
|
|
1690
|
+
listElement.appendChild(renderProjectRow(project, text));
|
|
1691
|
+
if (expandedBucket !== project.bucket) continue;
|
|
1692
|
+
for (const digest of digests) listElement.appendChild(renderDigestRow(digest, project.bucket, text));
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
styleButton(closeButton, "plain");
|
|
1696
|
+
styleButton(refreshButton, "primary");
|
|
1697
|
+
closeButton.type = "button";
|
|
1698
|
+
refreshButton.type = "button";
|
|
1699
|
+
closeButton.addEventListener("click", () => hide());
|
|
1700
|
+
refreshButton.addEventListener("click", () => {
|
|
1701
|
+
result = "";
|
|
1702
|
+
status = "";
|
|
1703
|
+
run(() => reload());
|
|
1704
|
+
});
|
|
1705
|
+
function hide() {
|
|
1706
|
+
if (disposed) return;
|
|
1707
|
+
confirmingBucket = null;
|
|
1708
|
+
confirmingDigest = null;
|
|
1709
|
+
root.style.display = "none";
|
|
1710
|
+
}
|
|
1711
|
+
function ensureActive() {
|
|
1712
|
+
if (disposed) throw new TypeError("extension cache dialog is disposed");
|
|
1713
|
+
}
|
|
1714
|
+
render();
|
|
1715
|
+
return Object.freeze({
|
|
1716
|
+
element: root,
|
|
1717
|
+
get locale() {
|
|
1718
|
+
return locale;
|
|
1719
|
+
},
|
|
1720
|
+
get expanded() {
|
|
1721
|
+
return expandedBucket;
|
|
1722
|
+
},
|
|
1723
|
+
async show(nextLocale) {
|
|
1724
|
+
ensureActive();
|
|
1725
|
+
if (nextLocale !== void 0) locale = nextLocale;
|
|
1726
|
+
status = "";
|
|
1727
|
+
result = "";
|
|
1728
|
+
root.style.display = "flex";
|
|
1729
|
+
await reloadSafely();
|
|
1730
|
+
return locale;
|
|
1731
|
+
},
|
|
1732
|
+
hide,
|
|
1733
|
+
async refresh() {
|
|
1734
|
+
ensureActive();
|
|
1735
|
+
await reload();
|
|
1736
|
+
},
|
|
1737
|
+
setLocale(nextLocale) {
|
|
1738
|
+
ensureActive();
|
|
1739
|
+
locale = nextLocale;
|
|
1740
|
+
render();
|
|
1741
|
+
return locale;
|
|
1742
|
+
},
|
|
1743
|
+
dispose() {
|
|
1744
|
+
if (disposed) return;
|
|
1745
|
+
disposed = true;
|
|
1746
|
+
root.remove();
|
|
1747
|
+
if (previousMountPosition === "" || previousMountPosition === "static") mount.style.position = previousMountPosition;
|
|
1748
|
+
}
|
|
1749
|
+
});
|
|
1750
|
+
}
|
|
1751
|
+
//#endregion
|
|
993
1752
|
//#region src/title-dialog.ts
|
|
994
1753
|
function requireLocaleText(locales, locale) {
|
|
995
1754
|
const text = locales[locale] ?? locales.en ?? Object.values(locales)[0];
|
|
@@ -1218,6 +1977,28 @@ var block_definitions_default = {
|
|
|
1218
1977
|
"blockType": "REPORTER",
|
|
1219
1978
|
"text": "last DSL storage error",
|
|
1220
1979
|
"description": "Returns the most recent DSL storage failure in the interface language, or an empty string."
|
|
1980
|
+
},
|
|
1981
|
+
{
|
|
1982
|
+
"opcode": "showExtensionCache",
|
|
1983
|
+
"blockType": "COMMAND",
|
|
1984
|
+
"text": "show extension cache manager",
|
|
1985
|
+
"description": "Shows the dialog that lists the cached extension bytes per project and releases them."
|
|
1986
|
+
},
|
|
1987
|
+
{
|
|
1988
|
+
"opcode": "setCachedProjectTitle",
|
|
1989
|
+
"blockType": "COMMAND",
|
|
1990
|
+
"text": "label cached project [URI] as [TITLE]",
|
|
1991
|
+
"description": "Records a readable title to show beside a project URI in the extension cache manager. A project URI is deliberately opaque, so without a title the rows cannot be told apart; the title is self-declared and is never checked against the URI.",
|
|
1992
|
+
"arguments": {
|
|
1993
|
+
"URI": {
|
|
1994
|
+
"type": "STRING",
|
|
1995
|
+
"defaultValue": "https://example.com/turbowarp-plus-projects/ab/cd/project"
|
|
1996
|
+
},
|
|
1997
|
+
"TITLE": {
|
|
1998
|
+
"type": "STRING",
|
|
1999
|
+
"defaultValue": "My project"
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
1221
2002
|
}
|
|
1222
2003
|
]
|
|
1223
2004
|
};
|
|
@@ -1242,12 +2023,14 @@ var titleLocales = Object.freeze({
|
|
|
1242
2023
|
var menuLocales = Object.freeze({
|
|
1243
2024
|
en: {
|
|
1244
2025
|
files: "DSL files",
|
|
2026
|
+
cache: "Extension cache",
|
|
1245
2027
|
reload: "Reload DSL",
|
|
1246
2028
|
about: "About",
|
|
1247
2029
|
close: "Close"
|
|
1248
2030
|
},
|
|
1249
2031
|
ja: {
|
|
1250
2032
|
files: "DSLファイル",
|
|
2033
|
+
cache: "機能拡張キャッシュ",
|
|
1251
2034
|
reload: "DSLを再読み込み",
|
|
1252
2035
|
about: "情報",
|
|
1253
2036
|
close: "閉じる"
|
|
@@ -1285,6 +2068,81 @@ var dslFilesLocales = Object.freeze({
|
|
|
1285
2068
|
empty: "保存されたDSLファイルはありません。「ファイルを追加」から保存してください。"
|
|
1286
2069
|
}
|
|
1287
2070
|
});
|
|
2071
|
+
/**
|
|
2072
|
+
* Wording for the extension cache screen.
|
|
2073
|
+
*
|
|
2074
|
+
* The notice is the load-bearing part, and it says three things on purpose. Releasing is phrased
|
|
2075
|
+
* as letting go of what a project *names*, never as deleting what it owns, because the cache is
|
|
2076
|
+
* addressed by content and one copy can be claimed by several projects. It states that nothing is
|
|
2077
|
+
* lost for good, so that an operator freeing space is not frightened into leaving it alone. And it
|
|
2078
|
+
* states that a project URI is self-declared, the same admission the DSL dialog and the security
|
|
2079
|
+
* dialog make about an author's name — the Loader re-checks every digest on use, so a project
|
|
2080
|
+
* that files its bytes under somebody else's URI corrupts this list and nothing else.
|
|
2081
|
+
*/
|
|
2082
|
+
var extensionCacheLocales = Object.freeze({
|
|
2083
|
+
en: {
|
|
2084
|
+
title: "Extension cache",
|
|
2085
|
+
notice: "Cached entries are addressed by their contents, not owned by a project. Releasing a project drops the digests that project names, which can take bytes another project would have reused. Nothing is lost for good: whatever is still needed is downloaded again the next time a project is opened. Each URI below is what that project called itself and is not verified; the loader re-checks every digest when it uses it, so a wrong URI only misleads this list.",
|
|
2086
|
+
total: "Cached in all: {entries} entries, {bytes}",
|
|
2087
|
+
shared: "Shared by every project (pinned by the catalog)",
|
|
2088
|
+
untitled: "(no title recorded)",
|
|
2089
|
+
entryCount: "{entries} entries",
|
|
2090
|
+
expand: "Entries",
|
|
2091
|
+
collapse: "Fold",
|
|
2092
|
+
release: "Release",
|
|
2093
|
+
confirmRelease: "Release?",
|
|
2094
|
+
cancel: "Cancel",
|
|
2095
|
+
close: "Close",
|
|
2096
|
+
refresh: "Reload",
|
|
2097
|
+
empty: "Nothing is cached. Opening a project that uses extensions fills this.",
|
|
2098
|
+
releaseResult: "Released {released} entries and freed {freed}.",
|
|
2099
|
+
lastUsed: "used {date}",
|
|
2100
|
+
originalSize: "{bytes} of JavaScript"
|
|
2101
|
+
},
|
|
2102
|
+
ja: {
|
|
2103
|
+
title: "機能拡張キャッシュ",
|
|
2104
|
+
notice: "キャッシュは内容で参照されており、作品が所有しているわけではありません。作品を手放すと、その作品が名指すdigestを落とします。別の作品が使い回したはずのバイト列まで落ちることがあります。失われたままにはなりません。必要なものは、次にその作品を開いたときに取り直します。下に出るURIは、その作品自身の申告で、検証していません。Loaderは使うたびにdigestを照合するので、URIが誤っていても狂うのはこの一覧の見え方だけです。",
|
|
2105
|
+
total: "キャッシュ全体: {entries}件、{bytes}",
|
|
2106
|
+
shared: "すべての作品で共有(カタログが指定したもの)",
|
|
2107
|
+
untitled: "(タイトル未登録)",
|
|
2108
|
+
entryCount: "{entries}件",
|
|
2109
|
+
expand: "中身",
|
|
2110
|
+
collapse: "たたむ",
|
|
2111
|
+
release: "手放す",
|
|
2112
|
+
confirmRelease: "手放す?",
|
|
2113
|
+
cancel: "やめる",
|
|
2114
|
+
close: "閉じる",
|
|
2115
|
+
refresh: "読み直す",
|
|
2116
|
+
empty: "キャッシュは空です。機能拡張を使う作品を開くと入ります。",
|
|
2117
|
+
releaseResult: "{released}件を手放し、{freed}を空けました。",
|
|
2118
|
+
lastUsed: "最終利用 {date}",
|
|
2119
|
+
originalSize: "JavaScript {bytes}"
|
|
2120
|
+
}
|
|
2121
|
+
});
|
|
2122
|
+
var cacheErrorMessages = Object.freeze({
|
|
2123
|
+
en: {
|
|
2124
|
+
unavailable: "Browser storage is unavailable, so the extension cache cannot be read.",
|
|
2125
|
+
failed: "The extension cache operation failed."
|
|
2126
|
+
},
|
|
2127
|
+
ja: {
|
|
2128
|
+
unavailable: "ブラウザの保存領域が使えないため、機能拡張キャッシュを読めません。",
|
|
2129
|
+
failed: "機能拡張キャッシュの操作に失敗しました。"
|
|
2130
|
+
}
|
|
2131
|
+
});
|
|
2132
|
+
/**
|
|
2133
|
+
* Turns a cache failure into operator-facing text.
|
|
2134
|
+
*
|
|
2135
|
+
* Only the codes this package assigns itself are translated. A failure raised inside IndexedDB
|
|
2136
|
+
* keeps its own message, because inventing a friendlier sentence for an unclassified error would
|
|
2137
|
+
* hide the one detail that makes it reportable.
|
|
2138
|
+
*/
|
|
2139
|
+
function describeCacheError(locale, error) {
|
|
2140
|
+
const code = error?.code;
|
|
2141
|
+
const messages = cacheErrorMessages[locale] ?? cacheErrorMessages.en;
|
|
2142
|
+
if (code !== void 0 && code in messages) return messages[code];
|
|
2143
|
+
if (error instanceof Error && error.message.length > 0) return error.message;
|
|
2144
|
+
return String(error);
|
|
2145
|
+
}
|
|
1288
2146
|
var storeErrorMessages = Object.freeze({
|
|
1289
2147
|
en: {
|
|
1290
2148
|
unavailable: "Browser storage is unavailable, so DSL files cannot be saved.",
|
|
@@ -1324,6 +2182,7 @@ var dslFileAccept = ".txt,.yaml,.yml,.json,.k4,.kamishibai";
|
|
|
1324
2182
|
/** Reserved action IDs the extension handles itself. A project may still remove or relabel them. */
|
|
1325
2183
|
var builtinActionIds = [
|
|
1326
2184
|
"files",
|
|
2185
|
+
"cache",
|
|
1327
2186
|
"reload",
|
|
1328
2187
|
"about",
|
|
1329
2188
|
"close"
|
|
@@ -1336,7 +2195,10 @@ var TurboWarpTitleMenuExtension = class {
|
|
|
1336
2195
|
this.titleDialog = null;
|
|
1337
2196
|
this.applicationMenu = null;
|
|
1338
2197
|
this.filesDialog = null;
|
|
2198
|
+
this.cacheDialog = null;
|
|
1339
2199
|
this.store = null;
|
|
2200
|
+
this.cache = null;
|
|
2201
|
+
this.projectTitles = /* @__PURE__ */ new Map();
|
|
1340
2202
|
this.openedRecord = null;
|
|
1341
2203
|
this.lastError = "";
|
|
1342
2204
|
this.menuActions = builtinActionIds.map((id) => ({
|
|
@@ -1430,6 +2292,25 @@ var TurboWarpTitleMenuExtension = class {
|
|
|
1430
2292
|
showDslFiles() {
|
|
1431
2293
|
return this.ensureFilesDialog().show(this.locale());
|
|
1432
2294
|
}
|
|
2295
|
+
showExtensionCache() {
|
|
2296
|
+
return this.ensureCacheDialog().show(this.locale());
|
|
2297
|
+
}
|
|
2298
|
+
/**
|
|
2299
|
+
* Records a title to show beside one project URI.
|
|
2300
|
+
*
|
|
2301
|
+
* An empty URI is ignored because there is nothing to label, and an empty title forgets the
|
|
2302
|
+
* label rather than showing a blank row: that is the only way a project can take one back.
|
|
2303
|
+
*/
|
|
2304
|
+
setCachedProjectTitle(args) {
|
|
2305
|
+
const uri = Scratch.Cast.toString(args.URI).trim();
|
|
2306
|
+
if (uri.length === 0) return;
|
|
2307
|
+
const title = Scratch.Cast.toString(args.TITLE).trim();
|
|
2308
|
+
if (title.length === 0) {
|
|
2309
|
+
this.projectTitles.delete(uri);
|
|
2310
|
+
return;
|
|
2311
|
+
}
|
|
2312
|
+
this.projectTitles.set(uri, title);
|
|
2313
|
+
}
|
|
1433
2314
|
/** The hat is started by the open path, so its own handler only has to accept the match. */
|
|
1434
2315
|
whenDslSourceOpened() {
|
|
1435
2316
|
return true;
|
|
@@ -1471,6 +2352,17 @@ var TurboWarpTitleMenuExtension = class {
|
|
|
1471
2352
|
this.store ?? (this.store = createDslStore({ databaseName: extensionConfig.id }));
|
|
1472
2353
|
return this.store;
|
|
1473
2354
|
}
|
|
2355
|
+
/**
|
|
2356
|
+
* Opens the extension cache on first use.
|
|
2357
|
+
*
|
|
2358
|
+
* No database name is passed: the Loader wrote those bytes and the Loader owns the name of the
|
|
2359
|
+
* place they are in. Naming it here would put the same string in two packages, which is the
|
|
2360
|
+
* cross-layer mismatch this family keeps rediscovering.
|
|
2361
|
+
*/
|
|
2362
|
+
requireCache() {
|
|
2363
|
+
this.cache ?? (this.cache = createExtensionCache({ titleFor: (bucket) => this.projectTitles.get(bucket) }));
|
|
2364
|
+
return this.cache;
|
|
2365
|
+
}
|
|
1474
2366
|
recordFailure(error) {
|
|
1475
2367
|
this.lastError = describeStoreError(this.locale(), error);
|
|
1476
2368
|
}
|
|
@@ -1496,6 +2388,7 @@ var TurboWarpTitleMenuExtension = class {
|
|
|
1496
2388
|
Scratch.vm?.runtime?.startHats?.(`${extensionConfig.id}_whenAppMenuActionSelected`, { ACTION: id });
|
|
1497
2389
|
if (!builtinActionIds.includes(id)) return;
|
|
1498
2390
|
if (id === "files") this.showDslFiles();
|
|
2391
|
+
if (id === "cache") this.showExtensionCache();
|
|
1499
2392
|
if (id === "reload") this.reloadOpenedDsl();
|
|
1500
2393
|
if (id === "about") this.showTitle();
|
|
1501
2394
|
if (id === "close") this.hideMenu();
|
|
@@ -1562,6 +2455,33 @@ var TurboWarpTitleMenuExtension = class {
|
|
|
1562
2455
|
});
|
|
1563
2456
|
return this.filesDialog;
|
|
1564
2457
|
}
|
|
2458
|
+
/**
|
|
2459
|
+
* Builds the cache dialog.
|
|
2460
|
+
*
|
|
2461
|
+
* Cache failures go to `lastDslError` through the same reporter, because an operator has one
|
|
2462
|
+
* place to look for "what went wrong with storage" and the two stores fail for the same
|
|
2463
|
+
* reasons — blocked storage, a full quota — even though they are separate databases.
|
|
2464
|
+
*/
|
|
2465
|
+
ensureCacheDialog() {
|
|
2466
|
+
if (this.cacheDialog) return this.cacheDialog;
|
|
2467
|
+
const mount = stageMount();
|
|
2468
|
+
this.cacheDialog = createExtensionCacheDialog({
|
|
2469
|
+
document: globalThis.document,
|
|
2470
|
+
...mount ? { mount } : {},
|
|
2471
|
+
locales: extensionCacheLocales,
|
|
2472
|
+
initialLocale: this.locale(),
|
|
2473
|
+
listProjects: () => this.requireCache().listProjects(),
|
|
2474
|
+
listDigests: (bucket) => this.requireCache().listDigests(bucket),
|
|
2475
|
+
totals: () => this.requireCache().totals(),
|
|
2476
|
+
onReleaseProject: (bucket) => this.requireCache().releaseProject(bucket),
|
|
2477
|
+
onReleaseDigest: (bucket, key) => this.requireCache().releaseDigests(bucket, [key]),
|
|
2478
|
+
describeError: (error) => describeCacheError(this.locale(), error),
|
|
2479
|
+
onError: (error) => {
|
|
2480
|
+
this.lastError = describeCacheError(this.locale(), error);
|
|
2481
|
+
}
|
|
2482
|
+
});
|
|
2483
|
+
return this.cacheDialog;
|
|
2484
|
+
}
|
|
1565
2485
|
async addDslFile() {
|
|
1566
2486
|
const chosen = await this.pickDslFile();
|
|
1567
2487
|
if (chosen === null) return;
|
|
@@ -1616,6 +2536,8 @@ var publicApi = Object.freeze({
|
|
|
1616
2536
|
createApplicationMenu: createAppShellApplicationMenu,
|
|
1617
2537
|
createDslFilesDialog,
|
|
1618
2538
|
createDslStore,
|
|
2539
|
+
createExtensionCache,
|
|
2540
|
+
createExtensionCacheDialog,
|
|
1619
2541
|
createTitleDialog,
|
|
1620
2542
|
dslOpenEventName,
|
|
1621
2543
|
dslReloadEventName
|