Added gzip compression on share link with fflate

This commit is contained in:
EpicKiwi
2023-08-30 00:01:49 +02:00
parent 9fd9425607
commit 25c914a319
3 changed files with 29 additions and 9 deletions

View File

@ -27,6 +27,7 @@
"astring": "^1.8.6", "astring": "^1.8.6",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.14",
"codemirror": "^6.0.1", "codemirror": "^6.0.1",
"fflate": "^0.8.0",
"lru-cache": "^10.0.1", "lru-cache": "^10.0.1",
"marked": "^7.0.3", "marked": "^7.0.3",
"postcss": "^8.4.27", "postcss": "^8.4.27",

View File

@ -27,6 +27,7 @@ import {
template_universes, template_universes,
} from "./AppSettings"; } from "./AppSettings";
import { tryEvaluate } from "./Evaluator"; import { tryEvaluate } from "./Evaluator";
import { gzipSync, decompressSync, strFromU8 } from 'fflate';
// Importing showdown and setting up the markdown converter // Importing showdown and setting up the markdown converter
import showdown from "showdown"; import showdown from "showdown";
@ -526,13 +527,13 @@ export class Editor {
this.settings.font_size = parseInt(new_value); this.settings.font_size = parseInt(new_value);
}); });
this.share_button.addEventListener("click", () => { this.share_button.addEventListener("click", async () => {
// trigger a manual save // trigger a manual save
this.currentFile().candidate = app.view.state.doc.toString(); this.currentFile().candidate = app.view.state.doc.toString();
this.currentFile().committed = app.view.state.doc.toString(); this.currentFile().committed = app.view.state.doc.toString();
this.settings.saveApplicationToLocalStorage(app.universes, app.settings); this.settings.saveApplicationToLocalStorage(app.universes, app.settings);
// encode as a blob! // encode as a blob!
this.share(); await this.share();
}); });
this.normal_mode_button.addEventListener("click", () => { this.normal_mode_button.addEventListener("click", () => {
@ -649,7 +650,8 @@ export class Editor {
if (url !== null) { if (url !== null) {
const universeParam = url.get("universe"); const universeParam = url.get("universe");
if (universeParam !== null) { if (universeParam !== null) {
new_universe = JSON.parse(atob(universeParam)); let data = Uint8Array.from(atob(universeParam), c => c.charCodeAt(0))
new_universe = JSON.parse(strFromU8(decompressSync(data)));
const randomName: string = uniqueNamesGenerator({ const randomName: string = uniqueNamesGenerator({
length: 2, separator: '_', length: 2, separator: '_',
dictionaries: [colors, animals], dictionaries: [colors, animals],
@ -702,12 +704,24 @@ export class Editor {
existing_universes!.innerHTML = final_html; existing_universes!.innerHTML = final_html;
} }
share() { async share() {
const hashed_table = btoa(
JSON.stringify({ async function bufferToBase64(buffer:Uint8Array) {
universe: this.settings.universes[this.selected_universe], const base64url: string = await new Promise(r => {
}) const reader = new FileReader()
); reader.onload = () => r(reader.result as string)
reader.readAsDataURL(new Blob([buffer]))
});
return base64url.slice(base64url.indexOf(',') + 1);
}
let data = JSON.stringify({
universe: this.settings.universes[this.selected_universe],
});
let encoded_data = gzipSync(new TextEncoder().encode(data));
// TODO make this async
// TODO maybe try with compression level 9
const hashed_table = await bufferToBase64(encoded_data);
const url = new URL(window.location.href); const url = new URL(window.location.href);
url.searchParams.set("universe", hashed_table); url.searchParams.set("universe", hashed_table);
window.history.replaceState({}, "", url.toString()); window.history.replaceState({}, "", url.toString());

View File

@ -767,6 +767,11 @@ fastq@^1.6.0:
dependencies: dependencies:
reusify "^1.0.4" reusify "^1.0.4"
fflate@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.8.0.tgz#f93ad1dcbe695a25ae378cf2386624969a7cda32"
integrity sha512-FAdS4qMuFjsJj6XHbBaZeXOgaypXp8iw/Tpyuq/w3XA41jjLHT8NPA+n7czH/DDhdncq0nAyDZmPeWXh2qmdIg==
fill-range@^7.0.1: fill-range@^7.0.1:
version "7.0.1" version "7.0.1"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40" resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40"