94 lines
3.3 KiB
TypeScript
94 lines
3.3 KiB
TypeScript
import { PrivateKey } from '@xo-cash/primitives';
|
|
|
|
// Core clients
|
|
import type { BaseSyncClient } from '../source/shared/base-client.js';
|
|
import { SSEClient } from '../source/sse/client.js';
|
|
import { WsClient } from '../source/ws/client.js';
|
|
|
|
// Experimental clients
|
|
import { EncryptedClientPerResource } from './experimental/encrypted-client-per-resource.js';
|
|
import { EncryptedClient } from './experimental/encrypted-client.js';
|
|
|
|
// Shared functions
|
|
import { writeMany, readMany } from './shared.js';
|
|
|
|
const privateKey = PrivateKey.fromString('c440ce6ac5b63ae7bc4af9891b80b7a88ccdeebba5e43246885e9ec56f912ac2');
|
|
|
|
const clients = {
|
|
sse: new SSEClient<{ message: string }>('https://dev.v2.sync.xo.harvmaster.com', privateKey),
|
|
ws: new WsClient<{ message: string }>('wss://dev.v2.sync.xo.harvmaster.com', privateKey),
|
|
encrypted: new EncryptedClient<{ message: string }>(new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey), 'secret'),
|
|
encryptedPerResource: new EncryptedClientPerResource<{ message: string }>(
|
|
new SSEClient<string>('https://dev.v2.sync.xo.harvmaster.com', privateKey),
|
|
),
|
|
};
|
|
|
|
// Test the suite for a given client
|
|
const testSuite = async (client: BaseSyncClient<{ message: string }>) => {
|
|
client.on('resource-instance-updated', (message) => {
|
|
console.log(message);
|
|
});
|
|
|
|
// Connect to the sync server
|
|
await client.connect();
|
|
|
|
// Subscribe to the resource
|
|
await client.subscribe('test');
|
|
|
|
// Write a new value to the resource
|
|
await client.write('test', {
|
|
message: 'Hello, world! this is the minimal sync client using ' + client.constructor.name,
|
|
});
|
|
|
|
// Read the current value of the resource
|
|
const data = await client.read('test');
|
|
console.log(data.map((resource) => resource.value.message));
|
|
|
|
// Use the shared functions to write and read many resources
|
|
const writtenResources = await writeMany(client);
|
|
console.dir(writtenResources, { depth: null });
|
|
|
|
const readResources = await readMany(client);
|
|
console.dir(readResources, { depth: null });
|
|
|
|
// If the user presses Ctrl+C, disconnect the client and exit the program
|
|
process.on('SIGINT', async () => {
|
|
await client.disconnect();
|
|
process.exit(0);
|
|
});
|
|
};
|
|
|
|
// Get the clients to test
|
|
const selectClients = (params: string[]): BaseSyncClient<{ message: string }>[] => {
|
|
// If no params are provided, test all clients
|
|
const names = params.length > 0 ? params : Object.keys(clients);
|
|
|
|
// Find values that are unknown to the clients object
|
|
const unknown = names.filter((n) => !(n in clients));
|
|
if (unknown.length > 0) {
|
|
console.error(`Invalid client type(s): ${unknown.join(', ')}. Expected: ${Object.keys(clients).join(', ')}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Return the clients that are known to the clients object
|
|
return names.map((n) => clients[n as keyof typeof clients]);
|
|
};
|
|
|
|
// Get the params
|
|
const params = process.argv.slice(2);
|
|
|
|
// Get the clients to test
|
|
const clientsToTest = selectClients(params);
|
|
|
|
// Test the clients
|
|
for (const client of clientsToTest) {
|
|
await testSuite(client);
|
|
}
|
|
|
|
// Wait for the user to press Ctrl+C
|
|
await new Promise((resolve) => process.on('SIGINT', resolve));
|
|
|
|
// Exit the program
|
|
console.log('Exiting...');
|
|
process.exit(0);
|