Firebase-Function — Storage Upload & Account-Creation Triggers
Responsibility
firebase-function hosts three independent, trigger-driven Cloud Functions — none is an HTTP service, so there is no Nginx gateway, no request/response cycle, and no public route for any of them.
Storage upload trigger
firebase-function is the glue between “a file landed in the staging bucket” and “content-processor has work to do.”
- Fires on
onObjectFinalizedfor the staging bucket the momentmedia-access-service’s presigned upload completes. - Extracts tracing/tenant identifiers either from GCS object metadata (
x-goog-meta-*, in production) or from path segments (the local Firebase Storage emulator drops custom metadata on its/v0/b/endpoint). - Publishes an event onto the Pub/Sub topic
content-processorsubscribes to, carryingtenant_id/correlation_id/traceparentin the message header so the trace started at the presigned-URL request continues unbroken into the async processing pipeline.
A mirrored onObjectDeleted trigger does the same for asset-bucket deletions, publishing to a separate delete-events topic.
Migration-bundle upload trigger (storage_migration_upload_event.js)
Shares the same onObjectFinalized GCS trigger on the staging bucket as the Storage upload trigger above — same onStorageUpload wrapper from node-lang-core-lib, just a different filter and a different downstream topic.
- Fires on every staging-bucket upload — specifically GCS’s
onObjectFinalized, which only fires once the object write is fully durable in the bucket — then checks the upload’stransaction_nameagainst a fixedmigration_bundleconstant; any other transaction name is ignored outright (returnsnull). See ADR-00045 for why batch creation waits for this event rather than a synchronous call from the uploader. - Derives
tenant_id/correlation_id/traceparent/transaction_idthe same way the other storage trigger does — GCS object metadata in production, path segments in the local Storage emulator (the sameresolveProductionIdentifiersfallback described above). - Publishes the generic shared upload envelope (
bucket,filePath,contentType,size,timeCreated,transaction_name,transaction_id,file_name_id— the same shape everyonStorageUploadcaller reuses, not a migration-specific payload) to a dedicated batch-uploaded Pub/Sub topic, under its own span name (publish.migration.batch.uploaded). tenant-migration’sMigrationBatchCreatedReceiveris the sole consumer — it derivesbatchIddirectly from the envelope’stransaction_idand creates themigration_batchesdocument.
There is no corresponding delete trigger for migration bundles — the one registered delete trigger (storage_asset_delete_event.js) filters only on the product-image transaction name and has no migration branch.
Account-creation trigger (user_account_created)
A Firebase v1 functions.auth.user().onCreate() background trigger — deliberately v1, not v2: firebase-functions v7’s v2 identity module only exposes blocking functions (beforeUserCreated/beforeUserSignedIn), with no v2 background equivalent for “an account was just created.” It fires the moment user-secure-engine creates a shopper’s Firebase account during phone-OTP signup.
- Builds a request-context store from the event itself:
gateway_request_idfromcontext.eventId,user_idfromuser.uid, a freshcorrelation_id/request_id, andtenant_idfrom a hardcoded environment placeholder (DEFAULT_TENANT_ID) — see Implementation Notes and the linked technical-debt entry. - Builds the same
InitUserRequestshapeuser-secure-engine’s own signup-verify call uses:phone_number/phone_number_verified(derived from whetheruser.phoneNumberis set at all — aUserRecordonly carries a phone number once it’s gone through Firebase’s own phone-auth linking, so presence already implies verification),accept_terms/accept_privacy_policy(both read from the account’s singleterms_acceptedcustom claim — by design, not a bug, since no separate privacy-acceptance claim exists),term_policy_version/privacy_policy_versionfrom the account’s custom claims,user_type: 'CUSTOMER'. - Calls the same
validateAndInitUseroperationuser-secure-engine’s synchronous signup path already called moments earlier, as an idempotent safety net — see Mobile Login & Signup: Component Topology and Data Flow’s flow D for the full dual-path rationale. - Logs success/failure; on failure it lets Firebase’s own automatic retry handle it, since the underlying operation is idempotent.
Why Node, and why serverless
- Firebase Cloud Functions’ native GCS trigger binding is the zero-poll way to react to “a file appeared in a bucket” — no dedicated worker has to poll the bucket or maintain its own watch loop.
- Node is the natural fit for Firebase’s own Functions SDK and
firebase-admintooling. - Because this hop has no Nginx gateway in front of it, the usual REST-entry observability identifier (
X-Gateway-Request-ID) doesn’t apply here —gateway_request_idis populated only when present in upstream metadata, falling back to the GCP Pub/Sub message’s ownevent.id.correlation_idis what carries end-to-end async tracking instead.
Tech Stack
| Concern | Choice |
|---|---|
| Language | Node.js |
| Platform | Firebase Cloud Functions (GCS storage triggers) |
| Messaging | Firebase Pub/Sub (publish only) |
| Observability | OTLP over HTTP (port 4318, internal to the Docker network) — Go/Rust services instead use gRPC OTLP on 4317 |
Implementation Notes
runWithRequestContext(name, fn, store)is the only way a span is opened here — trigger files never calltracer().startActiveSpan()directly.- Trace-parent restoration is centralized inside
runWithRequestContext: ifstore.traceparentis present and no span is already active, it restores parentage itself viapropagation.extract/otelContext.with, so the storage-upload pipeline stays a single trace from the presigned-URL request through tocontent-processor’s processed-image publish. gateway_request_idis deliberately not forwarded into the Pub/Sub message — it’s a single-hop REST concept and this hop isn’t REST; forwarding it would imply a false causality between an HTTP request and the async chain.correlation_iddoes that job instead.- The
user_account_createdtrigger’stenant_idis a single hardcoded environment placeholder, not derived per-event — correct only for a single-tenant deployment, see TD-00021. - The
user_account_createdtrigger reuses the exact sameinit_user-equivalent operationuser-secure-engine’s own synchronous signup path calls — it is a deliberately redundant idempotent safety net, not an independent source of truth; see Mobile Login & Signup: Component Topology and Data Flow for why both paths exist.
Related Documents
- tenant-migration — consumes the migration-bundle upload envelope this trigger publishes
- tenant-migration-orchestrator
- BRD-028 — Tenant Catalog Bulk Data Migration
- SR-029 — Tenant Catalog Bulk Data Migration Spec
- ADR-00043 — NDJSON Streaming and Wave-Barrier Execution for Tenant Catalog Migration
- ADR-00045 — Event-Driven Batch Creation From Storage-Upload Completion
- Architecture-00024 — Tenant Catalog Bulk Migration Component Topology and Data Flow
- TD-00023 — Tenant Catalog Bulk Migration Gaps
Full service reference: firebase-function/CLAUDE.md. Storage pipeline overview: Create high resolution image for different process (ADR). Account-creation pipeline overview: ADR-00041.