From ba573ed2fd3a99ed522b48e8e383ddcd0e5a6ef9 Mon Sep 17 00:00:00 2001 From: Anthony Catel Date: Wed, 30 Mar 2022 10:28:20 +0200 Subject: [PATCH] Add support for compressed URL --- package.json | 1 + src/components/Map.vue | 21 ++++++++++++++++++--- yarn.lock | 5 +++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 600e066..91c706f 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "@types/google.maps": "^3.48.3", "@vitejs/plugin-vue": "^2.2.4", "@vueuse/core": "^8.2.0", + "fflate": "^0.7.3", "js-base64": "^3.5.2", "tailwindcss": "^3.0.23", "tatween": "^0.2.0", diff --git a/src/components/Map.vue b/src/components/Map.vue index e6b69f4..9bdaf00 100644 --- a/src/components/Map.vue +++ b/src/components/Map.vue @@ -10,6 +10,7 @@ import { Base64 } from 'js-base64' import { onMounted, ref, watch, computed } from 'vue'; import { watchDebounced } from '@vueuse/core' + import { zlibSync, unzlibSync } from 'fflate'; const DEFAULT_MAP_POSITION = [48.862895, 2.286978, 18] @@ -167,15 +168,21 @@ } const loadHash = (hash: string) => { - if (hash[0] != 'b') { + if (hash[0] != 'b' && hash[0] != 'c') { return loadLegacyHash(hash); } - const buf = Base64.toUint8Array(hash.substr(1)); + const isCompressed = hash[0] == 'c'; + let buf = Base64.toUint8Array(hash.substr(1)); + if (!buf) { return; } + if (isCompressed) { + buf = unzlibSync(buf) + } + const meta = new Float32Array(buf.buffer, 0, 4); const data = new Float32Array(buf.buffer, 4*4); @@ -240,7 +247,15 @@ buf[4+i*2] = arrPoly.value[i].lat(); buf[4+i*2+1] = arrPoly.value[i].lng(); } - return 'b' + Base64.fromUint8Array(new Uint8Array(buf.buffer), true); + + let outbuf = new Uint8Array(buf.buffer); + const isCompressed = outbuf.byteLength >= 150; + + if (isCompressed) { + outbuf = zlibSync(outbuf, { level: 9 }); + } + + return (isCompressed ? 'c' : 'b') + Base64.fromUint8Array(outbuf, true); }) watch(() => props.density, () => updatePolygonColor()); diff --git a/yarn.lock b/yarn.lock index 81784cc..a9692fa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -596,6 +596,11 @@ fastq@^1.6.0: dependencies: reusify "^1.0.4" +fflate@^0.7.3: + version "0.7.3" + resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.7.3.tgz#288b034ff0e9c380eaa2feff48c787b8371b7fa5" + integrity sha512-0Zz1jOzJWERhyhsimS54VTqOteCNwRtIlh8isdL0AXLo0g7xNTfTL7oWrkmCnPhZGocKIkWHBistBrrpoNH3aw== + fill-range@^7.0.1: version "7.0.1" resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40"