如何在PC上本地测试Firebase的云功能

Inz*_*lik 33 function node.js firebase google-cloud-functions firebase-cloud-functions

今天,Firebase发布了全新的Cloud Products for Firebase产品,我刚创建了一个hello world功能,并将其部署在我现有的firebase项目中.

看起来它捆绑所有依赖项并将其上传到firebase就像aws lambda函数一样.但即使在代码的微小变化上也需要太多时间才能完成,并且还需要良好的互联网连接.如果由于某种原因您处于脱机状态,那么在您可以在本地计算机上执行和测试该功能的方法之前,您所处的代码就处于黑暗状态.

有没有办法在本地测试Firebase的云功能?

Fra*_*len 23

这里有一个firebaser

部署您的函数确实比我通常愿意等待的时间更长.我们正在努力改进这一点,并且(正如Brendan所说)正在开发一个本地模拟器.

但目前,我主要是将我的实际业务逻辑写入单独的Node脚本中.这样我就可以在本地命令提示符下测试它node speech.js.一旦我对该函数的工作感到满意,我就将其复制/粘贴到我的实际函数文件中,或者(更好)将speech模块导入到我的函数文件中并从那里调用它.

我快速挖掘的一个简短示例是我使用Cloud Vision API连接文本提取的时候.我有一个名为的文件ocr.js包含:

var fetch = require('node-fetch');

function extract_text(url, gcloud_authorization) {
  console.log('extract_text from image '+url+' with authorization '+gcloud_authorization);

  return fetch(url).then(function(res) {
    return res.buffer();
  }).then(function(buffer) {
    return fetch('https://vision.googleapis.com/v1/images:annotate?key='+gcloud_authorization, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        "requests":[
          {
            "image":{
              "content": buffer.toString('base64')
            },
            "features":[
              {
                "type":"TEXT_DETECTION",
                "maxResults":1
              }
            ]
          }
        ]
      })
    });
  }).then(function(res) {
    var json = res.json();
    if (res.status >= 200 && res.status < 300) {
      return json;
    } else {
      return json.then(Promise.reject.bind(Promise));
    }
  }).then(function(json) {
    if (json.responses && json.responses.length && json.responses[0].error) {
      return Promise.reject(json.responses[0].error);
    }
    return json.responses[0].textAnnotations[0].description;
  });
}

if (process.argv.length > 2) {
  // by passing the image URL and gcloud access token, you can test this module
  process.argv.forEach(a => console.log(a));
  extract_text(
    process.argv[2], // image URL
    process.argv[3]  // gcloud access token or API key
  ).then(function(description) {
    console.log(description);
  }).catch(function(error) {
    console.error(error);
  });
}

exports.extract_text = extract_text;
Run Code Online (Sandbox Code Playgroud)

然后在我的函数index.js中,我有:

var functions = require('firebase-functions');
var fetch = require('node-fetch');
var ocr = require('./ocr.js');

exports.ocr = functions.database().path('/messages/{room}/{id}').onWrite(function(event) {
  console.log('OCR triggered for /messages/'+event.params.room+'/'+event.params.id);

  if (!event.data || !event.data.exists()) return;
  if (event.data.ocr) return;
  if (event.data.val().text.indexOf("https://firebasestorage.googleapis.com/") !== 0) return; // only OCR images

  console.log(JSON.stringify(functions.env));

  return ocr.extract_text(event.data.val().text, functions.env.googlecloud.apikey).then(function(text) {
    return event.data.adminRef.update({ ocr: text });
  });
});
Run Code Online (Sandbox Code Playgroud)

因此,您可以看到最后一个文件实际上只是将"工作方法" ocr.extract_text连接到数据库位置.

请注意,这是一个不久前的项目,所以一些语法(主要是functions.env部分)可能会有所改变.


小智 17

这里有一个firebaser

要在本地调试Firebase的云功能,可以使用模拟器.有关详细信息,请参阅文档.

  • 适用于https功能的Alpha版firebase模拟器:https://firebase.google.com/docs/functions/local-emulator (3认同)
  • @BrianLenoski 似乎调试选项消失了?您提供的链接显示了如何在本地运行,但没有显示如何调试。 (2认同)

Inz*_*lik 8

在本地运行功能

https://firebase.google.com/docs/functions/local-emulator

要使用此功能,firebase-tools必须具有最低版本3.8.0,而firebase-functions SDK必须具有最低版本0.5.7.要更新它们,请在项目的functions /目录中运行以下命令:

npm install -g @google-cloud/functions-emulator
npm install --save firebase-functions
npm install -g firebase-tools
Run Code Online (Sandbox Code Playgroud)

要在本地运行函数,请使用firebase服务:

firebase serve --only functions
functions inspect myFn
functions call myFn # or call from browser
Run Code Online (Sandbox Code Playgroud)

警告:实验性功能.这是一项实验性功能,目前仅支持HTTPS功能的模拟.

更新:

嘿功能可信赖的测试人员,

我们刚刚发布了firebase-tools v3.11.0,它支持一个交互式shell,用于模拟所有类型的函数,并使用测试数据调用它们.感谢您的许多人参与此功能的反馈会议.

请参阅我们的文档,了解如何使用这个令人兴奋的新功能!

https://firebase.google.com/docs/functions/local-emulator#use_the_cloud_functions_shell