-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice-worker.js
More file actions
170 lines (151 loc) · 4.63 KB
/
Copy pathservice-worker.js
File metadata and controls
170 lines (151 loc) · 4.63 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
import {getFileContent, decrypt} from "./nodeless/modules/filemanager.js";
let lastMeta = {};
const main_htmls = [
"/app/",
"/app/src/",
"/app/src/index.html",
"/app/src/dashboard.html",
"/app/src/fileviewer.html",
"/app/src/login.html",
"/app/src/new.html",
]
const docs = [
"/app/src/docs/",
"/app/src/docs/forgot.html",
]
const images = [
"/app/src/images/",
"/app/src/images/material-symbols",
"/app/src/images/material-symbols/rounded.css",
"/app/src/images/material-symbols/material-symbols-rounded.woff2",
"/app/src/images/material-symbols/material-symbols-outlined.woff2",
"/app/src/images/material-symbols/outlined.css",
"/app/src/images/error.png",
"/app/src/images/logo.png",
"/app/src/images/success.png",
]
const main_scripts = [
"/app/src/scripts/",
"/app/src/scripts/fileviewer.js",
"/app/src/scripts/loading.js",
"/app/src/scripts/login.js",
"/app/src/scripts/main.js",
"/app/src/scripts/new-account.js",
]
const pdjs = [
]
const dashboard = [
"/app/src/scripts/dashboard-elements/",
"/app/src/scripts/dashboard-elements/dashboard.js",
"/app/src/scripts/dashboard-elements/manager.js",
"/app/src/scripts/dashboard-elements/render.js",
"/app/src/scripts/dashboard-elements/ui.js",
"/app/src/scripts/dashboard-elements/watcher.js",
"/app/src/scripts/dashboard-elements/render",
"/app/src/scripts/dashboard-elements/render/alert.js",
"/app/src/scripts/dashboard-elements/render/monitor.js",
"/app/src/scripts/dashboard-elements/render/alert.js"
]
const styles = [
"/app/src/styles/",
"/app/src/styles/general-form.css",
"/app/src/styles/loading.css",
"/app/src/styles/login.css",
"/app/src/styles/slider.css",
"/app/src/styles/new-account.css",
"/app/src/styles/main",
"/app/src/styles/main/alert.css",
"/app/src/styles/main/general.css",
"/app/src/styles/main/monitor.css",
"/app/src/styles/main/notes.css"
]
const APP_STATIC_RESOURCES = [
...main_htmls,
...docs,
...images,
...main_scripts,
...pdjs,
...styles
];
const VERSION = "v0.0.4";
const CACHE_NAME = `Maestro Cache ${VERSION}`
self.addEventListener("install", (event) => {
self.skipWaiting()
const preCache = async () => {
const cache = await caches.open(CACHE_NAME);
return cache.addAll(APP_STATIC_RESOURCES);
};
event.waitUntil(preCache());
});
self.addEventListener("activate", (event) => {
registerNotification("BDd3_hVL9fZi9Ybo2UUzA284WG5FZR30_95YeZJsiA321")
const removeOldCache = async () => {
const names = await caches.keys();
await Promise.all(
names.map((name) => {
if (name !== CACHE_NAME) {
return caches.delete(name);
}
return undefined;
}
))
}
event.waitUntil(removeOldCache())
})
async function cacheThenNetwork(request) {
const cachedResponse = await caches.match(request);
if (cachedResponse) {
//console.log("Found response in cache:", cachedResponse);
return cachedResponse;
}
//console.log("Falling back to network");
return fetch(request);
}
async function responseBlob(name,url) {
let blob = await getFileContent(name)
const deArray = await decrypt(await blob.arrayBuffer(), "aba1234")
const deBlob = new Blob([deArray.decrypted], {type: "application/pdf"})
const response = new Response(deBlob, {
status: 200,
headers: {
"Content-Type": "application/pdf",
"Content-Length": blob.size,
//"Content-Disposition": "attachment; filename=test.pdf"
}
})
return response
}
self.addEventListener("fetch", (event) => {
//console.log(`Handling fetch event for ${event.request.url}`);
const url = event.request.url
if (url.includes("pdfblobaba")) {
const index = url.indexOf("pdfblobaba")+11
let filename = decodeURIComponent(url.slice(index))
event.respondWith(responseBlob(filename));
} else {
event.respondWith(cacheThenNetwork(event.request));
}
});
const urlB64ToUint8Array = (base64String) => {
const padding = "=".repeat((4 - (base64String.length % 4)) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, "+")
.replace(/_/g, "/");
const rawData = atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
};
const registerNotification = async (key) => {
console.log("Notification Registarion")
try {
const applicationServerKey = urlB64ToUint8Array(key);
const options = { applicationServerKey, userVisibleOnly: true };
const subscription = await self.registration.pushManager.subscribe(options);
console.log(JSON.stringify(subscription))
} catch (err) {
console.log("Error", err);
}
}