aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/lib/ts
diff options
context:
space:
mode:
authorMatt Strapp <matt@mattstrapp.net>2023-09-27 16:16:10 -0500
committerMatt Strapp <matt@mattstrapp.net>2023-09-27 16:16:10 -0500
commit13d465c1c45e649a702457d67476694aeb553704 (patch)
treed0d3ba8ac0ff9546482b8f5e7294399613e6cb20 /app/src/lib/ts
parentWorkspaces! (diff)
downloadtrinkets-13d465c1c45e649a702457d67476694aeb553704.tar
trinkets-13d465c1c45e649a702457d67476694aeb553704.tar.gz
trinkets-13d465c1c45e649a702457d67476694aeb553704.tar.bz2
trinkets-13d465c1c45e649a702457d67476694aeb553704.tar.lz
trinkets-13d465c1c45e649a702457d67476694aeb553704.tar.xz
trinkets-13d465c1c45e649a702457d67476694aeb553704.tar.zst
trinkets-13d465c1c45e649a702457d67476694aeb553704.zip
Get rid of crc32, truncate instead
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'app/src/lib/ts')
-rw-r--r--app/src/lib/ts/crc32.worker.ts10
-rw-r--r--app/src/lib/ts/download.ts33
-rw-r--r--app/src/lib/ts/truncate.worker.ts10
3 files changed, 43 insertions, 10 deletions
diff --git a/app/src/lib/ts/crc32.worker.ts b/app/src/lib/ts/crc32.worker.ts
deleted file mode 100644
index 57c1bc9..0000000
--- a/app/src/lib/ts/crc32.worker.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-// TODO: Think about using a WebAssembly implementation of CRC32 instead of JavaScript.
-// WHY? The JavaScript implementation is not very extensible and is not very fast.
-import crc32 from 'crc/crc32';
-
-onmessage = async (message: MessageEvent<File>) => {
- const file = message.data;
- const buffer = await file.arrayBuffer();
- const crc = crc32(buffer);
- postMessage(crc);
-};
diff --git a/app/src/lib/ts/download.ts b/app/src/lib/ts/download.ts
new file mode 100644
index 0000000..a6167d7
--- /dev/null
+++ b/app/src/lib/ts/download.ts
@@ -0,0 +1,33 @@
+// Yoinked from https://web.dev/patterns/files/save-a-file/ for the polyfill
+
+export default async function saveFile(blob: ArrayBuffer, suggestedName: string | undefined) {
+ const supportsFSAccess = 'showSaveFilePicker' in window &&
+ (() => {
+ try {
+ return window.self === window.top;
+ } catch {
+ return false;
+ }
+ })();
+
+ if (supportsFSAccess) {
+ try {
+ const handle = await window.showSaveFilePicker({
+ suggestedName: suggestedName,
+ })
+ const writable = await handle.createWritable();
+ await writable.write(blob);
+ await writable.close();
+ } catch (err: unknown) {
+ // TypeScript
+ if ((err as Error)?.name !== 'AbortError') {
+ console.error(err);
+ }
+ }
+ } else {
+ const anchor = document.createElement('a');
+ anchor.download = suggestedName ?? 'truncated';
+ anchor.href = URL.createObjectURL(new Blob([blob]));
+ anchor.click();
+ }
+} \ No newline at end of file
diff --git a/app/src/lib/ts/truncate.worker.ts b/app/src/lib/ts/truncate.worker.ts
new file mode 100644
index 0000000..52eebb7
--- /dev/null
+++ b/app/src/lib/ts/truncate.worker.ts
@@ -0,0 +1,10 @@
+import type { TruncateRequest, TruncateResponse } from '../types/truncate-worker';
+
+
+onmessage = async (message: MessageEvent<TruncateRequest>) => {
+ const file = message.data.file;
+ const buffer = await file.arrayBuffer();
+ const truncated = buffer.slice(0, message.data.size);
+
+ postMessage({ file: truncated } as TruncateResponse);
+} \ No newline at end of file