Skip to content

Commit a9fa625

Browse files
committed
lib: use MIMEType.parse instead of constructor
Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent 7052202 commit a9fa625

4 files changed

Lines changed: 11 additions & 18 deletions

File tree

doc/api/util.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,10 @@ console.log(JSON.stringify(myMIMES));
18981898

18991899
### `MIMEType.parse(string)`
19001900

1901+
<!--
1902+
added: REPLACEME
1903+
-->
1904+
19011905
* `string` {string} The input MIME to parse
19021906
* Returns: {MIMEType|null}
19031907

lib/internal/data_url.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,8 @@ function dataURLProcessor(dataURL) {
117117
// mimeType.
118118
// 14. If mimeTypeRecord is failure, then set
119119
// mimeTypeRecord to text/plain;charset=US-ASCII.
120-
let mimeTypeRecord;
121-
122-
try {
123-
mimeTypeRecord = new MIMEType(mimeType);
124-
} catch {
125-
mimeTypeRecord = new MIMEType('text/plain;charset=US-ASCII');
126-
}
120+
const mimeTypeRecord = MIMEType.parse(mimeType) ||
121+
new MIMEType('text/plain;charset=US-ASCII');
127122

128123
// 15. Return a new data: URL struct whose MIME
129124
// type is mimeTypeRecord and body is body.

lib/internal/inspector/network.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,9 @@ function getNextRequestId() {
5252
};
5353

5454
function sniffMimeType(contentType) {
55-
let mimeType;
56-
let charset;
57-
try {
58-
const mimeTypeObj = new MIMEType(contentType);
59-
mimeType = StringPrototypeToLowerCase(mimeTypeObj.essence || '');
60-
charset = StringPrototypeToLowerCase(mimeTypeObj.params.get('charset') || '');
61-
} catch {
62-
mimeType = '';
63-
charset = '';
64-
}
55+
const mimeTypeObj = MIMEType.parse(contentType);
56+
const mimeType = StringPrototypeToLowerCase(mimeTypeObj?.essence || '');
57+
const charset = StringPrototypeToLowerCase(mimeTypeObj?.params.get('charset') || '');
6558

6659
return {
6760
__proto__: null,

lib/internal/mime.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ class MIMEType {
343343
#parameters;
344344
constructor(string, noThrowSymbol = null) {
345345
string = `${string}`;
346-
if (noThrowSymbol != null && typeof noThrowSymbol != 'symbol') {
346+
// noThrowSymbol can be null or kNoThrow, but not any other value
347+
if (noThrowSymbol != null && noThrowSymbol !== kNoThrow) {
347348
throw new ERR_ILLEGAL_CONSTRUCTOR();
348349
}
349350
const data = parseTypeAndSubtype(string, noThrowSymbol);

0 commit comments

Comments
 (0)