From 68d86fe46b8182a043cfc31bac5ca42f2b52e622 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 14 Mar 2024 17:17:33 +0100 Subject: Use fetch API for sending webhooks. The Node https module started getting timeouts and not closing down after req.end for reasons unknown to me. Let's take this moment as an opportunity to move to the new fetch API instead. --- server.js | 45 ++++++++++----------------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) (limited to 'server.js') diff --git a/server.js b/server.js index dcc6208..56a3413 100644 --- a/server.js +++ b/server.js @@ -5,7 +5,6 @@ const fs = require("fs") const crypto = require("crypto") const http = require("http") -const https = require("https") // for webhook requests const { WebSocketServer } = require("ws") const express = require("express") const url = require("url") @@ -2278,19 +2277,11 @@ function mail_verification_token(user, token) { */ const webhook_json_options = { - method: "POST", - timeout: 6000, - headers: { - "Content-Type": "application/json" - } + "Content-Type": "application/json" } const webhook_text_options = { - method: "POST", - timeout: 6000, - headers: { - "Content-Type": "text/plain" - } + "Content-Type": "text/plain" } function on_webhook_success(user_id) { @@ -2302,38 +2293,22 @@ function on_webhook_error(user_id, error) { SQL_UPDATE_WEBHOOK_ERROR.run(error, user_id) } -function send_webhook(user_id, webhook, message, retry=2) { +async function send_webhook(user_id, webhook, message, retry=2) { if (!WEBHOOKS) return try { const text = webhook.prefix + " " + message const data = webhook.format ? JSON.stringify({ [webhook.format]: text }) : text - const options = webhook.format ? webhook_json_options : webhook_text_options - const req = https.request(webhook.url, options, res => { - if (res.statusCode === 200 || res.statusCode === 204) - on_webhook_success(user_id) - else { - if (retry > 0) - retry_webhook(user_id, webhook, message, retry - 1) - else - on_webhook_error(user_id, res.statusCode + " " + http.STATUS_CODES[res.statusCode]) - } - }) - req.on("timeout", () => { + const headers = webhook.format ? webhook_json_options : webhook_text_options + const res = await fetch(webhook.url, { method: "POST", headers: headers, body: data }) + if (res.ok) + on_webhook_success(user_id) + else { if (retry > 0) retry_webhook(user_id, webhook, message, retry - 1) else - on_webhook_error(user_id, "Timeout") - req.abort() - }) - req.on("error", (err) => { - if (retry > 0) - retry_webhook(user_id, webhook, message, retry - 1) - else - on_webhook_error(user_id, err.toString()) - }) - req.write(data) - req.end() + on_webhook_error(user_id, res.status + ": " + res.statusText) + } } catch (err) { if (retry > 0) retry_webhook(user_id, webhook, message, retry - 1) -- cgit v1.2.3