Skip to content

Commit d3a9099

Browse files
committed
Update: make URL shorter
1 parent 9c83f43 commit d3a9099

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"material-design-icons": "^3.0.1",
2727
"monaco-editor": "^0.10.1",
2828
"npm-run-all": "^4.1.2",
29+
"pako": "^1.0.6",
2930
"rimraf": "^2.6.2",
3031
"shelljs": "^0.7.8",
3132
"string-replace-loader": "^1.3.0",

src/state.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pako from "pako"
12
import defaultCode from "./default-code.js"
23
import defaultConfig from "./default-config.js"
34

@@ -43,7 +44,10 @@ export function deserializeState(serializedText) {
4344
const state = createDefaultState()
4445

4546
try {
46-
const jsonText = decodeFromBase64(serializedText)
47+
// For backward compatibility, it can address non-compressed data.
48+
const compressed = !serializedText.startsWith("eyJj")
49+
const decodedText = decodeFromBase64(serializedText)
50+
const jsonText = compressed ? pako.inflate(decodedText, { to: "string" }) : decodedText
4751
const json = JSON.parse(jsonText)
4852

4953
if (typeof json === "object" && json !== null) {
@@ -81,5 +85,7 @@ export function serializeState(state) {
8185
},
8286
{}
8387
)
84-
return encodeToBase64(JSON.stringify({ code, rules }))
88+
const jsonText = JSON.stringify({ code, rules })
89+
const compressedText = pako.deflate(jsonText, { to: "string" })
90+
return encodeToBase64(compressedText)
8591
}

0 commit comments

Comments
 (0)