-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
234 lines (214 loc) · 7.83 KB
/
Copy pathstorage.js
File metadata and controls
234 lines (214 loc) · 7.83 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Offline episode storage — imported by the Rust app as a wasm-bindgen module
// snippet (see `#[wasm_bindgen(module = "/storage.js")]` in src/main.rs).
//
// Why IndexedDB + blob: object URLs (and NOT the Cache API): an <audio> element
// issues HTTP Range requests when seeking, and the Cache API serves only the
// full 200 response — it ignores Range, which breaks scrubbing unless a service
// worker hand-synthesizes 206 responses. A blob: URL minted from an IndexedDB
// Blob is range-served natively by the browser, fully offline, with no SW. So
// downloaded audio lives here, entirely separate from the app-shell sw.js.
//
// CORS note: fetch()-ing the mp3 to store it DOES require the host to send
// Access-Control-Allow-Origin (unlike <audio src> streaming, which never does).
// When that fails we return a reason string and the app keeps streaming online.
const DB_NAME = "podcasts";
const STORE = "episodes";
const VERSION = 1;
function openDB() {
return new Promise((resolve, reject) => {
const req = indexedDB.open(DB_NAME, VERSION);
req.onupgradeneeded = () => {
const db = req.result;
if (!db.objectStoreNames.contains(STORE)) {
db.createObjectStore(STORE, { keyPath: "key" });
}
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
function idbGet(key) {
return openDB().then(
(db) =>
new Promise((resolve, reject) => {
const tx = db.transaction(STORE, "readonly");
const r = tx.objectStore(STORE).get(key);
r.onsuccess = () => resolve(r.result || null);
r.onerror = () => reject(r.error);
// A transaction abort (db close / version change) fires neither
// onsuccess nor onerror — without this the Promise would hang.
tx.onabort = () => reject(tx.error || new Error("aborted"));
}),
);
}
function idbPut(value) {
return openDB().then(
(db) =>
new Promise((resolve, reject) => {
const tx = db.transaction(STORE, "readwrite");
tx.objectStore(STORE).put(value);
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
tx.onabort = () => reject(tx.error || new Error("aborted"));
}),
);
}
function idbDelete(key) {
return openDB().then(
(db) =>
new Promise((resolve, reject) => {
const tx = db.transaction(STORE, "readwrite");
tx.objectStore(STORE).delete(key);
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
tx.onabort = () => reject(tx.error || new Error("aborted"));
}),
);
}
function idbKeys() {
return openDB().then(
(db) =>
new Promise((resolve, reject) => {
const tx = db.transaction(STORE, "readonly");
const r = tx.objectStore(STORE).getAllKeys();
r.onsuccess = () => resolve(r.result || []);
r.onerror = () => reject(r.error);
tx.onabort = () => reject(tx.error || new Error("aborted"));
}),
);
}
async function quotaShortfall(bytes) {
// true if we can see a quota and `bytes` (plus headroom) won't fit.
try {
if (navigator.storage && navigator.storage.estimate) {
const { usage = 0, quota = 0 } = await navigator.storage.estimate();
return quota > 0 && quota - usage < bytes * 1.1;
}
} catch {
/* ignore — let the write attempt surface QuotaExceededError */
}
return false;
}
async function persistBlob(key, blob, onProgress) {
if (await quotaShortfall(blob.size)) return "quota";
try {
await idbPut({ key, blob, size: blob.size, mime: blob.type, savedAt: Date.now() });
} catch (e) {
return e && e.name === "QuotaExceededError" ? "quota" : "store";
}
if (onProgress) onProgress(blob.size, blob.size);
return "ok";
}
/// Download `url` and store it under `key`. Reports (received,total) bytes via
/// onProgress (total=0 when the host omits Content-Length). Returns a status:
/// "ok" | "cors" | "quota" | "store" | "http_<code>". Idempotent.
export async function downloadEpisode(key, url, onProgress) {
const existing = await idbGet(key).catch(() => null);
if (existing && existing.blob) return "ok";
// A download tap is a user gesture, the best moment to ask for durable storage
// (best-effort storage is evicted under pressure / after 7 idle days on Safari).
try {
if (navigator.storage && navigator.storage.persist) await navigator.storage.persist();
} catch {
/* non-fatal */
}
let resp;
try {
// NEVER mode:'no-cors' — an opaque response is unreadable and unslicable and
// pads quota by ~7MB/entry. A CORS/network failure means "stream online".
resp = await fetch(url, { mode: "cors" });
} catch {
return "cors";
}
if (!resp.ok) return "http_" + resp.status;
const mime = resp.headers.get("Content-Type") || "audio/mpeg";
const total = Number(resp.headers.get("Content-Length")) || 0;
if (total > 0 && (await quotaShortfall(total))) return "quota";
if (!resp.body || !resp.body.getReader) {
return await persistBlob(key, await resp.blob(), onProgress);
}
const reader = resp.body.getReader();
const chunks = [];
let received = 0;
let lastPct = -1;
for (;;) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
received += value.length;
if (onProgress) {
const pct = total > 0 ? Math.floor((received / total) * 100) : -1;
if (pct !== lastPct) {
lastPct = pct;
onProgress(received, total);
}
}
}
return await persistBlob(key, new Blob(chunks, { type: mime }), onProgress);
}
/// Mint a blob: object URL for the stored episode, or null if not downloaded.
/// Caller must revokeObjectUrl() it when done (on episode switch / unmount).
export async function getObjectUrl(key) {
const rec = await idbGet(key).catch(() => null);
if (!rec || !rec.blob) return null;
return URL.createObjectURL(rec.blob);
}
export function revokeObjectUrl(u) {
if (u) {
try {
URL.revokeObjectURL(u);
} catch {
/* already revoked */
}
}
}
export async function deleteEpisode(key) {
await idbDelete(key).catch(() => {});
}
/// Keys (= episode audio_url identities) of all downloaded episodes.
export async function listDownloaded() {
return await idbKeys().catch(() => []);
}
/// Lightweight metadata for every downloaded episode: { key, savedAt } per
/// record, for the startup auto-remove sweep (which needs savedAt — the download
/// time — to decide staleness; listDownloaded returns keys only). Uses a cursor
/// and reads ONLY key + savedAt so the audio blobs are never materialized into
/// the JS heap (getAll() would deserialize every Blob just to read two fields).
/// A record missing savedAt (none should exist — it shipped with the field)
/// reports 0, which the Rust predicate treats as "unknown age → never sweep".
export async function listDownloadedMeta() {
return openDB()
.then(
(db) =>
new Promise((resolve, reject) => {
const tx = db.transaction(STORE, "readonly");
const out = [];
const r = tx.objectStore(STORE).openCursor();
r.onsuccess = () => {
const cur = r.result;
if (!cur) {
resolve(out);
return;
}
out.push({ key: cur.key, savedAt: (cur.value || {}).savedAt || 0 });
cur.continue();
};
r.onerror = () => reject(r.error);
tx.onabort = () => reject(tx.error || new Error("aborted"));
}),
)
.catch(() => []);
}
/// Total bytes this origin is using (dominated by the audio blobs); for the UI's
/// storage line. Estimated/padded by the browser — display only.
export async function estimateStorage() {
try {
if (navigator.storage && navigator.storage.estimate) {
const { usage = 0 } = await navigator.storage.estimate();
return usage;
}
} catch {
/* ignore */
}
return 0;
}