Release v0.7.2 - Redesigned interface and persistent account settings
Validate and generate manual release / package (push) Canceled after 0s
Validate and generate manual release / package (push) Canceled after 0s
This commit is contained in:
1 parent
1d894f59fb
commit
398749210a
35 files changed
+2807
-256
No files matched your search
+11
-7
@@ -2,7 +2,7 @@ import {MusicError} from '../core/controller.mjs';
|
||||
import {MAReadClient} from '../providers/ma/client.mjs';
|
||||
import {MAPlaybackProvider} from '../providers/ma/provider.mjs';
|
||||
import {GMMAWebSocket} from '../providers/ma/websocket.mjs';
|
||||
import {randomKey} from '../providers/ma/webcrypto.mjs';
|
||||
import {hmac,randomKey} from '../providers/ma/webcrypto.mjs';
|
||||
export function gmAccessPolicy(baseUrl,pageUrl){
|
||||
let target,page;try{target=new URL(baseUrl);page=new URL(pageUrl);}catch{throw new MusicError('address-invalid');}
|
||||
if(target.username||target.password||target.search||target.hash||target.pathname!=='/'||!['http:','https:'].includes(target.protocol))throw new MusicError('denied');
|
||||
@@ -19,7 +19,7 @@ export class GMAudioHost{
|
||||
get session(){return this.#session;}
|
||||
get stream(){return this.#destination?.stream;}
|
||||
get connected(){return this.#live;}
|
||||
supports(ref){return ref?.provider==='ma'&&this.#live&&(this.#allowed.has(ref.id)||this.#catalogPlayback&&this.#catalog.some(c=>c.id===ref.id&&c.kind==='playlist')); }
|
||||
supports(ref){return ref?.provider==='ma'&&this.#live&&(this.#allowed.has(ref.id)||this.#catalogPlayback&&this.#catalog.some(c=>(c.id===ref.id||c.legacyId&&c.legacyId===ref.id)&&c.kind==='playlist')); }
|
||||
async prepare(profile){
|
||||
if(!this.#authority()||this.#live)throw new MusicError('denied');
|
||||
const base=gmAccessPolicy(profile.maUrl,this.pageUrl);this.#phase='API authentication';
|
||||
@@ -64,22 +64,24 @@ export class GMAudioHost{
|
||||
if(!this.#authority()||this.#live)throw new MusicError('denied');if(profile.webSocketAuthLogSafe!==true)throw new MusicError('auth-log-review-required');const base=gmAccessPolicy(profile.maUrl,this.pageUrl);
|
||||
const key=Uint8Array.from(atob(profile.catalogKey),c=>c.charCodeAt(0));if(key.length!==32||!profile.targetName)throw new MusicError('denied');
|
||||
if(!preserveTransport){this.#apiTransport?.close();this.#apiTransport=new GMMAWebSocket({baseUrl:base.origin,token:profile.token,createSocket:this.createSocket});}
|
||||
this.#client=new MAReadClient({baseUrl:base.origin,token:profile.token,target:profile.target,expectedName:profile.targetName,expectedProvider:profile.expectedProvider,approvedProviders:profile.approvedProviders,catalogKey:key,campaign:profile.namespace,syntheticSequences:profile.syntheticSequences??{},fetchImpl:this.#apiTransport.fetch});
|
||||
// The same authorized token, world and MA origin can resolve new assignments on another GM device.
|
||||
const portable=profile.portableCatalog&&await hmac(new TextEncoder().encode(profile.token),'adaptive-music portable catalog\0'+JSON.stringify([base.origin,profile.namespace]));
|
||||
this.#client=new MAReadClient({baseUrl:base.origin,token:profile.token,target:profile.target,expectedName:profile.targetName,expectedProvider:profile.expectedProvider,approvedProviders:profile.approvedProviders,catalogKey:portable||key,legacyCatalogKey:portable?key:undefined,campaign:profile.namespace,syntheticSequences:profile.syntheticSequences??{},fetchImpl:this.#apiTransport.fetch});
|
||||
try{await this.#client.verifyTarget({allowUnavailable:reconnecting});this.#catalog=await this.#client.catalog();this.#status='catalog-ready';return this.catalog;}catch(error){await this.close();throw error;}
|
||||
}
|
||||
start(profile){if(this.#starting)return Promise.reject(new MusicError('denied'));this.#starting=true;return this.#start(profile).finally(()=>{this.#starting=false;});}
|
||||
start(profile,{audioContext}={}){if(this.#starting)return Promise.reject(new MusicError('denied'));this.#starting=true;return this.#start(profile,audioContext).finally(()=>{this.#starting=false;});}
|
||||
#connectionLost(epoch,stage){
|
||||
if(epoch!==this.#epoch||!this.#live)return;
|
||||
void this.close();
|
||||
this.#status='connection-lost';this.#phase=stage;
|
||||
this.onDisconnect();
|
||||
}
|
||||
async #start(profile){
|
||||
async #start(profile,audioContext){
|
||||
if(!this.#authority()||this.#live)throw new MusicError('denied');
|
||||
const base=gmAccessPolicy(profile.maUrl,this.pageUrl);
|
||||
if(!profile.targetName||typeof profile.target!=='string'||!profile.identityStorage?.['sendspin-identity-sk']||!Array.isArray(profile.approvedProviders)||!profile.catalogKey||!profile.namespace)throw new MusicError('profile-required');
|
||||
const epoch=this.#epoch,check=()=>{if(epoch!==this.#epoch||!this.#authority())throw new MusicError('stale');};
|
||||
this.#lastError=undefined;this.#attachedQueue=false;this.#context=this.createContext();const unlocked=this.#context.resume();this.#destination=this.#context.createMediaStreamDestination();this.#frames=this.#samples=this.#energy=this.#lastAudio=this.#scheduled=this.#late=this.#ahead=0;
|
||||
this.#lastError=undefined;this.#attachedQueue=false;this.#context=audioContext??this.createContext();const unlocked=audioContext?null:this.#context.resume();this.#destination=this.#context.createMediaStreamDestination();this.#frames=this.#samples=this.#energy=this.#lastAudio=this.#scheduled=this.#late=this.#ahead=0;
|
||||
try{
|
||||
this.#sdk=await this.loadSDK();await unlocked;check();
|
||||
const storage=memoryStorage(profile.identityStorage),identity=this.#sdk.loadSendspinClientIdentity(storage);
|
||||
@@ -106,6 +108,7 @@ export class GMAudioHost{
|
||||
while(!this.#core.timeSyncInfo.synced&&Date.now()<deadline){check();await new Promise(resolve=>setTimeout(resolve,50));}
|
||||
if(!this.#core.timeSyncInfo.synced)throw new MusicError('sendspin-sync-failed');check();
|
||||
if(profile.autoProvision){this.#phase='dedicated receiver verification';Object.assign(profile,await this.inspect(profile,{transport:this.#apiTransport}));check();}
|
||||
this.#phase='catalog retrieval';
|
||||
await this.readCatalog(profile,{reconnecting:true,preserveTransport:true});check();
|
||||
this.#phase='dedicated queue verification';
|
||||
await this.#client.verifyTarget({checkRegistration:check});
|
||||
@@ -119,7 +122,7 @@ export class GMAudioHost{
|
||||
this.#session=crypto.randomUUID();this.#catalogPlayback=profile.allowCatalogPlayback===true;this.#live=true;this.#phase='ready';
|
||||
this.#provider=new MAPlaybackProvider({client:this.#client,campaign:profile.campaign,session:this.#session,fence:1,snapshotKey:randomKey(),authority:()=>{if(!this.#live||!this.#authority())throw new MusicError('stale');}});
|
||||
return {clientIdVerified:true,catalog:this.#catalog.length,codecs:['pcm'],remoteAudio:'webrtc',audioMeasured:false,distributedFencing:false,attachedExistingQueue:this.#attachedQueue};
|
||||
}catch(error){this.#lastError={code:error instanceof MusicError?error.code:'unavailable',stage:this.#phase,command:error.command,upstreamCode:error.upstreamCode,translationKey:error.translationKey};if(epoch===this.#epoch)await this.close();throw new MusicError(this.#lastError.code);}
|
||||
}catch(error){this.#lastError={code:error instanceof MusicError?error.code:'unavailable',stage:this.#phase,command:error.command,upstreamCode:error.upstreamCode,translationKey:error.translationKey};if(epoch===this.#epoch)await this.close();throw Object.assign(new MusicError(this.#lastError.code),this.#lastError);}
|
||||
}
|
||||
#authenticatedSocket(base,token,clientId){
|
||||
const url=new URL('/sendspin',base);url.protocol=base.protocol==='https:'?'wss:':'ws:';
|
||||
@@ -161,6 +164,7 @@ export class GMAudioHost{
|
||||
restore(snapshot,ctx){return this.#observe(()=>this.#ready().restore(snapshot,ctx));}
|
||||
stopOwned(ctx){return this.#ready().stopOwned(ctx);}
|
||||
control(action,value,ctx){return this.#observe(()=>this.#ready().control(action,value,ctx));}
|
||||
playbackStatus(){return this.#ready().playbackStatus();}
|
||||
setVolume(value){this.#ready();if(!Number.isFinite(value)||value<0||value>100)throw new MusicError('denied');this.#core.setVolume(Math.round(value));}
|
||||
async close(){this.#epoch++;this.#status='disconnected';this.#catalogPlayback=false;this.#live=false;this.#releaseLock?.();this.#releaseLock=null;this.#clear();this.#core?.disconnect('user_request');this.#socket?.close();this.#core=null;this.#socket=null;this.#provider=null;this.#apiTransport?.close();this.#apiTransport=null;this.#client=null;this.#catalog=[];this.#allowed.clear();for(const track of this.#destination?.stream.getTracks()??[])track.stop();this.#destination=null;this.#meter=null;const context=this.#context;this.#context=null;if(context&&context.state!=='closed')await context.close();}
|
||||
}
|
||||
Reference in new issue
Block a user