-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.js
168 lines (145 loc) · 5.38 KB
/
storage.js
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
//import Dexie from 'dexie';
function randomFill(array) {
for (let offset = array.byteOffset; offset < array.byteLength; offset += 65536) {
const length = Math.min(array.byteLength - offset, 65536);
const view = new Uint8Array(array.buffer, offset, length);
crypto.getRandomValues(view);
}
}
function getDatabase() {
const db = new Dexie('RubbishDatabase');
db.version(1).stores({
rubbish: '++id,length',
});
return db;
}
function hasNotification(permission) {
permission = permission || Notification.permission;
let color;
switch (permission) {
case 'granted':
color = 'green';
break;
case 'default':
color = 'orange';
break;
case 'denied':
default:
color = 'red';
break;
}
document.querySelector('#notification').style.color = color;
console.info(`Notification permission: ${permission}`);
return permission;
}
async function notification() {
const permission = await Notification.requestPermission();
// Google logic: Obviously, if the notification permission has been granted,
// it will auto-grant persistent storage when requested...
try {
await persist();
} catch {}
return hasNotification(permission);
}
async function getQuotaEstimate() {
// Get quota estimate
const estimate = await navigator.storage.estimate();
console.info(`Quota/Usage: ${Math.round(estimate.quota / 1048576, 2)}/${Math.round(estimate.usage / 1048576, 2)} MiB`);
return estimate;
}
async function isPersistent(persisted) {
persisted = persisted !== undefined ? persisted : await navigator.storage.persisted();
document.querySelector('#persistent').style.color = persisted ? 'green' : 'red';
console.info(`Storage: ${persisted ? 'persistent' : 'temporary'}`);
return persisted;
}
async function persist() {
const persisted = await navigator.storage.persist();
await isPersistent(persisted);
return persisted;
}
async function main(persist) {
// Install service worker
try {
if (navigator.serviceWorker.controller) {
console.info('Service Worker already registered');
} else {
const registration = await navigator.serviceWorker.register('serviceworker.js', {
scope: './'
});
console.info(`Service Worker registered for scope ${registration.scope}`);
}
} catch (error) {
console.error('Could not register service worker', error);
}
// Determine initial value of notification button
try {
hasNotification();
} catch {
console.warn('Could not query whether notification permission has been granted');
}
// Determine initial value of persist button
try {
await isPersistent();
} catch {
console.warn('Could not query whether storage is persistent');
}
// Get quota estimate
try {
await getQuotaEstimate();
} catch {
console.warn('Could not query storage quota');
}
// TODO: Verify the data persists even if data is being deleted
// TODO: Performance test with dexie-encrypted
}
async function getUsage(db) {
db = db || getDatabase();
return (await db.rubbish.orderBy('length').keys())
.reduce((accumulator, length) => accumulator + length, 0);
}
async function populate(target) {
// Store data
const db = getDatabase();
const array = new Uint8Array(10485760); // 10 MiB
let usage = await getUsage(db);
console.debug(`Usage at ${Math.round(usage / 1048576, 2)}/${Math.round(target / 1048576, 2)} MiB`);
try {
while (usage < target) {
randomFill(array);
await db.rubbish.add({
length: array.byteLength,
data: array,
});
usage += array.byteLength;
console.debug(`Currently at ${Math.round(usage / 1048576, 2)}/${Math.round(target / 1048576, 2)} MiB`);
}
} catch (error) {
console.error(`Failed at ${Math.round(usage / 1048576, 2)}/${Math.round(target / 1048576, 2)} MiB:`, error);
}
}
async function remove(target) {
let usage = await getUsage();
console.debug(`Usage at ${Math.round(usage / 1048576, 2)}/${Math.round(target / 1048576, 2)} MiB`);
// Remove all data
const db = getDatabase();
await db.rubbish.clear();
usage = await getUsage();
console.debug(`Usage at ${Math.round(usage / 1048576, 2)}/${Math.round(target / 1048576, 2)} MiB`);
}
function getTarget() {
// Parse and convert MiB to Bytes
return parseInt(document.querySelector('#target').value, 10) * 1048576;
}
window.Threema = {
hasNotification: hasNotification,
notification: () => notification().catch((error) => console.error(error)),
isPersistent: () => isPersistent().catch((error) => console.error(error)),
persist: () => persist().catch((error) => console.error(error)),
populate: (target) => populate(target || getTarget()).catch((error) => console.error(error)),
remove: (target) => remove(target || getTarget()).catch((error) => console.error(error)),
usage: (target) => getUsage()
.then((usage) => console.debug(`Usage at ${Math.round(usage / 1048576, 2)}/${Math.round((target || getTarget()) / 1048576, 2)} MiB`))
.catch((error) => console.error(error)),
};
main().catch((error) => console.error(error));