Fix Web Sockets. Form into library. Move demo to demo folder. Split demo between sse and websocket.

This commit is contained in:
2026-09-17 11:49:21 +00:00
parent 55d09d048e
commit 711ecba117
10 changed files with 134 additions and 96 deletions
+17 -18
View File
@@ -1,17 +1,18 @@
import { SSESession, toExtendedJson } from '@xo-cash/utils';
import { SSESession, toExtendedJson, fromExtendedJson } from '@xo-cash/utils';
import { PrivateKey } from '@xo-cash/primitives'
import { SyncClient } from '../shared/client.js';
export type SSEClientOptions = {
onMessage: (message: string) => void;
onMessage: (message: unknown) => void;
onError: (error: Error) => void;
};
export class SSEClient extends SyncClient {
private sseSession: SSESession;
private readonly url: string;
public sseSession: SSESession;
private readonly subscriptions: Set<string> = new Set();
private readonly messageListeners = new Set<(message: string) => void>();
private readonly messageListeners = new Set<(message: unknown) => void>();
private readonly errorListeners = new Set<(error: Error) => void>();
private privateKey: PrivateKey;
@@ -21,9 +22,10 @@ export class SSEClient extends SyncClient {
return `${this.url}/data/subscribe`;
}
constructor(private readonly url: string, privateKey: PrivateKey, options: Partial<SSEClientOptions> = {}) {
constructor(url: string, privateKey: PrivateKey, options: Partial<SSEClientOptions> = {}) {
super();
this.url = url;
this.privateKey = privateKey;
this.sseSession = new SSESession(`${this.url}/data/subscribe`, {
method: 'POST',
@@ -53,15 +55,12 @@ export class SSEClient extends SyncClient {
// Re-create the session with the additional resource ID in the subscription list
this.sseSession = new SSESession(this.subscriptionUrl, {
onRequest: async (request) => {
// Create the request authentication headers
const requestBody = toExtendedJson(request.body);
// Get the path from the URL
const url = new URL(this.url);
const url = new URL(this.subscriptionUrl);
const path = url.pathname;
// Sign the request body
const authHeaders = SyncClient.signRequest(this.privateKey.toBytes(), path, requestBody);
const authHeaders = SyncClient.signRequest(this.privateKey.toBytes(), path, request.body);
// Initialize the request headers if they don't exist
request.headers ??= {};
@@ -78,14 +77,14 @@ export class SSEClient extends SyncClient {
headers: {
'Content-Type': 'application/json',
},
body: body,
body,
method: 'POST',
});
// Create a listener to re-emit the messages
this.sseSession.on('message', (message) => {
for (const listener of this.messageListeners) {
listener(message.data);
listener(fromExtendedJson(message.data));
}
});
@@ -108,7 +107,7 @@ export class SSEClient extends SyncClient {
const valueBytes = this.textEncoder.encode(valueStr);
const resource = SyncClient.signWriteRequest(this.privateKey.toBytes(), resourceId, valueBytes);
const bodyStr = toExtendedJson({ resources: [resource] });
const body = toExtendedJson({ resources: [resource] });
const res = await fetch(url, {
method: 'POST',
@@ -116,10 +115,10 @@ export class SSEClient extends SyncClient {
// Headers arent required unless we have payment service running
headers: {
'Content-Type': 'application/json',
// ...SSEClient.authenticateRequest(this.privateKey, bodyStr),
...SyncClient.signRequest(this.privateKey.toBytes(), '/data/write', body),
},
body: bodyStr,
body,
});
if (!res.ok) {
@@ -127,7 +126,7 @@ export class SSEClient extends SyncClient {
throw new Error(`Failed to write resource [${resourceId}] (${res.statusText}): ${JSON.stringify(error)}`);
}
return res.json();
return fromExtendedJson(await res.text());
}
// Send a POST /data/read request to the server
@@ -139,7 +138,7 @@ export class SSEClient extends SyncClient {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// ...SSEClient.authenticateRequest(this.privateKey, body),
...SyncClient.signRequest(this.privateKey.toBytes(), '/data/get', body),
},
body,
});
@@ -148,7 +147,7 @@ export class SSEClient extends SyncClient {
throw new Error(`Failed to read resource: ${res.statusText}`);
}
return res.json();
return fromExtendedJson(await res.text());
}
// Destroy the current SSE Session, then re-create it with the additional resource ID in the subscription list
+1 -1
View File
@@ -1 +1 @@
export * from './client.js';
export * from './client.js';