@@ -36,6 +36,7 @@ function toASCIILower(str) {
3636
3737const SOLIDUS = '/' ;
3838const SEMICOLON = ';' ;
39+
3940function parseTypeAndSubtype ( str ) {
4041 // Skip only HTTP whitespace from start
4142 let position = SafeStringPrototypeSearch ( str , END_BEGINNING_WHITESPACE ) ;
@@ -72,12 +73,11 @@ function parseTypeAndSubtype(str) {
7273 throw new ERR_INVALID_MIME_SYNTAX ( 'subtype' , str , trimmedSubtype ) ;
7374 }
7475 const subtype = toASCIILower ( trimmedSubtype ) ;
75- return {
76- __proto__ : null ,
76+ return [
7777 type ,
7878 subtype ,
79- parametersStringIndex : position ,
80- } ;
79+ position ,
80+ ] ;
8181}
8282
8383const EQUALS_SEMICOLON_OR_END = / [ ; = ] | $ / ;
@@ -122,13 +122,24 @@ const encode = (value) => {
122122} ;
123123
124124class MIMEParams {
125- #data = new SafeMap ( ) ;
125+ #data = null ;
126+ #string = null ;
127+
128+ constructor ( str ) {
129+ if ( str !== undefined ) {
130+ this . #string = str ;
131+ } else {
132+ this . #data = new SafeMap ( ) ;
133+ }
134+ }
126135
127136 delete ( name ) {
137+ this . #parse( ) ;
128138 this . #data. delete ( name ) ;
129139 }
130140
131141 get ( name ) {
142+ this . #parse( ) ;
132143 const data = this . #data;
133144 if ( data . has ( name ) ) {
134145 return data . get ( name ) ;
@@ -137,10 +148,12 @@ class MIMEParams {
137148 }
138149
139150 has ( name ) {
151+ this . #parse( ) ;
140152 return this . #data. has ( name ) ;
141153 }
142154
143155 set ( name , value ) {
156+ this . #parse( ) ;
144157 const data = this . #data;
145158 name = `${ name } ` ;
146159 value = `${ value } ` ;
@@ -166,18 +179,22 @@ class MIMEParams {
166179 }
167180
168181 * entries ( ) {
182+ this . #parse( ) ;
169183 yield * this . #data. entries ( ) ;
170184 }
171185
172186 * keys ( ) {
187+ this . #parse( ) ;
173188 yield * this . #data. keys ( ) ;
174189 }
175190
176191 * values ( ) {
192+ this . #parse( ) ;
177193 yield * this . #data. values ( ) ;
178194 }
179195
180196 toString ( ) {
197+ this . #parse( ) ;
181198 let ret = '' ;
182199 for ( const { 0 : key , 1 : value } of this . #data) {
183200 const encoded = encode ( value ) ;
@@ -190,8 +207,12 @@ class MIMEParams {
190207
191208 // Used to act as a friendly class to stringifying stuff
192209 // not meant to be exposed to users, could inject invalid values
193- static parseParametersString ( str , position , params ) {
194- const paramsMap = params . #data;
210+ #parse( ) {
211+ if ( this . #data !== null ) return ; // already parsed
212+ this . #data = new SafeMap ( ) ;
213+ const paramsMap = this . #data;
214+ let position = 0 ;
215+ const str = this . #string;
195216 const endOfSource = SafeStringPrototypeSearch (
196217 StringPrototypeSlice ( str , position ) ,
197218 START_ENDING_WHITESPACE ,
@@ -270,7 +291,7 @@ class MIMEParams {
270291 NOT_HTTP_TOKEN_CODE_POINT ) === - 1 &&
271292 SafeStringPrototypeSearch ( parameterValue ,
272293 NOT_HTTP_QUOTED_STRING_CODE_POINT ) === - 1 &&
273- params . has ( parameterString ) === false
294+ paramsMap . has ( parameterString ) === false
274295 ) {
275296 paramsMap . set ( parameterString , parameterValue ) ;
276297 }
@@ -293,24 +314,16 @@ ObjectDefineProperty(MIMEParams.prototype, 'toJSON', {
293314 writable : true ,
294315} ) ;
295316
296- const { parseParametersString } = MIMEParams ;
297- delete MIMEParams . parseParametersString ;
298-
299317class MIMEType {
300318 #type;
301319 #subtype;
302320 #parameters;
303321 constructor ( string ) {
304322 string = `${ string } ` ;
305323 const data = parseTypeAndSubtype ( string ) ;
306- this . #type = data . type ;
307- this . #subtype = data . subtype ;
308- this . #parameters = new MIMEParams ( ) ;
309- parseParametersString (
310- string ,
311- data . parametersStringIndex ,
312- this . #parameters,
313- ) ;
324+ this . #type = data [ 0 ] ;
325+ this . #subtype = data [ 1 ] ;
326+ this . #parameters = new MIMEParams ( string . slice ( data [ 2 ] ) ) ;
314327 }
315328
316329 get type ( ) {
@@ -362,6 +375,8 @@ ObjectDefineProperty(MIMEType.prototype, 'toJSON', {
362375} ) ;
363376
364377module . exports = {
378+ toASCIILower,
379+ parseTypeAndSubtype,
365380 MIMEParams,
366381 MIMEType,
367382} ;
0 commit comments