@@ -5,6 +5,12 @@ const { isValidHTTPToken, isomorphicDecode } = require('./util')
5
5
6
6
const encoder = new TextEncoder ( )
7
7
8
+ // Regex
9
+ const HTTP_TOKEN_CODEPOINTS = / ^ [ ! # $ % & ' * + - . ^ _ | ~ A - z 0 - 9 ] + $ /
10
+ const HTTP_WHITESPACE_REGEX = / ( \u000A | \u000D | \u0009 | \u0020 ) / // eslint-disable-line
11
+ // https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
12
+ const HTTP_QUOTED_STRING_TOKENS = / ^ ( \u0009 | \x { 0020 } - \x { 007 E} | \x { 0080 } - \x { 00 FF} ) + $ / // eslint-disable-line
13
+
8
14
// https://fetch.spec.whatwg.org/#data-url-processor
9
15
/** @param {URL } dataURL */
10
16
function dataURLProcessor ( dataURL ) {
@@ -217,7 +223,7 @@ function parseMIMEType (input) {
217
223
// 4. If type is the empty string or does not solely
218
224
// contain HTTP token code points, then return failure.
219
225
// https://mimesniff.spec.whatwg.org/#http-token-code-point
220
- if ( type . length === 0 || ! / ^ [ ! # $ % & ' * + - . ^ _ | ~ A - z 0 - 9 ] + $ / . test ( type ) ) {
226
+ if ( type . length === 0 || ! HTTP_TOKEN_CODEPOINTS . test ( type ) ) {
221
227
return 'failure'
222
228
}
223
229
@@ -244,7 +250,7 @@ function parseMIMEType (input) {
244
250
245
251
// 9. If subtype is the empty string or does not solely
246
252
// contain HTTP token code points, then return failure.
247
- if ( subtype . length === 0 || ! / ^ [ ! # $ % & ' * + - . ^ _ | ~ A - z 0 - 9 ] + $ / . test ( subtype ) ) {
253
+ if ( subtype . length === 0 || ! HTTP_TOKEN_CODEPOINTS . test ( subtype ) ) {
248
254
return 'failure'
249
255
}
250
256
@@ -258,9 +264,7 @@ function parseMIMEType (input) {
258
264
/** @type {Map<string, string> } */
259
265
parameters : new Map ( ) ,
260
266
// https://mimesniff.spec.whatwg.org/#mime-type-essence
261
- get essence ( ) {
262
- return `${ this . type } /${ this . subtype } `
263
- }
267
+ essence : `${ type } /${ subtype } `
264
268
}
265
269
266
270
// 11. While position is not past the end of input:
@@ -272,7 +276,7 @@ function parseMIMEType (input) {
272
276
// whitespace from input given position.
273
277
collectASequenceOfCodePoints (
274
278
// https://fetch.spec.whatwg.org/#http-whitespace
275
- ( char ) => / ( \u000A | \u000D | \u0009 | \u0020 ) / . test ( char ) , // eslint-disable-line
279
+ char => HTTP_WHITESPACE_REGEX . test ( char ) ,
276
280
input ,
277
281
position
278
282
)
@@ -355,9 +359,8 @@ function parseMIMEType (input) {
355
359
// then set mimeType’s parameters[parameterName] to parameterValue.
356
360
if (
357
361
parameterName . length !== 0 &&
358
- / ^ [ ! # $ % & ' * + - . ^ _ | ~ A - z 0 - 9 ] + $ / . test ( parameterName ) &&
359
- // https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
360
- ! / ^ ( \u0009 | \x { 0020 } - \x { 007 E} | \x { 0080 } - \x { 00 FF} ) + $ / . test ( parameterValue ) && // eslint-disable-line
362
+ HTTP_TOKEN_CODEPOINTS . test ( parameterName ) &&
363
+ ! HTTP_QUOTED_STRING_TOKENS . test ( parameterValue ) &&
361
364
! mimeType . parameters . has ( parameterName )
362
365
) {
363
366
mimeType . parameters . set ( parameterName , parameterValue )
0 commit comments