Rewrite for extensibility & compactness

-This is a break from previous versions unfortunately
-Connecting is now a promise
-Chats, contacts & previously unread messages are supplied on connection
-Groups!
-Message confirmations are more reliable
-Timeout queries & connections
This commit is contained in:
Adhiraj
2020-05-14 20:13:32 +05:30
parent 512d45c5bd
commit b60bd03d21
10 changed files with 1216 additions and 778 deletions

View File

@@ -104,7 +104,16 @@ module.exports = {
},
// generate a buffer with random bytes of the specified length
randomBytes: function (length) { return Crypto.randomBytes(length) },
promiseTimeout: function(ms, promise) {
// Create a promise that rejects in <ms> milliseconds
let timeout = new Promise((_, reject) => {
let id = setTimeout(() => {
clearTimeout(id)
reject('Timed out')
}, ms)
})
return Promise.race([promise, timeout])
},
// whatsapp requires a message tag for every message, we just use the timestamp as one
generateMessageTag: function () { return new Date().getTime().toString() },
// generate a random 16 byte client ID