diff --git a/package.json b/package.json index ecd3b1c3..8ac0d689 100644 --- a/package.json +++ b/package.json @@ -50,5 +50,8 @@ }, "peerDependencies": { "vue": "^3.2.13" + }, + "dependencies": { + "fflate": "^0.7.3" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ab926bf1..f4fff07e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,6 +7,7 @@ specifiers: '@types/node': ^16.11.12 '@vitejs/plugin-vue': ^1.9.0 codemirror: ^5.62.3 + fflate: ^0.7.3 hash-sum: ^2.0.0 rimraf: ^3.0.2 sucrase: ^3.20.1 @@ -15,6 +16,9 @@ specifiers: vue: ^3.2.36 vue-tsc: ^0.34.15 +dependencies: + fflate: 0.7.3 + devDependencies: '@babel/types': 7.17.12 '@microsoft/api-extractor': 7.24.0 @@ -608,6 +612,10 @@ packages: resolution: {integrity: sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==} dev: true + /fflate/0.7.3: + resolution: {integrity: sha512-0Zz1jOzJWERhyhsimS54VTqOteCNwRtIlh8isdL0AXLo0g7xNTfTL7oWrkmCnPhZGocKIkWHBistBrrpoNH3aw==} + dev: false + /fs-extra/7.0.1: resolution: {integrity: sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==} engines: {node: '>=6 <7 || >=8'} diff --git a/src/utils.ts b/src/utils.ts index bfc6d708..fd458862 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +import { zlibSync, unzlibSync, strToU8, strFromU8 } from 'fflate' + export function debounce(fn: Function, n = 100) { let handle: any return (...args: any[]) => { @@ -8,12 +10,24 @@ export function debounce(fn: Function, n = 100) { } } -// prefer old unicode hacks for backward compatibility -// https://base64.guru/developers/javascript/examples/unicode-strings export function utoa(data: string): string { - return btoa(unescape(encodeURIComponent(data))) + const buffer = strToU8(data) + const zipped = zlibSync(buffer, { level: 9 }) + const binary = strFromU8(zipped, true) + return btoa(binary) } export function atou(base64: string): string { - return decodeURIComponent(escape(atob(base64))) + const binary = atob(base64) + + // zlib header (x78), level 9 (xDA) + if (binary.startsWith('\x78\xDA')) { + const buffer = strToU8(binary, true) + const unzipped = unzlibSync(buffer) + return strFromU8(unzipped) + } + + // old unicode hacks for backward compatibility + // https://base64.guru/developers/javascript/examples/unicode-strings + return decodeURIComponent(escape(binary)) }