feature: functions can now declare lifecycle hooks#1915
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for post-deployment lifecycle hooks (afterInstall and afterUpdate) by adding a new lifecycle module, updating the manifest stack loader to capture these hooks, and exposing the module exports in package.json. The review feedback highlights a correctness issue in src/runtime/loader.ts where falsy but valid JSON values (such as 0 or false) are incorrectly omitted during serialization, and suggests using TypeScript union types in src/lifecycle/index.ts to enforce mutually exclusive properties at compile-time.
02d17b8 to
9489ef4
Compare
…e falsy body/params in loader
inlined
left a comment
There was a problem hiding this comment.
Don't forget that when we publish this it should also go in index.ts
You're going to need to add the fake empty file to avoid eslint problems (v2 not src/v2)
|
|
||
| import { Expression } from "../params"; | ||
|
|
||
| export type TargetFunction = string | Expression<string>; |
There was a problem hiding this comment.
I don't think a function needs to be an expression. I'm also curious whether anything needs to be parameterized except for authorization which is automatic.
|
|
||
| export type HttpAction = | ||
| | { | ||
| function: TargetFunction; |
There was a problem hiding this comment.
What does it mean to pass a function and a URL? I would expect this type to be:
export type HttpAction = ({ function: TargetFunction} | { url: string }) & {
method, headers, body ...
}|
|
||
| const hooks = Object.entries(declaredLifecycleHooks); | ||
| if (hooks.length > 0) { | ||
| stack.lifecycleHooks = {}; |
There was a problem hiding this comment.
Why can't this all be replaced with stack.lifecycleHooks = hooks? Aren't they the wire protocol already?
inlined
left a comment
There was a problem hiding this comment.
Sorry, forgot to set approve. I trust that this feedback is straightforward enough to fix if you also agree about the expression thing
Description
This PR adds support for declaring codebase lifecycle hooks in the Firebase Functions SDK. Developers can now register actions (
afterInstallandafterUpdate) that are automatically executed post-deployment.Summary of Changes
1. Hook Registrations (
src/lifecycle/index.ts)firebase-functions/lifecycle:afterInstall(action: LifecycleAction): Registers a hook to run when resources in the codebase are deployed for the first time.afterUpdate(action: LifecycleAction): Registers a hook to run when resources in the codebase are updated.task: Triggers a task queue function with an optional payloadbody.call: Calls a function (callable trigger) with optionalparams.http: Triggers a HTTP endpoint via requesturl/function,method, andbody.afterInstallandafterUpdatecan be registered per codebase at runtime.2. Stack Manifest Parsing (
src/runtime/loader.ts,src/runtime/manifest.ts)ManifestLifecycleActionand updatedManifestStacktypes to include thelifecycleHooksfield.loadStackinsideloader.tsto capture any declared lifecycle hooks from global symbols during function discovery, mapping them into the compiled JSON/wire format returned tofirebase-tools.stackToWireserialization helper to recursively resolve CEL expressions and reset values insidelifecycleHooksproperties.3. Tests
spec/lifecycle/lifecycle.spec.tsto test hook registration and duplicate hook prevention.spec/runtime/loader.spec.tsverifying thatafterInstallandafterUpdatedeclarations are correctly discovered and serialized into theManifestStack.Code sample