| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- // Copyright 2022 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- import FirebaseAppCheckInterop
- import FirebaseAuthInterop
- import FirebaseCore
- import FirebaseMessagingInterop
- import FirebaseSharedSwift
- import Foundation
- #if COCOAPODS
- import GTMSessionFetcher
- #else
- import GTMSessionFetcherCore
- #endif
- // Avoids exposing internal FirebaseCore APIs to Swift users.
- @_implementationOnly import FirebaseCoreExtension
- /// File specific constants.
- private enum Constants {
- static let appCheckTokenHeader = "X-Firebase-AppCheck"
- static let fcmTokenHeader = "Firebase-Instance-ID-Token"
- }
- /// Cross SDK constants.
- enum FunctionsConstants {
- static let defaultRegion = "us-central1"
- }
- /// `Functions` is the client for Cloud Functions for a Firebase project.
- @objc(FIRFunctions) open class Functions: NSObject {
- // MARK: - Private Variables
- /// The network client to use for http requests.
- private let fetcherService: GTMSessionFetcherService
- /// The projectID to use for all function references.
- private let projectID: String
- /// A serializer to encode/decode data and return values.
- private let serializer = FunctionsSerializer()
- /// A factory for getting the metadata to include with function calls.
- private let contextProvider: FunctionsContextProvider
- /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
- /// containing all instances of Functions associated with the given app.
- private static var instances: [String: [Functions]] = [:]
- /// Lock to manage access to the instances array to avoid race conditions.
- private static var instancesLock: os_unfair_lock = .init()
- /// The custom domain to use for all functions references (optional).
- let customDomain: String?
- /// The region to use for all function references.
- let region: String
- // MARK: - Public APIs
- /// The current emulator origin, or `nil` if it is not set.
- open private(set) var emulatorOrigin: String?
- /// Creates a Cloud Functions client using the default or returns a pre-existing instance if it
- /// already exists.
- /// - Returns: A shared Functions instance initialized with the default `FirebaseApp`.
- @objc(functions) open class func functions() -> Functions {
- return functions(
- app: FirebaseApp.app(),
- region: FunctionsConstants.defaultRegion,
- customDomain: nil
- )
- }
- /// Creates a Cloud Functions client with the given app, or returns a pre-existing
- /// instance if one already exists.
- /// - Parameter app: The app for the Firebase project.
- /// - Returns: A shared Functions instance initialized with the specified `FirebaseApp`.
- @objc(functionsForApp:) open class func functions(app: FirebaseApp) -> Functions {
- return functions(app: app, region: FunctionsConstants.defaultRegion, customDomain: nil)
- }
- /// Creates a Cloud Functions client with the default app and given region.
- /// - Parameter region: The region for the HTTP trigger, such as `us-central1`.
- /// - Returns: A shared Functions instance initialized with the default `FirebaseApp` and a
- /// custom region.
- @objc(functionsForRegion:) open class func functions(region: String) -> Functions {
- return functions(app: FirebaseApp.app(), region: region, customDomain: nil)
- }
- /// Creates a Cloud Functions client with the given custom domain or returns a pre-existing
- /// instance if one already exists.
- /// - Parameter customDomain: A custom domain for the HTTP trigger, such as
- /// "https://mydomain.com".
- /// - Returns: A shared Functions instance initialized with the default `FirebaseApp` and a
- /// custom HTTP trigger domain.
- @objc(functionsForCustomDomain:) open class func functions(customDomain: String) -> Functions {
- return functions(app: FirebaseApp.app(),
- region: FunctionsConstants.defaultRegion, customDomain: customDomain)
- }
- /// Creates a Cloud Functions client with the given app and region, or returns a pre-existing
- /// instance if one already exists.
- /// - Parameters:
- /// - app: The app for the Firebase project.
- /// - region: The region for the HTTP trigger, such as `us-central1`.
- /// - Returns: An instance of `Functions` with a custom app and region.
- @objc(functionsForApp:region:) open class func functions(app: FirebaseApp,
- region: String) -> Functions {
- return functions(app: app, region: region, customDomain: nil)
- }
- /// Creates a Cloud Functions client with the given app and custom domain, or returns a
- /// pre-existing
- /// instance if one already exists.
- /// - Parameters:
- /// - app: The app for the Firebase project.
- /// - customDomain: A custom domain for the HTTP trigger, such as `https://mydomain.com`.
- /// - Returns: An instance of `Functions` with a custom app and HTTP trigger domain.
- @objc(functionsForApp:customDomain:) open class func functions(app: FirebaseApp,
- customDomain: String)
- -> Functions {
- return functions(app: app, region: FunctionsConstants.defaultRegion, customDomain: customDomain)
- }
- /// Creates a reference to the Callable HTTPS trigger with the given name.
- /// - Parameter name: The name of the Callable HTTPS trigger.
- /// - Returns: A reference to a Callable HTTPS trigger.
- @objc(HTTPSCallableWithName:) open func httpsCallable(_ name: String) -> HTTPSCallable {
- return HTTPSCallable(functions: self, name: name)
- }
- /// Creates a reference to the Callable HTTPS trigger with the given name and configuration
- /// options.
- /// - Parameters:
- /// - name: The name of the Callable HTTPS trigger.
- /// - options: The options with which to customize the Callable HTTPS trigger.
- /// - Returns: A reference to a Callable HTTPS trigger.
- @objc(HTTPSCallableWithName:options:) public func httpsCallable(_ name: String,
- options: HTTPSCallableOptions)
- -> HTTPSCallable {
- return HTTPSCallable(functions: self, name: name, options: options)
- }
- /// Creates a reference to the Callable HTTPS trigger with the given name.
- /// - Parameter url: The URL of the Callable HTTPS trigger.
- /// - Returns: A reference to a Callable HTTPS trigger.
- @objc(HTTPSCallableWithURL:) open func httpsCallable(_ url: URL) -> HTTPSCallable {
- return HTTPSCallable(functions: self, url: url)
- }
- /// Creates a reference to the Callable HTTPS trigger with the given name and configuration
- /// options.
- /// - Parameters:
- /// - url: The URL of the Callable HTTPS trigger.
- /// - options: The options with which to customize the Callable HTTPS trigger.
- /// - Returns: A reference to a Callable HTTPS trigger.
- @objc(HTTPSCallableWithURL:options:) public func httpsCallable(_ url: URL,
- options: HTTPSCallableOptions)
- -> HTTPSCallable {
- return HTTPSCallable(functions: self, url: url, options: options)
- }
- /// Creates a reference to the Callable HTTPS trigger with the given name, the type of an
- /// `Encodable`
- /// request and the type of a `Decodable` response.
- /// - Parameters:
- /// - name: The name of the Callable HTTPS trigger
- /// - requestAs: The type of the `Encodable` entity to use for requests to this `Callable`
- /// - responseAs: The type of the `Decodable` entity to use for responses from this `Callable`
- /// - encoder: The encoder instance to use to perform encoding.
- /// - decoder: The decoder instance to use to perform decoding.
- /// - Returns: A reference to an HTTPS-callable Cloud Function that can be used to make Cloud
- /// Functions invocations.
- open func httpsCallable<Request: Encodable,
- Response: Decodable>(_ name: String,
- requestAs: Request.Type = Request.self,
- responseAs: Response.Type = Response.self,
- encoder: FirebaseDataEncoder = FirebaseDataEncoder(
- ),
- decoder: FirebaseDataDecoder = FirebaseDataDecoder(
- ))
- -> Callable<Request, Response> {
- return Callable(
- callable: httpsCallable(name),
- encoder: encoder,
- decoder: decoder
- )
- }
- /// Creates a reference to the Callable HTTPS trigger with the given name, the type of an
- /// `Encodable`
- /// request and the type of a `Decodable` response.
- /// - Parameters:
- /// - name: The name of the Callable HTTPS trigger
- /// - options: The options with which to customize the Callable HTTPS trigger.
- /// - requestAs: The type of the `Encodable` entity to use for requests to this `Callable`
- /// - responseAs: The type of the `Decodable` entity to use for responses from this `Callable`
- /// - encoder: The encoder instance to use to perform encoding.
- /// - decoder: The decoder instance to use to perform decoding.
- /// - Returns: A reference to an HTTPS-callable Cloud Function that can be used to make Cloud
- /// Functions invocations.
- open func httpsCallable<Request: Encodable,
- Response: Decodable>(_ name: String,
- options: HTTPSCallableOptions,
- requestAs: Request.Type = Request.self,
- responseAs: Response.Type = Response.self,
- encoder: FirebaseDataEncoder = FirebaseDataEncoder(
- ),
- decoder: FirebaseDataDecoder = FirebaseDataDecoder(
- ))
- -> Callable<Request, Response> {
- return Callable(
- callable: httpsCallable(name, options: options),
- encoder: encoder,
- decoder: decoder
- )
- }
- /// Creates a reference to the Callable HTTPS trigger with the given name, the type of an
- /// `Encodable`
- /// request and the type of a `Decodable` response.
- /// - Parameters:
- /// - url: The url of the Callable HTTPS trigger
- /// - requestAs: The type of the `Encodable` entity to use for requests to this `Callable`
- /// - responseAs: The type of the `Decodable` entity to use for responses from this `Callable`
- /// - encoder: The encoder instance to use to perform encoding.
- /// - decoder: The decoder instance to use to perform decoding.
- /// - Returns: A reference to an HTTPS-callable Cloud Function that can be used to make Cloud
- /// Functions invocations.
- open func httpsCallable<Request: Encodable,
- Response: Decodable>(_ url: URL,
- requestAs: Request.Type = Request.self,
- responseAs: Response.Type = Response.self,
- encoder: FirebaseDataEncoder = FirebaseDataEncoder(
- ),
- decoder: FirebaseDataDecoder = FirebaseDataDecoder(
- ))
- -> Callable<Request, Response> {
- return Callable(
- callable: httpsCallable(url),
- encoder: encoder,
- decoder: decoder
- )
- }
- /// Creates a reference to the Callable HTTPS trigger with the given name, the type of an
- /// `Encodable`
- /// request and the type of a `Decodable` response.
- /// - Parameters:
- /// - url: The url of the Callable HTTPS trigger
- /// - options: The options with which to customize the Callable HTTPS trigger.
- /// - requestAs: The type of the `Encodable` entity to use for requests to this `Callable`
- /// - responseAs: The type of the `Decodable` entity to use for responses from this `Callable`
- /// - encoder: The encoder instance to use to perform encoding.
- /// - decoder: The decoder instance to use to perform decoding.
- /// - Returns: A reference to an HTTPS-callable Cloud Function that can be used to make Cloud
- /// Functions invocations.
- open func httpsCallable<Request: Encodable,
- Response: Decodable>(_ url: URL,
- options: HTTPSCallableOptions,
- requestAs: Request.Type = Request.self,
- responseAs: Response.Type = Response.self,
- encoder: FirebaseDataEncoder = FirebaseDataEncoder(
- ),
- decoder: FirebaseDataDecoder = FirebaseDataDecoder(
- ))
- -> Callable<Request, Response> {
- return Callable(
- callable: httpsCallable(url, options: options),
- encoder: encoder,
- decoder: decoder
- )
- }
- /**
- * Changes this instance to point to a Cloud Functions emulator running locally.
- * See https://firebase.google.com/docs/functions/local-emulator
- * - Parameters:
- * - host: The host of the local emulator, such as "localhost".
- * - port: The port of the local emulator, for example 5005.
- */
- @objc open func useEmulator(withHost host: String, port: Int) {
- let prefix = host.hasPrefix("http") ? "" : "http://"
- let origin = String(format: "\(prefix)\(host):%li", port)
- emulatorOrigin = origin
- }
- // MARK: - Private Funcs (or Internal for tests)
- /// Solely used to have one precondition and one location where we fetch from the container. This
- /// previously was avoided due to default arguments but that doesn't work well with Obj-C
- /// compatibility.
- private class func functions(app: FirebaseApp?, region: String,
- customDomain: String?) -> Functions {
- guard let app else {
- fatalError("`FirebaseApp.configure()` needs to be called before using Functions.")
- }
- os_unfair_lock_lock(&instancesLock)
- // Unlock before the function returns.
- defer { os_unfair_lock_unlock(&instancesLock) }
- if let associatedInstances = instances[app.name] {
- for instance in associatedInstances {
- // Domains may be nil, so handle with care.
- var equalDomains = false
- if let instanceCustomDomain = instance.customDomain {
- equalDomains = instanceCustomDomain == customDomain
- } else {
- equalDomains = customDomain == nil
- }
- // Check if it's a match.
- if instance.region == region, equalDomains {
- return instance
- }
- }
- }
- let newInstance = Functions(app: app, region: region, customDomain: customDomain)
- let existingInstances = instances[app.name, default: []]
- instances[app.name] = existingInstances + [newInstance]
- return newInstance
- }
- @objc init(projectID: String,
- region: String,
- customDomain: String?,
- auth: AuthInterop?,
- messaging: MessagingInterop?,
- appCheck: AppCheckInterop?,
- fetcherService: GTMSessionFetcherService = GTMSessionFetcherService()) {
- self.projectID = projectID
- self.region = region
- self.customDomain = customDomain
- emulatorOrigin = nil
- contextProvider = FunctionsContextProvider(auth: auth,
- messaging: messaging,
- appCheck: appCheck)
- self.fetcherService = fetcherService
- }
- /// Using the component system for initialization.
- convenience init(app: FirebaseApp,
- region: String,
- customDomain: String?) {
- // TODO: These are not optionals, but they should be.
- let auth = ComponentType<AuthInterop>.instance(for: AuthInterop.self, in: app.container)
- let messaging = ComponentType<MessagingInterop>.instance(for: MessagingInterop.self,
- in: app.container)
- let appCheck = ComponentType<AppCheckInterop>.instance(for: AppCheckInterop.self,
- in: app.container)
- guard let projectID = app.options.projectID else {
- fatalError("Firebase Functions requires the projectID to be set in the App's Options.")
- }
- self.init(projectID: projectID,
- region: region,
- customDomain: customDomain,
- auth: auth,
- messaging: messaging,
- appCheck: appCheck)
- }
- func urlWithName(_ name: String) -> String {
- assert(!name.isEmpty, "Name cannot be empty")
- // Check if we're using the emulator
- if let emulatorOrigin {
- return "\(emulatorOrigin)/\(projectID)/\(region)/\(name)"
- }
- // Check the custom domain.
- if let customDomain {
- return "\(customDomain)/\(name)"
- }
- return "https://\(region)-\(projectID).cloudfunctions.net/\(name)"
- }
- func callFunction(name: String,
- withObject data: Any?,
- options: HTTPSCallableOptions?,
- timeout: TimeInterval,
- completion: @escaping ((Result<HTTPSCallableResult, Error>) -> Void)) {
- // Get context first.
- contextProvider.getContext(options: options) { context, error in
- // Note: context is always non-nil since some checks could succeed, we're only failing if
- // there's an error.
- if let error {
- completion(.failure(error))
- } else {
- let url = self.urlWithName(name)
- self.callFunction(url: URL(string: url)!,
- withObject: data,
- options: options,
- timeout: timeout,
- context: context,
- completion: completion)
- }
- }
- }
- func callFunction(url: URL,
- withObject data: Any?,
- options: HTTPSCallableOptions?,
- timeout: TimeInterval,
- completion: @escaping ((Result<HTTPSCallableResult, Error>) -> Void)) {
- // Get context first.
- contextProvider.getContext(options: options) { context, error in
- // Note: context is always non-nil since some checks could succeed, we're only failing if
- // there's an error.
- if let error {
- completion(.failure(error))
- } else {
- self.callFunction(url: url,
- withObject: data,
- options: options,
- timeout: timeout,
- context: context,
- completion: completion)
- }
- }
- }
- private func callFunction(url: URL,
- withObject data: Any?,
- options: HTTPSCallableOptions?,
- timeout: TimeInterval,
- context: FunctionsContext,
- completion: @escaping ((Result<HTTPSCallableResult, Error>) -> Void)) {
- let request = URLRequest(url: url,
- cachePolicy: .useProtocolCachePolicy,
- timeoutInterval: timeout)
- let fetcher = fetcherService.fetcher(with: request)
- do {
- let data = data ?? NSNull()
- let encoded = try serializer.encode(data)
- let body = ["data": encoded]
- let payload = try JSONSerialization.data(withJSONObject: body)
- fetcher.bodyData = payload
- } catch {
- DispatchQueue.main.async {
- completion(.failure(error))
- }
- return
- }
- // Set the headers.
- fetcher.setRequestValue("application/json", forHTTPHeaderField: "Content-Type")
- if let authToken = context.authToken {
- let value = "Bearer \(authToken)"
- fetcher.setRequestValue(value, forHTTPHeaderField: "Authorization")
- }
- if let fcmToken = context.fcmToken {
- fetcher.setRequestValue(fcmToken, forHTTPHeaderField: Constants.fcmTokenHeader)
- }
- if options?.requireLimitedUseAppCheckTokens == true {
- if let appCheckToken = context.limitedUseAppCheckToken {
- fetcher.setRequestValue(
- appCheckToken,
- forHTTPHeaderField: Constants.appCheckTokenHeader
- )
- }
- } else if let appCheckToken = context.appCheckToken {
- fetcher.setRequestValue(
- appCheckToken,
- forHTTPHeaderField: Constants.appCheckTokenHeader
- )
- }
- // Override normal security rules if this is a local test.
- if emulatorOrigin != nil {
- fetcher.allowLocalhostRequest = true
- fetcher.allowedInsecureSchemes = ["http"]
- }
- fetcher.beginFetch { [self] data, error in
- let result: Result<HTTPSCallableResult, any Error>
- do {
- let data = try responseData(data: data, error: error)
- let json = try responseDataJSON(from: data)
- // TODO: Refactor `decode(_:)` so it either returns a non-optional object or throws
- let payload = try serializer.decode(json)
- // TODO: Remove `as Any` once `decode(_:)` is refactored
- result = .success(HTTPSCallableResult(data: payload as Any))
- } catch {
- result = .failure(error)
- }
- DispatchQueue.main.async {
- completion(result)
- }
- }
- }
- private func responseData(data: Data?, error: (any Error)?) throws -> Data {
- // Case 1: `error` is not `nil` -> always throws
- if let error = error as NSError? {
- let localError: (any Error)?
- if error.domain == kGTMSessionFetcherStatusDomain {
- localError = FunctionsErrorCode.errorForResponse(
- status: error.code,
- body: data,
- serializer: serializer
- )
- } else if error.domain == NSURLErrorDomain, error.code == NSURLErrorTimedOut {
- localError = FunctionsErrorCode.deadlineExceeded.generatedError(userInfo: nil)
- } else {
- localError = nil
- }
- throw localError ?? error
- }
- // Case 2: `data` is `nil` -> always throws
- guard let data else {
- throw FunctionsErrorCode.internal.generatedError(userInfo: nil)
- }
- // Case 3: `data` is not `nil` but might specify a custom error -> throws conditionally
- if let bodyError = FunctionsErrorCode.errorForResponse(
- status: 200,
- body: data,
- serializer: serializer
- ) {
- throw bodyError
- }
- // Case 4: `error` is `nil`; `data` is not `nil`; `data` doesn’t specify an error -> OK
- return data
- }
- private func responseDataJSON(from data: Data) throws -> Any {
- let responseJSONObject = try JSONSerialization.jsonObject(with: data)
- guard let responseJSON = responseJSONObject as? NSDictionary else {
- let userInfo = [NSLocalizedDescriptionKey: "Response was not a dictionary."]
- throw FunctionsErrorCode.internal.generatedError(userInfo: userInfo)
- }
- // `result` is checked for backwards compatibility:
- guard let dataJSON = responseJSON["data"] ?? responseJSON["result"] else {
- let userInfo = [NSLocalizedDescriptionKey: "Response is missing data field."]
- throw FunctionsErrorCode.internal.generatedError(userInfo: userInfo)
- }
- return dataJSON
- }
- }
|