Formatting

This commit is contained in:
2026-10-22 15:19:06 +00:00
parent 9cef3be6cb
commit 7f9bb97990
20 changed files with 12364 additions and 12277 deletions
+39 -35
View File
@@ -1,56 +1,60 @@
import { PrivateKey } from "@xo-cash/primitives";
import { PrivateKey } from '@xo-cash/primitives';
import type { BaseSyncClient } from "../source/shared/base-client.js";
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";
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`;
}
return `\x1b[32m${resourceId}\x1b[0m`;
};
const formatMessage = (message: string) => {
return `\x1b[34m${message}\x1b[0m`;
}
return `\x1b[34m${message}\x1b[0m`;
};
const resetFormatting = () => {
return `\x1b[0m`;
}
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()}`);
});
// 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('> ');
// Connect to the sync server and subscribe to the resource
await client.connect();
await client.subscribe('encrypted-test');
// Wait for the user to enter a command
const data = await new Promise((resolve) => process.stdin.once('data', resolve));
// Start the TUI loop
while (true) {
// Print the prompt
process.stdout.write('> ');
// If the user entered a command, write it to the resource
if (data) {
const message = data.toString().trim();
await client.write('encrypted-test', message);
// 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)),
}
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);
startTUI(client);