Add more characters to the alphabet

This commit is contained in:
Mikko Ahlroth 2022-10-27 11:01:59 +03:00
parent 5045591e8c
commit bee38949e2

View file

@ -3,14 +3,13 @@ import base from "./vendor/base-x.js";
const COMPRESS_WAIT = 500;
const ENCODER = new TextEncoder();
const BASE66 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~";
const BASE66CODEC = base(BASE66);
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789?/-._~$&+=";
const BASECODEC = base(ALPHABET);
const QUALITY = 11;
const MAX_URL = 2048;
const DECOMPRESS_CHUNK_SIZE = 1000;
const DECOMPRESS_CHUNK_TIMEOUT = 100;
let brotli;
let compressTimeout = null;
let rootURLSize = 0;
let statusEl = null;
@ -53,7 +52,7 @@ function maxHashLength() {
async function syncCompress(data) {
const utf8 = ENCODER.encode(data);
const compressed = compress(utf8, { quality: QUALITY });
const encoded = BASE66CODEC.encode(compressed);
const encoded = BASECODEC.encode(compressed);
statusEl.textContent = `Length: ${utf8.length} B -> ${compressed.length} B -> ${encoded.length}/${maxHashLength()} chars`;
@ -125,7 +124,7 @@ async function streamDecompress(data) {
}
async function init() {
brotli = await brotliInit();
await brotliInit();
codeEl = document.getElementById("code");
statusEl = document.getElementById("length");
@ -149,7 +148,7 @@ async function init() {
if (window.location.hash.length > 1) {
try {
const bytes = BASE66CODEC.decode(window.location.hash.substring(1));
const bytes = BASECODEC.decode(window.location.hash.substring(1));
await streamDecompress(bytes);
} catch (e) {
codeEl.textContent = e.stack;