From 573af086f3759e7677234db3cdeac9c7a6f5d662 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:50:15 +0100 Subject: [PATCH] deps: replace `raw-body` with native approach --- package.json | 5 ++--- src/structs/Webhook.ts | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 94c010e..3f90dbe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@top-gg/sdk", - "version": "3.1.6", + "version": "3.1.7", "description": "Official Top.gg Node SDK", "main": "./dist/index.js", "scripts": { @@ -44,8 +44,7 @@ "typescript": "^5.2.2" }, "dependencies": { - "raw-body": "^2.5.2", - "undici": "^5.23.0" + "undici": "^6.13.0" }, "types": "./dist/index.d.ts" } diff --git a/src/structs/Webhook.ts b/src/structs/Webhook.ts index d257ce8..06bfc16 100644 --- a/src/structs/Webhook.ts +++ b/src/structs/Webhook.ts @@ -1,4 +1,3 @@ -import getBody from "raw-body"; import { Request, Response, NextFunction } from "express"; import { WebhookPayload } from "../typings"; @@ -70,21 +69,25 @@ export class Webhook { req.headers.authorization !== this.authorization ) return res.status(403).json({ error: "Unauthorized" }); - // parse json + // parse json if (req.body) return resolve(this._formatIncoming(req.body)); - getBody(req, {}, (error, body) => { - if (error) return res.status(422).json({ error: "Malformed request" }); - try { + try { + const chunks: Uint8Array[] = []; + + req.on("data", (d) => chunks.push(d)); + + req.on("end", () => { + const body = Buffer.concat(chunks); const parsed = JSON.parse(body.toString("utf8")); resolve(this._formatIncoming(parsed)); - } catch (err) { - res.status(400).json({ error: "Invalid body" }); - resolve(false); - } - }); + }); + } catch { + res.status(400).json({ error: "Invalid body" }); + resolve(false); + } }); }