From a302505b285f4bbde521376dbd14a9fdf4de1c8f Mon Sep 17 00:00:00 2001 From: fraxken Date: Sun, 16 Nov 2025 03:48:36 +0100 Subject: [PATCH] refactor: remove content-type in favor of undici.parseMIMEType --- package.json | 2 -- src/class/undiciResponseHandler.ts | 29 +++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index b9208fc..3c1259e 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "@arethetypeswrong/cli": "^0.18.2", "@openally/config.eslint": "^2.2.0", "@openally/config.typescript": "1.2.1", - "@types/content-type": "^1.1.9", "@types/lru-cache": "^7.10.9", "@types/node": "24.9.1", "@types/statuses": "^2.0.6", @@ -47,7 +46,6 @@ }, "dependencies": { "@openally/result": "^1.2.1", - "content-type": "^1.0.5", "lru-cache": "11.2.2", "statuses": "^2.0.2", "undici": "7.16.0" diff --git a/src/class/undiciResponseHandler.ts b/src/class/undiciResponseHandler.ts index e8ade2e..4439540 100644 --- a/src/class/undiciResponseHandler.ts +++ b/src/class/undiciResponseHandler.ts @@ -4,8 +4,11 @@ import { promisify } from "node:util"; import { inflate, brotliDecompress, gunzip } from "node:zlib"; // Import Third-party Dependencies -import { Dispatcher } from "undici"; -import * as contentType from "content-type"; +import { + Dispatcher, + parseMIMEType, + type MIMEType +} from "undici"; // Import Internal Dependencies import { getEncodingCharset } from "../utils.js"; @@ -121,14 +124,17 @@ export class HttpieResponseHandler { let bodyAsString = ""; try { - const { type, parameters } = contentType.parse(contentTypeHeader); - bodyAsString = buffer.toString(getEncodingCharset(parameters.charset)); + const mime = parseMIMETypeWithError(contentTypeHeader); + + bodyAsString = buffer.toString( + getEncodingCharset(mime.parameters.get("charset")) + ); - if (type === "application/json") { + if (mime.essence === "application/json") { return JSON.parse(bodyAsString); } - if (type.startsWith("text/")) { + if (mime.essence.startsWith("text/")) { return bodyAsString; } } @@ -147,3 +153,14 @@ export class HttpieResponseHandler { return buffer; } } + +function parseMIMETypeWithError( + mimeType: string +): MIMEType { + const mime = parseMIMEType(mimeType); + if (mime === "failure") { + throw new Error("invalid media type"); + } + + return mime; +}