refactor: thens instead of awaits

This commit is contained in:
quenousimporte 2023-09-11 14:28:23 +02:00
parent 36458ad166
commit efb3f49e2d
1 changed files with 63 additions and 33 deletions

View File

@ -23,30 +23,52 @@ function simplifystring(str)
return str.toLowerCase().normalize('NFD').replace(/\p{Diacritic}/gu, ""); return str.toLowerCase().normalize('NFD').replace(/\p{Diacritic}/gu, "");
} }
async function decrypt(str) function decrypt(str)
{ {
var key = pgpkey.split("-----END PGP PUBLIC KEY BLOCK-----")[1]; var keystring = pgpkey.split("-----END PGP PUBLIC KEY BLOCK-----")[1];
var privateKey = await openpgp.readKey({ armoredKey: key }); var key = null;
var decrypted = await openpgp.decrypt({
message: await openpgp.readMessage({ armoredMessage: str }), return openpgp.readKey({ armoredKey: keystring })
decryptionKeys: privateKey }); .then(privateKey =>
const chunks = []; {
for await (const chunk of decrypted.data) { key = privateKey;
return openpgp.readMessage({ armoredMessage: str });
})
.then(message =>
{
return openpgp.decrypt({
message: message,
decryptionKeys: key })
})
.then(decrypted =>
{
var chunks = [];
for (const chunk of decrypted.data) {
chunks.push(chunk); chunks.push(chunk);
} }
return chunks.join(''); return chunks.join('');
});
} }
async function encrypt(str) function encrypt(str)
{ {
var key = pgpkey.split("-----BEGIN PGP PRIVATE KEY BLOCK-----")[0]; var keystring = pgpkey.split("-----BEGIN PGP PRIVATE KEY BLOCK-----")[0];
var publicKey = await openpgp.readKey({ armoredKey: key }); var key = null;
return await openpgp.encrypt({ return openpgp.readKey({ armoredKey: keystring })
message: await openpgp.createMessage({ text: str }), .then(publicKey =>
encryptionKeys: publicKey }); {
key = publicKey;
return openpgp.createMessage({ text: str });
})
.then(message =>
{
return openpgp.encrypt({
message: message,
encryptionKeys: key });
})
} }
async function saveifneeded() function saveifneeded()
{ {
var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" }); var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" });
if (currentnote.content != newcontent) if (currentnote.content != newcontent)
@ -57,8 +79,10 @@ async function saveifneeded()
notes.unshift(currentnote); notes.unshift(currentnote);
console.log("sending data file to server..."); console.log("sending data file to server...");
var encrypted = await encrypt(JSON.stringify(notes)); encrypt(JSON.stringify(notes))
axios.post(`${settings.url}/handler.php`, .then(encrypted =>
{
return axios.post(`${settings.url}/handler.php`,
{ {
action: "push", action: "push",
password: settings.password, password: settings.password,
@ -69,7 +93,9 @@ async function saveifneeded()
{ {
"Content-type": "application/x-www-form-urlencoded" "Content-type": "application/x-www-form-urlencoded"
} }
}).then(res => { });
})
.then(res => {
console.log("...done."); console.log("...done.");
}); });
} }
@ -82,7 +108,8 @@ async function saveifneeded()
function editnote() function editnote()
{ {
fs.writeFileSync("note.md", currentnote.content); fs.writeFileSync("note.md", currentnote.content);
cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr) cp.exec(`${settings.command} note.md`,
function (err, stdout, stderr)
{ {
clearInterval(intervalid); clearInterval(intervalid);
intervalid = null; intervalid = null;
@ -112,10 +139,13 @@ axios.post(`${settings.url}/handler.php`,
"Content-type": "application/x-www-form-urlencoded" "Content-type": "application/x-www-form-urlencoded"
} }
}) })
.then(async function(res) .then(function(res)
{ {
notes = JSON.parse(await decrypt(res.data)); return decrypt(res.data);
})
.then(json =>
{
notes = JSON.parse(json);
switch (command) switch (command)
{ {
case "help": case "help":