Firebase-Function — Storage Upload & Account-Creation Triggers

By Engineering Team•September 21, 2026•6 min read

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.”

  1. Fires on onObjectFinalized for the staging bucket the moment media-access-service’s presigned upload completes.
  2. 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).
  3. Publishes an event onto the Pub/Sub topic content-processor subscribes to, carrying tenant_id/correlation_id/traceparent in 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.

  1. 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’s transaction_name against a fixed migration_bundle constant; any other transaction name is ignored outright (returns null). See ADR-00045 for why batch creation waits for this event rather than a synchronous call from the uploader.
  2. Derives tenant_id/correlation_id/traceparent/transaction_id the same way the other storage trigger does — GCS object metadata in production, path segments in the local Storage emulator (the same resolveProductionIdentifiers fallback described above).
  3. Publishes the generic shared upload envelope (bucket, filePath, contentType, size, timeCreated, transaction_name, transaction_id, file_name_id — the same shape every onStorageUpload caller reuses, not a migration-specific payload) to a dedicated batch-uploaded Pub/Sub topic, under its own span name (publish.migration.batch.uploaded).
  4. tenant-migration’s MigrationBatchCreatedReceiver is the sole consumer — it derives batchId directly from the envelope’s transaction_id and creates the migration_batches document.

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.

  1. Builds a request-context store from the event itself: gateway_request_id from context.eventId, user_id from user.uid, a fresh correlation_id/request_id, and tenant_id from a hardcoded environment placeholder (DEFAULT_TENANT_ID) — see Implementation Notes and the linked technical-debt entry.
  2. Builds the same InitUserRequest shape user-secure-engine’s own signup-verify call uses: phone_number/phone_number_verified (derived from whether user.phoneNumber is set at all — a UserRecord only 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 single terms_accepted custom claim — by design, not a bug, since no separate privacy-acceptance claim exists), term_policy_version/privacy_policy_version from the account’s custom claims, user_type: 'CUSTOMER'.
  3. Calls the same validateAndInitUser operation user-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.
  4. 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-admin tooling.
  • 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_id is populated only when present in upstream metadata, falling back to the GCP Pub/Sub message’s own event.id. correlation_id is 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 call tracer().startActiveSpan() directly.
  • Trace-parent restoration is centralized inside runWithRequestContext: if store.traceparent is present and no span is already active, it restores parentage itself via propagation.extract/otelContext.with, so the storage-upload pipeline stays a single trace from the presigned-URL request through to content-processor’s processed-image publish.
  • gateway_request_id is 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_id does that job instead.
  • The user_account_created trigger’s tenant_id is a single hardcoded environment placeholder, not derived per-event — correct only for a single-tenant deployment, see TD-00021.
  • The user_account_created trigger reuses the exact same init_user-equivalent operation user-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.

Full service reference: firebase-function/CLAUDE.md. Storage pipeline overview: Create high resolution image for different process (ADR). Account-creation pipeline overview: ADR-00041.