@d1g1tal/media-type 4.0.2 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/media-type.js +9 -11
- package/dist/media-type.min.js +2 -2
- package/dist/media-type.min.js.map +3 -3
- package/package.json +1 -1
- package/src/media-type-parameters.js +3 -3
- package/src/media-type.js +6 -12
package/dist/media-type.js
CHANGED
|
@@ -63,10 +63,10 @@ var MediaType = (() => {
|
|
|
63
63
|
/**
|
|
64
64
|
* Create a new MediaTypeParameters instance.
|
|
65
65
|
*
|
|
66
|
-
* @param {
|
|
66
|
+
* @param {Array<Array<string>>} entries An array of [name, value] tuples.
|
|
67
67
|
*/
|
|
68
|
-
constructor(
|
|
69
|
-
this.#map =
|
|
68
|
+
constructor(entries) {
|
|
69
|
+
this.#map = new Map(entries);
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
72
|
* Gets the number of media type parameters.
|
|
@@ -292,16 +292,14 @@ var MediaType = (() => {
|
|
|
292
292
|
/**
|
|
293
293
|
* Create a new MediaType instance from a string representation.
|
|
294
294
|
*
|
|
295
|
-
* @param {string
|
|
295
|
+
* @param {string} mediaType The media type to parse.
|
|
296
|
+
* @param {Object} [parameters] Optional parameters.
|
|
296
297
|
*/
|
|
297
|
-
constructor(
|
|
298
|
-
|
|
299
|
-
options = parser_default(options);
|
|
300
|
-
}
|
|
301
|
-
const { type, subtype, parameters = new MediaTypeParameters() } = options;
|
|
298
|
+
constructor(mediaType, parameters = {}) {
|
|
299
|
+
const { type, subtype, parameters: parsedParameters } = parser_default(mediaType);
|
|
302
300
|
this.#type = type;
|
|
303
301
|
this.#subtype = subtype;
|
|
304
|
-
this.#parameters =
|
|
302
|
+
this.#parameters = new MediaTypeParameters([...parsedParameters, ...Object.entries(parameters).map(([name, value]) => [asciiLowercase(name), asciiLowercase(value)])]);
|
|
305
303
|
}
|
|
306
304
|
/**
|
|
307
305
|
* Static factory method for parsing a media type.
|
|
@@ -311,7 +309,7 @@ var MediaType = (() => {
|
|
|
311
309
|
*/
|
|
312
310
|
static parse(string) {
|
|
313
311
|
try {
|
|
314
|
-
return new MediaType(
|
|
312
|
+
return new MediaType(string);
|
|
315
313
|
} catch (e) {
|
|
316
314
|
throw new Error(`Could not parse media type string '${string}'`);
|
|
317
315
|
}
|
package/dist/media-type.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var MediaType=(()=>{var
|
|
2
|
-
`,"\r"],
|
|
1
|
+
var MediaType=(()=>{var T=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var k=(t,e)=>{for(var r in e)T(t,r,{get:e[r],enumerable:!0})},b=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of H(e))!S.call(t,i)&&i!==r&&T(t,i,{get:()=>e[i],enumerable:!(s=C(e,i))||s.enumerable});return t};var $=t=>b(T({},"__esModule",{value:!0}),t);var L={};k(L,{default:()=>l});var v=[" "," ",`
|
|
2
|
+
`,"\r"],x=/^[ \t\n\r]+/u,d=/[ \t\n\r]+$/u,j=/^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/u,E=/^[\t\u0020-\u007E\u0080-\u00FF]*$/u,m=t=>t.replace(x,"").replace(d,""),g=t=>t.replace(d,""),u=t=>v.includes(t),n=t=>j.test(t),p=t=>E.test(t),a=t=>{let e="";for(let[r,s=r.charCodeAt(0)]of t)e+=s>=65&&s<=90?String.fromCharCode(s+32):r;return e},f=(t,e)=>{let r="";for(let s=t.length,i;++e<s;)if(i=t[e],i=="\\")r+=++e<s?t[e]:i;else{if(i=='"')break;r+=i}return[r,e]};var h=class{#e;constructor(e){this.#e=new Map(e)}get size(){return this.#e.size}get(e){return this.#e.get(a(String(e)))}has(e){return this.#e.has(a(String(e)))}set(e,r){if(e=a(String(e)),r=String(r),!n(e))throw new Error(`Invalid media type parameter name "${e}": only HTTP token code points are valid.`);if(!p(r))throw new Error(`Invalid media type parameter value "${r}": only HTTP quoted-string token code points are valid.`);return this.#e.set(e,r),this}clear(){this.#e.clear()}delete(e){return e=a(String(e)),this.#e.delete(e)}forEach(e,r){this.#e.forEach(e,r)}keys(){return this.#e.keys()}values(){return this.#e.values()}entries(){return this.#e.entries()}[Symbol.iterator](){return this.#e[Symbol.iterator]()}[Symbol.toStringTag](){return"MediaTypeParameters"}};var z=t=>{t=m(t);let e=0,r="";for(;e<t.length&&t[e]!="/";)r+=t[e],++e;if(r.length===0||!n(r)||e>=t.length)return null;++e;let s="";for(;e<t.length&&t[e]!=";";)s+=t[e],++e;if(s=g(s),s.length===0||!n(s))return null;let i={type:a(r),subtype:a(s),parameters:new Map};for(;e<t.length;){for(++e;u(t[e]);)++e;let c="";for(;e<t.length&&t[e]!=";"&&t[e]!="=";)c+=t[e],++e;if(c=a(c),e<t.length){if(t[e]==";")continue;++e}let o=null;if(t[e]=='"')for([o,e]=f(t,e);e<t.length&&t[e]!=";";)++e;else{for(o="";e<t.length&&t[e]!=";";)o+=t[e],++e;if(o=g(o),o==="")continue}c.length>0&&n(c)&&p(o)&&!i.parameters.has(c)&&i.parameters.set(c,o)}return i},y=z;var W=t=>{let e=`${t.type}/${t.subtype}`;if(t.parameters.size===0)return e;for(let[r,s]of t.parameters)e+=`;${r}=`,(!n(s)||s.length===0)&&(s=`"${s.replace(/(["\\])/ug,"\\$1")}"`),e+=s;return e},w=W;var l=class{#e;#t;#r;constructor(e,r={}){let{type:s,subtype:i,parameters:c}=y(e);this.#e=s,this.#t=i,this.#r=new h([...c,...Object.entries(r).map(([o,P])=>[a(o),a(P)])])}static parse(e){try{return new l(e)}catch{throw new Error(`Could not parse media type string '${e}'`)}}get essence(){return`${this.#e}/${this.#t}`}get type(){return this.#e}set type(e){if(e=a(String(e)),e.length===0)throw new Error("Invalid type: must be a non-empty string");if(!n(e))throw new Error(`Invalid type ${e}: must contain only HTTP token code points`);this.#e=e}get subtype(){return this.#t}set subtype(e){if(e=a(String(e)),e.length===0)throw new Error("Invalid subtype: must be a non-empty string");if(!n(e))throw new Error(`Invalid subtype ${e}: must contain only HTTP token code points`);this.#t=e}get parameters(){return this.#r}toString(){return w(this)}isJavaScript({prohibitParameters:e=!1}={}){switch(this.#e){case"text":switch(this.#t){case"ecmascript":case"javascript":case"javascript1.0":case"javascript1.1":case"javascript1.2":case"javascript1.3":case"javascript1.4":case"javascript1.5":case"jscript":case"livescript":case"x-ecmascript":case"x-javascript":return!e||this.#r.size===0;default:return!1}case"application":switch(this.#t){case"ecmascript":case"javascript":case"x-ecmascript":case"x-javascript":return!e||this.#r.size===0;default:return!1}default:return!1}}isXML(){return this.#t==="xml"&&(this.#e==="text"||this.#e==="application")||this.#t.endsWith("+xml")}isHTML(){return this.#t==="html"&&this.#e==="text"}get[Symbol.toStringTag](){return"MediaType"}};return $(L);})();
|
|
3
3
|
window.MediaType = MediaType.default;
|
|
4
4
|
//# sourceMappingURL=media-type.min.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/media-type.js", "../src/utils.js", "../src/media-type-parameters.js", "../src/parser.js", "../src/serializer.js"],
|
|
4
|
-
"sourcesContent": ["import MediaTypeParameters from './media-type-parameters.js';\nimport parse from './parser.js';\nimport serialize from './serializer.js';\nimport { asciiLowercase, solelyContainsHTTPTokenCodePoints } from './utils.js';\n\n/**\n * Class used to parse media types.\n *\n * @module {MediaType} media-type\n * @see https://mimesniff.spec.whatwg.org/#understanding-mime-types\n */\nexport default class MediaType {\n\t/** @type {string} */\n\t#type;\n\t/** @type {string} */\n\t#subtype;\n\t/** @type {MediaTypeParameters} */\n\t#parameters;\n\n\t/**\n\t * Create a new MediaType instance from a string representation.\n\t *\n\t * @param {string|object} options The media type string or an object with type, subtype, and parameters.\n\t */\n\tconstructor(options) {\n\t\t// If options is a string, parse it\n\t\tif (typeof options === 'string') {\n\t\t\toptions = parse(options);\n\t\t}\n\n\t\tconst { type, subtype, parameters = new MediaTypeParameters() } = options;\n\t\tthis.#type = type;\n\t\tthis.#subtype = subtype;\n\n\t\t// Check if parameters are provided as a Map\n\t\tthis.#parameters = typeof parameters == Map && parameters.size > 0\t? new MediaTypeParameters(parameters.entries())\t: parameters;\n\t}\n\n\t/**\n\t * Static factory method for parsing a media type.\n\t *\n\t * @param {string} string The media type to parse.\n\t * @returns {MediaType} The parsed {@link MediaType} object or null if the string could not be parsed.\n\t */\n\tstatic parse(string) {\n\t\ttry {\n\t\t\treturn new MediaType(parse(string));\n\t\t} catch (e) {\n\t\t\tthrow new Error(`Could not parse media type string '${string}'`);\n\t\t}\n\t}\n\n\t/**\n\t * Gets the media type essence (type/subtype).\n\t *\n\t * @returns {string} The media type without any parameters\n\t */\n\tget essence() {\n\t\treturn `${this.#type}/${this.#subtype}`;\n\t}\n\n\t/**\n\t * Gets the type.\n\t *\n\t * @returns {string} The type.\n\t */\n\tget type() {\n\t\treturn this.#type;\n\t}\n\n\t/**\n\t * Sets the type.\n\t */\n\tset type(value) {\n\t\tvalue = asciiLowercase(String(value));\n\n\t\tif (value.length === 0) {\n\t\t\tthrow new Error('Invalid type: must be a non-empty string');\n\t\t}\n\t\tif (!solelyContainsHTTPTokenCodePoints(value)) {\n\t\t\tthrow new Error(`Invalid type ${value}: must contain only HTTP token code points`);\n\t\t}\n\n\t\tthis.#type = value;\n\t}\n\n\t/**\n\t * Gets the subtype.\n\t *\n\t * @returns {string} The subtype.\n\t */\n\tget subtype() {\n\t\treturn this.#subtype;\n\t}\n\n\t/**\n\t * Sets the subtype.\n\t */\n\tset subtype(value) {\n\t\tvalue = asciiLowercase(String(value));\n\n\t\tif (value.length === 0) {\n\t\t\tthrow new Error('Invalid subtype: must be a non-empty string');\n\t\t}\n\t\tif (!solelyContainsHTTPTokenCodePoints(value)) {\n\t\t\tthrow new Error(`Invalid subtype ${value}: must contain only HTTP token code points`);\n\t\t}\n\n\t\tthis.#subtype = value;\n\t}\n\n\t/**\n\t * Gets the parameters.\n\t *\n\t * @returns {MediaTypeParameters} The media type parameters.\n\t */\n\tget parameters() {\n\t\treturn this.#parameters;\n\t}\n\n\t/**\n\t * Gets the serialized version of the media type.\n\t *\n\t * @returns {string} The serialized media type.\n\t */\n\ttoString() {\n\t\t// The serialize function works on both 'media type records' (i.e. the results of parse) and on this class, since\n\t\t// this class's interface is identical.\n\t\treturn serialize(this);\n\t}\n\n\t/**\n\t * Determines if this instance is a JavaScript media type.\n\t *\n\t * @param {Object} [options] Optional options.\n\t * @param {boolean} [options.prohibitParameters=false] The option to prohibit parameters when checking if the media type is JavaScript.\n\t * @returns {boolean} true if this instance represents a JavaScript media type, false otherwise.\n\t */\n\tisJavaScript({prohibitParameters = false} = {}) {\n\t\tswitch (this.#type) {\n\t\t\tcase 'text': {\n\t\t\t\tswitch (this.#subtype) {\n\t\t\t\t\tcase 'ecmascript':\n\t\t\t\t\tcase 'javascript':\n\t\t\t\t\tcase 'javascript1.0':\n\t\t\t\t\tcase 'javascript1.1':\n\t\t\t\t\tcase 'javascript1.2':\n\t\t\t\t\tcase 'javascript1.3':\n\t\t\t\t\tcase 'javascript1.4':\n\t\t\t\t\tcase 'javascript1.5':\n\t\t\t\t\tcase 'jscript':\n\t\t\t\t\tcase 'livescript':\n\t\t\t\t\tcase 'x-ecmascript':\n\t\t\t\t\tcase 'x-javascript': return !prohibitParameters || this.#parameters.size === 0;\n\t\t\t\t\tdefault: return false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcase 'application': {\n\t\t\t\tswitch (this.#subtype) {\n\t\t\t\t\tcase 'ecmascript':\n\t\t\t\t\tcase 'javascript':\n\t\t\t\t\tcase 'x-ecmascript':\n\t\t\t\t\tcase 'x-javascript': return !prohibitParameters || this.#parameters.size === 0;\n\t\t\t\t\tdefault: return false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdefault: return false;\n\t\t}\n\t}\n\n\t/**\n\t * Determines if this instance is an XML media type.\n\t *\n\t * @returns {boolean} true if this instance represents an XML media type, false otherwise.\n\t */\n\tisXML() {\n\t\treturn (this.#subtype === 'xml' && (this.#type === 'text' || this.#type === 'application')) || this.#subtype.endsWith('+xml');\n\t}\n\n\t/**\n\t * Determines if this instance is an HTML media type.\n\t *\n\t * @returns {boolean} true if this instance represents an HTML media type, false otherwise.\n\t */\n\tisHTML() {\n\t\treturn this.#subtype === 'html' && this.#type === 'text';\n\t}\n\n\t/**\n\t * Gets the name of the class.\n\t *\n\t * @returns {string} The class name\n\t */\n\tget [Symbol.toStringTag]() {\n\t\treturn 'MediaType';\n\t}\n}", "/** @module utils */\n\nconst whitespaceCharacters = [' ', '\\t', '\\n', '\\r'];\nconst leadingWhitespace = /^[ \\t\\n\\r]+/u;\nconst trailingWhitespace = /[ \\t\\n\\r]+$/u;\nconst httpTokenCodePoints = /^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/u;\nconst httpQuotedTokenCodePoints = /^[\\t\\u0020-\\u007E\\u0080-\\u00FF]*$/u;\n\n/**\n * A function to remove any leading and trailing HTTP whitespace.\n *\n * @param {string} string The string to process.\n * @returns {string} The processed string.\n */\nconst removeLeadingAndTrailingHTTPWhitespace = (string) => string.replace(leadingWhitespace, '').replace(trailingWhitespace, '');\n\n/**\n * A function to remove any trailing HTTP whitespace.\n *\n * @param {string} string The string to process.\n * @returns {string} The processed string.\n */\nconst removeTrailingHTTPWhitespace = (string) => string.replace(trailingWhitespace, '');\n\n/**\n * Determines if the provided character is whitespace.\n *\n * @param {string} char The character to evaluate.\n * @returns {boolean} true if the character is whitespace, false otherwise.\n */\nconst isHTTPWhitespaceChar = (char) => whitespaceCharacters.includes(char);\n\n/**\n * Determines if the provided string contains only HTTP token code points.\n *\n * @param {string} string The string to evaluate.\n * @returns {boolean} true if the string contains only HTTP token code points.\n */\nconst solelyContainsHTTPTokenCodePoints = (string) => httpTokenCodePoints.test(string);\n\n/**\n * Determines if the provided string contains only quoted HTTP token code points.\n *\n * @param {string} string The string to evaluate.\n * @returns {boolean} true if the string contains only quoted HTTP token code points.\n */\nconst solelyContainsHTTPQuotedStringTokenCodePoints = (string) => httpQuotedTokenCodePoints.test(string);\n\n/**\n * A function to lower case ASCII characters.\n * This implementation iterates over each element of the string, which is treated as an iterable.\n * The elements are destructured into [char, charCode] pairs, where char represents the character,\n * and charCode is assigned the character code using char.charCodeAt(0). If the charCode is not\n * provided, it falls back to the character code obtained from char.charCodeAt(0).\n *\n * @param {string} string The string to process.\n * @returns {string} The processed string with all ASCII characters lower cased.\n */\nconst asciiLowercase = (string) => {\n\tlet result = '';\n\tfor (const [char, charCode = char.charCodeAt(0)] of string) {\n\t\tresult += charCode >= 65 && charCode <= 90 ? String.fromCharCode(charCode + 32) : char;\n\t}\n\n\treturn result;\n};\n\n/**\n * Collects all the HTTP quoted strings.\n * This variant only implements it with the extract-value flag set.\n *\n * @param {string} input The string to process.\n * @param {number} position The starting position.\n * @returns {Array<string|number>} An array that includes the resulting string and updated position.\n */\nconst collectAnHTTPQuotedString = (input, position) => {\n\tlet value = '';\n\n\tfor (let length = input.length, char; ++position < length;) {\n\t\tchar = input[position];\n\n\t\tif (char == '\\\\') {\n\t\t\tvalue += ++position < length ? input[position] : char;\n\t\t} else if (char == '\"') {\n\t\t\tbreak;\n\t\t} else {\n\t\t\tvalue += char;\n\t\t}\n\t}\n\n\treturn [value, position];\n};\n\nexport { removeLeadingAndTrailingHTTPWhitespace, removeTrailingHTTPWhitespace, isHTTPWhitespaceChar, solelyContainsHTTPTokenCodePoints, solelyContainsHTTPQuotedStringTokenCodePoints, asciiLowercase, collectAnHTTPQuotedString };\n", "import { asciiLowercase, solelyContainsHTTPQuotedStringTokenCodePoints, solelyContainsHTTPTokenCodePoints } from './utils.js';\n\n/**\n * Class representing the parameters for a media type record.\n * This class has the equivalent surface API to a JavaScript {@link Map}.\n *\n * However, {@link MediaTypeParameters} methods will always interpret their arguments\n * as appropriate for media types, so parameter names will be lowercased,\n * and attempting to set invalid characters will throw an {@link Error}.\n *\n * @example charset=utf-8\n * @see https://mimesniff.spec.whatwg.org/#mime-type-essence\n * @see https://mimesniff.spec.whatwg.org/#mime-type-essence-record\n * @see https://mimesniff.spec.whatwg.org/#mime-type-essence-record-creation\n * @see https://mimesniff.spec.whatwg.org/#mime-type-essence-record-creation-algorithm\n * @module {MediaTypeParameters} media-type-parameters\n * @author D1g1talEntr0py <jason.dimeo@gmail.com>\n */\nexport default class MediaTypeParameters {\n\t/** @type {Map<string, string>} */\n\t#map;\n\n\t/**\n\t * Create a new MediaTypeParameters instance.\n\t *\n\t * @param {Map<string, string>} map The map of parameters for a media type.\n\t */\n\tconstructor(map = new Map()) {\n\t\tthis.#map = map;\n\t}\n\n\t/**\n\t * Gets the number of media type parameters.\n\t *\n\t * @returns {number} The number of media type parameters\n\t */\n\tget size() {\n\t\treturn this.#map.size;\n\t}\n\n\t/**\n\t * Gets the media type parameter value for the supplied name.\n\t *\n\t * @param {string} name The name of the media type parameter to retrieve.\n\t * @returns {string} The media type parameter value.\n\t */\n\tget(name) {\n\t\treturn this.#map.get(asciiLowercase(String(name)));\n\t}\n\n\t/**\n\t * Indicates whether the media type parameter with the specified name exists or not.\n\t *\n\t * @param {string} name The name of the media type parameter to check.\n\t * @returns {boolean} true if the media type parameter exists, false otherwise.\n\t */\n\thas(name) {\n\t\treturn this.#map.has(asciiLowercase(String(name)));\n\t}\n\n\t/**\n\t * Adds a new media type parameter using the specified name and value to the MediaTypeParameters.\n\t * If an parameter with the same name already exists, the parameter will be updated.\n\t *\n\t * @param {string} name The name of the media type parameter to set.\n\t * @param {string} value The media type parameter value.\n\t * @returns {MediaTypeParameters} This instance.\n\t */\n\tset(name, value) {\n\t\tname = asciiLowercase(String(name));\n\t\tvalue = String(value);\n\n\t\tif (!solelyContainsHTTPTokenCodePoints(name)) {\n\t\t\tthrow new Error(`Invalid media type parameter name \"${name}\": only HTTP token code points are valid.`);\n\t\t}\n\n\t\tif (!solelyContainsHTTPQuotedStringTokenCodePoints(value)) {\n\t\t\tthrow new Error(`Invalid media type parameter value \"${value}\": only HTTP quoted-string token code points are valid.`);\n\t\t}\n\n\t\tthis.#map.set(name, value);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Clears all the media type parameters.\n\t */\n\tclear() {\n\t\tthis.#map.clear();\n\t}\n\n\t/**\n\t * Removes the media type parameter using the specified name.\n\t *\n\t * @param {string} name The name of the media type parameter to delete.\n\t * @returns {boolean} true if the parameter existed and has been removed, or false if the parameter does not exist.\n\t */\n\tdelete(name) {\n\t\tname = asciiLowercase(String(name));\n\t\treturn this.#map.delete(name);\n\t}\n\n\t/**\n\t * Executes a provided function once per each name/value pair in the MediaTypeParameters, in insertion order.\n\t *\n\t * @param {function(string, string): void} callback The function called on each iteration.\n\t * @param {*} [thisArg] Optional object when binding 'this' to the callback.\n\t */\n\tforEach(callback, thisArg) {\n\t\tthis.#map.forEach(callback, thisArg);\n\t}\n\n\t/**\n\t * Returns an iterable of parameter names.\n\t *\n\t * @returns {IterableIterator<string>} The {@link IterableIterator} of media type parameter names.\n\t */\n\tkeys() {\n\t\treturn this.#map.keys();\n\t}\n\n\t/**\n\t * Returns an iterable of parameter values.\n\t *\n\t * @returns {IterableIterator<string>} The {@link IterableIterator} of media type parameter values.\n\t */\n\tvalues() {\n\t\treturn this.#map.values();\n\t}\n\n\t/**\n\t * Returns an iterable of name, value pairs for every parameter entry in the media type parameters.\n\t *\n\t * @returns {IterableIterator<Array<Array<string>>>} The media type parameter entries.\n\t */\n\tentries() {\n\t\treturn this.#map.entries();\n\t}\n\n\t/**\n\t * A method that returns the default iterator for the {@link MediaTypeParameters}. Called by the semantics of the for-of statement.\n\t *\n\t * @returns {Iterator<string, string, undefined>} The {@link Symbol.iterator} for the media type parameters.\n\t */\n\t[Symbol.iterator]() {\n\t\treturn this.#map[Symbol.iterator]();\n\t}\n\n\t/**\n\t * Returns a string representation of the media type parameters.\n\t * This method is called by the `String()` function.\n\t *\n\t * @example\n\t * const parameters = new MediaTypeParameters(new Map([['charset', 'utf-8']]));\n\t * String(parameters); // 'charset=utf-8'\n\t * parameters.toString(); // 'charset=utf-8'\n\t * parameters + ''; // 'charset=utf-8'\n\t * `${parameters}`; // 'charset=utf-8'\n\t * parameters[Symbol.toStringTag]; // 'MediaTypeParameters'\n\t * parameters[Symbol.toStringTag](); // 'MediaTypeParameters'\n\t * Object.prototype.toString.call(parameters); // '[object MediaTypeParameters]'\n\t * parameters + ''; // 'charset=utf-8'\n\t * @returns {string} The string representation of the media type parameters.\n\t */\n\t[Symbol.toStringTag]() {\n\t\treturn 'MediaTypeParameters';\n\t}\n}", "import {\n\tasciiLowercase,\n\tcollectAnHTTPQuotedString, isHTTPWhitespaceChar, removeLeadingAndTrailingHTTPWhitespace,\n\tremoveTrailingHTTPWhitespace, solelyContainsHTTPQuotedStringTokenCodePoints, solelyContainsHTTPTokenCodePoints\n} from './utils.js';\n\n/**\n * Function to parse a media type.\n *\n * @module parser\n * @param {string} input The media type to parse\n * @returns {{ type: string, subtype: string, parameters: Map<string, string> }} An object populated with the parsed media type properties and any parameters.\n */\nconst parse = (input) => {\n\tinput = removeLeadingAndTrailingHTTPWhitespace(input);\n\n\tlet position = 0;\n\tlet type = '';\n\twhile (position < input.length && input[position] != '/') {\n\t\ttype += input[position];\n\t\t++position;\n\t}\n\n\tif (type.length === 0 || !solelyContainsHTTPTokenCodePoints(type)) {\n\t\treturn null;\n\t}\n\n\tif (position >= input.length) {\n\t\treturn null;\n\t}\n\n\t// Skips past \"/\"\n\t++position;\n\n\tlet subtype = '';\n\twhile (position < input.length && input[position] != ';') {\n\t\tsubtype += input[position];\n\t\t++position;\n\t}\n\n\tsubtype = removeTrailingHTTPWhitespace(subtype);\n\n\tif (subtype.length === 0 || !solelyContainsHTTPTokenCodePoints(subtype)) {\n\t\treturn null;\n\t}\n\n\tconst mediaType = {\n\t\ttype: asciiLowercase(type),\n\t\tsubtype: asciiLowercase(subtype),\n\t\tparameters: new Map()\n\t};\n\n\twhile (position < input.length) {\n\t\t// Skip past \";\"\n\t\t++position;\n\n\t\twhile (isHTTPWhitespaceChar(input[position])) {\n\t\t\t++position;\n\t\t}\n\n\t\tlet parameterName = '';\n\t\twhile (position < input.length && input[position] != ';' && input[position] != '=') {\n\t\t\tparameterName += input[position];\n\t\t\t++position;\n\t\t}\n\t\tparameterName = asciiLowercase(parameterName);\n\n\t\tif (position < input.length) {\n\t\t\tif (input[position] == ';') {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Skip past \"=\"\n\t\t\t++position;\n\t\t}\n\n\t\tlet parameterValue = null;\n\t\tif (input[position] == '\"') {\n\t\t\t[parameterValue, position] = collectAnHTTPQuotedString(input, position);\n\n\t\t\twhile (position < input.length && input[position] != ';') {\n\t\t\t\t++position;\n\t\t\t}\n\t\t} else {\n\t\t\tparameterValue = '';\n\t\t\twhile (position < input.length && input[position] != ';') {\n\t\t\t\tparameterValue += input[position];\n\t\t\t\t++position;\n\t\t\t}\n\n\t\t\tparameterValue = removeTrailingHTTPWhitespace(parameterValue);\n\n\t\t\tif (parameterValue === '') {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\n\t\tif (parameterName.length > 0 &&\tsolelyContainsHTTPTokenCodePoints(parameterName) &&\tsolelyContainsHTTPQuotedStringTokenCodePoints(parameterValue) && !mediaType.parameters.has(parameterName)) {\n\t\t\tmediaType.parameters.set(parameterName, parameterValue);\n\t\t}\n\t}\n\n\treturn mediaType;\n};\n\nexport default parse;", "import { solelyContainsHTTPTokenCodePoints } from './utils.js';\n\n/** @typedef { import('./media-type.js').default } MediaType */\n\n/**\n * A function that serializes the provided {@link mediaType} to a string.\n *\n * @module serializer\n * @param {MediaType} mediaType The media type to serialize.\n * @returns {string} The serialized media type.\n */\nconst serialize = (mediaType) => {\n\tlet serialization = `${mediaType.type}/${mediaType.subtype}`;\n\n\tif (mediaType.parameters.size === 0) {\n\t\treturn serialization;\n\t}\n\n\tfor (let [name, value] of mediaType.parameters) {\n\t\tserialization += `;${name}=`;\n\n\t\tif (!solelyContainsHTTPTokenCodePoints(value) || value.length === 0) {\n\t\t\tvalue = `\"${value.replace(/([\"\\\\])/ug, '\\\\$1')}\"`;\n\t\t}\n\n\t\tserialization += value;\n\t}\n\n\treturn serialization;\n};\n\nexport default serialize;"],
|
|
5
|
-
"mappings": "gbAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,ICEA,IAAMC,EAAuB,CAAC,IAAK,IAAM;AAAA,EAAM,IAAI,EAC7CC,EAAoB,eACpBC,EAAqB,eACrBC,EAAsB,iCACtBC,EAA4B,qCAQ5BC,EAA0CC,GAAWA,EAAO,QAAQL,EAAmB,EAAE,EAAE,QAAQC,EAAoB,EAAE,EAQzHK,EAAgCD,GAAWA,EAAO,QAAQJ,EAAoB,EAAE,EAQhFM,EAAwBC,GAAST,EAAqB,SAASS,CAAI,EAQnEC,EAAqCJ,GAAWH,EAAoB,KAAKG,CAAM,EAQ/EK,EAAiDL,GAAWF,EAA0B,KAAKE,CAAM,EAYjGM,EAAkBN,GAAW,CAClC,IAAIO,EAAS,GACb,OAAW,CAACJ,EAAMK,EAAWL,EAAK,WAAW,CAAC,CAAC,IAAKH,EACnDO,GAAUC,GAAY,IAAMA,GAAY,GAAK,OAAO,aAAaA,EAAW,EAAE,EAAIL,EAGnF,OAAOI,CACR,EAUME,EAA4B,CAACC,EAAOC,IAAa,CACtD,IAAIC,EAAQ,GAEZ,QAASC,EAASH,EAAM,OAAQP,EAAM,EAAEQ,EAAWE,GAGlD,GAFAV,EAAOO,EAAMC,CAAQ,EAEjBR,GAAQ,KACXS,GAAS,EAAED,EAAWE,EAASH,EAAMC,CAAQ,EAAIR,MAC3C,IAAIA,GAAQ,IAClB,MAEAS,GAAST,EAIX,MAAO,CAACS,EAAOD,CAAQ,CACxB,ECzEA,IAAqBG,EAArB,KAAyC,CAExCC,GAOA,YAAYC,
|
|
6
|
-
"names": ["media_type_exports", "__export", "MediaType", "whitespaceCharacters", "leadingWhitespace", "trailingWhitespace", "httpTokenCodePoints", "httpQuotedTokenCodePoints", "removeLeadingAndTrailingHTTPWhitespace", "string", "removeTrailingHTTPWhitespace", "isHTTPWhitespaceChar", "char", "solelyContainsHTTPTokenCodePoints", "solelyContainsHTTPQuotedStringTokenCodePoints", "asciiLowercase", "result", "charCode", "collectAnHTTPQuotedString", "input", "position", "value", "length", "MediaTypeParameters", "#map", "
|
|
4
|
+
"sourcesContent": ["import MediaTypeParameters from './media-type-parameters.js';\nimport parse from './parser.js';\nimport serialize from './serializer.js';\nimport { asciiLowercase, solelyContainsHTTPTokenCodePoints } from './utils.js';\n\n/**\n * Class used to parse media types.\n *\n * @module {MediaType} media-type\n * @see https://mimesniff.spec.whatwg.org/#understanding-mime-types\n */\nexport default class MediaType {\n\t/** @type {string} */\n\t#type;\n\t/** @type {string} */\n\t#subtype;\n\t/** @type {MediaTypeParameters} */\n\t#parameters;\n\n\t/**\n\t * Create a new MediaType instance from a string representation.\n\t *\n\t * @param {string} mediaType The media type to parse.\n\t * @param {Object} [parameters] Optional parameters.\n\t */\n\tconstructor(mediaType, parameters = {}) {\n\t\tconst { type, subtype, parameters: parsedParameters } = parse(mediaType);\n\t\tthis.#type = type;\n\t\tthis.#subtype = subtype;\n\t\tthis.#parameters = new MediaTypeParameters([...parsedParameters, ...Object.entries(parameters).map(([name, value]) => [asciiLowercase(name), asciiLowercase(value)])]);\n\t}\n\n\t/**\n\t * Static factory method for parsing a media type.\n\t *\n\t * @param {string} string The media type to parse.\n\t * @returns {MediaType} The parsed {@link MediaType} object or null if the string could not be parsed.\n\t */\n\tstatic parse(string) {\n\t\ttry {\n\t\t\treturn new MediaType(string);\n\t\t} catch (e) {\n\t\t\tthrow new Error(`Could not parse media type string '${string}'`);\n\t\t}\n\t}\n\n\t/**\n\t * Gets the media type essence (type/subtype).\n\t *\n\t * @returns {string} The media type without any parameters\n\t */\n\tget essence() {\n\t\treturn `${this.#type}/${this.#subtype}`;\n\t}\n\n\t/**\n\t * Gets the type.\n\t *\n\t * @returns {string} The type.\n\t */\n\tget type() {\n\t\treturn this.#type;\n\t}\n\n\t/**\n\t * Sets the type.\n\t */\n\tset type(value) {\n\t\tvalue = asciiLowercase(String(value));\n\n\t\tif (value.length === 0) {\n\t\t\tthrow new Error('Invalid type: must be a non-empty string');\n\t\t}\n\t\tif (!solelyContainsHTTPTokenCodePoints(value)) {\n\t\t\tthrow new Error(`Invalid type ${value}: must contain only HTTP token code points`);\n\t\t}\n\n\t\tthis.#type = value;\n\t}\n\n\t/**\n\t * Gets the subtype.\n\t *\n\t * @returns {string} The subtype.\n\t */\n\tget subtype() {\n\t\treturn this.#subtype;\n\t}\n\n\t/**\n\t * Sets the subtype.\n\t */\n\tset subtype(value) {\n\t\tvalue = asciiLowercase(String(value));\n\n\t\tif (value.length === 0) {\n\t\t\tthrow new Error('Invalid subtype: must be a non-empty string');\n\t\t}\n\t\tif (!solelyContainsHTTPTokenCodePoints(value)) {\n\t\t\tthrow new Error(`Invalid subtype ${value}: must contain only HTTP token code points`);\n\t\t}\n\n\t\tthis.#subtype = value;\n\t}\n\n\t/**\n\t * Gets the parameters.\n\t *\n\t * @returns {MediaTypeParameters} The media type parameters.\n\t */\n\tget parameters() {\n\t\treturn this.#parameters;\n\t}\n\n\t/**\n\t * Gets the serialized version of the media type.\n\t *\n\t * @returns {string} The serialized media type.\n\t */\n\ttoString() {\n\t\t// The serialize function works on both 'media type records' (i.e. the results of parse) and on this class, since\n\t\t// this class's interface is identical.\n\t\treturn serialize(this);\n\t}\n\n\t/**\n\t * Determines if this instance is a JavaScript media type.\n\t *\n\t * @param {Object} [options] Optional options.\n\t * @param {boolean} [options.prohibitParameters=false] The option to prohibit parameters when checking if the media type is JavaScript.\n\t * @returns {boolean} true if this instance represents a JavaScript media type, false otherwise.\n\t */\n\tisJavaScript({prohibitParameters = false} = {}) {\n\t\tswitch (this.#type) {\n\t\t\tcase 'text': {\n\t\t\t\tswitch (this.#subtype) {\n\t\t\t\t\tcase 'ecmascript':\n\t\t\t\t\tcase 'javascript':\n\t\t\t\t\tcase 'javascript1.0':\n\t\t\t\t\tcase 'javascript1.1':\n\t\t\t\t\tcase 'javascript1.2':\n\t\t\t\t\tcase 'javascript1.3':\n\t\t\t\t\tcase 'javascript1.4':\n\t\t\t\t\tcase 'javascript1.5':\n\t\t\t\t\tcase 'jscript':\n\t\t\t\t\tcase 'livescript':\n\t\t\t\t\tcase 'x-ecmascript':\n\t\t\t\t\tcase 'x-javascript': return !prohibitParameters || this.#parameters.size === 0;\n\t\t\t\t\tdefault: return false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcase 'application': {\n\t\t\t\tswitch (this.#subtype) {\n\t\t\t\t\tcase 'ecmascript':\n\t\t\t\t\tcase 'javascript':\n\t\t\t\t\tcase 'x-ecmascript':\n\t\t\t\t\tcase 'x-javascript': return !prohibitParameters || this.#parameters.size === 0;\n\t\t\t\t\tdefault: return false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tdefault: return false;\n\t\t}\n\t}\n\n\t/**\n\t * Determines if this instance is an XML media type.\n\t *\n\t * @returns {boolean} true if this instance represents an XML media type, false otherwise.\n\t */\n\tisXML() {\n\t\treturn (this.#subtype === 'xml' && (this.#type === 'text' || this.#type === 'application')) || this.#subtype.endsWith('+xml');\n\t}\n\n\t/**\n\t * Determines if this instance is an HTML media type.\n\t *\n\t * @returns {boolean} true if this instance represents an HTML media type, false otherwise.\n\t */\n\tisHTML() {\n\t\treturn this.#subtype === 'html' && this.#type === 'text';\n\t}\n\n\t/**\n\t * Gets the name of the class.\n\t *\n\t * @returns {string} The class name\n\t */\n\tget [Symbol.toStringTag]() {\n\t\treturn 'MediaType';\n\t}\n}", "/** @module utils */\n\nconst whitespaceCharacters = [' ', '\\t', '\\n', '\\r'];\nconst leadingWhitespace = /^[ \\t\\n\\r]+/u;\nconst trailingWhitespace = /[ \\t\\n\\r]+$/u;\nconst httpTokenCodePoints = /^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/u;\nconst httpQuotedTokenCodePoints = /^[\\t\\u0020-\\u007E\\u0080-\\u00FF]*$/u;\n\n/**\n * A function to remove any leading and trailing HTTP whitespace.\n *\n * @param {string} string The string to process.\n * @returns {string} The processed string.\n */\nconst removeLeadingAndTrailingHTTPWhitespace = (string) => string.replace(leadingWhitespace, '').replace(trailingWhitespace, '');\n\n/**\n * A function to remove any trailing HTTP whitespace.\n *\n * @param {string} string The string to process.\n * @returns {string} The processed string.\n */\nconst removeTrailingHTTPWhitespace = (string) => string.replace(trailingWhitespace, '');\n\n/**\n * Determines if the provided character is whitespace.\n *\n * @param {string} char The character to evaluate.\n * @returns {boolean} true if the character is whitespace, false otherwise.\n */\nconst isHTTPWhitespaceChar = (char) => whitespaceCharacters.includes(char);\n\n/**\n * Determines if the provided string contains only HTTP token code points.\n *\n * @param {string} string The string to evaluate.\n * @returns {boolean} true if the string contains only HTTP token code points.\n */\nconst solelyContainsHTTPTokenCodePoints = (string) => httpTokenCodePoints.test(string);\n\n/**\n * Determines if the provided string contains only quoted HTTP token code points.\n *\n * @param {string} string The string to evaluate.\n * @returns {boolean} true if the string contains only quoted HTTP token code points.\n */\nconst solelyContainsHTTPQuotedStringTokenCodePoints = (string) => httpQuotedTokenCodePoints.test(string);\n\n/**\n * A function to lower case ASCII characters.\n * This implementation iterates over each element of the string, which is treated as an iterable.\n * The elements are destructured into [char, charCode] pairs, where char represents the character,\n * and charCode is assigned the character code using char.charCodeAt(0). If the charCode is not\n * provided, it falls back to the character code obtained from char.charCodeAt(0).\n *\n * @param {string} string The string to process.\n * @returns {string} The processed string with all ASCII characters lower cased.\n */\nconst asciiLowercase = (string) => {\n\tlet result = '';\n\tfor (const [char, charCode = char.charCodeAt(0)] of string) {\n\t\tresult += charCode >= 65 && charCode <= 90 ? String.fromCharCode(charCode + 32) : char;\n\t}\n\n\treturn result;\n};\n\n/**\n * Collects all the HTTP quoted strings.\n * This variant only implements it with the extract-value flag set.\n *\n * @param {string} input The string to process.\n * @param {number} position The starting position.\n * @returns {Array<string|number>} An array that includes the resulting string and updated position.\n */\nconst collectAnHTTPQuotedString = (input, position) => {\n\tlet value = '';\n\n\tfor (let length = input.length, char; ++position < length;) {\n\t\tchar = input[position];\n\n\t\tif (char == '\\\\') {\n\t\t\tvalue += ++position < length ? input[position] : char;\n\t\t} else if (char == '\"') {\n\t\t\tbreak;\n\t\t} else {\n\t\t\tvalue += char;\n\t\t}\n\t}\n\n\treturn [value, position];\n};\n\nexport { removeLeadingAndTrailingHTTPWhitespace, removeTrailingHTTPWhitespace, isHTTPWhitespaceChar, solelyContainsHTTPTokenCodePoints, solelyContainsHTTPQuotedStringTokenCodePoints, asciiLowercase, collectAnHTTPQuotedString };\n", "import { asciiLowercase, solelyContainsHTTPQuotedStringTokenCodePoints, solelyContainsHTTPTokenCodePoints } from './utils.js';\n\n/**\n * Class representing the parameters for a media type record.\n * This class has the equivalent surface API to a JavaScript {@link Map}.\n *\n * However, {@link MediaTypeParameters} methods will always interpret their arguments\n * as appropriate for media types, so parameter names will be lowercased,\n * and attempting to set invalid characters will throw an {@link Error}.\n *\n * @example charset=utf-8\n * @see https://mimesniff.spec.whatwg.org/#mime-type-essence\n * @see https://mimesniff.spec.whatwg.org/#mime-type-essence-record\n * @see https://mimesniff.spec.whatwg.org/#mime-type-essence-record-creation\n * @see https://mimesniff.spec.whatwg.org/#mime-type-essence-record-creation-algorithm\n * @module {MediaTypeParameters} media-type-parameters\n * @author D1g1talEntr0py <jason.dimeo@gmail.com>\n */\nexport default class MediaTypeParameters {\n\t/** @type {Map<string, string>} */\n\t#map;\n\n\t/**\n\t * Create a new MediaTypeParameters instance.\n\t *\n\t * @param {Array<Array<string>>} entries An array of [name, value] tuples.\n\t */\n\tconstructor(entries) {\n\t\tthis.#map = new Map(entries);\n\t}\n\n\t/**\n\t * Gets the number of media type parameters.\n\t *\n\t * @returns {number} The number of media type parameters\n\t */\n\tget size() {\n\t\treturn this.#map.size;\n\t}\n\n\t/**\n\t * Gets the media type parameter value for the supplied name.\n\t *\n\t * @param {string} name The name of the media type parameter to retrieve.\n\t * @returns {string} The media type parameter value.\n\t */\n\tget(name) {\n\t\treturn this.#map.get(asciiLowercase(String(name)));\n\t}\n\n\t/**\n\t * Indicates whether the media type parameter with the specified name exists or not.\n\t *\n\t * @param {string} name The name of the media type parameter to check.\n\t * @returns {boolean} true if the media type parameter exists, false otherwise.\n\t */\n\thas(name) {\n\t\treturn this.#map.has(asciiLowercase(String(name)));\n\t}\n\n\t/**\n\t * Adds a new media type parameter using the specified name and value to the MediaTypeParameters.\n\t * If an parameter with the same name already exists, the parameter will be updated.\n\t *\n\t * @param {string} name The name of the media type parameter to set.\n\t * @param {string} value The media type parameter value.\n\t * @returns {MediaTypeParameters} This instance.\n\t */\n\tset(name, value) {\n\t\tname = asciiLowercase(String(name));\n\t\tvalue = String(value);\n\n\t\tif (!solelyContainsHTTPTokenCodePoints(name)) {\n\t\t\tthrow new Error(`Invalid media type parameter name \"${name}\": only HTTP token code points are valid.`);\n\t\t}\n\n\t\tif (!solelyContainsHTTPQuotedStringTokenCodePoints(value)) {\n\t\t\tthrow new Error(`Invalid media type parameter value \"${value}\": only HTTP quoted-string token code points are valid.`);\n\t\t}\n\n\t\tthis.#map.set(name, value);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Clears all the media type parameters.\n\t */\n\tclear() {\n\t\tthis.#map.clear();\n\t}\n\n\t/**\n\t * Removes the media type parameter using the specified name.\n\t *\n\t * @param {string} name The name of the media type parameter to delete.\n\t * @returns {boolean} true if the parameter existed and has been removed, or false if the parameter does not exist.\n\t */\n\tdelete(name) {\n\t\tname = asciiLowercase(String(name));\n\t\treturn this.#map.delete(name);\n\t}\n\n\t/**\n\t * Executes a provided function once per each name/value pair in the MediaTypeParameters, in insertion order.\n\t *\n\t * @param {function(string, string): void} callback The function called on each iteration.\n\t * @param {*} [thisArg] Optional object when binding 'this' to the callback.\n\t */\n\tforEach(callback, thisArg) {\n\t\tthis.#map.forEach(callback, thisArg);\n\t}\n\n\t/**\n\t * Returns an iterable of parameter names.\n\t *\n\t * @returns {IterableIterator<string>} The {@link IterableIterator} of media type parameter names.\n\t */\n\tkeys() {\n\t\treturn this.#map.keys();\n\t}\n\n\t/**\n\t * Returns an iterable of parameter values.\n\t *\n\t * @returns {IterableIterator<string>} The {@link IterableIterator} of media type parameter values.\n\t */\n\tvalues() {\n\t\treturn this.#map.values();\n\t}\n\n\t/**\n\t * Returns an iterable of name, value pairs for every parameter entry in the media type parameters.\n\t *\n\t * @returns {IterableIterator<Array<Array<string>>>} The media type parameter entries.\n\t */\n\tentries() {\n\t\treturn this.#map.entries();\n\t}\n\n\t/**\n\t * A method that returns the default iterator for the {@link MediaTypeParameters}. Called by the semantics of the for-of statement.\n\t *\n\t * @returns {Iterator<string, string, undefined>} The {@link Symbol.iterator} for the media type parameters.\n\t */\n\t[Symbol.iterator]() {\n\t\treturn this.#map[Symbol.iterator]();\n\t}\n\n\t/**\n\t * Returns a string representation of the media type parameters.\n\t * This method is called by the `String()` function.\n\t *\n\t * @example\n\t * const parameters = new MediaTypeParameters(new Map([['charset', 'utf-8']]));\n\t * String(parameters); // 'charset=utf-8'\n\t * parameters.toString(); // 'charset=utf-8'\n\t * parameters + ''; // 'charset=utf-8'\n\t * `${parameters}`; // 'charset=utf-8'\n\t * parameters[Symbol.toStringTag]; // 'MediaTypeParameters'\n\t * parameters[Symbol.toStringTag](); // 'MediaTypeParameters'\n\t * Object.prototype.toString.call(parameters); // '[object MediaTypeParameters]'\n\t * parameters + ''; // 'charset=utf-8'\n\t * @returns {string} The string representation of the media type parameters.\n\t */\n\t[Symbol.toStringTag]() {\n\t\treturn 'MediaTypeParameters';\n\t}\n}", "import {\n\tasciiLowercase,\n\tcollectAnHTTPQuotedString, isHTTPWhitespaceChar, removeLeadingAndTrailingHTTPWhitespace,\n\tremoveTrailingHTTPWhitespace, solelyContainsHTTPQuotedStringTokenCodePoints, solelyContainsHTTPTokenCodePoints\n} from './utils.js';\n\n/**\n * Function to parse a media type.\n *\n * @module parser\n * @param {string} input The media type to parse\n * @returns {{ type: string, subtype: string, parameters: Map<string, string> }} An object populated with the parsed media type properties and any parameters.\n */\nconst parse = (input) => {\n\tinput = removeLeadingAndTrailingHTTPWhitespace(input);\n\n\tlet position = 0;\n\tlet type = '';\n\twhile (position < input.length && input[position] != '/') {\n\t\ttype += input[position];\n\t\t++position;\n\t}\n\n\tif (type.length === 0 || !solelyContainsHTTPTokenCodePoints(type)) {\n\t\treturn null;\n\t}\n\n\tif (position >= input.length) {\n\t\treturn null;\n\t}\n\n\t// Skips past \"/\"\n\t++position;\n\n\tlet subtype = '';\n\twhile (position < input.length && input[position] != ';') {\n\t\tsubtype += input[position];\n\t\t++position;\n\t}\n\n\tsubtype = removeTrailingHTTPWhitespace(subtype);\n\n\tif (subtype.length === 0 || !solelyContainsHTTPTokenCodePoints(subtype)) {\n\t\treturn null;\n\t}\n\n\tconst mediaType = {\n\t\ttype: asciiLowercase(type),\n\t\tsubtype: asciiLowercase(subtype),\n\t\tparameters: new Map()\n\t};\n\n\twhile (position < input.length) {\n\t\t// Skip past \";\"\n\t\t++position;\n\n\t\twhile (isHTTPWhitespaceChar(input[position])) {\n\t\t\t++position;\n\t\t}\n\n\t\tlet parameterName = '';\n\t\twhile (position < input.length && input[position] != ';' && input[position] != '=') {\n\t\t\tparameterName += input[position];\n\t\t\t++position;\n\t\t}\n\t\tparameterName = asciiLowercase(parameterName);\n\n\t\tif (position < input.length) {\n\t\t\tif (input[position] == ';') {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Skip past \"=\"\n\t\t\t++position;\n\t\t}\n\n\t\tlet parameterValue = null;\n\t\tif (input[position] == '\"') {\n\t\t\t[parameterValue, position] = collectAnHTTPQuotedString(input, position);\n\n\t\t\twhile (position < input.length && input[position] != ';') {\n\t\t\t\t++position;\n\t\t\t}\n\t\t} else {\n\t\t\tparameterValue = '';\n\t\t\twhile (position < input.length && input[position] != ';') {\n\t\t\t\tparameterValue += input[position];\n\t\t\t\t++position;\n\t\t\t}\n\n\t\t\tparameterValue = removeTrailingHTTPWhitespace(parameterValue);\n\n\t\t\tif (parameterValue === '') {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\n\t\tif (parameterName.length > 0 &&\tsolelyContainsHTTPTokenCodePoints(parameterName) &&\tsolelyContainsHTTPQuotedStringTokenCodePoints(parameterValue) && !mediaType.parameters.has(parameterName)) {\n\t\t\tmediaType.parameters.set(parameterName, parameterValue);\n\t\t}\n\t}\n\n\treturn mediaType;\n};\n\nexport default parse;", "import { solelyContainsHTTPTokenCodePoints } from './utils.js';\n\n/** @typedef { import('./media-type.js').default } MediaType */\n\n/**\n * A function that serializes the provided {@link mediaType} to a string.\n *\n * @module serializer\n * @param {MediaType} mediaType The media type to serialize.\n * @returns {string} The serialized media type.\n */\nconst serialize = (mediaType) => {\n\tlet serialization = `${mediaType.type}/${mediaType.subtype}`;\n\n\tif (mediaType.parameters.size === 0) {\n\t\treturn serialization;\n\t}\n\n\tfor (let [name, value] of mediaType.parameters) {\n\t\tserialization += `;${name}=`;\n\n\t\tif (!solelyContainsHTTPTokenCodePoints(value) || value.length === 0) {\n\t\t\tvalue = `\"${value.replace(/([\"\\\\])/ug, '\\\\$1')}\"`;\n\t\t}\n\n\t\tserialization += value;\n\t}\n\n\treturn serialization;\n};\n\nexport default serialize;"],
|
|
5
|
+
"mappings": "gbAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,ICEA,IAAMC,EAAuB,CAAC,IAAK,IAAM;AAAA,EAAM,IAAI,EAC7CC,EAAoB,eACpBC,EAAqB,eACrBC,EAAsB,iCACtBC,EAA4B,qCAQ5BC,EAA0CC,GAAWA,EAAO,QAAQL,EAAmB,EAAE,EAAE,QAAQC,EAAoB,EAAE,EAQzHK,EAAgCD,GAAWA,EAAO,QAAQJ,EAAoB,EAAE,EAQhFM,EAAwBC,GAAST,EAAqB,SAASS,CAAI,EAQnEC,EAAqCJ,GAAWH,EAAoB,KAAKG,CAAM,EAQ/EK,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AK,eACL,IAAK,eAAgB,MAAO,CAACe,GAAsB,KAAKd,GAAY,OAAS,EAC7E,QAAS,MAAO,EACjB,CAED,IAAK,cACJ,OAAQ,KAAKD,GAAU,CACtB,IAAK,aACL,IAAK,aACL,IAAK,eACL,IAAK,eAAgB,MAAO,CAACe,GAAsB,KAAKd,GAAY,OAAS,EAC7E,QAAS,MAAO,EACjB,CAED,QAAS,MAAO,EACjB,CACD,CAOA,OAAQ,CACP,OAAQ,KAAKD,KAAa,QAAU,KAAKD,KAAU,QAAU,KAAKA,KAAU,gBAAmB,KAAKC,GAAS,SAAS,MAAM,CAC7H,CAOA,QAAS,CACR,OAAO,KAAKA,KAAa,QAAU,KAAKD,KAAU,MACnD,CAOA,IAAK,OAAO,WAAW,GAAI,CAC1B,MAAO,WACR,CACD",
|
|
6
|
+
"names": ["media_type_exports", "__export", "MediaType", "whitespaceCharacters", "leadingWhitespace", "trailingWhitespace", "httpTokenCodePoints", "httpQuotedTokenCodePoints", "removeLeadingAndTrailingHTTPWhitespace", "string", "removeTrailingHTTPWhitespace", "isHTTPWhitespaceChar", "char", "solelyContainsHTTPTokenCodePoints", "solelyContainsHTTPQuotedStringTokenCodePoints", "asciiLowercase", "result", "charCode", "collectAnHTTPQuotedString", "input", "position", "value", "length", "MediaTypeParameters", "#map", "entries", "name", "asciiLowercase", "value", "solelyContainsHTTPTokenCodePoints", "solelyContainsHTTPQuotedStringTokenCodePoints", "callback", "thisArg", "parse", "input", "removeLeadingAndTrailingHTTPWhitespace", "position", "type", "solelyContainsHTTPTokenCodePoints", "subtype", "removeTrailingHTTPWhitespace", "mediaType", "asciiLowercase", "isHTTPWhitespaceChar", "parameterName", "parameterValue", "collectAnHTTPQuotedString", "solelyContainsHTTPQuotedStringTokenCodePoints", "parser_default", "serialize", "mediaType", "serialization", "name", "value", "solelyContainsHTTPTokenCodePoints", "serializer_default", "MediaType", "#type", "#subtype", "#parameters", "mediaType", "parameters", "type", "subtype", "parsedParameters", "parser_default", "MediaTypeParameters", "name", "value", "asciiLowercase", "string", "solelyContainsHTTPTokenCodePoints", "serializer_default", "prohibitParameters"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d1g1tal/media-type",
|
|
3
3
|
"description": "Parses, serializes, and manipulates media types, according to the WHATWG MIME Sniffing Standard",
|
|
4
|
-
"version": "4.0
|
|
4
|
+
"version": "4.1.0",
|
|
5
5
|
"author": "Domenic Denicola <d@domenic.me> (https://domenic.me/)",
|
|
6
6
|
"maintainers": [
|
|
7
7
|
{
|
|
@@ -23,10 +23,10 @@ export default class MediaTypeParameters {
|
|
|
23
23
|
/**
|
|
24
24
|
* Create a new MediaTypeParameters instance.
|
|
25
25
|
*
|
|
26
|
-
* @param {
|
|
26
|
+
* @param {Array<Array<string>>} entries An array of [name, value] tuples.
|
|
27
27
|
*/
|
|
28
|
-
constructor(
|
|
29
|
-
this.#map =
|
|
28
|
+
constructor(entries) {
|
|
29
|
+
this.#map = new Map(entries);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/**
|
package/src/media-type.js
CHANGED
|
@@ -20,20 +20,14 @@ export default class MediaType {
|
|
|
20
20
|
/**
|
|
21
21
|
* Create a new MediaType instance from a string representation.
|
|
22
22
|
*
|
|
23
|
-
* @param {string
|
|
23
|
+
* @param {string} mediaType The media type to parse.
|
|
24
|
+
* @param {Object} [parameters] Optional parameters.
|
|
24
25
|
*/
|
|
25
|
-
constructor(
|
|
26
|
-
|
|
27
|
-
if (typeof options === 'string') {
|
|
28
|
-
options = parse(options);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const { type, subtype, parameters = new MediaTypeParameters() } = options;
|
|
26
|
+
constructor(mediaType, parameters = {}) {
|
|
27
|
+
const { type, subtype, parameters: parsedParameters } = parse(mediaType);
|
|
32
28
|
this.#type = type;
|
|
33
29
|
this.#subtype = subtype;
|
|
34
|
-
|
|
35
|
-
// Check if parameters are provided as a Map
|
|
36
|
-
this.#parameters = typeof parameters == Map && parameters.size > 0 ? new MediaTypeParameters(parameters.entries()) : parameters;
|
|
30
|
+
this.#parameters = new MediaTypeParameters([...parsedParameters, ...Object.entries(parameters).map(([name, value]) => [asciiLowercase(name), asciiLowercase(value)])]);
|
|
37
31
|
}
|
|
38
32
|
|
|
39
33
|
/**
|
|
@@ -44,7 +38,7 @@ export default class MediaType {
|
|
|
44
38
|
*/
|
|
45
39
|
static parse(string) {
|
|
46
40
|
try {
|
|
47
|
-
return new MediaType(
|
|
41
|
+
return new MediaType(string);
|
|
48
42
|
} catch (e) {
|
|
49
43
|
throw new Error(`Could not parse media type string '${string}'`);
|
|
50
44
|
}
|