[英]how to test Cloud Functions for Firebase locally on pc
今天 Firebase 發布了其全新的 Firebase 產品Cloud Functions ,我剛剛創建了一個 hello world 函數並將其部署到我現有的Firebase項目中。
看起來它捆綁了所有依賴項並將其上傳到 firebase,就像aws lambda 函數一樣。 但是即使是對代碼進行微小更改也需要花費太多時間,並且還需要良好的互聯網連接。 如果您由於某種原因離線,那么在您有辦法在本地機器上離線執行和測試該功能之前,您對正在編寫的代碼一無所知。
有沒有辦法在本地測試 Cloud Functions for Firebase?
火力士在這里
部署您的功能確實比我通常願意等待的時間更長。 我們正在努力改進這一點,並且(正如 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;
然后在我的函數 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 });
});
});
因此,正如您所看到的,最后一個文件實際上只是將“工作方法” ocr.extract_text
連接到數據庫位置。
請注意,這是一個前一段時間的項目,因此某些語法(主要是functions.env
部分)可能會發生一些變化。
在本地運行和調試/檢查功能
先決條件(谷歌雲功能和特定於火力的):
npm install -g @google-cloud/functions-emulator
npm install --save firebase-functions
npm install -g firebase-tools
運行和檢查/調試:首先在本地運行函數,然后檢查每個函數,最后運行每個特定函數以調試+檢查它。 使用functions start
作為firebase serve
的替代方案,並注意每個工具的文檔都可用(且有用)。
要按預期運行和調試特定功能myFn
(例如,在 Nodejs 中通過chrome://inspect
並注意這使用 Nodejs v10 工作,但不受官方支持):
firebase serve --only functions
functions inspect myFn
functions call myFn # or call from browser
附加文件:
https://firebase.google.com/docs/functions/local-emulatorhttps://cloud.google.com/functions/docs/emulator#debug-emulator https://github.com/GoogleCloudPlatform/cloud-functions-模擬器/維基
>> 有沒有辦法在本地測試 Cloud Functions for Firebase?
您可以使用以下命令啟動 firebase shell(在您的函數目錄中執行):
npm run build && firebase functions:shell
您可以像這樣在 shell 中調用您的函數:
helloWorld()
有關更多信息,請參閱此鏈接。
在這里回答: https : //github.com/firebase/firebase-functions/issues/4#issuecomment-286515989
Google Cloud Functions 還開源了一個本地模擬器,我們正在努力與 Cloud Functions for Firebase 建立更緊密的集成。 同時,您可以在此處查看: https : //github.com/GoogleCloudPlatform/cloud-functions-emulator/
模擬器確實允許您在本地運行函數。 這是解釋如何使用它的文檔: https : //cloud.google.com/functions/docs/emulator
一開始我無法讓單步工作。 我的過程與此處的許多答案中記錄的過程相同。
此外,這些頁面幾乎包含我需要的所有文檔:
我已經使用firebase serve --only functions
functions 運行了firebase serve --only functions
,但沒有啟動並運行調試器。 然后我遇到了直接使用模擬器的另一種方式,並設法達到了這樣的斷點:
# start the emulator
functions start
# allow inspection
functions inspect helloWorld
# call the function from the cli
functions call helloWorld
這行得通,我可以遇到斷點。
但是,當在郵遞員或瀏覽器中點擊該功能的端點時,我根本沒有收到任何響應。
我缺少的步驟是:
# deploy the function to the emulator
functions deploy helloWorld --trigger-http
# you need to toggle inspection after the deploy
functions inspect helloWorld
現在我可以從郵遞員或瀏覽器點擊函數的端點,並且斷點被擊中。
我推薦用於調試的出色NiM chrome 擴展,並希望這個答案對某人有所幫助,即使這是一個老問題。
首先,我建議您安裝以下依賴項,
npm install --save firebase-functions
npm install -g firebase-tools
如果已經安裝,那么您可以將其更新為最新版本。 通常,functions-emulator 帶有上述依賴項,但我仍然建議您更新它,
npm install -g @google-cloud/functions-emulator
更新后,轉到應用程序的功能文件夾並運行以下命令,
firebase serve --only functions
我希望它有幫助!
對於 vscode 用戶調試 HTTP 函數(webhooks 等)...
谷歌雲模擬器( firebase serve --only functions
)啟動一個單獨的進程來運行你的函數。 您可以使用 vscode 附加到此進程,但由於模擬器僅在調用第一個函數后才創建此進程,因此並不簡單。
app.get("/processid", function(request, response) {
response.send(`${process.pid}`);
});
firebase serve --only functions
啟動模擬器http://<localhost_url>/processid
端點。 這將創建進程並返回進程ID可能有更好的方法將所有這些粘合在一起。
現在有一個雲函數模擬器,可讓您在本地調用函數
完成 PoC 后,我將更新此答案以包含我使用的代碼和步驟。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.