Upload folder using huggingface_hub
Browse files- 6.2.0/browserstate/Index.svelte +63 -0
- 6.2.0/browserstate/crypto.ts +28 -0
- 6.2.0/browserstate/package.json +34 -0
6.2.0/browserstate/Index.svelte
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<svelte:options accessors={true} />
|
| 2 |
+
|
| 3 |
+
<script lang="ts">
|
| 4 |
+
import { onMount } from "svelte";
|
| 5 |
+
import { encrypt, decrypt } from "./crypto";
|
| 6 |
+
import { Gradio } from "@gradio/utils";
|
| 7 |
+
|
| 8 |
+
// export let storage_key: string;
|
| 9 |
+
// export let secret: string;
|
| 10 |
+
// export let default_value: any;
|
| 11 |
+
// export let value = default_value;
|
| 12 |
+
// let old_value = value;
|
| 13 |
+
|
| 14 |
+
let props = $props();
|
| 15 |
+
let gradio = new Gradio<
|
| 16 |
+
{
|
| 17 |
+
change: never;
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
storage_key: string;
|
| 21 |
+
secret: string;
|
| 22 |
+
default_value: any;
|
| 23 |
+
value: any;
|
| 24 |
+
}
|
| 25 |
+
>(props);
|
| 26 |
+
|
| 27 |
+
function load_value(): void {
|
| 28 |
+
const stored = localStorage.getItem(gradio.props.storage_key);
|
| 29 |
+
if (!stored) {
|
| 30 |
+
gradio.props.value = gradio.props.default_value;
|
| 31 |
+
return;
|
| 32 |
+
}
|
| 33 |
+
try {
|
| 34 |
+
const decrypted = decrypt(stored, gradio.props.secret);
|
| 35 |
+
gradio.props.value = JSON.parse(decrypted);
|
| 36 |
+
} catch (e) {
|
| 37 |
+
console.error("Error reading from localStorage:", e);
|
| 38 |
+
gradio.props.value = gradio.props.default_value;
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
function save_value(): void {
|
| 43 |
+
if (!gradio.props.value) return;
|
| 44 |
+
try {
|
| 45 |
+
const encrypted = encrypt(
|
| 46 |
+
JSON.stringify(gradio.props.value),
|
| 47 |
+
gradio.props.secret
|
| 48 |
+
);
|
| 49 |
+
localStorage.setItem(gradio.props.storage_key, encrypted);
|
| 50 |
+
} catch (e) {
|
| 51 |
+
console.error("Error writing to localStorage:", e);
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
$effect(() => {
|
| 56 |
+
save_value();
|
| 57 |
+
gradio.dispatch("change");
|
| 58 |
+
});
|
| 59 |
+
|
| 60 |
+
onMount(() => {
|
| 61 |
+
load_value();
|
| 62 |
+
});
|
| 63 |
+
</script>
|
6.2.0/browserstate/crypto.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import CryptoJS from "crypto-js";
|
| 2 |
+
|
| 3 |
+
export function encrypt(data: string, key: string): string {
|
| 4 |
+
const hashedKey = CryptoJS.SHA256(key).toString();
|
| 5 |
+
const iv = CryptoJS.lib.WordArray.random(16);
|
| 6 |
+
const encrypted = CryptoJS.AES.encrypt(data, hashedKey, {
|
| 7 |
+
iv: iv,
|
| 8 |
+
mode: CryptoJS.mode.CBC,
|
| 9 |
+
padding: CryptoJS.pad.Pkcs7
|
| 10 |
+
});
|
| 11 |
+
|
| 12 |
+
const ivString = CryptoJS.enc.Base64.stringify(iv);
|
| 13 |
+
const cipherString = encrypted.toString();
|
| 14 |
+
return ivString + ":" + cipherString;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
export function decrypt(encryptedData: string, key: string): string {
|
| 18 |
+
const hashedKey = CryptoJS.SHA256(key).toString();
|
| 19 |
+
const [ivString, cipherString] = encryptedData.split(":");
|
| 20 |
+
const iv = CryptoJS.enc.Base64.parse(ivString);
|
| 21 |
+
const decrypted = CryptoJS.AES.decrypt(cipherString, hashedKey, {
|
| 22 |
+
iv: iv,
|
| 23 |
+
mode: CryptoJS.mode.CBC,
|
| 24 |
+
padding: CryptoJS.pad.Pkcs7
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
return decrypted.toString(CryptoJS.enc.Utf8);
|
| 28 |
+
}
|
6.2.0/browserstate/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/browserstate",
|
| 3 |
+
"version": "0.3.3",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"main_changeset": true,
|
| 10 |
+
"exports": {
|
| 11 |
+
".": {
|
| 12 |
+
"gradio": "./Index.svelte",
|
| 13 |
+
"svelte": "./dist/Index.svelte",
|
| 14 |
+
"types": "./dist/Index.svelte.d.ts"
|
| 15 |
+
},
|
| 16 |
+
"./package.json": "./package.json"
|
| 17 |
+
},
|
| 18 |
+
"dependencies": {
|
| 19 |
+
"@gradio/utils": "workspace:^",
|
| 20 |
+
"crypto-js": "^4.2.0",
|
| 21 |
+
"dequal": "^2.0.3"
|
| 22 |
+
},
|
| 23 |
+
"peerDependencies": {
|
| 24 |
+
"svelte": "^5.43.4"
|
| 25 |
+
},
|
| 26 |
+
"repository": {
|
| 27 |
+
"type": "git",
|
| 28 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 29 |
+
"directory": "js/state"
|
| 30 |
+
},
|
| 31 |
+
"devDependencies": {
|
| 32 |
+
"@types/crypto-js": "^4.2.2"
|
| 33 |
+
}
|
| 34 |
+
}
|