From 7052202c49fd2b676fb994ff44aa6888f0d19bfb Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 2 Aug 2026 10:32:59 -0700 Subject: [PATCH 1/2] util: add non-throwing MIMEType.parse Similar to `URL.parse(...)`, the `MIMEType.parse(...)` API will return `null` if the input cannot be parsed as opposed to throwing the way the constructor does. Signed-off-by: James M Snell --- doc/api/util.md | 8 ++++++++ lib/internal/mime.js | 29 +++++++++++++++++++++++------ test/parallel/test-mime-api.js | 14 ++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index fd93b5272293..3ca1a169702a 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -1896,6 +1896,14 @@ console.log(JSON.stringify(myMIMES)); // Prints: ["image/png", "image/gif"] ``` +### `MIMEType.parse(string)` + +* `string` {string} The input MIME to parse +* Returns: {MIMEType|null} + +Attempts to parse the given `string` as a MIMEType. If the string cannot be +parsed, `null` is returned. + ## Class: `util.MIMEParams` + * `string` {string} The input MIME to parse * Returns: {MIMEType|null} diff --git a/lib/internal/data_url.js b/lib/internal/data_url.js index 3b8a82ba08d1..0aea5030ecad 100644 --- a/lib/internal/data_url.js +++ b/lib/internal/data_url.js @@ -117,13 +117,8 @@ function dataURLProcessor(dataURL) { // mimeType. // 14. If mimeTypeRecord is failure, then set // mimeTypeRecord to text/plain;charset=US-ASCII. - let mimeTypeRecord; - - try { - mimeTypeRecord = new MIMEType(mimeType); - } catch { - mimeTypeRecord = new MIMEType('text/plain;charset=US-ASCII'); - } + const mimeTypeRecord = MIMEType.parse(mimeType) || + new MIMEType('text/plain;charset=US-ASCII'); // 15. Return a new data: URL struct whose MIME // type is mimeTypeRecord and body is body. diff --git a/lib/internal/inspector/network.js b/lib/internal/inspector/network.js index ddd5b4750bee..e9ebd05c6627 100644 --- a/lib/internal/inspector/network.js +++ b/lib/internal/inspector/network.js @@ -52,16 +52,9 @@ function getNextRequestId() { }; function sniffMimeType(contentType) { - let mimeType; - let charset; - try { - const mimeTypeObj = new MIMEType(contentType); - mimeType = StringPrototypeToLowerCase(mimeTypeObj.essence || ''); - charset = StringPrototypeToLowerCase(mimeTypeObj.params.get('charset') || ''); - } catch { - mimeType = ''; - charset = ''; - } + const mimeTypeObj = MIMEType.parse(contentType); + const mimeType = StringPrototypeToLowerCase(mimeTypeObj?.essence || ''); + const charset = StringPrototypeToLowerCase(mimeTypeObj?.params.get('charset') || ''); return { __proto__: null, diff --git a/lib/internal/mime.js b/lib/internal/mime.js index 142b9b289d01..bb368a36aa56 100644 --- a/lib/internal/mime.js +++ b/lib/internal/mime.js @@ -343,7 +343,8 @@ class MIMEType { #parameters; constructor(string, noThrowSymbol = null) { string = `${string}`; - if (noThrowSymbol != null && typeof noThrowSymbol != 'symbol') { + // noThrowSymbol can be null or kNoThrow, but not any other value + if (noThrowSymbol != null && noThrowSymbol !== kNoThrow) { throw new ERR_ILLEGAL_CONSTRUCTOR(); } const data = parseTypeAndSubtype(string, noThrowSymbol);