206 lines
6.2 KiB
JavaScript
206 lines
6.2 KiB
JavaScript
import createPlugin from '@extism/extism';
|
|
|
|
let plugin = null;
|
|
let generatedDatabase = null;
|
|
|
|
function log(message, type = 'info') {
|
|
const consoleLog = document.getElementById('consoleLog');
|
|
const timestamp = new Date().toISOString().split('T')[1].split('.')[0];
|
|
const prefix = type === 'error' ? '[ERROR]' : type === 'success' ? '[OK]' : '[INFO]';
|
|
consoleLog.textContent += `${timestamp} ${prefix} ${message}\n`;
|
|
consoleLog.scrollTop = consoleLog.scrollHeight;
|
|
console[type === 'error' ? 'error' : 'log'](message);
|
|
}
|
|
|
|
function setStatus(message, type) {
|
|
const status = document.getElementById('status');
|
|
status.textContent = message;
|
|
status.className = `status ${type}`;
|
|
}
|
|
|
|
function formatOutput(data) {
|
|
try {
|
|
if (typeof data === 'string') {
|
|
const parsed = JSON.parse(data);
|
|
return JSON.stringify(parsed, null, 2);
|
|
}
|
|
return JSON.stringify(data, null, 2);
|
|
} catch {
|
|
return String(data);
|
|
}
|
|
}
|
|
|
|
async function loadPlugin() {
|
|
setStatus('Loading plugin...', 'loading');
|
|
log('Loading enclave.wasm...');
|
|
|
|
try {
|
|
const wasmUrl = new URL('./enclave.wasm', window.location.href).href;
|
|
log(`WASM URL: ${wasmUrl}`);
|
|
|
|
const manifest = {
|
|
wasm: [{ url: wasmUrl }]
|
|
};
|
|
|
|
plugin = await createPlugin(manifest, {
|
|
useWasi: true,
|
|
logger: console
|
|
});
|
|
|
|
setStatus('Plugin loaded successfully', 'success');
|
|
log('Plugin loaded successfully', 'success');
|
|
} catch (error) {
|
|
setStatus(`Failed to load plugin: ${error.message}`, 'error');
|
|
log(`Failed to load plugin: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
async function testGenerate() {
|
|
if (!plugin) {
|
|
log('Plugin not loaded', 'error');
|
|
return;
|
|
}
|
|
|
|
const output = document.getElementById('generateOutput');
|
|
const credential = document.getElementById('credentialInput').value;
|
|
|
|
log(`Calling generate() with credential: ${credential.substring(0, 20)}...`);
|
|
output.textContent = 'Running...';
|
|
|
|
try {
|
|
const input = JSON.stringify({ credential });
|
|
const result = await plugin.call('generate', input);
|
|
const data = result.json();
|
|
|
|
output.textContent = formatOutput(data);
|
|
log(`generate() completed. DID: ${data.did}`, 'success');
|
|
|
|
if (data.database) {
|
|
generatedDatabase = data.database;
|
|
log('Database buffer stored for load() test');
|
|
}
|
|
} catch (error) {
|
|
output.textContent = `Error: ${error.message}`;
|
|
log(`generate() failed: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
async function testLoad() {
|
|
if (!plugin) {
|
|
log('Plugin not loaded', 'error');
|
|
return;
|
|
}
|
|
|
|
const output = document.getElementById('loadOutput');
|
|
const databaseInput = document.getElementById('databaseInput').value;
|
|
|
|
if (!databaseInput) {
|
|
output.textContent = 'Error: Database buffer is required';
|
|
log('load() requires database buffer', 'error');
|
|
return;
|
|
}
|
|
|
|
log('Calling load()...');
|
|
output.textContent = 'Running...';
|
|
|
|
try {
|
|
const input = JSON.stringify({
|
|
database: Array.from(atob(databaseInput), c => c.charCodeAt(0))
|
|
});
|
|
const result = await plugin.call('load', input);
|
|
const data = result.json();
|
|
|
|
output.textContent = formatOutput(data);
|
|
log(`load() completed. Success: ${data.success}`, data.success ? 'success' : 'error');
|
|
} catch (error) {
|
|
output.textContent = `Error: ${error.message}`;
|
|
log(`load() failed: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
function useGeneratedDb() {
|
|
if (generatedDatabase) {
|
|
const base64 = btoa(String.fromCharCode(...generatedDatabase));
|
|
document.getElementById('databaseInput').value = base64;
|
|
log('Populated database input with generated database');
|
|
} else {
|
|
log('No generated database available. Run generate() first.', 'error');
|
|
}
|
|
}
|
|
|
|
async function testExec() {
|
|
if (!plugin) {
|
|
log('Plugin not loaded', 'error');
|
|
return;
|
|
}
|
|
|
|
const output = document.getElementById('execOutput');
|
|
const filter = document.getElementById('filterInput').value;
|
|
const token = document.getElementById('tokenInput').value;
|
|
|
|
if (!filter) {
|
|
output.textContent = 'Error: Filter is required';
|
|
log('exec() requires filter', 'error');
|
|
return;
|
|
}
|
|
|
|
log(`Calling exec() with filter: ${filter}`);
|
|
output.textContent = 'Running...';
|
|
|
|
try {
|
|
const input = JSON.stringify({ filter, token: token || undefined });
|
|
const result = await plugin.call('exec', input);
|
|
const data = result.json();
|
|
|
|
output.textContent = formatOutput(data);
|
|
log(`exec() completed. Success: ${data.success}`, data.success ? 'success' : 'error');
|
|
} catch (error) {
|
|
output.textContent = `Error: ${error.message}`;
|
|
log(`exec() failed: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
function setFilter(filter) {
|
|
document.getElementById('filterInput').value = filter;
|
|
}
|
|
|
|
async function testQuery() {
|
|
if (!plugin) {
|
|
log('Plugin not loaded', 'error');
|
|
return;
|
|
}
|
|
|
|
const output = document.getElementById('queryOutput');
|
|
const did = document.getElementById('didInput').value;
|
|
|
|
log(`Calling query() with DID: ${did || '(current)'}`);
|
|
output.textContent = 'Running...';
|
|
|
|
try {
|
|
const input = JSON.stringify({ did: did || '' });
|
|
const result = await plugin.call('query', input);
|
|
const data = result.json();
|
|
|
|
output.textContent = formatOutput(data);
|
|
log(`query() completed. DID: ${data.did}`, 'success');
|
|
} catch (error) {
|
|
output.textContent = `Error: ${error.message}`;
|
|
log(`query() failed: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
function clearLog() {
|
|
document.getElementById('consoleLog').textContent = '';
|
|
}
|
|
|
|
window.loadPlugin = loadPlugin;
|
|
window.testGenerate = testGenerate;
|
|
window.testLoad = testLoad;
|
|
window.useGeneratedDb = useGeneratedDb;
|
|
window.testExec = testExec;
|
|
window.setFilter = setFilter;
|
|
window.testQuery = testQuery;
|
|
window.clearLog = clearLog;
|
|
|
|
loadPlugin();
|