Skip to content

Commit 1054863

Browse files
committed
Implement Brotli compression worker and update index.html for improved performance
1 parent 3584190 commit 1054863

3 files changed

Lines changed: 50 additions & 14 deletions

File tree

App.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
private void OnFilesChange(InputFileChangeEventArgs e)
250250
{
251251
files.Clear();
252-
files.AddRange(e.GetMultipleFiles().Select(f => new Models.File
252+
files.AddRange(e.GetMultipleFiles(int.MaxValue).Select(f => new Models.File
253253
{
254254
BrowserFile = f,
255255
OriginalName = f.Name,

wwwroot/compression-worker.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import brotliWasm from 'https://cdn.jsdelivr.net/npm/brotli-wasm@3.0.1/index.web.js';
2+
3+
const brotliReady = brotliWasm;
4+
5+
self.onmessage = async ({ data: { id, type, payload, quality } }) => {
6+
try {
7+
const brotli = await brotliReady;
8+
const result = type === 'compress'
9+
? brotli.compress(payload, { quality })
10+
: brotli.decompress(payload);
11+
self.postMessage({ id, result }, [result.buffer]);
12+
} catch (err) {
13+
self.postMessage({ id, error: String(err) });
14+
}
15+
};

wwwroot/index.html

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin="anonymous" />
2323
<link rel="preconnect" href="https://www.googletagmanager.com" crossorigin="anonymous" />
2424
<link rel="dns-prefetch" href="https://media.ethicalads.io" />
25-
<link rel="preload" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" as="style" crossorigin="anonymous" />
25+
<link rel="preload" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" as="style" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous" />
2626
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous" />
2727
<link href="manifest.json" rel="manifest" />
2828
<link rel="apple-touch-icon" sizes="180x180" href="/assets/apple-touch-icon.png">
@@ -289,20 +289,41 @@ <h1 class="display-5 fw-bold">Online GZIP de/compressor</h1>
289289
setTimeout(() => URL.revokeObjectURL(url), 60000);
290290
};
291291
</script>
292-
<script type="module">
293-
import brotliWasm from 'https://cdn.jsdelivr.net/npm/brotli-wasm@3.0.1/index.web.js';
294-
const brotliReady = brotliWasm;
295-
window.brotliCompress = async (data, quality) => {
296-
const brotli = await brotliReady;
297-
return brotli.compress(data, { quality });
298-
};
299-
window.brotliDecompress = async (data) => {
300-
const brotli = await brotliReady;
301-
return brotli.decompress(data);
302-
};
292+
<script>
293+
(function () {
294+
const worker = new Worker('/compression-worker.js', { type: 'module' });
295+
const pending = new Map();
296+
let nextId = 0;
297+
298+
worker.onmessage = ({ data: { id, result, error } }) => {
299+
const { resolve, reject } = pending.get(id);
300+
pending.delete(id);
301+
error ? reject(new Error(error)) : resolve(result);
302+
};
303+
304+
worker.onerror = (e) => {
305+
pending.forEach(({ reject }) => reject(new Error('Compression worker error: ' + e.message)));
306+
pending.clear();
307+
};
308+
309+
window.brotliCompress = (data, quality) => new Promise((resolve, reject) => {
310+
const id = nextId++;
311+
pending.set(id, { resolve, reject });
312+
// slice() copies away from WASM linear memory before transferring
313+
const copy = data.slice();
314+
worker.postMessage({ id, type: 'compress', payload: copy, quality }, [copy.buffer]);
315+
});
316+
317+
window.brotliDecompress = (data) => new Promise((resolve, reject) => {
318+
const id = nextId++;
319+
pending.set(id, { resolve, reject });
320+
const copy = data.slice();
321+
worker.postMessage({ id, type: 'decompress', payload: copy }, [copy.buffer]);
322+
});
323+
})();
303324
</script>
304325
<script src="_framework/blazor.webassembly#[.{fingerprint}].js"></script>
305-
<script>navigator.serviceWorker.register('service-worker.js', { updateViaCache: 'none' });</script>
326+
<script>navigator.serviceWorker.register('service-worker.js', { updateViaCache: 'none' }).catch(() => {});</script>
306327
</body>
307328

308329
</html>

0 commit comments

Comments
 (0)