@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.
- package/LICENSE.txt +9 -4
- package/README.md +28 -45
- package/dist/media-type.js +276 -433
- package/dist/media-type.min.js +2 -3
- package/dist/media-type.min.js.map +4 -4
- package/package.json +80 -85
- package/src/media-type-parameters.js +31 -89
- package/src/media-type-parser.js +120 -0
- package/src/media-type.js +26 -122
- package/src/utils.js +1 -92
- package/index.d.ts +0 -5
- package/index.js +0 -5
- package/src/media-type-parameters.d.ts +0 -91
- package/src/media-type.d.ts +0 -91
- package/src/parser.d.ts +0 -13
- package/src/parser.js +0 -106
- package/src/serializer.d.ts +0 -11
- package/src/serializer.js +0 -32
- package/src/utils.d.ts +0 -52
package/dist/media-type.js
CHANGED
|
@@ -1,455 +1,298 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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 =
|
|
236
|
-
if (position <
|
|
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] =
|
|
245
|
-
while (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
|
-
|
|
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
|
|
260
|
-
|
|
141
|
+
if (parameterName && MediaTypeParameters.isValid(parameterName, parameterValue) && !parameters.has(parameterName)) {
|
|
142
|
+
parameters.set(parameterName, parameterValue);
|
|
261
143
|
}
|
|
262
144
|
}
|
|
263
|
-
return
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
189
|
+
value += char == "\\" && ++position < length ? input[position] : char;
|
|
279
190
|
}
|
|
280
|
-
return
|
|
281
|
-
}
|
|
282
|
-
|
|
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
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
this.#parameters
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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
|
+
};
|