Firebase Functions のバンドラーとして Bun を使用すると、Firestore の API 呼び出しがバグる問題

2025/10/19 23:11公開
Table of Contents
  1. 前提
  2. 問題発生
  3. 原因と対処法
  4. 参考

前提

以下のようなディレクトリ構造で、Firebase Cloud Functions で書いた TypeScript コードを Bun を用いてバンドル化する運用をしている。

├ lib
│ ├ index.js (生成)
│ └ package.json
├ src
│ └ index.ts
└ package.json

親のpackage.json

{
  "name": "functions",
  "type": "module",
  "scripts": {
    "lint": "eslint .",
    "build": "bun build src/index.ts --outdir lib --target node --minify",
    "build:watch": "bun build src/index.ts --outdir lib --target=node --sourcemap=linked --minify --watch",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "yarn shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "13.5.0",
    "firebase-functions": "6.5.0",
  },
  "private": true
}

libのpackage.json

{
  "name": "functions",
  "type": "module",
  "main": "./index.js",
  "engines": {
    "node": "22"
  }
}

問題発生

例えば、以下のようなコードがあるとする。

initializeApp()
const db = getFirestore()

export const onCommentReplyDeleted = functions.region("asia-northeast1")
  .firestore.document("/entries/{entryId}/comments/{commentId}")
  .onDelete(async (snapshot) => {
    await db.listCollections()
  })

デプロイしてトリガーしてみると、以下のような訳のわからないエラーが出て失敗する。

Error: undefined undefined: undefined
    at aM9 (file:///workspace/index.js:962:69746)
    at Object.onReceiveStatus (file:///workspace/index.js:966:5099)
    at Object.onReceiveStatus (file:///workspace/index.js:964:9174)
    at file:///workspace/index.js:970:23906
    at process.processTicksAndRejections (node:internal/process/task_queues:85:11)
for call at
    at V.makeServerStreamRequest (file:///workspace/index.js:966:4861)
    at V.<anonymous> (file:///workspace/index.js:966:7184)
    at file:///workspace/index.js:1123:161212
    at file:///workspace/index.js:614:110222
    at file:///workspace/index.js:614:50128
    at Object.request (file:///workspace/index.js:614:107261)
    at Timeout.b [as _onTimeout] (file:///workspace/index.js:614:103630)
    at listOnTimeout (node:internal/timers:588:17)
    at process.processTimers (node:internal/timers:523:7)
Caused by: Error
    at wJ0._getResponse (file:///workspace/index.js:962:4294)
    at wJ0._get (file:///workspace/index.js:962:4191)
    at file:///workspace/index.js:962:4142
    at file:///workspace/index.js:1123:174697
    at ZR6.with (file:///workspace/index.js:1123:14099)
    at AG0.with (file:///workspace/index.js:1123:14674)
    at SR6.startActiveSpan (file:///workspace/index.js:1123:17932)
    at pR6.startActiveSpan (file:///workspace/index.js:1123:18445)
    at yL6.startActiveSpan (file:///workspace/index.js:1123:174620)
    at wJ0.get (file:///workspace/index.js:962:4055)

原因と対処法

Firestore関連のAPI呼び出しは、内部的にはデフォルトで、HTTP/2以上が必須なgRPCが使用される。Bunでコードを1つのファイルにまとめると、gRPC関連のライブラリが壊れ、エラーが発生している模様。

そのため、以下のようにしてREST APIが使用されるようにFirestoreを設定してやると解決する。

const db = initializeFirestore(initializeApp(), {
  preferRest: true,
})

db.listCollections()

firebase-adminv13.5.0以上を使用すれば、initializeApp()は複数回呼び出してもエラーとならない。

参考

https://github.com/oven-sh/bun/issues/19601