-
Notifications
You must be signed in to change notification settings - Fork 965
/
CAPInstanceConfiguration.swift
78 lines (67 loc) · 2.61 KB
/
CAPInstanceConfiguration.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Foundation
extension InstanceConfiguration {
@objc public var appStartFileURL: URL {
if let path = appStartPath {
return appLocation.appendingPathComponent(path)
}
return appLocation
}
@objc public var appStartServerURL: URL {
if let path = appStartPath {
return serverURL.appendingPathComponent(path)
}
return serverURL
}
@objc public var errorPathURL: URL? {
guard let errorPath = errorPath else {
return nil
}
return localURL.appendingPathComponent(errorPath)
}
@available(*, deprecated, message: "Use getPluginConfig")
@objc public func getPluginConfigValue(_ pluginId: String, _ configKey: String) -> Any? {
return (pluginConfigurations as? JSObject)?[keyPath: KeyPath("\(pluginId).\(configKey)")]
}
@objc public func getPluginConfig(_ pluginId: String) -> PluginConfig {
if let cfg = (pluginConfigurations as? JSObject)?[keyPath: KeyPath("\(pluginId)")] as? JSObject {
return PluginConfig(config: cfg)
}
return PluginConfig(config: JSObject())
}
@objc public func shouldAllowNavigation(to host: String) -> Bool {
for hostname in allowedNavigationHostnames {
if doesHost(host, match: hostname) {
return true
}
}
return false
}
@available(*, deprecated, message: "Use direct property accessors")
@objc public func getValue(_ key: String) -> Any? {
return (legacyConfig as? JSObject)?[keyPath: KeyPath(key)]
}
@available(*, deprecated, message: "Use direct property accessors")
@objc public func getString(_ key: String) -> String? {
return (legacyConfig as? JSObject)?[keyPath: KeyPath(key)] as? String
}
// MARK: - Private
private func doesHost(_ host: String, match pattern: String) -> Bool {
// bail early in the simple case
if pattern == "*" {
return true
}
// break apart the pieces
var hostComponents = host.lowercased().split(separator: ".")
var patternComponents = pattern.lowercased().split(separator: ".")
guard hostComponents.count == patternComponents.count else {
return false
}
// remove any wildcard segments
for wildcard in patternComponents.enumerated().reversed().filter({ $0.element == "*" }) {
hostComponents.remove(at: wildcard.offset)
patternComponents.remove(at: wildcard.offset)
}
// match with what's left
return hostComponents == patternComponents
}
}