From 01fce997661e605b94c8de268cda79619212cd49 Mon Sep 17 00:00:00 2001 From: Martin Dimitrov Date: Mon, 11 Nov 2024 18:56:01 -0800 Subject: [PATCH] await transmission of discord notif request instead of waiting for it to complete --- .../functions/buzzer-activated.js | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/doorman-client/functions/buzzer-activated.js b/packages/doorman-client/functions/buzzer-activated.js index 7755d48..2f9de26 100644 --- a/packages/doorman-client/functions/buzzer-activated.js +++ b/packages/doorman-client/functions/buzzer-activated.js @@ -3,6 +3,29 @@ */ const { randomUUID } = require('crypto'); const fetch = require('node-fetch'); +const https = require('https'); +const http = require('http'); + +/** + * Helper function to do an HTTP request and just await transmission, but not await a response. + * ref: https://www.sensedeep.com/blog/posts/stories/lambda-fast-http.html + * @param {*} url - the URL to do HTTP request to + * @returns promise signalling HTTP request has been transmitted + */ +async function lambdaFastHttp(url) { + return new Promise((resolve, reject) => { + let req; + + if (url.startsWith("https://")) { + req = https.request(URL.parse(url)); + } else { + req = http.request(URL.parse(url)); + } + req.end(null, null, () => { + resolve(req); + }); + }); +} async function getConfig(context, buzzer) { return await fetch(context.DOORMAN_URL + `/api/door/info?buzzer=${buzzer}`) @@ -12,13 +35,13 @@ async function getConfig(context, buzzer) { }); } -function notifyDiscord(context, msg, u, optionalJsonStr) { - return fetch(context.DOORMAN_URL + +async function notifyDiscord(context, msg, u, optionalJsonStr) { + return lambdaFastHttp(context.DOORMAN_URL + `/api/door/notify?discordUser=${encodeURIComponent(JSON.stringify(u))}&msg=${encodeURIComponent(JSON.stringify(msg))}&json=${encodeURIComponent(JSON.stringify(optionalJsonStr))}`, ).catch(err => console.log(err)) } -function notifyAllDiscord(context, config, msg, optionalJsonStr) { +async function notifyAllDiscord(context, config, msg, optionalJsonStr) { return notifyDiscord(context, config.discordUsers.map(() => msg), config.discordUsers, config.discordUsers.map(() => optionalJsonStr).filter(Boolean)); } @@ -90,7 +113,7 @@ exports.handler = async function(context, event, callback) { console.log( invokeId, "UnlockPromise: I was the fastest, so I will attempt to notify discord users before resolving with unlock" ); - notifyAllDiscord(context, config, `🔓 Doorman buzzed someone up @ Door "${config.door}"`, JSON.stringify(body.fingerprint)); + await notifyAllDiscord(context, config, `🔓 Doorman buzzed someone up @ Door "${config.door}"`, JSON.stringify(body.fingerprint)); resolve(twiml); } else { console.log( @@ -113,7 +136,7 @@ exports.handler = async function(context, event, callback) { console.log( invokeId, "GracefulFallbackPromise: I was the fastest, so I will attempt to notify discord users before resolving with a call" ); - notifyAllDiscord( + await notifyAllDiscord( context, config, `📞 Somebody buzzed the door and it dialed through to fallback phone numbers @ Door "${config.door}"`,