68 lines
5.5 KiB
JavaScript
68 lines
5.5 KiB
JavaScript
import {randomUUID} from 'node:crypto';
|
|||
|
|
import {MusicError} from '../src/core/controller.mjs';
|
||
|
|
import {MAPlaybackProvider} from './ma-provider.mjs';
|
||
|
|
// Server-only bridge. No upstream IDs, raw snapshots, arbitrary commands or URLs cross it.
|
||
|
|
export class DevelopmentService {
|
||
|
|
#control; #campaign; #session; #client; #key; #allowed; #provider; #binding; #latest=0; #tail=Promise.resolve(); #handles=new Map(); #cache=new Map(); #pending=0;
|
||
|
|
constructor({control,campaign,session,client,snapshotKey,allowedIds=[],playbackEnabled=false,audio}){
|
||
|
|
this.#control=control;this.#campaign=campaign;this.#session=session;this.#client=client;this.#key=snapshotKey;
|
||
|
|
this.#allowed=new Set(allowedIds);this.playbackEnabled=playbackEnabled;this.audio=audio;
|
||
|
|
}
|
||
|
|
authorize(token,origin,body,role='controller'){
|
||
|
|
this.#control.authorize(token,origin,body.campaign,body.session,role);
|
||
|
|
if(body.campaign!==this.#campaign||body.session!==this.#session)throw new MusicError('denied');
|
||
|
|
}
|
||
|
|
async rpc(token,origin,body){
|
||
|
|
this.authorize(token,origin,body);
|
||
|
|
const base=['action','campaign','session'];
|
||
|
|
const shape={connect:[],diagnostics:[],catalog:[],renew:['fence'],release:['fence'],prepare:['generation','operationId','fence','id'],snapshot:['generation','operationId','fence'],apply:['generation','operationId','fence','handle'],restore:['generation','operationId','fence','handle'],stop:['generation','operationId','fence']};
|
||
|
|
if(!Object.hasOwn(shape,body.action)||Object.keys(body).some(k=>![...base,...shape[body.action]].includes(k)))throw new MusicError('denied');
|
||
|
|
const request={token,origin,campaign:this.#campaign,session:this.#session,fence:body.fence};
|
||
|
|
if(body.action==='connect'){
|
||
|
|
const lease=this.#control.acquire(request);request.fence=lease.fence;
|
||
|
|
this.#binding=request;this.#latest=0;this.#handles.clear();this.#cache.clear();
|
||
|
|
this.#provider=new MAPlaybackProvider({client:this.#client,campaign:this.#campaign,session:this.#session,fence:lease.fence,authority:this.#control.fenceGuard(request),snapshotKey:this.#key});return lease;
|
||
|
|
}
|
||
|
|
if(body.action==='diagnostics'){
|
||
|
|
let target='unavailable';try{await this.#client.verifyTarget();target='verified';}catch{}
|
||
|
|
this.authorize(token,origin,body);
|
||
|
|
return {target,catalog:true,playbackEnabled:this.playbackEnabled && target==='verified',allowedIds:[...this.#allowed],
|
||
|
|
expiresAt:this.#control.authorize(token,origin,body.campaign,body.session,'controller').expires,audioSource:this.audio?'synthetic-pcm':'none',sendspin:false,crossfade:false,ambientLayers:1};
|
||
|
|
}
|
||
|
|
if(body.action==='catalog'){const items=await this.#client.catalog();this.authorize(token,origin,body);return [...items,...[...this.#allowed].filter(id=>id.startsWith('synthetic-')).map(id=>({provider:'ma',id,kind:'sequence',name:id==='synthetic-ambient'?'Generated ambient test sequence':'Generated combat test sequence'}))];}
|
||
|
|
if(body.action==='renew')return this.#control.renew(request);
|
||
|
|
if(body.action==='release'){this.#control.release(request);return {ok:true};}
|
||
|
|
if(!this.playbackEnabled || !this.#provider || !Number.isSafeInteger(body.generation)||body.generation<1 || typeof body.operationId!=='string'||body.operationId.length>80 || !body.operationId ||
|
||
|
|
this.#binding.token!==token || this.#binding.fence!==body.fence)throw new MusicError('denied');
|
||
|
|
this.#control.fenceGuard(request)(body.fence);
|
||
|
|
if(body.generation<this.#latest)throw new MusicError('stale');
|
||
|
|
this.#latest=body.generation;
|
||
|
|
const signature=JSON.stringify(body),cached=this.#cache.get(body.operationId);
|
||
|
|
if(cached){if(cached.signature!==signature)throw new MusicError('denied');return cached.result;}
|
||
|
|
const ctx={campaign:this.#campaign,session:this.#session,generation:body.generation,check:()=>{
|
||
|
|
this.authorize(token,origin,body);this.#control.fenceGuard(request)(body.fence);if(body.generation!==this.#latest)throw new MusicError('stale');
|
||
|
|
}};
|
||
|
|
const put=(type,value)=>{
|
||
|
|
ctx.check();
|
||
|
|
for(const [id,entry]of this.#handles)if(entry.expires<=Date.now())this.#handles.delete(id);
|
||
|
|
if(this.#handles.size>=64)throw new MusicError('unavailable');
|
||
|
|
const handle=randomUUID(),expires=Date.now()+300000;this.#handles.set(handle,{type,value,expires,generation:body.generation});return {handle,expires};
|
||
|
|
};
|
||
|
|
const get=type=>{const entry=this.#handles.get(body.handle);if(!entry||entry.type!==type||entry.expires<=Date.now()||type==='prepared'&&entry.generation!==body.generation)throw new MusicError('expired-media');return entry.value;};
|
||
|
|
for(const [id,entry]of this.#cache)if(entry.generation<body.generation)this.#cache.delete(id);
|
||
|
|
if(this.#cache.size>=128||this.#pending>=16)throw new MusicError('unavailable');
|
||
|
|
this.#pending++;
|
||
|
|
const operation=this.#tail.catch(()=>{}).then(async()=>{
|
||
|
|
ctx.check();
|
||
|
|
switch(body.action){
|
||
|
|
case 'prepare':if(!this.#allowed.has(body.id))throw new MusicError('denied');return put('prepared',await this.#provider.resolve({provider:'ma',id:body.id},ctx));
|
||
|
|
case 'snapshot':return put('snapshot',await this.#provider.snapshot(ctx));
|
||
|
|
case 'apply':await this.#provider.apply(get('prepared'),ctx);return {ok:true};
|
||
|
|
case 'restore':await this.#provider.restore(get('snapshot'),ctx);return {ok:true};
|
||
|
|
case 'stop':await this.#provider.stopOwned(ctx);return {ok:true};
|
||
|
|
}
|
||
|
|
}).finally(()=>{this.#pending--;});
|
||
|
|
this.#tail=operation;
|
||
|
|
this.#cache.set(body.operationId,{signature,result:operation,generation:body.generation});return operation;
|
||
|
|
}
|
||
|
|
}
|