Skip to content

Commit 5ed322d

Browse files
authored
feat(frontend): don't assume secure protocol for kyros websocket/fs (#6420)
* feat(frontend): don't assume https as websocket/fs protocol * fix(frontend): actually do the same for websocket * fix(frontend): don't strip ws path
1 parent 8591bc8 commit 5ed322d

4 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎packages/api-client/src/features/node-auth.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AbstractFeature, type FeatureConfig } from '../core/abstract-feature'
22
import { ModrinthApiError } from '../core/errors'
33
import type { RequestContext } from '../types/request'
4+
import { getNodeBaseUrl } from '../utils/node-url'
45

56
/**
67
* Node authentication credentials
@@ -107,7 +108,7 @@ export class NodeAuthFeature extends AbstractFeature {
107108
}
108109

109110
private applyAuth(context: RequestContext, auth: NodeAuth): void {
110-
const baseUrl = `https://${auth.url.replace(/\/modrinth\/v\d+\/fs\/?$/, '')}`
111+
const baseUrl = getNodeBaseUrl(auth.url)
111112
context.url = this.buildUrl(context.path, baseUrl, context.options.version)
112113

113114
context.options.headers = {

‎packages/api-client/src/modules/kyros/files/v0.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AbstractModule } from '../../../core/abstract-module'
22
import type { UploadHandle, UploadProgress } from '../../../types/upload'
3+
import { getNodeBaseUrl } from '../../../utils/node-url'
34
import type { Archon } from '../../archon/types'
45
import type { Kyros } from '../types'
56

@@ -11,7 +12,7 @@ export class KyrosFilesV0Module extends AbstractModule {
1112
}
1213

1314
private getNodeBaseUrl(auth: NodeFsAuth): string {
14-
return `https://${auth.url.replace(/\/modrinth\/v\d+\/fs\/?$/, '')}`
15+
return getNodeBaseUrl(auth.url)
1516
}
1617

1718
/**

‎packages/api-client/src/platform/websocket-generic.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import mitt from 'mitt'
22

33
import { AbstractWebSocketClient, type WebSocketConnection } from '../core/abstract-websocket'
44
import type { Archon } from '../modules/archon/types'
5+
import { getNodeWebSocketUrl } from '../utils/node-url'
56

67
type WSEventMap = {
78
[K in Archon.Websocket.v0.WSEvent as `${string}:${K['event']}`]: K
@@ -19,7 +20,7 @@ export class GenericWebSocketClient extends AbstractWebSocketClient {
1920

2021
return new Promise((resolve, reject) => {
2122
try {
22-
const ws = new WebSocket(`wss://${auth.url}`)
23+
const ws = new WebSocket(getNodeWebSocketUrl(auth.url))
2324

2425
const connection: WebSocketConnection = {
2526
serverId,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const NODE_FS_PATH_REGEX = /\/modrinth\/v\d+\/fs\/?$/
2+
const HTTP_SCHEME_REGEX = /^https?:\/\//i
3+
const WS_SCHEME_REGEX = /^wss?:\/\//i
4+
5+
export function getNodeBaseUrl(url: string): string {
6+
const baseUrl = url.replace(NODE_FS_PATH_REGEX, '')
7+
return HTTP_SCHEME_REGEX.test(baseUrl) ? baseUrl : `https://${baseUrl}`
8+
}
9+
10+
export function getNodeWebSocketUrl(url: string): string {
11+
return WS_SCHEME_REGEX.test(url) ? url : `wss://${url}`
12+
}

0 commit comments

Comments
 (0)