@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/LICENSE.txt
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
Copyright
|
|
1
|
+
Copyright (c) 2023, Jason DiMeo
|
|
2
2
|
|
|
3
|
-
Permission
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided
|
|
4
|
+
that the above copyright notice and this permission notice appear in all copies.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
|
|
7
|
+
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
|
|
8
|
+
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
9
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
10
|
+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
Source: http://opensource.org/licenses/ISC
|
package/README.md
CHANGED
|
@@ -5,23 +5,23 @@ This package will parse [MIME types](https://mimesniff.spec.whatwg.org/#understa
|
|
|
5
5
|
This version is using ES Modules instead of commonJS.
|
|
6
6
|
|
|
7
7
|
```js
|
|
8
|
-
import
|
|
8
|
+
import MediaType from '@d1g1tal/media-type';
|
|
9
9
|
|
|
10
|
-
const mediaType = new MediaType(
|
|
10
|
+
const mediaType = new MediaType('Text/HTML;Charset="utf-8"');
|
|
11
11
|
|
|
12
|
-
console.assert(mediaType.toString()
|
|
12
|
+
console.assert(mediaType.toString() == 'text/html;charset=utf-8');
|
|
13
13
|
|
|
14
|
-
console.assert(mediaType.type
|
|
15
|
-
console.assert(mediaType.subtype
|
|
16
|
-
console.assert(mediaType.essence
|
|
17
|
-
console.assert(mediaType.parameters.get(
|
|
14
|
+
console.assert(mediaType.type == 'text');
|
|
15
|
+
console.assert(mediaType.subtype == 'html');
|
|
16
|
+
console.assert(mediaType.essence == 'text/html');
|
|
17
|
+
console.assert(mediaType.parameters.get('charset') == 'utf-8');
|
|
18
18
|
|
|
19
|
-
mediaType.parameters.set(
|
|
20
|
-
console.assert(mediaType.parameters.get(
|
|
21
|
-
console.assert(mediaType.toString()
|
|
22
|
-
|
|
23
|
-
console.assert(mediaType.
|
|
24
|
-
console.assert(mediaType.
|
|
19
|
+
mediaType.parameters.set('charset', 'windows-1252');
|
|
20
|
+
console.assert(mediaType.parameters.get('charset') == 'windows-1252');
|
|
21
|
+
console.assert(mediaType.toString() == 'text/html;charset=windows-1252');
|
|
22
|
+
// Still considering changing this to maybe 'includes' or 'contains'
|
|
23
|
+
console.assert(mediaType.matches('html') == true);
|
|
24
|
+
console.assert(mediaType.matches('xml') == false);
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
Parsing is a fairly complex process; see [the specification](https://mimesniff.spec.whatwg.org/#parsing-a-mime-type) for details (and similarly [for serialization](https://mimesniff.spec.whatwg.org/#serializing-a-mime-type)).
|
|
@@ -38,9 +38,9 @@ As an alternative to the constructor, you can use `MediaType.parse(string)`. The
|
|
|
38
38
|
|
|
39
39
|
### Properties
|
|
40
40
|
|
|
41
|
-
- `type`: the media type's [type](https://mimesniff.spec.whatwg.org/#mime-type-type), e.g. `
|
|
42
|
-
- `subtype`: the media type's [subtype](https://mimesniff.spec.whatwg.org/#mime-type-subtype), e.g. `
|
|
43
|
-
- `essence`: the media type's [essence](https://mimesniff.spec.whatwg.org/#mime-type-essence), e.g. `
|
|
41
|
+
- `type`: the media type's [type](https://mimesniff.spec.whatwg.org/#mime-type-type), e.g. `'text'`
|
|
42
|
+
- `subtype`: the media type's [subtype](https://mimesniff.spec.whatwg.org/#mime-type-subtype), e.g. `'html'`
|
|
43
|
+
- `essence`: the media type's [essence](https://mimesniff.spec.whatwg.org/#mime-type-essence), e.g. `'text/html'`
|
|
44
44
|
- `parameters`: an instance of `MediaTypeParameters`, containing this media type's [parameters](https://mimesniff.spec.whatwg.org/#mime-type-parameters)
|
|
45
45
|
|
|
46
46
|
`type` and `subtype` can be changed. They will be validated to be non-empty and only contain [HTTP token code points](https://mimesniff.spec.whatwg.org/#http-token-code-point).
|
|
@@ -52,52 +52,35 @@ As an alternative to the constructor, you can use `MediaType.parse(string)`. The
|
|
|
52
52
|
### Methods
|
|
53
53
|
|
|
54
54
|
- `toString()` serializes the media type to a string
|
|
55
|
-
- `
|
|
56
|
-
- `isXML()`: returns true if this instance represents [an XML media type](https://mimesniff.spec.whatwg.org/#xml-mime-type)
|
|
57
|
-
- `isJavaScript({ prohibitParameters })`: returns true if this instance represents [a JavaScript media type](https://html.spec.whatwg.org/multipage/scripting.html#javascript-mime-type). `prohibitParameters` can be set to true to disallow any parameters, i.e. to test if the media type's serialization is a [JavaScript media type essence match](https://mimesniff.spec.whatwg.org/#javascript-mime-type-essence-match).
|
|
58
|
-
|
|
59
|
-
_Note: the `isHTML()`, `isXML()`, and `isJavaScript()` methods are speculative, and may be removed or changed in future major versions. See [whatwg/mimesniff#48](https://github.com/whatwg/mimesniff/issues/48) for brainstorming in this area. Currently we implement these mainly because they are useful in jsdom._
|
|
55
|
+
- `matches(MediaType|string)`: Checks if the media type matches the specified type.
|
|
60
56
|
|
|
61
57
|
## `MediaTypeParameters` API
|
|
62
58
|
|
|
63
|
-
The `MediaTypeParameters` class, instances of which are returned by `MediaType.parameters`,
|
|
59
|
+
The `MediaTypeParameters` class, instances of which are returned by `MediaType.parameters`, extends a [JavaScript `Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) with both the keys and values being strings. The keys are parameter names, and the values are parameter values.
|
|
64
60
|
|
|
65
61
|
However, `MediaTypeParameters` methods will always interpret their arguments as appropriate for media types, so e.g. parameter names will be lowercased, and attempting to set invalid characters will throw.
|
|
66
62
|
|
|
67
63
|
Some examples:
|
|
68
64
|
|
|
69
65
|
```js
|
|
70
|
-
const mediaType = new MediaType(`x/x;a=b;c=D;E=
|
|
66
|
+
const mediaType = new MediaType(`x/x;a=b;c=D;E='F'`);
|
|
71
67
|
|
|
72
68
|
// Logs:
|
|
73
69
|
// a b
|
|
74
70
|
// c D
|
|
75
71
|
// e F
|
|
76
|
-
for (const [name, value] of mediaType.parameters) {
|
|
72
|
+
for (const [ name, value ] of mediaType.parameters) {
|
|
77
73
|
console.log(name, value);
|
|
78
74
|
}
|
|
79
75
|
|
|
80
|
-
console.assert(mediaType.parameters.has(
|
|
81
|
-
console.assert(mediaType.parameters.has(
|
|
82
|
-
console.assert(mediaType.parameters.get(
|
|
76
|
+
console.assert(mediaType.parameters.has('a'));
|
|
77
|
+
console.assert(mediaType.parameters.has('A'));
|
|
78
|
+
console.assert(mediaType.parameters.get('A') === 'b');
|
|
83
79
|
|
|
84
|
-
mediaType.parameters.set(
|
|
85
|
-
console.assert(mediaType.parameters.get(
|
|
86
|
-
console.assert(mediaType.toString() ===
|
|
80
|
+
mediaType.parameters.set('Q', 'X');
|
|
81
|
+
console.assert(mediaType.parameters.get('q') === 'X');
|
|
82
|
+
console.assert(mediaType.toString() === 'x/x;a=b;c=d;e=F;q=X');
|
|
87
83
|
|
|
88
84
|
// Throws:
|
|
89
|
-
mediaType.parameters.set(
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Raw parsing/serialization APIs
|
|
93
|
-
|
|
94
|
-
If you want primitives on which to build your own API, you can get direct access to the parsing and serialization algorithms as follows:
|
|
95
|
-
|
|
96
|
-
```js
|
|
97
|
-
import { parse } from '@d1g1tal/media-type';
|
|
98
|
-
import { serialize } from '@d1g1tal/media-type';
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
`parse(string)` returns an object containing the `type` and `subtype` strings, plus `parameters`, which is a `Map`. This is roughly our equivalent of the spec's [MIME type record](https://mimesniff.spec.whatwg.org/#mime-type). If parsing fails, it instead returns `null`.
|
|
102
|
-
|
|
103
|
-
`serialize(record)` operates on the such an object, giving back a string according to the serialization algorithm.
|
|
85
|
+
mediaType.parameters.set('@', 'x');
|
|
86
|
+
```
|