Parse Cloud : Module already being loaded - javascript
I am trying to use crypto module of nodejs and added all required modules into my cloud folder and changed all require('..') methods as require('cloud/..) but getting error while deploying to cloud :
Update failed with Could not load triggers. The error was Error: Module cloud/stream.js is already being loaded
at _stream_readable.js:26:14
at stream.js:28:19
at crypto.js:40:14
at main.js:3:14
Because stream_readable.js requires stream.js and there is a circular dependency between stream.js and stream_readable.js.
Is there any solution or workaround for this circular dependencies?
As show in the Documentation
https://www.parse.com/docs/cloud_code_guide#modules
you need to use exports when using your own 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.
I was stuck on this for a couple weeks as well. Turns out NodeJS isn't the best option (yet, anyways) for Parse Cloud Code signature signing/verification but I did find a solution.
According to Chris Stewart's response here, you can use the jsrsasign library instead.
Here's some cloud code of a working example:
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);
}
Related
Reverse a string using two-pointer method in JS
I am trying to reverse a string. I am aware of .reverse function and other methods in Js to do so, but i wanted to do it this two-pointer method. The problem is the string is not getting updated. Is there anything i am not aware of strings. Whats wrong here ? function reverseString(s) { let lengthOfStr = 0; if ((s.length - 1) % 2 == 0) { lengthOfStr = (s.length - 1) / 2 } else { lengthOfStr = ((s.length - 1) / 2) + 1; } let strLengthLast = s.length - 1; for (let i = 0; i <= lengthOfStr; i++) { let pt1 = s[i]; let pt2 = s[strLengthLast]; s[i] = pt2; s[strLengthLast] = pt1; console.log('----', s[i], s[strLengthLast]); strLengthLast--; } return s; } console.log(reverseString('hello'));
Unlike in C, strings in JavaScript are immutable, so you can't update them by indexing into them. Example: let s = 'abc'; s[1] = 'd'; console.log(s); // prints abc, not adc You'd need to do something more long-winded in place of s[i] = pt2;, like s = s.substring(0, i) + pt2 + s.substring(i + 1);, and similarly for s[strLengthLast] = pt1; (or combine them into one expression with 3 calls to substring).
I'm not sure why it doesnt update the string, but if you handle the replacement as an array/list it works as follows: function reverseString(s) { let lengthOfStr = 0; sSplit = s.split(""); if ((s.length - 1) % 2 === 0) { lengthOfStr = (s.length - 1) / 2 } else { lengthOfStr = ((s.length - 1) / 2) + 1; } let strLengthLast = s.length - 1; for (let i = 0; i <= lengthOfStr; i++) { let pt1 = sSplit[i]; let pt2 = sSplit[strLengthLast]; sSplit[i] = pt2; sSplit[strLengthLast] = pt1; console.log('----', sSplit[i], sSplit[strLengthLast],sSplit); strLengthLast--; } return sSplit.join(""); } console.log(reverseString('Hello')); returns: Hello => olleH
As covered in comment, answers and documentation, strings are immutable in JavaScript. The ability to apparently assign a property value to a primitive string value results from early JavaScript engine design that temporarily created a String object from primitive strings when calling a String.prototype method on the primitive. While assigning a property to the temporary object didn't error, it was useless since the object was discarded between calling the String method and resuming execution of user code. The good news is that this has been fixed. Putting "use strict"; at the beginning of a JavaScript file or function body causes the compiler to generate a syntax error that primitive string "properties" are read-only. There are many ways of writing a function to reverse strings without calling String.prototype.reverse. Here's another example function strReverse(str) { "use strict"; let rev = []; for( let i = str.length; i--;) { rev.push(str[i]); } return rev.join(''); } console.log( strReverse("Yellow") ); console.log( strReverse("").length);
I tried that way, hopefully might be helpful for someone. const input = 'hello'; /*input data*/ const inputArray = [...input]; /*convert input data to char array*/ function reverseString(inputArray) { let i = 0; let j = inputArray.length -1 ; while(i < j ) { const temp = inputArray[i]; inputArray[i] = inputArray[j]; inputArray[j] = temp; i++; j--; } }; reverseString(inputArray); console.log(inputArray) const finalResult = inputArray.join(""); console.log(finalResult); Thanks.
Scrambling and descrambling javascript array with salt
So i made a javascript string scrambler with md5. var MD5 = function(d){var r = M(V(Y(X(d),8*d.length)));return r.toLowerCase()};function M(d){for(var _,m="0123456789ABCDEF",f="",r=0;r<d.length;r++)_=d.charCodeAt(r),f+=m.charAt(_>>>4&15)+m.charAt(15&_);return f}function X(d){for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<<m%32;return _}function V(d){for(var _="",m=0;m<32*d.length;m+=8)_+=String.fromCharCode(d[m>>5]>>>m%32&255);return _}function Y(d,_){d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n<d.length;n+=16){var h=m,t=f,g=r,e=i;f=md5_ii(f=md5_ii(f=md5_ii(f=md5_ii(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_ff(f=md5_ff(f=md5_ff(f=md5_ff(f,r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+0],7,-680876936),f,r,d[n+1],12,-389564586),m,f,d[n+2],17,606105819),i,m,d[n+3],22,-1044525330),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+4],7,-176418897),f,r,d[n+5],12,1200080426),m,f,d[n+6],17,-1473231341),i,m,d[n+7],22,-45705983),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+8],7,1770035416),f,r,d[n+9],12,-1958414417),m,f,d[n+10],17,-42063),i,m,d[n+11],22,-1990404162),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+12],7,1804603682),f,r,d[n+13],12,-40341101),m,f,d[n+14],17,-1502002290),i,m,d[n+15],22,1236535329),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+1],5,-165796510),f,r,d[n+6],9,-1069501632),m,f,d[n+11],14,643717713),i,m,d[n+0],20,-373897302),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+5],5,-701558691),f,r,d[n+10],9,38016083),m,f,d[n+15],14,-660478335),i,m,d[n+4],20,-405537848),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+9],5,568446438),f,r,d[n+14],9,-1019803690),m,f,d[n+3],14,-187363961),i,m,d[n+8],20,1163531501),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+13],5,-1444681467),f,r,d[n+2],9,-51403784),m,f,d[n+7],14,1735328473),i,m,d[n+12],20,-1926607734),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+5],4,-378558),f,r,d[n+8],11,-2022574463),m,f,d[n+11],16,1839030562),i,m,d[n+14],23,-35309556),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+1],4,-1530992060),f,r,d[n+4],11,1272893353),m,f,d[n+7],16,-155497632),i,m,d[n+10],23,-1094730640),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+13],4,681279174),f,r,d[n+0],11,-358537222),m,f,d[n+3],16,-722521979),i,m,d[n+6],23,76029189),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+9],4,-640364487),f,r,d[n+12],11,-421815835),m,f,d[n+15],16,530742520),i,m,d[n+2],23,-995338651),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+0],6,-198630844),f,r,d[n+7],10,1126891415),m,f,d[n+14],15,-1416354905),i,m,d[n+5],21,-57434055),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+12],6,1700485571),f,r,d[n+3],10,-1894986606),m,f,d[n+10],15,-1051523),i,m,d[n+1],21,-2054922799),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+8],6,1873313359),f,r,d[n+15],10,-30611744),m,f,d[n+6],15,-1560198380),i,m,d[n+13],21,1309151649),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+4],6,-145523070),f,r,d[n+11],10,-1120210379),m,f,d[n+2],15,718787259),i,m,d[n+9],21,-343485551),m=safe_add(m,h),f=safe_add(f,t),r=safe_add(r,g),i=safe_add(i,e)}return Array(m,f,r,i)}function md5_cmn(d,_,m,f,r,i){return safe_add(bit_rol(safe_add(safe_add(_,d),safe_add(f,i)),r),m)}function md5_ff(d,_,m,f,r,i,n){return md5_cmn(_&m|~_&f,d,_,r,i,n)}function md5_gg(d,_,m,f,r,i,n){return md5_cmn(_&f|m&~f,d,_,r,i,n)}function md5_hh(d,_,m,f,r,i,n){return md5_cmn(_^m^f,d,_,r,i,n)}function md5_ii(d,_,m,f,r,i,n){return md5_cmn(m^(_|~f),d,_,r,i,n)}function safe_add(d,_){var m=(65535&d)+(65535&_);return(d>>16)+(_>>16)+(m>>16)<<16|65535&m}function bit_rol(d,_){return d<<_|d>>>32-_} var hex = { a: 10, b: 11, c: 13, d: 14, e: 15, f: 16 } function scrambleText(text, salt) { if(text.length > 32) throw "No more than 32 chars."; var strs = text.split(""); var final = ""; var hash = MD5(salt); var hashlength = hash.length, i = 0; while(i < hashlength) { if(!strs.length) break; var num = (hex[hash[i]] || parseInt(hash[i])) * 2 % strs.length; final += strs[num]; strs.splice(num, 1); ++i; } return final } I can scramble strings but how do I unscramble the scrambled text assuming I know the salt? I tried an attempt but it's not doing a good job function unscrambleText(text, salt) { var strs = text.split(""); var hash = MD5(salt); var hashlength = hash.length, i = strs.length - 1, c = 0;; var final = []; while(i >= 0) { var num = (hex[hash[i]] || parseInt(hash[i])) * 2 % (strs.length - i); final[num] = strs[c]; final += strs[num]; strs.splice(num, 1); --i; ++c; } return final } Can you guys help me solve this problem? I have been trying for hours. I take the md5 of the string called "salt" then use it as a reference to scramble the string. I only use strings to simplify the problem. In my actual code, I have an array of image datas that I want to scramble, then be able to descramble it example of image
Here's a silly script to scramble/unscramble text. The hash function is from here: Simple (non-secure) hash function for JavaScript? Apparently, it's a JavaScript version of a Java hash function. The script takes the "salt", hashes it, and uses each character in the hash as an offset to shift the letters up or down the ascii table. So it's essentially ROT n where n is one of the values of the hashed salt. "Rotten" is probably a pretty good description of this scramble function :D. const hash = (str) => str.split("").reduce((hash, char) => { hash = (hash << 5) - hash + char.charCodeAt(0); return hash & hash; }, 0); const offset = (num, i) => parseInt(num.toString().charAt(i % num.toString().length)); const scramble = (st, sa, d = 1) => st .split("") .map((c, i) => String.fromCharCode(c.charCodeAt(0) + offset(sa, i) * d)) .join(""); const unscramble = (str, salt) => scramble(str, salt, -1); const salt = "This is my salt"; const str = "My text to 'scramble'."; const scram = scramble(str, hash(salt)); const unscram = unscramble(scram, hash(salt)); console.log("scrambled:", scram); console.log("unscrambled:", unscram);
Backpropagation in an Tensorflow.js Neural Network
When I have been attempting to implement this function tf.train.stg(learningRate).minimize(loss)into my code in order to conduct back-propagation. I have been getting multiple errors such The f passed in variableGrads(f) must be a function. How would I implement the function above into the code bellow successfully? and Why does this error even occur? Neural Network: var X = tf.tensor([[1,2,3], [4,5,6], [7,8,9], [10,11,12]]) var Y = tf.tensor([[0,0,0],[0,0,0], [1,1,1]]) var m = X.shape[0] var a0 = tf.zeros([1,3]) var y_hat = tf.zeros([1,3]) var parameters = { "Wax": tf.randomUniform([1,3]), "Waa": tf.randomUniform([3,3]), "ba": tf.zeros([1,3]), "Wya": tf.randomUniform([3,3]), "by": tf.zeros([1,3]) } function RNN_cell_Foward(xt, a_prev, parameters){ var Wax = parameters["Wax"] var Waa = parameters["Waa"] var ba = parameters["ba"] var a_next = tf.sigmoid(tf.add(tf.add(tf.matMul(xt, Wax), tf.matMul(a_prev , Waa)),ba)) return a_next } function RNN_FowardProp(X, a0, parameters){ var T_x = X.shape[0] var a_next = a0 var i = 1 var Wya = parameters["Wya"] var by = parameters["by"] var l = 1 for(; i <= T_x; i++){ var X_i = X.slice([i-1,0],[1,-1]) for(; l <= X.shape[1]; l++){ var xt = X_i.slice([0,l-1],[1,1]) var a_next = RNN_cell_Foward(xt, a_next, parameters) } var y_pred = tf.sigmoid((tf.add(tf.matMul(a_next, Wya), by))) l = 1 if (i == 1){ var y_pred1 = y_pred } else if (i == 2) { var y_pred2 = y_pred } else if (i == 3) { var y_pred3 = y_pred } } var y_predx = tf.concat([y_pred1, y_pred2, y_pred3]) return y_predx } const learningRate = 0.01; var optimizer = tf.train.sgd(learningRate); var model = RNN_FowardProp(X, a0, parameters) var loss = tf.losses.meanSquaredError(Y, model) for (let f = 0; f < 10; f++) { optimizer.minimize(loss) } This is a neural network for sentiment classification which has a many to one structure.
The error says it all: The f passed in variableGrads(f) must be a function optimizer.minimize is expecting a function as parameter and not a tensor. Since the code is trying to minimize the meanSquaredError, the argument of minimize can be a function that computes the meanSquaredError between the predicted value and the expected one. const loss = (pred, label) => pred.sub(label).square().mean(); for (let f = 0; f < 10; f++) { optimizer.minimize(() => tf.losses.meanSquaredError(Y, model)) } Does it solve the issue, not completely yet ? The error will change for something like: variableGrads() expects at least one of the input variables to be trainable What does it mean ? When the optimizer is used, it expects the function passed as argument to contains variables whose values will be updated to minimize the function output. Here is the changes to be made: var Y = tf.tensor([[0,0,0],[0,0,0], [1,1,1]]).variable() // a variable instead // var loss = tf.losses.meanSquaredError(Y, model) // computed below in the minimize function const learningRate = 0.01; var optimizer = tf.train.sgd(learningRate); var model = RNN_FowardProp(X, a0, parameters); const loss = (pred, label) => pred.sub(label).square().mean(); for (let f = 0; f < 10; f++) { optimizer.minimize(() => tf.losses.meanSquaredError(Y, model)) }
64bit bitmask and javascript
I am currently fighting with a javascript problem where I have a 62bit bitmask that should be used as filter. I used the snippet from here but I cant get it to work for some cases. How to do bitwise AND in javascript on variables that are longer than 32 bit? function testBitmask(fd, filterMask){ var a = fd; var b = filterMask; var w = 4294967296; // 2^32 var aHI = a / w; var aLO = a % w; var bHI = b / w; var bLO = b % w; var aAll = (aHI & bHI) * w; var bAll = (aLO & bLO); var retVal = (aAll + bAll) == filterMask; console.log("retVal:",retVal) return retVal; } I dont understand why testBitmask(2147483648,2147483648) returns false, thats for 2^31. 2^32 => true. 2^33 => true. bAll gets negative here so I assume an overflow of the 32bit int, ideas?
If in javascript all numbers are 64 bit floating point and there are no 64 bit integers, you can't expect to define a and b (or fd and filtermask) with that precision, without rounding errors. Try to define an object which encapsulates the 64bit integer type. As an example you can look at the js-ctype implementation made by Mozilla MDN: https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/js-ctypes_reference/Int64 and in particular https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Working_with_data#64-bit_integers Theirs Int64 and UInt64 objects don't provide any methods for performing arithmetic, but you can pull out the high and low 32-bit portions and do math on them, then join them back together. A simple code example, using typed arrays instead: bitMask = function(high = 0x0,low = 0x0) { this.bm = new Uint32Array(2); if (arguments.length === 0 ) { this.bm[0] = 0x0; this.bm[1] = 0x0; } else if (arguments.length === 2 && typeof arguments[0] === "number" && typeof arguments[1] === "number") { this.bm[0] = arguments[1]; this.bm[1] = arguments[0]; } this.bwAND = function(filter) { result = new bitMask(); result.bm[0] = this.bm[0] & filter.bm[0]; result.bm[1] = this.bm[1] & filter.bm[1]; return result; } this.bwOR = function(filter) { result = new bitMask(); result.bm[0] = this.bm[0] | filter.bm[0]; result.bm[1] = this.bm[1] | filter.bm[1]; return result; } this.bwXOR = function(filter) { result = new bitMask(); result.bm[0] = this.bm[0] ^ filter.bm[0]; result.bm[1] = this.bm[1] ^ filter.bm[1]; return result; } this.bwNOT = function() { result = new bitMask(); result.bm[0] = ~this.bm[0]; result.bm[1] = ~this.bm[1]; return result; } this.bwEQUALS = function(b){ return (this.bm[0] == b.bm[0]) && (this.bm[1] == b.bm[1]); } this.toString = function() { var zeroes = "00000000000000000000000000000000"; var strH = this.bm[1].toString(2); var zerH = zeroes.substr(0,32-strH.length); var strL = this.bm[0].toString(2); var zerL = zeroes.substr(0,32-strL.length); return zerH + strH + zerL + strL; } } You can use it like this: var a = new bitMask(0x0FEDCBA9,0xFF00FF00); var b = new bitMask(0x12345678,0x0000FFFF); var c = b.bwAND(a); var d = b.bwOR(a); var e = b.bwXOR(a); var f = b.bwNOT(); var g = b.bwEQUALS(a); Results: a = 0000111111101101110010111010100111111111000000001111111100000000 b = 0001001000110100010101100111100000000000000000001111111111111111 a & b = 0000001000100100010000100010100000000000000000001111111100000000 a | b = 0001111111111101110111111111100111111111000000001111111111111111 a ^ b = 0001110111011001100111011101000111111111000000000000000011111111 ~b = 1110110111001011101010011000011111111111111111110000000000000000 (a == b)? = false
Javascript fails to give correct output in IE
<script> // This would be the place to edit if you want a different // Base32 implementation var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345' /** * Build a lookup table and memoize it * * Return an object that maps a character to its * byte value. */ var lookup = function() { var table = {} // Invert 'alphabet' for (var i = 0; i < alphabet.length; i++) { table[alphabet[i]] = i } lookup = function() { return table } return table } // Functions analogously to Encoder function Decoder() { var skip = 0 // how many bits we have from the previous character var byte = 0 // current byte we're producing this.output = '' // Consume a character from the stream, store // the output in this.output. As before, better // to use update(). this.readChar = function(char) { if (typeof char != 'string'){ if (typeof char == 'number') { char = String.fromCharCode(char) } } //char = char.toLowerCase() var val = lookup()[char] if (typeof val == 'undefined') { // character does not exist in our lookup table return // skip silently. An alternative would be: // throw Error('Could not find character "' + char + '" in lookup table.') } val <<= 3 // move to the high bits byte |= val >>> skip skip += 5 if (skip >= 8) { // we have enough to preduce output this.output += String.fromCharCode(byte) skip -= 8 if (skip > 0) byte = (val << (5 - skip)) & 255 else byte = 0 } } this.finish = function(check) { var output = this.output + (skip < 0 ? alphabet[bits >> 3] : '') + (check ? '$' : '') this.output = '' return output } } Decoder.prototype.update = function(input, flush) { for (var i = 0; i < input.length; i++) { this.readChar(input[i]) } var output = this.output this.output = '' if (flush) { output += this.finish() } return output } /** Convenience functions * * These are the ones to use if you just have a string and * want to convert it without dealing with streams and whatnot. */ // Base32-encoded string goes in, decoded data comes out. function decode(input) { var decoder = new Decoder() var output = decoder.update(input.split("").reverse().join("")+'A', true) return output } function toHex(str) { var hex = ''; for(var i=0;i<str.length;i++) { //hex += ''+("00" + str.charCodeAt(i).toString(16)).substr(-2); hex += str.charCodeAt(i).toString(16); } return hex; } convertHex = toHex(decode('A0C4KB')); alert(convertHex); </script> The above script works fine on FF and Chrome and gives me the correct hex value. The alert output comes as expected with abc2d0 For IE, this doesn't seem to work. I get all ffffffff This is a Base32 implementation that I pickep up from https://github.com/agnoster/base32-js
Internet Explorer's JScript engine doesn't support array access to string constants. You'll have to replace alphabet[i] with alphabet.charAt(i) to get it working. I thought MS had addressed this issue, though, but I could be wrong/too hopeful.