Skip to content

Commit 23b5f5f

Browse files
committed
chore: compress shared config URLs
1 parent 95e873f commit 23b5f5f

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"react-chartjs-2": "^5.3.0",
2525
"react-dom": "^19.1.0",
2626
"react-i18next": "^15.7.2",
27-
"tailwindcss": "^3.4.4"
27+
"tailwindcss": "^3.4.4",
28+
"lz-string": "^1.5.0"
2829
},
2930
"devDependencies": {
3031
"@eslint/js": "^9.25.0",

src/utils/__tests__/shareConfig.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ describe('shareConfig', () => {
1212
expect(decoded).toEqual(data);
1313
});
1414

15+
it('compresses data compared to base64', () => {
16+
const data = { text: 'a'.repeat(1000) };
17+
const encoded = encodeConfig(data);
18+
const json = JSON.stringify(data);
19+
const base64 = Buffer.from(json, 'utf-8').toString('base64');
20+
const raw = encodeURIComponent(base64);
21+
expect(encoded.length).toBeLessThan(raw.length);
22+
});
23+
1524
it('returns null for invalid input', () => {
1625
expect(decodeConfig('%')).toBeNull();
1726
});

src/utils/shareConfig.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1+
import LZString from 'lz-string';
2+
13
export function encodeConfig(data) {
24
const json = JSON.stringify(data);
3-
const base64 = typeof btoa === 'function'
4-
? btoa(json)
5-
: Buffer.from(json, 'utf-8').toString('base64');
6-
return encodeURIComponent(base64);
5+
return LZString.compressToEncodedURIComponent(json);
76
}
87

98
export function decodeConfig(encoded) {
109
try {
11-
const base64 = decodeURIComponent(encoded);
12-
const json = typeof atob === 'function'
13-
? atob(base64)
14-
: Buffer.from(base64, 'base64').toString('utf-8');
15-
return JSON.parse(json);
10+
const json = LZString.decompressFromEncodedURIComponent(encoded);
11+
return json ? JSON.parse(json) : null;
1612
} catch (e) {
1713
console.error('Failed to decode config', e);
1814
return null;

0 commit comments

Comments
 (0)