From fe7c88edf1137e638833f986897cd8e8f00134d4 Mon Sep 17 00:00:00 2001 From: Kael Date: Wed, 8 Jun 2022 03:10:45 +1000 Subject: [PATCH 1/4] feat: gzip serialized state --- package.json | 3 +++ pnpm-lock.yaml | 8 ++++++++ src/env.d.ts | 2 ++ src/utils.ts | 18 ++++++++++++++---- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index ecd3b1c3..87bf31e9 100644 --- a/package.json +++ b/package.json @@ -50,5 +50,8 @@ }, "peerDependencies": { "vue": "^3.2.13" + }, + "dependencies": { + "pako": "^2.0.4" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ab926bf1..bf365cfa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,7 @@ specifiers: '@vitejs/plugin-vue': ^1.9.0 codemirror: ^5.62.3 hash-sum: ^2.0.0 + pako: ^2.0.4 rimraf: ^3.0.2 sucrase: ^3.20.1 typescript: ^4.5.4 @@ -15,6 +16,9 @@ specifiers: vue: ^3.2.36 vue-tsc: ^0.34.15 +dependencies: + pako: 2.0.4 + devDependencies: '@babel/types': 7.17.12 '@microsoft/api-extractor': 7.24.0 @@ -766,6 +770,10 @@ packages: wrappy: 1.0.2 dev: true + /pako/2.0.4: + resolution: {integrity: sha512-v8tweI900AUkZN6heMU/4Uy4cXRc2AYNRggVmTR+dEncawDJgCdLMximOVA2p4qO57WMynangsfGRb5WD6L1Bg==} + dev: false + /path-is-absolute/1.0.1: resolution: {integrity: sha1-F0uSaHNVNP+8es5r9TpanhtcX18=} engines: {node: '>=0.10.0'} diff --git a/src/env.d.ts b/src/env.d.ts index 2cb444d2..3fc91fb4 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -5,3 +5,5 @@ declare module '*.vue' { const comp: ComponentOptions export default comp } + +declare module 'pako' diff --git a/src/utils.ts b/src/utils.ts index bfc6d708..161ef6a9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +import { gzip, ungzip } from 'pako' + export function debounce(fn: Function, n = 100) { let handle: any return (...args: any[]) => { @@ -8,12 +10,20 @@ 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 zipped: Uint8Array = gzip(data, { level: 9 }) + const b = Array.from(zipped, v => String.fromCharCode(v)).join('') + return btoa(b) } export function atou(base64: string): string { - return decodeURIComponent(escape(atob(base64))) + const b = atob(base64) + if (b.startsWith('\x1F\x8B')) { + const buffer = Uint8Array.from(b, (_, i) => b.charCodeAt(i)) + return ungzip(buffer, { to: 'string' }) + } + + // old unicode hacks for backward compatibility + // https://base64.guru/developers/javascript/examples/unicode-strings + return decodeURIComponent(escape(b)) } From 3e4fedc523e9071c515697a64253a84c9bfa4641 Mon Sep 17 00:00:00 2001 From: Kael Date: Wed, 8 Jun 2022 04:43:20 +1000 Subject: [PATCH 2/4] refactor: use deflate instead of gzip removes checksum and header --- src/utils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 161ef6a9..7a38a521 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import { gzip, ungzip } from 'pako' +import { deflate, inflate } from 'pako' export function debounce(fn: Function, n = 100) { let handle: any @@ -11,16 +11,16 @@ export function debounce(fn: Function, n = 100) { } export function utoa(data: string): string { - const zipped: Uint8Array = gzip(data, { level: 9 }) + const zipped: Uint8Array = deflate(data, { level: 9 }) const b = Array.from(zipped, v => String.fromCharCode(v)).join('') return btoa(b) } export function atou(base64: string): string { const b = atob(base64) - if (b.startsWith('\x1F\x8B')) { + if (b.startsWith('\x78\xDA')) { const buffer = Uint8Array.from(b, (_, i) => b.charCodeAt(i)) - return ungzip(buffer, { to: 'string' }) + return inflate(buffer, { to: 'string' }) } // old unicode hacks for backward compatibility From 84d81decdcce9872280dd6ccf500792366c66ffc Mon Sep 17 00:00:00 2001 From: Kael Date: Fri, 24 Jun 2022 19:35:41 +1000 Subject: [PATCH 3/4] refactor: use @types/pako --- package.json | 1 + pnpm-lock.yaml | 6 ++++++ src/env.d.ts | 2 -- src/utils.ts | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 87bf31e9..fc0244cb 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "@microsoft/api-extractor": "^7.19.2", "@types/codemirror": "^5.60.2", "@types/node": "^16.11.12", + "@types/pako": "^2.0.0", "@vitejs/plugin-vue": "^1.9.0", "codemirror": "^5.62.3", "hash-sum": "^2.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bf365cfa..afc9a137 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,7 @@ specifiers: '@microsoft/api-extractor': ^7.19.2 '@types/codemirror': ^5.60.2 '@types/node': ^16.11.12 + '@types/pako': ^2.0.0 '@vitejs/plugin-vue': ^1.9.0 codemirror: ^5.62.3 hash-sum: ^2.0.0 @@ -24,6 +25,7 @@ devDependencies: '@microsoft/api-extractor': 7.24.0 '@types/codemirror': 5.60.5 '@types/node': 16.11.36 + '@types/pako': 2.0.0 '@vitejs/plugin-vue': 1.10.2_vite@2.9.9 codemirror: 5.65.3 hash-sum: 2.0.0 @@ -148,6 +150,10 @@ packages: resolution: {integrity: sha512-FR5QJe+TaoZ2GsMHkjuwoNabr+UrJNRr2HNOo+r/7vhcuntM6Ee/pRPOnRhhL2XE9OOvX9VLEq+BcXl3VjNoWA==} dev: true + /@types/pako/2.0.0: + resolution: {integrity: sha512-10+iaz93qR5WYxTo+PMifD5TSxiOtdRaxBf7INGGXMQgTCu8Z/7GYWYFUOS3q/G0nE5boj1r4FEB+WSy7s5gbA==} + dev: true + /@types/tern/0.23.4: resolution: {integrity: sha512-JAUw1iXGO1qaWwEOzxTKJZ/5JxVeON9kvGZ/osgZaJImBnyjyn0cjovPsf6FNLmyGY8Vw9DoXZCMlfMkMwHRWg==} dependencies: diff --git a/src/env.d.ts b/src/env.d.ts index 3fc91fb4..2cb444d2 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -5,5 +5,3 @@ declare module '*.vue' { const comp: ComponentOptions export default comp } - -declare module 'pako' diff --git a/src/utils.ts b/src/utils.ts index 7a38a521..64e88888 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,7 +11,7 @@ export function debounce(fn: Function, n = 100) { } export function utoa(data: string): string { - const zipped: Uint8Array = deflate(data, { level: 9 }) + const zipped = deflate(data, { level: 9 }) const b = Array.from(zipped, v => String.fromCharCode(v)).join('') return btoa(b) } From a2e7822f44e5af10f868c889a46bde4fbd594290 Mon Sep 17 00:00:00 2001 From: Kael Date: Fri, 24 Jun 2022 21:06:15 +1000 Subject: [PATCH 4/4] refactor: replace pako with fflate --- package.json | 3 +-- pnpm-lock.yaml | 18 ++++++------------ src/utils.ts | 22 +++++++++++++--------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index fc0244cb..8ac0d689 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "@microsoft/api-extractor": "^7.19.2", "@types/codemirror": "^5.60.2", "@types/node": "^16.11.12", - "@types/pako": "^2.0.0", "@vitejs/plugin-vue": "^1.9.0", "codemirror": "^5.62.3", "hash-sum": "^2.0.0", @@ -53,6 +52,6 @@ "vue": "^3.2.13" }, "dependencies": { - "pako": "^2.0.4" + "fflate": "^0.7.3" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index afc9a137..f4fff07e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,11 +5,10 @@ specifiers: '@microsoft/api-extractor': ^7.19.2 '@types/codemirror': ^5.60.2 '@types/node': ^16.11.12 - '@types/pako': ^2.0.0 '@vitejs/plugin-vue': ^1.9.0 codemirror: ^5.62.3 + fflate: ^0.7.3 hash-sum: ^2.0.0 - pako: ^2.0.4 rimraf: ^3.0.2 sucrase: ^3.20.1 typescript: ^4.5.4 @@ -18,14 +17,13 @@ specifiers: vue-tsc: ^0.34.15 dependencies: - pako: 2.0.4 + fflate: 0.7.3 devDependencies: '@babel/types': 7.17.12 '@microsoft/api-extractor': 7.24.0 '@types/codemirror': 5.60.5 '@types/node': 16.11.36 - '@types/pako': 2.0.0 '@vitejs/plugin-vue': 1.10.2_vite@2.9.9 codemirror: 5.65.3 hash-sum: 2.0.0 @@ -150,10 +148,6 @@ packages: resolution: {integrity: sha512-FR5QJe+TaoZ2GsMHkjuwoNabr+UrJNRr2HNOo+r/7vhcuntM6Ee/pRPOnRhhL2XE9OOvX9VLEq+BcXl3VjNoWA==} dev: true - /@types/pako/2.0.0: - resolution: {integrity: sha512-10+iaz93qR5WYxTo+PMifD5TSxiOtdRaxBf7INGGXMQgTCu8Z/7GYWYFUOS3q/G0nE5boj1r4FEB+WSy7s5gbA==} - dev: true - /@types/tern/0.23.4: resolution: {integrity: sha512-JAUw1iXGO1qaWwEOzxTKJZ/5JxVeON9kvGZ/osgZaJImBnyjyn0cjovPsf6FNLmyGY8Vw9DoXZCMlfMkMwHRWg==} dependencies: @@ -618,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'} @@ -776,10 +774,6 @@ packages: wrappy: 1.0.2 dev: true - /pako/2.0.4: - resolution: {integrity: sha512-v8tweI900AUkZN6heMU/4Uy4cXRc2AYNRggVmTR+dEncawDJgCdLMximOVA2p4qO57WMynangsfGRb5WD6L1Bg==} - dev: false - /path-is-absolute/1.0.1: resolution: {integrity: sha1-F0uSaHNVNP+8es5r9TpanhtcX18=} engines: {node: '>=0.10.0'} diff --git a/src/utils.ts b/src/utils.ts index 64e88888..fd458862 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import { deflate, inflate } from 'pako' +import { zlibSync, unzlibSync, strToU8, strFromU8 } from 'fflate' export function debounce(fn: Function, n = 100) { let handle: any @@ -11,19 +11,23 @@ export function debounce(fn: Function, n = 100) { } export function utoa(data: string): string { - const zipped = deflate(data, { level: 9 }) - const b = Array.from(zipped, v => String.fromCharCode(v)).join('') - return btoa(b) + const buffer = strToU8(data) + const zipped = zlibSync(buffer, { level: 9 }) + const binary = strFromU8(zipped, true) + return btoa(binary) } export function atou(base64: string): string { - const b = atob(base64) - if (b.startsWith('\x78\xDA')) { - const buffer = Uint8Array.from(b, (_, i) => b.charCodeAt(i)) - return inflate(buffer, { to: 'string' }) + 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(b)) + return decodeURIComponent(escape(binary)) }