61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { PrivateKey } from '@xo-cash/primitives';
|
|
|
|
import type { BaseSyncClient } from '../source/shared/base-client.js';
|
|
|
|
import { SSEClient } from '../source/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);
|