|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Firebase |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +import { EventContext, Resource } from "../v1/cloud-functions"; |
| 24 | + |
| 25 | +export interface LegacyContextOptions<Params> { |
| 26 | + /** |
| 27 | + * The event type to expose via the legacy context. |
| 28 | + */ |
| 29 | + eventType: string; |
| 30 | + /** |
| 31 | + * The backing service name (e.g. firestore.googleapis.com). |
| 32 | + */ |
| 33 | + service: string; |
| 34 | + /** |
| 35 | + * Optional resource information. If omitted the function will attempt to |
| 36 | + * derive a resource from the CloudEvent source. |
| 37 | + */ |
| 38 | + resource?: string | Resource; |
| 39 | + /** |
| 40 | + * Event params to expose. |
| 41 | + */ |
| 42 | + params?: Params; |
| 43 | + /** |
| 44 | + * Optional override for the legacy timestamp. Defaults to the CloudEvent time. |
| 45 | + */ |
| 46 | + timestamp?: string; |
| 47 | + /** |
| 48 | + * Optional override for the legacy event ID. Defaults to the CloudEvent id. |
| 49 | + */ |
| 50 | + eventId?: string; |
| 51 | + /** |
| 52 | + * Optional auth information to surface. |
| 53 | + */ |
| 54 | + auth?: EventContext<Params>["auth"]; |
| 55 | + /** |
| 56 | + * Optional auth type information to surface. |
| 57 | + */ |
| 58 | + authType?: EventContext<Params>["authType"]; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Defines a lazily-evaluated property on an object with memoized computation. |
| 63 | + * |
| 64 | + * @internal |
| 65 | + */ |
| 66 | +export function defineLazyGetter<T extends object, K extends PropertyKey, V>( |
| 67 | + target: T, |
| 68 | + property: K, |
| 69 | + compute: () => V |
| 70 | +): void { |
| 71 | + if (Object.prototype.hasOwnProperty.call(target, property)) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + let cached: V; |
| 76 | + let initialized = false; |
| 77 | + |
| 78 | + Object.defineProperty(target, property, { |
| 79 | + configurable: true, |
| 80 | + enumerable: true, |
| 81 | + get(): V { |
| 82 | + if (!initialized) { |
| 83 | + cached = compute(); |
| 84 | + initialized = true; |
| 85 | + } |
| 86 | + return cached; |
| 87 | + }, |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Creates a v1-compatible EventContext from a v2 CloudEvent. |
| 93 | + * |
| 94 | + * @param event - The CloudEvent carrying the id/time/source metadata. |
| 95 | + * @param options - Legacy context configuration. |
| 96 | + * @internal |
| 97 | + */ |
| 98 | +export function makeLegacyEventContext<Params>( |
| 99 | + event: { id?: string; time?: string; source?: string }, |
| 100 | + options: LegacyContextOptions<Params> |
| 101 | +): EventContext<Params> { |
| 102 | + const resource = normalizeResource(options.service, options.resource ?? event.source); |
| 103 | + return { |
| 104 | + eventId: options.eventId ?? event.id ?? "", |
| 105 | + timestamp: options.timestamp ?? event.time ?? new Date().toISOString(), |
| 106 | + eventType: options.eventType, |
| 107 | + resource, |
| 108 | + params: (options.params ?? ({} as Params)) as Params, |
| 109 | + auth: options.auth, |
| 110 | + authType: options.authType, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * A bundle describing how to decorate an event with legacy helpers. |
| 116 | + */ |
| 117 | +export interface LegacyDecoration<Params, Extras extends Record<string, () => unknown>> { |
| 118 | + context: LegacyContextOptions<Params>; |
| 119 | + getters?: Extras; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Applies legacy context plus any number of additional lazy getters to the given event object. |
| 124 | + * |
| 125 | + * @internal |
| 126 | + */ |
| 127 | +export function decorateLegacyEvent< |
| 128 | + EventType extends object, |
| 129 | + Params, |
| 130 | + Extras extends Record<string, () => unknown> |
| 131 | +>(event: EventType, decoration: LegacyDecoration<Params, Extras>): void { |
| 132 | + defineLazyGetter( |
| 133 | + event as EventType & { context?: EventContext<Params> }, |
| 134 | + "context", |
| 135 | + () => makeLegacyEventContext(event as any, decoration.context) |
| 136 | + ); |
| 137 | + |
| 138 | + if (!decoration.getters) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + for (const [key, factory] of Object.entries(decoration.getters)) { |
| 143 | + defineLazyGetter(event, key as keyof EventType, factory as () => unknown); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function normalizeResource(service: string, resource?: string | Resource): Resource { |
| 148 | + if (!resource) { |
| 149 | + return { |
| 150 | + service, |
| 151 | + name: "", |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + if (typeof resource !== "string") { |
| 156 | + return { |
| 157 | + service: resource.service ?? service, |
| 158 | + name: resource.name, |
| 159 | + type: resource.type, |
| 160 | + labels: resource.labels, |
| 161 | + }; |
| 162 | + } |
| 163 | + |
| 164 | + let computedService = service; |
| 165 | + let name = resource.trim(); |
| 166 | + |
| 167 | + if (name.startsWith("//")) { |
| 168 | + const withoutScheme = name.slice(2); |
| 169 | + const firstSlash = withoutScheme.indexOf("/"); |
| 170 | + if (firstSlash === -1) { |
| 171 | + computedService = withoutScheme || service; |
| 172 | + name = ""; |
| 173 | + } else { |
| 174 | + computedService = withoutScheme.slice(0, firstSlash) || service; |
| 175 | + name = withoutScheme.slice(firstSlash + 1); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if (name.startsWith(computedService + "/")) { |
| 180 | + name = name.slice(computedService.length + 1); |
| 181 | + } |
| 182 | + |
| 183 | + name = name.replace(/^\/+/, ""); |
| 184 | + |
| 185 | + return { |
| 186 | + service: computedService, |
| 187 | + name, |
| 188 | + }; |
| 189 | +} |
0 commit comments