@@ -7,15 +7,29 @@ import Rego
77extension OPA {
88
99 /// BundleLoader abstracts over the details of retrieving a bundle.
10- public protocol BundleLoader {
10+ public protocol BundleLoader : Sendable {
1111 /// Needs a public constructor that can build from the config directly.
1212 init ( config: OPA . Config , bundleResourceName: String ) throws
1313
14+ /// Constructor for loading a discovery bundle.
15+ ///
16+ /// The loader should read the bundle location from `config.discovery`
17+ /// rather than `config.bundles`. Loaders that don't support discovery
18+ /// inherit a default implementation that throws.
19+ init ( discoveryConfig: OPA . Config ) throws
20+
1421 /// Load the bundle, based on the config and any existing state.
1522 mutating func load( ) async -> Result < OPA . Bundle , any Swift . Error >
1623
1724 /// Compatibility check, based on what's in the OPA config.
1825 static func compatibleWithConfig( config: OPA . Config , bundleResourceName: String ) -> Bool
26+
27+ /// Compatibility check for discovery bundle loading.
28+ ///
29+ /// Returns `true` if this loader type can handle fetching the
30+ /// discovery bundle described by `config.discovery`. The default
31+ /// implementation returns `false`.
32+ static func compatibleWithDiscoveryConfig( config: OPA . Config ) -> Bool
1933 }
2034
2135 /// HTTPBundleLoader is a slightly more specialized protocol to allow greater
@@ -26,7 +40,41 @@ extension OPA {
2640 config: OPA . Config , bundleResourceName: String , etag: String ? , headers: [ String : String ] ? ,
2741 httpClient: HTTPClient ? ) throws
2842
29- /// Used by the loader-manging task to determine whether to sleep or not between polls.
43+ /// Constructor for loading a discovery bundle over HTTP.
44+ ///
45+ /// The loader should read the bundle location from `config.discovery`
46+ /// rather than `config.bundles`. Loaders that don't support discovery
47+ /// inherit a default implementation that throws.
48+ init (
49+ discoveryConfig: OPA . Config , etag: String ? , headers: [ String : String ] ? ,
50+ httpClient: HTTPClient ? ) throws
51+
52+ /// Used by the loader-managing task to determine whether to sleep or not between polls.
3053 func isLongPollingEnabled( ) -> Bool
3154 }
3255}
56+
57+ // MARK: - Default Discovery Implementations
58+
59+ extension OPA . BundleLoader {
60+ /// Default: this loader does not support discovery.
61+ public static func compatibleWithDiscoveryConfig( config: OPA . Config ) -> Bool {
62+ return false
63+ }
64+
65+ /// Default: loader throws at init time.
66+ public init ( discoveryConfig: OPA . Config ) throws {
67+ throw RuntimeError ( code: . discoveryNotSupported, message: " Bundle loader does not support Discovery " )
68+ }
69+
70+ }
71+
72+ extension OPA . HTTPBundleLoader {
73+ /// Default: loader throws at init time.
74+ public init (
75+ discoveryConfig: OPA . Config , etag: String ? , headers: [ String : String ] ? ,
76+ httpClient: HTTPClient ?
77+ ) throws {
78+ throw RuntimeError ( code: . discoveryNotSupported, message: " Bundle loader does not support Discovery " )
79+ }
80+ }
0 commit comments