Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server : (web ui) Enable gzip compression for local storage #10945

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/server/webui/package-lock.json

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

1 change: 1 addition & 0 deletions examples/server/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"highlight.js": "^11.10.0",
"katex": "^0.16.15",
"markdown-it": "^14.1.0",
"pako": "^2.1.0",
"postcss": "^8.4.49",
"tailwindcss": "^3.4.15",
"textlinestream": "^1.1.1",
Expand Down
48 changes: 46 additions & 2 deletions examples/server/webui/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import daisyuiThemes from 'daisyui/src/theming/themes';
// ponyfill for missing ReadableStream asyncIterator on Safari
import { asyncIterator } from '@sec-ant/readable-stream/ponyfill/asyncIterator';

const isDev = import.meta.env.MODE === 'development';
// compression library
import pako from "pako";

const isDev = import.meta.env.MODE === "development";
const useCompression = false; // set to true if you want to use gzip compression for local storage

// utility functions
const isString = (x) => !!x.toLowerCase;
Expand All @@ -38,6 +42,45 @@ const copyStr = (textToCopy) => {
document.execCommand('copy');
}
};
const compressedStorage = {
getItem(key) {
const compressed = window.localStorage.getItem(key);
try {
const bytes = Uint8Array.from(atob(compressed), (c) => c.charCodeAt(0));
return new TextDecoder().decode(this.decompress(bytes));
} catch (error) {
return compressed;
}
},
setItem(key, value) {
const compressed = this.compress(new TextEncoder().encode(value));
const compressedHex = btoa(this.uintToString(compressed));
window.localStorage.setItem(key, compressedHex);
},
removeItem: (key) => window.localStorage.removeItem(key),
clear: () => window.localStorage.clear(),
key: (index) => window.localStorage.key(index),
length: window.localStorage.length,
[Symbol.iterator]: function* () {
for (let i = 0; i < window.localStorage.length; i++) {
yield window.localStorage.key(i);
}
},
uintToString(bytes) {
const chunkSize = 0x8000;
let result = "";
for (let i = 0; i < bytes.length; i += chunkSize) {
result += String.fromCharCode.apply(
null,
bytes.subarray(i, i + chunkSize)
);
}
return result;
},
compress: (encoded) => pako.gzip(encoded, { level: 9 }),
decompress: (bytes) => pako.ungzip(bytes),
};
const localStorage = useCompression ? compressedStorage : window.localStorage;

// constants
const BASE_URL = isDev
Expand Down Expand Up @@ -211,7 +254,8 @@ const StorageUtils = {
// manage conversations
getAllConversations() {
const res = [];
for (const key in localStorage) {
const storage = useCompression ? localStorage : Object.keys(localStorage);
for (const key of storage) {
if (key.startsWith('conv-')) {
res.push(JSON.parse(localStorage.getItem(key)));
}
Expand Down
Loading