将类部署为Firebase云函数的正确方法是什么



我想将一个类公开为Firebase云函数。下面是我的index.js:

"use strict";
const functions = require('firebase-functions');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });
var crypto = require('crypto');
class PaytmChecksum {
static encrypt(input, key) {
var cipher = crypto.createCipheriv('AES-128-CBC', key, PaytmChecksum.iv);
var encrypted = cipher.update(input, 'binary', 'base64');
encrypted += cipher.final('base64');
return encrypted;
}
static decrypt(encrypted, key) {
var decipher = crypto.createDecipheriv('AES-128-CBC', key, PaytmChecksum.iv);
var decrypted = decipher.update(encrypted, 'base64', 'binary');
try {
decrypted += decipher.final('binary');
}
catch (e) {
console.log(e);
}
return decrypted;
}
static generateSignature(params, key) {
if (typeof params !== "object" && typeof params !== "string") {
var error = "string or object expected, " + (typeof params) + " given.";
return Promise.reject(error);
}
if (typeof params !== "string"){
params = PaytmChecksum.getStringByParams(params);
}
return PaytmChecksum.generateSignatureByString(params, key);
}

static verifySignature(params, key, checksum) {
if (typeof params !== "object" && typeof params !== "string") {
var error = "string or object expected, " + (typeof params) + " given.";
return Promise.reject(error);
}
if(params.hasOwnProperty("CHECKSUMHASH")){
delete params.CHECKSUMHASH
}
if (typeof params !== "string"){
params = PaytmChecksum.getStringByParams(params);
}
return PaytmChecksum.verifySignatureByString(params, key, checksum);
}
static async generateSignatureByString(params, key) {
var salt = await PaytmChecksum.generateRandomString(4);
return PaytmChecksum.calculateChecksum(params, key, salt);
}
static verifySignatureByString(params, key, checksum) {
var paytm_hash = PaytmChecksum.decrypt(checksum, key);
var salt = paytm_hash.substr(paytm_hash.length - 4);
return (paytm_hash === PaytmChecksum.calculateHash(params, salt));
}
static generateRandomString(length) {
return new Promise((resolve, reject) => {
crypto.randomBytes((length * 3.0) / 4.0, (err, buf) => {
if (!err) {
var salt = buf.toString("base64");
resolve(salt);
}
else {
console.log("error occurred in generateRandomString: " + err);
reject(err);
}
});
});
}
static getStringByParams(params) {
var data = {};
Object.keys(params).sort().forEach((key,value) => {
data[key] = (params[key] !== null && params[key].toLowerCase() !== "null") ? params[key] : "";
});
return Object.values(data).join('|');
}
static calculateHash(params, salt) {
var finalString = params + "|" + salt;
return crypto.createHash('sha256').update(finalString).digest('hex') + salt;
}
static calculateChecksum(params, key, salt) {
var hashString = PaytmChecksum.calculateHash(params, salt);
return PaytmChecksum.encrypt(hashString,key);
}
}
PaytmChecksum.iv = '@@@@&&&&####$$$$';
modules.exports = PaytmChecksum;

当我部署这个功能时,它说部署成功,但我在Firebase控制台中没有看到这个功能。以下是部署命令的输出:

sr@SR:~/AndroidStudioProjects/BuddayWale/functions$ sudo firebase deploy --only functions
=== Deploying to 'buddaywale-b1923'...
i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint /home/sr/AndroidStudioProjects/BuddayWale/functions
> eslint .
✔  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
⚠  functions: The Node.js 8 runtime is deprecated and will be decommissioned on 2021-03-15. For more information, see: https://firebase.google.com/support/faq#functions-runtime
✔  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
✔  Deploy complete!
Project Console: https://console.firebase.google.com/project/buddaywale-b1923/overview

我错过了什么?我做错了什么?部署云功能的正确方式是什么?

您还可以使用express.js框架

以下是的示例

const express = require('express');
const app = express();
class PaytmChecksum{
//code
}
class PaytmChecksum1{
//code
}
app.post('/name',(req,res)=>{
let paymentChecksum = new PaytmChecksum();
let result = paymentChecksum. //your method
res.send(result);
})
app.post('/another/name',(req,res)=>{
let paymentChecksum1 = new PaytmChecksum1();
let result = paymentChecksum1. //your method
res.send(result);
})
exports.app = functions.https.onRequest(app);

这是单据。https://firebase.google.com/docs/hosting/functions

虽然您可以使用云函数代码中的类,但为了使Firebase能够连接您的函数,必须将它们声明为functions.,然后声明为触发器类型。

乍一看,我没有在您共享的代码中看到任何云函数声明(exports.helloWorld = functions.... => {(,所以这可以解释为什么您没有在云函数控制台中看到任何代码

在云函数中使用类的一种简单方法可以是:

exports.helloWorld = functions.https.onRequest((request, response) => {
let paymentChecksum = new PaytmChecksum();
let result = paymentChecksum.encrypt("...", "...")
response.send(result);
});

有关此方面的更多信息,包括触发代码的各种方式,请参阅代码中文档的链接:https://firebase.google.com/docs/functions/write-firebase-functions

相关内容

  • 没有找到相关文章