-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-cache.js
More file actions
213 lines (178 loc) · 5.73 KB
/
Copy pathimage-cache.js
File metadata and controls
213 lines (178 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Envato XPerience - Image Cache
* Stores product preview images in IndexedDB to avoid filling chrome.storage.local.
*/
(function () {
const DB_NAME = 'envato-xperience-cache';
const DB_VERSION = 1;
const STORE_NAME = 'productImages';
const TTL_MS = 7 * 24 * 60 * 60 * 1000;
const MAX_ENTRIES = 80;
const MAX_TOTAL_BYTES = 50 * 1024 * 1024;
let dbPromise = null;
function openDatabase() {
if (dbPromise) return dbPromise;
dbPromise = new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onupgradeneeded = function () {
const db = request.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
const store = db.createObjectStore(STORE_NAME, { keyPath: 'itemId' });
store.createIndex('updatedAt', 'updatedAt', { unique: false });
store.createIndex('lastAccessedAt', 'lastAccessedAt', { unique: false });
}
};
request.onsuccess = function () {
resolve(request.result);
};
request.onerror = function () {
reject(request.error);
};
});
return dbPromise;
}
function requestToPromise(request) {
return new Promise((resolve, reject) => {
request.onsuccess = function () {
resolve(request.result);
};
request.onerror = function () {
reject(request.error);
};
});
}
async function digestBlob(blob) {
const buffer = await blob.arrayBuffer();
const digest = await crypto.subtle.digest('SHA-256', buffer);
return Array.from(new Uint8Array(digest))
.map((value) => value.toString(16).padStart(2, '0'))
.join('');
}
function isFresh(entry) {
return entry && typeof entry.expiresAt === 'number' && entry.expiresAt > Date.now();
}
async function getEntry(itemId) {
const db = await openDatabase();
const transaction = db.transaction(STORE_NAME, 'readonly');
const store = transaction.objectStore(STORE_NAME);
return requestToPromise(store.get(itemId));
}
async function putEntry(entry) {
const db = await openDatabase();
const transaction = db.transaction(STORE_NAME, 'readwrite');
const store = transaction.objectStore(STORE_NAME);
await requestToPromise(store.put(entry));
}
async function deleteEntry(itemId) {
const db = await openDatabase();
const transaction = db.transaction(STORE_NAME, 'readwrite');
const store = transaction.objectStore(STORE_NAME);
await requestToPromise(store.delete(itemId));
}
async function getAllEntries() {
const db = await openDatabase();
const transaction = db.transaction(STORE_NAME, 'readonly');
const store = transaction.objectStore(STORE_NAME);
return requestToPromise(store.getAll());
}
async function touchEntry(entry) {
entry.lastAccessedAt = Date.now();
await putEntry(entry);
}
async function fetchImageBlob(url) {
const response = await fetch(url, { cache: 'force-cache', credentials: 'omit' });
if (!response.ok) {
throw new Error(`Image fetch failed with status ${response.status}`);
}
const blob = await response.blob();
if (!blob || !blob.size) {
throw new Error('Image fetch returned an empty blob');
}
return blob;
}
async function prune() {
const entries = await getAllEntries();
if (!entries || entries.length === 0) return;
const now = Date.now();
const expired = entries.filter((entry) => !isFresh(entry) || !entry.blob);
for (const entry of expired) {
await deleteEntry(entry.itemId);
}
const freshEntries = entries
.filter((entry) => isFresh(entry) && entry.blob)
.sort((a, b) => (a.lastAccessedAt || a.updatedAt || 0) - (b.lastAccessedAt || b.updatedAt || 0));
let totalBytes = freshEntries.reduce((sum, entry) => sum + (entry.size || 0), 0);
while (freshEntries.length > MAX_ENTRIES || totalBytes > MAX_TOTAL_BYTES) {
const entry = freshEntries.shift();
if (!entry) break;
totalBytes -= entry.size || 0;
await deleteEntry(entry.itemId);
}
}
async function getImage(itemId, sourceUrl) {
if (!itemId) return null;
const entry = await getEntry(itemId);
if (!entry || !isFresh(entry) || !entry.blob) {
return null;
}
if (sourceUrl && entry.sourceUrl && entry.sourceUrl !== sourceUrl) {
return null;
}
await touchEntry(entry);
return {
blob: entry.blob,
checksum: entry.checksum,
sourceUrl: entry.sourceUrl,
updatedAt: entry.updatedAt
};
}
async function cacheImage(itemId, sourceUrl) {
if (!itemId || !sourceUrl) return null;
const blob = await fetchImageBlob(sourceUrl);
const checksum = await digestBlob(blob);
const now = Date.now();
const entry = {
itemId,
sourceUrl,
blob,
checksum,
size: blob.size,
contentType: blob.type || 'image/*',
createdAt: now,
updatedAt: now,
lastAccessedAt: now,
expiresAt: now + TTL_MS
};
const previous = await getEntry(itemId);
if (previous && previous.checksum === checksum && previous.sourceUrl === sourceUrl && isFresh(previous)) {
previous.lastAccessedAt = now;
previous.updatedAt = now;
previous.expiresAt = now + TTL_MS;
await putEntry(previous);
return {
blob: previous.blob,
checksum: previous.checksum,
sourceUrl: previous.sourceUrl,
updatedAt: previous.updatedAt
};
}
await putEntry(entry);
await prune();
return {
blob,
checksum,
sourceUrl,
updatedAt: now
};
}
window.EnvatoImageCache = {
getImage,
cacheImage,
prune,
config: {
ttlMs: TTL_MS,
maxEntries: MAX_ENTRIES,
maxTotalBytes: MAX_TOTAL_BYTES
}
};
})();