Spaces:
Running
Running
fix spinning
Browse files- wasm-demo.js +50 -44
wasm-demo.js
CHANGED
@@ -51,53 +51,59 @@ show('tokenizerSpin', false);
|
|
51 |
console.log(`[Main] Client key size: ${res.clientKey.length} bytes`);
|
52 |
console.log(`[Main] Server key size: ${res.serverKey.length} bytes`);
|
53 |
clientKey = res.clientKey; serverKey = res.serverKey;
|
54 |
-
$('keygenStatus').textContent = 'keys generated β';
|
55 |
-
enable('btnEncrypt');
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
}
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
headers:{'Content-Type':'application/json'},
|
88 |
-
body: JSON.stringify({
|
89 |
-
server_key_b64: serverKeyB64
|
90 |
-
})
|
91 |
-
});
|
92 |
-
|
93 |
-
if (!handshakeResponse.ok) {
|
94 |
-
const errorText = await handshakeResponse.text();
|
95 |
-
throw new Error(`Server handshake failed: ${handshakeResponse.status} ${errorText}`);
|
96 |
}
|
97 |
-
|
98 |
-
const { uid } = await handshakeResponse.json();
|
99 |
-
sessionUid = uid;
|
100 |
-
console.log('[Main] Server handshake successful');
|
101 |
} else {
|
102 |
console.error('[Main] Key generation error:', e.data.error);
|
103 |
$('keygenStatus').textContent = `Error generating keys: ${e.data.error}`;
|
|
|
51 |
console.log(`[Main] Client key size: ${res.clientKey.length} bytes`);
|
52 |
console.log(`[Main] Server key size: ${res.serverKey.length} bytes`);
|
53 |
clientKey = res.clientKey; serverKey = res.serverKey;
|
|
|
|
|
54 |
|
55 |
+
try {
|
56 |
+
// Initialize encryption worker
|
57 |
+
encryptWorker = new Worker(new URL('./encrypt-worker.js', import.meta.url), { type: 'module' });
|
58 |
+
encryptWorker.onmessage = function(e) {
|
59 |
+
if (e.data.type === 'ready') {
|
60 |
+
console.log('[Main] Encryption worker ready');
|
61 |
+
} else if (e.data.type === 'success') {
|
62 |
+
encTokens = e.data.result;
|
63 |
+
console.log(`[Main] Encryption completed: ${encTokens.length} bytes`);
|
64 |
+
show('encryptSpin', false);
|
65 |
+
show('encIcon', true);
|
66 |
+
enable('btnEncrypt', true);
|
67 |
+
enable('btnSend');
|
68 |
+
$('encStatus').textContent = 'Your text is encrypted π';
|
69 |
+
} else if (e.data.type === 'error') {
|
70 |
+
console.error('[Main] Encryption error:', e.data.error);
|
71 |
+
show('encryptSpin', false);
|
72 |
+
enable('btnEncrypt', true);
|
73 |
+
$('encStatus').textContent = `Encryption failed: ${e.data.error}`;
|
74 |
+
alert(`Encryption failed: ${e.data.error}`);
|
75 |
+
}
|
76 |
+
};
|
77 |
+
|
78 |
+
// Initialize the worker with the client key
|
79 |
+
encryptWorker.postMessage({ type: 'init', clientKey });
|
80 |
+
|
81 |
+
console.log('[Main] Encoding server key to base64...');
|
82 |
+
const serverKeyB64 = await uint8ToBase64(serverKey);
|
83 |
+
console.log(`[Main] Server key base64 size: ${serverKeyB64.length} bytes`);
|
84 |
+
const handshakeResponse = await fetch(`${SERVER}/handshake`, {
|
85 |
+
method:'POST',
|
86 |
+
headers:{'Content-Type':'application/json'},
|
87 |
+
body: JSON.stringify({
|
88 |
+
server_key_b64: serverKeyB64
|
89 |
+
})
|
90 |
+
});
|
91 |
+
|
92 |
+
if (!handshakeResponse.ok) {
|
93 |
+
const errorText = await handshakeResponse.text();
|
94 |
+
throw new Error(`Server handshake failed: ${handshakeResponse.status} ${errorText}`);
|
95 |
}
|
96 |
+
|
97 |
+
const { uid } = await handshakeResponse.json();
|
98 |
+
sessionUid = uid;
|
99 |
+
console.log('[Main] Server handshake successful');
|
100 |
+
$('keygenStatus').textContent = 'keys generated β';
|
101 |
+
enable('btnEncrypt');
|
102 |
+
} catch (error) {
|
103 |
+
console.error('[Main] Server handshake error:', error);
|
104 |
+
$('keygenStatus').textContent = `Server handshake failed: ${error.message}`;
|
105 |
+
enable('btnEncrypt', false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
}
|
|
|
|
|
|
|
|
|
107 |
} else {
|
108 |
console.error('[Main] Key generation error:', e.data.error);
|
109 |
$('keygenStatus').textContent = `Error generating keys: ${e.data.error}`;
|