@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.
@@ -0,0 +1,91 @@
1
+ import { createIndexedDBContentCache, SHARED_BUCKET } from '@kubohiroya/turbowarp-extension-loader/indexeddb-cache';
2
+ export class ExtensionCacheError extends Error {
3
+ constructor(code, message, cause) {
4
+ super(message, cause === undefined ? undefined : { cause });
5
+ this.name = 'ExtensionCacheError';
6
+ this.code = code;
7
+ }
8
+ }
9
+ export { SHARED_BUCKET };
10
+ /**
11
+ * Compares two buckets for display.
12
+ *
13
+ * The shared bucket leads because it is the one row that is not a project: it holds what the
14
+ * catalog pinned for everyone, and an operator hunting for space should see it before deciding
15
+ * that some individual project is to blame. The rest follow by stored size, because size is the
16
+ * only reason anyone opens this screen; the bucket name breaks ties so the order is stable
17
+ * between listings rather than reflecting insertion order.
18
+ */
19
+ function compareBuckets(left, right) {
20
+ if (left.shared !== right.shared)
21
+ return left.shared ? -1 : 1;
22
+ if (left.bytes !== right.bytes)
23
+ return right.bytes - left.bytes;
24
+ return left.bucket < right.bucket ? -1 : left.bucket > right.bucket ? 1 : 0;
25
+ }
26
+ export function createExtensionCache(options = {}) {
27
+ let cache = null;
28
+ /**
29
+ * Opens the cache on first use.
30
+ *
31
+ * A browser with storage blocked fails here rather than when the dialog is built, so the menu
32
+ * entry stays clickable and the failure is reported in the dialog's own status line.
33
+ */
34
+ function requireCache() {
35
+ if (cache !== null)
36
+ return cache;
37
+ try {
38
+ cache = createIndexedDBContentCache({
39
+ ...(options.indexedDB === undefined ? {} : { indexedDB: options.indexedDB }),
40
+ ...(options.databaseName === undefined ? {} : { databaseName: options.databaseName })
41
+ });
42
+ }
43
+ catch (error) {
44
+ throw new ExtensionCacheError('unavailable', 'the extension cache is unavailable', error);
45
+ }
46
+ return cache;
47
+ }
48
+ return Object.freeze({
49
+ async listProjects() {
50
+ const buckets = await requireCache().listBuckets();
51
+ return buckets
52
+ .map((bucket) => {
53
+ const title = options.titleFor?.(bucket.bucket);
54
+ return {
55
+ bucket: bucket.bucket,
56
+ shared: bucket.bucket === SHARED_BUCKET,
57
+ ...(title === undefined || title.length === 0 ? {} : { title }),
58
+ entries: bucket.entries,
59
+ bytes: bucket.bytes
60
+ };
61
+ })
62
+ .sort(compareBuckets);
63
+ },
64
+ async listDigests(bucket) {
65
+ const usage = await requireCache().listBucket(bucket);
66
+ return usage
67
+ .map((entry) => ({
68
+ key: entry.key,
69
+ bytes: entry.bytes,
70
+ originalBytes: entry.originalBytes,
71
+ lastAccess: entry.lastAccess
72
+ }))
73
+ .sort((left, right) => right.lastAccess - left.lastAccess);
74
+ },
75
+ releaseProject(bucket) {
76
+ return requireCache().deleteBucket(bucket);
77
+ },
78
+ releaseDigests(bucket, keys) {
79
+ return requireCache().deleteFromBucket(bucket, keys);
80
+ },
81
+ totals() {
82
+ return requireCache().getSize();
83
+ },
84
+ async close() {
85
+ const open = cache;
86
+ cache = null;
87
+ if (open !== null)
88
+ await open.close();
89
+ }
90
+ });
91
+ }
@@ -15,8 +15,20 @@ export const titleLocales = Object.freeze({
15
15
  }
16
16
  });
17
17
  export const menuLocales = Object.freeze({
18
- en: { files: 'DSL files', reload: 'Reload DSL', about: 'About', close: 'Close' },
19
- ja: { files: 'DSLファイル', reload: 'DSLを再読み込み', about: '情報', close: '閉じる' }
18
+ en: {
19
+ files: 'DSL files',
20
+ cache: 'Extension cache',
21
+ reload: 'Reload DSL',
22
+ about: 'About',
23
+ close: 'Close'
24
+ },
25
+ ja: {
26
+ files: 'DSLファイル',
27
+ cache: '機能拡張キャッシュ',
28
+ reload: 'DSLを再読み込み',
29
+ about: '情報',
30
+ close: '閉じる'
31
+ }
20
32
  });
21
33
  export const dslFilesLocales = Object.freeze({
22
34
  en: {
@@ -50,6 +62,83 @@ export const dslFilesLocales = Object.freeze({
50
62
  empty: '保存されたDSLファイルはありません。「ファイルを追加」から保存してください。'
51
63
  }
52
64
  });
65
+ /**
66
+ * Wording for the extension cache screen.
67
+ *
68
+ * The notice is the load-bearing part, and it says three things on purpose. Releasing is phrased
69
+ * as letting go of what a project *names*, never as deleting what it owns, because the cache is
70
+ * addressed by content and one copy can be claimed by several projects. It states that nothing is
71
+ * lost for good, so that an operator freeing space is not frightened into leaving it alone. And it
72
+ * states that a project URI is self-declared, the same admission the DSL dialog and the security
73
+ * dialog make about an author's name — the Loader re-checks every digest on use, so a project
74
+ * that files its bytes under somebody else's URI corrupts this list and nothing else.
75
+ */
76
+ export const extensionCacheLocales = Object.freeze({
77
+ en: {
78
+ title: 'Extension cache',
79
+ 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.',
80
+ total: 'Cached in all: {entries} entries, {bytes}',
81
+ shared: 'Shared by every project (pinned by the catalog)',
82
+ untitled: '(no title recorded)',
83
+ entryCount: '{entries} entries',
84
+ expand: 'Entries',
85
+ collapse: 'Fold',
86
+ release: 'Release',
87
+ confirmRelease: 'Release?',
88
+ cancel: 'Cancel',
89
+ close: 'Close',
90
+ refresh: 'Reload',
91
+ empty: 'Nothing is cached. Opening a project that uses extensions fills this.',
92
+ releaseResult: 'Released {released} entries and freed {freed}.',
93
+ lastUsed: 'used {date}',
94
+ originalSize: '{bytes} of JavaScript'
95
+ },
96
+ ja: {
97
+ title: '機能拡張キャッシュ',
98
+ notice: 'キャッシュは内容で参照されており、作品が所有しているわけではありません。作品を手放すと、その作品が名指すdigestを落とします。別の作品が使い回したはずのバイト列まで落ちることがあります。失われたままにはなりません。必要なものは、次にその作品を開いたときに取り直します。下に出るURIは、その作品自身の申告で、検証していません。Loaderは使うたびにdigestを照合するので、URIが誤っていても狂うのはこの一覧の見え方だけです。',
99
+ total: 'キャッシュ全体: {entries}件、{bytes}',
100
+ shared: 'すべての作品で共有(カタログが指定したもの)',
101
+ untitled: '(タイトル未登録)',
102
+ entryCount: '{entries}件',
103
+ expand: '中身',
104
+ collapse: 'たたむ',
105
+ release: '手放す',
106
+ confirmRelease: '手放す?',
107
+ cancel: 'やめる',
108
+ close: '閉じる',
109
+ refresh: '読み直す',
110
+ empty: 'キャッシュは空です。機能拡張を使う作品を開くと入ります。',
111
+ releaseResult: '{released}件を手放し、{freed}を空けました。',
112
+ lastUsed: '最終利用 {date}',
113
+ originalSize: 'JavaScript {bytes}'
114
+ }
115
+ });
116
+ const cacheErrorMessages = Object.freeze({
117
+ en: {
118
+ unavailable: 'Browser storage is unavailable, so the extension cache cannot be read.',
119
+ failed: 'The extension cache operation failed.'
120
+ },
121
+ ja: {
122
+ unavailable: 'ブラウザの保存領域が使えないため、機能拡張キャッシュを読めません。',
123
+ failed: '機能拡張キャッシュの操作に失敗しました。'
124
+ }
125
+ });
126
+ /**
127
+ * Turns a cache failure into operator-facing text.
128
+ *
129
+ * Only the codes this package assigns itself are translated. A failure raised inside IndexedDB
130
+ * keeps its own message, because inventing a friendlier sentence for an unclassified error would
131
+ * hide the one detail that makes it reportable.
132
+ */
133
+ export function describeCacheError(locale, error) {
134
+ const code = error?.code;
135
+ const messages = cacheErrorMessages[locale] ?? cacheErrorMessages.en;
136
+ if (code !== undefined && code in messages)
137
+ return messages[code];
138
+ if (error instanceof Error && error.message.length > 0)
139
+ return error.message;
140
+ return String(error);
141
+ }
53
142
  const storeErrorMessages = Object.freeze({
54
143
  en: {
55
144
  unavailable: 'Browser storage is unavailable, so DSL files cannot be saved.',