UNPKG

openraas

Version:

Open Robot-as-a-Service Protocol - A comprehensive TypeScript library for building and consuming RaaS applications with X402 payment support on Solana

44 lines (43 loc) 1.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisRegistry = void 0; const ioredis_1 = __importDefault(require("ioredis")); class RedisRegistry { constructor(redisUrl) { this.redis = new ioredis_1.default(redisUrl); } async register(robot) { await this.redis.set(`robot:${robot.id}`, JSON.stringify(robot)); // Add to a set for discovery await this.redis.sadd('robots', robot.id); } async unregister(robotId) { await this.redis.del(`robot:${robotId}`); await this.redis.srem('robots', robotId); } async find(criteria) { const ids = await this.redis.smembers('robots'); if (ids.length === 0) return []; const robotsJson = await this.redis.mget(ids.map(id => `robot:${id}`)); const robots = robotsJson .filter((json) => json !== null) .map(json => JSON.parse(json)); return robots.filter((robot) => { for (const key in criteria) { if (robot[key] !== criteria[key]) { return false; } } return true; }); } async get(robotId) { const json = await this.redis.get(`robot:${robotId}`); return json ? JSON.parse(json) : null; } } exports.RedisRegistry = RedisRegistry;