| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0cf7993 commit 4299cd5
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1896,6 +1896,18 @@ console.log(JSON.stringify(myMIMES)); | |||
| 1896 | 1896 | // Prints: ["image/png", "image/gif"] | |
| 1897 | 1897 | ``` | |
| 1898 | 1898 | ||
| 1899 | + ### `MIMEType.parse(string)` | ||
| 1900 | + | ||
| 1901 | + <!-- | ||
| 1902 | + added: REPLACEME | ||
| 1903 | + --> | ||
| 1904 | + | ||
| 1905 | + * `string` {string} The input MIME to parse | ||
| 1906 | + * Returns: {MIMEType|null} | ||
| 1907 | + | ||
| 1908 | + Attempts to parse the given `string` as a MIMEType. If the string cannot be | ||
| 1909 | + parsed, `null` is returned. | ||
| 1910 | + | ||
| 1899 | 1911 | ## Class: `util.MIMEParams` | |
| 1900 | 1912 | ||
| 1901 | 1913 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,13 +117,8 @@ function dataURLProcessor(dataURL) { | |||
| 117 | 117 | // mimeType. | |
| 118 | 118 | // 14. If mimeTypeRecord is failure, then set | |
| 119 | 119 | // 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'); | ||
| 127 | 122 | ||
| 128 | 123 | // 15. Return a new data: URL struct whose MIME | |
| 129 | 124 | // type is mimeTypeRecord and body is body. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,16 +52,9 @@ function getNextRequestId() { | |||
| 52 | 52 | }; | |
| 53 | 53 | ||
| 54 | 54 | 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') || ''); | ||
| 65 | 58 | ||
| 66 | 59 | return { | |
| 67 | 60 | __proto__: null, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,9 +10,11 @@ const { | |||
| 10 | 10 | StringPrototypeIndexOf, | |
| 11 | 11 | StringPrototypeSlice, | |
| 12 | 12 | StringPrototypeToLowerCase, | |
| 13 | + Symbol, | ||
| 13 | 14 | SymbolIterator, | |
| 14 | 15 | } = primordials; | |
| 15 | 16 | const { | |
| 17 | + ERR_ILLEGAL_CONSTRUCTOR, | ||
| 16 | 18 | ERR_INVALID_MIME_SYNTAX, | |
| 17 | 19 | } = require('internal/errors').codes; | |
| 18 | 20 | ||
@@ -22,6 +24,8 @@ const NOT_HTTP_QUOTED_STRING_CODE_POINT = /[^\t\u0020-~\u0080-\u00FF]/g; | |||
| 22 | 24 | const END_BEGINNING_WHITESPACE = /[^\r\n\t ]|$/; | |
| 23 | 25 | const START_ENDING_WHITESPACE = /[\r\n\t ]*$/; | |
| 24 | 26 | ||
| 27 | + const kNoThrow = Symbol('kNoThrow'); | ||
| 28 | + | ||
| 25 | 29 | function toASCIILower(str) { | |
| 26 | 30 | // eslint-disable-next-line no-control-regex | |
| 27 | 31 | if (!/[^\x00-\x7f]/.test(str)) return StringPrototypeToLowerCase(str); | |
@@ -39,7 +43,7 @@ function toASCIILower(str) { | |||
| 39 | 43 | const SOLIDUS = '/'; | |
| 40 | 44 | const SEMICOLON = ';'; | |
| 41 | 45 | ||
| 42 | - function parseTypeAndSubtype(str) { | ||
| 46 | + function parseTypeAndSubtype(str, noThrow = null) { | ||
| 43 | 47 | // Skip only HTTP whitespace from start | |
| 44 | 48 | let position = SafeStringPrototypeSearch(str, END_BEGINNING_WHITESPACE); | |
| 45 | 49 | // read until '/' | |
@@ -50,6 +54,7 @@ function parseTypeAndSubtype(str) { | |||
| 50 | 54 | const invalidTypeIndex = SafeStringPrototypeSearch(trimmedType, | |
| 51 | 55 | NOT_HTTP_TOKEN_CODE_POINT); | |
| 52 | 56 | if (trimmedType === '' || invalidTypeIndex !== -1 || typeEnd === -1) { | |
| 57 | + if (noThrow === kNoThrow) return null; | ||
| 53 | 58 | throw new ERR_INVALID_MIME_SYNTAX('type', str, invalidTypeIndex); | |
| 54 | 59 | } | |
| 55 | 60 | // skip type and '/' | |
@@ -72,6 +77,7 @@ function parseTypeAndSubtype(str) { | |||
| 72 | 77 | const invalidSubtypeIndex = SafeStringPrototypeSearch(trimmedSubtype, | |
| 73 | 78 | NOT_HTTP_TOKEN_CODE_POINT); | |
| 74 | 79 | if (trimmedSubtype === '' || invalidSubtypeIndex !== -1) { | |
| 80 | + if (noThrow === kNoThrow) return null; | ||
| 75 | 81 | throw new ERR_INVALID_MIME_SYNTAX('subtype', str, invalidSubtypeIndex); | |
| 76 | 82 | } | |
| 77 | 83 | const subtype = toASCIILower(trimmedSubtype); | |
@@ -335,12 +341,24 @@ class MIMEType { | |||
| 335 | 341 | #type; | |
| 336 | 342 | #subtype; | |
| 337 | 343 | #parameters; | |
| 338 | - constructor(string) { | ||
| 344 | + constructor(string, noThrowSymbol = null) { | ||
| 339 | 345 | string = `${string}`; | |
| 340 | - const data = parseTypeAndSubtype(string); | ||
| 341 | - this.#type = data[0]; | ||
| 342 | - this.#subtype = data[1]; | ||
| 343 | - this.#parameters = instantiateMimeParams(StringPrototypeSlice(string, data[2])); | ||
| 346 | + // noThrowSymbol can be null or kNoThrow, but not any other value | ||
| 347 | + if (noThrowSymbol != null && noThrowSymbol !== kNoThrow) { | ||
| 348 | + throw new ERR_ILLEGAL_CONSTRUCTOR(); | ||
| 349 | + } | ||
| 350 | + const data = parseTypeAndSubtype(string, noThrowSymbol); | ||
| 351 | + if (data != null) { | ||
| 352 | + this.#type = data[0]; | ||
| 353 | + this.#subtype = data[1]; | ||
| 354 | + this.#parameters = instantiateMimeParams(StringPrototypeSlice(string, data[2])); | ||
| 355 | + } | ||
| 356 | + } | ||
| 357 | + | ||
| 358 | + // Like the constructor, but returns null instead of throwing on invalid input. | ||
| 359 | + static parse(string) { | ||
| 360 | + const mt = new MIMEType(string, kNoThrow); | ||
| 361 | + return mt.type ? mt : null; | ||
| 344 | 362 | } | |
| 345 | 363 | ||
| 346 | 364 | get type() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -185,3 +185,17 @@ assert.throws(() => params.set('x', `x${NOT_HTTP_QUOTED_STRING_CODE_POINT}`), /p | |||
| 185 | 185 | assert.strictEqual(params.has('foo'), false); | |
| 186 | 186 | assert.deepStrictEqual([...params], []); | |
| 187 | 187 | } | |
| 188 | + | ||
| 189 | + { | ||
| 190 | + // Non-throwing MimeType.parse, works for valid | ||
| 191 | + const mime = MIMEType.parse('text/plain;Charset=value'); | ||
| 192 | + assert.strictEqual(mime.params.get('Charset'), 'value'); | ||
| 193 | + assert.strictEqual(mime.params.get('charset'), 'value'); | ||
| 194 | + assert.strictEqual(mime.params.get('CHARSET'), 'value'); | ||
| 195 | + assert.strictEqual(mime.params.has('Charset'), true); | ||
| 196 | + assert.strictEqual(`${mime.params}`, 'charset=value'); | ||
| 197 | + | ||
| 198 | + // Returns null on Invalid | ||
| 199 | + const invalidMime = MIMEType.parse('text plain'); | ||
| 200 | + assert.strictEqual(invalidMime, null); | ||
| 201 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments