openraas
Version:
Open Robot-as-a-Service Protocol - A comprehensive TypeScript library for building and consuming RaaS applications with X402 payment support on Solana
47 lines (46 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RaaSServer = void 0;
const websocket_1 = require("../transport/websocket");
const middleware_1 = require("./middleware");
class RaaSServer {
constructor(robot, port = 8080) {
this.paymentMiddleware = null;
this.robot = robot;
this.transport = new websocket_1.WebSocketAdapter();
// In a real implementation, we'd start the WS server here or pass the server instance
// For this adapter, we assume it can also act as a server or we wrap a ws.Server
}
enableX402(receiverAddress, mint, price) {
this.paymentMiddleware = new middleware_1.PaymentMiddleware(receiverAddress, mint, price);
}
async start() {
// Mocking server start for the adapter
// In reality, WebSocketAdapter might need a 'listen' method or we pass a http server
console.log(`RaaS Server for robot ${this.robot.name} started.`);
this.transport.on('message', async (data) => {
await this.handleMessage(data);
});
// this.transport.listen(port); // Hypothetical
}
async handleMessage(data) {
// Create context
const ctx = {
req: data,
res: {},
send: (msg) => this.transport.send(msg),
next: async () => {
// Handle actual command
console.log('Command executed:', data);
this.transport.send({ status: 'success', data: 'Command executed' });
}
};
if (this.paymentMiddleware) {
await this.paymentMiddleware.handle(ctx);
}
else {
await ctx.next();
}
}
}
exports.RaaSServer = RaaSServer;