Skip to content

Commit

Permalink
Compress data export
Browse files Browse the repository at this point in the history
  • Loading branch information
qsantos committed Oct 19, 2024
1 parent 65e4ca2 commit 415f2a9
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,16 @@ cwPlayer.onCharacterPlay = (c) => {
}
};

/**
* @param {Blob} blob
* @return {Promise<Blob>}
*/
async function compressBlob(blob) {
const cs = new CompressionStream("gzip");
const compressedStream = blob.stream().pipeThrough(cs);
return await new Response(compressedStream).blob();
}

// inspired from https://stackoverflow.com/a/48968694/4457767
/** Let the user save some data as a file
* @param {Blob} data
Expand Down Expand Up @@ -1659,12 +1669,14 @@ function exportData() {
let sessions = null;
/** @type {import("./types").TransmittedCharacter[] | null} */
let characters = null;
function exportAsJsonWhenReady() {
async function exportAsJsonWhenReady() {
if (!sessions || !characters) {
return;
}
const data = JSON.stringify({ sessions, characters, settings });
saveFile(new Blob([data]), "morse-cat-data.json");
const blob = new Blob([data]);
const compressed = await compressBlob(blob);
saveFile(compressed, "morse-cat-data.json.gz");
button.classList.remove("spinning");
}
{
Expand Down

0 comments on commit 415f2a9

Please sign in to comment.