Skip to content

Commit 70816a1

Browse files
Release 20250615
1 parent 44da4dd commit 70816a1

File tree

7 files changed

+204
-14
lines changed

7 files changed

+204
-14
lines changed

hlp-user/Help-cs.zip

73 Bytes
Binary file not shown.

hlp-user/Help-en.zip

65 Bytes
Binary file not shown.

hlp/Help-cs.zip

37 Bytes
Binary file not shown.

hlp/Help-en.zip

55 Bytes
Binary file not shown.

hvdata/appmain.js

Lines changed: 194 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,213 @@
1-
let arcData = null;
1+
const STO_DATA = 'STO_DATA';
2+
const STO_HELP = 'STO_HELP';
3+
const STOF_TEXT = 'text';
4+
const STOF_B64 = 'base64';
5+
6+
const STORAGE_ENGINES = {
7+
'.zip': async (path) => newStorageZip(path),
8+
'/': async (path) => newStorageDir(path),
9+
};
10+
11+
var _Storage = (() => {
12+
var storagesC = new Map();
13+
14+
async function add(key, path) {
15+
for (const keyC in STORAGE_ENGINES) {
16+
if (path.endsWith(keyC)) {
17+
const eng = await STORAGE_ENGINES[keyC](path);
18+
storagesC.set(key, eng);
19+
return true;
20+
}
21+
}
22+
return null;
23+
}
24+
25+
async function search(key, filePath, format = STOF_TEXT) {
26+
if (!storagesC.has(key))
27+
return null;
28+
29+
return await storagesC.get(key).search(filePath, format);
30+
}
31+
32+
async function getSubdirs(key, parentPath) {
33+
if (!storagesC.has(key))
34+
return [];
35+
36+
return await storagesC.get(key).getSubdirs(parentPath);
37+
}
38+
39+
async function searchImage(key, filePath) {
40+
if (!storagesC.has(key))
41+
return null;
42+
43+
return await storagesC.get(key).searchImage(filePath);
44+
}
45+
46+
return {
47+
add,
48+
search,
49+
getSubdirs,
50+
searchImage
51+
};
52+
})();
53+
54+
async function newStorageZip(path) {
55+
var storageO = await init(path);
56+
57+
async function init(path) {
58+
return await ZIPHelpers.loadZipFromUrl(path);
59+
}
60+
61+
async function search(filePath, format = STOF_TEXT) {
62+
return await ZIPHelpers.searchArchiveForFile(filePath, storageO, format);
63+
}
64+
65+
async function getSubdirs(parentPath) {
66+
const subdirs = new Set();
67+
68+
storageO?.forEach((relativePath, file) => {
69+
if (relativePath.startsWith(parentPath) && relativePath !== parentPath)
70+
{
71+
const subPath = relativePath.slice(parentPath.length);
72+
const parts = subPath.split("/");
73+
74+
if (parts.length > 1) {
75+
subdirs.add(parts[0]);
76+
} else if (file.dir) {
77+
subdirs.add(parts[0]);
78+
}
79+
}
80+
});
81+
82+
return [...subdirs];
83+
}
84+
85+
async function searchImage(filePath) {
86+
const content = await search(filePath, STOF_B64);
87+
if (!content) return null;
88+
var mimeType = 'image/' + filePath.split('.').pop().toLowerCase();
89+
return `data:${mimeType};base64,${content}`;
90+
}
91+
92+
return {
93+
search,
94+
getSubdirs,
95+
searchImage
96+
};
97+
}
98+
99+
async function newStorageDir(path) {
100+
var storageO = await init(path);
101+
102+
async function init(path) {
103+
return path.replace(/\/$/, '');
104+
}
105+
async function search(filePath, format = STOF_TEXT) {
106+
const fpath = `${storageO}/${filePath}`;
107+
const response = await fetchDataOrEmpty(fpath);
108+
109+
switch (format) {
110+
case STOF_B64:
111+
const zip = new JSZip();
112+
const fname = "a.txt";
113+
zip.file(fname, response, { compression: "STORE" });
114+
const base64zip = await zip.generateAsync({ type: STOF_B64 });
115+
await zip.loadAsync(base64zip, { base64: true });
116+
const file = zip.file(fname);
117+
const b64Data = await file.async(STOF_B64);
118+
return b64Data;
119+
case STOF_TEXT:
120+
default:
121+
return toText(response);
122+
}
123+
}
124+
125+
function toText(ab) {
126+
const decoder = new TextDecoder("utf-8");
127+
const text = decoder.decode(ab);
128+
return text;
129+
}
130+
131+
async function getSubdirs(parentPath) {
132+
const list = search(`${storageO}/${parentPath}/__dir.lst`, format = STOF_TEXT);
133+
const text = toText(list);
134+
text = text.trim().replace(/\r\n/g, "\n").split('\n');
135+
136+
const subdirs = new Set();
137+
text?.forEach((line, index) => {
138+
subdirs.add(line);
139+
});
140+
141+
return [...subdirs];
142+
}
143+
144+
async function fetchDataOrEmpty(url) {
145+
try {
146+
const response = await fetchData(url);
147+
return response;
148+
} catch (error) {
149+
return new ArrayBuffer(0);
150+
}
151+
}
152+
153+
async function searchImage(filePath) {
154+
const fpath = `${storageO}/${filePath}`;
155+
const response = await fetchDataOrEmpty(fpath);
156+
if (response.byteLength == 0)
157+
return null;
158+
return fpath;
159+
}
160+
161+
return {
162+
search,
163+
getSubdirs,
164+
searchImage
165+
};
166+
}
2167

3168
async function main() {
4-
arcData = await loadZipFromUrl('hvdata/data.zip');
5-
const srcT = await searchArchiveForFile('appmainRun.js', arcData);
169+
var st = await _Storage.add(STO_DATA, 'hvdata/data.zip');
170+
const srcT = await _Storage.search(STO_DATA, 'appmainRun.js');
6171
appendJavaScript('appRun', srcT, document.body);
7172
runApp();
8173
}
9174

10-
async function loadZipFromUrl(url) {
175+
async function fetchData(url) {
11176
try {
12177
const response = await fetch(url);
13178
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
14179
const arrayBuffer = await response.arrayBuffer();
15-
16-
const arch = await JSZip.loadAsync(arrayBuffer);
17-
return arch;
180+
return arrayBuffer;
18181
} catch (error) {
19182
throw error;
20183
}
21184
}
22185

23-
async function searchArchiveForFile(fileName, arch) {
24-
try {
25-
const fileContent = await arch.file(fileName)?.async('text');
26-
return fileContent ?? "";
27-
} catch (error) {
28-
return "";
186+
const ZIPHelpers = (() => {
187+
async function loadZipFromUrl(url) {
188+
try {
189+
const arrayBuffer = await fetchData(url);
190+
const arch = await JSZip.loadAsync(arrayBuffer);
191+
return arch;
192+
} catch (error) {
193+
throw error;
194+
}
29195
}
30-
}
196+
197+
async function searchArchiveForFile(fileName, arch, format = STOF_TEXT) {
198+
try {
199+
const fileContent = await arch.file(fileName)?.async(format);
200+
return fileContent ?? "";
201+
} catch (error) {
202+
return "";
203+
}
204+
}
205+
206+
return {
207+
loadZipFromUrl,
208+
searchArchiveForFile
209+
};
210+
})();
31211

32212
function appendCSS(id, content) {
33213
//if (document.getElementById(id)) return;

hvdata/data.zip

628 Bytes
Binary file not shown.

sw.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@ self.addEventListener('fetch', (event) => {
2929
caches.match(event.request)
3030
.then((response) => response || fetch(event.request))
3131
);
32+
});
33+
34+
self.addEventListener('message', event => {
35+
if (event.data && event.data.action === 'clearCache') {
36+
caches.keys().then(keyList => {
37+
return Promise.all(
38+
keyList.map(name => caches.delete(name))
39+
);
40+
});
41+
}
3242
});

0 commit comments

Comments
 (0)