@d1g1tal/media-type 4.0.2 → 5.0.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.
@@ -1,455 +1,298 @@
1
- var MediaType = (() => {
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/media-type.js
21
- var media_type_exports = {};
22
- __export(media_type_exports, {
23
- default: () => MediaType
24
- });
1
+ // node_modules/.pnpm/@d1g1tal+chrysalis@2.2.0/node_modules/@d1g1tal/chrysalis/src/esm/object-type.js
2
+ var _type = (object) => object?.constructor ?? object?.prototype?.constructor ?? globalThis[Object.prototype.toString.call(object).slice(8, -1)] ?? object;
3
+ var object_type_default = _type;
25
4
 
26
- // src/utils.js
27
- var whitespaceCharacters = [" ", " ", "\n", "\r"];
28
- var leadingWhitespace = /^[ \t\n\r]+/u;
29
- var trailingWhitespace = /[ \t\n\r]+$/u;
30
- var httpTokenCodePoints = /^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/u;
31
- var httpQuotedTokenCodePoints = /^[\t\u0020-\u007E\u0080-\u00FF]*$/u;
32
- var removeLeadingAndTrailingHTTPWhitespace = (string) => string.replace(leadingWhitespace, "").replace(trailingWhitespace, "");
33
- var removeTrailingHTTPWhitespace = (string) => string.replace(trailingWhitespace, "");
34
- var isHTTPWhitespaceChar = (char) => whitespaceCharacters.includes(char);
35
- var solelyContainsHTTPTokenCodePoints = (string) => httpTokenCodePoints.test(string);
36
- var solelyContainsHTTPQuotedStringTokenCodePoints = (string) => httpQuotedTokenCodePoints.test(string);
37
- var asciiLowercase = (string) => {
38
- let result = "";
39
- for (const [char, charCode = char.charCodeAt(0)] of string) {
40
- result += charCode >= 65 && charCode <= 90 ? String.fromCharCode(charCode + 32) : char;
41
- }
42
- return result;
43
- };
44
- var collectAnHTTPQuotedString = (input, position) => {
45
- let value = "";
46
- for (let length = input.length, char; ++position < length; ) {
47
- char = input[position];
48
- if (char == "\\") {
49
- value += ++position < length ? input[position] : char;
50
- } else if (char == '"') {
51
- break;
52
- } else {
53
- value += char;
54
- }
55
- }
56
- return [value, position];
57
- };
5
+ // src/utils.js
6
+ var httpTokenCodePoints = /^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/u;
7
+ var utils_default = httpTokenCodePoints;
58
8
 
59
- // src/media-type-parameters.js
60
- var MediaTypeParameters = class {
61
- /** @type {Map<string, string>} */
62
- #map;
63
- /**
64
- * Create a new MediaTypeParameters instance.
65
- *
66
- * @param {Map<string, string>} map The map of parameters for a media type.
67
- */
68
- constructor(map = /* @__PURE__ */ new Map()) {
69
- this.#map = map;
70
- }
71
- /**
72
- * Gets the number of media type parameters.
73
- *
74
- * @returns {number} The number of media type parameters
75
- */
76
- get size() {
77
- return this.#map.size;
78
- }
79
- /**
80
- * Gets the media type parameter value for the supplied name.
81
- *
82
- * @param {string} name The name of the media type parameter to retrieve.
83
- * @returns {string} The media type parameter value.
84
- */
85
- get(name) {
86
- return this.#map.get(asciiLowercase(String(name)));
87
- }
88
- /**
89
- * Indicates whether the media type parameter with the specified name exists or not.
90
- *
91
- * @param {string} name The name of the media type parameter to check.
92
- * @returns {boolean} true if the media type parameter exists, false otherwise.
93
- */
94
- has(name) {
95
- return this.#map.has(asciiLowercase(String(name)));
96
- }
97
- /**
98
- * Adds a new media type parameter using the specified name and value to the MediaTypeParameters.
99
- * If an parameter with the same name already exists, the parameter will be updated.
100
- *
101
- * @param {string} name The name of the media type parameter to set.
102
- * @param {string} value The media type parameter value.
103
- * @returns {MediaTypeParameters} This instance.
104
- */
105
- set(name, value) {
106
- name = asciiLowercase(String(name));
107
- value = String(value);
108
- if (!solelyContainsHTTPTokenCodePoints(name)) {
109
- throw new Error(`Invalid media type parameter name "${name}": only HTTP token code points are valid.`);
110
- }
111
- if (!solelyContainsHTTPQuotedStringTokenCodePoints(value)) {
112
- throw new Error(`Invalid media type parameter value "${value}": only HTTP quoted-string token code points are valid.`);
113
- }
114
- this.#map.set(name, value);
115
- return this;
116
- }
117
- /**
118
- * Clears all the media type parameters.
119
- */
120
- clear() {
121
- this.#map.clear();
122
- }
123
- /**
124
- * Removes the media type parameter using the specified name.
125
- *
126
- * @param {string} name The name of the media type parameter to delete.
127
- * @returns {boolean} true if the parameter existed and has been removed, or false if the parameter does not exist.
128
- */
129
- delete(name) {
130
- name = asciiLowercase(String(name));
131
- return this.#map.delete(name);
132
- }
133
- /**
134
- * Executes a provided function once per each name/value pair in the MediaTypeParameters, in insertion order.
135
- *
136
- * @param {function(string, string): void} callback The function called on each iteration.
137
- * @param {*} [thisArg] Optional object when binding 'this' to the callback.
138
- */
139
- forEach(callback, thisArg) {
140
- this.#map.forEach(callback, thisArg);
141
- }
142
- /**
143
- * Returns an iterable of parameter names.
144
- *
145
- * @returns {IterableIterator<string>} The {@link IterableIterator} of media type parameter names.
146
- */
147
- keys() {
148
- return this.#map.keys();
149
- }
150
- /**
151
- * Returns an iterable of parameter values.
152
- *
153
- * @returns {IterableIterator<string>} The {@link IterableIterator} of media type parameter values.
154
- */
155
- values() {
156
- return this.#map.values();
157
- }
158
- /**
159
- * Returns an iterable of name, value pairs for every parameter entry in the media type parameters.
160
- *
161
- * @returns {IterableIterator<Array<Array<string>>>} The media type parameter entries.
162
- */
163
- entries() {
164
- return this.#map.entries();
165
- }
166
- /**
167
- * A method that returns the default iterator for the {@link MediaTypeParameters}. Called by the semantics of the for-of statement.
168
- *
169
- * @returns {Iterator<string, string, undefined>} The {@link Symbol.iterator} for the media type parameters.
170
- */
171
- [Symbol.iterator]() {
172
- return this.#map[Symbol.iterator]();
173
- }
174
- /**
175
- * Returns a string representation of the media type parameters.
176
- * This method is called by the `String()` function.
177
- *
178
- * @example
179
- * const parameters = new MediaTypeParameters(new Map([['charset', 'utf-8']]));
180
- * String(parameters); // 'charset=utf-8'
181
- * parameters.toString(); // 'charset=utf-8'
182
- * parameters + ''; // 'charset=utf-8'
183
- * `${parameters}`; // 'charset=utf-8'
184
- * parameters[Symbol.toStringTag]; // 'MediaTypeParameters'
185
- * parameters[Symbol.toStringTag](); // 'MediaTypeParameters'
186
- * Object.prototype.toString.call(parameters); // '[object MediaTypeParameters]'
187
- * parameters + ''; // 'charset=utf-8'
188
- * @returns {string} The string representation of the media type parameters.
189
- */
190
- [Symbol.toStringTag]() {
191
- return "MediaTypeParameters";
192
- }
193
- };
9
+ // src/media-type-parameters.js
10
+ var matcher = /(["\\])/ug;
11
+ var httpQuotedStringTokenCodePoints = /^[\t\u0020-\u007E\u0080-\u00FF]*$/u;
12
+ var MediaTypeParameters = class _MediaTypeParameters extends Map {
13
+ /**
14
+ * Create a new MediaTypeParameters instance.
15
+ *
16
+ * @param {Array<[string, string]>} entries An array of [name, value] tuples.
17
+ */
18
+ constructor(entries = []) {
19
+ super(entries);
20
+ }
21
+ /**
22
+ * Indicates whether the supplied name and value are valid media type parameters.
23
+ *
24
+ * @static
25
+ * @param {string} name The name of the media type parameter to validate.
26
+ * @param {string} value The media type parameter value to validate.
27
+ * @returns {boolean} true if the media type parameter is valid, false otherwise.
28
+ */
29
+ static isValid(name, value) {
30
+ return utils_default.test(name) && httpQuotedStringTokenCodePoints.test(value);
31
+ }
32
+ /**
33
+ * Gets the media type parameter value for the supplied name.
34
+ *
35
+ * @param {string} name The name of the media type parameter to retrieve.
36
+ * @returns {string} The media type parameter value.
37
+ */
38
+ get(name) {
39
+ return super.get(name.toLowerCase());
40
+ }
41
+ /**
42
+ * Indicates whether the media type parameter with the specified name exists or not.
43
+ *
44
+ * @param {string} name The name of the media type parameter to check.
45
+ * @returns {boolean} true if the media type parameter exists, false otherwise.
46
+ */
47
+ has(name) {
48
+ return super.has(name.toLowerCase());
49
+ }
50
+ /**
51
+ * Adds a new media type parameter using the specified name and value to the MediaTypeParameters.
52
+ * If an parameter with the same name already exists, the parameter will be updated.
53
+ *
54
+ * @param {string} name The name of the media type parameter to set.
55
+ * @param {string} value The media type parameter value.
56
+ * @returns {MediaTypeParameters} This instance.
57
+ */
58
+ set(name, value) {
59
+ if (!_MediaTypeParameters.isValid(name, value)) {
60
+ throw new Error(`Invalid media type parameter name/value: ${name}/${value}`);
61
+ }
62
+ super.set(name.toLowerCase(), value);
63
+ return this;
64
+ }
65
+ /**
66
+ * Removes the media type parameter using the specified name.
67
+ *
68
+ * @param {string} name The name of the media type parameter to delete.
69
+ * @returns {boolean} true if the parameter existed and has been removed, or false if the parameter does not exist.
70
+ */
71
+ delete(name) {
72
+ return super.delete(name.toLowerCase());
73
+ }
74
+ /**
75
+ * Returns a string representation of the media type parameters.
76
+ *
77
+ * @override
78
+ * @returns {string} The string representation of the media type parameters.
79
+ */
80
+ toString() {
81
+ return Array.from(this).map(([name, value]) => `;${name}=${!value || !utils_default.test(value) ? `"${value.replace(matcher, "\\$1")}"` : value}`).join("");
82
+ }
83
+ /**
84
+ * Returns the name of this class.
85
+ *
86
+ * @override
87
+ * @returns {string} The name of this class.
88
+ */
89
+ [Symbol.toStringTag]() {
90
+ return "MediaTypeParameters";
91
+ }
92
+ };
194
93
 
195
- // src/parser.js
196
- var parse = (input) => {
197
- input = removeLeadingAndTrailingHTTPWhitespace(input);
198
- let position = 0;
199
- let type = "";
200
- while (position < input.length && input[position] != "/") {
201
- type += input[position];
202
- ++position;
203
- }
204
- if (type.length === 0 || !solelyContainsHTTPTokenCodePoints(type)) {
205
- return null;
206
- }
207
- if (position >= input.length) {
208
- return null;
209
- }
210
- ++position;
211
- let subtype = "";
212
- while (position < input.length && input[position] != ";") {
213
- subtype += input[position];
214
- ++position;
215
- }
216
- subtype = removeTrailingHTTPWhitespace(subtype);
217
- if (subtype.length === 0 || !solelyContainsHTTPTokenCodePoints(subtype)) {
218
- return null;
219
- }
220
- const mediaType = {
221
- type: asciiLowercase(type),
222
- subtype: asciiLowercase(subtype),
223
- parameters: /* @__PURE__ */ new Map()
224
- };
225
- while (position < input.length) {
226
- ++position;
227
- while (isHTTPWhitespaceChar(input[position])) {
228
- ++position;
229
- }
230
- let parameterName = "";
231
- while (position < input.length && input[position] != ";" && input[position] != "=") {
232
- parameterName += input[position];
94
+ // src/media-type-parser.js
95
+ var whitespaceCharacters = [" ", " ", "\n", "\r"];
96
+ var trailingWhitespace = /[ \t\n\r]+$/u;
97
+ var leadingAndTrailingWhitespace = /^[ \t\n\r]+|[ \t\n\r]+$/ug;
98
+ var MediaTypeParser = class _MediaTypeParser {
99
+ /**
100
+ * Function to parse a media type.
101
+ *
102
+ * @static
103
+ * @param {string} input The media type to parse
104
+ * @returns {{ type: string, subtype: string, parameters: MediaTypeParameters }} An object populated with the parsed media type properties and any parameters.
105
+ */
106
+ static parse(input) {
107
+ input = input.replace(leadingAndTrailingWhitespace, "");
108
+ const length = input.length, trim = true, lowerCase = false;
109
+ let { position, result: type, subtype = "" } = _MediaTypeParser.#filterComponent({ input }, "/");
110
+ if (!type.length || position >= length || !utils_default.test(type)) {
111
+ throw new TypeError(_MediaTypeParser.#generateErrorMessage("type", type));
112
+ }
113
+ ({ position, result: subtype } = _MediaTypeParser.#filterComponent({ position: ++position, input, trim }, ";"));
114
+ if (!subtype.length || !utils_default.test(subtype)) {
115
+ throw new TypeError(_MediaTypeParser.#generateErrorMessage("subtype", subtype));
116
+ }
117
+ let parameterName = "", parameterValue = null;
118
+ const parameters = new MediaTypeParameters();
119
+ while (position++ < length) {
120
+ while (whitespaceCharacters.includes(input[position])) {
233
121
  ++position;
234
122
  }
235
- parameterName = asciiLowercase(parameterName);
236
- if (position < input.length) {
123
+ ({ position, result: parameterName } = _MediaTypeParser.#filterComponent({ position, input, lowerCase }, ";", "="));
124
+ if (position < length) {
237
125
  if (input[position] == ";") {
238
126
  continue;
239
127
  }
240
128
  ++position;
241
129
  }
242
- let parameterValue = null;
243
130
  if (input[position] == '"') {
244
- [parameterValue, position] = collectAnHTTPQuotedString(input, position);
245
- while (position < input.length && input[position] != ";") {
131
+ [parameterValue, position] = _MediaTypeParser.#collectHttpQuotedString(input, position);
132
+ while (position < length && input[position] != ";") {
246
133
  ++position;
247
134
  }
248
135
  } else {
249
- parameterValue = "";
250
- while (position < input.length && input[position] != ";") {
251
- parameterValue += input[position];
252
- ++position;
253
- }
254
- parameterValue = removeTrailingHTTPWhitespace(parameterValue);
255
- if (parameterValue === "") {
136
+ ({ position, result: parameterValue } = _MediaTypeParser.#filterComponent({ position, input, lowerCase, trim }, ";"));
137
+ if (!parameterValue) {
256
138
  continue;
257
139
  }
258
140
  }
259
- if (parameterName.length > 0 && solelyContainsHTTPTokenCodePoints(parameterName) && solelyContainsHTTPQuotedStringTokenCodePoints(parameterValue) && !mediaType.parameters.has(parameterName)) {
260
- mediaType.parameters.set(parameterName, parameterValue);
141
+ if (parameterName && MediaTypeParameters.isValid(parameterName, parameterValue) && !parameters.has(parameterName)) {
142
+ parameters.set(parameterName, parameterValue);
261
143
  }
262
144
  }
263
- return mediaType;
264
- };
265
- var parser_default = parse;
266
-
267
- // src/serializer.js
268
- var serialize = (mediaType) => {
269
- let serialization = `${mediaType.type}/${mediaType.subtype}`;
270
- if (mediaType.parameters.size === 0) {
271
- return serialization;
272
- }
273
- for (let [name, value] of mediaType.parameters) {
274
- serialization += `;${name}=`;
275
- if (!solelyContainsHTTPTokenCodePoints(value) || value.length === 0) {
276
- value = `"${value.replace(/(["\\])/ug, "\\$1")}"`;
145
+ return { type, subtype, parameters };
146
+ }
147
+ /**
148
+ * Filters a component from the input string.
149
+ *
150
+ * @private
151
+ * @static
152
+ * @param {Object} options The options.
153
+ * @param {number} [options.position] The starting position.
154
+ * @param {string} options.input The input string.
155
+ * @param {boolean} [options.lowerCase] Indicates whether the result should be lowercased.
156
+ * @param {boolean} [options.trim] Indicates whether the result should be trimmed.
157
+ * @param {string[]} charactersToFilter The characters to filter.
158
+ * @returns {{ position: number, result: string }} An object that includes the resulting string and updated position.
159
+ */
160
+ static #filterComponent({ position = 0, input, lowerCase = true, trim = false }, ...charactersToFilter) {
161
+ let result = "";
162
+ for (const length = input.length; position < length && !charactersToFilter.includes(input[position]); position++) {
163
+ result += input[position];
164
+ }
165
+ if (lowerCase) {
166
+ result = result.toLowerCase();
167
+ }
168
+ if (trim) {
169
+ result = result.replace(trailingWhitespace, "");
170
+ }
171
+ return { position, result };
172
+ }
173
+ /**
174
+ * Collects all the HTTP quoted strings.
175
+ * This variant only implements it with the extract-value flag set.
176
+ *
177
+ * @private
178
+ * @static
179
+ * @param {string} input The string to process.
180
+ * @param {number} position The starting position.
181
+ * @returns {Array<string|number>} An array that includes the resulting string and updated position.
182
+ */
183
+ static #collectHttpQuotedString(input, position) {
184
+ let value = "";
185
+ for (let length = input.length, char; ++position < length; ) {
186
+ if ((char = input[position]) == '"') {
187
+ break;
277
188
  }
278
- serialization += value;
189
+ value += char == "\\" && ++position < length ? input[position] : char;
279
190
  }
280
- return serialization;
281
- };
282
- var serializer_default = serialize;
191
+ return [value, position];
192
+ }
193
+ /**
194
+ * Generates an error message.
195
+ *
196
+ * @private
197
+ * @static
198
+ * @param {string} component The component name.
199
+ * @param {string} value The component value.
200
+ * @returns {string} The error message.
201
+ */
202
+ static #generateErrorMessage(component, value) {
203
+ return `Invalid ${component} "${value}": only HTTP token code points are valid.`;
204
+ }
205
+ };
283
206
 
284
- // src/media-type.js
285
- var MediaType = class {
286
- /** @type {string} */
287
- #type;
288
- /** @type {string} */
289
- #subtype;
290
- /** @type {MediaTypeParameters} */
291
- #parameters;
292
- /**
293
- * Create a new MediaType instance from a string representation.
294
- *
295
- * @param {string|object} options The media type string or an object with type, subtype, and parameters.
296
- */
297
- constructor(options) {
298
- if (typeof options === "string") {
299
- options = parser_default(options);
300
- }
301
- const { type, subtype, parameters = new MediaTypeParameters() } = options;
302
- this.#type = type;
303
- this.#subtype = subtype;
304
- this.#parameters = typeof parameters == Map && parameters.size > 0 ? new MediaTypeParameters(parameters.entries()) : parameters;
305
- }
306
- /**
307
- * Static factory method for parsing a media type.
308
- *
309
- * @param {string} string The media type to parse.
310
- * @returns {MediaType} The parsed {@link MediaType} object or null if the string could not be parsed.
311
- */
312
- static parse(string) {
313
- try {
314
- return new MediaType(parser_default(string));
315
- } catch (e) {
316
- throw new Error(`Could not parse media type string '${string}'`);
317
- }
318
- }
319
- /**
320
- * Gets the media type essence (type/subtype).
321
- *
322
- * @returns {string} The media type without any parameters
323
- */
324
- get essence() {
325
- return `${this.#type}/${this.#subtype}`;
326
- }
327
- /**
328
- * Gets the type.
329
- *
330
- * @returns {string} The type.
331
- */
332
- get type() {
333
- return this.#type;
334
- }
335
- /**
336
- * Sets the type.
337
- */
338
- set type(value) {
339
- value = asciiLowercase(String(value));
340
- if (value.length === 0) {
341
- throw new Error("Invalid type: must be a non-empty string");
342
- }
343
- if (!solelyContainsHTTPTokenCodePoints(value)) {
344
- throw new Error(`Invalid type ${value}: must contain only HTTP token code points`);
345
- }
346
- this.#type = value;
347
- }
348
- /**
349
- * Gets the subtype.
350
- *
351
- * @returns {string} The subtype.
352
- */
353
- get subtype() {
354
- return this.#subtype;
355
- }
356
- /**
357
- * Sets the subtype.
358
- */
359
- set subtype(value) {
360
- value = asciiLowercase(String(value));
361
- if (value.length === 0) {
362
- throw new Error("Invalid subtype: must be a non-empty string");
363
- }
364
- if (!solelyContainsHTTPTokenCodePoints(value)) {
365
- throw new Error(`Invalid subtype ${value}: must contain only HTTP token code points`);
366
- }
367
- this.#subtype = value;
368
- }
369
- /**
370
- * Gets the parameters.
371
- *
372
- * @returns {MediaTypeParameters} The media type parameters.
373
- */
374
- get parameters() {
375
- return this.#parameters;
376
- }
377
- /**
378
- * Gets the serialized version of the media type.
379
- *
380
- * @returns {string} The serialized media type.
381
- */
382
- toString() {
383
- return serializer_default(this);
384
- }
385
- /**
386
- * Determines if this instance is a JavaScript media type.
387
- *
388
- * @param {Object} [options] Optional options.
389
- * @param {boolean} [options.prohibitParameters=false] The option to prohibit parameters when checking if the media type is JavaScript.
390
- * @returns {boolean} true if this instance represents a JavaScript media type, false otherwise.
391
- */
392
- isJavaScript({ prohibitParameters = false } = {}) {
393
- switch (this.#type) {
394
- case "text": {
395
- switch (this.#subtype) {
396
- case "ecmascript":
397
- case "javascript":
398
- case "javascript1.0":
399
- case "javascript1.1":
400
- case "javascript1.2":
401
- case "javascript1.3":
402
- case "javascript1.4":
403
- case "javascript1.5":
404
- case "jscript":
405
- case "livescript":
406
- case "x-ecmascript":
407
- case "x-javascript":
408
- return !prohibitParameters || this.#parameters.size === 0;
409
- default:
410
- return false;
411
- }
412
- }
413
- case "application": {
414
- switch (this.#subtype) {
415
- case "ecmascript":
416
- case "javascript":
417
- case "x-ecmascript":
418
- case "x-javascript":
419
- return !prohibitParameters || this.#parameters.size === 0;
420
- default:
421
- return false;
422
- }
423
- }
424
- default:
425
- return false;
426
- }
427
- }
428
- /**
429
- * Determines if this instance is an XML media type.
430
- *
431
- * @returns {boolean} true if this instance represents an XML media type, false otherwise.
432
- */
433
- isXML() {
434
- return this.#subtype === "xml" && (this.#type === "text" || this.#type === "application") || this.#subtype.endsWith("+xml");
435
- }
436
- /**
437
- * Determines if this instance is an HTML media type.
438
- *
439
- * @returns {boolean} true if this instance represents an HTML media type, false otherwise.
440
- */
441
- isHTML() {
442
- return this.#subtype === "html" && this.#type === "text";
443
- }
444
- /**
445
- * Gets the name of the class.
446
- *
447
- * @returns {string} The class name
448
- */
449
- get [Symbol.toStringTag]() {
450
- return "MediaType";
451
- }
452
- };
453
- return __toCommonJS(media_type_exports);
454
- })();
455
- window.MediaType = MediaType.default;
207
+ // src/media-type.js
208
+ var MediaType = class _MediaType {
209
+ /** @type {string} */
210
+ #type;
211
+ /** @type {string} */
212
+ #subtype;
213
+ /** @type {MediaTypeParameters} */
214
+ #parameters;
215
+ /**
216
+ * Create a new MediaType instance from a string representation.
217
+ *
218
+ * @param {string} mediaType The media type to parse.
219
+ * @param {Object} [parameters] Optional parameters.
220
+ */
221
+ constructor(mediaType, parameters = {}) {
222
+ if (object_type_default(parameters) != Object) {
223
+ throw new TypeError("The parameters argument must be an object");
224
+ }
225
+ ({ type: this.#type, subtype: this.#subtype, parameters: this.#parameters } = MediaTypeParser.parse(mediaType));
226
+ for (const [name, value] of Object.entries(parameters)) {
227
+ this.#parameters.set(name, value);
228
+ }
229
+ }
230
+ static parse(mediaType) {
231
+ try {
232
+ return new _MediaType(mediaType);
233
+ } catch (e) {
234
+ }
235
+ return null;
236
+ }
237
+ /**
238
+ * Gets the type.
239
+ *
240
+ * @returns {string} The type.
241
+ */
242
+ get type() {
243
+ return this.#type;
244
+ }
245
+ /**
246
+ * Gets the subtype.
247
+ *
248
+ * @returns {string} The subtype.
249
+ */
250
+ get subtype() {
251
+ return this.#subtype;
252
+ }
253
+ /**
254
+ * Gets the media type essence (type/subtype).
255
+ *
256
+ * @returns {string} The media type without any parameters
257
+ */
258
+ get essence() {
259
+ return `${this.#type}/${this.#subtype}`;
260
+ }
261
+ /**
262
+ * Gets the parameters.
263
+ *
264
+ * @returns {MediaTypeParameters} The media type parameters.
265
+ */
266
+ get parameters() {
267
+ return this.#parameters;
268
+ }
269
+ /**
270
+ * Checks if the media type matches the specified type.
271
+ *
272
+ * @todo Fix string handling to parse the type and subtype from the string.
273
+ * @param {MediaType|string} type The media type to check.
274
+ * @returns {boolean} true if the media type matches the specified type, false otherwise.
275
+ */
276
+ matches(type) {
277
+ return this.#type == type?.type && this.#subtype == type?.subtype || this.essence.includes(type);
278
+ }
279
+ /**
280
+ * Gets the serialized version of the media type.
281
+ *
282
+ * @returns {string} The serialized media type.
283
+ */
284
+ toString() {
285
+ return `${this.essence}${this.#parameters.toString()}`;
286
+ }
287
+ /**
288
+ * Gets the name of the class.
289
+ *
290
+ * @returns {string} The class name
291
+ */
292
+ get [Symbol.toStringTag]() {
293
+ return "MediaType";
294
+ }
295
+ };
296
+ export {
297
+ MediaType as default
298
+ };