Closed9

Cloud FunctionsでHonoを試す

aipacommanderaipacommander
$ docker run --rm -p 81:80 -v $(pwd):/app -w /app -it node:lts-slim /bin/bash

以下、コンテナで試す

$ npm create hono@latest my-app
Need to install the following packages:
create-hono@0.3.1
Ok to proceed? (y) y

create-hono version 0.3.1
✔ Using target directory … my-app
✔ Which template do you want to use? › nodejs
/root/.npm/_npx/6bbb1d6f54609fb5/node_modules/create-hono/bin:10787
          throw new DegitError(`could not find commit hash for ${repo.ref}`, {
                ^

DegitError: could not find commit hash for main
    at Degit._cloneWithTar (/root/.npm/_npx/6bbb1d6f54609fb5/node_modules/create-hono/bin:10787:17)
    at async Degit.clone (/root/.npm/_npx/6bbb1d6f54609fb5/node_modules/create-hono/bin:10646:11) {
  code: 'MISSING_REF',
  ref: 'main'
}

Node.js v18.18.2
npm notice
npm notice New major version of npm available! 9.8.1 -> 10.2.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.2.0
npm notice Run npm install -g npm@10.2.0 to update!
npm notice
npm ERR! code 1
npm ERR! path /app
npm ERR! command failed
npm ERR! command sh -c create-hono my-app

npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2023-10-17T14_59_23_249Z-debug-0.log

よくわからんが、色々漁ったところ、gitコマンドがないんじゃなかろうかということで試した

$ apt update -y && apt install -y git
$ npm create hono@latest my-app

create-hono version 0.3.1
✔ Using target directory … my-app
✔ Which template do you want to use? › nodejs
cloned honojs/starter#main to /app/my-app
✔ Copied project files

お、でけた

aipacommanderaipacommander
$ npm install -g firebase-tools
$ firebase login --no-localhost
$ firebase init

これで、functionsだけ選択して用意

aipacommanderaipacommander

https://hono.dev/getting-started/aws-lambda

https://github.com/honojs/hono/blob/main/src/adapter/aws-lambda/handler.ts#L111

honoのawsのadapter?を参考に、Cloud Functionsで動作するように組んでみる(多分これで動くはず)

/**
 * Import function triggers from their respective submodules:
 *
 * import {onCall} from "firebase-functions/v2/https";
 * import {onDocumentWritten} from "firebase-functions/v2/firestore";
 *
 * See a full list of supported triggers at https://firebase.google.com/docs/functions
 */

import {onRequest} from "firebase-functions/v2/https"
import * as logger from "firebase-functions/logger"
import { Hono } from 'hono'

// Start writing functions
// https://firebase.google.com/docs/functions/typescript

const app = new Hono()
app.get('/', (c) => c.text('Hello Hono!'))
export const helloWorld = onRequest(async (request, response) => {
  const res = await app.fetch(request)
  logger.info(res)
  response.send("Hello from Firebase!")
});
aipacommanderaipacommander
  1. Cloud FunctionsでonRequest(Request, ...)で用意しているRequestをHono用にするために、Cloud Functions用のcreateRequestを用意する
  2. createRequestは new Request(url, requestInit) を返すので、urlとrequestInitを作る

なんだけど、Cloud FunctionsでもらうRequestの構造がよくわからないので今積んでいる

aipacommanderaipacommander

urlは適当な文字列 '/' だとエラーになることがわかって、今回は就寝

aipacommanderaipacommander

思ったけど、expressが遅いから高速化しているはずなのに、expressに乗っちゃったら駄目なのでは?

というわけでclose

このスクラップは2023/12/02にクローズされました