mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import type { ActiveContext } from '@/auth/hosts'
|
|
import { BaseError } from '@/errors/base'
|
|
import { ErrorCode } from '@/errors/codes'
|
|
|
|
export const DEFAULT_HOST = 'https://cloud.dify.ai'
|
|
|
|
export function openAPIBase(host: string): string {
|
|
return `${host.replace(/\/+$/, '')}/openapi/v1/`
|
|
}
|
|
|
|
export type ResolveHostOptions = {
|
|
raw: string
|
|
insecure: boolean
|
|
}
|
|
|
|
export function resolveHost(opts: ResolveHostOptions): string {
|
|
let raw = opts.raw.trim()
|
|
if (raw === '') raw = DEFAULT_HOST
|
|
if (!raw.includes('://')) raw = `https://${raw}`
|
|
let url: URL
|
|
try {
|
|
url = new URL(raw)
|
|
} catch (err) {
|
|
throw new BaseError({
|
|
code: ErrorCode.UsageInvalidFlag,
|
|
message: `host parse: ${(err as Error).message}`,
|
|
})
|
|
}
|
|
url.pathname = url.pathname.replace(/\/+$/, '')
|
|
if (url.protocol !== 'https:' && !(opts.insecure && url.protocol === 'http:')) {
|
|
throw new BaseError({
|
|
code: ErrorCode.UsageInvalidFlag,
|
|
message: 'only https:// hosts are accepted',
|
|
hint: 'add --insecure to allow http:// (local-dev only; user_code/device_code travel plaintext)',
|
|
})
|
|
}
|
|
const out = url.toString()
|
|
return out.endsWith('/') ? out.slice(0, -1) : out
|
|
}
|
|
|
|
export function hostWithScheme(host: string, scheme: string | undefined): string {
|
|
if (host.includes('://')) return host
|
|
const proto = scheme === undefined || scheme === '' ? 'https' : scheme
|
|
return `${proto}://${host}`
|
|
}
|
|
|
|
// Every call site that builds an HTTP client for the active host needs both its
|
|
// scheme-qualified host and whether TLS verification is disabled for it — derive
|
|
// them together so neither is forgotten independently.
|
|
export function activeHostInfo(active: Pick<ActiveContext, 'host' | 'scheme' | 'insecureTls'>): {
|
|
host: string
|
|
insecure: boolean
|
|
} {
|
|
return {
|
|
host: hostWithScheme(active.host, active.scheme),
|
|
insecure: active.insecureTls === true,
|
|
}
|
|
}
|
|
|
|
export function bareHost(raw: string): string {
|
|
try {
|
|
const u = new URL(raw)
|
|
return u.host !== '' ? u.host : raw
|
|
} catch {
|
|
return raw
|
|
}
|
|
}
|
|
|
|
export function validateVerificationURI(raw: string, insecure: boolean): void {
|
|
let url: URL
|
|
try {
|
|
url = new URL(raw.trim())
|
|
} catch {
|
|
throw new BaseError({
|
|
code: ErrorCode.Unknown,
|
|
message: `server returned invalid verification_uri "${raw}"`,
|
|
})
|
|
}
|
|
if (url.protocol !== 'https:' && !(insecure && url.protocol === 'http:')) {
|
|
throw new BaseError({
|
|
code: ErrorCode.Unknown,
|
|
message: `server returned verification_uri with unsupported scheme "${url.protocol.replace(':', '')}"`,
|
|
hint: 'expected https:// (use --insecure to allow http:// on local-dev hosts)',
|
|
})
|
|
}
|
|
if (url.host === '')
|
|
throw new BaseError({
|
|
code: ErrorCode.Unknown,
|
|
message: `server returned verification_uri without host: "${raw}"`,
|
|
})
|
|
}
|