Skip to content

Commit

Permalink
Revert "Use URL type internally instead of string"
Browse files Browse the repository at this point in the history
This reverts commit e8d6e9b.
  • Loading branch information
hiroshihorie committed Aug 21, 2024
1 parent 2f9ad28 commit 0c6b968
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Room+Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public enum StartReconnectReason {
// Room+ConnectSequences
extension Room {
// full connect sequence, doesn't update connection state
func fullConnectSequence(_ url: URL, _ token: String) async throws {
func fullConnectSequence(_ url: String, _ token: String) async throws {
let connectResponse = try await signalClient.connect(url,
token,
connectOptions: _state.connectOptions,
Expand Down
11 changes: 3 additions & 8 deletions Sources/LiveKit/Core/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class Room: NSObject, ObservableObject, Loggable {

// expose engine's vars
@objc
public var url: String? { _state.url?.absoluteString }
public var url: String? { _state.url }

@objc
public var token: String? { _state.token }
Expand Down Expand Up @@ -134,7 +134,7 @@ public class Room: NSObject, ObservableObject, Loggable {
var serverInfo: Livekit_ServerInfo?

// Engine
var url: URL?
var url: String?
var token: String?
// preferred reconnect mode which will be used only for next attempt
var nextReconnectMode: ReconnectMode?
Expand Down Expand Up @@ -283,12 +283,7 @@ public class Room: NSObject, ObservableObject, Loggable {
connectOptions: ConnectOptions? = nil,
roomOptions: RoomOptions? = nil) async throws
{
guard let url = URL(string: url), url.isValidForSocket else {
log("URL parse failed", .error)
throw LiveKitError(.failedToParseUrl)
}

log("Connecting to room...", .info)
log("connecting to room...", .info)

var state = _state.copy()

Expand Down
28 changes: 17 additions & 11 deletions Sources/LiveKit/Core/SignalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ actor SignalClient: Loggable {
}

@discardableResult
func connect(_ url: URL,
func connect(_ urlString: String,
_ token: String,
connectOptions: ConnectOptions? = nil,
reconnectMode: ReconnectMode? = nil,
Expand All @@ -122,11 +122,14 @@ actor SignalClient: Loggable {
log("[Connect] mode: \(String(describing: reconnectMode))")
}

let url = try Utils.buildUrl(url,
token,
connectOptions: connectOptions,
reconnectMode: reconnectMode,
adaptiveStream: adaptiveStream)
guard let url = Utils.buildUrl(urlString,
token,
connectOptions: connectOptions,
reconnectMode: reconnectMode,
adaptiveStream: adaptiveStream)
else {
throw LiveKitError(.failedToParseUrl)
}

if reconnectMode != nil {
log("[Connect] with url: \(url)")
Expand Down Expand Up @@ -176,11 +179,14 @@ actor SignalClient: Loggable {
await cleanUp(withError: error)

// Validate...
let validateUrl = try Utils.buildUrl(url,
token,
connectOptions: connectOptions,
adaptiveStream: adaptiveStream,
validate: true)
guard let validateUrl = Utils.buildUrl(urlString,
token,
connectOptions: connectOptions,
adaptiveStream: adaptiveStream,
validate: true)
else {
throw LiveKitError(.failedToParseUrl, message: "Failed to parse validation url")
}

log("Validating with url: \(validateUrl)...")
let validationResponse = try await HTTP.requestString(from: validateUrl)
Expand Down
6 changes: 6 additions & 0 deletions Sources/LiveKit/Extensions/Primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ extension Bool {
}
}

extension URL {
var isSecure: Bool {
scheme == "https" || scheme == "wss"
}
}

public extension Double {
func rounded(to places: Int) -> Double {
let divisor = pow(10.0, Double(places))
Expand Down
27 changes: 0 additions & 27 deletions Sources/LiveKit/Extensions/URL.swift

This file was deleted.

24 changes: 10 additions & 14 deletions Sources/LiveKit/Support/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,34 @@ class Utils {
}

static func buildUrl(
_ url: URL,
_ url: String,
_ token: String,
connectOptions: ConnectOptions? = nil,
reconnectMode: ReconnectMode? = nil,
adaptiveStream: Bool,
validate: Bool = false,
forceSecure: Bool = false
) throws -> URL {
) -> URL? {
// use default options if nil
let connectOptions = connectOptions ?? ConnectOptions()

let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
guard let parsedUrl = URL(string: url) else { return nil }

guard var builder = components else {
throw LiveKitError(.failedToParseUrl)
}
let components = URLComponents(url: parsedUrl, resolvingAgainstBaseURL: false)

guard var builder = components else { return nil }

let useSecure = url.isSecure || forceSecure
let useSecure = parsedUrl.isSecure || forceSecure
let httpScheme = useSecure ? "https" : "http"
let wsScheme = useSecure ? "wss" : "ws"

var pathSegments = url.pathComponents
var pathSegments = parsedUrl.pathComponents
// strip empty & slashes
pathSegments.removeAll(where: { $0.isEmpty || $0 == "/" })

// if already ending with `rtc` or `validate`
// and is not a dir, remove it
if !url.hasDirectoryPath,
if !parsedUrl.hasDirectoryPath,
!pathSegments.isEmpty,
["rtc", "validate"].contains(pathSegments.last!)
{
Expand Down Expand Up @@ -196,11 +196,7 @@ class Utils {

builder.queryItems = queryItems

guard let result = builder.url else {
throw LiveKitError(.failedToParseUrl)
}

return result
return builder.url
}

static func computeVideoEncodings(
Expand Down

0 comments on commit 0c6b968

Please sign in to comment.