NodeJS 将Firebase功能切换至Gen-2

ego6inou  于 2023-01-04  发布在  Node.js
关注(0)|答案(2)|浏览(230)

我刚刚看到我们的第二代云函数,看起来非常棒:https://cloud.google.com/functions/docs/2nd-gen/overview
但是我如何将第一代函数切换为第二代?我看到我可以create a new function as 2nd gen like this

const functions = require('@google-cloud/functions-framework');

functions.http('helloHttp', (req, res) => {
 res.send(`Hello ${req.query.name || req.body.name || 'World'}!`);
});

但是旧的函数怎么办?有没有办法或者我必须删除它们,然后逐个重新创建它们?

oxf4rvwz

oxf4rvwz1#

Cloud Function Gen2仅适用于产品Cloud函数,不适用于Firebase函数。
Firebase函数使用firebase-functions库,而Cloud函数使用@google-cloud/functions-framework库。
在Google Cloud控制台中,这两个函数看起来相同,但如果您尝试在Cloud Function gen1上部署Cloud Function gen2,则会收到以下错误:
Failed to create function, function projects/[PROJECT_NUMBER]/locations/[LOCATION]/functions/[FUNCTION_NAME] already exists under 'Gen 1' environment. Please delete conflicting function first or deploy the function with a different name.
您需要从Firebase函数完全迁移到全新创建的Cloud Functions gen2。

9q78igpj

9q78igpj2#

从2022年8月9日Cloud Functions 2nd Gen become Generally Available开始,事情可能已经发生了变化,所以我将记录在TypeScript项目中对我起作用的内容。

第一代

客户

"火线":"9.9.3"

import { httpsCallable } from "firebase/functions";
import { AddTwoNumbersInputParams, AddTwoNumbersInputResult } from "./util/shared/my-types";

// ...

const addTwoNumbersFuncCallable = httpsCallable<AddTwoNumbersInputParams, AddTwoNumbersInputResult>(
    firebaseFunctions,
    "addTwoNumbersFunc",
);

const result = await addTwoNumbersFuncCallable({
    firstNum: 3,
    secondNum: 5
});

console.log("Result", result.data);

服务器

" Firebase 功能":"^3.21.0"

import * as functions from "firebase-functions";
import { AddTwoNumbersInputParams, AddTwoNumbersInputResult } from "./util/shared/my-types";

// ...

export const addTwoNumbersFunc = functions.https.runWith({ memory: "1GB" }).onCall((params: AddTwoNumbersInputParams, ctx): AddTwoNumbersInputResult => {
    
    if (!ctx.auth) {
        throw new functions.https.HttpsError("unauthenticated", "You must be logged in to call server-side functions");
    }

    return { result: params.firstNum + params.secondNum };
}

共享

为了在客户端和服务器代码之间共享TypeScript接口AddTwoNumbersInputParams和AddTwoNumbersInputResult,我创建了一个指向目录util/shared的符号链接,该目录在名为my-types.ts的文件中包含以下定义:

export interface AddTwoNumbersInputParams {
    firstNum: number
    secondNum: number
}

export interface AddTwoNumbersInputResult {
    result: number
}

第二代

客户

"火线":"9.9.3"

import { httpsCallableFromURL } from "firebase/functions"; // see note1, note2, note3 below 

// ...

const addTwoNumbersFuncCallable = httpsCallableFromURL<AddTwoNumbersInputParams, AddTwoNumbersInputResult>(
    firebaseFunctions,
    "https://addtwonumbersfunc-fri67ycgta-uc.a.run.app",  // see note1, note2, note3 below 
);

const result = await addTwoNumbersFuncCallable({
    firstNum: 3,
    secondNum: 5
});

console.log("Result", result.data);

注1:callable documentation和http-events文档都说firebase deploy命令应该输出URL,但我没有看到它。我通过以下路径获得了它:

  1. Firebase Console
    1.单击您的项目
    1.单击"Functions(功能)"(在左侧的"Table of Contents"(目录)中,但如果未看到,请单击"All products"(所有产品),然后单击"Functions"(功能))
    1.在"触发器"列中复制函数的URL;其格式应为https://<lowercase_func_name>-<random-hash>-<region>.a.run.app
    注二:起初我担心第二代函数会在我的持续集成管道中引入手动步骤,因为它们现在输出URL(大约it said)。有几个Firebase项目代表了要提升到生产的不同阶段(谷歌推荐),我想我会有一个新的麻烦,复制和粘贴的URL为第二代功能的每一个部署。幸运的是,它没有那么糟糕,因为我想" URL在部署后保持稳定"。所以我只需要部署一次就可以获得URL,将其插入到我的客户端代码中,然后它在此后的每个部署中都保持不变。也就是说,我的每个Firebase项目的URL仍然是不同的。所以我将不得不做更多的工作来提升到生产。但也许他们会解决这个问题,因为他们说"在未来的版本中,第二代函数URL将更新为稳定且具有确定性。"。
    注3:我发现URL的事情太麻烦了,所以我尝试不使用它,并在Firebase函数模拟器上取得了成功,但在真正的Firebase项目上却没有。使用模拟器,我几乎可以继续使用接受函数名的httpsCallable()函数,而不是需要URL的httpsCallableFromURL()函数。它在模拟器上工作,但在真正的Firebase项目上没有。

服务器

" Firebase 功能":"^3.21.0"

import * as functionsV2 from "firebase-functions/v2";
import {CallableRequest} from "firebase-functions/lib/common/providers/https";
import {HttpsOptions} from "firebase-functions/lib/v2/providers/https";

// ... 

const httpsOptions: HttpsOptions = {
    memory: "16GiB"  // see note4 below
};

// see note5 below
export const addtwonumbersfunc = functionsV2.https.onCall(httpsOptions, (request: CallableRequest<AddTwoNumbersInputParams>): AddTwoNumbersInputResult => {
    if (!request.auth) {
        throw new functionsV2.https.HttpsError("unauthenticated", "You must be logged in to call server-side functions");
    }

    return { result: request.data.firstNum + request.data.secondNum };
});

注4:用于设置内存(等)的runWith()语法似乎已更改为接受HttpsOptionsHttpsOptions object that you pass to the onCall(),如memory。第二代的一个令人兴奋的地方是它提供了比第一代更高的内存分配,因此我在这里演示了这一点,但从"1GB"增加到"16GiB"(另请注意从"GB"到"GiB"的更改)。
注5:"函数名仅限于小写字母、数字和破折号。"但希望很快会有"支持在函数名中使用大写字母"。

共享

无需变更

相关问题