Skip to content

Commit

Permalink
feat: gzip serialized state (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaelWD authored Jun 26, 2022
1 parent e41a180 commit b12eb88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
},
"peerDependencies": {
"vue": "^3.2.13"
},
"dependencies": {
"fflate": "^0.7.3"
}
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { zlibSync, unzlibSync, strToU8, strFromU8 } from 'fflate'

export function debounce(fn: Function, n = 100) {
let handle: any
return (...args: any[]) => {
Expand All @@ -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))
}

0 comments on commit b12eb88

Please sign in to comment.