Add experiment encyption clients. Rewrite demos. Reduce implementation code. Add value decoding. Rename to base-client. Type inference and generics

This commit is contained in:
2026-10-22 14:51:46 +00:00
parent 2f9e46f659
commit df2c42e024
11 changed files with 923 additions and 283 deletions
+56
View File
@@ -0,0 +1,56 @@
import { PrivateKey } from "@xo-cash/primitives";
import type { BaseSyncClient } from "../src/shared/base-client.js";
import { SSEClient } from "../src/sse/client.js";
import { EncryptedClientPerResource } from "./experimental/encrypted-client-per-resource.js";
const privateKey = PrivateKey.fromString('c440ce6ac5b63ae7bc4af9891b80b7a88ccdeebba5e43246885e9ec56f912ac2');
// Define some formatting helpers
const formatResourceId = (resourceId: string) => {
return `\x1b[32m${resourceId}\x1b[0m`;
}
const formatMessage = (message: string) => {
return `\x1b[34m${message}\x1b[0m`;
}
const resetFormatting = () => {
return `\x1b[0m`;
}
const startTUI = async (client: BaseSyncClient<string>) => {
// Listen and log out the resource change
client.on('resource-instance-updated', (message) => {
// Print the message with colours
console.log(`${formatResourceId(message.resourceId)}:${formatResourceId(message.publicKey)} updated: ${formatMessage(message.value)}${resetFormatting()}`);
});
// Connect to the sync server and subscribe to the resource
await client.connect();
await client.subscribe('encrypted-test');
// Start the TUI loop
while (true) {
// Print the prompt
process.stdout.write('> ');
// Wait for the user to enter a command
const data = await new Promise((resolve) => process.stdin.once('data', resolve));
// If the user entered a command, write it to the resource
if (data) {
const message = data.toString().trim();
await client.write('encrypted-test', message);
}
}
}
const clients = {
'sse': new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey),
'encrypted': new EncryptedClientPerResource<string>(new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey)),
}
const params = process.argv.slice(2);
const client = params[0] === 'false' ? clients.sse : clients.encrypted;
startTUI(client);