Skip to content

Commit 4fdfcb7

Browse files
committed
refactor: replace pako with fflate
1 parent 84d81de commit 4fdfcb7

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"@microsoft/api-extractor": "^7.19.2",
3939
"@types/codemirror": "^5.60.2",
4040
"@types/node": "^16.11.12",
41-
"@types/pako": "^2.0.0",
4241
"@vitejs/plugin-vue": "^1.9.0",
4342
"codemirror": "^5.62.3",
4443
"hash-sum": "^2.0.0",
@@ -53,6 +52,6 @@
5352
"vue": "^3.2.13"
5453
},
5554
"dependencies": {
56-
"pako": "^2.0.4"
55+
"fflate": "^0.7.3"
5756
}
5857
}

pnpm-lock.yaml

Lines changed: 6 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deflate, inflate } from 'pako'
1+
import { zlibSync, unzlibSync, strToU8, strFromU8 } from 'fflate'
22

33
export function debounce(fn: Function, n = 100) {
44
let handle: any
@@ -11,19 +11,23 @@ export function debounce(fn: Function, n = 100) {
1111
}
1212

1313
export function utoa(data: string): string {
14-
const zipped = deflate(data, { level: 9 })
15-
const b = Array.from(zipped, v => String.fromCharCode(v)).join('')
16-
return btoa(b)
14+
const buffer = strToU8(data)
15+
const zipped = zlibSync(buffer, { level: 9 })
16+
const binary = strFromU8(zipped, true)
17+
return btoa(binary)
1718
}
1819

1920
export function atou(base64: string): string {
20-
const b = atob(base64)
21-
if (b.startsWith('\x78\xDA')) {
22-
const buffer = Uint8Array.from(b, (_, i) => b.charCodeAt(i))
23-
return inflate(buffer, { to: 'string' })
21+
const binary = atob(base64)
22+
23+
// DEFLATE header (x78), level 9 (xDA)
24+
if (binary.startsWith('\x78\xDA')) {
25+
const buffer = strToU8(binary, true)
26+
const unzipped = unzlibSync(buffer)
27+
return strFromU8(unzipped)
2428
}
2529

2630
// old unicode hacks for backward compatibility
2731
// https://base64.guru/developers/javascript/examples/unicode-strings
28-
return decodeURIComponent(escape(b))
32+
return decodeURIComponent(escape(binary))
2933
}

0 commit comments

Comments
 (0)