Skip to content

Commit 7a72722

Browse files
committed
chore(site): improve compressed
1 parent 76cef61 commit 7a72722

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

docs/.vitepress/theme/components/state/deserialize.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,32 @@ export function deserializeState(serializedString) {
1616
}
1717

1818
try {
19-
// For backward compatibility, it can address non-compressed data.
20-
const compressed = !serializedString.startsWith("eyJj")
21-
const decodedText = window.atob(serializedString)
22-
const jsonText = compressed
23-
? pako.inflate(decodedText, { to: "string" })
24-
: decodedText
25-
const json = JSON.parse(jsonText)
19+
// For backward compatibility
20+
const newCompressed = serializedString.startsWith("eJxd")
21+
const json = (
22+
newCompressed
23+
? () => {
24+
const compressedString = window.atob(serializedString)
25+
const uint8Arr = pako.inflate(
26+
Uint8Array.from(compressedString, (c) =>
27+
c.charCodeAt(0),
28+
),
29+
)
30+
const jsonText = new TextDecoder().decode(uint8Arr)
31+
return JSON.parse(jsonText)
32+
}
33+
: () => {
34+
const decodedText = window.atob(serializedString)
35+
try {
36+
const jsonText = pako.inflate(decodedText, {
37+
to: "string",
38+
})
39+
return JSON.parse(jsonText)
40+
} catch {
41+
return JSON.parse(decodedText)
42+
}
43+
}
44+
)()
2645

2746
if (typeof json === "object" && json != null) {
2847
if (typeof json.code === "string") {

docs/.vitepress/theme/components/state/serialize.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export function serializeState(state) {
2525
rules: state.rules ? getEnabledRules(state.rules) : undefined,
2626
}
2727
const jsonString = JSON.stringify(saveData)
28-
const compressedString = pako.deflate(jsonString, { to: "string" })
28+
const uint8Arr = new TextEncoder().encode(jsonString)
29+
const compressedString = String.fromCharCode(...pako.deflate(uint8Arr))
2930
const base64 =
3031
(typeof window !== "undefined" && window.btoa(compressedString)) ||
3132
compressedString

0 commit comments

Comments
 (0)