co-mono/packages/web-ui/src/state/storage-adapter.ts
Mario Zechner aaea0f4600 Integrate JailJS for CSP-restricted execution in browser extension
Major changes:
- Migrate browser-extension to use web-ui package (85% code reduction)
- Add JailJS content script with ES6+ transform support
- Expose DOM constructors (Event, KeyboardEvent, etc.) to JailJS
- Support top-level await by wrapping code in async IIFE
- Add returnFile() support in JailJS execution
- Refactor KeyStore into pluggable storage-adapter pattern
- Make ChatPanel configurable with sandboxUrlProvider and additionalTools
- Update jailjs to 0.1.1

Files deleted (33 duplicate files):
- All browser-extension components, dialogs, state, tools, utils
- Now using web-ui versions via @mariozechner/pi-web-ui

Files added:
- packages/browser-extension/src/content.ts (JailJS content script)
- packages/web-ui/src/state/storage-adapter.ts
- packages/web-ui/src/state/key-store.ts

Browser extension now has only 5 source files (down from 38).
2025-10-05 16:58:31 +02:00

77 lines
2 KiB
TypeScript

/**
* Generic storage adapter interface for key/value persistence
*/
export interface StorageAdapter {
get(key: string): Promise<string | null>;
set(key: string, value: string): Promise<void>;
remove(key: string): Promise<void>;
getAll(): Promise<Record<string, string>>;
}
/**
* LocalStorage implementation
*/
export class LocalStorageAdapter implements StorageAdapter {
async get(key: string): Promise<string | null> {
return localStorage.getItem(key);
}
async set(key: string, value: string): Promise<void> {
localStorage.setItem(key, value);
}
async remove(key: string): Promise<void> {
localStorage.removeItem(key);
}
async getAll(): Promise<Record<string, string>> {
const result: Record<string, string> = {};
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key) {
const value = localStorage.getItem(key);
if (value) result[key] = value;
}
}
return result;
}
}
/**
* Chrome/Firefox extension storage implementation
*/
export class ChromeStorageAdapter implements StorageAdapter {
private readonly storage: any;
constructor() {
const isBrowser = typeof globalThis !== "undefined";
const hasChrome = isBrowser && (globalThis as any).chrome?.storage;
const hasBrowser = isBrowser && (globalThis as any).browser?.storage;
if (hasBrowser) {
this.storage = (globalThis as any).browser.storage.local;
} else if (hasChrome) {
this.storage = (globalThis as any).chrome.storage.local;
} else {
throw new Error("Chrome/Browser storage not available");
}
}
async get(key: string): Promise<string | null> {
const result = await this.storage.get(key);
return result[key] || null;
}
async set(key: string, value: string): Promise<void> {
await this.storage.set({ [key]: value });
}
async remove(key: string): Promise<void> {
await this.storage.remove(key);
}
async getAll(): Promise<Record<string, string>> {
const result = await this.storage.get();
return result || {};
}
}