mirror of
https://github.com/xtr-dev/rondevu-server.git
synced 2025-12-14 12:53:23 +00:00
v0.5.0: Service discovery and FQN format refactoring
- Changed service FQN format: service:version@username (colon instead of @) - Added service discovery: direct lookup, random selection, paginated queries - Updated parseServiceFqn to handle optional username for discovery - Removed UUID privacy layer (service_index table) - Updated storage interface with discovery methods (discoverServices, getRandomService, getServiceByFqn) - Removed deprecated methods (getServiceByUuid, queryService, listServicesForUsername, findServicesByName, touchUsername, batchCreateServices) - Updated API routes: /services/:fqn with three modes (direct, random, paginated) - Changed offer/answer/ICE routes to offer-specific: /services/:fqn/offers/:offerId/* - Added extracted fields to services table (service_name, version, username) for efficient discovery - Created migration 0007 to update schema and migrate existing data - Added discovery indexes for performance 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,9 +8,9 @@ import {
|
||||
ClaimUsernameRequest,
|
||||
Service,
|
||||
CreateServiceRequest,
|
||||
ServiceInfo,
|
||||
} from './types.ts';
|
||||
import { generateOfferHash } from './hash-id.ts';
|
||||
import { parseServiceFqn } from '../crypto.ts';
|
||||
|
||||
const YEAR_IN_MS = 365 * 24 * 60 * 60 * 1000; // 365 days
|
||||
|
||||
@@ -84,36 +84,23 @@ export class D1Storage implements Storage {
|
||||
CREATE INDEX IF NOT EXISTS idx_usernames_expires ON usernames(expires_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_usernames_public_key ON usernames(public_key);
|
||||
|
||||
-- Services table
|
||||
-- Services table (new schema with extracted fields for discovery)
|
||||
CREATE TABLE IF NOT EXISTS services (
|
||||
id TEXT PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
service_fqn TEXT NOT NULL,
|
||||
service_name TEXT NOT NULL,
|
||||
version TEXT NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
expires_at INTEGER NOT NULL,
|
||||
is_public INTEGER NOT NULL DEFAULT 0,
|
||||
metadata TEXT,
|
||||
FOREIGN KEY (username) REFERENCES usernames(username) ON DELETE CASCADE,
|
||||
UNIQUE(username, service_fqn)
|
||||
UNIQUE(service_fqn)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_services_username ON services(username);
|
||||
CREATE INDEX IF NOT EXISTS idx_services_fqn ON services(service_fqn);
|
||||
CREATE INDEX IF NOT EXISTS idx_services_discovery ON services(service_name, version);
|
||||
CREATE INDEX IF NOT EXISTS idx_services_username ON services(username);
|
||||
CREATE INDEX IF NOT EXISTS idx_services_expires ON services(expires_at);
|
||||
|
||||
-- Service index table (privacy layer)
|
||||
CREATE TABLE IF NOT EXISTS service_index (
|
||||
uuid TEXT PRIMARY KEY,
|
||||
service_id TEXT NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
service_fqn TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
expires_at INTEGER NOT NULL,
|
||||
FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_service_index_username ON service_index(username);
|
||||
CREATE INDEX IF NOT EXISTS idx_service_index_expires ON service_index(expires_at);
|
||||
`);
|
||||
}
|
||||
|
||||
@@ -382,18 +369,6 @@ export class D1Storage implements Storage {
|
||||
};
|
||||
}
|
||||
|
||||
async touchUsername(username: string): Promise<boolean> {
|
||||
const now = Date.now();
|
||||
const expiresAt = now + YEAR_IN_MS;
|
||||
|
||||
const result = await this.db.prepare(`
|
||||
UPDATE usernames
|
||||
SET last_used = ?, expires_at = ?
|
||||
WHERE username = ? AND expires_at > ?
|
||||
`).bind(now, expiresAt, username, now).run();
|
||||
|
||||
return (result.meta.changes || 0) > 0;
|
||||
}
|
||||
|
||||
async deleteExpiredUsernames(now: number): Promise<number> {
|
||||
const result = await this.db.prepare(`
|
||||
@@ -407,36 +382,32 @@ export class D1Storage implements Storage {
|
||||
|
||||
async createService(request: CreateServiceRequest): Promise<{
|
||||
service: Service;
|
||||
indexUuid: string;
|
||||
offers: Offer[];
|
||||
}> {
|
||||
const serviceId = crypto.randomUUID();
|
||||
const indexUuid = crypto.randomUUID();
|
||||
const now = Date.now();
|
||||
|
||||
// Insert service
|
||||
// Parse FQN to extract components
|
||||
const parsed = parseServiceFqn(request.serviceFqn);
|
||||
if (!parsed) {
|
||||
throw new Error(`Invalid service FQN: ${request.serviceFqn}`);
|
||||
}
|
||||
if (!parsed.username) {
|
||||
throw new Error(`Service FQN must include username: ${request.serviceFqn}`);
|
||||
}
|
||||
|
||||
const { serviceName, version, username } = parsed;
|
||||
|
||||
// Insert service with extracted fields
|
||||
await this.db.prepare(`
|
||||
INSERT INTO services (id, username, service_fqn, created_at, expires_at, is_public, metadata)
|
||||
INSERT INTO services (id, service_fqn, service_name, version, username, created_at, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`).bind(
|
||||
serviceId,
|
||||
request.username,
|
||||
request.serviceFqn,
|
||||
now,
|
||||
request.expiresAt,
|
||||
request.isPublic ? 1 : 0,
|
||||
request.metadata || null
|
||||
).run();
|
||||
|
||||
// Insert service index
|
||||
await this.db.prepare(`
|
||||
INSERT INTO service_index (uuid, service_id, username, service_fqn, created_at, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`).bind(
|
||||
indexUuid,
|
||||
serviceId,
|
||||
request.username,
|
||||
request.serviceFqn,
|
||||
serviceName,
|
||||
version,
|
||||
username,
|
||||
now,
|
||||
request.expiresAt
|
||||
).run();
|
||||
@@ -448,36 +419,28 @@ export class D1Storage implements Storage {
|
||||
}));
|
||||
const offers = await this.createOffers(offerRequests);
|
||||
|
||||
// Touch username to extend expiry
|
||||
await this.touchUsername(request.username);
|
||||
// Touch username to extend expiry (inline logic)
|
||||
const expiresAt = now + YEAR_IN_MS;
|
||||
await this.db.prepare(`
|
||||
UPDATE usernames
|
||||
SET last_used = ?, expires_at = ?
|
||||
WHERE username = ? AND expires_at > ?
|
||||
`).bind(now, expiresAt, username, now).run();
|
||||
|
||||
return {
|
||||
service: {
|
||||
id: serviceId,
|
||||
username: request.username,
|
||||
serviceFqn: request.serviceFqn,
|
||||
serviceName,
|
||||
version,
|
||||
username,
|
||||
createdAt: now,
|
||||
expiresAt: request.expiresAt,
|
||||
isPublic: request.isPublic || false,
|
||||
metadata: request.metadata,
|
||||
},
|
||||
indexUuid,
|
||||
offers,
|
||||
};
|
||||
}
|
||||
|
||||
async batchCreateServices(requests: CreateServiceRequest[]): Promise<Array<{
|
||||
service: Service;
|
||||
indexUuid: string;
|
||||
offers: Offer[];
|
||||
}>> {
|
||||
const results = [];
|
||||
for (const request of requests) {
|
||||
const result = await this.createService(request);
|
||||
results.push(result);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
async getOffersForService(serviceId: string): Promise<Offer[]> {
|
||||
const result = await this.db.prepare(`
|
||||
@@ -506,12 +469,11 @@ export class D1Storage implements Storage {
|
||||
return this.rowToService(result as any);
|
||||
}
|
||||
|
||||
async getServiceByUuid(uuid: string): Promise<Service | null> {
|
||||
async getServiceByFqn(serviceFqn: string): Promise<Service | null> {
|
||||
const result = await this.db.prepare(`
|
||||
SELECT s.* FROM services s
|
||||
INNER JOIN service_index si ON s.id = si.service_id
|
||||
WHERE si.uuid = ? AND s.expires_at > ?
|
||||
`).bind(uuid, Date.now()).first();
|
||||
SELECT * FROM services
|
||||
WHERE service_fqn = ? AND expires_at > ?
|
||||
`).bind(serviceFqn, Date.now()).first();
|
||||
|
||||
if (!result) {
|
||||
return null;
|
||||
@@ -520,43 +482,29 @@ export class D1Storage implements Storage {
|
||||
return this.rowToService(result as any);
|
||||
}
|
||||
|
||||
async listServicesForUsername(username: string): Promise<ServiceInfo[]> {
|
||||
|
||||
|
||||
|
||||
|
||||
async discoverServices(
|
||||
serviceName: string,
|
||||
version: string,
|
||||
limit: number,
|
||||
offset: number
|
||||
): Promise<Service[]> {
|
||||
// Query for unique services with available offers
|
||||
// We join with offers and filter for available ones (answerer_peer_id IS NULL)
|
||||
const result = await this.db.prepare(`
|
||||
SELECT si.uuid, s.is_public, s.service_fqn, s.metadata
|
||||
FROM service_index si
|
||||
INNER JOIN services s ON si.service_id = s.id
|
||||
WHERE si.username = ? AND si.expires_at > ?
|
||||
SELECT DISTINCT s.* FROM services s
|
||||
INNER JOIN offers o ON o.service_id = s.id
|
||||
WHERE s.service_name = ?
|
||||
AND s.version = ?
|
||||
AND s.expires_at > ?
|
||||
AND o.answerer_peer_id IS NULL
|
||||
AND o.expires_at > ?
|
||||
ORDER BY s.created_at DESC
|
||||
`).bind(username, Date.now()).all();
|
||||
|
||||
if (!result.results) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return result.results.map((row: any) => ({
|
||||
uuid: row.uuid,
|
||||
isPublic: row.is_public === 1,
|
||||
serviceFqn: row.is_public === 1 ? row.service_fqn : undefined,
|
||||
metadata: row.is_public === 1 ? row.metadata || undefined : undefined,
|
||||
}));
|
||||
}
|
||||
|
||||
async queryService(username: string, serviceFqn: string): Promise<string | null> {
|
||||
const result = await this.db.prepare(`
|
||||
SELECT si.uuid FROM service_index si
|
||||
INNER JOIN services s ON si.service_id = s.id
|
||||
WHERE si.username = ? AND si.service_fqn = ? AND si.expires_at > ?
|
||||
`).bind(username, serviceFqn, Date.now()).first();
|
||||
|
||||
return result ? (result as any).uuid : null;
|
||||
}
|
||||
|
||||
async findServicesByName(username: string, serviceName: string): Promise<Service[]> {
|
||||
const result = await this.db.prepare(`
|
||||
SELECT * FROM services
|
||||
WHERE username = ? AND service_fqn LIKE ? AND expires_at > ?
|
||||
ORDER BY created_at DESC
|
||||
`).bind(username, `${serviceName}@%`, Date.now()).all();
|
||||
LIMIT ? OFFSET ?
|
||||
`).bind(serviceName, version, Date.now(), Date.now(), limit, offset).all();
|
||||
|
||||
if (!result.results) {
|
||||
return [];
|
||||
@@ -565,6 +513,27 @@ export class D1Storage implements Storage {
|
||||
return result.results.map(row => this.rowToService(row as any));
|
||||
}
|
||||
|
||||
async getRandomService(serviceName: string, version: string): Promise<Service | null> {
|
||||
// Get a random service with an available offer
|
||||
const result = await this.db.prepare(`
|
||||
SELECT s.* FROM services s
|
||||
INNER JOIN offers o ON o.service_id = s.id
|
||||
WHERE s.service_name = ?
|
||||
AND s.version = ?
|
||||
AND s.expires_at > ?
|
||||
AND o.answerer_peer_id IS NULL
|
||||
AND o.expires_at > ?
|
||||
ORDER BY RANDOM()
|
||||
LIMIT 1
|
||||
`).bind(serviceName, version, Date.now(), Date.now()).first();
|
||||
|
||||
if (!result) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.rowToService(result as any);
|
||||
}
|
||||
|
||||
async deleteService(serviceId: string, username: string): Promise<boolean> {
|
||||
const result = await this.db.prepare(`
|
||||
DELETE FROM services
|
||||
@@ -613,12 +582,12 @@ export class D1Storage implements Storage {
|
||||
private rowToService(row: any): Service {
|
||||
return {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
serviceFqn: row.service_fqn,
|
||||
serviceName: row.service_name,
|
||||
version: row.version,
|
||||
username: row.username,
|
||||
createdAt: row.created_at,
|
||||
expiresAt: row.expires_at,
|
||||
isPublic: row.is_public === 1,
|
||||
metadata: row.metadata || undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,58 +64,27 @@ export interface ClaimUsernameRequest {
|
||||
|
||||
/**
|
||||
* Represents a published service (can have multiple offers)
|
||||
* New format: service:version@username (e.g., chat:1.0.0@alice)
|
||||
*/
|
||||
export interface Service {
|
||||
id: string; // UUID v4
|
||||
username: string;
|
||||
serviceFqn: string; // com.example.chat@1.0.0
|
||||
serviceFqn: string; // Full FQN: chat:1.0.0@alice
|
||||
serviceName: string; // Extracted: chat
|
||||
version: string; // Extracted: 1.0.0
|
||||
username: string; // Extracted: alice
|
||||
createdAt: number;
|
||||
expiresAt: number;
|
||||
isPublic: boolean;
|
||||
metadata?: string; // JSON service description
|
||||
}
|
||||
|
||||
/**
|
||||
* Request to create a single service
|
||||
*/
|
||||
export interface CreateServiceRequest {
|
||||
username: string;
|
||||
serviceFqn: string;
|
||||
serviceFqn: string; // Full FQN with username: chat:1.0.0@alice
|
||||
expiresAt: number;
|
||||
isPublic?: boolean;
|
||||
metadata?: string;
|
||||
offers: CreateOfferRequest[]; // Multiple offers per service
|
||||
}
|
||||
|
||||
/**
|
||||
* Request to create multiple services in batch
|
||||
*/
|
||||
export interface BatchCreateServicesRequest {
|
||||
services: CreateServiceRequest[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a service index entry (privacy layer)
|
||||
*/
|
||||
export interface ServiceIndex {
|
||||
uuid: string; // Random UUID for privacy
|
||||
serviceId: string;
|
||||
username: string;
|
||||
serviceFqn: string;
|
||||
createdAt: number;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Service info for discovery (privacy-aware)
|
||||
*/
|
||||
export interface ServiceInfo {
|
||||
uuid: string;
|
||||
isPublic: boolean;
|
||||
serviceFqn?: string; // Only present if public
|
||||
metadata?: string; // Only present if public
|
||||
}
|
||||
|
||||
/**
|
||||
* Storage interface for rondevu DNS-like system
|
||||
* Implementations can use different backends (SQLite, D1, etc.)
|
||||
@@ -225,13 +194,6 @@ export interface Storage {
|
||||
*/
|
||||
getUsername(username: string): Promise<Username | null>;
|
||||
|
||||
/**
|
||||
* Updates the last_used timestamp for a username (extends expiry)
|
||||
* @param username Username to update
|
||||
* @returns true if updated, false if not found
|
||||
*/
|
||||
touchUsername(username: string): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Deletes all expired usernames
|
||||
* @param now Current timestamp
|
||||
@@ -244,24 +206,13 @@ export interface Storage {
|
||||
/**
|
||||
* Creates a new service with offers
|
||||
* @param request Service creation request (includes offers)
|
||||
* @returns Created service with generated ID, index UUID, and created offers
|
||||
* @returns Created service with generated ID and created offers
|
||||
*/
|
||||
createService(request: CreateServiceRequest): Promise<{
|
||||
service: Service;
|
||||
indexUuid: string;
|
||||
offers: Offer[];
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Creates multiple services with offers in batch
|
||||
* @param requests Array of service creation requests
|
||||
* @returns Array of created services with IDs, UUIDs, and offers
|
||||
*/
|
||||
batchCreateServices(requests: CreateServiceRequest[]): Promise<Array<{
|
||||
service: Service;
|
||||
indexUuid: string;
|
||||
offers: Offer[];
|
||||
}>>;
|
||||
|
||||
/**
|
||||
* Gets all offers for a service
|
||||
@@ -278,34 +229,40 @@ export interface Storage {
|
||||
getServiceById(serviceId: string): Promise<Service | null>;
|
||||
|
||||
/**
|
||||
* Gets a service by its index UUID
|
||||
* @param uuid Index UUID
|
||||
* Gets a service by its fully qualified name (FQN)
|
||||
* @param serviceFqn Full service FQN (e.g., "chat:1.0.0@alice")
|
||||
* @returns Service if found, null otherwise
|
||||
*/
|
||||
getServiceByUuid(uuid: string): Promise<Service | null>;
|
||||
getServiceByFqn(serviceFqn: string): Promise<Service | null>;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Lists all services for a username (with privacy filtering)
|
||||
* @param username Username to query
|
||||
* @returns Array of service info (UUIDs only for private services)
|
||||
* Discovers services by name and version with pagination
|
||||
* Returns unique available offers (where answerer_peer_id IS NULL)
|
||||
* @param serviceName Service name (e.g., 'chat')
|
||||
* @param version Version string for semver matching (e.g., '1.0.0')
|
||||
* @param limit Maximum number of unique services to return
|
||||
* @param offset Number of services to skip
|
||||
* @returns Array of services with available offers
|
||||
*/
|
||||
listServicesForUsername(username: string): Promise<ServiceInfo[]>;
|
||||
discoverServices(
|
||||
serviceName: string,
|
||||
version: string,
|
||||
limit: number,
|
||||
offset: number
|
||||
): Promise<Service[]>;
|
||||
|
||||
/**
|
||||
* Queries a service by username and FQN
|
||||
* @param username Username
|
||||
* @param serviceFqn Service FQN
|
||||
* @returns Service index UUID if found, null otherwise
|
||||
* Gets a random available service by name and version
|
||||
* Returns a single random offer that is available (answerer_peer_id IS NULL)
|
||||
* @param serviceName Service name (e.g., 'chat')
|
||||
* @param version Version string for semver matching (e.g., '1.0.0')
|
||||
* @returns Random service with available offer, or null if none found
|
||||
*/
|
||||
queryService(username: string, serviceFqn: string): Promise<string | null>;
|
||||
|
||||
/**
|
||||
* Finds all services by username and service name (without version)
|
||||
* @param username Username
|
||||
* @param serviceName Service name (e.g., 'com.example.chat')
|
||||
* @returns Array of services with matching service name
|
||||
*/
|
||||
findServicesByName(username: string, serviceName: string): Promise<Service[]>;
|
||||
getRandomService(serviceName: string, version: string): Promise<Service | null>;
|
||||
|
||||
/**
|
||||
* Deletes a service (with ownership verification)
|
||||
|
||||
Reference in New Issue
Block a user