mirror of
https://github.com/modrinth/code.git
synced 2026-08-26 01:26:23 +00:00
feat: hosting access tab (#5995)
* feat: implement access tab with dummy data * fix: spacing * feat: qa * feat: implement backend * qa: qa pass * feat: fix user "search" * fix: lint * feat: change to bitfield * feat: fix fields * fix: lint * fix: lint * feat: hook up api * feat: fix permissions * feat: audit log table event start * feat: better mobile mode for audit log table * feat: i18n * feat: qa * feat: enforce permissions * feat: email template start * feat: qa * fix: tooltip bug * feat: qa * impl: sse support in api-client * feat: sse impl * fix: desync path * feat: time frame picker from analytics * feat: QA * fix: spacing * fix: permisison audit log entries * fix: hosting manage page shared server detection * fix: lint * feat: qa + lint * feat: audit log table sort by time * feat: finish frontend panel stuff * fix: lint * fix: backend alignment * fix: lint * fix: supress friend errors * feat: qa * fix: qa * fix: lint * fix: utils barrel * fix: safari cookies in dev * fix: pin nuxt * feat: fixes + notif fix * fix: notifications * feat: qa * fix: notification sync not happening immediately * fix: qa * fix: qa * feat: qa * blog + prepr * feat: toast shit * blog images * thumbnail update one last time * prepr * feat: use reinvite route * update images * fix: reinvite stuff * fix: lint * fix: alignment of save bar * fix: notif sizing * fix: split up access * fix: lint * fix: lint * fix: link --------- Co-authored-by: Prospector <6166773+Prospector@users.noreply.github.com>
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import type { InferredClientModules } from '../modules'
|
||||
import { buildModuleStructure } from '../modules'
|
||||
import type { ClientConfig } from '../types/client'
|
||||
import type { BaseUrlConfig, ClientConfig } from '../types/client'
|
||||
import type { RequestContext, RequestOptions } from '../types/request'
|
||||
import type { UploadMetadata, UploadProgress, UploadRequestOptions } from '../types/upload'
|
||||
import type { AbstractFeature } from './abstract-feature'
|
||||
import type { AbstractModule } from './abstract-module'
|
||||
import type { AbstractSyncClient } from './abstract-sync'
|
||||
import { AbstractUploadClient } from './abstract-upload-client'
|
||||
import type { AbstractWebSocketClient } from './abstract-websocket'
|
||||
import { ModrinthApiError, ModrinthServerError } from './errors'
|
||||
@@ -32,7 +33,10 @@ export abstract class AbstractModrinthClient extends AbstractUploadClient {
|
||||
private _moduleNamespaces: Map<string, Record<string, AbstractModule>> = new Map()
|
||||
|
||||
public readonly labrinth!: InferredClientModules['labrinth']
|
||||
public readonly archon!: ArchonClientModules & { sockets: AbstractWebSocketClient }
|
||||
public readonly archon!: ArchonClientModules & {
|
||||
sockets: AbstractWebSocketClient
|
||||
sync: AbstractSyncClient
|
||||
}
|
||||
public readonly kyros!: InferredClientModules['kyros']
|
||||
public readonly iso3166!: InferredClientModules['iso3166']
|
||||
public readonly mclogs!: InferredClientModules['mclogs']
|
||||
@@ -116,9 +120,9 @@ export abstract class AbstractModrinthClient extends AbstractUploadClient {
|
||||
async request<T>(path: string, options: RequestOptions): Promise<T> {
|
||||
let baseUrl: string
|
||||
if (options.api === 'labrinth') {
|
||||
baseUrl = this.config.labrinthBaseUrl!
|
||||
baseUrl = this.resolveBaseUrl(this.config.labrinthBaseUrl!)
|
||||
} else if (options.api === 'archon') {
|
||||
baseUrl = this.config.archonBaseUrl!
|
||||
baseUrl = this.resolveBaseUrl(this.config.archonBaseUrl!)
|
||||
} else {
|
||||
baseUrl = options.api
|
||||
}
|
||||
@@ -160,13 +164,55 @@ export abstract class AbstractModrinthClient extends AbstractUploadClient {
|
||||
}
|
||||
}
|
||||
|
||||
async stream(path: string, options: RequestOptions): Promise<ReadableStream<Uint8Array>> {
|
||||
let baseUrl: string
|
||||
if (options.api === 'labrinth') {
|
||||
baseUrl = this.resolveBaseUrl(this.config.labrinthBaseUrl!)
|
||||
} else if (options.api === 'archon') {
|
||||
baseUrl = this.resolveBaseUrl(this.config.archonBaseUrl!)
|
||||
} else {
|
||||
baseUrl = options.api
|
||||
}
|
||||
|
||||
const url = this.buildUrl(path, baseUrl, options.version)
|
||||
const defaultHeaders = await this.buildDefaultHeaders()
|
||||
const mergedOptions: RequestOptions = {
|
||||
method: 'GET',
|
||||
retry: false,
|
||||
circuitBreaker: false,
|
||||
...options,
|
||||
headers: {
|
||||
...defaultHeaders,
|
||||
Accept: 'text/event-stream',
|
||||
...options.headers,
|
||||
},
|
||||
}
|
||||
this.attachArchonSentryCaptureHeader(mergedOptions)
|
||||
|
||||
const context = this.buildContext(url, path, mergedOptions)
|
||||
|
||||
try {
|
||||
return await this.executeFeatureChain<ReadableStream<Uint8Array>>(context, () =>
|
||||
this.executeStreamRequest(context.url, context.options),
|
||||
)
|
||||
} catch (error) {
|
||||
const apiError = this.normalizeError(error, context)
|
||||
await this.config.hooks?.onError?.(apiError, context)
|
||||
|
||||
throw apiError
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the feature chain and the actual request
|
||||
*
|
||||
* Features are executed in order, with each feature calling next() to continue.
|
||||
* The last "feature" in the chain is the actual request execution.
|
||||
*/
|
||||
protected async executeFeatureChain<T>(context: RequestContext): Promise<T> {
|
||||
protected async executeFeatureChain<T>(
|
||||
context: RequestContext,
|
||||
executeTerminal: () => Promise<T> = () => this.executeRequest<T>(context.url, context.options),
|
||||
): Promise<T> {
|
||||
// Filter to only features that should apply
|
||||
const applicableFeatures = this.features.filter((feature) => feature.shouldApply(context))
|
||||
|
||||
@@ -184,7 +230,7 @@ export abstract class AbstractModrinthClient extends AbstractUploadClient {
|
||||
} else {
|
||||
// We've reached the end of the chain, execute the actual request
|
||||
await this.config.hooks?.onRequest?.(context)
|
||||
return this.executeRequest<T>(context.url, context.options)
|
||||
return executeTerminal()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,6 +289,10 @@ export abstract class AbstractModrinthClient extends AbstractUploadClient {
|
||||
return `${base}${versionPath}${cleanPath}`
|
||||
}
|
||||
|
||||
protected resolveBaseUrl(baseUrl: BaseUrlConfig): string {
|
||||
return typeof baseUrl === 'function' ? baseUrl() : baseUrl
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the request context
|
||||
*/
|
||||
@@ -354,6 +404,11 @@ export abstract class AbstractModrinthClient extends AbstractUploadClient {
|
||||
*/
|
||||
protected abstract executeRequest<T>(url: string, options: RequestOptions): Promise<T>
|
||||
|
||||
protected abstract executeStreamRequest(
|
||||
url: string,
|
||||
options: RequestOptions,
|
||||
): Promise<ReadableStream<Uint8Array>>
|
||||
|
||||
/**
|
||||
* Execute the actual XHR upload
|
||||
*
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
import type mitt from 'mitt'
|
||||
|
||||
import type { Archon } from '../modules/archon/types'
|
||||
import type { RequestOptions } from '../types/request'
|
||||
|
||||
export type SyncEventType = Archon.Sync.v1.SyncEvent['type']
|
||||
|
||||
export type SyncEventOfType<E extends SyncEventType> = Extract<
|
||||
Archon.Sync.v1.SyncEvent,
|
||||
{ type: E }
|
||||
>
|
||||
|
||||
export type SyncEventHandler<E extends Archon.Sync.v1.SyncEvent = Archon.Sync.v1.SyncEvent> = (
|
||||
event: E,
|
||||
) => void
|
||||
|
||||
export type SyncStatusState =
|
||||
| 'idle'
|
||||
| 'connecting'
|
||||
| 'connected'
|
||||
| 'reconnecting'
|
||||
| 'disconnected'
|
||||
| 'error'
|
||||
|
||||
export type SyncStatus = {
|
||||
state: SyncStatusState
|
||||
connected: boolean
|
||||
reconnecting: boolean
|
||||
reconnectAttempts: number
|
||||
retryDelay: number
|
||||
lastEventId?: string
|
||||
error?: unknown
|
||||
}
|
||||
|
||||
export type SyncStatusHandler = (status: SyncStatus) => void
|
||||
|
||||
export type SyncConnectOptions = {
|
||||
intent?: Archon.Sync.v1.SyncIntent
|
||||
force?: boolean
|
||||
}
|
||||
|
||||
export type SyncConnection = {
|
||||
serverId: string
|
||||
intent: Archon.Sync.v1.SyncIntent
|
||||
controller?: AbortController
|
||||
reconnectAttempts: number
|
||||
reconnectTimer?: ReturnType<typeof setTimeout>
|
||||
reconnectResolve?: () => void
|
||||
retryDelay: number
|
||||
lastEventId?: string
|
||||
stopped: boolean
|
||||
status: SyncStatusState
|
||||
error?: unknown
|
||||
}
|
||||
|
||||
export type SyncEmitterEvents = Record<string, unknown>
|
||||
|
||||
export abstract class AbstractSyncClient {
|
||||
protected connections = new Map<string, SyncConnection>()
|
||||
protected abstract emitter: ReturnType<typeof mitt<SyncEmitterEvents>>
|
||||
|
||||
constructor(
|
||||
protected client: {
|
||||
stream: (path: string, options: RequestOptions) => Promise<ReadableStream<Uint8Array>>
|
||||
},
|
||||
) {}
|
||||
|
||||
abstract safeConnectServer(serverId: string, options?: SyncConnectOptions): Promise<void>
|
||||
|
||||
abstract disconnect(serverId: string): void
|
||||
|
||||
abstract disconnectAll(): void
|
||||
|
||||
on<E extends SyncEventType>(
|
||||
serverId: string,
|
||||
eventType: E,
|
||||
handler: SyncEventHandler<SyncEventOfType<E>>,
|
||||
): () => void {
|
||||
const eventKey = this.getEventKey(serverId, eventType)
|
||||
const wrapped = handler as (event: unknown) => void
|
||||
|
||||
this.emitter.on(eventKey, wrapped)
|
||||
|
||||
return () => {
|
||||
this.emitter.off(eventKey, wrapped)
|
||||
}
|
||||
}
|
||||
|
||||
onAny(serverId: string, handler: SyncEventHandler): () => void {
|
||||
const eventKey = this.getAnyEventKey(serverId)
|
||||
const wrapped = handler as (event: unknown) => void
|
||||
|
||||
this.emitter.on(eventKey, wrapped)
|
||||
|
||||
return () => {
|
||||
this.emitter.off(eventKey, wrapped)
|
||||
}
|
||||
}
|
||||
|
||||
onStatus(serverId: string, handler: SyncStatusHandler): () => void {
|
||||
const eventKey = this.getStatusEventKey(serverId)
|
||||
const wrapped = handler as (event: unknown) => void
|
||||
|
||||
this.emitter.on(eventKey, wrapped)
|
||||
|
||||
return () => {
|
||||
this.emitter.off(eventKey, wrapped)
|
||||
}
|
||||
}
|
||||
|
||||
getStatus(serverId: string): SyncStatus | null {
|
||||
const connection = this.connections.get(serverId)
|
||||
if (!connection) return null
|
||||
|
||||
return this.connectionToStatus(connection)
|
||||
}
|
||||
|
||||
protected emitSyncEvent(serverId: string, event: Archon.Sync.v1.SyncEvent): void {
|
||||
this.emitter.emit(this.getEventKey(serverId, event.type), event)
|
||||
this.emitter.emit(this.getAnyEventKey(serverId), event)
|
||||
}
|
||||
|
||||
protected updateStatus(
|
||||
connection: SyncConnection,
|
||||
status: SyncStatusState,
|
||||
error?: unknown,
|
||||
): void {
|
||||
connection.status = status
|
||||
connection.error = error
|
||||
this.emitter.emit(
|
||||
this.getStatusEventKey(connection.serverId),
|
||||
this.connectionToStatus(connection),
|
||||
)
|
||||
}
|
||||
|
||||
protected clearListeners(serverId: string): void {
|
||||
this.emitter.all.forEach((_handlers, type) => {
|
||||
if (type.toString().startsWith(`${serverId}:`)) {
|
||||
this.emitter.all.delete(type)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
protected connectionToStatus(connection: SyncConnection): SyncStatus {
|
||||
return {
|
||||
state: connection.status,
|
||||
connected: connection.status === 'connected',
|
||||
reconnecting: connection.status === 'reconnecting',
|
||||
reconnectAttempts: connection.reconnectAttempts,
|
||||
retryDelay: connection.retryDelay,
|
||||
lastEventId: connection.lastEventId,
|
||||
error: connection.error,
|
||||
}
|
||||
}
|
||||
|
||||
private getEventKey(serverId: string, eventType: string): string {
|
||||
return `${serverId}:${eventType}`
|
||||
}
|
||||
|
||||
private getAnyEventKey(serverId: string): string {
|
||||
return `${serverId}:*`
|
||||
}
|
||||
|
||||
private getStatusEventKey(serverId: string): string {
|
||||
return `${serverId}:__status`
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user