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
+50 -46
View File
@@ -1,74 +1,78 @@
import { PrivateKey } from "@xo-cash/primitives";
import { PrivateKey } from '@xo-cash/primitives';
// Core clients
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 { WsClient } from "../source/ws/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";
import { EncryptedClientPerResource } from './experimental/encrypted-client-per-resource.js';
import { EncryptedClient } from './experimental/encrypted-client.js';
// Shared functions
import { writeMany, readMany } from "./shared.js";
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)),
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);
});
client.on('resource-instance-updated', (message) => {
console.log(message);
});
// Connect to the sync server
await client.connect();
// Connect to the sync server
await client.connect();
// Subscribe to the resource
await client.subscribe('test');
// 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});
// 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));
// 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 });
// 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 });
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);
});
}
// 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);
// 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);
}
// 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]);
}
// 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);
@@ -78,7 +82,7 @@ const clientsToTest = selectClients(params);
// Test the clients
for (const client of clientsToTest) {
await testSuite(client);
await testSuite(client);
}
// Wait for the user to press Ctrl+C
@@ -86,4 +90,4 @@ await new Promise((resolve) => process.on('SIGINT', resolve));
// Exit the program
console.log('Exiting...');
process.exit(0);
process.exit(0);