1
1
import { parse as parseContentType } from "content-type" ;
2
- import { match as mediaTypeMatch } from "type-is" ;
3
2
4
3
5
4
const mediaTypePlugins = { } ;
@@ -24,7 +23,7 @@ export const parseResponse = (response) => {
24
23
25
24
const contentType = parseContentType ( contentTypeText ) ;
26
25
for ( const pattern in mediaTypePlugins ) {
27
- if ( mediaTypeMatch ( pattern , contentType . type ) ) {
26
+ if ( mimeMatch ( pattern , contentType . type ) ) {
28
27
return mediaTypePlugins [ pattern ] . parse ( response ) ;
29
28
}
30
29
}
@@ -34,6 +33,39 @@ export const parseResponse = (response) => {
34
33
} ) ;
35
34
} ;
36
35
36
+ const alpha = `A-Za-z` ;
37
+ const token = `[!#$%&'*\\-_.^\`|~\\d${ alpha } ]+` ;
38
+ const mediaRange = `(?<type>${ token } )/(?<subType>${ token } (?:\\+(?<suffix>${ token } ))?)` ;
39
+ const mediaRangePattern = new RegExp ( mediaRange ) ;
40
+
41
+ export const mimeMatch = ( expected , actual ) => {
42
+ if ( expected === actual ) {
43
+ return true ;
44
+ }
45
+
46
+ const expectedMatches = mediaRangePattern . exec ( expected ) ?. groups ;
47
+ if ( ! expectedMatches ) {
48
+ throw Error ( `Unable to parse media-range: ${ expected } ` ) ;
49
+ }
50
+
51
+ const actualMatches = mediaRangePattern . exec ( actual ) ?. groups ;
52
+ if ( ! actualMatches ) {
53
+ throw Error ( `Unable to parse media-type: ${ actual } ` ) ;
54
+ }
55
+
56
+ if ( expectedMatches . type === actualMatches . type || expectedMatches . type === "*" ) {
57
+ if ( expectedMatches . subType === actualMatches . subType || expectedMatches . subType === "*" ) {
58
+ return true ;
59
+ }
60
+
61
+ if ( expectedMatches . subType === actualMatches . suffix ) {
62
+ return true ;
63
+ }
64
+ }
65
+
66
+ return false ;
67
+ } ;
68
+
37
69
export const getFileMediaType = async ( path ) => {
38
70
for ( const contentType in mediaTypePlugins ) {
39
71
if ( await mediaTypePlugins [ contentType ] . fileMatcher ( path ) ) {
0 commit comments