我正在尝试使用nodejs的crypto模块并将所有必需的模块添加到我的云文件夹中并根据需要更改所有require('..')方法('cloud / ..)但在部署到云时遇到错误:< / p>
更新失败,无法加载触发器。错误是错误:模块cloud / stream.js已被加载
at _stream_readable.js:26:14 在stream.js:28:19 在crypto.js:40:14 在main.js:3:14 因为stream_readable.js需要stream.js,所以stream.js和stream_readable.js之间存在循环依赖关系。
此循环依赖项是否有任何解决方案或解决方法?
答案 0 :(得分:0)
如文档中所示 https://www.parse.com/docs/cloud_code_guide#modules
使用自己的模块时需要使用导出
var coolNames = ['Ralph', 'Skippy', 'Chip', 'Ned', 'Scooter'];
exports.isACoolName = function(name) {
return coolNames.indexOf(name) !== -1;
}
var name = require('cloud/name.js');
name.isACoolName('Fred'); // returns false
name.isACoolName('Skippy'); // returns true;
name.coolNames; // undefined.
答案 1 :(得分:0)
我也坚持了几个星期。事实证明,NodeJS不是Parse Cloud Code签名/验证的最佳选择(但无论如何),但我确实找到了解决方案。
根据Chris Stewart的回复here,您可以使用jsrsasign库代替。
这是一个工作示例的云代码:
Parse.Cloud.define("verifySignature", function(request, response) {
var KJUR = require('cloud/jsrsasign/npm/lib/jsrsasign.js');
var strPubKey = '-----BEGIN PUBLIC KEY-----(snip)';
// initialize
var pubKey = KJUR.KEYUTIL.getKey(strPubKey);
var data = request.params.data;
var signature = request.params.signature;
var verifier = new KJUR.Signature({"alg": "SHA1withRSA", "prov": "cryptojs/jsrsa"});
// initialize for signature validation
verifier.initVerifyByPublicKey(pubKey);
// update data
verifier.updateString(data);
// verify signature
if (verifier.verify(base64ToHex(signature))
response.success("Signature verified");
else
response.error("Signature could not be verified");
}
function base64ToHex(str) {
for (var i = 0, bin = atob(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
var tmp = bin.charCodeAt(i).toString(16);
if (tmp.length === 1) tmp = "0" + tmp;
hex[hex.length] = tmp;
}
return hex.join(" ");
}
function atob(base64) {
var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var table = tableStr.split("");
if (/(=[^=]+|={3,})$/.test(base64)) throw new Error("String contains an invalid character");
base64 = base64.replace(/=/g, "");
var n = base64.length & 3;
if (n === 1) throw new Error("String contains an invalid character");
for (var i = 0, j = 0, len = base64.length / 4, bin = []; i < len; ++i) {
var a = tableStr.indexOf(base64[j++] || "A"), b = tableStr.indexOf(base64[j++] || "A");
var c = tableStr.indexOf(base64[j++] || "A"), d = tableStr.indexOf(base64[j++] || "A");
if ((a | b | c | d) < 0) throw new Error("String contains an invalid character");
bin[bin.length] = ((a << 2) | (b >> 4)) & 255;
bin[bin.length] = ((b << 4) | (c >> 2)) & 255;
bin[bin.length] = ((c << 6) | d) & 255;
};
return String.fromCharCode.apply(null, bin).substr(0, bin.length + n - 4);
}