mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 04:01:58 +00:00
Feature/simple syncdb (#2464)
* simplify db sync on rpc endpoints * switch to patch-db master * update fe for websocket only stream * fix api --------- Co-authored-by: Matt Hill <mattnine@protonmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Observable, Subject } from 'rxjs'
|
||||
import { Observable } from 'rxjs'
|
||||
import { Update } from 'patch-db-client'
|
||||
import { RR } from './api.types'
|
||||
import { DataModel } from 'src/app/services/patch-db/data-model'
|
||||
@@ -6,8 +6,6 @@ import { Log } from '@start9labs/shared'
|
||||
import { WebSocketSubjectConfig } from 'rxjs/webSocket'
|
||||
|
||||
export abstract class ApiService {
|
||||
readonly patchStream$ = new Subject<Update<DataModel>[]>()
|
||||
|
||||
// http
|
||||
|
||||
// for getting static files: ex icons, instructions, licenses
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Inject, Injectable } from '@angular/core'
|
||||
import {
|
||||
decodeBase64,
|
||||
HttpOptions,
|
||||
HttpService,
|
||||
isRpcError,
|
||||
@@ -14,7 +13,7 @@ import { RR } from './api.types'
|
||||
import { parsePropertiesPermissive } from 'src/app/util/properties.util'
|
||||
import { ConfigService } from '../config.service'
|
||||
import { webSocket, WebSocketSubjectConfig } from 'rxjs/webSocket'
|
||||
import { Observable } from 'rxjs'
|
||||
import { Observable, filter, firstValueFrom } from 'rxjs'
|
||||
import { AuthService } from '../auth.service'
|
||||
import { DOCUMENT } from '@angular/common'
|
||||
import { DataModel } from '../patch-db/data-model'
|
||||
@@ -67,7 +66,7 @@ export class LiveApiService extends ApiService {
|
||||
// auth
|
||||
|
||||
async login(params: RR.LoginReq): Promise<RR.loginRes> {
|
||||
return this.rpcRequest({ method: 'auth.login', params }, false)
|
||||
return this.rpcRequest({ method: 'auth.login', params })
|
||||
}
|
||||
|
||||
async logout(params: RR.LogoutReq): Promise<RR.LogoutRes> {
|
||||
@@ -91,7 +90,7 @@ export class LiveApiService extends ApiService {
|
||||
// server
|
||||
|
||||
async echo(params: RR.EchoReq, urlOverride?: string): Promise<RR.EchoRes> {
|
||||
return this.rpcRequest({ method: 'echo', params }, false, urlOverride)
|
||||
return this.rpcRequest({ method: 'echo', params }, urlOverride)
|
||||
}
|
||||
|
||||
openPatchWebsocket$(): Observable<Update<DataModel>> {
|
||||
@@ -434,42 +433,28 @@ export class LiveApiService extends ApiService {
|
||||
|
||||
private async rpcRequest<T>(
|
||||
options: RPCOptions,
|
||||
addHeader = true,
|
||||
urlOverride?: string,
|
||||
): Promise<T> {
|
||||
if (addHeader) {
|
||||
options.headers = {
|
||||
'x-patch-sequence': String(this.patch.cache$.value.sequence),
|
||||
...(options.headers || {}),
|
||||
}
|
||||
}
|
||||
|
||||
const res = await this.http.rpcRequest<T>(options, urlOverride)
|
||||
const encodedUpdates = res.headers.get('x-patch-updates')
|
||||
const encodedError = res.headers.get('x-patch-error')
|
||||
const body = res.body
|
||||
|
||||
if (encodedUpdates) {
|
||||
const decoded = decodeBase64(encodedUpdates)
|
||||
const updates: Update<DataModel>[] = JSON.parse(decoded)
|
||||
this.patchStream$.next(updates)
|
||||
}
|
||||
|
||||
if (encodedError) {
|
||||
const error = decodeBase64(encodedError)
|
||||
console.error(error)
|
||||
}
|
||||
|
||||
const rpcRes = res.body
|
||||
|
||||
if (isRpcError(rpcRes)) {
|
||||
if (rpcRes.error.code === 34) {
|
||||
if (isRpcError(body)) {
|
||||
if (body.error.code === 34) {
|
||||
console.error('Unauthenticated, logging out')
|
||||
this.auth.setUnverified()
|
||||
}
|
||||
throw new RpcError(rpcRes.error)
|
||||
throw new RpcError(body.error)
|
||||
}
|
||||
|
||||
return rpcRes.result
|
||||
const patchSequence = res.headers.get('x-patch-sequence')
|
||||
if (patchSequence)
|
||||
await firstValueFrom(
|
||||
this.patch.cache$.pipe(
|
||||
filter(({ sequence }) => sequence >= Number(patchSequence)),
|
||||
),
|
||||
)
|
||||
|
||||
return body.result
|
||||
}
|
||||
|
||||
private async httpRequest<T>(opts: HttpOptions): Promise<T> {
|
||||
|
||||
@@ -65,7 +65,6 @@ export class MockApiService extends ApiService {
|
||||
.pipe(
|
||||
tap(() => {
|
||||
this.sequence = 0
|
||||
this.patchStream$.next([])
|
||||
}),
|
||||
switchMap(verified =>
|
||||
iif(
|
||||
@@ -108,7 +107,9 @@ export class MockApiService extends ApiService {
|
||||
value: params.value,
|
||||
},
|
||||
]
|
||||
return this.withRevision(patch)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
// auth
|
||||
@@ -289,7 +290,9 @@ export class MockApiService extends ApiService {
|
||||
value: initialProgress,
|
||||
},
|
||||
]
|
||||
return this.withRevision(patch, 'updating')
|
||||
this.mockRevision(patch)
|
||||
|
||||
return 'updating'
|
||||
}
|
||||
|
||||
async restartServer(
|
||||
@@ -332,7 +335,9 @@ export class MockApiService extends ApiService {
|
||||
value: params.enable,
|
||||
},
|
||||
]
|
||||
return this.withRevision(patch, null)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
// marketplace URLs
|
||||
@@ -385,7 +390,9 @@ export class MockApiService extends ApiService {
|
||||
value: 0,
|
||||
},
|
||||
]
|
||||
return this.withRevision(patch, Mock.Notifications)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return Mock.Notifications
|
||||
}
|
||||
|
||||
async deleteNotification(
|
||||
@@ -564,7 +571,9 @@ export class MockApiService extends ApiService {
|
||||
},
|
||||
]
|
||||
|
||||
return this.withRevision(originalPatch)
|
||||
this.mockRevision(originalPatch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
// package
|
||||
@@ -629,7 +638,9 @@ export class MockApiService extends ApiService {
|
||||
},
|
||||
},
|
||||
]
|
||||
return this.withRevision(patch)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async getPackageConfig(
|
||||
@@ -660,7 +671,9 @@ export class MockApiService extends ApiService {
|
||||
value: true,
|
||||
},
|
||||
]
|
||||
return this.withRevision(patch)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async restorePackages(
|
||||
@@ -684,7 +697,9 @@ export class MockApiService extends ApiService {
|
||||
}
|
||||
})
|
||||
|
||||
return this.withRevision(patch)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async executePackageAction(
|
||||
@@ -768,7 +783,9 @@ export class MockApiService extends ApiService {
|
||||
},
|
||||
]
|
||||
|
||||
return this.withRevision(originalPatch)
|
||||
this.mockRevision(originalPatch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async restartPackage(
|
||||
@@ -845,7 +862,9 @@ export class MockApiService extends ApiService {
|
||||
},
|
||||
]
|
||||
|
||||
return this.withRevision(patch)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async stopPackage(params: RR.StopPackageReq): Promise<RR.StopPackageRes> {
|
||||
@@ -876,7 +895,9 @@ export class MockApiService extends ApiService {
|
||||
},
|
||||
]
|
||||
|
||||
return this.withRevision(patch)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async uninstallPackage(
|
||||
@@ -902,7 +923,9 @@ export class MockApiService extends ApiService {
|
||||
},
|
||||
]
|
||||
|
||||
return this.withRevision(patch)
|
||||
this.mockRevision(patch)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
async dryConfigureDependency(
|
||||
@@ -1057,23 +1080,4 @@ export class MockApiService extends ApiService {
|
||||
}
|
||||
this.mockWsSource$.next(revision)
|
||||
}
|
||||
|
||||
private async withRevision<T>(
|
||||
patch: Operation<unknown>[],
|
||||
response: T | null = null,
|
||||
): Promise<T> {
|
||||
if (!this.sequence) {
|
||||
const { sequence } = this.bootstrapper.init()
|
||||
this.sequence = sequence
|
||||
}
|
||||
|
||||
this.patchStream$.next([
|
||||
{
|
||||
id: ++this.sequence,
|
||||
patch,
|
||||
},
|
||||
])
|
||||
|
||||
return response as T
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from 'rxjs/operators'
|
||||
import { Update } from 'patch-db-client'
|
||||
import { DataModel } from './data-model'
|
||||
import { defer, EMPTY, from, interval, merge, Observable } from 'rxjs'
|
||||
import { defer, EMPTY, from, interval, Observable } from 'rxjs'
|
||||
import { AuthService } from '../auth.service'
|
||||
import { ConnectionService } from '../connection.service'
|
||||
import { ApiService } from '../api/embassy-api.service'
|
||||
@@ -51,9 +51,7 @@ export function sourceFactory(
|
||||
)
|
||||
|
||||
return authService.isVerified$.pipe(
|
||||
switchMap(verified =>
|
||||
verified ? merge(websocket$, api.patchStream$) : EMPTY,
|
||||
),
|
||||
switchMap(verified => (verified ? websocket$ : EMPTY)),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user