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:
@@ -0,0 +1,267 @@
|
||||
import { binToHex, flattenBinArray, secp256k1, sha256 } from "@bitauth/libauth";
|
||||
|
||||
import { toExtendedJson, EventEmitter, fromExtendedJson } from "@xo-cash/utils";
|
||||
import type { WriteRequest } from "../types.js";
|
||||
import z from "zod";
|
||||
|
||||
export type AuthenticatedRequestHeaders = {
|
||||
'X-Public-Key': string;
|
||||
'X-Timestamp': string;
|
||||
'X-Signature': string;
|
||||
};
|
||||
|
||||
export type SignedPayload = { publicKey: string; signature: string };
|
||||
|
||||
export type XOSyncClientEventMap<T = unknown> = {
|
||||
'connection-updated': 'connected' | 'disconnected';
|
||||
'resource-instance-updated': Resource<T> & {
|
||||
resourceId: string;
|
||||
};
|
||||
'subscriptions-updated': Set<string>;
|
||||
'error': Error;
|
||||
};
|
||||
|
||||
export type Resource<PayloadType = unknown> = z.infer<typeof BaseSyncClient.resourceInstanceSchema> & {
|
||||
value: PayloadType;
|
||||
};
|
||||
|
||||
export type ResourcesRecord<PayloadType = unknown> = Record<string, { instances: Resource<PayloadType>[] }>;
|
||||
|
||||
export abstract class BaseSyncClient<T = unknown> extends EventEmitter<XOSyncClientEventMap<T>> {
|
||||
|
||||
/**
|
||||
* Connects to the sync server
|
||||
*/
|
||||
abstract connect(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Disconnects from the sync server
|
||||
*/
|
||||
abstract disconnect(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Sign and write one resource
|
||||
* @param resourceId - The resource ID to write to
|
||||
* @param value - The value to write
|
||||
* @returns The Resource Object with the decoded value
|
||||
*/
|
||||
async write(resourceId: string, value: T): Promise<Resource<T>[]> {
|
||||
// Write the resource to the server
|
||||
const response = await this.writeMany({ [resourceId]: value });
|
||||
|
||||
// Get the resource from the response
|
||||
const resource = response[resourceId];
|
||||
|
||||
// If the resource is not found, throw an error
|
||||
if (!resource) {
|
||||
throw new Error(`Resource ${resourceId} not found`);
|
||||
}
|
||||
|
||||
// Return the resource instances
|
||||
return resource.instances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes multiple values to resources
|
||||
* @param resources - The resources to write to
|
||||
* @returns The resources with the written values
|
||||
*/
|
||||
abstract writeMany(resources: Record<string, T>): Promise<ResourcesRecord<T>>;
|
||||
|
||||
/**
|
||||
* Reads a value from a resource
|
||||
* @param resourceId - The resource ID to read from
|
||||
* @returns The resource with the read value
|
||||
*/
|
||||
async read(resourceId: string): Promise<Resource<T>[]> {
|
||||
// Read the resource from the server
|
||||
const response = await this.readMany([ resourceId ]);
|
||||
|
||||
// Get the resource from the response
|
||||
const resource = response[resourceId];
|
||||
|
||||
// If the resource is not found, throw an error
|
||||
if (!resource) {
|
||||
throw new Error(`Resource ${resourceId} not found`);
|
||||
}
|
||||
|
||||
// Return the resource instances
|
||||
return resource.instances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads multiple values from resources
|
||||
* @param resourceIds - The resource IDs to read from
|
||||
* @returns The resources with the read values
|
||||
*/
|
||||
abstract readMany(resourceIds: string[]): Promise<ResourcesRecord<T>>;
|
||||
|
||||
/**
|
||||
* Subscribes to a resource
|
||||
* @param resourceId - The resource ID to subscribe to
|
||||
*/
|
||||
abstract subscribe(resourceId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Unsubscribes from a resource
|
||||
* @param resourceId - The resource ID to unsubscribe from
|
||||
*/
|
||||
abstract unsubscribe(resourceId: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Derives the resource private key from the private key and resource ID
|
||||
* @param privateKey - The private key to derive the resource private key from
|
||||
* @param resourceId - The resource ID to derive the resource private key from
|
||||
* @returns The resource private key
|
||||
*/
|
||||
static deriveResourcePrivateKey(privateKey: Uint8Array, resourceId: string): Uint8Array {
|
||||
// Convert the resource ID to bytes
|
||||
const resourceIdBytes = new TextEncoder().encode(resourceId);
|
||||
|
||||
// Hash the private key and resource ID
|
||||
return sha256.hash(flattenBinArray([ privateKey, resourceIdBytes ]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a payload with the private key and returns the public key and signature
|
||||
* @param privateKey - The private key to sign the payload with
|
||||
* @param payload - The payload to sign
|
||||
* @returns The public key and signature
|
||||
*/
|
||||
static signPayload(privateKey: Uint8Array, payload: string): SignedPayload {
|
||||
// Convert the payload to Binary and hash it
|
||||
const payloadHash = sha256.hash(new TextEncoder().encode(payload));
|
||||
|
||||
// Derive the public key
|
||||
const publicKey = secp256k1.derivePublicKeyCompressed(privateKey);
|
||||
|
||||
// If the public key is a string, throw an error
|
||||
if (typeof publicKey === 'string') {
|
||||
throw new Error('Failed to derive public key');
|
||||
}
|
||||
|
||||
// Sign the payload
|
||||
const signature = secp256k1.signMessageHashDER(privateKey, payloadHash);
|
||||
|
||||
// If the signature is a string, throw an error
|
||||
if (typeof signature === 'string') {
|
||||
throw new Error('Failed to sign message');
|
||||
}
|
||||
|
||||
return {
|
||||
publicKey: binToHex(publicKey),
|
||||
signature: binToHex(signature),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a write request with the private key and returns the write request
|
||||
* @param privateKey - The private key to sign the write request with
|
||||
* @param resourceId - The resource ID to write to
|
||||
* @param value - The value to write
|
||||
* @returns The write request
|
||||
*/
|
||||
static signWriteRequest(privateKey: Uint8Array, resourceId: string, value: Uint8Array): WriteRequest {
|
||||
// Derive the resource private key
|
||||
const derivedKey = this.deriveResourcePrivateKey(privateKey, resourceId);
|
||||
|
||||
// Create the payload
|
||||
const timestamp = Date.now();
|
||||
const payload = `${timestamp}:${resourceId}:${toExtendedJson(value)}`;
|
||||
|
||||
// Sign the payload
|
||||
const { publicKey, signature } = this.signPayload(derivedKey, payload);
|
||||
|
||||
return {
|
||||
id: resourceId,
|
||||
value,
|
||||
publicKey,
|
||||
timestamp,
|
||||
signature,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a request with the private key and returns the public key, timestamp, and signature
|
||||
* @param privateKey - The private key to sign the request with
|
||||
* @param path - The path of the request
|
||||
* @param body - The body of the request
|
||||
* @returns The public key, timestamp, and signature
|
||||
*/
|
||||
static signRequest(privateKey: Uint8Array, path: string, body: unknown): AuthenticatedRequestHeaders {
|
||||
// Create the payload
|
||||
const timestamp = Date.now();
|
||||
|
||||
// Convert the body to a string if it isn't already a string
|
||||
const bodyStr = typeof body === 'string' ? body : toExtendedJson(body);
|
||||
|
||||
// Create the payload as `Timestamp:Path:Body`
|
||||
const payload = `${timestamp}:${path}:${bodyStr}`;
|
||||
|
||||
// Sign the payload
|
||||
const { publicKey, signature } = this.signPayload(privateKey, payload);
|
||||
|
||||
return {
|
||||
'X-Public-Key': publicKey,
|
||||
'X-Timestamp': timestamp.toString(),
|
||||
'X-Signature': signature,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the 'blob' field of the Resource Object from the server from a read/write
|
||||
* @param response - The ResourceResponse Object from the server from a read/write
|
||||
* @returns The Resource Object with the decoded value
|
||||
*/
|
||||
static decodeResourcesResponse<T = unknown>(response: z.infer<typeof BaseSyncClient.resourcesResponseSchema>): ResourcesRecord<T> {
|
||||
return Object.entries(response.resources).reduce<ResourcesRecord<T>>((acc, [resourceId, resource]) => {
|
||||
// Get the instances
|
||||
const instances = resource.instances;
|
||||
|
||||
// Decode the instances
|
||||
const decodedInstances = instances.map((instance) => {
|
||||
return BaseSyncClient.decodeBlobValue<T>(instance);
|
||||
});
|
||||
|
||||
// Add the decoded instances to the accumulator
|
||||
acc[resourceId] = {
|
||||
...resource,
|
||||
instances: decodedInstances,
|
||||
};
|
||||
|
||||
// Return the accumulator
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
static decodeBlobValue<T = unknown>(instance: z.infer<typeof BaseSyncClient.resourceInstanceSchema>): Resource<T> {
|
||||
const decodedBlob = new TextDecoder().decode(instance.blob);
|
||||
const value = fromExtendedJson(decodedBlob) as T;
|
||||
|
||||
return {
|
||||
...instance,
|
||||
value,
|
||||
};
|
||||
}
|
||||
|
||||
static resourceInstanceSchema = z.object({
|
||||
resourceId: z.string(),
|
||||
blob: z.instanceof(Uint8Array),
|
||||
timestamp: z.number(),
|
||||
publicKey: z.string(),
|
||||
signature: z.string(),
|
||||
});
|
||||
|
||||
static rawResourcesSchema = z.object({
|
||||
instances: z.array(BaseSyncClient.resourceInstanceSchema),
|
||||
});
|
||||
|
||||
static resourcesResponseSchema = z.object({
|
||||
resources: z.record(
|
||||
// Resource ID
|
||||
z.string(),
|
||||
// Resource instances
|
||||
BaseSyncClient.rawResourcesSchema,
|
||||
),
|
||||
});
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
import { binToHex, flattenBinArray, secp256k1, sha256 } from "@bitauth/libauth";
|
||||
|
||||
import { toExtendedJson, EventEmitter } from "@xo-cash/utils";
|
||||
import type { WriteRequest } from "../types.js";
|
||||
import z from "zod";
|
||||
|
||||
export type AuthenticatedRequestHeaders = {
|
||||
'X-Public-Key': string;
|
||||
'X-Timestamp': string;
|
||||
'X-Signature': string;
|
||||
};
|
||||
|
||||
export type SignedPayload = { publicKey: string; signature: string };
|
||||
|
||||
export type XOSyncClientEventMap = {
|
||||
'connection-updated': 'connected' | 'disconnected';
|
||||
'message': {
|
||||
blob: unknown;
|
||||
timestamp: number;
|
||||
publicKey: string;
|
||||
signature: string;
|
||||
};
|
||||
'subscriptions-updated': Set<string>;
|
||||
'error': Error;
|
||||
};
|
||||
|
||||
export abstract class SyncClient extends EventEmitter<XOSyncClientEventMap> {
|
||||
|
||||
abstract connect(): Promise<void>;
|
||||
abstract disconnect(): Promise<void>;
|
||||
abstract write(resourceId: string, value: Record<string, unknown>): Promise<unknown>;
|
||||
abstract read(resourceId: string): Promise<unknown>;
|
||||
abstract subscribe(resourceId: string): Promise<void>;
|
||||
abstract unsubscribe(resourceId: string): Promise<unknown>;
|
||||
|
||||
/**
|
||||
* Derives the resource private key from the private key and resource ID
|
||||
* @param privateKey - The private key to derive the resource private key from
|
||||
* @param resourceId - The resource ID to derive the resource private key from
|
||||
* @returns The resource private key
|
||||
*/
|
||||
static deriveResourcePrivateKey(privateKey: Uint8Array, resourceId: string): Uint8Array {
|
||||
// Convert the resource ID to bytes
|
||||
const resourceIdBytes = new TextEncoder().encode(resourceId);
|
||||
|
||||
// Hash the private key and resource ID
|
||||
return sha256.hash(flattenBinArray([ privateKey, resourceIdBytes ]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a payload with the private key and returns the public key and signature
|
||||
* @param privateKey - The private key to sign the payload with
|
||||
* @param payload - The payload to sign
|
||||
* @returns The public key and signature
|
||||
*/
|
||||
static signPayload(privateKey: Uint8Array, payload: string): SignedPayload {
|
||||
// Convert the payload to Binary and hash it
|
||||
const payloadHash = sha256.hash(new TextEncoder().encode(payload));
|
||||
|
||||
// Derive the public key
|
||||
const publicKey = secp256k1.derivePublicKeyCompressed(privateKey);
|
||||
|
||||
// If the public key is a string, throw an error
|
||||
if (typeof publicKey === 'string') {
|
||||
throw new Error('Failed to derive public key');
|
||||
}
|
||||
|
||||
// Sign the payload
|
||||
const signature = secp256k1.signMessageHashDER(privateKey, payloadHash);
|
||||
|
||||
// If the signature is a string, throw an error
|
||||
if (typeof signature === 'string') {
|
||||
throw new Error('Failed to sign message');
|
||||
}
|
||||
|
||||
return {
|
||||
publicKey: binToHex(publicKey),
|
||||
signature: binToHex(signature),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a write request with the private key and returns the write request
|
||||
* @param privateKey - The private key to sign the write request with
|
||||
* @param resourceId - The resource ID to write to
|
||||
* @param value - The value to write
|
||||
* @returns The write request
|
||||
*/
|
||||
static signWriteRequest(privateKey: Uint8Array, resourceId: string, value: Uint8Array): WriteRequest {
|
||||
// Derive the resource private key
|
||||
const derivedKey = this.deriveResourcePrivateKey(privateKey, resourceId);
|
||||
|
||||
// Create the payload
|
||||
const timestamp = Date.now();
|
||||
const payload = `${timestamp}:${resourceId}:${toExtendedJson(value)}`;
|
||||
|
||||
// Sign the payload
|
||||
const { publicKey, signature } = this.signPayload(derivedKey, payload);
|
||||
|
||||
return {
|
||||
id: resourceId,
|
||||
value,
|
||||
publicKey,
|
||||
timestamp,
|
||||
signature,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a request with the private key and returns the public key, timestamp, and signature
|
||||
* @param privateKey - The private key to sign the request with
|
||||
* @param path - The path of the request
|
||||
* @param body - The body of the request
|
||||
* @returns The public key, timestamp, and signature
|
||||
*/
|
||||
static signRequest(privateKey: Uint8Array, path: string, body: unknown): AuthenticatedRequestHeaders {
|
||||
// Create the payload
|
||||
const timestamp = Date.now();
|
||||
|
||||
// Convert the body to a string if it isn't already a string
|
||||
const bodyStr = typeof body === 'string' ? body : toExtendedJson(body);
|
||||
|
||||
// Create the payload as `Timestamp:Path:Body`
|
||||
const payload = `${timestamp}:${path}:${bodyStr}`;
|
||||
|
||||
// Sign the payload
|
||||
const { publicKey, signature } = this.signPayload(privateKey, payload);
|
||||
|
||||
return {
|
||||
'X-Public-Key': publicKey,
|
||||
'X-Timestamp': timestamp.toString(),
|
||||
'X-Signature': signature,
|
||||
};
|
||||
}
|
||||
|
||||
static messageSchema = z.object({
|
||||
blob: z.unknown(),
|
||||
timestamp: z.number(),
|
||||
publicKey: z.string(),
|
||||
signature: z.string(),
|
||||
});
|
||||
}
|
||||
+113
-46
@@ -1,9 +1,9 @@
|
||||
import { SSESession, toExtendedJson, fromExtendedJson } from '@xo-cash/utils';
|
||||
import { PrivateKey } from '@xo-cash/primitives'
|
||||
|
||||
import { SyncClient } from '../shared/client.js';
|
||||
import { BaseSyncClient, type ResourcesRecord } from '../shared/base-client.js';
|
||||
|
||||
export class SSEClient extends SyncClient {
|
||||
export class SSEClient<PayloadType = unknown> extends BaseSyncClient<PayloadType> {
|
||||
private readonly url: string;
|
||||
public sseSession: SSESession | undefined;
|
||||
private readonly subscriptions: Set<string> = new Set();
|
||||
@@ -15,6 +15,11 @@ export class SSEClient extends SyncClient {
|
||||
return `${this.url}/data/subscribe`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new SSE client
|
||||
* @param url - The URL of the server
|
||||
* @param privateKey - The private key to use for authentication
|
||||
*/
|
||||
constructor(url: string, privateKey: PrivateKey) {
|
||||
super();
|
||||
|
||||
@@ -29,6 +34,9 @@ export class SSEClient extends SyncClient {
|
||||
}, 25);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the server
|
||||
*/
|
||||
async connect() {
|
||||
// The server rejects a subscribe request with no resource IDs, so there is nothing to connect to yet.
|
||||
// subscribe() connects once there is a subscription. read() and write() are plain fetches and don't need a session.
|
||||
@@ -41,10 +49,16 @@ export class SSEClient extends SyncClient {
|
||||
await this.sseSession.connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the server
|
||||
*/
|
||||
async disconnect() {
|
||||
await this.sseSession?.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconnects to the server
|
||||
*/
|
||||
async reconnect() {
|
||||
// Close the current session. Its subscription list is fixed in the request body, so it can't be reused
|
||||
this.sseSession?.close();
|
||||
@@ -54,6 +68,10 @@ export class SSEClient extends SyncClient {
|
||||
await this.connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new SSE session
|
||||
* @returns The SSE session
|
||||
*/
|
||||
private createSession() {
|
||||
// Build the request body
|
||||
const body = toExtendedJson({ resourceId: [ ...this.subscriptions ] });
|
||||
@@ -66,7 +84,7 @@ export class SSEClient extends SyncClient {
|
||||
const path = url.pathname;
|
||||
|
||||
// Sign the request body
|
||||
const authHeaders = SyncClient.signRequest(this.privateKey.toBytes(), path, request.body);
|
||||
const authHeaders = BaseSyncClient.signRequest(this.privateKey.toBytes(), path, request.body);
|
||||
|
||||
// Initialize the request headers if they don't exist
|
||||
request.headers ??= {};
|
||||
@@ -95,12 +113,15 @@ export class SSEClient extends SyncClient {
|
||||
|
||||
// Create a listener to re-emit the messages
|
||||
session.on('message', (message) => {
|
||||
const parsedMessage = SyncClient.messageSchema.safeParse(fromExtendedJson(message.data));
|
||||
const parsedMessage = BaseSyncClient.resourceInstanceSchema.safeParse(fromExtendedJson(message.data));
|
||||
if (!parsedMessage.success) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.emit('message', parsedMessage.data);
|
||||
this.emit('resource-instance-updated', {
|
||||
...parsedMessage.data,
|
||||
value: fromExtendedJson(new TextDecoder().decode(parsedMessage.data.blob)) as PayloadType,
|
||||
});
|
||||
});
|
||||
|
||||
// Create a listener to re-emit the errors
|
||||
@@ -112,69 +133,70 @@ export class SSEClient extends SyncClient {
|
||||
return session;
|
||||
}
|
||||
|
||||
// Send a POST /data/write request to the server
|
||||
async write(resourceId: string, value: Record<string, unknown>) {
|
||||
const url = `${this.url}/data/write`;
|
||||
|
||||
const valueStr = toExtendedJson(value);
|
||||
const valueBytes = this.textEncoder.encode(valueStr);
|
||||
const resource = SyncClient.signWriteRequest(this.privateKey.toBytes(), resourceId, valueBytes);
|
||||
|
||||
const body = toExtendedJson({ resources: [resource] });
|
||||
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
|
||||
// Headers arent required unless we have payment service running
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...SyncClient.signRequest(this.privateKey.toBytes(), '/data/write', body),
|
||||
},
|
||||
|
||||
body,
|
||||
/**
|
||||
* Send a POST /data/write request to the server
|
||||
* @param resources - The resources to write
|
||||
* @returns The Resource Object with the decoded value
|
||||
*/
|
||||
async writeMany(resources: Record<string, PayloadType>): Promise<ResourcesRecord<PayloadType>> {
|
||||
// Sign the resource write requests
|
||||
const signedResources = Object.entries(resources).map(([resourceId, value]) => {
|
||||
const valueStr = toExtendedJson(value);
|
||||
const valueBytes = this.textEncoder.encode(valueStr);
|
||||
return BaseSyncClient.signWriteRequest(this.privateKey.toBytes(), resourceId, valueBytes);
|
||||
});
|
||||
|
||||
// Send the resource write requests to the server
|
||||
const res = await this.sendRequest('/data/write', { resources: signedResources });
|
||||
|
||||
// If the response is not ok, throw an error
|
||||
if (!res.ok) {
|
||||
const error = await res.json();
|
||||
throw new Error(`Failed to write resource [${resourceId}] (${res.statusText}): ${JSON.stringify(error)}`);
|
||||
throw new Error(`Failed to write resources [${Object.keys(resources).join(', ')}] (${res.statusText}): ${JSON.stringify(error)}`);
|
||||
}
|
||||
|
||||
const text = fromExtendedJson(await res.text()) as { blob: ArrayBuffer, signature: string, publicKey: string, timestamp: number }[];
|
||||
// Decode the response and return the resources
|
||||
const decodedResources = await this.decodeResponse(res);
|
||||
|
||||
return text;
|
||||
// Return the decoded resources
|
||||
return decodedResources;
|
||||
}
|
||||
|
||||
// Send a POST /data/read request to the server
|
||||
async read(resourceId: string) {
|
||||
const url = `${this.url}/data/get`;
|
||||
const body = toExtendedJson({ resourceId: [ resourceId ] });
|
||||
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...SyncClient.signRequest(this.privateKey.toBytes(), '/data/get', body),
|
||||
},
|
||||
body,
|
||||
});
|
||||
/**
|
||||
* Send a POST /data/read request to the server
|
||||
* @param resourceIds - The resource IDs to read
|
||||
* @returns The resources with the read values
|
||||
*/
|
||||
async readMany(resourceIds: string[]): Promise<ResourcesRecord<PayloadType>> {
|
||||
// Send the resource read requests to the server
|
||||
const res = await this.sendRequest('/data/get', { resourceId: resourceIds });
|
||||
|
||||
// If the response is not ok, throw an error
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to read resource: ${res.statusText}`);
|
||||
throw new Error(`Failed to read resources [${resourceIds.join(', ')}] (${res.statusText})`);
|
||||
}
|
||||
|
||||
const text = fromExtendedJson(await res.text()) as { blob: ArrayBuffer, signature: string, publicKey: string, timestamp: number }[];
|
||||
// Decode the response and return the resources
|
||||
const decodedResources = await this.decodeResponse(res);
|
||||
|
||||
return text;
|
||||
// Return the decoded resources
|
||||
return decodedResources;
|
||||
}
|
||||
|
||||
// Destroy the current SSE Session, then re-create it with the additional resource ID in the subscription list
|
||||
/**
|
||||
* Destroy the current SSE Session, then re-create it with the additional resource ID in the subscription list
|
||||
* @param resourceId - The resource ID to add to the subscription list
|
||||
*/
|
||||
async subscribe(resourceId: string) {
|
||||
this.subscriptions.add(resourceId);
|
||||
|
||||
this.emit('subscriptions-updated', this.subscriptions);
|
||||
}
|
||||
|
||||
// Destroy the current SSE Session, then re-create it with the additional resource ID in the subscription list
|
||||
/**
|
||||
* Destroy the current SSE Session, then re-create it without the resource ID in the subscription list
|
||||
* @param resourceId - The resource ID to remove from the subscription list
|
||||
*/
|
||||
async unsubscribe(resourceId: string) {
|
||||
this.subscriptions.delete(resourceId);
|
||||
|
||||
@@ -184,4 +206,49 @@ export class SSEClient extends SyncClient {
|
||||
async handleOnConnectionUpdated(value: 'connected' | 'disconnected') {
|
||||
this.emit('connection-updated', value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request to the server
|
||||
* @param path - The path to send the request to
|
||||
* @param body - The body of the request
|
||||
* @returns The response from the server
|
||||
*/
|
||||
private async sendRequest(path: string, body: unknown): Promise<Response> {
|
||||
// Build the URL
|
||||
const url = `${this.url}${path}`;
|
||||
|
||||
// Format the body as Extended JSON
|
||||
const formattedBody = toExtendedJson(body);
|
||||
|
||||
// Send the request to the server
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...BaseSyncClient.signRequest(this.privateKey.toBytes(), path, body),
|
||||
},
|
||||
body: formattedBody,
|
||||
});
|
||||
|
||||
// Return the response
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the blobs on the response body to the type T
|
||||
* @param response - The response to decode
|
||||
* @returns The decoded resources
|
||||
*/
|
||||
private async decodeResponse(response: Response): Promise<ResourcesRecord<PayloadType>> {
|
||||
// Decode the response from Extended JSON
|
||||
const text = fromExtendedJson(await response.text())
|
||||
|
||||
// Parse the response as a record of resources
|
||||
const resources = BaseSyncClient.resourcesResponseSchema.parse(text);
|
||||
|
||||
// Decode the blobs and convert them from extended json to the type T
|
||||
const decodedResources = BaseSyncClient.decodeResourcesResponse<PayloadType>(resources);
|
||||
|
||||
return decodedResources;
|
||||
}
|
||||
}
|
||||
|
||||
+69
-22
@@ -2,7 +2,7 @@ import { ExponentialBackoff, fromExtendedJson, toExtendedJson } from '@xo-cash/u
|
||||
import type { PrivateKey } from '@xo-cash/primitives';
|
||||
|
||||
import { WsMessageSchema, WsSuccessResponseSchema, WsErrorResponseSchema, type WsMessage } from './types.js';
|
||||
import { SyncClient } from '../shared/client.js';
|
||||
import { BaseSyncClient, type ResourcesRecord } from '../shared/base-client.js';
|
||||
|
||||
/** A request sent over the sync server's WebSocket connection. */
|
||||
type WsRequest = {
|
||||
@@ -38,13 +38,14 @@ export type WsClientOptions = {
|
||||
*
|
||||
* Heartbeats and request timeouts are not implemented.
|
||||
*/
|
||||
export class WsClient extends SyncClient {
|
||||
export class WsClient<PayloadType = unknown> extends BaseSyncClient<PayloadType> {
|
||||
private readonly url: string;
|
||||
private readonly privateKey: PrivateKey;
|
||||
private readonly pendingRequests = new Map<string, PendingRequest>();
|
||||
private readonly messageListeners = new Set<(message: WsMessage) => void>();
|
||||
private readonly errorListeners = new Set<(error: Error) => void>();
|
||||
|
||||
private readonly textEncoder = new TextEncoder();
|
||||
|
||||
/** Resource IDs to (re)subscribe to whenever a socket opens. */
|
||||
private readonly subscriptions = new Set<string>();
|
||||
|
||||
@@ -167,19 +168,37 @@ export class WsClient extends SyncClient {
|
||||
return attempt;
|
||||
}
|
||||
|
||||
/** Read every stored instance of one resource. */
|
||||
async read(resourceId: string): Promise<unknown> {
|
||||
return this.request('/data/get', { resourceId: [resourceId] });
|
||||
async readMany(resourceIds: string[]): Promise<ResourcesRecord<PayloadType>> {
|
||||
// Read the resources from the server
|
||||
const response = await this.request('/data/get', { resourceId: resourceIds });
|
||||
|
||||
// Parse the response as a record of resources
|
||||
const resources = BaseSyncClient.resourcesResponseSchema.parse(response);
|
||||
|
||||
// Decode the resources and convert the blobs from Uint8Arrays to the type T
|
||||
const decodedResources = BaseSyncClient.decodeResourcesResponse<PayloadType>(resources);
|
||||
|
||||
return decodedResources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign and write one resource, using the same wire format as the SSE client.
|
||||
*/
|
||||
async write(resourceId: string, value: Record<string, unknown>): Promise<unknown> {
|
||||
const valueBytes = new TextEncoder().encode(toExtendedJson(value));
|
||||
const resource = SyncClient.signWriteRequest(this.privateKey.toBytes(), resourceId, valueBytes);
|
||||
async writeMany(resourcesToWrite: Record<string, PayloadType>): Promise<ResourcesRecord<PayloadType>> {
|
||||
// Sign the resources
|
||||
const signedResources = Object.entries(resourcesToWrite).map(([resourceId, value]) => {
|
||||
const valueStr = toExtendedJson(value);
|
||||
const valueBytes = this.textEncoder.encode(valueStr);
|
||||
return BaseSyncClient.signWriteRequest(this.privateKey.toBytes(), resourceId, valueBytes);
|
||||
});
|
||||
|
||||
return this.request('/data/write', { resources: [resource] });
|
||||
// Write the resources to the server
|
||||
const response = await this.request('/data/write', { resources: signedResources });
|
||||
|
||||
// Parse the response as a record of resources
|
||||
const resources = BaseSyncClient.resourcesResponseSchema.parse(response);
|
||||
|
||||
// Decode the resources and convert the blobs from Uint8Arrays to the type T
|
||||
const decodedResources = BaseSyncClient.decodeResourcesResponse<PayloadType>(resources);
|
||||
|
||||
return decodedResources;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,6 +207,8 @@ export class WsClient extends SyncClient {
|
||||
* The server never replies to a successful subscribe (the route stays pending until the topic is
|
||||
* removed), so this resolves once the frame is sent. A failed subscribe comes back as an `error`
|
||||
* message that matches no pending request, which handleMessage() reports through emitError().
|
||||
*
|
||||
* @param resourceId - The resource ID to subscribe to
|
||||
*/
|
||||
async subscribe(resourceId: string): Promise<void> {
|
||||
await this.send('/data/subscribe', { resourceId: [resourceId] });
|
||||
@@ -200,6 +221,7 @@ export class WsClient extends SyncClient {
|
||||
|
||||
/**
|
||||
* Unsubscribe from one resource topic.
|
||||
* @param resourceId - The resource ID to unsubscribe from
|
||||
*/
|
||||
async unsubscribe(resourceId: string): Promise<void> {
|
||||
this.subscriptions.delete(resourceId);
|
||||
@@ -225,7 +247,12 @@ export class WsClient extends SyncClient {
|
||||
});
|
||||
}
|
||||
|
||||
/** Connect if needed, then sign and send one request envelope using the shared Extended JSON codec. */
|
||||
/**
|
||||
* Connect if needed, then sign and send one request envelope using the shared Extended JSON codec.
|
||||
* @param path - The path of the request
|
||||
* @param body - The body of the request
|
||||
* @param id - The ID of the request
|
||||
*/
|
||||
private async send(path: string, body: unknown, id: string = crypto.randomUUID()): Promise<void> {
|
||||
const socket = await this.getWebSocket();
|
||||
|
||||
@@ -235,13 +262,17 @@ export class WsClient extends SyncClient {
|
||||
}
|
||||
|
||||
// Signed after connecting: the server rejects signatures older than its timestamp window (5 minutes by default).
|
||||
const headers = SyncClient.signRequest(this.privateKey.toBytes(), path, toExtendedJson(body));
|
||||
const headers = BaseSyncClient.signRequest(this.privateKey.toBytes(), path, toExtendedJson(body));
|
||||
const request: WsRequest = { id, path, body, headers };
|
||||
|
||||
socket.send(toExtendedJson(request));
|
||||
}
|
||||
|
||||
/** Resolve with the socket once it opens, or reject if the handshake fails. */
|
||||
/**
|
||||
* Resolve with the socket once it opens, or reject if the handshake fails.
|
||||
* @param socket - The WebSocket to wait for
|
||||
* @returns The WebSocket
|
||||
*/
|
||||
private static waitForOpen(socket: WebSocket): Promise<WebSocket> {
|
||||
return new Promise((resolve, reject) => {
|
||||
socket.onopen = () => resolve(socket);
|
||||
@@ -249,7 +280,10 @@ export class WsClient extends SyncClient {
|
||||
});
|
||||
}
|
||||
|
||||
/** Decode and route one frame received from the sync server. */
|
||||
/**
|
||||
* Decode and route one frame received from the sync server.
|
||||
* @param raw - The raw message received from the sync server
|
||||
*/
|
||||
private handleMessage(raw: string): void {
|
||||
const { success, data: message } = WsMessageSchema.safeParse(fromExtendedJson(raw));
|
||||
if (!success) {
|
||||
@@ -275,12 +309,15 @@ export class WsClient extends SyncClient {
|
||||
}
|
||||
|
||||
if (message.type === 'instance-changed') {
|
||||
const parsedMessage = SyncClient.messageSchema.safeParse(message.data);
|
||||
const parsedMessage = BaseSyncClient.resourceInstanceSchema.safeParse(message.data);
|
||||
if (!parsedMessage.success) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.emit('message', parsedMessage.data);
|
||||
this.emit('resource-instance-updated', {
|
||||
...parsedMessage.data,
|
||||
value: fromExtendedJson(new TextDecoder().decode(parsedMessage.data.blob)) as PayloadType,
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -311,14 +348,20 @@ export class WsClient extends SyncClient {
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to emit an error to all error listeners
|
||||
/**
|
||||
* Helper to emit an error to all error listeners
|
||||
* @param error - The error to emit
|
||||
*/
|
||||
private emitError(error: Error): void {
|
||||
for (const listener of this.errorListeners) {
|
||||
listener(error);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper for when the client disconnects
|
||||
/**
|
||||
* Helper for when the client disconnects
|
||||
* @param error - The error to reject the pending requests with
|
||||
*/
|
||||
private rejectPendingRequests(error: Error): void {
|
||||
for (const pending of this.pendingRequests.values()) {
|
||||
pending.reject(error);
|
||||
@@ -327,7 +370,11 @@ export class WsClient extends SyncClient {
|
||||
this.pendingRequests.clear();
|
||||
}
|
||||
|
||||
// Helper to convert an HTTP URL to a WebSocket URL
|
||||
/**
|
||||
* Helper to convert an HTTP URL to a WebSocket URL
|
||||
* @param httpUrl - The HTTP URL to convert
|
||||
* @returns The WebSocket URL
|
||||
*/
|
||||
static httpToWsUrl(httpUrl: string): string {
|
||||
const url = new URL(httpUrl);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user