@@ -84,10 +84,11 @@ def normalized_context input
8484 id = normalized_context_field input , "eventId"
8585 timestamp = normalized_context_field input , "timestamp"
8686 type = normalized_context_field input , "eventType"
87+ domain = normalized_context_field input , "domain"
8788 service , resource = analyze_resource normalized_context_field input , "resource"
8889 service ||= service_from_type type
8990 return nil unless id && timestamp && type && service && resource
90- { id : id , timestamp : timestamp , type : type , service : service , resource : resource }
91+ { id : id , timestamp : timestamp , type : type , service : service , resource : resource , domain : domain }
9192 end
9293
9394 def normalized_context_field input , field
@@ -114,7 +115,7 @@ def service_from_type type
114115 end
115116
116117 def construct_cloud_event context , data
117- source , subject = convert_source context [ :service ] , context [ :resource ]
118+ source , subject = convert_source context [ :service ] , context [ :resource ] , context [ :domain ]
118119 type = LEGACY_TYPE_TO_CE_TYPE [ context [ :type ] ]
119120 return nil unless type && source
120121 ce_data , data_subject = convert_data context , data
@@ -129,12 +130,24 @@ def construct_cloud_event context, data
129130 time : context [ :timestamp ]
130131 end
131132
132- def convert_source service , resource
133+ def convert_source service , resource , domain
133134 return [ "//#{ service } /#{ resource } " , nil ] unless CE_SERVICE_TO_RESOURCE_RE . key? service
134135
135136 match = CE_SERVICE_TO_RESOURCE_RE [ service ] . match resource
136137 return [ nil , nil ] unless match
137- [ "//#{ service } /#{ match [ 1 ] } " , match [ 2 ] ]
138+
139+ if service == "firebasedatabase.googleapis.com"
140+ return [ nil , nil ] if domain . nil?
141+ location = "us-central1"
142+ if domain != "firebaseio.com"
143+ location_match = domain . match ( /^([\w -]+)\. firebasedatabase\. app$/ )
144+ return [ nil , nil ] unless location_match
145+ location = location_match [ 1 ]
146+ end
147+ [ "//#{ service } /projects/_/locations/#{ location } /#{ match [ 1 ] } " , match [ 2 ] ]
148+ else
149+ [ "//#{ service } /#{ match [ 1 ] } " , match [ 2 ] ]
150+ end
138151 end
139152
140153 def convert_data context , data
@@ -192,7 +205,7 @@ def convert_data context, data
192205
193206 CE_SERVICE_TO_RESOURCE_RE = {
194207 "firebase.googleapis.com" => %r{^(projects/[^/]+)/(events/[^/]+)$} ,
195- "firebasedatabase.googleapis.com" => %r{^( projects/_/instances/[^/]+)/(refs/.+)$} ,
208+ "firebasedatabase.googleapis.com" => %r{^projects/_/( instances/[^/]+)/(refs/.+)$} ,
196209 "firestore.googleapis.com" => %r{^(projects/[^/]+/databases/\( default\) )/(documents/.+)$} ,
197210 "storage.googleapis.com" => %r{^(projects/[^/]+/buckets/[^/]+)/([^#]+)(?:#.*)?$}
198211 } . freeze
0 commit comments